relay.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071
  1. /* Copyright 2001 Matej Pfajfar.
  2. * Copyright 2001-2004 Roger Dingledine.
  3. * Copyright 2004 Roger Dingledine, Nick Mathewson. */
  4. /* See LICENSE for licensing information */
  5. /* $Id$ */
  6. const char relay_c_id[] = "$Id$";
  7. /**
  8. * \file relay.c
  9. * \brief Handle relay cell encryption/decryption, plus packaging and
  10. * receiving from circuits.
  11. **/
  12. #include "or.h"
  13. static int relay_crypt(circuit_t *circ, cell_t *cell, int cell_direction,
  14. crypt_path_t **layer_hint, char *recognized);
  15. static connection_t *relay_lookup_conn(circuit_t *circ, cell_t *cell, int cell_direction);
  16. static int
  17. connection_edge_process_relay_cell(cell_t *cell, circuit_t *circ,
  18. connection_t *conn,
  19. crypt_path_t *layer_hint);
  20. static void
  21. circuit_consider_sending_sendme(circuit_t *circ, crypt_path_t *layer_hint);
  22. static void
  23. circuit_resume_edge_reading(circuit_t *circ, crypt_path_t *layer_hint);
  24. static int
  25. circuit_resume_edge_reading_helper(connection_t *conn,
  26. circuit_t *circ,
  27. crypt_path_t *layer_hint);
  28. static int
  29. circuit_consider_stop_edge_reading(circuit_t *circ, crypt_path_t *layer_hint);
  30. /** Stats: how many relay cells have originated at this hop, or have
  31. * been relayed onward (not recognized at this hop)?
  32. */
  33. unsigned long stats_n_relay_cells_relayed = 0;
  34. /** Stats: how many relay cells have been delivered to streams at this
  35. * hop?
  36. */
  37. unsigned long stats_n_relay_cells_delivered = 0;
  38. /** Update digest from the payload of cell. Assign integrity part to
  39. * cell.
  40. */
  41. static void relay_set_digest(crypto_digest_env_t *digest, cell_t *cell) {
  42. char integrity[4];
  43. relay_header_t rh;
  44. crypto_digest_add_bytes(digest, cell->payload, CELL_PAYLOAD_SIZE);
  45. crypto_digest_get_digest(digest, integrity, 4);
  46. // log_fn(LOG_DEBUG,"Putting digest of %u %u %u %u into relay cell.",
  47. // integrity[0], integrity[1], integrity[2], integrity[3]);
  48. relay_header_unpack(&rh, cell->payload);
  49. memcpy(rh.integrity, integrity, 4);
  50. relay_header_pack(cell->payload, &rh);
  51. }
  52. /** Does the digest for this circuit indicate that this cell is for us?
  53. *
  54. * Update digest from the payload of cell (with the integrity part set
  55. * to 0). If the integrity part is valid, return 1, else restore digest
  56. * and cell to their original state and return 0.
  57. */
  58. static int relay_digest_matches(crypto_digest_env_t *digest, cell_t *cell) {
  59. char received_integrity[4], calculated_integrity[4];
  60. relay_header_t rh;
  61. crypto_digest_env_t *backup_digest=NULL;
  62. backup_digest = crypto_digest_dup(digest);
  63. relay_header_unpack(&rh, cell->payload);
  64. memcpy(received_integrity, rh.integrity, 4);
  65. memset(rh.integrity, 0, 4);
  66. relay_header_pack(cell->payload, &rh);
  67. // log_fn(LOG_DEBUG,"Reading digest of %u %u %u %u from relay cell.",
  68. // received_integrity[0], received_integrity[1],
  69. // received_integrity[2], received_integrity[3]);
  70. crypto_digest_add_bytes(digest, cell->payload, CELL_PAYLOAD_SIZE);
  71. crypto_digest_get_digest(digest, calculated_integrity, 4);
  72. if (memcmp(received_integrity, calculated_integrity, 4)) {
  73. // log_fn(LOG_INFO,"Recognized=0 but bad digest. Not recognizing.");
  74. // (%d vs %d).", received_integrity, calculated_integrity);
  75. /* restore digest to its old form */
  76. crypto_digest_assign(digest, backup_digest);
  77. /* restore the relay header */
  78. memcpy(rh.integrity, received_integrity, 4);
  79. relay_header_pack(cell->payload, &rh);
  80. crypto_free_digest_env(backup_digest);
  81. return 0;
  82. }
  83. crypto_free_digest_env(backup_digest);
  84. return 1;
  85. }
  86. /** Apply <b>cipher</b> to CELL_PAYLOAD_SIZE bytes of <b>in</b>
  87. * (in place).
  88. *
  89. * If <b>encrypt_mode</b> is 1 then encrypt, else decrypt.
  90. *
  91. * Return -1 if the crypto fails, else return 0.
  92. */
  93. static int relay_crypt_one_payload(crypto_cipher_env_t *cipher, char *in,
  94. int encrypt_mode) {
  95. char out[CELL_PAYLOAD_SIZE]; /* 'in' must be this size too */
  96. relay_header_t rh;
  97. relay_header_unpack(&rh, in);
  98. // log_fn(LOG_DEBUG,"before crypt: %d",rh.recognized);
  99. if (( encrypt_mode && crypto_cipher_encrypt(cipher, out, in, CELL_PAYLOAD_SIZE)) ||
  100. (!encrypt_mode && crypto_cipher_decrypt(cipher, out, in, CELL_PAYLOAD_SIZE))) {
  101. log_fn(LOG_WARN,"Error during relay encryption");
  102. return -1;
  103. }
  104. memcpy(in,out,CELL_PAYLOAD_SIZE);
  105. relay_header_unpack(&rh, in);
  106. // log_fn(LOG_DEBUG,"after crypt: %d",rh.recognized);
  107. return 0;
  108. }
  109. /** Receive a relay cell:
  110. * - Crypt it (encrypt APward, decrypt at AP, decrypt exitward).
  111. * - Check if recognized (if exitward).
  112. * - If recognized and the digest checks out, then find if there's
  113. * a conn that the cell is intended for, and deliver it to·
  114. * connection_edge.
  115. * - Else connection_or_write_cell_to_buf to the conn on the other
  116. * side of the circuit.
  117. */
  118. int circuit_receive_relay_cell(cell_t *cell, circuit_t *circ,
  119. int cell_direction) {
  120. connection_t *conn=NULL;
  121. crypt_path_t *layer_hint=NULL;
  122. char recognized=0;
  123. tor_assert(cell);
  124. tor_assert(circ);
  125. tor_assert(cell_direction == CELL_DIRECTION_OUT ||
  126. cell_direction == CELL_DIRECTION_IN);
  127. if (circ->marked_for_close)
  128. return 0;
  129. if (relay_crypt(circ, cell, cell_direction, &layer_hint, &recognized) < 0) {
  130. log_fn(LOG_WARN,"relay crypt failed. Dropping connection.");
  131. return -1;
  132. }
  133. if (recognized) {
  134. conn = relay_lookup_conn(circ, cell, cell_direction);
  135. if (cell_direction == CELL_DIRECTION_OUT) {
  136. ++stats_n_relay_cells_delivered;
  137. log_fn(LOG_DEBUG,"Sending away from origin.");
  138. if (connection_edge_process_relay_cell(cell, circ, conn, NULL) < 0) {
  139. log_fn(LOG_WARN,"connection_edge_process_relay_cell (away from origin) failed.");
  140. return -1;
  141. }
  142. }
  143. if (cell_direction == CELL_DIRECTION_IN) {
  144. ++stats_n_relay_cells_delivered;
  145. log_fn(LOG_DEBUG,"Sending to origin.");
  146. if (connection_edge_process_relay_cell(cell, circ, conn, layer_hint) < 0) {
  147. log_fn(LOG_WARN,"connection_edge_process_relay_cell (at origin) failed.");
  148. return -1;
  149. }
  150. }
  151. return 0;
  152. }
  153. /* not recognized. pass it on. */
  154. if (cell_direction == CELL_DIRECTION_OUT) {
  155. cell->circ_id = circ->n_circ_id; /* switch it */
  156. conn = circ->n_conn;
  157. } else {
  158. cell->circ_id = circ->p_circ_id; /* switch it */
  159. conn = circ->p_conn;
  160. }
  161. if (!conn) {
  162. if (circ->rend_splice && cell_direction == CELL_DIRECTION_OUT) {
  163. tor_assert(circ->purpose == CIRCUIT_PURPOSE_REND_ESTABLISHED);
  164. tor_assert(circ->rend_splice->purpose == CIRCUIT_PURPOSE_REND_ESTABLISHED);
  165. cell->circ_id = circ->rend_splice->p_circ_id;
  166. if (circuit_receive_relay_cell(cell, circ->rend_splice, CELL_DIRECTION_IN)<0) {
  167. log_fn(LOG_WARN, "Error relaying cell across rendezvous; closing circuits");
  168. circuit_mark_for_close(circ); /* XXXX Do this here, or just return -1? */
  169. return -1;
  170. }
  171. return 0;
  172. }
  173. log_fn(LOG_WARN,"Didn't recognize cell, but circ stops here! Closing circ.");
  174. return -1;
  175. }
  176. log_fn(LOG_DEBUG,"Passing on unrecognized cell.");
  177. ++stats_n_relay_cells_relayed;
  178. connection_or_write_cell_to_buf(cell, conn);
  179. return 0;
  180. }
  181. /** Do the appropriate en/decryptions for <b>cell</b> arriving on
  182. * <b>circ</b> in direction <b>cell_direction</b>.
  183. *
  184. * If cell_direction == CELL_DIRECTION_IN:
  185. * - If we're at the origin (we're the OP), for hops 1..N,
  186. * decrypt cell. If recognized, stop.
  187. * - Else (we're not the OP), encrypt one hop. Cell is not recognized.
  188. *
  189. * If cell_direction == CELL_DIRECTION_OUT:
  190. * - decrypt one hop. Check if recognized.
  191. *
  192. * If cell is recognized, set *recognized to 1, and set
  193. * *layer_hint to the hop that recognized it.
  194. *
  195. * Return -1 to indicate that we should mark the circuit for close,
  196. * else return 0.
  197. */
  198. /* wrap this into receive_relay_cell one day */
  199. static int relay_crypt(circuit_t *circ, cell_t *cell, int cell_direction,
  200. crypt_path_t **layer_hint, char *recognized) {
  201. crypt_path_t *thishop;
  202. relay_header_t rh;
  203. tor_assert(circ);
  204. tor_assert(cell);
  205. tor_assert(recognized);
  206. tor_assert(cell_direction == CELL_DIRECTION_IN ||
  207. cell_direction == CELL_DIRECTION_OUT);
  208. if (cell_direction == CELL_DIRECTION_IN) {
  209. if (CIRCUIT_IS_ORIGIN(circ)) { /* We're at the beginning of the circuit.
  210. We'll want to do layered decrypts. */
  211. tor_assert(circ->cpath);
  212. thishop = circ->cpath;
  213. if (thishop->state != CPATH_STATE_OPEN) {
  214. log_fn(LOG_WARN,"Relay cell before first created cell? Closing.");
  215. return -1;
  216. }
  217. do { /* Remember: cpath is in forward order, that is, first hop first. */
  218. tor_assert(thishop);
  219. if (relay_crypt_one_payload(thishop->b_crypto, cell->payload, 0) < 0)
  220. return -1;
  221. relay_header_unpack(&rh, cell->payload);
  222. if (rh.recognized == 0) {
  223. /* it's possibly recognized. have to check digest to be sure. */
  224. if (relay_digest_matches(thishop->b_digest, cell)) {
  225. *recognized = 1;
  226. *layer_hint = thishop;
  227. return 0;
  228. }
  229. }
  230. thishop = thishop->next;
  231. } while (thishop != circ->cpath && thishop->state == CPATH_STATE_OPEN);
  232. log_fn(LOG_WARN,"in-cell at OP not recognized. Closing.");
  233. return -1;
  234. } else { /* we're in the middle. Just one crypt. */
  235. if (relay_crypt_one_payload(circ->p_crypto, cell->payload, 1) < 0)
  236. return -1;
  237. // log_fn(LOG_DEBUG,"Skipping recognized check, because we're not the OP.");
  238. }
  239. } else /* cell_direction == CELL_DIRECTION_OUT */ {
  240. /* we're in the middle. Just one crypt. */
  241. if (relay_crypt_one_payload(circ->n_crypto, cell->payload, 0) < 0)
  242. return -1;
  243. relay_header_unpack(&rh, cell->payload);
  244. if (rh.recognized == 0) {
  245. /* it's possibly recognized. have to check digest to be sure. */
  246. if (relay_digest_matches(circ->n_digest, cell)) {
  247. *recognized = 1;
  248. return 0;
  249. }
  250. }
  251. }
  252. return 0;
  253. }
  254. /** Package a relay cell:
  255. * - Encrypt it to the right layer
  256. * - connection_or_write_cell_to_buf to the right conn
  257. */
  258. static int
  259. circuit_package_relay_cell(cell_t *cell, circuit_t *circ,
  260. int cell_direction,
  261. crypt_path_t *layer_hint)
  262. {
  263. connection_t *conn; /* where to send the cell */
  264. crypt_path_t *thishop; /* counter for repeated crypts */
  265. if (cell_direction == CELL_DIRECTION_OUT) {
  266. conn = circ->n_conn;
  267. if (!conn) {
  268. log_fn(LOG_WARN,"outgoing relay cell has n_conn==NULL. Dropping.");
  269. return 0; /* just drop it */
  270. }
  271. relay_set_digest(layer_hint->f_digest, cell);
  272. thishop = layer_hint;
  273. /* moving from farthest to nearest hop */
  274. do {
  275. tor_assert(thishop);
  276. log_fn(LOG_DEBUG,"crypting a layer of the relay cell.");
  277. if (relay_crypt_one_payload(thishop->f_crypto, cell->payload, 1) < 0) {
  278. return -1;
  279. }
  280. thishop = thishop->prev;
  281. } while (thishop != circ->cpath->prev);
  282. } else { /* incoming cell */
  283. conn = circ->p_conn;
  284. if (!conn) {
  285. log_fn(LOG_WARN,"incoming relay cell has p_conn==NULL. Dropping.");
  286. return 0; /* just drop it */
  287. }
  288. relay_set_digest(circ->p_digest, cell);
  289. if (relay_crypt_one_payload(circ->p_crypto, cell->payload, 1) < 0)
  290. return -1;
  291. }
  292. ++stats_n_relay_cells_relayed;
  293. connection_or_write_cell_to_buf(cell, conn);
  294. return 0;
  295. }
  296. /** If cell's stream_id matches the stream_id of any conn that's
  297. * attached to circ, return that conn, else return NULL.
  298. */
  299. static connection_t *
  300. relay_lookup_conn(circuit_t *circ, cell_t *cell, int cell_direction)
  301. {
  302. connection_t *tmpconn;
  303. relay_header_t rh;
  304. relay_header_unpack(&rh, cell->payload);
  305. if (!rh.stream_id)
  306. return NULL;
  307. /* IN or OUT cells could have come from either direction, now
  308. * that we allow rendezvous *to* an OP.
  309. */
  310. for (tmpconn = circ->n_streams; tmpconn; tmpconn=tmpconn->next_stream) {
  311. if (rh.stream_id == tmpconn->stream_id && !tmpconn->marked_for_close) {
  312. log_fn(LOG_DEBUG,"found conn for stream %d.", rh.stream_id);
  313. if (cell_direction == CELL_DIRECTION_OUT ||
  314. connection_edge_is_rendezvous_stream(tmpconn))
  315. return tmpconn;
  316. }
  317. }
  318. for (tmpconn = circ->p_streams; tmpconn; tmpconn=tmpconn->next_stream) {
  319. if (rh.stream_id == tmpconn->stream_id && !tmpconn->marked_for_close) {
  320. log_fn(LOG_DEBUG,"found conn for stream %d.", rh.stream_id);
  321. return tmpconn;
  322. }
  323. }
  324. for (tmpconn = circ->resolving_streams; tmpconn; tmpconn=tmpconn->next_stream) {
  325. if (rh.stream_id == tmpconn->stream_id && !tmpconn->marked_for_close) {
  326. log_fn(LOG_DEBUG,"found conn for stream %d.", rh.stream_id);
  327. return tmpconn;
  328. }
  329. }
  330. return NULL; /* probably a begin relay cell */
  331. }
  332. /** Pack the relay_header_t host-order structure <b>src</b> into
  333. * network-order in the buffer <b>dest</b>. See tor-spec.txt for details
  334. * about the wire format.
  335. */
  336. void relay_header_pack(char *dest, const relay_header_t *src) {
  337. *(uint8_t*)(dest) = src->command;
  338. set_uint16(dest+1, htons(src->recognized));
  339. set_uint16(dest+3, htons(src->stream_id));
  340. memcpy(dest+5, src->integrity, 4);
  341. set_uint16(dest+9, htons(src->length));
  342. }
  343. /** Unpack the network-order buffer <b>src</b> into a host-order
  344. * relay_header_t structure <b>dest</b>.
  345. */
  346. void relay_header_unpack(relay_header_t *dest, const char *src) {
  347. dest->command = *(uint8_t*)(src);
  348. dest->recognized = ntohs(get_uint16(src+1));
  349. dest->stream_id = ntohs(get_uint16(src+3));
  350. memcpy(dest->integrity, src+5, 4);
  351. dest->length = ntohs(get_uint16(src+9));
  352. }
  353. /** Make a relay cell out of <b>relay_command</b> and <b>payload</b>, and
  354. * send it onto the open circuit <b>circ</b>. <b>fromconn</b> is the stream
  355. * that's sending the relay cell, or NULL if it's a control cell.
  356. * <b>cpath_layer</b> is NULL for OR->OP cells, or the destination hop
  357. * for OP->OR cells.
  358. *
  359. * If you can't send the cell, mark the circuit for close and
  360. * return -1. Else return 0.
  361. */
  362. int connection_edge_send_command(connection_t *fromconn, circuit_t *circ,
  363. int relay_command, const char *payload,
  364. size_t payload_len, crypt_path_t *cpath_layer) {
  365. cell_t cell;
  366. relay_header_t rh;
  367. int cell_direction;
  368. if (!circ) {
  369. log_fn(LOG_WARN,"no circ. Closing conn.");
  370. tor_assert(fromconn);
  371. fromconn->has_sent_end = 1; /* no circ to send to */
  372. connection_mark_for_close(fromconn);
  373. return -1;
  374. }
  375. memset(&cell, 0, sizeof(cell_t));
  376. cell.command = CELL_RELAY;
  377. if (cpath_layer) {
  378. cell.circ_id = circ->n_circ_id;
  379. cell_direction = CELL_DIRECTION_OUT;
  380. } else {
  381. cell.circ_id = circ->p_circ_id;
  382. cell_direction = CELL_DIRECTION_IN;
  383. }
  384. memset(&rh, 0, sizeof(rh));
  385. rh.command = relay_command;
  386. if (fromconn)
  387. rh.stream_id = fromconn->stream_id; /* else it's 0 */
  388. rh.length = payload_len;
  389. relay_header_pack(cell.payload, &rh);
  390. if (payload_len)
  391. memcpy(cell.payload+RELAY_HEADER_SIZE, payload, payload_len);
  392. log_fn(LOG_DEBUG,"delivering %d cell %s.", relay_command,
  393. cell_direction == CELL_DIRECTION_OUT ? "forward" : "backward");
  394. if (circuit_package_relay_cell(&cell, circ, cell_direction, cpath_layer) < 0) {
  395. log_fn(LOG_WARN,"circuit_package_relay_cell failed. Closing.");
  396. circuit_mark_for_close(circ);
  397. return -1;
  398. }
  399. return 0;
  400. }
  401. /** Translate the <b>payload</b> of length <b>length</b>, which
  402. * came from a relay 'end' cell, into a static const string describing
  403. * why the stream is closing.
  404. */
  405. static const char *
  406. connection_edge_end_reason(char *payload, uint16_t length) {
  407. if (length < 1) {
  408. log_fn(LOG_WARN,"End cell arrived with length 0. Should be at least 1.");
  409. return "MALFORMED";
  410. }
  411. if (*payload < _MIN_END_STREAM_REASON || *payload > _MAX_END_STREAM_REASON) {
  412. log_fn(LOG_WARN,"Reason for ending (%d) not recognized.",*payload);
  413. return "MALFORMED";
  414. }
  415. switch (*payload) {
  416. case END_STREAM_REASON_MISC: return "misc error";
  417. case END_STREAM_REASON_RESOLVEFAILED: return "resolve failed";
  418. case END_STREAM_REASON_CONNECTFAILED: return "connect failed";
  419. case END_STREAM_REASON_EXITPOLICY: return "exit policy failed";
  420. case END_STREAM_REASON_DESTROY: return "destroyed";
  421. case END_STREAM_REASON_DONE: return "closed normally";
  422. case END_STREAM_REASON_TIMEOUT: return "gave up (timeout)";
  423. }
  424. tor_assert(0);
  425. return "";
  426. }
  427. /** How many times will I retry a stream that fails due to DNS
  428. * resolve failure?
  429. */
  430. #define MAX_RESOLVE_FAILURES 3
  431. /** An incoming relay cell has arrived from circuit <b>circ</b> to
  432. * stream <b>conn</b>.
  433. *
  434. * The arguments here are the same as in
  435. * connection_edge_process_relay_cell() below; this function is called
  436. * from there when <b>conn</b> is defined and not in an open state.
  437. */
  438. static int
  439. connection_edge_process_relay_cell_not_open(
  440. relay_header_t *rh, cell_t *cell, circuit_t *circ,
  441. connection_t *conn, crypt_path_t *layer_hint) {
  442. uint32_t addr;
  443. int reason;
  444. routerinfo_t *exitrouter;
  445. if (rh->command == RELAY_COMMAND_END) {
  446. reason = *(cell->payload+RELAY_HEADER_SIZE);
  447. /* We have to check this here, since we aren't connected yet. */
  448. if (rh->length >= 5 && reason == END_STREAM_REASON_EXITPOLICY) {
  449. if (conn->type != CONN_TYPE_AP) {
  450. log_fn(LOG_WARN,"Got an end because of exitpolicy, but we're not an AP. Closing.");
  451. return -1;
  452. }
  453. addr = ntohl(get_uint32(cell->payload+RELAY_HEADER_SIZE+1));
  454. if (addr) {
  455. log_fn(LOG_INFO,"Address %s refused due to exit policy. Retrying.",
  456. conn->socks_request->address);
  457. } else {
  458. log_fn(LOG_INFO,"Address %s resolved to 0.0.0.0. Closing,",
  459. conn->socks_request->address);
  460. conn->has_sent_end = 1; /* we just got an 'end', don't need to send one */
  461. connection_mark_for_close(conn);
  462. return 0;
  463. }
  464. client_dns_set_entry(conn->socks_request->address, addr);
  465. /* check if he *ought* to have allowed it */
  466. exitrouter = router_get_by_digest(circ->build_state->chosen_exit_digest);
  467. if (!exitrouter) {
  468. log_fn(LOG_INFO,"Skipping broken circ (exit router vanished)");
  469. return 0; /* this circuit is screwed and doesn't know it yet */
  470. }
  471. if (connection_ap_can_use_exit(conn, exitrouter)) {
  472. log_fn(LOG_NOTICE,"Exitrouter %s seems to be more restrictive than its exit policy. Not using this router as exit for now,", exitrouter->nickname);
  473. addr_policy_free(exitrouter->exit_policy);
  474. exitrouter->exit_policy =
  475. router_parse_addr_policy_from_string("reject *:*");
  476. }
  477. conn->state = AP_CONN_STATE_CIRCUIT_WAIT;
  478. circuit_detach_stream(circ,conn);
  479. if (connection_ap_handshake_attach_circuit(conn) >= 0)
  480. return 0;
  481. log_fn(LOG_INFO,"Giving up on retrying (from exitpolicy); conn can't be handled.");
  482. /* else, conn will get closed below */
  483. } else if (rh->length && reason == END_STREAM_REASON_RESOLVEFAILED) {
  484. if (client_dns_incr_failures(conn->socks_request->address)
  485. < MAX_RESOLVE_FAILURES) {
  486. /* We haven't retried too many times; reattach the connection. */
  487. log_fn(LOG_INFO,"Resolve of '%s' failed, trying again.",
  488. conn->socks_request->address);
  489. circuit_log_path(LOG_INFO,circ);
  490. conn->state = AP_CONN_STATE_CIRCUIT_WAIT;
  491. circuit_detach_stream(circ,conn);
  492. tor_assert(circ->timestamp_dirty);
  493. circ->timestamp_dirty -= get_options()->NewCircuitPeriod;
  494. /* make sure not to expire/retry the stream quite yet */
  495. conn->timestamp_lastread = time(NULL);
  496. if (connection_ap_handshake_attach_circuit(conn) >= 0)
  497. return 0;
  498. /* else, conn will get closed below */
  499. log_fn(LOG_INFO,"Giving up on retrying (from resolvefailed); conn can't be handled.");
  500. } else {
  501. log_fn(LOG_NOTICE,"Have tried resolving address '%s' at %d different places. Giving up.",
  502. conn->socks_request->address, MAX_RESOLVE_FAILURES);
  503. }
  504. }
  505. log_fn(LOG_INFO,"Edge got end (%s) before we're connected. Marking for close.",
  506. connection_edge_end_reason(cell->payload+RELAY_HEADER_SIZE, rh->length));
  507. if (CIRCUIT_IS_ORIGIN(circ))
  508. circuit_log_path(LOG_INFO,circ);
  509. conn->has_sent_end = 1; /* we just got an 'end', don't need to send one */
  510. connection_mark_for_close(conn);
  511. return 0;
  512. }
  513. if (conn->type == CONN_TYPE_AP && rh->command == RELAY_COMMAND_CONNECTED) {
  514. if (conn->state != AP_CONN_STATE_CONNECT_WAIT) {
  515. log_fn(LOG_WARN,"Got 'connected' while not in state connect_wait. Dropping.");
  516. return 0;
  517. }
  518. // log_fn(LOG_INFO,"Connected! Notifying application.");
  519. conn->state = AP_CONN_STATE_OPEN;
  520. log_fn(LOG_INFO,"'connected' received after %d seconds.",
  521. (int)(time(NULL) - conn->timestamp_lastread));
  522. if (rh->length >= 4) {
  523. addr = ntohl(get_uint32(cell->payload+RELAY_HEADER_SIZE));
  524. if (!addr) {
  525. log_fn(LOG_INFO,"...but it claims the IP address was 0.0.0.0. Closing.");
  526. connection_edge_end(conn, END_STREAM_REASON_MISC, conn->cpath_layer);
  527. connection_mark_for_close(conn);
  528. return 0;
  529. }
  530. client_dns_set_entry(conn->socks_request->address, addr);
  531. }
  532. circuit_log_path(LOG_INFO,circ);
  533. connection_ap_handshake_socks_reply(conn, NULL, 0, 1);
  534. conn->socks_request->has_finished = 1;
  535. /* handle anything that might have queued */
  536. if (connection_edge_package_raw_inbuf(conn, 1) < 0) {
  537. connection_edge_end(conn, END_STREAM_REASON_MISC, conn->cpath_layer);
  538. connection_mark_for_close(conn);
  539. return 0;
  540. }
  541. return 0;
  542. }
  543. if (conn->type == CONN_TYPE_AP && rh->command == RELAY_COMMAND_RESOLVED) {
  544. if (conn->state != AP_CONN_STATE_RESOLVE_WAIT) {
  545. log_fn(LOG_WARN,"Got a 'resolved' cell while not in state resolve_wait. Dropping.");
  546. return 0;
  547. }
  548. tor_assert(conn->socks_request->command == SOCKS_COMMAND_RESOLVE);
  549. if (rh->length < 2 || cell->payload[RELAY_HEADER_SIZE+1]+2>rh->length) {
  550. log_fn(LOG_WARN, "Dropping malformed 'resolved' cell");
  551. conn->has_sent_end = 1;
  552. connection_mark_for_close(conn);
  553. return 0;
  554. }
  555. connection_ap_handshake_socks_resolved(conn,
  556. cell->payload[RELAY_HEADER_SIZE], /*answer_type*/
  557. cell->payload[RELAY_HEADER_SIZE+1], /*answer_len*/
  558. cell->payload+RELAY_HEADER_SIZE+2); /* answer */
  559. conn->has_sent_end = 1;
  560. connection_mark_for_close(conn);
  561. conn->hold_open_until_flushed = 1;
  562. return 0;
  563. }
  564. log_fn(LOG_WARN,"Got an unexpected relay command %d, in state %d (%s). Closing.",
  565. rh->command, conn->state, conn_state_to_string[conn->type][conn->state]);
  566. connection_edge_end(conn, END_STREAM_REASON_MISC, conn->cpath_layer);
  567. connection_mark_for_close(conn);
  568. return -1;
  569. }
  570. /** An incoming relay cell has arrived on circuit <b>circ</b>. If
  571. * <b>conn</b> is NULL this is a control cell, else <b>cell</b> is
  572. * destined for <b>conn</b>.
  573. *
  574. * If <b>layer_hint</b> is defined, then we're the origin of the
  575. * circuit, and it specifies the hop that packaged <b>cell</b>.
  576. *
  577. * Return -1 if you want to tear down the circuit, else 0.
  578. */
  579. static int
  580. connection_edge_process_relay_cell(cell_t *cell, circuit_t *circ,
  581. connection_t *conn,
  582. crypt_path_t *layer_hint)
  583. {
  584. static int num_seen=0;
  585. relay_header_t rh;
  586. tor_assert(cell);
  587. tor_assert(circ);
  588. relay_header_unpack(&rh, cell->payload);
  589. // log_fn(LOG_DEBUG,"command %d stream %d", rh.command, rh.stream_id);
  590. num_seen++;
  591. log_fn(LOG_DEBUG,"Now seen %d relay cells here.", num_seen);
  592. /* either conn is NULL, in which case we've got a control cell, or else
  593. * conn points to the recognized stream. */
  594. if (conn &&
  595. conn->state != AP_CONN_STATE_OPEN &&
  596. conn->state != EXIT_CONN_STATE_OPEN) {
  597. return connection_edge_process_relay_cell_not_open(
  598. &rh, cell, circ, conn, layer_hint);
  599. }
  600. switch (rh.command) {
  601. case RELAY_COMMAND_DROP:
  602. log_fn(LOG_INFO,"Got a relay-level padding cell. Dropping.");
  603. return 0;
  604. case RELAY_COMMAND_BEGIN:
  605. if (layer_hint &&
  606. circ->purpose != CIRCUIT_PURPOSE_S_REND_JOINED) {
  607. log_fn(LOG_WARN,"relay begin request unsupported at AP. Dropping.");
  608. return 0;
  609. }
  610. if (conn) {
  611. log_fn(LOG_WARN,"begin cell for known stream. Dropping.");
  612. return 0;
  613. }
  614. connection_exit_begin_conn(cell, circ);
  615. return 0;
  616. case RELAY_COMMAND_DATA:
  617. ++stats_n_data_cells_received;
  618. if (( layer_hint && --layer_hint->deliver_window < 0) ||
  619. (!layer_hint && --circ->deliver_window < 0)) {
  620. log_fn(LOG_WARN,"(relay data) circ deliver_window below 0. Killing.");
  621. connection_edge_end(conn, END_STREAM_REASON_MISC, conn->cpath_layer);
  622. connection_mark_for_close(conn);
  623. return -1;
  624. }
  625. log_fn(LOG_DEBUG,"circ deliver_window now %d.", layer_hint ?
  626. layer_hint->deliver_window : circ->deliver_window);
  627. circuit_consider_sending_sendme(circ, layer_hint);
  628. if (!conn) {
  629. log_fn(LOG_INFO,"data cell dropped, unknown stream.");
  630. return 0;
  631. }
  632. if (--conn->deliver_window < 0) { /* is it below 0 after decrement? */
  633. log_fn(LOG_WARN,"(relay data) conn deliver_window below 0. Killing.");
  634. return -1; /* somebody's breaking protocol. kill the whole circuit. */
  635. }
  636. stats_n_data_bytes_received += rh.length;
  637. if (conn->type == CONN_TYPE_AP) {
  638. conn->stream_size += rh.length;
  639. log_fn(LOG_DEBUG,"%d: stream size now %d.", conn->s, (int)conn->stream_size);
  640. }
  641. connection_write_to_buf(cell->payload + RELAY_HEADER_SIZE,
  642. rh.length, conn);
  643. connection_edge_consider_sending_sendme(conn);
  644. return 0;
  645. case RELAY_COMMAND_END:
  646. if (!conn) {
  647. log_fn(LOG_INFO,"end cell (%s) dropped, unknown stream.",
  648. connection_edge_end_reason(cell->payload+RELAY_HEADER_SIZE, rh.length));
  649. return 0;
  650. }
  651. /* XXX add to this log_fn the exit node's nickname? */
  652. log_fn(LOG_INFO,"%d: end cell (%s) for stream %d. Removing stream. Size %d.",
  653. conn->s,
  654. connection_edge_end_reason(cell->payload+RELAY_HEADER_SIZE, rh.length),
  655. conn->stream_id, (int)conn->stream_size);
  656. #ifdef HALF_OPEN
  657. conn->done_sending = 1;
  658. shutdown(conn->s, 1); /* XXX check return; refactor NM */
  659. if (conn->done_receiving) {
  660. /* We just *got* an end; no reason to send one. */
  661. conn->has_sent_end = 1;
  662. connection_mark_for_close(conn);
  663. conn->hold_open_until_flushed = 1;
  664. }
  665. #else
  666. /* We just *got* an end; no reason to send one. */
  667. conn->has_sent_end = 1;
  668. if (!conn->marked_for_close) {
  669. /* only mark it if not already marked. it's possible to
  670. * get the 'end' right around when the client hangs up on us. */
  671. connection_mark_for_close(conn);
  672. conn->hold_open_until_flushed = 1;
  673. }
  674. #endif
  675. return 0;
  676. case RELAY_COMMAND_EXTEND:
  677. if (conn) {
  678. log_fn(LOG_WARN,"'extend' for non-zero stream. Dropping.");
  679. return 0;
  680. }
  681. return circuit_extend(cell, circ);
  682. case RELAY_COMMAND_EXTENDED:
  683. if (!layer_hint) {
  684. log_fn(LOG_WARN,"'extended' unsupported at non-origin. Dropping.");
  685. return 0;
  686. }
  687. log_fn(LOG_DEBUG,"Got an extended cell! Yay.");
  688. if (circuit_finish_handshake(circ, cell->payload+RELAY_HEADER_SIZE) < 0) {
  689. log_fn(LOG_WARN,"circuit_finish_handshake failed.");
  690. return -1;
  691. }
  692. if (circuit_send_next_onion_skin(circ)<0) {
  693. log_fn(LOG_INFO,"circuit_send_next_onion_skin() failed.");
  694. return -1;
  695. }
  696. return 0;
  697. case RELAY_COMMAND_TRUNCATE:
  698. if (layer_hint) {
  699. log_fn(LOG_WARN,"'truncate' unsupported at origin. Dropping.");
  700. return 0;
  701. }
  702. if (circ->n_conn) {
  703. connection_send_destroy(circ->n_circ_id, circ->n_conn);
  704. circ->n_conn = NULL;
  705. }
  706. log_fn(LOG_DEBUG, "Processed 'truncate', replying.");
  707. connection_edge_send_command(NULL, circ, RELAY_COMMAND_TRUNCATED,
  708. NULL, 0, NULL);
  709. return 0;
  710. case RELAY_COMMAND_TRUNCATED:
  711. if (!layer_hint) {
  712. log_fn(LOG_WARN,"'truncated' unsupported at non-origin. Dropping.");
  713. return 0;
  714. }
  715. circuit_truncated(circ, layer_hint);
  716. return 0;
  717. case RELAY_COMMAND_CONNECTED:
  718. if (conn) {
  719. log_fn(LOG_WARN,"'connected' unsupported while open. Closing circ.");
  720. return -1;
  721. }
  722. log_fn(LOG_INFO,"'connected' received, no conn attached anymore. Ignoring.");
  723. return 0;
  724. case RELAY_COMMAND_SENDME:
  725. if (!conn) {
  726. if (layer_hint) {
  727. layer_hint->package_window += CIRCWINDOW_INCREMENT;
  728. log_fn(LOG_DEBUG,"circ-level sendme at origin, packagewindow %d.",
  729. layer_hint->package_window);
  730. circuit_resume_edge_reading(circ, layer_hint);
  731. } else {
  732. circ->package_window += CIRCWINDOW_INCREMENT;
  733. log_fn(LOG_DEBUG,"circ-level sendme at non-origin, packagewindow %d.",
  734. circ->package_window);
  735. circuit_resume_edge_reading(circ, layer_hint);
  736. }
  737. return 0;
  738. }
  739. conn->package_window += STREAMWINDOW_INCREMENT;
  740. log_fn(LOG_DEBUG,"stream-level sendme, packagewindow now %d.", conn->package_window);
  741. connection_start_reading(conn);
  742. connection_edge_package_raw_inbuf(conn, 1); /* handle whatever might still be on the inbuf */
  743. return 0;
  744. case RELAY_COMMAND_RESOLVE:
  745. if (layer_hint) {
  746. log_fn(LOG_WARN,"resolve request unsupported at AP; dropping.");
  747. return 0;
  748. } else if (conn) {
  749. log_fn(LOG_WARN, "resolve request for known stream; dropping.");
  750. return 0;
  751. } else if (circ->purpose != CIRCUIT_PURPOSE_OR) {
  752. log_fn(LOG_WARN, "resolve request on circ with purpose %d; dropping",
  753. circ->purpose);
  754. return 0;
  755. }
  756. connection_exit_begin_resolve(cell, circ);
  757. return 0;
  758. case RELAY_COMMAND_RESOLVED:
  759. if (conn) {
  760. log_fn(LOG_WARN,"'resolved' unsupported while open. Closing circ.");
  761. return -1;
  762. }
  763. log_fn(LOG_INFO,"'resolved' received, no conn attached anymore. Ignoring.");
  764. return 0;
  765. case RELAY_COMMAND_ESTABLISH_INTRO:
  766. case RELAY_COMMAND_ESTABLISH_RENDEZVOUS:
  767. case RELAY_COMMAND_INTRODUCE1:
  768. case RELAY_COMMAND_INTRODUCE2:
  769. case RELAY_COMMAND_INTRODUCE_ACK:
  770. case RELAY_COMMAND_RENDEZVOUS1:
  771. case RELAY_COMMAND_RENDEZVOUS2:
  772. case RELAY_COMMAND_INTRO_ESTABLISHED:
  773. case RELAY_COMMAND_RENDEZVOUS_ESTABLISHED:
  774. rend_process_relay_cell(circ, rh.command, rh.length,
  775. cell->payload+RELAY_HEADER_SIZE);
  776. return 0;
  777. }
  778. log_fn(LOG_WARN,"unknown relay command %d.",rh.command);
  779. return -1;
  780. }
  781. uint64_t stats_n_data_cells_packaged = 0;
  782. uint64_t stats_n_data_bytes_packaged = 0;
  783. uint64_t stats_n_data_cells_received = 0;
  784. uint64_t stats_n_data_bytes_received = 0;
  785. /** While conn->inbuf has an entire relay payload of bytes on it,
  786. * and the appropriate package windows aren't empty, grab a cell
  787. * and send it down the circuit.
  788. *
  789. * Return -1 if conn should be marked for close, else return 0.
  790. */
  791. int connection_edge_package_raw_inbuf(connection_t *conn, int package_partial) {
  792. size_t amount_to_process, length;
  793. char payload[CELL_PAYLOAD_SIZE];
  794. circuit_t *circ;
  795. tor_assert(conn);
  796. tor_assert(!connection_speaks_cells(conn));
  797. repeat_connection_edge_package_raw_inbuf:
  798. circ = circuit_get_by_conn(conn);
  799. if (!circ) {
  800. log_fn(LOG_INFO,"conn has no circuits! Closing.");
  801. return -1;
  802. }
  803. if (circuit_consider_stop_edge_reading(circ, conn->cpath_layer))
  804. return 0;
  805. if (conn->package_window <= 0) {
  806. log_fn(LOG_INFO,"called with package_window %d. Skipping.", conn->package_window);
  807. connection_stop_reading(conn);
  808. return 0;
  809. }
  810. amount_to_process = buf_datalen(conn->inbuf);
  811. if (!amount_to_process)
  812. return 0;
  813. if (!package_partial && amount_to_process < RELAY_PAYLOAD_SIZE)
  814. return 0;
  815. if (amount_to_process > RELAY_PAYLOAD_SIZE) {
  816. length = RELAY_PAYLOAD_SIZE;
  817. } else {
  818. length = amount_to_process;
  819. }
  820. stats_n_data_bytes_packaged += length;
  821. stats_n_data_cells_packaged += 1;
  822. connection_fetch_from_buf(payload, length, conn);
  823. log_fn(LOG_DEBUG,"(%d) Packaging %d bytes (%d waiting).", conn->s,
  824. (int)length, (int)buf_datalen(conn->inbuf));
  825. if (conn->type == CONN_TYPE_EXIT) {
  826. conn->stream_size += length;
  827. log_fn(LOG_DEBUG,"%d: Stream size now %d.", conn->s, (int)conn->stream_size);
  828. }
  829. if (connection_edge_send_command(conn, circ, RELAY_COMMAND_DATA,
  830. payload, length, conn->cpath_layer) < 0)
  831. return 0; /* circuit is closed, don't continue */
  832. if (!conn->cpath_layer) { /* non-rendezvous exit */
  833. tor_assert(circ->package_window > 0);
  834. circ->package_window--;
  835. } else { /* we're an AP, or an exit on a rendezvous circ */
  836. tor_assert(conn->cpath_layer->package_window > 0);
  837. conn->cpath_layer->package_window--;
  838. }
  839. if (--conn->package_window <= 0) { /* is it 0 after decrement? */
  840. connection_stop_reading(conn);
  841. log_fn(LOG_DEBUG,"conn->package_window reached 0.");
  842. circuit_consider_stop_edge_reading(circ, conn->cpath_layer);
  843. return 0; /* don't process the inbuf any more */
  844. }
  845. log_fn(LOG_DEBUG,"conn->package_window is now %d",conn->package_window);
  846. /* handle more if there's more, or return 0 if there isn't */
  847. goto repeat_connection_edge_package_raw_inbuf;
  848. }
  849. /** Called when we've just received a relay data cell, or when
  850. * we've just finished flushing all bytes to stream <b>conn</b>.
  851. *
  852. * If conn->outbuf is not too full, and our deliver window is
  853. * low, send back a suitable number of stream-level sendme cells.
  854. */
  855. void connection_edge_consider_sending_sendme(connection_t *conn) {
  856. circuit_t *circ;
  857. if (connection_outbuf_too_full(conn))
  858. return;
  859. circ = circuit_get_by_conn(conn);
  860. if (!circ) {
  861. /* this can legitimately happen if the destroy has already
  862. * arrived and torn down the circuit */
  863. log_fn(LOG_INFO,"No circuit associated with conn. Skipping.");
  864. return;
  865. }
  866. while (conn->deliver_window < STREAMWINDOW_START - STREAMWINDOW_INCREMENT) {
  867. log_fn(LOG_DEBUG,"Outbuf %d, Queueing stream sendme.", (int)conn->outbuf_flushlen);
  868. conn->deliver_window += STREAMWINDOW_INCREMENT;
  869. if (connection_edge_send_command(conn, circ, RELAY_COMMAND_SENDME,
  870. NULL, 0, conn->cpath_layer) < 0) {
  871. log_fn(LOG_WARN,"connection_edge_send_command failed. Returning.");
  872. return; /* the circuit's closed, don't continue */
  873. }
  874. }
  875. }
  876. /** The circuit <b>circ</b> has received a circuit-level sendme
  877. * (on hop <b>layer_hint</b>, if we're the OP). Go through all the
  878. * attached streams and let them resume reading and packaging, if
  879. * their stream windows allow it.
  880. */
  881. static void
  882. circuit_resume_edge_reading(circuit_t *circ, crypt_path_t *layer_hint)
  883. {
  884. log_fn(LOG_DEBUG,"resuming");
  885. /* have to check both n_streams and p_streams, to handle rendezvous */
  886. if (circuit_resume_edge_reading_helper(circ->n_streams, circ, layer_hint) >= 0)
  887. circuit_resume_edge_reading_helper(circ->p_streams, circ, layer_hint);
  888. }
  889. /** A helper function for circuit_resume_edge_reading() above.
  890. * The arguments are the same, except that <b>conn</b> is the head
  891. * of a linked list of edge streams that should each be considered.
  892. */
  893. static int
  894. circuit_resume_edge_reading_helper(connection_t *conn,
  895. circuit_t *circ,
  896. crypt_path_t *layer_hint) {
  897. for ( ; conn; conn=conn->next_stream) {
  898. if ((!layer_hint && conn->package_window > 0) ||
  899. (layer_hint && conn->package_window > 0 && conn->cpath_layer == layer_hint)) {
  900. connection_start_reading(conn);
  901. /* handle whatever might still be on the inbuf */
  902. connection_edge_package_raw_inbuf(conn, 1);
  903. /* If the circuit won't accept any more data, return without looking
  904. * at any more of the streams. Any connections that should be stopped
  905. * have already been stopped by connection_edge_package_raw_inbuf. */
  906. if (circuit_consider_stop_edge_reading(circ, layer_hint))
  907. return -1;
  908. }
  909. }
  910. return 0;
  911. }
  912. /** Check if the package window for <b>circ</b> is empty (at
  913. * hop <b>layer_hint</b> if it's defined).
  914. *
  915. * If yes, tell edge streams to stop reading and return 1.
  916. * Else return 0.
  917. */
  918. static int
  919. circuit_consider_stop_edge_reading(circuit_t *circ, crypt_path_t *layer_hint)
  920. {
  921. connection_t *conn = NULL;
  922. if (!layer_hint) {
  923. log_fn(LOG_DEBUG,"considering circ->package_window %d", circ->package_window);
  924. if (circ->package_window <= 0) {
  925. log_fn(LOG_DEBUG,"yes, not-at-origin. stopped.");
  926. for (conn = circ->n_streams; conn; conn=conn->next_stream)
  927. connection_stop_reading(conn);
  928. return 1;
  929. }
  930. return 0;
  931. }
  932. /* else, layer hint is defined, use it */
  933. log_fn(LOG_DEBUG,"considering layer_hint->package_window %d", layer_hint->package_window);
  934. if (layer_hint->package_window <= 0) {
  935. log_fn(LOG_DEBUG,"yes, at-origin. stopped.");
  936. for (conn = circ->n_streams; conn; conn=conn->next_stream)
  937. if (conn->cpath_layer == layer_hint)
  938. connection_stop_reading(conn);
  939. for (conn = circ->p_streams; conn; conn=conn->next_stream)
  940. if (conn->cpath_layer == layer_hint)
  941. connection_stop_reading(conn);
  942. return 1;
  943. }
  944. return 0;
  945. }
  946. /** Check if the deliver_window for circuit <b>circ</b> (at hop
  947. * <b>layer_hint</b> if it's defined) is low enough that we should
  948. * send a circuit-level sendme back down the circuit. If so, send
  949. * enough sendmes that the window would be overfull if we sent any
  950. * more.
  951. */
  952. static void
  953. circuit_consider_sending_sendme(circuit_t *circ, crypt_path_t *layer_hint)
  954. {
  955. // log_fn(LOG_INFO,"Considering: layer_hint is %s",
  956. // layer_hint ? "defined" : "null");
  957. while ((layer_hint ? layer_hint->deliver_window : circ->deliver_window) <
  958. CIRCWINDOW_START - CIRCWINDOW_INCREMENT) {
  959. log_fn(LOG_DEBUG,"Queueing circuit sendme.");
  960. if (layer_hint)
  961. layer_hint->deliver_window += CIRCWINDOW_INCREMENT;
  962. else
  963. circ->deliver_window += CIRCWINDOW_INCREMENT;
  964. if (connection_edge_send_command(NULL, circ, RELAY_COMMAND_SENDME,
  965. NULL, 0, layer_hint) < 0) {
  966. log_fn(LOG_WARN,"connection_edge_send_command failed. Circuit's closed.");
  967. return; /* the circuit's closed, don't continue */
  968. }
  969. }
  970. }