channel.c 108 KB

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