channel.c 100 KB

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