first iteration of scrubbing sensitive strings from logs.
[tor.git] / src / or / relay.c
blob631c1ccf49ae9415ac76529bd1b9f76db0996bc5
1 /* Copyright 2001 Matej Pfajfar.
2 * Copyright 2001-2004 Roger Dingledine.
3 * Copyright 2004-2005 Roger Dingledine, Nick Mathewson. */
4 /* See LICENSE for licensing information */
5 /* $Id$ */
6 const char relay_c_id[] = "$Id$";
8 /**
9 * \file relay.c
10 * \brief Handle relay cell encryption/decryption, plus packaging and
11 * receiving from circuits.
12 **/
14 #include "or.h"
16 static int relay_crypt(circuit_t *circ, cell_t *cell, int cell_direction,
17 crypt_path_t **layer_hint, char *recognized);
18 static connection_t *relay_lookup_conn(circuit_t *circ, cell_t *cell, int cell_direction);
20 static int
21 connection_edge_process_relay_cell(cell_t *cell, circuit_t *circ,
22 connection_t *conn,
23 crypt_path_t *layer_hint);
24 static void
25 circuit_consider_sending_sendme(circuit_t *circ, crypt_path_t *layer_hint);
26 static void
27 circuit_resume_edge_reading(circuit_t *circ, crypt_path_t *layer_hint);
28 static int
29 circuit_resume_edge_reading_helper(connection_t *conn,
30 circuit_t *circ,
31 crypt_path_t *layer_hint);
32 static int
33 circuit_consider_stop_edge_reading(circuit_t *circ, crypt_path_t *layer_hint);
35 /** Stats: how many relay cells have originated at this hop, or have
36 * been relayed onward (not recognized at this hop)?
38 unsigned long stats_n_relay_cells_relayed = 0;
39 /** Stats: how many relay cells have been delivered to streams at this
40 * hop?
42 unsigned long stats_n_relay_cells_delivered = 0;
44 /** Update digest from the payload of cell. Assign integrity part to
45 * cell.
47 static void relay_set_digest(crypto_digest_env_t *digest, cell_t *cell) {
48 char integrity[4];
49 relay_header_t rh;
51 crypto_digest_add_bytes(digest, cell->payload, CELL_PAYLOAD_SIZE);
52 crypto_digest_get_digest(digest, integrity, 4);
53 // log_fn(LOG_DEBUG,"Putting digest of %u %u %u %u into relay cell.",
54 // integrity[0], integrity[1], integrity[2], integrity[3]);
55 relay_header_unpack(&rh, cell->payload);
56 memcpy(rh.integrity, integrity, 4);
57 relay_header_pack(cell->payload, &rh);
60 /** Does the digest for this circuit indicate that this cell is for us?
62 * Update digest from the payload of cell (with the integrity part set
63 * to 0). If the integrity part is valid, return 1, else restore digest
64 * and cell to their original state and return 0.
66 static int relay_digest_matches(crypto_digest_env_t *digest, cell_t *cell) {
67 char received_integrity[4], calculated_integrity[4];
68 relay_header_t rh;
69 crypto_digest_env_t *backup_digest=NULL;
71 backup_digest = crypto_digest_dup(digest);
73 relay_header_unpack(&rh, cell->payload);
74 memcpy(received_integrity, rh.integrity, 4);
75 memset(rh.integrity, 0, 4);
76 relay_header_pack(cell->payload, &rh);
78 // log_fn(LOG_DEBUG,"Reading digest of %u %u %u %u from relay cell.",
79 // received_integrity[0], received_integrity[1],
80 // received_integrity[2], received_integrity[3]);
82 crypto_digest_add_bytes(digest, cell->payload, CELL_PAYLOAD_SIZE);
83 crypto_digest_get_digest(digest, calculated_integrity, 4);
85 if (memcmp(received_integrity, calculated_integrity, 4)) {
86 // log_fn(LOG_INFO,"Recognized=0 but bad digest. Not recognizing.");
87 // (%d vs %d).", received_integrity, calculated_integrity);
88 /* restore digest to its old form */
89 crypto_digest_assign(digest, backup_digest);
90 /* restore the relay header */
91 memcpy(rh.integrity, received_integrity, 4);
92 relay_header_pack(cell->payload, &rh);
93 crypto_free_digest_env(backup_digest);
94 return 0;
96 crypto_free_digest_env(backup_digest);
97 return 1;
100 /** Apply <b>cipher</b> to CELL_PAYLOAD_SIZE bytes of <b>in</b>
101 * (in place).
103 * If <b>encrypt_mode</b> is 1 then encrypt, else decrypt.
105 * Return -1 if the crypto fails, else return 0.
107 static int relay_crypt_one_payload(crypto_cipher_env_t *cipher, char *in,
108 int encrypt_mode) {
109 char out[CELL_PAYLOAD_SIZE]; /* 'in' must be this size too */
111 if (( encrypt_mode && crypto_cipher_encrypt(cipher, out, in, CELL_PAYLOAD_SIZE)) ||
112 (!encrypt_mode && crypto_cipher_decrypt(cipher, out, in, CELL_PAYLOAD_SIZE))) {
113 log_fn(LOG_WARN,"Error during relay encryption");
114 return -1;
116 memcpy(in,out,CELL_PAYLOAD_SIZE);
117 return 0;
120 /** Receive a relay cell:
121 * - Crypt it (encrypt APward, decrypt at AP, decrypt exitward).
122 * - Check if recognized (if exitward).
123 * - If recognized and the digest checks out, then find if there's
124 * a conn that the cell is intended for, and deliver it to·
125 * connection_edge.
126 * - Else connection_or_write_cell_to_buf to the conn on the other
127 * side of the circuit.
129 int circuit_receive_relay_cell(cell_t *cell, circuit_t *circ,
130 int cell_direction) {
131 connection_t *conn=NULL;
132 crypt_path_t *layer_hint=NULL;
133 char recognized=0;
135 tor_assert(cell);
136 tor_assert(circ);
137 tor_assert(cell_direction == CELL_DIRECTION_OUT ||
138 cell_direction == CELL_DIRECTION_IN);
139 if (circ->marked_for_close)
140 return 0;
142 if (relay_crypt(circ, cell, cell_direction, &layer_hint, &recognized) < 0) {
143 log_fn(LOG_WARN,"relay crypt failed. Dropping connection.");
144 return -1;
147 if (recognized) {
148 conn = relay_lookup_conn(circ, cell, cell_direction);
149 if (cell_direction == CELL_DIRECTION_OUT) {
150 ++stats_n_relay_cells_delivered;
151 log_fn(LOG_DEBUG,"Sending away from origin.");
152 if (connection_edge_process_relay_cell(cell, circ, conn, NULL) < 0) {
153 log_fn(LOG_WARN,"connection_edge_process_relay_cell (away from origin) failed.");
154 return -1;
157 if (cell_direction == CELL_DIRECTION_IN) {
158 ++stats_n_relay_cells_delivered;
159 log_fn(LOG_DEBUG,"Sending to origin.");
160 if (connection_edge_process_relay_cell(cell, circ, conn, layer_hint) < 0) {
161 log_fn(LOG_WARN,"connection_edge_process_relay_cell (at origin) failed.");
162 return -1;
165 return 0;
168 /* not recognized. pass it on. */
169 if (cell_direction == CELL_DIRECTION_OUT) {
170 cell->circ_id = circ->n_circ_id; /* switch it */
171 conn = circ->n_conn;
172 } else {
173 cell->circ_id = circ->p_circ_id; /* switch it */
174 conn = circ->p_conn;
177 if (!conn) {
178 if (circ->rend_splice && cell_direction == CELL_DIRECTION_OUT) {
179 tor_assert(circ->purpose == CIRCUIT_PURPOSE_REND_ESTABLISHED);
180 tor_assert(circ->rend_splice->purpose == CIRCUIT_PURPOSE_REND_ESTABLISHED);
181 cell->circ_id = circ->rend_splice->p_circ_id;
182 if (circuit_receive_relay_cell(cell, circ->rend_splice, CELL_DIRECTION_IN)<0) {
183 log_fn(LOG_WARN, "Error relaying cell across rendezvous; closing circuits");
184 circuit_mark_for_close(circ); /* XXXX Do this here, or just return -1? */
185 return -1;
187 return 0;
189 log_fn(LOG_WARN,"Didn't recognize cell, but circ stops here! Closing circ.");
190 return -1;
193 log_fn(LOG_DEBUG,"Passing on unrecognized cell.");
194 ++stats_n_relay_cells_relayed;
195 connection_or_write_cell_to_buf(cell, conn);
196 return 0;
199 /** Do the appropriate en/decryptions for <b>cell</b> arriving on
200 * <b>circ</b> in direction <b>cell_direction</b>.
202 * If cell_direction == CELL_DIRECTION_IN:
203 * - If we're at the origin (we're the OP), for hops 1..N,
204 * decrypt cell. If recognized, stop.
205 * - Else (we're not the OP), encrypt one hop. Cell is not recognized.
207 * If cell_direction == CELL_DIRECTION_OUT:
208 * - decrypt one hop. Check if recognized.
210 * If cell is recognized, set *recognized to 1, and set
211 * *layer_hint to the hop that recognized it.
213 * Return -1 to indicate that we should mark the circuit for close,
214 * else return 0.
216 /* wrap this into receive_relay_cell one day */
217 static int relay_crypt(circuit_t *circ, cell_t *cell, int cell_direction,
218 crypt_path_t **layer_hint, char *recognized) {
219 crypt_path_t *thishop;
220 relay_header_t rh;
222 tor_assert(circ);
223 tor_assert(cell);
224 tor_assert(recognized);
225 tor_assert(cell_direction == CELL_DIRECTION_IN ||
226 cell_direction == CELL_DIRECTION_OUT);
228 if (cell_direction == CELL_DIRECTION_IN) {
229 if (CIRCUIT_IS_ORIGIN(circ)) { /* We're at the beginning of the circuit.
230 We'll want to do layered decrypts. */
231 tor_assert(circ->cpath);
232 thishop = circ->cpath;
233 if (thishop->state != CPATH_STATE_OPEN) {
234 log_fn(LOG_WARN,"Relay cell before first created cell? Closing.");
235 return -1;
237 do { /* Remember: cpath is in forward order, that is, first hop first. */
238 tor_assert(thishop);
240 if (relay_crypt_one_payload(thishop->b_crypto, cell->payload, 0) < 0)
241 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(thishop->b_digest, cell)) {
247 *recognized = 1;
248 *layer_hint = thishop;
249 return 0;
253 thishop = thishop->next;
254 } while (thishop != circ->cpath && thishop->state == CPATH_STATE_OPEN);
255 log_fn(LOG_WARN,"in-cell at OP not recognized. Closing.");
256 return -1;
257 } else { /* we're in the middle. Just one crypt. */
258 if (relay_crypt_one_payload(circ->p_crypto, cell->payload, 1) < 0)
259 return -1;
260 // log_fn(LOG_DEBUG,"Skipping recognized check, because we're not the OP.");
262 } else /* cell_direction == CELL_DIRECTION_OUT */ {
263 /* we're in the middle. Just one crypt. */
265 if (relay_crypt_one_payload(circ->n_crypto, cell->payload, 0) < 0)
266 return -1;
268 relay_header_unpack(&rh, cell->payload);
269 if (rh.recognized == 0) {
270 /* it's possibly recognized. have to check digest to be sure. */
271 if (relay_digest_matches(circ->n_digest, cell)) {
272 *recognized = 1;
273 return 0;
277 return 0;
280 /** Package a relay cell:
281 * - Encrypt it to the right layer
282 * - connection_or_write_cell_to_buf to the right conn
284 static int
285 circuit_package_relay_cell(cell_t *cell, circuit_t *circ,
286 int cell_direction,
287 crypt_path_t *layer_hint)
289 connection_t *conn; /* where to send the cell */
290 crypt_path_t *thishop; /* counter for repeated crypts */
292 if (cell_direction == CELL_DIRECTION_OUT) {
293 conn = circ->n_conn;
294 if (!conn) {
295 log_fn(LOG_WARN,"outgoing relay cell has n_conn==NULL. Dropping.");
296 return 0; /* just drop it */
298 relay_set_digest(layer_hint->f_digest, cell);
300 thishop = layer_hint;
301 /* moving from farthest to nearest hop */
302 do {
303 tor_assert(thishop);
305 log_fn(LOG_DEBUG,"crypting a layer of the relay cell.");
306 if (relay_crypt_one_payload(thishop->f_crypto, cell->payload, 1) < 0) {
307 return -1;
310 thishop = thishop->prev;
311 } while (thishop != circ->cpath->prev);
313 } else { /* incoming cell */
314 conn = circ->p_conn;
315 if (!conn) {
316 log_fn(LOG_WARN,"incoming relay cell has p_conn==NULL. Dropping.");
317 return 0; /* just drop it */
319 relay_set_digest(circ->p_digest, cell);
320 if (relay_crypt_one_payload(circ->p_crypto, cell->payload, 1) < 0)
321 return -1;
323 ++stats_n_relay_cells_relayed;
324 connection_or_write_cell_to_buf(cell, conn);
325 return 0;
328 /** If cell's stream_id matches the stream_id of any conn that's
329 * attached to circ, return that conn, else return NULL.
331 static connection_t *
332 relay_lookup_conn(circuit_t *circ, cell_t *cell, int cell_direction)
334 connection_t *tmpconn;
335 relay_header_t rh;
337 relay_header_unpack(&rh, cell->payload);
339 if (!rh.stream_id)
340 return NULL;
342 /* IN or OUT cells could have come from either direction, now
343 * that we allow rendezvous *to* an OP.
346 for (tmpconn = circ->n_streams; tmpconn; tmpconn=tmpconn->next_stream) {
347 if (rh.stream_id == tmpconn->stream_id && !tmpconn->marked_for_close) {
348 log_fn(LOG_DEBUG,"found conn for stream %d.", rh.stream_id);
349 if (cell_direction == CELL_DIRECTION_OUT ||
350 connection_edge_is_rendezvous_stream(tmpconn))
351 return tmpconn;
354 for (tmpconn = circ->p_streams; tmpconn; tmpconn=tmpconn->next_stream) {
355 if (rh.stream_id == tmpconn->stream_id && !tmpconn->marked_for_close) {
356 log_fn(LOG_DEBUG,"found conn for stream %d.", rh.stream_id);
357 return tmpconn;
360 for (tmpconn = circ->resolving_streams; tmpconn; tmpconn=tmpconn->next_stream) {
361 if (rh.stream_id == tmpconn->stream_id && !tmpconn->marked_for_close) {
362 log_fn(LOG_DEBUG,"found conn for stream %d.", rh.stream_id);
363 return tmpconn;
366 return NULL; /* probably a begin relay cell */
369 /** Pack the relay_header_t host-order structure <b>src</b> into
370 * network-order in the buffer <b>dest</b>. See tor-spec.txt for details
371 * about the wire format.
373 void relay_header_pack(char *dest, const relay_header_t *src) {
374 *(uint8_t*)(dest) = src->command;
376 set_uint16(dest+1, htons(src->recognized));
377 set_uint16(dest+3, htons(src->stream_id));
378 memcpy(dest+5, src->integrity, 4);
379 set_uint16(dest+9, htons(src->length));
382 /** Unpack the network-order buffer <b>src</b> into a host-order
383 * relay_header_t structure <b>dest</b>.
385 void relay_header_unpack(relay_header_t *dest, const char *src) {
386 dest->command = *(uint8_t*)(src);
388 dest->recognized = ntohs(get_uint16(src+1));
389 dest->stream_id = ntohs(get_uint16(src+3));
390 memcpy(dest->integrity, src+5, 4);
391 dest->length = ntohs(get_uint16(src+9));
394 /** Make a relay cell out of <b>relay_command</b> and <b>payload</b>, and
395 * send it onto the open circuit <b>circ</b>. <b>fromconn</b> is the stream
396 * that's sending the relay cell, or NULL if it's a control cell.
397 * <b>cpath_layer</b> is NULL for OR->OP cells, or the destination hop
398 * for OP->OR cells.
400 * If you can't send the cell, mark the circuit for close and
401 * return -1. Else return 0.
403 int connection_edge_send_command(connection_t *fromconn, circuit_t *circ,
404 int relay_command, const char *payload,
405 size_t payload_len, crypt_path_t *cpath_layer) {
406 cell_t cell;
407 relay_header_t rh;
408 int cell_direction;
410 if (fromconn && fromconn->marked_for_close) {
411 log_fn(LOG_WARN,"Bug: called on conn that's already marked for close at %s:%d.",
412 fromconn->marked_for_close_file, fromconn->marked_for_close);
413 return 0;
416 if (!circ) {
417 log_fn(LOG_WARN,"no circ. Closing conn.");
418 tor_assert(fromconn);
419 if (fromconn->type == CONN_TYPE_AP) {
420 connection_mark_unattached_ap(fromconn, END_STREAM_REASON_INTERNAL);
421 } else {
422 fromconn->has_sent_end = 1; /* no circ to send to */
423 connection_mark_for_close(fromconn);
425 return -1;
428 memset(&cell, 0, sizeof(cell_t));
429 cell.command = CELL_RELAY;
430 if (cpath_layer) {
431 cell.circ_id = circ->n_circ_id;
432 cell_direction = CELL_DIRECTION_OUT;
433 } else {
434 cell.circ_id = circ->p_circ_id;
435 cell_direction = CELL_DIRECTION_IN;
438 memset(&rh, 0, sizeof(rh));
439 rh.command = relay_command;
440 if (fromconn)
441 rh.stream_id = fromconn->stream_id; /* else it's 0 */
442 rh.length = payload_len;
443 relay_header_pack(cell.payload, &rh);
444 if (payload_len) {
445 tor_assert(payload_len <= RELAY_PAYLOAD_SIZE);
446 memcpy(cell.payload+RELAY_HEADER_SIZE, payload, payload_len);
449 log_fn(LOG_DEBUG,"delivering %d cell %s.", relay_command,
450 cell_direction == CELL_DIRECTION_OUT ? "forward" : "backward");
452 if (circuit_package_relay_cell(&cell, circ, cell_direction, cpath_layer) < 0) {
453 log_fn(LOG_WARN,"circuit_package_relay_cell failed. Closing.");
454 circuit_mark_for_close(circ);
455 return -1;
457 return 0;
460 /** Translate <b>reason</b>, which came from a relay 'end' cell,
461 * into a static const string describing why the stream is closing.
462 * <b>reason</b> is -1 if no reason was provided.
464 static const char *
465 connection_edge_end_reason_str(int reason) {
466 switch (reason) {
467 case -1:
468 log_fn(LOG_WARN,"End cell arrived with length 0. Should be at least 1.");
469 return "MALFORMED";
470 case END_STREAM_REASON_MISC: return "misc error";
471 case END_STREAM_REASON_RESOLVEFAILED: return "resolve failed";
472 case END_STREAM_REASON_CONNECTREFUSED: return "connection refused";
473 case END_STREAM_REASON_EXITPOLICY: return "exit policy failed";
474 case END_STREAM_REASON_DESTROY: return "destroyed";
475 case END_STREAM_REASON_DONE: return "closed normally";
476 case END_STREAM_REASON_TIMEOUT: return "gave up (timeout)";
477 case END_STREAM_REASON_HIBERNATING: return "server is hibernating";
478 case END_STREAM_REASON_INTERNAL: return "internal error at server";
479 case END_STREAM_REASON_RESOURCELIMIT: return "server out of resources";
480 case END_STREAM_REASON_CONNRESET: return "connection reset";
481 case END_STREAM_REASON_TORPROTOCOL: return "Tor protocol error";
482 default:
483 log_fn(LOG_WARN,"Reason for ending (%d) not recognized.",reason);
484 return "unknown";
488 /** Translate <b>reason</b> (as from a relay 'end' cell) into an
489 * appropriate SOCKS5 reply code.
491 socks5_reply_status_t
492 connection_edge_end_reason_socks5_response(int reason)
494 switch (reason) {
495 case END_STREAM_REASON_MISC:
496 return SOCKS5_GENERAL_ERROR;
497 case END_STREAM_REASON_RESOLVEFAILED:
498 return SOCKS5_HOST_UNREACHABLE;
499 case END_STREAM_REASON_CONNECTREFUSED:
500 return SOCKS5_CONNECTION_REFUSED;
501 case END_STREAM_REASON_EXITPOLICY:
502 return SOCKS5_NOT_ALLOWED;
503 case END_STREAM_REASON_DESTROY:
504 return SOCKS5_GENERAL_ERROR;
505 case END_STREAM_REASON_DONE:
506 return SOCKS5_SUCCEEDED;
507 case END_STREAM_REASON_TIMEOUT:
508 return SOCKS5_TTL_EXPIRED;
509 case END_STREAM_REASON_RESOURCELIMIT:
510 return SOCKS5_GENERAL_ERROR;
511 case END_STREAM_REASON_HIBERNATING:
512 return SOCKS5_GENERAL_ERROR;
513 case END_STREAM_REASON_INTERNAL:
514 return SOCKS5_GENERAL_ERROR;
515 case END_STREAM_REASON_CONNRESET:
516 return SOCKS5_CONNECTION_REFUSED;
517 case END_STREAM_REASON_TORPROTOCOL:
518 return SOCKS5_GENERAL_ERROR;
520 case END_STREAM_REASON_ALREADY_SOCKS_REPLIED:
521 return SOCKS5_SUCCEEDED; /* never used */
522 case END_STREAM_REASON_CANT_ATTACH:
523 return SOCKS5_GENERAL_ERROR;
524 case END_STREAM_REASON_NET_UNREACHABLE:
525 return SOCKS5_NET_UNREACHABLE;
526 default:
527 log_fn(LOG_WARN,"Reason for ending (%d) not recognized.",reason);
528 return SOCKS5_GENERAL_ERROR;
532 /* We need to use a few macros to deal with the fact that Windows
533 * decided that their sockets interface should be a permakludge.
534 * E_CASE is for errors where windows has both a EFOO and a WSAEFOO
535 * version, and S_CASE is for errors where windows has only a WSAEFOO
536 * version. (The E is for 'error', the S is for 'socket'). */
537 #ifdef MS_WINDOWS
538 #define E_CASE(s) case s: case WSA ## s
539 #define S_CASE(s) case WSA ## s
540 #else
541 #define E_CASE(s) case s
542 #define S_CASE(s) case s
543 #endif
546 errno_to_end_reason(int e)
548 switch (e) {
549 case EPIPE:
550 return END_STREAM_REASON_DONE;
551 E_CASE(EBADF):
552 E_CASE(EFAULT):
553 E_CASE(EINVAL):
554 S_CASE(EISCONN):
555 S_CASE(ENOTSOCK):
556 S_CASE(EPROTONOSUPPORT):
557 S_CASE(EAFNOSUPPORT):
558 E_CASE(EACCES):
559 S_CASE(ENOTCONN):
560 S_CASE(ENETUNREACH):
561 return END_STREAM_REASON_INTERNAL;
562 S_CASE(ECONNREFUSED):
563 return END_STREAM_REASON_CONNECTREFUSED;
564 S_CASE(ECONNRESET):
565 return END_STREAM_REASON_CONNRESET;
566 S_CASE(ETIMEDOUT):
567 return END_STREAM_REASON_TIMEOUT;
568 S_CASE(ENOBUFS):
569 case ENOMEM:
570 case ENFILE:
571 E_CASE(EMFILE):
572 return END_STREAM_REASON_RESOURCELIMIT;
573 default:
574 log_fn(LOG_INFO, "Didn't recognize errno %d (%s); telling the OP that we are ending a stream for 'misc' reason.",
575 e, tor_socket_strerror(e));
576 return END_STREAM_REASON_MISC;
580 /** How many times will I retry a stream that fails due to DNS
581 * resolve failure?
583 #define MAX_RESOLVE_FAILURES 3
585 /** Return 1 if reason is something that you should retry if you
586 * get the end cell before you've connected; else return 0. */
587 static int
588 edge_reason_is_retriable(int reason) {
589 return reason == END_STREAM_REASON_HIBERNATING ||
590 reason == END_STREAM_REASON_RESOURCELIMIT ||
591 reason == END_STREAM_REASON_EXITPOLICY ||
592 reason == END_STREAM_REASON_RESOLVEFAILED;
595 static int
596 connection_edge_process_end_not_open(
597 relay_header_t *rh, cell_t *cell, circuit_t *circ,
598 connection_t *conn, crypt_path_t *layer_hint) {
599 struct in_addr in;
600 routerinfo_t *exitrouter;
601 int reason = *(cell->payload+RELAY_HEADER_SIZE);
603 if (rh->length > 0 && edge_reason_is_retriable(reason)) {
604 if (conn->type != CONN_TYPE_AP) {
605 log_fn(LOG_WARN,"Got an end because of %s, but we're not an AP. Closing.",
606 connection_edge_end_reason_str(reason));
607 return -1;
609 log_fn(LOG_INFO,"Address '%s' refused due to '%s'. Considering retrying.",
610 safe_str(conn->socks_request->address),
611 connection_edge_end_reason_str(reason));
612 exitrouter = router_get_by_digest(circ->build_state->chosen_exit_digest);
613 if (!exitrouter) {
614 log_fn(LOG_INFO,"Skipping broken circ (exit router vanished)");
615 return 0; /* this circuit is screwed and doesn't know it yet */
617 switch (reason) {
618 case END_STREAM_REASON_EXITPOLICY:
619 if (rh->length >= 5) {
620 uint32_t addr = ntohl(get_uint32(cell->payload+RELAY_HEADER_SIZE+1));
621 if (!addr) {
622 log_fn(LOG_INFO,"Address '%s' resolved to 0.0.0.0. Closing,",
623 safe_str(conn->socks_request->address));
624 connection_mark_unattached_ap(conn, END_STREAM_REASON_TORPROTOCOL);
625 return 0;
627 client_dns_set_addressmap(conn->socks_request->address, addr,
628 conn->chosen_exit_name);
630 /* check if he *ought* to have allowed it */
631 if (rh->length < 5 ||
632 (!tor_inet_aton(conn->socks_request->address, &in) &&
633 !conn->chosen_exit_name)) {
634 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);
635 addr_policy_free(exitrouter->exit_policy);
636 exitrouter->exit_policy =
637 router_parse_addr_policy_from_string("reject *:*");
640 if (connection_ap_detach_retriable(conn, circ) >= 0)
641 return 0;
642 /* else, conn will get closed below */
643 break;
644 case END_STREAM_REASON_RESOLVEFAILED:
645 if (client_dns_incr_failures(conn->socks_request->address)
646 < MAX_RESOLVE_FAILURES) {
647 /* We haven't retried too many times; reattach the connection. */
648 circuit_log_path(LOG_INFO,circ);
649 tor_assert(circ->timestamp_dirty);
650 circ->timestamp_dirty -= get_options()->MaxCircuitDirtiness;
652 if (connection_ap_detach_retriable(conn, circ) >= 0)
653 return 0;
654 /* else, conn will get closed below */
655 } else {
656 log_fn(LOG_NOTICE,"Have tried resolving address '%s' at %d different places. Giving up.",
657 safe_str(conn->socks_request->address), MAX_RESOLVE_FAILURES);
659 break;
660 case END_STREAM_REASON_HIBERNATING:
661 case END_STREAM_REASON_RESOURCELIMIT:
662 addr_policy_free(exitrouter->exit_policy);
663 exitrouter->exit_policy =
664 router_parse_addr_policy_from_string("reject *:*");
666 if (connection_ap_detach_retriable(conn, circ) >= 0)
667 return 0;
668 /* else, will close below */
669 break;
670 } /* end switch */
671 log_fn(LOG_INFO,"Giving up on retrying; conn can't be handled.");
674 log_fn(LOG_INFO,"Edge got end (%s) before we're connected. Marking for close.",
675 connection_edge_end_reason_str(rh->length > 0 ? reason : -1));
676 if (conn->type == CONN_TYPE_AP) {
677 circuit_log_path(LOG_INFO,circ);
678 connection_mark_unattached_ap(conn, reason);
679 } else {
680 conn->has_sent_end = 1; /* we just got an 'end', don't need to send one */
681 connection_mark_for_close(conn);
683 return 0;
686 /** An incoming relay cell has arrived from circuit <b>circ</b> to
687 * stream <b>conn</b>.
689 * The arguments here are the same as in
690 * connection_edge_process_relay_cell() below; this function is called
691 * from there when <b>conn</b> is defined and not in an open state.
693 static int
694 connection_edge_process_relay_cell_not_open(
695 relay_header_t *rh, cell_t *cell, circuit_t *circ,
696 connection_t *conn, crypt_path_t *layer_hint) {
698 if (rh->command == RELAY_COMMAND_END)
699 return connection_edge_process_end_not_open(rh, cell, circ, conn, layer_hint);
701 if (conn->type == CONN_TYPE_AP && rh->command == RELAY_COMMAND_CONNECTED) {
702 if (conn->state != AP_CONN_STATE_CONNECT_WAIT) {
703 log_fn(LOG_WARN,"Got 'connected' while not in state connect_wait. Dropping.");
704 return 0;
706 // log_fn(LOG_INFO,"Connected! Notifying application.");
707 conn->state = AP_CONN_STATE_OPEN;
708 log_fn(LOG_INFO,"'connected' received after %d seconds.",
709 (int)(time(NULL) - conn->timestamp_lastread));
710 if (rh->length >= 4) {
711 uint32_t addr = ntohl(get_uint32(cell->payload+RELAY_HEADER_SIZE));
712 if (!addr) {
713 log_fn(LOG_INFO,"...but it claims the IP address was 0.0.0.0. Closing.");
714 connection_edge_end(conn, END_STREAM_REASON_TORPROTOCOL, conn->cpath_layer);
715 connection_mark_unattached_ap(conn, END_STREAM_REASON_TORPROTOCOL);
716 return 0;
718 client_dns_set_addressmap(conn->socks_request->address, addr,
719 conn->chosen_exit_name);
721 circuit_log_path(LOG_INFO,circ);
722 connection_ap_handshake_socks_reply(conn, NULL, 0, SOCKS5_SUCCEEDED);
723 /* handle anything that might have queued */
724 if (connection_edge_package_raw_inbuf(conn, 1) < 0) {
725 /* (We already sent an end cell if possible) */
726 connection_mark_for_close(conn);
727 return 0;
729 return 0;
731 if (conn->type == CONN_TYPE_AP && rh->command == RELAY_COMMAND_RESOLVED) {
732 if (conn->state != AP_CONN_STATE_RESOLVE_WAIT) {
733 log_fn(LOG_WARN,"Got a 'resolved' cell while not in state resolve_wait. Dropping.");
734 return 0;
736 tor_assert(conn->socks_request->command == SOCKS_COMMAND_RESOLVE);
737 if (rh->length < 2 || cell->payload[RELAY_HEADER_SIZE+1]+2>rh->length) {
738 log_fn(LOG_WARN, "Dropping malformed 'resolved' cell");
739 connection_mark_unattached_ap(conn, END_STREAM_REASON_TORPROTOCOL);
740 return 0;
742 connection_ap_handshake_socks_resolved(conn,
743 cell->payload[RELAY_HEADER_SIZE], /*answer_type*/
744 cell->payload[RELAY_HEADER_SIZE+1], /*answer_len*/
745 cell->payload+RELAY_HEADER_SIZE+2); /* answer */
746 connection_mark_unattached_ap(conn, END_STREAM_REASON_ALREADY_SOCKS_REPLIED);
747 return 0;
750 log_fn(LOG_WARN,"Got an unexpected relay command %d, in state %d (%s). Closing.",
751 rh->command, conn->state, conn_state_to_string(conn->type, conn->state));
752 connection_edge_end(conn, END_STREAM_REASON_TORPROTOCOL, conn->cpath_layer);
753 connection_mark_for_close(conn);
754 return -1;
757 /** An incoming relay cell has arrived on circuit <b>circ</b>. If
758 * <b>conn</b> is NULL this is a control cell, else <b>cell</b> is
759 * destined for <b>conn</b>.
761 * If <b>layer_hint</b> is defined, then we're the origin of the
762 * circuit, and it specifies the hop that packaged <b>cell</b>.
764 * Return -1 if you want to tear down the circuit, else 0.
766 static int
767 connection_edge_process_relay_cell(cell_t *cell, circuit_t *circ,
768 connection_t *conn,
769 crypt_path_t *layer_hint)
771 static int num_seen=0;
772 relay_header_t rh;
774 tor_assert(cell);
775 tor_assert(circ);
777 relay_header_unpack(&rh, cell->payload);
778 // log_fn(LOG_DEBUG,"command %d stream %d", rh.command, rh.stream_id);
779 num_seen++;
780 log_fn(LOG_DEBUG,"Now seen %d relay cells here.", num_seen);
782 /* either conn is NULL, in which case we've got a control cell, or else
783 * conn points to the recognized stream. */
785 if (conn && !connection_state_is_open(conn))
786 return connection_edge_process_relay_cell_not_open(
787 &rh, cell, circ, conn, layer_hint);
789 switch (rh.command) {
790 case RELAY_COMMAND_DROP:
791 log_fn(LOG_INFO,"Got a relay-level padding cell. Dropping.");
792 return 0;
793 case RELAY_COMMAND_BEGIN:
794 if (layer_hint &&
795 circ->purpose != CIRCUIT_PURPOSE_S_REND_JOINED) {
796 log_fn(LOG_WARN,"relay begin request unsupported at AP. Dropping.");
797 return 0;
799 if (conn) {
800 log_fn(LOG_WARN,"begin cell for known stream. Dropping.");
801 return 0;
803 connection_exit_begin_conn(cell, circ);
804 return 0;
805 case RELAY_COMMAND_DATA:
806 ++stats_n_data_cells_received;
807 if (( layer_hint && --layer_hint->deliver_window < 0) ||
808 (!layer_hint && --circ->deliver_window < 0)) {
809 log_fn(LOG_WARN,"(relay data) circ deliver_window below 0. Killing.");
810 connection_edge_end(conn, END_STREAM_REASON_TORPROTOCOL, conn->cpath_layer);
811 connection_mark_for_close(conn);
812 return -1;
814 log_fn(LOG_DEBUG,"circ deliver_window now %d.", layer_hint ?
815 layer_hint->deliver_window : circ->deliver_window);
817 circuit_consider_sending_sendme(circ, layer_hint);
819 if (!conn) {
820 log_fn(LOG_INFO,"data cell dropped, unknown stream.");
821 return 0;
824 if (--conn->deliver_window < 0) { /* is it below 0 after decrement? */
825 log_fn(LOG_WARN,"(relay data) conn deliver_window below 0. Killing.");
826 return -1; /* somebody's breaking protocol. kill the whole circuit. */
829 stats_n_data_bytes_received += rh.length;
830 if (conn->type == CONN_TYPE_AP) {
831 conn->stream_size += rh.length;
832 log_fn(LOG_DEBUG,"%d: stream size now %d.", conn->s, (int)conn->stream_size);
834 connection_write_to_buf(cell->payload + RELAY_HEADER_SIZE,
835 rh.length, conn);
836 connection_edge_consider_sending_sendme(conn);
837 return 0;
838 case RELAY_COMMAND_END:
839 if (!conn) {
840 log_fn(LOG_INFO,"end cell (%s) dropped, unknown stream.",
841 connection_edge_end_reason_str(rh.length > 0 ?
842 *(char *)(cell->payload+RELAY_HEADER_SIZE) : -1));
843 return 0;
845 /* XXX add to this log_fn the exit node's nickname? */
846 log_fn(LOG_INFO,"%d: end cell (%s) for stream %d. Removing stream. Size %d.",
847 conn->s,
848 connection_edge_end_reason_str(rh.length > 0 ?
849 *(char *)(cell->payload+RELAY_HEADER_SIZE) : -1),
850 conn->stream_id, (int)conn->stream_size);
851 if (conn->socks_request && !conn->socks_request->has_finished)
852 log_fn(LOG_WARN,"Bug: open stream hasn't sent socks answer yet? Closing.");
853 #ifdef HALF_OPEN
854 conn->done_sending = 1;
855 shutdown(conn->s, 1); /* XXX check return; refactor NM */
856 if (conn->done_receiving) {
857 /* We just *got* an end; no reason to send one. */
858 conn->has_sent_end = 1;
859 connection_mark_for_close(conn);
860 conn->hold_open_until_flushed = 1;
862 #else
863 /* We just *got* an end; no reason to send one. */
864 conn->has_sent_end = 1;
865 if (!conn->marked_for_close) {
866 /* only mark it if not already marked. it's possible to
867 * get the 'end' right around when the client hangs up on us. */
868 connection_mark_for_close(conn);
869 conn->hold_open_until_flushed = 1;
871 #endif
872 return 0;
873 case RELAY_COMMAND_EXTEND:
874 if (conn) {
875 log_fn(LOG_WARN,"'extend' for non-zero stream. Dropping.");
876 return 0;
878 return circuit_extend(cell, circ);
879 case RELAY_COMMAND_EXTENDED:
880 if (!layer_hint) {
881 log_fn(LOG_WARN,"'extended' unsupported at non-origin. Dropping.");
882 return 0;
884 log_fn(LOG_DEBUG,"Got an extended cell! Yay.");
885 if (circuit_finish_handshake(circ, CELL_CREATED,
886 cell->payload+RELAY_HEADER_SIZE) < 0) {
887 log_fn(LOG_WARN,"circuit_finish_handshake failed.");
888 return -1;
890 if (circuit_send_next_onion_skin(circ)<0) {
891 log_fn(LOG_INFO,"circuit_send_next_onion_skin() failed.");
892 return -1;
894 return 0;
895 case RELAY_COMMAND_TRUNCATE:
896 if (layer_hint) {
897 log_fn(LOG_WARN,"'truncate' unsupported at origin. Dropping.");
898 return 0;
900 if (circ->n_conn) {
901 connection_send_destroy(circ->n_circ_id, circ->n_conn);
902 circuit_set_circid_orconn(circ, 0, NULL, N_CONN_CHANGED);
904 log_fn(LOG_DEBUG, "Processed 'truncate', replying.");
905 connection_edge_send_command(NULL, circ, RELAY_COMMAND_TRUNCATED,
906 NULL, 0, NULL);
907 return 0;
908 case RELAY_COMMAND_TRUNCATED:
909 if (!layer_hint) {
910 log_fn(LOG_WARN,"'truncated' unsupported at non-origin. Dropping.");
911 return 0;
913 circuit_truncated(circ, layer_hint);
914 return 0;
915 case RELAY_COMMAND_CONNECTED:
916 if (conn) {
917 log_fn(LOG_WARN,"'connected' unsupported while open. Closing circ.");
918 return -1;
920 log_fn(LOG_INFO,"'connected' received, no conn attached anymore. Ignoring.");
921 return 0;
922 case RELAY_COMMAND_SENDME:
923 if (!conn) {
924 if (layer_hint) {
925 layer_hint->package_window += CIRCWINDOW_INCREMENT;
926 log_fn(LOG_DEBUG,"circ-level sendme at origin, packagewindow %d.",
927 layer_hint->package_window);
928 circuit_resume_edge_reading(circ, layer_hint);
929 } else {
930 circ->package_window += CIRCWINDOW_INCREMENT;
931 log_fn(LOG_DEBUG,"circ-level sendme at non-origin, packagewindow %d.",
932 circ->package_window);
933 circuit_resume_edge_reading(circ, layer_hint);
935 return 0;
937 conn->package_window += STREAMWINDOW_INCREMENT;
938 log_fn(LOG_DEBUG,"stream-level sendme, packagewindow now %d.", conn->package_window);
939 connection_start_reading(conn);
940 /* handle whatever might still be on the inbuf */
941 if (connection_edge_package_raw_inbuf(conn, 1) < 0) {
942 /* (We already sent an end cell if possible) */
943 connection_mark_for_close(conn);
944 return 0;
946 return 0;
947 case RELAY_COMMAND_RESOLVE:
948 if (layer_hint) {
949 log_fn(LOG_WARN,"resolve request unsupported at AP; dropping.");
950 return 0;
951 } else if (conn) {
952 log_fn(LOG_WARN, "resolve request for known stream; dropping.");
953 return 0;
954 } else if (circ->purpose != CIRCUIT_PURPOSE_OR) {
955 log_fn(LOG_WARN, "resolve request on circ with purpose %d; dropping",
956 circ->purpose);
957 return 0;
959 connection_exit_begin_resolve(cell, circ);
960 return 0;
961 case RELAY_COMMAND_RESOLVED:
962 if (conn) {
963 log_fn(LOG_WARN,"'resolved' unsupported while open. Closing circ.");
964 return -1;
966 log_fn(LOG_INFO,"'resolved' received, no conn attached anymore. Ignoring.");
967 return 0;
968 case RELAY_COMMAND_ESTABLISH_INTRO:
969 case RELAY_COMMAND_ESTABLISH_RENDEZVOUS:
970 case RELAY_COMMAND_INTRODUCE1:
971 case RELAY_COMMAND_INTRODUCE2:
972 case RELAY_COMMAND_INTRODUCE_ACK:
973 case RELAY_COMMAND_RENDEZVOUS1:
974 case RELAY_COMMAND_RENDEZVOUS2:
975 case RELAY_COMMAND_INTRO_ESTABLISHED:
976 case RELAY_COMMAND_RENDEZVOUS_ESTABLISHED:
977 rend_process_relay_cell(circ, rh.command, rh.length,
978 cell->payload+RELAY_HEADER_SIZE);
979 return 0;
981 log_fn(LOG_WARN,"unknown relay command %d.",rh.command);
982 return -1;
985 uint64_t stats_n_data_cells_packaged = 0;
986 uint64_t stats_n_data_bytes_packaged = 0;
987 uint64_t stats_n_data_cells_received = 0;
988 uint64_t stats_n_data_bytes_received = 0;
990 /** While conn->inbuf has an entire relay payload of bytes on it,
991 * and the appropriate package windows aren't empty, grab a cell
992 * and send it down the circuit.
994 * Return -1 (and send a RELAY_END cell if necessary) if conn should
995 * be marked for close, else return 0.
997 int connection_edge_package_raw_inbuf(connection_t *conn, int package_partial) {
998 size_t amount_to_process, length;
999 char payload[CELL_PAYLOAD_SIZE];
1000 circuit_t *circ;
1002 tor_assert(conn);
1003 tor_assert(!connection_speaks_cells(conn));
1004 if (conn->marked_for_close) {
1005 log_fn(LOG_WARN,"Bug: called on conn that's already marked for close at %s:%d.",
1006 conn->marked_for_close_file, conn->marked_for_close);
1007 return 0;
1010 repeat_connection_edge_package_raw_inbuf:
1012 circ = circuit_get_by_edge_conn(conn);
1013 if (!circ) {
1014 log_fn(LOG_INFO,"conn has no circuit! Closing.");
1015 return -1;
1018 if (circuit_consider_stop_edge_reading(circ, conn->cpath_layer))
1019 return 0;
1021 if (conn->package_window <= 0) {
1022 log_fn(LOG_INFO,"called with package_window %d. Skipping.", conn->package_window);
1023 connection_stop_reading(conn);
1024 return 0;
1027 amount_to_process = buf_datalen(conn->inbuf);
1029 if (!amount_to_process)
1030 return 0;
1032 if (!package_partial && amount_to_process < RELAY_PAYLOAD_SIZE)
1033 return 0;
1035 if (amount_to_process > RELAY_PAYLOAD_SIZE) {
1036 length = RELAY_PAYLOAD_SIZE;
1037 } else {
1038 length = amount_to_process;
1040 stats_n_data_bytes_packaged += length;
1041 stats_n_data_cells_packaged += 1;
1043 connection_fetch_from_buf(payload, length, conn);
1045 log_fn(LOG_DEBUG,"(%d) Packaging %d bytes (%d waiting).", conn->s,
1046 (int)length, (int)buf_datalen(conn->inbuf));
1047 if (conn->type == CONN_TYPE_EXIT) {
1048 conn->stream_size += length;
1049 log_fn(LOG_DEBUG,"%d: Stream size now %d.", conn->s, (int)conn->stream_size);
1052 if (connection_edge_send_command(conn, circ, RELAY_COMMAND_DATA,
1053 payload, length, conn->cpath_layer) < 0)
1054 /* circuit got marked for close, don't continue, don't need to mark conn */
1055 return 0;
1057 if (!conn->cpath_layer) { /* non-rendezvous exit */
1058 tor_assert(circ->package_window > 0);
1059 circ->package_window--;
1060 } else { /* we're an AP, or an exit on a rendezvous circ */
1061 tor_assert(conn->cpath_layer->package_window > 0);
1062 conn->cpath_layer->package_window--;
1065 if (--conn->package_window <= 0) { /* is it 0 after decrement? */
1066 connection_stop_reading(conn);
1067 log_fn(LOG_DEBUG,"conn->package_window reached 0.");
1068 circuit_consider_stop_edge_reading(circ, conn->cpath_layer);
1069 return 0; /* don't process the inbuf any more */
1071 log_fn(LOG_DEBUG,"conn->package_window is now %d",conn->package_window);
1073 /* handle more if there's more, or return 0 if there isn't */
1074 goto repeat_connection_edge_package_raw_inbuf;
1077 /** Called when we've just received a relay data cell, or when
1078 * we've just finished flushing all bytes to stream <b>conn</b>.
1080 * If conn->outbuf is not too full, and our deliver window is
1081 * low, send back a suitable number of stream-level sendme cells.
1083 void connection_edge_consider_sending_sendme(connection_t *conn) {
1084 circuit_t *circ;
1086 if (connection_outbuf_too_full(conn))
1087 return;
1089 circ = circuit_get_by_edge_conn(conn);
1090 if (!circ) {
1091 /* this can legitimately happen if the destroy has already
1092 * arrived and torn down the circuit */
1093 log_fn(LOG_INFO,"No circuit associated with conn. Skipping.");
1094 return;
1097 while (conn->deliver_window < STREAMWINDOW_START - STREAMWINDOW_INCREMENT) {
1098 log_fn(LOG_DEBUG,"Outbuf %d, Queueing stream sendme.", (int)conn->outbuf_flushlen);
1099 conn->deliver_window += STREAMWINDOW_INCREMENT;
1100 if (connection_edge_send_command(conn, circ, RELAY_COMMAND_SENDME,
1101 NULL, 0, conn->cpath_layer) < 0) {
1102 log_fn(LOG_WARN,"connection_edge_send_command failed. Returning.");
1103 return; /* the circuit's closed, don't continue */
1108 /** The circuit <b>circ</b> has received a circuit-level sendme
1109 * (on hop <b>layer_hint</b>, if we're the OP). Go through all the
1110 * attached streams and let them resume reading and packaging, if
1111 * their stream windows allow it.
1113 static void
1114 circuit_resume_edge_reading(circuit_t *circ, crypt_path_t *layer_hint)
1117 log_fn(LOG_DEBUG,"resuming");
1119 /* have to check both n_streams and p_streams, to handle rendezvous */
1120 if (circuit_resume_edge_reading_helper(circ->n_streams, circ, layer_hint) >= 0)
1121 circuit_resume_edge_reading_helper(circ->p_streams, circ, layer_hint);
1124 /** A helper function for circuit_resume_edge_reading() above.
1125 * The arguments are the same, except that <b>conn</b> is the head
1126 * of a linked list of edge streams that should each be considered.
1128 static int
1129 circuit_resume_edge_reading_helper(connection_t *conn,
1130 circuit_t *circ,
1131 crypt_path_t *layer_hint) {
1133 for ( ; conn; conn=conn->next_stream) {
1134 if (conn->marked_for_close)
1135 continue;
1136 if ((!layer_hint && conn->package_window > 0) ||
1137 (layer_hint && conn->package_window > 0 && conn->cpath_layer == layer_hint)) {
1138 connection_start_reading(conn);
1139 /* handle whatever might still be on the inbuf */
1140 if (connection_edge_package_raw_inbuf(conn, 1)<0) {
1141 /* (We already sent an end cell if possible) */
1142 connection_mark_for_close(conn);
1143 continue;
1146 /* If the circuit won't accept any more data, return without looking
1147 * at any more of the streams. Any connections that should be stopped
1148 * have already been stopped by connection_edge_package_raw_inbuf. */
1149 if (circuit_consider_stop_edge_reading(circ, layer_hint))
1150 return -1;
1153 return 0;
1156 /** Check if the package window for <b>circ</b> is empty (at
1157 * hop <b>layer_hint</b> if it's defined).
1159 * If yes, tell edge streams to stop reading and return 1.
1160 * Else return 0.
1162 static int
1163 circuit_consider_stop_edge_reading(circuit_t *circ, crypt_path_t *layer_hint)
1165 connection_t *conn = NULL;
1167 if (!layer_hint) {
1168 log_fn(LOG_DEBUG,"considering circ->package_window %d", circ->package_window);
1169 if (circ->package_window <= 0) {
1170 log_fn(LOG_DEBUG,"yes, not-at-origin. stopped.");
1171 for (conn = circ->n_streams; conn; conn=conn->next_stream)
1172 connection_stop_reading(conn);
1173 return 1;
1175 return 0;
1177 /* else, layer hint is defined, use it */
1178 log_fn(LOG_DEBUG,"considering layer_hint->package_window %d", layer_hint->package_window);
1179 if (layer_hint->package_window <= 0) {
1180 log_fn(LOG_DEBUG,"yes, at-origin. stopped.");
1181 for (conn = circ->n_streams; conn; conn=conn->next_stream)
1182 if (conn->cpath_layer == layer_hint)
1183 connection_stop_reading(conn);
1184 for (conn = circ->p_streams; conn; conn=conn->next_stream)
1185 if (conn->cpath_layer == layer_hint)
1186 connection_stop_reading(conn);
1187 return 1;
1189 return 0;
1192 /** Check if the deliver_window for circuit <b>circ</b> (at hop
1193 * <b>layer_hint</b> if it's defined) is low enough that we should
1194 * send a circuit-level sendme back down the circuit. If so, send
1195 * enough sendmes that the window would be overfull if we sent any
1196 * more.
1198 static void
1199 circuit_consider_sending_sendme(circuit_t *circ, crypt_path_t *layer_hint)
1201 // log_fn(LOG_INFO,"Considering: layer_hint is %s",
1202 // layer_hint ? "defined" : "null");
1203 while ((layer_hint ? layer_hint->deliver_window : circ->deliver_window) <
1204 CIRCWINDOW_START - CIRCWINDOW_INCREMENT) {
1205 log_fn(LOG_DEBUG,"Queueing circuit sendme.");
1206 if (layer_hint)
1207 layer_hint->deliver_window += CIRCWINDOW_INCREMENT;
1208 else
1209 circ->deliver_window += CIRCWINDOW_INCREMENT;
1210 if (connection_edge_send_command(NULL, circ, RELAY_COMMAND_SENDME,
1211 NULL, 0, layer_hint) < 0) {
1212 log_fn(LOG_WARN,"connection_edge_send_command failed. Circuit's closed.");
1213 return; /* the circuit's closed, don't continue */