channel.c 116 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194
  1. /* * Copyright (c) 2012-2013, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file channel.c
  5. * \brief OR-to-OR channel abstraction layer
  6. **/
  7. /*
  8. * Define this so channel.h gives us things only channel_t subclasses
  9. * should touch.
  10. */
  11. #define TOR_CHANNEL_INTERNAL_
  12. #include "or.h"
  13. #include "channel.h"
  14. #include "channeltls.h"
  15. #include "circuitbuild.h"
  16. #include "circuitlist.h"
  17. #include "circuitstats.h"
  18. #include "config.h"
  19. #include "connection_or.h" /* For var_cell_free() */
  20. #include "circuitmux.h"
  21. #include "entrynodes.h"
  22. #include "geoip.h"
  23. #include "nodelist.h"
  24. #include "relay.h"
  25. #include "rephist.h"
  26. #include "router.h"
  27. #include "routerlist.h"
  28. /* Cell queue structure */
  29. typedef struct cell_queue_entry_s cell_queue_entry_t;
  30. struct cell_queue_entry_s {
  31. TOR_SIMPLEQ_ENTRY(cell_queue_entry_s) next;
  32. enum {
  33. CELL_QUEUE_FIXED,
  34. CELL_QUEUE_VAR,
  35. CELL_QUEUE_PACKED
  36. } type;
  37. union {
  38. struct {
  39. cell_t *cell;
  40. } fixed;
  41. struct {
  42. var_cell_t *var_cell;
  43. } var;
  44. struct {
  45. packed_cell_t *packed_cell;
  46. } packed;
  47. } u;
  48. };
  49. /* Global lists of channels */
  50. /* All channel_t instances */
  51. static smartlist_t *all_channels = NULL;
  52. /* All channel_t instances not in ERROR or CLOSED states */
  53. static smartlist_t *active_channels = NULL;
  54. /* All channel_t instances in ERROR or CLOSED states */
  55. static smartlist_t *finished_channels = NULL;
  56. /* All channel_listener_t instances */
  57. static smartlist_t *all_listeners = NULL;
  58. /* All channel_listener_t instances in LISTENING state */
  59. static smartlist_t *active_listeners = NULL;
  60. /* All channel_listener_t instances in LISTENING state */
  61. static smartlist_t *finished_listeners = NULL;
  62. /* Counter for ID numbers */
  63. static uint64_t n_channels_allocated = 0;
  64. /* Digest->channel map
  65. *
  66. * Similar to the one used in connection_or.c, this maps from the identity
  67. * digest of a remote endpoint to a channel_t to that endpoint. Channels
  68. * should be placed here when registered and removed when they close or error.
  69. * If more than one channel exists, follow the next_with_same_id pointer
  70. * as a linked list.
  71. */
  72. HT_HEAD(channel_idmap, channel_idmap_entry_s) channel_identity_map =
  73. HT_INITIALIZER();
  74. typedef struct channel_idmap_entry_s {
  75. HT_ENTRY(channel_idmap_entry_s) node;
  76. uint8_t digest[DIGEST_LEN];
  77. TOR_LIST_HEAD(channel_list_s, channel_s) channel_list;
  78. } channel_idmap_entry_t;
  79. static INLINE unsigned
  80. channel_idmap_hash(const channel_idmap_entry_t *ent)
  81. {
  82. return (unsigned) siphash24g(ent->digest, DIGEST_LEN);
  83. }
  84. static INLINE int
  85. channel_idmap_eq(const channel_idmap_entry_t *a,
  86. const channel_idmap_entry_t *b)
  87. {
  88. return tor_memeq(a->digest, b->digest, DIGEST_LEN);
  89. }
  90. HT_PROTOTYPE(channel_idmap, channel_idmap_entry_s, node, channel_idmap_hash,
  91. channel_idmap_eq);
  92. HT_GENERATE(channel_idmap, channel_idmap_entry_s, node, channel_idmap_hash,
  93. channel_idmap_eq, 0.5, tor_malloc, tor_realloc, tor_free_);
  94. static cell_queue_entry_t * cell_queue_entry_dup(cell_queue_entry_t *q);
  95. static void cell_queue_entry_free(cell_queue_entry_t *q, int handed_off);
  96. #if 0
  97. static int cell_queue_entry_is_padding(cell_queue_entry_t *q);
  98. #endif
  99. static cell_queue_entry_t *
  100. cell_queue_entry_new_fixed(cell_t *cell);
  101. static cell_queue_entry_t *
  102. cell_queue_entry_new_var(var_cell_t *var_cell);
  103. static int is_destroy_cell(channel_t *chan,
  104. const cell_queue_entry_t *q, circid_t *circid_out);
  105. /* Functions to maintain the digest map */
  106. static void channel_add_to_digest_map(channel_t *chan);
  107. static void channel_remove_from_digest_map(channel_t *chan);
  108. /*
  109. * Flush cells from just the outgoing queue without trying to get them
  110. * from circuits; used internall by channel_flush_some_cells().
  111. */
  112. static ssize_t
  113. channel_flush_some_cells_from_outgoing_queue(channel_t *chan,
  114. ssize_t num_cells);
  115. static void channel_force_free(channel_t *chan);
  116. static void
  117. channel_free_list(smartlist_t *channels, int mark_for_close);
  118. static void
  119. channel_listener_free_list(smartlist_t *channels, int mark_for_close);
  120. static void channel_listener_force_free(channel_listener_t *chan_l);
  121. static void
  122. channel_write_cell_queue_entry(channel_t *chan, cell_queue_entry_t *q);
  123. /***********************************
  124. * Channel state utility functions *
  125. **********************************/
  126. /**
  127. * Indicate whether a given channel state is valid
  128. */
  129. int
  130. channel_state_is_valid(channel_state_t state)
  131. {
  132. int is_valid;
  133. switch (state) {
  134. case CHANNEL_STATE_CLOSED:
  135. case CHANNEL_STATE_CLOSING:
  136. case CHANNEL_STATE_ERROR:
  137. case CHANNEL_STATE_MAINT:
  138. case CHANNEL_STATE_OPENING:
  139. case CHANNEL_STATE_OPEN:
  140. is_valid = 1;
  141. break;
  142. case CHANNEL_STATE_LAST:
  143. default:
  144. is_valid = 0;
  145. }
  146. return is_valid;
  147. }
  148. /**
  149. * Indicate whether a given channel listener state is valid
  150. */
  151. int
  152. channel_listener_state_is_valid(channel_listener_state_t state)
  153. {
  154. int is_valid;
  155. switch (state) {
  156. case CHANNEL_LISTENER_STATE_CLOSED:
  157. case CHANNEL_LISTENER_STATE_LISTENING:
  158. case CHANNEL_LISTENER_STATE_CLOSING:
  159. case CHANNEL_LISTENER_STATE_ERROR:
  160. is_valid = 1;
  161. break;
  162. case CHANNEL_LISTENER_STATE_LAST:
  163. default:
  164. is_valid = 0;
  165. }
  166. return is_valid;
  167. }
  168. /**
  169. * Indicate whether a channel state transition is valid
  170. *
  171. * This function takes two channel states and indicates whether a
  172. * transition between them is permitted (see the state definitions and
  173. * transition table in or.h at the channel_state_t typedef).
  174. */
  175. int
  176. channel_state_can_transition(channel_state_t from, channel_state_t to)
  177. {
  178. int is_valid;
  179. switch (from) {
  180. case CHANNEL_STATE_CLOSED:
  181. is_valid = (to == CHANNEL_STATE_OPENING);
  182. break;
  183. case CHANNEL_STATE_CLOSING:
  184. is_valid = (to == CHANNEL_STATE_CLOSED ||
  185. to == CHANNEL_STATE_ERROR);
  186. break;
  187. case CHANNEL_STATE_ERROR:
  188. is_valid = 0;
  189. break;
  190. case CHANNEL_STATE_MAINT:
  191. is_valid = (to == CHANNEL_STATE_CLOSING ||
  192. to == CHANNEL_STATE_ERROR ||
  193. to == CHANNEL_STATE_OPEN);
  194. break;
  195. case CHANNEL_STATE_OPENING:
  196. is_valid = (to == CHANNEL_STATE_CLOSING ||
  197. to == CHANNEL_STATE_ERROR ||
  198. to == CHANNEL_STATE_OPEN);
  199. break;
  200. case CHANNEL_STATE_OPEN:
  201. is_valid = (to == CHANNEL_STATE_CLOSING ||
  202. to == CHANNEL_STATE_ERROR ||
  203. to == CHANNEL_STATE_MAINT);
  204. break;
  205. case CHANNEL_STATE_LAST:
  206. default:
  207. is_valid = 0;
  208. }
  209. return is_valid;
  210. }
  211. /**
  212. * Indicate whether a channel listener state transition is valid
  213. *
  214. * This function takes two channel listener states and indicates whether a
  215. * transition between them is permitted (see the state definitions and
  216. * transition table in or.h at the channel_listener_state_t typedef).
  217. */
  218. int
  219. channel_listener_state_can_transition(channel_listener_state_t from,
  220. channel_listener_state_t to)
  221. {
  222. int is_valid;
  223. switch (from) {
  224. case CHANNEL_LISTENER_STATE_CLOSED:
  225. is_valid = (to == CHANNEL_LISTENER_STATE_LISTENING);
  226. break;
  227. case CHANNEL_LISTENER_STATE_CLOSING:
  228. is_valid = (to == CHANNEL_LISTENER_STATE_CLOSED ||
  229. to == CHANNEL_LISTENER_STATE_ERROR);
  230. break;
  231. case CHANNEL_LISTENER_STATE_ERROR:
  232. is_valid = 0;
  233. break;
  234. case CHANNEL_LISTENER_STATE_LISTENING:
  235. is_valid = (to == CHANNEL_LISTENER_STATE_CLOSING ||
  236. to == CHANNEL_LISTENER_STATE_ERROR);
  237. break;
  238. case CHANNEL_LISTENER_STATE_LAST:
  239. default:
  240. is_valid = 0;
  241. }
  242. return is_valid;
  243. }
  244. /**
  245. * Return a human-readable description for a channel state
  246. */
  247. const char *
  248. channel_state_to_string(channel_state_t state)
  249. {
  250. const char *descr;
  251. switch (state) {
  252. case CHANNEL_STATE_CLOSED:
  253. descr = "closed";
  254. break;
  255. case CHANNEL_STATE_CLOSING:
  256. descr = "closing";
  257. break;
  258. case CHANNEL_STATE_ERROR:
  259. descr = "channel error";
  260. break;
  261. case CHANNEL_STATE_MAINT:
  262. descr = "temporarily suspended for maintenance";
  263. break;
  264. case CHANNEL_STATE_OPENING:
  265. descr = "opening";
  266. break;
  267. case CHANNEL_STATE_OPEN:
  268. descr = "open";
  269. break;
  270. case CHANNEL_STATE_LAST:
  271. default:
  272. descr = "unknown or invalid channel state";
  273. }
  274. return descr;
  275. }
  276. /**
  277. * Return a human-readable description for a channel listenier state
  278. */
  279. const char *
  280. channel_listener_state_to_string(channel_listener_state_t state)
  281. {
  282. const char *descr;
  283. switch (state) {
  284. case CHANNEL_LISTENER_STATE_CLOSED:
  285. descr = "closed";
  286. break;
  287. case CHANNEL_LISTENER_STATE_CLOSING:
  288. descr = "closing";
  289. break;
  290. case CHANNEL_LISTENER_STATE_ERROR:
  291. descr = "channel listener error";
  292. break;
  293. case CHANNEL_LISTENER_STATE_LISTENING:
  294. descr = "listening";
  295. break;
  296. case CHANNEL_LISTENER_STATE_LAST:
  297. default:
  298. descr = "unknown or invalid channel listener state";
  299. }
  300. return descr;
  301. }
  302. /***************************************
  303. * Channel registration/unregistration *
  304. ***************************************/
  305. /**
  306. * Register a channel
  307. *
  308. * This function registers a newly created channel in the global lists/maps
  309. * of active channels.
  310. */
  311. void
  312. channel_register(channel_t *chan)
  313. {
  314. tor_assert(chan);
  315. /* No-op if already registered */
  316. if (chan->registered) return;
  317. log_debug(LD_CHANNEL,
  318. "Registering channel %p (ID " U64_FORMAT ") "
  319. "in state %s (%d) with digest %s",
  320. chan, U64_PRINTF_ARG(chan->global_identifier),
  321. channel_state_to_string(chan->state), chan->state,
  322. hex_str(chan->identity_digest, DIGEST_LEN));
  323. /* Make sure we have all_channels, then add it */
  324. if (!all_channels) all_channels = smartlist_new();
  325. smartlist_add(all_channels, chan);
  326. /* Is it finished? */
  327. if (chan->state == CHANNEL_STATE_CLOSED ||
  328. chan->state == CHANNEL_STATE_ERROR) {
  329. /* Put it in the finished list, creating it if necessary */
  330. if (!finished_channels) finished_channels = smartlist_new();
  331. smartlist_add(finished_channels, chan);
  332. } else {
  333. /* Put it in the active list, creating it if necessary */
  334. if (!active_channels) active_channels = smartlist_new();
  335. smartlist_add(active_channels, chan);
  336. if (chan->state != CHANNEL_STATE_CLOSING) {
  337. /* It should have a digest set */
  338. if (!tor_digest_is_zero(chan->identity_digest)) {
  339. /* Yeah, we're good, add it to the map */
  340. channel_add_to_digest_map(chan);
  341. } else {
  342. log_info(LD_CHANNEL,
  343. "Channel %p (global ID " U64_FORMAT ") "
  344. "in state %s (%d) registered with no identity digest",
  345. chan, U64_PRINTF_ARG(chan->global_identifier),
  346. channel_state_to_string(chan->state), chan->state);
  347. }
  348. }
  349. }
  350. /* Mark it as registered */
  351. chan->registered = 1;
  352. }
  353. /**
  354. * Unregister a channel
  355. *
  356. * This function removes a channel from the global lists and maps and is used
  357. * when freeing a closed/errored channel.
  358. */
  359. void
  360. channel_unregister(channel_t *chan)
  361. {
  362. tor_assert(chan);
  363. /* No-op if not registered */
  364. if (!(chan->registered)) return;
  365. /* Is it finished? */
  366. if (chan->state == CHANNEL_STATE_CLOSED ||
  367. chan->state == CHANNEL_STATE_ERROR) {
  368. /* Get it out of the finished list */
  369. if (finished_channels) smartlist_remove(finished_channels, chan);
  370. } else {
  371. /* Get it out of the active list */
  372. if (active_channels) smartlist_remove(active_channels, chan);
  373. }
  374. /* Get it out of all_channels */
  375. if (all_channels) smartlist_remove(all_channels, chan);
  376. /* Mark it as unregistered */
  377. chan->registered = 0;
  378. /* Should it be in the digest map? */
  379. if (!tor_digest_is_zero(chan->identity_digest) &&
  380. !(chan->state == CHANNEL_STATE_CLOSING ||
  381. chan->state == CHANNEL_STATE_CLOSED ||
  382. chan->state == CHANNEL_STATE_ERROR)) {
  383. /* Remove it */
  384. channel_remove_from_digest_map(chan);
  385. }
  386. }
  387. /**
  388. * Register a channel listener
  389. *
  390. * This function registers a newly created channel listner in the global
  391. * lists/maps of active channel listeners.
  392. */
  393. void
  394. channel_listener_register(channel_listener_t *chan_l)
  395. {
  396. tor_assert(chan_l);
  397. /* No-op if already registered */
  398. if (chan_l->registered) return;
  399. log_debug(LD_CHANNEL,
  400. "Registering channel listener %p (ID " U64_FORMAT ") "
  401. "in state %s (%d)",
  402. chan_l, U64_PRINTF_ARG(chan_l->global_identifier),
  403. channel_listener_state_to_string(chan_l->state),
  404. chan_l->state);
  405. /* Make sure we have all_channels, then add it */
  406. if (!all_listeners) all_listeners = smartlist_new();
  407. smartlist_add(all_listeners, chan_l);
  408. /* Is it finished? */
  409. if (chan_l->state == CHANNEL_LISTENER_STATE_CLOSED ||
  410. chan_l->state == CHANNEL_LISTENER_STATE_ERROR) {
  411. /* Put it in the finished list, creating it if necessary */
  412. if (!finished_listeners) finished_listeners = smartlist_new();
  413. smartlist_add(finished_listeners, chan_l);
  414. } else {
  415. /* Put it in the active list, creating it if necessary */
  416. if (!active_listeners) active_listeners = smartlist_new();
  417. smartlist_add(active_listeners, chan_l);
  418. }
  419. /* Mark it as registered */
  420. chan_l->registered = 1;
  421. }
  422. /**
  423. * Unregister a channel listener
  424. *
  425. * This function removes a channel listener from the global lists and maps
  426. * and is used when freeing a closed/errored channel listener.
  427. */
  428. void
  429. channel_listener_unregister(channel_listener_t *chan_l)
  430. {
  431. tor_assert(chan_l);
  432. /* No-op if not registered */
  433. if (!(chan_l->registered)) return;
  434. /* Is it finished? */
  435. if (chan_l->state == CHANNEL_LISTENER_STATE_CLOSED ||
  436. chan_l->state == CHANNEL_LISTENER_STATE_ERROR) {
  437. /* Get it out of the finished list */
  438. if (finished_listeners) smartlist_remove(finished_listeners, chan_l);
  439. } else {
  440. /* Get it out of the active list */
  441. if (active_listeners) smartlist_remove(active_listeners, chan_l);
  442. }
  443. /* Get it out of all_channels */
  444. if (all_listeners) smartlist_remove(all_listeners, chan_l);
  445. /* Mark it as unregistered */
  446. chan_l->registered = 0;
  447. }
  448. /*********************************
  449. * Channel digest map maintenance
  450. *********************************/
  451. /**
  452. * Add a channel to the digest map
  453. *
  454. * This function adds a channel to the digest map and inserts it into the
  455. * correct linked list if channels with that remote endpoint identity digest
  456. * already exist.
  457. */
  458. static void
  459. channel_add_to_digest_map(channel_t *chan)
  460. {
  461. channel_idmap_entry_t *ent, search;
  462. tor_assert(chan);
  463. /* Assert that the state makes sense */
  464. tor_assert(!(chan->state == CHANNEL_STATE_CLOSING ||
  465. chan->state == CHANNEL_STATE_CLOSED ||
  466. chan->state == CHANNEL_STATE_ERROR));
  467. /* Assert that there is a digest */
  468. tor_assert(!tor_digest_is_zero(chan->identity_digest));
  469. memcpy(search.digest, chan->identity_digest, DIGEST_LEN);
  470. ent = HT_FIND(channel_idmap, &channel_identity_map, &search);
  471. if (! ent) {
  472. ent = tor_malloc(sizeof(channel_idmap_entry_t));
  473. memcpy(ent->digest, chan->identity_digest, DIGEST_LEN);
  474. TOR_LIST_INIT(&ent->channel_list);
  475. HT_INSERT(channel_idmap, &channel_identity_map, ent);
  476. }
  477. TOR_LIST_INSERT_HEAD(&ent->channel_list, chan, next_with_same_id);
  478. log_debug(LD_CHANNEL,
  479. "Added channel %p (global ID " U64_FORMAT ") "
  480. "to identity map in state %s (%d) with digest %s",
  481. chan, U64_PRINTF_ARG(chan->global_identifier),
  482. channel_state_to_string(chan->state), chan->state,
  483. hex_str(chan->identity_digest, DIGEST_LEN));
  484. }
  485. /**
  486. * Remove a channel from the digest map
  487. *
  488. * This function removes a channel from the digest map and the linked list of
  489. * channels for that digest if more than one exists.
  490. */
  491. static void
  492. channel_remove_from_digest_map(channel_t *chan)
  493. {
  494. channel_idmap_entry_t *ent, search;
  495. tor_assert(chan);
  496. /* Assert that there is a digest */
  497. tor_assert(!tor_digest_is_zero(chan->identity_digest));
  498. #if 0
  499. /* Make sure we have a map */
  500. if (!channel_identity_map) {
  501. /*
  502. * No identity map, so we can't find it by definition. This
  503. * case is similar to digestmap_get() failing below.
  504. */
  505. log_warn(LD_BUG,
  506. "Trying to remove channel %p (global ID " U64_FORMAT ") "
  507. "with digest %s from identity map, but didn't have any identity "
  508. "map",
  509. chan, U64_PRINTF_ARG(chan->global_identifier),
  510. hex_str(chan->identity_digest, DIGEST_LEN));
  511. /* Clear out its next/prev pointers */
  512. if (chan->next_with_same_id) {
  513. chan->next_with_same_id->prev_with_same_id = chan->prev_with_same_id;
  514. }
  515. if (chan->prev_with_same_id) {
  516. chan->prev_with_same_id->next_with_same_id = chan->next_with_same_id;
  517. }
  518. chan->next_with_same_id = NULL;
  519. chan->prev_with_same_id = NULL;
  520. return;
  521. }
  522. #endif
  523. /* Pull it out of its list, wherever that list is */
  524. TOR_LIST_REMOVE(chan, next_with_same_id);
  525. memcpy(search.digest, chan->identity_digest, DIGEST_LEN);
  526. ent = HT_FIND(channel_idmap, &channel_identity_map, &search);
  527. /* Look for it in the map */
  528. if (ent) {
  529. /* Okay, it's here */
  530. if (TOR_LIST_EMPTY(&ent->channel_list)) {
  531. HT_REMOVE(channel_idmap, &channel_identity_map, ent);
  532. tor_free(ent);
  533. }
  534. log_debug(LD_CHANNEL,
  535. "Removed channel %p (global ID " U64_FORMAT ") from "
  536. "identity map in state %s (%d) with digest %s",
  537. chan, U64_PRINTF_ARG(chan->global_identifier),
  538. channel_state_to_string(chan->state), chan->state,
  539. hex_str(chan->identity_digest, DIGEST_LEN));
  540. } else {
  541. /* Shouldn't happen */
  542. log_warn(LD_BUG,
  543. "Trying to remove channel %p (global ID " U64_FORMAT ") with "
  544. "digest %s from identity map, but couldn't find any with "
  545. "that digest",
  546. chan, U64_PRINTF_ARG(chan->global_identifier),
  547. hex_str(chan->identity_digest, DIGEST_LEN));
  548. }
  549. }
  550. /****************************
  551. * Channel lookup functions *
  552. ***************************/
  553. /**
  554. * Find channel by global ID
  555. *
  556. * This function searches for a channel by the global_identifier assigned
  557. * at initialization time. This identifier is unique for the lifetime of the
  558. * Tor process.
  559. */
  560. channel_t *
  561. channel_find_by_global_id(uint64_t global_identifier)
  562. {
  563. channel_t *rv = NULL;
  564. if (all_channels && smartlist_len(all_channels) > 0) {
  565. SMARTLIST_FOREACH_BEGIN(all_channels, channel_t *, curr) {
  566. if (curr->global_identifier == global_identifier) {
  567. rv = curr;
  568. break;
  569. }
  570. } SMARTLIST_FOREACH_END(curr);
  571. }
  572. return rv;
  573. }
  574. /**
  575. * Find channel by digest of the remote endpoint
  576. *
  577. * This function looks up a channel by the digest of its remote endpoint in
  578. * the channel digest map. It's possible that more than one channel to a
  579. * given endpoint exists. Use channel_next_with_digest() to walk the list.
  580. */
  581. channel_t *
  582. channel_find_by_remote_digest(const char *identity_digest)
  583. {
  584. channel_t *rv = NULL;
  585. channel_idmap_entry_t *ent, search;
  586. tor_assert(identity_digest);
  587. memcpy(search.digest, identity_digest, DIGEST_LEN);
  588. ent = HT_FIND(channel_idmap, &channel_identity_map, &search);
  589. if (ent) {
  590. rv = TOR_LIST_FIRST(&ent->channel_list);
  591. }
  592. return rv;
  593. }
  594. /**
  595. * Get next channel with digest
  596. *
  597. * This function takes a channel and finds the next channel in the list
  598. * with the same digest.
  599. */
  600. channel_t *
  601. channel_next_with_digest(channel_t *chan)
  602. {
  603. tor_assert(chan);
  604. return TOR_LIST_NEXT(chan, next_with_same_id);
  605. }
  606. /**
  607. * Initialize a channel
  608. *
  609. * This function should be called by subclasses to set up some per-channel
  610. * variables. I.e., this is the superclass constructor. Before this, the
  611. * channel should be allocated with tor_malloc_zero().
  612. */
  613. void
  614. channel_init(channel_t *chan)
  615. {
  616. tor_assert(chan);
  617. /* Assign an ID and bump the counter */
  618. chan->global_identifier = n_channels_allocated++;
  619. /* Init timestamp */
  620. chan->timestamp_last_had_circuits = time(NULL);
  621. /* Warn about exhausted circuit IDs no more than hourly. */
  622. chan->last_warned_circ_ids_exhausted.rate = 3600;
  623. /* Initialize queues. */
  624. TOR_SIMPLEQ_INIT(&chan->incoming_queue);
  625. TOR_SIMPLEQ_INIT(&chan->outgoing_queue);
  626. /* Initialize list entries. */
  627. memset(&chan->next_with_same_id, 0, sizeof(chan->next_with_same_id));
  628. /* Timestamp it */
  629. channel_timestamp_created(chan);
  630. /* It hasn't been open yet. */
  631. chan->has_been_open = 0;
  632. }
  633. /**
  634. * Initialize a channel listener
  635. *
  636. * This function should be called by subclasses to set up some per-channel
  637. * variables. I.e., this is the superclass constructor. Before this, the
  638. * channel listener should be allocated with tor_malloc_zero().
  639. */
  640. void
  641. channel_init_listener(channel_listener_t *chan_l)
  642. {
  643. tor_assert(chan_l);
  644. /* Assign an ID and bump the counter */
  645. chan_l->global_identifier = n_channels_allocated++;
  646. /* Timestamp it */
  647. channel_listener_timestamp_created(chan_l);
  648. }
  649. /**
  650. * Free a channel; nothing outside of channel.c and subclasses should call
  651. * this - it frees channels after they have closed and been unregistered.
  652. */
  653. void
  654. channel_free(channel_t *chan)
  655. {
  656. if (!chan) return;
  657. /* It must be closed or errored */
  658. tor_assert(chan->state == CHANNEL_STATE_CLOSED ||
  659. chan->state == CHANNEL_STATE_ERROR);
  660. /* It must be deregistered */
  661. tor_assert(!(chan->registered));
  662. log_debug(LD_CHANNEL,
  663. "Freeing channel " U64_FORMAT " at %p",
  664. U64_PRINTF_ARG(chan->global_identifier), chan);
  665. /*
  666. * Get rid of cmux policy before we do anything, so cmux policies don't
  667. * see channels in weird half-freed states.
  668. */
  669. if (chan->cmux) {
  670. circuitmux_set_policy(chan->cmux, NULL);
  671. }
  672. /* Call a free method if there is one */
  673. if (chan->free) chan->free(chan);
  674. channel_clear_remote_end(chan);
  675. /* Get rid of cmux */
  676. if (chan->cmux) {
  677. circuitmux_detach_all_circuits(chan->cmux, NULL);
  678. circuitmux_mark_destroyed_circids_usable(chan->cmux, chan);
  679. circuitmux_free(chan->cmux);
  680. chan->cmux = NULL;
  681. }
  682. /* We're in CLOSED or ERROR, so the cell queue is already empty */
  683. tor_free(chan);
  684. }
  685. /**
  686. * Free a channel listener; nothing outside of channel.c and subclasses
  687. * should call this - it frees channel listeners after they have closed and
  688. * been unregistered.
  689. */
  690. void
  691. channel_listener_free(channel_listener_t *chan_l)
  692. {
  693. if (!chan_l) return;
  694. log_debug(LD_CHANNEL,
  695. "Freeing channel_listener_t " U64_FORMAT " at %p",
  696. U64_PRINTF_ARG(chan_l->global_identifier),
  697. chan_l);
  698. /* It must be closed or errored */
  699. tor_assert(chan_l->state == CHANNEL_LISTENER_STATE_CLOSED ||
  700. chan_l->state == CHANNEL_LISTENER_STATE_ERROR);
  701. /* It must be deregistered */
  702. tor_assert(!(chan_l->registered));
  703. /* Call a free method if there is one */
  704. if (chan_l->free) chan_l->free(chan_l);
  705. /*
  706. * We're in CLOSED or ERROR, so the incoming channel queue is already
  707. * empty.
  708. */
  709. tor_free(chan_l);
  710. }
  711. /**
  712. * Free a channel and skip the state/registration asserts; this internal-
  713. * use-only function should be called only from channel_free_all() when
  714. * shutting down the Tor process.
  715. */
  716. static void
  717. channel_force_free(channel_t *chan)
  718. {
  719. cell_queue_entry_t *cell, *cell_tmp;
  720. tor_assert(chan);
  721. log_debug(LD_CHANNEL,
  722. "Force-freeing channel " U64_FORMAT " at %p",
  723. U64_PRINTF_ARG(chan->global_identifier), chan);
  724. /*
  725. * Get rid of cmux policy before we do anything, so cmux policies don't
  726. * see channels in weird half-freed states.
  727. */
  728. if (chan->cmux) {
  729. circuitmux_set_policy(chan->cmux, NULL);
  730. }
  731. /* Call a free method if there is one */
  732. if (chan->free) chan->free(chan);
  733. channel_clear_remote_end(chan);
  734. /* Get rid of cmux */
  735. if (chan->cmux) {
  736. circuitmux_free(chan->cmux);
  737. chan->cmux = NULL;
  738. }
  739. /* We might still have a cell queue; kill it */
  740. TOR_SIMPLEQ_FOREACH_SAFE(cell, &chan->incoming_queue, next, cell_tmp) {
  741. cell_queue_entry_free(cell, 0);
  742. }
  743. TOR_SIMPLEQ_INIT(&chan->incoming_queue);
  744. /* Outgoing cell queue is similar, but we can have to free packed cells */
  745. TOR_SIMPLEQ_FOREACH_SAFE(cell, &chan->outgoing_queue, next, cell_tmp) {
  746. cell_queue_entry_free(cell, 0);
  747. }
  748. TOR_SIMPLEQ_INIT(&chan->outgoing_queue);
  749. tor_free(chan);
  750. }
  751. /**
  752. * Free a channel listener and skip the state/reigstration asserts; this
  753. * internal-use-only function should be called only from channel_free_all()
  754. * when shutting down the Tor process.
  755. */
  756. static void
  757. channel_listener_force_free(channel_listener_t *chan_l)
  758. {
  759. tor_assert(chan_l);
  760. log_debug(LD_CHANNEL,
  761. "Force-freeing channel_listener_t " U64_FORMAT " at %p",
  762. U64_PRINTF_ARG(chan_l->global_identifier),
  763. chan_l);
  764. /* Call a free method if there is one */
  765. if (chan_l->free) chan_l->free(chan_l);
  766. /*
  767. * The incoming list just gets emptied and freed; we request close on
  768. * any channels we find there, but since we got called while shutting
  769. * down they will get deregistered and freed elsewhere anyway.
  770. */
  771. if (chan_l->incoming_list) {
  772. SMARTLIST_FOREACH_BEGIN(chan_l->incoming_list,
  773. channel_t *, qchan) {
  774. channel_mark_for_close(qchan);
  775. } SMARTLIST_FOREACH_END(qchan);
  776. smartlist_free(chan_l->incoming_list);
  777. chan_l->incoming_list = NULL;
  778. }
  779. tor_free(chan_l);
  780. }
  781. /**
  782. * Return the current registered listener for a channel listener
  783. *
  784. * This function returns a function pointer to the current registered
  785. * handler for new incoming channels on a channel listener.
  786. */
  787. channel_listener_fn_ptr
  788. channel_listener_get_listener_fn(channel_listener_t *chan_l)
  789. {
  790. tor_assert(chan_l);
  791. if (chan_l->state == CHANNEL_LISTENER_STATE_LISTENING)
  792. return chan_l->listener;
  793. return NULL;
  794. }
  795. /**
  796. * Set the listener for a channel listener
  797. *
  798. * This function sets the handler for new incoming channels on a channel
  799. * listener.
  800. */
  801. void
  802. channel_listener_set_listener_fn(channel_listener_t *chan_l,
  803. channel_listener_fn_ptr listener)
  804. {
  805. tor_assert(chan_l);
  806. tor_assert(chan_l->state == CHANNEL_LISTENER_STATE_LISTENING);
  807. log_debug(LD_CHANNEL,
  808. "Setting listener callback for channel listener %p "
  809. "(global ID " U64_FORMAT ") to %p",
  810. chan_l, U64_PRINTF_ARG(chan_l->global_identifier),
  811. listener);
  812. chan_l->listener = listener;
  813. if (chan_l->listener) channel_listener_process_incoming(chan_l);
  814. }
  815. /**
  816. * Return the fixed-length cell handler for a channel
  817. *
  818. * This function gets the handler for incoming fixed-length cells installed
  819. * on a channel.
  820. */
  821. channel_cell_handler_fn_ptr
  822. channel_get_cell_handler(channel_t *chan)
  823. {
  824. tor_assert(chan);
  825. if (chan->state == CHANNEL_STATE_OPENING ||
  826. chan->state == CHANNEL_STATE_OPEN ||
  827. chan->state == CHANNEL_STATE_MAINT)
  828. return chan->cell_handler;
  829. return NULL;
  830. }
  831. /**
  832. * Return the variable-length cell handler for a channel
  833. *
  834. * This function gets the handler for incoming variable-length cells
  835. * installed on a channel.
  836. */
  837. channel_var_cell_handler_fn_ptr
  838. channel_get_var_cell_handler(channel_t *chan)
  839. {
  840. tor_assert(chan);
  841. if (chan->state == CHANNEL_STATE_OPENING ||
  842. chan->state == CHANNEL_STATE_OPEN ||
  843. chan->state == CHANNEL_STATE_MAINT)
  844. return chan->var_cell_handler;
  845. return NULL;
  846. }
  847. /**
  848. * Set both cell handlers for a channel
  849. *
  850. * This function sets both the fixed-length and variable length cell handlers
  851. * for a channel and processes any incoming cells that had been blocked in the
  852. * queue because none were available.
  853. */
  854. void
  855. channel_set_cell_handlers(channel_t *chan,
  856. channel_cell_handler_fn_ptr cell_handler,
  857. channel_var_cell_handler_fn_ptr
  858. var_cell_handler)
  859. {
  860. int try_again = 0;
  861. tor_assert(chan);
  862. tor_assert(chan->state == CHANNEL_STATE_OPENING ||
  863. chan->state == CHANNEL_STATE_OPEN ||
  864. chan->state == CHANNEL_STATE_MAINT);
  865. log_debug(LD_CHANNEL,
  866. "Setting cell_handler callback for channel %p to %p",
  867. chan, cell_handler);
  868. log_debug(LD_CHANNEL,
  869. "Setting var_cell_handler callback for channel %p to %p",
  870. chan, var_cell_handler);
  871. /* Should we try the queue? */
  872. if (cell_handler &&
  873. cell_handler != chan->cell_handler) try_again = 1;
  874. if (var_cell_handler &&
  875. var_cell_handler != chan->var_cell_handler) try_again = 1;
  876. /* Change them */
  877. chan->cell_handler = cell_handler;
  878. chan->var_cell_handler = var_cell_handler;
  879. /* Re-run the queue if we have one and there's any reason to */
  880. if (! TOR_SIMPLEQ_EMPTY(&chan->incoming_queue) &&
  881. try_again &&
  882. (chan->cell_handler ||
  883. chan->var_cell_handler)) channel_process_cells(chan);
  884. }
  885. /*
  886. * On closing channels
  887. *
  888. * There are three functions that close channels, for use in
  889. * different circumstances:
  890. *
  891. * - Use channel_mark_for_close() for most cases
  892. * - Use channel_close_from_lower_layer() if you are connection_or.c
  893. * and the other end closes the underlying connection.
  894. * - Use channel_close_for_error() if you are connection_or.c and
  895. * some sort of error has occurred.
  896. */
  897. /**
  898. * Mark a channel for closure
  899. *
  900. * This function tries to close a channel_t; it will go into the CLOSING
  901. * state, and eventually the lower layer should put it into the CLOSED or
  902. * ERROR state. Then, channel_run_cleanup() will eventually free it.
  903. */
  904. void
  905. channel_mark_for_close(channel_t *chan)
  906. {
  907. tor_assert(chan != NULL);
  908. tor_assert(chan->close != NULL);
  909. /* If it's already in CLOSING, CLOSED or ERROR, this is a no-op */
  910. if (chan->state == CHANNEL_STATE_CLOSING ||
  911. chan->state == CHANNEL_STATE_CLOSED ||
  912. chan->state == CHANNEL_STATE_ERROR) return;
  913. log_debug(LD_CHANNEL,
  914. "Closing channel %p (global ID " U64_FORMAT ") "
  915. "by request",
  916. chan, U64_PRINTF_ARG(chan->global_identifier));
  917. /* Note closing by request from above */
  918. chan->reason_for_closing = CHANNEL_CLOSE_REQUESTED;
  919. /* Change state to CLOSING */
  920. channel_change_state(chan, CHANNEL_STATE_CLOSING);
  921. /* Tell the lower layer */
  922. chan->close(chan);
  923. /*
  924. * It's up to the lower layer to change state to CLOSED or ERROR when we're
  925. * ready; we'll try to free channels that are in the finished list from
  926. * channel_run_cleanup(). The lower layer should do this by calling
  927. * channel_closed().
  928. */
  929. }
  930. /**
  931. * Mark a channel listener for closure
  932. *
  933. * This function tries to close a channel_listener_t; it will go into the
  934. * CLOSING state, and eventually the lower layer should put it into the CLOSED
  935. * or ERROR state. Then, channel_run_cleanup() will eventually free it.
  936. */
  937. void
  938. channel_listener_mark_for_close(channel_listener_t *chan_l)
  939. {
  940. tor_assert(chan_l != NULL);
  941. tor_assert(chan_l->close != NULL);
  942. /* If it's already in CLOSING, CLOSED or ERROR, this is a no-op */
  943. if (chan_l->state == CHANNEL_LISTENER_STATE_CLOSING ||
  944. chan_l->state == CHANNEL_LISTENER_STATE_CLOSED ||
  945. chan_l->state == CHANNEL_LISTENER_STATE_ERROR) return;
  946. log_debug(LD_CHANNEL,
  947. "Closing channel listener %p (global ID " U64_FORMAT ") "
  948. "by request",
  949. chan_l, U64_PRINTF_ARG(chan_l->global_identifier));
  950. /* Note closing by request from above */
  951. chan_l->reason_for_closing = CHANNEL_LISTENER_CLOSE_REQUESTED;
  952. /* Change state to CLOSING */
  953. channel_listener_change_state(chan_l, CHANNEL_LISTENER_STATE_CLOSING);
  954. /* Tell the lower layer */
  955. chan_l->close(chan_l);
  956. /*
  957. * It's up to the lower layer to change state to CLOSED or ERROR when we're
  958. * ready; we'll try to free channels that are in the finished list from
  959. * channel_run_cleanup(). The lower layer should do this by calling
  960. * channel_listener_closed().
  961. */
  962. }
  963. /**
  964. * Close a channel from the lower layer
  965. *
  966. * Notify the channel code that the channel is being closed due to a non-error
  967. * condition in the lower layer. This does not call the close() method, since
  968. * the lower layer already knows.
  969. */
  970. void
  971. channel_close_from_lower_layer(channel_t *chan)
  972. {
  973. tor_assert(chan != NULL);
  974. /* If it's already in CLOSING, CLOSED or ERROR, this is a no-op */
  975. if (chan->state == CHANNEL_STATE_CLOSING ||
  976. chan->state == CHANNEL_STATE_CLOSED ||
  977. chan->state == CHANNEL_STATE_ERROR) return;
  978. log_debug(LD_CHANNEL,
  979. "Closing channel %p (global ID " U64_FORMAT ") "
  980. "due to lower-layer event",
  981. chan, U64_PRINTF_ARG(chan->global_identifier));
  982. /* Note closing by event from below */
  983. chan->reason_for_closing = CHANNEL_CLOSE_FROM_BELOW;
  984. /* Change state to CLOSING */
  985. channel_change_state(chan, CHANNEL_STATE_CLOSING);
  986. }
  987. /**
  988. * Close a channel listener from the lower layer
  989. *
  990. * Notify the channel code that the channel listener is being closed due to a
  991. * non-error condition in the lower layer. This does not call the close()
  992. * method, since the lower layer already knows.
  993. */
  994. void
  995. channel_listener_close_from_lower_layer(channel_listener_t *chan_l)
  996. {
  997. tor_assert(chan_l != NULL);
  998. /* If it's already in CLOSING, CLOSED or ERROR, this is a no-op */
  999. if (chan_l->state == CHANNEL_LISTENER_STATE_CLOSING ||
  1000. chan_l->state == CHANNEL_LISTENER_STATE_CLOSED ||
  1001. chan_l->state == CHANNEL_LISTENER_STATE_ERROR) return;
  1002. log_debug(LD_CHANNEL,
  1003. "Closing channel listener %p (global ID " U64_FORMAT ") "
  1004. "due to lower-layer event",
  1005. chan_l, U64_PRINTF_ARG(chan_l->global_identifier));
  1006. /* Note closing by event from below */
  1007. chan_l->reason_for_closing = CHANNEL_LISTENER_CLOSE_FROM_BELOW;
  1008. /* Change state to CLOSING */
  1009. channel_listener_change_state(chan_l, CHANNEL_LISTENER_STATE_CLOSING);
  1010. }
  1011. /**
  1012. * Notify that the channel is being closed due to an error condition
  1013. *
  1014. * This function is called by the lower layer implementing the transport
  1015. * when a channel must be closed due to an error condition. This does not
  1016. * call the channel's close method, since the lower layer already knows.
  1017. */
  1018. void
  1019. channel_close_for_error(channel_t *chan)
  1020. {
  1021. tor_assert(chan != NULL);
  1022. /* If it's already in CLOSING, CLOSED or ERROR, this is a no-op */
  1023. if (chan->state == CHANNEL_STATE_CLOSING ||
  1024. chan->state == CHANNEL_STATE_CLOSED ||
  1025. chan->state == CHANNEL_STATE_ERROR) return;
  1026. log_debug(LD_CHANNEL,
  1027. "Closing channel %p due to lower-layer error",
  1028. chan);
  1029. /* Note closing by event from below */
  1030. chan->reason_for_closing = CHANNEL_CLOSE_FOR_ERROR;
  1031. /* Change state to CLOSING */
  1032. channel_change_state(chan, CHANNEL_STATE_CLOSING);
  1033. }
  1034. /**
  1035. * Notify that the channel listener is being closed due to an error condition
  1036. *
  1037. * This function is called by the lower layer implementing the transport
  1038. * when a channel listener must be closed due to an error condition. This
  1039. * does not call the channel listener's close method, since the lower layer
  1040. * already knows.
  1041. */
  1042. void
  1043. channel_listener_close_for_error(channel_listener_t *chan_l)
  1044. {
  1045. tor_assert(chan_l != NULL);
  1046. /* If it's already in CLOSING, CLOSED or ERROR, this is a no-op */
  1047. if (chan_l->state == CHANNEL_LISTENER_STATE_CLOSING ||
  1048. chan_l->state == CHANNEL_LISTENER_STATE_CLOSED ||
  1049. chan_l->state == CHANNEL_LISTENER_STATE_ERROR) return;
  1050. log_debug(LD_CHANNEL,
  1051. "Closing channel listener %p (global ID " U64_FORMAT ") "
  1052. "due to lower-layer error",
  1053. chan_l, U64_PRINTF_ARG(chan_l->global_identifier));
  1054. /* Note closing by event from below */
  1055. chan_l->reason_for_closing = CHANNEL_LISTENER_CLOSE_FOR_ERROR;
  1056. /* Change state to CLOSING */
  1057. channel_listener_change_state(chan_l, CHANNEL_LISTENER_STATE_CLOSING);
  1058. }
  1059. /**
  1060. * Notify that the lower layer is finished closing the channel
  1061. *
  1062. * This function should be called by the lower layer when a channel
  1063. * is finished closing and it should be regarded as inactive and
  1064. * freed by the channel code.
  1065. */
  1066. void
  1067. channel_closed(channel_t *chan)
  1068. {
  1069. tor_assert(chan);
  1070. tor_assert(chan->state == CHANNEL_STATE_CLOSING ||
  1071. chan->state == CHANNEL_STATE_CLOSED ||
  1072. chan->state == CHANNEL_STATE_ERROR);
  1073. /* No-op if already inactive */
  1074. if (chan->state == CHANNEL_STATE_CLOSED ||
  1075. chan->state == CHANNEL_STATE_ERROR) return;
  1076. /* Inform any pending (not attached) circs that they should
  1077. * give up. */
  1078. if (! chan->has_been_open)
  1079. circuit_n_chan_done(chan, 0);
  1080. /* Now close all the attached circuits on it. */
  1081. circuit_unlink_all_from_channel(chan, END_CIRC_REASON_CHANNEL_CLOSED);
  1082. if (chan->reason_for_closing != CHANNEL_CLOSE_FOR_ERROR) {
  1083. channel_change_state(chan, CHANNEL_STATE_CLOSED);
  1084. } else {
  1085. channel_change_state(chan, CHANNEL_STATE_ERROR);
  1086. }
  1087. }
  1088. /**
  1089. * Notify that the lower layer is finished closing the channel listener
  1090. *
  1091. * This function should be called by the lower layer when a channel listener
  1092. * is finished closing and it should be regarded as inactive and
  1093. * freed by the channel code.
  1094. */
  1095. void
  1096. channel_listener_closed(channel_listener_t *chan_l)
  1097. {
  1098. tor_assert(chan_l);
  1099. tor_assert(chan_l->state == CHANNEL_LISTENER_STATE_CLOSING ||
  1100. chan_l->state == CHANNEL_LISTENER_STATE_CLOSED ||
  1101. chan_l->state == CHANNEL_LISTENER_STATE_ERROR);
  1102. /* No-op if already inactive */
  1103. if (chan_l->state == CHANNEL_LISTENER_STATE_CLOSED ||
  1104. chan_l->state == CHANNEL_LISTENER_STATE_ERROR) return;
  1105. if (chan_l->reason_for_closing != CHANNEL_LISTENER_CLOSE_FOR_ERROR) {
  1106. channel_listener_change_state(chan_l, CHANNEL_LISTENER_STATE_CLOSED);
  1107. } else {
  1108. channel_listener_change_state(chan_l, CHANNEL_LISTENER_STATE_ERROR);
  1109. }
  1110. }
  1111. /**
  1112. * Clear the identity_digest of a channel
  1113. *
  1114. * This function clears the identity digest of the remote endpoint for a
  1115. * channel; this is intended for use by the lower layer.
  1116. */
  1117. void
  1118. channel_clear_identity_digest(channel_t *chan)
  1119. {
  1120. int state_not_in_map;
  1121. tor_assert(chan);
  1122. log_debug(LD_CHANNEL,
  1123. "Clearing remote endpoint digest on channel %p with "
  1124. "global ID " U64_FORMAT,
  1125. chan, U64_PRINTF_ARG(chan->global_identifier));
  1126. state_not_in_map =
  1127. (chan->state == CHANNEL_STATE_CLOSING ||
  1128. chan->state == CHANNEL_STATE_CLOSED ||
  1129. chan->state == CHANNEL_STATE_ERROR);
  1130. if (!state_not_in_map && chan->registered &&
  1131. !tor_digest_is_zero(chan->identity_digest))
  1132. /* if it's registered get it out of the digest map */
  1133. channel_remove_from_digest_map(chan);
  1134. memset(chan->identity_digest, 0,
  1135. sizeof(chan->identity_digest));
  1136. }
  1137. /**
  1138. * Set the identity_digest of a channel
  1139. *
  1140. * This function sets the identity digest of the remote endpoint for a
  1141. * channel; this is intended for use by the lower layer.
  1142. */
  1143. void
  1144. channel_set_identity_digest(channel_t *chan,
  1145. const char *identity_digest)
  1146. {
  1147. int was_in_digest_map, should_be_in_digest_map, state_not_in_map;
  1148. tor_assert(chan);
  1149. log_debug(LD_CHANNEL,
  1150. "Setting remote endpoint digest on channel %p with "
  1151. "global ID " U64_FORMAT " to digest %s",
  1152. chan, U64_PRINTF_ARG(chan->global_identifier),
  1153. identity_digest ?
  1154. hex_str(identity_digest, DIGEST_LEN) : "(null)");
  1155. state_not_in_map =
  1156. (chan->state == CHANNEL_STATE_CLOSING ||
  1157. chan->state == CHANNEL_STATE_CLOSED ||
  1158. chan->state == CHANNEL_STATE_ERROR);
  1159. was_in_digest_map =
  1160. !state_not_in_map &&
  1161. chan->registered &&
  1162. !tor_digest_is_zero(chan->identity_digest);
  1163. should_be_in_digest_map =
  1164. !state_not_in_map &&
  1165. chan->registered &&
  1166. (identity_digest &&
  1167. !tor_digest_is_zero(identity_digest));
  1168. if (was_in_digest_map)
  1169. /* We should always remove it; we'll add it back if we're writing
  1170. * in a new digest.
  1171. */
  1172. channel_remove_from_digest_map(chan);
  1173. if (identity_digest) {
  1174. memcpy(chan->identity_digest,
  1175. identity_digest,
  1176. sizeof(chan->identity_digest));
  1177. } else {
  1178. memset(chan->identity_digest, 0,
  1179. sizeof(chan->identity_digest));
  1180. }
  1181. /* Put it in the digest map if we should */
  1182. if (should_be_in_digest_map)
  1183. channel_add_to_digest_map(chan);
  1184. }
  1185. /**
  1186. * Clear the remote end metadata (identity_digest/nickname) of a channel
  1187. *
  1188. * This function clears all the remote end info from a channel; this is
  1189. * intended for use by the lower layer.
  1190. */
  1191. void
  1192. channel_clear_remote_end(channel_t *chan)
  1193. {
  1194. int state_not_in_map;
  1195. tor_assert(chan);
  1196. log_debug(LD_CHANNEL,
  1197. "Clearing remote endpoint identity on channel %p with "
  1198. "global ID " U64_FORMAT,
  1199. chan, U64_PRINTF_ARG(chan->global_identifier));
  1200. state_not_in_map =
  1201. (chan->state == CHANNEL_STATE_CLOSING ||
  1202. chan->state == CHANNEL_STATE_CLOSED ||
  1203. chan->state == CHANNEL_STATE_ERROR);
  1204. if (!state_not_in_map && chan->registered &&
  1205. !tor_digest_is_zero(chan->identity_digest))
  1206. /* if it's registered get it out of the digest map */
  1207. channel_remove_from_digest_map(chan);
  1208. memset(chan->identity_digest, 0,
  1209. sizeof(chan->identity_digest));
  1210. tor_free(chan->nickname);
  1211. }
  1212. /**
  1213. * Set the remote end metadata (identity_digest/nickname) of a channel
  1214. *
  1215. * This function sets new remote end info on a channel; this is intended
  1216. * for use by the lower layer.
  1217. */
  1218. void
  1219. channel_set_remote_end(channel_t *chan,
  1220. const char *identity_digest,
  1221. const char *nickname)
  1222. {
  1223. int was_in_digest_map, should_be_in_digest_map, state_not_in_map;
  1224. tor_assert(chan);
  1225. log_debug(LD_CHANNEL,
  1226. "Setting remote endpoint identity on channel %p with "
  1227. "global ID " U64_FORMAT " to nickname %s, digest %s",
  1228. chan, U64_PRINTF_ARG(chan->global_identifier),
  1229. nickname ? nickname : "(null)",
  1230. identity_digest ?
  1231. hex_str(identity_digest, DIGEST_LEN) : "(null)");
  1232. state_not_in_map =
  1233. (chan->state == CHANNEL_STATE_CLOSING ||
  1234. chan->state == CHANNEL_STATE_CLOSED ||
  1235. chan->state == CHANNEL_STATE_ERROR);
  1236. was_in_digest_map =
  1237. !state_not_in_map &&
  1238. chan->registered &&
  1239. !tor_digest_is_zero(chan->identity_digest);
  1240. should_be_in_digest_map =
  1241. !state_not_in_map &&
  1242. chan->registered &&
  1243. (identity_digest &&
  1244. !tor_digest_is_zero(identity_digest));
  1245. if (was_in_digest_map)
  1246. /* We should always remove it; we'll add it back if we're writing
  1247. * in a new digest.
  1248. */
  1249. channel_remove_from_digest_map(chan);
  1250. if (identity_digest) {
  1251. memcpy(chan->identity_digest,
  1252. identity_digest,
  1253. sizeof(chan->identity_digest));
  1254. } else {
  1255. memset(chan->identity_digest, 0,
  1256. sizeof(chan->identity_digest));
  1257. }
  1258. tor_free(chan->nickname);
  1259. if (nickname)
  1260. chan->nickname = tor_strdup(nickname);
  1261. /* Put it in the digest map if we should */
  1262. if (should_be_in_digest_map)
  1263. channel_add_to_digest_map(chan);
  1264. }
  1265. /**
  1266. * Duplicate a cell queue entry; this is a shallow copy intended for use
  1267. * in channel_write_cell_queue_entry().
  1268. */
  1269. static cell_queue_entry_t *
  1270. cell_queue_entry_dup(cell_queue_entry_t *q)
  1271. {
  1272. cell_queue_entry_t *rv = NULL;
  1273. tor_assert(q);
  1274. rv = tor_malloc(sizeof(*rv));
  1275. memcpy(rv, q, sizeof(*rv));
  1276. return rv;
  1277. }
  1278. /**
  1279. * Free a cell_queue_entry_t; the handed_off parameter indicates whether
  1280. * the contents were passed to the lower layer (it is responsible for
  1281. * them) or not (we should free).
  1282. */
  1283. static void
  1284. cell_queue_entry_free(cell_queue_entry_t *q, int handed_off)
  1285. {
  1286. if (!q) return;
  1287. if (!handed_off) {
  1288. /*
  1289. * If we handed it off, the recipient becomes responsible (or
  1290. * with packed cells the channel_t subclass calls packed_cell
  1291. * free after writing out its contents; see, e.g.,
  1292. * channel_tls_write_packed_cell_method(). Otherwise, we have
  1293. * to take care of it here if possible.
  1294. */
  1295. switch (q->type) {
  1296. case CELL_QUEUE_FIXED:
  1297. if (q->u.fixed.cell) {
  1298. /*
  1299. * There doesn't seem to be a cell_free() function anywhere in the
  1300. * pre-channel code; just use tor_free()
  1301. */
  1302. tor_free(q->u.fixed.cell);
  1303. }
  1304. break;
  1305. case CELL_QUEUE_PACKED:
  1306. if (q->u.packed.packed_cell) {
  1307. packed_cell_free(q->u.packed.packed_cell);
  1308. }
  1309. break;
  1310. case CELL_QUEUE_VAR:
  1311. if (q->u.var.var_cell) {
  1312. /*
  1313. * This one's in connection_or.c; it'd be nice to figure out the
  1314. * whole flow of cells from one end to the other and factor the
  1315. * cell memory management functions like this out of the specific
  1316. * TLS lower layer.
  1317. */
  1318. var_cell_free(q->u.var.var_cell);
  1319. }
  1320. break;
  1321. default:
  1322. /*
  1323. * Nothing we can do if we don't know the type; this will
  1324. * have been warned about elsewhere.
  1325. */
  1326. break;
  1327. }
  1328. }
  1329. tor_free(q);
  1330. }
  1331. #if 0
  1332. /**
  1333. * Check whether a cell queue entry is padding; this is a helper function
  1334. * for channel_write_cell_queue_entry()
  1335. */
  1336. static int
  1337. cell_queue_entry_is_padding(cell_queue_entry_t *q)
  1338. {
  1339. tor_assert(q);
  1340. if (q->type == CELL_QUEUE_FIXED) {
  1341. if (q->u.fixed.cell) {
  1342. if (q->u.fixed.cell->command == CELL_PADDING ||
  1343. q->u.fixed.cell->command == CELL_VPADDING) {
  1344. return 1;
  1345. }
  1346. }
  1347. } else if (q->type == CELL_QUEUE_VAR) {
  1348. if (q->u.var.var_cell) {
  1349. if (q->u.var.var_cell->command == CELL_PADDING ||
  1350. q->u.var.var_cell->command == CELL_VPADDING) {
  1351. return 1;
  1352. }
  1353. }
  1354. }
  1355. return 0;
  1356. }
  1357. #endif
  1358. /**
  1359. * Allocate a new cell queue entry for a fixed-size cell
  1360. */
  1361. static cell_queue_entry_t *
  1362. cell_queue_entry_new_fixed(cell_t *cell)
  1363. {
  1364. cell_queue_entry_t *q = NULL;
  1365. tor_assert(cell);
  1366. q = tor_malloc(sizeof(*q));
  1367. q->type = CELL_QUEUE_FIXED;
  1368. q->u.fixed.cell = cell;
  1369. return q;
  1370. }
  1371. /**
  1372. * Allocate a new cell queue entry for a variable-size cell
  1373. */
  1374. static cell_queue_entry_t *
  1375. cell_queue_entry_new_var(var_cell_t *var_cell)
  1376. {
  1377. cell_queue_entry_t *q = NULL;
  1378. tor_assert(var_cell);
  1379. q = tor_malloc(sizeof(*q));
  1380. q->type = CELL_QUEUE_VAR;
  1381. q->u.var.var_cell = var_cell;
  1382. return q;
  1383. }
  1384. /**
  1385. * Write to a channel based on a cell_queue_entry_t
  1386. *
  1387. * Given a cell_queue_entry_t filled out by the caller, try to send the cell
  1388. * and queue it if we can't.
  1389. */
  1390. static void
  1391. channel_write_cell_queue_entry(channel_t *chan, cell_queue_entry_t *q)
  1392. {
  1393. int result = 0, sent = 0;
  1394. cell_queue_entry_t *tmp = NULL;
  1395. tor_assert(chan);
  1396. tor_assert(q);
  1397. /* Assert that the state makes sense for a cell write */
  1398. tor_assert(chan->state == CHANNEL_STATE_OPENING ||
  1399. chan->state == CHANNEL_STATE_OPEN ||
  1400. chan->state == CHANNEL_STATE_MAINT);
  1401. {
  1402. circid_t circ_id;
  1403. if (is_destroy_cell(chan, q, &circ_id)) {
  1404. channel_note_destroy_not_pending(chan, circ_id);
  1405. }
  1406. }
  1407. /* Can we send it right out? If so, try */
  1408. if (TOR_SIMPLEQ_EMPTY(&chan->outgoing_queue) &&
  1409. chan->state == CHANNEL_STATE_OPEN) {
  1410. /* Pick the right write function for this cell type and save the result */
  1411. switch (q->type) {
  1412. case CELL_QUEUE_FIXED:
  1413. tor_assert(chan->write_cell);
  1414. tor_assert(q->u.fixed.cell);
  1415. result = chan->write_cell(chan, q->u.fixed.cell);
  1416. break;
  1417. case CELL_QUEUE_PACKED:
  1418. tor_assert(chan->write_packed_cell);
  1419. tor_assert(q->u.packed.packed_cell);
  1420. result = chan->write_packed_cell(chan, q->u.packed.packed_cell);
  1421. break;
  1422. case CELL_QUEUE_VAR:
  1423. tor_assert(chan->write_var_cell);
  1424. tor_assert(q->u.var.var_cell);
  1425. result = chan->write_var_cell(chan, q->u.var.var_cell);
  1426. break;
  1427. default:
  1428. tor_assert(1);
  1429. }
  1430. /* Check if we got it out */
  1431. if (result > 0) {
  1432. sent = 1;
  1433. /* Timestamp for transmission */
  1434. channel_timestamp_xmit(chan);
  1435. /* If we're here the queue is empty, so it's drained too */
  1436. channel_timestamp_drained(chan);
  1437. /* Update the counter */
  1438. ++(chan->n_cells_xmitted);
  1439. }
  1440. }
  1441. if (!sent) {
  1442. /* Not sent, queue it */
  1443. /*
  1444. * We have to copy the queue entry passed in, since the caller probably
  1445. * used the stack.
  1446. */
  1447. tmp = cell_queue_entry_dup(q);
  1448. TOR_SIMPLEQ_INSERT_TAIL(&chan->outgoing_queue, tmp, next);
  1449. /* Try to process the queue? */
  1450. if (chan->state == CHANNEL_STATE_OPEN) channel_flush_cells(chan);
  1451. }
  1452. }
  1453. /**
  1454. * Write a cell to a channel
  1455. *
  1456. * Write a fixed-length cell to a channel using the write_cell() method.
  1457. * This is equivalent to the pre-channels connection_or_write_cell_to_buf();
  1458. * it is called by the transport-independent code to deliver a cell to a
  1459. * channel for transmission.
  1460. */
  1461. void
  1462. channel_write_cell(channel_t *chan, cell_t *cell)
  1463. {
  1464. cell_queue_entry_t q;
  1465. tor_assert(chan);
  1466. tor_assert(cell);
  1467. if (chan->state == CHANNEL_STATE_CLOSING) {
  1468. log_debug(LD_CHANNEL, "Discarding cell_t %p on closing channel %p with "
  1469. "global ID "U64_FORMAT, cell, chan,
  1470. U64_PRINTF_ARG(chan->global_identifier));
  1471. tor_free(cell);
  1472. return;
  1473. }
  1474. log_debug(LD_CHANNEL,
  1475. "Writing cell_t %p to channel %p with global ID "
  1476. U64_FORMAT,
  1477. cell, chan, U64_PRINTF_ARG(chan->global_identifier));
  1478. q.type = CELL_QUEUE_FIXED;
  1479. q.u.fixed.cell = cell;
  1480. channel_write_cell_queue_entry(chan, &q);
  1481. }
  1482. /**
  1483. * Write a packed cell to a channel
  1484. *
  1485. * Write a packed cell to a channel using the write_cell() method. This is
  1486. * called by the transport-independent code to deliver a packed cell to a
  1487. * channel for transmission.
  1488. */
  1489. void
  1490. channel_write_packed_cell(channel_t *chan, packed_cell_t *packed_cell)
  1491. {
  1492. cell_queue_entry_t q;
  1493. tor_assert(chan);
  1494. tor_assert(packed_cell);
  1495. if (chan->state == CHANNEL_STATE_CLOSING) {
  1496. log_debug(LD_CHANNEL, "Discarding packed_cell_t %p on closing channel %p "
  1497. "with global ID "U64_FORMAT, packed_cell, chan,
  1498. U64_PRINTF_ARG(chan->global_identifier));
  1499. packed_cell_free(packed_cell);
  1500. return;
  1501. }
  1502. log_debug(LD_CHANNEL,
  1503. "Writing packed_cell_t %p to channel %p with global ID "
  1504. U64_FORMAT,
  1505. packed_cell, chan,
  1506. U64_PRINTF_ARG(chan->global_identifier));
  1507. q.type = CELL_QUEUE_PACKED;
  1508. q.u.packed.packed_cell = packed_cell;
  1509. channel_write_cell_queue_entry(chan, &q);
  1510. }
  1511. /**
  1512. * Write a variable-length cell to a channel
  1513. *
  1514. * Write a variable-length cell to a channel using the write_cell() method.
  1515. * This is equivalent to the pre-channels
  1516. * connection_or_write_var_cell_to_buf(); it's called by the transport-
  1517. * independent code to deliver a var_cell to a channel for transmission.
  1518. */
  1519. void
  1520. channel_write_var_cell(channel_t *chan, var_cell_t *var_cell)
  1521. {
  1522. cell_queue_entry_t q;
  1523. tor_assert(chan);
  1524. tor_assert(var_cell);
  1525. if (chan->state == CHANNEL_STATE_CLOSING) {
  1526. log_debug(LD_CHANNEL, "Discarding var_cell_t %p on closing channel %p "
  1527. "with global ID "U64_FORMAT, var_cell, chan,
  1528. U64_PRINTF_ARG(chan->global_identifier));
  1529. var_cell_free(var_cell);
  1530. return;
  1531. }
  1532. log_debug(LD_CHANNEL,
  1533. "Writing var_cell_t %p to channel %p with global ID "
  1534. U64_FORMAT,
  1535. var_cell, chan,
  1536. U64_PRINTF_ARG(chan->global_identifier));
  1537. q.type = CELL_QUEUE_VAR;
  1538. q.u.var.var_cell = var_cell;
  1539. channel_write_cell_queue_entry(chan, &q);
  1540. }
  1541. /**
  1542. * Change channel state
  1543. *
  1544. * This internal and subclass use only function is used to change channel
  1545. * state, performing all transition validity checks and whatever actions
  1546. * are appropriate to the state transition in question.
  1547. */
  1548. void
  1549. channel_change_state(channel_t *chan, channel_state_t to_state)
  1550. {
  1551. channel_state_t from_state;
  1552. unsigned char was_active, is_active;
  1553. unsigned char was_in_id_map, is_in_id_map;
  1554. tor_assert(chan);
  1555. from_state = chan->state;
  1556. tor_assert(channel_state_is_valid(from_state));
  1557. tor_assert(channel_state_is_valid(to_state));
  1558. tor_assert(channel_state_can_transition(chan->state, to_state));
  1559. /* Check for no-op transitions */
  1560. if (from_state == to_state) {
  1561. log_debug(LD_CHANNEL,
  1562. "Got no-op transition from \"%s\" to itself on channel %p"
  1563. "(global ID " U64_FORMAT ")",
  1564. channel_state_to_string(to_state),
  1565. chan, U64_PRINTF_ARG(chan->global_identifier));
  1566. return;
  1567. }
  1568. /* If we're going to a closing or closed state, we must have a reason set */
  1569. if (to_state == CHANNEL_STATE_CLOSING ||
  1570. to_state == CHANNEL_STATE_CLOSED ||
  1571. to_state == CHANNEL_STATE_ERROR) {
  1572. tor_assert(chan->reason_for_closing != CHANNEL_NOT_CLOSING);
  1573. }
  1574. /*
  1575. * We need to maintain the queues here for some transitions:
  1576. * when we enter CHANNEL_STATE_OPEN (especially from CHANNEL_STATE_MAINT)
  1577. * we may have a backlog of cells to transmit, so drain the queues in
  1578. * that case, and when going to CHANNEL_STATE_CLOSED the subclass
  1579. * should have made sure to finish sending things (or gone to
  1580. * CHANNEL_STATE_ERROR if not possible), so we assert for that here.
  1581. */
  1582. log_debug(LD_CHANNEL,
  1583. "Changing state of channel %p (global ID " U64_FORMAT
  1584. ") from \"%s\" to \"%s\"",
  1585. chan,
  1586. U64_PRINTF_ARG(chan->global_identifier),
  1587. channel_state_to_string(chan->state),
  1588. channel_state_to_string(to_state));
  1589. chan->state = to_state;
  1590. /* Need to add to the right lists if the channel is registered */
  1591. if (chan->registered) {
  1592. was_active = !(from_state == CHANNEL_STATE_CLOSED ||
  1593. from_state == CHANNEL_STATE_ERROR);
  1594. is_active = !(to_state == CHANNEL_STATE_CLOSED ||
  1595. to_state == CHANNEL_STATE_ERROR);
  1596. /* Need to take off active list and put on finished list? */
  1597. if (was_active && !is_active) {
  1598. if (active_channels) smartlist_remove(active_channels, chan);
  1599. if (!finished_channels) finished_channels = smartlist_new();
  1600. smartlist_add(finished_channels, chan);
  1601. }
  1602. /* Need to put on active list? */
  1603. else if (!was_active && is_active) {
  1604. if (finished_channels) smartlist_remove(finished_channels, chan);
  1605. if (!active_channels) active_channels = smartlist_new();
  1606. smartlist_add(active_channels, chan);
  1607. }
  1608. if (!tor_digest_is_zero(chan->identity_digest)) {
  1609. /* Now we need to handle the identity map */
  1610. was_in_id_map = !(from_state == CHANNEL_STATE_CLOSING ||
  1611. from_state == CHANNEL_STATE_CLOSED ||
  1612. from_state == CHANNEL_STATE_ERROR);
  1613. is_in_id_map = !(to_state == CHANNEL_STATE_CLOSING ||
  1614. to_state == CHANNEL_STATE_CLOSED ||
  1615. to_state == CHANNEL_STATE_ERROR);
  1616. if (!was_in_id_map && is_in_id_map) channel_add_to_digest_map(chan);
  1617. else if (was_in_id_map && !is_in_id_map)
  1618. channel_remove_from_digest_map(chan);
  1619. }
  1620. }
  1621. /* Tell circuits if we opened and stuff */
  1622. if (to_state == CHANNEL_STATE_OPEN) {
  1623. channel_do_open_actions(chan);
  1624. chan->has_been_open = 1;
  1625. /* Check for queued cells to process */
  1626. if (! TOR_SIMPLEQ_EMPTY(&chan->incoming_queue))
  1627. channel_process_cells(chan);
  1628. if (! TOR_SIMPLEQ_EMPTY(&chan->outgoing_queue))
  1629. channel_flush_cells(chan);
  1630. } else if (to_state == CHANNEL_STATE_CLOSED ||
  1631. to_state == CHANNEL_STATE_ERROR) {
  1632. /* Assert that all queues are empty */
  1633. tor_assert(TOR_SIMPLEQ_EMPTY(&chan->incoming_queue));
  1634. tor_assert(TOR_SIMPLEQ_EMPTY(&chan->outgoing_queue));
  1635. }
  1636. }
  1637. /**
  1638. * Change channel listener state
  1639. *
  1640. * This internal and subclass use only function is used to change channel
  1641. * listener state, performing all transition validity checks and whatever
  1642. * actions are appropriate to the state transition in question.
  1643. */
  1644. void
  1645. channel_listener_change_state(channel_listener_t *chan_l,
  1646. channel_listener_state_t to_state)
  1647. {
  1648. channel_listener_state_t from_state;
  1649. unsigned char was_active, is_active;
  1650. tor_assert(chan_l);
  1651. from_state = chan_l->state;
  1652. tor_assert(channel_listener_state_is_valid(from_state));
  1653. tor_assert(channel_listener_state_is_valid(to_state));
  1654. tor_assert(channel_listener_state_can_transition(chan_l->state, to_state));
  1655. /* Check for no-op transitions */
  1656. if (from_state == to_state) {
  1657. log_debug(LD_CHANNEL,
  1658. "Got no-op transition from \"%s\" to itself on channel "
  1659. "listener %p (global ID " U64_FORMAT ")",
  1660. channel_listener_state_to_string(to_state),
  1661. chan_l, U64_PRINTF_ARG(chan_l->global_identifier));
  1662. return;
  1663. }
  1664. /* If we're going to a closing or closed state, we must have a reason set */
  1665. if (to_state == CHANNEL_LISTENER_STATE_CLOSING ||
  1666. to_state == CHANNEL_LISTENER_STATE_CLOSED ||
  1667. to_state == CHANNEL_LISTENER_STATE_ERROR) {
  1668. tor_assert(chan_l->reason_for_closing != CHANNEL_LISTENER_NOT_CLOSING);
  1669. }
  1670. /*
  1671. * We need to maintain the queues here for some transitions:
  1672. * when we enter CHANNEL_STATE_OPEN (especially from CHANNEL_STATE_MAINT)
  1673. * we may have a backlog of cells to transmit, so drain the queues in
  1674. * that case, and when going to CHANNEL_STATE_CLOSED the subclass
  1675. * should have made sure to finish sending things (or gone to
  1676. * CHANNEL_STATE_ERROR if not possible), so we assert for that here.
  1677. */
  1678. log_debug(LD_CHANNEL,
  1679. "Changing state of channel listener %p (global ID " U64_FORMAT
  1680. "from \"%s\" to \"%s\"",
  1681. chan_l, U64_PRINTF_ARG(chan_l->global_identifier),
  1682. channel_listener_state_to_string(chan_l->state),
  1683. channel_listener_state_to_string(to_state));
  1684. chan_l->state = to_state;
  1685. /* Need to add to the right lists if the channel listener is registered */
  1686. if (chan_l->registered) {
  1687. was_active = !(from_state == CHANNEL_LISTENER_STATE_CLOSED ||
  1688. from_state == CHANNEL_LISTENER_STATE_ERROR);
  1689. is_active = !(to_state == CHANNEL_LISTENER_STATE_CLOSED ||
  1690. to_state == CHANNEL_LISTENER_STATE_ERROR);
  1691. /* Need to take off active list and put on finished list? */
  1692. if (was_active && !is_active) {
  1693. if (active_listeners) smartlist_remove(active_listeners, chan_l);
  1694. if (!finished_listeners) finished_listeners = smartlist_new();
  1695. smartlist_add(finished_listeners, chan_l);
  1696. }
  1697. /* Need to put on active list? */
  1698. else if (!was_active && is_active) {
  1699. if (finished_listeners) smartlist_remove(finished_listeners, chan_l);
  1700. if (!active_listeners) active_listeners = smartlist_new();
  1701. smartlist_add(active_listeners, chan_l);
  1702. }
  1703. }
  1704. if (to_state == CHANNEL_LISTENER_STATE_CLOSED ||
  1705. to_state == CHANNEL_LISTENER_STATE_ERROR) {
  1706. /* Assert that the queue is empty */
  1707. tor_assert(!(chan_l->incoming_list) ||
  1708. smartlist_len(chan_l->incoming_list) == 0);
  1709. }
  1710. }
  1711. /**
  1712. * Try to flush cells to the lower layer
  1713. *
  1714. * this is called by the lower layer to indicate that it wants more cells;
  1715. * it will try to write up to num_cells cells from the channel's cell queue or
  1716. * from circuits active on that channel, or as many as it has available if
  1717. * num_cells == -1.
  1718. */
  1719. #define MAX_CELLS_TO_GET_FROM_CIRCUITS_FOR_UNLIMITED 256
  1720. ssize_t
  1721. channel_flush_some_cells(channel_t *chan, ssize_t num_cells)
  1722. {
  1723. unsigned int unlimited = 0;
  1724. ssize_t flushed = 0;
  1725. int num_cells_from_circs, clamped_num_cells;
  1726. tor_assert(chan);
  1727. if (num_cells < 0) unlimited = 1;
  1728. if (!unlimited && num_cells <= flushed) goto done;
  1729. /* If we aren't in CHANNEL_STATE_OPEN, nothing goes through */
  1730. if (chan->state == CHANNEL_STATE_OPEN) {
  1731. /* Try to flush as much as we can that's already queued */
  1732. flushed += channel_flush_some_cells_from_outgoing_queue(chan,
  1733. (unlimited ? -1 : num_cells - flushed));
  1734. if (!unlimited && num_cells <= flushed) goto done;
  1735. if (circuitmux_num_cells(chan->cmux) > 0) {
  1736. /* Calculate number of cells, including clamp */
  1737. if (unlimited) {
  1738. clamped_num_cells = MAX_CELLS_TO_GET_FROM_CIRCUITS_FOR_UNLIMITED;
  1739. } else {
  1740. if (num_cells - flushed >
  1741. MAX_CELLS_TO_GET_FROM_CIRCUITS_FOR_UNLIMITED) {
  1742. clamped_num_cells = MAX_CELLS_TO_GET_FROM_CIRCUITS_FOR_UNLIMITED;
  1743. } else {
  1744. clamped_num_cells = (int)(num_cells - flushed);
  1745. }
  1746. }
  1747. /* Try to get more cells from any active circuits */
  1748. num_cells_from_circs = channel_flush_from_first_active_circuit(
  1749. chan, clamped_num_cells);
  1750. /* If it claims we got some, process the queue again */
  1751. if (num_cells_from_circs > 0) {
  1752. flushed += channel_flush_some_cells_from_outgoing_queue(chan,
  1753. (unlimited ? -1 : num_cells - flushed));
  1754. }
  1755. }
  1756. }
  1757. done:
  1758. return flushed;
  1759. }
  1760. /**
  1761. * Flush cells from just the channel's outgoing cell queue
  1762. *
  1763. * This gets called from channel_flush_some_cells() above to flush cells
  1764. * just from the queue without trying for active_circuits.
  1765. */
  1766. static ssize_t
  1767. channel_flush_some_cells_from_outgoing_queue(channel_t *chan,
  1768. ssize_t num_cells)
  1769. {
  1770. unsigned int unlimited = 0;
  1771. ssize_t flushed = 0;
  1772. cell_queue_entry_t *q = NULL;
  1773. tor_assert(chan);
  1774. tor_assert(chan->write_cell);
  1775. tor_assert(chan->write_packed_cell);
  1776. tor_assert(chan->write_var_cell);
  1777. if (num_cells < 0) unlimited = 1;
  1778. if (!unlimited && num_cells <= flushed) return 0;
  1779. /* If we aren't in CHANNEL_STATE_OPEN, nothing goes through */
  1780. if (chan->state == CHANNEL_STATE_OPEN) {
  1781. while ((unlimited || num_cells > flushed) &&
  1782. NULL != (q = TOR_SIMPLEQ_FIRST(&chan->outgoing_queue))) {
  1783. if (1) {
  1784. /*
  1785. * Okay, we have a good queue entry, try to give it to the lower
  1786. * layer.
  1787. */
  1788. switch (q->type) {
  1789. case CELL_QUEUE_FIXED:
  1790. if (q->u.fixed.cell) {
  1791. if (chan->write_cell(chan,
  1792. q->u.fixed.cell)) {
  1793. ++flushed;
  1794. channel_timestamp_xmit(chan);
  1795. ++(chan->n_cells_xmitted);
  1796. cell_queue_entry_free(q, 1);
  1797. q = NULL;
  1798. }
  1799. /* Else couldn't write it; leave it on the queue */
  1800. } else {
  1801. /* This shouldn't happen */
  1802. log_info(LD_CHANNEL,
  1803. "Saw broken cell queue entry of type CELL_QUEUE_FIXED "
  1804. "with no cell on channel %p "
  1805. "(global ID " U64_FORMAT ").",
  1806. chan, U64_PRINTF_ARG(chan->global_identifier));
  1807. /* Throw it away */
  1808. cell_queue_entry_free(q, 0);
  1809. q = NULL;
  1810. }
  1811. break;
  1812. case CELL_QUEUE_PACKED:
  1813. if (q->u.packed.packed_cell) {
  1814. if (chan->write_packed_cell(chan,
  1815. q->u.packed.packed_cell)) {
  1816. ++flushed;
  1817. channel_timestamp_xmit(chan);
  1818. ++(chan->n_cells_xmitted);
  1819. cell_queue_entry_free(q, 1);
  1820. q = NULL;
  1821. }
  1822. /* Else couldn't write it; leave it on the queue */
  1823. } else {
  1824. /* This shouldn't happen */
  1825. log_info(LD_CHANNEL,
  1826. "Saw broken cell queue entry of type CELL_QUEUE_PACKED "
  1827. "with no cell on channel %p "
  1828. "(global ID " U64_FORMAT ").",
  1829. chan, U64_PRINTF_ARG(chan->global_identifier));
  1830. /* Throw it away */
  1831. cell_queue_entry_free(q, 0);
  1832. q = NULL;
  1833. }
  1834. break;
  1835. case CELL_QUEUE_VAR:
  1836. if (q->u.var.var_cell) {
  1837. if (chan->write_var_cell(chan,
  1838. q->u.var.var_cell)) {
  1839. ++flushed;
  1840. channel_timestamp_xmit(chan);
  1841. ++(chan->n_cells_xmitted);
  1842. cell_queue_entry_free(q, 1);
  1843. q = NULL;
  1844. }
  1845. /* Else couldn't write it; leave it on the queue */
  1846. } else {
  1847. /* This shouldn't happen */
  1848. log_info(LD_CHANNEL,
  1849. "Saw broken cell queue entry of type CELL_QUEUE_VAR "
  1850. "with no cell on channel %p "
  1851. "(global ID " U64_FORMAT ").",
  1852. chan, U64_PRINTF_ARG(chan->global_identifier));
  1853. /* Throw it away */
  1854. cell_queue_entry_free(q, 0);
  1855. q = NULL;
  1856. }
  1857. break;
  1858. default:
  1859. /* Unknown type, log and free it */
  1860. log_info(LD_CHANNEL,
  1861. "Saw an unknown cell queue entry type %d on channel %p "
  1862. "(global ID " U64_FORMAT "; ignoring it."
  1863. " Someone should fix this.",
  1864. q->type, chan, U64_PRINTF_ARG(chan->global_identifier));
  1865. cell_queue_entry_free(q, 0);
  1866. q = NULL;
  1867. }
  1868. /* if q got NULLed out, we used it and should remove the queue entry */
  1869. if (!q) TOR_SIMPLEQ_REMOVE_HEAD(&chan->outgoing_queue, next);
  1870. /* No cell removed from list, so we can't go on any further */
  1871. else break;
  1872. }
  1873. }
  1874. }
  1875. /* Did we drain the queue? */
  1876. if (TOR_SIMPLEQ_EMPTY(&chan->outgoing_queue)) {
  1877. channel_timestamp_drained(chan);
  1878. }
  1879. return flushed;
  1880. }
  1881. /**
  1882. * Flush as many cells as we possibly can from the queue
  1883. *
  1884. * This tries to flush as many cells from the queue as the lower layer
  1885. * will take. It just calls channel_flush_some_cells_from_outgoing_queue()
  1886. * in unlimited mode.
  1887. */
  1888. void
  1889. channel_flush_cells(channel_t *chan)
  1890. {
  1891. channel_flush_some_cells_from_outgoing_queue(chan, -1);
  1892. }
  1893. /**
  1894. * Check if any cells are available
  1895. *
  1896. * This gets used from the lower layer to check if any more cells are
  1897. * available.
  1898. */
  1899. int
  1900. channel_more_to_flush(channel_t *chan)
  1901. {
  1902. tor_assert(chan);
  1903. /* Check if we have any queued */
  1904. if (! TOR_SIMPLEQ_EMPTY(&chan->incoming_queue))
  1905. return 1;
  1906. /* Check if any circuits would like to queue some */
  1907. if (circuitmux_num_cells(chan->cmux) > 0) return 1;
  1908. /* Else no */
  1909. return 0;
  1910. }
  1911. /**
  1912. * Notify the channel we're done flushing the output in the lower layer
  1913. *
  1914. * Connection.c will call this when we've flushed the output; there's some
  1915. * dirreq-related maintenance to do.
  1916. */
  1917. void
  1918. channel_notify_flushed(channel_t *chan)
  1919. {
  1920. tor_assert(chan);
  1921. if (chan->dirreq_id != 0)
  1922. geoip_change_dirreq_state(chan->dirreq_id,
  1923. DIRREQ_TUNNELED,
  1924. DIRREQ_CHANNEL_BUFFER_FLUSHED);
  1925. }
  1926. /**
  1927. * Process the queue of incoming channels on a listener
  1928. *
  1929. * Use a listener's registered callback to process as many entries in the
  1930. * queue of incoming channels as possible.
  1931. */
  1932. void
  1933. channel_listener_process_incoming(channel_listener_t *listener)
  1934. {
  1935. tor_assert(listener);
  1936. /*
  1937. * CHANNEL_LISTENER_STATE_CLOSING permitted because we drain the queue
  1938. * while closing a listener.
  1939. */
  1940. tor_assert(listener->state == CHANNEL_LISTENER_STATE_LISTENING ||
  1941. listener->state == CHANNEL_LISTENER_STATE_CLOSING);
  1942. tor_assert(listener->listener);
  1943. log_debug(LD_CHANNEL,
  1944. "Processing queue of incoming connections for channel "
  1945. "listener %p (global ID " U64_FORMAT ")",
  1946. listener, U64_PRINTF_ARG(listener->global_identifier));
  1947. if (!(listener->incoming_list)) return;
  1948. SMARTLIST_FOREACH_BEGIN(listener->incoming_list,
  1949. channel_t *, chan) {
  1950. tor_assert(chan);
  1951. log_debug(LD_CHANNEL,
  1952. "Handling incoming channel %p (" U64_FORMAT ") "
  1953. "for listener %p (" U64_FORMAT ")",
  1954. chan,
  1955. U64_PRINTF_ARG(chan->global_identifier),
  1956. listener,
  1957. U64_PRINTF_ARG(listener->global_identifier));
  1958. /* Make sure this is set correctly */
  1959. channel_mark_incoming(chan);
  1960. listener->listener(listener, chan);
  1961. } SMARTLIST_FOREACH_END(chan);
  1962. smartlist_free(listener->incoming_list);
  1963. listener->incoming_list = NULL;
  1964. }
  1965. /**
  1966. * Take actions required when a channel becomes open
  1967. *
  1968. * Handle actions we should do when we know a channel is open; a lot of
  1969. * this comes from the old connection_or_set_state_open() of connection_or.c.
  1970. *
  1971. * Because of this mechanism, future channel_t subclasses should take care
  1972. * not to change a channel to from CHANNEL_STATE_OPENING to CHANNEL_STATE_OPEN
  1973. * until there is positive confirmation that the network is operational.
  1974. * In particular, anything UDP-based should not make this transition until a
  1975. * packet is received from the other side.
  1976. */
  1977. void
  1978. channel_do_open_actions(channel_t *chan)
  1979. {
  1980. tor_addr_t remote_addr;
  1981. int started_here, not_using = 0;
  1982. time_t now = time(NULL);
  1983. tor_assert(chan);
  1984. started_here = channel_is_outgoing(chan);
  1985. if (started_here) {
  1986. circuit_build_times_network_is_live(get_circuit_build_times_mutable());
  1987. rep_hist_note_connect_succeeded(chan->identity_digest, now);
  1988. if (entry_guard_register_connect_status(
  1989. chan->identity_digest, 1, 0, now) < 0) {
  1990. /* Close any circuits pending on this channel. We leave it in state
  1991. * 'open' though, because it didn't actually *fail* -- we just
  1992. * chose not to use it. */
  1993. log_debug(LD_OR,
  1994. "New entry guard was reachable, but closing this "
  1995. "connection so we can retry the earlier entry guards.");
  1996. circuit_n_chan_done(chan, 0);
  1997. not_using = 1;
  1998. }
  1999. router_set_status(chan->identity_digest, 1);
  2000. } else {
  2001. /* only report it to the geoip module if it's not a known router */
  2002. if (!router_get_by_id_digest(chan->identity_digest)) {
  2003. if (channel_get_addr_if_possible(chan, &remote_addr)) {
  2004. char *transport_name = NULL;
  2005. if (chan->get_transport_name(chan, &transport_name) < 0)
  2006. transport_name = NULL;
  2007. geoip_note_client_seen(GEOIP_CLIENT_CONNECT,
  2008. &remote_addr, transport_name,
  2009. now);
  2010. tor_free(transport_name);
  2011. }
  2012. /* Otherwise the underlying transport can't tell us this, so skip it */
  2013. }
  2014. }
  2015. if (!not_using) circuit_n_chan_done(chan, 1);
  2016. }
  2017. /**
  2018. * Queue an incoming channel on a listener
  2019. *
  2020. * Internal and subclass use only function to queue an incoming channel from
  2021. * a listener. A subclass of channel_listener_t should call this when a new
  2022. * incoming channel is created.
  2023. */
  2024. void
  2025. channel_listener_queue_incoming(channel_listener_t *listener,
  2026. channel_t *incoming)
  2027. {
  2028. int need_to_queue = 0;
  2029. tor_assert(listener);
  2030. tor_assert(listener->state == CHANNEL_LISTENER_STATE_LISTENING);
  2031. tor_assert(incoming);
  2032. log_debug(LD_CHANNEL,
  2033. "Queueing incoming channel %p (global ID " U64_FORMAT ") on "
  2034. "channel listener %p (global ID " U64_FORMAT ")",
  2035. incoming, U64_PRINTF_ARG(incoming->global_identifier),
  2036. listener, U64_PRINTF_ARG(listener->global_identifier));
  2037. /* Do we need to queue it, or can we just call the listener right away? */
  2038. if (!(listener->listener)) need_to_queue = 1;
  2039. if (listener->incoming_list &&
  2040. (smartlist_len(listener->incoming_list) > 0))
  2041. need_to_queue = 1;
  2042. /* If we need to queue and have no queue, create one */
  2043. if (need_to_queue && !(listener->incoming_list)) {
  2044. listener->incoming_list = smartlist_new();
  2045. }
  2046. /* Bump the counter and timestamp it */
  2047. channel_listener_timestamp_active(listener);
  2048. channel_listener_timestamp_accepted(listener);
  2049. ++(listener->n_accepted);
  2050. /* If we don't need to queue, process it right away */
  2051. if (!need_to_queue) {
  2052. tor_assert(listener->listener);
  2053. listener->listener(listener, incoming);
  2054. }
  2055. /*
  2056. * Otherwise, we need to queue; queue and then process the queue if
  2057. * we can.
  2058. */
  2059. else {
  2060. tor_assert(listener->incoming_list);
  2061. smartlist_add(listener->incoming_list, incoming);
  2062. if (listener->listener) channel_listener_process_incoming(listener);
  2063. }
  2064. }
  2065. /**
  2066. * Process queued incoming cells
  2067. *
  2068. * Process as many queued cells as we can from the incoming
  2069. * cell queue.
  2070. */
  2071. void
  2072. channel_process_cells(channel_t *chan)
  2073. {
  2074. cell_queue_entry_t *q;
  2075. tor_assert(chan);
  2076. tor_assert(chan->state == CHANNEL_STATE_CLOSING ||
  2077. chan->state == CHANNEL_STATE_MAINT ||
  2078. chan->state == CHANNEL_STATE_OPEN);
  2079. log_debug(LD_CHANNEL,
  2080. "Processing as many incoming cells as we can for channel %p",
  2081. chan);
  2082. /* Nothing we can do if we have no registered cell handlers */
  2083. if (!(chan->cell_handler ||
  2084. chan->var_cell_handler)) return;
  2085. /* Nothing we can do if we have no cells */
  2086. if (TOR_SIMPLEQ_EMPTY(&chan->incoming_queue)) return;
  2087. /*
  2088. * Process cells until we're done or find one we have no current handler
  2089. * for.
  2090. */
  2091. while (NULL != (q = TOR_SIMPLEQ_FIRST(&chan->incoming_queue))) {
  2092. tor_assert(q);
  2093. tor_assert(q->type == CELL_QUEUE_FIXED ||
  2094. q->type == CELL_QUEUE_VAR);
  2095. if (q->type == CELL_QUEUE_FIXED &&
  2096. chan->cell_handler) {
  2097. /* Handle a fixed-length cell */
  2098. TOR_SIMPLEQ_REMOVE_HEAD(&chan->incoming_queue, next);
  2099. tor_assert(q->u.fixed.cell);
  2100. log_debug(LD_CHANNEL,
  2101. "Processing incoming cell_t %p for channel %p (global ID "
  2102. U64_FORMAT ")",
  2103. q->u.fixed.cell, chan,
  2104. U64_PRINTF_ARG(chan->global_identifier));
  2105. chan->cell_handler(chan, q->u.fixed.cell);
  2106. tor_free(q);
  2107. } else if (q->type == CELL_QUEUE_VAR &&
  2108. chan->var_cell_handler) {
  2109. /* Handle a variable-length cell */
  2110. TOR_SIMPLEQ_REMOVE_HEAD(&chan->incoming_queue, next);
  2111. tor_assert(q->u.var.var_cell);
  2112. log_debug(LD_CHANNEL,
  2113. "Processing incoming var_cell_t %p for channel %p (global ID "
  2114. U64_FORMAT ")",
  2115. q->u.var.var_cell, chan,
  2116. U64_PRINTF_ARG(chan->global_identifier));
  2117. chan->var_cell_handler(chan, q->u.var.var_cell);
  2118. tor_free(q);
  2119. } else {
  2120. /* Can't handle this one */
  2121. break;
  2122. }
  2123. }
  2124. }
  2125. /**
  2126. * Queue incoming cell
  2127. *
  2128. * This should be called by a channel_t subclass to queue an incoming fixed-
  2129. * length cell for processing, and process it if possible.
  2130. */
  2131. void
  2132. channel_queue_cell(channel_t *chan, cell_t *cell)
  2133. {
  2134. int need_to_queue = 0;
  2135. cell_queue_entry_t *q;
  2136. tor_assert(chan);
  2137. tor_assert(cell);
  2138. tor_assert(chan->state == CHANNEL_STATE_OPEN);
  2139. /* Do we need to queue it, or can we just call the handler right away? */
  2140. if (!(chan->cell_handler)) need_to_queue = 1;
  2141. if (! TOR_SIMPLEQ_EMPTY(&chan->incoming_queue))
  2142. need_to_queue = 1;
  2143. /* Timestamp for receiving */
  2144. channel_timestamp_recv(chan);
  2145. /* Update the counter */
  2146. ++(chan->n_cells_recved);
  2147. /* If we don't need to queue we can just call cell_handler */
  2148. if (!need_to_queue) {
  2149. tor_assert(chan->cell_handler);
  2150. log_debug(LD_CHANNEL,
  2151. "Directly handling incoming cell_t %p for channel %p "
  2152. "(global ID " U64_FORMAT ")",
  2153. cell, chan,
  2154. U64_PRINTF_ARG(chan->global_identifier));
  2155. chan->cell_handler(chan, cell);
  2156. } else {
  2157. /* Otherwise queue it and then process the queue if possible. */
  2158. q = cell_queue_entry_new_fixed(cell);
  2159. log_debug(LD_CHANNEL,
  2160. "Queueing incoming cell_t %p for channel %p "
  2161. "(global ID " U64_FORMAT ")",
  2162. cell, chan,
  2163. U64_PRINTF_ARG(chan->global_identifier));
  2164. TOR_SIMPLEQ_INSERT_TAIL(&chan->incoming_queue, q, next);
  2165. if (chan->cell_handler ||
  2166. chan->var_cell_handler) {
  2167. channel_process_cells(chan);
  2168. }
  2169. }
  2170. }
  2171. /**
  2172. * Queue incoming variable-length cell
  2173. *
  2174. * This should be called by a channel_t subclass to queue an incoming
  2175. * variable-length cell for processing, and process it if possible.
  2176. */
  2177. void
  2178. channel_queue_var_cell(channel_t *chan, var_cell_t *var_cell)
  2179. {
  2180. int need_to_queue = 0;
  2181. cell_queue_entry_t *q;
  2182. tor_assert(chan);
  2183. tor_assert(var_cell);
  2184. tor_assert(chan->state == CHANNEL_STATE_OPEN);
  2185. /* Do we need to queue it, or can we just call the handler right away? */
  2186. if (!(chan->var_cell_handler)) need_to_queue = 1;
  2187. if (! TOR_SIMPLEQ_EMPTY(&chan->incoming_queue))
  2188. need_to_queue = 1;
  2189. /* Timestamp for receiving */
  2190. channel_timestamp_recv(chan);
  2191. /* Update the counter */
  2192. ++(chan->n_cells_recved);
  2193. /* If we don't need to queue we can just call cell_handler */
  2194. if (!need_to_queue) {
  2195. tor_assert(chan->var_cell_handler);
  2196. log_debug(LD_CHANNEL,
  2197. "Directly handling incoming var_cell_t %p for channel %p "
  2198. "(global ID " U64_FORMAT ")",
  2199. var_cell, chan,
  2200. U64_PRINTF_ARG(chan->global_identifier));
  2201. chan->var_cell_handler(chan, var_cell);
  2202. } else {
  2203. /* Otherwise queue it and then process the queue if possible. */
  2204. q = cell_queue_entry_new_var(var_cell);
  2205. log_debug(LD_CHANNEL,
  2206. "Queueing incoming var_cell_t %p for channel %p "
  2207. "(global ID " U64_FORMAT ")",
  2208. var_cell, chan,
  2209. U64_PRINTF_ARG(chan->global_identifier));
  2210. TOR_SIMPLEQ_INSERT_TAIL(&chan->incoming_queue, q, next);
  2211. if (chan->cell_handler ||
  2212. chan->var_cell_handler) {
  2213. channel_process_cells(chan);
  2214. }
  2215. }
  2216. }
  2217. /** If <b>packed_cell</b> on <b>chan</b> is a destroy cell, then set
  2218. * *<b>circid_out</b> to its circuit ID, and return true. Otherwise, return
  2219. * false. */
  2220. /* XXXX Move this function. */
  2221. int
  2222. packed_cell_is_destroy(channel_t *chan,
  2223. const packed_cell_t *packed_cell,
  2224. circid_t *circid_out)
  2225. {
  2226. if (chan->wide_circ_ids) {
  2227. if (packed_cell->body[4] == CELL_DESTROY) {
  2228. *circid_out = ntohl(get_uint32(packed_cell->body));
  2229. return 1;
  2230. }
  2231. } else {
  2232. if (packed_cell->body[2] == CELL_DESTROY) {
  2233. *circid_out = ntohs(get_uint16(packed_cell->body));
  2234. return 1;
  2235. }
  2236. }
  2237. return 0;
  2238. }
  2239. /** DOCDOC */
  2240. static int
  2241. is_destroy_cell(channel_t *chan,
  2242. const cell_queue_entry_t *q, circid_t *circid_out)
  2243. {
  2244. *circid_out = 0;
  2245. switch (q->type) {
  2246. case CELL_QUEUE_FIXED:
  2247. if (q->u.fixed.cell->command == CELL_DESTROY) {
  2248. *circid_out = q->u.fixed.cell->circ_id;
  2249. return 1;
  2250. }
  2251. break;
  2252. case CELL_QUEUE_VAR:
  2253. if (q->u.var.var_cell->command == CELL_DESTROY) {
  2254. *circid_out = q->u.var.var_cell->circ_id;
  2255. return 1;
  2256. }
  2257. break;
  2258. case CELL_QUEUE_PACKED:
  2259. return packed_cell_is_destroy(chan, q->u.packed.packed_cell, circid_out);
  2260. }
  2261. return 0;
  2262. }
  2263. /**
  2264. * Send destroy cell on a channel
  2265. *
  2266. * Write a destroy cell with circ ID <b>circ_id</b> and reason <b>reason</b>
  2267. * onto channel <b>chan</b>. Don't perform range-checking on reason:
  2268. * we may want to propagate reasons from other cells.
  2269. */
  2270. int
  2271. channel_send_destroy(circid_t circ_id, channel_t *chan, int reason)
  2272. {
  2273. tor_assert(chan);
  2274. if (circ_id == 0) {
  2275. log_warn(LD_BUG, "Attempted to send a destroy cell for circID 0 "
  2276. "on a channel " U64_FORMAT " at %p in state %s (%d)",
  2277. U64_PRINTF_ARG(chan->global_identifier),
  2278. chan, channel_state_to_string(chan->state),
  2279. chan->state);
  2280. return 0;
  2281. }
  2282. /* Check to make sure we can send on this channel first */
  2283. if (!(chan->state == CHANNEL_STATE_CLOSING ||
  2284. chan->state == CHANNEL_STATE_CLOSED ||
  2285. chan->state == CHANNEL_STATE_ERROR) &&
  2286. chan->cmux) {
  2287. channel_note_destroy_pending(chan, circ_id);
  2288. circuitmux_append_destroy_cell(chan, chan->cmux, circ_id, reason);
  2289. log_debug(LD_OR,
  2290. "Sending destroy (circID %u) on channel %p "
  2291. "(global ID " U64_FORMAT ")",
  2292. (unsigned)circ_id, chan,
  2293. U64_PRINTF_ARG(chan->global_identifier));
  2294. } else {
  2295. log_warn(LD_BUG,
  2296. "Someone called channel_send_destroy() for circID %u "
  2297. "on a channel " U64_FORMAT " at %p in state %s (%d)",
  2298. (unsigned)circ_id, U64_PRINTF_ARG(chan->global_identifier),
  2299. chan, channel_state_to_string(chan->state),
  2300. chan->state);
  2301. }
  2302. return 0;
  2303. }
  2304. /**
  2305. * Dump channel statistics to the log
  2306. *
  2307. * This is called from dumpstats() in main.c and spams the log with
  2308. * statistics on channels.
  2309. */
  2310. void
  2311. channel_dumpstats(int severity)
  2312. {
  2313. if (all_channels && smartlist_len(all_channels) > 0) {
  2314. tor_log(severity, LD_GENERAL,
  2315. "Dumping statistics about %d channels:",
  2316. smartlist_len(all_channels));
  2317. tor_log(severity, LD_GENERAL,
  2318. "%d are active, and %d are done and waiting for cleanup",
  2319. (active_channels != NULL) ?
  2320. smartlist_len(active_channels) : 0,
  2321. (finished_channels != NULL) ?
  2322. smartlist_len(finished_channels) : 0);
  2323. SMARTLIST_FOREACH(all_channels, channel_t *, chan,
  2324. channel_dump_statistics(chan, severity));
  2325. tor_log(severity, LD_GENERAL,
  2326. "Done spamming about channels now");
  2327. } else {
  2328. tor_log(severity, LD_GENERAL,
  2329. "No channels to dump");
  2330. }
  2331. }
  2332. /**
  2333. * Dump channel listener statistics to the log
  2334. *
  2335. * This is called from dumpstats() in main.c and spams the log with
  2336. * statistics on channel listeners.
  2337. */
  2338. void
  2339. channel_listener_dumpstats(int severity)
  2340. {
  2341. if (all_listeners && smartlist_len(all_listeners) > 0) {
  2342. tor_log(severity, LD_GENERAL,
  2343. "Dumping statistics about %d channel listeners:",
  2344. smartlist_len(all_listeners));
  2345. tor_log(severity, LD_GENERAL,
  2346. "%d are active and %d are done and waiting for cleanup",
  2347. (active_listeners != NULL) ?
  2348. smartlist_len(active_listeners) : 0,
  2349. (finished_listeners != NULL) ?
  2350. smartlist_len(finished_listeners) : 0);
  2351. SMARTLIST_FOREACH(all_listeners, channel_listener_t *, chan_l,
  2352. channel_listener_dump_statistics(chan_l, severity));
  2353. tor_log(severity, LD_GENERAL,
  2354. "Done spamming about channel listeners now");
  2355. } else {
  2356. tor_log(severity, LD_GENERAL,
  2357. "No channel listeners to dump");
  2358. }
  2359. }
  2360. /**
  2361. * Set the cmux policy on all active channels
  2362. */
  2363. void
  2364. channel_set_cmux_policy_everywhere(circuitmux_policy_t *pol)
  2365. {
  2366. if (!active_channels) return;
  2367. SMARTLIST_FOREACH_BEGIN(active_channels, channel_t *, curr) {
  2368. if (curr->cmux) {
  2369. circuitmux_set_policy(curr->cmux, pol);
  2370. }
  2371. } SMARTLIST_FOREACH_END(curr);
  2372. }
  2373. /**
  2374. * Clean up channels
  2375. *
  2376. * This gets called periodically from run_scheduled_events() in main.c;
  2377. * it cleans up after closed channels.
  2378. */
  2379. void
  2380. channel_run_cleanup(void)
  2381. {
  2382. channel_t *tmp = NULL;
  2383. /* Check if we need to do anything */
  2384. if (!finished_channels || smartlist_len(finished_channels) == 0) return;
  2385. /* Iterate through finished_channels and get rid of them */
  2386. SMARTLIST_FOREACH_BEGIN(finished_channels, channel_t *, curr) {
  2387. tmp = curr;
  2388. /* Remove it from the list */
  2389. SMARTLIST_DEL_CURRENT(finished_channels, curr);
  2390. /* Also unregister it */
  2391. channel_unregister(tmp);
  2392. /* ... and free it */
  2393. channel_free(tmp);
  2394. } SMARTLIST_FOREACH_END(curr);
  2395. }
  2396. /**
  2397. * Clean up channel listeners
  2398. *
  2399. * This gets called periodically from run_scheduled_events() in main.c;
  2400. * it cleans up after closed channel listeners.
  2401. */
  2402. void
  2403. channel_listener_run_cleanup(void)
  2404. {
  2405. channel_listener_t *tmp = NULL;
  2406. /* Check if we need to do anything */
  2407. if (!finished_listeners || smartlist_len(finished_listeners) == 0) return;
  2408. /* Iterate through finished_channels and get rid of them */
  2409. SMARTLIST_FOREACH_BEGIN(finished_listeners, channel_listener_t *, curr) {
  2410. tmp = curr;
  2411. /* Remove it from the list */
  2412. SMARTLIST_DEL_CURRENT(finished_listeners, curr);
  2413. /* Also unregister it */
  2414. channel_listener_unregister(tmp);
  2415. /* ... and free it */
  2416. channel_listener_free(tmp);
  2417. } SMARTLIST_FOREACH_END(curr);
  2418. }
  2419. /**
  2420. * Free a list of channels for channel_free_all()
  2421. */
  2422. static void
  2423. channel_free_list(smartlist_t *channels, int mark_for_close)
  2424. {
  2425. if (!channels) return;
  2426. SMARTLIST_FOREACH_BEGIN(channels, channel_t *, curr) {
  2427. /* Deregister and free it */
  2428. tor_assert(curr);
  2429. log_debug(LD_CHANNEL,
  2430. "Cleaning up channel %p (global ID " U64_FORMAT ") "
  2431. "in state %s (%d)",
  2432. curr, U64_PRINTF_ARG(curr->global_identifier),
  2433. channel_state_to_string(curr->state), curr->state);
  2434. /* Detach circuits early so they can find the channel */
  2435. if (curr->cmux) {
  2436. circuitmux_detach_all_circuits(curr->cmux, NULL);
  2437. }
  2438. channel_unregister(curr);
  2439. if (mark_for_close) {
  2440. if (!(curr->state == CHANNEL_STATE_CLOSING ||
  2441. curr->state == CHANNEL_STATE_CLOSED ||
  2442. curr->state == CHANNEL_STATE_ERROR)) {
  2443. channel_mark_for_close(curr);
  2444. }
  2445. channel_force_free(curr);
  2446. } else channel_free(curr);
  2447. } SMARTLIST_FOREACH_END(curr);
  2448. }
  2449. /**
  2450. * Free a list of channel listeners for channel_free_all()
  2451. */
  2452. static void
  2453. channel_listener_free_list(smartlist_t *listeners, int mark_for_close)
  2454. {
  2455. if (!listeners) return;
  2456. SMARTLIST_FOREACH_BEGIN(listeners, channel_listener_t *, curr) {
  2457. /* Deregister and free it */
  2458. tor_assert(curr);
  2459. log_debug(LD_CHANNEL,
  2460. "Cleaning up channel listener %p (global ID " U64_FORMAT ") "
  2461. "in state %s (%d)",
  2462. curr, U64_PRINTF_ARG(curr->global_identifier),
  2463. channel_listener_state_to_string(curr->state), curr->state);
  2464. channel_listener_unregister(curr);
  2465. if (mark_for_close) {
  2466. if (!(curr->state == CHANNEL_LISTENER_STATE_CLOSING ||
  2467. curr->state == CHANNEL_LISTENER_STATE_CLOSED ||
  2468. curr->state == CHANNEL_LISTENER_STATE_ERROR)) {
  2469. channel_listener_mark_for_close(curr);
  2470. }
  2471. channel_listener_force_free(curr);
  2472. } else channel_listener_free(curr);
  2473. } SMARTLIST_FOREACH_END(curr);
  2474. }
  2475. /**
  2476. * Close all channels and free everything
  2477. *
  2478. * This gets called from tor_free_all() in main.c to clean up on exit.
  2479. * It will close all registered channels and free associated storage,
  2480. * then free the all_channels, active_channels, listening_channels and
  2481. * finished_channels lists and also channel_identity_map.
  2482. */
  2483. void
  2484. channel_free_all(void)
  2485. {
  2486. log_debug(LD_CHANNEL,
  2487. "Shutting down channels...");
  2488. /* First, let's go for finished channels */
  2489. if (finished_channels) {
  2490. channel_free_list(finished_channels, 0);
  2491. smartlist_free(finished_channels);
  2492. finished_channels = NULL;
  2493. }
  2494. /* Now the finished listeners */
  2495. if (finished_listeners) {
  2496. channel_listener_free_list(finished_listeners, 0);
  2497. smartlist_free(finished_listeners);
  2498. finished_listeners = NULL;
  2499. }
  2500. /* Now all active channels */
  2501. if (active_channels) {
  2502. channel_free_list(active_channels, 1);
  2503. smartlist_free(active_channels);
  2504. active_channels = NULL;
  2505. }
  2506. /* Now all active listeners */
  2507. if (active_listeners) {
  2508. channel_listener_free_list(active_listeners, 1);
  2509. smartlist_free(active_listeners);
  2510. active_listeners = NULL;
  2511. }
  2512. /* Now all channels, in case any are left over */
  2513. if (all_channels) {
  2514. channel_free_list(all_channels, 1);
  2515. smartlist_free(all_channels);
  2516. all_channels = NULL;
  2517. }
  2518. /* Now all listeners, in case any are left over */
  2519. if (all_listeners) {
  2520. channel_listener_free_list(all_listeners, 1);
  2521. smartlist_free(all_listeners);
  2522. all_listeners = NULL;
  2523. }
  2524. /* Now free channel_identity_map */
  2525. log_debug(LD_CHANNEL,
  2526. "Freeing channel_identity_map");
  2527. /* Geez, anything still left over just won't die ... let it leak then */
  2528. HT_CLEAR(channel_idmap, &channel_identity_map);
  2529. log_debug(LD_CHANNEL,
  2530. "Done cleaning up after channels");
  2531. }
  2532. /**
  2533. * Connect to a given addr/port/digest
  2534. *
  2535. * This sets up a new outgoing channel; in the future if multiple
  2536. * channel_t subclasses are available, this is where the selection policy
  2537. * should go. It may also be desirable to fold port into tor_addr_t
  2538. * or make a new type including a tor_addr_t and port, so we have a
  2539. * single abstract object encapsulating all the protocol details of
  2540. * how to contact an OR.
  2541. */
  2542. channel_t *
  2543. channel_connect(const tor_addr_t *addr, uint16_t port,
  2544. const char *id_digest)
  2545. {
  2546. return channel_tls_connect(addr, port, id_digest);
  2547. }
  2548. /**
  2549. * Decide which of two channels to prefer for extending a circuit
  2550. *
  2551. * This function is called while extending a circuit and returns true iff
  2552. * a is 'better' than b. The most important criterion here is that a
  2553. * canonical channel is always better than a non-canonical one, but the
  2554. * number of circuits and the age are used as tie-breakers.
  2555. *
  2556. * This is based on the former connection_or_is_better() of connection_or.c
  2557. */
  2558. int
  2559. channel_is_better(time_t now, channel_t *a, channel_t *b,
  2560. int forgive_new_connections)
  2561. {
  2562. int a_grace, b_grace;
  2563. int a_is_canonical, b_is_canonical;
  2564. int a_has_circs, b_has_circs;
  2565. /*
  2566. * Do not definitively deprecate a new channel with no circuits on it
  2567. * until this much time has passed.
  2568. */
  2569. #define NEW_CHAN_GRACE_PERIOD (15*60)
  2570. tor_assert(a);
  2571. tor_assert(b);
  2572. /* Check if one is canonical and the other isn't first */
  2573. a_is_canonical = channel_is_canonical(a);
  2574. b_is_canonical = channel_is_canonical(b);
  2575. if (a_is_canonical && !b_is_canonical) return 1;
  2576. if (!a_is_canonical && b_is_canonical) return 0;
  2577. /*
  2578. * Okay, if we're here they tied on canonicity. Next we check if
  2579. * they have any circuits, and if one does and the other doesn't,
  2580. * we prefer the one that does, unless we are forgiving and the
  2581. * one that has no circuits is in its grace period.
  2582. */
  2583. a_has_circs = (channel_num_circuits(a) > 0);
  2584. b_has_circs = (channel_num_circuits(b) > 0);
  2585. a_grace = (forgive_new_connections &&
  2586. (now < channel_when_created(a) + NEW_CHAN_GRACE_PERIOD));
  2587. b_grace = (forgive_new_connections &&
  2588. (now < channel_when_created(b) + NEW_CHAN_GRACE_PERIOD));
  2589. if (a_has_circs && !b_has_circs && !b_grace) return 1;
  2590. if (!a_has_circs && b_has_circs && !a_grace) return 0;
  2591. /* They tied on circuits too; just prefer whichever is newer */
  2592. if (channel_when_created(a) > channel_when_created(b)) return 1;
  2593. else return 0;
  2594. }
  2595. /**
  2596. * Get a channel to extend a circuit
  2597. *
  2598. * Pick a suitable channel to extend a circuit to given the desired digest
  2599. * the address we believe is correct for that digest; this tries to see
  2600. * if we already have one for the requested endpoint, but if there is no good
  2601. * channel, set *msg_out to a message describing the channel's state
  2602. * and our next action, and set *launch_out to a boolean indicated whether
  2603. * the caller should try to launch a new channel with channel_connect().
  2604. */
  2605. channel_t *
  2606. channel_get_for_extend(const char *digest,
  2607. const tor_addr_t *target_addr,
  2608. const char **msg_out,
  2609. int *launch_out)
  2610. {
  2611. channel_t *chan, *best = NULL;
  2612. int n_inprogress_goodaddr = 0, n_old = 0;
  2613. int n_noncanonical = 0, n_possible = 0;
  2614. time_t now = approx_time();
  2615. tor_assert(msg_out);
  2616. tor_assert(launch_out);
  2617. chan = channel_find_by_remote_digest(digest);
  2618. /* Walk the list, unrefing the old one and refing the new at each
  2619. * iteration.
  2620. */
  2621. for (; chan; chan = channel_next_with_digest(chan)) {
  2622. tor_assert(tor_memeq(chan->identity_digest,
  2623. digest, DIGEST_LEN));
  2624. if (chan->state == CHANNEL_STATE_CLOSING ||
  2625. chan->state == CHANNEL_STATE_CLOSED ||
  2626. chan->state == CHANNEL_STATE_ERROR)
  2627. continue;
  2628. /* Never return a channel on which the other end appears to be
  2629. * a client. */
  2630. if (channel_is_client(chan)) {
  2631. continue;
  2632. }
  2633. /* Never return a non-open connection. */
  2634. if (chan->state != CHANNEL_STATE_OPEN) {
  2635. /* If the address matches, don't launch a new connection for this
  2636. * circuit. */
  2637. if (channel_matches_target_addr_for_extend(chan, target_addr))
  2638. ++n_inprogress_goodaddr;
  2639. continue;
  2640. }
  2641. /* Never return a connection that shouldn't be used for circs. */
  2642. if (channel_is_bad_for_new_circs(chan)) {
  2643. ++n_old;
  2644. continue;
  2645. }
  2646. /* Never return a non-canonical connection using a recent link protocol
  2647. * if the address is not what we wanted.
  2648. *
  2649. * The channel_is_canonical_is_reliable() function asks the lower layer
  2650. * if we should trust channel_is_canonical(). The below is from the
  2651. * comments of the old circuit_or_get_for_extend() and applies when
  2652. * the lower-layer transport is channel_tls_t.
  2653. *
  2654. * (For old link protocols, we can't rely on is_canonical getting
  2655. * set properly if we're talking to the right address, since we might
  2656. * have an out-of-date descriptor, and we will get no NETINFO cell to
  2657. * tell us about the right address.)
  2658. */
  2659. if (!channel_is_canonical(chan) &&
  2660. channel_is_canonical_is_reliable(chan) &&
  2661. !channel_matches_target_addr_for_extend(chan, target_addr)) {
  2662. ++n_noncanonical;
  2663. continue;
  2664. }
  2665. ++n_possible;
  2666. if (!best) {
  2667. best = chan; /* If we have no 'best' so far, this one is good enough. */
  2668. continue;
  2669. }
  2670. if (channel_is_better(now, chan, best, 0))
  2671. best = chan;
  2672. }
  2673. if (best) {
  2674. *msg_out = "Connection is fine; using it.";
  2675. *launch_out = 0;
  2676. return best;
  2677. } else if (n_inprogress_goodaddr) {
  2678. *msg_out = "Connection in progress; waiting.";
  2679. *launch_out = 0;
  2680. return NULL;
  2681. } else if (n_old || n_noncanonical) {
  2682. *msg_out = "Connections all too old, or too non-canonical. "
  2683. " Launching a new one.";
  2684. *launch_out = 1;
  2685. return NULL;
  2686. } else {
  2687. *msg_out = "Not connected. Connecting.";
  2688. *launch_out = 1;
  2689. return NULL;
  2690. }
  2691. }
  2692. /**
  2693. * Describe the transport subclass for a channel
  2694. *
  2695. * Invoke a method to get a string description of the lower-layer
  2696. * transport for this channel.
  2697. */
  2698. const char *
  2699. channel_describe_transport(channel_t *chan)
  2700. {
  2701. tor_assert(chan);
  2702. tor_assert(chan->describe_transport);
  2703. return chan->describe_transport(chan);
  2704. }
  2705. /**
  2706. * Describe the transport subclass for a channel listener
  2707. *
  2708. * Invoke a method to get a string description of the lower-layer
  2709. * transport for this channel listener.
  2710. */
  2711. const char *
  2712. channel_listener_describe_transport(channel_listener_t *chan_l)
  2713. {
  2714. tor_assert(chan_l);
  2715. tor_assert(chan_l->describe_transport);
  2716. return chan_l->describe_transport(chan_l);
  2717. }
  2718. /**
  2719. * Return the number of entries in <b>queue</b>
  2720. */
  2721. static int
  2722. chan_cell_queue_len(const chan_cell_queue_t *queue)
  2723. {
  2724. int r = 0;
  2725. cell_queue_entry_t *cell;
  2726. TOR_SIMPLEQ_FOREACH(cell, queue, next)
  2727. ++r;
  2728. return r;
  2729. }
  2730. /**
  2731. * Dump channel statistics
  2732. *
  2733. * Dump statistics for one channel to the log
  2734. */
  2735. void
  2736. channel_dump_statistics(channel_t *chan, int severity)
  2737. {
  2738. double avg, interval, age;
  2739. time_t now = time(NULL);
  2740. tor_addr_t remote_addr;
  2741. int have_remote_addr;
  2742. char *remote_addr_str;
  2743. tor_assert(chan);
  2744. age = (double)(now - chan->timestamp_created);
  2745. tor_log(severity, LD_GENERAL,
  2746. "Channel " U64_FORMAT " (at %p) with transport %s is in state "
  2747. "%s (%d)",
  2748. U64_PRINTF_ARG(chan->global_identifier), chan,
  2749. channel_describe_transport(chan),
  2750. channel_state_to_string(chan->state), chan->state);
  2751. tor_log(severity, LD_GENERAL,
  2752. " * Channel " U64_FORMAT " was created at " U64_FORMAT
  2753. " (" U64_FORMAT " seconds ago) "
  2754. "and last active at " U64_FORMAT " (" U64_FORMAT " seconds ago)",
  2755. U64_PRINTF_ARG(chan->global_identifier),
  2756. U64_PRINTF_ARG(chan->timestamp_created),
  2757. U64_PRINTF_ARG(now - chan->timestamp_created),
  2758. U64_PRINTF_ARG(chan->timestamp_active),
  2759. U64_PRINTF_ARG(now - chan->timestamp_active));
  2760. /* Handle digest and nickname */
  2761. if (!tor_digest_is_zero(chan->identity_digest)) {
  2762. if (chan->nickname) {
  2763. tor_log(severity, LD_GENERAL,
  2764. " * Channel " U64_FORMAT " says it is connected "
  2765. "to an OR with digest %s and nickname %s",
  2766. U64_PRINTF_ARG(chan->global_identifier),
  2767. hex_str(chan->identity_digest, DIGEST_LEN),
  2768. chan->nickname);
  2769. } else {
  2770. tor_log(severity, LD_GENERAL,
  2771. " * Channel " U64_FORMAT " says it is connected "
  2772. "to an OR with digest %s and no known nickname",
  2773. U64_PRINTF_ARG(chan->global_identifier),
  2774. hex_str(chan->identity_digest, DIGEST_LEN));
  2775. }
  2776. } else {
  2777. if (chan->nickname) {
  2778. tor_log(severity, LD_GENERAL,
  2779. " * Channel " U64_FORMAT " does not know the digest"
  2780. " of the OR it is connected to, but reports its nickname is %s",
  2781. U64_PRINTF_ARG(chan->global_identifier),
  2782. chan->nickname);
  2783. } else {
  2784. tor_log(severity, LD_GENERAL,
  2785. " * Channel " U64_FORMAT " does not know the digest"
  2786. " or the nickname of the OR it is connected to",
  2787. U64_PRINTF_ARG(chan->global_identifier));
  2788. }
  2789. }
  2790. /* Handle remote address and descriptions */
  2791. have_remote_addr = channel_get_addr_if_possible(chan, &remote_addr);
  2792. if (have_remote_addr) {
  2793. char *actual = tor_strdup(channel_get_actual_remote_descr(chan));
  2794. remote_addr_str = tor_dup_addr(&remote_addr);
  2795. tor_log(severity, LD_GENERAL,
  2796. " * Channel " U64_FORMAT " says its remote address"
  2797. " is %s, and gives a canonical description of \"%s\" and an "
  2798. "actual description of \"%s\"",
  2799. U64_PRINTF_ARG(chan->global_identifier),
  2800. safe_str(remote_addr_str),
  2801. safe_str(channel_get_canonical_remote_descr(chan)),
  2802. safe_str(actual));
  2803. tor_free(remote_addr_str);
  2804. tor_free(actual);
  2805. } else {
  2806. char *actual = tor_strdup(channel_get_actual_remote_descr(chan));
  2807. tor_log(severity, LD_GENERAL,
  2808. " * Channel " U64_FORMAT " does not know its remote "
  2809. "address, but gives a canonical description of \"%s\" and an "
  2810. "actual description of \"%s\"",
  2811. U64_PRINTF_ARG(chan->global_identifier),
  2812. channel_get_canonical_remote_descr(chan),
  2813. actual);
  2814. tor_free(actual);
  2815. }
  2816. /* Handle marks */
  2817. tor_log(severity, LD_GENERAL,
  2818. " * Channel " U64_FORMAT " has these marks: %s %s %s "
  2819. "%s %s %s",
  2820. U64_PRINTF_ARG(chan->global_identifier),
  2821. channel_is_bad_for_new_circs(chan) ?
  2822. "bad_for_new_circs" : "!bad_for_new_circs",
  2823. channel_is_canonical(chan) ?
  2824. "canonical" : "!canonical",
  2825. channel_is_canonical_is_reliable(chan) ?
  2826. "is_canonical_is_reliable" :
  2827. "!is_canonical_is_reliable",
  2828. channel_is_client(chan) ?
  2829. "client" : "!client",
  2830. channel_is_local(chan) ?
  2831. "local" : "!local",
  2832. channel_is_incoming(chan) ?
  2833. "incoming" : "outgoing");
  2834. /* Describe queues */
  2835. tor_log(severity, LD_GENERAL,
  2836. " * Channel " U64_FORMAT " has %d queued incoming cells"
  2837. " and %d queued outgoing cells",
  2838. U64_PRINTF_ARG(chan->global_identifier),
  2839. chan_cell_queue_len(&chan->incoming_queue),
  2840. chan_cell_queue_len(&chan->outgoing_queue));
  2841. /* Describe circuits */
  2842. tor_log(severity, LD_GENERAL,
  2843. " * Channel " U64_FORMAT " has %d active circuits out of"
  2844. " %d in total",
  2845. U64_PRINTF_ARG(chan->global_identifier),
  2846. (chan->cmux != NULL) ?
  2847. circuitmux_num_active_circuits(chan->cmux) : 0,
  2848. (chan->cmux != NULL) ?
  2849. circuitmux_num_circuits(chan->cmux) : 0);
  2850. /* Describe timestamps */
  2851. tor_log(severity, LD_GENERAL,
  2852. " * Channel " U64_FORMAT " was last used by a "
  2853. "client at " U64_FORMAT " (" U64_FORMAT " seconds ago)",
  2854. U64_PRINTF_ARG(chan->global_identifier),
  2855. U64_PRINTF_ARG(chan->timestamp_client),
  2856. U64_PRINTF_ARG(now - chan->timestamp_client));
  2857. tor_log(severity, LD_GENERAL,
  2858. " * Channel " U64_FORMAT " was last drained at "
  2859. U64_FORMAT " (" U64_FORMAT " seconds ago)",
  2860. U64_PRINTF_ARG(chan->global_identifier),
  2861. U64_PRINTF_ARG(chan->timestamp_drained),
  2862. U64_PRINTF_ARG(now - chan->timestamp_drained));
  2863. tor_log(severity, LD_GENERAL,
  2864. " * Channel " U64_FORMAT " last received a cell "
  2865. "at " U64_FORMAT " (" U64_FORMAT " seconds ago)",
  2866. U64_PRINTF_ARG(chan->global_identifier),
  2867. U64_PRINTF_ARG(chan->timestamp_recv),
  2868. U64_PRINTF_ARG(now - chan->timestamp_recv));
  2869. tor_log(severity, LD_GENERAL,
  2870. " * Channel " U64_FORMAT " last transmitted a cell "
  2871. "at " U64_FORMAT " (" U64_FORMAT " seconds ago)",
  2872. U64_PRINTF_ARG(chan->global_identifier),
  2873. U64_PRINTF_ARG(chan->timestamp_xmit),
  2874. U64_PRINTF_ARG(now - chan->timestamp_xmit));
  2875. /* Describe counters and rates */
  2876. tor_log(severity, LD_GENERAL,
  2877. " * Channel " U64_FORMAT " has received "
  2878. U64_FORMAT " cells and transmitted " U64_FORMAT,
  2879. U64_PRINTF_ARG(chan->global_identifier),
  2880. U64_PRINTF_ARG(chan->n_cells_recved),
  2881. U64_PRINTF_ARG(chan->n_cells_xmitted));
  2882. if (now > chan->timestamp_created &&
  2883. chan->timestamp_created > 0) {
  2884. if (chan->n_cells_recved > 0) {
  2885. avg = (double)(chan->n_cells_recved) / age;
  2886. if (avg >= 1.0) {
  2887. tor_log(severity, LD_GENERAL,
  2888. " * Channel " U64_FORMAT " has averaged %f "
  2889. "cells received per second",
  2890. U64_PRINTF_ARG(chan->global_identifier), avg);
  2891. } else if (avg >= 0.0) {
  2892. interval = 1.0 / avg;
  2893. tor_log(severity, LD_GENERAL,
  2894. " * Channel " U64_FORMAT " has averaged %f "
  2895. "seconds between received cells",
  2896. U64_PRINTF_ARG(chan->global_identifier), interval);
  2897. }
  2898. }
  2899. if (chan->n_cells_xmitted > 0) {
  2900. avg = (double)(chan->n_cells_xmitted) / age;
  2901. if (avg >= 1.0) {
  2902. tor_log(severity, LD_GENERAL,
  2903. " * Channel " U64_FORMAT " has averaged %f "
  2904. "cells transmitted per second",
  2905. U64_PRINTF_ARG(chan->global_identifier), avg);
  2906. } else if (avg >= 0.0) {
  2907. interval = 1.0 / avg;
  2908. tor_log(severity, LD_GENERAL,
  2909. " * Channel " U64_FORMAT " has averaged %f "
  2910. "seconds between transmitted cells",
  2911. U64_PRINTF_ARG(chan->global_identifier), interval);
  2912. }
  2913. }
  2914. }
  2915. /* Dump anything the lower layer has to say */
  2916. channel_dump_transport_statistics(chan, severity);
  2917. }
  2918. /**
  2919. * Dump channel listener statistics
  2920. *
  2921. * Dump statistics for one channel listener to the log
  2922. */
  2923. void
  2924. channel_listener_dump_statistics(channel_listener_t *chan_l, int severity)
  2925. {
  2926. double avg, interval, age;
  2927. time_t now = time(NULL);
  2928. tor_assert(chan_l);
  2929. age = (double)(now - chan_l->timestamp_created);
  2930. tor_log(severity, LD_GENERAL,
  2931. "Channel listener " U64_FORMAT " (at %p) with transport %s is in "
  2932. "state %s (%d)",
  2933. U64_PRINTF_ARG(chan_l->global_identifier), chan_l,
  2934. channel_listener_describe_transport(chan_l),
  2935. channel_listener_state_to_string(chan_l->state), chan_l->state);
  2936. tor_log(severity, LD_GENERAL,
  2937. " * Channel listener " U64_FORMAT " was created at " U64_FORMAT
  2938. " (" U64_FORMAT " seconds ago) "
  2939. "and last active at " U64_FORMAT " (" U64_FORMAT " seconds ago)",
  2940. U64_PRINTF_ARG(chan_l->global_identifier),
  2941. U64_PRINTF_ARG(chan_l->timestamp_created),
  2942. U64_PRINTF_ARG(now - chan_l->timestamp_created),
  2943. U64_PRINTF_ARG(chan_l->timestamp_active),
  2944. U64_PRINTF_ARG(now - chan_l->timestamp_active));
  2945. tor_log(severity, LD_GENERAL,
  2946. " * Channel listener " U64_FORMAT " last accepted an incoming "
  2947. "channel at " U64_FORMAT " (" U64_FORMAT " seconds ago) "
  2948. "and has accepted " U64_FORMAT " channels in total",
  2949. U64_PRINTF_ARG(chan_l->global_identifier),
  2950. U64_PRINTF_ARG(chan_l->timestamp_accepted),
  2951. U64_PRINTF_ARG(now - chan_l->timestamp_accepted),
  2952. U64_PRINTF_ARG(chan_l->n_accepted));
  2953. /*
  2954. * If it's sensible to do so, get the rate of incoming channels on this
  2955. * listener
  2956. */
  2957. if (now > chan_l->timestamp_created &&
  2958. chan_l->timestamp_created > 0 &&
  2959. chan_l->n_accepted > 0) {
  2960. avg = (double)(chan_l->n_accepted) / age;
  2961. if (avg >= 1.0) {
  2962. tor_log(severity, LD_GENERAL,
  2963. " * Channel listener " U64_FORMAT " has averaged %f incoming "
  2964. "channels per second",
  2965. U64_PRINTF_ARG(chan_l->global_identifier), avg);
  2966. } else if (avg >= 0.0) {
  2967. interval = 1.0 / avg;
  2968. tor_log(severity, LD_GENERAL,
  2969. " * Channel listener " U64_FORMAT " has averaged %f seconds "
  2970. "between incoming channels",
  2971. U64_PRINTF_ARG(chan_l->global_identifier), interval);
  2972. }
  2973. }
  2974. /* Dump anything the lower layer has to say */
  2975. channel_listener_dump_transport_statistics(chan_l, severity);
  2976. }
  2977. /**
  2978. * Invoke transport-specific stats dump for channel
  2979. *
  2980. * If there is a lower-layer statistics dump method, invoke it
  2981. */
  2982. void
  2983. channel_dump_transport_statistics(channel_t *chan, int severity)
  2984. {
  2985. tor_assert(chan);
  2986. if (chan->dumpstats) chan->dumpstats(chan, severity);
  2987. }
  2988. /**
  2989. * Invoke transport-specific stats dump for channel listener
  2990. *
  2991. * If there is a lower-layer statistics dump method, invoke it
  2992. */
  2993. void
  2994. channel_listener_dump_transport_statistics(channel_listener_t *chan_l,
  2995. int severity)
  2996. {
  2997. tor_assert(chan_l);
  2998. if (chan_l->dumpstats) chan_l->dumpstats(chan_l, severity);
  2999. }
  3000. /**
  3001. * Return text description of the remote endpoint
  3002. *
  3003. * This function return a test provided by the lower layer of the remote
  3004. * endpoint for this channel; it should specify the actual address connected
  3005. * to/from.
  3006. *
  3007. * Subsequent calls to channel_get_{actual,canonical}_remote_{address,descr}
  3008. * may invalidate the return value from this function.
  3009. */
  3010. const char *
  3011. channel_get_actual_remote_descr(channel_t *chan)
  3012. {
  3013. tor_assert(chan);
  3014. tor_assert(chan->get_remote_descr);
  3015. /* Param 1 indicates the actual description */
  3016. return chan->get_remote_descr(chan, GRD_FLAG_ORIGINAL);
  3017. }
  3018. /**
  3019. * Return the text address of the remote endpoint.
  3020. *
  3021. * Subsequent calls to channel_get_{actual,canonical}_remote_{address,descr}
  3022. * may invalidate the return value from this function.
  3023. */
  3024. const char *
  3025. channel_get_actual_remote_address(channel_t *chan)
  3026. {
  3027. /* Param 1 indicates the actual description */
  3028. return chan->get_remote_descr(chan, GRD_FLAG_ORIGINAL|GRD_FLAG_ADDR_ONLY);
  3029. }
  3030. /**
  3031. * Return text description of the remote endpoint canonical address
  3032. *
  3033. * This function return a test provided by the lower layer of the remote
  3034. * endpoint for this channel; it should use the known canonical address for
  3035. * this OR's identity digest if possible.
  3036. *
  3037. * Subsequent calls to channel_get_{actual,canonical}_remote_{address,descr}
  3038. * may invalidate the return value from this function.
  3039. */
  3040. const char *
  3041. channel_get_canonical_remote_descr(channel_t *chan)
  3042. {
  3043. tor_assert(chan);
  3044. tor_assert(chan->get_remote_descr);
  3045. /* Param 0 indicates the canonicalized description */
  3046. return chan->get_remote_descr(chan, 0);
  3047. }
  3048. /**
  3049. * Get remote address if possible.
  3050. *
  3051. * Write the remote address out to a tor_addr_t if the underlying transport
  3052. * supports this operation, and return 1. Return 0 if the underlying transport
  3053. * doesn't let us do this.
  3054. */
  3055. int
  3056. channel_get_addr_if_possible(channel_t *chan, tor_addr_t *addr_out)
  3057. {
  3058. tor_assert(chan);
  3059. tor_assert(addr_out);
  3060. if (chan->get_remote_addr)
  3061. return chan->get_remote_addr(chan, addr_out);
  3062. /* Else no support, method not implemented */
  3063. else return 0;
  3064. }
  3065. /**
  3066. * Check if there are outgoing queue writes on this channel
  3067. *
  3068. * Indicate if either we have queued cells, or if not, whether the underlying
  3069. * lower-layer transport thinks it has an output queue.
  3070. */
  3071. int
  3072. channel_has_queued_writes(channel_t *chan)
  3073. {
  3074. int has_writes = 0;
  3075. tor_assert(chan);
  3076. tor_assert(chan->has_queued_writes);
  3077. if (! TOR_SIMPLEQ_EMPTY(&chan->outgoing_queue)) {
  3078. has_writes = 1;
  3079. } else {
  3080. /* Check with the lower layer */
  3081. has_writes = chan->has_queued_writes(chan);
  3082. }
  3083. return has_writes;
  3084. }
  3085. /**
  3086. * Check the is_bad_for_new_circs flag
  3087. *
  3088. * This function returns the is_bad_for_new_circs flag of the specified
  3089. * channel.
  3090. */
  3091. int
  3092. channel_is_bad_for_new_circs(channel_t *chan)
  3093. {
  3094. tor_assert(chan);
  3095. return chan->is_bad_for_new_circs;
  3096. }
  3097. /**
  3098. * Mark a channel as bad for new circuits
  3099. *
  3100. * Set the is_bad_for_new_circs_flag on chan.
  3101. */
  3102. void
  3103. channel_mark_bad_for_new_circs(channel_t *chan)
  3104. {
  3105. tor_assert(chan);
  3106. chan->is_bad_for_new_circs = 1;
  3107. }
  3108. /**
  3109. * Get the client flag
  3110. *
  3111. * This returns the client flag of a channel, which will be set if
  3112. * command_process_create_cell() in command.c thinks this is a connection
  3113. * from a client.
  3114. */
  3115. int
  3116. channel_is_client(channel_t *chan)
  3117. {
  3118. tor_assert(chan);
  3119. return chan->is_client;
  3120. }
  3121. /**
  3122. * Set the client flag
  3123. *
  3124. * Mark a channel as being from a client
  3125. */
  3126. void
  3127. channel_mark_client(channel_t *chan)
  3128. {
  3129. tor_assert(chan);
  3130. chan->is_client = 1;
  3131. }
  3132. /**
  3133. * Get the canonical flag for a channel
  3134. *
  3135. * This returns the is_canonical for a channel; this flag is determined by
  3136. * the lower layer and can't be set in a transport-independent way.
  3137. */
  3138. int
  3139. channel_is_canonical(channel_t *chan)
  3140. {
  3141. tor_assert(chan);
  3142. tor_assert(chan->is_canonical);
  3143. return chan->is_canonical(chan, 0);
  3144. }
  3145. /**
  3146. * Test if the canonical flag is reliable
  3147. *
  3148. * This function asks if the lower layer thinks it's safe to trust the
  3149. * result of channel_is_canonical()
  3150. */
  3151. int
  3152. channel_is_canonical_is_reliable(channel_t *chan)
  3153. {
  3154. tor_assert(chan);
  3155. tor_assert(chan->is_canonical);
  3156. return chan->is_canonical(chan, 1);
  3157. }
  3158. /**
  3159. * Test incoming flag
  3160. *
  3161. * This function gets the incoming flag; this is set when a listener spawns
  3162. * a channel. If this returns true the channel was remotely initiated.
  3163. */
  3164. int
  3165. channel_is_incoming(channel_t *chan)
  3166. {
  3167. tor_assert(chan);
  3168. return chan->is_incoming;
  3169. }
  3170. /**
  3171. * Set the incoming flag
  3172. *
  3173. * This function is called when a channel arrives on a listening channel
  3174. * to mark it as incoming.
  3175. */
  3176. void
  3177. channel_mark_incoming(channel_t *chan)
  3178. {
  3179. tor_assert(chan);
  3180. chan->is_incoming = 1;
  3181. }
  3182. /**
  3183. * Test local flag
  3184. *
  3185. * This function gets the local flag; the lower layer should set this when
  3186. * setting up the channel if is_local_addr() is true for all of the
  3187. * destinations it will communicate with on behalf of this channel. It's
  3188. * used to decide whether to declare the network reachable when seeing incoming
  3189. * traffic on the channel.
  3190. */
  3191. int
  3192. channel_is_local(channel_t *chan)
  3193. {
  3194. tor_assert(chan);
  3195. return chan->is_local;
  3196. }
  3197. /**
  3198. * Set the local flag
  3199. *
  3200. * This internal-only function should be called by the lower layer if the
  3201. * channel is to a local address. See channel_is_local() above or the
  3202. * description of the is_local bit in channel.h
  3203. */
  3204. void
  3205. channel_mark_local(channel_t *chan)
  3206. {
  3207. tor_assert(chan);
  3208. chan->is_local = 1;
  3209. }
  3210. /**
  3211. * Test outgoing flag
  3212. *
  3213. * This function gets the outgoing flag; this is the inverse of the incoming
  3214. * bit set when a listener spawns a channel. If this returns true the channel
  3215. * was locally initiated.
  3216. */
  3217. int
  3218. channel_is_outgoing(channel_t *chan)
  3219. {
  3220. tor_assert(chan);
  3221. return !(chan->is_incoming);
  3222. }
  3223. /**
  3224. * Mark a channel as outgoing
  3225. *
  3226. * This function clears the incoming flag and thus marks a channel as
  3227. * outgoing.
  3228. */
  3229. void
  3230. channel_mark_outgoing(channel_t *chan)
  3231. {
  3232. tor_assert(chan);
  3233. chan->is_incoming = 0;
  3234. }
  3235. /*********************
  3236. * Timestamp updates *
  3237. ********************/
  3238. /**
  3239. * Update the created timestamp for a channel
  3240. *
  3241. * This updates the channel's created timestamp and should only be called
  3242. * from channel_init().
  3243. */
  3244. void
  3245. channel_timestamp_created(channel_t *chan)
  3246. {
  3247. time_t now = time(NULL);
  3248. tor_assert(chan);
  3249. chan->timestamp_created = now;
  3250. }
  3251. /**
  3252. * Update the created timestamp for a channel listener
  3253. *
  3254. * This updates the channel listener's created timestamp and should only be
  3255. * called from channel_init_listener().
  3256. */
  3257. void
  3258. channel_listener_timestamp_created(channel_listener_t *chan_l)
  3259. {
  3260. time_t now = time(NULL);
  3261. tor_assert(chan_l);
  3262. chan_l->timestamp_created = now;
  3263. }
  3264. /**
  3265. * Update the last active timestamp for a channel
  3266. *
  3267. * This function updates the channel's last active timestamp; it should be
  3268. * called by the lower layer whenever there is activity on the channel which
  3269. * does not lead to a cell being transmitted or received; the active timestamp
  3270. * is also updated from channel_timestamp_recv() and channel_timestamp_xmit(),
  3271. * but it should be updated for things like the v3 handshake and stuff that
  3272. * produce activity only visible to the lower layer.
  3273. */
  3274. void
  3275. channel_timestamp_active(channel_t *chan)
  3276. {
  3277. time_t now = time(NULL);
  3278. tor_assert(chan);
  3279. chan->timestamp_active = now;
  3280. }
  3281. /**
  3282. * Update the last active timestamp for a channel listener
  3283. */
  3284. void
  3285. channel_listener_timestamp_active(channel_listener_t *chan_l)
  3286. {
  3287. time_t now = time(NULL);
  3288. tor_assert(chan_l);
  3289. chan_l->timestamp_active = now;
  3290. }
  3291. /**
  3292. * Update the last accepted timestamp.
  3293. *
  3294. * This function updates the channel listener's last accepted timestamp; it
  3295. * should be called whenever a new incoming channel is accepted on a
  3296. * listener.
  3297. */
  3298. void
  3299. channel_listener_timestamp_accepted(channel_listener_t *chan_l)
  3300. {
  3301. time_t now = time(NULL);
  3302. tor_assert(chan_l);
  3303. chan_l->timestamp_active = now;
  3304. chan_l->timestamp_accepted = now;
  3305. }
  3306. /**
  3307. * Update client timestamp
  3308. *
  3309. * This function is called by relay.c to timestamp a channel that appears to
  3310. * be used as a client.
  3311. */
  3312. void
  3313. channel_timestamp_client(channel_t *chan)
  3314. {
  3315. time_t now = time(NULL);
  3316. tor_assert(chan);
  3317. chan->timestamp_client = now;
  3318. }
  3319. /**
  3320. * Update the last drained timestamp
  3321. *
  3322. * This is called whenever we transmit a cell which leaves the outgoing cell
  3323. * queue completely empty. It also updates the xmit time and the active time.
  3324. */
  3325. void
  3326. channel_timestamp_drained(channel_t *chan)
  3327. {
  3328. time_t now = time(NULL);
  3329. tor_assert(chan);
  3330. chan->timestamp_active = now;
  3331. chan->timestamp_drained = now;
  3332. chan->timestamp_xmit = now;
  3333. }
  3334. /**
  3335. * Update the recv timestamp
  3336. *
  3337. * This is called whenever we get an incoming cell from the lower layer.
  3338. * This also updates the active timestamp.
  3339. */
  3340. void
  3341. channel_timestamp_recv(channel_t *chan)
  3342. {
  3343. time_t now = time(NULL);
  3344. tor_assert(chan);
  3345. chan->timestamp_active = now;
  3346. chan->timestamp_recv = now;
  3347. }
  3348. /**
  3349. * Update the xmit timestamp
  3350. * This is called whenever we pass an outgoing cell to the lower layer. This
  3351. * also updates the active timestamp.
  3352. */
  3353. void
  3354. channel_timestamp_xmit(channel_t *chan)
  3355. {
  3356. time_t now = time(NULL);
  3357. tor_assert(chan);
  3358. chan->timestamp_active = now;
  3359. chan->timestamp_xmit = now;
  3360. }
  3361. /***************************************************************
  3362. * Timestamp queries - see above for definitions of timestamps *
  3363. **************************************************************/
  3364. /**
  3365. * Query created timestamp for a channel
  3366. */
  3367. time_t
  3368. channel_when_created(channel_t *chan)
  3369. {
  3370. tor_assert(chan);
  3371. return chan->timestamp_created;
  3372. }
  3373. /**
  3374. * Query created timestamp for a channel listener
  3375. */
  3376. time_t
  3377. channel_listener_when_created(channel_listener_t *chan_l)
  3378. {
  3379. tor_assert(chan_l);
  3380. return chan_l->timestamp_created;
  3381. }
  3382. /**
  3383. * Query last active timestamp for a channel
  3384. */
  3385. time_t
  3386. channel_when_last_active(channel_t *chan)
  3387. {
  3388. tor_assert(chan);
  3389. return chan->timestamp_active;
  3390. }
  3391. /**
  3392. * Query last active timestamp for a channel listener
  3393. */
  3394. time_t
  3395. channel_listener_when_last_active(channel_listener_t *chan_l)
  3396. {
  3397. tor_assert(chan_l);
  3398. return chan_l->timestamp_active;
  3399. }
  3400. /**
  3401. * Query last accepted timestamp for a channel listener
  3402. */
  3403. time_t
  3404. channel_listener_when_last_accepted(channel_listener_t *chan_l)
  3405. {
  3406. tor_assert(chan_l);
  3407. return chan_l->timestamp_accepted;
  3408. }
  3409. /**
  3410. * Query client timestamp
  3411. */
  3412. time_t
  3413. channel_when_last_client(channel_t *chan)
  3414. {
  3415. tor_assert(chan);
  3416. return chan->timestamp_client;
  3417. }
  3418. /**
  3419. * Query drained timestamp
  3420. */
  3421. time_t
  3422. channel_when_last_drained(channel_t *chan)
  3423. {
  3424. tor_assert(chan);
  3425. return chan->timestamp_drained;
  3426. }
  3427. /**
  3428. * Query recv timestamp
  3429. */
  3430. time_t
  3431. channel_when_last_recv(channel_t *chan)
  3432. {
  3433. tor_assert(chan);
  3434. return chan->timestamp_recv;
  3435. }
  3436. /**
  3437. * Query xmit timestamp
  3438. */
  3439. time_t
  3440. channel_when_last_xmit(channel_t *chan)
  3441. {
  3442. tor_assert(chan);
  3443. return chan->timestamp_xmit;
  3444. }
  3445. /**
  3446. * Query accepted counter
  3447. */
  3448. uint64_t
  3449. channel_listener_count_accepted(channel_listener_t *chan_l)
  3450. {
  3451. tor_assert(chan_l);
  3452. return chan_l->n_accepted;
  3453. }
  3454. /**
  3455. * Query received cell counter
  3456. */
  3457. uint64_t
  3458. channel_count_recved(channel_t *chan)
  3459. {
  3460. tor_assert(chan);
  3461. return chan->n_cells_recved;
  3462. }
  3463. /**
  3464. * Query transmitted cell counter
  3465. */
  3466. uint64_t
  3467. channel_count_xmitted(channel_t *chan)
  3468. {
  3469. tor_assert(chan);
  3470. return chan->n_cells_xmitted;
  3471. }
  3472. /**
  3473. * Check if a channel matches an extend_info_t
  3474. *
  3475. * This function calls the lower layer and asks if this channel matches a
  3476. * given extend_info_t.
  3477. */
  3478. int
  3479. channel_matches_extend_info(channel_t *chan, extend_info_t *extend_info)
  3480. {
  3481. tor_assert(chan);
  3482. tor_assert(chan->matches_extend_info);
  3483. tor_assert(extend_info);
  3484. return chan->matches_extend_info(chan, extend_info);
  3485. }
  3486. /**
  3487. * Check if a channel matches a given target address; return true iff we do.
  3488. *
  3489. * This function calls into the lower layer and asks if this channel thinks
  3490. * it matches a given target address for circuit extension purposes.
  3491. */
  3492. int
  3493. channel_matches_target_addr_for_extend(channel_t *chan,
  3494. const tor_addr_t *target)
  3495. {
  3496. tor_assert(chan);
  3497. tor_assert(chan->matches_target);
  3498. tor_assert(target);
  3499. return chan->matches_target(chan, target);
  3500. }
  3501. /**
  3502. * Return the total number of circuits used by a channel
  3503. *
  3504. * @param chan Channel to query
  3505. * @return Number of circuits using this as n_chan or p_chan
  3506. */
  3507. unsigned int
  3508. channel_num_circuits(channel_t *chan)
  3509. {
  3510. tor_assert(chan);
  3511. return chan->num_n_circuits +
  3512. chan->num_p_circuits;
  3513. }
  3514. /**
  3515. * Set up circuit ID generation
  3516. *
  3517. * This is called when setting up a channel and replaces the old
  3518. * connection_or_set_circid_type()
  3519. */
  3520. void
  3521. channel_set_circid_type(channel_t *chan,
  3522. crypto_pk_t *identity_rcvd,
  3523. int consider_identity)
  3524. {
  3525. int started_here;
  3526. crypto_pk_t *our_identity;
  3527. tor_assert(chan);
  3528. started_here = channel_is_outgoing(chan);
  3529. if (! consider_identity) {
  3530. if (started_here)
  3531. chan->circ_id_type = CIRC_ID_TYPE_HIGHER;
  3532. else
  3533. chan->circ_id_type = CIRC_ID_TYPE_LOWER;
  3534. return;
  3535. }
  3536. our_identity = started_here ?
  3537. get_tlsclient_identity_key() : get_server_identity_key();
  3538. if (identity_rcvd) {
  3539. if (crypto_pk_cmp_keys(our_identity, identity_rcvd) < 0) {
  3540. chan->circ_id_type = CIRC_ID_TYPE_LOWER;
  3541. } else {
  3542. chan->circ_id_type = CIRC_ID_TYPE_HIGHER;
  3543. }
  3544. } else {
  3545. chan->circ_id_type = CIRC_ID_TYPE_NEITHER;
  3546. }
  3547. }