channel.c 128 KB

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