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-2012, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
9 * \brief General high-level functions to handle reading and writing
15 #include "circuitbuild.h"
16 #include "circuitlist.h"
17 #include "circuituse.h"
19 #include "connection.h"
20 #include "connection_edge.h"
21 #include "connection_or.h"
23 #include "cpuworker.h"
24 #include "directory.h"
33 #include "rendclient.h"
34 #include "rendcommon.h"
37 #include "routerparse.h"
39 #ifdef USE_BUFFEREVENTS
40 #include <event2/event.h>
47 static connection_t
*connection_listener_new(
48 const struct sockaddr
*listensockaddr
,
49 socklen_t listensocklen
, int type
,
51 const port_cfg_t
*portcfg
);
52 static void connection_init(time_t now
, connection_t
*conn
, int type
,
54 static int connection_init_accepted_conn(connection_t
*conn
,
55 const listener_connection_t
*listener
);
56 static int connection_handle_listener_read(connection_t
*conn
, int new_type
);
57 #ifndef USE_BUFFEREVENTS
58 static int connection_bucket_should_increase(int bucket
,
59 or_connection_t
*conn
);
61 static int connection_finished_flushing(connection_t
*conn
);
62 static int connection_flushed_some(connection_t
*conn
);
63 static int connection_finished_connecting(connection_t
*conn
);
64 static int connection_reached_eof(connection_t
*conn
);
65 static int connection_read_to_buf(connection_t
*conn
, ssize_t
*max_to_read
,
67 static int connection_process_inbuf(connection_t
*conn
, int package_partial
);
68 static void client_check_address_changed(tor_socket_t sock
);
69 static void set_constrained_socket_buffers(tor_socket_t sock
, int size
);
71 static const char *connection_proxy_state_to_string(int state
);
72 static int connection_read_https_proxy_response(connection_t
*conn
);
73 static void connection_send_socks5_connect(connection_t
*conn
);
74 static const char *proxy_type_to_string(int proxy_type
);
75 static int get_proxy_type(void);
77 /** The last addresses that our network interface seemed to have been
78 * binding to. We use this as one way to detect when our IP changes.
80 * XXX024 We should really use the entire list of interfaces here.
82 static tor_addr_t
*last_interface_ipv4
= NULL
;
83 /* DOCDOC last_interface_ipv6 */
84 static tor_addr_t
*last_interface_ipv6
= NULL
;
85 /** A list of tor_addr_t for addresses we've used in outgoing connections.
86 * Used to detect IP address changes. */
87 static smartlist_t
*outgoing_addrs
= NULL
;
89 #define CASE_ANY_LISTENER_TYPE \
90 case CONN_TYPE_OR_LISTENER: \
91 case CONN_TYPE_AP_LISTENER: \
92 case CONN_TYPE_DIR_LISTENER: \
93 case CONN_TYPE_CONTROL_LISTENER: \
94 case CONN_TYPE_AP_TRANS_LISTENER: \
95 case CONN_TYPE_AP_NATD_LISTENER: \
96 case CONN_TYPE_AP_DNS_LISTENER
98 /**************************************************************/
101 * Return the human-readable name for the connection type <b>type</b>
104 conn_type_to_string(int type
)
108 case CONN_TYPE_OR_LISTENER
: return "OR listener";
109 case CONN_TYPE_OR
: return "OR";
110 case CONN_TYPE_EXIT
: return "Exit";
111 case CONN_TYPE_AP_LISTENER
: return "Socks listener";
112 case CONN_TYPE_AP_TRANS_LISTENER
:
113 return "Transparent pf/netfilter listener";
114 case CONN_TYPE_AP_NATD_LISTENER
: return "Transparent natd listener";
115 case CONN_TYPE_AP_DNS_LISTENER
: return "DNS listener";
116 case CONN_TYPE_AP
: return "Socks";
117 case CONN_TYPE_DIR_LISTENER
: return "Directory listener";
118 case CONN_TYPE_DIR
: return "Directory";
119 case CONN_TYPE_CPUWORKER
: return "CPU worker";
120 case CONN_TYPE_CONTROL_LISTENER
: return "Control listener";
121 case CONN_TYPE_CONTROL
: return "Control";
123 log_warn(LD_BUG
, "unknown connection type %d", type
);
124 tor_snprintf(buf
, sizeof(buf
), "unknown [%d]", type
);
130 * Return the human-readable name for the connection state <b>state</b>
131 * for the connection type <b>type</b>
134 conn_state_to_string(int type
, int state
)
138 CASE_ANY_LISTENER_TYPE
:
139 if (state
== LISTENER_STATE_READY
)
144 case OR_CONN_STATE_CONNECTING
: return "connect()ing";
145 case OR_CONN_STATE_PROXY_HANDSHAKING
: return "handshaking (proxy)";
146 case OR_CONN_STATE_TLS_HANDSHAKING
: return "handshaking (TLS)";
147 case OR_CONN_STATE_TLS_CLIENT_RENEGOTIATING
:
148 return "renegotiating (TLS, v2 handshake)";
149 case OR_CONN_STATE_TLS_SERVER_RENEGOTIATING
:
150 return "waiting for renegotiation or V3 handshake";
151 case OR_CONN_STATE_OR_HANDSHAKING_V2
:
152 return "handshaking (Tor, v2 handshake)";
153 case OR_CONN_STATE_OR_HANDSHAKING_V3
:
154 return "handshaking (Tor, v3 handshake)";
155 case OR_CONN_STATE_OPEN
: return "open";
160 case EXIT_CONN_STATE_RESOLVING
: return "waiting for dest info";
161 case EXIT_CONN_STATE_CONNECTING
: return "connecting";
162 case EXIT_CONN_STATE_OPEN
: return "open";
163 case EXIT_CONN_STATE_RESOLVEFAILED
: return "resolve failed";
168 case AP_CONN_STATE_SOCKS_WAIT
: return "waiting for socks info";
169 case AP_CONN_STATE_NATD_WAIT
: return "waiting for natd dest info";
170 case AP_CONN_STATE_RENDDESC_WAIT
: return "waiting for rendezvous desc";
171 case AP_CONN_STATE_CONTROLLER_WAIT
: return "waiting for controller";
172 case AP_CONN_STATE_CIRCUIT_WAIT
: return "waiting for circuit";
173 case AP_CONN_STATE_CONNECT_WAIT
: return "waiting for connect response";
174 case AP_CONN_STATE_RESOLVE_WAIT
: return "waiting for resolve response";
175 case AP_CONN_STATE_OPEN
: return "open";
180 case DIR_CONN_STATE_CONNECTING
: return "connecting";
181 case DIR_CONN_STATE_CLIENT_SENDING
: return "client sending";
182 case DIR_CONN_STATE_CLIENT_READING
: return "client reading";
183 case DIR_CONN_STATE_CLIENT_FINISHED
: return "client finished";
184 case DIR_CONN_STATE_SERVER_COMMAND_WAIT
: return "waiting for command";
185 case DIR_CONN_STATE_SERVER_WRITING
: return "writing";
188 case CONN_TYPE_CPUWORKER
:
190 case CPUWORKER_STATE_IDLE
: return "idle";
191 case CPUWORKER_STATE_BUSY_ONION
: return "busy with onion";
194 case CONN_TYPE_CONTROL
:
196 case CONTROL_CONN_STATE_OPEN
: return "open (protocol v1)";
197 case CONTROL_CONN_STATE_NEEDAUTH
:
198 return "waiting for authentication (protocol v1)";
203 log_warn(LD_BUG
, "unknown connection state %d (type %d)", state
, type
);
204 tor_snprintf(buf
, sizeof(buf
),
205 "unknown state [%d] on unknown [%s] connection",
206 state
, conn_type_to_string(type
));
210 #ifdef USE_BUFFEREVENTS
211 /** Return true iff the connection's type is one that can use a
212 bufferevent-based implementation. */
214 connection_type_uses_bufferevent(connection_t
*conn
)
216 switch (conn
->type
) {
220 case CONN_TYPE_CONTROL
:
222 case CONN_TYPE_CPUWORKER
:
230 /** Allocate and return a new dir_connection_t, initialized as by
231 * connection_init(). */
233 dir_connection_new(int socket_family
)
235 dir_connection_t
*dir_conn
= tor_malloc_zero(sizeof(dir_connection_t
));
236 connection_init(time(NULL
), TO_CONN(dir_conn
), CONN_TYPE_DIR
, socket_family
);
240 /** Allocate and return a new or_connection_t, initialized as by
241 * connection_init(). */
243 or_connection_new(int socket_family
)
245 or_connection_t
*or_conn
= tor_malloc_zero(sizeof(or_connection_t
));
246 time_t now
= time(NULL
);
247 connection_init(now
, TO_CONN(or_conn
), CONN_TYPE_OR
, socket_family
);
249 or_conn
->timestamp_last_added_nonpadding
= time(NULL
);
250 or_conn
->next_circ_id
= crypto_rand_int(1<<15);
252 or_conn
->active_circuit_pqueue
= smartlist_new();
253 or_conn
->active_circuit_pqueue_last_recalibrated
= cell_ewma_get_tick();
258 /** Allocate and return a new entry_connection_t, initialized as by
259 * connection_init(). */
261 entry_connection_new(int type
, int socket_family
)
263 entry_connection_t
*entry_conn
= tor_malloc_zero(sizeof(entry_connection_t
));
264 tor_assert(type
== CONN_TYPE_AP
);
265 connection_init(time(NULL
), ENTRY_TO_CONN(entry_conn
), type
, socket_family
);
266 entry_conn
->socks_request
= socks_request_new();
270 /** Allocate and return a new edge_connection_t, initialized as by
271 * connection_init(). */
273 edge_connection_new(int type
, int socket_family
)
275 edge_connection_t
*edge_conn
= tor_malloc_zero(sizeof(edge_connection_t
));
276 tor_assert(type
== CONN_TYPE_EXIT
);
277 connection_init(time(NULL
), TO_CONN(edge_conn
), type
, socket_family
);
281 /** Allocate and return a new control_connection_t, initialized as by
282 * connection_init(). */
283 control_connection_t
*
284 control_connection_new(int socket_family
)
286 control_connection_t
*control_conn
=
287 tor_malloc_zero(sizeof(control_connection_t
));
288 connection_init(time(NULL
),
289 TO_CONN(control_conn
), CONN_TYPE_CONTROL
, socket_family
);
290 log_notice(LD_CONTROL
, "New control connection opened.");
294 /** Allocate and return a new listener_connection_t, initialized as by
295 * connection_init(). */
296 listener_connection_t
*
297 listener_connection_new(int type
, int socket_family
)
299 listener_connection_t
*listener_conn
=
300 tor_malloc_zero(sizeof(listener_connection_t
));
301 connection_init(time(NULL
), TO_CONN(listener_conn
), type
, socket_family
);
302 return listener_conn
;
305 /** Allocate, initialize, and return a new connection_t subtype of <b>type</b>
306 * to make or receive connections of address family <b>socket_family</b>. The
307 * type should be one of the CONN_TYPE_* constants. */
309 connection_new(int type
, int socket_family
)
313 return TO_CONN(or_connection_new(socket_family
));
316 return TO_CONN(edge_connection_new(type
, socket_family
));
319 return ENTRY_TO_CONN(entry_connection_new(type
, socket_family
));
322 return TO_CONN(dir_connection_new(socket_family
));
324 case CONN_TYPE_CONTROL
:
325 return TO_CONN(control_connection_new(socket_family
));
327 CASE_ANY_LISTENER_TYPE
:
328 return TO_CONN(listener_connection_new(type
, socket_family
));
331 connection_t
*conn
= tor_malloc_zero(sizeof(connection_t
));
332 connection_init(time(NULL
), conn
, type
, socket_family
);
338 /** Initializes conn. (you must call connection_add() to link it into the main
341 * Set conn-\>type to <b>type</b>. Set conn-\>s and conn-\>conn_array_index to
342 * -1 to signify they are not yet assigned.
344 * If conn is not a listener type, allocate buffers for it. If it's
345 * an AP type, allocate space to store the socks_request.
347 * Assign a pseudorandom next_circ_id between 0 and 2**15.
349 * Initialize conn's timestamps to now.
352 connection_init(time_t now
, connection_t
*conn
, int type
, int socket_family
)
354 static uint64_t n_connections_allocated
= 1;
358 conn
->magic
= OR_CONNECTION_MAGIC
;
361 conn
->magic
= EDGE_CONNECTION_MAGIC
;
364 conn
->magic
= ENTRY_CONNECTION_MAGIC
;
367 conn
->magic
= DIR_CONNECTION_MAGIC
;
369 case CONN_TYPE_CONTROL
:
370 conn
->magic
= CONTROL_CONNECTION_MAGIC
;
372 CASE_ANY_LISTENER_TYPE
:
373 conn
->magic
= LISTENER_CONNECTION_MAGIC
;
376 conn
->magic
= BASE_CONNECTION_MAGIC
;
380 conn
->s
= TOR_INVALID_SOCKET
; /* give it a default of 'not used' */
381 conn
->conn_array_index
= -1; /* also default to 'not used' */
382 conn
->global_identifier
= n_connections_allocated
++;
385 conn
->socket_family
= socket_family
;
386 #ifndef USE_BUFFEREVENTS
387 if (!connection_is_listener(conn
)) {
388 /* listeners never use their buf */
389 conn
->inbuf
= buf_new();
390 conn
->outbuf
= buf_new();
394 conn
->timestamp_created
= now
;
395 conn
->timestamp_lastread
= now
;
396 conn
->timestamp_lastwritten
= now
;
399 /** Create a link between <b>conn_a</b> and <b>conn_b</b>. */
401 connection_link_connections(connection_t
*conn_a
, connection_t
*conn_b
)
403 tor_assert(! SOCKET_OK(conn_a
->s
));
404 tor_assert(! SOCKET_OK(conn_b
->s
));
408 conn_a
->linked_conn
= conn_b
;
409 conn_b
->linked_conn
= conn_a
;
412 /** Deallocate memory used by <b>conn</b>. Deallocate its buffers if
413 * necessary, close its socket if necessary, and mark the directory as dirty
414 * if <b>conn</b> is an OR or OP connection.
417 _connection_free(connection_t
*conn
)
424 switch (conn
->type
) {
426 tor_assert(conn
->magic
== OR_CONNECTION_MAGIC
);
427 mem
= TO_OR_CONN(conn
);
428 memlen
= sizeof(or_connection_t
);
431 tor_assert(conn
->magic
== ENTRY_CONNECTION_MAGIC
);
432 mem
= TO_ENTRY_CONN(conn
);
433 memlen
= sizeof(entry_connection_t
);
436 tor_assert(conn
->magic
== EDGE_CONNECTION_MAGIC
);
437 mem
= TO_EDGE_CONN(conn
);
438 memlen
= sizeof(edge_connection_t
);
441 tor_assert(conn
->magic
== DIR_CONNECTION_MAGIC
);
442 mem
= TO_DIR_CONN(conn
);
443 memlen
= sizeof(dir_connection_t
);
445 case CONN_TYPE_CONTROL
:
446 tor_assert(conn
->magic
== CONTROL_CONNECTION_MAGIC
);
447 mem
= TO_CONTROL_CONN(conn
);
448 memlen
= sizeof(control_connection_t
);
450 CASE_ANY_LISTENER_TYPE
:
451 tor_assert(conn
->magic
== LISTENER_CONNECTION_MAGIC
);
452 mem
= TO_LISTENER_CONN(conn
);
453 memlen
= sizeof(listener_connection_t
);
456 tor_assert(conn
->magic
== BASE_CONNECTION_MAGIC
);
458 memlen
= sizeof(connection_t
);
463 log_info(LD_GENERAL
, "Freeing linked %s connection [%s] with %d "
464 "bytes on inbuf, %d on outbuf.",
465 conn_type_to_string(conn
->type
),
466 conn_state_to_string(conn
->type
, conn
->state
),
467 (int)connection_get_inbuf_len(conn
),
468 (int)connection_get_outbuf_len(conn
));
471 if (!connection_is_listener(conn
)) {
472 buf_free(conn
->inbuf
);
473 buf_free(conn
->outbuf
);
475 if (conn
->socket_family
== AF_UNIX
) {
476 /* For now only control ports can be Unix domain sockets
477 * and listeners at the same time */
478 tor_assert(conn
->type
== CONN_TYPE_CONTROL_LISTENER
);
480 if (unlink(conn
->address
) < 0 && errno
!= ENOENT
) {
481 log_warn(LD_NET
, "Could not unlink %s: %s", conn
->address
,
487 tor_free(conn
->address
);
489 if (connection_speaks_cells(conn
)) {
490 or_connection_t
*or_conn
= TO_OR_CONN(conn
);
491 tor_tls_free(or_conn
->tls
);
493 or_handshake_state_free(or_conn
->handshake_state
);
494 or_conn
->handshake_state
= NULL
;
495 smartlist_free(or_conn
->active_circuit_pqueue
);
496 tor_free(or_conn
->nickname
);
498 if (conn
->type
== CONN_TYPE_AP
) {
499 entry_connection_t
*entry_conn
= TO_ENTRY_CONN(conn
);
500 tor_free(entry_conn
->chosen_exit_name
);
501 tor_free(entry_conn
->original_dest_address
);
502 if (entry_conn
->socks_request
)
503 socks_request_free(entry_conn
->socks_request
);
504 if (entry_conn
->pending_optimistic_data
) {
505 generic_buffer_free(entry_conn
->pending_optimistic_data
);
507 if (entry_conn
->sending_optimistic_data
) {
508 generic_buffer_free(entry_conn
->sending_optimistic_data
);
511 if (CONN_IS_EDGE(conn
)) {
512 rend_data_free(TO_EDGE_CONN(conn
)->rend_data
);
514 if (conn
->type
== CONN_TYPE_CONTROL
) {
515 control_connection_t
*control_conn
= TO_CONTROL_CONN(conn
);
516 tor_free(control_conn
->safecookie_client_hash
);
517 tor_free(control_conn
->incoming_cmd
);
520 tor_free(conn
->read_event
); /* Probably already freed by connection_free. */
521 tor_free(conn
->write_event
); /* Probably already freed by connection_free. */
522 IF_HAS_BUFFEREVENT(conn
, {
523 /* This was a workaround to handle bugs in some old versions of libevent
524 * where callbacks can occur after calling bufferevent_free(). Setting
525 * the callbacks to NULL prevented this. It shouldn't be necessary any
526 * more, but let's not tempt fate for now. */
527 bufferevent_setcb(conn
->bufev
, NULL
, NULL
, NULL
, NULL
);
528 bufferevent_free(conn
->bufev
);
532 if (conn
->type
== CONN_TYPE_DIR
) {
533 dir_connection_t
*dir_conn
= TO_DIR_CONN(conn
);
534 tor_free(dir_conn
->requested_resource
);
536 tor_zlib_free(dir_conn
->zlib_state
);
537 if (dir_conn
->fingerprint_stack
) {
538 SMARTLIST_FOREACH(dir_conn
->fingerprint_stack
, char *, cp
, tor_free(cp
));
539 smartlist_free(dir_conn
->fingerprint_stack
);
542 cached_dir_decref(dir_conn
->cached_dir
);
543 rend_data_free(dir_conn
->rend_data
);
546 if (SOCKET_OK(conn
->s
)) {
547 log_debug(LD_NET
,"closing fd %d.",(int)conn
->s
);
548 tor_close_socket(conn
->s
);
549 conn
->s
= TOR_INVALID_SOCKET
;
552 if (conn
->type
== CONN_TYPE_OR
&&
553 !tor_digest_is_zero(TO_OR_CONN(conn
)->identity_digest
)) {
554 log_warn(LD_BUG
, "called on OR conn with non-zeroed identity_digest");
555 connection_or_remove_from_identity_map(TO_OR_CONN(conn
));
557 #ifdef USE_BUFFEREVENTS
558 if (conn
->type
== CONN_TYPE_OR
&& TO_OR_CONN(conn
)->bucket_cfg
) {
559 ev_token_bucket_cfg_free(TO_OR_CONN(conn
)->bucket_cfg
);
560 TO_OR_CONN(conn
)->bucket_cfg
= NULL
;
564 memwipe(mem
, 0xCC, memlen
); /* poison memory */
568 /** Make sure <b>conn</b> isn't in any of the global conn lists; then free it.
571 connection_free(connection_t
*conn
)
575 tor_assert(!connection_is_on_closeable_list(conn
));
576 tor_assert(!connection_in_array(conn
));
577 if (conn
->linked_conn
) {
578 log_err(LD_BUG
, "Called with conn->linked_conn still set.");
579 tor_fragile_assert();
580 conn
->linked_conn
->linked_conn
= NULL
;
581 if (! conn
->linked_conn
->marked_for_close
&&
582 conn
->linked_conn
->reading_from_linked_conn
)
583 connection_start_reading(conn
->linked_conn
);
584 conn
->linked_conn
= NULL
;
586 if (connection_speaks_cells(conn
)) {
587 if (!tor_digest_is_zero(TO_OR_CONN(conn
)->identity_digest
)) {
588 connection_or_remove_from_identity_map(TO_OR_CONN(conn
));
591 if (conn
->type
== CONN_TYPE_CONTROL
) {
592 connection_control_closed(TO_CONTROL_CONN(conn
));
594 connection_unregister_events(conn
);
595 _connection_free(conn
);
599 * Called when we're about to finally unlink and free a connection:
600 * perform necessary accounting and cleanup
601 * - Directory conns that failed to fetch a rendezvous descriptor
602 * need to inform pending rendezvous streams.
603 * - OR conns need to call rep_hist_note_*() to record status.
604 * - AP conns need to send a socks reject if necessary.
605 * - Exit conns need to call connection_dns_remove() if necessary.
606 * - AP and Exit conns need to send an end cell if they can.
607 * - DNS conns need to fail any resolves that are pending on them.
608 * - OR and edge connections need to be unlinked from circuits.
611 connection_about_to_close_connection(connection_t
*conn
)
613 tor_assert(conn
->marked_for_close
);
615 switch (conn
->type
) {
617 connection_dir_about_to_close(TO_DIR_CONN(conn
));
620 connection_or_about_to_close(TO_OR_CONN(conn
));
623 connection_ap_about_to_close(TO_ENTRY_CONN(conn
));
626 connection_exit_about_to_close(TO_EDGE_CONN(conn
));
631 /** Return true iff connection_close_immediate() has been called on this
633 #define CONN_IS_CLOSED(c) \
634 ((c)->linked ? ((c)->linked_conn_is_closed) : (! SOCKET_OK(c->s)))
636 /** Close the underlying socket for <b>conn</b>, so we don't try to
637 * flush it. Must be used in conjunction with (right before)
638 * connection_mark_for_close().
641 connection_close_immediate(connection_t
*conn
)
643 assert_connection_ok(conn
,0);
644 if (CONN_IS_CLOSED(conn
)) {
645 log_err(LD_BUG
,"Attempt to close already-closed connection.");
646 tor_fragile_assert();
649 if (conn
->outbuf_flushlen
) {
650 log_info(LD_NET
,"fd %d, type %s, state %s, %d bytes on outbuf.",
651 (int)conn
->s
, conn_type_to_string(conn
->type
),
652 conn_state_to_string(conn
->type
, conn
->state
),
653 (int)conn
->outbuf_flushlen
);
656 connection_unregister_events(conn
);
658 if (SOCKET_OK(conn
->s
))
659 tor_close_socket(conn
->s
);
660 conn
->s
= TOR_INVALID_SOCKET
;
662 conn
->linked_conn_is_closed
= 1;
664 buf_clear(conn
->outbuf
);
665 conn
->outbuf_flushlen
= 0;
668 /** Mark <b>conn</b> to be closed next time we loop through
669 * conn_close_if_marked() in main.c. */
671 _connection_mark_for_close(connection_t
*conn
, int line
, const char *file
)
673 assert_connection_ok(conn
,0);
675 tor_assert(line
< 1<<16); /* marked_for_close can only fit a uint16_t. */
678 if (conn
->marked_for_close
) {
679 log(LOG_WARN
,LD_BUG
,"Duplicate call to connection_mark_for_close at %s:%d"
680 " (first at %s:%d)", file
, line
, conn
->marked_for_close_file
,
681 conn
->marked_for_close
);
682 tor_fragile_assert();
686 conn
->marked_for_close
= line
;
687 conn
->marked_for_close_file
= file
;
688 add_connection_to_closeable_list(conn
);
690 /* in case we're going to be held-open-til-flushed, reset
691 * the number of seconds since last successful write, so
692 * we get our whole 15 seconds */
693 conn
->timestamp_lastwritten
= time(NULL
);
696 /** Find each connection that has hold_open_until_flushed set to
697 * 1 but hasn't written in the past 15 seconds, and set
698 * hold_open_until_flushed to 0. This means it will get cleaned
699 * up in the next loop through close_if_marked() in main.c.
702 connection_expire_held_open(void)
705 smartlist_t
*conns
= get_connection_array();
709 SMARTLIST_FOREACH_BEGIN(conns
, connection_t
*, conn
) {
710 /* If we've been holding the connection open, but we haven't written
713 if (conn
->hold_open_until_flushed
) {
714 tor_assert(conn
->marked_for_close
);
715 if (now
- conn
->timestamp_lastwritten
>= 15) {
717 if (conn
->type
== CONN_TYPE_EXIT
||
718 (conn
->type
== CONN_TYPE_DIR
&&
719 conn
->purpose
== DIR_PURPOSE_SERVER
))
722 severity
= LOG_NOTICE
;
723 log_fn(severity
, LD_NET
,
724 "Giving up on marked_for_close conn that's been flushing "
725 "for 15s (fd %d, type %s, state %s).",
726 (int)conn
->s
, conn_type_to_string(conn
->type
),
727 conn_state_to_string(conn
->type
, conn
->state
));
728 conn
->hold_open_until_flushed
= 0;
731 } SMARTLIST_FOREACH_END(conn
);
734 #if defined(HAVE_SYS_UN_H) || defined(RUNNING_DOXYGEN)
735 /** Create an AF_UNIX listenaddr struct.
736 * <b>listenaddress</b> provides the path to the Unix socket.
738 * Eventually <b>listenaddress</b> will also optionally contain user, group,
739 * and file permissions for the new socket. But not yet. XXX
740 * Also, since we do not create the socket here the information doesn't help
743 * If not NULL <b>readable_address</b> will contain a copy of the path part of
744 * <b>listenaddress</b>.
746 * The listenaddr struct has to be freed by the caller.
748 static struct sockaddr_un
*
749 create_unix_sockaddr(const char *listenaddress
, char **readable_address
,
752 struct sockaddr_un
*sockaddr
= NULL
;
754 sockaddr
= tor_malloc_zero(sizeof(struct sockaddr_un
));
755 sockaddr
->sun_family
= AF_UNIX
;
756 if (strlcpy(sockaddr
->sun_path
, listenaddress
, sizeof(sockaddr
->sun_path
))
757 >= sizeof(sockaddr
->sun_path
)) {
758 log_warn(LD_CONFIG
, "Unix socket path '%s' is too long to fit.",
759 escaped(listenaddress
));
764 if (readable_address
)
765 *readable_address
= tor_strdup(listenaddress
);
767 *len_out
= sizeof(struct sockaddr_un
);
771 static struct sockaddr
*
772 create_unix_sockaddr(const char *listenaddress
, char **readable_address
,
776 (void)readable_address
;
777 log_fn(LOG_ERR
, LD_BUG
,
778 "Unix domain sockets not supported, yet we tried to create one.");
780 tor_fragile_assert();
783 #endif /* HAVE_SYS_UN_H */
785 /** Warn that an accept or a connect has failed because we're running up
786 * against our ulimit. Rate-limit these warnings so that we don't spam
789 warn_too_many_conns(void)
791 #define WARN_TOO_MANY_CONNS_INTERVAL (6*60*60)
792 static ratelim_t last_warned
= RATELIM_INIT(WARN_TOO_MANY_CONNS_INTERVAL
);
794 if ((m
= rate_limit_log(&last_warned
, approx_time()))) {
795 int n_conns
= get_n_open_sockets();
796 log_warn(LD_NET
,"Failing because we have %d connections already. Please "
797 "raise your ulimit -n.%s", n_conns
, m
);
799 control_event_general_status(LOG_WARN
, "TOO_MANY_CONNECTIONS CURRENT=%d",
805 /** Check whether we should be willing to open an AF_UNIX socket in
806 * <b>path</b>. Return 0 if we should go ahead and -1 if we shouldn't. */
808 check_location_for_unix_socket(const or_options_t
*options
, const char *path
)
811 char *p
= tor_strdup(path
);
812 cpd_check_t flags
= CPD_CHECK_MODE_ONLY
;
813 if (get_parent_directory(p
)<0)
816 if (options
->ControlSocketsGroupWritable
)
817 flags
|= CPD_GROUP_OK
;
819 if (check_private_dir(p
, flags
, options
->User
) < 0) {
820 char *escpath
, *escdir
;
821 escpath
= esc_for_log(path
);
822 escdir
= esc_for_log(p
);
823 log_warn(LD_GENERAL
, "Before Tor can create a control socket in %s, the "
824 "directory %s needs to exist, and to be accessible only by the "
825 "user%s account that is running Tor. (On some Unix systems, "
826 "anybody who can list a socket can connect to it, so Tor is "
827 "being careful.)", escpath
, escdir
,
828 options
->ControlSocketsGroupWritable
? " and group" : "");
841 /** Tell the TCP stack that it shouldn't wait for a long time after
842 * <b>sock</b> has closed before reusing its port. */
844 make_socket_reuseable(tor_socket_t sock
)
851 /* REUSEADDR on normal places means you can rebind to the port
852 * right after somebody else has let it go. But REUSEADDR on win32
853 * means you can bind to the port _even when somebody else
854 * already has it bound_. So, don't do that on Win32. */
855 setsockopt(sock
, SOL_SOCKET
, SO_REUSEADDR
, (void*) &one
,
856 (socklen_t
)sizeof(one
));
860 /** Bind a new non-blocking socket listening to the socket described
861 * by <b>listensockaddr</b>.
863 * <b>address</b> is only used for logging purposes and to add the information
866 static connection_t
*
867 connection_listener_new(const struct sockaddr
*listensockaddr
,
869 int type
, const char *address
,
870 const port_cfg_t
*port_cfg
)
872 listener_connection_t
*lis_conn
;
874 tor_socket_t s
; /* the socket we're going to make */
875 or_options_t
const *options
= get_options();
876 #if defined(HAVE_PWD_H) && defined(HAVE_SYS_UN_H)
877 struct passwd
*pw
= NULL
;
879 uint16_t usePort
= 0, gotPort
= 0;
880 int start_reading
= 0;
881 static int global_next_session_group
= SESSION_GROUP_FIRST_AUTO
;
884 if (get_n_open_sockets() >= get_options()->_ConnLimit
-1) {
885 warn_too_many_conns();
889 if (listensockaddr
->sa_family
== AF_INET
||
890 listensockaddr
->sa_family
== AF_INET6
) {
891 int is_tcp
= (type
!= CONN_TYPE_AP_DNS_LISTENER
);
895 tor_addr_from_sockaddr(&addr
, listensockaddr
, &usePort
);
897 log_notice(LD_NET
, "Opening %s on %s:%d",
898 conn_type_to_string(type
), fmt_addr(&addr
), usePort
);
900 s
= tor_open_socket(tor_addr_family(&addr
),
901 is_tcp
? SOCK_STREAM
: SOCK_DGRAM
,
902 is_tcp
? IPPROTO_TCP
: IPPROTO_UDP
);
904 log_warn(LD_NET
,"Socket creation failed: %s",
905 tor_socket_strerror(tor_socket_errno(-1)));
909 make_socket_reuseable(s
);
912 if (listensockaddr
->sa_family
== AF_INET6
) {
914 /* In Redmond, this kind of thing passes for standards-conformance. */
919 /* We need to set IPV6_V6ONLY so that this socket can't get used for
920 * IPv4 connections. */
921 if (setsockopt(s
,IPPROTO_IPV6
, IPV6_V6ONLY
,
922 (void*)&one
, sizeof(one
))<0) {
923 int e
= tor_socket_errno(s
);
924 log_warn(LD_NET
, "Error setting IPV6_V6ONLY flag: %s",
925 tor_socket_strerror(e
));
926 /* Keep going; probably not harmful. */
931 if (bind(s
,listensockaddr
,socklen
) < 0) {
932 const char *helpfulhint
= "";
933 int e
= tor_socket_errno(s
);
934 if (ERRNO_IS_EADDRINUSE(e
))
935 helpfulhint
= ". Is Tor already running?";
936 log_warn(LD_NET
, "Could not bind to %s:%u: %s%s", address
, usePort
,
937 tor_socket_strerror(e
), helpfulhint
);
943 if (listen(s
,SOMAXCONN
) < 0) {
944 log_warn(LD_NET
, "Could not listen on %s:%u: %s", address
, usePort
,
945 tor_socket_strerror(tor_socket_errno(s
)));
955 struct sockaddr_storage ss
;
956 socklen_t ss_len
=sizeof(ss
);
957 if (getsockname(s
, (struct sockaddr
*)&ss
, &ss_len
)<0) {
958 log_warn(LD_NET
, "getsockname() couldn't learn address for %s: %s",
959 conn_type_to_string(type
),
960 tor_socket_strerror(tor_socket_errno(s
)));
963 tor_addr_from_sockaddr(&addr2
, (struct sockaddr
*)&ss
, &gotPort
);
966 } else if (listensockaddr
->sa_family
== AF_UNIX
) {
969 /* For now only control ports can be Unix domain sockets
970 * and listeners at the same time */
971 tor_assert(type
== CONN_TYPE_CONTROL_LISTENER
);
973 if (check_location_for_unix_socket(options
, address
) < 0)
976 log_notice(LD_NET
, "Opening %s on %s",
977 conn_type_to_string(type
), address
);
979 tor_addr_make_unspec(&addr
);
981 if (unlink(address
) < 0 && errno
!= ENOENT
) {
982 log_warn(LD_NET
, "Could not unlink %s: %s", address
,
986 s
= tor_open_socket(AF_UNIX
, SOCK_STREAM
, 0);
987 if (! SOCKET_OK(s
)) {
988 log_warn(LD_NET
,"Socket creation failed: %s.", strerror(errno
));
992 if (bind(s
, listensockaddr
, (socklen_t
)sizeof(struct sockaddr_un
)) == -1) {
993 log_warn(LD_NET
,"Bind to %s failed: %s.", address
,
994 tor_socket_strerror(tor_socket_errno(s
)));
999 pw
= getpwnam(options
->User
);
1001 log_warn(LD_NET
,"Unable to chown() %s socket: user %s not found.",
1002 address
, options
->User
);
1003 } else if (chown(address
, pw
->pw_uid
, pw
->pw_gid
) < 0) {
1004 log_warn(LD_NET
,"Unable to chown() %s socket: %s.",
1005 address
, strerror(errno
));
1010 if (options
->ControlSocketsGroupWritable
) {
1011 /* We need to use chmod; fchmod doesn't work on sockets on all
1013 if (chmod(address
, 0660) < 0) {
1014 log_warn(LD_FS
,"Unable to make %s group-writable.", address
);
1015 tor_close_socket(s
);
1020 if (listen(s
,SOMAXCONN
) < 0) {
1021 log_warn(LD_NET
, "Could not listen on %s: %s", address
,
1022 tor_socket_strerror(tor_socket_errno(s
)));
1023 tor_close_socket(s
);
1028 #endif /* HAVE_SYS_UN_H */
1030 log_err(LD_BUG
,"Got unexpected address family %d.",
1031 listensockaddr
->sa_family
);
1035 set_socket_nonblocking(s
);
1037 lis_conn
= listener_connection_new(type
, listensockaddr
->sa_family
);
1038 conn
= TO_CONN(lis_conn
);
1039 conn
->socket_family
= listensockaddr
->sa_family
;
1041 conn
->address
= tor_strdup(address
);
1042 conn
->port
= gotPort
;
1043 tor_addr_copy(&conn
->addr
, &addr
);
1045 if (port_cfg
->isolation_flags
) {
1046 lis_conn
->isolation_flags
= port_cfg
->isolation_flags
;
1047 if (port_cfg
->session_group
>= 0) {
1048 lis_conn
->session_group
= port_cfg
->session_group
;
1050 /* This can wrap after around INT_MAX listeners are opened. But I don't
1051 * believe that matters, since you would need to open a ridiculous
1052 * number of listeners while keeping the early ones open before you ever
1053 * hit this. An OR with a dozen ports open, for example, would have to
1054 * close and re-open its listeners every second for 4 years nonstop.
1056 lis_conn
->session_group
= global_next_session_group
--;
1060 if (connection_add(conn
) < 0) { /* no space, forget it */
1061 log_warn(LD_NET
,"connection_add for listener failed. Giving up.");
1062 connection_free(conn
);
1066 log_fn(usePort
==gotPort
? LOG_DEBUG
: LOG_NOTICE
, LD_NET
,
1067 "%s listening on port %u.",
1068 conn_type_to_string(type
), gotPort
);
1070 conn
->state
= LISTENER_STATE_READY
;
1071 if (start_reading
) {
1072 connection_start_reading(conn
);
1074 tor_assert(type
== CONN_TYPE_AP_DNS_LISTENER
);
1075 dnsserv_configure_listener(conn
);
1084 /** Do basic sanity checking on a newly received socket. Return 0
1085 * if it looks ok, else return -1.
1087 * Notably, some TCP stacks can erroneously have accept() return successfully
1088 * with socklen 0, when the client sends an RST before the accept call (as
1089 * nmap does). We want to detect that, and not go on with the connection.
1092 check_sockaddr(struct sockaddr
*sa
, int len
, int level
)
1096 if (sa
->sa_family
== AF_INET
) {
1097 struct sockaddr_in
*sin
=(struct sockaddr_in
*)sa
;
1098 if (len
!= sizeof(struct sockaddr_in
)) {
1099 log_fn(level
, LD_NET
, "Length of address not as expected: %d vs %d",
1100 len
,(int)sizeof(struct sockaddr_in
));
1103 if (sin
->sin_addr
.s_addr
== 0 || sin
->sin_port
== 0) {
1104 log_fn(level
, LD_NET
,
1105 "Address for new connection has address/port equal to zero.");
1108 } else if (sa
->sa_family
== AF_INET6
) {
1109 struct sockaddr_in6
*sin6
=(struct sockaddr_in6
*)sa
;
1110 if (len
!= sizeof(struct sockaddr_in6
)) {
1111 log_fn(level
, LD_NET
, "Length of address not as expected: %d vs %d",
1112 len
,(int)sizeof(struct sockaddr_in6
));
1115 if (tor_mem_is_zero((void*)sin6
->sin6_addr
.s6_addr
, 16) ||
1116 sin6
->sin6_port
== 0) {
1117 log_fn(level
, LD_NET
,
1118 "Address for new connection has address/port equal to zero.");
1127 /** Check whether the socket family from an accepted socket <b>got</b> is the
1128 * same as the one that <b>listener</b> is waiting for. If it isn't, log
1129 * a useful message and return -1. Else return 0.
1131 * This is annoying, but can apparently happen on some Darwins. */
1133 check_sockaddr_family_match(sa_family_t got
, connection_t
*listener
)
1135 if (got
!= listener
->socket_family
) {
1136 log_info(LD_BUG
, "A listener connection returned a socket with a "
1137 "mismatched family. %s for addr_family %d gave us a socket "
1138 "with address family %d. Dropping.",
1139 conn_type_to_string(listener
->type
),
1140 (int)listener
->socket_family
,
1147 /** The listener connection <b>conn</b> told poll() it wanted to read.
1148 * Call accept() on conn-\>s, and add the new connection if necessary.
1151 connection_handle_listener_read(connection_t
*conn
, int new_type
)
1153 tor_socket_t news
; /* the new socket */
1154 connection_t
*newconn
;
1155 /* information about the remote peer when connecting to other routers */
1156 struct sockaddr_storage addrbuf
;
1157 struct sockaddr
*remote
= (struct sockaddr
*)&addrbuf
;
1158 /* length of the remote address. Must be whatever accept() needs. */
1159 socklen_t remotelen
= (socklen_t
)sizeof(addrbuf
);
1160 const or_options_t
*options
= get_options();
1162 tor_assert((size_t)remotelen
>= sizeof(struct sockaddr_in
));
1163 memset(&addrbuf
, 0, sizeof(addrbuf
));
1165 news
= tor_accept_socket(conn
->s
,remote
,&remotelen
);
1166 if (!SOCKET_OK(news
)) { /* accept() error */
1167 int e
= tor_socket_errno(conn
->s
);
1168 if (ERRNO_IS_ACCEPT_EAGAIN(e
)) {
1169 return 0; /* he hung up before we could accept(). that's fine. */
1170 } else if (ERRNO_IS_ACCEPT_RESOURCE_LIMIT(e
)) {
1171 warn_too_many_conns();
1174 /* else there was a real error. */
1175 log_warn(LD_NET
,"accept() failed: %s. Closing listener.",
1176 tor_socket_strerror(e
));
1177 connection_mark_for_close(conn
);
1181 "Connection accepted on socket %d (child of fd %d).",
1182 (int)news
,(int)conn
->s
);
1184 make_socket_reuseable(news
);
1185 set_socket_nonblocking(news
);
1187 if (options
->ConstrainedSockets
)
1188 set_constrained_socket_buffers(news
, (int)options
->ConstrainedSockSize
);
1190 if (check_sockaddr_family_match(remote
->sa_family
, conn
) < 0) {
1191 tor_close_socket(news
);
1195 if (conn
->socket_family
== AF_INET
|| conn
->socket_family
== AF_INET6
) {
1198 if (check_sockaddr(remote
, remotelen
, LOG_INFO
)<0) {
1200 "accept() returned a strange address; closing connection.");
1201 tor_close_socket(news
);
1205 if (check_sockaddr_family_match(remote
->sa_family
, conn
) < 0) {
1206 tor_close_socket(news
);
1210 tor_addr_from_sockaddr(&addr
, remote
, &port
);
1212 /* process entrance policies here, before we even create the connection */
1213 if (new_type
== CONN_TYPE_AP
) {
1214 /* check sockspolicy to see if we should accept it */
1215 if (socks_policy_permits_address(&addr
) == 0) {
1217 "Denying socks connection from untrusted address %s.",
1219 tor_close_socket(news
);
1223 if (new_type
== CONN_TYPE_DIR
) {
1224 /* check dirpolicy to see if we should accept it */
1225 if (dir_policy_permits_address(&addr
) == 0) {
1226 log_notice(LD_DIRSERV
,"Denying dir connection from address %s.",
1228 tor_close_socket(news
);
1233 newconn
= connection_new(new_type
, conn
->socket_family
);
1236 /* remember the remote address */
1237 tor_addr_copy(&newconn
->addr
, &addr
);
1238 newconn
->port
= port
;
1239 newconn
->address
= tor_dup_addr(&addr
);
1241 } else if (conn
->socket_family
== AF_UNIX
) {
1242 /* For now only control ports can be Unix domain sockets
1243 * and listeners at the same time */
1244 tor_assert(conn
->type
== CONN_TYPE_CONTROL_LISTENER
);
1246 newconn
= connection_new(new_type
, conn
->socket_family
);
1249 /* remember the remote address -- do we have anything sane to put here? */
1250 tor_addr_make_unspec(&newconn
->addr
);
1252 newconn
->address
= tor_strdup(conn
->address
);
1257 if (connection_add(newconn
) < 0) { /* no space, forget it */
1258 connection_free(newconn
);
1259 return 0; /* no need to tear down the parent */
1262 if (connection_init_accepted_conn(newconn
, TO_LISTENER_CONN(conn
)) < 0) {
1263 if (! newconn
->marked_for_close
)
1264 connection_mark_for_close(newconn
);
1270 /** Initialize states for newly accepted connection <b>conn</b>.
1271 * If conn is an OR, start the TLS handshake.
1272 * If conn is a transparent AP, get its original destination
1273 * and place it in circuit_wait.
1276 connection_init_accepted_conn(connection_t
*conn
,
1277 const listener_connection_t
*listener
)
1279 connection_start_reading(conn
);
1281 switch (conn
->type
) {
1283 control_event_or_conn_status(TO_OR_CONN(conn
), OR_CONN_EVENT_NEW
, 0);
1284 return connection_tls_start_handshake(TO_OR_CONN(conn
), 1);
1286 TO_ENTRY_CONN(conn
)->isolation_flags
= listener
->isolation_flags
;
1287 TO_ENTRY_CONN(conn
)->session_group
= listener
->session_group
;
1288 TO_ENTRY_CONN(conn
)->nym_epoch
= get_signewnym_epoch();
1289 TO_ENTRY_CONN(conn
)->socks_request
->listener_type
= listener
->_base
.type
;
1290 switch (TO_CONN(listener
)->type
) {
1291 case CONN_TYPE_AP_LISTENER
:
1292 conn
->state
= AP_CONN_STATE_SOCKS_WAIT
;
1294 case CONN_TYPE_AP_TRANS_LISTENER
:
1295 TO_ENTRY_CONN(conn
)->is_transparent_ap
= 1;
1296 conn
->state
= AP_CONN_STATE_CIRCUIT_WAIT
;
1297 return connection_ap_process_transparent(TO_ENTRY_CONN(conn
));
1298 case CONN_TYPE_AP_NATD_LISTENER
:
1299 TO_ENTRY_CONN(conn
)->is_transparent_ap
= 1;
1300 conn
->state
= AP_CONN_STATE_NATD_WAIT
;
1305 conn
->purpose
= DIR_PURPOSE_SERVER
;
1306 conn
->state
= DIR_CONN_STATE_SERVER_COMMAND_WAIT
;
1308 case CONN_TYPE_CONTROL
:
1309 conn
->state
= CONTROL_CONN_STATE_NEEDAUTH
;
1315 /** Take conn, make a nonblocking socket; try to connect to
1316 * addr:port (they arrive in *host order*). If fail, return -1 and if
1317 * applicable put your best guess about errno into *<b>socket_error</b>.
1318 * Else assign s to conn-\>s: if connected return 1, if EAGAIN return 0.
1320 * address is used to make the logs useful.
1322 * On success, add conn to the list of polled connections.
1325 connection_connect(connection_t
*conn
, const char *address
,
1326 const tor_addr_t
*addr
, uint16_t port
, int *socket_error
)
1330 struct sockaddr_storage addrbuf
;
1331 struct sockaddr
*dest_addr
;
1333 const or_options_t
*options
= get_options();
1334 int protocol_family
;
1336 if (get_n_open_sockets() >= get_options()->_ConnLimit
-1) {
1337 warn_too_many_conns();
1338 *socket_error
= SOCK_ERRNO(ENOBUFS
);
1342 if (tor_addr_family(addr
) == AF_INET6
)
1343 protocol_family
= PF_INET6
;
1345 protocol_family
= PF_INET
;
1347 if (get_options()->DisableNetwork
) {
1348 /* We should never even try to connect anyplace if DisableNetwork is set.
1349 * Warn if we do, and refuse to make the connection. */
1350 static ratelim_t disablenet_violated
= RATELIM_INIT(30*60);
1353 *socket_error
= WSAENETUNREACH
;
1355 *socket_error
= ENETUNREACH
;
1357 if ((m
= rate_limit_log(&disablenet_violated
, approx_time()))) {
1358 log_warn(LD_BUG
, "Tried to open a socket with DisableNetwork set.%s", m
);
1361 tor_fragile_assert();
1365 s
= tor_open_socket(protocol_family
,SOCK_STREAM
,IPPROTO_TCP
);
1366 if (! SOCKET_OK(s
)) {
1367 *socket_error
= tor_socket_errno(-1);
1368 log_warn(LD_NET
,"Error creating network socket: %s",
1369 tor_socket_strerror(*socket_error
));
1373 make_socket_reuseable(s
);
1375 if (options
->OutboundBindAddress
&& !tor_addr_is_loopback(addr
)) {
1376 struct sockaddr_in ext_addr
;
1378 memset(&ext_addr
, 0, sizeof(ext_addr
));
1379 ext_addr
.sin_family
= AF_INET
;
1380 ext_addr
.sin_port
= 0;
1381 if (!tor_inet_aton(options
->OutboundBindAddress
, &ext_addr
.sin_addr
)) {
1382 log_warn(LD_CONFIG
,"Outbound bind address '%s' didn't parse. Ignoring.",
1383 options
->OutboundBindAddress
);
1385 if (bind(s
, (struct sockaddr
*)&ext_addr
,
1386 (socklen_t
)sizeof(ext_addr
)) < 0) {
1387 *socket_error
= tor_socket_errno(s
);
1388 log_warn(LD_NET
,"Error binding network socket: %s",
1389 tor_socket_strerror(*socket_error
));
1390 tor_close_socket(s
);
1396 set_socket_nonblocking(s
);
1398 if (options
->ConstrainedSockets
)
1399 set_constrained_socket_buffers(s
, (int)options
->ConstrainedSockSize
);
1401 memset(&addrbuf
,0,sizeof(addrbuf
));
1402 dest_addr
= (struct sockaddr
*) &addrbuf
;
1403 dest_addr_len
= tor_addr_to_sockaddr(addr
, port
, dest_addr
, sizeof(addrbuf
));
1404 tor_assert(dest_addr_len
> 0);
1406 log_debug(LD_NET
, "Connecting to %s:%u.",
1407 escaped_safe_str_client(address
), port
);
1409 if (connect(s
, dest_addr
, (socklen_t
)dest_addr_len
) < 0) {
1410 int e
= tor_socket_errno(s
);
1411 if (!ERRNO_IS_CONN_EINPROGRESS(e
)) {
1412 /* yuck. kill it. */
1415 "connect() to %s:%u failed: %s",
1416 escaped_safe_str_client(address
),
1417 port
, tor_socket_strerror(e
));
1418 tor_close_socket(s
);
1425 /* it succeeded. we're connected. */
1426 log_fn(inprogress
?LOG_DEBUG
:LOG_INFO
, LD_NET
,
1427 "Connection to %s:%u %s (sock %d).",
1428 escaped_safe_str_client(address
),
1429 port
, inprogress
?"in progress":"established", s
);
1431 if (connection_add_connecting(conn
) < 0) {
1432 /* no space, forget it */
1433 *socket_error
= SOCK_ERRNO(ENOBUFS
);
1436 return inprogress
? 0 : 1;
1439 /** Convert state number to string representation for logging purposes.
1442 connection_proxy_state_to_string(int state
)
1444 static const char *unknown
= "???";
1445 static const char *states
[] = {
1448 "PROXY_HTTPS_WANT_CONNECT_OK",
1449 "PROXY_SOCKS4_WANT_CONNECT_OK",
1450 "PROXY_SOCKS5_WANT_AUTH_METHOD_NONE",
1451 "PROXY_SOCKS5_WANT_AUTH_METHOD_RFC1929",
1452 "PROXY_SOCKS5_WANT_AUTH_RFC1929_OK",
1453 "PROXY_SOCKS5_WANT_CONNECT_OK",
1457 if (state
< PROXY_NONE
|| state
> PROXY_CONNECTED
)
1460 return states
[state
];
1463 /** Write a proxy request of <b>type</b> (socks4, socks5, https) to conn
1464 * for conn->addr:conn->port, authenticating with the auth details given
1465 * in the configuration (if available). SOCKS 5 and HTTP CONNECT proxies
1466 * support authentication.
1468 * Returns -1 if conn->addr is incompatible with the proxy protocol, and
1471 * Use connection_read_proxy_handshake() to complete the handshake.
1474 connection_proxy_connect(connection_t
*conn
, int type
)
1476 const or_options_t
*options
;
1480 options
= get_options();
1483 case PROXY_CONNECT
: {
1485 char *base64_authenticator
=NULL
;
1486 const char *authenticator
= options
->HTTPSProxyAuthenticator
;
1488 /* Send HTTP CONNECT and authentication (if available) in
1491 if (authenticator
) {
1492 base64_authenticator
= alloc_http_authenticator(authenticator
);
1493 if (!base64_authenticator
)
1494 log_warn(LD_OR
, "Encoding https authenticator failed");
1497 if (base64_authenticator
) {
1498 const char *addr
= fmt_addr(&conn
->addr
);
1499 tor_snprintf(buf
, sizeof(buf
), "CONNECT %s:%d HTTP/1.1\r\n"
1501 "Proxy-Authorization: Basic %s\r\n\r\n",
1504 base64_authenticator
);
1505 tor_free(base64_authenticator
);
1507 tor_snprintf(buf
, sizeof(buf
), "CONNECT %s:%d HTTP/1.0\r\n\r\n",
1508 fmt_addr(&conn
->addr
), conn
->port
);
1511 connection_write_to_buf(buf
, strlen(buf
), conn
);
1512 conn
->proxy_state
= PROXY_HTTPS_WANT_CONNECT_OK
;
1516 case PROXY_SOCKS4
: {
1517 unsigned char buf
[9];
1521 /* Send a SOCKS4 connect request with empty user id */
1523 if (tor_addr_family(&conn
->addr
) != AF_INET
) {
1524 log_warn(LD_NET
, "SOCKS4 client is incompatible with IPv6");
1528 ip4addr
= tor_addr_to_ipv4n(&conn
->addr
);
1529 portn
= htons(conn
->port
);
1531 buf
[0] = 4; /* version */
1532 buf
[1] = SOCKS_COMMAND_CONNECT
; /* command */
1533 memcpy(buf
+ 2, &portn
, 2); /* port */
1534 memcpy(buf
+ 4, &ip4addr
, 4); /* addr */
1535 buf
[8] = 0; /* userid (empty) */
1537 connection_write_to_buf((char *)buf
, sizeof(buf
), conn
);
1538 conn
->proxy_state
= PROXY_SOCKS4_WANT_CONNECT_OK
;
1542 case PROXY_SOCKS5
: {
1543 unsigned char buf
[4]; /* fields: vers, num methods, method list */
1545 /* Send a SOCKS5 greeting (connect request must wait) */
1547 buf
[0] = 5; /* version */
1549 /* number of auth methods */
1550 if (options
->Socks5ProxyUsername
) {
1552 buf
[2] = 0x00; /* no authentication */
1553 buf
[3] = 0x02; /* rfc1929 Username/Passwd auth */
1554 conn
->proxy_state
= PROXY_SOCKS5_WANT_AUTH_METHOD_RFC1929
;
1557 buf
[2] = 0x00; /* no authentication */
1558 conn
->proxy_state
= PROXY_SOCKS5_WANT_AUTH_METHOD_NONE
;
1561 connection_write_to_buf((char *)buf
, 2 + buf
[1], conn
);
1566 log_err(LD_BUG
, "Invalid proxy protocol, %d", type
);
1567 tor_fragile_assert();
1571 log_debug(LD_NET
, "set state %s",
1572 connection_proxy_state_to_string(conn
->proxy_state
));
1577 /** Read conn's inbuf. If the http response from the proxy is all
1578 * here, make sure it's good news, then return 1. If it's bad news,
1579 * return -1. Else return 0 and hope for better luck next time.
1582 connection_read_https_proxy_response(connection_t
*conn
)
1589 switch (fetch_from_buf_http(conn
->inbuf
,
1590 &headers
, MAX_HEADERS_SIZE
,
1591 NULL
, NULL
, 10000, 0)) {
1592 case -1: /* overflow */
1593 log_warn(LD_PROTOCOL
,
1594 "Your https proxy sent back an oversized response. Closing.");
1597 log_info(LD_NET
,"https proxy response not all here yet. Waiting.");
1599 /* case 1, fall through */
1602 if (parse_http_response(headers
, &status_code
, &date_header
,
1603 NULL
, &reason
) < 0) {
1605 "Unparseable headers from proxy (connecting to '%s'). Closing.",
1610 if (!reason
) reason
= tor_strdup("[no reason given]");
1612 if (status_code
== 200) {
1614 "HTTPS connect to '%s' successful! (200 %s) Starting TLS.",
1615 conn
->address
, escaped(reason
));
1619 /* else, bad news on the status code */
1620 switch (status_code
) {
1623 "The https proxy refused to allow connection to %s "
1624 "(status code %d, %s). Closing.",
1625 conn
->address
, status_code
, escaped(reason
));
1629 "The https proxy sent back an unexpected status code %d (%s). "
1631 status_code
, escaped(reason
));
1638 /** Send SOCKS5 CONNECT command to <b>conn</b>, copying <b>conn->addr</b>
1639 * and <b>conn->port</b> into the request.
1642 connection_send_socks5_connect(connection_t
*conn
)
1644 unsigned char buf
[1024];
1646 uint16_t port
= htons(conn
->port
);
1648 buf
[0] = 5; /* version */
1649 buf
[1] = SOCKS_COMMAND_CONNECT
; /* command */
1650 buf
[2] = 0; /* reserved */
1652 if (tor_addr_family(&conn
->addr
) == AF_INET
) {
1653 uint32_t addr
= tor_addr_to_ipv4n(&conn
->addr
);
1657 memcpy(buf
+ 4, &addr
, 4);
1658 memcpy(buf
+ 8, &port
, 2);
1659 } else { /* AF_INET6 */
1662 memcpy(buf
+ 4, tor_addr_to_in6(&conn
->addr
), 16);
1663 memcpy(buf
+ 20, &port
, 2);
1666 connection_write_to_buf((char *)buf
, reqsize
, conn
);
1668 conn
->proxy_state
= PROXY_SOCKS5_WANT_CONNECT_OK
;
1671 /** Wrapper around fetch_from_(buf/evbuffer)_socks_client: see those functions
1672 * for documentation of its behavior. */
1674 connection_fetch_from_buf_socks_client(connection_t
*conn
,
1675 int state
, char **reason
)
1677 IF_HAS_BUFFEREVENT(conn
, {
1678 struct evbuffer
*input
= bufferevent_get_input(conn
->bufev
);
1679 return fetch_from_evbuffer_socks_client(input
, state
, reason
);
1680 }) ELSE_IF_NO_BUFFEREVENT
{
1681 return fetch_from_buf_socks_client(conn
->inbuf
, state
, reason
);
1685 /** Call this from connection_*_process_inbuf() to advance the proxy
1688 * No matter what proxy protocol is used, if this function returns 1, the
1689 * handshake is complete, and the data remaining on inbuf may contain the
1690 * start of the communication with the requested server.
1692 * Returns 0 if the current buffer contains an incomplete response, and -1
1696 connection_read_proxy_handshake(connection_t
*conn
)
1699 char *reason
= NULL
;
1701 log_debug(LD_NET
, "enter state %s",
1702 connection_proxy_state_to_string(conn
->proxy_state
));
1704 switch (conn
->proxy_state
) {
1705 case PROXY_HTTPS_WANT_CONNECT_OK
:
1706 ret
= connection_read_https_proxy_response(conn
);
1708 conn
->proxy_state
= PROXY_CONNECTED
;
1711 case PROXY_SOCKS4_WANT_CONNECT_OK
:
1712 ret
= connection_fetch_from_buf_socks_client(conn
,
1716 conn
->proxy_state
= PROXY_CONNECTED
;
1719 case PROXY_SOCKS5_WANT_AUTH_METHOD_NONE
:
1720 ret
= connection_fetch_from_buf_socks_client(conn
,
1723 /* no auth needed, do connect */
1725 connection_send_socks5_connect(conn
);
1730 case PROXY_SOCKS5_WANT_AUTH_METHOD_RFC1929
:
1731 ret
= connection_fetch_from_buf_socks_client(conn
,
1735 /* send auth if needed, otherwise do connect */
1737 connection_send_socks5_connect(conn
);
1739 } else if (ret
== 2) {
1740 unsigned char buf
[1024];
1741 size_t reqsize
, usize
, psize
;
1742 const char *user
, *pass
;
1744 user
= get_options()->Socks5ProxyUsername
;
1745 pass
= get_options()->Socks5ProxyPassword
;
1746 tor_assert(user
&& pass
);
1748 /* XXX len of user and pass must be <= 255 !!! */
1749 usize
= strlen(user
);
1750 psize
= strlen(pass
);
1751 tor_assert(usize
<= 255 && psize
<= 255);
1752 reqsize
= 3 + usize
+ psize
;
1754 buf
[0] = 1; /* negotiation version */
1756 memcpy(buf
+ 2, user
, usize
);
1757 buf
[2 + usize
] = psize
;
1758 memcpy(buf
+ 3 + usize
, pass
, psize
);
1760 connection_write_to_buf((char *)buf
, reqsize
, conn
);
1762 conn
->proxy_state
= PROXY_SOCKS5_WANT_AUTH_RFC1929_OK
;
1767 case PROXY_SOCKS5_WANT_AUTH_RFC1929_OK
:
1768 ret
= connection_fetch_from_buf_socks_client(conn
,
1771 /* send the connect request */
1773 connection_send_socks5_connect(conn
);
1778 case PROXY_SOCKS5_WANT_CONNECT_OK
:
1779 ret
= connection_fetch_from_buf_socks_client(conn
,
1783 conn
->proxy_state
= PROXY_CONNECTED
;
1787 log_err(LD_BUG
, "Invalid proxy_state for reading, %d",
1789 tor_fragile_assert();
1794 log_debug(LD_NET
, "leaving state %s",
1795 connection_proxy_state_to_string(conn
->proxy_state
));
1799 log_warn(LD_NET
, "Proxy Client: unable to connect to %s:%d (%s)",
1800 conn
->address
, conn
->port
, escaped(reason
));
1803 log_warn(LD_NET
, "Proxy Client: unable to connect to %s:%d",
1804 conn
->address
, conn
->port
);
1806 } else if (ret
== 1) {
1807 log_info(LD_NET
, "Proxy Client: connection to %s:%d successful",
1808 conn
->address
, conn
->port
);
1814 /** Given a list of listener connections in <b>old_conns</b>, and list of
1815 * port_cfg_t entries in <b>ports</b>, open a new listener for every port in
1816 * <b>ports</b> that does not already have a listener in <b>old_conns</b>.
1818 * Remove from <b>old_conns</b> every connection that has a corresponding
1819 * entry in <b>ports</b>. Add to <b>new_conns</b> new every connection we
1822 * If <b>control_listeners_only</b> is true, then we only open control
1823 * listeners, and we do not remove any noncontrol listeners from old_conns.
1825 * Return 0 on success, -1 on failure.
1828 retry_listener_ports(smartlist_t
*old_conns
,
1829 const smartlist_t
*ports
,
1830 smartlist_t
*new_conns
,
1831 int control_listeners_only
)
1833 smartlist_t
*launch
= smartlist_new();
1836 if (control_listeners_only
) {
1837 SMARTLIST_FOREACH(ports
, port_cfg_t
*, p
, {
1838 if (p
->type
== CONN_TYPE_CONTROL_LISTENER
)
1839 smartlist_add(launch
, p
);
1842 smartlist_add_all(launch
, ports
);
1845 /* Iterate through old_conns, comparing it to launch: remove from both lists
1846 * each pair of elements that corresponds to the same port. */
1847 SMARTLIST_FOREACH_BEGIN(old_conns
, connection_t
*, conn
) {
1848 const port_cfg_t
*found_port
= NULL
;
1850 /* Okay, so this is a listener. Is it configured? */
1851 SMARTLIST_FOREACH_BEGIN(launch
, const port_cfg_t
*, wanted
) {
1852 if (conn
->type
!= wanted
->type
)
1854 if ((conn
->socket_family
!= AF_UNIX
&& wanted
->is_unix_addr
) ||
1855 (conn
->socket_family
== AF_UNIX
&& ! wanted
->is_unix_addr
))
1858 if (wanted
->no_listen
)
1859 continue; /* We don't want to open a listener for this one */
1861 if (wanted
->is_unix_addr
) {
1862 if (conn
->socket_family
== AF_UNIX
&&
1863 !strcmp(wanted
->unix_addr
, conn
->address
)) {
1864 found_port
= wanted
;
1869 if (wanted
->port
== CFG_AUTO_PORT
) {
1872 port_matches
= (wanted
->port
== conn
->port
);
1874 if (port_matches
&& tor_addr_eq(&wanted
->addr
, &conn
->addr
)) {
1875 found_port
= wanted
;
1879 } SMARTLIST_FOREACH_END(wanted
);
1882 /* This listener is already running; we don't need to launch it. */
1883 //log_debug(LD_NET, "Already have %s on %s:%d",
1884 // conn_type_to_string(found_port->type), conn->address, conn->port);
1885 smartlist_remove(launch
, found_port
);
1886 /* And we can remove the connection from old_conns too. */
1887 SMARTLIST_DEL_CURRENT(old_conns
, conn
);
1889 } SMARTLIST_FOREACH_END(conn
);
1891 /* Now open all the listeners that are configured but not opened. */
1892 SMARTLIST_FOREACH_BEGIN(launch
, const port_cfg_t
*, port
) {
1893 struct sockaddr
*listensockaddr
;
1894 socklen_t listensocklen
= 0;
1897 int real_port
= port
->port
== CFG_AUTO_PORT
? 0 : port
->port
;
1898 tor_assert(real_port
<= UINT16_MAX
);
1899 if (port
->no_listen
)
1902 if (port
->is_unix_addr
) {
1903 listensockaddr
= (struct sockaddr
*)
1904 create_unix_sockaddr(port
->unix_addr
,
1905 &address
, &listensocklen
);
1907 listensockaddr
= tor_malloc(sizeof(struct sockaddr_storage
));
1908 listensocklen
= tor_addr_to_sockaddr(&port
->addr
,
1911 sizeof(struct sockaddr_storage
));
1912 address
= tor_dup_addr(&port
->addr
);
1915 if (listensockaddr
) {
1916 conn
= connection_listener_new(listensockaddr
, listensocklen
,
1917 port
->type
, address
, port
);
1918 tor_free(listensockaddr
);
1928 smartlist_add(new_conns
, conn
);
1930 } SMARTLIST_FOREACH_END(port
);
1932 smartlist_free(launch
);
1937 /** Launch listeners for each port you should have open. Only launch
1938 * listeners who are not already open, and only close listeners we no longer
1941 * Add all old conns that should be closed to <b>replaced_conns</b>.
1942 * Add all new connections to <b>new_conns</b>.
1944 * If <b>close_all_noncontrol</b> is true, then we only open control
1945 * listeners, and we close all other listeners.
1948 retry_all_listeners(smartlist_t
*replaced_conns
,
1949 smartlist_t
*new_conns
, int close_all_noncontrol
)
1951 smartlist_t
*listeners
= smartlist_new();
1952 const or_options_t
*options
= get_options();
1954 const uint16_t old_or_port
= router_get_advertised_or_port(options
);
1955 const uint16_t old_dir_port
= router_get_advertised_dir_port(options
, 0);
1957 SMARTLIST_FOREACH_BEGIN(get_connection_array(), connection_t
*, conn
) {
1958 if (connection_is_listener(conn
) && !conn
->marked_for_close
)
1959 smartlist_add(listeners
, conn
);
1960 } SMARTLIST_FOREACH_END(conn
);
1962 if (retry_listener_ports(listeners
,
1963 get_configured_ports(),
1965 close_all_noncontrol
) < 0)
1968 /* Any members that were still in 'listeners' don't correspond to
1969 * any configured port. Kill 'em. */
1970 SMARTLIST_FOREACH_BEGIN(listeners
, connection_t
*, conn
) {
1971 log_notice(LD_NET
, "Closing no-longer-configured %s on %s:%d",
1972 conn_type_to_string(conn
->type
), conn
->address
, conn
->port
);
1973 if (replaced_conns
) {
1974 smartlist_add(replaced_conns
, conn
);
1976 connection_close_immediate(conn
);
1977 connection_mark_for_close(conn
);
1979 } SMARTLIST_FOREACH_END(conn
);
1981 smartlist_free(listeners
);
1983 /* XXXprop186 should take all advertised ports into account */
1984 if (old_or_port
!= router_get_advertised_or_port(options
) ||
1985 old_dir_port
!= router_get_advertised_dir_port(options
, 0)) {
1986 /* Our chosen ORPort or DirPort is not what it used to be: the
1987 * descriptor we had (if any) should be regenerated. (We won't
1988 * automatically notice this because of changes in the option,
1989 * since the value could be "auto".) */
1990 mark_my_descriptor_dirty("Chosen Or/DirPort changed");
1996 /** Mark every listener of type other than CONTROL_LISTENER to be closed. */
1998 connection_mark_all_noncontrol_listeners(void)
2000 SMARTLIST_FOREACH_BEGIN(get_connection_array(), connection_t
*, conn
) {
2001 if (conn
->marked_for_close
)
2003 if (conn
->type
== CONN_TYPE_CONTROL_LISTENER
)
2005 if (connection_is_listener(conn
))
2006 connection_mark_for_close(conn
);
2007 } SMARTLIST_FOREACH_END(conn
);
2010 /** Mark every external connection not used for controllers for close. */
2012 connection_mark_all_noncontrol_connections(void)
2014 SMARTLIST_FOREACH_BEGIN(get_connection_array(), connection_t
*, conn
) {
2015 if (conn
->marked_for_close
)
2017 switch (conn
->type
) {
2018 case CONN_TYPE_CPUWORKER
:
2019 case CONN_TYPE_CONTROL_LISTENER
:
2020 case CONN_TYPE_CONTROL
:
2023 connection_mark_unattached_ap(TO_ENTRY_CONN(conn
),
2024 END_STREAM_REASON_HIBERNATING
);
2027 connection_mark_for_close(conn
);
2030 } SMARTLIST_FOREACH_END(conn
);
2033 /** Return 1 if we should apply rate limiting to <b>conn</b>, and 0
2035 * Right now this just checks if it's an internal IP address or an
2036 * internal connection. We also check if the connection uses pluggable
2037 * transports, since we should then limit it even if it comes from an
2038 * internal IP address. */
2040 connection_is_rate_limited(connection_t
*conn
)
2042 const or_options_t
*options
= get_options();
2044 return 0; /* Internal connection */
2045 else if (! options
->CountPrivateBandwidth
&&
2046 (tor_addr_family(&conn
->addr
) == AF_UNSPEC
|| /* no address */
2047 tor_addr_is_internal(&conn
->addr
, 0)))
2048 return 0; /* Internal address */
2053 #ifdef USE_BUFFEREVENTS
2054 static struct bufferevent_rate_limit_group
*global_rate_limit
= NULL
;
2056 extern int global_read_bucket
, global_write_bucket
;
2057 extern int global_relayed_read_bucket
, global_relayed_write_bucket
;
2059 /** Did either global write bucket run dry last second? If so,
2060 * we are likely to run dry again this second, so be stingy with the
2061 * tokens we just put in. */
2062 static int write_buckets_empty_last_second
= 0;
2065 /** How many seconds of no active local circuits will make the
2066 * connection revert to the "relayed" bandwidth class? */
2067 #define CLIENT_IDLE_TIME_FOR_PRIORITY 30
2069 #ifndef USE_BUFFEREVENTS
2070 /** Return 1 if <b>conn</b> should use tokens from the "relayed"
2071 * bandwidth rates, else 0. Currently, only OR conns with bandwidth
2072 * class 1, and directory conns that are serving data out, count.
2075 connection_counts_as_relayed_traffic(connection_t
*conn
, time_t now
)
2077 if (conn
->type
== CONN_TYPE_OR
&&
2078 TO_OR_CONN(conn
)->client_used
+ CLIENT_IDLE_TIME_FOR_PRIORITY
< now
)
2080 if (conn
->type
== CONN_TYPE_DIR
&& DIR_CONN_IS_SERVER(conn
))
2085 /** Helper function to decide how many bytes out of <b>global_bucket</b>
2086 * we're willing to use for this transaction. <b>base</b> is the size
2087 * of a cell on the network; <b>priority</b> says whether we should
2088 * write many of them or just a few; and <b>conn_bucket</b> (if
2089 * non-negative) provides an upper limit for our answer. */
2091 connection_bucket_round_robin(int base
, int priority
,
2092 ssize_t global_bucket
, ssize_t conn_bucket
)
2095 ssize_t num_bytes_high
= (priority
? 32 : 16) * base
;
2096 ssize_t num_bytes_low
= (priority
? 4 : 2) * base
;
2098 /* Do a rudimentary round-robin so one circuit can't hog a connection.
2099 * Pick at most 32 cells, at least 4 cells if possible, and if we're in
2100 * the middle pick 1/8 of the available bandwidth. */
2101 at_most
= global_bucket
/ 8;
2102 at_most
-= (at_most
% base
); /* round down */
2103 if (at_most
> num_bytes_high
) /* 16 KB, or 8 KB for low-priority */
2104 at_most
= num_bytes_high
;
2105 else if (at_most
< num_bytes_low
) /* 2 KB, or 1 KB for low-priority */
2106 at_most
= num_bytes_low
;
2108 if (at_most
> global_bucket
)
2109 at_most
= global_bucket
;
2111 if (conn_bucket
>= 0 && at_most
> conn_bucket
)
2112 at_most
= conn_bucket
;
2119 /** How many bytes at most can we read onto this connection? */
2121 connection_bucket_read_limit(connection_t
*conn
, time_t now
)
2123 int base
= connection_speaks_cells(conn
) ?
2124 CELL_NETWORK_SIZE
: RELAY_PAYLOAD_SIZE
;
2125 int priority
= conn
->type
!= CONN_TYPE_DIR
;
2126 int conn_bucket
= -1;
2127 int global_bucket
= global_read_bucket
;
2129 if (connection_speaks_cells(conn
)) {
2130 or_connection_t
*or_conn
= TO_OR_CONN(conn
);
2131 if (conn
->state
== OR_CONN_STATE_OPEN
)
2132 conn_bucket
= or_conn
->read_bucket
;
2135 if (!connection_is_rate_limited(conn
)) {
2136 /* be willing to read on local conns even if our buckets are empty */
2137 return conn_bucket
>=0 ? conn_bucket
: 1<<14;
2140 if (connection_counts_as_relayed_traffic(conn
, now
) &&
2141 global_relayed_read_bucket
<= global_read_bucket
)
2142 global_bucket
= global_relayed_read_bucket
;
2144 return connection_bucket_round_robin(base
, priority
,
2145 global_bucket
, conn_bucket
);
2148 /** How many bytes at most can we write onto this connection? */
2150 connection_bucket_write_limit(connection_t
*conn
, time_t now
)
2152 int base
= connection_speaks_cells(conn
) ?
2153 CELL_NETWORK_SIZE
: RELAY_PAYLOAD_SIZE
;
2154 int priority
= conn
->type
!= CONN_TYPE_DIR
;
2155 int conn_bucket
= (int)conn
->outbuf_flushlen
;
2156 int global_bucket
= global_write_bucket
;
2158 if (!connection_is_rate_limited(conn
)) {
2159 /* be willing to write to local conns even if our buckets are empty */
2160 return conn
->outbuf_flushlen
;
2163 if (connection_speaks_cells(conn
)) {
2164 /* use the per-conn write limit if it's lower, but if it's less
2165 * than zero just use zero */
2166 or_connection_t
*or_conn
= TO_OR_CONN(conn
);
2167 if (conn
->state
== OR_CONN_STATE_OPEN
)
2168 if (or_conn
->write_bucket
< conn_bucket
)
2169 conn_bucket
= or_conn
->write_bucket
>= 0 ?
2170 or_conn
->write_bucket
: 0;
2173 if (connection_counts_as_relayed_traffic(conn
, now
) &&
2174 global_relayed_write_bucket
<= global_write_bucket
)
2175 global_bucket
= global_relayed_write_bucket
;
2177 return connection_bucket_round_robin(base
, priority
,
2178 global_bucket
, conn_bucket
);
2182 connection_bucket_read_limit(connection_t
*conn
, time_t now
)
2185 return bufferevent_get_max_to_read(conn
->bufev
);
2188 connection_bucket_write_limit(connection_t
*conn
, time_t now
)
2191 return bufferevent_get_max_to_write(conn
->bufev
);
2195 /** Return 1 if the global write buckets are low enough that we
2196 * shouldn't send <b>attempt</b> bytes of low-priority directory stuff
2197 * out to <b>conn</b>. Else return 0.
2199 * Priority is 1 for v1 requests (directories and running-routers),
2200 * and 2 for v2 requests (statuses and descriptors). But see FFFF in
2201 * directory_handle_command_get() for why we don't use priority 2 yet.
2203 * There are a lot of parameters we could use here:
2204 * - global_relayed_write_bucket. Low is bad.
2205 * - global_write_bucket. Low is bad.
2206 * - bandwidthrate. Low is bad.
2207 * - bandwidthburst. Not a big factor?
2208 * - attempt. High is bad.
2209 * - total bytes queued on outbufs. High is bad. But I'm wary of
2210 * using this, since a few slow-flushing queues will pump up the
2211 * number without meaning what we meant to mean. What we really
2212 * mean is "total directory bytes added to outbufs recently", but
2213 * that's harder to quantify and harder to keep track of.
2216 global_write_bucket_low(connection_t
*conn
, size_t attempt
, int priority
)
2218 #ifdef USE_BUFFEREVENTS
2219 ssize_t smaller_bucket
= bufferevent_get_max_to_write(conn
->bufev
);
2221 int smaller_bucket
= global_write_bucket
< global_relayed_write_bucket
?
2222 global_write_bucket
: global_relayed_write_bucket
;
2224 if (authdir_mode(get_options()) && priority
>1)
2225 return 0; /* there's always room to answer v2 if we're an auth dir */
2227 if (!connection_is_rate_limited(conn
))
2228 return 0; /* local conns don't get limited */
2230 if (smaller_bucket
< (int)attempt
)
2231 return 1; /* not enough space no matter the priority */
2233 #ifndef USE_BUFFEREVENTS
2234 if (write_buckets_empty_last_second
)
2235 return 1; /* we're already hitting our limits, no more please */
2238 if (priority
== 1) { /* old-style v1 query */
2239 /* Could we handle *two* of these requests within the next two seconds? */
2240 const or_options_t
*options
= get_options();
2241 int64_t can_write
= (int64_t)smaller_bucket
2242 + 2*(options
->RelayBandwidthRate
? options
->RelayBandwidthRate
:
2243 options
->BandwidthRate
);
2244 if (can_write
< 2*(int64_t)attempt
)
2246 } else { /* v2 query */
2247 /* no further constraints yet */
2252 /** Helper: adjusts our bandwidth history and informs the controller as
2253 * appropriate, given that we have just read <b>num_read</b> bytes and written
2254 * <b>num_written</b> bytes on <b>conn</b>. */
2256 record_num_bytes_transferred_impl(connection_t
*conn
,
2257 time_t now
, size_t num_read
, size_t num_written
)
2259 /* Count bytes of answering direct and tunneled directory requests */
2260 if (conn
->type
== CONN_TYPE_DIR
&& conn
->purpose
== DIR_PURPOSE_SERVER
) {
2262 rep_hist_note_dir_bytes_read(num_read
, now
);
2263 if (num_written
> 0)
2264 rep_hist_note_dir_bytes_written(num_written
, now
);
2267 if (!connection_is_rate_limited(conn
))
2268 return; /* local IPs are free */
2270 if (conn
->type
== CONN_TYPE_OR
)
2271 rep_hist_note_or_conn_bytes(conn
->global_identifier
, num_read
,
2275 rep_hist_note_bytes_read(num_read
, now
);
2277 if (num_written
> 0) {
2278 rep_hist_note_bytes_written(num_written
, now
);
2280 if (conn
->type
== CONN_TYPE_EXIT
)
2281 rep_hist_note_exit_bytes(conn
->port
, num_written
, num_read
);
2284 #ifdef USE_BUFFEREVENTS
2285 /** Wrapper around fetch_from_(buf/evbuffer)_socks_client: see those functions
2286 * for documentation of its behavior. */
2288 record_num_bytes_transferred(connection_t
*conn
,
2289 time_t now
, size_t num_read
, size_t num_written
)
2291 /* XXX024 check if this is necessary */
2292 if (num_written
>= INT_MAX
|| num_read
>= INT_MAX
) {
2293 log_err(LD_BUG
, "Value out of range. num_read=%lu, num_written=%lu, "
2294 "connection type=%s, state=%s",
2295 (unsigned long)num_read
, (unsigned long)num_written
,
2296 conn_type_to_string(conn
->type
),
2297 conn_state_to_string(conn
->type
, conn
->state
));
2298 if (num_written
>= INT_MAX
) num_written
= 1;
2299 if (num_read
>= INT_MAX
) num_read
= 1;
2300 tor_fragile_assert();
2303 record_num_bytes_transferred_impl(conn
,now
,num_read
,num_written
);
2307 #ifndef USE_BUFFEREVENTS
2308 /** We just read <b>num_read</b> and wrote <b>num_written</b> bytes
2309 * onto <b>conn</b>. Decrement buckets appropriately. */
2311 connection_buckets_decrement(connection_t
*conn
, time_t now
,
2312 size_t num_read
, size_t num_written
)
2314 if (num_written
>= INT_MAX
|| num_read
>= INT_MAX
) {
2315 log_err(LD_BUG
, "Value out of range. num_read=%lu, num_written=%lu, "
2316 "connection type=%s, state=%s",
2317 (unsigned long)num_read
, (unsigned long)num_written
,
2318 conn_type_to_string(conn
->type
),
2319 conn_state_to_string(conn
->type
, conn
->state
));
2320 if (num_written
>= INT_MAX
) num_written
= 1;
2321 if (num_read
>= INT_MAX
) num_read
= 1;
2322 tor_fragile_assert();
2325 record_num_bytes_transferred_impl(conn
, now
, num_read
, num_written
);
2327 if (!connection_is_rate_limited(conn
))
2328 return; /* local IPs are free */
2330 if (connection_counts_as_relayed_traffic(conn
, now
)) {
2331 global_relayed_read_bucket
-= (int)num_read
;
2332 global_relayed_write_bucket
-= (int)num_written
;
2334 global_read_bucket
-= (int)num_read
;
2335 global_write_bucket
-= (int)num_written
;
2336 if (connection_speaks_cells(conn
) && conn
->state
== OR_CONN_STATE_OPEN
) {
2337 TO_OR_CONN(conn
)->read_bucket
-= (int)num_read
;
2338 TO_OR_CONN(conn
)->write_bucket
-= (int)num_written
;
2342 /** If we have exhausted our global buckets, or the buckets for conn,
2345 connection_consider_empty_read_buckets(connection_t
*conn
)
2349 if (global_read_bucket
<= 0) {
2350 reason
= "global read bucket exhausted. Pausing.";
2351 } else if (connection_counts_as_relayed_traffic(conn
, approx_time()) &&
2352 global_relayed_read_bucket
<= 0) {
2353 reason
= "global relayed read bucket exhausted. Pausing.";
2354 } else if (connection_speaks_cells(conn
) &&
2355 conn
->state
== OR_CONN_STATE_OPEN
&&
2356 TO_OR_CONN(conn
)->read_bucket
<= 0) {
2357 reason
= "connection read bucket exhausted. Pausing.";
2359 return; /* all good, no need to stop it */
2361 LOG_FN_CONN(conn
, (LOG_DEBUG
, LD_NET
, "%s", reason
));
2362 conn
->read_blocked_on_bw
= 1;
2363 connection_stop_reading(conn
);
2366 /** If we have exhausted our global buckets, or the buckets for conn,
2369 connection_consider_empty_write_buckets(connection_t
*conn
)
2373 if (global_write_bucket
<= 0) {
2374 reason
= "global write bucket exhausted. Pausing.";
2375 } else if (connection_counts_as_relayed_traffic(conn
, approx_time()) &&
2376 global_relayed_write_bucket
<= 0) {
2377 reason
= "global relayed write bucket exhausted. Pausing.";
2378 } else if (connection_speaks_cells(conn
) &&
2379 conn
->state
== OR_CONN_STATE_OPEN
&&
2380 TO_OR_CONN(conn
)->write_bucket
<= 0) {
2381 reason
= "connection write bucket exhausted. Pausing.";
2383 return; /* all good, no need to stop it */
2385 LOG_FN_CONN(conn
, (LOG_DEBUG
, LD_NET
, "%s", reason
));
2386 conn
->write_blocked_on_bw
= 1;
2387 connection_stop_writing(conn
);
2390 /** Initialize the global read bucket to options-\>BandwidthBurst. */
2392 connection_bucket_init(void)
2394 const or_options_t
*options
= get_options();
2395 /* start it at max traffic */
2396 global_read_bucket
= (int)options
->BandwidthBurst
;
2397 global_write_bucket
= (int)options
->BandwidthBurst
;
2398 if (options
->RelayBandwidthRate
) {
2399 global_relayed_read_bucket
= (int)options
->RelayBandwidthBurst
;
2400 global_relayed_write_bucket
= (int)options
->RelayBandwidthBurst
;
2402 global_relayed_read_bucket
= (int)options
->BandwidthBurst
;
2403 global_relayed_write_bucket
= (int)options
->BandwidthBurst
;
2407 /** Refill a single <b>bucket</b> called <b>name</b> with bandwidth rate per
2408 * second <b>rate</b> and bandwidth burst <b>burst</b>, assuming that
2409 * <b>milliseconds_elapsed</b> milliseconds have passed since the last
2412 connection_bucket_refill_helper(int *bucket
, int rate
, int burst
,
2413 int milliseconds_elapsed
,
2416 int starting_bucket
= *bucket
;
2417 if (starting_bucket
< burst
&& milliseconds_elapsed
> 0) {
2418 int64_t incr
= (((int64_t)rate
) * milliseconds_elapsed
) / 1000;
2419 if ((burst
- starting_bucket
) < incr
) {
2420 *bucket
= burst
; /* We would overflow the bucket; just set it to
2423 *bucket
+= (int)incr
;
2424 if (*bucket
> burst
|| *bucket
< starting_bucket
) {
2425 /* If we overflow the burst, or underflow our starting bucket,
2426 * cap the bucket value to burst. */
2427 /* XXXX this might be redundant now, but it doesn't show up
2428 * in profiles. Remove it after analysis. */
2432 log(LOG_DEBUG
, LD_NET
,"%s now %d.", name
, *bucket
);
2436 /** Time has passed; increment buckets appropriately. */
2438 connection_bucket_refill(int milliseconds_elapsed
, time_t now
)
2440 const or_options_t
*options
= get_options();
2441 smartlist_t
*conns
= get_connection_array();
2442 int bandwidthrate
, bandwidthburst
, relayrate
, relayburst
;
2444 bandwidthrate
= (int)options
->BandwidthRate
;
2445 bandwidthburst
= (int)options
->BandwidthBurst
;
2447 if (options
->RelayBandwidthRate
) {
2448 relayrate
= (int)options
->RelayBandwidthRate
;
2449 relayburst
= (int)options
->RelayBandwidthBurst
;
2451 relayrate
= bandwidthrate
;
2452 relayburst
= bandwidthburst
;
2455 tor_assert(milliseconds_elapsed
>= 0);
2457 write_buckets_empty_last_second
=
2458 global_relayed_write_bucket
<= 0 || global_write_bucket
<= 0;
2460 /* refill the global buckets */
2461 connection_bucket_refill_helper(&global_read_bucket
,
2462 bandwidthrate
, bandwidthburst
,
2463 milliseconds_elapsed
,
2464 "global_read_bucket");
2465 connection_bucket_refill_helper(&global_write_bucket
,
2466 bandwidthrate
, bandwidthburst
,
2467 milliseconds_elapsed
,
2468 "global_write_bucket");
2469 connection_bucket_refill_helper(&global_relayed_read_bucket
,
2470 relayrate
, relayburst
,
2471 milliseconds_elapsed
,
2472 "global_relayed_read_bucket");
2473 connection_bucket_refill_helper(&global_relayed_write_bucket
,
2474 relayrate
, relayburst
,
2475 milliseconds_elapsed
,
2476 "global_relayed_write_bucket");
2478 /* refill the per-connection buckets */
2479 SMARTLIST_FOREACH_BEGIN(conns
, connection_t
*, conn
) {
2480 if (connection_speaks_cells(conn
)) {
2481 or_connection_t
*or_conn
= TO_OR_CONN(conn
);
2482 int orbandwidthrate
= or_conn
->bandwidthrate
;
2483 int orbandwidthburst
= or_conn
->bandwidthburst
;
2484 if (connection_bucket_should_increase(or_conn
->read_bucket
, or_conn
)) {
2485 connection_bucket_refill_helper(&or_conn
->read_bucket
,
2488 milliseconds_elapsed
,
2489 "or_conn->read_bucket");
2491 if (connection_bucket_should_increase(or_conn
->write_bucket
, or_conn
)) {
2492 connection_bucket_refill_helper(&or_conn
->write_bucket
,
2495 milliseconds_elapsed
,
2496 "or_conn->write_bucket");
2500 if (conn
->read_blocked_on_bw
== 1 /* marked to turn reading back on now */
2501 && global_read_bucket
> 0 /* and we're allowed to read */
2502 && (!connection_counts_as_relayed_traffic(conn
, now
) ||
2503 global_relayed_read_bucket
> 0) /* even if we're relayed traffic */
2504 && (!connection_speaks_cells(conn
) ||
2505 conn
->state
!= OR_CONN_STATE_OPEN
||
2506 TO_OR_CONN(conn
)->read_bucket
> 0)) {
2507 /* and either a non-cell conn or a cell conn with non-empty bucket */
2508 LOG_FN_CONN(conn
, (LOG_DEBUG
,LD_NET
,
2509 "waking up conn (fd %d) for read", (int)conn
->s
));
2510 conn
->read_blocked_on_bw
= 0;
2511 connection_start_reading(conn
);
2514 if (conn
->write_blocked_on_bw
== 1
2515 && global_write_bucket
> 0 /* and we're allowed to write */
2516 && (!connection_counts_as_relayed_traffic(conn
, now
) ||
2517 global_relayed_write_bucket
> 0) /* even if it's relayed traffic */
2518 && (!connection_speaks_cells(conn
) ||
2519 conn
->state
!= OR_CONN_STATE_OPEN
||
2520 TO_OR_CONN(conn
)->write_bucket
> 0)) {
2521 LOG_FN_CONN(conn
, (LOG_DEBUG
,LD_NET
,
2522 "waking up conn (fd %d) for write", (int)conn
->s
));
2523 conn
->write_blocked_on_bw
= 0;
2524 connection_start_writing(conn
);
2526 } SMARTLIST_FOREACH_END(conn
);
2529 /** Is the <b>bucket</b> for connection <b>conn</b> low enough that we
2530 * should add another pile of tokens to it?
2533 connection_bucket_should_increase(int bucket
, or_connection_t
*conn
)
2537 if (conn
->_base
.state
!= OR_CONN_STATE_OPEN
)
2538 return 0; /* only open connections play the rate limiting game */
2539 if (bucket
>= conn
->bandwidthburst
)
2546 connection_buckets_decrement(connection_t
*conn
, time_t now
,
2547 size_t num_read
, size_t num_written
)
2553 /* Libevent does this for us. */
2557 connection_bucket_refill(int seconds_elapsed
, time_t now
)
2559 (void) seconds_elapsed
;
2561 /* Libevent does this for us. */
2564 connection_bucket_init(void)
2566 const or_options_t
*options
= get_options();
2567 const struct timeval
*tick
= tor_libevent_get_one_tick_timeout();
2568 struct ev_token_bucket_cfg
*bucket_cfg
;
2570 uint64_t rate
, burst
;
2571 if (options
->RelayBandwidthRate
) {
2572 rate
= options
->RelayBandwidthRate
;
2573 burst
= options
->RelayBandwidthBurst
;
2575 rate
= options
->BandwidthRate
;
2576 burst
= options
->BandwidthBurst
;
2579 /* This can't overflow, since TokenBucketRefillInterval <= 1000,
2580 * and rate started out less than INT32_MAX. */
2581 rate
= (rate
* options
->TokenBucketRefillInterval
) / 1000;
2583 bucket_cfg
= ev_token_bucket_cfg_new((uint32_t)rate
, (uint32_t)burst
,
2584 (uint32_t)rate
, (uint32_t)burst
,
2587 if (!global_rate_limit
) {
2589 bufferevent_rate_limit_group_new(tor_libevent_get_base(), bucket_cfg
);
2591 bufferevent_rate_limit_group_set_cfg(global_rate_limit
, bucket_cfg
);
2593 ev_token_bucket_cfg_free(bucket_cfg
);
2597 connection_get_rate_limit_totals(uint64_t *read_out
, uint64_t *written_out
)
2599 if (global_rate_limit
== NULL
) {
2600 *read_out
= *written_out
= 0;
2602 bufferevent_rate_limit_group_get_totals(
2603 global_rate_limit
, read_out
, written_out
);
2607 /** Perform whatever operations are needed on <b>conn</b> to enable
2610 connection_enable_rate_limiting(connection_t
*conn
)
2613 if (!global_rate_limit
)
2614 connection_bucket_init();
2615 tor_add_bufferevent_to_rate_limit_group(conn
->bufev
, global_rate_limit
);
2620 connection_consider_empty_write_buckets(connection_t
*conn
)
2625 connection_consider_empty_read_buckets(connection_t
*conn
)
2631 /** Read bytes from conn-\>s and process them.
2633 * It calls connection_read_to_buf() to bring in any new bytes,
2634 * and then calls connection_process_inbuf() to process them.
2636 * Mark the connection and return -1 if you want to close it, else
2640 connection_handle_read_impl(connection_t
*conn
)
2642 ssize_t max_to_read
=-1, try_to_read
;
2643 size_t before
, n_read
= 0;
2644 int socket_error
= 0;
2646 if (conn
->marked_for_close
)
2647 return 0; /* do nothing */
2649 conn
->timestamp_lastread
= approx_time();
2651 switch (conn
->type
) {
2652 case CONN_TYPE_OR_LISTENER
:
2653 return connection_handle_listener_read(conn
, CONN_TYPE_OR
);
2654 case CONN_TYPE_AP_LISTENER
:
2655 case CONN_TYPE_AP_TRANS_LISTENER
:
2656 case CONN_TYPE_AP_NATD_LISTENER
:
2657 return connection_handle_listener_read(conn
, CONN_TYPE_AP
);
2658 case CONN_TYPE_DIR_LISTENER
:
2659 return connection_handle_listener_read(conn
, CONN_TYPE_DIR
);
2660 case CONN_TYPE_CONTROL_LISTENER
:
2661 return connection_handle_listener_read(conn
, CONN_TYPE_CONTROL
);
2662 case CONN_TYPE_AP_DNS_LISTENER
:
2663 /* This should never happen; eventdns.c handles the reads here. */
2664 tor_fragile_assert();
2669 try_to_read
= max_to_read
;
2670 tor_assert(!conn
->marked_for_close
);
2672 before
= buf_datalen(conn
->inbuf
);
2673 if (connection_read_to_buf(conn
, &max_to_read
, &socket_error
) < 0) {
2674 /* There's a read error; kill the connection.*/
2675 if (conn
->type
== CONN_TYPE_OR
&&
2676 conn
->state
== OR_CONN_STATE_CONNECTING
) {
2677 connection_or_connect_failed(TO_OR_CONN(conn
),
2678 errno_to_orconn_end_reason(socket_error
),
2679 tor_socket_strerror(socket_error
));
2681 if (CONN_IS_EDGE(conn
)) {
2682 edge_connection_t
*edge_conn
= TO_EDGE_CONN(conn
);
2683 connection_edge_end_errno(edge_conn
);
2684 if (conn
->type
== CONN_TYPE_AP
&& TO_ENTRY_CONN(conn
)->socks_request
) {
2685 /* broken, don't send a socks reply back */
2686 TO_ENTRY_CONN(conn
)->socks_request
->has_finished
= 1;
2689 connection_close_immediate(conn
); /* Don't flush; connection is dead. */
2690 connection_mark_for_close(conn
);
2693 n_read
+= buf_datalen(conn
->inbuf
) - before
;
2694 if (CONN_IS_EDGE(conn
) && try_to_read
!= max_to_read
) {
2695 /* instruct it not to try to package partial cells. */
2696 if (connection_process_inbuf(conn
, 0) < 0) {
2699 if (!conn
->marked_for_close
&&
2700 connection_is_reading(conn
) &&
2701 !conn
->inbuf_reached_eof
&&
2703 goto loop_again
; /* try reading again, in case more is here now */
2705 /* one last try, packaging partial cells and all. */
2706 if (!conn
->marked_for_close
&&
2707 connection_process_inbuf(conn
, 1) < 0) {
2710 if (conn
->linked_conn
) {
2711 /* The other side's handle_write() will never actually get called, so
2712 * we need to invoke the appropriate callbacks ourself. */
2713 connection_t
*linked
= conn
->linked_conn
;
2716 /* Probably a no-op, since linked conns typically don't count for
2717 * bandwidth rate limiting. But do it anyway so we can keep stats
2718 * accurately. Note that since we read the bytes from conn, and
2719 * we're writing the bytes onto the linked connection, we count
2720 * these as <i>written</i> bytes. */
2721 connection_buckets_decrement(linked
, approx_time(), 0, n_read
);
2723 if (connection_flushed_some(linked
) < 0)
2724 connection_mark_for_close(linked
);
2725 if (!connection_wants_to_flush(linked
))
2726 connection_finished_flushing(linked
);
2729 if (!buf_datalen(linked
->outbuf
) && conn
->active_on_link
)
2730 connection_stop_reading_from_linked_conn(conn
);
2732 /* If we hit the EOF, call connection_reached_eof(). */
2733 if (!conn
->marked_for_close
&&
2734 conn
->inbuf_reached_eof
&&
2735 connection_reached_eof(conn
) < 0) {
2741 /* DOCDOC connection_handle_read */
2743 connection_handle_read(connection_t
*conn
)
2747 tor_gettimeofday_cache_clear();
2748 res
= connection_handle_read_impl(conn
);
2752 /** Pull in new bytes from conn-\>s or conn-\>linked_conn onto conn-\>inbuf,
2753 * either directly or via TLS. Reduce the token buckets by the number of bytes
2756 * If *max_to_read is -1, then decide it ourselves, else go with the
2757 * value passed to us. When returning, if it's changed, subtract the
2758 * number of bytes we read from *max_to_read.
2760 * Return -1 if we want to break conn, else return 0.
2763 connection_read_to_buf(connection_t
*conn
, ssize_t
*max_to_read
,
2767 ssize_t at_most
= *max_to_read
;
2768 size_t slack_in_buf
, more_to_read
;
2769 size_t n_read
= 0, n_written
= 0;
2771 if (at_most
== -1) { /* we need to initialize it */
2772 /* how many bytes are we allowed to read? */
2773 at_most
= connection_bucket_read_limit(conn
, approx_time());
2776 slack_in_buf
= buf_slack(conn
->inbuf
);
2778 if ((size_t)at_most
> slack_in_buf
&& slack_in_buf
>= 1024) {
2779 more_to_read
= at_most
- slack_in_buf
;
2780 at_most
= slack_in_buf
;
2785 if (connection_speaks_cells(conn
) &&
2786 conn
->state
> OR_CONN_STATE_PROXY_HANDSHAKING
) {
2788 or_connection_t
*or_conn
= TO_OR_CONN(conn
);
2789 size_t initial_size
;
2790 if (conn
->state
== OR_CONN_STATE_TLS_HANDSHAKING
||
2791 conn
->state
== OR_CONN_STATE_TLS_CLIENT_RENEGOTIATING
) {
2792 /* continue handshaking even if global token bucket is empty */
2793 return connection_tls_continue_handshake(or_conn
);
2797 "%d: starting, inbuf_datalen %ld (%d pending in tls object)."
2799 (int)conn
->s
,(long)buf_datalen(conn
->inbuf
),
2800 tor_tls_get_pending_bytes(or_conn
->tls
), (long)at_most
);
2802 initial_size
= buf_datalen(conn
->inbuf
);
2803 /* else open, or closing */
2804 result
= read_to_buf_tls(or_conn
->tls
, at_most
, conn
->inbuf
);
2805 if (TOR_TLS_IS_ERROR(result
) || result
== TOR_TLS_CLOSE
)
2806 or_conn
->tls_error
= result
;
2808 or_conn
->tls_error
= 0;
2812 case TOR_TLS_ERROR_IO
:
2813 log_debug(LD_NET
,"TLS connection closed %son read. Closing. "
2814 "(Nickname %s, address %s)",
2815 result
== TOR_TLS_CLOSE
? "cleanly " : "",
2816 or_conn
->nickname
? or_conn
->nickname
: "not set",
2819 CASE_TOR_TLS_ERROR_ANY_NONIO
:
2820 log_debug(LD_NET
,"tls error [%s]. breaking (nickname %s, address %s).",
2821 tor_tls_err_to_string(result
),
2822 or_conn
->nickname
? or_conn
->nickname
: "not set",
2825 case TOR_TLS_WANTWRITE
:
2826 connection_start_writing(conn
);
2828 case TOR_TLS_WANTREAD
: /* we're already reading */
2829 case TOR_TLS_DONE
: /* no data read, so nothing to process */
2831 break; /* so we call bucket_decrement below */
2835 pending
= tor_tls_get_pending_bytes(or_conn
->tls
);
2837 /* If we have any pending bytes, we read them now. This *can*
2838 * take us over our read allotment, but really we shouldn't be
2839 * believing that SSL bytes are the same as TCP bytes anyway. */
2840 int r2
= read_to_buf_tls(or_conn
->tls
, pending
, conn
->inbuf
);
2842 log_warn(LD_BUG
, "apparently, reading pending bytes can fail.");
2846 result
= (int)(buf_datalen(conn
->inbuf
)-initial_size
);
2847 tor_tls_get_n_raw_bytes(or_conn
->tls
, &n_read
, &n_written
);
2848 log_debug(LD_GENERAL
, "After TLS read of %d: %ld read, %ld written",
2849 result
, (long)n_read
, (long)n_written
);
2850 } else if (conn
->linked
) {
2851 if (conn
->linked_conn
) {
2852 result
= move_buf_to_buf(conn
->inbuf
, conn
->linked_conn
->outbuf
,
2853 &conn
->linked_conn
->outbuf_flushlen
);
2857 //log_notice(LD_GENERAL, "Moved %d bytes on an internal link!", result);
2858 /* If the other side has disappeared, or if it's been marked for close and
2859 * we flushed its outbuf, then we should set our inbuf_reached_eof. */
2860 if (!conn
->linked_conn
||
2861 (conn
->linked_conn
->marked_for_close
&&
2862 buf_datalen(conn
->linked_conn
->outbuf
) == 0))
2863 conn
->inbuf_reached_eof
= 1;
2865 n_read
= (size_t) result
;
2867 /* !connection_speaks_cells, !conn->linked_conn. */
2868 int reached_eof
= 0;
2869 CONN_LOG_PROTECT(conn
,
2870 result
= read_to_buf(conn
->s
, at_most
, conn
->inbuf
, &reached_eof
,
2873 conn
->inbuf_reached_eof
= 1;
2875 // log_fn(LOG_DEBUG,"read_to_buf returned %d.",read_result);
2879 n_read
= (size_t) result
;
2883 /* change *max_to_read */
2884 *max_to_read
= at_most
- n_read
;
2886 /* Update edge_conn->n_read */
2887 if (conn
->type
== CONN_TYPE_AP
) {
2888 edge_connection_t
*edge_conn
= TO_EDGE_CONN(conn
);
2889 /* Check for overflow: */
2890 if (PREDICT_LIKELY(UINT32_MAX
- edge_conn
->n_read
> n_read
))
2891 edge_conn
->n_read
+= (int)n_read
;
2893 edge_conn
->n_read
= UINT32_MAX
;
2897 connection_buckets_decrement(conn
, approx_time(), n_read
, n_written
);
2899 if (more_to_read
&& result
== at_most
) {
2900 slack_in_buf
= buf_slack(conn
->inbuf
);
2901 at_most
= more_to_read
;
2905 /* Call even if result is 0, since the global read bucket may
2906 * have reached 0 on a different conn, and this guy needs to
2907 * know to stop reading. */
2908 connection_consider_empty_read_buckets(conn
);
2909 if (n_written
> 0 && connection_is_writing(conn
))
2910 connection_consider_empty_write_buckets(conn
);
2915 #ifdef USE_BUFFEREVENTS
2916 /* XXXX These generic versions could be simplified by making them
2919 /** Callback: Invoked whenever bytes are added to or drained from an input
2920 * evbuffer. Used to track the number of bytes read. */
2922 evbuffer_inbuf_callback(struct evbuffer
*buf
,
2923 const struct evbuffer_cb_info
*info
, void *arg
)
2925 connection_t
*conn
= arg
;
2927 /* XXXX These need to get real counts on the non-nested TLS case. - NM */
2928 if (info
->n_added
) {
2929 time_t now
= approx_time();
2930 conn
->timestamp_lastread
= now
;
2931 record_num_bytes_transferred(conn
, now
, info
->n_added
, 0);
2932 connection_consider_empty_read_buckets(conn
);
2933 if (conn
->type
== CONN_TYPE_AP
) {
2934 edge_connection_t
*edge_conn
= TO_EDGE_CONN(conn
);
2935 /*XXXX024 check for overflow*/
2936 edge_conn
->n_read
+= (int)info
->n_added
;
2941 /** Callback: Invoked whenever bytes are added to or drained from an output
2942 * evbuffer. Used to track the number of bytes written. */
2944 evbuffer_outbuf_callback(struct evbuffer
*buf
,
2945 const struct evbuffer_cb_info
*info
, void *arg
)
2947 connection_t
*conn
= arg
;
2949 if (info
->n_deleted
) {
2950 time_t now
= approx_time();
2951 conn
->timestamp_lastwritten
= now
;
2952 record_num_bytes_transferred(conn
, now
, 0, info
->n_deleted
);
2953 connection_consider_empty_write_buckets(conn
);
2954 if (conn
->type
== CONN_TYPE_AP
) {
2955 edge_connection_t
*edge_conn
= TO_EDGE_CONN(conn
);
2956 /*XXXX024 check for overflow*/
2957 edge_conn
->n_written
+= (int)info
->n_deleted
;
2962 /** Callback: invoked whenever a bufferevent has read data. */
2964 connection_handle_read_cb(struct bufferevent
*bufev
, void *arg
)
2966 connection_t
*conn
= arg
;
2968 if (!conn
->marked_for_close
) {
2969 if (connection_process_inbuf(conn
, 1)<0) /* XXXX Always 1? */
2970 if (!conn
->marked_for_close
)
2971 connection_mark_for_close(conn
);
2975 /** Callback: invoked whenever a bufferevent has written data. */
2977 connection_handle_write_cb(struct bufferevent
*bufev
, void *arg
)
2979 connection_t
*conn
= arg
;
2980 struct evbuffer
*output
;
2981 if (connection_flushed_some(conn
)<0) {
2982 if (!conn
->marked_for_close
)
2983 connection_mark_for_close(conn
);
2987 output
= bufferevent_get_output(bufev
);
2988 if (!evbuffer_get_length(output
)) {
2989 connection_finished_flushing(conn
);
2990 if (conn
->marked_for_close
&& conn
->hold_open_until_flushed
) {
2991 conn
->hold_open_until_flushed
= 0;
2994 bufferevent_flush(conn
->bufev
, EV_WRITE
, BEV_FINISHED
);
3000 /** Callback: invoked whenever a bufferevent has had an event (like a
3001 * connection, or an eof, or an error) occur. */
3003 connection_handle_event_cb(struct bufferevent
*bufev
, short event
, void *arg
)
3005 connection_t
*conn
= arg
;
3007 if (conn
->marked_for_close
)
3010 if (event
& BEV_EVENT_CONNECTED
) {
3011 tor_assert(connection_state_is_connecting(conn
));
3012 if (connection_finished_connecting(conn
)<0)
3015 if (event
& BEV_EVENT_EOF
) {
3016 if (!conn
->marked_for_close
) {
3017 conn
->inbuf_reached_eof
= 1;
3018 if (connection_reached_eof(conn
)<0)
3022 if (event
& BEV_EVENT_ERROR
) {
3023 int socket_error
= evutil_socket_geterror(conn
->s
);
3024 if (conn
->type
== CONN_TYPE_OR
&&
3025 conn
->state
== OR_CONN_STATE_CONNECTING
) {
3026 connection_or_connect_failed(TO_OR_CONN(conn
),
3027 errno_to_orconn_end_reason(socket_error
),
3028 tor_socket_strerror(socket_error
));
3029 } else if (CONN_IS_EDGE(conn
)) {
3030 edge_connection_t
*edge_conn
= TO_EDGE_CONN(conn
);
3031 if (!edge_conn
->edge_has_sent_end
)
3032 connection_edge_end_errno(edge_conn
);
3033 if (conn
->type
== CONN_TYPE_AP
&& TO_ENTRY_CONN(conn
)->socks_request
) {
3034 /* broken, don't send a socks reply back */
3035 TO_ENTRY_CONN(conn
)->socks_request
->has_finished
= 1;
3038 connection_close_immediate(conn
); /* Connection is dead. */
3039 if (!conn
->marked_for_close
)
3040 connection_mark_for_close(conn
);
3044 /** Set up the generic callbacks for the bufferevent on <b>conn</b>. */
3046 connection_configure_bufferevent_callbacks(connection_t
*conn
)
3048 struct bufferevent
*bufev
;
3049 struct evbuffer
*input
, *output
;
3050 tor_assert(conn
->bufev
);
3051 bufev
= conn
->bufev
;
3052 bufferevent_setcb(bufev
,
3053 connection_handle_read_cb
,
3054 connection_handle_write_cb
,
3055 connection_handle_event_cb
,
3057 /* Set a fairly high write low-watermark so that we get the write callback
3058 called whenever data is written to bring us under 128K. Leave the
3059 high-watermark at 0.
3061 bufferevent_setwatermark(bufev
, EV_WRITE
, 128*1024, 0);
3063 input
= bufferevent_get_input(bufev
);
3064 output
= bufferevent_get_output(bufev
);
3065 evbuffer_add_cb(input
, evbuffer_inbuf_callback
, conn
);
3066 evbuffer_add_cb(output
, evbuffer_outbuf_callback
, conn
);
3070 /** A pass-through to fetch_from_buf. */
3072 connection_fetch_from_buf(char *string
, size_t len
, connection_t
*conn
)
3074 IF_HAS_BUFFEREVENT(conn
, {
3075 /* XXX overflow -seb */
3076 return (int)bufferevent_read(conn
->bufev
, string
, len
);
3077 }) ELSE_IF_NO_BUFFEREVENT
{
3078 return fetch_from_buf(string
, len
, conn
->inbuf
);
3082 /** As fetch_from_buf_line(), but read from a connection's input buffer. */
3084 connection_fetch_from_buf_line(connection_t
*conn
, char *data
,
3087 IF_HAS_BUFFEREVENT(conn
, {
3090 struct evbuffer
*input
= bufferevent_get_input(conn
->bufev
);
3091 struct evbuffer_ptr ptr
=
3092 evbuffer_search_eol(input
, NULL
, &eol_len
, EVBUFFER_EOL_LF
);
3094 return 0; /* No EOL found. */
3095 if ((size_t)ptr
.pos
+eol_len
>= *data_len
) {
3096 return -1; /* Too long */
3098 *data_len
= ptr
.pos
+eol_len
;
3099 r
= evbuffer_remove(input
, data
, ptr
.pos
+eol_len
);
3101 data
[ptr
.pos
+eol_len
] = '\0';
3103 }) ELSE_IF_NO_BUFFEREVENT
{
3104 return fetch_from_buf_line(conn
->inbuf
, data
, data_len
);
3108 /** As fetch_from_buf_http, but fetches from a conncetion's input buffer_t or
3109 * its bufferevent as appropriate. */
3111 connection_fetch_from_buf_http(connection_t
*conn
,
3112 char **headers_out
, size_t max_headerlen
,
3113 char **body_out
, size_t *body_used
,
3114 size_t max_bodylen
, int force_complete
)
3116 IF_HAS_BUFFEREVENT(conn
, {
3117 struct evbuffer
*input
= bufferevent_get_input(conn
->bufev
);
3118 return fetch_from_evbuffer_http(input
, headers_out
, max_headerlen
,
3119 body_out
, body_used
, max_bodylen
, force_complete
);
3120 }) ELSE_IF_NO_BUFFEREVENT
{
3121 return fetch_from_buf_http(conn
->inbuf
, headers_out
, max_headerlen
,
3122 body_out
, body_used
, max_bodylen
, force_complete
);
3126 /** Return conn-\>outbuf_flushlen: how many bytes conn wants to flush
3127 * from its outbuf. */
3129 connection_wants_to_flush(connection_t
*conn
)
3131 return conn
->outbuf_flushlen
> 0;
3134 /** Are there too many bytes on edge connection <b>conn</b>'s outbuf to
3135 * send back a relay-level sendme yet? Return 1 if so, 0 if not. Used by
3136 * connection_edge_consider_sending_sendme().
3139 connection_outbuf_too_full(connection_t
*conn
)
3141 return (conn
->outbuf_flushlen
> 10*CELL_PAYLOAD_SIZE
);
3144 /** Try to flush more bytes onto <b>conn</b>-\>s.
3146 * This function gets called either from conn_write() in main.c
3147 * when poll() has declared that conn wants to write, or below
3148 * from connection_write_to_buf() when an entire TLS record is ready.
3150 * Update <b>conn</b>-\>timestamp_lastwritten to now, and call flush_buf
3151 * or flush_buf_tls appropriately. If it succeeds and there are no more
3152 * more bytes on <b>conn</b>-\>outbuf, then call connection_finished_flushing
3155 * If <b>force</b>, then write as many bytes as possible, ignoring bandwidth
3156 * limits. (Used for flushing messages to controller connections on fatal
3159 * Mark the connection and return -1 if you want to close it, else
3163 connection_handle_write_impl(connection_t
*conn
, int force
)
3166 socklen_t len
=(socklen_t
)sizeof(e
);
3168 ssize_t max_to_write
;
3169 time_t now
= approx_time();
3170 size_t n_read
= 0, n_written
= 0;
3172 tor_assert(!connection_is_listener(conn
));
3174 if (conn
->marked_for_close
|| !SOCKET_OK(conn
->s
))
3175 return 0; /* do nothing */
3177 if (conn
->in_flushed_some
) {
3178 log_warn(LD_BUG
, "called recursively from inside conn->in_flushed_some");
3182 conn
->timestamp_lastwritten
= now
;
3184 /* Sometimes, "writable" means "connected". */
3185 if (connection_state_is_connecting(conn
)) {
3186 if (getsockopt(conn
->s
, SOL_SOCKET
, SO_ERROR
, (void*)&e
, &len
) < 0) {
3187 log_warn(LD_BUG
, "getsockopt() syscall failed");
3188 if (CONN_IS_EDGE(conn
))
3189 connection_edge_end_errno(TO_EDGE_CONN(conn
));
3190 connection_mark_for_close(conn
);
3194 /* some sort of error, but maybe just inprogress still */
3195 if (!ERRNO_IS_CONN_EINPROGRESS(e
)) {
3196 log_info(LD_NET
,"in-progress connect failed. Removing. (%s)",
3197 tor_socket_strerror(e
));
3198 if (CONN_IS_EDGE(conn
))
3199 connection_edge_end_errno(TO_EDGE_CONN(conn
));
3200 if (conn
->type
== CONN_TYPE_OR
)
3201 connection_or_connect_failed(TO_OR_CONN(conn
),
3202 errno_to_orconn_end_reason(e
),
3203 tor_socket_strerror(e
));
3205 connection_close_immediate(conn
);
3206 connection_mark_for_close(conn
);
3209 return 0; /* no change, see if next time is better */
3212 /* The connection is successful. */
3213 if (connection_finished_connecting(conn
)<0)
3217 max_to_write
= force
? (ssize_t
)conn
->outbuf_flushlen
3218 : connection_bucket_write_limit(conn
, now
);
3220 if (connection_speaks_cells(conn
) &&
3221 conn
->state
> OR_CONN_STATE_PROXY_HANDSHAKING
) {
3222 or_connection_t
*or_conn
= TO_OR_CONN(conn
);
3223 if (conn
->state
== OR_CONN_STATE_TLS_HANDSHAKING
||
3224 conn
->state
== OR_CONN_STATE_TLS_CLIENT_RENEGOTIATING
) {
3225 connection_stop_writing(conn
);
3226 if (connection_tls_continue_handshake(or_conn
) < 0) {
3227 /* Don't flush; connection is dead. */
3228 connection_close_immediate(conn
);
3229 connection_mark_for_close(conn
);
3233 } else if (conn
->state
== OR_CONN_STATE_TLS_SERVER_RENEGOTIATING
) {
3234 return connection_handle_read(conn
);
3237 /* else open, or closing */
3238 result
= flush_buf_tls(or_conn
->tls
, conn
->outbuf
,
3239 max_to_write
, &conn
->outbuf_flushlen
);
3241 /* If we just flushed the last bytes, check if this tunneled dir
3242 * request is done. */
3243 /* XXXX move this to flushed_some or finished_flushing -NM */
3244 if (buf_datalen(conn
->outbuf
) == 0 && conn
->dirreq_id
)
3245 geoip_change_dirreq_state(conn
->dirreq_id
, DIRREQ_TUNNELED
,
3246 DIRREQ_OR_CONN_BUFFER_FLUSHED
);
3249 CASE_TOR_TLS_ERROR_ANY
:
3251 log_info(LD_NET
,result
!=TOR_TLS_CLOSE
?
3252 "tls error. breaking.":"TLS connection closed on flush");
3253 /* Don't flush; connection is dead. */
3254 connection_close_immediate(conn
);
3255 connection_mark_for_close(conn
);
3257 case TOR_TLS_WANTWRITE
:
3258 log_debug(LD_NET
,"wanted write.");
3259 /* we're already writing */
3261 case TOR_TLS_WANTREAD
:
3262 /* Make sure to avoid a loop if the receive buckets are empty. */
3263 log_debug(LD_NET
,"wanted read.");
3264 if (!connection_is_reading(conn
)) {
3265 connection_stop_writing(conn
);
3266 conn
->write_blocked_on_bw
= 1;
3267 /* we'll start reading again when we get more tokens in our
3268 * read bucket; then we'll start writing again too.
3271 /* else no problem, we're already reading */
3273 /* case TOR_TLS_DONE:
3274 * for TOR_TLS_DONE, fall through to check if the flushlen
3275 * is empty, so we can stop writing.
3279 tor_tls_get_n_raw_bytes(or_conn
->tls
, &n_read
, &n_written
);
3280 log_debug(LD_GENERAL
, "After TLS write of %d: %ld read, %ld written",
3281 result
, (long)n_read
, (long)n_written
);
3283 CONN_LOG_PROTECT(conn
,
3284 result
= flush_buf(conn
->s
, conn
->outbuf
,
3285 max_to_write
, &conn
->outbuf_flushlen
));
3287 if (CONN_IS_EDGE(conn
))
3288 connection_edge_end_errno(TO_EDGE_CONN(conn
));
3290 connection_close_immediate(conn
); /* Don't flush; connection is dead. */
3291 connection_mark_for_close(conn
);
3294 n_written
= (size_t) result
;
3297 if (n_written
&& conn
->type
== CONN_TYPE_AP
) {
3298 edge_connection_t
*edge_conn
= TO_EDGE_CONN(conn
);
3300 /* Check for overflow: */
3301 if (PREDICT_LIKELY(UINT32_MAX
- edge_conn
->n_written
> n_written
))
3302 edge_conn
->n_written
+= (int)n_written
;
3304 edge_conn
->n_written
= UINT32_MAX
;
3307 connection_buckets_decrement(conn
, approx_time(), n_read
, n_written
);
3310 /* If we wrote any bytes from our buffer, then call the appropriate
3312 if (connection_flushed_some(conn
) < 0)
3313 connection_mark_for_close(conn
);
3316 if (!connection_wants_to_flush(conn
)) { /* it's done flushing */
3317 if (connection_finished_flushing(conn
) < 0) {
3318 /* already marked */
3324 /* Call even if result is 0, since the global write bucket may
3325 * have reached 0 on a different conn, and this guy needs to
3326 * know to stop writing. */
3327 connection_consider_empty_write_buckets(conn
);
3328 if (n_read
> 0 && connection_is_reading(conn
))
3329 connection_consider_empty_read_buckets(conn
);
3334 /* DOCDOC connection_handle_write */
3336 connection_handle_write(connection_t
*conn
, int force
)
3339 tor_gettimeofday_cache_clear();
3340 res
= connection_handle_write_impl(conn
, force
);
3345 * Try to flush data that's waiting for a write on <b>conn</b>. Return
3346 * -1 on failure, 0 on success.
3348 * Don't use this function for regular writing; the buffers/bufferevents
3349 * system should be good enough at scheduling writes there. Instead, this
3350 * function is for cases when we're about to exit or something and we want
3351 * to report it right away.
3354 connection_flush(connection_t
*conn
)
3356 IF_HAS_BUFFEREVENT(conn
, {
3357 int r
= bufferevent_flush(conn
->bufev
, EV_WRITE
, BEV_FLUSH
);
3358 return (r
< 0) ? -1 : 0;
3360 return connection_handle_write(conn
, 1);
3363 /** OpenSSL TLS record size is 16383; this is close. The goal here is to
3364 * push data out as soon as we know there's enough for a TLS record, so
3365 * during periods of high load we won't read entire megabytes from
3366 * input before pushing any data out. It also has the feature of not
3367 * growing huge outbufs unless something is slow. */
3368 #define MIN_TLS_FLUSHLEN 15872
3370 /** Append <b>len</b> bytes of <b>string</b> onto <b>conn</b>'s
3371 * outbuf, and ask it to start writing.
3373 * If <b>zlib</b> is nonzero, this is a directory connection that should get
3374 * its contents compressed or decompressed as they're written. If zlib is
3375 * negative, this is the last data to be compressed, and the connection's zlib
3376 * state should be flushed.
3378 * If it's an OR conn and an entire TLS record is ready, then try to
3379 * flush the record now. Similarly, if it's a local control connection
3380 * and a 64k chunk is ready, try to flush it all, so we don't end up with
3381 * many megabytes of controller info queued at once.
3384 _connection_write_to_buf_impl(const char *string
, size_t len
,
3385 connection_t
*conn
, int zlib
)
3387 /* XXXX This function really needs to return -1 on failure. */
3390 if (!len
&& !(zlib
<0))
3392 /* if it's marked for close, only allow write if we mean to flush it */
3393 if (conn
->marked_for_close
&& !conn
->hold_open_until_flushed
)
3396 IF_HAS_BUFFEREVENT(conn
, {
3398 int done
= zlib
< 0;
3399 r
= write_to_evbuffer_zlib(bufferevent_get_output(conn
->bufev
),
3400 TO_DIR_CONN(conn
)->zlib_state
,
3403 r
= bufferevent_write(conn
->bufev
, string
, len
);
3406 /* XXXX mark for close? */
3407 log_warn(LD_NET
, "bufferevent_write failed! That shouldn't happen.");
3412 old_datalen
= buf_datalen(conn
->outbuf
);
3414 dir_connection_t
*dir_conn
= TO_DIR_CONN(conn
);
3415 int done
= zlib
< 0;
3416 CONN_LOG_PROTECT(conn
, r
= write_to_buf_zlib(conn
->outbuf
,
3417 dir_conn
->zlib_state
,
3418 string
, len
, done
));
3420 CONN_LOG_PROTECT(conn
, r
= write_to_buf(string
, len
, conn
->outbuf
));
3423 if (CONN_IS_EDGE(conn
)) {
3424 /* if it failed, it means we have our package/delivery windows set
3425 wrong compared to our max outbuf size. close the whole circuit. */
3427 "write_to_buf failed. Closing circuit (fd %d).", (int)conn
->s
);
3428 circuit_mark_for_close(circuit_get_by_edge_conn(TO_EDGE_CONN(conn
)),
3429 END_CIRC_REASON_INTERNAL
);
3432 "write_to_buf failed. Closing connection (fd %d).",
3434 connection_mark_for_close(conn
);
3439 /* If we receive optimistic data in the EXIT_CONN_STATE_RESOLVING
3440 * state, we don't want to try to write it right away, since
3441 * conn->write_event won't be set yet. Otherwise, write data from
3442 * this conn as the socket is available. */
3443 if (conn
->write_event
) {
3444 connection_start_writing(conn
);
3447 conn
->outbuf_flushlen
+= buf_datalen(conn
->outbuf
) - old_datalen
;
3450 conn
->outbuf_flushlen
+= len
;
3452 /* Should we try flushing the outbuf now? */
3453 if (conn
->in_flushed_some
) {
3454 /* Don't flush the outbuf when the reason we're writing more stuff is
3455 * _because_ we flushed the outbuf. That's unfair. */
3459 if (conn
->type
== CONN_TYPE_OR
&&
3460 conn
->outbuf_flushlen
-len
< MIN_TLS_FLUSHLEN
&&
3461 conn
->outbuf_flushlen
>= MIN_TLS_FLUSHLEN
) {
3462 /* We just pushed outbuf_flushlen to MIN_TLS_FLUSHLEN or above;
3463 * we can send out a full TLS frame now if we like. */
3464 extra
= conn
->outbuf_flushlen
- MIN_TLS_FLUSHLEN
;
3465 conn
->outbuf_flushlen
= MIN_TLS_FLUSHLEN
;
3466 } else if (conn
->type
== CONN_TYPE_CONTROL
&&
3467 !connection_is_rate_limited(conn
) &&
3468 conn
->outbuf_flushlen
-len
< 1<<16 &&
3469 conn
->outbuf_flushlen
>= 1<<16) {
3470 /* just try to flush all of it */
3472 return; /* no need to try flushing */
3474 if (connection_handle_write(conn
, 0) < 0) {
3475 if (!conn
->marked_for_close
) {
3476 /* this connection is broken. remove it. */
3477 log_warn(LD_BUG
, "unhandled error on write for "
3478 "conn (type %d, fd %d); removing",
3479 conn
->type
, (int)conn
->s
);
3480 tor_fragile_assert();
3481 /* do a close-immediate here, so we don't try to flush */
3482 connection_close_immediate(conn
);
3487 conn
->outbuf_flushlen
+= extra
;
3488 connection_start_writing(conn
);
3493 /** Return a connection with given type, address, port, and purpose;
3494 * or NULL if no such connection exists. */
3496 connection_get_by_type_addr_port_purpose(int type
,
3497 const tor_addr_t
*addr
, uint16_t port
,
3500 smartlist_t
*conns
= get_connection_array();
3501 SMARTLIST_FOREACH(conns
, connection_t
*, conn
,
3503 if (conn
->type
== type
&&
3504 tor_addr_eq(&conn
->addr
, addr
) &&
3505 conn
->port
== port
&&
3506 conn
->purpose
== purpose
&&
3507 !conn
->marked_for_close
)
3513 /** Return the stream with id <b>id</b> if it is not already marked for
3517 connection_get_by_global_id(uint64_t id
)
3519 smartlist_t
*conns
= get_connection_array();
3520 SMARTLIST_FOREACH(conns
, connection_t
*, conn
,
3522 if (conn
->global_identifier
== id
)
3528 /** Return a connection of type <b>type</b> that is not marked for close.
3531 connection_get_by_type(int type
)
3533 smartlist_t
*conns
= get_connection_array();
3534 SMARTLIST_FOREACH(conns
, connection_t
*, conn
,
3536 if (conn
->type
== type
&& !conn
->marked_for_close
)
3542 /** Return a connection of type <b>type</b> that is in state <b>state</b>,
3543 * and that is not marked for close.
3546 connection_get_by_type_state(int type
, int state
)
3548 smartlist_t
*conns
= get_connection_array();
3549 SMARTLIST_FOREACH(conns
, connection_t
*, conn
,
3551 if (conn
->type
== type
&& conn
->state
== state
&& !conn
->marked_for_close
)
3557 /** Return a connection of type <b>type</b> that has rendquery equal
3558 * to <b>rendquery</b>, and that is not marked for close. If state
3559 * is non-zero, conn must be of that state too.
3562 connection_get_by_type_state_rendquery(int type
, int state
,
3563 const char *rendquery
)
3565 smartlist_t
*conns
= get_connection_array();
3567 tor_assert(type
== CONN_TYPE_DIR
||
3568 type
== CONN_TYPE_AP
|| type
== CONN_TYPE_EXIT
);
3569 tor_assert(rendquery
);
3571 SMARTLIST_FOREACH_BEGIN(conns
, connection_t
*, conn
) {
3572 if (conn
->type
== type
&&
3573 !conn
->marked_for_close
&&
3574 (!state
|| state
== conn
->state
)) {
3575 if (type
== CONN_TYPE_DIR
&&
3576 TO_DIR_CONN(conn
)->rend_data
&&
3577 !rend_cmp_service_ids(rendquery
,
3578 TO_DIR_CONN(conn
)->rend_data
->onion_address
))
3580 else if (CONN_IS_EDGE(conn
) &&
3581 TO_EDGE_CONN(conn
)->rend_data
&&
3582 !rend_cmp_service_ids(rendquery
,
3583 TO_EDGE_CONN(conn
)->rend_data
->onion_address
))
3586 } SMARTLIST_FOREACH_END(conn
);
3590 /** Return a directory connection (if any one exists) that is fetching
3591 * the item described by <b>state</b>/<b>resource</b> */
3593 connection_dir_get_by_purpose_and_resource(int purpose
,
3594 const char *resource
)
3596 smartlist_t
*conns
= get_connection_array();
3598 SMARTLIST_FOREACH_BEGIN(conns
, connection_t
*, conn
) {
3599 dir_connection_t
*dirconn
;
3600 if (conn
->type
!= CONN_TYPE_DIR
|| conn
->marked_for_close
||
3601 conn
->purpose
!= purpose
)
3603 dirconn
= TO_DIR_CONN(conn
);
3604 if (dirconn
->requested_resource
== NULL
) {
3605 if (resource
== NULL
)
3607 } else if (resource
) {
3608 if (0 == strcmp(resource
, dirconn
->requested_resource
))
3611 } SMARTLIST_FOREACH_END(conn
);
3616 /** Return an open, non-marked connection of a given type and purpose, or NULL
3617 * if no such connection exists. */
3619 connection_get_by_type_purpose(int type
, int purpose
)
3621 smartlist_t
*conns
= get_connection_array();
3622 SMARTLIST_FOREACH(conns
, connection_t
*, conn
,
3624 if (conn
->type
== type
&&
3625 !conn
->marked_for_close
&&
3626 (purpose
== conn
->purpose
))
3632 /** Return 1 if <b>conn</b> is a listener conn, else return 0. */
3634 connection_is_listener(connection_t
*conn
)
3636 if (conn
->type
== CONN_TYPE_OR_LISTENER
||
3637 conn
->type
== CONN_TYPE_AP_LISTENER
||
3638 conn
->type
== CONN_TYPE_AP_TRANS_LISTENER
||
3639 conn
->type
== CONN_TYPE_AP_DNS_LISTENER
||
3640 conn
->type
== CONN_TYPE_AP_NATD_LISTENER
||
3641 conn
->type
== CONN_TYPE_DIR_LISTENER
||
3642 conn
->type
== CONN_TYPE_CONTROL_LISTENER
)
3647 /** Return 1 if <b>conn</b> is in state "open" and is not marked
3648 * for close, else return 0.
3651 connection_state_is_open(connection_t
*conn
)
3655 if (conn
->marked_for_close
)
3658 if ((conn
->type
== CONN_TYPE_OR
&& conn
->state
== OR_CONN_STATE_OPEN
) ||
3659 (conn
->type
== CONN_TYPE_AP
&& conn
->state
== AP_CONN_STATE_OPEN
) ||
3660 (conn
->type
== CONN_TYPE_EXIT
&& conn
->state
== EXIT_CONN_STATE_OPEN
) ||
3661 (conn
->type
== CONN_TYPE_CONTROL
&&
3662 conn
->state
== CONTROL_CONN_STATE_OPEN
))
3668 /** Return 1 if conn is in 'connecting' state, else return 0. */
3670 connection_state_is_connecting(connection_t
*conn
)
3674 if (conn
->marked_for_close
)
3679 return conn
->state
== OR_CONN_STATE_CONNECTING
;
3680 case CONN_TYPE_EXIT
:
3681 return conn
->state
== EXIT_CONN_STATE_CONNECTING
;
3683 return conn
->state
== DIR_CONN_STATE_CONNECTING
;
3689 /** Allocates a base64'ed authenticator for use in http or https
3690 * auth, based on the input string <b>authenticator</b>. Returns it
3691 * if success, else returns NULL. */
3693 alloc_http_authenticator(const char *authenticator
)
3695 /* an authenticator in Basic authentication
3696 * is just the string "username:password" */
3697 const size_t authenticator_length
= strlen(authenticator
);
3698 /* The base64_encode function needs a minimum buffer length
3700 const size_t base64_authenticator_length
= (authenticator_length
/48+1)*66;
3701 char *base64_authenticator
= tor_malloc(base64_authenticator_length
);
3702 if (base64_encode(base64_authenticator
, base64_authenticator_length
,
3703 authenticator
, authenticator_length
) < 0) {
3704 tor_free(base64_authenticator
); /* free and set to null */
3707 ssize_t len
= strlen(base64_authenticator
);
3709 /* remove all newline occurrences within the string */
3710 for (i
=0; i
< len
; ++i
) {
3711 if ('\n' != base64_authenticator
[i
]) {
3712 base64_authenticator
[j
] = base64_authenticator
[i
];
3716 base64_authenticator
[j
]='\0';
3718 return base64_authenticator
;
3721 /** Given a socket handle, check whether the local address (sockname) of the
3722 * socket is one that we've connected from before. If so, double-check
3723 * whether our address has changed and we need to generate keys. If we do,
3727 client_check_address_changed(tor_socket_t sock
)
3729 struct sockaddr_storage out_sockaddr
;
3730 socklen_t out_addr_len
= (socklen_t
) sizeof(out_sockaddr
);
3731 tor_addr_t out_addr
, iface_addr
;
3732 tor_addr_t
**last_interface_ip_ptr
;
3735 if (!outgoing_addrs
)
3736 outgoing_addrs
= smartlist_new();
3738 if (getsockname(sock
, (struct sockaddr
*)&out_sockaddr
, &out_addr_len
)<0) {
3739 int e
= tor_socket_errno(sock
);
3740 log_warn(LD_NET
, "getsockname() to check for address change failed: %s",
3741 tor_socket_strerror(e
));
3744 tor_addr_from_sockaddr(&out_addr
, (struct sockaddr
*)&out_sockaddr
, NULL
);
3745 family
= tor_addr_family(&out_addr
);
3747 if (family
== AF_INET
)
3748 last_interface_ip_ptr
= &last_interface_ipv4
;
3749 else if (family
== AF_INET6
)
3750 last_interface_ip_ptr
= &last_interface_ipv6
;
3754 if (! *last_interface_ip_ptr
) {
3755 tor_addr_t
*a
= tor_malloc_zero(sizeof(tor_addr_t
));
3756 if (get_interface_address6(LOG_INFO
, family
, a
)==0) {
3757 *last_interface_ip_ptr
= a
;
3763 /* If we've used this address previously, we're okay. */
3764 SMARTLIST_FOREACH(outgoing_addrs
, const tor_addr_t
*, a_ptr
,
3765 if (tor_addr_eq(a_ptr
, &out_addr
))
3769 /* Uh-oh. We haven't connected from this address before. Has the interface
3770 * address changed? */
3771 if (get_interface_address6(LOG_INFO
, family
, &iface_addr
)<0)
3774 if (tor_addr_eq(&iface_addr
, *last_interface_ip_ptr
)) {
3775 /* Nope, it hasn't changed. Add this address to the list. */
3776 smartlist_add(outgoing_addrs
, tor_memdup(&out_addr
, sizeof(tor_addr_t
)));
3778 /* The interface changed. We're a client, so we need to regenerate our
3779 * keys. First, reset the state. */
3780 log(LOG_NOTICE
, LD_NET
, "Our IP address has changed. Rotating keys...");
3781 tor_addr_copy(*last_interface_ip_ptr
, &iface_addr
);
3782 SMARTLIST_FOREACH(outgoing_addrs
, tor_addr_t
*, a_ptr
, tor_free(a_ptr
));
3783 smartlist_clear(outgoing_addrs
);
3784 smartlist_add(outgoing_addrs
, tor_memdup(&out_addr
, sizeof(tor_addr_t
)));
3785 /* Okay, now change our keys. */
3786 ip_address_changed(1);
3790 /** Some systems have limited system buffers for recv and xmit on
3791 * sockets allocated in a virtual server or similar environment. For a Tor
3792 * server this can produce the "Error creating network socket: No buffer
3793 * space available" error once all available TCP buffer space is consumed.
3794 * This method will attempt to constrain the buffers allocated for the socket
3795 * to the desired size to stay below system TCP buffer limits.
3798 set_constrained_socket_buffers(tor_socket_t sock
, int size
)
3800 void *sz
= (void*)&size
;
3801 socklen_t sz_sz
= (socklen_t
) sizeof(size
);
3802 if (setsockopt(sock
, SOL_SOCKET
, SO_SNDBUF
, sz
, sz_sz
) < 0) {
3803 int e
= tor_socket_errno(sock
);
3804 log_warn(LD_NET
, "setsockopt() to constrain send "
3805 "buffer to %d bytes failed: %s", size
, tor_socket_strerror(e
));
3807 if (setsockopt(sock
, SOL_SOCKET
, SO_RCVBUF
, sz
, sz_sz
) < 0) {
3808 int e
= tor_socket_errno(sock
);
3809 log_warn(LD_NET
, "setsockopt() to constrain recv "
3810 "buffer to %d bytes failed: %s", size
, tor_socket_strerror(e
));
3814 /** Process new bytes that have arrived on conn-\>inbuf.
3816 * This function just passes conn to the connection-specific
3817 * connection_*_process_inbuf() function. It also passes in
3818 * package_partial if wanted.
3821 connection_process_inbuf(connection_t
*conn
, int package_partial
)
3825 switch (conn
->type
) {
3827 return connection_or_process_inbuf(TO_OR_CONN(conn
));
3828 case CONN_TYPE_EXIT
:
3830 return connection_edge_process_inbuf(TO_EDGE_CONN(conn
),
3833 return connection_dir_process_inbuf(TO_DIR_CONN(conn
));
3834 case CONN_TYPE_CPUWORKER
:
3835 return connection_cpu_process_inbuf(conn
);
3836 case CONN_TYPE_CONTROL
:
3837 return connection_control_process_inbuf(TO_CONTROL_CONN(conn
));
3839 log_err(LD_BUG
,"got unexpected conn type %d.", conn
->type
);
3840 tor_fragile_assert();
3845 /** Called whenever we've written data on a connection. */
3847 connection_flushed_some(connection_t
*conn
)
3850 tor_assert(!conn
->in_flushed_some
);
3851 conn
->in_flushed_some
= 1;
3852 if (conn
->type
== CONN_TYPE_DIR
&&
3853 conn
->state
== DIR_CONN_STATE_SERVER_WRITING
) {
3854 r
= connection_dirserv_flushed_some(TO_DIR_CONN(conn
));
3855 } else if (conn
->type
== CONN_TYPE_OR
) {
3856 r
= connection_or_flushed_some(TO_OR_CONN(conn
));
3857 } else if (CONN_IS_EDGE(conn
)) {
3858 r
= connection_edge_flushed_some(TO_EDGE_CONN(conn
));
3860 conn
->in_flushed_some
= 0;
3864 /** We just finished flushing bytes from conn-\>outbuf, and there
3865 * are no more bytes remaining.
3867 * This function just passes conn to the connection-specific
3868 * connection_*_finished_flushing() function.
3871 connection_finished_flushing(connection_t
*conn
)
3875 /* If the connection is closed, don't try to do anything more here. */
3876 if (CONN_IS_CLOSED(conn
))
3879 // log_fn(LOG_DEBUG,"entered. Socket %u.", conn->s);
3881 IF_HAS_NO_BUFFEREVENT(conn
)
3882 connection_stop_writing(conn
);
3884 switch (conn
->type
) {
3886 return connection_or_finished_flushing(TO_OR_CONN(conn
));
3888 case CONN_TYPE_EXIT
:
3889 return connection_edge_finished_flushing(TO_EDGE_CONN(conn
));
3891 return connection_dir_finished_flushing(TO_DIR_CONN(conn
));
3892 case CONN_TYPE_CPUWORKER
:
3893 return connection_cpu_finished_flushing(conn
);
3894 case CONN_TYPE_CONTROL
:
3895 return connection_control_finished_flushing(TO_CONTROL_CONN(conn
));
3897 log_err(LD_BUG
,"got unexpected conn type %d.", conn
->type
);
3898 tor_fragile_assert();
3903 /** Called when our attempt to connect() to another server has just
3906 * This function just passes conn to the connection-specific
3907 * connection_*_finished_connecting() function.
3910 connection_finished_connecting(connection_t
*conn
)
3914 if (!server_mode(get_options())) {
3915 /* See whether getsockname() says our address changed. We need to do this
3916 * now that the connection has finished, because getsockname() on Windows
3917 * won't work until then. */
3918 client_check_address_changed(conn
->s
);
3924 return connection_or_finished_connecting(TO_OR_CONN(conn
));
3925 case CONN_TYPE_EXIT
:
3926 return connection_edge_finished_connecting(TO_EDGE_CONN(conn
));
3928 return connection_dir_finished_connecting(TO_DIR_CONN(conn
));
3930 log_err(LD_BUG
,"got unexpected conn type %d.", conn
->type
);
3931 tor_fragile_assert();
3936 /** Callback: invoked when a connection reaches an EOF event. */
3938 connection_reached_eof(connection_t
*conn
)
3940 switch (conn
->type
) {
3942 return connection_or_reached_eof(TO_OR_CONN(conn
));
3944 case CONN_TYPE_EXIT
:
3945 return connection_edge_reached_eof(TO_EDGE_CONN(conn
));
3947 return connection_dir_reached_eof(TO_DIR_CONN(conn
));
3948 case CONN_TYPE_CPUWORKER
:
3949 return connection_cpu_reached_eof(conn
);
3950 case CONN_TYPE_CONTROL
:
3951 return connection_control_reached_eof(TO_CONTROL_CONN(conn
));
3953 log_err(LD_BUG
,"got unexpected conn type %d.", conn
->type
);
3954 tor_fragile_assert();
3959 /** Log how many bytes are used by buffers of different kinds and sizes. */
3961 connection_dump_buffer_mem_stats(int severity
)
3963 uint64_t used_by_type
[_CONN_TYPE_MAX
+1];
3964 uint64_t alloc_by_type
[_CONN_TYPE_MAX
+1];
3965 int n_conns_by_type
[_CONN_TYPE_MAX
+1];
3966 uint64_t total_alloc
= 0;
3967 uint64_t total_used
= 0;
3969 smartlist_t
*conns
= get_connection_array();
3971 memset(used_by_type
, 0, sizeof(used_by_type
));
3972 memset(alloc_by_type
, 0, sizeof(alloc_by_type
));
3973 memset(n_conns_by_type
, 0, sizeof(n_conns_by_type
));
3975 SMARTLIST_FOREACH_BEGIN(conns
, connection_t
*, c
) {
3977 ++n_conns_by_type
[tp
];
3979 used_by_type
[tp
] += buf_datalen(c
->inbuf
);
3980 alloc_by_type
[tp
] += buf_allocation(c
->inbuf
);
3983 used_by_type
[tp
] += buf_datalen(c
->outbuf
);
3984 alloc_by_type
[tp
] += buf_allocation(c
->outbuf
);
3986 } SMARTLIST_FOREACH_END(c
);
3987 for (i
=0; i
<= _CONN_TYPE_MAX
; ++i
) {
3988 total_used
+= used_by_type
[i
];
3989 total_alloc
+= alloc_by_type
[i
];
3992 log(severity
, LD_GENERAL
,
3993 "In buffers for %d connections: "U64_FORMAT
" used/"U64_FORMAT
" allocated",
3994 smartlist_len(conns
),
3995 U64_PRINTF_ARG(total_used
), U64_PRINTF_ARG(total_alloc
));
3996 for (i
=_CONN_TYPE_MIN
; i
<= _CONN_TYPE_MAX
; ++i
) {
3997 if (!n_conns_by_type
[i
])
3999 log(severity
, LD_GENERAL
,
4000 " For %d %s connections: "U64_FORMAT
" used/"U64_FORMAT
" allocated",
4001 n_conns_by_type
[i
], conn_type_to_string(i
),
4002 U64_PRINTF_ARG(used_by_type
[i
]), U64_PRINTF_ARG(alloc_by_type
[i
]));
4006 /** Verify that connection <b>conn</b> has all of its invariants
4007 * correct. Trigger an assert if anything is invalid.
4010 assert_connection_ok(connection_t
*conn
, time_t now
)
4012 (void) now
; /* XXXX unused. */
4014 tor_assert(conn
->type
>= _CONN_TYPE_MIN
);
4015 tor_assert(conn
->type
<= _CONN_TYPE_MAX
);
4017 #ifdef USE_BUFFEREVENTS
4019 tor_assert(conn
->read_event
== NULL
);
4020 tor_assert(conn
->write_event
== NULL
);
4021 tor_assert(conn
->inbuf
== NULL
);
4022 tor_assert(conn
->outbuf
== NULL
);
4026 switch (conn
->type
) {
4028 tor_assert(conn
->magic
== OR_CONNECTION_MAGIC
);
4031 tor_assert(conn
->magic
== ENTRY_CONNECTION_MAGIC
);
4033 case CONN_TYPE_EXIT
:
4034 tor_assert(conn
->magic
== EDGE_CONNECTION_MAGIC
);
4037 tor_assert(conn
->magic
== DIR_CONNECTION_MAGIC
);
4039 case CONN_TYPE_CONTROL
:
4040 tor_assert(conn
->magic
== CONTROL_CONNECTION_MAGIC
);
4042 CASE_ANY_LISTENER_TYPE
:
4043 tor_assert(conn
->magic
== LISTENER_CONNECTION_MAGIC
);
4046 tor_assert(conn
->magic
== BASE_CONNECTION_MAGIC
);
4050 if (conn
->linked_conn
) {
4051 tor_assert(conn
->linked_conn
->linked_conn
== conn
);
4052 tor_assert(conn
->linked
);
4055 tor_assert(!SOCKET_OK(conn
->s
));
4057 if (conn
->outbuf_flushlen
> 0) {
4058 /* With optimistic data, we may have queued data in
4059 * EXIT_CONN_STATE_RESOLVING while the conn is not yet marked to writing.
4061 tor_assert((conn
->type
== CONN_TYPE_EXIT
&&
4062 conn
->state
== EXIT_CONN_STATE_RESOLVING
) ||
4063 connection_is_writing(conn
) ||
4064 conn
->write_blocked_on_bw
||
4065 (CONN_IS_EDGE(conn
) &&
4066 TO_EDGE_CONN(conn
)->edge_blocked_on_circ
));
4069 if (conn
->hold_open_until_flushed
)
4070 tor_assert(conn
->marked_for_close
);
4072 /* XXXX check: read_blocked_on_bw, write_blocked_on_bw, s, conn_array_index,
4073 * marked_for_close. */
4077 assert_buf_ok(conn
->inbuf
);
4079 assert_buf_ok(conn
->outbuf
);
4081 if (conn
->type
== CONN_TYPE_OR
) {
4082 or_connection_t
*or_conn
= TO_OR_CONN(conn
);
4083 if (conn
->state
== OR_CONN_STATE_OPEN
) {
4084 /* tor_assert(conn->bandwidth > 0); */
4085 /* the above isn't necessarily true: if we just did a TLS
4086 * handshake but we didn't recognize the other peer, or it
4087 * gave a bad cert/etc, then we won't have assigned bandwidth,
4088 * yet it will be open. -RD
4090 // tor_assert(conn->read_bucket >= 0);
4092 // tor_assert(conn->addr && conn->port);
4093 tor_assert(conn
->address
);
4094 if (conn
->state
> OR_CONN_STATE_PROXY_HANDSHAKING
)
4095 tor_assert(or_conn
->tls
);
4098 if (CONN_IS_EDGE(conn
)) {
4099 /* XXX unchecked: package window, deliver window. */
4100 if (conn
->type
== CONN_TYPE_AP
) {
4101 entry_connection_t
*entry_conn
= TO_ENTRY_CONN(conn
);
4102 if (entry_conn
->chosen_exit_optional
|| entry_conn
->chosen_exit_retries
)
4103 tor_assert(entry_conn
->chosen_exit_name
);
4105 tor_assert(entry_conn
->socks_request
);
4106 if (conn
->state
== AP_CONN_STATE_OPEN
) {
4107 tor_assert(entry_conn
->socks_request
->has_finished
);
4108 if (!conn
->marked_for_close
) {
4109 tor_assert(ENTRY_TO_EDGE_CONN(entry_conn
)->cpath_layer
);
4110 assert_cpath_layer_ok(ENTRY_TO_EDGE_CONN(entry_conn
)->cpath_layer
);
4114 if (conn
->type
== CONN_TYPE_EXIT
) {
4115 tor_assert(conn
->purpose
== EXIT_PURPOSE_CONNECT
||
4116 conn
->purpose
== EXIT_PURPOSE_RESOLVE
);
4118 } else if (conn
->type
== CONN_TYPE_DIR
) {
4120 /* Purpose is only used for dir and exit types currently */
4121 tor_assert(!conn
->purpose
);
4126 CASE_ANY_LISTENER_TYPE
:
4127 tor_assert(conn
->state
== LISTENER_STATE_READY
);
4130 tor_assert(conn
->state
>= _OR_CONN_STATE_MIN
);
4131 tor_assert(conn
->state
<= _OR_CONN_STATE_MAX
);
4132 tor_assert(TO_OR_CONN(conn
)->n_circuits
>= 0);
4134 case CONN_TYPE_EXIT
:
4135 tor_assert(conn
->state
>= _EXIT_CONN_STATE_MIN
);
4136 tor_assert(conn
->state
<= _EXIT_CONN_STATE_MAX
);
4137 tor_assert(conn
->purpose
>= _EXIT_PURPOSE_MIN
);
4138 tor_assert(conn
->purpose
<= _EXIT_PURPOSE_MAX
);
4141 tor_assert(conn
->state
>= _AP_CONN_STATE_MIN
);
4142 tor_assert(conn
->state
<= _AP_CONN_STATE_MAX
);
4143 tor_assert(TO_ENTRY_CONN(conn
)->socks_request
);
4146 tor_assert(conn
->state
>= _DIR_CONN_STATE_MIN
);
4147 tor_assert(conn
->state
<= _DIR_CONN_STATE_MAX
);
4148 tor_assert(conn
->purpose
>= _DIR_PURPOSE_MIN
);
4149 tor_assert(conn
->purpose
<= _DIR_PURPOSE_MAX
);
4151 case CONN_TYPE_CPUWORKER
:
4152 tor_assert(conn
->state
>= _CPUWORKER_STATE_MIN
);
4153 tor_assert(conn
->state
<= _CPUWORKER_STATE_MAX
);
4155 case CONN_TYPE_CONTROL
:
4156 tor_assert(conn
->state
>= _CONTROL_CONN_STATE_MIN
);
4157 tor_assert(conn
->state
<= _CONTROL_CONN_STATE_MAX
);
4164 /** Fills <b>addr</b> and <b>port</b> with the details of the global
4165 * proxy server we are using.
4166 * <b>conn</b> contains the connection we are using the proxy for.
4168 * Return 0 on success, -1 on failure.
4171 get_proxy_addrport(tor_addr_t
*addr
, uint16_t *port
, int *proxy_type
,
4172 const connection_t
*conn
)
4174 const or_options_t
*options
= get_options();
4176 if (options
->HTTPSProxy
) {
4177 tor_addr_copy(addr
, &options
->HTTPSProxyAddr
);
4178 *port
= options
->HTTPSProxyPort
;
4179 *proxy_type
= PROXY_CONNECT
;
4181 } else if (options
->Socks4Proxy
) {
4182 tor_addr_copy(addr
, &options
->Socks4ProxyAddr
);
4183 *port
= options
->Socks4ProxyPort
;
4184 *proxy_type
= PROXY_SOCKS4
;
4186 } else if (options
->Socks5Proxy
) {
4187 tor_addr_copy(addr
, &options
->Socks5ProxyAddr
);
4188 *port
= options
->Socks5ProxyPort
;
4189 *proxy_type
= PROXY_SOCKS5
;
4191 } else if (options
->ClientTransportPlugin
||
4193 const transport_t
*transport
= NULL
;
4195 r
= find_transport_by_bridge_addrport(&conn
->addr
, conn
->port
, &transport
);
4198 if (transport
) { /* transport found */
4199 tor_addr_copy(addr
, &transport
->addr
);
4200 *port
= transport
->port
;
4201 *proxy_type
= transport
->socks_version
;
4206 *proxy_type
= PROXY_NONE
;
4210 /** Returns the global proxy type used by tor. */
4212 get_proxy_type(void)
4214 const or_options_t
*options
= get_options();
4216 if (options
->HTTPSProxy
)
4217 return PROXY_CONNECT
;
4218 else if (options
->Socks4Proxy
)
4219 return PROXY_SOCKS4
;
4220 else if (options
->Socks5Proxy
)
4221 return PROXY_SOCKS5
;
4222 else if (options
->ClientTransportPlugin
)
4223 return PROXY_PLUGGABLE
;
4228 /** Log a failed connection to a proxy server.
4229 * <b>conn</b> is the connection we use the proxy server for. */
4231 log_failed_proxy_connection(connection_t
*conn
)
4233 tor_addr_t proxy_addr
;
4234 uint16_t proxy_port
;
4237 if (get_proxy_addrport(&proxy_addr
, &proxy_port
, &proxy_type
, conn
) != 0)
4238 return; /* if we have no proxy set up, leave this function. */
4241 "The connection to the %s proxy server at %s:%u just failed. "
4242 "Make sure that the proxy server is up and running.",
4243 proxy_type_to_string(get_proxy_type()), fmt_addr(&proxy_addr
),
4247 /** Return string representation of <b>proxy_type</b>. */
4249 proxy_type_to_string(int proxy_type
)
4251 switch (proxy_type
) {
4252 case PROXY_CONNECT
: return "HTTP";
4253 case PROXY_SOCKS4
: return "SOCKS4";
4254 case PROXY_SOCKS5
: return "SOCKS5";
4255 case PROXY_PLUGGABLE
: return "pluggable transports SOCKS";
4256 case PROXY_NONE
: return "NULL";
4257 default: tor_assert(0);
4259 return NULL
; /*Unreached*/
4262 /** Call _connection_free() on every connection in our array, and release all
4263 * storage held by connection.c. This is used by cpuworkers and dnsworkers
4264 * when they fork, so they don't keep resources held open (especially
4267 * Don't do the checks in connection_free(), because they will
4271 connection_free_all(void)
4273 smartlist_t
*conns
= get_connection_array();
4275 /* We don't want to log any messages to controllers. */
4276 SMARTLIST_FOREACH(conns
, connection_t
*, conn
,
4277 if (conn
->type
== CONN_TYPE_CONTROL
)
4278 TO_CONTROL_CONN(conn
)->event_mask
= 0);
4280 control_update_global_event_mask();
4282 /* Unlink everything from the identity map. */
4283 connection_or_clear_identity_map();
4285 /* Clear out our list of broken connections */
4286 clear_broken_connection_map(0);
4288 SMARTLIST_FOREACH(conns
, connection_t
*, conn
, _connection_free(conn
));
4290 if (outgoing_addrs
) {
4291 SMARTLIST_FOREACH(outgoing_addrs
, tor_addr_t
*, addr
, tor_free(addr
));
4292 smartlist_free(outgoing_addrs
);
4293 outgoing_addrs
= NULL
;
4296 #ifdef USE_BUFFEREVENTS
4297 if (global_rate_limit
)
4298 bufferevent_rate_limit_group_free(global_rate_limit
);