Add Coccinelle patch for replacing NULL/non-NULL tt_assert().
[tor.git] / src / or / connection.c
blob31a682387d6794669a4c19ddcc1666ffa0d05a7e
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-2017, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
7 /**
8 * \file connection.c
9 * \brief General high-level functions to handle reading and writing
10 * on connections.
12 * Each connection (ideally) represents a TLS connection, a TCP socket, a unix
13 * socket, or a UDP socket on which reads and writes can occur. (But see
14 * connection_edge.c for cases where connections can also represent streams
15 * that do not have a corresponding socket.)
17 * The module implements the abstract type, connection_t. The subtypes are:
18 * <ul>
19 * <li>listener_connection_t, implemented here in connection.c
20 * <li>dir_connection_t, implemented in directory.c
21 * <li>or_connection_t, implemented in connection_or.c
22 * <li>edge_connection_t, implemented in connection_edge.c, along with
23 * its subtype(s):
24 * <ul><li>entry_connection_t, also implemented in connection_edge.c
25 * </ul>
26 * <li>control_connection_t, implemented in control.c
27 * </ul>
29 * The base type implemented in this module is responsible for basic
30 * rate limiting, flow control, and marshalling bytes onto and off of the
31 * network (either directly or via TLS).
33 * Connections are registered with the main loop with connection_add(). As
34 * they become able to read or write register the fact with the event main
35 * loop by calling connection_watch_events(), connection_start_reading(), or
36 * connection_start_writing(). When they no longer want to read or write,
37 * they call connection_stop_reading() or connection_stop_writing().
39 * To queue data to be written on a connection, call
40 * connection_write_to_buf(). When data arrives, the
41 * connection_process_inbuf() callback is invoked, which dispatches to a
42 * type-specific function (such as connection_edge_process_inbuf() for
43 * example). Connection types that need notice of when data has been written
44 * receive notification via connection_flushed_some() and
45 * connection_finished_flushing(). These functions all delegate to
46 * type-specific implementations.
48 * Additionally, beyond the core of connection_t, this module also implements:
49 * <ul>
50 * <li>Listeners, which wait for incoming sockets and launch connections
51 * <li>Outgoing SOCKS proxy support
52 * <li>Outgoing HTTP proxy support
53 * <li>An out-of-sockets handler for dealing with socket exhaustion
54 * </ul>
55 **/
57 #define CONNECTION_PRIVATE
58 #include "or.h"
59 #include "bridges.h"
60 #include "buffers.h"
62 * Define this so we get channel internal functions, since we're implementing
63 * part of a subclass (channel_tls_t).
65 #define TOR_CHANNEL_INTERNAL_
66 #define CONNECTION_PRIVATE
67 #include "backtrace.h"
68 #include "channel.h"
69 #include "channeltls.h"
70 #include "circuitbuild.h"
71 #include "circuitlist.h"
72 #include "circuituse.h"
73 #include "config.h"
74 #include "connection.h"
75 #include "connection_edge.h"
76 #include "connection_or.h"
77 #include "control.h"
78 #include "directory.h"
79 #include "dirserv.h"
80 #include "dns.h"
81 #include "dnsserv.h"
82 #include "entrynodes.h"
83 #include "ext_orport.h"
84 #include "geoip.h"
85 #include "main.h"
86 #include "hs_common.h"
87 #include "hs_ident.h"
88 #include "nodelist.h"
89 #include "policies.h"
90 #include "reasons.h"
91 #include "relay.h"
92 #include "rendclient.h"
93 #include "rendcommon.h"
94 #include "rephist.h"
95 #include "router.h"
96 #include "routerlist.h"
97 #include "transports.h"
98 #include "routerparse.h"
99 #include "sandbox.h"
100 #include "transports.h"
102 #ifdef HAVE_PWD_H
103 #include <pwd.h>
104 #endif
106 #ifdef HAVE_SYS_UN_H
107 #include <sys/socket.h>
108 #include <sys/un.h>
109 #endif
111 static connection_t *connection_listener_new(
112 const struct sockaddr *listensockaddr,
113 socklen_t listensocklen, int type,
114 const char *address,
115 const port_cfg_t *portcfg);
116 static void connection_init(time_t now, connection_t *conn, int type,
117 int socket_family);
118 static int connection_init_accepted_conn(connection_t *conn,
119 const listener_connection_t *listener);
120 static int connection_handle_listener_read(connection_t *conn, int new_type);
121 static int connection_bucket_should_increase(int bucket,
122 or_connection_t *conn);
123 static int connection_finished_flushing(connection_t *conn);
124 static int connection_flushed_some(connection_t *conn);
125 static int connection_finished_connecting(connection_t *conn);
126 static int connection_reached_eof(connection_t *conn);
127 static int connection_read_to_buf(connection_t *conn, ssize_t *max_to_read,
128 int *socket_error);
129 static int connection_process_inbuf(connection_t *conn, int package_partial);
130 static void client_check_address_changed(tor_socket_t sock);
131 static void set_constrained_socket_buffers(tor_socket_t sock, int size);
133 static const char *connection_proxy_state_to_string(int state);
134 static int connection_read_https_proxy_response(connection_t *conn);
135 static void connection_send_socks5_connect(connection_t *conn);
136 static const char *proxy_type_to_string(int proxy_type);
137 static int get_proxy_type(void);
138 const tor_addr_t *conn_get_outbound_address(sa_family_t family,
139 const or_options_t *options, unsigned int conn_type);
141 /** The last addresses that our network interface seemed to have been
142 * binding to. We use this as one way to detect when our IP changes.
144 * XXXX+ We should really use the entire list of interfaces here.
146 static tor_addr_t *last_interface_ipv4 = NULL;
147 /* DOCDOC last_interface_ipv6 */
148 static tor_addr_t *last_interface_ipv6 = NULL;
149 /** A list of tor_addr_t for addresses we've used in outgoing connections.
150 * Used to detect IP address changes. */
151 static smartlist_t *outgoing_addrs = NULL;
153 #define CASE_ANY_LISTENER_TYPE \
154 case CONN_TYPE_OR_LISTENER: \
155 case CONN_TYPE_EXT_OR_LISTENER: \
156 case CONN_TYPE_AP_LISTENER: \
157 case CONN_TYPE_DIR_LISTENER: \
158 case CONN_TYPE_CONTROL_LISTENER: \
159 case CONN_TYPE_AP_TRANS_LISTENER: \
160 case CONN_TYPE_AP_NATD_LISTENER: \
161 case CONN_TYPE_AP_DNS_LISTENER
163 /**************************************************************/
166 * Return the human-readable name for the connection type <b>type</b>
168 const char *
169 conn_type_to_string(int type)
171 static char buf[64];
172 switch (type) {
173 case CONN_TYPE_OR_LISTENER: return "OR listener";
174 case CONN_TYPE_OR: return "OR";
175 case CONN_TYPE_EXIT: return "Exit";
176 case CONN_TYPE_AP_LISTENER: return "Socks listener";
177 case CONN_TYPE_AP_TRANS_LISTENER:
178 return "Transparent pf/netfilter listener";
179 case CONN_TYPE_AP_NATD_LISTENER: return "Transparent natd listener";
180 case CONN_TYPE_AP_DNS_LISTENER: return "DNS listener";
181 case CONN_TYPE_AP: return "Socks";
182 case CONN_TYPE_DIR_LISTENER: return "Directory listener";
183 case CONN_TYPE_DIR: return "Directory";
184 case CONN_TYPE_CONTROL_LISTENER: return "Control listener";
185 case CONN_TYPE_CONTROL: return "Control";
186 case CONN_TYPE_EXT_OR: return "Extended OR";
187 case CONN_TYPE_EXT_OR_LISTENER: return "Extended OR listener";
188 default:
189 log_warn(LD_BUG, "unknown connection type %d", type);
190 tor_snprintf(buf, sizeof(buf), "unknown [%d]", type);
191 return buf;
196 * Return the human-readable name for the connection state <b>state</b>
197 * for the connection type <b>type</b>
199 const char *
200 conn_state_to_string(int type, int state)
202 static char buf[96];
203 switch (type) {
204 CASE_ANY_LISTENER_TYPE:
205 if (state == LISTENER_STATE_READY)
206 return "ready";
207 break;
208 case CONN_TYPE_OR:
209 switch (state) {
210 case OR_CONN_STATE_CONNECTING: return "connect()ing";
211 case OR_CONN_STATE_PROXY_HANDSHAKING: return "handshaking (proxy)";
212 case OR_CONN_STATE_TLS_HANDSHAKING: return "handshaking (TLS)";
213 case OR_CONN_STATE_TLS_CLIENT_RENEGOTIATING:
214 return "renegotiating (TLS, v2 handshake)";
215 case OR_CONN_STATE_TLS_SERVER_RENEGOTIATING:
216 return "waiting for renegotiation or V3 handshake";
217 case OR_CONN_STATE_OR_HANDSHAKING_V2:
218 return "handshaking (Tor, v2 handshake)";
219 case OR_CONN_STATE_OR_HANDSHAKING_V3:
220 return "handshaking (Tor, v3 handshake)";
221 case OR_CONN_STATE_OPEN: return "open";
223 break;
224 case CONN_TYPE_EXT_OR:
225 switch (state) {
226 case EXT_OR_CONN_STATE_AUTH_WAIT_AUTH_TYPE:
227 return "waiting for authentication type";
228 case EXT_OR_CONN_STATE_AUTH_WAIT_CLIENT_NONCE:
229 return "waiting for client nonce";
230 case EXT_OR_CONN_STATE_AUTH_WAIT_CLIENT_HASH:
231 return "waiting for client hash";
232 case EXT_OR_CONN_STATE_OPEN: return "open";
233 case EXT_OR_CONN_STATE_FLUSHING: return "flushing final OKAY";
235 break;
236 case CONN_TYPE_EXIT:
237 switch (state) {
238 case EXIT_CONN_STATE_RESOLVING: return "waiting for dest info";
239 case EXIT_CONN_STATE_CONNECTING: return "connecting";
240 case EXIT_CONN_STATE_OPEN: return "open";
241 case EXIT_CONN_STATE_RESOLVEFAILED: return "resolve failed";
243 break;
244 case CONN_TYPE_AP:
245 switch (state) {
246 case AP_CONN_STATE_SOCKS_WAIT: return "waiting for socks info";
247 case AP_CONN_STATE_NATD_WAIT: return "waiting for natd dest info";
248 case AP_CONN_STATE_RENDDESC_WAIT: return "waiting for rendezvous desc";
249 case AP_CONN_STATE_CONTROLLER_WAIT: return "waiting for controller";
250 case AP_CONN_STATE_CIRCUIT_WAIT: return "waiting for circuit";
251 case AP_CONN_STATE_CONNECT_WAIT: return "waiting for connect response";
252 case AP_CONN_STATE_RESOLVE_WAIT: return "waiting for resolve response";
253 case AP_CONN_STATE_OPEN: return "open";
255 break;
256 case CONN_TYPE_DIR:
257 switch (state) {
258 case DIR_CONN_STATE_CONNECTING: return "connecting";
259 case DIR_CONN_STATE_CLIENT_SENDING: return "client sending";
260 case DIR_CONN_STATE_CLIENT_READING: return "client reading";
261 case DIR_CONN_STATE_CLIENT_FINISHED: return "client finished";
262 case DIR_CONN_STATE_SERVER_COMMAND_WAIT: return "waiting for command";
263 case DIR_CONN_STATE_SERVER_WRITING: return "writing";
265 break;
266 case CONN_TYPE_CONTROL:
267 switch (state) {
268 case CONTROL_CONN_STATE_OPEN: return "open (protocol v1)";
269 case CONTROL_CONN_STATE_NEEDAUTH:
270 return "waiting for authentication (protocol v1)";
272 break;
275 log_warn(LD_BUG, "unknown connection state %d (type %d)", state, type);
276 tor_snprintf(buf, sizeof(buf),
277 "unknown state [%d] on unknown [%s] connection",
278 state, conn_type_to_string(type));
279 return buf;
282 /** Allocate and return a new dir_connection_t, initialized as by
283 * connection_init(). */
284 dir_connection_t *
285 dir_connection_new(int socket_family)
287 dir_connection_t *dir_conn = tor_malloc_zero(sizeof(dir_connection_t));
288 connection_init(time(NULL), TO_CONN(dir_conn), CONN_TYPE_DIR, socket_family);
289 return dir_conn;
292 /** Allocate and return a new or_connection_t, initialized as by
293 * connection_init().
295 * Initialize active_circuit_pqueue.
297 * Set active_circuit_pqueue_last_recalibrated to current cell_ewma tick.
299 or_connection_t *
300 or_connection_new(int type, int socket_family)
302 or_connection_t *or_conn = tor_malloc_zero(sizeof(or_connection_t));
303 time_t now = time(NULL);
304 tor_assert(type == CONN_TYPE_OR || type == CONN_TYPE_EXT_OR);
305 connection_init(now, TO_CONN(or_conn), type, socket_family);
307 connection_or_set_canonical(or_conn, 0);
309 if (type == CONN_TYPE_EXT_OR)
310 connection_or_set_ext_or_identifier(or_conn);
312 return or_conn;
315 /** Allocate and return a new entry_connection_t, initialized as by
316 * connection_init().
318 * Allocate space to store the socks_request.
320 entry_connection_t *
321 entry_connection_new(int type, int socket_family)
323 entry_connection_t *entry_conn = tor_malloc_zero(sizeof(entry_connection_t));
324 tor_assert(type == CONN_TYPE_AP);
325 connection_init(time(NULL), ENTRY_TO_CONN(entry_conn), type, socket_family);
326 entry_conn->socks_request = socks_request_new();
327 /* If this is coming from a listener, we'll set it up based on the listener
328 * in a little while. Otherwise, we're doing this as a linked connection
329 * of some kind, and we should set it up here based on the socket family */
330 if (socket_family == AF_INET)
331 entry_conn->entry_cfg.ipv4_traffic = 1;
332 else if (socket_family == AF_INET6)
333 entry_conn->entry_cfg.ipv6_traffic = 1;
334 else if (socket_family == AF_UNIX)
335 entry_conn->is_socks_socket = 1;
336 return entry_conn;
339 /** Allocate and return a new edge_connection_t, initialized as by
340 * connection_init(). */
341 edge_connection_t *
342 edge_connection_new(int type, int socket_family)
344 edge_connection_t *edge_conn = tor_malloc_zero(sizeof(edge_connection_t));
345 tor_assert(type == CONN_TYPE_EXIT);
346 connection_init(time(NULL), TO_CONN(edge_conn), type, socket_family);
347 return edge_conn;
350 /** Allocate and return a new control_connection_t, initialized as by
351 * connection_init(). */
352 control_connection_t *
353 control_connection_new(int socket_family)
355 control_connection_t *control_conn =
356 tor_malloc_zero(sizeof(control_connection_t));
357 connection_init(time(NULL),
358 TO_CONN(control_conn), CONN_TYPE_CONTROL, socket_family);
359 return control_conn;
362 /** Allocate and return a new listener_connection_t, initialized as by
363 * connection_init(). */
364 listener_connection_t *
365 listener_connection_new(int type, int socket_family)
367 listener_connection_t *listener_conn =
368 tor_malloc_zero(sizeof(listener_connection_t));
369 connection_init(time(NULL), TO_CONN(listener_conn), type, socket_family);
370 return listener_conn;
373 /** Allocate, initialize, and return a new connection_t subtype of <b>type</b>
374 * to make or receive connections of address family <b>socket_family</b>. The
375 * type should be one of the CONN_TYPE_* constants. */
376 connection_t *
377 connection_new(int type, int socket_family)
379 switch (type) {
380 case CONN_TYPE_OR:
381 case CONN_TYPE_EXT_OR:
382 return TO_CONN(or_connection_new(type, socket_family));
384 case CONN_TYPE_EXIT:
385 return TO_CONN(edge_connection_new(type, socket_family));
387 case CONN_TYPE_AP:
388 return ENTRY_TO_CONN(entry_connection_new(type, socket_family));
390 case CONN_TYPE_DIR:
391 return TO_CONN(dir_connection_new(socket_family));
393 case CONN_TYPE_CONTROL:
394 return TO_CONN(control_connection_new(socket_family));
396 CASE_ANY_LISTENER_TYPE:
397 return TO_CONN(listener_connection_new(type, socket_family));
399 default: {
400 connection_t *conn = tor_malloc_zero(sizeof(connection_t));
401 connection_init(time(NULL), conn, type, socket_family);
402 return conn;
407 /** Initializes conn. (you must call connection_add() to link it into the main
408 * array).
410 * Set conn-\>magic to the correct value.
412 * Set conn-\>type to <b>type</b>. Set conn-\>s and conn-\>conn_array_index to
413 * -1 to signify they are not yet assigned.
415 * Initialize conn's timestamps to now.
417 static void
418 connection_init(time_t now, connection_t *conn, int type, int socket_family)
420 static uint64_t n_connections_allocated = 1;
422 switch (type) {
423 case CONN_TYPE_OR:
424 case CONN_TYPE_EXT_OR:
425 conn->magic = OR_CONNECTION_MAGIC;
426 break;
427 case CONN_TYPE_EXIT:
428 conn->magic = EDGE_CONNECTION_MAGIC;
429 break;
430 case CONN_TYPE_AP:
431 conn->magic = ENTRY_CONNECTION_MAGIC;
432 break;
433 case CONN_TYPE_DIR:
434 conn->magic = DIR_CONNECTION_MAGIC;
435 break;
436 case CONN_TYPE_CONTROL:
437 conn->magic = CONTROL_CONNECTION_MAGIC;
438 break;
439 CASE_ANY_LISTENER_TYPE:
440 conn->magic = LISTENER_CONNECTION_MAGIC;
441 break;
442 default:
443 conn->magic = BASE_CONNECTION_MAGIC;
444 break;
447 conn->s = TOR_INVALID_SOCKET; /* give it a default of 'not used' */
448 conn->conn_array_index = -1; /* also default to 'not used' */
449 conn->global_identifier = n_connections_allocated++;
451 conn->type = type;
452 conn->socket_family = socket_family;
453 if (!connection_is_listener(conn)) {
454 /* listeners never use their buf */
455 conn->inbuf = buf_new();
456 conn->outbuf = buf_new();
459 conn->timestamp_created = now;
460 conn->timestamp_lastread = now;
461 conn->timestamp_lastwritten = now;
464 /** Create a link between <b>conn_a</b> and <b>conn_b</b>. */
465 void
466 connection_link_connections(connection_t *conn_a, connection_t *conn_b)
468 tor_assert(! SOCKET_OK(conn_a->s));
469 tor_assert(! SOCKET_OK(conn_b->s));
471 conn_a->linked = 1;
472 conn_b->linked = 1;
473 conn_a->linked_conn = conn_b;
474 conn_b->linked_conn = conn_a;
477 /** Return true iff the provided connection listener type supports AF_UNIX
478 * sockets. */
480 conn_listener_type_supports_af_unix(int type)
482 /* For now only control ports or SOCKS ports can be Unix domain sockets
483 * and listeners at the same time */
484 switch (type) {
485 case CONN_TYPE_CONTROL_LISTENER:
486 case CONN_TYPE_AP_LISTENER:
487 return 1;
488 default:
489 return 0;
493 /** Deallocate memory used by <b>conn</b>. Deallocate its buffers if
494 * necessary, close its socket if necessary, and mark the directory as dirty
495 * if <b>conn</b> is an OR or OP connection.
497 STATIC void
498 connection_free_(connection_t *conn)
500 void *mem;
501 size_t memlen;
502 if (!conn)
503 return;
505 switch (conn->type) {
506 case CONN_TYPE_OR:
507 case CONN_TYPE_EXT_OR:
508 tor_assert(conn->magic == OR_CONNECTION_MAGIC);
509 mem = TO_OR_CONN(conn);
510 memlen = sizeof(or_connection_t);
511 break;
512 case CONN_TYPE_AP:
513 tor_assert(conn->magic == ENTRY_CONNECTION_MAGIC);
514 mem = TO_ENTRY_CONN(conn);
515 memlen = sizeof(entry_connection_t);
516 break;
517 case CONN_TYPE_EXIT:
518 tor_assert(conn->magic == EDGE_CONNECTION_MAGIC);
519 mem = TO_EDGE_CONN(conn);
520 memlen = sizeof(edge_connection_t);
521 break;
522 case CONN_TYPE_DIR:
523 tor_assert(conn->magic == DIR_CONNECTION_MAGIC);
524 mem = TO_DIR_CONN(conn);
525 memlen = sizeof(dir_connection_t);
526 break;
527 case CONN_TYPE_CONTROL:
528 tor_assert(conn->magic == CONTROL_CONNECTION_MAGIC);
529 mem = TO_CONTROL_CONN(conn);
530 memlen = sizeof(control_connection_t);
531 break;
532 CASE_ANY_LISTENER_TYPE:
533 tor_assert(conn->magic == LISTENER_CONNECTION_MAGIC);
534 mem = TO_LISTENER_CONN(conn);
535 memlen = sizeof(listener_connection_t);
536 break;
537 default:
538 tor_assert(conn->magic == BASE_CONNECTION_MAGIC);
539 mem = conn;
540 memlen = sizeof(connection_t);
541 break;
544 if (conn->linked) {
545 log_info(LD_GENERAL, "Freeing linked %s connection [%s] with %d "
546 "bytes on inbuf, %d on outbuf.",
547 conn_type_to_string(conn->type),
548 conn_state_to_string(conn->type, conn->state),
549 (int)connection_get_inbuf_len(conn),
550 (int)connection_get_outbuf_len(conn));
553 if (!connection_is_listener(conn)) {
554 buf_free(conn->inbuf);
555 buf_free(conn->outbuf);
556 } else {
557 if (conn->socket_family == AF_UNIX) {
558 /* For now only control and SOCKS ports can be Unix domain sockets
559 * and listeners at the same time */
560 tor_assert(conn_listener_type_supports_af_unix(conn->type));
562 if (unlink(conn->address) < 0 && errno != ENOENT) {
563 log_warn(LD_NET, "Could not unlink %s: %s", conn->address,
564 strerror(errno));
569 tor_free(conn->address);
571 if (connection_speaks_cells(conn)) {
572 or_connection_t *or_conn = TO_OR_CONN(conn);
573 tor_tls_free(or_conn->tls);
574 or_conn->tls = NULL;
575 or_handshake_state_free(or_conn->handshake_state);
576 or_conn->handshake_state = NULL;
577 tor_free(or_conn->nickname);
578 if (or_conn->chan) {
579 /* Owww, this shouldn't happen, but... */
580 log_info(LD_CHANNEL,
581 "Freeing orconn at %p, saw channel %p with ID "
582 U64_FORMAT " left un-NULLed",
583 or_conn, TLS_CHAN_TO_BASE(or_conn->chan),
584 U64_PRINTF_ARG(
585 TLS_CHAN_TO_BASE(or_conn->chan)->global_identifier));
586 if (!CHANNEL_FINISHED(TLS_CHAN_TO_BASE(or_conn->chan))) {
587 channel_close_for_error(TLS_CHAN_TO_BASE(or_conn->chan));
590 or_conn->chan->conn = NULL;
591 or_conn->chan = NULL;
594 if (conn->type == CONN_TYPE_AP) {
595 entry_connection_t *entry_conn = TO_ENTRY_CONN(conn);
596 tor_free(entry_conn->chosen_exit_name);
597 tor_free(entry_conn->original_dest_address);
598 if (entry_conn->socks_request)
599 socks_request_free(entry_conn->socks_request);
600 if (entry_conn->pending_optimistic_data) {
601 buf_free(entry_conn->pending_optimistic_data);
603 if (entry_conn->sending_optimistic_data) {
604 buf_free(entry_conn->sending_optimistic_data);
607 if (CONN_IS_EDGE(conn)) {
608 rend_data_free(TO_EDGE_CONN(conn)->rend_data);
609 hs_ident_edge_conn_free(TO_EDGE_CONN(conn)->hs_ident);
611 if (conn->type == CONN_TYPE_CONTROL) {
612 control_connection_t *control_conn = TO_CONTROL_CONN(conn);
613 tor_free(control_conn->safecookie_client_hash);
614 tor_free(control_conn->incoming_cmd);
615 if (control_conn->ephemeral_onion_services) {
616 SMARTLIST_FOREACH(control_conn->ephemeral_onion_services, char *, cp, {
617 memwipe(cp, 0, strlen(cp));
618 tor_free(cp);
620 smartlist_free(control_conn->ephemeral_onion_services);
624 /* Probably already freed by connection_free. */
625 tor_event_free(conn->read_event);
626 tor_event_free(conn->write_event);
627 conn->read_event = conn->write_event = NULL;
629 if (conn->type == CONN_TYPE_DIR) {
630 dir_connection_t *dir_conn = TO_DIR_CONN(conn);
631 tor_free(dir_conn->requested_resource);
633 tor_compress_free(dir_conn->compress_state);
634 if (dir_conn->spool) {
635 SMARTLIST_FOREACH(dir_conn->spool, spooled_resource_t *, spooled,
636 spooled_resource_free(spooled));
637 smartlist_free(dir_conn->spool);
640 rend_data_free(dir_conn->rend_data);
641 hs_ident_dir_conn_free(dir_conn->hs_ident);
642 if (dir_conn->guard_state) {
643 /* Cancel before freeing, if it's still there. */
644 entry_guard_cancel(&dir_conn->guard_state);
646 circuit_guard_state_free(dir_conn->guard_state);
649 if (SOCKET_OK(conn->s)) {
650 log_debug(LD_NET,"closing fd %d.",(int)conn->s);
651 tor_close_socket(conn->s);
652 conn->s = TOR_INVALID_SOCKET;
655 if (conn->type == CONN_TYPE_OR &&
656 !tor_digest_is_zero(TO_OR_CONN(conn)->identity_digest)) {
657 log_warn(LD_BUG, "called on OR conn with non-zeroed identity_digest");
658 connection_or_clear_identity(TO_OR_CONN(conn));
660 if (conn->type == CONN_TYPE_OR || conn->type == CONN_TYPE_EXT_OR) {
661 connection_or_remove_from_ext_or_id_map(TO_OR_CONN(conn));
662 tor_free(TO_OR_CONN(conn)->ext_or_conn_id);
663 tor_free(TO_OR_CONN(conn)->ext_or_auth_correct_client_hash);
664 tor_free(TO_OR_CONN(conn)->ext_or_transport);
667 memwipe(mem, 0xCC, memlen); /* poison memory */
668 tor_free(mem);
671 /** Make sure <b>conn</b> isn't in any of the global conn lists; then free it.
673 MOCK_IMPL(void,
674 connection_free,(connection_t *conn))
676 if (!conn)
677 return;
678 tor_assert(!connection_is_on_closeable_list(conn));
679 tor_assert(!connection_in_array(conn));
680 if (BUG(conn->linked_conn)) {
681 conn->linked_conn->linked_conn = NULL;
682 if (! conn->linked_conn->marked_for_close &&
683 conn->linked_conn->reading_from_linked_conn)
684 connection_start_reading(conn->linked_conn);
685 conn->linked_conn = NULL;
687 if (connection_speaks_cells(conn)) {
688 if (!tor_digest_is_zero(TO_OR_CONN(conn)->identity_digest)) {
689 connection_or_clear_identity(TO_OR_CONN(conn));
692 if (conn->type == CONN_TYPE_CONTROL) {
693 connection_control_closed(TO_CONTROL_CONN(conn));
695 #if 1
696 /* DEBUGGING */
697 if (conn->type == CONN_TYPE_AP) {
698 connection_ap_warn_and_unmark_if_pending_circ(TO_ENTRY_CONN(conn),
699 "connection_free");
701 #endif
702 connection_unregister_events(conn);
703 connection_free_(conn);
707 * Called when we're about to finally unlink and free a connection:
708 * perform necessary accounting and cleanup
709 * - Directory conns that failed to fetch a rendezvous descriptor
710 * need to inform pending rendezvous streams.
711 * - OR conns need to call rep_hist_note_*() to record status.
712 * - AP conns need to send a socks reject if necessary.
713 * - Exit conns need to call connection_dns_remove() if necessary.
714 * - AP and Exit conns need to send an end cell if they can.
715 * - DNS conns need to fail any resolves that are pending on them.
716 * - OR and edge connections need to be unlinked from circuits.
718 void
719 connection_about_to_close_connection(connection_t *conn)
721 tor_assert(conn->marked_for_close);
723 switch (conn->type) {
724 case CONN_TYPE_DIR:
725 connection_dir_about_to_close(TO_DIR_CONN(conn));
726 break;
727 case CONN_TYPE_OR:
728 case CONN_TYPE_EXT_OR:
729 connection_or_about_to_close(TO_OR_CONN(conn));
730 break;
731 case CONN_TYPE_AP:
732 connection_ap_about_to_close(TO_ENTRY_CONN(conn));
733 break;
734 case CONN_TYPE_EXIT:
735 connection_exit_about_to_close(TO_EDGE_CONN(conn));
736 break;
740 /** Return true iff connection_close_immediate() has been called on this
741 * connection. */
742 #define CONN_IS_CLOSED(c) \
743 ((c)->linked ? ((c)->linked_conn_is_closed) : (! SOCKET_OK(c->s)))
745 /** Close the underlying socket for <b>conn</b>, so we don't try to
746 * flush it. Must be used in conjunction with (right before)
747 * connection_mark_for_close().
749 void
750 connection_close_immediate(connection_t *conn)
752 assert_connection_ok(conn,0);
753 if (CONN_IS_CLOSED(conn)) {
754 log_err(LD_BUG,"Attempt to close already-closed connection.");
755 tor_fragile_assert();
756 return;
758 if (conn->outbuf_flushlen) {
759 log_info(LD_NET,"fd %d, type %s, state %s, %d bytes on outbuf.",
760 (int)conn->s, conn_type_to_string(conn->type),
761 conn_state_to_string(conn->type, conn->state),
762 (int)conn->outbuf_flushlen);
765 connection_unregister_events(conn);
767 if (SOCKET_OK(conn->s))
768 tor_close_socket(conn->s);
769 conn->s = TOR_INVALID_SOCKET;
770 if (conn->linked)
771 conn->linked_conn_is_closed = 1;
772 if (conn->outbuf)
773 buf_clear(conn->outbuf);
774 conn->outbuf_flushlen = 0;
777 /** Mark <b>conn</b> to be closed next time we loop through
778 * conn_close_if_marked() in main.c. */
779 void
780 connection_mark_for_close_(connection_t *conn, int line, const char *file)
782 assert_connection_ok(conn,0);
783 tor_assert(line);
784 tor_assert(line < 1<<16); /* marked_for_close can only fit a uint16_t. */
785 tor_assert(file);
787 if (conn->type == CONN_TYPE_OR) {
789 * An or_connection should have been closed through one of the channel-
790 * aware functions in connection_or.c. We'll assume this is an error
791 * close and do that, and log a bug warning.
793 log_warn(LD_CHANNEL | LD_BUG,
794 "Something tried to close an or_connection_t without going "
795 "through channels at %s:%d",
796 file, line);
797 connection_or_close_for_error(TO_OR_CONN(conn), 0);
798 } else {
799 /* Pass it down to the real function */
800 connection_mark_for_close_internal_(conn, line, file);
804 /** Mark <b>conn</b> to be closed next time we loop through
805 * conn_close_if_marked() in main.c; the _internal version bypasses the
806 * CONN_TYPE_OR checks; this should be called when you either are sure that
807 * if this is an or_connection_t the controlling channel has been notified
808 * (e.g. with connection_or_notify_error()), or you actually are the
809 * connection_or_close_for_error() or connection_or_close_normally function.
810 * For all other cases, use connection_mark_and_flush() instead, which
811 * checks for or_connection_t properly, instead. See below.
813 MOCK_IMPL(void,
814 connection_mark_for_close_internal_, (connection_t *conn,
815 int line, const char *file))
817 assert_connection_ok(conn,0);
818 tor_assert(line);
819 tor_assert(line < 1<<16); /* marked_for_close can only fit a uint16_t. */
820 tor_assert(file);
822 if (conn->marked_for_close) {
823 log_warn(LD_BUG,"Duplicate call to connection_mark_for_close at %s:%d"
824 " (first at %s:%d)", file, line, conn->marked_for_close_file,
825 conn->marked_for_close);
826 tor_fragile_assert();
827 return;
830 if (conn->type == CONN_TYPE_OR) {
832 * Bad news if this happens without telling the controlling channel; do
833 * this so we can find things that call this wrongly when the asserts hit.
835 log_debug(LD_CHANNEL,
836 "Calling connection_mark_for_close_internal_() on an OR conn "
837 "at %s:%d",
838 file, line);
841 conn->marked_for_close = line;
842 conn->marked_for_close_file = file;
843 add_connection_to_closeable_list(conn);
845 /* in case we're going to be held-open-til-flushed, reset
846 * the number of seconds since last successful write, so
847 * we get our whole 15 seconds */
848 conn->timestamp_lastwritten = time(NULL);
851 /** Find each connection that has hold_open_until_flushed set to
852 * 1 but hasn't written in the past 15 seconds, and set
853 * hold_open_until_flushed to 0. This means it will get cleaned
854 * up in the next loop through close_if_marked() in main.c.
856 void
857 connection_expire_held_open(void)
859 time_t now;
860 smartlist_t *conns = get_connection_array();
862 now = time(NULL);
864 SMARTLIST_FOREACH_BEGIN(conns, connection_t *, conn) {
865 /* If we've been holding the connection open, but we haven't written
866 * for 15 seconds...
868 if (conn->hold_open_until_flushed) {
869 tor_assert(conn->marked_for_close);
870 if (now - conn->timestamp_lastwritten >= 15) {
871 int severity;
872 if (conn->type == CONN_TYPE_EXIT ||
873 (conn->type == CONN_TYPE_DIR &&
874 conn->purpose == DIR_PURPOSE_SERVER))
875 severity = LOG_INFO;
876 else
877 severity = LOG_NOTICE;
878 log_fn(severity, LD_NET,
879 "Giving up on marked_for_close conn that's been flushing "
880 "for 15s (fd %d, type %s, state %s).",
881 (int)conn->s, conn_type_to_string(conn->type),
882 conn_state_to_string(conn->type, conn->state));
883 conn->hold_open_until_flushed = 0;
886 } SMARTLIST_FOREACH_END(conn);
889 #if defined(HAVE_SYS_UN_H) || defined(RUNNING_DOXYGEN)
890 /** Create an AF_UNIX listenaddr struct.
891 * <b>listenaddress</b> provides the path to the Unix socket.
893 * Eventually <b>listenaddress</b> will also optionally contain user, group,
894 * and file permissions for the new socket. But not yet. XXX
895 * Also, since we do not create the socket here the information doesn't help
896 * here.
898 * If not NULL <b>readable_address</b> will contain a copy of the path part of
899 * <b>listenaddress</b>.
901 * The listenaddr struct has to be freed by the caller.
903 static struct sockaddr_un *
904 create_unix_sockaddr(const char *listenaddress, char **readable_address,
905 socklen_t *len_out)
907 struct sockaddr_un *sockaddr = NULL;
909 sockaddr = tor_malloc_zero(sizeof(struct sockaddr_un));
910 sockaddr->sun_family = AF_UNIX;
911 if (strlcpy(sockaddr->sun_path, listenaddress, sizeof(sockaddr->sun_path))
912 >= sizeof(sockaddr->sun_path)) {
913 log_warn(LD_CONFIG, "Unix socket path '%s' is too long to fit.",
914 escaped(listenaddress));
915 tor_free(sockaddr);
916 return NULL;
919 if (readable_address)
920 *readable_address = tor_strdup(listenaddress);
922 *len_out = sizeof(struct sockaddr_un);
923 return sockaddr;
925 #else
926 static struct sockaddr *
927 create_unix_sockaddr(const char *listenaddress, char **readable_address,
928 socklen_t *len_out)
930 (void)listenaddress;
931 (void)readable_address;
932 log_fn(LOG_ERR, LD_BUG,
933 "Unix domain sockets not supported, yet we tried to create one.");
934 *len_out = 0;
935 tor_fragile_assert();
936 return NULL;
938 #endif /* HAVE_SYS_UN_H */
940 /** Warn that an accept or a connect has failed because we're running out of
941 * TCP sockets we can use on current system. Rate-limit these warnings so
942 * that we don't spam the log. */
943 static void
944 warn_too_many_conns(void)
946 #define WARN_TOO_MANY_CONNS_INTERVAL (6*60*60)
947 static ratelim_t last_warned = RATELIM_INIT(WARN_TOO_MANY_CONNS_INTERVAL);
948 char *m;
949 if ((m = rate_limit_log(&last_warned, approx_time()))) {
950 int n_conns = get_n_open_sockets();
951 log_warn(LD_NET,"Failing because we have %d connections already. Please "
952 "read doc/TUNING for guidance.%s", n_conns, m);
953 tor_free(m);
954 control_event_general_status(LOG_WARN, "TOO_MANY_CONNECTIONS CURRENT=%d",
955 n_conns);
959 #ifdef HAVE_SYS_UN_H
961 #define UNIX_SOCKET_PURPOSE_CONTROL_SOCKET 0
962 #define UNIX_SOCKET_PURPOSE_SOCKS_SOCKET 1
964 /** Check if the purpose isn't one of the ones we know what to do with */
966 static int
967 is_valid_unix_socket_purpose(int purpose)
969 int valid = 0;
971 switch (purpose) {
972 case UNIX_SOCKET_PURPOSE_CONTROL_SOCKET:
973 case UNIX_SOCKET_PURPOSE_SOCKS_SOCKET:
974 valid = 1;
975 break;
978 return valid;
981 /** Return a string description of a unix socket purpose */
982 static const char *
983 unix_socket_purpose_to_string(int purpose)
985 const char *s = "unknown-purpose socket";
987 switch (purpose) {
988 case UNIX_SOCKET_PURPOSE_CONTROL_SOCKET:
989 s = "control socket";
990 break;
991 case UNIX_SOCKET_PURPOSE_SOCKS_SOCKET:
992 s = "SOCKS socket";
993 break;
996 return s;
999 /** Check whether we should be willing to open an AF_UNIX socket in
1000 * <b>path</b>. Return 0 if we should go ahead and -1 if we shouldn't. */
1001 static int
1002 check_location_for_unix_socket(const or_options_t *options, const char *path,
1003 int purpose, const port_cfg_t *port)
1005 int r = -1;
1006 char *p = NULL;
1008 tor_assert(is_valid_unix_socket_purpose(purpose));
1010 p = tor_strdup(path);
1011 cpd_check_t flags = CPD_CHECK_MODE_ONLY;
1012 if (get_parent_directory(p)<0 || p[0] != '/') {
1013 log_warn(LD_GENERAL, "Bad unix socket address '%s'. Tor does not support "
1014 "relative paths for unix sockets.", path);
1015 goto done;
1018 if (port->is_world_writable) {
1019 /* World-writable sockets can go anywhere. */
1020 r = 0;
1021 goto done;
1024 if (port->is_group_writable) {
1025 flags |= CPD_GROUP_OK;
1028 if (port->relax_dirmode_check) {
1029 flags |= CPD_RELAX_DIRMODE_CHECK;
1032 if (check_private_dir(p, flags, options->User) < 0) {
1033 char *escpath, *escdir;
1034 escpath = esc_for_log(path);
1035 escdir = esc_for_log(p);
1036 log_warn(LD_GENERAL, "Before Tor can create a %s in %s, the directory "
1037 "%s needs to exist, and to be accessible only by the user%s "
1038 "account that is running Tor. (On some Unix systems, anybody "
1039 "who can list a socket can connect to it, so Tor is being "
1040 "careful.)",
1041 unix_socket_purpose_to_string(purpose), escpath, escdir,
1042 port->is_group_writable ? " and group" : "");
1043 tor_free(escpath);
1044 tor_free(escdir);
1045 goto done;
1048 r = 0;
1049 done:
1050 tor_free(p);
1051 return r;
1053 #endif
1055 /** Tell the TCP stack that it shouldn't wait for a long time after
1056 * <b>sock</b> has closed before reusing its port. Return 0 on success,
1057 * -1 on failure. */
1058 static int
1059 make_socket_reuseable(tor_socket_t sock)
1061 #ifdef _WIN32
1062 (void) sock;
1063 return 0;
1064 #else
1065 int one=1;
1067 /* REUSEADDR on normal places means you can rebind to the port
1068 * right after somebody else has let it go. But REUSEADDR on win32
1069 * means you can bind to the port _even when somebody else
1070 * already has it bound_. So, don't do that on Win32. */
1071 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void*) &one,
1072 (socklen_t)sizeof(one)) == -1) {
1073 return -1;
1075 return 0;
1076 #endif
1079 #ifdef _WIN32
1080 /** Tell the Windows TCP stack to prevent other applications from receiving
1081 * traffic from tor's open ports. Return 0 on success, -1 on failure. */
1082 static int
1083 make_win32_socket_exclusive(tor_socket_t sock)
1085 #ifdef SO_EXCLUSIVEADDRUSE
1086 int one=1;
1088 /* Any socket that sets REUSEADDR on win32 can bind to a port _even when
1089 * somebody else already has it bound_, and _even if the original socket
1090 * didn't set REUSEADDR_. Use EXCLUSIVEADDRUSE to prevent this port-stealing
1091 * on win32. */
1092 if (setsockopt(sock, SOL_SOCKET, SO_EXCLUSIVEADDRUSE, (void*) &one,
1093 (socklen_t)sizeof(one))) {
1094 return -1;
1096 return 0;
1097 #else
1098 (void) sock;
1099 return 0;
1100 #endif
1102 #endif
1104 /** Max backlog to pass to listen. We start at */
1105 static int listen_limit = INT_MAX;
1107 /* Listen on <b>fd</b> with appropriate backlog. Return as for listen. */
1108 static int
1109 tor_listen(tor_socket_t fd)
1111 int r;
1113 if ((r = listen(fd, listen_limit)) < 0) {
1114 if (listen_limit == SOMAXCONN)
1115 return r;
1116 if ((r = listen(fd, SOMAXCONN)) == 0) {
1117 listen_limit = SOMAXCONN;
1118 log_warn(LD_NET, "Setting listen backlog to INT_MAX connections "
1119 "didn't work, but SOMAXCONN did. Lowering backlog limit.");
1122 return r;
1125 /** Bind a new non-blocking socket listening to the socket described
1126 * by <b>listensockaddr</b>.
1128 * <b>address</b> is only used for logging purposes and to add the information
1129 * to the conn.
1131 static connection_t *
1132 connection_listener_new(const struct sockaddr *listensockaddr,
1133 socklen_t socklen,
1134 int type, const char *address,
1135 const port_cfg_t *port_cfg)
1137 listener_connection_t *lis_conn;
1138 connection_t *conn = NULL;
1139 tor_socket_t s = TOR_INVALID_SOCKET; /* the socket we're going to make */
1140 or_options_t const *options = get_options();
1141 (void) options; /* Windows doesn't use this. */
1142 #if defined(HAVE_PWD_H) && defined(HAVE_SYS_UN_H)
1143 const struct passwd *pw = NULL;
1144 #endif
1145 uint16_t usePort = 0, gotPort = 0;
1146 int start_reading = 0;
1147 static int global_next_session_group = SESSION_GROUP_FIRST_AUTO;
1148 tor_addr_t addr;
1149 int exhaustion = 0;
1151 if (listensockaddr->sa_family == AF_INET ||
1152 listensockaddr->sa_family == AF_INET6) {
1153 int is_stream = (type != CONN_TYPE_AP_DNS_LISTENER);
1154 if (is_stream)
1155 start_reading = 1;
1157 tor_addr_from_sockaddr(&addr, listensockaddr, &usePort);
1158 log_notice(LD_NET, "Opening %s on %s",
1159 conn_type_to_string(type), fmt_addrport(&addr, usePort));
1161 s = tor_open_socket_nonblocking(tor_addr_family(&addr),
1162 is_stream ? SOCK_STREAM : SOCK_DGRAM,
1163 is_stream ? IPPROTO_TCP: IPPROTO_UDP);
1164 if (!SOCKET_OK(s)) {
1165 int e = tor_socket_errno(s);
1166 if (ERRNO_IS_RESOURCE_LIMIT(e)) {
1167 warn_too_many_conns();
1169 * We'll call the OOS handler at the error exit, so set the
1170 * exhaustion flag for it.
1172 exhaustion = 1;
1173 } else {
1174 log_warn(LD_NET, "Socket creation failed: %s",
1175 tor_socket_strerror(e));
1177 goto err;
1180 if (make_socket_reuseable(s) < 0) {
1181 log_warn(LD_NET, "Error setting SO_REUSEADDR flag on %s: %s",
1182 conn_type_to_string(type),
1183 tor_socket_strerror(errno));
1186 #ifdef _WIN32
1187 if (make_win32_socket_exclusive(s) < 0) {
1188 log_warn(LD_NET, "Error setting SO_EXCLUSIVEADDRUSE flag on %s: %s",
1189 conn_type_to_string(type),
1190 tor_socket_strerror(errno));
1192 #endif
1194 #if defined(USE_TRANSPARENT) && defined(IP_TRANSPARENT)
1195 if (options->TransProxyType_parsed == TPT_TPROXY &&
1196 type == CONN_TYPE_AP_TRANS_LISTENER) {
1197 int one = 1;
1198 if (setsockopt(s, SOL_IP, IP_TRANSPARENT, (void*)&one,
1199 (socklen_t)sizeof(one)) < 0) {
1200 const char *extra = "";
1201 int e = tor_socket_errno(s);
1202 if (e == EPERM)
1203 extra = "TransTPROXY requires root privileges or similar"
1204 " capabilities.";
1205 log_warn(LD_NET, "Error setting IP_TRANSPARENT flag: %s.%s",
1206 tor_socket_strerror(e), extra);
1209 #endif
1211 #ifdef IPV6_V6ONLY
1212 if (listensockaddr->sa_family == AF_INET6) {
1213 int one = 1;
1214 /* We need to set IPV6_V6ONLY so that this socket can't get used for
1215 * IPv4 connections. */
1216 if (setsockopt(s,IPPROTO_IPV6, IPV6_V6ONLY,
1217 (void*)&one, (socklen_t)sizeof(one)) < 0) {
1218 int e = tor_socket_errno(s);
1219 log_warn(LD_NET, "Error setting IPV6_V6ONLY flag: %s",
1220 tor_socket_strerror(e));
1221 /* Keep going; probably not harmful. */
1224 #endif
1226 if (bind(s,listensockaddr,socklen) < 0) {
1227 const char *helpfulhint = "";
1228 int e = tor_socket_errno(s);
1229 if (ERRNO_IS_EADDRINUSE(e))
1230 helpfulhint = ". Is Tor already running?";
1231 log_warn(LD_NET, "Could not bind to %s:%u: %s%s", address, usePort,
1232 tor_socket_strerror(e), helpfulhint);
1233 goto err;
1236 if (is_stream) {
1237 if (tor_listen(s) < 0) {
1238 log_warn(LD_NET, "Could not listen on %s:%u: %s", address, usePort,
1239 tor_socket_strerror(tor_socket_errno(s)));
1240 goto err;
1244 if (usePort != 0) {
1245 gotPort = usePort;
1246 } else {
1247 tor_addr_t addr2;
1248 struct sockaddr_storage ss;
1249 socklen_t ss_len=sizeof(ss);
1250 if (getsockname(s, (struct sockaddr*)&ss, &ss_len)<0) {
1251 log_warn(LD_NET, "getsockname() couldn't learn address for %s: %s",
1252 conn_type_to_string(type),
1253 tor_socket_strerror(tor_socket_errno(s)));
1254 gotPort = 0;
1256 tor_addr_from_sockaddr(&addr2, (struct sockaddr*)&ss, &gotPort);
1258 #ifdef HAVE_SYS_UN_H
1260 * AF_UNIX generic setup stuff
1262 } else if (listensockaddr->sa_family == AF_UNIX) {
1263 /* We want to start reading for both AF_UNIX cases */
1264 start_reading = 1;
1266 tor_assert(conn_listener_type_supports_af_unix(type));
1268 if (check_location_for_unix_socket(options, address,
1269 (type == CONN_TYPE_CONTROL_LISTENER) ?
1270 UNIX_SOCKET_PURPOSE_CONTROL_SOCKET :
1271 UNIX_SOCKET_PURPOSE_SOCKS_SOCKET, port_cfg) < 0) {
1272 goto err;
1275 log_notice(LD_NET, "Opening %s on %s",
1276 conn_type_to_string(type), address);
1278 tor_addr_make_unspec(&addr);
1280 if (unlink(address) < 0 && errno != ENOENT) {
1281 log_warn(LD_NET, "Could not unlink %s: %s", address,
1282 strerror(errno));
1283 goto err;
1286 s = tor_open_socket_nonblocking(AF_UNIX, SOCK_STREAM, 0);
1287 if (! SOCKET_OK(s)) {
1288 int e = tor_socket_errno(s);
1289 if (ERRNO_IS_RESOURCE_LIMIT(e)) {
1290 warn_too_many_conns();
1292 * We'll call the OOS handler at the error exit, so set the
1293 * exhaustion flag for it.
1295 exhaustion = 1;
1296 } else {
1297 log_warn(LD_NET,"Socket creation failed: %s.", strerror(e));
1299 goto err;
1302 if (bind(s, listensockaddr,
1303 (socklen_t)sizeof(struct sockaddr_un)) == -1) {
1304 log_warn(LD_NET,"Bind to %s failed: %s.", address,
1305 tor_socket_strerror(tor_socket_errno(s)));
1306 goto err;
1309 #ifdef HAVE_PWD_H
1310 if (options->User) {
1311 pw = tor_getpwnam(options->User);
1312 struct stat st;
1313 if (pw == NULL) {
1314 log_warn(LD_NET,"Unable to chown() %s socket: user %s not found.",
1315 address, options->User);
1316 goto err;
1317 } else if (fstat(s, &st) == 0 &&
1318 st.st_uid == pw->pw_uid && st.st_gid == pw->pw_gid) {
1319 /* No change needed */
1320 } else if (chown(sandbox_intern_string(address),
1321 pw->pw_uid, pw->pw_gid) < 0) {
1322 log_warn(LD_NET,"Unable to chown() %s socket: %s.",
1323 address, strerror(errno));
1324 goto err;
1327 #endif
1330 unsigned mode;
1331 const char *status;
1332 struct stat st;
1333 if (port_cfg->is_world_writable) {
1334 mode = 0666;
1335 status = "world-writable";
1336 } else if (port_cfg->is_group_writable) {
1337 mode = 0660;
1338 status = "group-writable";
1339 } else {
1340 mode = 0600;
1341 status = "private";
1343 /* We need to use chmod; fchmod doesn't work on sockets on all
1344 * platforms. */
1345 if (fstat(s, &st) == 0 && (st.st_mode & 0777) == mode) {
1346 /* no change needed */
1347 } else if (chmod(sandbox_intern_string(address), mode) < 0) {
1348 log_warn(LD_FS,"Unable to make %s %s.", address, status);
1349 goto err;
1353 if (listen(s, SOMAXCONN) < 0) {
1354 log_warn(LD_NET, "Could not listen on %s: %s", address,
1355 tor_socket_strerror(tor_socket_errno(s)));
1356 goto err;
1358 #endif /* HAVE_SYS_UN_H */
1359 } else {
1360 log_err(LD_BUG, "Got unexpected address family %d.",
1361 listensockaddr->sa_family);
1362 tor_assert(0);
1365 lis_conn = listener_connection_new(type, listensockaddr->sa_family);
1366 conn = TO_CONN(lis_conn);
1367 conn->socket_family = listensockaddr->sa_family;
1368 conn->s = s;
1369 s = TOR_INVALID_SOCKET; /* Prevent double-close */
1370 conn->address = tor_strdup(address);
1371 conn->port = gotPort;
1372 tor_addr_copy(&conn->addr, &addr);
1374 memcpy(&lis_conn->entry_cfg, &port_cfg->entry_cfg, sizeof(entry_port_cfg_t));
1376 if (port_cfg->entry_cfg.isolation_flags) {
1377 lis_conn->entry_cfg.isolation_flags = port_cfg->entry_cfg.isolation_flags;
1378 if (port_cfg->entry_cfg.session_group >= 0) {
1379 lis_conn->entry_cfg.session_group = port_cfg->entry_cfg.session_group;
1380 } else {
1381 /* This can wrap after around INT_MAX listeners are opened. But I don't
1382 * believe that matters, since you would need to open a ridiculous
1383 * number of listeners while keeping the early ones open before you ever
1384 * hit this. An OR with a dozen ports open, for example, would have to
1385 * close and re-open its listeners every second for 4 years nonstop.
1387 lis_conn->entry_cfg.session_group = global_next_session_group--;
1391 if (type != CONN_TYPE_AP_LISTENER) {
1392 lis_conn->entry_cfg.ipv4_traffic = 1;
1393 lis_conn->entry_cfg.ipv6_traffic = 1;
1394 lis_conn->entry_cfg.prefer_ipv6 = 0;
1397 if (connection_add(conn) < 0) { /* no space, forget it */
1398 log_warn(LD_NET,"connection_add for listener failed. Giving up.");
1399 goto err;
1402 log_fn(usePort==gotPort ? LOG_DEBUG : LOG_NOTICE, LD_NET,
1403 "%s listening on port %u.",
1404 conn_type_to_string(type), gotPort);
1406 conn->state = LISTENER_STATE_READY;
1407 if (start_reading) {
1408 connection_start_reading(conn);
1409 } else {
1410 tor_assert(type == CONN_TYPE_AP_DNS_LISTENER);
1411 dnsserv_configure_listener(conn);
1415 * Normal exit; call the OOS handler since connection count just changed;
1416 * the exhaustion flag will always be zero here though.
1418 connection_check_oos(get_n_open_sockets(), 0);
1420 return conn;
1422 err:
1423 if (SOCKET_OK(s))
1424 tor_close_socket(s);
1425 if (conn)
1426 connection_free(conn);
1428 /* Call the OOS handler, indicate if we saw an exhaustion-related error */
1429 connection_check_oos(get_n_open_sockets(), exhaustion);
1431 return NULL;
1434 /** Do basic sanity checking on a newly received socket. Return 0
1435 * if it looks ok, else return -1.
1437 * Notably, some TCP stacks can erroneously have accept() return successfully
1438 * with socklen 0, when the client sends an RST before the accept call (as
1439 * nmap does). We want to detect that, and not go on with the connection.
1441 static int
1442 check_sockaddr(const struct sockaddr *sa, int len, int level)
1444 int ok = 1;
1446 if (sa->sa_family == AF_INET) {
1447 struct sockaddr_in *sin=(struct sockaddr_in*)sa;
1448 if (len != sizeof(struct sockaddr_in)) {
1449 log_fn(level, LD_NET, "Length of address not as expected: %d vs %d",
1450 len,(int)sizeof(struct sockaddr_in));
1451 ok = 0;
1453 if (sin->sin_addr.s_addr == 0 || sin->sin_port == 0) {
1454 log_fn(level, LD_NET,
1455 "Address for new connection has address/port equal to zero.");
1456 ok = 0;
1458 } else if (sa->sa_family == AF_INET6) {
1459 struct sockaddr_in6 *sin6=(struct sockaddr_in6*)sa;
1460 if (len != sizeof(struct sockaddr_in6)) {
1461 log_fn(level, LD_NET, "Length of address not as expected: %d vs %d",
1462 len,(int)sizeof(struct sockaddr_in6));
1463 ok = 0;
1465 if (tor_mem_is_zero((void*)sin6->sin6_addr.s6_addr, 16) ||
1466 sin6->sin6_port == 0) {
1467 log_fn(level, LD_NET,
1468 "Address for new connection has address/port equal to zero.");
1469 ok = 0;
1471 } else if (sa->sa_family == AF_UNIX) {
1472 ok = 1;
1473 } else {
1474 ok = 0;
1476 return ok ? 0 : -1;
1479 /** Check whether the socket family from an accepted socket <b>got</b> is the
1480 * same as the one that <b>listener</b> is waiting for. If it isn't, log
1481 * a useful message and return -1. Else return 0.
1483 * This is annoying, but can apparently happen on some Darwins. */
1484 static int
1485 check_sockaddr_family_match(sa_family_t got, connection_t *listener)
1487 if (got != listener->socket_family) {
1488 log_info(LD_BUG, "A listener connection returned a socket with a "
1489 "mismatched family. %s for addr_family %d gave us a socket "
1490 "with address family %d. Dropping.",
1491 conn_type_to_string(listener->type),
1492 (int)listener->socket_family,
1493 (int)got);
1494 return -1;
1496 return 0;
1499 /** The listener connection <b>conn</b> told poll() it wanted to read.
1500 * Call accept() on conn-\>s, and add the new connection if necessary.
1502 static int
1503 connection_handle_listener_read(connection_t *conn, int new_type)
1505 tor_socket_t news; /* the new socket */
1506 connection_t *newconn = 0;
1507 /* information about the remote peer when connecting to other routers */
1508 struct sockaddr_storage addrbuf;
1509 struct sockaddr *remote = (struct sockaddr*)&addrbuf;
1510 /* length of the remote address. Must be whatever accept() needs. */
1511 socklen_t remotelen = (socklen_t)sizeof(addrbuf);
1512 const or_options_t *options = get_options();
1514 tor_assert((size_t)remotelen >= sizeof(struct sockaddr_in));
1515 memset(&addrbuf, 0, sizeof(addrbuf));
1517 news = tor_accept_socket_nonblocking(conn->s,remote,&remotelen);
1518 if (!SOCKET_OK(news)) { /* accept() error */
1519 int e = tor_socket_errno(conn->s);
1520 if (ERRNO_IS_ACCEPT_EAGAIN(e)) {
1522 * they hung up before we could accept(). that's fine.
1524 * give the OOS handler a chance to run though
1526 connection_check_oos(get_n_open_sockets(), 0);
1527 return 0;
1528 } else if (ERRNO_IS_RESOURCE_LIMIT(e)) {
1529 warn_too_many_conns();
1530 /* Exhaustion; tell the OOS handler */
1531 connection_check_oos(get_n_open_sockets(), 1);
1532 return 0;
1534 /* else there was a real error. */
1535 log_warn(LD_NET,"accept() failed: %s. Closing listener.",
1536 tor_socket_strerror(e));
1537 connection_mark_for_close(conn);
1538 /* Tell the OOS handler about this too */
1539 connection_check_oos(get_n_open_sockets(), 0);
1540 return -1;
1542 log_debug(LD_NET,
1543 "Connection accepted on socket %d (child of fd %d).",
1544 (int)news,(int)conn->s);
1546 /* We accepted a new conn; run OOS handler */
1547 connection_check_oos(get_n_open_sockets(), 0);
1549 if (make_socket_reuseable(news) < 0) {
1550 if (tor_socket_errno(news) == EINVAL) {
1551 /* This can happen on OSX if we get a badly timed shutdown. */
1552 log_debug(LD_NET, "make_socket_reuseable returned EINVAL");
1553 } else {
1554 log_warn(LD_NET, "Error setting SO_REUSEADDR flag on %s: %s",
1555 conn_type_to_string(new_type),
1556 tor_socket_strerror(errno));
1558 tor_close_socket(news);
1559 return 0;
1562 if (options->ConstrainedSockets)
1563 set_constrained_socket_buffers(news, (int)options->ConstrainedSockSize);
1565 if (check_sockaddr_family_match(remote->sa_family, conn) < 0) {
1566 tor_close_socket(news);
1567 return 0;
1570 if (conn->socket_family == AF_INET || conn->socket_family == AF_INET6 ||
1571 (conn->socket_family == AF_UNIX && new_type == CONN_TYPE_AP)) {
1572 tor_addr_t addr;
1573 uint16_t port;
1574 if (check_sockaddr(remote, remotelen, LOG_INFO)<0) {
1575 log_info(LD_NET,
1576 "accept() returned a strange address; closing connection.");
1577 tor_close_socket(news);
1578 return 0;
1581 tor_addr_from_sockaddr(&addr, remote, &port);
1583 /* process entrance policies here, before we even create the connection */
1584 if (new_type == CONN_TYPE_AP) {
1585 /* check sockspolicy to see if we should accept it */
1586 if (socks_policy_permits_address(&addr) == 0) {
1587 log_notice(LD_APP,
1588 "Denying socks connection from untrusted address %s.",
1589 fmt_and_decorate_addr(&addr));
1590 tor_close_socket(news);
1591 return 0;
1594 if (new_type == CONN_TYPE_DIR) {
1595 /* check dirpolicy to see if we should accept it */
1596 if (dir_policy_permits_address(&addr) == 0) {
1597 log_notice(LD_DIRSERV,"Denying dir connection from address %s.",
1598 fmt_and_decorate_addr(&addr));
1599 tor_close_socket(news);
1600 return 0;
1604 newconn = connection_new(new_type, conn->socket_family);
1605 newconn->s = news;
1607 /* remember the remote address */
1608 tor_addr_copy(&newconn->addr, &addr);
1609 if (new_type == CONN_TYPE_AP && conn->socket_family == AF_UNIX) {
1610 newconn->port = 0;
1611 newconn->address = tor_strdup(conn->address);
1612 } else {
1613 newconn->port = port;
1614 newconn->address = tor_addr_to_str_dup(&addr);
1617 if (new_type == CONN_TYPE_AP && conn->socket_family != AF_UNIX) {
1618 log_info(LD_NET, "New SOCKS connection opened from %s.",
1619 fmt_and_decorate_addr(&addr));
1621 if (new_type == CONN_TYPE_AP && conn->socket_family == AF_UNIX) {
1622 log_info(LD_NET, "New SOCKS AF_UNIX connection opened");
1624 if (new_type == CONN_TYPE_CONTROL) {
1625 log_notice(LD_CONTROL, "New control connection opened from %s.",
1626 fmt_and_decorate_addr(&addr));
1629 } else if (conn->socket_family == AF_UNIX && conn->type != CONN_TYPE_AP) {
1630 tor_assert(conn->type == CONN_TYPE_CONTROL_LISTENER);
1631 tor_assert(new_type == CONN_TYPE_CONTROL);
1632 log_notice(LD_CONTROL, "New control connection opened.");
1634 newconn = connection_new(new_type, conn->socket_family);
1635 newconn->s = news;
1637 /* remember the remote address -- do we have anything sane to put here? */
1638 tor_addr_make_unspec(&newconn->addr);
1639 newconn->port = 1;
1640 newconn->address = tor_strdup(conn->address);
1641 } else {
1642 tor_assert(0);
1645 if (connection_add(newconn) < 0) { /* no space, forget it */
1646 connection_free(newconn);
1647 return 0; /* no need to tear down the parent */
1650 if (connection_init_accepted_conn(newconn, TO_LISTENER_CONN(conn)) < 0) {
1651 if (! newconn->marked_for_close)
1652 connection_mark_for_close(newconn);
1653 return 0;
1655 return 0;
1658 /** Initialize states for newly accepted connection <b>conn</b>.
1659 * If conn is an OR, start the TLS handshake.
1660 * If conn is a transparent AP, get its original destination
1661 * and place it in circuit_wait.
1663 static int
1664 connection_init_accepted_conn(connection_t *conn,
1665 const listener_connection_t *listener)
1667 int rv;
1669 connection_start_reading(conn);
1671 switch (conn->type) {
1672 case CONN_TYPE_EXT_OR:
1673 /* Initiate Extended ORPort authentication. */
1674 return connection_ext_or_start_auth(TO_OR_CONN(conn));
1675 case CONN_TYPE_OR:
1676 control_event_or_conn_status(TO_OR_CONN(conn), OR_CONN_EVENT_NEW, 0);
1677 rv = connection_tls_start_handshake(TO_OR_CONN(conn), 1);
1678 if (rv < 0) {
1679 connection_or_close_for_error(TO_OR_CONN(conn), 0);
1681 return rv;
1682 break;
1683 case CONN_TYPE_AP:
1684 memcpy(&TO_ENTRY_CONN(conn)->entry_cfg, &listener->entry_cfg,
1685 sizeof(entry_port_cfg_t));
1686 TO_ENTRY_CONN(conn)->nym_epoch = get_signewnym_epoch();
1687 TO_ENTRY_CONN(conn)->socks_request->listener_type = listener->base_.type;
1689 switch (TO_CONN(listener)->type) {
1690 case CONN_TYPE_AP_LISTENER:
1691 conn->state = AP_CONN_STATE_SOCKS_WAIT;
1692 TO_ENTRY_CONN(conn)->socks_request->socks_prefer_no_auth =
1693 listener->entry_cfg.socks_prefer_no_auth;
1694 break;
1695 case CONN_TYPE_AP_TRANS_LISTENER:
1696 TO_ENTRY_CONN(conn)->is_transparent_ap = 1;
1697 /* XXXX028 -- is this correct still, with the addition of
1698 * pending_entry_connections ? */
1699 conn->state = AP_CONN_STATE_CIRCUIT_WAIT;
1700 return connection_ap_process_transparent(TO_ENTRY_CONN(conn));
1701 case CONN_TYPE_AP_NATD_LISTENER:
1702 TO_ENTRY_CONN(conn)->is_transparent_ap = 1;
1703 conn->state = AP_CONN_STATE_NATD_WAIT;
1704 break;
1706 break;
1707 case CONN_TYPE_DIR:
1708 conn->purpose = DIR_PURPOSE_SERVER;
1709 conn->state = DIR_CONN_STATE_SERVER_COMMAND_WAIT;
1710 break;
1711 case CONN_TYPE_CONTROL:
1712 conn->state = CONTROL_CONN_STATE_NEEDAUTH;
1713 break;
1715 return 0;
1718 /** Take conn, make a nonblocking socket; try to connect to
1719 * sa, binding to bindaddr if sa is not localhost. If fail, return -1 and if
1720 * applicable put your best guess about errno into *<b>socket_error</b>.
1721 * If connected return 1, if EAGAIN return 0.
1723 MOCK_IMPL(STATIC int,
1724 connection_connect_sockaddr,(connection_t *conn,
1725 const struct sockaddr *sa,
1726 socklen_t sa_len,
1727 const struct sockaddr *bindaddr,
1728 socklen_t bindaddr_len,
1729 int *socket_error))
1731 tor_socket_t s;
1732 int inprogress = 0;
1733 const or_options_t *options = get_options();
1735 tor_assert(conn);
1736 tor_assert(sa);
1737 tor_assert(socket_error);
1739 if (get_options()->DisableNetwork) {
1740 /* We should never even try to connect anyplace if DisableNetwork is set.
1741 * Warn if we do, and refuse to make the connection. */
1742 static ratelim_t disablenet_violated = RATELIM_INIT(30*60);
1743 *socket_error = SOCK_ERRNO(ENETUNREACH);
1744 log_fn_ratelim(&disablenet_violated, LOG_WARN, LD_BUG,
1745 "Tried to open a socket with DisableNetwork set.");
1746 tor_fragile_assert();
1747 return -1;
1750 const int protocol_family = sa->sa_family;
1751 const int proto = (sa->sa_family == AF_INET6 ||
1752 sa->sa_family == AF_INET) ? IPPROTO_TCP : 0;
1754 s = tor_open_socket_nonblocking(protocol_family, SOCK_STREAM, proto);
1755 if (! SOCKET_OK(s)) {
1757 * Early OOS handler calls; it matters if it's an exhaustion-related
1758 * error or not.
1760 *socket_error = tor_socket_errno(s);
1761 if (ERRNO_IS_RESOURCE_LIMIT(*socket_error)) {
1762 warn_too_many_conns();
1763 connection_check_oos(get_n_open_sockets(), 1);
1764 } else {
1765 log_warn(LD_NET,"Error creating network socket: %s",
1766 tor_socket_strerror(*socket_error));
1767 connection_check_oos(get_n_open_sockets(), 0);
1769 return -1;
1772 if (make_socket_reuseable(s) < 0) {
1773 log_warn(LD_NET, "Error setting SO_REUSEADDR flag on new connection: %s",
1774 tor_socket_strerror(errno));
1778 * We've got the socket open; give the OOS handler a chance to check
1779 * against configured maximum socket number, but tell it no exhaustion
1780 * failure.
1782 connection_check_oos(get_n_open_sockets(), 0);
1784 if (bindaddr && bind(s, bindaddr, bindaddr_len) < 0) {
1785 *socket_error = tor_socket_errno(s);
1786 log_warn(LD_NET,"Error binding network socket: %s",
1787 tor_socket_strerror(*socket_error));
1788 tor_close_socket(s);
1789 return -1;
1792 tor_assert(options);
1793 if (options->ConstrainedSockets)
1794 set_constrained_socket_buffers(s, (int)options->ConstrainedSockSize);
1796 if (connect(s, sa, sa_len) < 0) {
1797 int e = tor_socket_errno(s);
1798 if (!ERRNO_IS_CONN_EINPROGRESS(e)) {
1799 /* yuck. kill it. */
1800 *socket_error = e;
1801 log_info(LD_NET,
1802 "connect() to socket failed: %s",
1803 tor_socket_strerror(e));
1804 tor_close_socket(s);
1805 return -1;
1806 } else {
1807 inprogress = 1;
1811 /* it succeeded. we're connected. */
1812 log_fn(inprogress ? LOG_DEBUG : LOG_INFO, LD_NET,
1813 "Connection to socket %s (sock "TOR_SOCKET_T_FORMAT").",
1814 inprogress ? "in progress" : "established", s);
1815 conn->s = s;
1816 if (connection_add_connecting(conn) < 0) {
1817 /* no space, forget it */
1818 *socket_error = SOCK_ERRNO(ENOBUFS);
1819 return -1;
1822 return inprogress ? 0 : 1;
1825 /* Log a message if connection attempt is made when IPv4 or IPv6 is disabled.
1826 * Log a less severe message if we couldn't conform to ClientPreferIPv6ORPort
1827 * or ClientPreferIPv6ORPort. */
1828 static void
1829 connection_connect_log_client_use_ip_version(const connection_t *conn)
1831 const or_options_t *options = get_options();
1833 /* Only clients care about ClientUseIPv4/6, bail out early on servers, and
1834 * on connections we don't care about */
1835 if (server_mode(options) || !conn || conn->type == CONN_TYPE_EXIT) {
1836 return;
1839 /* We're only prepared to log OR and DIR connections here */
1840 if (conn->type != CONN_TYPE_OR && conn->type != CONN_TYPE_DIR) {
1841 return;
1844 const int must_ipv4 = !fascist_firewall_use_ipv6(options);
1845 const int must_ipv6 = (options->ClientUseIPv4 == 0);
1846 const int pref_ipv6 = (conn->type == CONN_TYPE_OR
1847 ? fascist_firewall_prefer_ipv6_orport(options)
1848 : fascist_firewall_prefer_ipv6_dirport(options));
1849 tor_addr_t real_addr;
1850 tor_addr_make_null(&real_addr, AF_UNSPEC);
1852 /* OR conns keep the original address in real_addr, as addr gets overwritten
1853 * with the descriptor address */
1854 if (conn->type == CONN_TYPE_OR) {
1855 const or_connection_t *or_conn = TO_OR_CONN((connection_t *)conn);
1856 tor_addr_copy(&real_addr, &or_conn->real_addr);
1857 } else if (conn->type == CONN_TYPE_DIR) {
1858 tor_addr_copy(&real_addr, &conn->addr);
1861 /* Check if we broke a mandatory address family restriction */
1862 if ((must_ipv4 && tor_addr_family(&real_addr) == AF_INET6)
1863 || (must_ipv6 && tor_addr_family(&real_addr) == AF_INET)) {
1864 static int logged_backtrace = 0;
1865 log_info(LD_BUG, "Outgoing %s connection to %s violated ClientUseIPv%s 0.",
1866 conn->type == CONN_TYPE_OR ? "OR" : "Dir",
1867 fmt_addr(&real_addr),
1868 options->ClientUseIPv4 == 0 ? "4" : "6");
1869 if (!logged_backtrace) {
1870 log_backtrace(LOG_INFO, LD_BUG, "Address came from");
1871 logged_backtrace = 1;
1875 /* Bridges are allowed to break IPv4/IPv6 ORPort preferences to connect to
1876 * the node's configured address when ClientPreferIPv6ORPort is auto */
1877 if (options->UseBridges && conn->type == CONN_TYPE_OR
1878 && options->ClientPreferIPv6ORPort == -1) {
1879 return;
1882 /* Check if we couldn't satisfy an address family preference */
1883 if ((!pref_ipv6 && tor_addr_family(&real_addr) == AF_INET6)
1884 || (pref_ipv6 && tor_addr_family(&real_addr) == AF_INET)) {
1885 log_info(LD_NET, "Outgoing connection to %s doesn't satisfy "
1886 "ClientPreferIPv6%sPort %d, with ClientUseIPv4 %d, and "
1887 "fascist_firewall_use_ipv6 %d (ClientUseIPv6 %d and UseBridges "
1888 "%d).",
1889 fmt_addr(&real_addr),
1890 conn->type == CONN_TYPE_OR ? "OR" : "Dir",
1891 conn->type == CONN_TYPE_OR ? options->ClientPreferIPv6ORPort
1892 : options->ClientPreferIPv6DirPort,
1893 options->ClientUseIPv4, fascist_firewall_use_ipv6(options),
1894 options->ClientUseIPv6, options->UseBridges);
1898 /** Retrieve the outbound address depending on the protocol (IPv4 or IPv6)
1899 * and the connection type (relay, exit, ...)
1900 * Return a socket address or NULL in case nothing is configured.
1902 const tor_addr_t *
1903 conn_get_outbound_address(sa_family_t family,
1904 const or_options_t *options, unsigned int conn_type)
1906 const tor_addr_t *ext_addr = NULL;
1908 int fam_index;
1909 switch (family) {
1910 case AF_INET:
1911 fam_index = 0;
1912 break;
1913 case AF_INET6:
1914 fam_index = 1;
1915 break;
1916 default:
1917 return NULL;
1920 // If an exit connection, use the exit address (if present)
1921 if (conn_type == CONN_TYPE_EXIT) {
1922 if (!tor_addr_is_null(
1923 &options->OutboundBindAddresses[OUTBOUND_ADDR_EXIT][fam_index])) {
1924 ext_addr = &options->OutboundBindAddresses[OUTBOUND_ADDR_EXIT]
1925 [fam_index];
1926 } else if (!tor_addr_is_null(
1927 &options->OutboundBindAddresses[OUTBOUND_ADDR_EXIT_AND_OR]
1928 [fam_index])) {
1929 ext_addr = &options->OutboundBindAddresses[OUTBOUND_ADDR_EXIT_AND_OR]
1930 [fam_index];
1932 } else { // All non-exit connections
1933 if (!tor_addr_is_null(
1934 &options->OutboundBindAddresses[OUTBOUND_ADDR_OR][fam_index])) {
1935 ext_addr = &options->OutboundBindAddresses[OUTBOUND_ADDR_OR]
1936 [fam_index];
1937 } else if (!tor_addr_is_null(
1938 &options->OutboundBindAddresses[OUTBOUND_ADDR_EXIT_AND_OR]
1939 [fam_index])) {
1940 ext_addr = &options->OutboundBindAddresses[OUTBOUND_ADDR_EXIT_AND_OR]
1941 [fam_index];
1944 return ext_addr;
1947 /** Take conn, make a nonblocking socket; try to connect to
1948 * addr:port (port arrives in *host order*). If fail, return -1 and if
1949 * applicable put your best guess about errno into *<b>socket_error</b>.
1950 * Else assign s to conn-\>s: if connected return 1, if EAGAIN return 0.
1952 * addr:port can be different to conn->addr:conn->port if connecting through
1953 * a proxy.
1955 * address is used to make the logs useful.
1957 * On success, add conn to the list of polled connections.
1960 connection_connect(connection_t *conn, const char *address,
1961 const tor_addr_t *addr, uint16_t port, int *socket_error)
1963 struct sockaddr_storage addrbuf;
1964 struct sockaddr_storage bind_addr_ss;
1965 struct sockaddr *bind_addr = NULL;
1966 struct sockaddr *dest_addr;
1967 int dest_addr_len, bind_addr_len = 0;
1969 /* Log if we didn't stick to ClientUseIPv4/6 or ClientPreferIPv6OR/DirPort
1971 connection_connect_log_client_use_ip_version(conn);
1973 if (!tor_addr_is_loopback(addr)) {
1974 const tor_addr_t *ext_addr = NULL;
1975 ext_addr = conn_get_outbound_address(tor_addr_family(addr), get_options(),
1976 conn->type);
1977 if (ext_addr) {
1978 memset(&bind_addr_ss, 0, sizeof(bind_addr_ss));
1979 bind_addr_len = tor_addr_to_sockaddr(ext_addr, 0,
1980 (struct sockaddr *) &bind_addr_ss,
1981 sizeof(bind_addr_ss));
1982 if (bind_addr_len == 0) {
1983 log_warn(LD_NET,
1984 "Error converting OutboundBindAddress %s into sockaddr. "
1985 "Ignoring.", fmt_and_decorate_addr(ext_addr));
1986 } else {
1987 bind_addr = (struct sockaddr *)&bind_addr_ss;
1992 memset(&addrbuf,0,sizeof(addrbuf));
1993 dest_addr = (struct sockaddr*) &addrbuf;
1994 dest_addr_len = tor_addr_to_sockaddr(addr, port, dest_addr, sizeof(addrbuf));
1995 tor_assert(dest_addr_len > 0);
1997 log_debug(LD_NET, "Connecting to %s:%u.",
1998 escaped_safe_str_client(address), port);
2000 return connection_connect_sockaddr(conn, dest_addr, dest_addr_len,
2001 bind_addr, bind_addr_len, socket_error);
2004 #ifdef HAVE_SYS_UN_H
2006 /** Take conn, make a nonblocking socket; try to connect to
2007 * an AF_UNIX socket at socket_path. If fail, return -1 and if applicable
2008 * put your best guess about errno into *<b>socket_error</b>. Else assign s
2009 * to conn-\>s: if connected return 1, if EAGAIN return 0.
2011 * On success, add conn to the list of polled connections.
2014 connection_connect_unix(connection_t *conn, const char *socket_path,
2015 int *socket_error)
2017 struct sockaddr_un dest_addr;
2019 tor_assert(socket_path);
2021 /* Check that we'll be able to fit it into dest_addr later */
2022 if (strlen(socket_path) + 1 > sizeof(dest_addr.sun_path)) {
2023 log_warn(LD_NET,
2024 "Path %s is too long for an AF_UNIX socket\n",
2025 escaped_safe_str_client(socket_path));
2026 *socket_error = SOCK_ERRNO(ENAMETOOLONG);
2027 return -1;
2030 memset(&dest_addr, 0, sizeof(dest_addr));
2031 dest_addr.sun_family = AF_UNIX;
2032 strlcpy(dest_addr.sun_path, socket_path, sizeof(dest_addr.sun_path));
2034 log_debug(LD_NET,
2035 "Connecting to AF_UNIX socket at %s.",
2036 escaped_safe_str_client(socket_path));
2038 return connection_connect_sockaddr(conn,
2039 (struct sockaddr *)&dest_addr, sizeof(dest_addr),
2040 NULL, 0, socket_error);
2043 #endif /* defined(HAVE_SYS_UN_H) */
2045 /** Convert state number to string representation for logging purposes.
2047 static const char *
2048 connection_proxy_state_to_string(int state)
2050 static const char *unknown = "???";
2051 static const char *states[] = {
2052 "PROXY_NONE",
2053 "PROXY_INFANT",
2054 "PROXY_HTTPS_WANT_CONNECT_OK",
2055 "PROXY_SOCKS4_WANT_CONNECT_OK",
2056 "PROXY_SOCKS5_WANT_AUTH_METHOD_NONE",
2057 "PROXY_SOCKS5_WANT_AUTH_METHOD_RFC1929",
2058 "PROXY_SOCKS5_WANT_AUTH_RFC1929_OK",
2059 "PROXY_SOCKS5_WANT_CONNECT_OK",
2060 "PROXY_CONNECTED",
2063 if (state < PROXY_NONE || state > PROXY_CONNECTED)
2064 return unknown;
2066 return states[state];
2069 /** Returns the global proxy type used by tor. Use this function for
2070 * logging or high-level purposes, don't use it to fill the
2071 * <b>proxy_type</b> field of or_connection_t; use the actual proxy
2072 * protocol instead.*/
2073 static int
2074 get_proxy_type(void)
2076 const or_options_t *options = get_options();
2078 if (options->ClientTransportPlugin)
2079 return PROXY_PLUGGABLE;
2080 else if (options->HTTPSProxy)
2081 return PROXY_CONNECT;
2082 else if (options->Socks4Proxy)
2083 return PROXY_SOCKS4;
2084 else if (options->Socks5Proxy)
2085 return PROXY_SOCKS5;
2086 else
2087 return PROXY_NONE;
2090 /* One byte for the version, one for the command, two for the
2091 port, and four for the addr... and, one more for the
2092 username NUL: */
2093 #define SOCKS4_STANDARD_BUFFER_SIZE (1 + 1 + 2 + 4 + 1)
2095 /** Write a proxy request of <b>type</b> (socks4, socks5, https) to conn
2096 * for conn->addr:conn->port, authenticating with the auth details given
2097 * in the configuration (if available). SOCKS 5 and HTTP CONNECT proxies
2098 * support authentication.
2100 * Returns -1 if conn->addr is incompatible with the proxy protocol, and
2101 * 0 otherwise.
2103 * Use connection_read_proxy_handshake() to complete the handshake.
2106 connection_proxy_connect(connection_t *conn, int type)
2108 const or_options_t *options;
2110 tor_assert(conn);
2112 options = get_options();
2114 switch (type) {
2115 case PROXY_CONNECT: {
2116 char buf[1024];
2117 char *base64_authenticator=NULL;
2118 const char *authenticator = options->HTTPSProxyAuthenticator;
2120 /* Send HTTP CONNECT and authentication (if available) in
2121 * one request */
2123 if (authenticator) {
2124 base64_authenticator = alloc_http_authenticator(authenticator);
2125 if (!base64_authenticator)
2126 log_warn(LD_OR, "Encoding https authenticator failed");
2129 if (base64_authenticator) {
2130 const char *addrport = fmt_addrport(&conn->addr, conn->port);
2131 tor_snprintf(buf, sizeof(buf), "CONNECT %s HTTP/1.1\r\n"
2132 "Host: %s\r\n"
2133 "Proxy-Authorization: Basic %s\r\n\r\n",
2134 addrport,
2135 addrport,
2136 base64_authenticator);
2137 tor_free(base64_authenticator);
2138 } else {
2139 tor_snprintf(buf, sizeof(buf), "CONNECT %s HTTP/1.0\r\n\r\n",
2140 fmt_addrport(&conn->addr, conn->port));
2143 connection_write_to_buf(buf, strlen(buf), conn);
2144 conn->proxy_state = PROXY_HTTPS_WANT_CONNECT_OK;
2145 break;
2148 case PROXY_SOCKS4: {
2149 unsigned char *buf;
2150 uint16_t portn;
2151 uint32_t ip4addr;
2152 size_t buf_size = 0;
2153 char *socks_args_string = NULL;
2155 /* Send a SOCKS4 connect request */
2157 if (tor_addr_family(&conn->addr) != AF_INET) {
2158 log_warn(LD_NET, "SOCKS4 client is incompatible with IPv6");
2159 return -1;
2162 { /* If we are here because we are trying to connect to a
2163 pluggable transport proxy, check if we have any SOCKS
2164 arguments to transmit. If we do, compress all arguments to
2165 a single string in 'socks_args_string': */
2167 if (get_proxy_type() == PROXY_PLUGGABLE) {
2168 socks_args_string =
2169 pt_get_socks_args_for_proxy_addrport(&conn->addr, conn->port);
2170 if (socks_args_string)
2171 log_debug(LD_NET, "Sending out '%s' as our SOCKS argument string.",
2172 socks_args_string);
2176 { /* Figure out the buffer size we need for the SOCKS message: */
2178 buf_size = SOCKS4_STANDARD_BUFFER_SIZE;
2180 /* If we have a SOCKS argument string, consider its size when
2181 calculating the buffer size: */
2182 if (socks_args_string)
2183 buf_size += strlen(socks_args_string);
2186 buf = tor_malloc_zero(buf_size);
2188 ip4addr = tor_addr_to_ipv4n(&conn->addr);
2189 portn = htons(conn->port);
2191 buf[0] = 4; /* version */
2192 buf[1] = SOCKS_COMMAND_CONNECT; /* command */
2193 memcpy(buf + 2, &portn, 2); /* port */
2194 memcpy(buf + 4, &ip4addr, 4); /* addr */
2196 /* Next packet field is the userid. If we have pluggable
2197 transport SOCKS arguments, we have to embed them
2198 there. Otherwise, we use an empty userid. */
2199 if (socks_args_string) { /* place the SOCKS args string: */
2200 tor_assert(strlen(socks_args_string) > 0);
2201 tor_assert(buf_size >=
2202 SOCKS4_STANDARD_BUFFER_SIZE + strlen(socks_args_string));
2203 strlcpy((char *)buf + 8, socks_args_string, buf_size - 8);
2204 tor_free(socks_args_string);
2205 } else {
2206 buf[8] = 0; /* no userid */
2209 connection_write_to_buf((char *)buf, buf_size, conn);
2210 tor_free(buf);
2212 conn->proxy_state = PROXY_SOCKS4_WANT_CONNECT_OK;
2213 break;
2216 case PROXY_SOCKS5: {
2217 unsigned char buf[4]; /* fields: vers, num methods, method list */
2219 /* Send a SOCKS5 greeting (connect request must wait) */
2221 buf[0] = 5; /* version */
2223 /* We have to use SOCKS5 authentication, if we have a
2224 Socks5ProxyUsername or if we want to pass arguments to our
2225 pluggable transport proxy: */
2226 if ((options->Socks5ProxyUsername) ||
2227 (get_proxy_type() == PROXY_PLUGGABLE &&
2228 (get_socks_args_by_bridge_addrport(&conn->addr, conn->port)))) {
2229 /* number of auth methods */
2230 buf[1] = 2;
2231 buf[2] = 0x00; /* no authentication */
2232 buf[3] = 0x02; /* rfc1929 Username/Passwd auth */
2233 conn->proxy_state = PROXY_SOCKS5_WANT_AUTH_METHOD_RFC1929;
2234 } else {
2235 buf[1] = 1;
2236 buf[2] = 0x00; /* no authentication */
2237 conn->proxy_state = PROXY_SOCKS5_WANT_AUTH_METHOD_NONE;
2240 connection_write_to_buf((char *)buf, 2 + buf[1], conn);
2241 break;
2244 default:
2245 log_err(LD_BUG, "Invalid proxy protocol, %d", type);
2246 tor_fragile_assert();
2247 return -1;
2250 log_debug(LD_NET, "set state %s",
2251 connection_proxy_state_to_string(conn->proxy_state));
2253 return 0;
2256 /** Read conn's inbuf. If the http response from the proxy is all
2257 * here, make sure it's good news, then return 1. If it's bad news,
2258 * return -1. Else return 0 and hope for better luck next time.
2260 static int
2261 connection_read_https_proxy_response(connection_t *conn)
2263 char *headers;
2264 char *reason=NULL;
2265 int status_code;
2266 time_t date_header;
2268 switch (fetch_from_buf_http(conn->inbuf,
2269 &headers, MAX_HEADERS_SIZE,
2270 NULL, NULL, 10000, 0)) {
2271 case -1: /* overflow */
2272 log_warn(LD_PROTOCOL,
2273 "Your https proxy sent back an oversized response. Closing.");
2274 return -1;
2275 case 0:
2276 log_info(LD_NET,"https proxy response not all here yet. Waiting.");
2277 return 0;
2278 /* case 1, fall through */
2281 if (parse_http_response(headers, &status_code, &date_header,
2282 NULL, &reason) < 0) {
2283 log_warn(LD_NET,
2284 "Unparseable headers from proxy (connecting to '%s'). Closing.",
2285 conn->address);
2286 tor_free(headers);
2287 return -1;
2289 tor_free(headers);
2290 if (!reason) reason = tor_strdup("[no reason given]");
2292 if (status_code == 200) {
2293 log_info(LD_NET,
2294 "HTTPS connect to '%s' successful! (200 %s) Starting TLS.",
2295 conn->address, escaped(reason));
2296 tor_free(reason);
2297 return 1;
2299 /* else, bad news on the status code */
2300 switch (status_code) {
2301 case 403:
2302 log_warn(LD_NET,
2303 "The https proxy refused to allow connection to %s "
2304 "(status code %d, %s). Closing.",
2305 conn->address, status_code, escaped(reason));
2306 break;
2307 default:
2308 log_warn(LD_NET,
2309 "The https proxy sent back an unexpected status code %d (%s). "
2310 "Closing.",
2311 status_code, escaped(reason));
2312 break;
2314 tor_free(reason);
2315 return -1;
2318 /** Send SOCKS5 CONNECT command to <b>conn</b>, copying <b>conn->addr</b>
2319 * and <b>conn->port</b> into the request.
2321 static void
2322 connection_send_socks5_connect(connection_t *conn)
2324 unsigned char buf[1024];
2325 size_t reqsize = 6;
2326 uint16_t port = htons(conn->port);
2328 buf[0] = 5; /* version */
2329 buf[1] = SOCKS_COMMAND_CONNECT; /* command */
2330 buf[2] = 0; /* reserved */
2332 if (tor_addr_family(&conn->addr) == AF_INET) {
2333 uint32_t addr = tor_addr_to_ipv4n(&conn->addr);
2335 buf[3] = 1;
2336 reqsize += 4;
2337 memcpy(buf + 4, &addr, 4);
2338 memcpy(buf + 8, &port, 2);
2339 } else { /* AF_INET6 */
2340 buf[3] = 4;
2341 reqsize += 16;
2342 memcpy(buf + 4, tor_addr_to_in6_addr8(&conn->addr), 16);
2343 memcpy(buf + 20, &port, 2);
2346 connection_write_to_buf((char *)buf, reqsize, conn);
2348 conn->proxy_state = PROXY_SOCKS5_WANT_CONNECT_OK;
2351 /** Wrapper around fetch_from_buf_socks_client: see that functions
2352 * for documentation of its behavior. */
2353 static int
2354 connection_fetch_from_buf_socks_client(connection_t *conn,
2355 int state, char **reason)
2357 return fetch_from_buf_socks_client(conn->inbuf, state, reason);
2360 /** Call this from connection_*_process_inbuf() to advance the proxy
2361 * handshake.
2363 * No matter what proxy protocol is used, if this function returns 1, the
2364 * handshake is complete, and the data remaining on inbuf may contain the
2365 * start of the communication with the requested server.
2367 * Returns 0 if the current buffer contains an incomplete response, and -1
2368 * on error.
2371 connection_read_proxy_handshake(connection_t *conn)
2373 int ret = 0;
2374 char *reason = NULL;
2376 log_debug(LD_NET, "enter state %s",
2377 connection_proxy_state_to_string(conn->proxy_state));
2379 switch (conn->proxy_state) {
2380 case PROXY_HTTPS_WANT_CONNECT_OK:
2381 ret = connection_read_https_proxy_response(conn);
2382 if (ret == 1)
2383 conn->proxy_state = PROXY_CONNECTED;
2384 break;
2386 case PROXY_SOCKS4_WANT_CONNECT_OK:
2387 ret = connection_fetch_from_buf_socks_client(conn,
2388 conn->proxy_state,
2389 &reason);
2390 if (ret == 1)
2391 conn->proxy_state = PROXY_CONNECTED;
2392 break;
2394 case PROXY_SOCKS5_WANT_AUTH_METHOD_NONE:
2395 ret = connection_fetch_from_buf_socks_client(conn,
2396 conn->proxy_state,
2397 &reason);
2398 /* no auth needed, do connect */
2399 if (ret == 1) {
2400 connection_send_socks5_connect(conn);
2401 ret = 0;
2403 break;
2405 case PROXY_SOCKS5_WANT_AUTH_METHOD_RFC1929:
2406 ret = connection_fetch_from_buf_socks_client(conn,
2407 conn->proxy_state,
2408 &reason);
2410 /* send auth if needed, otherwise do connect */
2411 if (ret == 1) {
2412 connection_send_socks5_connect(conn);
2413 ret = 0;
2414 } else if (ret == 2) {
2415 unsigned char buf[1024];
2416 size_t reqsize, usize, psize;
2417 const char *user, *pass;
2418 char *socks_args_string = NULL;
2420 if (get_proxy_type() == PROXY_PLUGGABLE) {
2421 socks_args_string =
2422 pt_get_socks_args_for_proxy_addrport(&conn->addr, conn->port);
2423 if (!socks_args_string) {
2424 log_warn(LD_NET, "Could not create SOCKS args string.");
2425 ret = -1;
2426 break;
2429 log_debug(LD_NET, "SOCKS5 arguments: %s", socks_args_string);
2430 tor_assert(strlen(socks_args_string) > 0);
2431 tor_assert(strlen(socks_args_string) <= MAX_SOCKS5_AUTH_SIZE_TOTAL);
2433 if (strlen(socks_args_string) > MAX_SOCKS5_AUTH_FIELD_SIZE) {
2434 user = socks_args_string;
2435 usize = MAX_SOCKS5_AUTH_FIELD_SIZE;
2436 pass = socks_args_string + MAX_SOCKS5_AUTH_FIELD_SIZE;
2437 psize = strlen(socks_args_string) - MAX_SOCKS5_AUTH_FIELD_SIZE;
2438 } else {
2439 user = socks_args_string;
2440 usize = strlen(socks_args_string);
2441 pass = "\0";
2442 psize = 1;
2444 } else if (get_options()->Socks5ProxyUsername) {
2445 user = get_options()->Socks5ProxyUsername;
2446 pass = get_options()->Socks5ProxyPassword;
2447 tor_assert(user && pass);
2448 usize = strlen(user);
2449 psize = strlen(pass);
2450 } else {
2451 log_err(LD_BUG, "We entered %s for no reason!", __func__);
2452 tor_fragile_assert();
2453 ret = -1;
2454 break;
2457 /* Username and password lengths should have been checked
2458 above and during torrc parsing. */
2459 tor_assert(usize <= MAX_SOCKS5_AUTH_FIELD_SIZE &&
2460 psize <= MAX_SOCKS5_AUTH_FIELD_SIZE);
2461 reqsize = 3 + usize + psize;
2463 buf[0] = 1; /* negotiation version */
2464 buf[1] = usize;
2465 memcpy(buf + 2, user, usize);
2466 buf[2 + usize] = psize;
2467 memcpy(buf + 3 + usize, pass, psize);
2469 if (socks_args_string)
2470 tor_free(socks_args_string);
2472 connection_write_to_buf((char *)buf, reqsize, conn);
2474 conn->proxy_state = PROXY_SOCKS5_WANT_AUTH_RFC1929_OK;
2475 ret = 0;
2477 break;
2479 case PROXY_SOCKS5_WANT_AUTH_RFC1929_OK:
2480 ret = connection_fetch_from_buf_socks_client(conn,
2481 conn->proxy_state,
2482 &reason);
2483 /* send the connect request */
2484 if (ret == 1) {
2485 connection_send_socks5_connect(conn);
2486 ret = 0;
2488 break;
2490 case PROXY_SOCKS5_WANT_CONNECT_OK:
2491 ret = connection_fetch_from_buf_socks_client(conn,
2492 conn->proxy_state,
2493 &reason);
2494 if (ret == 1)
2495 conn->proxy_state = PROXY_CONNECTED;
2496 break;
2498 default:
2499 log_err(LD_BUG, "Invalid proxy_state for reading, %d",
2500 conn->proxy_state);
2501 tor_fragile_assert();
2502 ret = -1;
2503 break;
2506 log_debug(LD_NET, "leaving state %s",
2507 connection_proxy_state_to_string(conn->proxy_state));
2509 if (ret < 0) {
2510 if (reason) {
2511 log_warn(LD_NET, "Proxy Client: unable to connect to %s:%d (%s)",
2512 conn->address, conn->port, escaped(reason));
2513 tor_free(reason);
2514 } else {
2515 log_warn(LD_NET, "Proxy Client: unable to connect to %s:%d",
2516 conn->address, conn->port);
2518 } else if (ret == 1) {
2519 log_info(LD_NET, "Proxy Client: connection to %s:%d successful",
2520 conn->address, conn->port);
2523 return ret;
2526 /** Given a list of listener connections in <b>old_conns</b>, and list of
2527 * port_cfg_t entries in <b>ports</b>, open a new listener for every port in
2528 * <b>ports</b> that does not already have a listener in <b>old_conns</b>.
2530 * Remove from <b>old_conns</b> every connection that has a corresponding
2531 * entry in <b>ports</b>. Add to <b>new_conns</b> new every connection we
2532 * launch.
2534 * If <b>control_listeners_only</b> is true, then we only open control
2535 * listeners, and we do not remove any noncontrol listeners from old_conns.
2537 * Return 0 on success, -1 on failure.
2539 static int
2540 retry_listener_ports(smartlist_t *old_conns,
2541 const smartlist_t *ports,
2542 smartlist_t *new_conns,
2543 int control_listeners_only)
2545 smartlist_t *launch = smartlist_new();
2546 int r = 0;
2548 if (control_listeners_only) {
2549 SMARTLIST_FOREACH(ports, port_cfg_t *, p, {
2550 if (p->type == CONN_TYPE_CONTROL_LISTENER)
2551 smartlist_add(launch, p);
2553 } else {
2554 smartlist_add_all(launch, ports);
2557 /* Iterate through old_conns, comparing it to launch: remove from both lists
2558 * each pair of elements that corresponds to the same port. */
2559 SMARTLIST_FOREACH_BEGIN(old_conns, connection_t *, conn) {
2560 const port_cfg_t *found_port = NULL;
2562 /* Okay, so this is a listener. Is it configured? */
2563 SMARTLIST_FOREACH_BEGIN(launch, const port_cfg_t *, wanted) {
2564 if (conn->type != wanted->type)
2565 continue;
2566 if ((conn->socket_family != AF_UNIX && wanted->is_unix_addr) ||
2567 (conn->socket_family == AF_UNIX && ! wanted->is_unix_addr))
2568 continue;
2570 if (wanted->server_cfg.no_listen)
2571 continue; /* We don't want to open a listener for this one */
2573 if (wanted->is_unix_addr) {
2574 if (conn->socket_family == AF_UNIX &&
2575 !strcmp(wanted->unix_addr, conn->address)) {
2576 found_port = wanted;
2577 break;
2579 } else {
2580 int port_matches;
2581 if (wanted->port == CFG_AUTO_PORT) {
2582 port_matches = 1;
2583 } else {
2584 port_matches = (wanted->port == conn->port);
2586 if (port_matches && tor_addr_eq(&wanted->addr, &conn->addr)) {
2587 found_port = wanted;
2588 break;
2591 } SMARTLIST_FOREACH_END(wanted);
2593 if (found_port) {
2594 /* This listener is already running; we don't need to launch it. */
2595 //log_debug(LD_NET, "Already have %s on %s:%d",
2596 // conn_type_to_string(found_port->type), conn->address, conn->port);
2597 smartlist_remove(launch, found_port);
2598 /* And we can remove the connection from old_conns too. */
2599 SMARTLIST_DEL_CURRENT(old_conns, conn);
2601 } SMARTLIST_FOREACH_END(conn);
2603 /* Now open all the listeners that are configured but not opened. */
2604 SMARTLIST_FOREACH_BEGIN(launch, const port_cfg_t *, port) {
2605 struct sockaddr *listensockaddr;
2606 socklen_t listensocklen = 0;
2607 char *address=NULL;
2608 connection_t *conn;
2609 int real_port = port->port == CFG_AUTO_PORT ? 0 : port->port;
2610 tor_assert(real_port <= UINT16_MAX);
2611 if (port->server_cfg.no_listen)
2612 continue;
2614 #ifndef _WIN32
2615 /* We don't need to be root to create a UNIX socket, so defer until after
2616 * setuid. */
2617 const or_options_t *options = get_options();
2618 if (port->is_unix_addr && !geteuid() && (options->User) &&
2619 strcmp(options->User, "root"))
2620 continue;
2621 #endif
2623 if (port->is_unix_addr) {
2624 listensockaddr = (struct sockaddr *)
2625 create_unix_sockaddr(port->unix_addr,
2626 &address, &listensocklen);
2627 } else {
2628 listensockaddr = tor_malloc(sizeof(struct sockaddr_storage));
2629 listensocklen = tor_addr_to_sockaddr(&port->addr,
2630 real_port,
2631 listensockaddr,
2632 sizeof(struct sockaddr_storage));
2633 address = tor_addr_to_str_dup(&port->addr);
2636 if (listensockaddr) {
2637 conn = connection_listener_new(listensockaddr, listensocklen,
2638 port->type, address, port);
2639 tor_free(listensockaddr);
2640 tor_free(address);
2641 } else {
2642 conn = NULL;
2645 if (!conn) {
2646 r = -1;
2647 } else {
2648 if (new_conns)
2649 smartlist_add(new_conns, conn);
2651 } SMARTLIST_FOREACH_END(port);
2653 smartlist_free(launch);
2655 return r;
2658 /** Launch listeners for each port you should have open. Only launch
2659 * listeners who are not already open, and only close listeners we no longer
2660 * want.
2662 * Add all old conns that should be closed to <b>replaced_conns</b>.
2663 * Add all new connections to <b>new_conns</b>.
2665 * If <b>close_all_noncontrol</b> is true, then we only open control
2666 * listeners, and we close all other listeners.
2669 retry_all_listeners(smartlist_t *replaced_conns,
2670 smartlist_t *new_conns, int close_all_noncontrol)
2672 smartlist_t *listeners = smartlist_new();
2673 const or_options_t *options = get_options();
2674 int retval = 0;
2675 const uint16_t old_or_port = router_get_advertised_or_port(options);
2676 const uint16_t old_or_port_ipv6 =
2677 router_get_advertised_or_port_by_af(options,AF_INET6);
2678 const uint16_t old_dir_port = router_get_advertised_dir_port(options, 0);
2680 SMARTLIST_FOREACH_BEGIN(get_connection_array(), connection_t *, conn) {
2681 if (connection_is_listener(conn) && !conn->marked_for_close)
2682 smartlist_add(listeners, conn);
2683 } SMARTLIST_FOREACH_END(conn);
2685 if (retry_listener_ports(listeners,
2686 get_configured_ports(),
2687 new_conns,
2688 close_all_noncontrol) < 0)
2689 retval = -1;
2691 /* Any members that were still in 'listeners' don't correspond to
2692 * any configured port. Kill 'em. */
2693 SMARTLIST_FOREACH_BEGIN(listeners, connection_t *, conn) {
2694 log_notice(LD_NET, "Closing no-longer-configured %s on %s:%d",
2695 conn_type_to_string(conn->type), conn->address, conn->port);
2696 if (replaced_conns) {
2697 smartlist_add(replaced_conns, conn);
2698 } else {
2699 connection_close_immediate(conn);
2700 connection_mark_for_close(conn);
2702 } SMARTLIST_FOREACH_END(conn);
2704 smartlist_free(listeners);
2706 if (old_or_port != router_get_advertised_or_port(options) ||
2707 old_or_port_ipv6 != router_get_advertised_or_port_by_af(options,
2708 AF_INET6) ||
2709 old_dir_port != router_get_advertised_dir_port(options, 0)) {
2710 /* Our chosen ORPort or DirPort is not what it used to be: the
2711 * descriptor we had (if any) should be regenerated. (We won't
2712 * automatically notice this because of changes in the option,
2713 * since the value could be "auto".) */
2714 mark_my_descriptor_dirty("Chosen Or/DirPort changed");
2717 return retval;
2720 /** Mark every listener of type other than CONTROL_LISTENER to be closed. */
2721 void
2722 connection_mark_all_noncontrol_listeners(void)
2724 SMARTLIST_FOREACH_BEGIN(get_connection_array(), connection_t *, conn) {
2725 if (conn->marked_for_close)
2726 continue;
2727 if (conn->type == CONN_TYPE_CONTROL_LISTENER)
2728 continue;
2729 if (connection_is_listener(conn))
2730 connection_mark_for_close(conn);
2731 } SMARTLIST_FOREACH_END(conn);
2734 /** Mark every external connection not used for controllers for close. */
2735 void
2736 connection_mark_all_noncontrol_connections(void)
2738 SMARTLIST_FOREACH_BEGIN(get_connection_array(), connection_t *, conn) {
2739 if (conn->marked_for_close)
2740 continue;
2741 switch (conn->type) {
2742 case CONN_TYPE_CONTROL_LISTENER:
2743 case CONN_TYPE_CONTROL:
2744 break;
2745 case CONN_TYPE_AP:
2746 connection_mark_unattached_ap(TO_ENTRY_CONN(conn),
2747 END_STREAM_REASON_HIBERNATING);
2748 break;
2749 case CONN_TYPE_OR:
2751 or_connection_t *orconn = TO_OR_CONN(conn);
2752 if (orconn->chan) {
2753 connection_or_close_normally(orconn, 0);
2754 } else {
2756 * There should have been one, but mark for close and hope
2757 * for the best..
2759 connection_mark_for_close(conn);
2762 break;
2763 default:
2764 connection_mark_for_close(conn);
2765 break;
2767 } SMARTLIST_FOREACH_END(conn);
2770 /** Return 1 if we should apply rate limiting to <b>conn</b>, and 0
2771 * otherwise.
2772 * Right now this just checks if it's an internal IP address or an
2773 * internal connection. We also should, but don't, check if the connection
2774 * uses pluggable transports, since we should then limit it even if it
2775 * comes from an internal IP address. */
2776 static int
2777 connection_is_rate_limited(connection_t *conn)
2779 const or_options_t *options = get_options();
2780 if (conn->linked)
2781 return 0; /* Internal connection */
2782 else if (! options->CountPrivateBandwidth &&
2783 (tor_addr_family(&conn->addr) == AF_UNSPEC || /* no address */
2784 tor_addr_family(&conn->addr) == AF_UNIX || /* no address */
2785 tor_addr_is_internal(&conn->addr, 0)))
2786 return 0; /* Internal address */
2787 else
2788 return 1;
2791 /** Did either global write bucket run dry last second? If so,
2792 * we are likely to run dry again this second, so be stingy with the
2793 * tokens we just put in. */
2794 static int write_buckets_empty_last_second = 0;
2796 /** How many seconds of no active local circuits will make the
2797 * connection revert to the "relayed" bandwidth class? */
2798 #define CLIENT_IDLE_TIME_FOR_PRIORITY 30
2800 /** Return 1 if <b>conn</b> should use tokens from the "relayed"
2801 * bandwidth rates, else 0. Currently, only OR conns with bandwidth
2802 * class 1, and directory conns that are serving data out, count.
2804 static int
2805 connection_counts_as_relayed_traffic(connection_t *conn, time_t now)
2807 if (conn->type == CONN_TYPE_OR &&
2808 connection_or_client_used(TO_OR_CONN(conn)) +
2809 CLIENT_IDLE_TIME_FOR_PRIORITY < now)
2810 return 1;
2811 if (conn->type == CONN_TYPE_DIR && DIR_CONN_IS_SERVER(conn))
2812 return 1;
2813 return 0;
2816 /** Helper function to decide how many bytes out of <b>global_bucket</b>
2817 * we're willing to use for this transaction. <b>base</b> is the size
2818 * of a cell on the network; <b>priority</b> says whether we should
2819 * write many of them or just a few; and <b>conn_bucket</b> (if
2820 * non-negative) provides an upper limit for our answer. */
2821 static ssize_t
2822 connection_bucket_round_robin(int base, int priority,
2823 ssize_t global_bucket, ssize_t conn_bucket)
2825 ssize_t at_most;
2826 ssize_t num_bytes_high = (priority ? 32 : 16) * base;
2827 ssize_t num_bytes_low = (priority ? 4 : 2) * base;
2829 /* Do a rudimentary round-robin so one circuit can't hog a connection.
2830 * Pick at most 32 cells, at least 4 cells if possible, and if we're in
2831 * the middle pick 1/8 of the available bandwidth. */
2832 at_most = global_bucket / 8;
2833 at_most -= (at_most % base); /* round down */
2834 if (at_most > num_bytes_high) /* 16 KB, or 8 KB for low-priority */
2835 at_most = num_bytes_high;
2836 else if (at_most < num_bytes_low) /* 2 KB, or 1 KB for low-priority */
2837 at_most = num_bytes_low;
2839 if (at_most > global_bucket)
2840 at_most = global_bucket;
2842 if (conn_bucket >= 0 && at_most > conn_bucket)
2843 at_most = conn_bucket;
2845 if (at_most < 0)
2846 return 0;
2847 return at_most;
2850 /** How many bytes at most can we read onto this connection? */
2851 static ssize_t
2852 connection_bucket_read_limit(connection_t *conn, time_t now)
2854 int base = RELAY_PAYLOAD_SIZE;
2855 int priority = conn->type != CONN_TYPE_DIR;
2856 int conn_bucket = -1;
2857 int global_bucket = global_read_bucket;
2859 if (connection_speaks_cells(conn)) {
2860 or_connection_t *or_conn = TO_OR_CONN(conn);
2861 if (conn->state == OR_CONN_STATE_OPEN)
2862 conn_bucket = or_conn->read_bucket;
2863 base = get_cell_network_size(or_conn->wide_circ_ids);
2866 if (!connection_is_rate_limited(conn)) {
2867 /* be willing to read on local conns even if our buckets are empty */
2868 return conn_bucket>=0 ? conn_bucket : 1<<14;
2871 if (connection_counts_as_relayed_traffic(conn, now) &&
2872 global_relayed_read_bucket <= global_read_bucket)
2873 global_bucket = global_relayed_read_bucket;
2875 return connection_bucket_round_robin(base, priority,
2876 global_bucket, conn_bucket);
2879 /** How many bytes at most can we write onto this connection? */
2880 ssize_t
2881 connection_bucket_write_limit(connection_t *conn, time_t now)
2883 int base = RELAY_PAYLOAD_SIZE;
2884 int priority = conn->type != CONN_TYPE_DIR;
2885 int conn_bucket = (int)conn->outbuf_flushlen;
2886 int global_bucket = global_write_bucket;
2888 if (!connection_is_rate_limited(conn)) {
2889 /* be willing to write to local conns even if our buckets are empty */
2890 return conn->outbuf_flushlen;
2893 if (connection_speaks_cells(conn)) {
2894 /* use the per-conn write limit if it's lower, but if it's less
2895 * than zero just use zero */
2896 or_connection_t *or_conn = TO_OR_CONN(conn);
2897 if (conn->state == OR_CONN_STATE_OPEN)
2898 if (or_conn->write_bucket < conn_bucket)
2899 conn_bucket = or_conn->write_bucket >= 0 ?
2900 or_conn->write_bucket : 0;
2901 base = get_cell_network_size(or_conn->wide_circ_ids);
2904 if (connection_counts_as_relayed_traffic(conn, now) &&
2905 global_relayed_write_bucket <= global_write_bucket)
2906 global_bucket = global_relayed_write_bucket;
2908 return connection_bucket_round_robin(base, priority,
2909 global_bucket, conn_bucket);
2912 /** Return 1 if the global write buckets are low enough that we
2913 * shouldn't send <b>attempt</b> bytes of low-priority directory stuff
2914 * out to <b>conn</b>. Else return 0.
2916 * Priority was 1 for v1 requests (directories and running-routers),
2917 * and 2 for v2 requests and later (statuses and descriptors).
2919 * There are a lot of parameters we could use here:
2920 * - global_relayed_write_bucket. Low is bad.
2921 * - global_write_bucket. Low is bad.
2922 * - bandwidthrate. Low is bad.
2923 * - bandwidthburst. Not a big factor?
2924 * - attempt. High is bad.
2925 * - total bytes queued on outbufs. High is bad. But I'm wary of
2926 * using this, since a few slow-flushing queues will pump up the
2927 * number without meaning what we meant to mean. What we really
2928 * mean is "total directory bytes added to outbufs recently", but
2929 * that's harder to quantify and harder to keep track of.
2932 global_write_bucket_low(connection_t *conn, size_t attempt, int priority)
2934 int smaller_bucket = global_write_bucket < global_relayed_write_bucket ?
2935 global_write_bucket : global_relayed_write_bucket;
2936 if (authdir_mode(get_options()) && priority>1)
2937 return 0; /* there's always room to answer v2 if we're an auth dir */
2939 if (!connection_is_rate_limited(conn))
2940 return 0; /* local conns don't get limited */
2942 if (smaller_bucket < (int)attempt)
2943 return 1; /* not enough space no matter the priority */
2945 if (write_buckets_empty_last_second)
2946 return 1; /* we're already hitting our limits, no more please */
2948 if (priority == 1) { /* old-style v1 query */
2949 /* Could we handle *two* of these requests within the next two seconds? */
2950 const or_options_t *options = get_options();
2951 int64_t can_write = (int64_t)smaller_bucket
2952 + 2*(options->RelayBandwidthRate ? options->RelayBandwidthRate :
2953 options->BandwidthRate);
2954 if (can_write < 2*(int64_t)attempt)
2955 return 1;
2956 } else { /* v2 query */
2957 /* no further constraints yet */
2959 return 0;
2962 /** Helper: adjusts our bandwidth history and informs the controller as
2963 * appropriate, given that we have just read <b>num_read</b> bytes and written
2964 * <b>num_written</b> bytes on <b>conn</b>. */
2965 static void
2966 record_num_bytes_transferred_impl(connection_t *conn,
2967 time_t now, size_t num_read, size_t num_written)
2969 /* Count bytes of answering direct and tunneled directory requests */
2970 if (conn->type == CONN_TYPE_DIR && conn->purpose == DIR_PURPOSE_SERVER) {
2971 if (num_read > 0)
2972 rep_hist_note_dir_bytes_read(num_read, now);
2973 if (num_written > 0)
2974 rep_hist_note_dir_bytes_written(num_written, now);
2977 if (!connection_is_rate_limited(conn))
2978 return; /* local IPs are free */
2980 if (conn->type == CONN_TYPE_OR)
2981 rep_hist_note_or_conn_bytes(conn->global_identifier, num_read,
2982 num_written, now);
2984 if (num_read > 0) {
2985 rep_hist_note_bytes_read(num_read, now);
2987 if (num_written > 0) {
2988 rep_hist_note_bytes_written(num_written, now);
2990 if (conn->type == CONN_TYPE_EXIT)
2991 rep_hist_note_exit_bytes(conn->port, num_written, num_read);
2994 /** Helper: convert given <b>tvnow</b> time value to milliseconds since
2995 * midnight. */
2996 static uint32_t
2997 msec_since_midnight(const struct timeval *tvnow)
2999 return (uint32_t)(((tvnow->tv_sec % 86400L) * 1000L) +
3000 ((uint32_t)tvnow->tv_usec / (uint32_t)1000L));
3003 /** Helper: return the time in milliseconds since <b>last_empty_time</b>
3004 * when a bucket ran empty that previously had <b>tokens_before</b> tokens
3005 * now has <b>tokens_after</b> tokens after refilling at timestamp
3006 * <b>tvnow</b>, capped at <b>milliseconds_elapsed</b> milliseconds since
3007 * last refilling that bucket. Return 0 if the bucket has not been empty
3008 * since the last refill or has not been refilled. */
3009 uint32_t
3010 bucket_millis_empty(int tokens_before, uint32_t last_empty_time,
3011 int tokens_after, int milliseconds_elapsed,
3012 const struct timeval *tvnow)
3014 uint32_t result = 0, refilled;
3015 if (tokens_before <= 0 && tokens_after > tokens_before) {
3016 refilled = msec_since_midnight(tvnow);
3017 result = (uint32_t)((refilled + 86400L * 1000L - last_empty_time) %
3018 (86400L * 1000L));
3019 if (result > (uint32_t)milliseconds_elapsed)
3020 result = (uint32_t)milliseconds_elapsed;
3022 return result;
3025 /** Check if a bucket which had <b>tokens_before</b> tokens and which got
3026 * <b>tokens_removed</b> tokens removed at timestamp <b>tvnow</b> has run
3027 * out of tokens, and if so, note the milliseconds since midnight in
3028 * <b>timestamp_var</b> for the next TB_EMPTY event. */
3029 void
3030 connection_buckets_note_empty_ts(uint32_t *timestamp_var,
3031 int tokens_before, size_t tokens_removed,
3032 const struct timeval *tvnow)
3034 if (tokens_before > 0 && (uint32_t)tokens_before <= tokens_removed)
3035 *timestamp_var = msec_since_midnight(tvnow);
3038 /** Last time at which the global or relay buckets were emptied in msec
3039 * since midnight. */
3040 static uint32_t global_relayed_read_emptied = 0,
3041 global_relayed_write_emptied = 0,
3042 global_read_emptied = 0,
3043 global_write_emptied = 0;
3045 /** We just read <b>num_read</b> and wrote <b>num_written</b> bytes
3046 * onto <b>conn</b>. Decrement buckets appropriately. */
3047 static void
3048 connection_buckets_decrement(connection_t *conn, time_t now,
3049 size_t num_read, size_t num_written)
3051 if (num_written >= INT_MAX || num_read >= INT_MAX) {
3052 log_err(LD_BUG, "Value out of range. num_read=%lu, num_written=%lu, "
3053 "connection type=%s, state=%s",
3054 (unsigned long)num_read, (unsigned long)num_written,
3055 conn_type_to_string(conn->type),
3056 conn_state_to_string(conn->type, conn->state));
3057 if (num_written >= INT_MAX) num_written = 1;
3058 if (num_read >= INT_MAX) num_read = 1;
3059 tor_fragile_assert();
3062 record_num_bytes_transferred_impl(conn, now, num_read, num_written);
3064 if (!connection_is_rate_limited(conn))
3065 return; /* local IPs are free */
3067 /* If one or more of our token buckets ran dry just now, note the
3068 * timestamp for TB_EMPTY events. */
3069 if (get_options()->TestingEnableTbEmptyEvent) {
3070 struct timeval tvnow;
3071 tor_gettimeofday_cached(&tvnow);
3072 if (connection_counts_as_relayed_traffic(conn, now)) {
3073 connection_buckets_note_empty_ts(&global_relayed_read_emptied,
3074 global_relayed_read_bucket, num_read, &tvnow);
3075 connection_buckets_note_empty_ts(&global_relayed_write_emptied,
3076 global_relayed_write_bucket, num_written, &tvnow);
3078 connection_buckets_note_empty_ts(&global_read_emptied,
3079 global_read_bucket, num_read, &tvnow);
3080 connection_buckets_note_empty_ts(&global_write_emptied,
3081 global_write_bucket, num_written, &tvnow);
3082 if (connection_speaks_cells(conn) && conn->state == OR_CONN_STATE_OPEN) {
3083 or_connection_t *or_conn = TO_OR_CONN(conn);
3084 connection_buckets_note_empty_ts(&or_conn->read_emptied_time,
3085 or_conn->read_bucket, num_read, &tvnow);
3086 connection_buckets_note_empty_ts(&or_conn->write_emptied_time,
3087 or_conn->write_bucket, num_written, &tvnow);
3091 if (connection_counts_as_relayed_traffic(conn, now)) {
3092 global_relayed_read_bucket -= (int)num_read;
3093 global_relayed_write_bucket -= (int)num_written;
3095 global_read_bucket -= (int)num_read;
3096 global_write_bucket -= (int)num_written;
3097 if (connection_speaks_cells(conn) && conn->state == OR_CONN_STATE_OPEN) {
3098 TO_OR_CONN(conn)->read_bucket -= (int)num_read;
3099 TO_OR_CONN(conn)->write_bucket -= (int)num_written;
3103 /** If we have exhausted our global buckets, or the buckets for conn,
3104 * stop reading. */
3105 static void
3106 connection_consider_empty_read_buckets(connection_t *conn)
3108 const char *reason;
3110 if (!connection_is_rate_limited(conn))
3111 return; /* Always okay. */
3113 if (global_read_bucket <= 0) {
3114 reason = "global read bucket exhausted. Pausing.";
3115 } else if (connection_counts_as_relayed_traffic(conn, approx_time()) &&
3116 global_relayed_read_bucket <= 0) {
3117 reason = "global relayed read bucket exhausted. Pausing.";
3118 } else if (connection_speaks_cells(conn) &&
3119 conn->state == OR_CONN_STATE_OPEN &&
3120 TO_OR_CONN(conn)->read_bucket <= 0) {
3121 reason = "connection read bucket exhausted. Pausing.";
3122 } else
3123 return; /* all good, no need to stop it */
3125 LOG_FN_CONN(conn, (LOG_DEBUG, LD_NET, "%s", reason));
3126 conn->read_blocked_on_bw = 1;
3127 connection_stop_reading(conn);
3130 /** If we have exhausted our global buckets, or the buckets for conn,
3131 * stop writing. */
3132 static void
3133 connection_consider_empty_write_buckets(connection_t *conn)
3135 const char *reason;
3137 if (!connection_is_rate_limited(conn))
3138 return; /* Always okay. */
3140 if (global_write_bucket <= 0) {
3141 reason = "global write bucket exhausted. Pausing.";
3142 } else if (connection_counts_as_relayed_traffic(conn, approx_time()) &&
3143 global_relayed_write_bucket <= 0) {
3144 reason = "global relayed write bucket exhausted. Pausing.";
3145 } else if (connection_speaks_cells(conn) &&
3146 conn->state == OR_CONN_STATE_OPEN &&
3147 TO_OR_CONN(conn)->write_bucket <= 0) {
3148 reason = "connection write bucket exhausted. Pausing.";
3149 } else
3150 return; /* all good, no need to stop it */
3152 LOG_FN_CONN(conn, (LOG_DEBUG, LD_NET, "%s", reason));
3153 conn->write_blocked_on_bw = 1;
3154 connection_stop_writing(conn);
3157 /** Initialize the global read bucket to options-\>BandwidthBurst. */
3158 void
3159 connection_bucket_init(void)
3161 const or_options_t *options = get_options();
3162 /* start it at max traffic */
3163 global_read_bucket = (int)options->BandwidthBurst;
3164 global_write_bucket = (int)options->BandwidthBurst;
3165 if (options->RelayBandwidthRate) {
3166 global_relayed_read_bucket = (int)options->RelayBandwidthBurst;
3167 global_relayed_write_bucket = (int)options->RelayBandwidthBurst;
3168 } else {
3169 global_relayed_read_bucket = (int)options->BandwidthBurst;
3170 global_relayed_write_bucket = (int)options->BandwidthBurst;
3174 /** Refill a single <b>bucket</b> called <b>name</b> with bandwidth rate per
3175 * second <b>rate</b> and bandwidth burst <b>burst</b>, assuming that
3176 * <b>milliseconds_elapsed</b> milliseconds have passed since the last
3177 * call. */
3178 static void
3179 connection_bucket_refill_helper(int *bucket, int rate, int burst,
3180 int milliseconds_elapsed,
3181 const char *name)
3183 int starting_bucket = *bucket;
3184 if (starting_bucket < burst && milliseconds_elapsed > 0) {
3185 int64_t incr = (((int64_t)rate) * milliseconds_elapsed) / 1000;
3186 if ((burst - starting_bucket) < incr) {
3187 *bucket = burst; /* We would overflow the bucket; just set it to
3188 * the maximum. */
3189 } else {
3190 *bucket += (int)incr;
3191 if (*bucket > burst || *bucket < starting_bucket) {
3192 /* If we overflow the burst, or underflow our starting bucket,
3193 * cap the bucket value to burst. */
3194 /* XXXX this might be redundant now, but it doesn't show up
3195 * in profiles. Remove it after analysis. */
3196 *bucket = burst;
3199 log_debug(LD_NET,"%s now %d.", name, *bucket);
3203 /** Time has passed; increment buckets appropriately. */
3204 void
3205 connection_bucket_refill(int milliseconds_elapsed, time_t now)
3207 const or_options_t *options = get_options();
3208 smartlist_t *conns = get_connection_array();
3209 int bandwidthrate, bandwidthburst, relayrate, relayburst;
3211 int prev_global_read = global_read_bucket;
3212 int prev_global_write = global_write_bucket;
3213 int prev_relay_read = global_relayed_read_bucket;
3214 int prev_relay_write = global_relayed_write_bucket;
3215 struct timeval tvnow; /*< Only used if TB_EMPTY events are enabled. */
3217 bandwidthrate = (int)options->BandwidthRate;
3218 bandwidthburst = (int)options->BandwidthBurst;
3220 if (options->RelayBandwidthRate) {
3221 relayrate = (int)options->RelayBandwidthRate;
3222 relayburst = (int)options->RelayBandwidthBurst;
3223 } else {
3224 relayrate = bandwidthrate;
3225 relayburst = bandwidthburst;
3228 tor_assert(milliseconds_elapsed >= 0);
3230 write_buckets_empty_last_second =
3231 global_relayed_write_bucket <= 0 || global_write_bucket <= 0;
3233 /* refill the global buckets */
3234 connection_bucket_refill_helper(&global_read_bucket,
3235 bandwidthrate, bandwidthburst,
3236 milliseconds_elapsed,
3237 "global_read_bucket");
3238 connection_bucket_refill_helper(&global_write_bucket,
3239 bandwidthrate, bandwidthburst,
3240 milliseconds_elapsed,
3241 "global_write_bucket");
3242 connection_bucket_refill_helper(&global_relayed_read_bucket,
3243 relayrate, relayburst,
3244 milliseconds_elapsed,
3245 "global_relayed_read_bucket");
3246 connection_bucket_refill_helper(&global_relayed_write_bucket,
3247 relayrate, relayburst,
3248 milliseconds_elapsed,
3249 "global_relayed_write_bucket");
3251 /* If buckets were empty before and have now been refilled, tell any
3252 * interested controllers. */
3253 if (get_options()->TestingEnableTbEmptyEvent) {
3254 uint32_t global_read_empty_time, global_write_empty_time,
3255 relay_read_empty_time, relay_write_empty_time;
3256 tor_gettimeofday_cached(&tvnow);
3257 global_read_empty_time = bucket_millis_empty(prev_global_read,
3258 global_read_emptied, global_read_bucket,
3259 milliseconds_elapsed, &tvnow);
3260 global_write_empty_time = bucket_millis_empty(prev_global_write,
3261 global_write_emptied, global_write_bucket,
3262 milliseconds_elapsed, &tvnow);
3263 control_event_tb_empty("GLOBAL", global_read_empty_time,
3264 global_write_empty_time, milliseconds_elapsed);
3265 relay_read_empty_time = bucket_millis_empty(prev_relay_read,
3266 global_relayed_read_emptied,
3267 global_relayed_read_bucket,
3268 milliseconds_elapsed, &tvnow);
3269 relay_write_empty_time = bucket_millis_empty(prev_relay_write,
3270 global_relayed_write_emptied,
3271 global_relayed_write_bucket,
3272 milliseconds_elapsed, &tvnow);
3273 control_event_tb_empty("RELAY", relay_read_empty_time,
3274 relay_write_empty_time, milliseconds_elapsed);
3277 /* refill the per-connection buckets */
3278 SMARTLIST_FOREACH_BEGIN(conns, connection_t *, conn) {
3279 if (connection_speaks_cells(conn)) {
3280 or_connection_t *or_conn = TO_OR_CONN(conn);
3281 int orbandwidthrate = or_conn->bandwidthrate;
3282 int orbandwidthburst = or_conn->bandwidthburst;
3284 int prev_conn_read = or_conn->read_bucket;
3285 int prev_conn_write = or_conn->write_bucket;
3287 if (connection_bucket_should_increase(or_conn->read_bucket, or_conn)) {
3288 connection_bucket_refill_helper(&or_conn->read_bucket,
3289 orbandwidthrate,
3290 orbandwidthburst,
3291 milliseconds_elapsed,
3292 "or_conn->read_bucket");
3294 if (connection_bucket_should_increase(or_conn->write_bucket, or_conn)) {
3295 connection_bucket_refill_helper(&or_conn->write_bucket,
3296 orbandwidthrate,
3297 orbandwidthburst,
3298 milliseconds_elapsed,
3299 "or_conn->write_bucket");
3302 /* If buckets were empty before and have now been refilled, tell any
3303 * interested controllers. */
3304 if (get_options()->TestingEnableTbEmptyEvent) {
3305 char *bucket;
3306 uint32_t conn_read_empty_time, conn_write_empty_time;
3307 tor_asprintf(&bucket, "ORCONN ID="U64_FORMAT,
3308 U64_PRINTF_ARG(or_conn->base_.global_identifier));
3309 conn_read_empty_time = bucket_millis_empty(prev_conn_read,
3310 or_conn->read_emptied_time,
3311 or_conn->read_bucket,
3312 milliseconds_elapsed, &tvnow);
3313 conn_write_empty_time = bucket_millis_empty(prev_conn_write,
3314 or_conn->write_emptied_time,
3315 or_conn->write_bucket,
3316 milliseconds_elapsed, &tvnow);
3317 control_event_tb_empty(bucket, conn_read_empty_time,
3318 conn_write_empty_time,
3319 milliseconds_elapsed);
3320 tor_free(bucket);
3324 if (conn->read_blocked_on_bw == 1 /* marked to turn reading back on now */
3325 && global_read_bucket > 0 /* and we're allowed to read */
3326 && (!connection_counts_as_relayed_traffic(conn, now) ||
3327 global_relayed_read_bucket > 0) /* even if we're relayed traffic */
3328 && (!connection_speaks_cells(conn) ||
3329 conn->state != OR_CONN_STATE_OPEN ||
3330 TO_OR_CONN(conn)->read_bucket > 0)) {
3331 /* and either a non-cell conn or a cell conn with non-empty bucket */
3332 LOG_FN_CONN(conn, (LOG_DEBUG,LD_NET,
3333 "waking up conn (fd %d) for read", (int)conn->s));
3334 conn->read_blocked_on_bw = 0;
3335 connection_start_reading(conn);
3338 if (conn->write_blocked_on_bw == 1
3339 && global_write_bucket > 0 /* and we're allowed to write */
3340 && (!connection_counts_as_relayed_traffic(conn, now) ||
3341 global_relayed_write_bucket > 0) /* even if it's relayed traffic */
3342 && (!connection_speaks_cells(conn) ||
3343 conn->state != OR_CONN_STATE_OPEN ||
3344 TO_OR_CONN(conn)->write_bucket > 0)) {
3345 LOG_FN_CONN(conn, (LOG_DEBUG,LD_NET,
3346 "waking up conn (fd %d) for write", (int)conn->s));
3347 conn->write_blocked_on_bw = 0;
3348 connection_start_writing(conn);
3350 } SMARTLIST_FOREACH_END(conn);
3353 /** Is the <b>bucket</b> for connection <b>conn</b> low enough that we
3354 * should add another pile of tokens to it?
3356 static int
3357 connection_bucket_should_increase(int bucket, or_connection_t *conn)
3359 tor_assert(conn);
3361 if (conn->base_.state != OR_CONN_STATE_OPEN)
3362 return 0; /* only open connections play the rate limiting game */
3363 if (bucket >= conn->bandwidthburst)
3364 return 0;
3366 return 1;
3369 /** Read bytes from conn-\>s and process them.
3371 * It calls connection_read_to_buf() to bring in any new bytes,
3372 * and then calls connection_process_inbuf() to process them.
3374 * Mark the connection and return -1 if you want to close it, else
3375 * return 0.
3377 static int
3378 connection_handle_read_impl(connection_t *conn)
3380 ssize_t max_to_read=-1, try_to_read;
3381 size_t before, n_read = 0;
3382 int socket_error = 0;
3384 if (conn->marked_for_close)
3385 return 0; /* do nothing */
3387 conn->timestamp_lastread = approx_time();
3389 switch (conn->type) {
3390 case CONN_TYPE_OR_LISTENER:
3391 return connection_handle_listener_read(conn, CONN_TYPE_OR);
3392 case CONN_TYPE_EXT_OR_LISTENER:
3393 return connection_handle_listener_read(conn, CONN_TYPE_EXT_OR);
3394 case CONN_TYPE_AP_LISTENER:
3395 case CONN_TYPE_AP_TRANS_LISTENER:
3396 case CONN_TYPE_AP_NATD_LISTENER:
3397 return connection_handle_listener_read(conn, CONN_TYPE_AP);
3398 case CONN_TYPE_DIR_LISTENER:
3399 return connection_handle_listener_read(conn, CONN_TYPE_DIR);
3400 case CONN_TYPE_CONTROL_LISTENER:
3401 return connection_handle_listener_read(conn, CONN_TYPE_CONTROL);
3402 case CONN_TYPE_AP_DNS_LISTENER:
3403 /* This should never happen; eventdns.c handles the reads here. */
3404 tor_fragile_assert();
3405 return 0;
3408 loop_again:
3409 try_to_read = max_to_read;
3410 tor_assert(!conn->marked_for_close);
3412 before = buf_datalen(conn->inbuf);
3413 if (connection_read_to_buf(conn, &max_to_read, &socket_error) < 0) {
3414 /* There's a read error; kill the connection.*/
3415 if (conn->type == CONN_TYPE_OR) {
3416 connection_or_notify_error(TO_OR_CONN(conn),
3417 socket_error != 0 ?
3418 errno_to_orconn_end_reason(socket_error) :
3419 END_OR_CONN_REASON_CONNRESET,
3420 socket_error != 0 ?
3421 tor_socket_strerror(socket_error) :
3422 "(unknown, errno was 0)");
3424 if (CONN_IS_EDGE(conn)) {
3425 edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
3426 connection_edge_end_errno(edge_conn);
3427 if (conn->type == CONN_TYPE_AP && TO_ENTRY_CONN(conn)->socks_request) {
3428 /* broken, don't send a socks reply back */
3429 TO_ENTRY_CONN(conn)->socks_request->has_finished = 1;
3432 connection_close_immediate(conn); /* Don't flush; connection is dead. */
3434 * This can bypass normal channel checking since we did
3435 * connection_or_notify_error() above.
3437 connection_mark_for_close_internal(conn);
3438 return -1;
3440 n_read += buf_datalen(conn->inbuf) - before;
3441 if (CONN_IS_EDGE(conn) && try_to_read != max_to_read) {
3442 /* instruct it not to try to package partial cells. */
3443 if (connection_process_inbuf(conn, 0) < 0) {
3444 return -1;
3446 if (!conn->marked_for_close &&
3447 connection_is_reading(conn) &&
3448 !conn->inbuf_reached_eof &&
3449 max_to_read > 0)
3450 goto loop_again; /* try reading again, in case more is here now */
3452 /* one last try, packaging partial cells and all. */
3453 if (!conn->marked_for_close &&
3454 connection_process_inbuf(conn, 1) < 0) {
3455 return -1;
3457 if (conn->linked_conn) {
3458 /* The other side's handle_write() will never actually get called, so
3459 * we need to invoke the appropriate callbacks ourself. */
3460 connection_t *linked = conn->linked_conn;
3462 if (n_read) {
3463 /* Probably a no-op, since linked conns typically don't count for
3464 * bandwidth rate limiting. But do it anyway so we can keep stats
3465 * accurately. Note that since we read the bytes from conn, and
3466 * we're writing the bytes onto the linked connection, we count
3467 * these as <i>written</i> bytes. */
3468 connection_buckets_decrement(linked, approx_time(), 0, n_read);
3470 if (connection_flushed_some(linked) < 0)
3471 connection_mark_for_close(linked);
3472 if (!connection_wants_to_flush(linked))
3473 connection_finished_flushing(linked);
3476 if (!buf_datalen(linked->outbuf) && conn->active_on_link)
3477 connection_stop_reading_from_linked_conn(conn);
3479 /* If we hit the EOF, call connection_reached_eof(). */
3480 if (!conn->marked_for_close &&
3481 conn->inbuf_reached_eof &&
3482 connection_reached_eof(conn) < 0) {
3483 return -1;
3485 return 0;
3488 /* DOCDOC connection_handle_read */
3490 connection_handle_read(connection_t *conn)
3492 int res;
3494 tor_gettimeofday_cache_clear();
3495 res = connection_handle_read_impl(conn);
3496 return res;
3499 /** Pull in new bytes from conn-\>s or conn-\>linked_conn onto conn-\>inbuf,
3500 * either directly or via TLS. Reduce the token buckets by the number of bytes
3501 * read.
3503 * If *max_to_read is -1, then decide it ourselves, else go with the
3504 * value passed to us. When returning, if it's changed, subtract the
3505 * number of bytes we read from *max_to_read.
3507 * Return -1 if we want to break conn, else return 0.
3509 static int
3510 connection_read_to_buf(connection_t *conn, ssize_t *max_to_read,
3511 int *socket_error)
3513 int result;
3514 ssize_t at_most = *max_to_read;
3515 size_t slack_in_buf, more_to_read;
3516 size_t n_read = 0, n_written = 0;
3518 if (at_most == -1) { /* we need to initialize it */
3519 /* how many bytes are we allowed to read? */
3520 at_most = connection_bucket_read_limit(conn, approx_time());
3523 slack_in_buf = buf_slack(conn->inbuf);
3524 again:
3525 if ((size_t)at_most > slack_in_buf && slack_in_buf >= 1024) {
3526 more_to_read = at_most - slack_in_buf;
3527 at_most = slack_in_buf;
3528 } else {
3529 more_to_read = 0;
3532 if (connection_speaks_cells(conn) &&
3533 conn->state > OR_CONN_STATE_PROXY_HANDSHAKING) {
3534 int pending;
3535 or_connection_t *or_conn = TO_OR_CONN(conn);
3536 size_t initial_size;
3537 if (conn->state == OR_CONN_STATE_TLS_HANDSHAKING ||
3538 conn->state == OR_CONN_STATE_TLS_CLIENT_RENEGOTIATING) {
3539 /* continue handshaking even if global token bucket is empty */
3540 return connection_tls_continue_handshake(or_conn);
3543 log_debug(LD_NET,
3544 "%d: starting, inbuf_datalen %ld (%d pending in tls object)."
3545 " at_most %ld.",
3546 (int)conn->s,(long)buf_datalen(conn->inbuf),
3547 tor_tls_get_pending_bytes(or_conn->tls), (long)at_most);
3549 initial_size = buf_datalen(conn->inbuf);
3550 /* else open, or closing */
3551 result = read_to_buf_tls(or_conn->tls, at_most, conn->inbuf);
3552 if (TOR_TLS_IS_ERROR(result) || result == TOR_TLS_CLOSE)
3553 or_conn->tls_error = result;
3554 else
3555 or_conn->tls_error = 0;
3557 switch (result) {
3558 case TOR_TLS_CLOSE:
3559 case TOR_TLS_ERROR_IO:
3560 log_debug(LD_NET,"TLS connection closed %son read. Closing. "
3561 "(Nickname %s, address %s)",
3562 result == TOR_TLS_CLOSE ? "cleanly " : "",
3563 or_conn->nickname ? or_conn->nickname : "not set",
3564 conn->address);
3565 return result;
3566 CASE_TOR_TLS_ERROR_ANY_NONIO:
3567 log_debug(LD_NET,"tls error [%s]. breaking (nickname %s, address %s).",
3568 tor_tls_err_to_string(result),
3569 or_conn->nickname ? or_conn->nickname : "not set",
3570 conn->address);
3571 return result;
3572 case TOR_TLS_WANTWRITE:
3573 connection_start_writing(conn);
3574 return 0;
3575 case TOR_TLS_WANTREAD:
3576 if (conn->in_connection_handle_write) {
3577 /* We've been invoked from connection_handle_write, because we're
3578 * waiting for a TLS renegotiation, the renegotiation started, and
3579 * SSL_read returned WANTWRITE. But now SSL_read is saying WANTREAD
3580 * again. Stop waiting for write events now, or else we'll
3581 * busy-loop until data arrives for us to read. */
3582 connection_stop_writing(conn);
3583 if (!connection_is_reading(conn))
3584 connection_start_reading(conn);
3586 /* we're already reading, one hopes */
3587 result = 0;
3588 break;
3589 case TOR_TLS_DONE: /* no data read, so nothing to process */
3590 result = 0;
3591 break; /* so we call bucket_decrement below */
3592 default:
3593 break;
3595 pending = tor_tls_get_pending_bytes(or_conn->tls);
3596 if (pending) {
3597 /* If we have any pending bytes, we read them now. This *can*
3598 * take us over our read allotment, but really we shouldn't be
3599 * believing that SSL bytes are the same as TCP bytes anyway. */
3600 int r2 = read_to_buf_tls(or_conn->tls, pending, conn->inbuf);
3601 if (BUG(r2<0)) {
3602 log_warn(LD_BUG, "apparently, reading pending bytes can fail.");
3603 return -1;
3606 result = (int)(buf_datalen(conn->inbuf)-initial_size);
3607 tor_tls_get_n_raw_bytes(or_conn->tls, &n_read, &n_written);
3608 log_debug(LD_GENERAL, "After TLS read of %d: %ld read, %ld written",
3609 result, (long)n_read, (long)n_written);
3610 } else if (conn->linked) {
3611 if (conn->linked_conn) {
3612 result = move_buf_to_buf(conn->inbuf, conn->linked_conn->outbuf,
3613 &conn->linked_conn->outbuf_flushlen);
3614 } else {
3615 result = 0;
3617 //log_notice(LD_GENERAL, "Moved %d bytes on an internal link!", result);
3618 /* If the other side has disappeared, or if it's been marked for close and
3619 * we flushed its outbuf, then we should set our inbuf_reached_eof. */
3620 if (!conn->linked_conn ||
3621 (conn->linked_conn->marked_for_close &&
3622 buf_datalen(conn->linked_conn->outbuf) == 0))
3623 conn->inbuf_reached_eof = 1;
3625 n_read = (size_t) result;
3626 } else {
3627 /* !connection_speaks_cells, !conn->linked_conn. */
3628 int reached_eof = 0;
3629 CONN_LOG_PROTECT(conn,
3630 result = read_to_buf(conn->s, at_most, conn->inbuf, &reached_eof,
3631 socket_error));
3632 if (reached_eof)
3633 conn->inbuf_reached_eof = 1;
3635 // log_fn(LOG_DEBUG,"read_to_buf returned %d.",read_result);
3637 if (result < 0)
3638 return -1;
3639 n_read = (size_t) result;
3642 if (n_read > 0) {
3643 /* change *max_to_read */
3644 *max_to_read = at_most - n_read;
3646 /* Update edge_conn->n_read and ocirc->n_read_circ_bw */
3647 if (conn->type == CONN_TYPE_AP) {
3648 edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
3649 circuit_t *circ = circuit_get_by_edge_conn(edge_conn);
3650 origin_circuit_t *ocirc;
3652 /* Check for overflow: */
3653 if (PREDICT_LIKELY(UINT32_MAX - edge_conn->n_read > n_read))
3654 edge_conn->n_read += (int)n_read;
3655 else
3656 edge_conn->n_read = UINT32_MAX;
3658 if (circ && CIRCUIT_IS_ORIGIN(circ)) {
3659 ocirc = TO_ORIGIN_CIRCUIT(circ);
3660 if (PREDICT_LIKELY(UINT32_MAX - ocirc->n_read_circ_bw > n_read))
3661 ocirc->n_read_circ_bw += (int)n_read;
3662 else
3663 ocirc->n_read_circ_bw = UINT32_MAX;
3667 /* If CONN_BW events are enabled, update conn->n_read_conn_bw for
3668 * OR/DIR/EXIT connections, checking for overflow. */
3669 if (get_options()->TestingEnableConnBwEvent &&
3670 (conn->type == CONN_TYPE_OR ||
3671 conn->type == CONN_TYPE_DIR ||
3672 conn->type == CONN_TYPE_EXIT)) {
3673 if (PREDICT_LIKELY(UINT32_MAX - conn->n_read_conn_bw > n_read))
3674 conn->n_read_conn_bw += (int)n_read;
3675 else
3676 conn->n_read_conn_bw = UINT32_MAX;
3680 connection_buckets_decrement(conn, approx_time(), n_read, n_written);
3682 if (more_to_read && result == at_most) {
3683 slack_in_buf = buf_slack(conn->inbuf);
3684 at_most = more_to_read;
3685 goto again;
3688 /* Call even if result is 0, since the global read bucket may
3689 * have reached 0 on a different conn, and this connection needs to
3690 * know to stop reading. */
3691 connection_consider_empty_read_buckets(conn);
3692 if (n_written > 0 && connection_is_writing(conn))
3693 connection_consider_empty_write_buckets(conn);
3695 return 0;
3698 /** A pass-through to fetch_from_buf. */
3700 connection_fetch_from_buf(char *string, size_t len, connection_t *conn)
3702 return fetch_from_buf(string, len, conn->inbuf);
3705 /** As fetch_from_buf_line(), but read from a connection's input buffer. */
3707 connection_fetch_from_buf_line(connection_t *conn, char *data,
3708 size_t *data_len)
3710 return fetch_from_buf_line(conn->inbuf, data, data_len);
3713 /** As fetch_from_buf_http, but fetches from a connection's input buffer_t as
3714 * appropriate. */
3716 connection_fetch_from_buf_http(connection_t *conn,
3717 char **headers_out, size_t max_headerlen,
3718 char **body_out, size_t *body_used,
3719 size_t max_bodylen, int force_complete)
3721 return fetch_from_buf_http(conn->inbuf, headers_out, max_headerlen,
3722 body_out, body_used, max_bodylen, force_complete);
3725 /** Return conn-\>outbuf_flushlen: how many bytes conn wants to flush
3726 * from its outbuf. */
3728 connection_wants_to_flush(connection_t *conn)
3730 return conn->outbuf_flushlen > 0;
3733 /** Are there too many bytes on edge connection <b>conn</b>'s outbuf to
3734 * send back a relay-level sendme yet? Return 1 if so, 0 if not. Used by
3735 * connection_edge_consider_sending_sendme().
3738 connection_outbuf_too_full(connection_t *conn)
3740 return (conn->outbuf_flushlen > 10*CELL_PAYLOAD_SIZE);
3743 /** Try to flush more bytes onto <b>conn</b>-\>s.
3745 * This function gets called either from conn_write_callback() in main.c
3746 * when libevent tells us that conn wants to write, or below
3747 * from connection_write_to_buf() when an entire TLS record is ready.
3749 * Update <b>conn</b>-\>timestamp_lastwritten to now, and call flush_buf
3750 * or flush_buf_tls appropriately. If it succeeds and there are no more
3751 * more bytes on <b>conn</b>-\>outbuf, then call connection_finished_flushing
3752 * on it too.
3754 * If <b>force</b>, then write as many bytes as possible, ignoring bandwidth
3755 * limits. (Used for flushing messages to controller connections on fatal
3756 * errors.)
3758 * Mark the connection and return -1 if you want to close it, else
3759 * return 0.
3761 static int
3762 connection_handle_write_impl(connection_t *conn, int force)
3764 int e;
3765 socklen_t len=(socklen_t)sizeof(e);
3766 int result;
3767 ssize_t max_to_write;
3768 time_t now = approx_time();
3769 size_t n_read = 0, n_written = 0;
3770 int dont_stop_writing = 0;
3772 tor_assert(!connection_is_listener(conn));
3774 if (conn->marked_for_close || !SOCKET_OK(conn->s))
3775 return 0; /* do nothing */
3777 if (conn->in_flushed_some) {
3778 log_warn(LD_BUG, "called recursively from inside conn->in_flushed_some");
3779 return 0;
3782 conn->timestamp_lastwritten = now;
3784 /* Sometimes, "writable" means "connected". */
3785 if (connection_state_is_connecting(conn)) {
3786 if (getsockopt(conn->s, SOL_SOCKET, SO_ERROR, (void*)&e, &len) < 0) {
3787 log_warn(LD_BUG, "getsockopt() syscall failed");
3788 if (conn->type == CONN_TYPE_OR) {
3789 or_connection_t *orconn = TO_OR_CONN(conn);
3790 connection_or_close_for_error(orconn, 0);
3791 } else {
3792 if (CONN_IS_EDGE(conn)) {
3793 connection_edge_end_errno(TO_EDGE_CONN(conn));
3795 connection_mark_for_close(conn);
3797 return -1;
3799 if (e) {
3800 /* some sort of error, but maybe just inprogress still */
3801 if (!ERRNO_IS_CONN_EINPROGRESS(e)) {
3802 log_info(LD_NET,"in-progress connect failed. Removing. (%s)",
3803 tor_socket_strerror(e));
3804 if (CONN_IS_EDGE(conn))
3805 connection_edge_end_errno(TO_EDGE_CONN(conn));
3806 if (conn->type == CONN_TYPE_OR)
3807 connection_or_notify_error(TO_OR_CONN(conn),
3808 errno_to_orconn_end_reason(e),
3809 tor_socket_strerror(e));
3811 connection_close_immediate(conn);
3813 * This can bypass normal channel checking since we did
3814 * connection_or_notify_error() above.
3816 connection_mark_for_close_internal(conn);
3817 return -1;
3818 } else {
3819 return 0; /* no change, see if next time is better */
3822 /* The connection is successful. */
3823 if (connection_finished_connecting(conn)<0)
3824 return -1;
3827 max_to_write = force ? (ssize_t)conn->outbuf_flushlen
3828 : connection_bucket_write_limit(conn, now);
3830 if (connection_speaks_cells(conn) &&
3831 conn->state > OR_CONN_STATE_PROXY_HANDSHAKING) {
3832 or_connection_t *or_conn = TO_OR_CONN(conn);
3833 size_t initial_size;
3834 if (conn->state == OR_CONN_STATE_TLS_HANDSHAKING ||
3835 conn->state == OR_CONN_STATE_TLS_CLIENT_RENEGOTIATING) {
3836 connection_stop_writing(conn);
3837 if (connection_tls_continue_handshake(or_conn) < 0) {
3838 /* Don't flush; connection is dead. */
3839 connection_or_notify_error(or_conn,
3840 END_OR_CONN_REASON_MISC,
3841 "TLS error in connection_tls_"
3842 "continue_handshake()");
3843 connection_close_immediate(conn);
3845 * This can bypass normal channel checking since we did
3846 * connection_or_notify_error() above.
3848 connection_mark_for_close_internal(conn);
3849 return -1;
3851 return 0;
3852 } else if (conn->state == OR_CONN_STATE_TLS_SERVER_RENEGOTIATING) {
3853 return connection_handle_read(conn);
3856 /* else open, or closing */
3857 initial_size = buf_datalen(conn->outbuf);
3858 result = flush_buf_tls(or_conn->tls, conn->outbuf,
3859 max_to_write, &conn->outbuf_flushlen);
3861 /* If we just flushed the last bytes, tell the channel on the
3862 * or_conn to check if it needs to geoip_change_dirreq_state() */
3863 /* XXXX move this to flushed_some or finished_flushing -NM */
3864 if (buf_datalen(conn->outbuf) == 0 && or_conn->chan)
3865 channel_notify_flushed(TLS_CHAN_TO_BASE(or_conn->chan));
3867 switch (result) {
3868 CASE_TOR_TLS_ERROR_ANY:
3869 case TOR_TLS_CLOSE:
3870 log_info(LD_NET, result != TOR_TLS_CLOSE ?
3871 "tls error. breaking.":"TLS connection closed on flush");
3872 /* Don't flush; connection is dead. */
3873 connection_or_notify_error(or_conn,
3874 END_OR_CONN_REASON_MISC,
3875 result != TOR_TLS_CLOSE ?
3876 "TLS error in during flush" :
3877 "TLS closed during flush");
3878 connection_close_immediate(conn);
3880 * This can bypass normal channel checking since we did
3881 * connection_or_notify_error() above.
3883 connection_mark_for_close_internal(conn);
3884 return -1;
3885 case TOR_TLS_WANTWRITE:
3886 log_debug(LD_NET,"wanted write.");
3887 /* we're already writing */
3888 dont_stop_writing = 1;
3889 break;
3890 case TOR_TLS_WANTREAD:
3891 /* Make sure to avoid a loop if the receive buckets are empty. */
3892 log_debug(LD_NET,"wanted read.");
3893 if (!connection_is_reading(conn)) {
3894 connection_stop_writing(conn);
3895 conn->write_blocked_on_bw = 1;
3896 /* we'll start reading again when we get more tokens in our
3897 * read bucket; then we'll start writing again too.
3900 /* else no problem, we're already reading */
3901 return 0;
3902 /* case TOR_TLS_DONE:
3903 * for TOR_TLS_DONE, fall through to check if the flushlen
3904 * is empty, so we can stop writing.
3908 tor_tls_get_n_raw_bytes(or_conn->tls, &n_read, &n_written);
3909 log_debug(LD_GENERAL, "After TLS write of %d: %ld read, %ld written",
3910 result, (long)n_read, (long)n_written);
3911 or_conn->bytes_xmitted += result;
3912 or_conn->bytes_xmitted_by_tls += n_written;
3913 /* So we notice bytes were written even on error */
3914 /* XXXX This cast is safe since we can never write INT_MAX bytes in a
3915 * single set of TLS operations. But it looks kinda ugly. If we refactor
3916 * the *_buf_tls functions, we should make them return ssize_t or size_t
3917 * or something. */
3918 result = (int)(initial_size-buf_datalen(conn->outbuf));
3919 } else {
3920 CONN_LOG_PROTECT(conn,
3921 result = flush_buf(conn->s, conn->outbuf,
3922 max_to_write, &conn->outbuf_flushlen));
3923 if (result < 0) {
3924 if (CONN_IS_EDGE(conn))
3925 connection_edge_end_errno(TO_EDGE_CONN(conn));
3926 if (conn->type == CONN_TYPE_AP) {
3927 /* writing failed; we couldn't send a SOCKS reply if we wanted to */
3928 TO_ENTRY_CONN(conn)->socks_request->has_finished = 1;
3931 connection_close_immediate(conn); /* Don't flush; connection is dead. */
3932 connection_mark_for_close(conn);
3933 return -1;
3935 n_written = (size_t) result;
3938 if (n_written && conn->type == CONN_TYPE_AP) {
3939 edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
3940 circuit_t *circ = circuit_get_by_edge_conn(edge_conn);
3941 origin_circuit_t *ocirc;
3943 /* Check for overflow: */
3944 if (PREDICT_LIKELY(UINT32_MAX - edge_conn->n_written > n_written))
3945 edge_conn->n_written += (int)n_written;
3946 else
3947 edge_conn->n_written = UINT32_MAX;
3949 if (circ && CIRCUIT_IS_ORIGIN(circ)) {
3950 ocirc = TO_ORIGIN_CIRCUIT(circ);
3951 if (PREDICT_LIKELY(UINT32_MAX - ocirc->n_written_circ_bw > n_written))
3952 ocirc->n_written_circ_bw += (int)n_written;
3953 else
3954 ocirc->n_written_circ_bw = UINT32_MAX;
3958 /* If CONN_BW events are enabled, update conn->n_written_conn_bw for
3959 * OR/DIR/EXIT connections, checking for overflow. */
3960 if (n_written && get_options()->TestingEnableConnBwEvent &&
3961 (conn->type == CONN_TYPE_OR ||
3962 conn->type == CONN_TYPE_DIR ||
3963 conn->type == CONN_TYPE_EXIT)) {
3964 if (PREDICT_LIKELY(UINT32_MAX - conn->n_written_conn_bw > n_written))
3965 conn->n_written_conn_bw += (int)n_written;
3966 else
3967 conn->n_written_conn_bw = UINT32_MAX;
3970 connection_buckets_decrement(conn, approx_time(), n_read, n_written);
3972 if (result > 0) {
3973 /* If we wrote any bytes from our buffer, then call the appropriate
3974 * functions. */
3975 if (connection_flushed_some(conn) < 0) {
3976 if (connection_speaks_cells(conn)) {
3977 connection_or_notify_error(TO_OR_CONN(conn),
3978 END_OR_CONN_REASON_MISC,
3979 "Got error back from "
3980 "connection_flushed_some()");
3984 * This can bypass normal channel checking since we did
3985 * connection_or_notify_error() above.
3987 connection_mark_for_close_internal(conn);
3991 if (!connection_wants_to_flush(conn) &&
3992 !dont_stop_writing) { /* it's done flushing */
3993 if (connection_finished_flushing(conn) < 0) {
3994 /* already marked */
3995 return -1;
3997 return 0;
4000 /* Call even if result is 0, since the global write bucket may
4001 * have reached 0 on a different conn, and this connection needs to
4002 * know to stop writing. */
4003 connection_consider_empty_write_buckets(conn);
4004 if (n_read > 0 && connection_is_reading(conn))
4005 connection_consider_empty_read_buckets(conn);
4007 return 0;
4010 /* DOCDOC connection_handle_write */
4012 connection_handle_write(connection_t *conn, int force)
4014 int res;
4015 tor_gettimeofday_cache_clear();
4016 conn->in_connection_handle_write = 1;
4017 res = connection_handle_write_impl(conn, force);
4018 conn->in_connection_handle_write = 0;
4019 return res;
4023 * Try to flush data that's waiting for a write on <b>conn</b>. Return
4024 * -1 on failure, 0 on success.
4026 * Don't use this function for regular writing; the buffers
4027 * system should be good enough at scheduling writes there. Instead, this
4028 * function is for cases when we're about to exit or something and we want
4029 * to report it right away.
4032 connection_flush(connection_t *conn)
4034 return connection_handle_write(conn, 1);
4037 /** Append <b>len</b> bytes of <b>string</b> onto <b>conn</b>'s
4038 * outbuf, and ask it to start writing.
4040 * If <b>zlib</b> is nonzero, this is a directory connection that should get
4041 * its contents compressed or decompressed as they're written. If zlib is
4042 * negative, this is the last data to be compressed, and the connection's zlib
4043 * state should be flushed.
4045 MOCK_IMPL(void,
4046 connection_write_to_buf_impl_,(const char *string, size_t len,
4047 connection_t *conn, int zlib))
4049 /* XXXX This function really needs to return -1 on failure. */
4050 int r;
4051 size_t old_datalen;
4052 if (!len && !(zlib<0))
4053 return;
4054 /* if it's marked for close, only allow write if we mean to flush it */
4055 if (conn->marked_for_close && !conn->hold_open_until_flushed)
4056 return;
4058 old_datalen = buf_datalen(conn->outbuf);
4059 if (zlib) {
4060 dir_connection_t *dir_conn = TO_DIR_CONN(conn);
4061 int done = zlib < 0;
4062 CONN_LOG_PROTECT(conn, r = write_to_buf_compress(conn->outbuf,
4063 dir_conn->compress_state,
4064 string, len, done));
4065 } else {
4066 CONN_LOG_PROTECT(conn, r = write_to_buf(string, len, conn->outbuf));
4068 if (r < 0) {
4069 if (CONN_IS_EDGE(conn)) {
4070 /* if it failed, it means we have our package/delivery windows set
4071 wrong compared to our max outbuf size. close the whole circuit. */
4072 log_warn(LD_NET,
4073 "write_to_buf failed. Closing circuit (fd %d).", (int)conn->s);
4074 circuit_mark_for_close(circuit_get_by_edge_conn(TO_EDGE_CONN(conn)),
4075 END_CIRC_REASON_INTERNAL);
4076 } else if (conn->type == CONN_TYPE_OR) {
4077 or_connection_t *orconn = TO_OR_CONN(conn);
4078 log_warn(LD_NET,
4079 "write_to_buf failed on an orconn; notifying of error "
4080 "(fd %d)", (int)(conn->s));
4081 connection_or_close_for_error(orconn, 0);
4082 } else {
4083 log_warn(LD_NET,
4084 "write_to_buf failed. Closing connection (fd %d).",
4085 (int)conn->s);
4086 connection_mark_for_close(conn);
4088 return;
4091 /* If we receive optimistic data in the EXIT_CONN_STATE_RESOLVING
4092 * state, we don't want to try to write it right away, since
4093 * conn->write_event won't be set yet. Otherwise, write data from
4094 * this conn as the socket is available. */
4095 if (conn->write_event) {
4096 connection_start_writing(conn);
4098 if (zlib) {
4099 conn->outbuf_flushlen += buf_datalen(conn->outbuf) - old_datalen;
4100 } else {
4101 conn->outbuf_flushlen += len;
4105 #define CONN_GET_ALL_TEMPLATE(var, test) \
4106 STMT_BEGIN \
4107 smartlist_t *conns = get_connection_array(); \
4108 smartlist_t *ret_conns = smartlist_new(); \
4109 SMARTLIST_FOREACH_BEGIN(conns, connection_t *, var) { \
4110 if (var && (test) && !var->marked_for_close) \
4111 smartlist_add(ret_conns, var); \
4112 } SMARTLIST_FOREACH_END(var); \
4113 return ret_conns; \
4114 STMT_END
4116 /* Return a list of connections that aren't close and matches the given state.
4117 * The returned list can be empty and must be freed using smartlist_free().
4118 * The caller does NOT have owernship of the objects in the list so it must
4119 * not free them nor reference them as they can disapear. */
4120 smartlist_t *
4121 connection_list_by_type_state(int type, int state)
4123 CONN_GET_ALL_TEMPLATE(conn, (conn->type == type && conn->state == state));
4126 /** Return a connection_t * from get_connection_array() that satisfies test on
4127 * var, and that is not marked for close. */
4128 #define CONN_GET_TEMPLATE(var, test) \
4129 STMT_BEGIN \
4130 smartlist_t *conns = get_connection_array(); \
4131 SMARTLIST_FOREACH(conns, connection_t *, var, \
4133 if (var && (test) && !var->marked_for_close) \
4134 return var; \
4135 }); \
4136 return NULL; \
4137 STMT_END
4139 /** Return a connection with given type, address, port, and purpose;
4140 * or NULL if no such connection exists (or if all such connections are marked
4141 * for close). */
4142 MOCK_IMPL(connection_t *,
4143 connection_get_by_type_addr_port_purpose,(int type,
4144 const tor_addr_t *addr, uint16_t port,
4145 int purpose))
4147 CONN_GET_TEMPLATE(conn,
4148 (conn->type == type &&
4149 tor_addr_eq(&conn->addr, addr) &&
4150 conn->port == port &&
4151 conn->purpose == purpose));
4154 /** Return the stream with id <b>id</b> if it is not already marked for
4155 * close.
4157 connection_t *
4158 connection_get_by_global_id(uint64_t id)
4160 CONN_GET_TEMPLATE(conn, conn->global_identifier == id);
4163 /** Return a connection of type <b>type</b> that is not marked for close.
4165 connection_t *
4166 connection_get_by_type(int type)
4168 CONN_GET_TEMPLATE(conn, conn->type == type);
4171 /** Return a connection of type <b>type</b> that is in state <b>state</b>,
4172 * and that is not marked for close.
4174 connection_t *
4175 connection_get_by_type_state(int type, int state)
4177 CONN_GET_TEMPLATE(conn, conn->type == type && conn->state == state);
4180 /** Return a connection of type <b>type</b> that has rendquery equal
4181 * to <b>rendquery</b>, and that is not marked for close. If state
4182 * is non-zero, conn must be of that state too.
4184 connection_t *
4185 connection_get_by_type_state_rendquery(int type, int state,
4186 const char *rendquery)
4188 tor_assert(type == CONN_TYPE_DIR ||
4189 type == CONN_TYPE_AP || type == CONN_TYPE_EXIT);
4190 tor_assert(rendquery);
4192 CONN_GET_TEMPLATE(conn,
4193 (conn->type == type &&
4194 (!state || state == conn->state)) &&
4196 (type == CONN_TYPE_DIR &&
4197 TO_DIR_CONN(conn)->rend_data &&
4198 !rend_cmp_service_ids(rendquery,
4199 rend_data_get_address(TO_DIR_CONN(conn)->rend_data)))
4201 (CONN_IS_EDGE(conn) &&
4202 TO_EDGE_CONN(conn)->rend_data &&
4203 !rend_cmp_service_ids(rendquery,
4204 rend_data_get_address(TO_EDGE_CONN(conn)->rend_data)))
4208 /** Return a new smartlist of dir_connection_t * from get_connection_array()
4209 * that satisfy conn_test on connection_t *conn_var, and dirconn_test on
4210 * dir_connection_t *dirconn_var. conn_var must be of CONN_TYPE_DIR and not
4211 * marked for close to be included in the list. */
4212 #define DIR_CONN_LIST_TEMPLATE(conn_var, conn_test, \
4213 dirconn_var, dirconn_test) \
4214 STMT_BEGIN \
4215 smartlist_t *conns = get_connection_array(); \
4216 smartlist_t *dir_conns = smartlist_new(); \
4217 SMARTLIST_FOREACH_BEGIN(conns, connection_t *, conn_var) { \
4218 if (conn_var && (conn_test) \
4219 && conn_var->type == CONN_TYPE_DIR \
4220 && !conn_var->marked_for_close) { \
4221 dir_connection_t *dirconn_var = TO_DIR_CONN(conn_var); \
4222 if (dirconn_var && (dirconn_test)) { \
4223 smartlist_add(dir_conns, dirconn_var); \
4226 } SMARTLIST_FOREACH_END(conn_var); \
4227 return dir_conns; \
4228 STMT_END
4230 /** Return a list of directory connections that are fetching the item
4231 * described by <b>purpose</b>/<b>resource</b>. If there are none,
4232 * return an empty list. This list must be freed using smartlist_free,
4233 * but the pointers in it must not be freed.
4234 * Note that this list should not be cached, as the pointers in it can be
4235 * freed if their connections close. */
4236 smartlist_t *
4237 connection_dir_list_by_purpose_and_resource(
4238 int purpose,
4239 const char *resource)
4241 DIR_CONN_LIST_TEMPLATE(conn,
4242 conn->purpose == purpose,
4243 dirconn,
4244 0 == strcmp_opt(resource,
4245 dirconn->requested_resource));
4248 /** Return a list of directory connections that are fetching the item
4249 * described by <b>purpose</b>/<b>resource</b>/<b>state</b>. If there are
4250 * none, return an empty list. This list must be freed using smartlist_free,
4251 * but the pointers in it must not be freed.
4252 * Note that this list should not be cached, as the pointers in it can be
4253 * freed if their connections close. */
4254 smartlist_t *
4255 connection_dir_list_by_purpose_resource_and_state(
4256 int purpose,
4257 const char *resource,
4258 int state)
4260 DIR_CONN_LIST_TEMPLATE(conn,
4261 conn->purpose == purpose && conn->state == state,
4262 dirconn,
4263 0 == strcmp_opt(resource,
4264 dirconn->requested_resource));
4267 #undef DIR_CONN_LIST_TEMPLATE
4269 /** Return an arbitrary active OR connection that isn't <b>this_conn</b>.
4271 * We use this to guess if we should tell the controller that we
4272 * didn't manage to connect to any of our bridges. */
4273 static connection_t *
4274 connection_get_another_active_or_conn(const or_connection_t *this_conn)
4276 CONN_GET_TEMPLATE(conn,
4277 conn != TO_CONN(this_conn) && conn->type == CONN_TYPE_OR);
4280 /** Return 1 if there are any active OR connections apart from
4281 * <b>this_conn</b>.
4283 * We use this to guess if we should tell the controller that we
4284 * didn't manage to connect to any of our bridges. */
4286 any_other_active_or_conns(const or_connection_t *this_conn)
4288 connection_t *conn = connection_get_another_active_or_conn(this_conn);
4289 if (conn != NULL) {
4290 log_debug(LD_DIR, "%s: Found an OR connection: %s",
4291 __func__, conn->address);
4292 return 1;
4295 return 0;
4298 #undef CONN_GET_TEMPLATE
4300 /** Return 1 if <b>conn</b> is a listener conn, else return 0. */
4302 connection_is_listener(connection_t *conn)
4304 if (conn->type == CONN_TYPE_OR_LISTENER ||
4305 conn->type == CONN_TYPE_EXT_OR_LISTENER ||
4306 conn->type == CONN_TYPE_AP_LISTENER ||
4307 conn->type == CONN_TYPE_AP_TRANS_LISTENER ||
4308 conn->type == CONN_TYPE_AP_DNS_LISTENER ||
4309 conn->type == CONN_TYPE_AP_NATD_LISTENER ||
4310 conn->type == CONN_TYPE_DIR_LISTENER ||
4311 conn->type == CONN_TYPE_CONTROL_LISTENER)
4312 return 1;
4313 return 0;
4316 /** Return 1 if <b>conn</b> is in state "open" and is not marked
4317 * for close, else return 0.
4320 connection_state_is_open(connection_t *conn)
4322 tor_assert(conn);
4324 if (conn->marked_for_close)
4325 return 0;
4327 if ((conn->type == CONN_TYPE_OR && conn->state == OR_CONN_STATE_OPEN) ||
4328 (conn->type == CONN_TYPE_EXT_OR) ||
4329 (conn->type == CONN_TYPE_AP && conn->state == AP_CONN_STATE_OPEN) ||
4330 (conn->type == CONN_TYPE_EXIT && conn->state == EXIT_CONN_STATE_OPEN) ||
4331 (conn->type == CONN_TYPE_CONTROL &&
4332 conn->state == CONTROL_CONN_STATE_OPEN))
4333 return 1;
4335 return 0;
4338 /** Return 1 if conn is in 'connecting' state, else return 0. */
4340 connection_state_is_connecting(connection_t *conn)
4342 tor_assert(conn);
4344 if (conn->marked_for_close)
4345 return 0;
4346 switch (conn->type)
4348 case CONN_TYPE_OR:
4349 return conn->state == OR_CONN_STATE_CONNECTING;
4350 case CONN_TYPE_EXIT:
4351 return conn->state == EXIT_CONN_STATE_CONNECTING;
4352 case CONN_TYPE_DIR:
4353 return conn->state == DIR_CONN_STATE_CONNECTING;
4356 return 0;
4359 /** Allocates a base64'ed authenticator for use in http or https
4360 * auth, based on the input string <b>authenticator</b>. Returns it
4361 * if success, else returns NULL. */
4362 char *
4363 alloc_http_authenticator(const char *authenticator)
4365 /* an authenticator in Basic authentication
4366 * is just the string "username:password" */
4367 const size_t authenticator_length = strlen(authenticator);
4368 const size_t base64_authenticator_length =
4369 base64_encode_size(authenticator_length, 0) + 1;
4370 char *base64_authenticator = tor_malloc(base64_authenticator_length);
4371 if (base64_encode(base64_authenticator, base64_authenticator_length,
4372 authenticator, authenticator_length, 0) < 0) {
4373 tor_free(base64_authenticator); /* free and set to null */
4375 return base64_authenticator;
4378 /** Given a socket handle, check whether the local address (sockname) of the
4379 * socket is one that we've connected from before. If so, double-check
4380 * whether our address has changed and we need to generate keys. If we do,
4381 * call init_keys().
4383 static void
4384 client_check_address_changed(tor_socket_t sock)
4386 struct sockaddr_storage out_sockaddr;
4387 socklen_t out_addr_len = (socklen_t) sizeof(out_sockaddr);
4388 tor_addr_t out_addr, iface_addr;
4389 tor_addr_t **last_interface_ip_ptr;
4390 sa_family_t family;
4392 if (!outgoing_addrs)
4393 outgoing_addrs = smartlist_new();
4395 if (getsockname(sock, (struct sockaddr*)&out_sockaddr, &out_addr_len)<0) {
4396 int e = tor_socket_errno(sock);
4397 log_warn(LD_NET, "getsockname() to check for address change failed: %s",
4398 tor_socket_strerror(e));
4399 return;
4401 tor_addr_from_sockaddr(&out_addr, (struct sockaddr*)&out_sockaddr, NULL);
4402 family = tor_addr_family(&out_addr);
4404 if (family == AF_INET)
4405 last_interface_ip_ptr = &last_interface_ipv4;
4406 else if (family == AF_INET6)
4407 last_interface_ip_ptr = &last_interface_ipv6;
4408 else
4409 return;
4411 if (! *last_interface_ip_ptr) {
4412 tor_addr_t *a = tor_malloc_zero(sizeof(tor_addr_t));
4413 if (get_interface_address6(LOG_INFO, family, a)==0) {
4414 *last_interface_ip_ptr = a;
4415 } else {
4416 tor_free(a);
4420 /* If we've used this address previously, we're okay. */
4421 SMARTLIST_FOREACH(outgoing_addrs, const tor_addr_t *, a_ptr,
4422 if (tor_addr_eq(a_ptr, &out_addr))
4423 return;
4426 /* Uh-oh. We haven't connected from this address before. Has the interface
4427 * address changed? */
4428 if (get_interface_address6(LOG_INFO, family, &iface_addr)<0)
4429 return;
4431 if (tor_addr_eq(&iface_addr, *last_interface_ip_ptr)) {
4432 /* Nope, it hasn't changed. Add this address to the list. */
4433 smartlist_add(outgoing_addrs, tor_memdup(&out_addr, sizeof(tor_addr_t)));
4434 } else {
4435 /* The interface changed. We're a client, so we need to regenerate our
4436 * keys. First, reset the state. */
4437 log_notice(LD_NET, "Our IP address has changed. Rotating keys...");
4438 tor_addr_copy(*last_interface_ip_ptr, &iface_addr);
4439 SMARTLIST_FOREACH(outgoing_addrs, tor_addr_t*, a_ptr, tor_free(a_ptr));
4440 smartlist_clear(outgoing_addrs);
4441 smartlist_add(outgoing_addrs, tor_memdup(&out_addr, sizeof(tor_addr_t)));
4442 /* We'll need to resolve ourselves again. */
4443 reset_last_resolved_addr();
4444 /* Okay, now change our keys. */
4445 ip_address_changed(1);
4449 /** Some systems have limited system buffers for recv and xmit on
4450 * sockets allocated in a virtual server or similar environment. For a Tor
4451 * server this can produce the "Error creating network socket: No buffer
4452 * space available" error once all available TCP buffer space is consumed.
4453 * This method will attempt to constrain the buffers allocated for the socket
4454 * to the desired size to stay below system TCP buffer limits.
4456 static void
4457 set_constrained_socket_buffers(tor_socket_t sock, int size)
4459 void *sz = (void*)&size;
4460 socklen_t sz_sz = (socklen_t) sizeof(size);
4461 if (setsockopt(sock, SOL_SOCKET, SO_SNDBUF, sz, sz_sz) < 0) {
4462 int e = tor_socket_errno(sock);
4463 log_warn(LD_NET, "setsockopt() to constrain send "
4464 "buffer to %d bytes failed: %s", size, tor_socket_strerror(e));
4466 if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF, sz, sz_sz) < 0) {
4467 int e = tor_socket_errno(sock);
4468 log_warn(LD_NET, "setsockopt() to constrain recv "
4469 "buffer to %d bytes failed: %s", size, tor_socket_strerror(e));
4473 /** Process new bytes that have arrived on conn-\>inbuf.
4475 * This function just passes conn to the connection-specific
4476 * connection_*_process_inbuf() function. It also passes in
4477 * package_partial if wanted.
4479 static int
4480 connection_process_inbuf(connection_t *conn, int package_partial)
4482 tor_assert(conn);
4484 switch (conn->type) {
4485 case CONN_TYPE_OR:
4486 return connection_or_process_inbuf(TO_OR_CONN(conn));
4487 case CONN_TYPE_EXT_OR:
4488 return connection_ext_or_process_inbuf(TO_OR_CONN(conn));
4489 case CONN_TYPE_EXIT:
4490 case CONN_TYPE_AP:
4491 return connection_edge_process_inbuf(TO_EDGE_CONN(conn),
4492 package_partial);
4493 case CONN_TYPE_DIR:
4494 return connection_dir_process_inbuf(TO_DIR_CONN(conn));
4495 case CONN_TYPE_CONTROL:
4496 return connection_control_process_inbuf(TO_CONTROL_CONN(conn));
4497 default:
4498 log_err(LD_BUG,"got unexpected conn type %d.", conn->type);
4499 tor_fragile_assert();
4500 return -1;
4504 /** Called whenever we've written data on a connection. */
4505 static int
4506 connection_flushed_some(connection_t *conn)
4508 int r = 0;
4509 tor_assert(!conn->in_flushed_some);
4510 conn->in_flushed_some = 1;
4511 if (conn->type == CONN_TYPE_DIR &&
4512 conn->state == DIR_CONN_STATE_SERVER_WRITING) {
4513 r = connection_dirserv_flushed_some(TO_DIR_CONN(conn));
4514 } else if (conn->type == CONN_TYPE_OR) {
4515 r = connection_or_flushed_some(TO_OR_CONN(conn));
4516 } else if (CONN_IS_EDGE(conn)) {
4517 r = connection_edge_flushed_some(TO_EDGE_CONN(conn));
4519 conn->in_flushed_some = 0;
4520 return r;
4523 /** We just finished flushing bytes to the appropriately low network layer,
4524 * and there are no more bytes remaining in conn-\>outbuf or
4525 * conn-\>tls to be flushed.
4527 * This function just passes conn to the connection-specific
4528 * connection_*_finished_flushing() function.
4530 static int
4531 connection_finished_flushing(connection_t *conn)
4533 tor_assert(conn);
4535 /* If the connection is closed, don't try to do anything more here. */
4536 if (CONN_IS_CLOSED(conn))
4537 return 0;
4539 // log_fn(LOG_DEBUG,"entered. Socket %u.", conn->s);
4541 connection_stop_writing(conn);
4543 switch (conn->type) {
4544 case CONN_TYPE_OR:
4545 return connection_or_finished_flushing(TO_OR_CONN(conn));
4546 case CONN_TYPE_EXT_OR:
4547 return connection_ext_or_finished_flushing(TO_OR_CONN(conn));
4548 case CONN_TYPE_AP:
4549 case CONN_TYPE_EXIT:
4550 return connection_edge_finished_flushing(TO_EDGE_CONN(conn));
4551 case CONN_TYPE_DIR:
4552 return connection_dir_finished_flushing(TO_DIR_CONN(conn));
4553 case CONN_TYPE_CONTROL:
4554 return connection_control_finished_flushing(TO_CONTROL_CONN(conn));
4555 default:
4556 log_err(LD_BUG,"got unexpected conn type %d.", conn->type);
4557 tor_fragile_assert();
4558 return -1;
4562 /** Called when our attempt to connect() to another server has just
4563 * succeeded.
4565 * This function just passes conn to the connection-specific
4566 * connection_*_finished_connecting() function.
4568 static int
4569 connection_finished_connecting(connection_t *conn)
4571 tor_assert(conn);
4573 if (!server_mode(get_options())) {
4574 /* See whether getsockname() says our address changed. We need to do this
4575 * now that the connection has finished, because getsockname() on Windows
4576 * won't work until then. */
4577 client_check_address_changed(conn->s);
4580 switch (conn->type)
4582 case CONN_TYPE_OR:
4583 return connection_or_finished_connecting(TO_OR_CONN(conn));
4584 case CONN_TYPE_EXIT:
4585 return connection_edge_finished_connecting(TO_EDGE_CONN(conn));
4586 case CONN_TYPE_DIR:
4587 return connection_dir_finished_connecting(TO_DIR_CONN(conn));
4588 default:
4589 log_err(LD_BUG,"got unexpected conn type %d.", conn->type);
4590 tor_fragile_assert();
4591 return -1;
4595 /** Callback: invoked when a connection reaches an EOF event. */
4596 static int
4597 connection_reached_eof(connection_t *conn)
4599 switch (conn->type) {
4600 case CONN_TYPE_OR:
4601 case CONN_TYPE_EXT_OR:
4602 return connection_or_reached_eof(TO_OR_CONN(conn));
4603 case CONN_TYPE_AP:
4604 case CONN_TYPE_EXIT:
4605 return connection_edge_reached_eof(TO_EDGE_CONN(conn));
4606 case CONN_TYPE_DIR:
4607 return connection_dir_reached_eof(TO_DIR_CONN(conn));
4608 case CONN_TYPE_CONTROL:
4609 return connection_control_reached_eof(TO_CONTROL_CONN(conn));
4610 default:
4611 log_err(LD_BUG,"got unexpected conn type %d.", conn->type);
4612 tor_fragile_assert();
4613 return -1;
4617 /** Comparator for the two-orconn case in OOS victim sort */
4618 static int
4619 oos_victim_comparator_for_orconns(or_connection_t *a, or_connection_t *b)
4621 int a_circs, b_circs;
4622 /* Fewer circuits == higher priority for OOS kill, sort earlier */
4624 a_circs = connection_or_get_num_circuits(a);
4625 b_circs = connection_or_get_num_circuits(b);
4627 if (a_circs < b_circs) return 1;
4628 else if (a_circs > b_circs) return -1;
4629 else return 0;
4632 /** Sort comparator for OOS victims; better targets sort before worse
4633 * ones. */
4634 static int
4635 oos_victim_comparator(const void **a_v, const void **b_v)
4637 connection_t *a = NULL, *b = NULL;
4639 /* Get connection pointers out */
4641 a = (connection_t *)(*a_v);
4642 b = (connection_t *)(*b_v);
4644 tor_assert(a != NULL);
4645 tor_assert(b != NULL);
4648 * We always prefer orconns as victims currently; we won't even see
4649 * these non-orconn cases, but if we do, sort them after orconns.
4651 if (a->type == CONN_TYPE_OR && b->type == CONN_TYPE_OR) {
4652 return oos_victim_comparator_for_orconns(TO_OR_CONN(a), TO_OR_CONN(b));
4653 } else {
4655 * One isn't an orconn; if one is, it goes first. We currently have no
4656 * opinions about cases where neither is an orconn.
4658 if (a->type == CONN_TYPE_OR) return -1;
4659 else if (b->type == CONN_TYPE_OR) return 1;
4660 else return 0;
4664 /** Pick n victim connections for the OOS handler and return them in a
4665 * smartlist.
4667 MOCK_IMPL(STATIC smartlist_t *,
4668 pick_oos_victims, (int n))
4670 smartlist_t *eligible = NULL, *victims = NULL;
4671 smartlist_t *conns;
4672 int conn_counts_by_type[CONN_TYPE_MAX_ + 1], i;
4675 * Big damn assumption (someone improve this someday!):
4677 * Socket exhaustion normally happens on high-volume relays, and so
4678 * most of the connections involved are orconns. We should pick victims
4679 * by assembling a list of all orconns, and sorting them in order of
4680 * how much 'damage' by some metric we'd be doing by dropping them.
4682 * If we move on from orconns, we should probably think about incoming
4683 * directory connections next, or exit connections. Things we should
4684 * probably never kill are controller connections and listeners.
4686 * This function will count how many connections of different types
4687 * exist and log it for purposes of gathering data on typical OOS
4688 * situations to guide future improvements.
4691 /* First, get the connection array */
4692 conns = get_connection_array();
4694 * Iterate it and pick out eligible connection types, and log some stats
4695 * along the way.
4697 eligible = smartlist_new();
4698 memset(conn_counts_by_type, 0, sizeof(conn_counts_by_type));
4699 SMARTLIST_FOREACH_BEGIN(conns, connection_t *, c) {
4700 /* Bump the counter */
4701 tor_assert(c->type <= CONN_TYPE_MAX_);
4702 ++(conn_counts_by_type[c->type]);
4704 /* Skip anything without a socket we can free */
4705 if (!(SOCKET_OK(c->s))) {
4706 continue;
4709 /* Skip anything we would count as moribund */
4710 if (connection_is_moribund(c)) {
4711 continue;
4714 switch (c->type) {
4715 case CONN_TYPE_OR:
4716 /* We've got an orconn, it's eligible to be OOSed */
4717 smartlist_add(eligible, c);
4718 break;
4719 default:
4720 /* We don't know what to do with it, ignore it */
4721 break;
4723 } SMARTLIST_FOREACH_END(c);
4725 /* Log some stats */
4726 if (smartlist_len(conns) > 0) {
4727 /* At least one counter must be non-zero */
4728 log_info(LD_NET, "Some stats on conn types seen during OOS follow");
4729 for (i = CONN_TYPE_MIN_; i <= CONN_TYPE_MAX_; ++i) {
4730 /* Did we see any? */
4731 if (conn_counts_by_type[i] > 0) {
4732 log_info(LD_NET, "%s: %d conns",
4733 conn_type_to_string(i),
4734 conn_counts_by_type[i]);
4737 log_info(LD_NET, "Done with OOS conn type stats");
4740 /* Did we find more eligible targets than we want to kill? */
4741 if (smartlist_len(eligible) > n) {
4742 /* Sort the list in order of target preference */
4743 smartlist_sort(eligible, oos_victim_comparator);
4744 /* Pick first n as victims */
4745 victims = smartlist_new();
4746 for (i = 0; i < n; ++i) {
4747 smartlist_add(victims, smartlist_get(eligible, i));
4749 /* Free the original list */
4750 smartlist_free(eligible);
4751 } else {
4752 /* No, we can just call them all victims */
4753 victims = eligible;
4756 return victims;
4759 /** Kill a list of connections for the OOS handler. */
4760 MOCK_IMPL(STATIC void,
4761 kill_conn_list_for_oos, (smartlist_t *conns))
4763 if (!conns) return;
4765 SMARTLIST_FOREACH_BEGIN(conns, connection_t *, c) {
4766 /* Make sure the channel layer gets told about orconns */
4767 if (c->type == CONN_TYPE_OR) {
4768 connection_or_close_for_error(TO_OR_CONN(c), 1);
4769 } else {
4770 connection_mark_for_close(c);
4772 } SMARTLIST_FOREACH_END(c);
4774 log_notice(LD_NET,
4775 "OOS handler marked %d connections",
4776 smartlist_len(conns));
4779 /** Out-of-Sockets handler; n_socks is the current number of open
4780 * sockets, and failed is non-zero if a socket exhaustion related
4781 * error immediately preceded this call. This is where to do
4782 * circuit-killing heuristics as needed.
4784 void
4785 connection_check_oos(int n_socks, int failed)
4787 int target_n_socks = 0, moribund_socks, socks_to_kill;
4788 smartlist_t *conns;
4790 /* Early exit: is OOS checking disabled? */
4791 if (get_options()->DisableOOSCheck) {
4792 return;
4795 /* Sanity-check args */
4796 tor_assert(n_socks >= 0);
4799 * Make some log noise; keep it at debug level since this gets a chance
4800 * to run on every connection attempt.
4802 log_debug(LD_NET,
4803 "Running the OOS handler (%d open sockets, %s)",
4804 n_socks, (failed != 0) ? "exhaustion seen" : "no exhaustion");
4807 * Check if we're really handling an OOS condition, and if so decide how
4808 * many sockets we want to get down to. Be sure we check if the threshold
4809 * is distinct from zero first; it's possible for this to be called a few
4810 * times before we've finished reading the config.
4812 if (n_socks >= get_options()->ConnLimit_high_thresh &&
4813 get_options()->ConnLimit_high_thresh != 0 &&
4814 get_options()->ConnLimit_ != 0) {
4815 /* Try to get down to the low threshold */
4816 target_n_socks = get_options()->ConnLimit_low_thresh;
4817 log_notice(LD_NET,
4818 "Current number of sockets %d is greater than configured "
4819 "limit %d; OOS handler trying to get down to %d",
4820 n_socks, get_options()->ConnLimit_high_thresh,
4821 target_n_socks);
4822 } else if (failed) {
4824 * If we're not at the limit but we hit a socket exhaustion error, try to
4825 * drop some (but not as aggressively as ConnLimit_low_threshold, which is
4826 * 3/4 of ConnLimit_)
4828 target_n_socks = (n_socks * 9) / 10;
4829 log_notice(LD_NET,
4830 "We saw socket exhaustion at %d open sockets; OOS handler "
4831 "trying to get down to %d",
4832 n_socks, target_n_socks);
4835 if (target_n_socks > 0) {
4837 * It's an OOS!
4839 * Count moribund sockets; it's be important that anything we decide
4840 * to get rid of here but don't immediately close get counted as moribund
4841 * on subsequent invocations so we don't try to kill too many things if
4842 * connection_check_oos() gets called multiple times.
4844 moribund_socks = connection_count_moribund();
4846 if (moribund_socks < n_socks - target_n_socks) {
4847 socks_to_kill = n_socks - target_n_socks - moribund_socks;
4849 conns = pick_oos_victims(socks_to_kill);
4850 if (conns) {
4851 kill_conn_list_for_oos(conns);
4852 log_notice(LD_NET,
4853 "OOS handler killed %d conns", smartlist_len(conns));
4854 smartlist_free(conns);
4855 } else {
4856 log_notice(LD_NET, "OOS handler failed to pick any victim conns");
4858 } else {
4859 log_notice(LD_NET,
4860 "Not killing any sockets for OOS because there are %d "
4861 "already moribund, and we only want to eliminate %d",
4862 moribund_socks, n_socks - target_n_socks);
4867 /** Log how many bytes are used by buffers of different kinds and sizes. */
4868 void
4869 connection_dump_buffer_mem_stats(int severity)
4871 uint64_t used_by_type[CONN_TYPE_MAX_+1];
4872 uint64_t alloc_by_type[CONN_TYPE_MAX_+1];
4873 int n_conns_by_type[CONN_TYPE_MAX_+1];
4874 uint64_t total_alloc = 0;
4875 uint64_t total_used = 0;
4876 int i;
4877 smartlist_t *conns = get_connection_array();
4879 memset(used_by_type, 0, sizeof(used_by_type));
4880 memset(alloc_by_type, 0, sizeof(alloc_by_type));
4881 memset(n_conns_by_type, 0, sizeof(n_conns_by_type));
4883 SMARTLIST_FOREACH_BEGIN(conns, connection_t *, c) {
4884 int tp = c->type;
4885 ++n_conns_by_type[tp];
4886 if (c->inbuf) {
4887 used_by_type[tp] += buf_datalen(c->inbuf);
4888 alloc_by_type[tp] += buf_allocation(c->inbuf);
4890 if (c->outbuf) {
4891 used_by_type[tp] += buf_datalen(c->outbuf);
4892 alloc_by_type[tp] += buf_allocation(c->outbuf);
4894 } SMARTLIST_FOREACH_END(c);
4895 for (i=0; i <= CONN_TYPE_MAX_; ++i) {
4896 total_used += used_by_type[i];
4897 total_alloc += alloc_by_type[i];
4900 tor_log(severity, LD_GENERAL,
4901 "In buffers for %d connections: "U64_FORMAT" used/"U64_FORMAT" allocated",
4902 smartlist_len(conns),
4903 U64_PRINTF_ARG(total_used), U64_PRINTF_ARG(total_alloc));
4904 for (i=CONN_TYPE_MIN_; i <= CONN_TYPE_MAX_; ++i) {
4905 if (!n_conns_by_type[i])
4906 continue;
4907 tor_log(severity, LD_GENERAL,
4908 " For %d %s connections: "U64_FORMAT" used/"U64_FORMAT" allocated",
4909 n_conns_by_type[i], conn_type_to_string(i),
4910 U64_PRINTF_ARG(used_by_type[i]), U64_PRINTF_ARG(alloc_by_type[i]));
4914 /** Verify that connection <b>conn</b> has all of its invariants
4915 * correct. Trigger an assert if anything is invalid.
4917 void
4918 assert_connection_ok(connection_t *conn, time_t now)
4920 (void) now; /* XXXX unused. */
4921 tor_assert(conn);
4922 tor_assert(conn->type >= CONN_TYPE_MIN_);
4923 tor_assert(conn->type <= CONN_TYPE_MAX_);
4925 switch (conn->type) {
4926 case CONN_TYPE_OR:
4927 case CONN_TYPE_EXT_OR:
4928 tor_assert(conn->magic == OR_CONNECTION_MAGIC);
4929 break;
4930 case CONN_TYPE_AP:
4931 tor_assert(conn->magic == ENTRY_CONNECTION_MAGIC);
4932 break;
4933 case CONN_TYPE_EXIT:
4934 tor_assert(conn->magic == EDGE_CONNECTION_MAGIC);
4935 break;
4936 case CONN_TYPE_DIR:
4937 tor_assert(conn->magic == DIR_CONNECTION_MAGIC);
4938 break;
4939 case CONN_TYPE_CONTROL:
4940 tor_assert(conn->magic == CONTROL_CONNECTION_MAGIC);
4941 break;
4942 CASE_ANY_LISTENER_TYPE:
4943 tor_assert(conn->magic == LISTENER_CONNECTION_MAGIC);
4944 break;
4945 default:
4946 tor_assert(conn->magic == BASE_CONNECTION_MAGIC);
4947 break;
4950 if (conn->linked_conn) {
4951 tor_assert(conn->linked_conn->linked_conn == conn);
4952 tor_assert(conn->linked);
4954 if (conn->linked)
4955 tor_assert(!SOCKET_OK(conn->s));
4957 if (conn->outbuf_flushlen > 0) {
4958 /* With optimistic data, we may have queued data in
4959 * EXIT_CONN_STATE_RESOLVING while the conn is not yet marked to writing.
4960 * */
4961 tor_assert((conn->type == CONN_TYPE_EXIT &&
4962 conn->state == EXIT_CONN_STATE_RESOLVING) ||
4963 connection_is_writing(conn) ||
4964 conn->write_blocked_on_bw ||
4965 (CONN_IS_EDGE(conn) &&
4966 TO_EDGE_CONN(conn)->edge_blocked_on_circ));
4969 if (conn->hold_open_until_flushed)
4970 tor_assert(conn->marked_for_close);
4972 /* XXXX check: read_blocked_on_bw, write_blocked_on_bw, s, conn_array_index,
4973 * marked_for_close. */
4975 /* buffers */
4976 if (conn->inbuf)
4977 assert_buf_ok(conn->inbuf);
4978 if (conn->outbuf)
4979 assert_buf_ok(conn->outbuf);
4981 if (conn->type == CONN_TYPE_OR) {
4982 or_connection_t *or_conn = TO_OR_CONN(conn);
4983 if (conn->state == OR_CONN_STATE_OPEN) {
4984 /* tor_assert(conn->bandwidth > 0); */
4985 /* the above isn't necessarily true: if we just did a TLS
4986 * handshake but we didn't recognize the other peer, or it
4987 * gave a bad cert/etc, then we won't have assigned bandwidth,
4988 * yet it will be open. -RD
4990 // tor_assert(conn->read_bucket >= 0);
4992 // tor_assert(conn->addr && conn->port);
4993 tor_assert(conn->address);
4994 if (conn->state > OR_CONN_STATE_PROXY_HANDSHAKING)
4995 tor_assert(or_conn->tls);
4998 if (CONN_IS_EDGE(conn)) {
4999 /* XXX unchecked: package window, deliver window. */
5000 if (conn->type == CONN_TYPE_AP) {
5001 entry_connection_t *entry_conn = TO_ENTRY_CONN(conn);
5002 if (entry_conn->chosen_exit_optional || entry_conn->chosen_exit_retries)
5003 tor_assert(entry_conn->chosen_exit_name);
5005 tor_assert(entry_conn->socks_request);
5006 if (conn->state == AP_CONN_STATE_OPEN) {
5007 tor_assert(entry_conn->socks_request->has_finished);
5008 if (!conn->marked_for_close) {
5009 tor_assert(ENTRY_TO_EDGE_CONN(entry_conn)->cpath_layer);
5010 assert_cpath_layer_ok(ENTRY_TO_EDGE_CONN(entry_conn)->cpath_layer);
5014 if (conn->type == CONN_TYPE_EXIT) {
5015 tor_assert(conn->purpose == EXIT_PURPOSE_CONNECT ||
5016 conn->purpose == EXIT_PURPOSE_RESOLVE);
5018 } else if (conn->type == CONN_TYPE_DIR) {
5019 } else {
5020 /* Purpose is only used for dir and exit types currently */
5021 tor_assert(!conn->purpose);
5024 switch (conn->type)
5026 CASE_ANY_LISTENER_TYPE:
5027 tor_assert(conn->state == LISTENER_STATE_READY);
5028 break;
5029 case CONN_TYPE_OR:
5030 tor_assert(conn->state >= OR_CONN_STATE_MIN_);
5031 tor_assert(conn->state <= OR_CONN_STATE_MAX_);
5032 break;
5033 case CONN_TYPE_EXT_OR:
5034 tor_assert(conn->state >= EXT_OR_CONN_STATE_MIN_);
5035 tor_assert(conn->state <= EXT_OR_CONN_STATE_MAX_);
5036 break;
5037 case CONN_TYPE_EXIT:
5038 tor_assert(conn->state >= EXIT_CONN_STATE_MIN_);
5039 tor_assert(conn->state <= EXIT_CONN_STATE_MAX_);
5040 tor_assert(conn->purpose >= EXIT_PURPOSE_MIN_);
5041 tor_assert(conn->purpose <= EXIT_PURPOSE_MAX_);
5042 break;
5043 case CONN_TYPE_AP:
5044 tor_assert(conn->state >= AP_CONN_STATE_MIN_);
5045 tor_assert(conn->state <= AP_CONN_STATE_MAX_);
5046 tor_assert(TO_ENTRY_CONN(conn)->socks_request);
5047 break;
5048 case CONN_TYPE_DIR:
5049 tor_assert(conn->state >= DIR_CONN_STATE_MIN_);
5050 tor_assert(conn->state <= DIR_CONN_STATE_MAX_);
5051 tor_assert(conn->purpose >= DIR_PURPOSE_MIN_);
5052 tor_assert(conn->purpose <= DIR_PURPOSE_MAX_);
5053 break;
5054 case CONN_TYPE_CONTROL:
5055 tor_assert(conn->state >= CONTROL_CONN_STATE_MIN_);
5056 tor_assert(conn->state <= CONTROL_CONN_STATE_MAX_);
5057 break;
5058 default:
5059 tor_assert(0);
5063 /** Fills <b>addr</b> and <b>port</b> with the details of the global
5064 * proxy server we are using.
5065 * <b>conn</b> contains the connection we are using the proxy for.
5067 * Return 0 on success, -1 on failure.
5070 get_proxy_addrport(tor_addr_t *addr, uint16_t *port, int *proxy_type,
5071 const connection_t *conn)
5073 const or_options_t *options = get_options();
5075 /* Client Transport Plugins can use another proxy, but that should be hidden
5076 * from the rest of tor (as the plugin is responsible for dealing with the
5077 * proxy), check it first, then check the rest of the proxy types to allow
5078 * the config to have unused ClientTransportPlugin entries.
5080 if (options->ClientTransportPlugin) {
5081 const transport_t *transport = NULL;
5082 int r;
5083 r = get_transport_by_bridge_addrport(&conn->addr, conn->port, &transport);
5084 if (r<0)
5085 return -1;
5086 if (transport) { /* transport found */
5087 tor_addr_copy(addr, &transport->addr);
5088 *port = transport->port;
5089 *proxy_type = transport->socks_version;
5090 return 0;
5093 /* Unused ClientTransportPlugin. */
5096 if (options->HTTPSProxy) {
5097 tor_addr_copy(addr, &options->HTTPSProxyAddr);
5098 *port = options->HTTPSProxyPort;
5099 *proxy_type = PROXY_CONNECT;
5100 return 0;
5101 } else if (options->Socks4Proxy) {
5102 tor_addr_copy(addr, &options->Socks4ProxyAddr);
5103 *port = options->Socks4ProxyPort;
5104 *proxy_type = PROXY_SOCKS4;
5105 return 0;
5106 } else if (options->Socks5Proxy) {
5107 tor_addr_copy(addr, &options->Socks5ProxyAddr);
5108 *port = options->Socks5ProxyPort;
5109 *proxy_type = PROXY_SOCKS5;
5110 return 0;
5113 tor_addr_make_unspec(addr);
5114 *port = 0;
5115 *proxy_type = PROXY_NONE;
5116 return 0;
5119 /** Log a failed connection to a proxy server.
5120 * <b>conn</b> is the connection we use the proxy server for. */
5121 void
5122 log_failed_proxy_connection(connection_t *conn)
5124 tor_addr_t proxy_addr;
5125 uint16_t proxy_port;
5126 int proxy_type;
5128 if (get_proxy_addrport(&proxy_addr, &proxy_port, &proxy_type, conn) != 0)
5129 return; /* if we have no proxy set up, leave this function. */
5131 log_warn(LD_NET,
5132 "The connection to the %s proxy server at %s just failed. "
5133 "Make sure that the proxy server is up and running.",
5134 proxy_type_to_string(proxy_type),
5135 fmt_addrport(&proxy_addr, proxy_port));
5138 /** Return string representation of <b>proxy_type</b>. */
5139 static const char *
5140 proxy_type_to_string(int proxy_type)
5142 switch (proxy_type) {
5143 case PROXY_CONNECT: return "HTTP";
5144 case PROXY_SOCKS4: return "SOCKS4";
5145 case PROXY_SOCKS5: return "SOCKS5";
5146 case PROXY_PLUGGABLE: return "pluggable transports SOCKS";
5147 case PROXY_NONE: return "NULL";
5148 default: tor_assert(0);
5150 return NULL; /*Unreached*/
5153 /** Call connection_free_() on every connection in our array, and release all
5154 * storage held by connection.c.
5156 * Don't do the checks in connection_free(), because they will
5157 * fail.
5159 void
5160 connection_free_all(void)
5162 smartlist_t *conns = get_connection_array();
5164 /* We don't want to log any messages to controllers. */
5165 SMARTLIST_FOREACH(conns, connection_t *, conn,
5166 if (conn->type == CONN_TYPE_CONTROL)
5167 TO_CONTROL_CONN(conn)->event_mask = 0);
5169 control_update_global_event_mask();
5171 /* Unlink everything from the identity map. */
5172 connection_or_clear_identity_map();
5173 connection_or_clear_ext_or_id_map();
5175 /* Clear out our list of broken connections */
5176 clear_broken_connection_map(0);
5178 SMARTLIST_FOREACH(conns, connection_t *, conn, connection_free_(conn));
5180 if (outgoing_addrs) {
5181 SMARTLIST_FOREACH(outgoing_addrs, tor_addr_t *, addr, tor_free(addr));
5182 smartlist_free(outgoing_addrs);
5183 outgoing_addrs = NULL;
5186 tor_free(last_interface_ipv4);
5187 tor_free(last_interface_ipv6);
5190 /** Log a warning, and possibly emit a control event, that <b>received</b> came
5191 * at a skewed time. <b>trusted</b> indicates that the <b>source</b> was one
5192 * that we had more faith in and therefore the warning level should have higher
5193 * severity.
5195 void
5196 clock_skew_warning(const connection_t *conn, long apparent_skew, int trusted,
5197 log_domain_mask_t domain, const char *received,
5198 const char *source)
5200 char dbuf[64];
5201 char *ext_source = NULL;
5202 format_time_interval(dbuf, sizeof(dbuf), apparent_skew);
5203 if (conn)
5204 tor_asprintf(&ext_source, "%s:%s:%d", source, conn->address, conn->port);
5205 else
5206 ext_source = tor_strdup(source);
5207 log_fn(trusted ? LOG_WARN : LOG_INFO, domain,
5208 "Received %s with skewed time (%s): "
5209 "It seems that our clock is %s by %s, or that theirs is %s%s. "
5210 "Tor requires an accurate clock to work: please check your time, "
5211 "timezone, and date settings.", received, ext_source,
5212 apparent_skew > 0 ? "ahead" : "behind", dbuf,
5213 apparent_skew > 0 ? "behind" : "ahead",
5214 (!conn || trusted) ? "" : ", or they are sending us the wrong time");
5215 if (trusted)
5216 control_event_general_status(LOG_WARN, "CLOCK_SKEW SKEW=%ld SOURCE=%s",
5217 apparent_skew, ext_source);
5218 tor_free(ext_source);