channel.c 129 KB

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