channel.c 112 KB

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