channel.c 107 KB

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