channel.c 112 KB

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