channel.c 114 KB

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