relay.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075
  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_addressmap(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 (conn->type != CONN_TYPE_AP) {
  485. log_fn(LOG_WARN,"Got an end because of resolvefailed, but we're not an AP. Closing.");
  486. return -1;
  487. }
  488. if (client_dns_incr_failures(conn->socks_request->address)
  489. < MAX_RESOLVE_FAILURES) {
  490. /* We haven't retried too many times; reattach the connection. */
  491. log_fn(LOG_INFO,"Resolve of '%s' failed, trying again.",
  492. conn->socks_request->address);
  493. circuit_log_path(LOG_INFO,circ);
  494. conn->state = AP_CONN_STATE_CIRCUIT_WAIT;
  495. circuit_detach_stream(circ,conn);
  496. tor_assert(circ->timestamp_dirty);
  497. circ->timestamp_dirty -= get_options()->MaxCircuitDirtiness;
  498. /* make sure not to expire/retry the stream quite yet */
  499. conn->timestamp_lastread = time(NULL);
  500. if (connection_ap_handshake_attach_circuit(conn) >= 0)
  501. return 0;
  502. /* else, conn will get closed below */
  503. log_fn(LOG_INFO,"Giving up on retrying (from resolvefailed); conn can't be handled.");
  504. } else {
  505. log_fn(LOG_NOTICE,"Have tried resolving address '%s' at %d different places. Giving up.",
  506. conn->socks_request->address, MAX_RESOLVE_FAILURES);
  507. }
  508. }
  509. log_fn(LOG_INFO,"Edge got end (%s) before we're connected. Marking for close.",
  510. connection_edge_end_reason(cell->payload+RELAY_HEADER_SIZE, rh->length));
  511. if (CIRCUIT_IS_ORIGIN(circ))
  512. circuit_log_path(LOG_INFO,circ);
  513. conn->has_sent_end = 1; /* we just got an 'end', don't need to send one */
  514. connection_mark_for_close(conn);
  515. return 0;
  516. }
  517. if (conn->type == CONN_TYPE_AP && rh->command == RELAY_COMMAND_CONNECTED) {
  518. if (conn->state != AP_CONN_STATE_CONNECT_WAIT) {
  519. log_fn(LOG_WARN,"Got 'connected' while not in state connect_wait. Dropping.");
  520. return 0;
  521. }
  522. // log_fn(LOG_INFO,"Connected! Notifying application.");
  523. conn->state = AP_CONN_STATE_OPEN;
  524. log_fn(LOG_INFO,"'connected' received after %d seconds.",
  525. (int)(time(NULL) - conn->timestamp_lastread));
  526. if (rh->length >= 4) {
  527. addr = ntohl(get_uint32(cell->payload+RELAY_HEADER_SIZE));
  528. if (!addr) {
  529. log_fn(LOG_INFO,"...but it claims the IP address was 0.0.0.0. Closing.");
  530. connection_edge_end(conn, END_STREAM_REASON_MISC, conn->cpath_layer);
  531. connection_mark_for_close(conn);
  532. return 0;
  533. }
  534. client_dns_set_addressmap(conn->socks_request->address, addr);
  535. }
  536. circuit_log_path(LOG_INFO,circ);
  537. connection_ap_handshake_socks_reply(conn, NULL, 0, 1);
  538. conn->socks_request->has_finished = 1;
  539. /* handle anything that might have queued */
  540. if (connection_edge_package_raw_inbuf(conn, 1) < 0) {
  541. connection_edge_end(conn, END_STREAM_REASON_MISC, conn->cpath_layer);
  542. connection_mark_for_close(conn);
  543. return 0;
  544. }
  545. return 0;
  546. }
  547. if (conn->type == CONN_TYPE_AP && rh->command == RELAY_COMMAND_RESOLVED) {
  548. if (conn->state != AP_CONN_STATE_RESOLVE_WAIT) {
  549. log_fn(LOG_WARN,"Got a 'resolved' cell while not in state resolve_wait. Dropping.");
  550. return 0;
  551. }
  552. tor_assert(conn->socks_request->command == SOCKS_COMMAND_RESOLVE);
  553. if (rh->length < 2 || cell->payload[RELAY_HEADER_SIZE+1]+2>rh->length) {
  554. log_fn(LOG_WARN, "Dropping malformed 'resolved' cell");
  555. conn->has_sent_end = 1;
  556. connection_mark_for_close(conn);
  557. return 0;
  558. }
  559. connection_ap_handshake_socks_resolved(conn,
  560. cell->payload[RELAY_HEADER_SIZE], /*answer_type*/
  561. cell->payload[RELAY_HEADER_SIZE+1], /*answer_len*/
  562. cell->payload+RELAY_HEADER_SIZE+2); /* answer */
  563. conn->has_sent_end = 1;
  564. connection_mark_for_close(conn);
  565. conn->hold_open_until_flushed = 1;
  566. return 0;
  567. }
  568. log_fn(LOG_WARN,"Got an unexpected relay command %d, in state %d (%s). Closing.",
  569. rh->command, conn->state, conn_state_to_string[conn->type][conn->state]);
  570. connection_edge_end(conn, END_STREAM_REASON_MISC, conn->cpath_layer);
  571. connection_mark_for_close(conn);
  572. return -1;
  573. }
  574. /** An incoming relay cell has arrived on circuit <b>circ</b>. If
  575. * <b>conn</b> is NULL this is a control cell, else <b>cell</b> is
  576. * destined for <b>conn</b>.
  577. *
  578. * If <b>layer_hint</b> is defined, then we're the origin of the
  579. * circuit, and it specifies the hop that packaged <b>cell</b>.
  580. *
  581. * Return -1 if you want to tear down the circuit, else 0.
  582. */
  583. static int
  584. connection_edge_process_relay_cell(cell_t *cell, circuit_t *circ,
  585. connection_t *conn,
  586. crypt_path_t *layer_hint)
  587. {
  588. static int num_seen=0;
  589. relay_header_t rh;
  590. tor_assert(cell);
  591. tor_assert(circ);
  592. relay_header_unpack(&rh, cell->payload);
  593. // log_fn(LOG_DEBUG,"command %d stream %d", rh.command, rh.stream_id);
  594. num_seen++;
  595. log_fn(LOG_DEBUG,"Now seen %d relay cells here.", num_seen);
  596. /* either conn is NULL, in which case we've got a control cell, or else
  597. * conn points to the recognized stream. */
  598. if (conn &&
  599. conn->state != AP_CONN_STATE_OPEN &&
  600. conn->state != EXIT_CONN_STATE_OPEN) {
  601. return connection_edge_process_relay_cell_not_open(
  602. &rh, cell, circ, conn, layer_hint);
  603. }
  604. switch (rh.command) {
  605. case RELAY_COMMAND_DROP:
  606. log_fn(LOG_INFO,"Got a relay-level padding cell. Dropping.");
  607. return 0;
  608. case RELAY_COMMAND_BEGIN:
  609. if (layer_hint &&
  610. circ->purpose != CIRCUIT_PURPOSE_S_REND_JOINED) {
  611. log_fn(LOG_WARN,"relay begin request unsupported at AP. Dropping.");
  612. return 0;
  613. }
  614. if (conn) {
  615. log_fn(LOG_WARN,"begin cell for known stream. Dropping.");
  616. return 0;
  617. }
  618. connection_exit_begin_conn(cell, circ);
  619. return 0;
  620. case RELAY_COMMAND_DATA:
  621. ++stats_n_data_cells_received;
  622. if (( layer_hint && --layer_hint->deliver_window < 0) ||
  623. (!layer_hint && --circ->deliver_window < 0)) {
  624. log_fn(LOG_WARN,"(relay data) circ deliver_window below 0. Killing.");
  625. connection_edge_end(conn, END_STREAM_REASON_MISC, conn->cpath_layer);
  626. connection_mark_for_close(conn);
  627. return -1;
  628. }
  629. log_fn(LOG_DEBUG,"circ deliver_window now %d.", layer_hint ?
  630. layer_hint->deliver_window : circ->deliver_window);
  631. circuit_consider_sending_sendme(circ, layer_hint);
  632. if (!conn) {
  633. log_fn(LOG_INFO,"data cell dropped, unknown stream.");
  634. return 0;
  635. }
  636. if (--conn->deliver_window < 0) { /* is it below 0 after decrement? */
  637. log_fn(LOG_WARN,"(relay data) conn deliver_window below 0. Killing.");
  638. return -1; /* somebody's breaking protocol. kill the whole circuit. */
  639. }
  640. stats_n_data_bytes_received += rh.length;
  641. if (conn->type == CONN_TYPE_AP) {
  642. conn->stream_size += rh.length;
  643. log_fn(LOG_DEBUG,"%d: stream size now %d.", conn->s, (int)conn->stream_size);
  644. }
  645. connection_write_to_buf(cell->payload + RELAY_HEADER_SIZE,
  646. rh.length, conn);
  647. connection_edge_consider_sending_sendme(conn);
  648. return 0;
  649. case RELAY_COMMAND_END:
  650. if (!conn) {
  651. log_fn(LOG_INFO,"end cell (%s) dropped, unknown stream.",
  652. connection_edge_end_reason(cell->payload+RELAY_HEADER_SIZE, rh.length));
  653. return 0;
  654. }
  655. /* XXX add to this log_fn the exit node's nickname? */
  656. log_fn(LOG_INFO,"%d: end cell (%s) for stream %d. Removing stream. Size %d.",
  657. conn->s,
  658. connection_edge_end_reason(cell->payload+RELAY_HEADER_SIZE, rh.length),
  659. conn->stream_id, (int)conn->stream_size);
  660. #ifdef HALF_OPEN
  661. conn->done_sending = 1;
  662. shutdown(conn->s, 1); /* XXX check return; refactor NM */
  663. if (conn->done_receiving) {
  664. /* We just *got* an end; no reason to send one. */
  665. conn->has_sent_end = 1;
  666. connection_mark_for_close(conn);
  667. conn->hold_open_until_flushed = 1;
  668. }
  669. #else
  670. /* We just *got* an end; no reason to send one. */
  671. conn->has_sent_end = 1;
  672. if (!conn->marked_for_close) {
  673. /* only mark it if not already marked. it's possible to
  674. * get the 'end' right around when the client hangs up on us. */
  675. connection_mark_for_close(conn);
  676. conn->hold_open_until_flushed = 1;
  677. }
  678. #endif
  679. return 0;
  680. case RELAY_COMMAND_EXTEND:
  681. if (conn) {
  682. log_fn(LOG_WARN,"'extend' for non-zero stream. Dropping.");
  683. return 0;
  684. }
  685. return circuit_extend(cell, circ);
  686. case RELAY_COMMAND_EXTENDED:
  687. if (!layer_hint) {
  688. log_fn(LOG_WARN,"'extended' unsupported at non-origin. Dropping.");
  689. return 0;
  690. }
  691. log_fn(LOG_DEBUG,"Got an extended cell! Yay.");
  692. if (circuit_finish_handshake(circ, cell->payload+RELAY_HEADER_SIZE) < 0) {
  693. log_fn(LOG_WARN,"circuit_finish_handshake failed.");
  694. return -1;
  695. }
  696. if (circuit_send_next_onion_skin(circ)<0) {
  697. log_fn(LOG_INFO,"circuit_send_next_onion_skin() failed.");
  698. return -1;
  699. }
  700. return 0;
  701. case RELAY_COMMAND_TRUNCATE:
  702. if (layer_hint) {
  703. log_fn(LOG_WARN,"'truncate' unsupported at origin. Dropping.");
  704. return 0;
  705. }
  706. if (circ->n_conn) {
  707. connection_send_destroy(circ->n_circ_id, circ->n_conn);
  708. circ->n_conn = NULL;
  709. }
  710. log_fn(LOG_DEBUG, "Processed 'truncate', replying.");
  711. connection_edge_send_command(NULL, circ, RELAY_COMMAND_TRUNCATED,
  712. NULL, 0, NULL);
  713. return 0;
  714. case RELAY_COMMAND_TRUNCATED:
  715. if (!layer_hint) {
  716. log_fn(LOG_WARN,"'truncated' unsupported at non-origin. Dropping.");
  717. return 0;
  718. }
  719. circuit_truncated(circ, layer_hint);
  720. return 0;
  721. case RELAY_COMMAND_CONNECTED:
  722. if (conn) {
  723. log_fn(LOG_WARN,"'connected' unsupported while open. Closing circ.");
  724. return -1;
  725. }
  726. log_fn(LOG_INFO,"'connected' received, no conn attached anymore. Ignoring.");
  727. return 0;
  728. case RELAY_COMMAND_SENDME:
  729. if (!conn) {
  730. if (layer_hint) {
  731. layer_hint->package_window += CIRCWINDOW_INCREMENT;
  732. log_fn(LOG_DEBUG,"circ-level sendme at origin, packagewindow %d.",
  733. layer_hint->package_window);
  734. circuit_resume_edge_reading(circ, layer_hint);
  735. } else {
  736. circ->package_window += CIRCWINDOW_INCREMENT;
  737. log_fn(LOG_DEBUG,"circ-level sendme at non-origin, packagewindow %d.",
  738. circ->package_window);
  739. circuit_resume_edge_reading(circ, layer_hint);
  740. }
  741. return 0;
  742. }
  743. conn->package_window += STREAMWINDOW_INCREMENT;
  744. log_fn(LOG_DEBUG,"stream-level sendme, packagewindow now %d.", conn->package_window);
  745. connection_start_reading(conn);
  746. connection_edge_package_raw_inbuf(conn, 1); /* handle whatever might still be on the inbuf */
  747. return 0;
  748. case RELAY_COMMAND_RESOLVE:
  749. if (layer_hint) {
  750. log_fn(LOG_WARN,"resolve request unsupported at AP; dropping.");
  751. return 0;
  752. } else if (conn) {
  753. log_fn(LOG_WARN, "resolve request for known stream; dropping.");
  754. return 0;
  755. } else if (circ->purpose != CIRCUIT_PURPOSE_OR) {
  756. log_fn(LOG_WARN, "resolve request on circ with purpose %d; dropping",
  757. circ->purpose);
  758. return 0;
  759. }
  760. connection_exit_begin_resolve(cell, circ);
  761. return 0;
  762. case RELAY_COMMAND_RESOLVED:
  763. if (conn) {
  764. log_fn(LOG_WARN,"'resolved' unsupported while open. Closing circ.");
  765. return -1;
  766. }
  767. log_fn(LOG_INFO,"'resolved' received, no conn attached anymore. Ignoring.");
  768. return 0;
  769. case RELAY_COMMAND_ESTABLISH_INTRO:
  770. case RELAY_COMMAND_ESTABLISH_RENDEZVOUS:
  771. case RELAY_COMMAND_INTRODUCE1:
  772. case RELAY_COMMAND_INTRODUCE2:
  773. case RELAY_COMMAND_INTRODUCE_ACK:
  774. case RELAY_COMMAND_RENDEZVOUS1:
  775. case RELAY_COMMAND_RENDEZVOUS2:
  776. case RELAY_COMMAND_INTRO_ESTABLISHED:
  777. case RELAY_COMMAND_RENDEZVOUS_ESTABLISHED:
  778. rend_process_relay_cell(circ, rh.command, rh.length,
  779. cell->payload+RELAY_HEADER_SIZE);
  780. return 0;
  781. }
  782. log_fn(LOG_WARN,"unknown relay command %d.",rh.command);
  783. return -1;
  784. }
  785. uint64_t stats_n_data_cells_packaged = 0;
  786. uint64_t stats_n_data_bytes_packaged = 0;
  787. uint64_t stats_n_data_cells_received = 0;
  788. uint64_t stats_n_data_bytes_received = 0;
  789. /** While conn->inbuf has an entire relay payload of bytes on it,
  790. * and the appropriate package windows aren't empty, grab a cell
  791. * and send it down the circuit.
  792. *
  793. * Return -1 if conn should be marked for close, else return 0.
  794. */
  795. int connection_edge_package_raw_inbuf(connection_t *conn, int package_partial) {
  796. size_t amount_to_process, length;
  797. char payload[CELL_PAYLOAD_SIZE];
  798. circuit_t *circ;
  799. tor_assert(conn);
  800. tor_assert(!connection_speaks_cells(conn));
  801. repeat_connection_edge_package_raw_inbuf:
  802. circ = circuit_get_by_conn(conn);
  803. if (!circ) {
  804. log_fn(LOG_INFO,"conn has no circuits! Closing.");
  805. return -1;
  806. }
  807. if (circuit_consider_stop_edge_reading(circ, conn->cpath_layer))
  808. return 0;
  809. if (conn->package_window <= 0) {
  810. log_fn(LOG_INFO,"called with package_window %d. Skipping.", conn->package_window);
  811. connection_stop_reading(conn);
  812. return 0;
  813. }
  814. amount_to_process = buf_datalen(conn->inbuf);
  815. if (!amount_to_process)
  816. return 0;
  817. if (!package_partial && amount_to_process < RELAY_PAYLOAD_SIZE)
  818. return 0;
  819. if (amount_to_process > RELAY_PAYLOAD_SIZE) {
  820. length = RELAY_PAYLOAD_SIZE;
  821. } else {
  822. length = amount_to_process;
  823. }
  824. stats_n_data_bytes_packaged += length;
  825. stats_n_data_cells_packaged += 1;
  826. connection_fetch_from_buf(payload, length, conn);
  827. log_fn(LOG_DEBUG,"(%d) Packaging %d bytes (%d waiting).", conn->s,
  828. (int)length, (int)buf_datalen(conn->inbuf));
  829. if (conn->type == CONN_TYPE_EXIT) {
  830. conn->stream_size += length;
  831. log_fn(LOG_DEBUG,"%d: Stream size now %d.", conn->s, (int)conn->stream_size);
  832. }
  833. if (connection_edge_send_command(conn, circ, RELAY_COMMAND_DATA,
  834. payload, length, conn->cpath_layer) < 0)
  835. return 0; /* circuit is closed, don't continue */
  836. if (!conn->cpath_layer) { /* non-rendezvous exit */
  837. tor_assert(circ->package_window > 0);
  838. circ->package_window--;
  839. } else { /* we're an AP, or an exit on a rendezvous circ */
  840. tor_assert(conn->cpath_layer->package_window > 0);
  841. conn->cpath_layer->package_window--;
  842. }
  843. if (--conn->package_window <= 0) { /* is it 0 after decrement? */
  844. connection_stop_reading(conn);
  845. log_fn(LOG_DEBUG,"conn->package_window reached 0.");
  846. circuit_consider_stop_edge_reading(circ, conn->cpath_layer);
  847. return 0; /* don't process the inbuf any more */
  848. }
  849. log_fn(LOG_DEBUG,"conn->package_window is now %d",conn->package_window);
  850. /* handle more if there's more, or return 0 if there isn't */
  851. goto repeat_connection_edge_package_raw_inbuf;
  852. }
  853. /** Called when we've just received a relay data cell, or when
  854. * we've just finished flushing all bytes to stream <b>conn</b>.
  855. *
  856. * If conn->outbuf is not too full, and our deliver window is
  857. * low, send back a suitable number of stream-level sendme cells.
  858. */
  859. void connection_edge_consider_sending_sendme(connection_t *conn) {
  860. circuit_t *circ;
  861. if (connection_outbuf_too_full(conn))
  862. return;
  863. circ = circuit_get_by_conn(conn);
  864. if (!circ) {
  865. /* this can legitimately happen if the destroy has already
  866. * arrived and torn down the circuit */
  867. log_fn(LOG_INFO,"No circuit associated with conn. Skipping.");
  868. return;
  869. }
  870. while (conn->deliver_window < STREAMWINDOW_START - STREAMWINDOW_INCREMENT) {
  871. log_fn(LOG_DEBUG,"Outbuf %d, Queueing stream sendme.", (int)conn->outbuf_flushlen);
  872. conn->deliver_window += STREAMWINDOW_INCREMENT;
  873. if (connection_edge_send_command(conn, circ, RELAY_COMMAND_SENDME,
  874. NULL, 0, conn->cpath_layer) < 0) {
  875. log_fn(LOG_WARN,"connection_edge_send_command failed. Returning.");
  876. return; /* the circuit's closed, don't continue */
  877. }
  878. }
  879. }
  880. /** The circuit <b>circ</b> has received a circuit-level sendme
  881. * (on hop <b>layer_hint</b>, if we're the OP). Go through all the
  882. * attached streams and let them resume reading and packaging, if
  883. * their stream windows allow it.
  884. */
  885. static void
  886. circuit_resume_edge_reading(circuit_t *circ, crypt_path_t *layer_hint)
  887. {
  888. log_fn(LOG_DEBUG,"resuming");
  889. /* have to check both n_streams and p_streams, to handle rendezvous */
  890. if (circuit_resume_edge_reading_helper(circ->n_streams, circ, layer_hint) >= 0)
  891. circuit_resume_edge_reading_helper(circ->p_streams, circ, layer_hint);
  892. }
  893. /** A helper function for circuit_resume_edge_reading() above.
  894. * The arguments are the same, except that <b>conn</b> is the head
  895. * of a linked list of edge streams that should each be considered.
  896. */
  897. static int
  898. circuit_resume_edge_reading_helper(connection_t *conn,
  899. circuit_t *circ,
  900. crypt_path_t *layer_hint) {
  901. for ( ; conn; conn=conn->next_stream) {
  902. if ((!layer_hint && conn->package_window > 0) ||
  903. (layer_hint && conn->package_window > 0 && conn->cpath_layer == layer_hint)) {
  904. connection_start_reading(conn);
  905. /* handle whatever might still be on the inbuf */
  906. connection_edge_package_raw_inbuf(conn, 1);
  907. /* If the circuit won't accept any more data, return without looking
  908. * at any more of the streams. Any connections that should be stopped
  909. * have already been stopped by connection_edge_package_raw_inbuf. */
  910. if (circuit_consider_stop_edge_reading(circ, layer_hint))
  911. return -1;
  912. }
  913. }
  914. return 0;
  915. }
  916. /** Check if the package window for <b>circ</b> is empty (at
  917. * hop <b>layer_hint</b> if it's defined).
  918. *
  919. * If yes, tell edge streams to stop reading and return 1.
  920. * Else return 0.
  921. */
  922. static int
  923. circuit_consider_stop_edge_reading(circuit_t *circ, crypt_path_t *layer_hint)
  924. {
  925. connection_t *conn = NULL;
  926. if (!layer_hint) {
  927. log_fn(LOG_DEBUG,"considering circ->package_window %d", circ->package_window);
  928. if (circ->package_window <= 0) {
  929. log_fn(LOG_DEBUG,"yes, not-at-origin. stopped.");
  930. for (conn = circ->n_streams; conn; conn=conn->next_stream)
  931. connection_stop_reading(conn);
  932. return 1;
  933. }
  934. return 0;
  935. }
  936. /* else, layer hint is defined, use it */
  937. log_fn(LOG_DEBUG,"considering layer_hint->package_window %d", layer_hint->package_window);
  938. if (layer_hint->package_window <= 0) {
  939. log_fn(LOG_DEBUG,"yes, at-origin. stopped.");
  940. for (conn = circ->n_streams; conn; conn=conn->next_stream)
  941. if (conn->cpath_layer == layer_hint)
  942. connection_stop_reading(conn);
  943. for (conn = circ->p_streams; conn; conn=conn->next_stream)
  944. if (conn->cpath_layer == layer_hint)
  945. connection_stop_reading(conn);
  946. return 1;
  947. }
  948. return 0;
  949. }
  950. /** Check if the deliver_window for circuit <b>circ</b> (at hop
  951. * <b>layer_hint</b> if it's defined) is low enough that we should
  952. * send a circuit-level sendme back down the circuit. If so, send
  953. * enough sendmes that the window would be overfull if we sent any
  954. * more.
  955. */
  956. static void
  957. circuit_consider_sending_sendme(circuit_t *circ, crypt_path_t *layer_hint)
  958. {
  959. // log_fn(LOG_INFO,"Considering: layer_hint is %s",
  960. // layer_hint ? "defined" : "null");
  961. while ((layer_hint ? layer_hint->deliver_window : circ->deliver_window) <
  962. CIRCWINDOW_START - CIRCWINDOW_INCREMENT) {
  963. log_fn(LOG_DEBUG,"Queueing circuit sendme.");
  964. if (layer_hint)
  965. layer_hint->deliver_window += CIRCWINDOW_INCREMENT;
  966. else
  967. circ->deliver_window += CIRCWINDOW_INCREMENT;
  968. if (connection_edge_send_command(NULL, circ, RELAY_COMMAND_SENDME,
  969. NULL, 0, layer_hint) < 0) {
  970. log_fn(LOG_WARN,"connection_edge_send_command failed. Circuit's closed.");
  971. return; /* the circuit's closed, don't continue */
  972. }
  973. }
  974. }