channel.c 112 KB

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