channel.c 114 KB

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