channel.c 98 KB

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