Trigger OOS on bind failures (fixes #40597)
[tor.git] / src / core / mainloop / connection.c
blob75fc3c891835c04b2b06c4f0211c16bb3c047799
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-2021, 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_buf_add(). 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 "core/or/or.h"
59 #include "feature/client/bridges.h"
60 #include "lib/buf/buffers.h"
61 #include "lib/tls/buffers_tls.h"
62 #include "lib/err/backtrace.h"
65 * Define this so we get channel internal functions, since we're implementing
66 * part of a subclass (channel_tls_t).
68 #define CHANNEL_OBJECT_PRIVATE
69 #include "app/config/config.h"
70 #include "app/config/resolve_addr.h"
71 #include "core/mainloop/connection.h"
72 #include "core/mainloop/mainloop.h"
73 #include "core/mainloop/netstatus.h"
74 #include "core/or/channel.h"
75 #include "core/or/channeltls.h"
76 #include "core/or/circuitbuild.h"
77 #include "core/or/circuitlist.h"
78 #include "core/or/circuituse.h"
79 #include "core/or/connection_edge.h"
80 #include "core/or/connection_or.h"
81 #include "core/or/dos.h"
82 #include "core/or/policies.h"
83 #include "core/or/reasons.h"
84 #include "core/or/relay.h"
85 #include "core/or/status.h"
86 #include "core/or/crypt_path.h"
87 #include "core/proto/proto_haproxy.h"
88 #include "core/proto/proto_http.h"
89 #include "core/proto/proto_socks.h"
90 #include "feature/client/dnsserv.h"
91 #include "feature/client/entrynodes.h"
92 #include "feature/client/transports.h"
93 #include "feature/control/control.h"
94 #include "feature/control/control_events.h"
95 #include "feature/dirauth/authmode.h"
96 #include "feature/dirauth/dirauth_config.h"
97 #include "feature/dircache/dirserv.h"
98 #include "feature/dircommon/directory.h"
99 #include "feature/hibernate/hibernate.h"
100 #include "feature/hs/hs_common.h"
101 #include "feature/hs/hs_ident.h"
102 #include "feature/hs/hs_metrics.h"
103 #include "feature/metrics/metrics.h"
104 #include "feature/nodelist/nodelist.h"
105 #include "feature/nodelist/routerlist.h"
106 #include "feature/relay/dns.h"
107 #include "feature/relay/ext_orport.h"
108 #include "feature/relay/routermode.h"
109 #include "feature/rend/rendcommon.h"
110 #include "feature/stats/connstats.h"
111 #include "feature/stats/rephist.h"
112 #include "feature/stats/bwhist.h"
113 #include "lib/crypt_ops/crypto_util.h"
114 #include "lib/crypt_ops/crypto_format.h"
115 #include "lib/geoip/geoip.h"
117 #include "lib/cc/ctassert.h"
118 #include "lib/sandbox/sandbox.h"
119 #include "lib/net/buffers_net.h"
120 #include "lib/net/address.h"
121 #include "lib/tls/tortls.h"
122 #include "lib/evloop/compat_libevent.h"
123 #include "lib/compress/compress.h"
125 #ifdef HAVE_PWD_H
126 #include <pwd.h>
127 #endif
129 #ifdef HAVE_UNISTD_H
130 #include <unistd.h>
131 #endif
132 #ifdef HAVE_SYS_STAT_H
133 #include <sys/stat.h>
134 #endif
136 #ifdef HAVE_SYS_UN_H
137 #include <sys/socket.h>
138 #include <sys/un.h>
139 #endif
141 #include "feature/dircommon/dir_connection_st.h"
142 #include "feature/control/control_connection_st.h"
143 #include "core/or/entry_connection_st.h"
144 #include "core/or/listener_connection_st.h"
145 #include "core/or/or_connection_st.h"
146 #include "core/or/port_cfg_st.h"
147 #include "feature/nodelist/routerinfo_st.h"
148 #include "core/or/socks_request_st.h"
150 #include "core/or/congestion_control_flow.h"
153 * On Windows and Linux we cannot reliably bind() a socket to an
154 * address and port if: 1) There's already a socket bound to wildcard
155 * address (0.0.0.0 or ::) with the same port; 2) We try to bind()
156 * to wildcard address and there's another socket bound to a
157 * specific address and the same port.
159 * To address this problem on these two platforms we implement a
160 * routine that:
161 * 1) Checks if first attempt to bind() a new socket failed with
162 * EADDRINUSE.
163 * 2) If so, it will close the appropriate old listener connection and
164 * 3) Attempts bind()'ing the new listener socket again.
166 * Just to be safe, we are enabling listener rebind code on all platforms,
167 * to account for unexpected cases where it may be needed.
169 #define ENABLE_LISTENER_REBIND
171 static connection_t *connection_listener_new(
172 const struct sockaddr *listensockaddr,
173 socklen_t listensocklen, int type,
174 const char *address,
175 const port_cfg_t *portcfg,
176 int *addr_in_use);
177 static connection_t *connection_listener_new_for_port(
178 const port_cfg_t *port,
179 int *defer, int *addr_in_use);
180 static void connection_init(time_t now, connection_t *conn, int type,
181 int socket_family);
182 static int connection_handle_listener_read(connection_t *conn, int new_type);
183 static int connection_finished_flushing(connection_t *conn);
184 static int connection_flushed_some(connection_t *conn);
185 static int connection_finished_connecting(connection_t *conn);
186 static int connection_reached_eof(connection_t *conn);
187 static int connection_buf_read_from_socket(connection_t *conn,
188 ssize_t *max_to_read,
189 int *socket_error);
190 static int connection_process_inbuf(connection_t *conn, int package_partial);
191 static void client_check_address_changed(tor_socket_t sock);
192 static void set_constrained_socket_buffers(tor_socket_t sock, int size);
194 static const char *connection_proxy_state_to_string(int state);
195 static int connection_read_https_proxy_response(connection_t *conn);
196 static void connection_send_socks5_connect(connection_t *conn);
197 static const char *proxy_type_to_string(int proxy_type);
198 static int conn_get_proxy_type(const connection_t *conn);
199 const tor_addr_t *conn_get_outbound_address(sa_family_t family,
200 const or_options_t *options, unsigned int conn_type);
201 static void reenable_blocked_connection_init(const or_options_t *options);
202 static void reenable_blocked_connection_schedule(void);
204 /** The last addresses that our network interface seemed to have been
205 * binding to. We use this as one way to detect when our IP changes.
207 * XXXX+ We should really use the entire list of interfaces here.
209 static tor_addr_t *last_interface_ipv4 = NULL;
210 /* DOCDOC last_interface_ipv6 */
211 static tor_addr_t *last_interface_ipv6 = NULL;
212 /** A list of tor_addr_t for addresses we've used in outgoing connections.
213 * Used to detect IP address changes. */
214 static smartlist_t *outgoing_addrs = NULL;
216 #define CASE_ANY_LISTENER_TYPE \
217 case CONN_TYPE_OR_LISTENER: \
218 case CONN_TYPE_EXT_OR_LISTENER: \
219 case CONN_TYPE_AP_LISTENER: \
220 case CONN_TYPE_DIR_LISTENER: \
221 case CONN_TYPE_CONTROL_LISTENER: \
222 case CONN_TYPE_AP_TRANS_LISTENER: \
223 case CONN_TYPE_AP_NATD_LISTENER: \
224 case CONN_TYPE_AP_DNS_LISTENER: \
225 case CONN_TYPE_AP_HTTP_CONNECT_LISTENER: \
226 case CONN_TYPE_METRICS_LISTENER
228 /**************************************************************/
231 * Cast a `connection_t *` to a `listener_connection_t *`.
233 * Exit with an assertion failure if the input is not a
234 * `listener_connection_t`.
236 listener_connection_t *
237 TO_LISTENER_CONN(connection_t *c)
239 tor_assert(c->magic == LISTENER_CONNECTION_MAGIC);
240 return DOWNCAST(listener_connection_t, c);
244 * Cast a `const connection_t *` to a `const listener_connection_t *`.
246 * Exit with an assertion failure if the input is not a
247 * `listener_connection_t`.
249 const listener_connection_t *
250 CONST_TO_LISTENER_CONN(const connection_t *c)
252 return TO_LISTENER_CONN((connection_t *)c);
255 size_t
256 connection_get_inbuf_len(const connection_t *conn)
258 return conn->inbuf ? buf_datalen(conn->inbuf) : 0;
261 size_t
262 connection_get_outbuf_len(const connection_t *conn)
264 return conn->outbuf ? buf_datalen(conn->outbuf) : 0;
268 * Return the human-readable name for the connection type <b>type</b>
270 const char *
271 conn_type_to_string(int type)
273 static char buf[64];
274 switch (type) {
275 case CONN_TYPE_OR_LISTENER: return "OR listener";
276 case CONN_TYPE_OR: return "OR";
277 case CONN_TYPE_EXIT: return "Exit";
278 case CONN_TYPE_AP_LISTENER: return "Socks listener";
279 case CONN_TYPE_AP_TRANS_LISTENER:
280 return "Transparent pf/netfilter listener";
281 case CONN_TYPE_AP_NATD_LISTENER: return "Transparent natd listener";
282 case CONN_TYPE_AP_DNS_LISTENER: return "DNS listener";
283 case CONN_TYPE_AP: return "Socks";
284 case CONN_TYPE_DIR_LISTENER: return "Directory listener";
285 case CONN_TYPE_DIR: return "Directory";
286 case CONN_TYPE_CONTROL_LISTENER: return "Control listener";
287 case CONN_TYPE_CONTROL: return "Control";
288 case CONN_TYPE_EXT_OR: return "Extended OR";
289 case CONN_TYPE_EXT_OR_LISTENER: return "Extended OR listener";
290 case CONN_TYPE_AP_HTTP_CONNECT_LISTENER: return "HTTP tunnel listener";
291 case CONN_TYPE_METRICS_LISTENER: return "Metrics listener";
292 case CONN_TYPE_METRICS: return "Metrics";
293 default:
294 log_warn(LD_BUG, "unknown connection type %d", type);
295 tor_snprintf(buf, sizeof(buf), "unknown [%d]", type);
296 return buf;
301 * Return the human-readable name for the connection state <b>state</b>
302 * for the connection type <b>type</b>
304 const char *
305 conn_state_to_string(int type, int state)
307 static char buf[96];
308 switch (type) {
309 CASE_ANY_LISTENER_TYPE:
310 if (state == LISTENER_STATE_READY)
311 return "ready";
312 break;
313 case CONN_TYPE_OR:
314 switch (state) {
315 case OR_CONN_STATE_CONNECTING: return "connect()ing";
316 case OR_CONN_STATE_PROXY_HANDSHAKING: return "handshaking (proxy)";
317 case OR_CONN_STATE_TLS_HANDSHAKING: return "handshaking (TLS)";
318 case OR_CONN_STATE_TLS_CLIENT_RENEGOTIATING:
319 return "renegotiating (TLS, v2 handshake)";
320 case OR_CONN_STATE_TLS_SERVER_RENEGOTIATING:
321 return "waiting for renegotiation or V3 handshake";
322 case OR_CONN_STATE_OR_HANDSHAKING_V2:
323 return "handshaking (Tor, v2 handshake)";
324 case OR_CONN_STATE_OR_HANDSHAKING_V3:
325 return "handshaking (Tor, v3 handshake)";
326 case OR_CONN_STATE_OPEN: return "open";
328 break;
329 case CONN_TYPE_EXT_OR:
330 switch (state) {
331 case EXT_OR_CONN_STATE_AUTH_WAIT_AUTH_TYPE:
332 return "waiting for authentication type";
333 case EXT_OR_CONN_STATE_AUTH_WAIT_CLIENT_NONCE:
334 return "waiting for client nonce";
335 case EXT_OR_CONN_STATE_AUTH_WAIT_CLIENT_HASH:
336 return "waiting for client hash";
337 case EXT_OR_CONN_STATE_OPEN: return "open";
338 case EXT_OR_CONN_STATE_FLUSHING: return "flushing final OKAY";
340 break;
341 case CONN_TYPE_EXIT:
342 switch (state) {
343 case EXIT_CONN_STATE_RESOLVING: return "waiting for dest info";
344 case EXIT_CONN_STATE_CONNECTING: return "connecting";
345 case EXIT_CONN_STATE_OPEN: return "open";
346 case EXIT_CONN_STATE_RESOLVEFAILED: return "resolve failed";
348 break;
349 case CONN_TYPE_AP:
350 switch (state) {
351 case AP_CONN_STATE_SOCKS_WAIT: return "waiting for socks info";
352 case AP_CONN_STATE_NATD_WAIT: return "waiting for natd dest info";
353 case AP_CONN_STATE_RENDDESC_WAIT: return "waiting for rendezvous desc";
354 case AP_CONN_STATE_CONTROLLER_WAIT: return "waiting for controller";
355 case AP_CONN_STATE_CIRCUIT_WAIT: return "waiting for circuit";
356 case AP_CONN_STATE_CONNECT_WAIT: return "waiting for connect response";
357 case AP_CONN_STATE_RESOLVE_WAIT: return "waiting for resolve response";
358 case AP_CONN_STATE_OPEN: return "open";
360 break;
361 case CONN_TYPE_DIR:
362 switch (state) {
363 case DIR_CONN_STATE_CONNECTING: return "connecting";
364 case DIR_CONN_STATE_CLIENT_SENDING: return "client sending";
365 case DIR_CONN_STATE_CLIENT_READING: return "client reading";
366 case DIR_CONN_STATE_CLIENT_FINISHED: return "client finished";
367 case DIR_CONN_STATE_SERVER_COMMAND_WAIT: return "waiting for command";
368 case DIR_CONN_STATE_SERVER_WRITING: return "writing";
370 break;
371 case CONN_TYPE_CONTROL:
372 switch (state) {
373 case CONTROL_CONN_STATE_OPEN: return "open (protocol v1)";
374 case CONTROL_CONN_STATE_NEEDAUTH:
375 return "waiting for authentication (protocol v1)";
377 break;
380 if (state == 0) {
381 return "uninitialized";
384 log_warn(LD_BUG, "unknown connection state %d (type %d)", state, type);
385 tor_snprintf(buf, sizeof(buf),
386 "unknown state [%d] on unknown [%s] connection",
387 state, conn_type_to_string(type));
388 tor_assert_nonfatal_unreached_once();
389 return buf;
393 * Helper: describe the peer or address of connection @a conn in a
394 * human-readable manner.
396 * Returns a pointer to a static buffer; future calls to
397 * connection_describe_peer_internal() will invalidate this buffer.
399 * If <b>include_preposition</b> is true, include a preposition before the
400 * peer address.
402 * Nobody should parse the output of this function; it can and will change in
403 * future versions of tor.
405 static const char *
406 connection_describe_peer_internal(const connection_t *conn,
407 bool include_preposition)
409 IF_BUG_ONCE(!conn) {
410 return "null peer";
413 static char peer_buf[256];
414 const tor_addr_t *addr = &conn->addr;
415 const char *address = NULL;
416 const char *prep;
417 bool scrub = false;
418 char extra_buf[128];
419 extra_buf[0] = 0;
421 /* First, figure out the preposition to use */
422 switch (conn->type) {
423 CASE_ANY_LISTENER_TYPE:
424 prep = "on";
425 break;
426 case CONN_TYPE_EXIT:
427 prep = "to";
428 break;
429 case CONN_TYPE_CONTROL:
430 case CONN_TYPE_AP:
431 case CONN_TYPE_EXT_OR:
432 prep = "from";
433 break;
434 default:
435 prep = "with";
436 break;
439 /* Now figure out the address. */
440 if (conn->socket_family == AF_UNIX) {
441 /* For unix sockets, we always use the `address` string. */
442 address = conn->address ? conn->address : "unix socket";
443 } else if (conn->type == CONN_TYPE_OR) {
444 /* For OR connections, we have a lot to do. */
445 const or_connection_t *or_conn = CONST_TO_OR_CONN(conn);
446 /* We report the IDs we're talking to... */
447 if (fast_digest_is_zero(or_conn->identity_digest)) {
448 // This could be a client, so scrub it. No identity to report.
449 scrub = true;
450 } else {
451 const ed25519_public_key_t *ed_id =
452 connection_or_get_alleged_ed25519_id(or_conn);
453 char ed_id_buf[ED25519_BASE64_LEN+1];
454 char rsa_id_buf[HEX_DIGEST_LEN+1];
455 if (ed_id) {
456 ed25519_public_to_base64(ed_id_buf, ed_id);
457 } else {
458 strlcpy(ed_id_buf, "<none>", sizeof(ed_id_buf));
460 base16_encode(rsa_id_buf, sizeof(rsa_id_buf),
461 or_conn->identity_digest, DIGEST_LEN);
462 tor_snprintf(extra_buf, sizeof(extra_buf),
463 " ID=%s RSA_ID=%s", ed_id_buf, rsa_id_buf);
465 if (! scrub && (! tor_addr_eq(addr, &or_conn->canonical_orport.addr) ||
466 conn->port != or_conn->canonical_orport.port)) {
467 /* We report canonical address, if it's different */
468 char canonical_addr_buf[TOR_ADDR_BUF_LEN];
469 if (tor_addr_to_str(canonical_addr_buf, &or_conn->canonical_orport.addr,
470 sizeof(canonical_addr_buf), 1)) {
471 tor_snprintf(extra_buf+strlen(extra_buf),
472 sizeof(extra_buf)-strlen(extra_buf),
473 " canonical_addr=%s:%"PRIu16,
474 canonical_addr_buf,
475 or_conn->canonical_orport.port);
478 } else if (conn->type == CONN_TYPE_EXIT) {
479 scrub = true; /* This is a client's request; scrub it with SafeLogging. */
480 if (tor_addr_is_null(addr)) {
481 address = conn->address;
482 strlcpy(extra_buf, " (DNS lookup pending)", sizeof(extra_buf));
486 char addr_buf[TOR_ADDR_BUF_LEN];
487 if (address == NULL) {
488 if (tor_addr_family(addr) == 0) {
489 address = "<unset>";
490 } else {
491 address = tor_addr_to_str(addr_buf, addr, sizeof(addr_buf), 1);
492 if (!address) {
493 address = "<can't format!>";
494 tor_assert_nonfatal_unreached_once();
499 char portbuf[7];
500 portbuf[0]=0;
501 if (scrub && get_options()->SafeLogging_ != SAFELOG_SCRUB_NONE) {
502 address = "[scrubbed]";
503 } else {
504 /* Only set the port if we're not scrubbing the address. */
505 if (conn->port != 0) {
506 tor_snprintf(portbuf, sizeof(portbuf), ":%d", conn->port);
510 const char *sp = include_preposition ? " " : "";
511 if (! include_preposition)
512 prep = "";
514 tor_snprintf(peer_buf, sizeof(peer_buf),
515 "%s%s%s%s%s", prep, sp, address, portbuf, extra_buf);
516 return peer_buf;
520 * Describe the peer or address of connection @a conn in a
521 * human-readable manner.
523 * Returns a pointer to a static buffer; future calls to
524 * connection_describe_peer() or connection_describe() will invalidate this
525 * buffer.
527 * Nobody should parse the output of this function; it can and will change in
528 * future versions of tor.
530 const char *
531 connection_describe_peer(const connection_t *conn)
533 return connection_describe_peer_internal(conn, false);
537 * Describe a connection for logging purposes.
539 * Returns a pointer to a static buffer; future calls to connection_describe()
540 * will invalidate this buffer.
542 * Nobody should parse the output of this function; it can and will change in
543 * future versions of tor.
545 const char *
546 connection_describe(const connection_t *conn)
548 IF_BUG_ONCE(!conn) {
549 return "null connection";
551 static char desc_buf[256];
552 const char *peer = connection_describe_peer_internal(conn, true);
553 tor_snprintf(desc_buf, sizeof(desc_buf),
554 "%s connection (%s) %s",
555 conn_type_to_string(conn->type),
556 conn_state_to_string(conn->type, conn->state),
557 peer);
558 return desc_buf;
561 /** Allocate and return a new dir_connection_t, initialized as by
562 * connection_init(). */
563 dir_connection_t *
564 dir_connection_new(int socket_family)
566 dir_connection_t *dir_conn = tor_malloc_zero(sizeof(dir_connection_t));
567 connection_init(time(NULL), TO_CONN(dir_conn), CONN_TYPE_DIR, socket_family);
568 return dir_conn;
571 /** Allocate and return a new or_connection_t, initialized as by
572 * connection_init().
574 * Initialize active_circuit_pqueue.
576 * Set active_circuit_pqueue_last_recalibrated to current cell_ewma tick.
578 or_connection_t *
579 or_connection_new(int type, int socket_family)
581 or_connection_t *or_conn = tor_malloc_zero(sizeof(or_connection_t));
582 time_t now = time(NULL);
583 tor_assert(type == CONN_TYPE_OR || type == CONN_TYPE_EXT_OR);
584 connection_init(now, TO_CONN(or_conn), type, socket_family);
586 tor_addr_make_unspec(&or_conn->canonical_orport.addr);
587 connection_or_set_canonical(or_conn, 0);
589 if (type == CONN_TYPE_EXT_OR) {
590 /* If we aren't told an address for this connection, we should
591 * presume it isn't local, and should be rate-limited. */
592 TO_CONN(or_conn)->always_rate_limit_as_remote = 1;
593 connection_or_set_ext_or_identifier(or_conn);
596 return or_conn;
599 /** Allocate and return a new entry_connection_t, initialized as by
600 * connection_init().
602 * Allocate space to store the socks_request.
604 entry_connection_t *
605 entry_connection_new(int type, int socket_family)
607 entry_connection_t *entry_conn = tor_malloc_zero(sizeof(entry_connection_t));
608 tor_assert(type == CONN_TYPE_AP);
609 connection_init(time(NULL), ENTRY_TO_CONN(entry_conn), type, socket_family);
610 entry_conn->socks_request = socks_request_new();
611 /* If this is coming from a listener, we'll set it up based on the listener
612 * in a little while. Otherwise, we're doing this as a linked connection
613 * of some kind, and we should set it up here based on the socket family */
614 if (socket_family == AF_INET)
615 entry_conn->entry_cfg.ipv4_traffic = 1;
616 else if (socket_family == AF_INET6)
617 entry_conn->entry_cfg.ipv6_traffic = 1;
619 /* Initialize the read token bucket to the maximum value which is the same as
620 * no rate limiting. */
621 token_bucket_rw_init(&ENTRY_TO_EDGE_CONN(entry_conn)->bucket, INT32_MAX,
622 INT32_MAX, monotime_coarse_get_stamp());
623 return entry_conn;
626 /** Allocate and return a new edge_connection_t, initialized as by
627 * connection_init(). */
628 edge_connection_t *
629 edge_connection_new(int type, int socket_family)
631 edge_connection_t *edge_conn = tor_malloc_zero(sizeof(edge_connection_t));
632 tor_assert(type == CONN_TYPE_EXIT);
633 connection_init(time(NULL), TO_CONN(edge_conn), type, socket_family);
634 /* Initialize the read token bucket to the maximum value which is the same as
635 * no rate limiting. */
636 token_bucket_rw_init(&edge_conn->bucket, INT32_MAX, INT32_MAX,
637 monotime_coarse_get_stamp());
638 return edge_conn;
641 /** Allocate and return a new control_connection_t, initialized as by
642 * connection_init(). */
643 control_connection_t *
644 control_connection_new(int socket_family)
646 control_connection_t *control_conn =
647 tor_malloc_zero(sizeof(control_connection_t));
648 connection_init(time(NULL),
649 TO_CONN(control_conn), CONN_TYPE_CONTROL, socket_family);
650 return control_conn;
653 /** Allocate and return a new listener_connection_t, initialized as by
654 * connection_init(). */
655 listener_connection_t *
656 listener_connection_new(int type, int socket_family)
658 listener_connection_t *listener_conn =
659 tor_malloc_zero(sizeof(listener_connection_t));
660 connection_init(time(NULL), TO_CONN(listener_conn), type, socket_family);
661 return listener_conn;
664 /** Allocate, initialize, and return a new connection_t subtype of <b>type</b>
665 * to make or receive connections of address family <b>socket_family</b>. The
666 * type should be one of the CONN_TYPE_* constants. */
667 connection_t *
668 connection_new(int type, int socket_family)
670 switch (type) {
671 case CONN_TYPE_OR:
672 case CONN_TYPE_EXT_OR:
673 return TO_CONN(or_connection_new(type, socket_family));
675 case CONN_TYPE_EXIT:
676 return TO_CONN(edge_connection_new(type, socket_family));
678 case CONN_TYPE_AP:
679 return ENTRY_TO_CONN(entry_connection_new(type, socket_family));
681 case CONN_TYPE_DIR:
682 return TO_CONN(dir_connection_new(socket_family));
684 case CONN_TYPE_CONTROL:
685 return TO_CONN(control_connection_new(socket_family));
687 CASE_ANY_LISTENER_TYPE:
688 return TO_CONN(listener_connection_new(type, socket_family));
690 default: {
691 connection_t *conn = tor_malloc_zero(sizeof(connection_t));
692 connection_init(time(NULL), conn, type, socket_family);
693 return conn;
698 /** Initializes conn. (you must call connection_add() to link it into the main
699 * array).
701 * Set conn-\>magic to the correct value.
703 * Set conn-\>type to <b>type</b>. Set conn-\>s and conn-\>conn_array_index to
704 * -1 to signify they are not yet assigned.
706 * Initialize conn's timestamps to now.
708 static void
709 connection_init(time_t now, connection_t *conn, int type, int socket_family)
711 static uint64_t n_connections_allocated = 1;
713 switch (type) {
714 case CONN_TYPE_OR:
715 case CONN_TYPE_EXT_OR:
716 conn->magic = OR_CONNECTION_MAGIC;
717 break;
718 case CONN_TYPE_EXIT:
719 conn->magic = EDGE_CONNECTION_MAGIC;
720 break;
721 case CONN_TYPE_AP:
722 conn->magic = ENTRY_CONNECTION_MAGIC;
723 break;
724 case CONN_TYPE_DIR:
725 conn->magic = DIR_CONNECTION_MAGIC;
726 break;
727 case CONN_TYPE_CONTROL:
728 conn->magic = CONTROL_CONNECTION_MAGIC;
729 break;
730 CASE_ANY_LISTENER_TYPE:
731 conn->magic = LISTENER_CONNECTION_MAGIC;
732 break;
733 default:
734 conn->magic = BASE_CONNECTION_MAGIC;
735 break;
738 conn->s = TOR_INVALID_SOCKET; /* give it a default of 'not used' */
739 conn->conn_array_index = -1; /* also default to 'not used' */
740 conn->global_identifier = n_connections_allocated++;
742 conn->type = type;
743 conn->socket_family = socket_family;
744 if (!connection_is_listener(conn)) {
745 /* listeners never use their buf */
746 conn->inbuf = buf_new();
747 conn->outbuf = buf_new();
750 conn->timestamp_created = now;
751 conn->timestamp_last_read_allowed = now;
752 conn->timestamp_last_write_allowed = now;
755 /** Create a link between <b>conn_a</b> and <b>conn_b</b>. */
756 void
757 connection_link_connections(connection_t *conn_a, connection_t *conn_b)
759 tor_assert(! SOCKET_OK(conn_a->s));
760 tor_assert(! SOCKET_OK(conn_b->s));
762 conn_a->linked = 1;
763 conn_b->linked = 1;
764 conn_a->linked_conn = conn_b;
765 conn_b->linked_conn = conn_a;
768 /** Return true iff the provided connection listener type supports AF_UNIX
769 * sockets. */
771 conn_listener_type_supports_af_unix(int type)
773 /* For now only control ports or SOCKS ports can be Unix domain sockets
774 * and listeners at the same time */
775 switch (type) {
776 case CONN_TYPE_CONTROL_LISTENER:
777 case CONN_TYPE_AP_LISTENER:
778 return 1;
779 default:
780 return 0;
784 /** Deallocate memory used by <b>conn</b>. Deallocate its buffers if
785 * necessary, close its socket if necessary, and mark the directory as dirty
786 * if <b>conn</b> is an OR or OP connection.
788 STATIC void
789 connection_free_minimal(connection_t *conn)
791 void *mem;
792 size_t memlen;
793 if (!conn)
794 return;
796 switch (conn->type) {
797 case CONN_TYPE_OR:
798 case CONN_TYPE_EXT_OR:
799 tor_assert(conn->magic == OR_CONNECTION_MAGIC);
800 mem = TO_OR_CONN(conn);
801 memlen = sizeof(or_connection_t);
802 break;
803 case CONN_TYPE_AP:
804 tor_assert(conn->magic == ENTRY_CONNECTION_MAGIC);
805 mem = TO_ENTRY_CONN(conn);
806 memlen = sizeof(entry_connection_t);
807 break;
808 case CONN_TYPE_EXIT:
809 tor_assert(conn->magic == EDGE_CONNECTION_MAGIC);
810 mem = TO_EDGE_CONN(conn);
811 memlen = sizeof(edge_connection_t);
812 break;
813 case CONN_TYPE_DIR:
814 tor_assert(conn->magic == DIR_CONNECTION_MAGIC);
815 mem = TO_DIR_CONN(conn);
816 memlen = sizeof(dir_connection_t);
817 break;
818 case CONN_TYPE_CONTROL:
819 tor_assert(conn->magic == CONTROL_CONNECTION_MAGIC);
820 mem = TO_CONTROL_CONN(conn);
821 memlen = sizeof(control_connection_t);
822 break;
823 CASE_ANY_LISTENER_TYPE:
824 tor_assert(conn->magic == LISTENER_CONNECTION_MAGIC);
825 mem = TO_LISTENER_CONN(conn);
826 memlen = sizeof(listener_connection_t);
827 break;
828 default:
829 tor_assert(conn->magic == BASE_CONNECTION_MAGIC);
830 mem = conn;
831 memlen = sizeof(connection_t);
832 break;
835 if (conn->linked) {
836 log_info(LD_GENERAL, "Freeing linked %s connection [%s] with %d "
837 "bytes on inbuf, %d on outbuf.",
838 conn_type_to_string(conn->type),
839 conn_state_to_string(conn->type, conn->state),
840 (int)connection_get_inbuf_len(conn),
841 (int)connection_get_outbuf_len(conn));
844 if (!connection_is_listener(conn)) {
845 buf_free(conn->inbuf);
846 buf_free(conn->outbuf);
847 } else {
848 if (conn->socket_family == AF_UNIX) {
849 /* For now only control and SOCKS ports can be Unix domain sockets
850 * and listeners at the same time */
851 tor_assert(conn_listener_type_supports_af_unix(conn->type));
853 if (unlink(conn->address) < 0 && errno != ENOENT) {
854 log_warn(LD_NET, "Could not unlink %s: %s", conn->address,
855 strerror(errno));
860 tor_str_wipe_and_free(conn->address);
862 if (connection_speaks_cells(conn)) {
863 or_connection_t *or_conn = TO_OR_CONN(conn);
864 if (or_conn->tls) {
865 if (! SOCKET_OK(conn->s)) {
866 /* The socket has been closed by somebody else; we must tell the
867 * TLS object not to close it. */
868 tor_tls_release_socket(or_conn->tls);
869 } else {
870 /* The tor_tls_free() call below will close the socket; we must tell
871 * the code below not to close it a second time. */
872 tor_release_socket_ownership(conn->s);
873 conn->s = TOR_INVALID_SOCKET;
875 tor_tls_free(or_conn->tls);
876 or_conn->tls = NULL;
878 or_handshake_state_free(or_conn->handshake_state);
879 or_conn->handshake_state = NULL;
880 tor_str_wipe_and_free(or_conn->nickname);
881 if (or_conn->chan) {
882 /* Owww, this shouldn't happen, but... */
883 channel_t *base_chan = TLS_CHAN_TO_BASE(or_conn->chan);
884 tor_assert(base_chan);
885 log_info(LD_CHANNEL,
886 "Freeing orconn at %p, saw channel %p with ID "
887 "%"PRIu64 " left un-NULLed",
888 or_conn, base_chan,
889 base_chan->global_identifier);
890 if (!CHANNEL_FINISHED(base_chan)) {
891 channel_close_for_error(base_chan);
894 or_conn->chan->conn = NULL;
895 or_conn->chan = NULL;
898 if (conn->type == CONN_TYPE_AP) {
899 entry_connection_t *entry_conn = TO_ENTRY_CONN(conn);
900 tor_str_wipe_and_free(entry_conn->chosen_exit_name);
901 tor_str_wipe_and_free(entry_conn->original_dest_address);
902 if (entry_conn->socks_request)
903 socks_request_free(entry_conn->socks_request);
904 if (entry_conn->pending_optimistic_data) {
905 buf_free(entry_conn->pending_optimistic_data);
907 if (entry_conn->sending_optimistic_data) {
908 buf_free(entry_conn->sending_optimistic_data);
911 if (CONN_IS_EDGE(conn)) {
912 hs_ident_edge_conn_free(TO_EDGE_CONN(conn)->hs_ident);
914 if (conn->type == CONN_TYPE_CONTROL) {
915 control_connection_t *control_conn = TO_CONTROL_CONN(conn);
916 tor_free(control_conn->safecookie_client_hash);
917 tor_free(control_conn->incoming_cmd);
918 tor_free(control_conn->current_cmd);
919 if (control_conn->ephemeral_onion_services) {
920 SMARTLIST_FOREACH(control_conn->ephemeral_onion_services, char *, cp, {
921 memwipe(cp, 0, strlen(cp));
922 tor_free(cp);
924 smartlist_free(control_conn->ephemeral_onion_services);
928 /* Probably already freed by connection_free. */
929 tor_event_free(conn->read_event);
930 tor_event_free(conn->write_event);
931 conn->read_event = conn->write_event = NULL;
933 if (conn->type == CONN_TYPE_DIR) {
934 dir_connection_t *dir_conn = TO_DIR_CONN(conn);
935 tor_free(dir_conn->requested_resource);
937 tor_compress_free(dir_conn->compress_state);
938 dir_conn_clear_spool(dir_conn);
940 hs_ident_dir_conn_free(dir_conn->hs_ident);
941 if (dir_conn->guard_state) {
942 /* Cancel before freeing, if it's still there. */
943 entry_guard_cancel(&dir_conn->guard_state);
945 circuit_guard_state_free(dir_conn->guard_state);
948 if (SOCKET_OK(conn->s)) {
949 log_debug(LD_NET,"closing fd %d.",(int)conn->s);
950 tor_close_socket(conn->s);
951 conn->s = TOR_INVALID_SOCKET;
954 if (conn->type == CONN_TYPE_OR &&
955 !tor_digest_is_zero(TO_OR_CONN(conn)->identity_digest)) {
956 log_warn(LD_BUG, "called on OR conn with non-zeroed identity_digest");
957 connection_or_clear_identity(TO_OR_CONN(conn));
959 if (conn->type == CONN_TYPE_OR || conn->type == CONN_TYPE_EXT_OR) {
960 tor_free(TO_OR_CONN(conn)->ext_or_conn_id);
961 tor_free(TO_OR_CONN(conn)->ext_or_auth_correct_client_hash);
962 tor_free(TO_OR_CONN(conn)->ext_or_transport);
965 memwipe(mem, 0xCC, memlen); /* poison memory */
966 tor_free(mem);
969 /** Make sure <b>conn</b> isn't in any of the global conn lists; then free it.
971 MOCK_IMPL(void,
972 connection_free_,(connection_t *conn))
974 if (!conn)
975 return;
976 tor_assert(!connection_is_on_closeable_list(conn));
977 tor_assert(!connection_in_array(conn));
978 if (BUG(conn->linked_conn)) {
979 conn->linked_conn->linked_conn = NULL;
980 if (! conn->linked_conn->marked_for_close &&
981 conn->linked_conn->reading_from_linked_conn)
982 connection_start_reading(conn->linked_conn);
983 conn->linked_conn = NULL;
985 if (connection_speaks_cells(conn)) {
986 if (!tor_digest_is_zero(TO_OR_CONN(conn)->identity_digest)) {
987 connection_or_clear_identity(TO_OR_CONN(conn));
990 if (conn->type == CONN_TYPE_CONTROL) {
991 connection_control_closed(TO_CONTROL_CONN(conn));
993 #if 1
994 /* DEBUGGING */
995 if (conn->type == CONN_TYPE_AP) {
996 connection_ap_warn_and_unmark_if_pending_circ(TO_ENTRY_CONN(conn),
997 "connection_free");
999 #endif /* 1 */
1001 /* Notify the circuit creation DoS mitigation subsystem that an OR client
1002 * connection has been closed. And only do that if we track it. */
1003 if (conn->type == CONN_TYPE_OR) {
1004 dos_close_client_conn(TO_OR_CONN(conn));
1007 connection_unregister_events(conn);
1008 connection_free_minimal(conn);
1012 * Called when we're about to finally unlink and free a connection:
1013 * perform necessary accounting and cleanup
1014 * - Directory conns that failed to fetch a rendezvous descriptor
1015 * need to inform pending rendezvous streams.
1016 * - OR conns need to call rep_hist_note_*() to record status.
1017 * - AP conns need to send a socks reject if necessary.
1018 * - Exit conns need to call connection_dns_remove() if necessary.
1019 * - AP and Exit conns need to send an end cell if they can.
1020 * - DNS conns need to fail any resolves that are pending on them.
1021 * - OR and edge connections need to be unlinked from circuits.
1023 void
1024 connection_about_to_close_connection(connection_t *conn)
1026 tor_assert(conn->marked_for_close);
1028 switch (conn->type) {
1029 case CONN_TYPE_DIR:
1030 connection_dir_about_to_close(TO_DIR_CONN(conn));
1031 break;
1032 case CONN_TYPE_OR:
1033 case CONN_TYPE_EXT_OR:
1034 connection_or_about_to_close(TO_OR_CONN(conn));
1035 break;
1036 case CONN_TYPE_AP:
1037 connection_ap_about_to_close(TO_ENTRY_CONN(conn));
1038 break;
1039 case CONN_TYPE_EXIT:
1040 connection_exit_about_to_close(TO_EDGE_CONN(conn));
1041 break;
1045 /** Return true iff connection_close_immediate() has been called on this
1046 * connection. */
1047 #define CONN_IS_CLOSED(c) \
1048 ((c)->linked ? ((c)->linked_conn_is_closed) : (! SOCKET_OK(c->s)))
1050 /** Close the underlying socket for <b>conn</b>, so we don't try to
1051 * flush it. Must be used in conjunction with (right before)
1052 * connection_mark_for_close().
1054 void
1055 connection_close_immediate(connection_t *conn)
1057 assert_connection_ok(conn,0);
1058 if (CONN_IS_CLOSED(conn)) {
1059 log_err(LD_BUG,"Attempt to close already-closed connection.");
1060 tor_fragile_assert();
1061 return;
1063 if (connection_get_outbuf_len(conn)) {
1064 log_info(LD_NET,"fd %d, type %s, state %s, %"TOR_PRIuSZ" bytes on outbuf.",
1065 (int)conn->s, conn_type_to_string(conn->type),
1066 conn_state_to_string(conn->type, conn->state),
1067 buf_datalen(conn->outbuf));
1070 connection_unregister_events(conn);
1072 /* Prevent the event from getting unblocked. */
1073 conn->read_blocked_on_bw = 0;
1074 conn->write_blocked_on_bw = 0;
1076 if (SOCKET_OK(conn->s))
1077 tor_close_socket(conn->s);
1078 conn->s = TOR_INVALID_SOCKET;
1079 if (conn->linked)
1080 conn->linked_conn_is_closed = 1;
1081 if (conn->outbuf)
1082 buf_clear(conn->outbuf);
1085 /** Mark <b>conn</b> to be closed next time we loop through
1086 * conn_close_if_marked() in main.c. */
1087 void
1088 connection_mark_for_close_(connection_t *conn, int line, const char *file)
1090 assert_connection_ok(conn,0);
1091 tor_assert(line);
1092 tor_assert(line < 1<<16); /* marked_for_close can only fit a uint16_t. */
1093 tor_assert(file);
1095 if (conn->type == CONN_TYPE_OR) {
1097 * An or_connection should have been closed through one of the channel-
1098 * aware functions in connection_or.c. We'll assume this is an error
1099 * close and do that, and log a bug warning.
1101 log_warn(LD_CHANNEL | LD_BUG,
1102 "Something tried to close an or_connection_t without going "
1103 "through channels at %s:%d",
1104 file, line);
1105 connection_or_close_for_error(TO_OR_CONN(conn), 0);
1106 } else {
1107 /* Pass it down to the real function */
1108 connection_mark_for_close_internal_(conn, line, file);
1112 /** Mark <b>conn</b> to be closed next time we loop through
1113 * conn_close_if_marked() in main.c.
1115 * This _internal version bypasses the CONN_TYPE_OR checks; this should be
1116 * called when you either are sure that if this is an or_connection_t the
1117 * controlling channel has been notified (e.g. with
1118 * connection_or_notify_error()), or you actually are the
1119 * connection_or_close_for_error() or connection_or_close_normally() function.
1120 * For all other cases, use connection_mark_and_flush() which checks for
1121 * or_connection_t properly, instead. See below.
1123 * We want to keep this function simple and quick, since it can be called from
1124 * quite deep in the call chain, and hence it should avoid having side-effects
1125 * that interfere with its callers view of the connection.
1127 MOCK_IMPL(void,
1128 connection_mark_for_close_internal_, (connection_t *conn,
1129 int line, const char *file))
1131 assert_connection_ok(conn,0);
1132 tor_assert(line);
1133 tor_assert(line < 1<<16); /* marked_for_close can only fit a uint16_t. */
1134 tor_assert(file);
1136 if (conn->marked_for_close) {
1137 log_warn(LD_BUG,"Duplicate call to connection_mark_for_close at %s:%d"
1138 " (first at %s:%d)", file, line, conn->marked_for_close_file,
1139 conn->marked_for_close);
1140 tor_fragile_assert();
1141 return;
1144 if (conn->type == CONN_TYPE_OR) {
1146 * Bad news if this happens without telling the controlling channel; do
1147 * this so we can find things that call this wrongly when the asserts hit.
1149 log_debug(LD_CHANNEL,
1150 "Calling connection_mark_for_close_internal_() on an OR conn "
1151 "at %s:%d",
1152 file, line);
1155 conn->marked_for_close = line;
1156 conn->marked_for_close_file = file;
1157 add_connection_to_closeable_list(conn);
1159 /* in case we're going to be held-open-til-flushed, reset
1160 * the number of seconds since last successful write, so
1161 * we get our whole 15 seconds */
1162 conn->timestamp_last_write_allowed = time(NULL);
1165 /** Find each connection that has hold_open_until_flushed set to
1166 * 1 but hasn't written in the past 15 seconds, and set
1167 * hold_open_until_flushed to 0. This means it will get cleaned
1168 * up in the next loop through close_if_marked() in main.c.
1170 void
1171 connection_expire_held_open(void)
1173 time_t now;
1174 smartlist_t *conns = get_connection_array();
1176 now = time(NULL);
1178 SMARTLIST_FOREACH_BEGIN(conns, connection_t *, conn) {
1179 /* If we've been holding the connection open, but we haven't written
1180 * for 15 seconds...
1182 if (conn->hold_open_until_flushed) {
1183 tor_assert(conn->marked_for_close);
1184 if (now - conn->timestamp_last_write_allowed >= 15) {
1185 int severity;
1186 if (conn->type == CONN_TYPE_EXIT ||
1187 (conn->type == CONN_TYPE_DIR &&
1188 conn->purpose == DIR_PURPOSE_SERVER))
1189 severity = LOG_INFO;
1190 else
1191 severity = LOG_NOTICE;
1192 log_fn(severity, LD_NET,
1193 "Giving up on marked_for_close conn that's been flushing "
1194 "for 15s (fd %d, type %s, state %s).",
1195 (int)conn->s, conn_type_to_string(conn->type),
1196 conn_state_to_string(conn->type, conn->state));
1197 conn->hold_open_until_flushed = 0;
1200 } SMARTLIST_FOREACH_END(conn);
1203 #if defined(HAVE_SYS_UN_H) || defined(RUNNING_DOXYGEN)
1204 /** Create an AF_UNIX listenaddr struct.
1205 * <b>listenaddress</b> provides the path to the Unix socket.
1207 * Eventually <b>listenaddress</b> will also optionally contain user, group,
1208 * and file permissions for the new socket. But not yet. XXX
1209 * Also, since we do not create the socket here the information doesn't help
1210 * here.
1212 * If not NULL <b>readable_address</b> will contain a copy of the path part of
1213 * <b>listenaddress</b>.
1215 * The listenaddr struct has to be freed by the caller.
1217 static struct sockaddr_un *
1218 create_unix_sockaddr(const char *listenaddress, char **readable_address,
1219 socklen_t *len_out)
1221 struct sockaddr_un *sockaddr = NULL;
1223 sockaddr = tor_malloc_zero(sizeof(struct sockaddr_un));
1224 sockaddr->sun_family = AF_UNIX;
1225 if (strlcpy(sockaddr->sun_path, listenaddress, sizeof(sockaddr->sun_path))
1226 >= sizeof(sockaddr->sun_path)) {
1227 log_warn(LD_CONFIG, "Unix socket path '%s' is too long to fit.",
1228 escaped(listenaddress));
1229 tor_free(sockaddr);
1230 return NULL;
1233 if (readable_address)
1234 *readable_address = tor_strdup(listenaddress);
1236 *len_out = sizeof(struct sockaddr_un);
1237 return sockaddr;
1239 #else /* !(defined(HAVE_SYS_UN_H) || defined(RUNNING_DOXYGEN)) */
1240 static struct sockaddr *
1241 create_unix_sockaddr(const char *listenaddress, char **readable_address,
1242 socklen_t *len_out)
1244 (void)listenaddress;
1245 (void)readable_address;
1246 log_fn(LOG_ERR, LD_BUG,
1247 "Unix domain sockets not supported, yet we tried to create one.");
1248 *len_out = 0;
1249 tor_fragile_assert();
1250 return NULL;
1252 #endif /* defined(HAVE_SYS_UN_H) || defined(RUNNING_DOXYGEN) */
1255 * A socket failed from resource exhaustion.
1257 * Among other actions, warn that an accept or a connect has failed because
1258 * we're running out of TCP sockets we can use on current system. Rate-limit
1259 * these warnings so that we don't spam the log. */
1260 static void
1261 socket_failed_from_resource_exhaustion(void)
1263 /* When we get to this point we know that a socket could not be
1264 * established. However the kernel does not let us know whether the reason is
1265 * because we ran out of TCP source ports, or because we exhausted all the
1266 * FDs on this system, or for any other reason.
1268 * For this reason, we are going to use the following heuristic: If our
1269 * system supports a lot of sockets, we will assume that it's a problem of
1270 * TCP port exhaustion. Otherwise, if our system does not support many
1271 * sockets, we will assume that this is because of file descriptor
1272 * exhaustion.
1274 if (get_max_sockets() > 65535) {
1275 /* TCP port exhaustion */
1276 rep_hist_note_tcp_exhaustion();
1277 } else {
1278 /* File descriptor exhaustion */
1279 rep_hist_note_overload(OVERLOAD_FD_EXHAUSTED);
1282 #define WARN_TOO_MANY_CONNS_INTERVAL (6*60*60)
1283 static ratelim_t last_warned = RATELIM_INIT(WARN_TOO_MANY_CONNS_INTERVAL);
1284 char *m;
1285 if ((m = rate_limit_log(&last_warned, approx_time()))) {
1286 int n_conns = get_n_open_sockets();
1287 log_warn(LD_NET,"Failing because we have %d connections already. Please "
1288 "read doc/TUNING for guidance.%s", n_conns, m);
1289 tor_free(m);
1290 control_event_general_status(LOG_WARN, "TOO_MANY_CONNECTIONS CURRENT=%d",
1291 n_conns);
1295 #ifdef HAVE_SYS_UN_H
1297 #define UNIX_SOCKET_PURPOSE_CONTROL_SOCKET 0
1298 #define UNIX_SOCKET_PURPOSE_SOCKS_SOCKET 1
1300 /** Check if the purpose isn't one of the ones we know what to do with */
1302 static int
1303 is_valid_unix_socket_purpose(int purpose)
1305 int valid = 0;
1307 switch (purpose) {
1308 case UNIX_SOCKET_PURPOSE_CONTROL_SOCKET:
1309 case UNIX_SOCKET_PURPOSE_SOCKS_SOCKET:
1310 valid = 1;
1311 break;
1314 return valid;
1317 /** Return a string description of a unix socket purpose */
1318 static const char *
1319 unix_socket_purpose_to_string(int purpose)
1321 const char *s = "unknown-purpose socket";
1323 switch (purpose) {
1324 case UNIX_SOCKET_PURPOSE_CONTROL_SOCKET:
1325 s = "control socket";
1326 break;
1327 case UNIX_SOCKET_PURPOSE_SOCKS_SOCKET:
1328 s = "SOCKS socket";
1329 break;
1332 return s;
1335 /** Check whether we should be willing to open an AF_UNIX socket in
1336 * <b>path</b>. Return 0 if we should go ahead and -1 if we shouldn't. */
1337 static int
1338 check_location_for_unix_socket(const or_options_t *options, const char *path,
1339 int purpose, const port_cfg_t *port)
1341 int r = -1;
1342 char *p = NULL;
1344 tor_assert(is_valid_unix_socket_purpose(purpose));
1346 p = tor_strdup(path);
1347 cpd_check_t flags = CPD_CHECK_MODE_ONLY;
1348 if (get_parent_directory(p)<0 || p[0] != '/') {
1349 log_warn(LD_GENERAL, "Bad unix socket address '%s'. Tor does not support "
1350 "relative paths for unix sockets.", path);
1351 goto done;
1354 if (port->is_world_writable) {
1355 /* World-writable sockets can go anywhere. */
1356 r = 0;
1357 goto done;
1360 if (port->is_group_writable) {
1361 flags |= CPD_GROUP_OK;
1364 if (port->relax_dirmode_check) {
1365 flags |= CPD_RELAX_DIRMODE_CHECK;
1368 if (check_private_dir(p, flags, options->User) < 0) {
1369 char *escpath, *escdir;
1370 escpath = esc_for_log(path);
1371 escdir = esc_for_log(p);
1372 log_warn(LD_GENERAL, "Before Tor can create a %s in %s, the directory "
1373 "%s needs to exist, and to be accessible only by the user%s "
1374 "account that is running Tor. (On some Unix systems, anybody "
1375 "who can list a socket can connect to it, so Tor is being "
1376 "careful.)",
1377 unix_socket_purpose_to_string(purpose), escpath, escdir,
1378 port->is_group_writable ? " and group" : "");
1379 tor_free(escpath);
1380 tor_free(escdir);
1381 goto done;
1384 r = 0;
1385 done:
1386 tor_free(p);
1387 return r;
1389 #endif /* defined(HAVE_SYS_UN_H) */
1391 /** Tell the TCP stack that it shouldn't wait for a long time after
1392 * <b>sock</b> has closed before reusing its port. Return 0 on success,
1393 * -1 on failure. */
1394 static int
1395 make_socket_reuseable(tor_socket_t sock)
1397 #ifdef _WIN32
1398 (void) sock;
1399 return 0;
1400 #else
1401 int one=1;
1403 /* REUSEADDR on normal places means you can rebind to the port
1404 * right after somebody else has let it go. But REUSEADDR on win32
1405 * means you can bind to the port _even when somebody else
1406 * already has it bound_. So, don't do that on Win32. */
1407 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void*) &one,
1408 (socklen_t)sizeof(one)) == -1) {
1409 return -1;
1411 return 0;
1412 #endif /* defined(_WIN32) */
1415 #ifdef _WIN32
1416 /** Tell the Windows TCP stack to prevent other applications from receiving
1417 * traffic from tor's open ports. Return 0 on success, -1 on failure. */
1418 static int
1419 make_win32_socket_exclusive(tor_socket_t sock)
1421 #ifdef SO_EXCLUSIVEADDRUSE
1422 int one=1;
1424 /* Any socket that sets REUSEADDR on win32 can bind to a port _even when
1425 * somebody else already has it bound_, and _even if the original socket
1426 * didn't set REUSEADDR_. Use EXCLUSIVEADDRUSE to prevent this port-stealing
1427 * on win32. */
1428 if (setsockopt(sock, SOL_SOCKET, SO_EXCLUSIVEADDRUSE, (void*) &one,
1429 (socklen_t)sizeof(one))) {
1430 return -1;
1432 return 0;
1433 #else /* !defined(SO_EXCLUSIVEADDRUSE) */
1434 (void) sock;
1435 return 0;
1436 #endif /* defined(SO_EXCLUSIVEADDRUSE) */
1438 #endif /* defined(_WIN32) */
1440 /** Max backlog to pass to listen. We start at */
1441 static int listen_limit = INT_MAX;
1443 /* Listen on <b>fd</b> with appropriate backlog. Return as for listen. */
1444 static int
1445 tor_listen(tor_socket_t fd)
1447 int r;
1449 if ((r = listen(fd, listen_limit)) < 0) {
1450 if (listen_limit == SOMAXCONN)
1451 return r;
1452 if ((r = listen(fd, SOMAXCONN)) == 0) {
1453 listen_limit = SOMAXCONN;
1454 log_warn(LD_NET, "Setting listen backlog to INT_MAX connections "
1455 "didn't work, but SOMAXCONN did. Lowering backlog limit.");
1458 return r;
1461 /** Bind a new non-blocking socket listening to the socket described
1462 * by <b>listensockaddr</b>.
1464 * <b>address</b> is only used for logging purposes and to add the information
1465 * to the conn.
1467 * Set <b>addr_in_use</b> to true in case socket binding fails with
1468 * EADDRINUSE.
1470 static connection_t *
1471 connection_listener_new(const struct sockaddr *listensockaddr,
1472 socklen_t socklen,
1473 int type, const char *address,
1474 const port_cfg_t *port_cfg,
1475 int *addr_in_use)
1477 listener_connection_t *lis_conn;
1478 connection_t *conn = NULL;
1479 tor_socket_t s = TOR_INVALID_SOCKET; /* the socket we're going to make */
1480 or_options_t const *options = get_options();
1481 (void) options; /* Windows doesn't use this. */
1482 #if defined(HAVE_PWD_H) && defined(HAVE_SYS_UN_H)
1483 const struct passwd *pw = NULL;
1484 #endif
1485 uint16_t usePort = 0, gotPort = 0;
1486 int start_reading = 0;
1487 static int global_next_session_group = SESSION_GROUP_FIRST_AUTO;
1488 tor_addr_t addr;
1489 int exhaustion = 0;
1491 if (addr_in_use)
1492 *addr_in_use = 0;
1494 if (listensockaddr->sa_family == AF_INET ||
1495 listensockaddr->sa_family == AF_INET6) {
1496 int is_stream = (type != CONN_TYPE_AP_DNS_LISTENER);
1497 if (is_stream)
1498 start_reading = 1;
1500 tor_addr_from_sockaddr(&addr, listensockaddr, &usePort);
1501 log_notice(LD_NET, "Opening %s on %s",
1502 conn_type_to_string(type), fmt_addrport(&addr, usePort));
1504 s = tor_open_socket_nonblocking(tor_addr_family(&addr),
1505 is_stream ? SOCK_STREAM : SOCK_DGRAM,
1506 is_stream ? IPPROTO_TCP: IPPROTO_UDP);
1507 if (!SOCKET_OK(s)) {
1508 int e = tor_socket_errno(s);
1509 if (ERRNO_IS_RESOURCE_LIMIT(e)) {
1510 socket_failed_from_resource_exhaustion();
1512 * We'll call the OOS handler at the error exit, so set the
1513 * exhaustion flag for it.
1515 exhaustion = 1;
1516 } else {
1517 log_warn(LD_NET, "Socket creation failed: %s",
1518 tor_socket_strerror(e));
1520 goto err;
1523 if (make_socket_reuseable(s) < 0) {
1524 log_warn(LD_NET, "Error setting SO_REUSEADDR flag on %s: %s",
1525 conn_type_to_string(type),
1526 tor_socket_strerror(errno));
1529 #ifdef _WIN32
1530 if (make_win32_socket_exclusive(s) < 0) {
1531 log_warn(LD_NET, "Error setting SO_EXCLUSIVEADDRUSE flag on %s: %s",
1532 conn_type_to_string(type),
1533 tor_socket_strerror(errno));
1535 #endif /* defined(_WIN32) */
1537 #if defined(USE_TRANSPARENT) && defined(IP_TRANSPARENT)
1538 if (options->TransProxyType_parsed == TPT_TPROXY &&
1539 type == CONN_TYPE_AP_TRANS_LISTENER) {
1540 int one = 1;
1541 if (setsockopt(s, SOL_IP, IP_TRANSPARENT, (void*)&one,
1542 (socklen_t)sizeof(one)) < 0) {
1543 const char *extra = "";
1544 int e = tor_socket_errno(s);
1545 if (e == EPERM)
1546 extra = "TransTPROXY requires root privileges or similar"
1547 " capabilities.";
1548 log_warn(LD_NET, "Error setting IP_TRANSPARENT flag: %s.%s",
1549 tor_socket_strerror(e), extra);
1552 #endif /* defined(USE_TRANSPARENT) && defined(IP_TRANSPARENT) */
1554 #ifdef IPV6_V6ONLY
1555 if (listensockaddr->sa_family == AF_INET6) {
1556 int one = 1;
1557 /* We need to set IPV6_V6ONLY so that this socket can't get used for
1558 * IPv4 connections. */
1559 if (setsockopt(s,IPPROTO_IPV6, IPV6_V6ONLY,
1560 (void*)&one, (socklen_t)sizeof(one)) < 0) {
1561 int e = tor_socket_errno(s);
1562 log_warn(LD_NET, "Error setting IPV6_V6ONLY flag: %s",
1563 tor_socket_strerror(e));
1564 /* Keep going; probably not harmful. */
1567 #endif /* defined(IPV6_V6ONLY) */
1569 if (bind(s,listensockaddr,socklen) < 0) {
1570 const char *helpfulhint = "";
1571 int e = tor_socket_errno(s);
1572 if (ERRNO_IS_EADDRINUSE(e)) {
1573 helpfulhint = ". Is Tor already running?";
1574 if (addr_in_use)
1575 *addr_in_use = 1;
1577 log_warn(LD_NET, "Could not bind to %s:%u: %s%s", address, usePort,
1578 tor_socket_strerror(e), helpfulhint);
1579 goto err;
1582 if (is_stream) {
1583 if (tor_listen(s) < 0) {
1584 log_warn(LD_NET, "Could not listen on %s:%u: %s", address, usePort,
1585 tor_socket_strerror(tor_socket_errno(s)));
1586 goto err;
1590 if (usePort != 0) {
1591 gotPort = usePort;
1592 } else {
1593 tor_addr_t addr2;
1594 struct sockaddr_storage ss;
1595 socklen_t ss_len=sizeof(ss);
1596 if (getsockname(s, (struct sockaddr*)&ss, &ss_len)<0) {
1597 log_warn(LD_NET, "getsockname() couldn't learn address for %s: %s",
1598 conn_type_to_string(type),
1599 tor_socket_strerror(tor_socket_errno(s)));
1600 gotPort = 0;
1602 tor_addr_from_sockaddr(&addr2, (struct sockaddr*)&ss, &gotPort);
1604 #ifdef HAVE_SYS_UN_H
1606 * AF_UNIX generic setup stuff
1608 } else if (listensockaddr->sa_family == AF_UNIX) {
1609 /* We want to start reading for both AF_UNIX cases */
1610 start_reading = 1;
1612 tor_assert(conn_listener_type_supports_af_unix(type));
1614 if (check_location_for_unix_socket(options, address,
1615 (type == CONN_TYPE_CONTROL_LISTENER) ?
1616 UNIX_SOCKET_PURPOSE_CONTROL_SOCKET :
1617 UNIX_SOCKET_PURPOSE_SOCKS_SOCKET, port_cfg) < 0) {
1618 goto err;
1621 log_notice(LD_NET, "Opening %s on %s",
1622 conn_type_to_string(type), address);
1624 tor_addr_make_unspec(&addr);
1626 if (unlink(address) < 0 && errno != ENOENT) {
1627 log_warn(LD_NET, "Could not unlink %s: %s", address,
1628 strerror(errno));
1629 goto err;
1632 s = tor_open_socket_nonblocking(AF_UNIX, SOCK_STREAM, 0);
1633 if (! SOCKET_OK(s)) {
1634 int e = tor_socket_errno(s);
1635 if (ERRNO_IS_RESOURCE_LIMIT(e)) {
1636 socket_failed_from_resource_exhaustion();
1638 * We'll call the OOS handler at the error exit, so set the
1639 * exhaustion flag for it.
1641 exhaustion = 1;
1642 } else {
1643 log_warn(LD_NET,"Socket creation failed: %s.", strerror(e));
1645 goto err;
1648 if (bind(s, listensockaddr,
1649 (socklen_t)sizeof(struct sockaddr_un)) == -1) {
1650 log_warn(LD_NET,"Bind to %s failed: %s.", address,
1651 tor_socket_strerror(tor_socket_errno(s)));
1652 goto err;
1655 #ifdef HAVE_PWD_H
1656 if (options->User) {
1657 pw = tor_getpwnam(options->User);
1658 struct stat st;
1659 if (pw == NULL) {
1660 log_warn(LD_NET,"Unable to chown() %s socket: user %s not found.",
1661 address, options->User);
1662 goto err;
1663 } else if (fstat(s, &st) == 0 &&
1664 st.st_uid == pw->pw_uid && st.st_gid == pw->pw_gid) {
1665 /* No change needed */
1666 } else if (chown(sandbox_intern_string(address),
1667 pw->pw_uid, pw->pw_gid) < 0) {
1668 log_warn(LD_NET,"Unable to chown() %s socket: %s.",
1669 address, strerror(errno));
1670 goto err;
1673 #endif /* defined(HAVE_PWD_H) */
1676 unsigned mode;
1677 const char *status;
1678 struct stat st;
1679 if (port_cfg->is_world_writable) {
1680 mode = 0666;
1681 status = "world-writable";
1682 } else if (port_cfg->is_group_writable) {
1683 mode = 0660;
1684 status = "group-writable";
1685 } else {
1686 mode = 0600;
1687 status = "private";
1689 /* We need to use chmod; fchmod doesn't work on sockets on all
1690 * platforms. */
1691 if (fstat(s, &st) == 0 && (st.st_mode & 0777) == mode) {
1692 /* no change needed */
1693 } else if (chmod(sandbox_intern_string(address), mode) < 0) {
1694 log_warn(LD_FS,"Unable to make %s %s.", address, status);
1695 goto err;
1699 if (listen(s, SOMAXCONN) < 0) {
1700 log_warn(LD_NET, "Could not listen on %s: %s", address,
1701 tor_socket_strerror(tor_socket_errno(s)));
1702 goto err;
1705 #ifndef __APPLE__
1706 /* This code was introduced to help debug #28229. */
1707 int value;
1708 socklen_t len = sizeof(value);
1710 if (!getsockopt(s, SOL_SOCKET, SO_ACCEPTCONN, &value, &len)) {
1711 if (value == 0) {
1712 log_err(LD_NET, "Could not listen on %s - "
1713 "getsockopt(.,SO_ACCEPTCONN,.) yields 0.", address);
1714 goto err;
1717 #endif /* !defined(__APPLE__) */
1718 #endif /* defined(HAVE_SYS_UN_H) */
1719 } else {
1720 log_err(LD_BUG, "Got unexpected address family %d.",
1721 listensockaddr->sa_family);
1722 tor_assert(0);
1725 lis_conn = listener_connection_new(type, listensockaddr->sa_family);
1726 conn = TO_CONN(lis_conn);
1727 conn->socket_family = listensockaddr->sa_family;
1728 conn->s = s;
1729 s = TOR_INVALID_SOCKET; /* Prevent double-close */
1730 conn->address = tor_strdup(address);
1731 conn->port = gotPort;
1732 tor_addr_copy(&conn->addr, &addr);
1734 memcpy(&lis_conn->entry_cfg, &port_cfg->entry_cfg, sizeof(entry_port_cfg_t));
1736 if (port_cfg->entry_cfg.isolation_flags) {
1737 lis_conn->entry_cfg.isolation_flags = port_cfg->entry_cfg.isolation_flags;
1738 if (port_cfg->entry_cfg.session_group >= 0) {
1739 lis_conn->entry_cfg.session_group = port_cfg->entry_cfg.session_group;
1740 } else {
1741 /* This can wrap after around INT_MAX listeners are opened. But I don't
1742 * believe that matters, since you would need to open a ridiculous
1743 * number of listeners while keeping the early ones open before you ever
1744 * hit this. An OR with a dozen ports open, for example, would have to
1745 * close and re-open its listeners every second for 4 years nonstop.
1747 lis_conn->entry_cfg.session_group = global_next_session_group--;
1751 if (connection_add(conn) < 0) { /* no space, forget it */
1752 log_warn(LD_NET,"connection_add for listener failed. Giving up.");
1753 goto err;
1756 log_fn(usePort==gotPort ? LOG_DEBUG : LOG_NOTICE, LD_NET,
1757 "%s listening on port %u.",
1758 conn_type_to_string(type), gotPort);
1760 conn->state = LISTENER_STATE_READY;
1761 if (start_reading) {
1762 connection_start_reading(conn);
1763 } else {
1764 tor_assert(type == CONN_TYPE_AP_DNS_LISTENER);
1765 dnsserv_configure_listener(conn);
1769 * Normal exit; call the OOS handler since connection count just changed;
1770 * the exhaustion flag will always be zero here though.
1772 connection_check_oos(get_n_open_sockets(), 0);
1774 log_notice(LD_NET, "Opened %s", connection_describe(conn));
1776 return conn;
1778 err:
1779 if (SOCKET_OK(s))
1780 tor_close_socket(s);
1781 if (conn)
1782 connection_free(conn);
1784 /* Call the OOS handler, indicate if we saw an exhaustion-related error */
1785 connection_check_oos(get_n_open_sockets(), exhaustion);
1787 return NULL;
1791 * Create a new listener connection for a given <b>port</b>. In case we
1792 * for a reason that is not an error condition, set <b>defer</b>
1793 * to true. If we cannot bind listening socket because address is already
1794 * in use, set <b>addr_in_use</b> to true.
1796 static connection_t *
1797 connection_listener_new_for_port(const port_cfg_t *port,
1798 int *defer, int *addr_in_use)
1800 connection_t *conn;
1801 struct sockaddr *listensockaddr;
1802 socklen_t listensocklen = 0;
1803 char *address=NULL;
1804 int real_port = port->port == CFG_AUTO_PORT ? 0 : port->port;
1805 tor_assert(real_port <= UINT16_MAX);
1807 if (defer)
1808 *defer = 0;
1810 if (port->server_cfg.no_listen) {
1811 if (defer)
1812 *defer = 1;
1813 return NULL;
1816 #ifndef _WIN32
1817 /* We don't need to be root to create a UNIX socket, so defer until after
1818 * setuid. */
1819 const or_options_t *options = get_options();
1820 if (port->is_unix_addr && !geteuid() && (options->User) &&
1821 strcmp(options->User, "root")) {
1822 if (defer)
1823 *defer = 1;
1824 return NULL;
1826 #endif /* !defined(_WIN32) */
1828 if (port->is_unix_addr) {
1829 listensockaddr = (struct sockaddr *)
1830 create_unix_sockaddr(port->unix_addr,
1831 &address, &listensocklen);
1832 } else {
1833 listensockaddr = tor_malloc(sizeof(struct sockaddr_storage));
1834 listensocklen = tor_addr_to_sockaddr(&port->addr,
1835 real_port,
1836 listensockaddr,
1837 sizeof(struct sockaddr_storage));
1838 address = tor_addr_to_str_dup(&port->addr);
1841 if (listensockaddr) {
1842 conn = connection_listener_new(listensockaddr, listensocklen,
1843 port->type, address, port,
1844 addr_in_use);
1845 tor_free(listensockaddr);
1846 tor_free(address);
1847 } else {
1848 conn = NULL;
1851 return conn;
1854 /** Do basic sanity checking on a newly received socket. Return 0
1855 * if it looks ok, else return -1.
1857 * Notably, some TCP stacks can erroneously have accept() return successfully
1858 * with socklen 0, when the client sends an RST before the accept call (as
1859 * nmap does). We want to detect that, and not go on with the connection.
1861 static int
1862 check_sockaddr(const struct sockaddr *sa, int len, int level)
1864 int ok = 1;
1866 if (sa->sa_family == AF_INET) {
1867 struct sockaddr_in *sin=(struct sockaddr_in*)sa;
1868 if (len != sizeof(struct sockaddr_in)) {
1869 log_fn(level, LD_NET, "Length of address not as expected: %d vs %d",
1870 len,(int)sizeof(struct sockaddr_in));
1871 ok = 0;
1873 if (sin->sin_addr.s_addr == 0 || sin->sin_port == 0) {
1874 log_fn(level, LD_NET,
1875 "Address for new connection has address/port equal to zero.");
1876 ok = 0;
1878 } else if (sa->sa_family == AF_INET6) {
1879 struct sockaddr_in6 *sin6=(struct sockaddr_in6*)sa;
1880 if (len != sizeof(struct sockaddr_in6)) {
1881 log_fn(level, LD_NET, "Length of address not as expected: %d vs %d",
1882 len,(int)sizeof(struct sockaddr_in6));
1883 ok = 0;
1885 if (fast_mem_is_zero((void*)sin6->sin6_addr.s6_addr, 16) ||
1886 sin6->sin6_port == 0) {
1887 log_fn(level, LD_NET,
1888 "Address for new connection has address/port equal to zero.");
1889 ok = 0;
1891 } else if (sa->sa_family == AF_UNIX) {
1892 ok = 1;
1893 } else {
1894 ok = 0;
1896 return ok ? 0 : -1;
1899 /** Check whether the socket family from an accepted socket <b>got</b> is the
1900 * same as the one that <b>listener</b> is waiting for. If it isn't, log
1901 * a useful message and return -1. Else return 0.
1903 * This is annoying, but can apparently happen on some Darwins. */
1904 static int
1905 check_sockaddr_family_match(sa_family_t got, connection_t *listener)
1907 if (got != listener->socket_family) {
1908 log_info(LD_BUG, "A listener connection returned a socket with a "
1909 "mismatched family. %s for addr_family %d gave us a socket "
1910 "with address family %d. Dropping.",
1911 conn_type_to_string(listener->type),
1912 (int)listener->socket_family,
1913 (int)got);
1914 return -1;
1916 return 0;
1919 /** The listener connection <b>conn</b> told poll() it wanted to read.
1920 * Call accept() on conn-\>s, and add the new connection if necessary.
1922 static int
1923 connection_handle_listener_read(connection_t *conn, int new_type)
1925 tor_socket_t news; /* the new socket */
1926 connection_t *newconn = 0;
1927 /* information about the remote peer when connecting to other routers */
1928 struct sockaddr_storage addrbuf;
1929 struct sockaddr *remote = (struct sockaddr*)&addrbuf;
1930 /* length of the remote address. Must be whatever accept() needs. */
1931 socklen_t remotelen = (socklen_t)sizeof(addrbuf);
1932 const or_options_t *options = get_options();
1934 tor_assert((size_t)remotelen >= sizeof(struct sockaddr_in));
1935 memset(&addrbuf, 0, sizeof(addrbuf));
1937 news = tor_accept_socket_nonblocking(conn->s,remote,&remotelen);
1938 if (!SOCKET_OK(news)) { /* accept() error */
1939 int e = tor_socket_errno(conn->s);
1940 if (ERRNO_IS_ACCEPT_EAGAIN(e)) {
1942 * they hung up before we could accept(). that's fine.
1944 * give the OOS handler a chance to run though
1946 connection_check_oos(get_n_open_sockets(), 0);
1947 return 0;
1948 } else if (ERRNO_IS_RESOURCE_LIMIT(e)) {
1949 socket_failed_from_resource_exhaustion();
1950 /* Exhaustion; tell the OOS handler */
1951 connection_check_oos(get_n_open_sockets(), 1);
1952 return 0;
1954 /* else there was a real error. */
1955 log_warn(LD_NET,"accept() failed: %s. Closing listener.",
1956 tor_socket_strerror(e));
1957 connection_mark_for_close(conn);
1958 /* Tell the OOS handler about this too */
1959 connection_check_oos(get_n_open_sockets(), 0);
1960 return -1;
1962 log_debug(LD_NET,
1963 "Connection accepted on socket %d (child of fd %d).",
1964 (int)news,(int)conn->s);
1966 /* We accepted a new conn; run OOS handler */
1967 connection_check_oos(get_n_open_sockets(), 0);
1969 if (make_socket_reuseable(news) < 0) {
1970 if (tor_socket_errno(news) == EINVAL) {
1971 /* This can happen on OSX if we get a badly timed shutdown. */
1972 log_debug(LD_NET, "make_socket_reuseable returned EINVAL");
1973 } else {
1974 log_warn(LD_NET, "Error setting SO_REUSEADDR flag on %s: %s",
1975 conn_type_to_string(new_type),
1976 tor_socket_strerror(errno));
1978 tor_close_socket(news);
1979 return 0;
1982 if (options->ConstrainedSockets)
1983 set_constrained_socket_buffers(news, (int)options->ConstrainedSockSize);
1985 if (check_sockaddr_family_match(remote->sa_family, conn) < 0) {
1986 tor_close_socket(news);
1987 return 0;
1990 if (conn->socket_family == AF_INET || conn->socket_family == AF_INET6 ||
1991 (conn->socket_family == AF_UNIX && new_type == CONN_TYPE_AP)) {
1992 tor_addr_t addr;
1993 uint16_t port;
1994 if (check_sockaddr(remote, remotelen, LOG_INFO)<0) {
1995 log_info(LD_NET,
1996 "accept() returned a strange address; closing connection.");
1997 tor_close_socket(news);
1998 return 0;
2001 tor_addr_from_sockaddr(&addr, remote, &port);
2003 /* process entrance policies here, before we even create the connection */
2004 if (new_type == CONN_TYPE_AP) {
2005 /* check sockspolicy to see if we should accept it */
2006 if (socks_policy_permits_address(&addr) == 0) {
2007 log_notice(LD_APP,
2008 "Denying socks connection from untrusted address %s.",
2009 fmt_and_decorate_addr(&addr));
2010 tor_close_socket(news);
2011 return 0;
2014 if (new_type == CONN_TYPE_DIR) {
2015 /* check dirpolicy to see if we should accept it */
2016 if (dir_policy_permits_address(&addr) == 0) {
2017 log_notice(LD_DIRSERV,"Denying dir connection from address %s.",
2018 fmt_and_decorate_addr(&addr));
2019 tor_close_socket(news);
2020 return 0;
2023 if (new_type == CONN_TYPE_OR) {
2024 /* Assess with the connection DoS mitigation subsystem if this address
2025 * can open a new connection. */
2026 if (dos_conn_addr_get_defense_type(&addr) == DOS_CONN_DEFENSE_CLOSE) {
2027 tor_close_socket(news);
2028 return 0;
2032 newconn = connection_new(new_type, conn->socket_family);
2033 newconn->s = news;
2035 /* remember the remote address */
2036 tor_addr_copy(&newconn->addr, &addr);
2037 if (new_type == CONN_TYPE_AP && conn->socket_family == AF_UNIX) {
2038 newconn->port = 0;
2039 newconn->address = tor_strdup(conn->address);
2040 } else {
2041 newconn->port = port;
2042 newconn->address = tor_addr_to_str_dup(&addr);
2045 if (new_type == CONN_TYPE_AP && conn->socket_family != AF_UNIX) {
2046 log_info(LD_NET, "New SOCKS connection opened from %s.",
2047 fmt_and_decorate_addr(&addr));
2049 if (new_type == CONN_TYPE_AP && conn->socket_family == AF_UNIX) {
2050 log_info(LD_NET, "New SOCKS AF_UNIX connection opened");
2052 if (new_type == CONN_TYPE_CONTROL) {
2053 log_notice(LD_CONTROL, "New control connection opened from %s.",
2054 fmt_and_decorate_addr(&addr));
2056 if (new_type == CONN_TYPE_METRICS) {
2057 log_info(LD_CONTROL, "New metrics connection opened from %s.",
2058 fmt_and_decorate_addr(&addr));
2061 } else if (conn->socket_family == AF_UNIX && conn->type != CONN_TYPE_AP) {
2062 tor_assert(conn->type == CONN_TYPE_CONTROL_LISTENER);
2063 tor_assert(new_type == CONN_TYPE_CONTROL);
2064 log_notice(LD_CONTROL, "New control connection opened.");
2066 newconn = connection_new(new_type, conn->socket_family);
2067 newconn->s = news;
2069 /* remember the remote address -- do we have anything sane to put here? */
2070 tor_addr_make_unspec(&newconn->addr);
2071 newconn->port = 1;
2072 newconn->address = tor_strdup(conn->address);
2073 } else {
2074 tor_assert(0);
2077 if (connection_add(newconn) < 0) { /* no space, forget it */
2078 connection_free(newconn);
2079 return 0; /* no need to tear down the parent */
2082 if (connection_init_accepted_conn(newconn, TO_LISTENER_CONN(conn)) < 0) {
2083 if (! newconn->marked_for_close)
2084 connection_mark_for_close(newconn);
2085 return 0;
2088 note_connection(true /* inbound */, conn->socket_family);
2090 return 0;
2093 /** Initialize states for newly accepted connection <b>conn</b>.
2095 * If conn is an OR, start the TLS handshake.
2097 * If conn is a transparent AP, get its original destination
2098 * and place it in circuit_wait.
2100 * The <b>listener</b> parameter is only used for AP connections.
2103 connection_init_accepted_conn(connection_t *conn,
2104 const listener_connection_t *listener)
2106 int rv;
2108 connection_start_reading(conn);
2110 switch (conn->type) {
2111 case CONN_TYPE_EXT_OR:
2112 /* Initiate Extended ORPort authentication. */
2113 return connection_ext_or_start_auth(TO_OR_CONN(conn));
2114 case CONN_TYPE_OR:
2115 connection_or_event_status(TO_OR_CONN(conn), OR_CONN_EVENT_NEW, 0);
2116 rv = connection_tls_start_handshake(TO_OR_CONN(conn), 1);
2117 if (rv < 0) {
2118 connection_or_close_for_error(TO_OR_CONN(conn), 0);
2120 return rv;
2121 break;
2122 case CONN_TYPE_AP:
2123 memcpy(&TO_ENTRY_CONN(conn)->entry_cfg, &listener->entry_cfg,
2124 sizeof(entry_port_cfg_t));
2125 TO_ENTRY_CONN(conn)->nym_epoch = get_signewnym_epoch();
2126 TO_ENTRY_CONN(conn)->socks_request->listener_type = listener->base_.type;
2128 /* Any incoming connection on an entry port counts as user activity. */
2129 note_user_activity(approx_time());
2131 switch (TO_CONN(listener)->type) {
2132 case CONN_TYPE_AP_LISTENER:
2133 conn->state = AP_CONN_STATE_SOCKS_WAIT;
2134 TO_ENTRY_CONN(conn)->socks_request->socks_prefer_no_auth =
2135 listener->entry_cfg.socks_prefer_no_auth;
2136 TO_ENTRY_CONN(conn)->socks_request->socks_use_extended_errors =
2137 listener->entry_cfg.extended_socks5_codes;
2138 break;
2139 case CONN_TYPE_AP_TRANS_LISTENER:
2140 TO_ENTRY_CONN(conn)->is_transparent_ap = 1;
2141 /* XXXX028 -- is this correct still, with the addition of
2142 * pending_entry_connections ? */
2143 conn->state = AP_CONN_STATE_CIRCUIT_WAIT;
2144 return connection_ap_process_transparent(TO_ENTRY_CONN(conn));
2145 case CONN_TYPE_AP_NATD_LISTENER:
2146 TO_ENTRY_CONN(conn)->is_transparent_ap = 1;
2147 conn->state = AP_CONN_STATE_NATD_WAIT;
2148 break;
2149 case CONN_TYPE_AP_HTTP_CONNECT_LISTENER:
2150 conn->state = AP_CONN_STATE_HTTP_CONNECT_WAIT;
2152 break;
2153 case CONN_TYPE_DIR:
2154 conn->purpose = DIR_PURPOSE_SERVER;
2155 conn->state = DIR_CONN_STATE_SERVER_COMMAND_WAIT;
2156 break;
2157 case CONN_TYPE_CONTROL:
2158 conn->state = CONTROL_CONN_STATE_NEEDAUTH;
2159 break;
2161 return 0;
2164 /** Take conn, make a nonblocking socket; try to connect to
2165 * sa, binding to bindaddr if sa is not localhost. If fail, return -1 and if
2166 * applicable put your best guess about errno into *<b>socket_error</b>.
2167 * If connected return 1, if EAGAIN return 0.
2169 MOCK_IMPL(STATIC int,
2170 connection_connect_sockaddr,(connection_t *conn,
2171 const struct sockaddr *sa,
2172 socklen_t sa_len,
2173 const struct sockaddr *bindaddr,
2174 socklen_t bindaddr_len,
2175 int *socket_error))
2177 tor_socket_t s;
2178 int inprogress = 0;
2179 const or_options_t *options = get_options();
2181 tor_assert(conn);
2182 tor_assert(sa);
2183 tor_assert(socket_error);
2185 if (net_is_completely_disabled()) {
2186 /* We should never even try to connect anyplace if the network is
2187 * completely shut off.
2189 * (We don't check net_is_disabled() here, since we still sometimes
2190 * want to open connections when we're in soft hibernation.)
2192 static ratelim_t disablenet_violated = RATELIM_INIT(30*60);
2193 *socket_error = SOCK_ERRNO(ENETUNREACH);
2194 log_fn_ratelim(&disablenet_violated, LOG_WARN, LD_BUG,
2195 "Tried to open a socket with DisableNetwork set.");
2196 tor_fragile_assert();
2197 return -1;
2200 const int protocol_family = sa->sa_family;
2201 const int proto = (sa->sa_family == AF_INET6 ||
2202 sa->sa_family == AF_INET) ? IPPROTO_TCP : 0;
2204 s = tor_open_socket_nonblocking(protocol_family, SOCK_STREAM, proto);
2205 if (! SOCKET_OK(s)) {
2207 * Early OOS handler calls; it matters if it's an exhaustion-related
2208 * error or not.
2210 *socket_error = tor_socket_errno(s);
2211 if (ERRNO_IS_RESOURCE_LIMIT(*socket_error)) {
2212 socket_failed_from_resource_exhaustion();
2213 connection_check_oos(get_n_open_sockets(), 1);
2214 } else {
2215 log_warn(LD_NET,"Error creating network socket: %s",
2216 tor_socket_strerror(*socket_error));
2217 connection_check_oos(get_n_open_sockets(), 0);
2219 return -1;
2222 if (make_socket_reuseable(s) < 0) {
2223 log_warn(LD_NET, "Error setting SO_REUSEADDR flag on new connection: %s",
2224 tor_socket_strerror(errno));
2227 if (bindaddr && bind(s, bindaddr, bindaddr_len) < 0) {
2228 *socket_error = tor_socket_errno(s);
2229 if (ERRNO_IS_EADDRINUSE(*socket_error)) {
2230 socket_failed_from_resource_exhaustion();
2231 connection_check_oos(get_n_open_sockets(), 1);
2232 } else {
2233 log_warn(LD_NET,"Error binding network socket: %s",
2234 tor_socket_strerror(*socket_error));
2235 connection_check_oos(get_n_open_sockets(), 0);
2237 tor_close_socket(s);
2238 return -1;
2242 * We've got the socket open and bound; give the OOS handler a chance to
2243 * check against configured maximum socket number, but tell it no exhaustion
2244 * failure.
2246 connection_check_oos(get_n_open_sockets(), 0);
2248 tor_assert(options);
2249 if (options->ConstrainedSockets)
2250 set_constrained_socket_buffers(s, (int)options->ConstrainedSockSize);
2252 if (connect(s, sa, sa_len) < 0) {
2253 int e = tor_socket_errno(s);
2254 if (!ERRNO_IS_CONN_EINPROGRESS(e)) {
2255 /* yuck. kill it. */
2256 *socket_error = e;
2257 log_info(LD_NET,
2258 "connect() to socket failed: %s",
2259 tor_socket_strerror(e));
2260 tor_close_socket(s);
2261 return -1;
2262 } else {
2263 inprogress = 1;
2267 note_connection(false /* outbound */, conn->socket_family);
2269 /* it succeeded. we're connected. */
2270 log_fn(inprogress ? LOG_DEBUG : LOG_INFO, LD_NET,
2271 "Connection to socket %s (sock "TOR_SOCKET_T_FORMAT").",
2272 inprogress ? "in progress" : "established", s);
2273 conn->s = s;
2274 if (connection_add_connecting(conn) < 0) {
2275 /* no space, forget it */
2276 *socket_error = SOCK_ERRNO(ENOBUFS);
2277 return -1;
2280 return inprogress ? 0 : 1;
2283 /* Log a message if connection attempt is made when IPv4 or IPv6 is disabled.
2284 * Log a less severe message if we couldn't conform to ClientPreferIPv6ORPort
2285 * or ClientPreferIPv6ORPort. */
2286 static void
2287 connection_connect_log_client_use_ip_version(const connection_t *conn)
2289 const or_options_t *options = get_options();
2291 /* Only clients care about ClientUseIPv4/6, bail out early on servers, and
2292 * on connections we don't care about */
2293 if (server_mode(options) || !conn || conn->type == CONN_TYPE_EXIT) {
2294 return;
2297 /* We're only prepared to log OR and DIR connections here */
2298 if (conn->type != CONN_TYPE_OR && conn->type != CONN_TYPE_DIR) {
2299 return;
2302 const int must_ipv4 = !reachable_addr_use_ipv6(options);
2303 const int must_ipv6 = (options->ClientUseIPv4 == 0);
2304 const int pref_ipv6 = (conn->type == CONN_TYPE_OR
2305 ? reachable_addr_prefer_ipv6_orport(options)
2306 : reachable_addr_prefer_ipv6_dirport(options));
2307 tor_addr_t real_addr;
2308 tor_addr_copy(&real_addr, &conn->addr);
2310 /* Check if we broke a mandatory address family restriction */
2311 if ((must_ipv4 && tor_addr_family(&real_addr) == AF_INET6)
2312 || (must_ipv6 && tor_addr_family(&real_addr) == AF_INET)) {
2313 static int logged_backtrace = 0;
2314 log_info(LD_BUG, "Outgoing %s connection to %s violated ClientUseIPv%s 0.",
2315 conn->type == CONN_TYPE_OR ? "OR" : "Dir",
2316 fmt_addr(&real_addr),
2317 options->ClientUseIPv4 == 0 ? "4" : "6");
2318 if (!logged_backtrace) {
2319 log_backtrace(LOG_INFO, LD_BUG, "Address came from");
2320 logged_backtrace = 1;
2324 /* Bridges are allowed to break IPv4/IPv6 ORPort preferences to connect to
2325 * the node's configured address when ClientPreferIPv6ORPort is auto */
2326 if (options->UseBridges && conn->type == CONN_TYPE_OR
2327 && options->ClientPreferIPv6ORPort == -1) {
2328 return;
2331 if (reachable_addr_use_ipv6(options)) {
2332 log_info(LD_NET, "Our outgoing connection is using IPv%d.",
2333 tor_addr_family(&real_addr) == AF_INET6 ? 6 : 4);
2336 /* Check if we couldn't satisfy an address family preference */
2337 if ((!pref_ipv6 && tor_addr_family(&real_addr) == AF_INET6)
2338 || (pref_ipv6 && tor_addr_family(&real_addr) == AF_INET)) {
2339 log_info(LD_NET, "Outgoing connection to %s doesn't satisfy "
2340 "ClientPreferIPv6%sPort %d, with ClientUseIPv4 %d, and "
2341 "reachable_addr_use_ipv6 %d (ClientUseIPv6 %d and UseBridges "
2342 "%d).",
2343 fmt_addr(&real_addr),
2344 conn->type == CONN_TYPE_OR ? "OR" : "Dir",
2345 conn->type == CONN_TYPE_OR ? options->ClientPreferIPv6ORPort
2346 : options->ClientPreferIPv6DirPort,
2347 options->ClientUseIPv4, reachable_addr_use_ipv6(options),
2348 options->ClientUseIPv6, options->UseBridges);
2352 /** Retrieve the outbound address depending on the protocol (IPv4 or IPv6)
2353 * and the connection type (relay, exit, ...)
2354 * Return a socket address or NULL in case nothing is configured.
2356 const tor_addr_t *
2357 conn_get_outbound_address(sa_family_t family,
2358 const or_options_t *options, unsigned int conn_type)
2360 const tor_addr_t *ext_addr = NULL;
2362 int fam_index;
2363 switch (family) {
2364 case AF_INET:
2365 fam_index = 0;
2366 break;
2367 case AF_INET6:
2368 fam_index = 1;
2369 break;
2370 default:
2371 return NULL;
2374 // If an exit connection, use the exit address (if present)
2375 if (conn_type == CONN_TYPE_EXIT) {
2376 if (!tor_addr_is_null(
2377 &options->OutboundBindAddresses[OUTBOUND_ADDR_EXIT][fam_index])) {
2378 ext_addr = &options->OutboundBindAddresses[OUTBOUND_ADDR_EXIT]
2379 [fam_index];
2380 } else if (!tor_addr_is_null(
2381 &options->OutboundBindAddresses[OUTBOUND_ADDR_ANY]
2382 [fam_index])) {
2383 ext_addr = &options->OutboundBindAddresses[OUTBOUND_ADDR_ANY]
2384 [fam_index];
2386 } else { // All non-exit connections
2387 if (!tor_addr_is_null(
2388 &options->OutboundBindAddresses[OUTBOUND_ADDR_OR][fam_index])) {
2389 ext_addr = &options->OutboundBindAddresses[OUTBOUND_ADDR_OR]
2390 [fam_index];
2391 } else if (!tor_addr_is_null(
2392 &options->OutboundBindAddresses[OUTBOUND_ADDR_ANY]
2393 [fam_index])) {
2394 ext_addr = &options->OutboundBindAddresses[OUTBOUND_ADDR_ANY]
2395 [fam_index];
2398 return ext_addr;
2401 /** Take conn, make a nonblocking socket; try to connect to
2402 * addr:port (port arrives in *host order*). If fail, return -1 and if
2403 * applicable put your best guess about errno into *<b>socket_error</b>.
2404 * Else assign s to conn-\>s: if connected return 1, if EAGAIN return 0.
2406 * addr:port can be different to conn->addr:conn->port if connecting through
2407 * a proxy.
2409 * address is used to make the logs useful.
2411 * On success, add conn to the list of polled connections.
2414 connection_connect(connection_t *conn, const char *address,
2415 const tor_addr_t *addr, uint16_t port, int *socket_error)
2417 struct sockaddr_storage addrbuf;
2418 struct sockaddr_storage bind_addr_ss;
2419 struct sockaddr *bind_addr = NULL;
2420 struct sockaddr *dest_addr;
2421 int dest_addr_len, bind_addr_len = 0;
2423 /* Log if we didn't stick to ClientUseIPv4/6 or ClientPreferIPv6OR/DirPort
2425 connection_connect_log_client_use_ip_version(conn);
2427 if (!tor_addr_is_loopback(addr)) {
2428 const tor_addr_t *ext_addr = NULL;
2429 ext_addr = conn_get_outbound_address(tor_addr_family(addr), get_options(),
2430 conn->type);
2431 if (ext_addr) {
2432 memset(&bind_addr_ss, 0, sizeof(bind_addr_ss));
2433 bind_addr_len = tor_addr_to_sockaddr(ext_addr, 0,
2434 (struct sockaddr *) &bind_addr_ss,
2435 sizeof(bind_addr_ss));
2436 if (bind_addr_len == 0) {
2437 log_warn(LD_NET,
2438 "Error converting OutboundBindAddress %s into sockaddr. "
2439 "Ignoring.", fmt_and_decorate_addr(ext_addr));
2440 } else {
2441 bind_addr = (struct sockaddr *)&bind_addr_ss;
2446 memset(&addrbuf,0,sizeof(addrbuf));
2447 dest_addr = (struct sockaddr*) &addrbuf;
2448 dest_addr_len = tor_addr_to_sockaddr(addr, port, dest_addr, sizeof(addrbuf));
2449 tor_assert(dest_addr_len > 0);
2451 log_debug(LD_NET, "Connecting to %s:%u.",
2452 escaped_safe_str_client(address), port);
2454 return connection_connect_sockaddr(conn, dest_addr, dest_addr_len,
2455 bind_addr, bind_addr_len, socket_error);
2458 #ifdef HAVE_SYS_UN_H
2460 /** Take conn, make a nonblocking socket; try to connect to
2461 * an AF_UNIX socket at socket_path. If fail, return -1 and if applicable
2462 * put your best guess about errno into *<b>socket_error</b>. Else assign s
2463 * to conn-\>s: if connected return 1, if EAGAIN return 0.
2465 * On success, add conn to the list of polled connections.
2468 connection_connect_unix(connection_t *conn, const char *socket_path,
2469 int *socket_error)
2471 struct sockaddr_un dest_addr;
2473 tor_assert(socket_path);
2475 /* Check that we'll be able to fit it into dest_addr later */
2476 if (strlen(socket_path) + 1 > sizeof(dest_addr.sun_path)) {
2477 log_warn(LD_NET,
2478 "Path %s is too long for an AF_UNIX socket\n",
2479 escaped_safe_str_client(socket_path));
2480 *socket_error = SOCK_ERRNO(ENAMETOOLONG);
2481 return -1;
2484 memset(&dest_addr, 0, sizeof(dest_addr));
2485 dest_addr.sun_family = AF_UNIX;
2486 strlcpy(dest_addr.sun_path, socket_path, sizeof(dest_addr.sun_path));
2488 log_debug(LD_NET,
2489 "Connecting to AF_UNIX socket at %s.",
2490 escaped_safe_str_client(socket_path));
2492 return connection_connect_sockaddr(conn,
2493 (struct sockaddr *)&dest_addr, sizeof(dest_addr),
2494 NULL, 0, socket_error);
2497 #endif /* defined(HAVE_SYS_UN_H) */
2499 /** Convert state number to string representation for logging purposes.
2501 static const char *
2502 connection_proxy_state_to_string(int state)
2504 static const char *unknown = "???";
2505 static const char *states[] = {
2506 "PROXY_NONE",
2507 "PROXY_INFANT",
2508 "PROXY_HTTPS_WANT_CONNECT_OK",
2509 "PROXY_SOCKS4_WANT_CONNECT_OK",
2510 "PROXY_SOCKS5_WANT_AUTH_METHOD_NONE",
2511 "PROXY_SOCKS5_WANT_AUTH_METHOD_RFC1929",
2512 "PROXY_SOCKS5_WANT_AUTH_RFC1929_OK",
2513 "PROXY_SOCKS5_WANT_CONNECT_OK",
2514 "PROXY_HAPROXY_WAIT_FOR_FLUSH",
2515 "PROXY_CONNECTED",
2518 CTASSERT(ARRAY_LENGTH(states) == PROXY_CONNECTED+1);
2520 if (state < PROXY_NONE || state > PROXY_CONNECTED)
2521 return unknown;
2523 return states[state];
2526 /** Returns the proxy type used by tor for a single connection, for
2527 * logging or high-level purposes. Don't use it to fill the
2528 * <b>proxy_type</b> field of or_connection_t; use the actual proxy
2529 * protocol instead.*/
2530 static int
2531 conn_get_proxy_type(const connection_t *conn)
2533 const or_options_t *options = get_options();
2535 if (options->ClientTransportPlugin) {
2536 /* If we have plugins configured *and* this addr/port is a known bridge
2537 * with a transport, then we should be PROXY_PLUGGABLE. */
2538 const transport_t *transport = NULL;
2539 int r;
2540 r = get_transport_by_bridge_addrport(&conn->addr, conn->port, &transport);
2541 if (r == 0 && transport)
2542 return PROXY_PLUGGABLE;
2545 /* In all other cases, we're using a global proxy. */
2546 if (options->HTTPSProxy)
2547 return PROXY_CONNECT;
2548 else if (options->Socks4Proxy)
2549 return PROXY_SOCKS4;
2550 else if (options->Socks5Proxy)
2551 return PROXY_SOCKS5;
2552 else if (options->TCPProxy) {
2553 /* The only supported protocol in TCPProxy is haproxy. */
2554 tor_assert(options->TCPProxyProtocol == TCP_PROXY_PROTOCOL_HAPROXY);
2555 return PROXY_HAPROXY;
2556 } else
2557 return PROXY_NONE;
2560 /* One byte for the version, one for the command, two for the
2561 port, and four for the addr... and, one more for the
2562 username NUL: */
2563 #define SOCKS4_STANDARD_BUFFER_SIZE (1 + 1 + 2 + 4 + 1)
2565 /** Write a proxy request of https to conn for conn->addr:conn->port,
2566 * authenticating with the auth details given in the configuration
2567 * (if available).
2569 * Returns -1 if conn->addr is incompatible with the proxy protocol, and
2570 * 0 otherwise.
2572 static int
2573 connection_https_proxy_connect(connection_t *conn)
2575 tor_assert(conn);
2577 const or_options_t *options = get_options();
2578 char buf[1024];
2579 char *base64_authenticator = NULL;
2580 const char *authenticator = options->HTTPSProxyAuthenticator;
2582 /* Send HTTP CONNECT and authentication (if available) in
2583 * one request */
2585 if (authenticator) {
2586 base64_authenticator = alloc_http_authenticator(authenticator);
2587 if (!base64_authenticator)
2588 log_warn(LD_OR, "Encoding https authenticator failed");
2591 if (base64_authenticator) {
2592 const char *addrport = fmt_addrport(&conn->addr, conn->port);
2593 tor_snprintf(buf, sizeof(buf), "CONNECT %s HTTP/1.1\r\n"
2594 "Host: %s\r\n"
2595 "Proxy-Authorization: Basic %s\r\n\r\n",
2596 addrport,
2597 addrport,
2598 base64_authenticator);
2599 tor_free(base64_authenticator);
2600 } else {
2601 tor_snprintf(buf, sizeof(buf), "CONNECT %s HTTP/1.0\r\n\r\n",
2602 fmt_addrport(&conn->addr, conn->port));
2605 connection_buf_add(buf, strlen(buf), conn);
2606 conn->proxy_state = PROXY_HTTPS_WANT_CONNECT_OK;
2608 return 0;
2611 /** Write a proxy request of socks4 to conn for conn->addr:conn->port.
2613 * Returns -1 if conn->addr is incompatible with the proxy protocol, and
2614 * 0 otherwise.
2616 static int
2617 connection_socks4_proxy_connect(connection_t *conn)
2619 tor_assert(conn);
2621 unsigned char *buf;
2622 uint16_t portn;
2623 uint32_t ip4addr;
2624 size_t buf_size = 0;
2625 char *socks_args_string = NULL;
2627 /* Send a SOCKS4 connect request */
2629 if (tor_addr_family(&conn->addr) != AF_INET) {
2630 log_warn(LD_NET, "SOCKS4 client is incompatible with IPv6");
2631 return -1;
2634 { /* If we are here because we are trying to connect to a
2635 pluggable transport proxy, check if we have any SOCKS
2636 arguments to transmit. If we do, compress all arguments to
2637 a single string in 'socks_args_string': */
2639 if (conn_get_proxy_type(conn) == PROXY_PLUGGABLE) {
2640 socks_args_string =
2641 pt_get_socks_args_for_proxy_addrport(&conn->addr, conn->port);
2642 if (socks_args_string)
2643 log_debug(LD_NET, "Sending out '%s' as our SOCKS argument string.",
2644 socks_args_string);
2648 { /* Figure out the buffer size we need for the SOCKS message: */
2650 buf_size = SOCKS4_STANDARD_BUFFER_SIZE;
2652 /* If we have a SOCKS argument string, consider its size when
2653 calculating the buffer size: */
2654 if (socks_args_string)
2655 buf_size += strlen(socks_args_string);
2658 buf = tor_malloc_zero(buf_size);
2660 ip4addr = tor_addr_to_ipv4n(&conn->addr);
2661 portn = htons(conn->port);
2663 buf[0] = 4; /* version */
2664 buf[1] = SOCKS_COMMAND_CONNECT; /* command */
2665 memcpy(buf + 2, &portn, 2); /* port */
2666 memcpy(buf + 4, &ip4addr, 4); /* addr */
2668 /* Next packet field is the userid. If we have pluggable
2669 transport SOCKS arguments, we have to embed them
2670 there. Otherwise, we use an empty userid. */
2671 if (socks_args_string) { /* place the SOCKS args string: */
2672 tor_assert(strlen(socks_args_string) > 0);
2673 tor_assert(buf_size >=
2674 SOCKS4_STANDARD_BUFFER_SIZE + strlen(socks_args_string));
2675 strlcpy((char *)buf + 8, socks_args_string, buf_size - 8);
2676 tor_free(socks_args_string);
2677 } else {
2678 buf[8] = 0; /* no userid */
2681 connection_buf_add((char *)buf, buf_size, conn);
2682 tor_free(buf);
2684 conn->proxy_state = PROXY_SOCKS4_WANT_CONNECT_OK;
2685 return 0;
2688 /** Write a proxy request of socks5 to conn for conn->addr:conn->port,
2689 * authenticating with the auth details given in the configuration
2690 * (if available).
2692 * Returns -1 if conn->addr is incompatible with the proxy protocol, and
2693 * 0 otherwise.
2695 static int
2696 connection_socks5_proxy_connect(connection_t *conn)
2698 tor_assert(conn);
2700 const or_options_t *options = get_options();
2701 unsigned char buf[4]; /* fields: vers, num methods, method list */
2703 /* Send a SOCKS5 greeting (connect request must wait) */
2705 buf[0] = 5; /* version */
2707 /* We have to use SOCKS5 authentication, if we have a
2708 Socks5ProxyUsername or if we want to pass arguments to our
2709 pluggable transport proxy: */
2710 if ((options->Socks5ProxyUsername) ||
2711 (conn_get_proxy_type(conn) == PROXY_PLUGGABLE &&
2712 (get_socks_args_by_bridge_addrport(&conn->addr, conn->port)))) {
2713 /* number of auth methods */
2714 buf[1] = 2;
2715 buf[2] = 0x00; /* no authentication */
2716 buf[3] = 0x02; /* rfc1929 Username/Passwd auth */
2717 conn->proxy_state = PROXY_SOCKS5_WANT_AUTH_METHOD_RFC1929;
2718 } else {
2719 buf[1] = 1;
2720 buf[2] = 0x00; /* no authentication */
2721 conn->proxy_state = PROXY_SOCKS5_WANT_AUTH_METHOD_NONE;
2724 connection_buf_add((char *)buf, 2 + buf[1], conn);
2725 return 0;
2728 /** Write a proxy request of haproxy to conn for conn->addr:conn->port.
2730 * Returns -1 if conn->addr is incompatible with the proxy protocol, and
2731 * 0 otherwise.
2733 static int
2734 connection_haproxy_proxy_connect(connection_t *conn)
2736 int ret = 0;
2737 tor_addr_port_t *addr_port = tor_addr_port_new(&conn->addr, conn->port);
2738 char *buf = haproxy_format_proxy_header_line(addr_port);
2740 if (buf == NULL) {
2741 ret = -1;
2742 goto done;
2745 connection_buf_add(buf, strlen(buf), conn);
2746 /* In haproxy, we don't have to wait for the response, but we wait for ack.
2747 * So we can set the state to be PROXY_HAPROXY_WAIT_FOR_FLUSH. */
2748 conn->proxy_state = PROXY_HAPROXY_WAIT_FOR_FLUSH;
2750 ret = 0;
2751 done:
2752 tor_free(buf);
2753 tor_free(addr_port);
2754 return ret;
2757 /** Write a proxy request of <b>type</b> (socks4, socks5, https, haproxy)
2758 * to conn for conn->addr:conn->port, authenticating with the auth details
2759 * given in the configuration (if available). SOCKS 5 and HTTP CONNECT
2760 * proxies support authentication.
2762 * Returns -1 if conn->addr is incompatible with the proxy protocol, and
2763 * 0 otherwise.
2765 * Use connection_read_proxy_handshake() to complete the handshake.
2768 connection_proxy_connect(connection_t *conn, int type)
2770 int ret = 0;
2772 tor_assert(conn);
2774 switch (type) {
2775 case PROXY_CONNECT:
2776 ret = connection_https_proxy_connect(conn);
2777 break;
2779 case PROXY_SOCKS4:
2780 ret = connection_socks4_proxy_connect(conn);
2781 break;
2783 case PROXY_SOCKS5:
2784 ret = connection_socks5_proxy_connect(conn);
2785 break;
2787 case PROXY_HAPROXY:
2788 ret = connection_haproxy_proxy_connect(conn);
2789 break;
2791 default:
2792 log_err(LD_BUG, "Invalid proxy protocol, %d", type);
2793 tor_fragile_assert();
2794 ret = -1;
2795 break;
2798 if (ret == 0) {
2799 log_debug(LD_NET, "set state %s",
2800 connection_proxy_state_to_string(conn->proxy_state));
2803 return ret;
2806 /** Read conn's inbuf. If the http response from the proxy is all
2807 * here, make sure it's good news, then return 1. If it's bad news,
2808 * return -1. Else return 0 and hope for better luck next time.
2810 static int
2811 connection_read_https_proxy_response(connection_t *conn)
2813 char *headers;
2814 char *reason=NULL;
2815 int status_code;
2816 time_t date_header;
2818 switch (fetch_from_buf_http(conn->inbuf,
2819 &headers, MAX_HEADERS_SIZE,
2820 NULL, NULL, 10000, 0)) {
2821 case -1: /* overflow */
2822 log_warn(LD_PROTOCOL,
2823 "Your https proxy sent back an oversized response. Closing.");
2824 return -1;
2825 case 0:
2826 log_info(LD_NET,"https proxy response not all here yet. Waiting.");
2827 return 0;
2828 /* case 1, fall through */
2831 if (parse_http_response(headers, &status_code, &date_header,
2832 NULL, &reason) < 0) {
2833 log_warn(LD_NET,
2834 "Unparseable headers from proxy (%s). Closing.",
2835 connection_describe(conn));
2836 tor_free(headers);
2837 return -1;
2839 tor_free(headers);
2840 if (!reason) reason = tor_strdup("[no reason given]");
2842 if (status_code == 200) {
2843 log_info(LD_NET,
2844 "HTTPS connect for %s successful! (200 %s) Starting TLS.",
2845 connection_describe(conn), escaped(reason));
2846 tor_free(reason);
2847 return 1;
2849 /* else, bad news on the status code */
2850 switch (status_code) {
2851 case 403:
2852 log_warn(LD_NET,
2853 "The https proxy refused to allow connection to %s "
2854 "(status code %d, %s). Closing.",
2855 conn->address, status_code, escaped(reason));
2856 break;
2857 default:
2858 log_warn(LD_NET,
2859 "The https proxy sent back an unexpected status code %d (%s). "
2860 "Closing.",
2861 status_code, escaped(reason));
2862 break;
2864 tor_free(reason);
2865 return -1;
2868 /** Send SOCKS5 CONNECT command to <b>conn</b>, copying <b>conn->addr</b>
2869 * and <b>conn->port</b> into the request.
2871 static void
2872 connection_send_socks5_connect(connection_t *conn)
2874 unsigned char buf[1024];
2875 size_t reqsize = 6;
2876 uint16_t port = htons(conn->port);
2878 buf[0] = 5; /* version */
2879 buf[1] = SOCKS_COMMAND_CONNECT; /* command */
2880 buf[2] = 0; /* reserved */
2882 if (tor_addr_family(&conn->addr) == AF_INET) {
2883 uint32_t addr = tor_addr_to_ipv4n(&conn->addr);
2885 buf[3] = 1;
2886 reqsize += 4;
2887 memcpy(buf + 4, &addr, 4);
2888 memcpy(buf + 8, &port, 2);
2889 } else { /* AF_INET6 */
2890 buf[3] = 4;
2891 reqsize += 16;
2892 memcpy(buf + 4, tor_addr_to_in6_addr8(&conn->addr), 16);
2893 memcpy(buf + 20, &port, 2);
2896 connection_buf_add((char *)buf, reqsize, conn);
2898 conn->proxy_state = PROXY_SOCKS5_WANT_CONNECT_OK;
2901 /** Wrapper around fetch_from_buf_socks_client: see that functions
2902 * for documentation of its behavior. */
2903 static int
2904 connection_fetch_from_buf_socks_client(connection_t *conn,
2905 int state, char **reason)
2907 return fetch_from_buf_socks_client(conn->inbuf, state, reason);
2910 /** Call this from connection_*_process_inbuf() to advance the proxy
2911 * handshake.
2913 * No matter what proxy protocol is used, if this function returns 1, the
2914 * handshake is complete, and the data remaining on inbuf may contain the
2915 * start of the communication with the requested server.
2917 * Returns 0 if the current buffer contains an incomplete response, and -1
2918 * on error.
2921 connection_read_proxy_handshake(connection_t *conn)
2923 int ret = 0;
2924 char *reason = NULL;
2926 log_debug(LD_NET, "enter state %s",
2927 connection_proxy_state_to_string(conn->proxy_state));
2929 switch (conn->proxy_state) {
2930 case PROXY_HTTPS_WANT_CONNECT_OK:
2931 ret = connection_read_https_proxy_response(conn);
2932 if (ret == 1)
2933 conn->proxy_state = PROXY_CONNECTED;
2934 break;
2936 case PROXY_SOCKS4_WANT_CONNECT_OK:
2937 ret = connection_fetch_from_buf_socks_client(conn,
2938 conn->proxy_state,
2939 &reason);
2940 if (ret == 1)
2941 conn->proxy_state = PROXY_CONNECTED;
2942 break;
2944 case PROXY_SOCKS5_WANT_AUTH_METHOD_NONE:
2945 ret = connection_fetch_from_buf_socks_client(conn,
2946 conn->proxy_state,
2947 &reason);
2948 /* no auth needed, do connect */
2949 if (ret == 1) {
2950 connection_send_socks5_connect(conn);
2951 ret = 0;
2953 break;
2955 case PROXY_SOCKS5_WANT_AUTH_METHOD_RFC1929:
2956 ret = connection_fetch_from_buf_socks_client(conn,
2957 conn->proxy_state,
2958 &reason);
2960 /* send auth if needed, otherwise do connect */
2961 if (ret == 1) {
2962 connection_send_socks5_connect(conn);
2963 ret = 0;
2964 } else if (ret == 2) {
2965 unsigned char buf[1024];
2966 size_t reqsize, usize, psize;
2967 const char *user, *pass;
2968 char *socks_args_string = NULL;
2970 if (conn_get_proxy_type(conn) == PROXY_PLUGGABLE) {
2971 socks_args_string =
2972 pt_get_socks_args_for_proxy_addrport(&conn->addr, conn->port);
2973 if (!socks_args_string) {
2974 log_warn(LD_NET, "Could not create SOCKS args string for PT.");
2975 ret = -1;
2976 break;
2979 log_debug(LD_NET, "PT SOCKS5 arguments: %s", socks_args_string);
2980 tor_assert(strlen(socks_args_string) > 0);
2981 tor_assert(strlen(socks_args_string) <= MAX_SOCKS5_AUTH_SIZE_TOTAL);
2983 if (strlen(socks_args_string) > MAX_SOCKS5_AUTH_FIELD_SIZE) {
2984 user = socks_args_string;
2985 usize = MAX_SOCKS5_AUTH_FIELD_SIZE;
2986 pass = socks_args_string + MAX_SOCKS5_AUTH_FIELD_SIZE;
2987 psize = strlen(socks_args_string) - MAX_SOCKS5_AUTH_FIELD_SIZE;
2988 } else {
2989 user = socks_args_string;
2990 usize = strlen(socks_args_string);
2991 pass = "\0";
2992 psize = 1;
2994 } else if (get_options()->Socks5ProxyUsername) {
2995 user = get_options()->Socks5ProxyUsername;
2996 pass = get_options()->Socks5ProxyPassword;
2997 tor_assert(user && pass);
2998 usize = strlen(user);
2999 psize = strlen(pass);
3000 } else {
3001 log_err(LD_BUG, "We entered %s for no reason!", __func__);
3002 tor_fragile_assert();
3003 ret = -1;
3004 break;
3007 /* Username and password lengths should have been checked
3008 above and during torrc parsing. */
3009 tor_assert(usize <= MAX_SOCKS5_AUTH_FIELD_SIZE &&
3010 psize <= MAX_SOCKS5_AUTH_FIELD_SIZE);
3011 reqsize = 3 + usize + psize;
3013 buf[0] = 1; /* negotiation version */
3014 buf[1] = usize;
3015 memcpy(buf + 2, user, usize);
3016 buf[2 + usize] = psize;
3017 memcpy(buf + 3 + usize, pass, psize);
3019 if (socks_args_string)
3020 tor_free(socks_args_string);
3022 connection_buf_add((char *)buf, reqsize, conn);
3024 conn->proxy_state = PROXY_SOCKS5_WANT_AUTH_RFC1929_OK;
3025 ret = 0;
3027 break;
3029 case PROXY_SOCKS5_WANT_AUTH_RFC1929_OK:
3030 ret = connection_fetch_from_buf_socks_client(conn,
3031 conn->proxy_state,
3032 &reason);
3033 /* send the connect request */
3034 if (ret == 1) {
3035 connection_send_socks5_connect(conn);
3036 ret = 0;
3038 break;
3040 case PROXY_SOCKS5_WANT_CONNECT_OK:
3041 ret = connection_fetch_from_buf_socks_client(conn,
3042 conn->proxy_state,
3043 &reason);
3044 if (ret == 1)
3045 conn->proxy_state = PROXY_CONNECTED;
3046 break;
3048 default:
3049 log_err(LD_BUG, "Invalid proxy_state for reading, %d",
3050 conn->proxy_state);
3051 tor_fragile_assert();
3052 ret = -1;
3053 break;
3056 log_debug(LD_NET, "leaving state %s",
3057 connection_proxy_state_to_string(conn->proxy_state));
3059 if (ret < 0) {
3060 if (reason) {
3061 log_warn(LD_NET, "Proxy Client: unable to connect %s (%s)",
3062 connection_describe(conn), escaped(reason));
3063 tor_free(reason);
3064 } else {
3065 log_warn(LD_NET, "Proxy Client: unable to connect %s",
3066 connection_describe(conn));
3068 } else if (ret == 1) {
3069 log_info(LD_NET, "Proxy Client: %s successful",
3070 connection_describe(conn));
3073 return ret;
3076 /** Given a list of listener connections in <b>old_conns</b>, and list of
3077 * port_cfg_t entries in <b>ports</b>, open a new listener for every port in
3078 * <b>ports</b> that does not already have a listener in <b>old_conns</b>.
3080 * Remove from <b>old_conns</b> every connection that has a corresponding
3081 * entry in <b>ports</b>. Add to <b>new_conns</b> new every connection we
3082 * launch. If we may need to perform socket rebind when creating new
3083 * listener that replaces old one, create a <b>listener_replacement_t</b>
3084 * struct for affected pair and add it to <b>replacements</b>.
3086 * If <b>control_listeners_only</b> is true, then we only open control
3087 * listeners, and we do not remove any noncontrol listeners from
3088 * old_conns.
3090 * Return 0 on success, -1 on failure.
3092 static int
3093 retry_listener_ports(smartlist_t *old_conns,
3094 const smartlist_t *ports,
3095 smartlist_t *new_conns,
3096 smartlist_t *replacements,
3097 int control_listeners_only)
3099 #ifndef ENABLE_LISTENER_REBIND
3100 (void)replacements;
3101 #endif
3103 smartlist_t *launch = smartlist_new();
3104 int r = 0;
3106 if (control_listeners_only) {
3107 SMARTLIST_FOREACH(ports, port_cfg_t *, p, {
3108 if (p->type == CONN_TYPE_CONTROL_LISTENER)
3109 smartlist_add(launch, p);
3111 } else {
3112 smartlist_add_all(launch, ports);
3115 /* Iterate through old_conns, comparing it to launch: remove from both lists
3116 * each pair of elements that corresponds to the same port. */
3117 SMARTLIST_FOREACH_BEGIN(old_conns, connection_t *, conn) {
3118 const port_cfg_t *found_port = NULL;
3120 /* Okay, so this is a listener. Is it configured? */
3121 /* That is, is it either: 1) exact match - address and port
3122 * pair match exactly between old listener and new port; or 2)
3123 * wildcard match - port matches exactly, but *one* of the
3124 * addresses is wildcard (0.0.0.0 or ::)?
3126 SMARTLIST_FOREACH_BEGIN(launch, const port_cfg_t *, wanted) {
3127 if (conn->type != wanted->type)
3128 continue;
3129 if ((conn->socket_family != AF_UNIX && wanted->is_unix_addr) ||
3130 (conn->socket_family == AF_UNIX && ! wanted->is_unix_addr))
3131 continue;
3133 if (wanted->server_cfg.no_listen)
3134 continue; /* We don't want to open a listener for this one */
3136 if (wanted->is_unix_addr) {
3137 if (conn->socket_family == AF_UNIX &&
3138 !strcmp(wanted->unix_addr, conn->address)) {
3139 found_port = wanted;
3140 break;
3142 } else {
3143 /* Numeric values of old and new port match exactly. */
3144 const int port_matches_exact = (wanted->port == conn->port);
3145 /* Ports match semantically - either their specific values
3146 match exactly, or new port is 'auto'.
3148 const int port_matches = (wanted->port == CFG_AUTO_PORT ||
3149 port_matches_exact);
3151 if (port_matches && tor_addr_eq(&wanted->addr, &conn->addr)) {
3152 found_port = wanted;
3153 break;
3155 #ifdef ENABLE_LISTENER_REBIND
3156 /* Rebinding may be needed if all of the following are true:
3157 * 1) Address family is the same in old and new listeners.
3158 * 2) Port number matches exactly (numeric value is the same).
3159 * 3) *One* of listeners (either old one or new one) has a
3160 * wildcard IP address (0.0.0.0 or [::]).
3162 * These are the exact conditions for a first bind() syscall
3163 * to fail with EADDRINUSE.
3165 const int may_need_rebind =
3166 tor_addr_family(&wanted->addr) == tor_addr_family(&conn->addr) &&
3167 port_matches_exact && bool_neq(tor_addr_is_null(&wanted->addr),
3168 tor_addr_is_null(&conn->addr));
3169 if (replacements && may_need_rebind) {
3170 listener_replacement_t *replacement =
3171 tor_malloc(sizeof(listener_replacement_t));
3173 replacement->old_conn = conn;
3174 replacement->new_port = wanted;
3175 smartlist_add(replacements, replacement);
3177 SMARTLIST_DEL_CURRENT(launch, wanted);
3178 SMARTLIST_DEL_CURRENT(old_conns, conn);
3179 break;
3181 #endif /* defined(ENABLE_LISTENER_REBIND) */
3183 } SMARTLIST_FOREACH_END(wanted);
3185 if (found_port) {
3186 /* This listener is already running; we don't need to launch it. */
3187 //log_debug(LD_NET, "Already have %s on %s:%d",
3188 // conn_type_to_string(found_port->type), conn->address, conn->port);
3189 smartlist_remove(launch, found_port);
3190 /* And we can remove the connection from old_conns too. */
3191 SMARTLIST_DEL_CURRENT(old_conns, conn);
3193 } SMARTLIST_FOREACH_END(conn);
3195 /* Now open all the listeners that are configured but not opened. */
3196 SMARTLIST_FOREACH_BEGIN(launch, const port_cfg_t *, port) {
3197 int skip = 0;
3198 connection_t *conn = connection_listener_new_for_port(port, &skip, NULL);
3200 if (conn && new_conns)
3201 smartlist_add(new_conns, conn);
3202 else if (!skip)
3203 r = -1;
3204 } SMARTLIST_FOREACH_END(port);
3206 smartlist_free(launch);
3208 return r;
3211 /** Launch listeners for each port you should have open. Only launch
3212 * listeners who are not already open, and only close listeners we no longer
3213 * want.
3215 * Add all new connections to <b>new_conns</b>.
3217 * If <b>close_all_noncontrol</b> is true, then we only open control
3218 * listeners, and we close all other listeners.
3221 retry_all_listeners(smartlist_t *new_conns, int close_all_noncontrol)
3223 smartlist_t *listeners = smartlist_new();
3224 smartlist_t *replacements = smartlist_new();
3225 const or_options_t *options = get_options();
3226 int retval = 0;
3227 const uint16_t old_or_port = routerconf_find_or_port(options, AF_INET);
3228 const uint16_t old_or_port_ipv6 =
3229 routerconf_find_or_port(options,AF_INET6);
3230 const uint16_t old_dir_port = routerconf_find_dir_port(options, 0);
3232 SMARTLIST_FOREACH_BEGIN(get_connection_array(), connection_t *, conn) {
3233 if (connection_is_listener(conn) && !conn->marked_for_close)
3234 smartlist_add(listeners, conn);
3235 } SMARTLIST_FOREACH_END(conn);
3237 if (retry_listener_ports(listeners,
3238 get_configured_ports(),
3239 new_conns,
3240 replacements,
3241 close_all_noncontrol) < 0)
3242 retval = -1;
3244 #ifdef ENABLE_LISTENER_REBIND
3245 if (smartlist_len(replacements))
3246 log_debug(LD_NET, "%d replacements - starting rebinding loop.",
3247 smartlist_len(replacements));
3249 SMARTLIST_FOREACH_BEGIN(replacements, listener_replacement_t *, r) {
3250 int addr_in_use = 0;
3251 int skip = 0;
3253 tor_assert(r->new_port);
3254 tor_assert(r->old_conn);
3256 connection_t *new_conn =
3257 connection_listener_new_for_port(r->new_port, &skip, &addr_in_use);
3258 connection_t *old_conn = r->old_conn;
3260 if (skip) {
3261 log_debug(LD_NET, "Skipping creating new listener for %s",
3262 connection_describe(old_conn));
3263 continue;
3266 connection_close_immediate(old_conn);
3267 connection_mark_for_close(old_conn);
3269 if (addr_in_use) {
3270 new_conn = connection_listener_new_for_port(r->new_port,
3271 &skip, &addr_in_use);
3274 /* There are many reasons why we can't open a new listener port so in case
3275 * we hit those, bail early so tor can stop. */
3276 if (!new_conn) {
3277 log_warn(LD_NET, "Unable to create listener port: %s:%d",
3278 fmt_and_decorate_addr(&r->new_port->addr), r->new_port->port);
3279 retval = -1;
3280 break;
3283 smartlist_add(new_conns, new_conn);
3285 char *old_desc = tor_strdup(connection_describe(old_conn));
3286 log_notice(LD_NET, "Closed no-longer-configured %s "
3287 "(replaced by %s)",
3288 old_desc, connection_describe(new_conn));
3289 tor_free(old_desc);
3290 } SMARTLIST_FOREACH_END(r);
3291 #endif /* defined(ENABLE_LISTENER_REBIND) */
3293 /* Any members that were still in 'listeners' don't correspond to
3294 * any configured port. Kill 'em. */
3295 SMARTLIST_FOREACH_BEGIN(listeners, connection_t *, conn) {
3296 log_notice(LD_NET, "Closing no-longer-configured %s on %s:%d",
3297 conn_type_to_string(conn->type),
3298 fmt_and_decorate_addr(&conn->addr), conn->port);
3299 connection_close_immediate(conn);
3300 connection_mark_for_close(conn);
3301 } SMARTLIST_FOREACH_END(conn);
3303 smartlist_free(listeners);
3304 /* Cleanup any remaining listener replacement. */
3305 SMARTLIST_FOREACH(replacements, listener_replacement_t *, r, tor_free(r));
3306 smartlist_free(replacements);
3308 if (old_or_port != routerconf_find_or_port(options, AF_INET) ||
3309 old_or_port_ipv6 != routerconf_find_or_port(options, AF_INET6) ||
3310 old_dir_port != routerconf_find_dir_port(options, 0)) {
3311 /* Our chosen ORPort or DirPort is not what it used to be: the
3312 * descriptor we had (if any) should be regenerated. (We won't
3313 * automatically notice this because of changes in the option,
3314 * since the value could be "auto".) */
3315 mark_my_descriptor_dirty("Chosen Or/DirPort changed");
3318 return retval;
3321 /** Mark every listener of type other than CONTROL_LISTENER to be closed. */
3322 void
3323 connection_mark_all_noncontrol_listeners(void)
3325 SMARTLIST_FOREACH_BEGIN(get_connection_array(), connection_t *, conn) {
3326 if (conn->marked_for_close)
3327 continue;
3328 if (conn->type == CONN_TYPE_CONTROL_LISTENER)
3329 continue;
3330 if (connection_is_listener(conn))
3331 connection_mark_for_close(conn);
3332 } SMARTLIST_FOREACH_END(conn);
3335 /** Mark every external connection not used for controllers for close. */
3336 void
3337 connection_mark_all_noncontrol_connections(void)
3339 SMARTLIST_FOREACH_BEGIN(get_connection_array(), connection_t *, conn) {
3340 if (conn->marked_for_close)
3341 continue;
3342 switch (conn->type) {
3343 case CONN_TYPE_CONTROL_LISTENER:
3344 case CONN_TYPE_CONTROL:
3345 break;
3346 case CONN_TYPE_AP:
3347 connection_mark_unattached_ap(TO_ENTRY_CONN(conn),
3348 END_STREAM_REASON_HIBERNATING);
3349 break;
3350 case CONN_TYPE_OR:
3352 or_connection_t *orconn = TO_OR_CONN(conn);
3353 if (orconn->chan) {
3354 connection_or_close_normally(orconn, 0);
3355 } else {
3357 * There should have been one, but mark for close and hope
3358 * for the best..
3360 connection_mark_for_close(conn);
3363 break;
3364 default:
3365 connection_mark_for_close(conn);
3366 break;
3368 } SMARTLIST_FOREACH_END(conn);
3371 /** Return 1 if we should apply rate limiting to <b>conn</b>, and 0
3372 * otherwise.
3373 * Right now this just checks if it's an internal IP address or an
3374 * internal connection. We also should, but don't, check if the connection
3375 * uses pluggable transports, since we should then limit it even if it
3376 * comes from an internal IP address. */
3377 static int
3378 connection_is_rate_limited(const connection_t *conn)
3380 const or_options_t *options = get_options();
3381 if (conn->linked)
3382 return 0; /* Internal connection */
3383 else if (! options->CountPrivateBandwidth &&
3384 ! conn->always_rate_limit_as_remote &&
3385 (tor_addr_family(&conn->addr) == AF_UNSPEC || /* no address */
3386 tor_addr_family(&conn->addr) == AF_UNIX || /* no address */
3387 tor_addr_is_internal(&conn->addr, 0)))
3388 return 0; /* Internal address */
3389 else
3390 return 1;
3393 /** When was either global write bucket last empty? If this was recent, then
3394 * we're probably low on bandwidth, and we should be stingy with our bandwidth
3395 * usage. */
3396 static time_t write_buckets_last_empty_at = -100;
3398 /** How many seconds of no active local circuits will make the
3399 * connection revert to the "relayed" bandwidth class? */
3400 #define CLIENT_IDLE_TIME_FOR_PRIORITY 30
3402 /** Return 1 if <b>conn</b> should use tokens from the "relayed"
3403 * bandwidth rates, else 0. Currently, only OR conns with bandwidth
3404 * class 1, and directory conns that are serving data out, count.
3406 static int
3407 connection_counts_as_relayed_traffic(connection_t *conn, time_t now)
3409 if (conn->type == CONN_TYPE_OR &&
3410 connection_or_client_used(TO_OR_CONN(conn)) +
3411 CLIENT_IDLE_TIME_FOR_PRIORITY < now)
3412 return 1;
3413 if (conn->type == CONN_TYPE_DIR && DIR_CONN_IS_SERVER(conn))
3414 return 1;
3415 return 0;
3418 /** Helper function to decide how many bytes out of <b>global_bucket</b>
3419 * we're willing to use for this transaction. <b>base</b> is the size
3420 * of a cell on the network; <b>priority</b> says whether we should
3421 * write many of them or just a few; and <b>conn_bucket</b> (if
3422 * non-negative) provides an upper limit for our answer. */
3423 static ssize_t
3424 connection_bucket_get_share(int base, int priority,
3425 ssize_t global_bucket_val, ssize_t conn_bucket)
3427 ssize_t at_most;
3428 ssize_t num_bytes_high = (priority ? 32 : 16) * base;
3429 ssize_t num_bytes_low = (priority ? 4 : 2) * base;
3431 /* Do a rudimentary limiting so one circuit can't hog a connection.
3432 * Pick at most 32 cells, at least 4 cells if possible, and if we're in
3433 * the middle pick 1/8 of the available bandwidth. */
3434 at_most = global_bucket_val / 8;
3435 at_most -= (at_most % base); /* round down */
3436 if (at_most > num_bytes_high) /* 16 KB, or 8 KB for low-priority */
3437 at_most = num_bytes_high;
3438 else if (at_most < num_bytes_low) /* 2 KB, or 1 KB for low-priority */
3439 at_most = num_bytes_low;
3441 if (at_most > global_bucket_val)
3442 at_most = global_bucket_val;
3444 if (conn_bucket >= 0 && at_most > conn_bucket)
3445 at_most = conn_bucket;
3447 if (at_most < 0)
3448 return 0;
3449 return at_most;
3452 /** How many bytes at most can we read onto this connection? */
3453 static ssize_t
3454 connection_bucket_read_limit(connection_t *conn, time_t now)
3456 int base = RELAY_PAYLOAD_SIZE;
3457 int priority = conn->type != CONN_TYPE_DIR;
3458 ssize_t conn_bucket = -1;
3459 size_t global_bucket_val = token_bucket_rw_get_read(&global_bucket);
3460 if (global_bucket_val == 0) {
3461 /* We reached our global read limit: count this as an overload.
3463 * The token bucket is always initialized (see connection_bucket_init() and
3464 * options_validate_relay_bandwidth()) and hence we can assume that if the
3465 * token ever hits zero, it's a limit that got popped and not the bucket
3466 * being uninitialized.
3468 rep_hist_note_overload(OVERLOAD_READ);
3471 if (connection_speaks_cells(conn)) {
3472 or_connection_t *or_conn = TO_OR_CONN(conn);
3473 if (conn->state == OR_CONN_STATE_OPEN)
3474 conn_bucket = token_bucket_rw_get_read(&or_conn->bucket);
3475 base = get_cell_network_size(or_conn->wide_circ_ids);
3478 /* Edge connection have their own read bucket due to flow control being able
3479 * to set a rate limit for them. However, for exit connections, we still need
3480 * to honor the global bucket as well. */
3481 if (CONN_IS_EDGE(conn)) {
3482 const edge_connection_t *edge_conn = CONST_TO_EDGE_CONN(conn);
3483 conn_bucket = token_bucket_rw_get_read(&edge_conn->bucket);
3484 if (conn->type == CONN_TYPE_EXIT) {
3485 /* Decide between our limit and the global one. */
3486 goto end;
3488 return conn_bucket;
3491 if (!connection_is_rate_limited(conn)) {
3492 /* be willing to read on local conns even if our buckets are empty */
3493 return conn_bucket>=0 ? conn_bucket : 1<<14;
3496 if (connection_counts_as_relayed_traffic(conn, now)) {
3497 size_t relayed = token_bucket_rw_get_read(&global_relayed_bucket);
3498 global_bucket_val = MIN(global_bucket_val, relayed);
3501 end:
3502 return connection_bucket_get_share(base, priority,
3503 global_bucket_val, conn_bucket);
3506 /** How many bytes at most can we write onto this connection? */
3507 ssize_t
3508 connection_bucket_write_limit(connection_t *conn, time_t now)
3510 int base = RELAY_PAYLOAD_SIZE;
3511 int priority = conn->type != CONN_TYPE_DIR;
3512 size_t conn_bucket = buf_datalen(conn->outbuf);
3513 size_t global_bucket_val = token_bucket_rw_get_write(&global_bucket);
3514 if (global_bucket_val == 0) {
3515 /* We reached our global write limit: We should count this as an overload.
3516 * See above function for more information */
3517 rep_hist_note_overload(OVERLOAD_WRITE);
3520 if (!connection_is_rate_limited(conn)) {
3521 /* be willing to write to local conns even if our buckets are empty */
3522 return conn_bucket;
3525 if (connection_speaks_cells(conn)) {
3526 /* use the per-conn write limit if it's lower */
3527 or_connection_t *or_conn = TO_OR_CONN(conn);
3528 if (conn->state == OR_CONN_STATE_OPEN)
3529 conn_bucket = MIN(conn_bucket,
3530 token_bucket_rw_get_write(&or_conn->bucket));
3531 base = get_cell_network_size(or_conn->wide_circ_ids);
3534 if (connection_counts_as_relayed_traffic(conn, now)) {
3535 size_t relayed = token_bucket_rw_get_write(&global_relayed_bucket);
3536 global_bucket_val = MIN(global_bucket_val, relayed);
3539 return connection_bucket_get_share(base, priority,
3540 global_bucket_val, conn_bucket);
3543 /** Return true iff the global write buckets are low enough that we
3544 * shouldn't send <b>attempt</b> bytes of low-priority directory stuff
3545 * out to <b>conn</b>.
3547 * If we are a directory authority, always answer dir requests thus true is
3548 * always returned.
3550 * Note: There are a lot of parameters we could use here:
3551 * - global_relayed_write_bucket. Low is bad.
3552 * - global_write_bucket. Low is bad.
3553 * - bandwidthrate. Low is bad.
3554 * - bandwidthburst. Not a big factor?
3555 * - attempt. High is bad.
3556 * - total bytes queued on outbufs. High is bad. But I'm wary of
3557 * using this, since a few slow-flushing queues will pump up the
3558 * number without meaning what we meant to mean. What we really
3559 * mean is "total directory bytes added to outbufs recently", but
3560 * that's harder to quantify and harder to keep track of.
3562 bool
3563 connection_dir_is_global_write_low(const connection_t *conn, size_t attempt)
3565 size_t smaller_bucket =
3566 MIN(token_bucket_rw_get_write(&global_bucket),
3567 token_bucket_rw_get_write(&global_relayed_bucket));
3569 /* Special case for authorities (directory only). */
3570 if (authdir_mode_v3(get_options())) {
3571 /* Are we configured to possibly reject requests under load? */
3572 if (!dirauth_should_reject_requests_under_load()) {
3573 /* Answer request no matter what. */
3574 return false;
3576 /* Always answer requests from a known relay which includes the other
3577 * authorities. The following looks up the addresses for relays that we
3578 * have their descriptor _and_ any configured trusted directories. */
3579 if (nodelist_probably_contains_address(&conn->addr)) {
3580 return false;
3584 if (!connection_is_rate_limited(conn))
3585 return false; /* local conns don't get limited */
3587 if (smaller_bucket < attempt)
3588 return true; /* not enough space. */
3591 const time_t diff = approx_time() - write_buckets_last_empty_at;
3592 if (diff <= 1)
3593 return true; /* we're already hitting our limits, no more please */
3595 return false;
3598 /** When did we last tell the accounting subsystem about transmitted
3599 * bandwidth? */
3600 static time_t last_recorded_accounting_at = 0;
3602 /** Helper: adjusts our bandwidth history and informs the controller as
3603 * appropriate, given that we have just read <b>num_read</b> bytes and written
3604 * <b>num_written</b> bytes on <b>conn</b>. */
3605 static void
3606 record_num_bytes_transferred_impl(connection_t *conn,
3607 time_t now, size_t num_read, size_t num_written)
3609 /* Count bytes of answering direct and tunneled directory requests */
3610 if (conn->type == CONN_TYPE_DIR && conn->purpose == DIR_PURPOSE_SERVER) {
3611 if (num_read > 0)
3612 bwhist_note_dir_bytes_read(num_read, now);
3613 if (num_written > 0)
3614 bwhist_note_dir_bytes_written(num_written, now);
3617 /* Linked connections and internal IPs aren't counted for statistics or
3618 * accounting:
3619 * - counting linked connections would double-count BEGINDIR bytes, because
3620 * they are sent as Dir bytes on the linked connection, and OR bytes on
3621 * the OR connection;
3622 * - relays and clients don't connect to internal IPs, unless specifically
3623 * configured to do so. If they are configured that way, we don't count
3624 * internal bytes.
3626 if (!connection_is_rate_limited(conn))
3627 return;
3629 const bool is_ipv6 = (conn->socket_family == AF_INET6);
3630 if (conn->type == CONN_TYPE_OR)
3631 conn_stats_note_or_conn_bytes(conn->global_identifier, num_read,
3632 num_written, now, is_ipv6);
3634 if (num_read > 0) {
3635 bwhist_note_bytes_read(num_read, now, is_ipv6);
3637 if (num_written > 0) {
3638 bwhist_note_bytes_written(num_written, now, is_ipv6);
3640 if (conn->type == CONN_TYPE_EXIT)
3641 rep_hist_note_exit_bytes(conn->port, num_written, num_read);
3643 /* Remember these bytes towards statistics. */
3644 stats_increment_bytes_read_and_written(num_read, num_written);
3646 /* Remember these bytes towards accounting. */
3647 if (accounting_is_enabled(get_options())) {
3648 if (now > last_recorded_accounting_at && last_recorded_accounting_at) {
3649 accounting_add_bytes(num_read, num_written,
3650 (int)(now - last_recorded_accounting_at));
3651 } else {
3652 accounting_add_bytes(num_read, num_written, 0);
3654 last_recorded_accounting_at = now;
3658 /** We just read <b>num_read</b> and wrote <b>num_written</b> bytes
3659 * onto <b>conn</b>. Decrement buckets appropriately. */
3660 static void
3661 connection_buckets_decrement(connection_t *conn, time_t now,
3662 size_t num_read, size_t num_written)
3664 if (num_written >= INT_MAX || num_read >= INT_MAX) {
3665 log_err(LD_BUG, "Value out of range. num_read=%lu, num_written=%lu, "
3666 "connection type=%s, state=%s",
3667 (unsigned long)num_read, (unsigned long)num_written,
3668 conn_type_to_string(conn->type),
3669 conn_state_to_string(conn->type, conn->state));
3670 tor_assert_nonfatal_unreached();
3671 if (num_written >= INT_MAX)
3672 num_written = 1;
3673 if (num_read >= INT_MAX)
3674 num_read = 1;
3677 record_num_bytes_transferred_impl(conn, now, num_read, num_written);
3679 /* Edge connection need to decrement the read side of the bucket used by our
3680 * congestion control. */
3681 if (CONN_IS_EDGE(conn) && num_read > 0) {
3682 edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
3683 token_bucket_rw_dec(&edge_conn->bucket, num_read, 0);
3686 if (!connection_is_rate_limited(conn))
3687 return; /* local IPs are free */
3689 unsigned flags = 0;
3690 if (connection_counts_as_relayed_traffic(conn, now)) {
3691 flags = token_bucket_rw_dec(&global_relayed_bucket, num_read, num_written);
3693 flags |= token_bucket_rw_dec(&global_bucket, num_read, num_written);
3695 if (flags & TB_WRITE) {
3696 write_buckets_last_empty_at = now;
3698 if (connection_speaks_cells(conn) && conn->state == OR_CONN_STATE_OPEN) {
3699 or_connection_t *or_conn = TO_OR_CONN(conn);
3700 token_bucket_rw_dec(&or_conn->bucket, num_read, num_written);
3705 * Mark <b>conn</b> as needing to stop reading because bandwidth has been
3706 * exhausted. If <b>is_global_bw</b>, it is closing because global bandwidth
3707 * limit has been exhausted. Otherwise, it is closing because its own
3708 * bandwidth limit has been exhausted.
3710 void
3711 connection_read_bw_exhausted(connection_t *conn, bool is_global_bw)
3713 (void)is_global_bw;
3714 conn->read_blocked_on_bw = 1;
3715 connection_stop_reading(conn);
3716 reenable_blocked_connection_schedule();
3720 * Mark <b>conn</b> as needing to stop reading because write bandwidth has
3721 * been exhausted. If <b>is_global_bw</b>, it is closing because global
3722 * bandwidth limit has been exhausted. Otherwise, it is closing because its
3723 * own bandwidth limit has been exhausted.
3725 void
3726 connection_write_bw_exhausted(connection_t *conn, bool is_global_bw)
3728 (void)is_global_bw;
3729 conn->write_blocked_on_bw = 1;
3730 connection_stop_writing(conn);
3731 reenable_blocked_connection_schedule();
3734 /** If we have exhausted our global buckets, or the buckets for conn,
3735 * stop reading. */
3736 void
3737 connection_consider_empty_read_buckets(connection_t *conn)
3739 int is_global = 1;
3740 const char *reason;
3742 if (CONN_IS_EDGE(conn) &&
3743 token_bucket_rw_get_read(&TO_EDGE_CONN(conn)->bucket) <= 0) {
3744 reason = "edge connection read bucket exhausted. Pausing.";
3745 is_global = false;
3746 } else if (!connection_is_rate_limited(conn)) {
3747 return; /* Always okay. */
3748 } else if (token_bucket_rw_get_read(&global_bucket) <= 0) {
3749 reason = "global read bucket exhausted. Pausing.";
3750 } else if (connection_counts_as_relayed_traffic(conn, approx_time()) &&
3751 token_bucket_rw_get_read(&global_relayed_bucket) <= 0) {
3752 reason = "global relayed read bucket exhausted. Pausing.";
3753 } else if (connection_speaks_cells(conn) &&
3754 conn->state == OR_CONN_STATE_OPEN &&
3755 token_bucket_rw_get_read(&TO_OR_CONN(conn)->bucket) <= 0) {
3756 reason = "connection read bucket exhausted. Pausing.";
3757 is_global = false;
3758 } else {
3759 return; /* all good, no need to stop it */
3762 LOG_FN_CONN(conn, (LOG_DEBUG, LD_NET, "%s", reason));
3763 connection_read_bw_exhausted(conn, is_global);
3766 /** If we have exhausted our global buckets, or the buckets for conn,
3767 * stop writing. */
3768 void
3769 connection_consider_empty_write_buckets(connection_t *conn)
3771 const char *reason;
3773 if (!connection_is_rate_limited(conn))
3774 return; /* Always okay. */
3776 bool is_global = true;
3777 if (token_bucket_rw_get_write(&global_bucket) <= 0) {
3778 reason = "global write bucket exhausted. Pausing.";
3779 } else if (connection_counts_as_relayed_traffic(conn, approx_time()) &&
3780 token_bucket_rw_get_write(&global_relayed_bucket) <= 0) {
3781 reason = "global relayed write bucket exhausted. Pausing.";
3782 } else if (connection_speaks_cells(conn) &&
3783 conn->state == OR_CONN_STATE_OPEN &&
3784 token_bucket_rw_get_write(&TO_OR_CONN(conn)->bucket) <= 0) {
3785 reason = "connection write bucket exhausted. Pausing.";
3786 is_global = false;
3787 } else
3788 return; /* all good, no need to stop it */
3790 LOG_FN_CONN(conn, (LOG_DEBUG, LD_NET, "%s", reason));
3791 connection_write_bw_exhausted(conn, is_global);
3794 /** Initialize the global buckets to the values configured in the
3795 * options */
3796 void
3797 connection_bucket_init(void)
3799 const or_options_t *options = get_options();
3800 const uint32_t now_ts = monotime_coarse_get_stamp();
3801 token_bucket_rw_init(&global_bucket,
3802 (int32_t)options->BandwidthRate,
3803 (int32_t)options->BandwidthBurst,
3804 now_ts);
3805 if (options->RelayBandwidthRate) {
3806 token_bucket_rw_init(&global_relayed_bucket,
3807 (int32_t)options->RelayBandwidthRate,
3808 (int32_t)options->RelayBandwidthBurst,
3809 now_ts);
3810 } else {
3811 token_bucket_rw_init(&global_relayed_bucket,
3812 (int32_t)options->BandwidthRate,
3813 (int32_t)options->BandwidthBurst,
3814 now_ts);
3817 reenable_blocked_connection_init(options);
3820 /** Update the global connection bucket settings to a new value. */
3821 void
3822 connection_bucket_adjust(const or_options_t *options)
3824 token_bucket_rw_adjust(&global_bucket,
3825 (int32_t)options->BandwidthRate,
3826 (int32_t)options->BandwidthBurst);
3827 if (options->RelayBandwidthRate) {
3828 token_bucket_rw_adjust(&global_relayed_bucket,
3829 (int32_t)options->RelayBandwidthRate,
3830 (int32_t)options->RelayBandwidthBurst);
3831 } else {
3832 token_bucket_rw_adjust(&global_relayed_bucket,
3833 (int32_t)options->BandwidthRate,
3834 (int32_t)options->BandwidthBurst);
3839 * Cached value of the last coarse-timestamp when we refilled the
3840 * global buckets.
3842 static uint32_t last_refilled_global_buckets_ts=0;
3844 * Refill the token buckets for a single connection <b>conn</b>, and the
3845 * global token buckets as appropriate. Requires that <b>now_ts</b> is
3846 * the time in coarse timestamp units.
3848 static void
3849 connection_bucket_refill_single(connection_t *conn, uint32_t now_ts)
3851 /* Note that we only check for equality here: the underlying
3852 * token bucket functions can handle moving backwards in time if they
3853 * need to. */
3854 if (now_ts != last_refilled_global_buckets_ts) {
3855 token_bucket_rw_refill(&global_bucket, now_ts);
3856 token_bucket_rw_refill(&global_relayed_bucket, now_ts);
3857 last_refilled_global_buckets_ts = now_ts;
3860 if (connection_speaks_cells(conn) && conn->state == OR_CONN_STATE_OPEN) {
3861 or_connection_t *or_conn = TO_OR_CONN(conn);
3862 token_bucket_rw_refill(&or_conn->bucket, now_ts);
3865 if (CONN_IS_EDGE(conn)) {
3866 token_bucket_rw_refill(&TO_EDGE_CONN(conn)->bucket, now_ts);
3871 * Event to re-enable all connections that were previously blocked on read or
3872 * write.
3874 static mainloop_event_t *reenable_blocked_connections_ev = NULL;
3876 /** True iff reenable_blocked_connections_ev is currently scheduled. */
3877 static int reenable_blocked_connections_is_scheduled = 0;
3879 /** Delay after which to run reenable_blocked_connections_ev. */
3880 static struct timeval reenable_blocked_connections_delay;
3883 * Re-enable all connections that were previously blocked on read or write.
3884 * This event is scheduled after enough time has elapsed to be sure
3885 * that the buckets will refill when the connections have something to do.
3887 static void
3888 reenable_blocked_connections_cb(mainloop_event_t *ev, void *arg)
3890 (void)ev;
3891 (void)arg;
3892 SMARTLIST_FOREACH_BEGIN(get_connection_array(), connection_t *, conn) {
3893 if (conn->read_blocked_on_bw == 1) {
3894 connection_start_reading(conn);
3895 conn->read_blocked_on_bw = 0;
3897 if (conn->write_blocked_on_bw == 1) {
3898 connection_start_writing(conn);
3899 conn->write_blocked_on_bw = 0;
3901 } SMARTLIST_FOREACH_END(conn);
3903 reenable_blocked_connections_is_scheduled = 0;
3907 * Initialize the mainloop event that we use to wake up connections that
3908 * find themselves blocked on bandwidth.
3910 static void
3911 reenable_blocked_connection_init(const or_options_t *options)
3913 if (! reenable_blocked_connections_ev) {
3914 reenable_blocked_connections_ev =
3915 mainloop_event_new(reenable_blocked_connections_cb, NULL);
3916 reenable_blocked_connections_is_scheduled = 0;
3918 time_t sec = options->TokenBucketRefillInterval / 1000;
3919 int msec = (options->TokenBucketRefillInterval % 1000);
3920 reenable_blocked_connections_delay.tv_sec = sec;
3921 reenable_blocked_connections_delay.tv_usec = msec * 1000;
3925 * Called when we have blocked a connection for being low on bandwidth:
3926 * schedule an event to reenable such connections, if it is not already
3927 * scheduled.
3929 static void
3930 reenable_blocked_connection_schedule(void)
3932 if (reenable_blocked_connections_is_scheduled)
3933 return;
3934 if (BUG(reenable_blocked_connections_ev == NULL)) {
3935 reenable_blocked_connection_init(get_options());
3937 mainloop_event_schedule(reenable_blocked_connections_ev,
3938 &reenable_blocked_connections_delay);
3939 reenable_blocked_connections_is_scheduled = 1;
3942 /** Read bytes from conn-\>s and process them.
3944 * It calls connection_buf_read_from_socket() to bring in any new bytes,
3945 * and then calls connection_process_inbuf() to process them.
3947 * Mark the connection and return -1 if you want to close it, else
3948 * return 0.
3950 static int
3951 connection_handle_read_impl(connection_t *conn)
3953 ssize_t max_to_read=-1, try_to_read;
3954 size_t before, n_read = 0;
3955 int socket_error = 0;
3957 if (conn->marked_for_close)
3958 return 0; /* do nothing */
3960 conn->timestamp_last_read_allowed = approx_time();
3962 connection_bucket_refill_single(conn, monotime_coarse_get_stamp());
3964 switch (conn->type) {
3965 case CONN_TYPE_OR_LISTENER:
3966 return connection_handle_listener_read(conn, CONN_TYPE_OR);
3967 case CONN_TYPE_EXT_OR_LISTENER:
3968 return connection_handle_listener_read(conn, CONN_TYPE_EXT_OR);
3969 case CONN_TYPE_AP_LISTENER:
3970 case CONN_TYPE_AP_TRANS_LISTENER:
3971 case CONN_TYPE_AP_NATD_LISTENER:
3972 case CONN_TYPE_AP_HTTP_CONNECT_LISTENER:
3973 return connection_handle_listener_read(conn, CONN_TYPE_AP);
3974 case CONN_TYPE_DIR_LISTENER:
3975 return connection_handle_listener_read(conn, CONN_TYPE_DIR);
3976 case CONN_TYPE_CONTROL_LISTENER:
3977 return connection_handle_listener_read(conn, CONN_TYPE_CONTROL);
3978 case CONN_TYPE_METRICS_LISTENER:
3979 return connection_handle_listener_read(conn, CONN_TYPE_METRICS);
3980 case CONN_TYPE_AP_DNS_LISTENER:
3981 /* This should never happen; eventdns.c handles the reads here. */
3982 tor_fragile_assert();
3983 return 0;
3986 loop_again:
3987 try_to_read = max_to_read;
3988 tor_assert(!conn->marked_for_close);
3990 before = buf_datalen(conn->inbuf);
3991 if (connection_buf_read_from_socket(conn, &max_to_read, &socket_error) < 0) {
3992 /* There's a read error; kill the connection.*/
3993 if (conn->type == CONN_TYPE_OR) {
3994 connection_or_notify_error(TO_OR_CONN(conn),
3995 socket_error != 0 ?
3996 errno_to_orconn_end_reason(socket_error) :
3997 END_OR_CONN_REASON_CONNRESET,
3998 socket_error != 0 ?
3999 tor_socket_strerror(socket_error) :
4000 "(unknown, errno was 0)");
4002 if (CONN_IS_EDGE(conn)) {
4003 edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
4004 connection_edge_end_errno(edge_conn);
4005 if (conn->type == CONN_TYPE_AP && TO_ENTRY_CONN(conn)->socks_request) {
4006 /* broken, don't send a socks reply back */
4007 TO_ENTRY_CONN(conn)->socks_request->has_finished = 1;
4010 connection_close_immediate(conn); /* Don't flush; connection is dead. */
4012 * This can bypass normal channel checking since we did
4013 * connection_or_notify_error() above.
4015 connection_mark_for_close_internal(conn);
4016 return -1;
4018 n_read += buf_datalen(conn->inbuf) - before;
4019 if (CONN_IS_EDGE(conn) && try_to_read != max_to_read) {
4020 /* instruct it not to try to package partial cells. */
4021 if (connection_process_inbuf(conn, 0) < 0) {
4022 return -1;
4024 if (!conn->marked_for_close &&
4025 connection_is_reading(conn) &&
4026 !conn->inbuf_reached_eof &&
4027 max_to_read > 0)
4028 goto loop_again; /* try reading again, in case more is here now */
4030 /* one last try, packaging partial cells and all. */
4031 if (!conn->marked_for_close &&
4032 connection_process_inbuf(conn, 1) < 0) {
4033 return -1;
4035 if (conn->linked_conn) {
4036 /* The other side's handle_write() will never actually get called, so
4037 * we need to invoke the appropriate callbacks ourself. */
4038 connection_t *linked = conn->linked_conn;
4040 if (n_read) {
4041 /* Probably a no-op, since linked conns typically don't count for
4042 * bandwidth rate limiting. But do it anyway so we can keep stats
4043 * accurately. Note that since we read the bytes from conn, and
4044 * we're writing the bytes onto the linked connection, we count
4045 * these as <i>written</i> bytes. */
4046 connection_buckets_decrement(linked, approx_time(), 0, n_read);
4048 if (connection_flushed_some(linked) < 0)
4049 connection_mark_for_close(linked);
4050 if (!connection_wants_to_flush(linked))
4051 connection_finished_flushing(linked);
4054 if (!buf_datalen(linked->outbuf) && conn->active_on_link)
4055 connection_stop_reading_from_linked_conn(conn);
4057 /* If we hit the EOF, call connection_reached_eof(). */
4058 if (!conn->marked_for_close &&
4059 conn->inbuf_reached_eof &&
4060 connection_reached_eof(conn) < 0) {
4061 return -1;
4063 return 0;
4066 /* DOCDOC connection_handle_read */
4068 connection_handle_read(connection_t *conn)
4070 int res;
4071 update_current_time(time(NULL));
4072 res = connection_handle_read_impl(conn);
4073 return res;
4076 /** Pull in new bytes from conn-\>s or conn-\>linked_conn onto conn-\>inbuf,
4077 * either directly or via TLS. Reduce the token buckets by the number of bytes
4078 * read.
4080 * If *max_to_read is -1, then decide it ourselves, else go with the
4081 * value passed to us. When returning, if it's changed, subtract the
4082 * number of bytes we read from *max_to_read.
4084 * Return -1 if we want to break conn, else return 0.
4086 static int
4087 connection_buf_read_from_socket(connection_t *conn, ssize_t *max_to_read,
4088 int *socket_error)
4090 int result;
4091 ssize_t at_most = *max_to_read;
4092 size_t slack_in_buf, more_to_read;
4093 size_t n_read = 0, n_written = 0;
4095 if (at_most == -1) { /* we need to initialize it */
4096 /* how many bytes are we allowed to read? */
4097 at_most = connection_bucket_read_limit(conn, approx_time());
4100 /* Do not allow inbuf to grow past BUF_MAX_LEN. */
4101 const ssize_t maximum = BUF_MAX_LEN - buf_datalen(conn->inbuf);
4102 if (at_most > maximum) {
4103 at_most = maximum;
4106 slack_in_buf = buf_slack(conn->inbuf);
4107 again:
4108 if ((size_t)at_most > slack_in_buf && slack_in_buf >= 1024) {
4109 more_to_read = at_most - slack_in_buf;
4110 at_most = slack_in_buf;
4111 } else {
4112 more_to_read = 0;
4115 if (connection_speaks_cells(conn) &&
4116 conn->state > OR_CONN_STATE_PROXY_HANDSHAKING) {
4117 int pending;
4118 or_connection_t *or_conn = TO_OR_CONN(conn);
4119 size_t initial_size;
4120 if (conn->state == OR_CONN_STATE_TLS_HANDSHAKING ||
4121 conn->state == OR_CONN_STATE_TLS_CLIENT_RENEGOTIATING) {
4122 /* continue handshaking even if global token bucket is empty */
4123 return connection_tls_continue_handshake(or_conn);
4126 log_debug(LD_NET,
4127 "%d: starting, inbuf_datalen %ld (%d pending in tls object)."
4128 " at_most %ld.",
4129 (int)conn->s,(long)buf_datalen(conn->inbuf),
4130 tor_tls_get_pending_bytes(or_conn->tls), (long)at_most);
4132 initial_size = buf_datalen(conn->inbuf);
4133 /* else open, or closing */
4134 result = buf_read_from_tls(conn->inbuf, or_conn->tls, at_most);
4135 if (TOR_TLS_IS_ERROR(result) || result == TOR_TLS_CLOSE)
4136 or_conn->tls_error = result;
4137 else
4138 or_conn->tls_error = 0;
4140 switch (result) {
4141 case TOR_TLS_CLOSE:
4142 case TOR_TLS_ERROR_IO:
4143 log_debug(LD_NET,"TLS %s closed %son read. Closing.",
4144 connection_describe(conn),
4145 result == TOR_TLS_CLOSE ? "cleanly " : "");
4146 return result;
4147 CASE_TOR_TLS_ERROR_ANY_NONIO:
4148 log_debug(LD_NET,"tls error [%s] from %s. Breaking.",
4149 tor_tls_err_to_string(result),
4150 connection_describe(conn));
4151 return result;
4152 case TOR_TLS_WANTWRITE:
4153 connection_start_writing(conn);
4154 return 0;
4155 case TOR_TLS_WANTREAD:
4156 if (conn->in_connection_handle_write) {
4157 /* We've been invoked from connection_handle_write, because we're
4158 * waiting for a TLS renegotiation, the renegotiation started, and
4159 * SSL_read returned WANTWRITE. But now SSL_read is saying WANTREAD
4160 * again. Stop waiting for write events now, or else we'll
4161 * busy-loop until data arrives for us to read.
4162 * XXX: remove this when v2 handshakes support is dropped. */
4163 connection_stop_writing(conn);
4164 if (!connection_is_reading(conn))
4165 connection_start_reading(conn);
4167 /* we're already reading, one hopes */
4168 break;
4169 case TOR_TLS_DONE: /* no data read, so nothing to process */
4170 break; /* so we call bucket_decrement below */
4171 default:
4172 break;
4174 pending = tor_tls_get_pending_bytes(or_conn->tls);
4175 if (pending) {
4176 /* If we have any pending bytes, we read them now. This *can*
4177 * take us over our read allotment, but really we shouldn't be
4178 * believing that SSL bytes are the same as TCP bytes anyway. */
4179 int r2 = buf_read_from_tls(conn->inbuf, or_conn->tls, pending);
4180 if (BUG(r2<0)) {
4181 log_warn(LD_BUG, "apparently, reading pending bytes can fail.");
4182 return -1;
4185 result = (int)(buf_datalen(conn->inbuf)-initial_size);
4186 tor_tls_get_n_raw_bytes(or_conn->tls, &n_read, &n_written);
4187 log_debug(LD_GENERAL, "After TLS read of %d: %ld read, %ld written",
4188 result, (long)n_read, (long)n_written);
4189 } else if (conn->linked) {
4190 if (conn->linked_conn) {
4191 result = (int) buf_move_all(conn->inbuf, conn->linked_conn->outbuf);
4192 } else {
4193 result = 0;
4195 //log_notice(LD_GENERAL, "Moved %d bytes on an internal link!", result);
4196 /* If the other side has disappeared, or if it's been marked for close and
4197 * we flushed its outbuf, then we should set our inbuf_reached_eof. */
4198 if (!conn->linked_conn ||
4199 (conn->linked_conn->marked_for_close &&
4200 buf_datalen(conn->linked_conn->outbuf) == 0))
4201 conn->inbuf_reached_eof = 1;
4203 n_read = (size_t) result;
4204 } else {
4205 /* !connection_speaks_cells, !conn->linked_conn. */
4206 int reached_eof = 0;
4207 CONN_LOG_PROTECT(conn,
4208 result = buf_read_from_socket(conn->inbuf, conn->s,
4209 at_most,
4210 &reached_eof,
4211 socket_error));
4212 if (reached_eof)
4213 conn->inbuf_reached_eof = 1;
4215 // log_fn(LOG_DEBUG,"read_to_buf returned %d.",read_result);
4217 if (result < 0)
4218 return -1;
4219 n_read = (size_t) result;
4222 if (n_read > 0) {
4223 /* change *max_to_read */
4224 *max_to_read = at_most - n_read;
4226 /* Onion service application connection. Note read bytes for metrics. */
4227 if (CONN_IS_EDGE(conn) && TO_EDGE_CONN(conn)->hs_ident) {
4228 edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
4229 hs_metrics_app_read_bytes(&edge_conn->hs_ident->identity_pk,
4230 edge_conn->hs_ident->orig_virtual_port,
4231 n_read);
4234 /* Update edge_conn->n_read */
4235 if (conn->type == CONN_TYPE_AP) {
4236 edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
4238 /* Check for overflow: */
4239 if (PREDICT_LIKELY(UINT32_MAX - edge_conn->n_read > n_read))
4240 edge_conn->n_read += (int)n_read;
4241 else
4242 edge_conn->n_read = UINT32_MAX;
4245 /* If CONN_BW events are enabled, update conn->n_read_conn_bw for
4246 * OR/DIR/EXIT connections, checking for overflow. */
4247 if (get_options()->TestingEnableConnBwEvent &&
4248 (conn->type == CONN_TYPE_OR ||
4249 conn->type == CONN_TYPE_DIR ||
4250 conn->type == CONN_TYPE_EXIT)) {
4251 if (PREDICT_LIKELY(UINT32_MAX - conn->n_read_conn_bw > n_read))
4252 conn->n_read_conn_bw += (int)n_read;
4253 else
4254 conn->n_read_conn_bw = UINT32_MAX;
4258 connection_buckets_decrement(conn, approx_time(), n_read, n_written);
4260 if (more_to_read && result == at_most) {
4261 slack_in_buf = buf_slack(conn->inbuf);
4262 at_most = more_to_read;
4263 goto again;
4266 /* Call even if result is 0, since the global read bucket may
4267 * have reached 0 on a different conn, and this connection needs to
4268 * know to stop reading. */
4269 connection_consider_empty_read_buckets(conn);
4270 if (n_written > 0 && connection_is_writing(conn))
4271 connection_consider_empty_write_buckets(conn);
4273 return 0;
4276 /** A pass-through to fetch_from_buf. */
4278 connection_buf_get_bytes(char *string, size_t len, connection_t *conn)
4280 return buf_get_bytes(conn->inbuf, string, len);
4283 /** As buf_get_line(), but read from a connection's input buffer. */
4285 connection_buf_get_line(connection_t *conn, char *data,
4286 size_t *data_len)
4288 return buf_get_line(conn->inbuf, data, data_len);
4291 /** As fetch_from_buf_http, but fetches from a connection's input buffer_t as
4292 * appropriate. */
4294 connection_fetch_from_buf_http(connection_t *conn,
4295 char **headers_out, size_t max_headerlen,
4296 char **body_out, size_t *body_used,
4297 size_t max_bodylen, int force_complete)
4299 return fetch_from_buf_http(conn->inbuf, headers_out, max_headerlen,
4300 body_out, body_used, max_bodylen, force_complete);
4303 /** Return true if this connection has data to flush. */
4305 connection_wants_to_flush(connection_t *conn)
4307 return connection_get_outbuf_len(conn) > 0;
4310 /** Are there too many bytes on edge connection <b>conn</b>'s outbuf to
4311 * send back a relay-level sendme yet? Return 1 if so, 0 if not. Used by
4312 * connection_edge_consider_sending_sendme().
4315 connection_outbuf_too_full(connection_t *conn)
4317 return connection_get_outbuf_len(conn) > 10*CELL_PAYLOAD_SIZE;
4321 * On Windows Vista and Windows 7, tune the send buffer size according to a
4322 * hint from the OS.
4324 * This should help fix slow upload rates.
4326 static void
4327 update_send_buffer_size(tor_socket_t sock)
4329 #ifdef _WIN32
4330 /* We only do this on Vista and 7, because earlier versions of Windows
4331 * don't have the SIO_IDEAL_SEND_BACKLOG_QUERY functionality, and on
4332 * later versions it isn't necessary. */
4333 static int isVistaOr7 = -1;
4334 if (isVistaOr7 == -1) {
4335 isVistaOr7 = 0;
4336 OSVERSIONINFO osvi = { 0 };
4337 osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
4338 GetVersionEx(&osvi);
4339 if (osvi.dwMajorVersion == 6 && osvi.dwMinorVersion < 2)
4340 isVistaOr7 = 1;
4342 if (!isVistaOr7)
4343 return;
4344 if (get_options()->ConstrainedSockets)
4345 return;
4346 ULONG isb = 0;
4347 DWORD bytesReturned = 0;
4348 if (!WSAIoctl(sock, SIO_IDEAL_SEND_BACKLOG_QUERY, NULL, 0,
4349 &isb, sizeof(isb), &bytesReturned, NULL, NULL)) {
4350 setsockopt(sock, SOL_SOCKET, SO_SNDBUF, (const char*)&isb, sizeof(isb));
4352 #else /* !defined(_WIN32) */
4353 (void) sock;
4354 #endif /* defined(_WIN32) */
4357 /** Try to flush more bytes onto <b>conn</b>-\>s.
4359 * This function is called in connection_handle_write(), which gets
4360 * called from conn_write_callback() in main.c when libevent tells us
4361 * that <b>conn</b> wants to write.
4363 * Update <b>conn</b>-\>timestamp_last_write_allowed to now, and call flush_buf
4364 * or flush_buf_tls appropriately. If it succeeds and there are no more
4365 * more bytes on <b>conn</b>-\>outbuf, then call connection_finished_flushing
4366 * on it too.
4368 * If <b>force</b>, then write as many bytes as possible, ignoring bandwidth
4369 * limits. (Used for flushing messages to controller connections on fatal
4370 * errors.)
4372 * Mark the connection and return -1 if you want to close it, else
4373 * return 0.
4375 static int
4376 connection_handle_write_impl(connection_t *conn, int force)
4378 int e;
4379 socklen_t len=(socklen_t)sizeof(e);
4380 int result;
4381 ssize_t max_to_write;
4382 time_t now = approx_time();
4383 size_t n_read = 0, n_written = 0;
4384 int dont_stop_writing = 0;
4386 tor_assert(!connection_is_listener(conn));
4388 if (conn->marked_for_close || !SOCKET_OK(conn->s))
4389 return 0; /* do nothing */
4391 if (conn->in_flushed_some) {
4392 log_warn(LD_BUG, "called recursively from inside conn->in_flushed_some");
4393 return 0;
4396 conn->timestamp_last_write_allowed = now;
4398 connection_bucket_refill_single(conn, monotime_coarse_get_stamp());
4400 /* Sometimes, "writable" means "connected". */
4401 if (connection_state_is_connecting(conn)) {
4402 if (getsockopt(conn->s, SOL_SOCKET, SO_ERROR, (void*)&e, &len) < 0) {
4403 log_warn(LD_BUG, "getsockopt() syscall failed");
4404 if (conn->type == CONN_TYPE_OR) {
4405 or_connection_t *orconn = TO_OR_CONN(conn);
4406 connection_or_close_for_error(orconn, 0);
4407 } else {
4408 if (CONN_IS_EDGE(conn)) {
4409 connection_edge_end_errno(TO_EDGE_CONN(conn));
4411 connection_mark_for_close(conn);
4413 return -1;
4415 if (e) {
4416 /* some sort of error, but maybe just inprogress still */
4417 if (!ERRNO_IS_CONN_EINPROGRESS(e)) {
4418 log_info(LD_NET,"in-progress connect failed. Removing. (%s)",
4419 tor_socket_strerror(e));
4420 if (CONN_IS_EDGE(conn))
4421 connection_edge_end_errno(TO_EDGE_CONN(conn));
4422 if (conn->type == CONN_TYPE_OR)
4423 connection_or_notify_error(TO_OR_CONN(conn),
4424 errno_to_orconn_end_reason(e),
4425 tor_socket_strerror(e));
4427 connection_close_immediate(conn);
4429 * This can bypass normal channel checking since we did
4430 * connection_or_notify_error() above.
4432 connection_mark_for_close_internal(conn);
4433 return -1;
4434 } else {
4435 return 0; /* no change, see if next time is better */
4438 /* The connection is successful. */
4439 if (connection_finished_connecting(conn)<0)
4440 return -1;
4443 max_to_write = force ? (ssize_t)buf_datalen(conn->outbuf)
4444 : connection_bucket_write_limit(conn, now);
4446 if (connection_speaks_cells(conn) &&
4447 conn->state > OR_CONN_STATE_PROXY_HANDSHAKING) {
4448 or_connection_t *or_conn = TO_OR_CONN(conn);
4449 size_t initial_size;
4450 if (conn->state == OR_CONN_STATE_TLS_HANDSHAKING ||
4451 conn->state == OR_CONN_STATE_TLS_CLIENT_RENEGOTIATING) {
4452 connection_stop_writing(conn);
4453 if (connection_tls_continue_handshake(or_conn) < 0) {
4454 /* Don't flush; connection is dead. */
4455 connection_or_notify_error(or_conn,
4456 END_OR_CONN_REASON_MISC,
4457 "TLS error in connection_tls_"
4458 "continue_handshake()");
4459 connection_close_immediate(conn);
4461 * This can bypass normal channel checking since we did
4462 * connection_or_notify_error() above.
4464 connection_mark_for_close_internal(conn);
4465 return -1;
4467 return 0;
4468 } else if (conn->state == OR_CONN_STATE_TLS_SERVER_RENEGOTIATING) {
4469 return connection_handle_read(conn);
4472 /* else open, or closing */
4473 initial_size = buf_datalen(conn->outbuf);
4474 result = buf_flush_to_tls(conn->outbuf, or_conn->tls,
4475 max_to_write);
4477 if (result >= 0)
4478 update_send_buffer_size(conn->s);
4480 /* If we just flushed the last bytes, tell the channel on the
4481 * or_conn to check if it needs to geoip_change_dirreq_state() */
4482 /* XXXX move this to flushed_some or finished_flushing -NM */
4483 if (buf_datalen(conn->outbuf) == 0 && or_conn->chan)
4484 channel_notify_flushed(TLS_CHAN_TO_BASE(or_conn->chan));
4486 switch (result) {
4487 CASE_TOR_TLS_ERROR_ANY:
4488 case TOR_TLS_CLOSE:
4489 or_conn->tls_error = result;
4490 log_info(LD_NET, result != TOR_TLS_CLOSE ?
4491 "tls error. breaking.":"TLS connection closed on flush");
4492 /* Don't flush; connection is dead. */
4493 connection_or_notify_error(or_conn,
4494 END_OR_CONN_REASON_MISC,
4495 result != TOR_TLS_CLOSE ?
4496 "TLS error in during flush" :
4497 "TLS closed during flush");
4498 connection_close_immediate(conn);
4500 * This can bypass normal channel checking since we did
4501 * connection_or_notify_error() above.
4503 connection_mark_for_close_internal(conn);
4504 return -1;
4505 case TOR_TLS_WANTWRITE:
4506 log_debug(LD_NET,"wanted write.");
4507 /* we're already writing */
4508 dont_stop_writing = 1;
4509 break;
4510 case TOR_TLS_WANTREAD:
4511 /* Make sure to avoid a loop if the receive buckets are empty. */
4512 log_debug(LD_NET,"wanted read.");
4513 if (!connection_is_reading(conn)) {
4514 connection_write_bw_exhausted(conn, true);
4515 /* we'll start reading again when we get more tokens in our
4516 * read bucket; then we'll start writing again too.
4519 /* else no problem, we're already reading */
4520 return 0;
4521 /* case TOR_TLS_DONE:
4522 * for TOR_TLS_DONE, fall through to check if the flushlen
4523 * is empty, so we can stop writing.
4527 tor_tls_get_n_raw_bytes(or_conn->tls, &n_read, &n_written);
4528 log_debug(LD_GENERAL, "After TLS write of %d: %ld read, %ld written",
4529 result, (long)n_read, (long)n_written);
4530 or_conn->bytes_xmitted += result;
4531 or_conn->bytes_xmitted_by_tls += n_written;
4532 /* So we notice bytes were written even on error */
4533 /* XXXX This cast is safe since we can never write INT_MAX bytes in a
4534 * single set of TLS operations. But it looks kinda ugly. If we refactor
4535 * the *_buf_tls functions, we should make them return ssize_t or size_t
4536 * or something. */
4537 result = (int)(initial_size-buf_datalen(conn->outbuf));
4538 } else {
4539 CONN_LOG_PROTECT(conn,
4540 result = buf_flush_to_socket(conn->outbuf, conn->s,
4541 max_to_write));
4542 if (result < 0) {
4543 if (CONN_IS_EDGE(conn))
4544 connection_edge_end_errno(TO_EDGE_CONN(conn));
4545 if (conn->type == CONN_TYPE_AP) {
4546 /* writing failed; we couldn't send a SOCKS reply if we wanted to */
4547 TO_ENTRY_CONN(conn)->socks_request->has_finished = 1;
4550 connection_close_immediate(conn); /* Don't flush; connection is dead. */
4551 connection_mark_for_close(conn);
4552 return -1;
4554 update_send_buffer_size(conn->s);
4555 n_written = (size_t) result;
4558 if (n_written && conn->type == CONN_TYPE_AP) {
4559 edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
4561 /* Check for overflow: */
4562 if (PREDICT_LIKELY(UINT32_MAX - edge_conn->n_written > n_written))
4563 edge_conn->n_written += (int)n_written;
4564 else
4565 edge_conn->n_written = UINT32_MAX;
4568 /* If CONN_BW events are enabled, update conn->n_written_conn_bw for
4569 * OR/DIR/EXIT connections, checking for overflow. */
4570 if (n_written && get_options()->TestingEnableConnBwEvent &&
4571 (conn->type == CONN_TYPE_OR ||
4572 conn->type == CONN_TYPE_DIR ||
4573 conn->type == CONN_TYPE_EXIT)) {
4574 if (PREDICT_LIKELY(UINT32_MAX - conn->n_written_conn_bw > n_written))
4575 conn->n_written_conn_bw += (int)n_written;
4576 else
4577 conn->n_written_conn_bw = UINT32_MAX;
4580 connection_buckets_decrement(conn, approx_time(), n_read, n_written);
4582 if (result > 0) {
4583 /* If we wrote any bytes from our buffer, then call the appropriate
4584 * functions. */
4585 if (connection_flushed_some(conn) < 0) {
4586 if (connection_speaks_cells(conn)) {
4587 connection_or_notify_error(TO_OR_CONN(conn),
4588 END_OR_CONN_REASON_MISC,
4589 "Got error back from "
4590 "connection_flushed_some()");
4594 * This can bypass normal channel checking since we did
4595 * connection_or_notify_error() above.
4597 connection_mark_for_close_internal(conn);
4601 if (!connection_wants_to_flush(conn) &&
4602 !dont_stop_writing) { /* it's done flushing */
4603 if (connection_finished_flushing(conn) < 0) {
4604 /* already marked */
4605 goto err;
4607 goto done;
4610 /* Call even if result is 0, since the global write bucket may
4611 * have reached 0 on a different conn, and this connection needs to
4612 * know to stop writing. */
4613 connection_consider_empty_write_buckets(conn);
4614 if (n_read > 0 && connection_is_reading(conn))
4615 connection_consider_empty_read_buckets(conn);
4617 done:
4618 /* If this is an edge connection with congestion control, check to see
4619 * if it is time to send an xon */
4620 if (conn_uses_flow_control(conn)) {
4621 flow_control_decide_xon(TO_EDGE_CONN(conn), n_written);
4624 return 0;
4626 err:
4627 return -1;
4630 /* DOCDOC connection_handle_write */
4632 connection_handle_write(connection_t *conn, int force)
4634 int res;
4635 update_current_time(time(NULL));
4636 /* connection_handle_write_impl() might call connection_handle_read()
4637 * if we're in the middle of a v2 handshake, in which case it needs this
4638 * flag set. */
4639 conn->in_connection_handle_write = 1;
4640 res = connection_handle_write_impl(conn, force);
4641 conn->in_connection_handle_write = 0;
4642 return res;
4646 * Try to flush data that's waiting for a write on <b>conn</b>. Return
4647 * -1 on failure, 0 on success.
4649 * Don't use this function for regular writing; the buffers
4650 * system should be good enough at scheduling writes there. Instead, this
4651 * function is for cases when we're about to exit or something and we want
4652 * to report it right away.
4655 connection_flush(connection_t *conn)
4657 return connection_handle_write(conn, 1);
4660 /** Helper for connection_write_to_buf_impl and connection_write_buf_to_buf:
4662 * Return true iff it is okay to queue bytes on <b>conn</b>'s outbuf for
4663 * writing.
4665 static int
4666 connection_may_write_to_buf(connection_t *conn)
4668 /* if it's marked for close, only allow write if we mean to flush it */
4669 if (conn->marked_for_close && !conn->hold_open_until_flushed)
4670 return 0;
4672 return 1;
4675 /** Helper for connection_write_to_buf_impl and connection_write_buf_to_buf:
4677 * Called when an attempt to add bytes on <b>conn</b>'s outbuf has failed;
4678 * mark the connection and warn as appropriate.
4680 static void
4681 connection_write_to_buf_failed(connection_t *conn)
4683 if (CONN_IS_EDGE(conn)) {
4684 /* if it failed, it means we have our package/delivery windows set
4685 wrong compared to our max outbuf size. close the whole circuit. */
4686 log_warn(LD_NET,
4687 "write_to_buf failed. Closing circuit (fd %d).", (int)conn->s);
4688 circuit_mark_for_close(circuit_get_by_edge_conn(TO_EDGE_CONN(conn)),
4689 END_CIRC_REASON_INTERNAL);
4690 } else if (conn->type == CONN_TYPE_OR) {
4691 or_connection_t *orconn = TO_OR_CONN(conn);
4692 log_warn(LD_NET,
4693 "write_to_buf failed on an orconn; notifying of error "
4694 "(fd %d)", (int)(conn->s));
4695 connection_or_close_for_error(orconn, 0);
4696 } else {
4697 log_warn(LD_NET,
4698 "write_to_buf failed. Closing connection (fd %d).",
4699 (int)conn->s);
4700 connection_mark_for_close(conn);
4704 /** Helper for connection_write_to_buf_impl and connection_write_buf_to_buf:
4706 * Called when an attempt to add bytes on <b>conn</b>'s outbuf has succeeded:
4707 * start writing if appropriate.
4709 static void
4710 connection_write_to_buf_commit(connection_t *conn)
4712 /* If we receive optimistic data in the EXIT_CONN_STATE_RESOLVING
4713 * state, we don't want to try to write it right away, since
4714 * conn->write_event won't be set yet. Otherwise, write data from
4715 * this conn as the socket is available. */
4716 if (conn->write_event) {
4717 connection_start_writing(conn);
4721 /** Append <b>len</b> bytes of <b>string</b> onto <b>conn</b>'s
4722 * outbuf, and ask it to start writing.
4724 * If <b>zlib</b> is nonzero, this is a directory connection that should get
4725 * its contents compressed or decompressed as they're written. If zlib is
4726 * negative, this is the last data to be compressed, and the connection's zlib
4727 * state should be flushed.
4729 MOCK_IMPL(void,
4730 connection_write_to_buf_impl_,(const char *string, size_t len,
4731 connection_t *conn, int zlib))
4733 /* XXXX This function really needs to return -1 on failure. */
4734 int r;
4735 if (!len && !(zlib<0))
4736 return;
4738 if (!connection_may_write_to_buf(conn))
4739 return;
4741 if (zlib) {
4742 dir_connection_t *dir_conn = TO_DIR_CONN(conn);
4743 int done = zlib < 0;
4744 CONN_LOG_PROTECT(conn, r = buf_add_compress(conn->outbuf,
4745 dir_conn->compress_state,
4746 string, len, done));
4747 } else {
4748 CONN_LOG_PROTECT(conn, r = buf_add(conn->outbuf, string, len));
4750 if (r < 0) {
4751 connection_write_to_buf_failed(conn);
4752 return;
4754 connection_write_to_buf_commit(conn);
4758 * Write a <b>string</b> (of size <b>len</b> to directory connection
4759 * <b>dir_conn</b>. Apply compression if connection is configured to use
4760 * it and finalize it if <b>done</b> is true.
4762 void
4763 connection_dir_buf_add(const char *string, size_t len,
4764 dir_connection_t *dir_conn, int done)
4766 if (dir_conn->compress_state != NULL) {
4767 connection_buf_add_compress(string, len, dir_conn, done);
4768 return;
4771 connection_buf_add(string, len, TO_CONN(dir_conn));
4774 void
4775 connection_buf_add_compress(const char *string, size_t len,
4776 dir_connection_t *conn, int done)
4778 connection_write_to_buf_impl_(string, len, TO_CONN(conn), done ? -1 : 1);
4782 * Add all bytes from <b>buf</b> to <b>conn</b>'s outbuf, draining them
4783 * from <b>buf</b>. (If the connection is marked and will soon be closed,
4784 * nothing is drained.)
4786 void
4787 connection_buf_add_buf(connection_t *conn, buf_t *buf)
4789 tor_assert(conn);
4790 tor_assert(buf);
4791 size_t len = buf_datalen(buf);
4792 if (len == 0)
4793 return;
4795 if (!connection_may_write_to_buf(conn))
4796 return;
4798 buf_move_all(conn->outbuf, buf);
4799 connection_write_to_buf_commit(conn);
4802 #define CONN_GET_ALL_TEMPLATE(var, test) \
4803 STMT_BEGIN \
4804 smartlist_t *conns = get_connection_array(); \
4805 smartlist_t *ret_conns = smartlist_new(); \
4806 SMARTLIST_FOREACH_BEGIN(conns, connection_t *, var) { \
4807 if (var && (test) && !var->marked_for_close) \
4808 smartlist_add(ret_conns, var); \
4809 } SMARTLIST_FOREACH_END(var); \
4810 return ret_conns; \
4811 STMT_END
4813 /* Return a list of connections that aren't close and matches the given type
4814 * and state. The returned list can be empty and must be freed using
4815 * smartlist_free(). The caller does NOT have ownership of the objects in the
4816 * list so it must not free them nor reference them as they can disappear. */
4817 smartlist_t *
4818 connection_list_by_type_state(int type, int state)
4820 CONN_GET_ALL_TEMPLATE(conn, (conn->type == type && conn->state == state));
4823 /* Return a list of connections that aren't close and matches the given type
4824 * and purpose. The returned list can be empty and must be freed using
4825 * smartlist_free(). The caller does NOT have ownership of the objects in the
4826 * list so it must not free them nor reference them as they can disappear. */
4827 smartlist_t *
4828 connection_list_by_type_purpose(int type, int purpose)
4830 CONN_GET_ALL_TEMPLATE(conn,
4831 (conn->type == type && conn->purpose == purpose));
4834 /** Return a connection_t * from get_connection_array() that satisfies test on
4835 * var, and that is not marked for close. */
4836 #define CONN_GET_TEMPLATE(var, test) \
4837 STMT_BEGIN \
4838 smartlist_t *conns = get_connection_array(); \
4839 SMARTLIST_FOREACH(conns, connection_t *, var, \
4841 if (var && (test) && !var->marked_for_close) \
4842 return var; \
4843 }); \
4844 return NULL; \
4845 STMT_END
4847 /** Return a connection with given type, address, port, and purpose;
4848 * or NULL if no such connection exists (or if all such connections are marked
4849 * for close). */
4850 MOCK_IMPL(connection_t *,
4851 connection_get_by_type_addr_port_purpose,(int type,
4852 const tor_addr_t *addr, uint16_t port,
4853 int purpose))
4855 CONN_GET_TEMPLATE(conn,
4856 (conn->type == type &&
4857 tor_addr_eq(&conn->addr, addr) &&
4858 conn->port == port &&
4859 conn->purpose == purpose));
4862 /** Return the stream with id <b>id</b> if it is not already marked for
4863 * close.
4865 connection_t *
4866 connection_get_by_global_id(uint64_t id)
4868 CONN_GET_TEMPLATE(conn, conn->global_identifier == id);
4871 /** Return a connection of type <b>type</b> that is not marked for close.
4873 connection_t *
4874 connection_get_by_type(int type)
4876 CONN_GET_TEMPLATE(conn, conn->type == type);
4879 /** Return a connection of type <b>type</b> that is in state <b>state</b>,
4880 * and that is not marked for close.
4882 connection_t *
4883 connection_get_by_type_state(int type, int state)
4885 CONN_GET_TEMPLATE(conn, conn->type == type && conn->state == state);
4889 * Return a connection of type <b>type</b> that is not an internally linked
4890 * connection, and is not marked for close.
4892 MOCK_IMPL(connection_t *,
4893 connection_get_by_type_nonlinked,(int type))
4895 CONN_GET_TEMPLATE(conn, conn->type == type && !conn->linked);
4898 /** Return a new smartlist of dir_connection_t * from get_connection_array()
4899 * that satisfy conn_test on connection_t *conn_var, and dirconn_test on
4900 * dir_connection_t *dirconn_var. conn_var must be of CONN_TYPE_DIR and not
4901 * marked for close to be included in the list. */
4902 #define DIR_CONN_LIST_TEMPLATE(conn_var, conn_test, \
4903 dirconn_var, dirconn_test) \
4904 STMT_BEGIN \
4905 smartlist_t *conns = get_connection_array(); \
4906 smartlist_t *dir_conns = smartlist_new(); \
4907 SMARTLIST_FOREACH_BEGIN(conns, connection_t *, conn_var) { \
4908 if (conn_var && (conn_test) \
4909 && conn_var->type == CONN_TYPE_DIR \
4910 && !conn_var->marked_for_close) { \
4911 dir_connection_t *dirconn_var = TO_DIR_CONN(conn_var); \
4912 if (dirconn_var && (dirconn_test)) { \
4913 smartlist_add(dir_conns, dirconn_var); \
4916 } SMARTLIST_FOREACH_END(conn_var); \
4917 return dir_conns; \
4918 STMT_END
4920 /** Return a list of directory connections that are fetching the item
4921 * described by <b>purpose</b>/<b>resource</b>. If there are none,
4922 * return an empty list. This list must be freed using smartlist_free,
4923 * but the pointers in it must not be freed.
4924 * Note that this list should not be cached, as the pointers in it can be
4925 * freed if their connections close. */
4926 smartlist_t *
4927 connection_dir_list_by_purpose_and_resource(
4928 int purpose,
4929 const char *resource)
4931 DIR_CONN_LIST_TEMPLATE(conn,
4932 conn->purpose == purpose,
4933 dirconn,
4934 0 == strcmp_opt(resource,
4935 dirconn->requested_resource));
4938 /** Return a list of directory connections that are fetching the item
4939 * described by <b>purpose</b>/<b>resource</b>/<b>state</b>. If there are
4940 * none, return an empty list. This list must be freed using smartlist_free,
4941 * but the pointers in it must not be freed.
4942 * Note that this list should not be cached, as the pointers in it can be
4943 * freed if their connections close. */
4944 smartlist_t *
4945 connection_dir_list_by_purpose_resource_and_state(
4946 int purpose,
4947 const char *resource,
4948 int state)
4950 DIR_CONN_LIST_TEMPLATE(conn,
4951 conn->purpose == purpose && conn->state == state,
4952 dirconn,
4953 0 == strcmp_opt(resource,
4954 dirconn->requested_resource));
4957 #undef DIR_CONN_LIST_TEMPLATE
4959 /** Return an arbitrary active OR connection that isn't <b>this_conn</b>.
4961 * We use this to guess if we should tell the controller that we
4962 * didn't manage to connect to any of our bridges. */
4963 static connection_t *
4964 connection_get_another_active_or_conn(const or_connection_t *this_conn)
4966 CONN_GET_TEMPLATE(conn,
4967 conn != TO_CONN(this_conn) && conn->type == CONN_TYPE_OR);
4970 /** Return 1 if there are any active OR connections apart from
4971 * <b>this_conn</b>.
4973 * We use this to guess if we should tell the controller that we
4974 * didn't manage to connect to any of our bridges. */
4976 any_other_active_or_conns(const or_connection_t *this_conn)
4978 connection_t *conn = connection_get_another_active_or_conn(this_conn);
4979 if (conn != NULL) {
4980 log_debug(LD_DIR, "%s: Found an OR connection: %s",
4981 __func__, connection_describe(conn));
4982 return 1;
4985 return 0;
4988 #undef CONN_GET_TEMPLATE
4990 /** Return 1 if <b>conn</b> is a listener conn, else return 0. */
4992 connection_is_listener(connection_t *conn)
4994 if (conn->type == CONN_TYPE_OR_LISTENER ||
4995 conn->type == CONN_TYPE_EXT_OR_LISTENER ||
4996 conn->type == CONN_TYPE_AP_LISTENER ||
4997 conn->type == CONN_TYPE_AP_TRANS_LISTENER ||
4998 conn->type == CONN_TYPE_AP_DNS_LISTENER ||
4999 conn->type == CONN_TYPE_AP_NATD_LISTENER ||
5000 conn->type == CONN_TYPE_AP_HTTP_CONNECT_LISTENER ||
5001 conn->type == CONN_TYPE_DIR_LISTENER ||
5002 conn->type == CONN_TYPE_METRICS_LISTENER ||
5003 conn->type == CONN_TYPE_CONTROL_LISTENER)
5004 return 1;
5005 return 0;
5008 /** Return 1 if <b>conn</b> is in state "open" and is not marked
5009 * for close, else return 0.
5012 connection_state_is_open(connection_t *conn)
5014 tor_assert(conn);
5016 if (conn->marked_for_close)
5017 return 0;
5019 if ((conn->type == CONN_TYPE_OR && conn->state == OR_CONN_STATE_OPEN) ||
5020 (conn->type == CONN_TYPE_EXT_OR) ||
5021 (conn->type == CONN_TYPE_AP && conn->state == AP_CONN_STATE_OPEN) ||
5022 (conn->type == CONN_TYPE_EXIT && conn->state == EXIT_CONN_STATE_OPEN) ||
5023 (conn->type == CONN_TYPE_CONTROL &&
5024 conn->state == CONTROL_CONN_STATE_OPEN))
5025 return 1;
5027 return 0;
5030 /** Return 1 if conn is in 'connecting' state, else return 0. */
5032 connection_state_is_connecting(connection_t *conn)
5034 tor_assert(conn);
5036 if (conn->marked_for_close)
5037 return 0;
5038 switch (conn->type)
5040 case CONN_TYPE_OR:
5041 return conn->state == OR_CONN_STATE_CONNECTING;
5042 case CONN_TYPE_EXIT:
5043 return conn->state == EXIT_CONN_STATE_CONNECTING;
5044 case CONN_TYPE_DIR:
5045 return conn->state == DIR_CONN_STATE_CONNECTING;
5048 return 0;
5051 /** Allocates a base64'ed authenticator for use in http or https
5052 * auth, based on the input string <b>authenticator</b>. Returns it
5053 * if success, else returns NULL. */
5054 char *
5055 alloc_http_authenticator(const char *authenticator)
5057 /* an authenticator in Basic authentication
5058 * is just the string "username:password" */
5059 const size_t authenticator_length = strlen(authenticator);
5060 const size_t base64_authenticator_length =
5061 base64_encode_size(authenticator_length, 0) + 1;
5062 char *base64_authenticator = tor_malloc(base64_authenticator_length);
5063 if (base64_encode(base64_authenticator, base64_authenticator_length,
5064 authenticator, authenticator_length, 0) < 0) {
5065 tor_free(base64_authenticator); /* free and set to null */
5067 return base64_authenticator;
5070 /** Given a socket handle, check whether the local address (sockname) of the
5071 * socket is one that we've connected from before. If so, double-check
5072 * whether our address has changed and we need to generate keys. If we do,
5073 * call init_keys().
5075 static void
5076 client_check_address_changed(tor_socket_t sock)
5078 tor_addr_t out_addr, iface_addr;
5079 tor_addr_t **last_interface_ip_ptr;
5080 sa_family_t family;
5082 if (!outgoing_addrs)
5083 outgoing_addrs = smartlist_new();
5085 if (tor_addr_from_getsockname(&out_addr, sock) < 0) {
5086 int e = tor_socket_errno(sock);
5087 log_warn(LD_NET, "getsockname() to check for address change failed: %s",
5088 tor_socket_strerror(e));
5089 return;
5091 family = tor_addr_family(&out_addr);
5093 if (family == AF_INET)
5094 last_interface_ip_ptr = &last_interface_ipv4;
5095 else if (family == AF_INET6)
5096 last_interface_ip_ptr = &last_interface_ipv6;
5097 else
5098 return;
5100 if (! *last_interface_ip_ptr) {
5101 tor_addr_t *a = tor_malloc_zero(sizeof(tor_addr_t));
5102 if (get_interface_address6(LOG_INFO, family, a)==0) {
5103 *last_interface_ip_ptr = a;
5104 } else {
5105 tor_free(a);
5109 /* If we've used this address previously, we're okay. */
5110 SMARTLIST_FOREACH(outgoing_addrs, const tor_addr_t *, a_ptr,
5111 if (tor_addr_eq(a_ptr, &out_addr))
5112 return;
5115 /* Uh-oh. We haven't connected from this address before. Has the interface
5116 * address changed? */
5117 if (get_interface_address6(LOG_INFO, family, &iface_addr)<0)
5118 return;
5120 if (tor_addr_eq(&iface_addr, *last_interface_ip_ptr)) {
5121 /* Nope, it hasn't changed. Add this address to the list. */
5122 smartlist_add(outgoing_addrs, tor_memdup(&out_addr, sizeof(tor_addr_t)));
5123 } else {
5124 /* The interface changed. We're a client, so we need to regenerate our
5125 * keys. First, reset the state. */
5126 log_notice(LD_NET, "Our IP address has changed. Rotating keys...");
5127 tor_addr_copy(*last_interface_ip_ptr, &iface_addr);
5128 SMARTLIST_FOREACH(outgoing_addrs, tor_addr_t*, a_ptr, tor_free(a_ptr));
5129 smartlist_clear(outgoing_addrs);
5130 smartlist_add(outgoing_addrs, tor_memdup(&out_addr, sizeof(tor_addr_t)));
5131 /* We'll need to resolve ourselves again. */
5132 resolved_addr_reset_last(AF_INET);
5133 /* Okay, now change our keys. */
5134 ip_address_changed(1);
5138 /** Some systems have limited system buffers for recv and xmit on
5139 * sockets allocated in a virtual server or similar environment. For a Tor
5140 * server this can produce the "Error creating network socket: No buffer
5141 * space available" error once all available TCP buffer space is consumed.
5142 * This method will attempt to constrain the buffers allocated for the socket
5143 * to the desired size to stay below system TCP buffer limits.
5145 static void
5146 set_constrained_socket_buffers(tor_socket_t sock, int size)
5148 void *sz = (void*)&size;
5149 socklen_t sz_sz = (socklen_t) sizeof(size);
5150 if (setsockopt(sock, SOL_SOCKET, SO_SNDBUF, sz, sz_sz) < 0) {
5151 int e = tor_socket_errno(sock);
5152 log_warn(LD_NET, "setsockopt() to constrain send "
5153 "buffer to %d bytes failed: %s", size, tor_socket_strerror(e));
5155 if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF, sz, sz_sz) < 0) {
5156 int e = tor_socket_errno(sock);
5157 log_warn(LD_NET, "setsockopt() to constrain recv "
5158 "buffer to %d bytes failed: %s", size, tor_socket_strerror(e));
5162 /** Process new bytes that have arrived on conn-\>inbuf.
5164 * This function just passes conn to the connection-specific
5165 * connection_*_process_inbuf() function. It also passes in
5166 * package_partial if wanted.
5168 static int
5169 connection_process_inbuf(connection_t *conn, int package_partial)
5171 tor_assert(conn);
5173 switch (conn->type) {
5174 case CONN_TYPE_OR:
5175 return connection_or_process_inbuf(TO_OR_CONN(conn));
5176 case CONN_TYPE_EXT_OR:
5177 return connection_ext_or_process_inbuf(TO_OR_CONN(conn));
5178 case CONN_TYPE_EXIT:
5179 case CONN_TYPE_AP:
5180 return connection_edge_process_inbuf(TO_EDGE_CONN(conn),
5181 package_partial);
5182 case CONN_TYPE_DIR:
5183 return connection_dir_process_inbuf(TO_DIR_CONN(conn));
5184 case CONN_TYPE_CONTROL:
5185 return connection_control_process_inbuf(TO_CONTROL_CONN(conn));
5186 case CONN_TYPE_METRICS:
5187 return metrics_connection_process_inbuf(conn);
5188 default:
5189 log_err(LD_BUG,"got unexpected conn type %d.", conn->type);
5190 tor_fragile_assert();
5191 return -1;
5195 /** Called whenever we've written data on a connection. */
5196 static int
5197 connection_flushed_some(connection_t *conn)
5199 int r = 0;
5200 tor_assert(!conn->in_flushed_some);
5201 conn->in_flushed_some = 1;
5202 if (conn->type == CONN_TYPE_DIR &&
5203 conn->state == DIR_CONN_STATE_SERVER_WRITING) {
5204 r = connection_dirserv_flushed_some(TO_DIR_CONN(conn));
5205 } else if (conn->type == CONN_TYPE_OR) {
5206 r = connection_or_flushed_some(TO_OR_CONN(conn));
5207 } else if (CONN_IS_EDGE(conn)) {
5208 r = connection_edge_flushed_some(TO_EDGE_CONN(conn));
5210 conn->in_flushed_some = 0;
5211 return r;
5214 /** We just finished flushing bytes to the appropriately low network layer,
5215 * and there are no more bytes remaining in conn-\>outbuf or
5216 * conn-\>tls to be flushed.
5218 * This function just passes conn to the connection-specific
5219 * connection_*_finished_flushing() function.
5221 static int
5222 connection_finished_flushing(connection_t *conn)
5224 tor_assert(conn);
5226 /* If the connection is closed, don't try to do anything more here. */
5227 if (CONN_IS_CLOSED(conn))
5228 return 0;
5230 // log_fn(LOG_DEBUG,"entered. Socket %u.", conn->s);
5232 connection_stop_writing(conn);
5234 switch (conn->type) {
5235 case CONN_TYPE_OR:
5236 return connection_or_finished_flushing(TO_OR_CONN(conn));
5237 case CONN_TYPE_EXT_OR:
5238 return connection_ext_or_finished_flushing(TO_OR_CONN(conn));
5239 case CONN_TYPE_AP:
5240 case CONN_TYPE_EXIT:
5241 return connection_edge_finished_flushing(TO_EDGE_CONN(conn));
5242 case CONN_TYPE_DIR:
5243 return connection_dir_finished_flushing(TO_DIR_CONN(conn));
5244 case CONN_TYPE_CONTROL:
5245 return connection_control_finished_flushing(TO_CONTROL_CONN(conn));
5246 case CONN_TYPE_METRICS:
5247 return metrics_connection_finished_flushing(conn);
5248 default:
5249 log_err(LD_BUG,"got unexpected conn type %d.", conn->type);
5250 tor_fragile_assert();
5251 return -1;
5255 /** Called when our attempt to connect() to a server has just succeeded.
5257 * This function checks if the interface address has changed (clients only),
5258 * and then passes conn to the connection-specific
5259 * connection_*_finished_connecting() function.
5261 static int
5262 connection_finished_connecting(connection_t *conn)
5264 tor_assert(conn);
5266 if (!server_mode(get_options())) {
5267 /* See whether getsockname() says our address changed. We need to do this
5268 * now that the connection has finished, because getsockname() on Windows
5269 * won't work until then. */
5270 client_check_address_changed(conn->s);
5273 switch (conn->type)
5275 case CONN_TYPE_OR:
5276 return connection_or_finished_connecting(TO_OR_CONN(conn));
5277 case CONN_TYPE_EXIT:
5278 return connection_edge_finished_connecting(TO_EDGE_CONN(conn));
5279 case CONN_TYPE_DIR:
5280 return connection_dir_finished_connecting(TO_DIR_CONN(conn));
5281 default:
5282 log_err(LD_BUG,"got unexpected conn type %d.", conn->type);
5283 tor_fragile_assert();
5284 return -1;
5288 /** Callback: invoked when a connection reaches an EOF event. */
5289 static int
5290 connection_reached_eof(connection_t *conn)
5292 switch (conn->type) {
5293 case CONN_TYPE_OR:
5294 case CONN_TYPE_EXT_OR:
5295 return connection_or_reached_eof(TO_OR_CONN(conn));
5296 case CONN_TYPE_AP:
5297 case CONN_TYPE_EXIT:
5298 return connection_edge_reached_eof(TO_EDGE_CONN(conn));
5299 case CONN_TYPE_DIR:
5300 return connection_dir_reached_eof(TO_DIR_CONN(conn));
5301 case CONN_TYPE_CONTROL:
5302 return connection_control_reached_eof(TO_CONTROL_CONN(conn));
5303 case CONN_TYPE_METRICS:
5304 return metrics_connection_reached_eof(conn);
5305 default:
5306 log_err(LD_BUG,"got unexpected conn type %d.", conn->type);
5307 tor_fragile_assert();
5308 return -1;
5312 /** Comparator for the two-orconn case in OOS victim sort */
5313 static int
5314 oos_victim_comparator_for_orconns(or_connection_t *a, or_connection_t *b)
5316 int a_circs, b_circs;
5317 /* Fewer circuits == higher priority for OOS kill, sort earlier */
5319 a_circs = connection_or_get_num_circuits(a);
5320 b_circs = connection_or_get_num_circuits(b);
5322 if (a_circs < b_circs) return 1;
5323 else if (a_circs > b_circs) return -1;
5324 else return 0;
5327 /** Sort comparator for OOS victims; better targets sort before worse
5328 * ones. */
5329 static int
5330 oos_victim_comparator(const void **a_v, const void **b_v)
5332 connection_t *a = NULL, *b = NULL;
5334 /* Get connection pointers out */
5336 a = (connection_t *)(*a_v);
5337 b = (connection_t *)(*b_v);
5339 tor_assert(a != NULL);
5340 tor_assert(b != NULL);
5343 * We always prefer orconns as victims currently; we won't even see
5344 * these non-orconn cases, but if we do, sort them after orconns.
5346 if (a->type == CONN_TYPE_OR && b->type == CONN_TYPE_OR) {
5347 return oos_victim_comparator_for_orconns(TO_OR_CONN(a), TO_OR_CONN(b));
5348 } else {
5350 * One isn't an orconn; if one is, it goes first. We currently have no
5351 * opinions about cases where neither is an orconn.
5353 if (a->type == CONN_TYPE_OR) return -1;
5354 else if (b->type == CONN_TYPE_OR) return 1;
5355 else return 0;
5359 /** Pick n victim connections for the OOS handler and return them in a
5360 * smartlist.
5362 MOCK_IMPL(STATIC smartlist_t *,
5363 pick_oos_victims, (int n))
5365 smartlist_t *eligible = NULL, *victims = NULL;
5366 smartlist_t *conns;
5367 int conn_counts_by_type[CONN_TYPE_MAX_ + 1], i;
5370 * Big damn assumption (someone improve this someday!):
5372 * Socket exhaustion normally happens on high-volume relays, and so
5373 * most of the connections involved are orconns. We should pick victims
5374 * by assembling a list of all orconns, and sorting them in order of
5375 * how much 'damage' by some metric we'd be doing by dropping them.
5377 * If we move on from orconns, we should probably think about incoming
5378 * directory connections next, or exit connections. Things we should
5379 * probably never kill are controller connections and listeners.
5381 * This function will count how many connections of different types
5382 * exist and log it for purposes of gathering data on typical OOS
5383 * situations to guide future improvements.
5386 /* First, get the connection array */
5387 conns = get_connection_array();
5389 * Iterate it and pick out eligible connection types, and log some stats
5390 * along the way.
5392 eligible = smartlist_new();
5393 memset(conn_counts_by_type, 0, sizeof(conn_counts_by_type));
5394 SMARTLIST_FOREACH_BEGIN(conns, connection_t *, c) {
5395 /* Bump the counter */
5396 tor_assert(c->type <= CONN_TYPE_MAX_);
5397 ++(conn_counts_by_type[c->type]);
5399 /* Skip anything without a socket we can free */
5400 if (!(SOCKET_OK(c->s))) {
5401 continue;
5404 /* Skip anything we would count as moribund */
5405 if (connection_is_moribund(c)) {
5406 continue;
5409 switch (c->type) {
5410 case CONN_TYPE_OR:
5411 /* We've got an orconn, it's eligible to be OOSed */
5412 smartlist_add(eligible, c);
5413 break;
5414 default:
5415 /* We don't know what to do with it, ignore it */
5416 break;
5418 } SMARTLIST_FOREACH_END(c);
5420 /* Log some stats */
5421 if (smartlist_len(conns) > 0) {
5422 /* At least one counter must be non-zero */
5423 log_info(LD_NET, "Some stats on conn types seen during OOS follow");
5424 for (i = CONN_TYPE_MIN_; i <= CONN_TYPE_MAX_; ++i) {
5425 /* Did we see any? */
5426 if (conn_counts_by_type[i] > 0) {
5427 log_info(LD_NET, "%s: %d conns",
5428 conn_type_to_string(i),
5429 conn_counts_by_type[i]);
5432 log_info(LD_NET, "Done with OOS conn type stats");
5435 /* Did we find more eligible targets than we want to kill? */
5436 if (smartlist_len(eligible) > n) {
5437 /* Sort the list in order of target preference */
5438 smartlist_sort(eligible, oos_victim_comparator);
5439 /* Pick first n as victims */
5440 victims = smartlist_new();
5441 for (i = 0; i < n; ++i) {
5442 smartlist_add(victims, smartlist_get(eligible, i));
5444 /* Free the original list */
5445 smartlist_free(eligible);
5446 } else {
5447 /* No, we can just call them all victims */
5448 victims = eligible;
5451 return victims;
5454 /** Kill a list of connections for the OOS handler. */
5455 MOCK_IMPL(STATIC void,
5456 kill_conn_list_for_oos, (smartlist_t *conns))
5458 if (!conns) return;
5460 SMARTLIST_FOREACH_BEGIN(conns, connection_t *, c) {
5461 /* Make sure the channel layer gets told about orconns */
5462 if (c->type == CONN_TYPE_OR) {
5463 connection_or_close_for_error(TO_OR_CONN(c), 1);
5464 } else {
5465 connection_mark_for_close(c);
5467 } SMARTLIST_FOREACH_END(c);
5469 log_notice(LD_NET,
5470 "OOS handler marked %d connections",
5471 smartlist_len(conns));
5474 /** Check if a connection is on the way out so the OOS handler doesn't try
5475 * to kill more than it needs. */
5477 connection_is_moribund(connection_t *conn)
5479 if (conn != NULL &&
5480 (conn->conn_array_index < 0 ||
5481 conn->marked_for_close)) {
5482 return 1;
5483 } else {
5484 return 0;
5488 /** Out-of-Sockets handler; n_socks is the current number of open
5489 * sockets, and failed is non-zero if a socket exhaustion related
5490 * error immediately preceded this call. This is where to do
5491 * circuit-killing heuristics as needed.
5493 void
5494 connection_check_oos(int n_socks, int failed)
5496 int target_n_socks = 0, moribund_socks, socks_to_kill;
5497 smartlist_t *conns;
5499 /* Early exit: is OOS checking disabled? */
5500 if (get_options()->DisableOOSCheck) {
5501 return;
5504 /* Sanity-check args */
5505 tor_assert(n_socks >= 0);
5508 * Make some log noise; keep it at debug level since this gets a chance
5509 * to run on every connection attempt.
5511 log_debug(LD_NET,
5512 "Running the OOS handler (%d open sockets, %s)",
5513 n_socks, (failed != 0) ? "exhaustion seen" : "no exhaustion");
5516 * Check if we're really handling an OOS condition, and if so decide how
5517 * many sockets we want to get down to. Be sure we check if the threshold
5518 * is distinct from zero first; it's possible for this to be called a few
5519 * times before we've finished reading the config.
5521 if (n_socks >= get_options()->ConnLimit_high_thresh &&
5522 get_options()->ConnLimit_high_thresh != 0 &&
5523 get_options()->ConnLimit_ != 0) {
5524 /* Try to get down to the low threshold */
5525 target_n_socks = get_options()->ConnLimit_low_thresh;
5526 log_notice(LD_NET,
5527 "Current number of sockets %d is greater than configured "
5528 "limit %d; OOS handler trying to get down to %d",
5529 n_socks, get_options()->ConnLimit_high_thresh,
5530 target_n_socks);
5531 } else if (failed) {
5533 * If we're not at the limit but we hit a socket exhaustion error, try to
5534 * drop some (but not as aggressively as ConnLimit_low_threshold, which is
5535 * 3/4 of ConnLimit_)
5537 target_n_socks = (n_socks * 9) / 10;
5538 log_notice(LD_NET,
5539 "We saw socket exhaustion at %d open sockets; OOS handler "
5540 "trying to get down to %d",
5541 n_socks, target_n_socks);
5544 if (target_n_socks > 0) {
5546 * It's an OOS!
5548 * Count moribund sockets; it's be important that anything we decide
5549 * to get rid of here but don't immediately close get counted as moribund
5550 * on subsequent invocations so we don't try to kill too many things if
5551 * connection_check_oos() gets called multiple times.
5553 moribund_socks = connection_count_moribund();
5555 if (moribund_socks < n_socks - target_n_socks) {
5556 socks_to_kill = n_socks - target_n_socks - moribund_socks;
5558 conns = pick_oos_victims(socks_to_kill);
5559 if (conns) {
5560 kill_conn_list_for_oos(conns);
5561 log_notice(LD_NET,
5562 "OOS handler killed %d conns", smartlist_len(conns));
5563 smartlist_free(conns);
5564 } else {
5565 log_notice(LD_NET, "OOS handler failed to pick any victim conns");
5567 } else {
5568 log_notice(LD_NET,
5569 "Not killing any sockets for OOS because there are %d "
5570 "already moribund, and we only want to eliminate %d",
5571 moribund_socks, n_socks - target_n_socks);
5576 /** Log how many bytes are used by buffers of different kinds and sizes. */
5577 void
5578 connection_dump_buffer_mem_stats(int severity)
5580 uint64_t used_by_type[CONN_TYPE_MAX_+1];
5581 uint64_t alloc_by_type[CONN_TYPE_MAX_+1];
5582 int n_conns_by_type[CONN_TYPE_MAX_+1];
5583 uint64_t total_alloc = 0;
5584 uint64_t total_used = 0;
5585 int i;
5586 smartlist_t *conns = get_connection_array();
5588 memset(used_by_type, 0, sizeof(used_by_type));
5589 memset(alloc_by_type, 0, sizeof(alloc_by_type));
5590 memset(n_conns_by_type, 0, sizeof(n_conns_by_type));
5592 SMARTLIST_FOREACH_BEGIN(conns, connection_t *, c) {
5593 int tp = c->type;
5594 ++n_conns_by_type[tp];
5595 if (c->inbuf) {
5596 used_by_type[tp] += buf_datalen(c->inbuf);
5597 alloc_by_type[tp] += buf_allocation(c->inbuf);
5599 if (c->outbuf) {
5600 used_by_type[tp] += buf_datalen(c->outbuf);
5601 alloc_by_type[tp] += buf_allocation(c->outbuf);
5603 } SMARTLIST_FOREACH_END(c);
5604 for (i=0; i <= CONN_TYPE_MAX_; ++i) {
5605 total_used += used_by_type[i];
5606 total_alloc += alloc_by_type[i];
5609 tor_log(severity, LD_GENERAL,
5610 "In buffers for %d connections: %"PRIu64" used/%"PRIu64" allocated",
5611 smartlist_len(conns),
5612 (total_used), (total_alloc));
5613 for (i=CONN_TYPE_MIN_; i <= CONN_TYPE_MAX_; ++i) {
5614 if (!n_conns_by_type[i])
5615 continue;
5616 tor_log(severity, LD_GENERAL,
5617 " For %d %s connections: %"PRIu64" used/%"PRIu64" allocated",
5618 n_conns_by_type[i], conn_type_to_string(i),
5619 (used_by_type[i]), (alloc_by_type[i]));
5623 /** Verify that connection <b>conn</b> has all of its invariants
5624 * correct. Trigger an assert if anything is invalid.
5626 void
5627 assert_connection_ok(connection_t *conn, time_t now)
5629 (void) now; /* XXXX unused. */
5630 tor_assert(conn);
5631 tor_assert(conn->type >= CONN_TYPE_MIN_);
5632 tor_assert(conn->type <= CONN_TYPE_MAX_);
5634 switch (conn->type) {
5635 case CONN_TYPE_OR:
5636 case CONN_TYPE_EXT_OR:
5637 tor_assert(conn->magic == OR_CONNECTION_MAGIC);
5638 break;
5639 case CONN_TYPE_AP:
5640 tor_assert(conn->magic == ENTRY_CONNECTION_MAGIC);
5641 break;
5642 case CONN_TYPE_EXIT:
5643 tor_assert(conn->magic == EDGE_CONNECTION_MAGIC);
5644 break;
5645 case CONN_TYPE_DIR:
5646 tor_assert(conn->magic == DIR_CONNECTION_MAGIC);
5647 break;
5648 case CONN_TYPE_CONTROL:
5649 tor_assert(conn->magic == CONTROL_CONNECTION_MAGIC);
5650 break;
5651 CASE_ANY_LISTENER_TYPE:
5652 tor_assert(conn->magic == LISTENER_CONNECTION_MAGIC);
5653 break;
5654 default:
5655 tor_assert(conn->magic == BASE_CONNECTION_MAGIC);
5656 break;
5659 if (conn->linked_conn) {
5660 tor_assert(conn->linked_conn->linked_conn == conn);
5661 tor_assert(conn->linked);
5663 if (conn->linked)
5664 tor_assert(!SOCKET_OK(conn->s));
5666 if (conn->hold_open_until_flushed)
5667 tor_assert(conn->marked_for_close);
5669 /* XXXX check: read_blocked_on_bw, write_blocked_on_bw, s, conn_array_index,
5670 * marked_for_close. */
5672 /* buffers */
5673 if (conn->inbuf)
5674 buf_assert_ok(conn->inbuf);
5675 if (conn->outbuf)
5676 buf_assert_ok(conn->outbuf);
5678 if (conn->type == CONN_TYPE_OR) {
5679 or_connection_t *or_conn = TO_OR_CONN(conn);
5680 if (conn->state == OR_CONN_STATE_OPEN) {
5681 /* tor_assert(conn->bandwidth > 0); */
5682 /* the above isn't necessarily true: if we just did a TLS
5683 * handshake but we didn't recognize the other peer, or it
5684 * gave a bad cert/etc, then we won't have assigned bandwidth,
5685 * yet it will be open. -RD
5687 // tor_assert(conn->read_bucket >= 0);
5689 // tor_assert(conn->addr && conn->port);
5690 tor_assert(conn->address);
5691 if (conn->state > OR_CONN_STATE_PROXY_HANDSHAKING)
5692 tor_assert(or_conn->tls);
5695 if (CONN_IS_EDGE(conn)) {
5696 /* XXX unchecked: package window, deliver window. */
5697 if (conn->type == CONN_TYPE_AP) {
5698 entry_connection_t *entry_conn = TO_ENTRY_CONN(conn);
5699 if (entry_conn->chosen_exit_optional || entry_conn->chosen_exit_retries)
5700 tor_assert(entry_conn->chosen_exit_name);
5702 tor_assert(entry_conn->socks_request);
5703 if (conn->state == AP_CONN_STATE_OPEN) {
5704 tor_assert(entry_conn->socks_request->has_finished);
5705 if (!conn->marked_for_close) {
5706 tor_assert(ENTRY_TO_EDGE_CONN(entry_conn)->cpath_layer);
5707 cpath_assert_layer_ok(ENTRY_TO_EDGE_CONN(entry_conn)->cpath_layer);
5711 if (conn->type == CONN_TYPE_EXIT) {
5712 tor_assert(conn->purpose == EXIT_PURPOSE_CONNECT ||
5713 conn->purpose == EXIT_PURPOSE_RESOLVE);
5715 } else if (conn->type == CONN_TYPE_DIR) {
5716 } else {
5717 /* Purpose is only used for dir and exit types currently */
5718 tor_assert(!conn->purpose);
5721 switch (conn->type)
5723 CASE_ANY_LISTENER_TYPE:
5724 tor_assert(conn->state == LISTENER_STATE_READY);
5725 break;
5726 case CONN_TYPE_OR:
5727 tor_assert(conn->state >= OR_CONN_STATE_MIN_);
5728 tor_assert(conn->state <= OR_CONN_STATE_MAX_);
5729 break;
5730 case CONN_TYPE_EXT_OR:
5731 tor_assert(conn->state >= EXT_OR_CONN_STATE_MIN_);
5732 tor_assert(conn->state <= EXT_OR_CONN_STATE_MAX_);
5733 break;
5734 case CONN_TYPE_EXIT:
5735 tor_assert(conn->state >= EXIT_CONN_STATE_MIN_);
5736 tor_assert(conn->state <= EXIT_CONN_STATE_MAX_);
5737 tor_assert(conn->purpose >= EXIT_PURPOSE_MIN_);
5738 tor_assert(conn->purpose <= EXIT_PURPOSE_MAX_);
5739 break;
5740 case CONN_TYPE_AP:
5741 tor_assert(conn->state >= AP_CONN_STATE_MIN_);
5742 tor_assert(conn->state <= AP_CONN_STATE_MAX_);
5743 tor_assert(TO_ENTRY_CONN(conn)->socks_request);
5744 break;
5745 case CONN_TYPE_DIR:
5746 tor_assert(conn->state >= DIR_CONN_STATE_MIN_);
5747 tor_assert(conn->state <= DIR_CONN_STATE_MAX_);
5748 tor_assert(conn->purpose >= DIR_PURPOSE_MIN_);
5749 tor_assert(conn->purpose <= DIR_PURPOSE_MAX_);
5750 break;
5751 case CONN_TYPE_CONTROL:
5752 tor_assert(conn->state >= CONTROL_CONN_STATE_MIN_);
5753 tor_assert(conn->state <= CONTROL_CONN_STATE_MAX_);
5754 break;
5755 case CONN_TYPE_METRICS:
5756 /* No state. */
5757 break;
5758 default:
5759 tor_assert(0);
5763 /** Fills <b>addr</b> and <b>port</b> with the details of the global
5764 * proxy server we are using. Store a 1 to the int pointed to by
5765 * <b>is_put_out</b> if the connection is using a pluggable
5766 * transport; store 0 otherwise. <b>conn</b> contains the connection
5767 * we are using the proxy for.
5769 * Return 0 on success, -1 on failure.
5772 get_proxy_addrport(tor_addr_t *addr, uint16_t *port, int *proxy_type,
5773 int *is_pt_out, const connection_t *conn)
5775 const or_options_t *options = get_options();
5777 *is_pt_out = 0;
5778 /* Client Transport Plugins can use another proxy, but that should be hidden
5779 * from the rest of tor (as the plugin is responsible for dealing with the
5780 * proxy), check it first, then check the rest of the proxy types to allow
5781 * the config to have unused ClientTransportPlugin entries.
5783 if (options->ClientTransportPlugin) {
5784 const transport_t *transport = NULL;
5785 int r;
5786 r = get_transport_by_bridge_addrport(&conn->addr, conn->port, &transport);
5787 if (r<0)
5788 return -1;
5789 if (transport) { /* transport found */
5790 tor_addr_copy(addr, &transport->addr);
5791 *port = transport->port;
5792 *proxy_type = transport->socks_version;
5793 *is_pt_out = 1;
5794 return 0;
5797 /* Unused ClientTransportPlugin. */
5800 if (options->HTTPSProxy) {
5801 tor_addr_copy(addr, &options->HTTPSProxyAddr);
5802 *port = options->HTTPSProxyPort;
5803 *proxy_type = PROXY_CONNECT;
5804 return 0;
5805 } else if (options->Socks4Proxy) {
5806 tor_addr_copy(addr, &options->Socks4ProxyAddr);
5807 *port = options->Socks4ProxyPort;
5808 *proxy_type = PROXY_SOCKS4;
5809 return 0;
5810 } else if (options->Socks5Proxy) {
5811 tor_addr_copy(addr, &options->Socks5ProxyAddr);
5812 *port = options->Socks5ProxyPort;
5813 *proxy_type = PROXY_SOCKS5;
5814 return 0;
5815 } else if (options->TCPProxy) {
5816 tor_addr_copy(addr, &options->TCPProxyAddr);
5817 *port = options->TCPProxyPort;
5818 /* The only supported protocol in TCPProxy is haproxy. */
5819 tor_assert(options->TCPProxyProtocol == TCP_PROXY_PROTOCOL_HAPROXY);
5820 *proxy_type = PROXY_HAPROXY;
5821 return 0;
5824 tor_addr_make_unspec(addr);
5825 *port = 0;
5826 *proxy_type = PROXY_NONE;
5827 return 0;
5830 /** Log a failed connection to a proxy server.
5831 * <b>conn</b> is the connection we use the proxy server for. */
5832 void
5833 log_failed_proxy_connection(connection_t *conn)
5835 tor_addr_t proxy_addr;
5836 uint16_t proxy_port;
5837 int proxy_type, is_pt;
5839 if (get_proxy_addrport(&proxy_addr, &proxy_port, &proxy_type, &is_pt,
5840 conn) != 0)
5841 return; /* if we have no proxy set up, leave this function. */
5843 (void)is_pt;
5844 log_warn(LD_NET,
5845 "The connection to the %s proxy server at %s just failed. "
5846 "Make sure that the proxy server is up and running.",
5847 proxy_type_to_string(proxy_type),
5848 fmt_addrport(&proxy_addr, proxy_port));
5851 /** Return string representation of <b>proxy_type</b>. */
5852 static const char *
5853 proxy_type_to_string(int proxy_type)
5855 switch (proxy_type) {
5856 case PROXY_CONNECT: return "HTTP";
5857 case PROXY_SOCKS4: return "SOCKS4";
5858 case PROXY_SOCKS5: return "SOCKS5";
5859 case PROXY_HAPROXY: return "HAPROXY";
5860 case PROXY_PLUGGABLE: return "pluggable transports SOCKS";
5861 case PROXY_NONE: return "NULL";
5862 default: tor_assert(0);
5864 return NULL; /*Unreached*/
5867 /** Call connection_free_minimal() on every connection in our array, and
5868 * release all storage held by connection.c.
5870 * Don't do the checks in connection_free(), because they will
5871 * fail.
5873 void
5874 connection_free_all(void)
5876 smartlist_t *conns = get_connection_array();
5878 /* We don't want to log any messages to controllers. */
5879 SMARTLIST_FOREACH(conns, connection_t *, conn,
5880 if (conn->type == CONN_TYPE_CONTROL)
5881 TO_CONTROL_CONN(conn)->event_mask = 0);
5883 control_update_global_event_mask();
5885 /* Unlink everything from the identity map. */
5886 connection_or_clear_identity_map();
5888 /* Clear out our list of broken connections */
5889 clear_broken_connection_map(0);
5891 SMARTLIST_FOREACH(conns, connection_t *, conn,
5892 connection_free_minimal(conn));
5894 if (outgoing_addrs) {
5895 SMARTLIST_FOREACH(outgoing_addrs, tor_addr_t *, addr, tor_free(addr));
5896 smartlist_free(outgoing_addrs);
5897 outgoing_addrs = NULL;
5900 tor_free(last_interface_ipv4);
5901 tor_free(last_interface_ipv6);
5902 last_recorded_accounting_at = 0;
5904 mainloop_event_free(reenable_blocked_connections_ev);
5905 reenable_blocked_connections_is_scheduled = 0;
5906 memset(&reenable_blocked_connections_delay, 0, sizeof(struct timeval));
5909 /** Log a warning, and possibly emit a control event, that <b>received</b> came
5910 * at a skewed time. <b>trusted</b> indicates that the <b>source</b> was one
5911 * that we had more faith in and therefore the warning level should have higher
5912 * severity.
5914 MOCK_IMPL(void,
5915 clock_skew_warning, (const connection_t *conn, long apparent_skew, int trusted,
5916 log_domain_mask_t domain, const char *received,
5917 const char *source))
5919 char dbuf[64];
5920 char *ext_source = NULL, *warn = NULL;
5921 format_time_interval(dbuf, sizeof(dbuf), apparent_skew);
5922 if (conn)
5923 tor_asprintf(&ext_source, "%s:%s:%d", source,
5924 fmt_and_decorate_addr(&conn->addr), conn->port);
5925 else
5926 ext_source = tor_strdup(source);
5927 log_fn(trusted ? LOG_WARN : LOG_INFO, domain,
5928 "Received %s with skewed time (%s): "
5929 "It seems that our clock is %s by %s, or that theirs is %s%s. "
5930 "Tor requires an accurate clock to work: please check your time, "
5931 "timezone, and date settings.", received, ext_source,
5932 apparent_skew > 0 ? "ahead" : "behind", dbuf,
5933 apparent_skew > 0 ? "behind" : "ahead",
5934 (!conn || trusted) ? "" : ", or they are sending us the wrong time");
5935 if (trusted) {
5936 control_event_general_status(LOG_WARN, "CLOCK_SKEW SKEW=%ld SOURCE=%s",
5937 apparent_skew, ext_source);
5938 tor_asprintf(&warn, "Clock skew %ld in %s from %s", apparent_skew,
5939 received, source);
5940 control_event_bootstrap_problem(warn, "CLOCK_SKEW", conn, 1);
5942 tor_free(warn);
5943 tor_free(ext_source);