channel.c 115 KB

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