channel.c 129 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595
  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. /* Tell the scheduler we're increasing the queue size */
  3866. scheduler_adjust_queue_size(chan, 1, adj);
  3867. }
  3868. } else if (queued < chan->bytes_queued_for_xmit) {
  3869. adj = chan->bytes_queued_for_xmit - queued;
  3870. log_debug(LD_CHANNEL,
  3871. "Decreasing queue size for channel " U64_FORMAT " by " U64_FORMAT
  3872. " from " U64_FORMAT " to " U64_FORMAT,
  3873. U64_PRINTF_ARG(chan->global_identifier),
  3874. U64_PRINTF_ARG(adj),
  3875. U64_PRINTF_ARG(chan->bytes_queued_for_xmit),
  3876. U64_PRINTF_ARG(queued));
  3877. /* Update the channel's estimate */
  3878. chan->bytes_queued_for_xmit = queued;
  3879. /* Update the global queue size estimate if appropriate */
  3880. if (chan->state == CHANNEL_STATE_OPEN ||
  3881. chan->state == CHANNEL_STATE_MAINT) {
  3882. estimated_total_queue_size -= adj;
  3883. log_debug(LD_CHANNEL,
  3884. "Decreasing global queue size by " U64_FORMAT " for channel "
  3885. U64_FORMAT ", new size is " U64_FORMAT,
  3886. U64_PRINTF_ARG(adj), U64_PRINTF_ARG(chan->global_identifier),
  3887. U64_PRINTF_ARG(estimated_total_queue_size));
  3888. /* Tell the scheduler we're decreasing the queue size */
  3889. scheduler_adjust_queue_size(chan, -1, adj);
  3890. }
  3891. }
  3892. }