channel.c 114 KB

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