channel.c 116 KB

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