channel.c 129 KB

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