channel.c 116 KB

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