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 */
7 const char relay_c_id
[] =
12 * \brief Handle relay cell encryption/decryption, plus packaging and
13 * receiving from circuits, plus queueing on circuits.
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
,
23 crypt_path_t
*layer_hint
);
26 connection_edge_process_relay_cell(cell_t
*cell
, circuit_t
*circ
,
27 edge_connection_t
*conn
,
28 crypt_path_t
*layer_hint
);
30 circuit_consider_sending_sendme(circuit_t
*circ
, crypt_path_t
*layer_hint
);
32 circuit_resume_edge_reading(circuit_t
*circ
, crypt_path_t
*layer_hint
);
34 circuit_resume_edge_reading_helper(edge_connection_t
*conn
,
36 crypt_path_t
*layer_hint
);
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
47 uint64_t stats_n_relay_cells_delivered
= 0;
49 /** Update digest from the payload of cell. Assign integrity part to
53 relay_set_digest(crypto_digest_env_t
*digest
, cell_t
*cell
)
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.
74 relay_digest_matches(crypto_digest_env_t
*digest
, cell_t
*cell
)
76 char received_integrity
[4], calculated_integrity
[4];
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
);
105 crypto_free_digest_env(backup_digest
);
109 /** Apply <b>cipher</b> to CELL_PAYLOAD_SIZE bytes of <b>in</b>
112 * If <b>encrypt_mode</b> is 1 then encrypt, else decrypt.
114 * Return -1 if the crypto fails, else return 0.
117 relay_crypt_one_payload(crypto_cipher_env_t
*cipher
, char *in
,
122 r
= crypto_cipher_crypt_inplace(cipher
, in
, CELL_PAYLOAD_SIZE
);
125 log_warn(LD_BUG
,"Error during relay encryption");
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
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
;
153 tor_assert(cell_direction
== CELL_DIRECTION_OUT
||
154 cell_direction
== CELL_DIRECTION_IN
);
155 if (circ
->marked_for_close
)
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
;
164 edge_connection_t
*conn
= relay_lookup_conn(circ
, cell
, cell_direction
,
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
))
171 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
172 "connection_edge_process_relay_cell (away from origin) "
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
,
183 "connection_edge_process_relay_cell (at origin) failed.");
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
;
198 log_fn(LOG_PROTOCOL_WARN
, LD_OR
,
199 "Dropping unrecognized inbound cell on origin circuit.");
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 "
216 /* XXXX Do this here, or just return -1? */
217 circuit_mark_for_close(circ
, -reason
);
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
233 append_cell_to_circuit_queue(circ
, or_conn
, cell
, cell_direction
);
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,
255 relay_crypt(circuit_t
*circ
, cell_t
*cell
, int cell_direction
,
256 crypt_path_t
**layer_hint
, char *recognized
)
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
;
271 if (thishop
->state
!= CPATH_STATE_OPEN
) {
272 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
273 "Relay cell before first created cell? Closing.");
276 do { /* Remember: cpath is in forward order, that is, first hop first. */
279 if (relay_crypt_one_payload(thishop
->b_crypto
, cell
->payload
, 0) < 0)
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
)) {
287 *layer_hint
= thishop
;
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.");
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)
301 // log_fn(LOG_DEBUG,"Skipping recognized check, because we're not "
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)
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
)) {
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>.
328 circuit_package_relay_cell(cell_t
*cell
, circuit_t
*circ
,
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 */
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 */
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) {
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)
371 ++stats_n_relay_cells_relayed
;
373 append_cell_to_circuit_queue(circ
, conn
, cell
, cell_direction
);
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
;
387 relay_header_unpack(&rh
, cell
->payload
);
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
);
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
))
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
);
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.
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>.
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
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
)
475 /* XXXX NM Split this function into a separate versions per circuit type? */
478 tor_assert(payload_len
<= RELAY_PAYLOAD_SIZE
);
480 memset(&cell
, 0, sizeof(cell_t
));
481 cell
.command
= CELL_RELAY
;
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
;
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
);
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
)
511 log_warn(LD_BUG
,"circuit_package_relay_cell failed. Closing.");
512 circuit_mark_for_close(circ
, END_CIRC_REASON_INTERNAL
);
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
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
,
532 /* XXXX NM Split this function into a separate versions per circuit type? */
534 tor_assert(fromconn
);
535 circ
= fromconn
->on_circuit
;
537 if (fromconn
->_base
.marked_for_close
) {
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
);
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
);
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
));
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.
568 connection_edge_end_reason_str(int reason
)
572 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
573 "End cell arrived with length 0. Should be at least 1.");
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";
589 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
590 "Reason for ending (%d) not recognized.",reason
);
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
) {
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
;
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'). */
655 #define E_CASE(s) case s: case WSA ## s
656 #define S_CASE(s) case WSA ## s
658 #define E_CASE(s) case s
659 #define S_CASE(s) case s
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
)
670 return END_STREAM_REASON_DONE
;
676 S_CASE(EPROTONOSUPPORT
):
677 S_CASE(EAFNOSUPPORT
):
681 return END_STREAM_REASON_INTERNAL
;
682 S_CASE(ECONNREFUSED
):
683 return END_STREAM_REASON_CONNECTREFUSED
;
685 return END_STREAM_REASON_CONNRESET
;
687 return END_STREAM_REASON_TIMEOUT
;
692 return END_STREAM_REASON_RESOURCELIMIT
;
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. */
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().
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
)
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
));
738 router_get_by_digest(circ
->build_state
->chosen_exit
->identity_digest
);
740 case END_STREAM_REASON_EXITPOLICY
:
741 if (rh
->length
>= 5) {
742 uint32_t addr
= ntohl(get_uint32(cell
->payload
+RELAY_HEADER_SIZE
+1));
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
);
751 ttl
= (int)ntohl(get_uint32(cell
->payload
+RELAY_HEADER_SIZE
+5));
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 */
760 (tor_inet_aton(conn
->socks_request
->address
, &in
) &&
761 !conn
->chosen_exit_name
))) {
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
),
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)
790 /* else, conn will get closed below */
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)
814 /* else, conn will get closed below */
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
);
825 case END_STREAM_REASON_HIBERNATING
:
826 case END_STREAM_REASON_RESOURCELIMIT
:
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)
837 /* else, will close below */
840 log_info(LD_APP
,"Giving up on retrying; conn can't be handled.");
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
);
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
));
860 /** Helper: change the socks_request->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. */
864 remap_event_helper(edge_connection_t
*conn
, uint32_t new_addr
)
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.
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
,
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.");
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
));
910 if (!addr
|| (get_options()->ClientDNSRejectInternalAddresses
&&
911 is_internal_IP(addr
, 0))) {
912 char buf
[INET_NTOA_BUF_LEN
];
914 a
.s_addr
= htonl(addr
);
915 tor_inet_ntoa(&a
, buf
, sizeof(buf
));
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
);
923 ttl
= (int)ntohl(get_uint32(cell
->payload
+RELAY_HEADER_SIZE
+4));
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);
948 case DIR_PURPOSE_FETCH_CONSENSUS
:
949 control_event_bootstrap(BOOTSTRAP_STATUS_LOADING_STATUS
, 0);
951 case DIR_PURPOSE_FETCH_SERVERDESC
:
952 control_event_bootstrap(BOOTSTRAP_STATUS_LOADING_DESCRIPTORS
,
953 count_loading_descriptors_progress());
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
));
966 if (conn
->_base
.type
== CONN_TYPE_AP
&&
967 rh
->command
== RELAY_COMMAND_RESOLVED
) {
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.");
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
);
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
+
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
];
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
);
1006 connection_ap_handshake_socks_resolved(conn
,
1008 cell
->payload
[RELAY_HEADER_SIZE
+1], /*answer_len*/
1009 cell
->payload
+RELAY_HEADER_SIZE
+2, /*answer*/
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
);
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);
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.
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;
1048 unsigned domain
= layer_hint
?LD_APP
:LD_EXIT
;
1054 relay_header_unpack(&rh
, cell
->payload
);
1055 // log_fn(LOG_DEBUG,"command %d stream %d", rh.command, rh.stream_id);
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.");
1076 case RELAY_COMMAND_BEGIN
:
1077 case RELAY_COMMAND_BEGIN_DIR
:
1079 circ
->purpose
!= CIRCUIT_PURPOSE_S_REND_JOINED
) {
1080 log_fn(LOG_PROTOCOL_WARN
, LD_APP
,
1081 "Relay begin request unsupported at AP. Dropping.");
1085 log_fn(LOG_PROTOCOL_WARN
, domain
,
1086 "Begin cell for known stream. Dropping.");
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
);
1106 log_info(domain
,"data cell dropped, unknown stream.");
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
);
1121 case RELAY_COMMAND_END
:
1122 reason
= rh
.length
> 0 ?
1123 *(uint8_t *)(cell
->payload
+RELAY_HEADER_SIZE
) : END_STREAM_REASON_MISC
;
1125 log_info(domain
,"end cell (%s) dropped, unknown stream.",
1126 connection_edge_end_reason_str(reason
));
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.",
1132 connection_edge_end_reason_str(reason
),
1134 if (conn
->socks_request
&& !conn
->socks_request
->has_finished
)
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;
1148 case RELAY_COMMAND_EXTEND
:
1150 log_fn(LOG_PROTOCOL_WARN
, domain
,
1151 "'extend' cell received for non-zero stream. Dropping.");
1154 return circuit_extend(cell
, circ
);
1155 case RELAY_COMMAND_EXTENDED
:
1157 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
1158 "'extended' unsupported at non-origin. Dropping.");
1161 log_debug(domain
,"Got an extended cell! Yay.");
1162 if ((reason
= circuit_finish_handshake(TO_ORIGIN_CIRCUIT(circ
),
1164 cell
->payload
+RELAY_HEADER_SIZE
)) < 0) {
1165 log_warn(domain
,"circuit_finish_handshake failed.");
1168 if ((reason
=circuit_send_next_onion_skin(TO_ORIGIN_CIRCUIT(circ
)))<0) {
1169 log_info(domain
,"circuit_send_next_onion_skin() failed.");
1173 case RELAY_COMMAND_TRUNCATE
:
1175 log_fn(LOG_PROTOCOL_WARN
, LD_APP
,
1176 "'truncate' unsupported at origin. Dropping.");
1180 uint8_t trunc_reason
= *(uint8_t*)(cell
->payload
+ RELAY_HEADER_SIZE
);
1181 connection_or_send_destroy(circ
->n_circ_id
, circ
->n_conn
,
1183 circuit_set_n_circid_orconn(circ
, 0, NULL
);
1185 log_debug(LD_EXIT
, "Processed 'truncate', replying.");
1188 payload
[0] = (char)END_CIRC_REASON_REQUESTED
;
1189 relay_send_command_from_edge(0, circ
, RELAY_COMMAND_TRUNCATED
,
1190 payload
, sizeof(payload
), NULL
);
1193 case RELAY_COMMAND_TRUNCATED
:
1195 log_fn(LOG_PROTOCOL_WARN
, LD_EXIT
,
1196 "'truncated' unsupported at non-origin. Dropping.");
1199 circuit_truncated(TO_ORIGIN_CIRCUIT(circ
), layer_hint
);
1201 case RELAY_COMMAND_CONNECTED
:
1203 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
1204 "'connected' unsupported while open. Closing circ.");
1205 return -END_CIRC_REASON_TORPROTOCOL
;
1208 "'connected' received, no conn attached anymore. Ignoring.");
1210 case RELAY_COMMAND_SENDME
:
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
);
1218 circ
->package_window
+= CIRCWINDOW_INCREMENT
;
1220 "circ-level sendme at non-origin, packagewindow %d.",
1221 circ
->package_window
);
1222 circuit_resume_edge_reading(circ
, layer_hint
);
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
));
1237 case RELAY_COMMAND_RESOLVE
:
1239 log_fn(LOG_PROTOCOL_WARN
, LD_APP
,
1240 "resolve request unsupported at AP; dropping.");
1243 log_fn(LOG_PROTOCOL_WARN
, domain
,
1244 "resolve request for known stream; dropping.");
1246 } else if (circ
->purpose
!= CIRCUIT_PURPOSE_OR
) {
1247 log_fn(LOG_PROTOCOL_WARN
, domain
,
1248 "resolve request on circ with purpose %d; dropping",
1252 connection_exit_begin_resolve(cell
, TO_OR_CIRCUIT(circ
));
1254 case RELAY_COMMAND_RESOLVED
:
1256 log_fn(LOG_PROTOCOL_WARN
, domain
,
1257 "'resolved' unsupported while open. Closing circ.");
1258 return -END_CIRC_REASON_TORPROTOCOL
;
1261 "'resolved' received, no conn attached anymore. Ignoring.");
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
);
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.",
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
];
1301 unsigned domain
= conn
->cpath_layer
? LD_APP
: LD_EXIT
;
1305 if (conn
->_base
.marked_for_close
) {
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
);
1312 repeat_connection_edge_package_raw_inbuf
:
1314 circ
= circuit_get_by_edge_conn(conn
);
1316 log_info(domain
,"conn has no circuit! Closing.");
1317 conn
->end_reason
= END_STREAM_REASON_CANT_ATTACH
;
1321 if (circuit_consider_stop_edge_reading(circ
, conn
->cpath_layer
))
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
));
1331 amount_to_process
= buf_datalen(conn
->_base
.inbuf
);
1333 if (!amount_to_process
)
1336 if (!package_partial
&& amount_to_process
< RELAY_PAYLOAD_SIZE
)
1339 if (amount_to_process
> RELAY_PAYLOAD_SIZE
) {
1340 length
= RELAY_PAYLOAD_SIZE
;
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 */
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.
1384 connection_edge_consider_sending_sendme(edge_connection_t
*conn
)
1388 if (connection_outbuf_too_full(TO_CONN(conn
)))
1391 circ
= circuit_get_by_edge_conn(conn
);
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.");
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
,
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.
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
,
1427 circuit_resume_edge_reading_helper(TO_OR_CIRCUIT(circ
)->n_streams
,
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.
1436 circuit_resume_edge_reading_helper(edge_connection_t
*conn
,
1438 crypt_path_t
*layer_hint
)
1440 for ( ; conn
; conn
=conn
->next_stream
) {
1441 if (conn
->_base
.marked_for_close
)
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
));
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
))
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.
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
;
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
));
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
));
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
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.");
1517 layer_hint
->deliver_window
+= CIRCWINDOW_INCREMENT
;
1519 circ
->deliver_window
+= CIRCWINDOW_INCREMENT
;
1520 if (relay_send_command_from_edge(0, circ
, RELAY_COMMAND_SENDME
,
1521 NULL
, 0, layer_hint
) < 0) {
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
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)
1540 #define assert_active_circuits_ok_paranoid(conn)
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
1549 static mp_pool_t
*cell_pool
= NULL
;
1550 /** Allocate structures to hold cells. */
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. */
1560 free_cell_pool(void)
1562 /* Maybe we haven't called init_cell_pool yet; need to check for it. */
1564 mp_pool_destroy(cell_pool
);
1569 /** Free excess storage in cell pool. */
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>. */
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
);
1593 dump_cell_pool_usage(int severity
)
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
;
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
);
1609 /* ENABLE_CELL_POOL isn't defined: here are some stubs to use tor_malloc()
1610 * and tor_free() instead. */
1612 init_cell_pool(void)
1617 free_cell_pool(void)
1622 clean_cell_pool(void)
1627 packed_cell_free(packed_cell_t
*cell
)
1629 --total_cells_allocated
;
1633 static INLINE packed_cell_t
*
1634 packed_cell_alloc(void)
1636 ++total_cells_allocated
;
1637 return tor_malloc(sizeof(packed_cell_t
));
1640 dump_cell_pool_usage(int severity
)
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();
1656 /** Append <b>cell</b> to the end of <b>queue</b>. */
1658 cell_queue_append(cell_queue_t
*queue
, packed_cell_t
*cell
)
1661 tor_assert(!queue
->tail
->next
);
1662 queue
->tail
->next
= cell
;
1671 /** Append a newly allocated copy of <b>cell</b> to the end of <b>queue</b> */
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>. */
1680 cell_queue_clear(cell_queue_t
*queue
)
1682 packed_cell_t
*cell
, *next
;
1686 packed_cell_free(cell
);
1689 queue
->head
= queue
->tail
= NULL
;
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
;
1701 queue
->head
= cell
->next
;
1702 if (cell
== queue
->tail
) {
1703 tor_assert(!queue
->head
);
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
)
1717 if (conn
== circ
->n_conn
) {
1718 return &circ
->next_active_on_n_conn
;
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
)
1733 if (conn
== circ
->n_conn
) {
1734 return &circ
->prev_active_on_n_conn
;
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. */
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. */
1755 if (! conn
->active_circuits
) {
1756 conn
->active_circuits
= circ
;
1757 *prevp
= *nextp
= circ
;
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
;
1763 *prev_circ_on_conn_p(head
, conn
) = circ
;
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. */
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. */
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
);
1788 conn
->active_circuits
= NULL
;
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
1802 connection_or_unlink_all_active_circs(or_connection_t
*orconn
)
1804 circuit_t
*head
= orconn
->active_circuits
;
1805 circuit_t
*cur
= head
;
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
;
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. */
1821 set_streams_blocked_on_circ(circuit_t
*circ
, or_connection_t
*orconn
,
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
;
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.*/
1846 if (connection_is_reading(conn
))
1847 connection_stop_reading(conn
);
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>->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
,
1865 cell_queue_t
*queue
;
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
;
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
);
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
1894 assert_active_circuits_ok_paranoid(conn
);
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
);
1914 conn
->timestamp_last_added_nonpadding
= now
;
1918 /** Add <b>cell</b> to the queue of <b>circ</b> writing to <b>orconn</b>
1919 * transmitting in <b>direction</b>. */
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
;
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
1966 assert_active_circuits_ok(or_connection_t
*orconn
)
1968 circuit_t
*head
= orconn
->active_circuits
;
1969 circuit_t
*cur
= head
;
1973 circuit_t
*next
= *next_circ_on_conn_p(cur
, orconn
);
1974 circuit_t
*prev
= *prev_circ_on_conn_p(cur
, orconn
);
1977 tor_assert(*next_circ_on_conn_p(prev
, orconn
) == cur
);
1978 tor_assert(*prev_circ_on_conn_p(next
, orconn
) == cur
);
1980 } while (cur
!= head
);