channel.c 110 KB

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