report partial bootstrapping progress as we fetch descriptors
[tor/rransom.git] / src / or / relay.c
blob33ee055cea7e782a84179872eb87205025752c44
1 /* Copyright (c) 2001 Matej Pfajfar.
2 * Copyright (c) 2001-2004, Roger Dingledine.
3 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
4 * Copyright (c) 2007-2008, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
6 /* $Id$ */
7 const char relay_c_id[] =
8 "$Id$";
10 /**
11 * \file relay.c
12 * \brief Handle relay cell encryption/decryption, plus packaging and
13 * receiving from circuits, plus queueing on circuits.
14 **/
16 #include "or.h"
17 #include "mempool.h"
19 static int relay_crypt(circuit_t *circ, cell_t *cell, int cell_direction,
20 crypt_path_t **layer_hint, char *recognized);
21 static edge_connection_t *relay_lookup_conn(circuit_t *circ, cell_t *cell,
22 int cell_direction,
23 crypt_path_t *layer_hint);
25 static int
26 connection_edge_process_relay_cell(cell_t *cell, circuit_t *circ,
27 edge_connection_t *conn,
28 crypt_path_t *layer_hint);
29 static void
30 circuit_consider_sending_sendme(circuit_t *circ, crypt_path_t *layer_hint);
31 static void
32 circuit_resume_edge_reading(circuit_t *circ, crypt_path_t *layer_hint);
33 static int
34 circuit_resume_edge_reading_helper(edge_connection_t *conn,
35 circuit_t *circ,
36 crypt_path_t *layer_hint);
37 static int
38 circuit_consider_stop_edge_reading(circuit_t *circ, crypt_path_t *layer_hint);
40 /** Stats: how many relay cells have originated at this hop, or have
41 * been relayed onward (not recognized at this hop)?
43 uint64_t stats_n_relay_cells_relayed = 0;
44 /** Stats: how many relay cells have been delivered to streams at this
45 * hop?
47 uint64_t stats_n_relay_cells_delivered = 0;
49 /** Update digest from the payload of cell. Assign integrity part to
50 * cell.
52 static void
53 relay_set_digest(crypto_digest_env_t *digest, cell_t *cell)
55 char integrity[4];
56 relay_header_t rh;
58 crypto_digest_add_bytes(digest, cell->payload, CELL_PAYLOAD_SIZE);
59 crypto_digest_get_digest(digest, integrity, 4);
60 // log_fn(LOG_DEBUG,"Putting digest of %u %u %u %u into relay cell.",
61 // integrity[0], integrity[1], integrity[2], integrity[3]);
62 relay_header_unpack(&rh, cell->payload);
63 memcpy(rh.integrity, integrity, 4);
64 relay_header_pack(cell->payload, &rh);
67 /** Does the digest for this circuit indicate that this cell is for us?
69 * Update digest from the payload of cell (with the integrity part set
70 * to 0). If the integrity part is valid, return 1, else restore digest
71 * and cell to their original state and return 0.
73 static int
74 relay_digest_matches(crypto_digest_env_t *digest, cell_t *cell)
76 char received_integrity[4], calculated_integrity[4];
77 relay_header_t rh;
78 crypto_digest_env_t *backup_digest=NULL;
80 backup_digest = crypto_digest_dup(digest);
82 relay_header_unpack(&rh, cell->payload);
83 memcpy(received_integrity, rh.integrity, 4);
84 memset(rh.integrity, 0, 4);
85 relay_header_pack(cell->payload, &rh);
87 // log_fn(LOG_DEBUG,"Reading digest of %u %u %u %u from relay cell.",
88 // received_integrity[0], received_integrity[1],
89 // received_integrity[2], received_integrity[3]);
91 crypto_digest_add_bytes(digest, cell->payload, CELL_PAYLOAD_SIZE);
92 crypto_digest_get_digest(digest, calculated_integrity, 4);
94 if (memcmp(received_integrity, calculated_integrity, 4)) {
95 // log_fn(LOG_INFO,"Recognized=0 but bad digest. Not recognizing.");
96 // (%d vs %d).", received_integrity, calculated_integrity);
97 /* restore digest to its old form */
98 crypto_digest_assign(digest, backup_digest);
99 /* restore the relay header */
100 memcpy(rh.integrity, received_integrity, 4);
101 relay_header_pack(cell->payload, &rh);
102 crypto_free_digest_env(backup_digest);
103 return 0;
105 crypto_free_digest_env(backup_digest);
106 return 1;
109 /** Apply <b>cipher</b> to CELL_PAYLOAD_SIZE bytes of <b>in</b>
110 * (in place).
112 * If <b>encrypt_mode</b> is 1 then encrypt, else decrypt.
114 * Return -1 if the crypto fails, else return 0.
116 static int
117 relay_crypt_one_payload(crypto_cipher_env_t *cipher, char *in,
118 int encrypt_mode)
120 int r;
121 (void)encrypt_mode;
122 r = crypto_cipher_crypt_inplace(cipher, in, CELL_PAYLOAD_SIZE);
124 if (r) {
125 log_warn(LD_BUG,"Error during relay encryption");
126 return -1;
128 return 0;
131 /** Receive a relay cell:
132 * - Crypt it (encrypt if headed toward the origin or if we <b>are</b> the
133 * origin; decrypt if we're headed toward the exit).
134 * - Check if recognized (if exitward).
135 * - If recognized and the digest checks out, then find if there's a stream
136 * that the cell is intended for, and deliver it to the right
137 * connection_edge.
138 * - If not recognized, then we need to relay it: append it to the appropriate
139 * cell_queue on <b>circ</b>.
141 * Return -<b>reason</b> on failure.
144 circuit_receive_relay_cell(cell_t *cell, circuit_t *circ, int cell_direction)
146 or_connection_t *or_conn=NULL;
147 crypt_path_t *layer_hint=NULL;
148 char recognized=0;
149 int reason;
151 tor_assert(cell);
152 tor_assert(circ);
153 tor_assert(cell_direction == CELL_DIRECTION_OUT ||
154 cell_direction == CELL_DIRECTION_IN);
155 if (circ->marked_for_close)
156 return 0;
158 if (relay_crypt(circ, cell, cell_direction, &layer_hint, &recognized) < 0) {
159 log_warn(LD_BUG,"relay crypt failed. Dropping connection.");
160 return -END_CIRC_REASON_INTERNAL;
163 if (recognized) {
164 edge_connection_t *conn = relay_lookup_conn(circ, cell, cell_direction,
165 layer_hint);
166 if (cell_direction == CELL_DIRECTION_OUT) {
167 ++stats_n_relay_cells_delivered;
168 log_debug(LD_OR,"Sending away from origin.");
169 if ((reason=connection_edge_process_relay_cell(cell, circ, conn, NULL))
170 < 0) {
171 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
172 "connection_edge_process_relay_cell (away from origin) "
173 "failed.");
174 return reason;
177 if (cell_direction == CELL_DIRECTION_IN) {
178 ++stats_n_relay_cells_delivered;
179 log_debug(LD_OR,"Sending to origin.");
180 if ((reason = connection_edge_process_relay_cell(cell, circ, conn,
181 layer_hint)) < 0) {
182 log_warn(LD_OR,
183 "connection_edge_process_relay_cell (at origin) failed.");
184 return reason;
187 return 0;
190 /* not recognized. pass it on. */
191 if (cell_direction == CELL_DIRECTION_OUT) {
192 cell->circ_id = circ->n_circ_id; /* switch it */
193 or_conn = circ->n_conn;
194 } else if (! CIRCUIT_IS_ORIGIN(circ)) {
195 cell->circ_id = TO_OR_CIRCUIT(circ)->p_circ_id; /* switch it */
196 or_conn = TO_OR_CIRCUIT(circ)->p_conn;
197 } else {
198 log_fn(LOG_PROTOCOL_WARN, LD_OR,
199 "Dropping unrecognized inbound cell on origin circuit.");
200 return 0;
203 if (!or_conn) {
204 // XXXX Can this splice stuff be done more cleanly?
205 if (! CIRCUIT_IS_ORIGIN(circ) &&
206 TO_OR_CIRCUIT(circ)->rend_splice &&
207 cell_direction == CELL_DIRECTION_OUT) {
208 or_circuit_t *splice = TO_OR_CIRCUIT(circ)->rend_splice;
209 tor_assert(circ->purpose == CIRCUIT_PURPOSE_REND_ESTABLISHED);
210 tor_assert(splice->_base.purpose == CIRCUIT_PURPOSE_REND_ESTABLISHED);
211 cell->circ_id = splice->p_circ_id;
212 if ((reason = circuit_receive_relay_cell(cell, TO_CIRCUIT(splice),
213 CELL_DIRECTION_IN)) < 0) {
214 log_warn(LD_REND, "Error relaying cell across rendezvous; closing "
215 "circuits");
216 /* XXXX Do this here, or just return -1? */
217 circuit_mark_for_close(circ, -reason);
218 return reason;
220 return 0;
222 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
223 "Didn't recognize cell, but circ stops here! Closing circ.");
224 return -END_CIRC_REASON_TORPROTOCOL;
227 log_debug(LD_OR,"Passing on unrecognized cell.");
229 ++stats_n_relay_cells_relayed; /* XXXX no longer quite accurate {cells}
230 * we might kill the circ before we relay
231 * the cells. */
233 append_cell_to_circuit_queue(circ, or_conn, cell, cell_direction);
234 return 0;
237 /** Do the appropriate en/decryptions for <b>cell</b> arriving on
238 * <b>circ</b> in direction <b>cell_direction</b>.
240 * If cell_direction == CELL_DIRECTION_IN:
241 * - If we're at the origin (we're the OP), for hops 1..N,
242 * decrypt cell. If recognized, stop.
243 * - Else (we're not the OP), encrypt one hop. Cell is not recognized.
245 * If cell_direction == CELL_DIRECTION_OUT:
246 * - decrypt one hop. Check if recognized.
248 * If cell is recognized, set *recognized to 1, and set
249 * *layer_hint to the hop that recognized it.
251 * Return -1 to indicate that we should mark the circuit for close,
252 * else return 0.
254 static int
255 relay_crypt(circuit_t *circ, cell_t *cell, int cell_direction,
256 crypt_path_t **layer_hint, char *recognized)
258 relay_header_t rh;
260 tor_assert(circ);
261 tor_assert(cell);
262 tor_assert(recognized);
263 tor_assert(cell_direction == CELL_DIRECTION_IN ||
264 cell_direction == CELL_DIRECTION_OUT);
266 if (cell_direction == CELL_DIRECTION_IN) {
267 if (CIRCUIT_IS_ORIGIN(circ)) { /* We're at the beginning of the circuit.
268 * We'll want to do layered decrypts. */
269 crypt_path_t *thishop, *cpath = TO_ORIGIN_CIRCUIT(circ)->cpath;
270 thishop = cpath;
271 if (thishop->state != CPATH_STATE_OPEN) {
272 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
273 "Relay cell before first created cell? Closing.");
274 return -1;
276 do { /* Remember: cpath is in forward order, that is, first hop first. */
277 tor_assert(thishop);
279 if (relay_crypt_one_payload(thishop->b_crypto, cell->payload, 0) < 0)
280 return -1;
282 relay_header_unpack(&rh, cell->payload);
283 if (rh.recognized == 0) {
284 /* it's possibly recognized. have to check digest to be sure. */
285 if (relay_digest_matches(thishop->b_digest, cell)) {
286 *recognized = 1;
287 *layer_hint = thishop;
288 return 0;
292 thishop = thishop->next;
293 } while (thishop != cpath && thishop->state == CPATH_STATE_OPEN);
294 log_fn(LOG_PROTOCOL_WARN, LD_OR,
295 "Incoming cell at client not recognized. Closing.");
296 return -1;
297 } else { /* we're in the middle. Just one crypt. */
298 if (relay_crypt_one_payload(TO_OR_CIRCUIT(circ)->p_crypto,
299 cell->payload, 1) < 0)
300 return -1;
301 // log_fn(LOG_DEBUG,"Skipping recognized check, because we're not "
302 // "the client.");
304 } else /* cell_direction == CELL_DIRECTION_OUT */ {
305 /* we're in the middle. Just one crypt. */
307 if (relay_crypt_one_payload(TO_OR_CIRCUIT(circ)->n_crypto,
308 cell->payload, 0) < 0)
309 return -1;
311 relay_header_unpack(&rh, cell->payload);
312 if (rh.recognized == 0) {
313 /* it's possibly recognized. have to check digest to be sure. */
314 if (relay_digest_matches(TO_OR_CIRCUIT(circ)->n_digest, cell)) {
315 *recognized = 1;
316 return 0;
320 return 0;
323 /** Package a relay cell from an edge:
324 * - Encrypt it to the right layer
325 * - Append it to the appropriate cell_queue on <b>circ</b>.
327 static int
328 circuit_package_relay_cell(cell_t *cell, circuit_t *circ,
329 int cell_direction,
330 crypt_path_t *layer_hint)
332 or_connection_t *conn; /* where to send the cell */
334 if (cell_direction == CELL_DIRECTION_OUT) {
335 crypt_path_t *thishop; /* counter for repeated crypts */
336 conn = circ->n_conn;
337 if (!CIRCUIT_IS_ORIGIN(circ) || !conn) {
338 log_warn(LD_BUG,"outgoing relay cell has n_conn==NULL. Dropping.");
339 return 0; /* just drop it */
341 relay_set_digest(layer_hint->f_digest, cell);
343 thishop = layer_hint;
344 /* moving from farthest to nearest hop */
345 do {
346 tor_assert(thishop);
347 /* XXXX RD This is a bug, right? */
348 log_debug(LD_OR,"crypting a layer of the relay cell.");
349 if (relay_crypt_one_payload(thishop->f_crypto, cell->payload, 1) < 0) {
350 return -1;
353 thishop = thishop->prev;
354 } while (thishop != TO_ORIGIN_CIRCUIT(circ)->cpath->prev);
356 } else { /* incoming cell */
357 or_circuit_t *or_circ;
358 if (CIRCUIT_IS_ORIGIN(circ)) {
359 /* We should never package an _incoming_ cell from the circuit
360 * origin; that means we messed up somewhere. */
361 log_warn(LD_BUG,"incoming relay cell at origin circuit. Dropping.");
362 assert_circuit_ok(circ);
363 return 0; /* just drop it */
365 or_circ = TO_OR_CIRCUIT(circ);
366 conn = or_circ->p_conn;
367 relay_set_digest(or_circ->p_digest, cell);
368 if (relay_crypt_one_payload(or_circ->p_crypto, cell->payload, 1) < 0)
369 return -1;
371 ++stats_n_relay_cells_relayed;
373 append_cell_to_circuit_queue(circ, conn, cell, cell_direction);
374 return 0;
377 /** If cell's stream_id matches the stream_id of any conn that's
378 * attached to circ, return that conn, else return NULL.
380 static edge_connection_t *
381 relay_lookup_conn(circuit_t *circ, cell_t *cell, int cell_direction,
382 crypt_path_t *layer_hint)
384 edge_connection_t *tmpconn;
385 relay_header_t rh;
387 relay_header_unpack(&rh, cell->payload);
389 if (!rh.stream_id)
390 return NULL;
392 /* IN or OUT cells could have come from either direction, now
393 * that we allow rendezvous *to* an OP.
396 if (CIRCUIT_IS_ORIGIN(circ)) {
397 for (tmpconn = TO_ORIGIN_CIRCUIT(circ)->p_streams; tmpconn;
398 tmpconn=tmpconn->next_stream) {
399 if (rh.stream_id == tmpconn->stream_id &&
400 !tmpconn->_base.marked_for_close &&
401 tmpconn->cpath_layer == layer_hint) {
402 log_debug(LD_APP,"found conn for stream %d.", rh.stream_id);
403 return tmpconn;
406 } else {
407 for (tmpconn = TO_OR_CIRCUIT(circ)->n_streams; tmpconn;
408 tmpconn=tmpconn->next_stream) {
409 if (rh.stream_id == tmpconn->stream_id &&
410 !tmpconn->_base.marked_for_close) {
411 log_debug(LD_EXIT,"found conn for stream %d.", rh.stream_id);
412 if (cell_direction == CELL_DIRECTION_OUT ||
413 connection_edge_is_rendezvous_stream(tmpconn))
414 return tmpconn;
417 for (tmpconn = TO_OR_CIRCUIT(circ)->resolving_streams; tmpconn;
418 tmpconn=tmpconn->next_stream) {
419 if (rh.stream_id == tmpconn->stream_id &&
420 !tmpconn->_base.marked_for_close) {
421 log_debug(LD_EXIT,"found conn for stream %d.", rh.stream_id);
422 return tmpconn;
426 return NULL; /* probably a begin relay cell */
429 /** Pack the relay_header_t host-order structure <b>src</b> into
430 * network-order in the buffer <b>dest</b>. See tor-spec.txt for details
431 * about the wire format.
433 void
434 relay_header_pack(char *dest, const relay_header_t *src)
436 *(uint8_t*)(dest) = src->command;
438 set_uint16(dest+1, htons(src->recognized));
439 set_uint16(dest+3, htons(src->stream_id));
440 memcpy(dest+5, src->integrity, 4);
441 set_uint16(dest+9, htons(src->length));
444 /** Unpack the network-order buffer <b>src</b> into a host-order
445 * relay_header_t structure <b>dest</b>.
447 void
448 relay_header_unpack(relay_header_t *dest, const char *src)
450 dest->command = *(uint8_t*)(src);
452 dest->recognized = ntohs(get_uint16(src+1));
453 dest->stream_id = ntohs(get_uint16(src+3));
454 memcpy(dest->integrity, src+5, 4);
455 dest->length = ntohs(get_uint16(src+9));
458 /** Make a relay cell out of <b>relay_command</b> and <b>payload</b>, and send
459 * it onto the open circuit <b>circ</b>. <b>stream_id</b> is the ID on
460 * <b>circ</b> for the stream that's sending the relay cell, or 0 if it's a
461 * control cell. <b>cpath_layer</b> is NULL for OR->OP cells, or the
462 * destination hop for OP->OR cells.
464 * If you can't send the cell, mark the circuit for close and return -1. Else
465 * return 0.
468 relay_send_command_from_edge(uint16_t stream_id, circuit_t *circ,
469 uint8_t relay_command, const char *payload,
470 size_t payload_len, crypt_path_t *cpath_layer)
472 cell_t cell;
473 relay_header_t rh;
474 int cell_direction;
475 /* XXXX NM Split this function into a separate versions per circuit type? */
477 tor_assert(circ);
478 tor_assert(payload_len <= RELAY_PAYLOAD_SIZE);
480 memset(&cell, 0, sizeof(cell_t));
481 cell.command = CELL_RELAY;
482 if (cpath_layer) {
483 cell.circ_id = circ->n_circ_id;
484 cell_direction = CELL_DIRECTION_OUT;
485 } else if (! CIRCUIT_IS_ORIGIN(circ)) {
486 cell.circ_id = TO_OR_CIRCUIT(circ)->p_circ_id;
487 cell_direction = CELL_DIRECTION_IN;
488 } else {
489 return -1;
492 memset(&rh, 0, sizeof(rh));
493 rh.command = relay_command;
494 rh.stream_id = stream_id;
495 rh.length = payload_len;
496 relay_header_pack(cell.payload, &rh);
497 if (payload_len)
498 memcpy(cell.payload+RELAY_HEADER_SIZE, payload, payload_len);
500 log_debug(LD_OR,"delivering %d cell %s.", relay_command,
501 cell_direction == CELL_DIRECTION_OUT ? "forward" : "backward");
503 if (cell_direction == CELL_DIRECTION_OUT && circ->n_conn) {
504 /* if we're using relaybandwidthrate, this conn wants priority */
505 /* XXXX021 the call to time() seems little too frequent */
506 circ->n_conn->client_used = time(NULL);
509 if (circuit_package_relay_cell(&cell, circ, cell_direction, cpath_layer)
510 < 0) {
511 log_warn(LD_BUG,"circuit_package_relay_cell failed. Closing.");
512 circuit_mark_for_close(circ, END_CIRC_REASON_INTERNAL);
513 return -1;
515 return 0;
518 /** Make a relay cell out of <b>relay_command</b> and <b>payload</b>, and
519 * send it onto the open circuit <b>circ</b>. <b>fromconn</b> is the stream
520 * that's sending the relay cell, or NULL if it's a control cell.
521 * <b>cpath_layer</b> is NULL for OR->OP cells, or the destination hop
522 * for OP->OR cells.
524 * If you can't send the cell, mark the circuit for close and
525 * return -1. Else return 0.
528 connection_edge_send_command(edge_connection_t *fromconn,
529 uint8_t relay_command, const char *payload,
530 size_t payload_len)
532 /* XXXX NM Split this function into a separate versions per circuit type? */
533 circuit_t *circ;
534 tor_assert(fromconn);
535 circ = fromconn->on_circuit;
537 if (fromconn->_base.marked_for_close) {
538 log_warn(LD_BUG,
539 "called on conn that's already marked for close at %s:%d.",
540 fromconn->_base.marked_for_close_file,
541 fromconn->_base.marked_for_close);
542 return 0;
545 if (!circ) {
546 if (fromconn->_base.type == CONN_TYPE_AP) {
547 log_info(LD_APP,"no circ. Closing conn.");
548 connection_mark_unattached_ap(fromconn, END_STREAM_REASON_INTERNAL);
549 } else {
550 log_info(LD_EXIT,"no circ. Closing conn.");
551 fromconn->_base.edge_has_sent_end = 1; /* no circ to send to */
552 fromconn->end_reason = END_STREAM_REASON_INTERNAL;
553 connection_mark_for_close(TO_CONN(fromconn));
555 return -1;
558 return relay_send_command_from_edge(fromconn->stream_id, circ,
559 relay_command, payload,
560 payload_len, fromconn->cpath_layer);
563 /** Translate <b>reason</b>, which came from a relay 'end' cell,
564 * into a static const string describing why the stream is closing.
565 * <b>reason</b> is -1 if no reason was provided.
567 static const char *
568 connection_edge_end_reason_str(int reason)
570 switch (reason) {
571 case -1:
572 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
573 "End cell arrived with length 0. Should be at least 1.");
574 return "MALFORMED";
575 case END_STREAM_REASON_MISC: return "misc error";
576 case END_STREAM_REASON_RESOLVEFAILED: return "resolve failed";
577 case END_STREAM_REASON_CONNECTREFUSED: return "connection refused";
578 case END_STREAM_REASON_EXITPOLICY: return "exit policy failed";
579 case END_STREAM_REASON_DESTROY: return "destroyed";
580 case END_STREAM_REASON_DONE: return "closed normally";
581 case END_STREAM_REASON_TIMEOUT: return "gave up (timeout)";
582 case END_STREAM_REASON_HIBERNATING: return "server is hibernating";
583 case END_STREAM_REASON_INTERNAL: return "internal error at server";
584 case END_STREAM_REASON_RESOURCELIMIT: return "server out of resources";
585 case END_STREAM_REASON_CONNRESET: return "connection reset";
586 case END_STREAM_REASON_TORPROTOCOL: return "Tor protocol error";
587 case END_STREAM_REASON_NOTDIRECTORY: return "not a directory";
588 default:
589 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
590 "Reason for ending (%d) not recognized.",reason);
591 return "unknown";
595 /** Translate <b>reason</b> (as from a relay 'end' cell) into an
596 * appropriate SOCKS5 reply code.
598 * A reason of 0 means that we're not actually expecting to send
599 * this code back to the socks client; we just call it 'succeeded'
600 * to keep things simple.
602 socks5_reply_status_t
603 connection_edge_end_reason_socks5_response(int reason)
605 switch (reason & END_STREAM_REASON_MASK) {
606 case 0:
607 return SOCKS5_SUCCEEDED;
608 case END_STREAM_REASON_MISC:
609 return SOCKS5_GENERAL_ERROR;
610 case END_STREAM_REASON_RESOLVEFAILED:
611 return SOCKS5_HOST_UNREACHABLE;
612 case END_STREAM_REASON_CONNECTREFUSED:
613 return SOCKS5_CONNECTION_REFUSED;
614 case END_STREAM_REASON_ENTRYPOLICY:
615 return SOCKS5_NOT_ALLOWED;
616 case END_STREAM_REASON_EXITPOLICY:
617 return SOCKS5_NOT_ALLOWED;
618 case END_STREAM_REASON_DESTROY:
619 return SOCKS5_GENERAL_ERROR;
620 case END_STREAM_REASON_DONE:
621 return SOCKS5_SUCCEEDED;
622 case END_STREAM_REASON_TIMEOUT:
623 return SOCKS5_TTL_EXPIRED;
624 case END_STREAM_REASON_RESOURCELIMIT:
625 return SOCKS5_GENERAL_ERROR;
626 case END_STREAM_REASON_HIBERNATING:
627 return SOCKS5_GENERAL_ERROR;
628 case END_STREAM_REASON_INTERNAL:
629 return SOCKS5_GENERAL_ERROR;
630 case END_STREAM_REASON_CONNRESET:
631 return SOCKS5_CONNECTION_REFUSED;
632 case END_STREAM_REASON_TORPROTOCOL:
633 return SOCKS5_GENERAL_ERROR;
635 case END_STREAM_REASON_CANT_ATTACH:
636 return SOCKS5_GENERAL_ERROR;
637 case END_STREAM_REASON_NET_UNREACHABLE:
638 return SOCKS5_NET_UNREACHABLE;
639 case END_STREAM_REASON_SOCKSPROTOCOL:
640 return SOCKS5_GENERAL_ERROR;
641 default:
642 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
643 "Reason for ending (%d) not recognized; "
644 "sending generic socks error.", reason);
645 return SOCKS5_GENERAL_ERROR;
649 /* We need to use a few macros to deal with the fact that Windows
650 * decided that their sockets interface should be a permakludge.
651 * E_CASE is for errors where windows has both a EFOO and a WSAEFOO
652 * version, and S_CASE is for errors where windows has only a WSAEFOO
653 * version. (The E is for 'error', the S is for 'socket'). */
654 #ifdef MS_WINDOWS
655 #define E_CASE(s) case s: case WSA ## s
656 #define S_CASE(s) case WSA ## s
657 #else
658 #define E_CASE(s) case s
659 #define S_CASE(s) case s
660 #endif
662 /** Given an errno from a failed exit connection, return a reason code
663 * appropriate for use in a RELAY END cell.
666 errno_to_end_reason(int e)
668 switch (e) {
669 case EPIPE:
670 return END_STREAM_REASON_DONE;
671 E_CASE(EBADF):
672 E_CASE(EFAULT):
673 E_CASE(EINVAL):
674 S_CASE(EISCONN):
675 S_CASE(ENOTSOCK):
676 S_CASE(EPROTONOSUPPORT):
677 S_CASE(EAFNOSUPPORT):
678 E_CASE(EACCES):
679 S_CASE(ENOTCONN):
680 S_CASE(ENETUNREACH):
681 return END_STREAM_REASON_INTERNAL;
682 S_CASE(ECONNREFUSED):
683 return END_STREAM_REASON_CONNECTREFUSED;
684 S_CASE(ECONNRESET):
685 return END_STREAM_REASON_CONNRESET;
686 S_CASE(ETIMEDOUT):
687 return END_STREAM_REASON_TIMEOUT;
688 S_CASE(ENOBUFS):
689 case ENOMEM:
690 case ENFILE:
691 E_CASE(EMFILE):
692 return END_STREAM_REASON_RESOURCELIMIT;
693 default:
694 log_info(LD_EXIT, "Didn't recognize errno %d (%s); telling the client "
695 "that we are ending a stream for 'misc' reason.",
696 e, tor_socket_strerror(e));
697 return END_STREAM_REASON_MISC;
701 /** How many times will I retry a stream that fails due to DNS
702 * resolve failure or misc error?
704 #define MAX_RESOLVE_FAILURES 3
706 /** Return 1 if reason is something that you should retry if you
707 * get the end cell before you've connected; else return 0. */
708 static int
709 edge_reason_is_retriable(int reason)
711 return reason == END_STREAM_REASON_HIBERNATING ||
712 reason == END_STREAM_REASON_RESOURCELIMIT ||
713 reason == END_STREAM_REASON_EXITPOLICY ||
714 reason == END_STREAM_REASON_RESOLVEFAILED ||
715 reason == END_STREAM_REASON_MISC;
718 /** Called when we receive an END cell on a stream that isn't open yet.
719 * Arguments are as for connection_edge_process_relay_cell().
721 static int
722 connection_edge_process_end_not_open(
723 relay_header_t *rh, cell_t *cell, origin_circuit_t *circ,
724 edge_connection_t *conn, crypt_path_t *layer_hint)
726 struct in_addr in;
727 routerinfo_t *exitrouter;
728 int reason = *(cell->payload+RELAY_HEADER_SIZE);
729 int control_reason = reason | END_STREAM_REASON_FLAG_REMOTE;
730 (void) layer_hint; /* unused */
732 if (rh->length > 0 && edge_reason_is_retriable(reason) &&
733 conn->_base.type == CONN_TYPE_AP) {
734 log_info(LD_APP,"Address '%s' refused due to '%s'. Considering retrying.",
735 safe_str(conn->socks_request->address),
736 connection_edge_end_reason_str(reason));
737 exitrouter =
738 router_get_by_digest(circ->build_state->chosen_exit->identity_digest);
739 switch (reason) {
740 case END_STREAM_REASON_EXITPOLICY:
741 if (rh->length >= 5) {
742 uint32_t addr = ntohl(get_uint32(cell->payload+RELAY_HEADER_SIZE+1));
743 int ttl;
744 if (!addr) {
745 log_info(LD_APP,"Address '%s' resolved to 0.0.0.0. Closing,",
746 safe_str(conn->socks_request->address));
747 connection_mark_unattached_ap(conn, END_STREAM_REASON_TORPROTOCOL);
748 return 0;
750 if (rh->length >= 9)
751 ttl = (int)ntohl(get_uint32(cell->payload+RELAY_HEADER_SIZE+5));
752 else
753 ttl = -1;
754 client_dns_set_addressmap(conn->socks_request->address, addr,
755 conn->chosen_exit_name, ttl);
757 /* check if he *ought* to have allowed it */
758 if (exitrouter &&
759 (rh->length < 5 ||
760 (tor_inet_aton(conn->socks_request->address, &in) &&
761 !conn->chosen_exit_name))) {
762 log_info(LD_APP,
763 "Exitrouter '%s' seems to be more restrictive than its exit "
764 "policy. Not using this router as exit for now.",
765 exitrouter->nickname);
766 policies_set_router_exitpolicy_to_reject_all(exitrouter);
768 /* rewrite it to an IP if we learned one. */
769 if (addressmap_rewrite(conn->socks_request->address,
770 sizeof(conn->socks_request->address),
771 NULL)) {
772 control_event_stream_status(conn, STREAM_EVENT_REMAP, 0);
774 if (conn->_base.chosen_exit_optional ||
775 conn->_base.chosen_exit_retries) {
776 /* stop wanting a specific exit */
777 conn->_base.chosen_exit_optional = 0;
778 /* A non-zero chosen_exit_retries can happen if we set a
779 * TrackHostExits for this address under a port that the exit
780 * relay allows, but then try the same address with a different
781 * port that it doesn't allow to exit. We shouldn't unregister
782 * the mapping, since it is probably still wanted on the
783 * original port. But now we give away to the exit relay that
784 * we probably have a TrackHostExits on it. So be it. */
785 conn->_base.chosen_exit_retries = 0;
786 tor_free(conn->chosen_exit_name); /* clears it */
788 if (connection_ap_detach_retriable(conn, circ, control_reason) >= 0)
789 return 0;
790 /* else, conn will get closed below */
791 break;
792 case END_STREAM_REASON_CONNECTREFUSED:
793 if (!conn->_base.chosen_exit_optional)
794 break; /* break means it'll close, below */
795 /* Else fall through: expire this circuit, clear the
796 * chosen_exit_name field, and try again. */
797 case END_STREAM_REASON_RESOLVEFAILED:
798 case END_STREAM_REASON_TIMEOUT:
799 case END_STREAM_REASON_MISC:
800 if (client_dns_incr_failures(conn->socks_request->address)
801 < MAX_RESOLVE_FAILURES) {
802 /* We haven't retried too many times; reattach the connection. */
803 circuit_log_path(LOG_INFO,LD_APP,circ);
804 tor_assert(circ->_base.timestamp_dirty);
805 circ->_base.timestamp_dirty -= get_options()->MaxCircuitDirtiness;
807 if (conn->_base.chosen_exit_optional) {
808 /* stop wanting a specific exit */
809 conn->_base.chosen_exit_optional = 0;
810 tor_free(conn->chosen_exit_name); /* clears it */
812 if (connection_ap_detach_retriable(conn, circ, control_reason) >= 0)
813 return 0;
814 /* else, conn will get closed below */
815 } else {
816 log_notice(LD_APP,
817 "Have tried resolving or connecting to address '%s' "
818 "at %d different places. Giving up.",
819 safe_str(conn->socks_request->address),
820 MAX_RESOLVE_FAILURES);
821 /* clear the failures, so it will have a full try next time */
822 client_dns_clear_failures(conn->socks_request->address);
824 break;
825 case END_STREAM_REASON_HIBERNATING:
826 case END_STREAM_REASON_RESOURCELIMIT:
827 if (exitrouter) {
828 policies_set_router_exitpolicy_to_reject_all(exitrouter);
830 if (conn->_base.chosen_exit_optional) {
831 /* stop wanting a specific exit */
832 conn->_base.chosen_exit_optional = 0;
833 tor_free(conn->chosen_exit_name); /* clears it */
835 if (connection_ap_detach_retriable(conn, circ, control_reason) >= 0)
836 return 0;
837 /* else, will close below */
838 break;
839 } /* end switch */
840 log_info(LD_APP,"Giving up on retrying; conn can't be handled.");
843 log_info(LD_APP,
844 "Edge got end (%s) before we're connected. Marking for close.",
845 connection_edge_end_reason_str(rh->length > 0 ? reason : -1));
846 if (conn->_base.type == CONN_TYPE_AP) {
847 circuit_log_path(LOG_INFO,LD_APP,circ);
848 /* need to test because of detach_retriable*/
849 if (!conn->_base.marked_for_close)
850 connection_mark_unattached_ap(conn, control_reason);
851 } else {
852 /* we just got an 'end', don't need to send one */
853 conn->_base.edge_has_sent_end = 1;
854 conn->end_reason = control_reason;
855 connection_mark_for_close(TO_CONN(conn));
857 return 0;
860 /** Helper: change the socks_request-&gt;address field on conn to the
861 * dotted-quad representation of <b>new_addr</b> (given in host order),
862 * and send an appropriate REMAP event. */
863 static void
864 remap_event_helper(edge_connection_t *conn, uint32_t new_addr)
866 struct in_addr in;
868 in.s_addr = htonl(new_addr);
869 tor_inet_ntoa(&in, conn->socks_request->address,
870 sizeof(conn->socks_request->address));
871 control_event_stream_status(conn, STREAM_EVENT_REMAP,
872 REMAP_STREAM_SOURCE_EXIT);
875 /** An incoming relay cell has arrived from circuit <b>circ</b> to
876 * stream <b>conn</b>.
878 * The arguments here are the same as in
879 * connection_edge_process_relay_cell() below; this function is called
880 * from there when <b>conn</b> is defined and not in an open state.
882 static int
883 connection_edge_process_relay_cell_not_open(
884 relay_header_t *rh, cell_t *cell, circuit_t *circ,
885 edge_connection_t *conn, crypt_path_t *layer_hint)
887 if (rh->command == RELAY_COMMAND_END) {
888 if (CIRCUIT_IS_ORIGIN(circ))
889 return connection_edge_process_end_not_open(rh, cell,
890 TO_ORIGIN_CIRCUIT(circ), conn,
891 layer_hint);
892 else
893 return 0;
896 if (conn->_base.type == CONN_TYPE_AP &&
897 rh->command == RELAY_COMMAND_CONNECTED) {
898 tor_assert(CIRCUIT_IS_ORIGIN(circ));
899 if (conn->_base.state != AP_CONN_STATE_CONNECT_WAIT) {
900 log_fn(LOG_PROTOCOL_WARN, LD_APP,
901 "Got 'connected' while not in state connect_wait. Dropping.");
902 return 0;
904 conn->_base.state = AP_CONN_STATE_OPEN;
905 log_info(LD_APP,"'connected' received after %d seconds.",
906 (int)(time(NULL) - conn->_base.timestamp_lastread));
907 if (rh->length >= 4) {
908 uint32_t addr = ntohl(get_uint32(cell->payload+RELAY_HEADER_SIZE));
909 int ttl;
910 if (!addr || (get_options()->ClientDNSRejectInternalAddresses &&
911 is_internal_IP(addr, 0))) {
912 char buf[INET_NTOA_BUF_LEN];
913 struct in_addr a;
914 a.s_addr = htonl(addr);
915 tor_inet_ntoa(&a, buf, sizeof(buf));
916 log_info(LD_APP,
917 "...but it claims the IP address was %s. Closing.", buf);
918 connection_edge_end(conn, END_STREAM_REASON_TORPROTOCOL);
919 connection_mark_unattached_ap(conn, END_STREAM_REASON_TORPROTOCOL);
920 return 0;
922 if (rh->length >= 8)
923 ttl = (int)ntohl(get_uint32(cell->payload+RELAY_HEADER_SIZE+4));
924 else
925 ttl = -1;
926 client_dns_set_addressmap(conn->socks_request->address, addr,
927 conn->chosen_exit_name, ttl);
929 remap_event_helper(conn, addr);
931 circuit_log_path(LOG_INFO,LD_APP,TO_ORIGIN_CIRCUIT(circ));
932 /* don't send a socks reply to transparent conns */
933 if (!conn->socks_request->has_finished)
934 connection_ap_handshake_socks_reply(conn, NULL, 0, 0);
936 /* Was it a linked dir conn? If so, a dir request just started to
937 * fetch something; this could be a bootstrap status milestone. */
938 log_debug(LD_APP, "considering");
939 if (TO_CONN(conn)->linked_conn &&
940 TO_CONN(conn)->linked_conn->type == CONN_TYPE_DIR) {
941 connection_t *dirconn = TO_CONN(conn)->linked_conn;
942 log_debug(LD_APP, "it is! %d", dirconn->purpose);
943 switch (dirconn->purpose) {
944 case DIR_PURPOSE_FETCH_CERTIFICATE:
945 if (consensus_is_waiting_for_certs())
946 control_event_bootstrap(BOOTSTRAP_STATUS_LOADING_KEYS, 0);
947 break;
948 case DIR_PURPOSE_FETCH_CONSENSUS:
949 control_event_bootstrap(BOOTSTRAP_STATUS_LOADING_STATUS, 0);
950 break;
951 case DIR_PURPOSE_FETCH_SERVERDESC:
952 control_event_bootstrap(BOOTSTRAP_STATUS_LOADING_DESCRIPTORS,
953 count_loading_descriptors_progress());
954 break;
958 /* handle anything that might have queued */
959 if (connection_edge_package_raw_inbuf(conn, 1) < 0) {
960 /* (We already sent an end cell if possible) */
961 connection_mark_for_close(TO_CONN(conn));
962 return 0;
964 return 0;
966 if (conn->_base.type == CONN_TYPE_AP &&
967 rh->command == RELAY_COMMAND_RESOLVED) {
968 int ttl;
969 int answer_len;
970 uint8_t answer_type;
971 if (conn->_base.state != AP_CONN_STATE_RESOLVE_WAIT) {
972 log_fn(LOG_PROTOCOL_WARN, LD_APP, "Got a 'resolved' cell while "
973 "not in state resolve_wait. Dropping.");
974 return 0;
976 tor_assert(SOCKS_COMMAND_IS_RESOLVE(conn->socks_request->command));
977 answer_len = cell->payload[RELAY_HEADER_SIZE+1];
978 if (rh->length < 2 || answer_len+2>rh->length) {
979 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
980 "Dropping malformed 'resolved' cell");
981 connection_mark_unattached_ap(conn, END_STREAM_REASON_TORPROTOCOL);
982 return 0;
984 answer_type = cell->payload[RELAY_HEADER_SIZE];
985 if (rh->length >= answer_len+6)
986 ttl = (int)ntohl(get_uint32(cell->payload+RELAY_HEADER_SIZE+
987 2+answer_len));
988 else
989 ttl = -1;
990 if (answer_type == RESOLVED_TYPE_IPV4 && answer_len >= 4) {
991 uint32_t addr = ntohl(get_uint32(cell->payload+RELAY_HEADER_SIZE+2));
992 if (get_options()->ClientDNSRejectInternalAddresses &&
993 is_internal_IP(addr, 0)) {
994 char buf[INET_NTOA_BUF_LEN];
995 struct in_addr a;
996 a.s_addr = htonl(addr);
997 tor_inet_ntoa(&a, buf, sizeof(buf));
998 log_info(LD_APP,"Got a resolve with answer %s. Rejecting.", buf);
999 connection_ap_handshake_socks_resolved(conn,
1000 RESOLVED_TYPE_ERROR_TRANSIENT,
1001 0, NULL, 0, TIME_MAX);
1002 connection_mark_unattached_ap(conn, END_STREAM_REASON_TORPROTOCOL);
1003 return 0;
1006 connection_ap_handshake_socks_resolved(conn,
1007 answer_type,
1008 cell->payload[RELAY_HEADER_SIZE+1], /*answer_len*/
1009 cell->payload+RELAY_HEADER_SIZE+2, /*answer*/
1010 ttl,
1011 -1);
1012 if (answer_type == RESOLVED_TYPE_IPV4) {
1013 uint32_t addr = ntohl(get_uint32(cell->payload+RELAY_HEADER_SIZE+2));
1014 remap_event_helper(conn, addr);
1016 connection_mark_unattached_ap(conn,
1017 END_STREAM_REASON_DONE |
1018 END_STREAM_REASON_FLAG_ALREADY_SOCKS_REPLIED);
1019 return 0;
1022 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
1023 "Got an unexpected relay command %d, in state %d (%s). Dropping.",
1024 rh->command, conn->_base.state,
1025 conn_state_to_string(conn->_base.type, conn->_base.state));
1026 return 0; /* for forward compatibility, don't kill the circuit */
1027 // connection_edge_end(conn, END_STREAM_REASON_TORPROTOCOL);
1028 // connection_mark_for_close(conn);
1029 // return -1;
1032 /** An incoming relay cell has arrived on circuit <b>circ</b>. If
1033 * <b>conn</b> is NULL this is a control cell, else <b>cell</b> is
1034 * destined for <b>conn</b>.
1036 * If <b>layer_hint</b> is defined, then we're the origin of the
1037 * circuit, and it specifies the hop that packaged <b>cell</b>.
1039 * Return -reason if you want to warn and tear down the circuit, else 0.
1041 static int
1042 connection_edge_process_relay_cell(cell_t *cell, circuit_t *circ,
1043 edge_connection_t *conn,
1044 crypt_path_t *layer_hint)
1046 static int num_seen=0;
1047 relay_header_t rh;
1048 unsigned domain = layer_hint?LD_APP:LD_EXIT;
1049 int reason;
1051 tor_assert(cell);
1052 tor_assert(circ);
1054 relay_header_unpack(&rh, cell->payload);
1055 // log_fn(LOG_DEBUG,"command %d stream %d", rh.command, rh.stream_id);
1056 num_seen++;
1057 log_debug(domain, "Now seen %d relay cells here.", num_seen);
1059 if (rh.length > RELAY_PAYLOAD_SIZE) {
1060 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
1061 "Relay cell length field too long. Closing circuit.");
1062 return - END_CIRC_REASON_TORPROTOCOL;
1065 /* either conn is NULL, in which case we've got a control cell, or else
1066 * conn points to the recognized stream. */
1068 if (conn && !connection_state_is_open(TO_CONN(conn)))
1069 return connection_edge_process_relay_cell_not_open(
1070 &rh, cell, circ, conn, layer_hint);
1072 switch (rh.command) {
1073 case RELAY_COMMAND_DROP:
1074 // log_info(domain,"Got a relay-level padding cell. Dropping.");
1075 return 0;
1076 case RELAY_COMMAND_BEGIN:
1077 case RELAY_COMMAND_BEGIN_DIR:
1078 if (layer_hint &&
1079 circ->purpose != CIRCUIT_PURPOSE_S_REND_JOINED) {
1080 log_fn(LOG_PROTOCOL_WARN, LD_APP,
1081 "Relay begin request unsupported at AP. Dropping.");
1082 return 0;
1084 if (conn) {
1085 log_fn(LOG_PROTOCOL_WARN, domain,
1086 "Begin cell for known stream. Dropping.");
1087 return 0;
1089 return connection_exit_begin_conn(cell, circ);
1090 case RELAY_COMMAND_DATA:
1091 ++stats_n_data_cells_received;
1092 if (( layer_hint && --layer_hint->deliver_window < 0) ||
1093 (!layer_hint && --circ->deliver_window < 0)) {
1094 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
1095 "(relay data) circ deliver_window below 0. Killing.");
1096 connection_edge_end(conn, END_STREAM_REASON_TORPROTOCOL);
1097 connection_mark_for_close(TO_CONN(conn));
1098 return -END_CIRC_REASON_TORPROTOCOL;
1100 log_debug(domain,"circ deliver_window now %d.", layer_hint ?
1101 layer_hint->deliver_window : circ->deliver_window);
1103 circuit_consider_sending_sendme(circ, layer_hint);
1105 if (!conn) {
1106 log_info(domain,"data cell dropped, unknown stream.");
1107 return 0;
1110 if (--conn->deliver_window < 0) { /* is it below 0 after decrement? */
1111 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
1112 "(relay data) conn deliver_window below 0. Killing.");
1113 return -END_CIRC_REASON_TORPROTOCOL;
1116 stats_n_data_bytes_received += rh.length;
1117 connection_write_to_buf(cell->payload + RELAY_HEADER_SIZE,
1118 rh.length, TO_CONN(conn));
1119 connection_edge_consider_sending_sendme(conn);
1120 return 0;
1121 case RELAY_COMMAND_END:
1122 reason = rh.length > 0 ?
1123 *(uint8_t *)(cell->payload+RELAY_HEADER_SIZE) : END_STREAM_REASON_MISC;
1124 if (!conn) {
1125 log_info(domain,"end cell (%s) dropped, unknown stream.",
1126 connection_edge_end_reason_str(reason));
1127 return 0;
1129 /* XXX add to this log_fn the exit node's nickname? */
1130 log_info(domain,"%d: end cell (%s) for stream %d. Removing stream.",
1131 conn->_base.s,
1132 connection_edge_end_reason_str(reason),
1133 conn->stream_id);
1134 if (conn->socks_request && !conn->socks_request->has_finished)
1135 log_warn(LD_BUG,
1136 "open stream hasn't sent socks answer yet? Closing.");
1137 /* We just *got* an end; no reason to send one. */
1138 conn->_base.edge_has_sent_end = 1;
1139 if (!conn->end_reason)
1140 conn->end_reason = reason | END_STREAM_REASON_FLAG_REMOTE;
1141 if (!conn->_base.marked_for_close) {
1142 /* only mark it if not already marked. it's possible to
1143 * get the 'end' right around when the client hangs up on us. */
1144 connection_mark_for_close(TO_CONN(conn));
1145 conn->_base.hold_open_until_flushed = 1;
1147 return 0;
1148 case RELAY_COMMAND_EXTEND:
1149 if (conn) {
1150 log_fn(LOG_PROTOCOL_WARN, domain,
1151 "'extend' cell received for non-zero stream. Dropping.");
1152 return 0;
1154 return circuit_extend(cell, circ);
1155 case RELAY_COMMAND_EXTENDED:
1156 if (!layer_hint) {
1157 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
1158 "'extended' unsupported at non-origin. Dropping.");
1159 return 0;
1161 log_debug(domain,"Got an extended cell! Yay.");
1162 if ((reason = circuit_finish_handshake(TO_ORIGIN_CIRCUIT(circ),
1163 CELL_CREATED,
1164 cell->payload+RELAY_HEADER_SIZE)) < 0) {
1165 log_warn(domain,"circuit_finish_handshake failed.");
1166 return reason;
1168 if ((reason=circuit_send_next_onion_skin(TO_ORIGIN_CIRCUIT(circ)))<0) {
1169 log_info(domain,"circuit_send_next_onion_skin() failed.");
1170 return reason;
1172 return 0;
1173 case RELAY_COMMAND_TRUNCATE:
1174 if (layer_hint) {
1175 log_fn(LOG_PROTOCOL_WARN, LD_APP,
1176 "'truncate' unsupported at origin. Dropping.");
1177 return 0;
1179 if (circ->n_conn) {
1180 uint8_t trunc_reason = *(uint8_t*)(cell->payload + RELAY_HEADER_SIZE);
1181 connection_or_send_destroy(circ->n_circ_id, circ->n_conn,
1182 trunc_reason);
1183 circuit_set_n_circid_orconn(circ, 0, NULL);
1185 log_debug(LD_EXIT, "Processed 'truncate', replying.");
1187 char payload[1];
1188 payload[0] = (char)END_CIRC_REASON_REQUESTED;
1189 relay_send_command_from_edge(0, circ, RELAY_COMMAND_TRUNCATED,
1190 payload, sizeof(payload), NULL);
1192 return 0;
1193 case RELAY_COMMAND_TRUNCATED:
1194 if (!layer_hint) {
1195 log_fn(LOG_PROTOCOL_WARN, LD_EXIT,
1196 "'truncated' unsupported at non-origin. Dropping.");
1197 return 0;
1199 circuit_truncated(TO_ORIGIN_CIRCUIT(circ), layer_hint);
1200 return 0;
1201 case RELAY_COMMAND_CONNECTED:
1202 if (conn) {
1203 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
1204 "'connected' unsupported while open. Closing circ.");
1205 return -END_CIRC_REASON_TORPROTOCOL;
1207 log_info(domain,
1208 "'connected' received, no conn attached anymore. Ignoring.");
1209 return 0;
1210 case RELAY_COMMAND_SENDME:
1211 if (!conn) {
1212 if (layer_hint) {
1213 layer_hint->package_window += CIRCWINDOW_INCREMENT;
1214 log_debug(LD_APP,"circ-level sendme at origin, packagewindow %d.",
1215 layer_hint->package_window);
1216 circuit_resume_edge_reading(circ, layer_hint);
1217 } else {
1218 circ->package_window += CIRCWINDOW_INCREMENT;
1219 log_debug(LD_APP,
1220 "circ-level sendme at non-origin, packagewindow %d.",
1221 circ->package_window);
1222 circuit_resume_edge_reading(circ, layer_hint);
1224 return 0;
1226 conn->package_window += STREAMWINDOW_INCREMENT;
1227 log_debug(domain,"stream-level sendme, packagewindow now %d.",
1228 conn->package_window);
1229 connection_start_reading(TO_CONN(conn));
1230 /* handle whatever might still be on the inbuf */
1231 if (connection_edge_package_raw_inbuf(conn, 1) < 0) {
1232 /* (We already sent an end cell if possible) */
1233 connection_mark_for_close(TO_CONN(conn));
1234 return 0;
1236 return 0;
1237 case RELAY_COMMAND_RESOLVE:
1238 if (layer_hint) {
1239 log_fn(LOG_PROTOCOL_WARN, LD_APP,
1240 "resolve request unsupported at AP; dropping.");
1241 return 0;
1242 } else if (conn) {
1243 log_fn(LOG_PROTOCOL_WARN, domain,
1244 "resolve request for known stream; dropping.");
1245 return 0;
1246 } else if (circ->purpose != CIRCUIT_PURPOSE_OR) {
1247 log_fn(LOG_PROTOCOL_WARN, domain,
1248 "resolve request on circ with purpose %d; dropping",
1249 circ->purpose);
1250 return 0;
1252 connection_exit_begin_resolve(cell, TO_OR_CIRCUIT(circ));
1253 return 0;
1254 case RELAY_COMMAND_RESOLVED:
1255 if (conn) {
1256 log_fn(LOG_PROTOCOL_WARN, domain,
1257 "'resolved' unsupported while open. Closing circ.");
1258 return -END_CIRC_REASON_TORPROTOCOL;
1260 log_info(domain,
1261 "'resolved' received, no conn attached anymore. Ignoring.");
1262 return 0;
1263 case RELAY_COMMAND_ESTABLISH_INTRO:
1264 case RELAY_COMMAND_ESTABLISH_RENDEZVOUS:
1265 case RELAY_COMMAND_INTRODUCE1:
1266 case RELAY_COMMAND_INTRODUCE2:
1267 case RELAY_COMMAND_INTRODUCE_ACK:
1268 case RELAY_COMMAND_RENDEZVOUS1:
1269 case RELAY_COMMAND_RENDEZVOUS2:
1270 case RELAY_COMMAND_INTRO_ESTABLISHED:
1271 case RELAY_COMMAND_RENDEZVOUS_ESTABLISHED:
1272 rend_process_relay_cell(circ, rh.command, rh.length,
1273 cell->payload+RELAY_HEADER_SIZE);
1274 return 0;
1276 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
1277 "Received unknown relay command %d. Perhaps the other side is using "
1278 "a newer version of Tor? Dropping.",
1279 rh.command);
1280 return 0; /* for forward compatibility, don't kill the circuit */
1283 uint64_t stats_n_data_cells_packaged = 0;
1284 uint64_t stats_n_data_bytes_packaged = 0;
1285 uint64_t stats_n_data_cells_received = 0;
1286 uint64_t stats_n_data_bytes_received = 0;
1288 /** While conn->inbuf has an entire relay payload of bytes on it,
1289 * and the appropriate package windows aren't empty, grab a cell
1290 * and send it down the circuit.
1292 * Return -1 (and send a RELAY_END cell if necessary) if conn should
1293 * be marked for close, else return 0.
1296 connection_edge_package_raw_inbuf(edge_connection_t *conn, int package_partial)
1298 size_t amount_to_process, length;
1299 char payload[CELL_PAYLOAD_SIZE];
1300 circuit_t *circ;
1301 unsigned domain = conn->cpath_layer ? LD_APP : LD_EXIT;
1303 tor_assert(conn);
1305 if (conn->_base.marked_for_close) {
1306 log_warn(LD_BUG,
1307 "called on conn that's already marked for close at %s:%d.",
1308 conn->_base.marked_for_close_file, conn->_base.marked_for_close);
1309 return 0;
1312 repeat_connection_edge_package_raw_inbuf:
1314 circ = circuit_get_by_edge_conn(conn);
1315 if (!circ) {
1316 log_info(domain,"conn has no circuit! Closing.");
1317 conn->end_reason = END_STREAM_REASON_CANT_ATTACH;
1318 return -1;
1321 if (circuit_consider_stop_edge_reading(circ, conn->cpath_layer))
1322 return 0;
1324 if (conn->package_window <= 0) {
1325 log_info(domain,"called with package_window %d. Skipping.",
1326 conn->package_window);
1327 connection_stop_reading(TO_CONN(conn));
1328 return 0;
1331 amount_to_process = buf_datalen(conn->_base.inbuf);
1333 if (!amount_to_process)
1334 return 0;
1336 if (!package_partial && amount_to_process < RELAY_PAYLOAD_SIZE)
1337 return 0;
1339 if (amount_to_process > RELAY_PAYLOAD_SIZE) {
1340 length = RELAY_PAYLOAD_SIZE;
1341 } else {
1342 length = amount_to_process;
1344 stats_n_data_bytes_packaged += length;
1345 stats_n_data_cells_packaged += 1;
1347 connection_fetch_from_buf(payload, length, TO_CONN(conn));
1349 log_debug(domain,"(%d) Packaging %d bytes (%d waiting).", conn->_base.s,
1350 (int)length, (int)buf_datalen(conn->_base.inbuf));
1352 if (connection_edge_send_command(conn, RELAY_COMMAND_DATA,
1353 payload, length) < 0 )
1354 /* circuit got marked for close, don't continue, don't need to mark conn */
1355 return 0;
1357 if (!conn->cpath_layer) { /* non-rendezvous exit */
1358 tor_assert(circ->package_window > 0);
1359 circ->package_window--;
1360 } else { /* we're an AP, or an exit on a rendezvous circ */
1361 tor_assert(conn->cpath_layer->package_window > 0);
1362 conn->cpath_layer->package_window--;
1365 if (--conn->package_window <= 0) { /* is it 0 after decrement? */
1366 connection_stop_reading(TO_CONN(conn));
1367 log_debug(domain,"conn->package_window reached 0.");
1368 circuit_consider_stop_edge_reading(circ, conn->cpath_layer);
1369 return 0; /* don't process the inbuf any more */
1371 log_debug(domain,"conn->package_window is now %d",conn->package_window);
1373 /* handle more if there's more, or return 0 if there isn't */
1374 goto repeat_connection_edge_package_raw_inbuf;
1377 /** Called when we've just received a relay data cell, or when
1378 * we've just finished flushing all bytes to stream <b>conn</b>.
1380 * If conn->outbuf is not too full, and our deliver window is
1381 * low, send back a suitable number of stream-level sendme cells.
1383 void
1384 connection_edge_consider_sending_sendme(edge_connection_t *conn)
1386 circuit_t *circ;
1388 if (connection_outbuf_too_full(TO_CONN(conn)))
1389 return;
1391 circ = circuit_get_by_edge_conn(conn);
1392 if (!circ) {
1393 /* this can legitimately happen if the destroy has already
1394 * arrived and torn down the circuit */
1395 log_info(LD_APP,"No circuit associated with conn. Skipping.");
1396 return;
1399 while (conn->deliver_window < STREAMWINDOW_START - STREAMWINDOW_INCREMENT) {
1400 log_debug(conn->cpath_layer?LD_APP:LD_EXIT,
1401 "Outbuf %d, Queueing stream sendme.",
1402 (int)conn->_base.outbuf_flushlen);
1403 conn->deliver_window += STREAMWINDOW_INCREMENT;
1404 if (connection_edge_send_command(conn, RELAY_COMMAND_SENDME,
1405 NULL, 0) < 0) {
1406 log_warn(LD_APP,"connection_edge_send_command failed. Skipping.");
1407 return; /* the circuit's closed, don't continue */
1412 /** The circuit <b>circ</b> has received a circuit-level sendme
1413 * (on hop <b>layer_hint</b>, if we're the OP). Go through all the
1414 * attached streams and let them resume reading and packaging, if
1415 * their stream windows allow it.
1417 static void
1418 circuit_resume_edge_reading(circuit_t *circ, crypt_path_t *layer_hint)
1421 log_debug(layer_hint?LD_APP:LD_EXIT,"resuming");
1423 if (CIRCUIT_IS_ORIGIN(circ))
1424 circuit_resume_edge_reading_helper(TO_ORIGIN_CIRCUIT(circ)->p_streams,
1425 circ, layer_hint);
1426 else
1427 circuit_resume_edge_reading_helper(TO_OR_CIRCUIT(circ)->n_streams,
1428 circ, layer_hint);
1431 /** A helper function for circuit_resume_edge_reading() above.
1432 * The arguments are the same, except that <b>conn</b> is the head
1433 * of a linked list of edge streams that should each be considered.
1435 static int
1436 circuit_resume_edge_reading_helper(edge_connection_t *conn,
1437 circuit_t *circ,
1438 crypt_path_t *layer_hint)
1440 for ( ; conn; conn=conn->next_stream) {
1441 if (conn->_base.marked_for_close)
1442 continue;
1443 if ((!layer_hint && conn->package_window > 0) ||
1444 (layer_hint && conn->package_window > 0 &&
1445 conn->cpath_layer == layer_hint)) {
1446 connection_start_reading(TO_CONN(conn));
1447 /* handle whatever might still be on the inbuf */
1448 if (connection_edge_package_raw_inbuf(conn, 1)<0) {
1449 /* (We already sent an end cell if possible) */
1450 connection_mark_for_close(TO_CONN(conn));
1451 continue;
1454 /* If the circuit won't accept any more data, return without looking
1455 * at any more of the streams. Any connections that should be stopped
1456 * have already been stopped by connection_edge_package_raw_inbuf. */
1457 if (circuit_consider_stop_edge_reading(circ, layer_hint))
1458 return -1;
1461 return 0;
1464 /** Check if the package window for <b>circ</b> is empty (at
1465 * hop <b>layer_hint</b> if it's defined).
1467 * If yes, tell edge streams to stop reading and return 1.
1468 * Else return 0.
1470 static int
1471 circuit_consider_stop_edge_reading(circuit_t *circ, crypt_path_t *layer_hint)
1473 edge_connection_t *conn = NULL;
1474 unsigned domain = layer_hint ? LD_APP : LD_EXIT;
1476 if (!layer_hint) {
1477 or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
1478 log_debug(domain,"considering circ->package_window %d",
1479 circ->package_window);
1480 if (circ->package_window <= 0) {
1481 log_debug(domain,"yes, not-at-origin. stopped.");
1482 for (conn = or_circ->n_streams; conn; conn=conn->next_stream)
1483 connection_stop_reading(TO_CONN(conn));
1484 return 1;
1486 return 0;
1488 /* else, layer hint is defined, use it */
1489 log_debug(domain,"considering layer_hint->package_window %d",
1490 layer_hint->package_window);
1491 if (layer_hint->package_window <= 0) {
1492 log_debug(domain,"yes, at-origin. stopped.");
1493 for (conn = TO_ORIGIN_CIRCUIT(circ)->p_streams; conn;
1494 conn=conn->next_stream)
1495 if (conn->cpath_layer == layer_hint)
1496 connection_stop_reading(TO_CONN(conn));
1497 return 1;
1499 return 0;
1502 /** Check if the deliver_window for circuit <b>circ</b> (at hop
1503 * <b>layer_hint</b> if it's defined) is low enough that we should
1504 * send a circuit-level sendme back down the circuit. If so, send
1505 * enough sendmes that the window would be overfull if we sent any
1506 * more.
1508 static void
1509 circuit_consider_sending_sendme(circuit_t *circ, crypt_path_t *layer_hint)
1511 // log_fn(LOG_INFO,"Considering: layer_hint is %s",
1512 // layer_hint ? "defined" : "null");
1513 while ((layer_hint ? layer_hint->deliver_window : circ->deliver_window) <
1514 CIRCWINDOW_START - CIRCWINDOW_INCREMENT) {
1515 log_debug(LD_CIRC,"Queueing circuit sendme.");
1516 if (layer_hint)
1517 layer_hint->deliver_window += CIRCWINDOW_INCREMENT;
1518 else
1519 circ->deliver_window += CIRCWINDOW_INCREMENT;
1520 if (relay_send_command_from_edge(0, circ, RELAY_COMMAND_SENDME,
1521 NULL, 0, layer_hint) < 0) {
1522 log_warn(LD_CIRC,
1523 "relay_send_command_from_edge failed. Circuit's closed.");
1524 return; /* the circuit's closed, don't continue */
1529 /** Stop reading on edge connections when we have this many cells
1530 * waiting on the appropriate queue. */
1531 #define CELL_QUEUE_HIGHWATER_SIZE 256
1532 /** Start reading from edge connections again when we get down to this many
1533 * cells. */
1534 #define CELL_QUEUE_LOWWATER_SIZE 64
1536 #ifdef ACTIVE_CIRCUITS_PARANOIA
1537 #define assert_active_circuits_ok_paranoid(conn) \
1538 assert_active_circuits_ok(conn)
1539 #else
1540 #define assert_active_circuits_ok_paranoid(conn)
1541 #endif
1543 /** The total number of cells we have allocated from the memory pool. */
1544 static int total_cells_allocated = 0;
1546 #ifdef ENABLE_CELL_POOL /* Defined in ./configure. True by default. */
1547 /* XXX021 make cell pools the only option once we know they work and improve
1548 * matters? -RD */
1549 static mp_pool_t *cell_pool = NULL;
1550 /** Allocate structures to hold cells. */
1551 void
1552 init_cell_pool(void)
1554 tor_assert(!cell_pool);
1555 cell_pool = mp_pool_new(sizeof(packed_cell_t), 128*1024);
1558 /** Free all storage used to hold cells. */
1559 void
1560 free_cell_pool(void)
1562 /* Maybe we haven't called init_cell_pool yet; need to check for it. */
1563 if (cell_pool) {
1564 mp_pool_destroy(cell_pool);
1565 cell_pool = NULL;
1569 /** Free excess storage in cell pool. */
1570 void
1571 clean_cell_pool(void)
1573 tor_assert(cell_pool);
1574 mp_pool_clean(cell_pool, 0, 1);
1577 /** Release storage held by <b>cell</b>. */
1578 static INLINE void
1579 packed_cell_free(packed_cell_t *cell)
1581 --total_cells_allocated;
1582 mp_pool_release(cell);
1585 /** Allocate and return a new packed_cell_t. */
1586 static INLINE packed_cell_t *
1587 packed_cell_alloc(void)
1589 ++total_cells_allocated;
1590 return mp_pool_get(cell_pool);
1592 void
1593 dump_cell_pool_usage(int severity)
1595 circuit_t *c;
1596 int n_circs = 0;
1597 int n_cells = 0;
1598 for (c = _circuit_get_global_list(); c; c = c->next) {
1599 n_cells += c->n_conn_cells.n;
1600 if (!CIRCUIT_IS_ORIGIN(c))
1601 n_cells += TO_OR_CIRCUIT(c)->p_conn_cells.n;
1602 ++n_circs;
1604 log(severity, LD_MM, "%d cells allocated on %d circuits. %d cells leaked.",
1605 n_cells, n_circs, total_cells_allocated - n_cells);
1606 mp_pool_log_status(cell_pool, severity);
1608 #else
1609 /* ENABLE_CELL_POOL isn't defined: here are some stubs to use tor_malloc()
1610 * and tor_free() instead. */
1611 void
1612 init_cell_pool(void)
1616 void
1617 free_cell_pool(void)
1621 void
1622 clean_cell_pool(void)
1626 static INLINE void
1627 packed_cell_free(packed_cell_t *cell)
1629 --total_cells_allocated;
1630 tor_free(cell);
1633 static INLINE packed_cell_t *
1634 packed_cell_alloc(void)
1636 ++total_cells_allocated;
1637 return tor_malloc(sizeof(packed_cell_t));
1639 void
1640 dump_cell_pool_usage(int severity)
1642 (void) severity;
1644 #endif
1646 /** Allocate a new copy of packed <b>cell</b>. */
1647 static INLINE packed_cell_t *
1648 packed_cell_copy(const cell_t *cell)
1650 packed_cell_t *c = packed_cell_alloc();
1651 cell_pack(c, cell);
1652 c->next = NULL;
1653 return c;
1656 /** Append <b>cell</b> to the end of <b>queue</b>. */
1657 void
1658 cell_queue_append(cell_queue_t *queue, packed_cell_t *cell)
1660 if (queue->tail) {
1661 tor_assert(!queue->tail->next);
1662 queue->tail->next = cell;
1663 } else {
1664 queue->head = cell;
1666 queue->tail = cell;
1667 cell->next = NULL;
1668 ++queue->n;
1671 /** Append a newly allocated copy of <b>cell</b> to the end of <b>queue</b> */
1672 void
1673 cell_queue_append_packed_copy(cell_queue_t *queue, const cell_t *cell)
1675 cell_queue_append(queue, packed_cell_copy(cell));
1678 /** Remove and free every cell in <b>queue</b>. */
1679 void
1680 cell_queue_clear(cell_queue_t *queue)
1682 packed_cell_t *cell, *next;
1683 cell = queue->head;
1684 while (cell) {
1685 next = cell->next;
1686 packed_cell_free(cell);
1687 cell = next;
1689 queue->head = queue->tail = NULL;
1690 queue->n = 0;
1693 /** Extract and return the cell at the head of <b>queue</b>; return NULL if
1694 * <b>queue</b> is empty. */
1695 static INLINE packed_cell_t *
1696 cell_queue_pop(cell_queue_t *queue)
1698 packed_cell_t *cell = queue->head;
1699 if (!cell)
1700 return NULL;
1701 queue->head = cell->next;
1702 if (cell == queue->tail) {
1703 tor_assert(!queue->head);
1704 queue->tail = NULL;
1706 --queue->n;
1707 return cell;
1710 /** Return a pointer to the "next_active_on_{n,p}_conn" pointer of <b>circ</b>,
1711 * depending on whether <b>conn</b> matches n_conn or p_conn. */
1712 static INLINE circuit_t **
1713 next_circ_on_conn_p(circuit_t *circ, or_connection_t *conn)
1715 tor_assert(circ);
1716 tor_assert(conn);
1717 if (conn == circ->n_conn) {
1718 return &circ->next_active_on_n_conn;
1719 } else {
1720 or_circuit_t *orcirc = TO_OR_CIRCUIT(circ);
1721 tor_assert(conn == orcirc->p_conn);
1722 return &orcirc->next_active_on_p_conn;
1726 /** Return a pointer to the "prev_active_on_{n,p}_conn" pointer of <b>circ</b>,
1727 * depending on whether <b>conn</b> matches n_conn or p_conn. */
1728 static INLINE circuit_t **
1729 prev_circ_on_conn_p(circuit_t *circ, or_connection_t *conn)
1731 tor_assert(circ);
1732 tor_assert(conn);
1733 if (conn == circ->n_conn) {
1734 return &circ->prev_active_on_n_conn;
1735 } else {
1736 or_circuit_t *orcirc = TO_OR_CIRCUIT(circ);
1737 tor_assert(conn == orcirc->p_conn);
1738 return &orcirc->prev_active_on_p_conn;
1742 /** Add <b>circ</b> to the list of circuits with pending cells on
1743 * <b>conn</b>. No effect if <b>circ</b> is already unlinked. */
1744 void
1745 make_circuit_active_on_conn(circuit_t *circ, or_connection_t *conn)
1747 circuit_t **nextp = next_circ_on_conn_p(circ, conn);
1748 circuit_t **prevp = prev_circ_on_conn_p(circ, conn);
1750 if (*nextp && *prevp) {
1751 /* Already active. */
1752 return;
1755 if (! conn->active_circuits) {
1756 conn->active_circuits = circ;
1757 *prevp = *nextp = circ;
1758 } else {
1759 circuit_t *head = conn->active_circuits;
1760 circuit_t *old_tail = *prev_circ_on_conn_p(head, conn);
1761 *next_circ_on_conn_p(old_tail, conn) = circ;
1762 *nextp = head;
1763 *prev_circ_on_conn_p(head, conn) = circ;
1764 *prevp = old_tail;
1766 assert_active_circuits_ok_paranoid(conn);
1769 /** Remove <b>circ</b> to the list of circuits with pending cells on
1770 * <b>conn</b>. No effect if <b>circ</b> is already unlinked. */
1771 void
1772 make_circuit_inactive_on_conn(circuit_t *circ, or_connection_t *conn)
1774 circuit_t **nextp = next_circ_on_conn_p(circ, conn);
1775 circuit_t **prevp = prev_circ_on_conn_p(circ, conn);
1776 circuit_t *next = *nextp, *prev = *prevp;
1778 if (!next && !prev) {
1779 /* Already inactive. */
1780 return;
1783 tor_assert(next && prev);
1784 tor_assert(*prev_circ_on_conn_p(next, conn) == circ);
1785 tor_assert(*next_circ_on_conn_p(prev, conn) == circ);
1787 if (next == circ) {
1788 conn->active_circuits = NULL;
1789 } else {
1790 *prev_circ_on_conn_p(next, conn) = prev;
1791 *next_circ_on_conn_p(prev, conn) = next;
1792 if (conn->active_circuits == circ)
1793 conn->active_circuits = next;
1795 *prevp = *nextp = NULL;
1796 assert_active_circuits_ok_paranoid(conn);
1799 /** Remove all circuits from the list of circuits with pending cells on
1800 * <b>conn</b>. */
1801 void
1802 connection_or_unlink_all_active_circs(or_connection_t *orconn)
1804 circuit_t *head = orconn->active_circuits;
1805 circuit_t *cur = head;
1806 if (! head)
1807 return;
1808 do {
1809 circuit_t *next = *next_circ_on_conn_p(cur, orconn);
1810 *prev_circ_on_conn_p(cur, orconn) = NULL;
1811 *next_circ_on_conn_p(cur, orconn) = NULL;
1812 cur = next;
1813 } while (cur != head);
1814 orconn->active_circuits = NULL;
1817 /** Block (if <b>block</b> is true) or unblock (if <b>block</b> is false)
1818 * every edge connection that is using <b>circ</b> to write to <b>orconn</b>,
1819 * and start or stop reading as appropriate. */
1820 static void
1821 set_streams_blocked_on_circ(circuit_t *circ, or_connection_t *orconn,
1822 int block)
1824 edge_connection_t *edge = NULL;
1825 if (circ->n_conn == orconn) {
1826 circ->streams_blocked_on_n_conn = block;
1827 if (CIRCUIT_IS_ORIGIN(circ))
1828 edge = TO_ORIGIN_CIRCUIT(circ)->p_streams;
1829 } else {
1830 circ->streams_blocked_on_p_conn = block;
1831 tor_assert(!CIRCUIT_IS_ORIGIN(circ));
1832 edge = TO_OR_CIRCUIT(circ)->n_streams;
1835 for (; edge; edge = edge->next_stream) {
1836 connection_t *conn = TO_CONN(edge);
1837 conn->edge_blocked_on_circ = block;
1839 if (!conn->read_event) {
1840 /* This connection is a placeholder for something; probably a DNS
1841 * request. It can't actually stop or start reading.*/
1842 continue;
1845 if (block) {
1846 if (connection_is_reading(conn))
1847 connection_stop_reading(conn);
1848 } else {
1849 /* Is this right? */
1850 if (!connection_is_reading(conn))
1851 connection_start_reading(conn);
1856 /** Pull as many cells as possible (but no more than <b>max</b>) from the
1857 * queue of the first active circuit on <b>conn</b>, and write then to
1858 * <b>conn</b>-&gt;outbuf. Return the number of cells written. Advance
1859 * the active circuit pointer to the next active circuit in the ring. */
1861 connection_or_flush_from_first_active_circuit(or_connection_t *conn, int max,
1862 time_t now)
1864 int n_flushed;
1865 cell_queue_t *queue;
1866 circuit_t *circ;
1867 int streams_blocked;
1868 circ = conn->active_circuits;
1869 if (!circ) return 0;
1870 assert_active_circuits_ok_paranoid(conn);
1871 if (circ->n_conn == conn) {
1872 queue = &circ->n_conn_cells;
1873 streams_blocked = circ->streams_blocked_on_n_conn;
1874 } else {
1875 queue = &TO_OR_CIRCUIT(circ)->p_conn_cells;
1876 streams_blocked = circ->streams_blocked_on_p_conn;
1878 tor_assert(*next_circ_on_conn_p(circ,conn));
1880 for (n_flushed = 0; n_flushed < max && queue->head; ) {
1881 packed_cell_t *cell = cell_queue_pop(queue);
1882 tor_assert(*next_circ_on_conn_p(circ,conn));
1884 connection_write_to_buf(cell->body, CELL_NETWORK_SIZE, TO_CONN(conn));
1886 packed_cell_free(cell);
1887 ++n_flushed;
1888 if (circ != conn->active_circuits) {
1889 /* If this happens, the current circuit just got made inactive by
1890 * a call in connection_write_to_buf(). That's nothing to worry about:
1891 * circuit_make_inactive_on_conn() already advanced conn->active_circuits
1892 * for us.
1894 assert_active_circuits_ok_paranoid(conn);
1895 goto done;
1898 tor_assert(*next_circ_on_conn_p(circ,conn));
1899 assert_active_circuits_ok_paranoid(conn);
1900 conn->active_circuits = *next_circ_on_conn_p(circ, conn);
1902 /* Is the cell queue low enough to unblock all the streams that are waiting
1903 * to write to this circuit? */
1904 if (streams_blocked && queue->n <= CELL_QUEUE_LOWWATER_SIZE)
1905 set_streams_blocked_on_circ(circ, conn, 0); /* unblock streams */
1907 /* Did we just ran out of cells on this queue? */
1908 if (queue->n == 0) {
1909 log_debug(LD_GENERAL, "Made a circuit inactive.");
1910 make_circuit_inactive_on_conn(circ, conn);
1912 done:
1913 if (n_flushed)
1914 conn->timestamp_last_added_nonpadding = now;
1915 return n_flushed;
1918 /** Add <b>cell</b> to the queue of <b>circ</b> writing to <b>orconn</b>
1919 * transmitting in <b>direction</b>. */
1920 void
1921 append_cell_to_circuit_queue(circuit_t *circ, or_connection_t *orconn,
1922 cell_t *cell, int direction)
1924 cell_queue_t *queue;
1925 int streams_blocked;
1926 if (direction == CELL_DIRECTION_OUT) {
1927 queue = &circ->n_conn_cells;
1928 streams_blocked = circ->streams_blocked_on_n_conn;
1929 } else {
1930 or_circuit_t *orcirc = TO_OR_CIRCUIT(circ);
1931 queue = &orcirc->p_conn_cells;
1932 streams_blocked = circ->streams_blocked_on_p_conn;
1934 if (cell->command == CELL_RELAY_EARLY && orconn->link_proto < 2) {
1935 /* V1 connections don't understand RELAY_EARLY. */
1936 cell->command = CELL_RELAY;
1939 cell_queue_append_packed_copy(queue, cell);
1941 /* If we have too many cells on the circuit, we should stop reading from
1942 * the edge streams for a while. */
1943 if (!streams_blocked && queue->n >= CELL_QUEUE_HIGHWATER_SIZE)
1944 set_streams_blocked_on_circ(circ, orconn, 1); /* block streams */
1946 if (queue->n == 1) {
1947 /* This was the first cell added to the queue. We need to make this
1948 * circuit active. */
1949 log_debug(LD_GENERAL, "Made a circuit active.");
1950 make_circuit_active_on_conn(circ, orconn);
1953 if (! buf_datalen(orconn->_base.outbuf)) {
1954 /* There is no data at all waiting to be sent on the outbuf. Add a
1955 * cell, so that we can notice when it gets flushed, flushed_some can
1956 * get called, and we can start putting more data onto the buffer then.
1958 log_debug(LD_GENERAL, "Primed a buffer.");
1959 connection_or_flush_from_first_active_circuit(orconn, 1, time(NULL));
1963 /** Fail with an assert if the active circuits ring on <b>orconn</b> is
1964 * corrupt. */
1965 void
1966 assert_active_circuits_ok(or_connection_t *orconn)
1968 circuit_t *head = orconn->active_circuits;
1969 circuit_t *cur = head;
1970 if (! head)
1971 return;
1972 do {
1973 circuit_t *next = *next_circ_on_conn_p(cur, orconn);
1974 circuit_t *prev = *prev_circ_on_conn_p(cur, orconn);
1975 tor_assert(next);
1976 tor_assert(prev);
1977 tor_assert(*next_circ_on_conn_p(prev, orconn) == cur);
1978 tor_assert(*prev_circ_on_conn_p(next, orconn) == cur);
1979 cur = next;
1980 } while (cur != head);