channel.c 113 KB

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