Remove/fix remaining evbuffer references
[tor.git] / src / or / connection.c
blob68e442df54fb377384ab03732a07468a155c2862
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-2016, 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.
11 **/
13 #define CONNECTION_PRIVATE
14 #include "or.h"
15 #include "buffers.h"
17 * Define this so we get channel internal functions, since we're implementing
18 * part of a subclass (channel_tls_t).
20 #define TOR_CHANNEL_INTERNAL_
21 #define CONNECTION_PRIVATE
22 #include "backtrace.h"
23 #include "channel.h"
24 #include "channeltls.h"
25 #include "circuitbuild.h"
26 #include "circuitlist.h"
27 #include "circuituse.h"
28 #include "config.h"
29 #include "connection.h"
30 #include "connection_edge.h"
31 #include "connection_or.h"
32 #include "control.h"
33 #include "directory.h"
34 #include "dirserv.h"
35 #include "dns.h"
36 #include "dnsserv.h"
37 #include "entrynodes.h"
38 #include "ext_orport.h"
39 #include "geoip.h"
40 #include "main.h"
41 #include "nodelist.h"
42 #include "policies.h"
43 #include "reasons.h"
44 #include "relay.h"
45 #include "rendclient.h"
46 #include "rendcommon.h"
47 #include "rephist.h"
48 #include "router.h"
49 #include "routerlist.h"
50 #include "transports.h"
51 #include "routerparse.h"
52 #include "sandbox.h"
53 #include "transports.h"
55 #ifdef HAVE_PWD_H
56 #include <pwd.h>
57 #endif
59 #ifdef HAVE_SYS_UN_H
60 #include <sys/socket.h>
61 #include <sys/un.h>
62 #endif
64 static connection_t *connection_listener_new(
65 const struct sockaddr *listensockaddr,
66 socklen_t listensocklen, int type,
67 const char *address,
68 const port_cfg_t *portcfg);
69 static void connection_init(time_t now, connection_t *conn, int type,
70 int socket_family);
71 static int connection_init_accepted_conn(connection_t *conn,
72 const listener_connection_t *listener);
73 static int connection_handle_listener_read(connection_t *conn, int new_type);
74 static int connection_bucket_should_increase(int bucket,
75 or_connection_t *conn);
76 static int connection_finished_flushing(connection_t *conn);
77 static int connection_flushed_some(connection_t *conn);
78 static int connection_finished_connecting(connection_t *conn);
79 static int connection_reached_eof(connection_t *conn);
80 static int connection_read_to_buf(connection_t *conn, ssize_t *max_to_read,
81 int *socket_error);
82 static int connection_process_inbuf(connection_t *conn, int package_partial);
83 static void client_check_address_changed(tor_socket_t sock);
84 static void set_constrained_socket_buffers(tor_socket_t sock, int size);
86 static const char *connection_proxy_state_to_string(int state);
87 static int connection_read_https_proxy_response(connection_t *conn);
88 static void connection_send_socks5_connect(connection_t *conn);
89 static const char *proxy_type_to_string(int proxy_type);
90 static int get_proxy_type(void);
92 /** The last addresses that our network interface seemed to have been
93 * binding to. We use this as one way to detect when our IP changes.
95 * XXXX+ We should really use the entire list of interfaces here.
96 **/
97 static tor_addr_t *last_interface_ipv4 = NULL;
98 /* DOCDOC last_interface_ipv6 */
99 static tor_addr_t *last_interface_ipv6 = NULL;
100 /** A list of tor_addr_t for addresses we've used in outgoing connections.
101 * Used to detect IP address changes. */
102 static smartlist_t *outgoing_addrs = NULL;
104 #define CASE_ANY_LISTENER_TYPE \
105 case CONN_TYPE_OR_LISTENER: \
106 case CONN_TYPE_EXT_OR_LISTENER: \
107 case CONN_TYPE_AP_LISTENER: \
108 case CONN_TYPE_DIR_LISTENER: \
109 case CONN_TYPE_CONTROL_LISTENER: \
110 case CONN_TYPE_AP_TRANS_LISTENER: \
111 case CONN_TYPE_AP_NATD_LISTENER: \
112 case CONN_TYPE_AP_DNS_LISTENER
114 /**************************************************************/
117 * Return the human-readable name for the connection type <b>type</b>
119 const char *
120 conn_type_to_string(int type)
122 static char buf[64];
123 switch (type) {
124 case CONN_TYPE_OR_LISTENER: return "OR listener";
125 case CONN_TYPE_OR: return "OR";
126 case CONN_TYPE_EXIT: return "Exit";
127 case CONN_TYPE_AP_LISTENER: return "Socks listener";
128 case CONN_TYPE_AP_TRANS_LISTENER:
129 return "Transparent pf/netfilter listener";
130 case CONN_TYPE_AP_NATD_LISTENER: return "Transparent natd listener";
131 case CONN_TYPE_AP_DNS_LISTENER: return "DNS listener";
132 case CONN_TYPE_AP: return "Socks";
133 case CONN_TYPE_DIR_LISTENER: return "Directory listener";
134 case CONN_TYPE_DIR: return "Directory";
135 case CONN_TYPE_CONTROL_LISTENER: return "Control listener";
136 case CONN_TYPE_CONTROL: return "Control";
137 case CONN_TYPE_EXT_OR: return "Extended OR";
138 case CONN_TYPE_EXT_OR_LISTENER: return "Extended OR listener";
139 default:
140 log_warn(LD_BUG, "unknown connection type %d", type);
141 tor_snprintf(buf, sizeof(buf), "unknown [%d]", type);
142 return buf;
147 * Return the human-readable name for the connection state <b>state</b>
148 * for the connection type <b>type</b>
150 const char *
151 conn_state_to_string(int type, int state)
153 static char buf[96];
154 switch (type) {
155 CASE_ANY_LISTENER_TYPE:
156 if (state == LISTENER_STATE_READY)
157 return "ready";
158 break;
159 case CONN_TYPE_OR:
160 switch (state) {
161 case OR_CONN_STATE_CONNECTING: return "connect()ing";
162 case OR_CONN_STATE_PROXY_HANDSHAKING: return "handshaking (proxy)";
163 case OR_CONN_STATE_TLS_HANDSHAKING: return "handshaking (TLS)";
164 case OR_CONN_STATE_TLS_CLIENT_RENEGOTIATING:
165 return "renegotiating (TLS, v2 handshake)";
166 case OR_CONN_STATE_TLS_SERVER_RENEGOTIATING:
167 return "waiting for renegotiation or V3 handshake";
168 case OR_CONN_STATE_OR_HANDSHAKING_V2:
169 return "handshaking (Tor, v2 handshake)";
170 case OR_CONN_STATE_OR_HANDSHAKING_V3:
171 return "handshaking (Tor, v3 handshake)";
172 case OR_CONN_STATE_OPEN: return "open";
174 break;
175 case CONN_TYPE_EXT_OR:
176 switch (state) {
177 case EXT_OR_CONN_STATE_AUTH_WAIT_AUTH_TYPE:
178 return "waiting for authentication type";
179 case EXT_OR_CONN_STATE_AUTH_WAIT_CLIENT_NONCE:
180 return "waiting for client nonce";
181 case EXT_OR_CONN_STATE_AUTH_WAIT_CLIENT_HASH:
182 return "waiting for client hash";
183 case EXT_OR_CONN_STATE_OPEN: return "open";
184 case EXT_OR_CONN_STATE_FLUSHING: return "flushing final OKAY";
186 break;
187 case CONN_TYPE_EXIT:
188 switch (state) {
189 case EXIT_CONN_STATE_RESOLVING: return "waiting for dest info";
190 case EXIT_CONN_STATE_CONNECTING: return "connecting";
191 case EXIT_CONN_STATE_OPEN: return "open";
192 case EXIT_CONN_STATE_RESOLVEFAILED: return "resolve failed";
194 break;
195 case CONN_TYPE_AP:
196 switch (state) {
197 case AP_CONN_STATE_SOCKS_WAIT: return "waiting for socks info";
198 case AP_CONN_STATE_NATD_WAIT: return "waiting for natd dest info";
199 case AP_CONN_STATE_RENDDESC_WAIT: return "waiting for rendezvous desc";
200 case AP_CONN_STATE_CONTROLLER_WAIT: return "waiting for controller";
201 case AP_CONN_STATE_CIRCUIT_WAIT: return "waiting for circuit";
202 case AP_CONN_STATE_CONNECT_WAIT: return "waiting for connect response";
203 case AP_CONN_STATE_RESOLVE_WAIT: return "waiting for resolve response";
204 case AP_CONN_STATE_OPEN: return "open";
206 break;
207 case CONN_TYPE_DIR:
208 switch (state) {
209 case DIR_CONN_STATE_CONNECTING: return "connecting";
210 case DIR_CONN_STATE_CLIENT_SENDING: return "client sending";
211 case DIR_CONN_STATE_CLIENT_READING: return "client reading";
212 case DIR_CONN_STATE_CLIENT_FINISHED: return "client finished";
213 case DIR_CONN_STATE_SERVER_COMMAND_WAIT: return "waiting for command";
214 case DIR_CONN_STATE_SERVER_WRITING: return "writing";
216 break;
217 case CONN_TYPE_CONTROL:
218 switch (state) {
219 case CONTROL_CONN_STATE_OPEN: return "open (protocol v1)";
220 case CONTROL_CONN_STATE_NEEDAUTH:
221 return "waiting for authentication (protocol v1)";
223 break;
226 log_warn(LD_BUG, "unknown connection state %d (type %d)", state, type);
227 tor_snprintf(buf, sizeof(buf),
228 "unknown state [%d] on unknown [%s] connection",
229 state, conn_type_to_string(type));
230 return buf;
233 /** Allocate and return a new dir_connection_t, initialized as by
234 * connection_init(). */
235 dir_connection_t *
236 dir_connection_new(int socket_family)
238 dir_connection_t *dir_conn = tor_malloc_zero(sizeof(dir_connection_t));
239 connection_init(time(NULL), TO_CONN(dir_conn), CONN_TYPE_DIR, socket_family);
240 return dir_conn;
243 /** Allocate and return a new or_connection_t, initialized as by
244 * connection_init().
246 * Initialize active_circuit_pqueue.
248 * Set active_circuit_pqueue_last_recalibrated to current cell_ewma tick.
250 or_connection_t *
251 or_connection_new(int type, int socket_family)
253 or_connection_t *or_conn = tor_malloc_zero(sizeof(or_connection_t));
254 time_t now = time(NULL);
255 tor_assert(type == CONN_TYPE_OR || type == CONN_TYPE_EXT_OR);
256 connection_init(now, TO_CONN(or_conn), type, socket_family);
258 connection_or_set_canonical(or_conn, 0);
260 if (type == CONN_TYPE_EXT_OR)
261 connection_or_set_ext_or_identifier(or_conn);
263 return or_conn;
266 /** Allocate and return a new entry_connection_t, initialized as by
267 * connection_init().
269 * Allocate space to store the socks_request.
271 entry_connection_t *
272 entry_connection_new(int type, int socket_family)
274 entry_connection_t *entry_conn = tor_malloc_zero(sizeof(entry_connection_t));
275 tor_assert(type == CONN_TYPE_AP);
276 connection_init(time(NULL), ENTRY_TO_CONN(entry_conn), type, socket_family);
277 entry_conn->socks_request = socks_request_new();
278 /* If this is coming from a listener, we'll set it up based on the listener
279 * in a little while. Otherwise, we're doing this as a linked connection
280 * of some kind, and we should set it up here based on the socket family */
281 if (socket_family == AF_INET)
282 entry_conn->entry_cfg.ipv4_traffic = 1;
283 else if (socket_family == AF_INET6)
284 entry_conn->entry_cfg.ipv6_traffic = 1;
285 else if (socket_family == AF_UNIX)
286 entry_conn->is_socks_socket = 1;
287 return entry_conn;
290 /** Allocate and return a new edge_connection_t, initialized as by
291 * connection_init(). */
292 edge_connection_t *
293 edge_connection_new(int type, int socket_family)
295 edge_connection_t *edge_conn = tor_malloc_zero(sizeof(edge_connection_t));
296 tor_assert(type == CONN_TYPE_EXIT);
297 connection_init(time(NULL), TO_CONN(edge_conn), type, socket_family);
298 return edge_conn;
301 /** Allocate and return a new control_connection_t, initialized as by
302 * connection_init(). */
303 control_connection_t *
304 control_connection_new(int socket_family)
306 control_connection_t *control_conn =
307 tor_malloc_zero(sizeof(control_connection_t));
308 connection_init(time(NULL),
309 TO_CONN(control_conn), CONN_TYPE_CONTROL, socket_family);
310 return control_conn;
313 /** Allocate and return a new listener_connection_t, initialized as by
314 * connection_init(). */
315 listener_connection_t *
316 listener_connection_new(int type, int socket_family)
318 listener_connection_t *listener_conn =
319 tor_malloc_zero(sizeof(listener_connection_t));
320 connection_init(time(NULL), TO_CONN(listener_conn), type, socket_family);
321 return listener_conn;
324 /** Allocate, initialize, and return a new connection_t subtype of <b>type</b>
325 * to make or receive connections of address family <b>socket_family</b>. The
326 * type should be one of the CONN_TYPE_* constants. */
327 connection_t *
328 connection_new(int type, int socket_family)
330 switch (type) {
331 case CONN_TYPE_OR:
332 case CONN_TYPE_EXT_OR:
333 return TO_CONN(or_connection_new(type, socket_family));
335 case CONN_TYPE_EXIT:
336 return TO_CONN(edge_connection_new(type, socket_family));
338 case CONN_TYPE_AP:
339 return ENTRY_TO_CONN(entry_connection_new(type, socket_family));
341 case CONN_TYPE_DIR:
342 return TO_CONN(dir_connection_new(socket_family));
344 case CONN_TYPE_CONTROL:
345 return TO_CONN(control_connection_new(socket_family));
347 CASE_ANY_LISTENER_TYPE:
348 return TO_CONN(listener_connection_new(type, socket_family));
350 default: {
351 connection_t *conn = tor_malloc_zero(sizeof(connection_t));
352 connection_init(time(NULL), conn, type, socket_family);
353 return conn;
358 /** Initializes conn. (you must call connection_add() to link it into the main
359 * array).
361 * Set conn-\>magic to the correct value.
363 * Set conn-\>type to <b>type</b>. Set conn-\>s and conn-\>conn_array_index to
364 * -1 to signify they are not yet assigned.
366 * Initialize conn's timestamps to now.
368 static void
369 connection_init(time_t now, connection_t *conn, int type, int socket_family)
371 static uint64_t n_connections_allocated = 1;
373 switch (type) {
374 case CONN_TYPE_OR:
375 case CONN_TYPE_EXT_OR:
376 conn->magic = OR_CONNECTION_MAGIC;
377 break;
378 case CONN_TYPE_EXIT:
379 conn->magic = EDGE_CONNECTION_MAGIC;
380 break;
381 case CONN_TYPE_AP:
382 conn->magic = ENTRY_CONNECTION_MAGIC;
383 break;
384 case CONN_TYPE_DIR:
385 conn->magic = DIR_CONNECTION_MAGIC;
386 break;
387 case CONN_TYPE_CONTROL:
388 conn->magic = CONTROL_CONNECTION_MAGIC;
389 break;
390 CASE_ANY_LISTENER_TYPE:
391 conn->magic = LISTENER_CONNECTION_MAGIC;
392 break;
393 default:
394 conn->magic = BASE_CONNECTION_MAGIC;
395 break;
398 conn->s = TOR_INVALID_SOCKET; /* give it a default of 'not used' */
399 conn->conn_array_index = -1; /* also default to 'not used' */
400 conn->global_identifier = n_connections_allocated++;
402 conn->type = type;
403 conn->socket_family = socket_family;
404 if (!connection_is_listener(conn)) {
405 /* listeners never use their buf */
406 conn->inbuf = buf_new();
407 conn->outbuf = buf_new();
410 conn->timestamp_created = now;
411 conn->timestamp_lastread = now;
412 conn->timestamp_lastwritten = now;
415 /** Create a link between <b>conn_a</b> and <b>conn_b</b>. */
416 void
417 connection_link_connections(connection_t *conn_a, connection_t *conn_b)
419 tor_assert(! SOCKET_OK(conn_a->s));
420 tor_assert(! SOCKET_OK(conn_b->s));
422 conn_a->linked = 1;
423 conn_b->linked = 1;
424 conn_a->linked_conn = conn_b;
425 conn_b->linked_conn = conn_a;
428 /** Return true iff the provided connection listener type supports AF_UNIX
429 * sockets. */
431 conn_listener_type_supports_af_unix(int type)
433 /* For now only control ports or SOCKS ports can be Unix domain sockets
434 * and listeners at the same time */
435 switch (type) {
436 case CONN_TYPE_CONTROL_LISTENER:
437 case CONN_TYPE_AP_LISTENER:
438 return 1;
439 default:
440 return 0;
444 /** Deallocate memory used by <b>conn</b>. Deallocate its buffers if
445 * necessary, close its socket if necessary, and mark the directory as dirty
446 * if <b>conn</b> is an OR or OP connection.
448 STATIC void
449 connection_free_(connection_t *conn)
451 void *mem;
452 size_t memlen;
453 if (!conn)
454 return;
456 switch (conn->type) {
457 case CONN_TYPE_OR:
458 case CONN_TYPE_EXT_OR:
459 tor_assert(conn->magic == OR_CONNECTION_MAGIC);
460 mem = TO_OR_CONN(conn);
461 memlen = sizeof(or_connection_t);
462 break;
463 case CONN_TYPE_AP:
464 tor_assert(conn->magic == ENTRY_CONNECTION_MAGIC);
465 mem = TO_ENTRY_CONN(conn);
466 memlen = sizeof(entry_connection_t);
467 break;
468 case CONN_TYPE_EXIT:
469 tor_assert(conn->magic == EDGE_CONNECTION_MAGIC);
470 mem = TO_EDGE_CONN(conn);
471 memlen = sizeof(edge_connection_t);
472 break;
473 case CONN_TYPE_DIR:
474 tor_assert(conn->magic == DIR_CONNECTION_MAGIC);
475 mem = TO_DIR_CONN(conn);
476 memlen = sizeof(dir_connection_t);
477 break;
478 case CONN_TYPE_CONTROL:
479 tor_assert(conn->magic == CONTROL_CONNECTION_MAGIC);
480 mem = TO_CONTROL_CONN(conn);
481 memlen = sizeof(control_connection_t);
482 break;
483 CASE_ANY_LISTENER_TYPE:
484 tor_assert(conn->magic == LISTENER_CONNECTION_MAGIC);
485 mem = TO_LISTENER_CONN(conn);
486 memlen = sizeof(listener_connection_t);
487 break;
488 default:
489 tor_assert(conn->magic == BASE_CONNECTION_MAGIC);
490 mem = conn;
491 memlen = sizeof(connection_t);
492 break;
495 if (conn->linked) {
496 log_info(LD_GENERAL, "Freeing linked %s connection [%s] with %d "
497 "bytes on inbuf, %d on outbuf.",
498 conn_type_to_string(conn->type),
499 conn_state_to_string(conn->type, conn->state),
500 (int)connection_get_inbuf_len(conn),
501 (int)connection_get_outbuf_len(conn));
504 if (!connection_is_listener(conn)) {
505 buf_free(conn->inbuf);
506 buf_free(conn->outbuf);
507 } else {
508 if (conn->socket_family == AF_UNIX) {
509 /* For now only control and SOCKS ports can be Unix domain sockets
510 * and listeners at the same time */
511 tor_assert(conn_listener_type_supports_af_unix(conn->type));
513 if (unlink(conn->address) < 0 && errno != ENOENT) {
514 log_warn(LD_NET, "Could not unlink %s: %s", conn->address,
515 strerror(errno));
520 tor_free(conn->address);
522 if (connection_speaks_cells(conn)) {
523 or_connection_t *or_conn = TO_OR_CONN(conn);
524 tor_tls_free(or_conn->tls);
525 or_conn->tls = NULL;
526 or_handshake_state_free(or_conn->handshake_state);
527 or_conn->handshake_state = NULL;
528 tor_free(or_conn->nickname);
529 if (or_conn->chan) {
530 /* Owww, this shouldn't happen, but... */
531 log_info(LD_CHANNEL,
532 "Freeing orconn at %p, saw channel %p with ID "
533 U64_FORMAT " left un-NULLed",
534 or_conn, TLS_CHAN_TO_BASE(or_conn->chan),
535 U64_PRINTF_ARG(
536 TLS_CHAN_TO_BASE(or_conn->chan)->global_identifier));
537 if (!CHANNEL_FINISHED(TLS_CHAN_TO_BASE(or_conn->chan))) {
538 channel_close_for_error(TLS_CHAN_TO_BASE(or_conn->chan));
541 or_conn->chan->conn = NULL;
542 or_conn->chan = NULL;
545 if (conn->type == CONN_TYPE_AP) {
546 entry_connection_t *entry_conn = TO_ENTRY_CONN(conn);
547 tor_free(entry_conn->chosen_exit_name);
548 tor_free(entry_conn->original_dest_address);
549 if (entry_conn->socks_request)
550 socks_request_free(entry_conn->socks_request);
551 if (entry_conn->pending_optimistic_data) {
552 buf_free(entry_conn->pending_optimistic_data);
554 if (entry_conn->sending_optimistic_data) {
555 buf_free(entry_conn->sending_optimistic_data);
558 if (CONN_IS_EDGE(conn)) {
559 rend_data_free(TO_EDGE_CONN(conn)->rend_data);
561 if (conn->type == CONN_TYPE_CONTROL) {
562 control_connection_t *control_conn = TO_CONTROL_CONN(conn);
563 tor_free(control_conn->safecookie_client_hash);
564 tor_free(control_conn->incoming_cmd);
565 if (control_conn->ephemeral_onion_services) {
566 SMARTLIST_FOREACH(control_conn->ephemeral_onion_services, char *, cp, {
567 memwipe(cp, 0, strlen(cp));
568 tor_free(cp);
570 smartlist_free(control_conn->ephemeral_onion_services);
574 /* Probably already freed by connection_free. */
575 tor_event_free(conn->read_event);
576 tor_event_free(conn->write_event);
577 conn->read_event = conn->write_event = NULL;
579 if (conn->type == CONN_TYPE_DIR) {
580 dir_connection_t *dir_conn = TO_DIR_CONN(conn);
581 tor_free(dir_conn->requested_resource);
583 tor_zlib_free(dir_conn->zlib_state);
584 if (dir_conn->fingerprint_stack) {
585 SMARTLIST_FOREACH(dir_conn->fingerprint_stack, char *, cp, tor_free(cp));
586 smartlist_free(dir_conn->fingerprint_stack);
589 cached_dir_decref(dir_conn->cached_dir);
590 rend_data_free(dir_conn->rend_data);
593 if (SOCKET_OK(conn->s)) {
594 log_debug(LD_NET,"closing fd %d.",(int)conn->s);
595 tor_close_socket(conn->s);
596 conn->s = TOR_INVALID_SOCKET;
599 if (conn->type == CONN_TYPE_OR &&
600 !tor_digest_is_zero(TO_OR_CONN(conn)->identity_digest)) {
601 log_warn(LD_BUG, "called on OR conn with non-zeroed identity_digest");
602 connection_or_remove_from_identity_map(TO_OR_CONN(conn));
604 if (conn->type == CONN_TYPE_OR || conn->type == CONN_TYPE_EXT_OR) {
605 connection_or_remove_from_ext_or_id_map(TO_OR_CONN(conn));
606 tor_free(TO_OR_CONN(conn)->ext_or_conn_id);
607 tor_free(TO_OR_CONN(conn)->ext_or_auth_correct_client_hash);
608 tor_free(TO_OR_CONN(conn)->ext_or_transport);
611 memwipe(mem, 0xCC, memlen); /* poison memory */
612 tor_free(mem);
615 /** Make sure <b>conn</b> isn't in any of the global conn lists; then free it.
617 MOCK_IMPL(void,
618 connection_free,(connection_t *conn))
620 if (!conn)
621 return;
622 tor_assert(!connection_is_on_closeable_list(conn));
623 tor_assert(!connection_in_array(conn));
624 if (BUG(conn->linked_conn)) {
625 conn->linked_conn->linked_conn = NULL;
626 if (! conn->linked_conn->marked_for_close &&
627 conn->linked_conn->reading_from_linked_conn)
628 connection_start_reading(conn->linked_conn);
629 conn->linked_conn = NULL;
631 if (connection_speaks_cells(conn)) {
632 if (!tor_digest_is_zero(TO_OR_CONN(conn)->identity_digest)) {
633 connection_or_remove_from_identity_map(TO_OR_CONN(conn));
636 if (conn->type == CONN_TYPE_CONTROL) {
637 connection_control_closed(TO_CONTROL_CONN(conn));
639 #if 1
640 /* DEBUGGING */
641 if (conn->type == CONN_TYPE_AP) {
642 connection_ap_warn_and_unmark_if_pending_circ(TO_ENTRY_CONN(conn),
643 "connection_free");
645 #endif
646 connection_unregister_events(conn);
647 connection_free_(conn);
651 * Called when we're about to finally unlink and free a connection:
652 * perform necessary accounting and cleanup
653 * - Directory conns that failed to fetch a rendezvous descriptor
654 * need to inform pending rendezvous streams.
655 * - OR conns need to call rep_hist_note_*() to record status.
656 * - AP conns need to send a socks reject if necessary.
657 * - Exit conns need to call connection_dns_remove() if necessary.
658 * - AP and Exit conns need to send an end cell if they can.
659 * - DNS conns need to fail any resolves that are pending on them.
660 * - OR and edge connections need to be unlinked from circuits.
662 void
663 connection_about_to_close_connection(connection_t *conn)
665 tor_assert(conn->marked_for_close);
667 switch (conn->type) {
668 case CONN_TYPE_DIR:
669 connection_dir_about_to_close(TO_DIR_CONN(conn));
670 break;
671 case CONN_TYPE_OR:
672 case CONN_TYPE_EXT_OR:
673 connection_or_about_to_close(TO_OR_CONN(conn));
674 break;
675 case CONN_TYPE_AP:
676 connection_ap_about_to_close(TO_ENTRY_CONN(conn));
677 break;
678 case CONN_TYPE_EXIT:
679 connection_exit_about_to_close(TO_EDGE_CONN(conn));
680 break;
684 /** Return true iff connection_close_immediate() has been called on this
685 * connection. */
686 #define CONN_IS_CLOSED(c) \
687 ((c)->linked ? ((c)->linked_conn_is_closed) : (! SOCKET_OK(c->s)))
689 /** Close the underlying socket for <b>conn</b>, so we don't try to
690 * flush it. Must be used in conjunction with (right before)
691 * connection_mark_for_close().
693 void
694 connection_close_immediate(connection_t *conn)
696 assert_connection_ok(conn,0);
697 if (CONN_IS_CLOSED(conn)) {
698 log_err(LD_BUG,"Attempt to close already-closed connection.");
699 tor_fragile_assert();
700 return;
702 if (conn->outbuf_flushlen) {
703 log_info(LD_NET,"fd %d, type %s, state %s, %d bytes on outbuf.",
704 (int)conn->s, conn_type_to_string(conn->type),
705 conn_state_to_string(conn->type, conn->state),
706 (int)conn->outbuf_flushlen);
709 connection_unregister_events(conn);
711 if (SOCKET_OK(conn->s))
712 tor_close_socket(conn->s);
713 conn->s = TOR_INVALID_SOCKET;
714 if (conn->linked)
715 conn->linked_conn_is_closed = 1;
716 if (conn->outbuf)
717 buf_clear(conn->outbuf);
718 conn->outbuf_flushlen = 0;
721 /** Mark <b>conn</b> to be closed next time we loop through
722 * conn_close_if_marked() in main.c. */
723 void
724 connection_mark_for_close_(connection_t *conn, int line, const char *file)
726 assert_connection_ok(conn,0);
727 tor_assert(line);
728 tor_assert(line < 1<<16); /* marked_for_close can only fit a uint16_t. */
729 tor_assert(file);
731 if (conn->type == CONN_TYPE_OR) {
733 * An or_connection should have been closed through one of the channel-
734 * aware functions in connection_or.c. We'll assume this is an error
735 * close and do that, and log a bug warning.
737 log_warn(LD_CHANNEL | LD_BUG,
738 "Something tried to close an or_connection_t without going "
739 "through channels at %s:%d",
740 file, line);
741 connection_or_close_for_error(TO_OR_CONN(conn), 0);
742 } else {
743 /* Pass it down to the real function */
744 connection_mark_for_close_internal_(conn, line, file);
748 /** Mark <b>conn</b> to be closed next time we loop through
749 * conn_close_if_marked() in main.c; the _internal version bypasses the
750 * CONN_TYPE_OR checks; this should be called when you either are sure that
751 * if this is an or_connection_t the controlling channel has been notified
752 * (e.g. with connection_or_notify_error()), or you actually are the
753 * connection_or_close_for_error() or connection_or_close_normally function.
754 * For all other cases, use connection_mark_and_flush() instead, which
755 * checks for or_connection_t properly, instead. See below.
757 void
758 connection_mark_for_close_internal_(connection_t *conn,
759 int line, const char *file)
761 assert_connection_ok(conn,0);
762 tor_assert(line);
763 tor_assert(line < 1<<16); /* marked_for_close can only fit a uint16_t. */
764 tor_assert(file);
766 if (conn->marked_for_close) {
767 log_warn(LD_BUG,"Duplicate call to connection_mark_for_close at %s:%d"
768 " (first at %s:%d)", file, line, conn->marked_for_close_file,
769 conn->marked_for_close);
770 tor_fragile_assert();
771 return;
774 if (conn->type == CONN_TYPE_OR) {
776 * Bad news if this happens without telling the controlling channel; do
777 * this so we can find things that call this wrongly when the asserts hit.
779 log_debug(LD_CHANNEL,
780 "Calling connection_mark_for_close_internal_() on an OR conn "
781 "at %s:%d",
782 file, line);
785 conn->marked_for_close = line;
786 conn->marked_for_close_file = file;
787 add_connection_to_closeable_list(conn);
789 /* in case we're going to be held-open-til-flushed, reset
790 * the number of seconds since last successful write, so
791 * we get our whole 15 seconds */
792 conn->timestamp_lastwritten = time(NULL);
795 /** Find each connection that has hold_open_until_flushed set to
796 * 1 but hasn't written in the past 15 seconds, and set
797 * hold_open_until_flushed to 0. This means it will get cleaned
798 * up in the next loop through close_if_marked() in main.c.
800 void
801 connection_expire_held_open(void)
803 time_t now;
804 smartlist_t *conns = get_connection_array();
806 now = time(NULL);
808 SMARTLIST_FOREACH_BEGIN(conns, connection_t *, conn) {
809 /* If we've been holding the connection open, but we haven't written
810 * for 15 seconds...
812 if (conn->hold_open_until_flushed) {
813 tor_assert(conn->marked_for_close);
814 if (now - conn->timestamp_lastwritten >= 15) {
815 int severity;
816 if (conn->type == CONN_TYPE_EXIT ||
817 (conn->type == CONN_TYPE_DIR &&
818 conn->purpose == DIR_PURPOSE_SERVER))
819 severity = LOG_INFO;
820 else
821 severity = LOG_NOTICE;
822 log_fn(severity, LD_NET,
823 "Giving up on marked_for_close conn that's been flushing "
824 "for 15s (fd %d, type %s, state %s).",
825 (int)conn->s, conn_type_to_string(conn->type),
826 conn_state_to_string(conn->type, conn->state));
827 conn->hold_open_until_flushed = 0;
830 } SMARTLIST_FOREACH_END(conn);
833 #if defined(HAVE_SYS_UN_H) || defined(RUNNING_DOXYGEN)
834 /** Create an AF_UNIX listenaddr struct.
835 * <b>listenaddress</b> provides the path to the Unix socket.
837 * Eventually <b>listenaddress</b> will also optionally contain user, group,
838 * and file permissions for the new socket. But not yet. XXX
839 * Also, since we do not create the socket here the information doesn't help
840 * here.
842 * If not NULL <b>readable_address</b> will contain a copy of the path part of
843 * <b>listenaddress</b>.
845 * The listenaddr struct has to be freed by the caller.
847 static struct sockaddr_un *
848 create_unix_sockaddr(const char *listenaddress, char **readable_address,
849 socklen_t *len_out)
851 struct sockaddr_un *sockaddr = NULL;
853 sockaddr = tor_malloc_zero(sizeof(struct sockaddr_un));
854 sockaddr->sun_family = AF_UNIX;
855 if (strlcpy(sockaddr->sun_path, listenaddress, sizeof(sockaddr->sun_path))
856 >= sizeof(sockaddr->sun_path)) {
857 log_warn(LD_CONFIG, "Unix socket path '%s' is too long to fit.",
858 escaped(listenaddress));
859 tor_free(sockaddr);
860 return NULL;
863 if (readable_address)
864 *readable_address = tor_strdup(listenaddress);
866 *len_out = sizeof(struct sockaddr_un);
867 return sockaddr;
869 #else
870 static struct sockaddr *
871 create_unix_sockaddr(const char *listenaddress, char **readable_address,
872 socklen_t *len_out)
874 (void)listenaddress;
875 (void)readable_address;
876 log_fn(LOG_ERR, LD_BUG,
877 "Unix domain sockets not supported, yet we tried to create one.");
878 *len_out = 0;
879 tor_fragile_assert();
880 return NULL;
882 #endif /* HAVE_SYS_UN_H */
884 /** Warn that an accept or a connect has failed because we're running out of
885 * TCP sockets we can use on current system. Rate-limit these warnings so
886 * that we don't spam the log. */
887 static void
888 warn_too_many_conns(void)
890 #define WARN_TOO_MANY_CONNS_INTERVAL (6*60*60)
891 static ratelim_t last_warned = RATELIM_INIT(WARN_TOO_MANY_CONNS_INTERVAL);
892 char *m;
893 if ((m = rate_limit_log(&last_warned, approx_time()))) {
894 int n_conns = get_n_open_sockets();
895 log_warn(LD_NET,"Failing because we have %d connections already. Please "
896 "read doc/TUNING for guidance.%s", n_conns, m);
897 tor_free(m);
898 control_event_general_status(LOG_WARN, "TOO_MANY_CONNECTIONS CURRENT=%d",
899 n_conns);
903 #ifdef HAVE_SYS_UN_H
905 #define UNIX_SOCKET_PURPOSE_CONTROL_SOCKET 0
906 #define UNIX_SOCKET_PURPOSE_SOCKS_SOCKET 1
908 /** Check if the purpose isn't one of the ones we know what to do with */
910 static int
911 is_valid_unix_socket_purpose(int purpose)
913 int valid = 0;
915 switch (purpose) {
916 case UNIX_SOCKET_PURPOSE_CONTROL_SOCKET:
917 case UNIX_SOCKET_PURPOSE_SOCKS_SOCKET:
918 valid = 1;
919 break;
922 return valid;
925 /** Return a string description of a unix socket purpose */
926 static const char *
927 unix_socket_purpose_to_string(int purpose)
929 const char *s = "unknown-purpose socket";
931 switch (purpose) {
932 case UNIX_SOCKET_PURPOSE_CONTROL_SOCKET:
933 s = "control socket";
934 break;
935 case UNIX_SOCKET_PURPOSE_SOCKS_SOCKET:
936 s = "SOCKS socket";
937 break;
940 return s;
943 /** Check whether we should be willing to open an AF_UNIX socket in
944 * <b>path</b>. Return 0 if we should go ahead and -1 if we shouldn't. */
945 static int
946 check_location_for_unix_socket(const or_options_t *options, const char *path,
947 int purpose, const port_cfg_t *port)
949 int r = -1;
950 char *p = NULL;
952 tor_assert(is_valid_unix_socket_purpose(purpose));
954 p = tor_strdup(path);
955 cpd_check_t flags = CPD_CHECK_MODE_ONLY;
956 if (get_parent_directory(p)<0 || p[0] != '/') {
957 log_warn(LD_GENERAL, "Bad unix socket address '%s'. Tor does not support "
958 "relative paths for unix sockets.", path);
959 goto done;
962 if (port->is_world_writable) {
963 /* World-writable sockets can go anywhere. */
964 r = 0;
965 goto done;
968 if (port->is_group_writable) {
969 flags |= CPD_GROUP_OK;
972 if (port->relax_dirmode_check) {
973 flags |= CPD_RELAX_DIRMODE_CHECK;
976 if (check_private_dir(p, flags, options->User) < 0) {
977 char *escpath, *escdir;
978 escpath = esc_for_log(path);
979 escdir = esc_for_log(p);
980 log_warn(LD_GENERAL, "Before Tor can create a %s in %s, the directory "
981 "%s needs to exist, and to be accessible only by the user%s "
982 "account that is running Tor. (On some Unix systems, anybody "
983 "who can list a socket can connect to it, so Tor is being "
984 "careful.)",
985 unix_socket_purpose_to_string(purpose), escpath, escdir,
986 port->is_group_writable ? " and group" : "");
987 tor_free(escpath);
988 tor_free(escdir);
989 goto done;
992 r = 0;
993 done:
994 tor_free(p);
995 return r;
997 #endif
999 /** Tell the TCP stack that it shouldn't wait for a long time after
1000 * <b>sock</b> has closed before reusing its port. Return 0 on success,
1001 * -1 on failure. */
1002 static int
1003 make_socket_reuseable(tor_socket_t sock)
1005 #ifdef _WIN32
1006 (void) sock;
1007 return 0;
1008 #else
1009 int one=1;
1011 /* REUSEADDR on normal places means you can rebind to the port
1012 * right after somebody else has let it go. But REUSEADDR on win32
1013 * means you can bind to the port _even when somebody else
1014 * already has it bound_. So, don't do that on Win32. */
1015 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void*) &one,
1016 (socklen_t)sizeof(one)) == -1) {
1017 return -1;
1019 return 0;
1020 #endif
1023 #ifdef _WIN32
1024 /** Tell the Windows TCP stack to prevent other applications from receiving
1025 * traffic from tor's open ports. Return 0 on success, -1 on failure. */
1026 static int
1027 make_win32_socket_exclusive(tor_socket_t sock)
1029 #ifdef SO_EXCLUSIVEADDRUSE
1030 int one=1;
1032 /* Any socket that sets REUSEADDR on win32 can bind to a port _even when
1033 * somebody else already has it bound_, and _even if the original socket
1034 * didn't set REUSEADDR_. Use EXCLUSIVEADDRUSE to prevent this port-stealing
1035 * on win32. */
1036 if (setsockopt(sock, SOL_SOCKET, SO_EXCLUSIVEADDRUSE, (void*) &one,
1037 (socklen_t)sizeof(one))) {
1038 return -1;
1040 return 0;
1041 #else
1042 (void) sock;
1043 return 0;
1044 #endif
1046 #endif
1048 /** Max backlog to pass to listen. We start at */
1049 static int listen_limit = INT_MAX;
1051 /* Listen on <b>fd</b> with appropriate backlog. Return as for listen. */
1052 static int
1053 tor_listen(tor_socket_t fd)
1055 int r;
1057 if ((r = listen(fd, listen_limit)) < 0) {
1058 if (listen_limit == SOMAXCONN)
1059 return r;
1060 if ((r = listen(fd, SOMAXCONN)) == 0) {
1061 listen_limit = SOMAXCONN;
1062 log_warn(LD_NET, "Setting listen backlog to INT_MAX connections "
1063 "didn't work, but SOMAXCONN did. Lowering backlog limit.");
1066 return r;
1069 /** Bind a new non-blocking socket listening to the socket described
1070 * by <b>listensockaddr</b>.
1072 * <b>address</b> is only used for logging purposes and to add the information
1073 * to the conn.
1075 static connection_t *
1076 connection_listener_new(const struct sockaddr *listensockaddr,
1077 socklen_t socklen,
1078 int type, const char *address,
1079 const port_cfg_t *port_cfg)
1081 listener_connection_t *lis_conn;
1082 connection_t *conn = NULL;
1083 tor_socket_t s = TOR_INVALID_SOCKET; /* the socket we're going to make */
1084 or_options_t const *options = get_options();
1085 (void) options; /* Windows doesn't use this. */
1086 #if defined(HAVE_PWD_H) && defined(HAVE_SYS_UN_H)
1087 const struct passwd *pw = NULL;
1088 #endif
1089 uint16_t usePort = 0, gotPort = 0;
1090 int start_reading = 0;
1091 static int global_next_session_group = SESSION_GROUP_FIRST_AUTO;
1092 tor_addr_t addr;
1094 if (listensockaddr->sa_family == AF_INET ||
1095 listensockaddr->sa_family == AF_INET6) {
1096 int is_stream = (type != CONN_TYPE_AP_DNS_LISTENER);
1097 if (is_stream)
1098 start_reading = 1;
1100 tor_addr_from_sockaddr(&addr, listensockaddr, &usePort);
1101 log_notice(LD_NET, "Opening %s on %s",
1102 conn_type_to_string(type), fmt_addrport(&addr, usePort));
1104 s = tor_open_socket_nonblocking(tor_addr_family(&addr),
1105 is_stream ? SOCK_STREAM : SOCK_DGRAM,
1106 is_stream ? IPPROTO_TCP: IPPROTO_UDP);
1107 if (!SOCKET_OK(s)) {
1108 int e = tor_socket_errno(s);
1109 if (ERRNO_IS_RESOURCE_LIMIT(e)) {
1110 warn_too_many_conns();
1111 } else {
1112 log_warn(LD_NET, "Socket creation failed: %s",
1113 tor_socket_strerror(e));
1115 goto err;
1118 if (make_socket_reuseable(s) < 0) {
1119 log_warn(LD_NET, "Error setting SO_REUSEADDR flag on %s: %s",
1120 conn_type_to_string(type),
1121 tor_socket_strerror(errno));
1124 #ifdef _WIN32
1125 if (make_win32_socket_exclusive(s) < 0) {
1126 log_warn(LD_NET, "Error setting SO_EXCLUSIVEADDRUSE flag on %s: %s",
1127 conn_type_to_string(type),
1128 tor_socket_strerror(errno));
1130 #endif
1132 #if defined(USE_TRANSPARENT) && defined(IP_TRANSPARENT)
1133 if (options->TransProxyType_parsed == TPT_TPROXY &&
1134 type == CONN_TYPE_AP_TRANS_LISTENER) {
1135 int one = 1;
1136 if (setsockopt(s, SOL_IP, IP_TRANSPARENT, (void*)&one,
1137 (socklen_t)sizeof(one)) < 0) {
1138 const char *extra = "";
1139 int e = tor_socket_errno(s);
1140 if (e == EPERM)
1141 extra = "TransTPROXY requires root privileges or similar"
1142 " capabilities.";
1143 log_warn(LD_NET, "Error setting IP_TRANSPARENT flag: %s.%s",
1144 tor_socket_strerror(e), extra);
1147 #endif
1149 #ifdef IPV6_V6ONLY
1150 if (listensockaddr->sa_family == AF_INET6) {
1151 int one = 1;
1152 /* We need to set IPV6_V6ONLY so that this socket can't get used for
1153 * IPv4 connections. */
1154 if (setsockopt(s,IPPROTO_IPV6, IPV6_V6ONLY,
1155 (void*)&one, (socklen_t)sizeof(one)) < 0) {
1156 int e = tor_socket_errno(s);
1157 log_warn(LD_NET, "Error setting IPV6_V6ONLY flag: %s",
1158 tor_socket_strerror(e));
1159 /* Keep going; probably not harmful. */
1162 #endif
1164 if (bind(s,listensockaddr,socklen) < 0) {
1165 const char *helpfulhint = "";
1166 int e = tor_socket_errno(s);
1167 if (ERRNO_IS_EADDRINUSE(e))
1168 helpfulhint = ". Is Tor already running?";
1169 log_warn(LD_NET, "Could not bind to %s:%u: %s%s", address, usePort,
1170 tor_socket_strerror(e), helpfulhint);
1171 goto err;
1174 if (is_stream) {
1175 if (tor_listen(s) < 0) {
1176 log_warn(LD_NET, "Could not listen on %s:%u: %s", address, usePort,
1177 tor_socket_strerror(tor_socket_errno(s)));
1178 goto err;
1182 if (usePort != 0) {
1183 gotPort = usePort;
1184 } else {
1185 tor_addr_t addr2;
1186 struct sockaddr_storage ss;
1187 socklen_t ss_len=sizeof(ss);
1188 if (getsockname(s, (struct sockaddr*)&ss, &ss_len)<0) {
1189 log_warn(LD_NET, "getsockname() couldn't learn address for %s: %s",
1190 conn_type_to_string(type),
1191 tor_socket_strerror(tor_socket_errno(s)));
1192 gotPort = 0;
1194 tor_addr_from_sockaddr(&addr2, (struct sockaddr*)&ss, &gotPort);
1196 #ifdef HAVE_SYS_UN_H
1198 * AF_UNIX generic setup stuff
1200 } else if (listensockaddr->sa_family == AF_UNIX) {
1201 /* We want to start reading for both AF_UNIX cases */
1202 start_reading = 1;
1204 tor_assert(conn_listener_type_supports_af_unix(type));
1206 if (check_location_for_unix_socket(options, address,
1207 (type == CONN_TYPE_CONTROL_LISTENER) ?
1208 UNIX_SOCKET_PURPOSE_CONTROL_SOCKET :
1209 UNIX_SOCKET_PURPOSE_SOCKS_SOCKET, port_cfg) < 0) {
1210 goto err;
1213 log_notice(LD_NET, "Opening %s on %s",
1214 conn_type_to_string(type), address);
1216 tor_addr_make_unspec(&addr);
1218 if (unlink(address) < 0 && errno != ENOENT) {
1219 log_warn(LD_NET, "Could not unlink %s: %s", address,
1220 strerror(errno));
1221 goto err;
1224 s = tor_open_socket_nonblocking(AF_UNIX, SOCK_STREAM, 0);
1225 if (! SOCKET_OK(s)) {
1226 int e = tor_socket_errno(s);
1227 if (ERRNO_IS_RESOURCE_LIMIT(e)) {
1228 warn_too_many_conns();
1229 } else {
1230 log_warn(LD_NET,"Socket creation failed: %s.", strerror(e));
1232 goto err;
1235 if (bind(s, listensockaddr,
1236 (socklen_t)sizeof(struct sockaddr_un)) == -1) {
1237 log_warn(LD_NET,"Bind to %s failed: %s.", address,
1238 tor_socket_strerror(tor_socket_errno(s)));
1239 goto err;
1242 #ifdef HAVE_PWD_H
1243 if (options->User) {
1244 pw = tor_getpwnam(options->User);
1245 struct stat st;
1246 if (pw == NULL) {
1247 log_warn(LD_NET,"Unable to chown() %s socket: user %s not found.",
1248 address, options->User);
1249 goto err;
1250 } else if (fstat(s, &st) == 0 &&
1251 st.st_uid == pw->pw_uid && st.st_gid == pw->pw_gid) {
1252 /* No change needed */
1253 } else if (chown(sandbox_intern_string(address),
1254 pw->pw_uid, pw->pw_gid) < 0) {
1255 log_warn(LD_NET,"Unable to chown() %s socket: %s.",
1256 address, strerror(errno));
1257 goto err;
1260 #endif
1263 unsigned mode;
1264 const char *status;
1265 struct stat st;
1266 if (port_cfg->is_world_writable) {
1267 mode = 0666;
1268 status = "world-writable";
1269 } else if (port_cfg->is_group_writable) {
1270 mode = 0660;
1271 status = "group-writable";
1272 } else {
1273 mode = 0600;
1274 status = "private";
1276 /* We need to use chmod; fchmod doesn't work on sockets on all
1277 * platforms. */
1278 if (fstat(s, &st) == 0 && (st.st_mode & 0777) == mode) {
1279 /* no change needed */
1280 } else if (chmod(sandbox_intern_string(address), mode) < 0) {
1281 log_warn(LD_FS,"Unable to make %s %s.", address, status);
1282 goto err;
1286 if (listen(s, SOMAXCONN) < 0) {
1287 log_warn(LD_NET, "Could not listen on %s: %s", address,
1288 tor_socket_strerror(tor_socket_errno(s)));
1289 goto err;
1291 #endif /* HAVE_SYS_UN_H */
1292 } else {
1293 log_err(LD_BUG, "Got unexpected address family %d.",
1294 listensockaddr->sa_family);
1295 tor_assert(0);
1298 lis_conn = listener_connection_new(type, listensockaddr->sa_family);
1299 conn = TO_CONN(lis_conn);
1300 conn->socket_family = listensockaddr->sa_family;
1301 conn->s = s;
1302 s = TOR_INVALID_SOCKET; /* Prevent double-close */
1303 conn->address = tor_strdup(address);
1304 conn->port = gotPort;
1305 tor_addr_copy(&conn->addr, &addr);
1307 memcpy(&lis_conn->entry_cfg, &port_cfg->entry_cfg, sizeof(entry_port_cfg_t));
1309 if (port_cfg->entry_cfg.isolation_flags) {
1310 lis_conn->entry_cfg.isolation_flags = port_cfg->entry_cfg.isolation_flags;
1311 if (port_cfg->entry_cfg.session_group >= 0) {
1312 lis_conn->entry_cfg.session_group = port_cfg->entry_cfg.session_group;
1313 } else {
1314 /* This can wrap after around INT_MAX listeners are opened. But I don't
1315 * believe that matters, since you would need to open a ridiculous
1316 * number of listeners while keeping the early ones open before you ever
1317 * hit this. An OR with a dozen ports open, for example, would have to
1318 * close and re-open its listeners every second for 4 years nonstop.
1320 lis_conn->entry_cfg.session_group = global_next_session_group--;
1324 if (type != CONN_TYPE_AP_LISTENER) {
1325 lis_conn->entry_cfg.ipv4_traffic = 1;
1326 lis_conn->entry_cfg.ipv6_traffic = 1;
1327 lis_conn->entry_cfg.prefer_ipv6 = 0;
1330 if (connection_add(conn) < 0) { /* no space, forget it */
1331 log_warn(LD_NET,"connection_add for listener failed. Giving up.");
1332 goto err;
1335 log_fn(usePort==gotPort ? LOG_DEBUG : LOG_NOTICE, LD_NET,
1336 "%s listening on port %u.",
1337 conn_type_to_string(type), gotPort);
1339 conn->state = LISTENER_STATE_READY;
1340 if (start_reading) {
1341 connection_start_reading(conn);
1342 } else {
1343 tor_assert(type == CONN_TYPE_AP_DNS_LISTENER);
1344 dnsserv_configure_listener(conn);
1347 return conn;
1349 err:
1350 if (SOCKET_OK(s))
1351 tor_close_socket(s);
1352 if (conn)
1353 connection_free(conn);
1355 return NULL;
1358 /** Do basic sanity checking on a newly received socket. Return 0
1359 * if it looks ok, else return -1.
1361 * Notably, some TCP stacks can erroneously have accept() return successfully
1362 * with socklen 0, when the client sends an RST before the accept call (as
1363 * nmap does). We want to detect that, and not go on with the connection.
1365 static int
1366 check_sockaddr(const struct sockaddr *sa, int len, int level)
1368 int ok = 1;
1370 if (sa->sa_family == AF_INET) {
1371 struct sockaddr_in *sin=(struct sockaddr_in*)sa;
1372 if (len != sizeof(struct sockaddr_in)) {
1373 log_fn(level, LD_NET, "Length of address not as expected: %d vs %d",
1374 len,(int)sizeof(struct sockaddr_in));
1375 ok = 0;
1377 if (sin->sin_addr.s_addr == 0 || sin->sin_port == 0) {
1378 log_fn(level, LD_NET,
1379 "Address for new connection has address/port equal to zero.");
1380 ok = 0;
1382 } else if (sa->sa_family == AF_INET6) {
1383 struct sockaddr_in6 *sin6=(struct sockaddr_in6*)sa;
1384 if (len != sizeof(struct sockaddr_in6)) {
1385 log_fn(level, LD_NET, "Length of address not as expected: %d vs %d",
1386 len,(int)sizeof(struct sockaddr_in6));
1387 ok = 0;
1389 if (tor_mem_is_zero((void*)sin6->sin6_addr.s6_addr, 16) ||
1390 sin6->sin6_port == 0) {
1391 log_fn(level, LD_NET,
1392 "Address for new connection has address/port equal to zero.");
1393 ok = 0;
1395 } else if (sa->sa_family == AF_UNIX) {
1396 ok = 1;
1397 } else {
1398 ok = 0;
1400 return ok ? 0 : -1;
1403 /** Check whether the socket family from an accepted socket <b>got</b> is the
1404 * same as the one that <b>listener</b> is waiting for. If it isn't, log
1405 * a useful message and return -1. Else return 0.
1407 * This is annoying, but can apparently happen on some Darwins. */
1408 static int
1409 check_sockaddr_family_match(sa_family_t got, connection_t *listener)
1411 if (got != listener->socket_family) {
1412 log_info(LD_BUG, "A listener connection returned a socket with a "
1413 "mismatched family. %s for addr_family %d gave us a socket "
1414 "with address family %d. Dropping.",
1415 conn_type_to_string(listener->type),
1416 (int)listener->socket_family,
1417 (int)got);
1418 return -1;
1420 return 0;
1423 /** The listener connection <b>conn</b> told poll() it wanted to read.
1424 * Call accept() on conn-\>s, and add the new connection if necessary.
1426 static int
1427 connection_handle_listener_read(connection_t *conn, int new_type)
1429 tor_socket_t news; /* the new socket */
1430 connection_t *newconn = 0;
1431 /* information about the remote peer when connecting to other routers */
1432 struct sockaddr_storage addrbuf;
1433 struct sockaddr *remote = (struct sockaddr*)&addrbuf;
1434 /* length of the remote address. Must be whatever accept() needs. */
1435 socklen_t remotelen = (socklen_t)sizeof(addrbuf);
1436 const or_options_t *options = get_options();
1438 tor_assert((size_t)remotelen >= sizeof(struct sockaddr_in));
1439 memset(&addrbuf, 0, sizeof(addrbuf));
1441 news = tor_accept_socket_nonblocking(conn->s,remote,&remotelen);
1442 if (!SOCKET_OK(news)) { /* accept() error */
1443 int e = tor_socket_errno(conn->s);
1444 if (ERRNO_IS_ACCEPT_EAGAIN(e)) {
1445 return 0; /* they hung up before we could accept(). that's fine. */
1446 } else if (ERRNO_IS_RESOURCE_LIMIT(e)) {
1447 warn_too_many_conns();
1448 return 0;
1450 /* else there was a real error. */
1451 log_warn(LD_NET,"accept() failed: %s. Closing listener.",
1452 tor_socket_strerror(e));
1453 connection_mark_for_close(conn);
1454 return -1;
1456 log_debug(LD_NET,
1457 "Connection accepted on socket %d (child of fd %d).",
1458 (int)news,(int)conn->s);
1460 if (make_socket_reuseable(news) < 0) {
1461 if (tor_socket_errno(news) == EINVAL) {
1462 /* This can happen on OSX if we get a badly timed shutdown. */
1463 log_debug(LD_NET, "make_socket_reuseable returned EINVAL");
1464 } else {
1465 log_warn(LD_NET, "Error setting SO_REUSEADDR flag on %s: %s",
1466 conn_type_to_string(new_type),
1467 tor_socket_strerror(errno));
1469 tor_close_socket(news);
1470 return 0;
1473 if (options->ConstrainedSockets)
1474 set_constrained_socket_buffers(news, (int)options->ConstrainedSockSize);
1476 if (check_sockaddr_family_match(remote->sa_family, conn) < 0) {
1477 tor_close_socket(news);
1478 return 0;
1481 if (conn->socket_family == AF_INET || conn->socket_family == AF_INET6 ||
1482 (conn->socket_family == AF_UNIX && new_type == CONN_TYPE_AP)) {
1483 tor_addr_t addr;
1484 uint16_t port;
1485 if (check_sockaddr(remote, remotelen, LOG_INFO)<0) {
1486 log_info(LD_NET,
1487 "accept() returned a strange address; closing connection.");
1488 tor_close_socket(news);
1489 return 0;
1492 tor_addr_from_sockaddr(&addr, remote, &port);
1494 /* process entrance policies here, before we even create the connection */
1495 if (new_type == CONN_TYPE_AP) {
1496 /* check sockspolicy to see if we should accept it */
1497 if (socks_policy_permits_address(&addr) == 0) {
1498 log_notice(LD_APP,
1499 "Denying socks connection from untrusted address %s.",
1500 fmt_and_decorate_addr(&addr));
1501 tor_close_socket(news);
1502 return 0;
1505 if (new_type == CONN_TYPE_DIR) {
1506 /* check dirpolicy to see if we should accept it */
1507 if (dir_policy_permits_address(&addr) == 0) {
1508 log_notice(LD_DIRSERV,"Denying dir connection from address %s.",
1509 fmt_and_decorate_addr(&addr));
1510 tor_close_socket(news);
1511 return 0;
1515 newconn = connection_new(new_type, conn->socket_family);
1516 newconn->s = news;
1518 /* remember the remote address */
1519 tor_addr_copy(&newconn->addr, &addr);
1520 newconn->port = port;
1521 newconn->address = tor_addr_to_str_dup(&addr);
1523 if (new_type == CONN_TYPE_AP && conn->socket_family != AF_UNIX) {
1524 log_info(LD_NET, "New SOCKS connection opened from %s.",
1525 fmt_and_decorate_addr(&addr));
1527 if (new_type == CONN_TYPE_AP && conn->socket_family == AF_UNIX) {
1528 newconn->port = 0;
1529 newconn->address = tor_strdup(conn->address);
1530 log_info(LD_NET, "New SOCKS AF_UNIX connection opened");
1532 if (new_type == CONN_TYPE_CONTROL) {
1533 log_notice(LD_CONTROL, "New control connection opened from %s.",
1534 fmt_and_decorate_addr(&addr));
1537 } else if (conn->socket_family == AF_UNIX && conn->type != CONN_TYPE_AP) {
1538 tor_assert(conn->type == CONN_TYPE_CONTROL_LISTENER);
1539 tor_assert(new_type == CONN_TYPE_CONTROL);
1540 log_notice(LD_CONTROL, "New control connection opened.");
1542 newconn = connection_new(new_type, conn->socket_family);
1543 newconn->s = news;
1545 /* remember the remote address -- do we have anything sane to put here? */
1546 tor_addr_make_unspec(&newconn->addr);
1547 newconn->port = 1;
1548 newconn->address = tor_strdup(conn->address);
1549 } else {
1550 tor_assert(0);
1553 if (connection_add(newconn) < 0) { /* no space, forget it */
1554 connection_free(newconn);
1555 return 0; /* no need to tear down the parent */
1558 if (connection_init_accepted_conn(newconn, TO_LISTENER_CONN(conn)) < 0) {
1559 if (! newconn->marked_for_close)
1560 connection_mark_for_close(newconn);
1561 return 0;
1563 return 0;
1566 /** Initialize states for newly accepted connection <b>conn</b>.
1567 * If conn is an OR, start the TLS handshake.
1568 * If conn is a transparent AP, get its original destination
1569 * and place it in circuit_wait.
1571 static int
1572 connection_init_accepted_conn(connection_t *conn,
1573 const listener_connection_t *listener)
1575 int rv;
1577 connection_start_reading(conn);
1579 switch (conn->type) {
1580 case CONN_TYPE_EXT_OR:
1581 /* Initiate Extended ORPort authentication. */
1582 return connection_ext_or_start_auth(TO_OR_CONN(conn));
1583 case CONN_TYPE_OR:
1584 control_event_or_conn_status(TO_OR_CONN(conn), OR_CONN_EVENT_NEW, 0);
1585 rv = connection_tls_start_handshake(TO_OR_CONN(conn), 1);
1586 if (rv < 0) {
1587 connection_or_close_for_error(TO_OR_CONN(conn), 0);
1589 return rv;
1590 break;
1591 case CONN_TYPE_AP:
1592 memcpy(&TO_ENTRY_CONN(conn)->entry_cfg, &listener->entry_cfg,
1593 sizeof(entry_port_cfg_t));
1594 TO_ENTRY_CONN(conn)->nym_epoch = get_signewnym_epoch();
1595 TO_ENTRY_CONN(conn)->socks_request->listener_type = listener->base_.type;
1597 switch (TO_CONN(listener)->type) {
1598 case CONN_TYPE_AP_LISTENER:
1599 conn->state = AP_CONN_STATE_SOCKS_WAIT;
1600 TO_ENTRY_CONN(conn)->socks_request->socks_prefer_no_auth =
1601 listener->entry_cfg.socks_prefer_no_auth;
1602 break;
1603 case CONN_TYPE_AP_TRANS_LISTENER:
1604 TO_ENTRY_CONN(conn)->is_transparent_ap = 1;
1605 /* XXXX028 -- is this correct still, with the addition of
1606 * pending_entry_connections ? */
1607 conn->state = AP_CONN_STATE_CIRCUIT_WAIT;
1608 return connection_ap_process_transparent(TO_ENTRY_CONN(conn));
1609 case CONN_TYPE_AP_NATD_LISTENER:
1610 TO_ENTRY_CONN(conn)->is_transparent_ap = 1;
1611 conn->state = AP_CONN_STATE_NATD_WAIT;
1612 break;
1614 break;
1615 case CONN_TYPE_DIR:
1616 conn->purpose = DIR_PURPOSE_SERVER;
1617 conn->state = DIR_CONN_STATE_SERVER_COMMAND_WAIT;
1618 break;
1619 case CONN_TYPE_CONTROL:
1620 conn->state = CONTROL_CONN_STATE_NEEDAUTH;
1621 break;
1623 return 0;
1626 /** Take conn, make a nonblocking socket; try to connect to
1627 * sa, binding to bindaddr if sa is not localhost. If fail, return -1 and if
1628 * applicable put your best guess about errno into *<b>socket_error</b>.
1629 * If connected return 1, if EAGAIN return 0.
1631 MOCK_IMPL(STATIC int,
1632 connection_connect_sockaddr,(connection_t *conn,
1633 const struct sockaddr *sa,
1634 socklen_t sa_len,
1635 const struct sockaddr *bindaddr,
1636 socklen_t bindaddr_len,
1637 int *socket_error))
1639 tor_socket_t s;
1640 int inprogress = 0;
1641 const or_options_t *options = get_options();
1643 tor_assert(conn);
1644 tor_assert(sa);
1645 tor_assert(socket_error);
1647 if (get_options()->DisableNetwork) {
1648 /* We should never even try to connect anyplace if DisableNetwork is set.
1649 * Warn if we do, and refuse to make the connection. */
1650 static ratelim_t disablenet_violated = RATELIM_INIT(30*60);
1651 *socket_error = SOCK_ERRNO(ENETUNREACH);
1652 log_fn_ratelim(&disablenet_violated, LOG_WARN, LD_BUG,
1653 "Tried to open a socket with DisableNetwork set.");
1654 tor_fragile_assert();
1655 return -1;
1658 const int protocol_family = sa->sa_family;
1659 const int proto = (sa->sa_family == AF_INET6 ||
1660 sa->sa_family == AF_INET) ? IPPROTO_TCP : 0;
1662 s = tor_open_socket_nonblocking(protocol_family, SOCK_STREAM, proto);
1663 if (! SOCKET_OK(s)) {
1664 *socket_error = tor_socket_errno(s);
1665 if (ERRNO_IS_RESOURCE_LIMIT(*socket_error)) {
1666 warn_too_many_conns();
1667 } else {
1668 log_warn(LD_NET,"Error creating network socket: %s",
1669 tor_socket_strerror(*socket_error));
1671 return -1;
1674 if (make_socket_reuseable(s) < 0) {
1675 log_warn(LD_NET, "Error setting SO_REUSEADDR flag on new connection: %s",
1676 tor_socket_strerror(errno));
1679 if (bindaddr && bind(s, bindaddr, bindaddr_len) < 0) {
1680 *socket_error = tor_socket_errno(s);
1681 log_warn(LD_NET,"Error binding network socket: %s",
1682 tor_socket_strerror(*socket_error));
1683 tor_close_socket(s);
1684 return -1;
1687 tor_assert(options);
1688 if (options->ConstrainedSockets)
1689 set_constrained_socket_buffers(s, (int)options->ConstrainedSockSize);
1691 if (connect(s, sa, sa_len) < 0) {
1692 int e = tor_socket_errno(s);
1693 if (!ERRNO_IS_CONN_EINPROGRESS(e)) {
1694 /* yuck. kill it. */
1695 *socket_error = e;
1696 log_info(LD_NET,
1697 "connect() to socket failed: %s",
1698 tor_socket_strerror(e));
1699 tor_close_socket(s);
1700 return -1;
1701 } else {
1702 inprogress = 1;
1706 /* it succeeded. we're connected. */
1707 log_fn(inprogress ? LOG_DEBUG : LOG_INFO, LD_NET,
1708 "Connection to socket %s (sock "TOR_SOCKET_T_FORMAT").",
1709 inprogress ? "in progress" : "established", s);
1710 conn->s = s;
1711 if (connection_add_connecting(conn) < 0) {
1712 /* no space, forget it */
1713 *socket_error = SOCK_ERRNO(ENOBUFS);
1714 return -1;
1717 return inprogress ? 0 : 1;
1720 /* Log a message if connection attempt is made when IPv4 or IPv6 is disabled.
1721 * Log a less severe message if we couldn't conform to ClientPreferIPv6ORPort
1722 * or ClientPreferIPv6ORPort. */
1723 static void
1724 connection_connect_log_client_use_ip_version(const connection_t *conn)
1726 const or_options_t *options = get_options();
1728 /* Only clients care about ClientUseIPv4/6, bail out early on servers, and
1729 * on connections we don't care about */
1730 if (server_mode(options) || !conn || conn->type == CONN_TYPE_EXIT) {
1731 return;
1734 /* We're only prepared to log OR and DIR connections here */
1735 if (conn->type != CONN_TYPE_OR && conn->type != CONN_TYPE_DIR) {
1736 return;
1739 const int must_ipv4 = !fascist_firewall_use_ipv6(options);
1740 const int must_ipv6 = (options->ClientUseIPv4 == 0);
1741 const int pref_ipv6 = (conn->type == CONN_TYPE_OR
1742 ? fascist_firewall_prefer_ipv6_orport(options)
1743 : fascist_firewall_prefer_ipv6_dirport(options));
1744 tor_addr_t real_addr;
1745 tor_addr_make_null(&real_addr, AF_UNSPEC);
1747 /* OR conns keep the original address in real_addr, as addr gets overwritten
1748 * with the descriptor address */
1749 if (conn->type == CONN_TYPE_OR) {
1750 const or_connection_t *or_conn = TO_OR_CONN((connection_t *)conn);
1751 tor_addr_copy(&real_addr, &or_conn->real_addr);
1752 } else if (conn->type == CONN_TYPE_DIR) {
1753 tor_addr_copy(&real_addr, &conn->addr);
1756 /* Check if we broke a mandatory address family restriction */
1757 if ((must_ipv4 && tor_addr_family(&real_addr) == AF_INET6)
1758 || (must_ipv6 && tor_addr_family(&real_addr) == AF_INET)) {
1759 static int logged_backtrace = 0;
1760 log_info(LD_BUG, "Outgoing %s connection to %s violated ClientUseIPv%s 0.",
1761 conn->type == CONN_TYPE_OR ? "OR" : "Dir",
1762 fmt_addr(&real_addr),
1763 options->ClientUseIPv4 == 0 ? "4" : "6");
1764 if (!logged_backtrace) {
1765 log_backtrace(LOG_INFO, LD_BUG, "Address came from");
1766 logged_backtrace = 1;
1770 /* Bridges are allowed to break IPv4/IPv6 ORPort preferences to connect to
1771 * the node's configured address when ClientPreferIPv6ORPort is auto */
1772 if (options->UseBridges && conn->type == CONN_TYPE_OR
1773 && options->ClientPreferIPv6ORPort == -1) {
1774 return;
1777 /* Check if we couldn't satisfy an address family preference */
1778 if ((!pref_ipv6 && tor_addr_family(&real_addr) == AF_INET6)
1779 || (pref_ipv6 && tor_addr_family(&real_addr) == AF_INET)) {
1780 log_info(LD_NET, "Outgoing connection to %s doesn't satisfy "
1781 "ClientPreferIPv6%sPort %d, with ClientUseIPv4 %d, and "
1782 "fascist_firewall_use_ipv6 %d (ClientUseIPv6 %d and UseBridges "
1783 "%d).",
1784 fmt_addr(&real_addr),
1785 conn->type == CONN_TYPE_OR ? "OR" : "Dir",
1786 conn->type == CONN_TYPE_OR ? options->ClientPreferIPv6ORPort
1787 : options->ClientPreferIPv6DirPort,
1788 options->ClientUseIPv4, fascist_firewall_use_ipv6(options),
1789 options->ClientUseIPv6, options->UseBridges);
1793 /** Take conn, make a nonblocking socket; try to connect to
1794 * addr:port (port arrives in *host order*). If fail, return -1 and if
1795 * applicable put your best guess about errno into *<b>socket_error</b>.
1796 * Else assign s to conn-\>s: if connected return 1, if EAGAIN return 0.
1798 * addr:port can be different to conn->addr:conn->port if connecting through
1799 * a proxy.
1801 * address is used to make the logs useful.
1803 * On success, add conn to the list of polled connections.
1806 connection_connect(connection_t *conn, const char *address,
1807 const tor_addr_t *addr, uint16_t port, int *socket_error)
1809 struct sockaddr_storage addrbuf;
1810 struct sockaddr_storage bind_addr_ss;
1811 struct sockaddr *bind_addr = NULL;
1812 struct sockaddr *dest_addr;
1813 int dest_addr_len, bind_addr_len = 0;
1814 const or_options_t *options = get_options();
1815 int protocol_family;
1817 /* Log if we didn't stick to ClientUseIPv4/6 or ClientPreferIPv6OR/DirPort
1819 connection_connect_log_client_use_ip_version(conn);
1821 if (tor_addr_family(addr) == AF_INET6)
1822 protocol_family = PF_INET6;
1823 else
1824 protocol_family = PF_INET;
1826 if (!tor_addr_is_loopback(addr)) {
1827 const tor_addr_t *ext_addr = NULL;
1828 if (protocol_family == AF_INET &&
1829 !tor_addr_is_null(&options->OutboundBindAddressIPv4_))
1830 ext_addr = &options->OutboundBindAddressIPv4_;
1831 else if (protocol_family == AF_INET6 &&
1832 !tor_addr_is_null(&options->OutboundBindAddressIPv6_))
1833 ext_addr = &options->OutboundBindAddressIPv6_;
1834 if (ext_addr) {
1835 memset(&bind_addr_ss, 0, sizeof(bind_addr_ss));
1836 bind_addr_len = tor_addr_to_sockaddr(ext_addr, 0,
1837 (struct sockaddr *) &bind_addr_ss,
1838 sizeof(bind_addr_ss));
1839 if (bind_addr_len == 0) {
1840 log_warn(LD_NET,
1841 "Error converting OutboundBindAddress %s into sockaddr. "
1842 "Ignoring.", fmt_and_decorate_addr(ext_addr));
1843 } else {
1844 bind_addr = (struct sockaddr *)&bind_addr_ss;
1849 memset(&addrbuf,0,sizeof(addrbuf));
1850 dest_addr = (struct sockaddr*) &addrbuf;
1851 dest_addr_len = tor_addr_to_sockaddr(addr, port, dest_addr, sizeof(addrbuf));
1852 tor_assert(dest_addr_len > 0);
1854 log_debug(LD_NET, "Connecting to %s:%u.",
1855 escaped_safe_str_client(address), port);
1857 return connection_connect_sockaddr(conn, dest_addr, dest_addr_len,
1858 bind_addr, bind_addr_len, socket_error);
1861 #ifdef HAVE_SYS_UN_H
1863 /** Take conn, make a nonblocking socket; try to connect to
1864 * an AF_UNIX socket at socket_path. If fail, return -1 and if applicable
1865 * put your best guess about errno into *<b>socket_error</b>. Else assign s
1866 * to conn-\>s: if connected return 1, if EAGAIN return 0.
1868 * On success, add conn to the list of polled connections.
1871 connection_connect_unix(connection_t *conn, const char *socket_path,
1872 int *socket_error)
1874 struct sockaddr_un dest_addr;
1876 tor_assert(socket_path);
1878 /* Check that we'll be able to fit it into dest_addr later */
1879 if (strlen(socket_path) + 1 > sizeof(dest_addr.sun_path)) {
1880 log_warn(LD_NET,
1881 "Path %s is too long for an AF_UNIX socket\n",
1882 escaped_safe_str_client(socket_path));
1883 *socket_error = SOCK_ERRNO(ENAMETOOLONG);
1884 return -1;
1887 memset(&dest_addr, 0, sizeof(dest_addr));
1888 dest_addr.sun_family = AF_UNIX;
1889 strlcpy(dest_addr.sun_path, socket_path, sizeof(dest_addr.sun_path));
1891 log_debug(LD_NET,
1892 "Connecting to AF_UNIX socket at %s.",
1893 escaped_safe_str_client(socket_path));
1895 return connection_connect_sockaddr(conn,
1896 (struct sockaddr *)&dest_addr, sizeof(dest_addr),
1897 NULL, 0, socket_error);
1900 #endif /* defined(HAVE_SYS_UN_H) */
1902 /** Convert state number to string representation for logging purposes.
1904 static const char *
1905 connection_proxy_state_to_string(int state)
1907 static const char *unknown = "???";
1908 static const char *states[] = {
1909 "PROXY_NONE",
1910 "PROXY_INFANT",
1911 "PROXY_HTTPS_WANT_CONNECT_OK",
1912 "PROXY_SOCKS4_WANT_CONNECT_OK",
1913 "PROXY_SOCKS5_WANT_AUTH_METHOD_NONE",
1914 "PROXY_SOCKS5_WANT_AUTH_METHOD_RFC1929",
1915 "PROXY_SOCKS5_WANT_AUTH_RFC1929_OK",
1916 "PROXY_SOCKS5_WANT_CONNECT_OK",
1917 "PROXY_CONNECTED",
1920 if (state < PROXY_NONE || state > PROXY_CONNECTED)
1921 return unknown;
1923 return states[state];
1926 /** Returns the global proxy type used by tor. Use this function for
1927 * logging or high-level purposes, don't use it to fill the
1928 * <b>proxy_type</b> field of or_connection_t; use the actual proxy
1929 * protocol instead.*/
1930 static int
1931 get_proxy_type(void)
1933 const or_options_t *options = get_options();
1935 if (options->ClientTransportPlugin)
1936 return PROXY_PLUGGABLE;
1937 else if (options->HTTPSProxy)
1938 return PROXY_CONNECT;
1939 else if (options->Socks4Proxy)
1940 return PROXY_SOCKS4;
1941 else if (options->Socks5Proxy)
1942 return PROXY_SOCKS5;
1943 else
1944 return PROXY_NONE;
1947 /* One byte for the version, one for the command, two for the
1948 port, and four for the addr... and, one more for the
1949 username NUL: */
1950 #define SOCKS4_STANDARD_BUFFER_SIZE (1 + 1 + 2 + 4 + 1)
1952 /** Write a proxy request of <b>type</b> (socks4, socks5, https) to conn
1953 * for conn->addr:conn->port, authenticating with the auth details given
1954 * in the configuration (if available). SOCKS 5 and HTTP CONNECT proxies
1955 * support authentication.
1957 * Returns -1 if conn->addr is incompatible with the proxy protocol, and
1958 * 0 otherwise.
1960 * Use connection_read_proxy_handshake() to complete the handshake.
1963 connection_proxy_connect(connection_t *conn, int type)
1965 const or_options_t *options;
1967 tor_assert(conn);
1969 options = get_options();
1971 switch (type) {
1972 case PROXY_CONNECT: {
1973 char buf[1024];
1974 char *base64_authenticator=NULL;
1975 const char *authenticator = options->HTTPSProxyAuthenticator;
1977 /* Send HTTP CONNECT and authentication (if available) in
1978 * one request */
1980 if (authenticator) {
1981 base64_authenticator = alloc_http_authenticator(authenticator);
1982 if (!base64_authenticator)
1983 log_warn(LD_OR, "Encoding https authenticator failed");
1986 if (base64_authenticator) {
1987 const char *addrport = fmt_addrport(&conn->addr, conn->port);
1988 tor_snprintf(buf, sizeof(buf), "CONNECT %s HTTP/1.1\r\n"
1989 "Host: %s\r\n"
1990 "Proxy-Authorization: Basic %s\r\n\r\n",
1991 addrport,
1992 addrport,
1993 base64_authenticator);
1994 tor_free(base64_authenticator);
1995 } else {
1996 tor_snprintf(buf, sizeof(buf), "CONNECT %s HTTP/1.0\r\n\r\n",
1997 fmt_addrport(&conn->addr, conn->port));
2000 connection_write_to_buf(buf, strlen(buf), conn);
2001 conn->proxy_state = PROXY_HTTPS_WANT_CONNECT_OK;
2002 break;
2005 case PROXY_SOCKS4: {
2006 unsigned char *buf;
2007 uint16_t portn;
2008 uint32_t ip4addr;
2009 size_t buf_size = 0;
2010 char *socks_args_string = NULL;
2012 /* Send a SOCKS4 connect request */
2014 if (tor_addr_family(&conn->addr) != AF_INET) {
2015 log_warn(LD_NET, "SOCKS4 client is incompatible with IPv6");
2016 return -1;
2019 { /* If we are here because we are trying to connect to a
2020 pluggable transport proxy, check if we have any SOCKS
2021 arguments to transmit. If we do, compress all arguments to
2022 a single string in 'socks_args_string': */
2024 if (get_proxy_type() == PROXY_PLUGGABLE) {
2025 socks_args_string =
2026 pt_get_socks_args_for_proxy_addrport(&conn->addr, conn->port);
2027 if (socks_args_string)
2028 log_debug(LD_NET, "Sending out '%s' as our SOCKS argument string.",
2029 socks_args_string);
2033 { /* Figure out the buffer size we need for the SOCKS message: */
2035 buf_size = SOCKS4_STANDARD_BUFFER_SIZE;
2037 /* If we have a SOCKS argument string, consider its size when
2038 calculating the buffer size: */
2039 if (socks_args_string)
2040 buf_size += strlen(socks_args_string);
2043 buf = tor_malloc_zero(buf_size);
2045 ip4addr = tor_addr_to_ipv4n(&conn->addr);
2046 portn = htons(conn->port);
2048 buf[0] = 4; /* version */
2049 buf[1] = SOCKS_COMMAND_CONNECT; /* command */
2050 memcpy(buf + 2, &portn, 2); /* port */
2051 memcpy(buf + 4, &ip4addr, 4); /* addr */
2053 /* Next packet field is the userid. If we have pluggable
2054 transport SOCKS arguments, we have to embed them
2055 there. Otherwise, we use an empty userid. */
2056 if (socks_args_string) { /* place the SOCKS args string: */
2057 tor_assert(strlen(socks_args_string) > 0);
2058 tor_assert(buf_size >=
2059 SOCKS4_STANDARD_BUFFER_SIZE + strlen(socks_args_string));
2060 strlcpy((char *)buf + 8, socks_args_string, buf_size - 8);
2061 tor_free(socks_args_string);
2062 } else {
2063 buf[8] = 0; /* no userid */
2066 connection_write_to_buf((char *)buf, buf_size, conn);
2067 tor_free(buf);
2069 conn->proxy_state = PROXY_SOCKS4_WANT_CONNECT_OK;
2070 break;
2073 case PROXY_SOCKS5: {
2074 unsigned char buf[4]; /* fields: vers, num methods, method list */
2076 /* Send a SOCKS5 greeting (connect request must wait) */
2078 buf[0] = 5; /* version */
2080 /* We have to use SOCKS5 authentication, if we have a
2081 Socks5ProxyUsername or if we want to pass arguments to our
2082 pluggable transport proxy: */
2083 if ((options->Socks5ProxyUsername) ||
2084 (get_proxy_type() == PROXY_PLUGGABLE &&
2085 (get_socks_args_by_bridge_addrport(&conn->addr, conn->port)))) {
2086 /* number of auth methods */
2087 buf[1] = 2;
2088 buf[2] = 0x00; /* no authentication */
2089 buf[3] = 0x02; /* rfc1929 Username/Passwd auth */
2090 conn->proxy_state = PROXY_SOCKS5_WANT_AUTH_METHOD_RFC1929;
2091 } else {
2092 buf[1] = 1;
2093 buf[2] = 0x00; /* no authentication */
2094 conn->proxy_state = PROXY_SOCKS5_WANT_AUTH_METHOD_NONE;
2097 connection_write_to_buf((char *)buf, 2 + buf[1], conn);
2098 break;
2101 default:
2102 log_err(LD_BUG, "Invalid proxy protocol, %d", type);
2103 tor_fragile_assert();
2104 return -1;
2107 log_debug(LD_NET, "set state %s",
2108 connection_proxy_state_to_string(conn->proxy_state));
2110 return 0;
2113 /** Read conn's inbuf. If the http response from the proxy is all
2114 * here, make sure it's good news, then return 1. If it's bad news,
2115 * return -1. Else return 0 and hope for better luck next time.
2117 static int
2118 connection_read_https_proxy_response(connection_t *conn)
2120 char *headers;
2121 char *reason=NULL;
2122 int status_code;
2123 time_t date_header;
2125 switch (fetch_from_buf_http(conn->inbuf,
2126 &headers, MAX_HEADERS_SIZE,
2127 NULL, NULL, 10000, 0)) {
2128 case -1: /* overflow */
2129 log_warn(LD_PROTOCOL,
2130 "Your https proxy sent back an oversized response. Closing.");
2131 return -1;
2132 case 0:
2133 log_info(LD_NET,"https proxy response not all here yet. Waiting.");
2134 return 0;
2135 /* case 1, fall through */
2138 if (parse_http_response(headers, &status_code, &date_header,
2139 NULL, &reason) < 0) {
2140 log_warn(LD_NET,
2141 "Unparseable headers from proxy (connecting to '%s'). Closing.",
2142 conn->address);
2143 tor_free(headers);
2144 return -1;
2146 tor_free(headers);
2147 if (!reason) reason = tor_strdup("[no reason given]");
2149 if (status_code == 200) {
2150 log_info(LD_NET,
2151 "HTTPS connect to '%s' successful! (200 %s) Starting TLS.",
2152 conn->address, escaped(reason));
2153 tor_free(reason);
2154 return 1;
2156 /* else, bad news on the status code */
2157 switch (status_code) {
2158 case 403:
2159 log_warn(LD_NET,
2160 "The https proxy refused to allow connection to %s "
2161 "(status code %d, %s). Closing.",
2162 conn->address, status_code, escaped(reason));
2163 break;
2164 default:
2165 log_warn(LD_NET,
2166 "The https proxy sent back an unexpected status code %d (%s). "
2167 "Closing.",
2168 status_code, escaped(reason));
2169 break;
2171 tor_free(reason);
2172 return -1;
2175 /** Send SOCKS5 CONNECT command to <b>conn</b>, copying <b>conn->addr</b>
2176 * and <b>conn->port</b> into the request.
2178 static void
2179 connection_send_socks5_connect(connection_t *conn)
2181 unsigned char buf[1024];
2182 size_t reqsize = 6;
2183 uint16_t port = htons(conn->port);
2185 buf[0] = 5; /* version */
2186 buf[1] = SOCKS_COMMAND_CONNECT; /* command */
2187 buf[2] = 0; /* reserved */
2189 if (tor_addr_family(&conn->addr) == AF_INET) {
2190 uint32_t addr = tor_addr_to_ipv4n(&conn->addr);
2192 buf[3] = 1;
2193 reqsize += 4;
2194 memcpy(buf + 4, &addr, 4);
2195 memcpy(buf + 8, &port, 2);
2196 } else { /* AF_INET6 */
2197 buf[3] = 4;
2198 reqsize += 16;
2199 memcpy(buf + 4, tor_addr_to_in6_addr8(&conn->addr), 16);
2200 memcpy(buf + 20, &port, 2);
2203 connection_write_to_buf((char *)buf, reqsize, conn);
2205 conn->proxy_state = PROXY_SOCKS5_WANT_CONNECT_OK;
2208 /** Wrapper around fetch_from_buf_socks_client: see that functions
2209 * for documentation of its behavior. */
2210 static int
2211 connection_fetch_from_buf_socks_client(connection_t *conn,
2212 int state, char **reason)
2214 return fetch_from_buf_socks_client(conn->inbuf, state, reason);
2217 /** Call this from connection_*_process_inbuf() to advance the proxy
2218 * handshake.
2220 * No matter what proxy protocol is used, if this function returns 1, the
2221 * handshake is complete, and the data remaining on inbuf may contain the
2222 * start of the communication with the requested server.
2224 * Returns 0 if the current buffer contains an incomplete response, and -1
2225 * on error.
2228 connection_read_proxy_handshake(connection_t *conn)
2230 int ret = 0;
2231 char *reason = NULL;
2233 log_debug(LD_NET, "enter state %s",
2234 connection_proxy_state_to_string(conn->proxy_state));
2236 switch (conn->proxy_state) {
2237 case PROXY_HTTPS_WANT_CONNECT_OK:
2238 ret = connection_read_https_proxy_response(conn);
2239 if (ret == 1)
2240 conn->proxy_state = PROXY_CONNECTED;
2241 break;
2243 case PROXY_SOCKS4_WANT_CONNECT_OK:
2244 ret = connection_fetch_from_buf_socks_client(conn,
2245 conn->proxy_state,
2246 &reason);
2247 if (ret == 1)
2248 conn->proxy_state = PROXY_CONNECTED;
2249 break;
2251 case PROXY_SOCKS5_WANT_AUTH_METHOD_NONE:
2252 ret = connection_fetch_from_buf_socks_client(conn,
2253 conn->proxy_state,
2254 &reason);
2255 /* no auth needed, do connect */
2256 if (ret == 1) {
2257 connection_send_socks5_connect(conn);
2258 ret = 0;
2260 break;
2262 case PROXY_SOCKS5_WANT_AUTH_METHOD_RFC1929:
2263 ret = connection_fetch_from_buf_socks_client(conn,
2264 conn->proxy_state,
2265 &reason);
2267 /* send auth if needed, otherwise do connect */
2268 if (ret == 1) {
2269 connection_send_socks5_connect(conn);
2270 ret = 0;
2271 } else if (ret == 2) {
2272 unsigned char buf[1024];
2273 size_t reqsize, usize, psize;
2274 const char *user, *pass;
2275 char *socks_args_string = NULL;
2277 if (get_proxy_type() == PROXY_PLUGGABLE) {
2278 socks_args_string =
2279 pt_get_socks_args_for_proxy_addrport(&conn->addr, conn->port);
2280 if (!socks_args_string) {
2281 log_warn(LD_NET, "Could not create SOCKS args string.");
2282 ret = -1;
2283 break;
2286 log_debug(LD_NET, "SOCKS5 arguments: %s", socks_args_string);
2287 tor_assert(strlen(socks_args_string) > 0);
2288 tor_assert(strlen(socks_args_string) <= MAX_SOCKS5_AUTH_SIZE_TOTAL);
2290 if (strlen(socks_args_string) > MAX_SOCKS5_AUTH_FIELD_SIZE) {
2291 user = socks_args_string;
2292 usize = MAX_SOCKS5_AUTH_FIELD_SIZE;
2293 pass = socks_args_string + MAX_SOCKS5_AUTH_FIELD_SIZE;
2294 psize = strlen(socks_args_string) - MAX_SOCKS5_AUTH_FIELD_SIZE;
2295 } else {
2296 user = socks_args_string;
2297 usize = strlen(socks_args_string);
2298 pass = "\0";
2299 psize = 1;
2301 } else if (get_options()->Socks5ProxyUsername) {
2302 user = get_options()->Socks5ProxyUsername;
2303 pass = get_options()->Socks5ProxyPassword;
2304 tor_assert(user && pass);
2305 usize = strlen(user);
2306 psize = strlen(pass);
2307 } else {
2308 log_err(LD_BUG, "We entered %s for no reason!", __func__);
2309 tor_fragile_assert();
2310 ret = -1;
2311 break;
2314 /* Username and password lengths should have been checked
2315 above and during torrc parsing. */
2316 tor_assert(usize <= MAX_SOCKS5_AUTH_FIELD_SIZE &&
2317 psize <= MAX_SOCKS5_AUTH_FIELD_SIZE);
2318 reqsize = 3 + usize + psize;
2320 buf[0] = 1; /* negotiation version */
2321 buf[1] = usize;
2322 memcpy(buf + 2, user, usize);
2323 buf[2 + usize] = psize;
2324 memcpy(buf + 3 + usize, pass, psize);
2326 if (socks_args_string)
2327 tor_free(socks_args_string);
2329 connection_write_to_buf((char *)buf, reqsize, conn);
2331 conn->proxy_state = PROXY_SOCKS5_WANT_AUTH_RFC1929_OK;
2332 ret = 0;
2334 break;
2336 case PROXY_SOCKS5_WANT_AUTH_RFC1929_OK:
2337 ret = connection_fetch_from_buf_socks_client(conn,
2338 conn->proxy_state,
2339 &reason);
2340 /* send the connect request */
2341 if (ret == 1) {
2342 connection_send_socks5_connect(conn);
2343 ret = 0;
2345 break;
2347 case PROXY_SOCKS5_WANT_CONNECT_OK:
2348 ret = connection_fetch_from_buf_socks_client(conn,
2349 conn->proxy_state,
2350 &reason);
2351 if (ret == 1)
2352 conn->proxy_state = PROXY_CONNECTED;
2353 break;
2355 default:
2356 log_err(LD_BUG, "Invalid proxy_state for reading, %d",
2357 conn->proxy_state);
2358 tor_fragile_assert();
2359 ret = -1;
2360 break;
2363 log_debug(LD_NET, "leaving state %s",
2364 connection_proxy_state_to_string(conn->proxy_state));
2366 if (ret < 0) {
2367 if (reason) {
2368 log_warn(LD_NET, "Proxy Client: unable to connect to %s:%d (%s)",
2369 conn->address, conn->port, escaped(reason));
2370 tor_free(reason);
2371 } else {
2372 log_warn(LD_NET, "Proxy Client: unable to connect to %s:%d",
2373 conn->address, conn->port);
2375 } else if (ret == 1) {
2376 log_info(LD_NET, "Proxy Client: connection to %s:%d successful",
2377 conn->address, conn->port);
2380 return ret;
2383 /** Given a list of listener connections in <b>old_conns</b>, and list of
2384 * port_cfg_t entries in <b>ports</b>, open a new listener for every port in
2385 * <b>ports</b> that does not already have a listener in <b>old_conns</b>.
2387 * Remove from <b>old_conns</b> every connection that has a corresponding
2388 * entry in <b>ports</b>. Add to <b>new_conns</b> new every connection we
2389 * launch.
2391 * If <b>control_listeners_only</b> is true, then we only open control
2392 * listeners, and we do not remove any noncontrol listeners from old_conns.
2394 * Return 0 on success, -1 on failure.
2396 static int
2397 retry_listener_ports(smartlist_t *old_conns,
2398 const smartlist_t *ports,
2399 smartlist_t *new_conns,
2400 int control_listeners_only)
2402 smartlist_t *launch = smartlist_new();
2403 int r = 0;
2405 if (control_listeners_only) {
2406 SMARTLIST_FOREACH(ports, port_cfg_t *, p, {
2407 if (p->type == CONN_TYPE_CONTROL_LISTENER)
2408 smartlist_add(launch, p);
2410 } else {
2411 smartlist_add_all(launch, ports);
2414 /* Iterate through old_conns, comparing it to launch: remove from both lists
2415 * each pair of elements that corresponds to the same port. */
2416 SMARTLIST_FOREACH_BEGIN(old_conns, connection_t *, conn) {
2417 const port_cfg_t *found_port = NULL;
2419 /* Okay, so this is a listener. Is it configured? */
2420 SMARTLIST_FOREACH_BEGIN(launch, const port_cfg_t *, wanted) {
2421 if (conn->type != wanted->type)
2422 continue;
2423 if ((conn->socket_family != AF_UNIX && wanted->is_unix_addr) ||
2424 (conn->socket_family == AF_UNIX && ! wanted->is_unix_addr))
2425 continue;
2427 if (wanted->server_cfg.no_listen)
2428 continue; /* We don't want to open a listener for this one */
2430 if (wanted->is_unix_addr) {
2431 if (conn->socket_family == AF_UNIX &&
2432 !strcmp(wanted->unix_addr, conn->address)) {
2433 found_port = wanted;
2434 break;
2436 } else {
2437 int port_matches;
2438 if (wanted->port == CFG_AUTO_PORT) {
2439 port_matches = 1;
2440 } else {
2441 port_matches = (wanted->port == conn->port);
2443 if (port_matches && tor_addr_eq(&wanted->addr, &conn->addr)) {
2444 found_port = wanted;
2445 break;
2448 } SMARTLIST_FOREACH_END(wanted);
2450 if (found_port) {
2451 /* This listener is already running; we don't need to launch it. */
2452 //log_debug(LD_NET, "Already have %s on %s:%d",
2453 // conn_type_to_string(found_port->type), conn->address, conn->port);
2454 smartlist_remove(launch, found_port);
2455 /* And we can remove the connection from old_conns too. */
2456 SMARTLIST_DEL_CURRENT(old_conns, conn);
2458 } SMARTLIST_FOREACH_END(conn);
2460 /* Now open all the listeners that are configured but not opened. */
2461 SMARTLIST_FOREACH_BEGIN(launch, const port_cfg_t *, port) {
2462 struct sockaddr *listensockaddr;
2463 socklen_t listensocklen = 0;
2464 char *address=NULL;
2465 connection_t *conn;
2466 int real_port = port->port == CFG_AUTO_PORT ? 0 : port->port;
2467 tor_assert(real_port <= UINT16_MAX);
2468 if (port->server_cfg.no_listen)
2469 continue;
2471 #ifndef _WIN32
2472 /* We don't need to be root to create a UNIX socket, so defer until after
2473 * setuid. */
2474 const or_options_t *options = get_options();
2475 if (port->is_unix_addr && !geteuid() && (options->User) &&
2476 strcmp(options->User, "root"))
2477 continue;
2478 #endif
2480 if (port->is_unix_addr) {
2481 listensockaddr = (struct sockaddr *)
2482 create_unix_sockaddr(port->unix_addr,
2483 &address, &listensocklen);
2484 } else {
2485 listensockaddr = tor_malloc(sizeof(struct sockaddr_storage));
2486 listensocklen = tor_addr_to_sockaddr(&port->addr,
2487 real_port,
2488 listensockaddr,
2489 sizeof(struct sockaddr_storage));
2490 address = tor_addr_to_str_dup(&port->addr);
2493 if (listensockaddr) {
2494 conn = connection_listener_new(listensockaddr, listensocklen,
2495 port->type, address, port);
2496 tor_free(listensockaddr);
2497 tor_free(address);
2498 } else {
2499 conn = NULL;
2502 if (!conn) {
2503 r = -1;
2504 } else {
2505 if (new_conns)
2506 smartlist_add(new_conns, conn);
2508 } SMARTLIST_FOREACH_END(port);
2510 smartlist_free(launch);
2512 return r;
2515 /** Launch listeners for each port you should have open. Only launch
2516 * listeners who are not already open, and only close listeners we no longer
2517 * want.
2519 * Add all old conns that should be closed to <b>replaced_conns</b>.
2520 * Add all new connections to <b>new_conns</b>.
2522 * If <b>close_all_noncontrol</b> is true, then we only open control
2523 * listeners, and we close all other listeners.
2526 retry_all_listeners(smartlist_t *replaced_conns,
2527 smartlist_t *new_conns, int close_all_noncontrol)
2529 smartlist_t *listeners = smartlist_new();
2530 const or_options_t *options = get_options();
2531 int retval = 0;
2532 const uint16_t old_or_port = router_get_advertised_or_port(options);
2533 const uint16_t old_or_port_ipv6 =
2534 router_get_advertised_or_port_by_af(options,AF_INET6);
2535 const uint16_t old_dir_port = router_get_advertised_dir_port(options, 0);
2537 SMARTLIST_FOREACH_BEGIN(get_connection_array(), connection_t *, conn) {
2538 if (connection_is_listener(conn) && !conn->marked_for_close)
2539 smartlist_add(listeners, conn);
2540 } SMARTLIST_FOREACH_END(conn);
2542 if (retry_listener_ports(listeners,
2543 get_configured_ports(),
2544 new_conns,
2545 close_all_noncontrol) < 0)
2546 retval = -1;
2548 /* Any members that were still in 'listeners' don't correspond to
2549 * any configured port. Kill 'em. */
2550 SMARTLIST_FOREACH_BEGIN(listeners, connection_t *, conn) {
2551 log_notice(LD_NET, "Closing no-longer-configured %s on %s:%d",
2552 conn_type_to_string(conn->type), conn->address, conn->port);
2553 if (replaced_conns) {
2554 smartlist_add(replaced_conns, conn);
2555 } else {
2556 connection_close_immediate(conn);
2557 connection_mark_for_close(conn);
2559 } SMARTLIST_FOREACH_END(conn);
2561 smartlist_free(listeners);
2563 if (old_or_port != router_get_advertised_or_port(options) ||
2564 old_or_port_ipv6 != router_get_advertised_or_port_by_af(options,
2565 AF_INET6) ||
2566 old_dir_port != router_get_advertised_dir_port(options, 0)) {
2567 /* Our chosen ORPort or DirPort is not what it used to be: the
2568 * descriptor we had (if any) should be regenerated. (We won't
2569 * automatically notice this because of changes in the option,
2570 * since the value could be "auto".) */
2571 mark_my_descriptor_dirty("Chosen Or/DirPort changed");
2574 return retval;
2577 /** Mark every listener of type other than CONTROL_LISTENER to be closed. */
2578 void
2579 connection_mark_all_noncontrol_listeners(void)
2581 SMARTLIST_FOREACH_BEGIN(get_connection_array(), connection_t *, conn) {
2582 if (conn->marked_for_close)
2583 continue;
2584 if (conn->type == CONN_TYPE_CONTROL_LISTENER)
2585 continue;
2586 if (connection_is_listener(conn))
2587 connection_mark_for_close(conn);
2588 } SMARTLIST_FOREACH_END(conn);
2591 /** Mark every external connection not used for controllers for close. */
2592 void
2593 connection_mark_all_noncontrol_connections(void)
2595 SMARTLIST_FOREACH_BEGIN(get_connection_array(), connection_t *, conn) {
2596 if (conn->marked_for_close)
2597 continue;
2598 switch (conn->type) {
2599 case CONN_TYPE_CONTROL_LISTENER:
2600 case CONN_TYPE_CONTROL:
2601 break;
2602 case CONN_TYPE_AP:
2603 connection_mark_unattached_ap(TO_ENTRY_CONN(conn),
2604 END_STREAM_REASON_HIBERNATING);
2605 break;
2606 case CONN_TYPE_OR:
2608 or_connection_t *orconn = TO_OR_CONN(conn);
2609 if (orconn->chan) {
2610 connection_or_close_normally(orconn, 0);
2611 } else {
2613 * There should have been one, but mark for close and hope
2614 * for the best..
2616 connection_mark_for_close(conn);
2619 break;
2620 default:
2621 connection_mark_for_close(conn);
2622 break;
2624 } SMARTLIST_FOREACH_END(conn);
2627 /** Return 1 if we should apply rate limiting to <b>conn</b>, and 0
2628 * otherwise.
2629 * Right now this just checks if it's an internal IP address or an
2630 * internal connection. We also should, but don't, check if the connection
2631 * uses pluggable transports, since we should then limit it even if it
2632 * comes from an internal IP address. */
2633 static int
2634 connection_is_rate_limited(connection_t *conn)
2636 const or_options_t *options = get_options();
2637 if (conn->linked)
2638 return 0; /* Internal connection */
2639 else if (! options->CountPrivateBandwidth &&
2640 (tor_addr_family(&conn->addr) == AF_UNSPEC || /* no address */
2641 tor_addr_family(&conn->addr) == AF_UNIX || /* no address */
2642 tor_addr_is_internal(&conn->addr, 0)))
2643 return 0; /* Internal address */
2644 else
2645 return 1;
2648 /** Did either global write bucket run dry last second? If so,
2649 * we are likely to run dry again this second, so be stingy with the
2650 * tokens we just put in. */
2651 static int write_buckets_empty_last_second = 0;
2653 /** How many seconds of no active local circuits will make the
2654 * connection revert to the "relayed" bandwidth class? */
2655 #define CLIENT_IDLE_TIME_FOR_PRIORITY 30
2657 /** Return 1 if <b>conn</b> should use tokens from the "relayed"
2658 * bandwidth rates, else 0. Currently, only OR conns with bandwidth
2659 * class 1, and directory conns that are serving data out, count.
2661 static int
2662 connection_counts_as_relayed_traffic(connection_t *conn, time_t now)
2664 if (conn->type == CONN_TYPE_OR &&
2665 connection_or_client_used(TO_OR_CONN(conn)) +
2666 CLIENT_IDLE_TIME_FOR_PRIORITY < now)
2667 return 1;
2668 if (conn->type == CONN_TYPE_DIR && DIR_CONN_IS_SERVER(conn))
2669 return 1;
2670 return 0;
2673 /** Helper function to decide how many bytes out of <b>global_bucket</b>
2674 * we're willing to use for this transaction. <b>base</b> is the size
2675 * of a cell on the network; <b>priority</b> says whether we should
2676 * write many of them or just a few; and <b>conn_bucket</b> (if
2677 * non-negative) provides an upper limit for our answer. */
2678 static ssize_t
2679 connection_bucket_round_robin(int base, int priority,
2680 ssize_t global_bucket, ssize_t conn_bucket)
2682 ssize_t at_most;
2683 ssize_t num_bytes_high = (priority ? 32 : 16) * base;
2684 ssize_t num_bytes_low = (priority ? 4 : 2) * base;
2686 /* Do a rudimentary round-robin so one circuit can't hog a connection.
2687 * Pick at most 32 cells, at least 4 cells if possible, and if we're in
2688 * the middle pick 1/8 of the available bandwidth. */
2689 at_most = global_bucket / 8;
2690 at_most -= (at_most % base); /* round down */
2691 if (at_most > num_bytes_high) /* 16 KB, or 8 KB for low-priority */
2692 at_most = num_bytes_high;
2693 else if (at_most < num_bytes_low) /* 2 KB, or 1 KB for low-priority */
2694 at_most = num_bytes_low;
2696 if (at_most > global_bucket)
2697 at_most = global_bucket;
2699 if (conn_bucket >= 0 && at_most > conn_bucket)
2700 at_most = conn_bucket;
2702 if (at_most < 0)
2703 return 0;
2704 return at_most;
2707 /** How many bytes at most can we read onto this connection? */
2708 static ssize_t
2709 connection_bucket_read_limit(connection_t *conn, time_t now)
2711 int base = RELAY_PAYLOAD_SIZE;
2712 int priority = conn->type != CONN_TYPE_DIR;
2713 int conn_bucket = -1;
2714 int global_bucket = global_read_bucket;
2716 if (connection_speaks_cells(conn)) {
2717 or_connection_t *or_conn = TO_OR_CONN(conn);
2718 if (conn->state == OR_CONN_STATE_OPEN)
2719 conn_bucket = or_conn->read_bucket;
2720 base = get_cell_network_size(or_conn->wide_circ_ids);
2723 if (!connection_is_rate_limited(conn)) {
2724 /* be willing to read on local conns even if our buckets are empty */
2725 return conn_bucket>=0 ? conn_bucket : 1<<14;
2728 if (connection_counts_as_relayed_traffic(conn, now) &&
2729 global_relayed_read_bucket <= global_read_bucket)
2730 global_bucket = global_relayed_read_bucket;
2732 return connection_bucket_round_robin(base, priority,
2733 global_bucket, conn_bucket);
2736 /** How many bytes at most can we write onto this connection? */
2737 ssize_t
2738 connection_bucket_write_limit(connection_t *conn, time_t now)
2740 int base = RELAY_PAYLOAD_SIZE;
2741 int priority = conn->type != CONN_TYPE_DIR;
2742 int conn_bucket = (int)conn->outbuf_flushlen;
2743 int global_bucket = global_write_bucket;
2745 if (!connection_is_rate_limited(conn)) {
2746 /* be willing to write to local conns even if our buckets are empty */
2747 return conn->outbuf_flushlen;
2750 if (connection_speaks_cells(conn)) {
2751 /* use the per-conn write limit if it's lower, but if it's less
2752 * than zero just use zero */
2753 or_connection_t *or_conn = TO_OR_CONN(conn);
2754 if (conn->state == OR_CONN_STATE_OPEN)
2755 if (or_conn->write_bucket < conn_bucket)
2756 conn_bucket = or_conn->write_bucket >= 0 ?
2757 or_conn->write_bucket : 0;
2758 base = get_cell_network_size(or_conn->wide_circ_ids);
2761 if (connection_counts_as_relayed_traffic(conn, now) &&
2762 global_relayed_write_bucket <= global_write_bucket)
2763 global_bucket = global_relayed_write_bucket;
2765 return connection_bucket_round_robin(base, priority,
2766 global_bucket, conn_bucket);
2769 /** Return 1 if the global write buckets are low enough that we
2770 * shouldn't send <b>attempt</b> bytes of low-priority directory stuff
2771 * out to <b>conn</b>. Else return 0.
2773 * Priority was 1 for v1 requests (directories and running-routers),
2774 * and 2 for v2 requests and later (statuses and descriptors).
2776 * There are a lot of parameters we could use here:
2777 * - global_relayed_write_bucket. Low is bad.
2778 * - global_write_bucket. Low is bad.
2779 * - bandwidthrate. Low is bad.
2780 * - bandwidthburst. Not a big factor?
2781 * - attempt. High is bad.
2782 * - total bytes queued on outbufs. High is bad. But I'm wary of
2783 * using this, since a few slow-flushing queues will pump up the
2784 * number without meaning what we meant to mean. What we really
2785 * mean is "total directory bytes added to outbufs recently", but
2786 * that's harder to quantify and harder to keep track of.
2789 global_write_bucket_low(connection_t *conn, size_t attempt, int priority)
2791 int smaller_bucket = global_write_bucket < global_relayed_write_bucket ?
2792 global_write_bucket : global_relayed_write_bucket;
2793 if (authdir_mode(get_options()) && priority>1)
2794 return 0; /* there's always room to answer v2 if we're an auth dir */
2796 if (!connection_is_rate_limited(conn))
2797 return 0; /* local conns don't get limited */
2799 if (smaller_bucket < (int)attempt)
2800 return 1; /* not enough space no matter the priority */
2802 if (write_buckets_empty_last_second)
2803 return 1; /* we're already hitting our limits, no more please */
2805 if (priority == 1) { /* old-style v1 query */
2806 /* Could we handle *two* of these requests within the next two seconds? */
2807 const or_options_t *options = get_options();
2808 int64_t can_write = (int64_t)smaller_bucket
2809 + 2*(options->RelayBandwidthRate ? options->RelayBandwidthRate :
2810 options->BandwidthRate);
2811 if (can_write < 2*(int64_t)attempt)
2812 return 1;
2813 } else { /* v2 query */
2814 /* no further constraints yet */
2816 return 0;
2819 /** Helper: adjusts our bandwidth history and informs the controller as
2820 * appropriate, given that we have just read <b>num_read</b> bytes and written
2821 * <b>num_written</b> bytes on <b>conn</b>. */
2822 static void
2823 record_num_bytes_transferred_impl(connection_t *conn,
2824 time_t now, size_t num_read, size_t num_written)
2826 /* Count bytes of answering direct and tunneled directory requests */
2827 if (conn->type == CONN_TYPE_DIR && conn->purpose == DIR_PURPOSE_SERVER) {
2828 if (num_read > 0)
2829 rep_hist_note_dir_bytes_read(num_read, now);
2830 if (num_written > 0)
2831 rep_hist_note_dir_bytes_written(num_written, now);
2834 if (!connection_is_rate_limited(conn))
2835 return; /* local IPs are free */
2837 if (conn->type == CONN_TYPE_OR)
2838 rep_hist_note_or_conn_bytes(conn->global_identifier, num_read,
2839 num_written, now);
2841 if (num_read > 0) {
2842 rep_hist_note_bytes_read(num_read, now);
2844 if (num_written > 0) {
2845 rep_hist_note_bytes_written(num_written, now);
2847 if (conn->type == CONN_TYPE_EXIT)
2848 rep_hist_note_exit_bytes(conn->port, num_written, num_read);
2851 /** Helper: convert given <b>tvnow</b> time value to milliseconds since
2852 * midnight. */
2853 static uint32_t
2854 msec_since_midnight(const struct timeval *tvnow)
2856 return (uint32_t)(((tvnow->tv_sec % 86400L) * 1000L) +
2857 ((uint32_t)tvnow->tv_usec / (uint32_t)1000L));
2860 /** Helper: return the time in milliseconds since <b>last_empty_time</b>
2861 * when a bucket ran empty that previously had <b>tokens_before</b> tokens
2862 * now has <b>tokens_after</b> tokens after refilling at timestamp
2863 * <b>tvnow</b>, capped at <b>milliseconds_elapsed</b> milliseconds since
2864 * last refilling that bucket. Return 0 if the bucket has not been empty
2865 * since the last refill or has not been refilled. */
2866 uint32_t
2867 bucket_millis_empty(int tokens_before, uint32_t last_empty_time,
2868 int tokens_after, int milliseconds_elapsed,
2869 const struct timeval *tvnow)
2871 uint32_t result = 0, refilled;
2872 if (tokens_before <= 0 && tokens_after > tokens_before) {
2873 refilled = msec_since_midnight(tvnow);
2874 result = (uint32_t)((refilled + 86400L * 1000L - last_empty_time) %
2875 (86400L * 1000L));
2876 if (result > (uint32_t)milliseconds_elapsed)
2877 result = (uint32_t)milliseconds_elapsed;
2879 return result;
2882 /** Check if a bucket which had <b>tokens_before</b> tokens and which got
2883 * <b>tokens_removed</b> tokens removed at timestamp <b>tvnow</b> has run
2884 * out of tokens, and if so, note the milliseconds since midnight in
2885 * <b>timestamp_var</b> for the next TB_EMPTY event. */
2886 void
2887 connection_buckets_note_empty_ts(uint32_t *timestamp_var,
2888 int tokens_before, size_t tokens_removed,
2889 const struct timeval *tvnow)
2891 if (tokens_before > 0 && (uint32_t)tokens_before <= tokens_removed)
2892 *timestamp_var = msec_since_midnight(tvnow);
2895 /** Last time at which the global or relay buckets were emptied in msec
2896 * since midnight. */
2897 static uint32_t global_relayed_read_emptied = 0,
2898 global_relayed_write_emptied = 0,
2899 global_read_emptied = 0,
2900 global_write_emptied = 0;
2902 /** We just read <b>num_read</b> and wrote <b>num_written</b> bytes
2903 * onto <b>conn</b>. Decrement buckets appropriately. */
2904 static void
2905 connection_buckets_decrement(connection_t *conn, time_t now,
2906 size_t num_read, size_t num_written)
2908 if (num_written >= INT_MAX || num_read >= INT_MAX) {
2909 log_err(LD_BUG, "Value out of range. num_read=%lu, num_written=%lu, "
2910 "connection type=%s, state=%s",
2911 (unsigned long)num_read, (unsigned long)num_written,
2912 conn_type_to_string(conn->type),
2913 conn_state_to_string(conn->type, conn->state));
2914 if (num_written >= INT_MAX) num_written = 1;
2915 if (num_read >= INT_MAX) num_read = 1;
2916 tor_fragile_assert();
2919 record_num_bytes_transferred_impl(conn, now, num_read, num_written);
2921 if (!connection_is_rate_limited(conn))
2922 return; /* local IPs are free */
2924 /* If one or more of our token buckets ran dry just now, note the
2925 * timestamp for TB_EMPTY events. */
2926 if (get_options()->TestingEnableTbEmptyEvent) {
2927 struct timeval tvnow;
2928 tor_gettimeofday_cached(&tvnow);
2929 if (connection_counts_as_relayed_traffic(conn, now)) {
2930 connection_buckets_note_empty_ts(&global_relayed_read_emptied,
2931 global_relayed_read_bucket, num_read, &tvnow);
2932 connection_buckets_note_empty_ts(&global_relayed_write_emptied,
2933 global_relayed_write_bucket, num_written, &tvnow);
2935 connection_buckets_note_empty_ts(&global_read_emptied,
2936 global_read_bucket, num_read, &tvnow);
2937 connection_buckets_note_empty_ts(&global_write_emptied,
2938 global_write_bucket, num_written, &tvnow);
2939 if (connection_speaks_cells(conn) && conn->state == OR_CONN_STATE_OPEN) {
2940 or_connection_t *or_conn = TO_OR_CONN(conn);
2941 connection_buckets_note_empty_ts(&or_conn->read_emptied_time,
2942 or_conn->read_bucket, num_read, &tvnow);
2943 connection_buckets_note_empty_ts(&or_conn->write_emptied_time,
2944 or_conn->write_bucket, num_written, &tvnow);
2948 if (connection_counts_as_relayed_traffic(conn, now)) {
2949 global_relayed_read_bucket -= (int)num_read;
2950 global_relayed_write_bucket -= (int)num_written;
2952 global_read_bucket -= (int)num_read;
2953 global_write_bucket -= (int)num_written;
2954 if (connection_speaks_cells(conn) && conn->state == OR_CONN_STATE_OPEN) {
2955 TO_OR_CONN(conn)->read_bucket -= (int)num_read;
2956 TO_OR_CONN(conn)->write_bucket -= (int)num_written;
2960 /** If we have exhausted our global buckets, or the buckets for conn,
2961 * stop reading. */
2962 static void
2963 connection_consider_empty_read_buckets(connection_t *conn)
2965 const char *reason;
2967 if (!connection_is_rate_limited(conn))
2968 return; /* Always okay. */
2970 if (global_read_bucket <= 0) {
2971 reason = "global read bucket exhausted. Pausing.";
2972 } else if (connection_counts_as_relayed_traffic(conn, approx_time()) &&
2973 global_relayed_read_bucket <= 0) {
2974 reason = "global relayed read bucket exhausted. Pausing.";
2975 } else if (connection_speaks_cells(conn) &&
2976 conn->state == OR_CONN_STATE_OPEN &&
2977 TO_OR_CONN(conn)->read_bucket <= 0) {
2978 reason = "connection read bucket exhausted. Pausing.";
2979 } else
2980 return; /* all good, no need to stop it */
2982 LOG_FN_CONN(conn, (LOG_DEBUG, LD_NET, "%s", reason));
2983 conn->read_blocked_on_bw = 1;
2984 connection_stop_reading(conn);
2987 /** If we have exhausted our global buckets, or the buckets for conn,
2988 * stop writing. */
2989 static void
2990 connection_consider_empty_write_buckets(connection_t *conn)
2992 const char *reason;
2994 if (!connection_is_rate_limited(conn))
2995 return; /* Always okay. */
2997 if (global_write_bucket <= 0) {
2998 reason = "global write bucket exhausted. Pausing.";
2999 } else if (connection_counts_as_relayed_traffic(conn, approx_time()) &&
3000 global_relayed_write_bucket <= 0) {
3001 reason = "global relayed write bucket exhausted. Pausing.";
3002 } else if (connection_speaks_cells(conn) &&
3003 conn->state == OR_CONN_STATE_OPEN &&
3004 TO_OR_CONN(conn)->write_bucket <= 0) {
3005 reason = "connection write bucket exhausted. Pausing.";
3006 } else
3007 return; /* all good, no need to stop it */
3009 LOG_FN_CONN(conn, (LOG_DEBUG, LD_NET, "%s", reason));
3010 conn->write_blocked_on_bw = 1;
3011 connection_stop_writing(conn);
3014 /** Initialize the global read bucket to options-\>BandwidthBurst. */
3015 void
3016 connection_bucket_init(void)
3018 const or_options_t *options = get_options();
3019 /* start it at max traffic */
3020 global_read_bucket = (int)options->BandwidthBurst;
3021 global_write_bucket = (int)options->BandwidthBurst;
3022 if (options->RelayBandwidthRate) {
3023 global_relayed_read_bucket = (int)options->RelayBandwidthBurst;
3024 global_relayed_write_bucket = (int)options->RelayBandwidthBurst;
3025 } else {
3026 global_relayed_read_bucket = (int)options->BandwidthBurst;
3027 global_relayed_write_bucket = (int)options->BandwidthBurst;
3031 /** Refill a single <b>bucket</b> called <b>name</b> with bandwidth rate per
3032 * second <b>rate</b> and bandwidth burst <b>burst</b>, assuming that
3033 * <b>milliseconds_elapsed</b> milliseconds have passed since the last
3034 * call. */
3035 static void
3036 connection_bucket_refill_helper(int *bucket, int rate, int burst,
3037 int milliseconds_elapsed,
3038 const char *name)
3040 int starting_bucket = *bucket;
3041 if (starting_bucket < burst && milliseconds_elapsed > 0) {
3042 int64_t incr = (((int64_t)rate) * milliseconds_elapsed) / 1000;
3043 if ((burst - starting_bucket) < incr) {
3044 *bucket = burst; /* We would overflow the bucket; just set it to
3045 * the maximum. */
3046 } else {
3047 *bucket += (int)incr;
3048 if (*bucket > burst || *bucket < starting_bucket) {
3049 /* If we overflow the burst, or underflow our starting bucket,
3050 * cap the bucket value to burst. */
3051 /* XXXX this might be redundant now, but it doesn't show up
3052 * in profiles. Remove it after analysis. */
3053 *bucket = burst;
3056 log_debug(LD_NET,"%s now %d.", name, *bucket);
3060 /** Time has passed; increment buckets appropriately. */
3061 void
3062 connection_bucket_refill(int milliseconds_elapsed, time_t now)
3064 const or_options_t *options = get_options();
3065 smartlist_t *conns = get_connection_array();
3066 int bandwidthrate, bandwidthburst, relayrate, relayburst;
3068 int prev_global_read = global_read_bucket;
3069 int prev_global_write = global_write_bucket;
3070 int prev_relay_read = global_relayed_read_bucket;
3071 int prev_relay_write = global_relayed_write_bucket;
3072 struct timeval tvnow; /*< Only used if TB_EMPTY events are enabled. */
3074 bandwidthrate = (int)options->BandwidthRate;
3075 bandwidthburst = (int)options->BandwidthBurst;
3077 if (options->RelayBandwidthRate) {
3078 relayrate = (int)options->RelayBandwidthRate;
3079 relayburst = (int)options->RelayBandwidthBurst;
3080 } else {
3081 relayrate = bandwidthrate;
3082 relayburst = bandwidthburst;
3085 tor_assert(milliseconds_elapsed >= 0);
3087 write_buckets_empty_last_second =
3088 global_relayed_write_bucket <= 0 || global_write_bucket <= 0;
3090 /* refill the global buckets */
3091 connection_bucket_refill_helper(&global_read_bucket,
3092 bandwidthrate, bandwidthburst,
3093 milliseconds_elapsed,
3094 "global_read_bucket");
3095 connection_bucket_refill_helper(&global_write_bucket,
3096 bandwidthrate, bandwidthburst,
3097 milliseconds_elapsed,
3098 "global_write_bucket");
3099 connection_bucket_refill_helper(&global_relayed_read_bucket,
3100 relayrate, relayburst,
3101 milliseconds_elapsed,
3102 "global_relayed_read_bucket");
3103 connection_bucket_refill_helper(&global_relayed_write_bucket,
3104 relayrate, relayburst,
3105 milliseconds_elapsed,
3106 "global_relayed_write_bucket");
3108 /* If buckets were empty before and have now been refilled, tell any
3109 * interested controllers. */
3110 if (get_options()->TestingEnableTbEmptyEvent) {
3111 uint32_t global_read_empty_time, global_write_empty_time,
3112 relay_read_empty_time, relay_write_empty_time;
3113 tor_gettimeofday_cached(&tvnow);
3114 global_read_empty_time = bucket_millis_empty(prev_global_read,
3115 global_read_emptied, global_read_bucket,
3116 milliseconds_elapsed, &tvnow);
3117 global_write_empty_time = bucket_millis_empty(prev_global_write,
3118 global_write_emptied, global_write_bucket,
3119 milliseconds_elapsed, &tvnow);
3120 control_event_tb_empty("GLOBAL", global_read_empty_time,
3121 global_write_empty_time, milliseconds_elapsed);
3122 relay_read_empty_time = bucket_millis_empty(prev_relay_read,
3123 global_relayed_read_emptied,
3124 global_relayed_read_bucket,
3125 milliseconds_elapsed, &tvnow);
3126 relay_write_empty_time = bucket_millis_empty(prev_relay_write,
3127 global_relayed_write_emptied,
3128 global_relayed_write_bucket,
3129 milliseconds_elapsed, &tvnow);
3130 control_event_tb_empty("RELAY", relay_read_empty_time,
3131 relay_write_empty_time, milliseconds_elapsed);
3134 /* refill the per-connection buckets */
3135 SMARTLIST_FOREACH_BEGIN(conns, connection_t *, conn) {
3136 if (connection_speaks_cells(conn)) {
3137 or_connection_t *or_conn = TO_OR_CONN(conn);
3138 int orbandwidthrate = or_conn->bandwidthrate;
3139 int orbandwidthburst = or_conn->bandwidthburst;
3141 int prev_conn_read = or_conn->read_bucket;
3142 int prev_conn_write = or_conn->write_bucket;
3144 if (connection_bucket_should_increase(or_conn->read_bucket, or_conn)) {
3145 connection_bucket_refill_helper(&or_conn->read_bucket,
3146 orbandwidthrate,
3147 orbandwidthburst,
3148 milliseconds_elapsed,
3149 "or_conn->read_bucket");
3151 if (connection_bucket_should_increase(or_conn->write_bucket, or_conn)) {
3152 connection_bucket_refill_helper(&or_conn->write_bucket,
3153 orbandwidthrate,
3154 orbandwidthburst,
3155 milliseconds_elapsed,
3156 "or_conn->write_bucket");
3159 /* If buckets were empty before and have now been refilled, tell any
3160 * interested controllers. */
3161 if (get_options()->TestingEnableTbEmptyEvent) {
3162 char *bucket;
3163 uint32_t conn_read_empty_time, conn_write_empty_time;
3164 tor_asprintf(&bucket, "ORCONN ID="U64_FORMAT,
3165 U64_PRINTF_ARG(or_conn->base_.global_identifier));
3166 conn_read_empty_time = bucket_millis_empty(prev_conn_read,
3167 or_conn->read_emptied_time,
3168 or_conn->read_bucket,
3169 milliseconds_elapsed, &tvnow);
3170 conn_write_empty_time = bucket_millis_empty(prev_conn_write,
3171 or_conn->write_emptied_time,
3172 or_conn->write_bucket,
3173 milliseconds_elapsed, &tvnow);
3174 control_event_tb_empty(bucket, conn_read_empty_time,
3175 conn_write_empty_time,
3176 milliseconds_elapsed);
3177 tor_free(bucket);
3181 if (conn->read_blocked_on_bw == 1 /* marked to turn reading back on now */
3182 && global_read_bucket > 0 /* and we're allowed to read */
3183 && (!connection_counts_as_relayed_traffic(conn, now) ||
3184 global_relayed_read_bucket > 0) /* even if we're relayed traffic */
3185 && (!connection_speaks_cells(conn) ||
3186 conn->state != OR_CONN_STATE_OPEN ||
3187 TO_OR_CONN(conn)->read_bucket > 0)) {
3188 /* and either a non-cell conn or a cell conn with non-empty bucket */
3189 LOG_FN_CONN(conn, (LOG_DEBUG,LD_NET,
3190 "waking up conn (fd %d) for read", (int)conn->s));
3191 conn->read_blocked_on_bw = 0;
3192 connection_start_reading(conn);
3195 if (conn->write_blocked_on_bw == 1
3196 && global_write_bucket > 0 /* and we're allowed to write */
3197 && (!connection_counts_as_relayed_traffic(conn, now) ||
3198 global_relayed_write_bucket > 0) /* even if it's relayed traffic */
3199 && (!connection_speaks_cells(conn) ||
3200 conn->state != OR_CONN_STATE_OPEN ||
3201 TO_OR_CONN(conn)->write_bucket > 0)) {
3202 LOG_FN_CONN(conn, (LOG_DEBUG,LD_NET,
3203 "waking up conn (fd %d) for write", (int)conn->s));
3204 conn->write_blocked_on_bw = 0;
3205 connection_start_writing(conn);
3207 } SMARTLIST_FOREACH_END(conn);
3210 /** Is the <b>bucket</b> for connection <b>conn</b> low enough that we
3211 * should add another pile of tokens to it?
3213 static int
3214 connection_bucket_should_increase(int bucket, or_connection_t *conn)
3216 tor_assert(conn);
3218 if (conn->base_.state != OR_CONN_STATE_OPEN)
3219 return 0; /* only open connections play the rate limiting game */
3220 if (bucket >= conn->bandwidthburst)
3221 return 0;
3223 return 1;
3226 /** Read bytes from conn-\>s and process them.
3228 * It calls connection_read_to_buf() to bring in any new bytes,
3229 * and then calls connection_process_inbuf() to process them.
3231 * Mark the connection and return -1 if you want to close it, else
3232 * return 0.
3234 static int
3235 connection_handle_read_impl(connection_t *conn)
3237 ssize_t max_to_read=-1, try_to_read;
3238 size_t before, n_read = 0;
3239 int socket_error = 0;
3241 if (conn->marked_for_close)
3242 return 0; /* do nothing */
3244 conn->timestamp_lastread = approx_time();
3246 switch (conn->type) {
3247 case CONN_TYPE_OR_LISTENER:
3248 return connection_handle_listener_read(conn, CONN_TYPE_OR);
3249 case CONN_TYPE_EXT_OR_LISTENER:
3250 return connection_handle_listener_read(conn, CONN_TYPE_EXT_OR);
3251 case CONN_TYPE_AP_LISTENER:
3252 case CONN_TYPE_AP_TRANS_LISTENER:
3253 case CONN_TYPE_AP_NATD_LISTENER:
3254 return connection_handle_listener_read(conn, CONN_TYPE_AP);
3255 case CONN_TYPE_DIR_LISTENER:
3256 return connection_handle_listener_read(conn, CONN_TYPE_DIR);
3257 case CONN_TYPE_CONTROL_LISTENER:
3258 return connection_handle_listener_read(conn, CONN_TYPE_CONTROL);
3259 case CONN_TYPE_AP_DNS_LISTENER:
3260 /* This should never happen; eventdns.c handles the reads here. */
3261 tor_fragile_assert();
3262 return 0;
3265 loop_again:
3266 try_to_read = max_to_read;
3267 tor_assert(!conn->marked_for_close);
3269 before = buf_datalen(conn->inbuf);
3270 if (connection_read_to_buf(conn, &max_to_read, &socket_error) < 0) {
3271 /* There's a read error; kill the connection.*/
3272 if (conn->type == CONN_TYPE_OR) {
3273 connection_or_notify_error(TO_OR_CONN(conn),
3274 socket_error != 0 ?
3275 errno_to_orconn_end_reason(socket_error) :
3276 END_OR_CONN_REASON_CONNRESET,
3277 socket_error != 0 ?
3278 tor_socket_strerror(socket_error) :
3279 "(unknown, errno was 0)");
3281 if (CONN_IS_EDGE(conn)) {
3282 edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
3283 connection_edge_end_errno(edge_conn);
3284 if (conn->type == CONN_TYPE_AP && TO_ENTRY_CONN(conn)->socks_request) {
3285 /* broken, don't send a socks reply back */
3286 TO_ENTRY_CONN(conn)->socks_request->has_finished = 1;
3289 connection_close_immediate(conn); /* Don't flush; connection is dead. */
3291 * This can bypass normal channel checking since we did
3292 * connection_or_notify_error() above.
3294 connection_mark_for_close_internal(conn);
3295 return -1;
3297 n_read += buf_datalen(conn->inbuf) - before;
3298 if (CONN_IS_EDGE(conn) && try_to_read != max_to_read) {
3299 /* instruct it not to try to package partial cells. */
3300 if (connection_process_inbuf(conn, 0) < 0) {
3301 return -1;
3303 if (!conn->marked_for_close &&
3304 connection_is_reading(conn) &&
3305 !conn->inbuf_reached_eof &&
3306 max_to_read > 0)
3307 goto loop_again; /* try reading again, in case more is here now */
3309 /* one last try, packaging partial cells and all. */
3310 if (!conn->marked_for_close &&
3311 connection_process_inbuf(conn, 1) < 0) {
3312 return -1;
3314 if (conn->linked_conn) {
3315 /* The other side's handle_write() will never actually get called, so
3316 * we need to invoke the appropriate callbacks ourself. */
3317 connection_t *linked = conn->linked_conn;
3319 if (n_read) {
3320 /* Probably a no-op, since linked conns typically don't count for
3321 * bandwidth rate limiting. But do it anyway so we can keep stats
3322 * accurately. Note that since we read the bytes from conn, and
3323 * we're writing the bytes onto the linked connection, we count
3324 * these as <i>written</i> bytes. */
3325 connection_buckets_decrement(linked, approx_time(), 0, n_read);
3327 if (connection_flushed_some(linked) < 0)
3328 connection_mark_for_close(linked);
3329 if (!connection_wants_to_flush(linked))
3330 connection_finished_flushing(linked);
3333 if (!buf_datalen(linked->outbuf) && conn->active_on_link)
3334 connection_stop_reading_from_linked_conn(conn);
3336 /* If we hit the EOF, call connection_reached_eof(). */
3337 if (!conn->marked_for_close &&
3338 conn->inbuf_reached_eof &&
3339 connection_reached_eof(conn) < 0) {
3340 return -1;
3342 return 0;
3345 /* DOCDOC connection_handle_read */
3347 connection_handle_read(connection_t *conn)
3349 int res;
3351 tor_gettimeofday_cache_clear();
3352 res = connection_handle_read_impl(conn);
3353 return res;
3356 /** Pull in new bytes from conn-\>s or conn-\>linked_conn onto conn-\>inbuf,
3357 * either directly or via TLS. Reduce the token buckets by the number of bytes
3358 * read.
3360 * If *max_to_read is -1, then decide it ourselves, else go with the
3361 * value passed to us. When returning, if it's changed, subtract the
3362 * number of bytes we read from *max_to_read.
3364 * Return -1 if we want to break conn, else return 0.
3366 static int
3367 connection_read_to_buf(connection_t *conn, ssize_t *max_to_read,
3368 int *socket_error)
3370 int result;
3371 ssize_t at_most = *max_to_read;
3372 size_t slack_in_buf, more_to_read;
3373 size_t n_read = 0, n_written = 0;
3375 if (at_most == -1) { /* we need to initialize it */
3376 /* how many bytes are we allowed to read? */
3377 at_most = connection_bucket_read_limit(conn, approx_time());
3380 slack_in_buf = buf_slack(conn->inbuf);
3381 again:
3382 if ((size_t)at_most > slack_in_buf && slack_in_buf >= 1024) {
3383 more_to_read = at_most - slack_in_buf;
3384 at_most = slack_in_buf;
3385 } else {
3386 more_to_read = 0;
3389 if (connection_speaks_cells(conn) &&
3390 conn->state > OR_CONN_STATE_PROXY_HANDSHAKING) {
3391 int pending;
3392 or_connection_t *or_conn = TO_OR_CONN(conn);
3393 size_t initial_size;
3394 if (conn->state == OR_CONN_STATE_TLS_HANDSHAKING ||
3395 conn->state == OR_CONN_STATE_TLS_CLIENT_RENEGOTIATING) {
3396 /* continue handshaking even if global token bucket is empty */
3397 return connection_tls_continue_handshake(or_conn);
3400 log_debug(LD_NET,
3401 "%d: starting, inbuf_datalen %ld (%d pending in tls object)."
3402 " at_most %ld.",
3403 (int)conn->s,(long)buf_datalen(conn->inbuf),
3404 tor_tls_get_pending_bytes(or_conn->tls), (long)at_most);
3406 initial_size = buf_datalen(conn->inbuf);
3407 /* else open, or closing */
3408 result = read_to_buf_tls(or_conn->tls, at_most, conn->inbuf);
3409 if (TOR_TLS_IS_ERROR(result) || result == TOR_TLS_CLOSE)
3410 or_conn->tls_error = result;
3411 else
3412 or_conn->tls_error = 0;
3414 switch (result) {
3415 case TOR_TLS_CLOSE:
3416 case TOR_TLS_ERROR_IO:
3417 log_debug(LD_NET,"TLS connection closed %son read. Closing. "
3418 "(Nickname %s, address %s)",
3419 result == TOR_TLS_CLOSE ? "cleanly " : "",
3420 or_conn->nickname ? or_conn->nickname : "not set",
3421 conn->address);
3422 return result;
3423 CASE_TOR_TLS_ERROR_ANY_NONIO:
3424 log_debug(LD_NET,"tls error [%s]. breaking (nickname %s, address %s).",
3425 tor_tls_err_to_string(result),
3426 or_conn->nickname ? or_conn->nickname : "not set",
3427 conn->address);
3428 return result;
3429 case TOR_TLS_WANTWRITE:
3430 connection_start_writing(conn);
3431 return 0;
3432 case TOR_TLS_WANTREAD:
3433 if (conn->in_connection_handle_write) {
3434 /* We've been invoked from connection_handle_write, because we're
3435 * waiting for a TLS renegotiation, the renegotiation started, and
3436 * SSL_read returned WANTWRITE. But now SSL_read is saying WANTREAD
3437 * again. Stop waiting for write events now, or else we'll
3438 * busy-loop until data arrives for us to read. */
3439 connection_stop_writing(conn);
3440 if (!connection_is_reading(conn))
3441 connection_start_reading(conn);
3443 /* we're already reading, one hopes */
3444 result = 0;
3445 break;
3446 case TOR_TLS_DONE: /* no data read, so nothing to process */
3447 result = 0;
3448 break; /* so we call bucket_decrement below */
3449 default:
3450 break;
3452 pending = tor_tls_get_pending_bytes(or_conn->tls);
3453 if (pending) {
3454 /* If we have any pending bytes, we read them now. This *can*
3455 * take us over our read allotment, but really we shouldn't be
3456 * believing that SSL bytes are the same as TCP bytes anyway. */
3457 int r2 = read_to_buf_tls(or_conn->tls, pending, conn->inbuf);
3458 if (BUG(r2<0)) {
3459 log_warn(LD_BUG, "apparently, reading pending bytes can fail.");
3460 return -1;
3463 result = (int)(buf_datalen(conn->inbuf)-initial_size);
3464 tor_tls_get_n_raw_bytes(or_conn->tls, &n_read, &n_written);
3465 log_debug(LD_GENERAL, "After TLS read of %d: %ld read, %ld written",
3466 result, (long)n_read, (long)n_written);
3467 } else if (conn->linked) {
3468 if (conn->linked_conn) {
3469 result = move_buf_to_buf(conn->inbuf, conn->linked_conn->outbuf,
3470 &conn->linked_conn->outbuf_flushlen);
3471 } else {
3472 result = 0;
3474 //log_notice(LD_GENERAL, "Moved %d bytes on an internal link!", result);
3475 /* If the other side has disappeared, or if it's been marked for close and
3476 * we flushed its outbuf, then we should set our inbuf_reached_eof. */
3477 if (!conn->linked_conn ||
3478 (conn->linked_conn->marked_for_close &&
3479 buf_datalen(conn->linked_conn->outbuf) == 0))
3480 conn->inbuf_reached_eof = 1;
3482 n_read = (size_t) result;
3483 } else {
3484 /* !connection_speaks_cells, !conn->linked_conn. */
3485 int reached_eof = 0;
3486 CONN_LOG_PROTECT(conn,
3487 result = read_to_buf(conn->s, at_most, conn->inbuf, &reached_eof,
3488 socket_error));
3489 if (reached_eof)
3490 conn->inbuf_reached_eof = 1;
3492 // log_fn(LOG_DEBUG,"read_to_buf returned %d.",read_result);
3494 if (result < 0)
3495 return -1;
3496 n_read = (size_t) result;
3499 if (n_read > 0) {
3500 /* change *max_to_read */
3501 *max_to_read = at_most - n_read;
3503 /* Update edge_conn->n_read and ocirc->n_read_circ_bw */
3504 if (conn->type == CONN_TYPE_AP) {
3505 edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
3506 circuit_t *circ = circuit_get_by_edge_conn(edge_conn);
3507 origin_circuit_t *ocirc;
3509 /* Check for overflow: */
3510 if (PREDICT_LIKELY(UINT32_MAX - edge_conn->n_read > n_read))
3511 edge_conn->n_read += (int)n_read;
3512 else
3513 edge_conn->n_read = UINT32_MAX;
3515 if (circ && CIRCUIT_IS_ORIGIN(circ)) {
3516 ocirc = TO_ORIGIN_CIRCUIT(circ);
3517 if (PREDICT_LIKELY(UINT32_MAX - ocirc->n_read_circ_bw > n_read))
3518 ocirc->n_read_circ_bw += (int)n_read;
3519 else
3520 ocirc->n_read_circ_bw = UINT32_MAX;
3524 /* If CONN_BW events are enabled, update conn->n_read_conn_bw for
3525 * OR/DIR/EXIT connections, checking for overflow. */
3526 if (get_options()->TestingEnableConnBwEvent &&
3527 (conn->type == CONN_TYPE_OR ||
3528 conn->type == CONN_TYPE_DIR ||
3529 conn->type == CONN_TYPE_EXIT)) {
3530 if (PREDICT_LIKELY(UINT32_MAX - conn->n_read_conn_bw > n_read))
3531 conn->n_read_conn_bw += (int)n_read;
3532 else
3533 conn->n_read_conn_bw = UINT32_MAX;
3537 connection_buckets_decrement(conn, approx_time(), n_read, n_written);
3539 if (more_to_read && result == at_most) {
3540 slack_in_buf = buf_slack(conn->inbuf);
3541 at_most = more_to_read;
3542 goto again;
3545 /* Call even if result is 0, since the global read bucket may
3546 * have reached 0 on a different conn, and this connection needs to
3547 * know to stop reading. */
3548 connection_consider_empty_read_buckets(conn);
3549 if (n_written > 0 && connection_is_writing(conn))
3550 connection_consider_empty_write_buckets(conn);
3552 return 0;
3555 /** A pass-through to fetch_from_buf. */
3557 connection_fetch_from_buf(char *string, size_t len, connection_t *conn)
3559 return fetch_from_buf(string, len, conn->inbuf);
3562 /** As fetch_from_buf_line(), but read from a connection's input buffer. */
3564 connection_fetch_from_buf_line(connection_t *conn, char *data,
3565 size_t *data_len)
3567 return fetch_from_buf_line(conn->inbuf, data, data_len);
3570 /** As fetch_from_buf_http, but fetches from a connection's input buffer_t as
3571 * appropriate. */
3573 connection_fetch_from_buf_http(connection_t *conn,
3574 char **headers_out, size_t max_headerlen,
3575 char **body_out, size_t *body_used,
3576 size_t max_bodylen, int force_complete)
3578 return fetch_from_buf_http(conn->inbuf, headers_out, max_headerlen,
3579 body_out, body_used, max_bodylen, force_complete);
3582 /** Return conn-\>outbuf_flushlen: how many bytes conn wants to flush
3583 * from its outbuf. */
3585 connection_wants_to_flush(connection_t *conn)
3587 return conn->outbuf_flushlen > 0;
3590 /** Are there too many bytes on edge connection <b>conn</b>'s outbuf to
3591 * send back a relay-level sendme yet? Return 1 if so, 0 if not. Used by
3592 * connection_edge_consider_sending_sendme().
3595 connection_outbuf_too_full(connection_t *conn)
3597 return (conn->outbuf_flushlen > 10*CELL_PAYLOAD_SIZE);
3600 /** Try to flush more bytes onto <b>conn</b>-\>s.
3602 * This function gets called either from conn_write_callback() in main.c
3603 * when libevent tells us that conn wants to write, or below
3604 * from connection_write_to_buf() when an entire TLS record is ready.
3606 * Update <b>conn</b>-\>timestamp_lastwritten to now, and call flush_buf
3607 * or flush_buf_tls appropriately. If it succeeds and there are no more
3608 * more bytes on <b>conn</b>-\>outbuf, then call connection_finished_flushing
3609 * on it too.
3611 * If <b>force</b>, then write as many bytes as possible, ignoring bandwidth
3612 * limits. (Used for flushing messages to controller connections on fatal
3613 * errors.)
3615 * Mark the connection and return -1 if you want to close it, else
3616 * return 0.
3618 static int
3619 connection_handle_write_impl(connection_t *conn, int force)
3621 int e;
3622 socklen_t len=(socklen_t)sizeof(e);
3623 int result;
3624 ssize_t max_to_write;
3625 time_t now = approx_time();
3626 size_t n_read = 0, n_written = 0;
3627 int dont_stop_writing = 0;
3629 tor_assert(!connection_is_listener(conn));
3631 if (conn->marked_for_close || !SOCKET_OK(conn->s))
3632 return 0; /* do nothing */
3634 if (conn->in_flushed_some) {
3635 log_warn(LD_BUG, "called recursively from inside conn->in_flushed_some");
3636 return 0;
3639 conn->timestamp_lastwritten = now;
3641 /* Sometimes, "writable" means "connected". */
3642 if (connection_state_is_connecting(conn)) {
3643 if (getsockopt(conn->s, SOL_SOCKET, SO_ERROR, (void*)&e, &len) < 0) {
3644 log_warn(LD_BUG, "getsockopt() syscall failed");
3645 if (conn->type == CONN_TYPE_OR) {
3646 or_connection_t *orconn = TO_OR_CONN(conn);
3647 connection_or_close_for_error(orconn, 0);
3648 } else {
3649 if (CONN_IS_EDGE(conn)) {
3650 connection_edge_end_errno(TO_EDGE_CONN(conn));
3652 connection_mark_for_close(conn);
3654 return -1;
3656 if (e) {
3657 /* some sort of error, but maybe just inprogress still */
3658 if (!ERRNO_IS_CONN_EINPROGRESS(e)) {
3659 log_info(LD_NET,"in-progress connect failed. Removing. (%s)",
3660 tor_socket_strerror(e));
3661 if (CONN_IS_EDGE(conn))
3662 connection_edge_end_errno(TO_EDGE_CONN(conn));
3663 if (conn->type == CONN_TYPE_OR)
3664 connection_or_notify_error(TO_OR_CONN(conn),
3665 errno_to_orconn_end_reason(e),
3666 tor_socket_strerror(e));
3668 connection_close_immediate(conn);
3670 * This can bypass normal channel checking since we did
3671 * connection_or_notify_error() above.
3673 connection_mark_for_close_internal(conn);
3674 return -1;
3675 } else {
3676 return 0; /* no change, see if next time is better */
3679 /* The connection is successful. */
3680 if (connection_finished_connecting(conn)<0)
3681 return -1;
3684 max_to_write = force ? (ssize_t)conn->outbuf_flushlen
3685 : connection_bucket_write_limit(conn, now);
3687 if (connection_speaks_cells(conn) &&
3688 conn->state > OR_CONN_STATE_PROXY_HANDSHAKING) {
3689 or_connection_t *or_conn = TO_OR_CONN(conn);
3690 size_t initial_size;
3691 if (conn->state == OR_CONN_STATE_TLS_HANDSHAKING ||
3692 conn->state == OR_CONN_STATE_TLS_CLIENT_RENEGOTIATING) {
3693 connection_stop_writing(conn);
3694 if (connection_tls_continue_handshake(or_conn) < 0) {
3695 /* Don't flush; connection is dead. */
3696 connection_or_notify_error(or_conn,
3697 END_OR_CONN_REASON_MISC,
3698 "TLS error in connection_tls_"
3699 "continue_handshake()");
3700 connection_close_immediate(conn);
3702 * This can bypass normal channel checking since we did
3703 * connection_or_notify_error() above.
3705 connection_mark_for_close_internal(conn);
3706 return -1;
3708 return 0;
3709 } else if (conn->state == OR_CONN_STATE_TLS_SERVER_RENEGOTIATING) {
3710 return connection_handle_read(conn);
3713 /* else open, or closing */
3714 initial_size = buf_datalen(conn->outbuf);
3715 result = flush_buf_tls(or_conn->tls, conn->outbuf,
3716 max_to_write, &conn->outbuf_flushlen);
3718 /* If we just flushed the last bytes, tell the channel on the
3719 * or_conn to check if it needs to geoip_change_dirreq_state() */
3720 /* XXXX move this to flushed_some or finished_flushing -NM */
3721 if (buf_datalen(conn->outbuf) == 0 && or_conn->chan)
3722 channel_notify_flushed(TLS_CHAN_TO_BASE(or_conn->chan));
3724 switch (result) {
3725 CASE_TOR_TLS_ERROR_ANY:
3726 case TOR_TLS_CLOSE:
3727 log_info(LD_NET, result != TOR_TLS_CLOSE ?
3728 "tls error. breaking.":"TLS connection closed on flush");
3729 /* Don't flush; connection is dead. */
3730 connection_or_notify_error(or_conn,
3731 END_OR_CONN_REASON_MISC,
3732 result != TOR_TLS_CLOSE ?
3733 "TLS error in during flush" :
3734 "TLS closed during flush");
3735 connection_close_immediate(conn);
3737 * This can bypass normal channel checking since we did
3738 * connection_or_notify_error() above.
3740 connection_mark_for_close_internal(conn);
3741 return -1;
3742 case TOR_TLS_WANTWRITE:
3743 log_debug(LD_NET,"wanted write.");
3744 /* we're already writing */
3745 dont_stop_writing = 1;
3746 break;
3747 case TOR_TLS_WANTREAD:
3748 /* Make sure to avoid a loop if the receive buckets are empty. */
3749 log_debug(LD_NET,"wanted read.");
3750 if (!connection_is_reading(conn)) {
3751 connection_stop_writing(conn);
3752 conn->write_blocked_on_bw = 1;
3753 /* we'll start reading again when we get more tokens in our
3754 * read bucket; then we'll start writing again too.
3757 /* else no problem, we're already reading */
3758 return 0;
3759 /* case TOR_TLS_DONE:
3760 * for TOR_TLS_DONE, fall through to check if the flushlen
3761 * is empty, so we can stop writing.
3765 tor_tls_get_n_raw_bytes(or_conn->tls, &n_read, &n_written);
3766 log_debug(LD_GENERAL, "After TLS write of %d: %ld read, %ld written",
3767 result, (long)n_read, (long)n_written);
3768 or_conn->bytes_xmitted += result;
3769 or_conn->bytes_xmitted_by_tls += n_written;
3770 /* So we notice bytes were written even on error */
3771 /* XXXX This cast is safe since we can never write INT_MAX bytes in a
3772 * single set of TLS operations. But it looks kinda ugly. If we refactor
3773 * the *_buf_tls functions, we should make them return ssize_t or size_t
3774 * or something. */
3775 result = (int)(initial_size-buf_datalen(conn->outbuf));
3776 } else {
3777 CONN_LOG_PROTECT(conn,
3778 result = flush_buf(conn->s, conn->outbuf,
3779 max_to_write, &conn->outbuf_flushlen));
3780 if (result < 0) {
3781 if (CONN_IS_EDGE(conn))
3782 connection_edge_end_errno(TO_EDGE_CONN(conn));
3783 if (conn->type == CONN_TYPE_AP) {
3784 /* writing failed; we couldn't send a SOCKS reply if we wanted to */
3785 TO_ENTRY_CONN(conn)->socks_request->has_finished = 1;
3788 connection_close_immediate(conn); /* Don't flush; connection is dead. */
3789 connection_mark_for_close(conn);
3790 return -1;
3792 n_written = (size_t) result;
3795 if (n_written && conn->type == CONN_TYPE_AP) {
3796 edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
3797 circuit_t *circ = circuit_get_by_edge_conn(edge_conn);
3798 origin_circuit_t *ocirc;
3800 /* Check for overflow: */
3801 if (PREDICT_LIKELY(UINT32_MAX - edge_conn->n_written > n_written))
3802 edge_conn->n_written += (int)n_written;
3803 else
3804 edge_conn->n_written = UINT32_MAX;
3806 if (circ && CIRCUIT_IS_ORIGIN(circ)) {
3807 ocirc = TO_ORIGIN_CIRCUIT(circ);
3808 if (PREDICT_LIKELY(UINT32_MAX - ocirc->n_written_circ_bw > n_written))
3809 ocirc->n_written_circ_bw += (int)n_written;
3810 else
3811 ocirc->n_written_circ_bw = UINT32_MAX;
3815 /* If CONN_BW events are enabled, update conn->n_written_conn_bw for
3816 * OR/DIR/EXIT connections, checking for overflow. */
3817 if (n_written && get_options()->TestingEnableConnBwEvent &&
3818 (conn->type == CONN_TYPE_OR ||
3819 conn->type == CONN_TYPE_DIR ||
3820 conn->type == CONN_TYPE_EXIT)) {
3821 if (PREDICT_LIKELY(UINT32_MAX - conn->n_written_conn_bw > n_written))
3822 conn->n_written_conn_bw += (int)n_written;
3823 else
3824 conn->n_written_conn_bw = UINT32_MAX;
3827 connection_buckets_decrement(conn, approx_time(), n_read, n_written);
3829 if (result > 0) {
3830 /* If we wrote any bytes from our buffer, then call the appropriate
3831 * functions. */
3832 if (connection_flushed_some(conn) < 0) {
3833 if (connection_speaks_cells(conn)) {
3834 connection_or_notify_error(TO_OR_CONN(conn),
3835 END_OR_CONN_REASON_MISC,
3836 "Got error back from "
3837 "connection_flushed_some()");
3841 * This can bypass normal channel checking since we did
3842 * connection_or_notify_error() above.
3844 connection_mark_for_close_internal(conn);
3848 if (!connection_wants_to_flush(conn) &&
3849 !dont_stop_writing) { /* it's done flushing */
3850 if (connection_finished_flushing(conn) < 0) {
3851 /* already marked */
3852 return -1;
3854 return 0;
3857 /* Call even if result is 0, since the global write bucket may
3858 * have reached 0 on a different conn, and this connection needs to
3859 * know to stop writing. */
3860 connection_consider_empty_write_buckets(conn);
3861 if (n_read > 0 && connection_is_reading(conn))
3862 connection_consider_empty_read_buckets(conn);
3864 return 0;
3867 /* DOCDOC connection_handle_write */
3869 connection_handle_write(connection_t *conn, int force)
3871 int res;
3872 tor_gettimeofday_cache_clear();
3873 conn->in_connection_handle_write = 1;
3874 res = connection_handle_write_impl(conn, force);
3875 conn->in_connection_handle_write = 0;
3876 return res;
3880 * Try to flush data that's waiting for a write on <b>conn</b>. Return
3881 * -1 on failure, 0 on success.
3883 * Don't use this function for regular writing; the buffers
3884 * system should be good enough at scheduling writes there. Instead, this
3885 * function is for cases when we're about to exit or something and we want
3886 * to report it right away.
3889 connection_flush(connection_t *conn)
3891 return connection_handle_write(conn, 1);
3894 /** Append <b>len</b> bytes of <b>string</b> onto <b>conn</b>'s
3895 * outbuf, and ask it to start writing.
3897 * If <b>zlib</b> is nonzero, this is a directory connection that should get
3898 * its contents compressed or decompressed as they're written. If zlib is
3899 * negative, this is the last data to be compressed, and the connection's zlib
3900 * state should be flushed.
3902 * If it's a local control connection and a 64k chunk is ready, try to flush
3903 * it all, so we don't end up with many megabytes of controller info queued at
3904 * once.
3906 MOCK_IMPL(void,
3907 connection_write_to_buf_impl_,(const char *string, size_t len,
3908 connection_t *conn, int zlib))
3910 /* XXXX This function really needs to return -1 on failure. */
3911 int r;
3912 size_t old_datalen;
3913 if (!len && !(zlib<0))
3914 return;
3915 /* if it's marked for close, only allow write if we mean to flush it */
3916 if (conn->marked_for_close && !conn->hold_open_until_flushed)
3917 return;
3919 old_datalen = buf_datalen(conn->outbuf);
3920 if (zlib) {
3921 dir_connection_t *dir_conn = TO_DIR_CONN(conn);
3922 int done = zlib < 0;
3923 CONN_LOG_PROTECT(conn, r = write_to_buf_zlib(conn->outbuf,
3924 dir_conn->zlib_state,
3925 string, len, done));
3926 } else {
3927 CONN_LOG_PROTECT(conn, r = write_to_buf(string, len, conn->outbuf));
3929 if (r < 0) {
3930 if (CONN_IS_EDGE(conn)) {
3931 /* if it failed, it means we have our package/delivery windows set
3932 wrong compared to our max outbuf size. close the whole circuit. */
3933 log_warn(LD_NET,
3934 "write_to_buf failed. Closing circuit (fd %d).", (int)conn->s);
3935 circuit_mark_for_close(circuit_get_by_edge_conn(TO_EDGE_CONN(conn)),
3936 END_CIRC_REASON_INTERNAL);
3937 } else if (conn->type == CONN_TYPE_OR) {
3938 or_connection_t *orconn = TO_OR_CONN(conn);
3939 log_warn(LD_NET,
3940 "write_to_buf failed on an orconn; notifying of error "
3941 "(fd %d)", (int)(conn->s));
3942 connection_or_close_for_error(orconn, 0);
3943 } else {
3944 log_warn(LD_NET,
3945 "write_to_buf failed. Closing connection (fd %d).",
3946 (int)conn->s);
3947 connection_mark_for_close(conn);
3949 return;
3952 /* If we receive optimistic data in the EXIT_CONN_STATE_RESOLVING
3953 * state, we don't want to try to write it right away, since
3954 * conn->write_event won't be set yet. Otherwise, write data from
3955 * this conn as the socket is available. */
3956 if (conn->write_event) {
3957 connection_start_writing(conn);
3959 if (zlib) {
3960 conn->outbuf_flushlen += buf_datalen(conn->outbuf) - old_datalen;
3961 } else {
3962 conn->outbuf_flushlen += len;
3966 /** Return a connection_t * from get_connection_array() that satisfies test on
3967 * var, and that is not marked for close. */
3968 #define CONN_GET_TEMPLATE(var, test) \
3969 STMT_BEGIN \
3970 smartlist_t *conns = get_connection_array(); \
3971 SMARTLIST_FOREACH(conns, connection_t *, var, \
3973 if (var && (test) && !var->marked_for_close) \
3974 return var; \
3975 }); \
3976 return NULL; \
3977 STMT_END
3979 /** Return a connection with given type, address, port, and purpose;
3980 * or NULL if no such connection exists (or if all such connections are marked
3981 * for close). */
3982 MOCK_IMPL(connection_t *,
3983 connection_get_by_type_addr_port_purpose,(int type,
3984 const tor_addr_t *addr, uint16_t port,
3985 int purpose))
3987 CONN_GET_TEMPLATE(conn,
3988 (conn->type == type &&
3989 tor_addr_eq(&conn->addr, addr) &&
3990 conn->port == port &&
3991 conn->purpose == purpose));
3994 /** Return the stream with id <b>id</b> if it is not already marked for
3995 * close.
3997 connection_t *
3998 connection_get_by_global_id(uint64_t id)
4000 CONN_GET_TEMPLATE(conn, conn->global_identifier == id);
4003 /** Return a connection of type <b>type</b> that is not marked for close.
4005 connection_t *
4006 connection_get_by_type(int type)
4008 CONN_GET_TEMPLATE(conn, conn->type == type);
4011 /** Return a connection of type <b>type</b> that is in state <b>state</b>,
4012 * and that is not marked for close.
4014 connection_t *
4015 connection_get_by_type_state(int type, int state)
4017 CONN_GET_TEMPLATE(conn, conn->type == type && conn->state == state);
4020 /** Return a connection of type <b>type</b> that has rendquery equal
4021 * to <b>rendquery</b>, and that is not marked for close. If state
4022 * is non-zero, conn must be of that state too.
4024 connection_t *
4025 connection_get_by_type_state_rendquery(int type, int state,
4026 const char *rendquery)
4028 tor_assert(type == CONN_TYPE_DIR ||
4029 type == CONN_TYPE_AP || type == CONN_TYPE_EXIT);
4030 tor_assert(rendquery);
4032 CONN_GET_TEMPLATE(conn,
4033 (conn->type == type &&
4034 (!state || state == conn->state)) &&
4036 (type == CONN_TYPE_DIR &&
4037 TO_DIR_CONN(conn)->rend_data &&
4038 !rend_cmp_service_ids(rendquery,
4039 TO_DIR_CONN(conn)->rend_data->onion_address))
4041 (CONN_IS_EDGE(conn) &&
4042 TO_EDGE_CONN(conn)->rend_data &&
4043 !rend_cmp_service_ids(rendquery,
4044 TO_EDGE_CONN(conn)->rend_data->onion_address))
4048 /** Return a new smartlist of dir_connection_t * from get_connection_array()
4049 * that satisfy conn_test on connection_t *conn_var, and dirconn_test on
4050 * dir_connection_t *dirconn_var. conn_var must be of CONN_TYPE_DIR and not
4051 * marked for close to be included in the list. */
4052 #define DIR_CONN_LIST_TEMPLATE(conn_var, conn_test, \
4053 dirconn_var, dirconn_test) \
4054 STMT_BEGIN \
4055 smartlist_t *conns = get_connection_array(); \
4056 smartlist_t *dir_conns = smartlist_new(); \
4057 SMARTLIST_FOREACH_BEGIN(conns, connection_t *, conn_var) { \
4058 if (conn_var && (conn_test) \
4059 && conn_var->type == CONN_TYPE_DIR \
4060 && !conn_var->marked_for_close) { \
4061 dir_connection_t *dirconn_var = TO_DIR_CONN(conn_var); \
4062 if (dirconn_var && (dirconn_test)) { \
4063 smartlist_add(dir_conns, dirconn_var); \
4066 } SMARTLIST_FOREACH_END(conn_var); \
4067 return dir_conns; \
4068 STMT_END
4070 /** Return a list of directory connections that are fetching the item
4071 * described by <b>purpose</b>/<b>resource</b>. If there are none,
4072 * return an empty list. This list must be freed using smartlist_free,
4073 * but the pointers in it must not be freed.
4074 * Note that this list should not be cached, as the pointers in it can be
4075 * freed if their connections close. */
4076 smartlist_t *
4077 connection_dir_list_by_purpose_and_resource(
4078 int purpose,
4079 const char *resource)
4081 DIR_CONN_LIST_TEMPLATE(conn,
4082 conn->purpose == purpose,
4083 dirconn,
4084 0 == strcmp_opt(resource,
4085 dirconn->requested_resource));
4088 /** Return a list of directory connections that are fetching the item
4089 * described by <b>purpose</b>/<b>resource</b>/<b>state</b>. If there are
4090 * none, return an empty list. This list must be freed using smartlist_free,
4091 * but the pointers in it must not be freed.
4092 * Note that this list should not be cached, as the pointers in it can be
4093 * freed if their connections close. */
4094 smartlist_t *
4095 connection_dir_list_by_purpose_resource_and_state(
4096 int purpose,
4097 const char *resource,
4098 int state)
4100 DIR_CONN_LIST_TEMPLATE(conn,
4101 conn->purpose == purpose && conn->state == state,
4102 dirconn,
4103 0 == strcmp_opt(resource,
4104 dirconn->requested_resource));
4107 #undef DIR_CONN_LIST_TEMPLATE
4109 /** Return an arbitrary active OR connection that isn't <b>this_conn</b>.
4111 * We use this to guess if we should tell the controller that we
4112 * didn't manage to connect to any of our bridges. */
4113 static connection_t *
4114 connection_get_another_active_or_conn(const or_connection_t *this_conn)
4116 CONN_GET_TEMPLATE(conn,
4117 conn != TO_CONN(this_conn) && conn->type == CONN_TYPE_OR);
4120 /** Return 1 if there are any active OR connections apart from
4121 * <b>this_conn</b>.
4123 * We use this to guess if we should tell the controller that we
4124 * didn't manage to connect to any of our bridges. */
4126 any_other_active_or_conns(const or_connection_t *this_conn)
4128 connection_t *conn = connection_get_another_active_or_conn(this_conn);
4129 if (conn != NULL) {
4130 log_debug(LD_DIR, "%s: Found an OR connection: %s",
4131 __func__, conn->address);
4132 return 1;
4135 return 0;
4138 #undef CONN_GET_TEMPLATE
4140 /** Return 1 if <b>conn</b> is a listener conn, else return 0. */
4142 connection_is_listener(connection_t *conn)
4144 if (conn->type == CONN_TYPE_OR_LISTENER ||
4145 conn->type == CONN_TYPE_EXT_OR_LISTENER ||
4146 conn->type == CONN_TYPE_AP_LISTENER ||
4147 conn->type == CONN_TYPE_AP_TRANS_LISTENER ||
4148 conn->type == CONN_TYPE_AP_DNS_LISTENER ||
4149 conn->type == CONN_TYPE_AP_NATD_LISTENER ||
4150 conn->type == CONN_TYPE_DIR_LISTENER ||
4151 conn->type == CONN_TYPE_CONTROL_LISTENER)
4152 return 1;
4153 return 0;
4156 /** Return 1 if <b>conn</b> is in state "open" and is not marked
4157 * for close, else return 0.
4160 connection_state_is_open(connection_t *conn)
4162 tor_assert(conn);
4164 if (conn->marked_for_close)
4165 return 0;
4167 if ((conn->type == CONN_TYPE_OR && conn->state == OR_CONN_STATE_OPEN) ||
4168 (conn->type == CONN_TYPE_EXT_OR) ||
4169 (conn->type == CONN_TYPE_AP && conn->state == AP_CONN_STATE_OPEN) ||
4170 (conn->type == CONN_TYPE_EXIT && conn->state == EXIT_CONN_STATE_OPEN) ||
4171 (conn->type == CONN_TYPE_CONTROL &&
4172 conn->state == CONTROL_CONN_STATE_OPEN))
4173 return 1;
4175 return 0;
4178 /** Return 1 if conn is in 'connecting' state, else return 0. */
4180 connection_state_is_connecting(connection_t *conn)
4182 tor_assert(conn);
4184 if (conn->marked_for_close)
4185 return 0;
4186 switch (conn->type)
4188 case CONN_TYPE_OR:
4189 return conn->state == OR_CONN_STATE_CONNECTING;
4190 case CONN_TYPE_EXIT:
4191 return conn->state == EXIT_CONN_STATE_CONNECTING;
4192 case CONN_TYPE_DIR:
4193 return conn->state == DIR_CONN_STATE_CONNECTING;
4196 return 0;
4199 /** Allocates a base64'ed authenticator for use in http or https
4200 * auth, based on the input string <b>authenticator</b>. Returns it
4201 * if success, else returns NULL. */
4202 char *
4203 alloc_http_authenticator(const char *authenticator)
4205 /* an authenticator in Basic authentication
4206 * is just the string "username:password" */
4207 const size_t authenticator_length = strlen(authenticator);
4208 const size_t base64_authenticator_length =
4209 base64_encode_size(authenticator_length, 0) + 1;
4210 char *base64_authenticator = tor_malloc(base64_authenticator_length);
4211 if (base64_encode(base64_authenticator, base64_authenticator_length,
4212 authenticator, authenticator_length, 0) < 0) {
4213 tor_free(base64_authenticator); /* free and set to null */
4215 return base64_authenticator;
4218 /** Given a socket handle, check whether the local address (sockname) of the
4219 * socket is one that we've connected from before. If so, double-check
4220 * whether our address has changed and we need to generate keys. If we do,
4221 * call init_keys().
4223 static void
4224 client_check_address_changed(tor_socket_t sock)
4226 struct sockaddr_storage out_sockaddr;
4227 socklen_t out_addr_len = (socklen_t) sizeof(out_sockaddr);
4228 tor_addr_t out_addr, iface_addr;
4229 tor_addr_t **last_interface_ip_ptr;
4230 sa_family_t family;
4232 if (!outgoing_addrs)
4233 outgoing_addrs = smartlist_new();
4235 if (getsockname(sock, (struct sockaddr*)&out_sockaddr, &out_addr_len)<0) {
4236 int e = tor_socket_errno(sock);
4237 log_warn(LD_NET, "getsockname() to check for address change failed: %s",
4238 tor_socket_strerror(e));
4239 return;
4241 tor_addr_from_sockaddr(&out_addr, (struct sockaddr*)&out_sockaddr, NULL);
4242 family = tor_addr_family(&out_addr);
4244 if (family == AF_INET)
4245 last_interface_ip_ptr = &last_interface_ipv4;
4246 else if (family == AF_INET6)
4247 last_interface_ip_ptr = &last_interface_ipv6;
4248 else
4249 return;
4251 if (! *last_interface_ip_ptr) {
4252 tor_addr_t *a = tor_malloc_zero(sizeof(tor_addr_t));
4253 if (get_interface_address6(LOG_INFO, family, a)==0) {
4254 *last_interface_ip_ptr = a;
4255 } else {
4256 tor_free(a);
4260 /* If we've used this address previously, we're okay. */
4261 SMARTLIST_FOREACH(outgoing_addrs, const tor_addr_t *, a_ptr,
4262 if (tor_addr_eq(a_ptr, &out_addr))
4263 return;
4266 /* Uh-oh. We haven't connected from this address before. Has the interface
4267 * address changed? */
4268 if (get_interface_address6(LOG_INFO, family, &iface_addr)<0)
4269 return;
4271 if (tor_addr_eq(&iface_addr, *last_interface_ip_ptr)) {
4272 /* Nope, it hasn't changed. Add this address to the list. */
4273 smartlist_add(outgoing_addrs, tor_memdup(&out_addr, sizeof(tor_addr_t)));
4274 } else {
4275 /* The interface changed. We're a client, so we need to regenerate our
4276 * keys. First, reset the state. */
4277 log_notice(LD_NET, "Our IP address has changed. Rotating keys...");
4278 tor_addr_copy(*last_interface_ip_ptr, &iface_addr);
4279 SMARTLIST_FOREACH(outgoing_addrs, tor_addr_t*, a_ptr, tor_free(a_ptr));
4280 smartlist_clear(outgoing_addrs);
4281 smartlist_add(outgoing_addrs, tor_memdup(&out_addr, sizeof(tor_addr_t)));
4282 /* We'll need to resolve ourselves again. */
4283 reset_last_resolved_addr();
4284 /* Okay, now change our keys. */
4285 ip_address_changed(1);
4289 /** Some systems have limited system buffers for recv and xmit on
4290 * sockets allocated in a virtual server or similar environment. For a Tor
4291 * server this can produce the "Error creating network socket: No buffer
4292 * space available" error once all available TCP buffer space is consumed.
4293 * This method will attempt to constrain the buffers allocated for the socket
4294 * to the desired size to stay below system TCP buffer limits.
4296 static void
4297 set_constrained_socket_buffers(tor_socket_t sock, int size)
4299 void *sz = (void*)&size;
4300 socklen_t sz_sz = (socklen_t) sizeof(size);
4301 if (setsockopt(sock, SOL_SOCKET, SO_SNDBUF, sz, sz_sz) < 0) {
4302 int e = tor_socket_errno(sock);
4303 log_warn(LD_NET, "setsockopt() to constrain send "
4304 "buffer to %d bytes failed: %s", size, tor_socket_strerror(e));
4306 if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF, sz, sz_sz) < 0) {
4307 int e = tor_socket_errno(sock);
4308 log_warn(LD_NET, "setsockopt() to constrain recv "
4309 "buffer to %d bytes failed: %s", size, tor_socket_strerror(e));
4313 /** Process new bytes that have arrived on conn-\>inbuf.
4315 * This function just passes conn to the connection-specific
4316 * connection_*_process_inbuf() function. It also passes in
4317 * package_partial if wanted.
4319 static int
4320 connection_process_inbuf(connection_t *conn, int package_partial)
4322 tor_assert(conn);
4324 switch (conn->type) {
4325 case CONN_TYPE_OR:
4326 return connection_or_process_inbuf(TO_OR_CONN(conn));
4327 case CONN_TYPE_EXT_OR:
4328 return connection_ext_or_process_inbuf(TO_OR_CONN(conn));
4329 case CONN_TYPE_EXIT:
4330 case CONN_TYPE_AP:
4331 return connection_edge_process_inbuf(TO_EDGE_CONN(conn),
4332 package_partial);
4333 case CONN_TYPE_DIR:
4334 return connection_dir_process_inbuf(TO_DIR_CONN(conn));
4335 case CONN_TYPE_CONTROL:
4336 return connection_control_process_inbuf(TO_CONTROL_CONN(conn));
4337 default:
4338 log_err(LD_BUG,"got unexpected conn type %d.", conn->type);
4339 tor_fragile_assert();
4340 return -1;
4344 /** Called whenever we've written data on a connection. */
4345 static int
4346 connection_flushed_some(connection_t *conn)
4348 int r = 0;
4349 tor_assert(!conn->in_flushed_some);
4350 conn->in_flushed_some = 1;
4351 if (conn->type == CONN_TYPE_DIR &&
4352 conn->state == DIR_CONN_STATE_SERVER_WRITING) {
4353 r = connection_dirserv_flushed_some(TO_DIR_CONN(conn));
4354 } else if (conn->type == CONN_TYPE_OR) {
4355 r = connection_or_flushed_some(TO_OR_CONN(conn));
4356 } else if (CONN_IS_EDGE(conn)) {
4357 r = connection_edge_flushed_some(TO_EDGE_CONN(conn));
4359 conn->in_flushed_some = 0;
4360 return r;
4363 /** We just finished flushing bytes to the appropriately low network layer,
4364 * and there are no more bytes remaining in conn-\>outbuf or
4365 * conn-\>tls to be flushed.
4367 * This function just passes conn to the connection-specific
4368 * connection_*_finished_flushing() function.
4370 static int
4371 connection_finished_flushing(connection_t *conn)
4373 tor_assert(conn);
4375 /* If the connection is closed, don't try to do anything more here. */
4376 if (CONN_IS_CLOSED(conn))
4377 return 0;
4379 // log_fn(LOG_DEBUG,"entered. Socket %u.", conn->s);
4381 connection_stop_writing(conn);
4383 switch (conn->type) {
4384 case CONN_TYPE_OR:
4385 return connection_or_finished_flushing(TO_OR_CONN(conn));
4386 case CONN_TYPE_EXT_OR:
4387 return connection_ext_or_finished_flushing(TO_OR_CONN(conn));
4388 case CONN_TYPE_AP:
4389 case CONN_TYPE_EXIT:
4390 return connection_edge_finished_flushing(TO_EDGE_CONN(conn));
4391 case CONN_TYPE_DIR:
4392 return connection_dir_finished_flushing(TO_DIR_CONN(conn));
4393 case CONN_TYPE_CONTROL:
4394 return connection_control_finished_flushing(TO_CONTROL_CONN(conn));
4395 default:
4396 log_err(LD_BUG,"got unexpected conn type %d.", conn->type);
4397 tor_fragile_assert();
4398 return -1;
4402 /** Called when our attempt to connect() to another server has just
4403 * succeeded.
4405 * This function just passes conn to the connection-specific
4406 * connection_*_finished_connecting() function.
4408 static int
4409 connection_finished_connecting(connection_t *conn)
4411 tor_assert(conn);
4413 if (!server_mode(get_options())) {
4414 /* See whether getsockname() says our address changed. We need to do this
4415 * now that the connection has finished, because getsockname() on Windows
4416 * won't work until then. */
4417 client_check_address_changed(conn->s);
4420 switch (conn->type)
4422 case CONN_TYPE_OR:
4423 return connection_or_finished_connecting(TO_OR_CONN(conn));
4424 case CONN_TYPE_EXIT:
4425 return connection_edge_finished_connecting(TO_EDGE_CONN(conn));
4426 case CONN_TYPE_DIR:
4427 return connection_dir_finished_connecting(TO_DIR_CONN(conn));
4428 default:
4429 log_err(LD_BUG,"got unexpected conn type %d.", conn->type);
4430 tor_fragile_assert();
4431 return -1;
4435 /** Callback: invoked when a connection reaches an EOF event. */
4436 static int
4437 connection_reached_eof(connection_t *conn)
4439 switch (conn->type) {
4440 case CONN_TYPE_OR:
4441 case CONN_TYPE_EXT_OR:
4442 return connection_or_reached_eof(TO_OR_CONN(conn));
4443 case CONN_TYPE_AP:
4444 case CONN_TYPE_EXIT:
4445 return connection_edge_reached_eof(TO_EDGE_CONN(conn));
4446 case CONN_TYPE_DIR:
4447 return connection_dir_reached_eof(TO_DIR_CONN(conn));
4448 case CONN_TYPE_CONTROL:
4449 return connection_control_reached_eof(TO_CONTROL_CONN(conn));
4450 default:
4451 log_err(LD_BUG,"got unexpected conn type %d.", conn->type);
4452 tor_fragile_assert();
4453 return -1;
4457 /** Log how many bytes are used by buffers of different kinds and sizes. */
4458 void
4459 connection_dump_buffer_mem_stats(int severity)
4461 uint64_t used_by_type[CONN_TYPE_MAX_+1];
4462 uint64_t alloc_by_type[CONN_TYPE_MAX_+1];
4463 int n_conns_by_type[CONN_TYPE_MAX_+1];
4464 uint64_t total_alloc = 0;
4465 uint64_t total_used = 0;
4466 int i;
4467 smartlist_t *conns = get_connection_array();
4469 memset(used_by_type, 0, sizeof(used_by_type));
4470 memset(alloc_by_type, 0, sizeof(alloc_by_type));
4471 memset(n_conns_by_type, 0, sizeof(n_conns_by_type));
4473 SMARTLIST_FOREACH_BEGIN(conns, connection_t *, c) {
4474 int tp = c->type;
4475 ++n_conns_by_type[tp];
4476 if (c->inbuf) {
4477 used_by_type[tp] += buf_datalen(c->inbuf);
4478 alloc_by_type[tp] += buf_allocation(c->inbuf);
4480 if (c->outbuf) {
4481 used_by_type[tp] += buf_datalen(c->outbuf);
4482 alloc_by_type[tp] += buf_allocation(c->outbuf);
4484 } SMARTLIST_FOREACH_END(c);
4485 for (i=0; i <= CONN_TYPE_MAX_; ++i) {
4486 total_used += used_by_type[i];
4487 total_alloc += alloc_by_type[i];
4490 tor_log(severity, LD_GENERAL,
4491 "In buffers for %d connections: "U64_FORMAT" used/"U64_FORMAT" allocated",
4492 smartlist_len(conns),
4493 U64_PRINTF_ARG(total_used), U64_PRINTF_ARG(total_alloc));
4494 for (i=CONN_TYPE_MIN_; i <= CONN_TYPE_MAX_; ++i) {
4495 if (!n_conns_by_type[i])
4496 continue;
4497 tor_log(severity, LD_GENERAL,
4498 " For %d %s connections: "U64_FORMAT" used/"U64_FORMAT" allocated",
4499 n_conns_by_type[i], conn_type_to_string(i),
4500 U64_PRINTF_ARG(used_by_type[i]), U64_PRINTF_ARG(alloc_by_type[i]));
4504 /** Verify that connection <b>conn</b> has all of its invariants
4505 * correct. Trigger an assert if anything is invalid.
4507 void
4508 assert_connection_ok(connection_t *conn, time_t now)
4510 (void) now; /* XXXX unused. */
4511 tor_assert(conn);
4512 tor_assert(conn->type >= CONN_TYPE_MIN_);
4513 tor_assert(conn->type <= CONN_TYPE_MAX_);
4515 switch (conn->type) {
4516 case CONN_TYPE_OR:
4517 case CONN_TYPE_EXT_OR:
4518 tor_assert(conn->magic == OR_CONNECTION_MAGIC);
4519 break;
4520 case CONN_TYPE_AP:
4521 tor_assert(conn->magic == ENTRY_CONNECTION_MAGIC);
4522 break;
4523 case CONN_TYPE_EXIT:
4524 tor_assert(conn->magic == EDGE_CONNECTION_MAGIC);
4525 break;
4526 case CONN_TYPE_DIR:
4527 tor_assert(conn->magic == DIR_CONNECTION_MAGIC);
4528 break;
4529 case CONN_TYPE_CONTROL:
4530 tor_assert(conn->magic == CONTROL_CONNECTION_MAGIC);
4531 break;
4532 CASE_ANY_LISTENER_TYPE:
4533 tor_assert(conn->magic == LISTENER_CONNECTION_MAGIC);
4534 break;
4535 default:
4536 tor_assert(conn->magic == BASE_CONNECTION_MAGIC);
4537 break;
4540 if (conn->linked_conn) {
4541 tor_assert(conn->linked_conn->linked_conn == conn);
4542 tor_assert(conn->linked);
4544 if (conn->linked)
4545 tor_assert(!SOCKET_OK(conn->s));
4547 if (conn->outbuf_flushlen > 0) {
4548 /* With optimistic data, we may have queued data in
4549 * EXIT_CONN_STATE_RESOLVING while the conn is not yet marked to writing.
4550 * */
4551 tor_assert((conn->type == CONN_TYPE_EXIT &&
4552 conn->state == EXIT_CONN_STATE_RESOLVING) ||
4553 connection_is_writing(conn) ||
4554 conn->write_blocked_on_bw ||
4555 (CONN_IS_EDGE(conn) &&
4556 TO_EDGE_CONN(conn)->edge_blocked_on_circ));
4559 if (conn->hold_open_until_flushed)
4560 tor_assert(conn->marked_for_close);
4562 /* XXXX check: read_blocked_on_bw, write_blocked_on_bw, s, conn_array_index,
4563 * marked_for_close. */
4565 /* buffers */
4566 if (conn->inbuf)
4567 assert_buf_ok(conn->inbuf);
4568 if (conn->outbuf)
4569 assert_buf_ok(conn->outbuf);
4571 if (conn->type == CONN_TYPE_OR) {
4572 or_connection_t *or_conn = TO_OR_CONN(conn);
4573 if (conn->state == OR_CONN_STATE_OPEN) {
4574 /* tor_assert(conn->bandwidth > 0); */
4575 /* the above isn't necessarily true: if we just did a TLS
4576 * handshake but we didn't recognize the other peer, or it
4577 * gave a bad cert/etc, then we won't have assigned bandwidth,
4578 * yet it will be open. -RD
4580 // tor_assert(conn->read_bucket >= 0);
4582 // tor_assert(conn->addr && conn->port);
4583 tor_assert(conn->address);
4584 if (conn->state > OR_CONN_STATE_PROXY_HANDSHAKING)
4585 tor_assert(or_conn->tls);
4588 if (CONN_IS_EDGE(conn)) {
4589 /* XXX unchecked: package window, deliver window. */
4590 if (conn->type == CONN_TYPE_AP) {
4591 entry_connection_t *entry_conn = TO_ENTRY_CONN(conn);
4592 if (entry_conn->chosen_exit_optional || entry_conn->chosen_exit_retries)
4593 tor_assert(entry_conn->chosen_exit_name);
4595 tor_assert(entry_conn->socks_request);
4596 if (conn->state == AP_CONN_STATE_OPEN) {
4597 tor_assert(entry_conn->socks_request->has_finished);
4598 if (!conn->marked_for_close) {
4599 tor_assert(ENTRY_TO_EDGE_CONN(entry_conn)->cpath_layer);
4600 assert_cpath_layer_ok(ENTRY_TO_EDGE_CONN(entry_conn)->cpath_layer);
4604 if (conn->type == CONN_TYPE_EXIT) {
4605 tor_assert(conn->purpose == EXIT_PURPOSE_CONNECT ||
4606 conn->purpose == EXIT_PURPOSE_RESOLVE);
4608 } else if (conn->type == CONN_TYPE_DIR) {
4609 } else {
4610 /* Purpose is only used for dir and exit types currently */
4611 tor_assert(!conn->purpose);
4614 switch (conn->type)
4616 CASE_ANY_LISTENER_TYPE:
4617 tor_assert(conn->state == LISTENER_STATE_READY);
4618 break;
4619 case CONN_TYPE_OR:
4620 tor_assert(conn->state >= OR_CONN_STATE_MIN_);
4621 tor_assert(conn->state <= OR_CONN_STATE_MAX_);
4622 break;
4623 case CONN_TYPE_EXT_OR:
4624 tor_assert(conn->state >= EXT_OR_CONN_STATE_MIN_);
4625 tor_assert(conn->state <= EXT_OR_CONN_STATE_MAX_);
4626 break;
4627 case CONN_TYPE_EXIT:
4628 tor_assert(conn->state >= EXIT_CONN_STATE_MIN_);
4629 tor_assert(conn->state <= EXIT_CONN_STATE_MAX_);
4630 tor_assert(conn->purpose >= EXIT_PURPOSE_MIN_);
4631 tor_assert(conn->purpose <= EXIT_PURPOSE_MAX_);
4632 break;
4633 case CONN_TYPE_AP:
4634 tor_assert(conn->state >= AP_CONN_STATE_MIN_);
4635 tor_assert(conn->state <= AP_CONN_STATE_MAX_);
4636 tor_assert(TO_ENTRY_CONN(conn)->socks_request);
4637 break;
4638 case CONN_TYPE_DIR:
4639 tor_assert(conn->state >= DIR_CONN_STATE_MIN_);
4640 tor_assert(conn->state <= DIR_CONN_STATE_MAX_);
4641 tor_assert(conn->purpose >= DIR_PURPOSE_MIN_);
4642 tor_assert(conn->purpose <= DIR_PURPOSE_MAX_);
4643 break;
4644 case CONN_TYPE_CONTROL:
4645 tor_assert(conn->state >= CONTROL_CONN_STATE_MIN_);
4646 tor_assert(conn->state <= CONTROL_CONN_STATE_MAX_);
4647 break;
4648 default:
4649 tor_assert(0);
4653 /** Fills <b>addr</b> and <b>port</b> with the details of the global
4654 * proxy server we are using.
4655 * <b>conn</b> contains the connection we are using the proxy for.
4657 * Return 0 on success, -1 on failure.
4660 get_proxy_addrport(tor_addr_t *addr, uint16_t *port, int *proxy_type,
4661 const connection_t *conn)
4663 const or_options_t *options = get_options();
4665 /* Client Transport Plugins can use another proxy, but that should be hidden
4666 * from the rest of tor (as the plugin is responsible for dealing with the
4667 * proxy), check it first, then check the rest of the proxy types to allow
4668 * the config to have unused ClientTransportPlugin entries.
4670 if (options->ClientTransportPlugin) {
4671 const transport_t *transport = NULL;
4672 int r;
4673 r = get_transport_by_bridge_addrport(&conn->addr, conn->port, &transport);
4674 if (r<0)
4675 return -1;
4676 if (transport) { /* transport found */
4677 tor_addr_copy(addr, &transport->addr);
4678 *port = transport->port;
4679 *proxy_type = transport->socks_version;
4680 return 0;
4683 /* Unused ClientTransportPlugin. */
4686 if (options->HTTPSProxy) {
4687 tor_addr_copy(addr, &options->HTTPSProxyAddr);
4688 *port = options->HTTPSProxyPort;
4689 *proxy_type = PROXY_CONNECT;
4690 return 0;
4691 } else if (options->Socks4Proxy) {
4692 tor_addr_copy(addr, &options->Socks4ProxyAddr);
4693 *port = options->Socks4ProxyPort;
4694 *proxy_type = PROXY_SOCKS4;
4695 return 0;
4696 } else if (options->Socks5Proxy) {
4697 tor_addr_copy(addr, &options->Socks5ProxyAddr);
4698 *port = options->Socks5ProxyPort;
4699 *proxy_type = PROXY_SOCKS5;
4700 return 0;
4703 tor_addr_make_unspec(addr);
4704 *port = 0;
4705 *proxy_type = PROXY_NONE;
4706 return 0;
4709 /** Log a failed connection to a proxy server.
4710 * <b>conn</b> is the connection we use the proxy server for. */
4711 void
4712 log_failed_proxy_connection(connection_t *conn)
4714 tor_addr_t proxy_addr;
4715 uint16_t proxy_port;
4716 int proxy_type;
4718 if (get_proxy_addrport(&proxy_addr, &proxy_port, &proxy_type, conn) != 0)
4719 return; /* if we have no proxy set up, leave this function. */
4721 log_warn(LD_NET,
4722 "The connection to the %s proxy server at %s just failed. "
4723 "Make sure that the proxy server is up and running.",
4724 proxy_type_to_string(proxy_type),
4725 fmt_addrport(&proxy_addr, proxy_port));
4728 /** Return string representation of <b>proxy_type</b>. */
4729 static const char *
4730 proxy_type_to_string(int proxy_type)
4732 switch (proxy_type) {
4733 case PROXY_CONNECT: return "HTTP";
4734 case PROXY_SOCKS4: return "SOCKS4";
4735 case PROXY_SOCKS5: return "SOCKS5";
4736 case PROXY_PLUGGABLE: return "pluggable transports SOCKS";
4737 case PROXY_NONE: return "NULL";
4738 default: tor_assert(0);
4740 return NULL; /*Unreached*/
4743 /** Call connection_free_() on every connection in our array, and release all
4744 * storage held by connection.c.
4746 * Don't do the checks in connection_free(), because they will
4747 * fail.
4749 void
4750 connection_free_all(void)
4752 smartlist_t *conns = get_connection_array();
4754 /* We don't want to log any messages to controllers. */
4755 SMARTLIST_FOREACH(conns, connection_t *, conn,
4756 if (conn->type == CONN_TYPE_CONTROL)
4757 TO_CONTROL_CONN(conn)->event_mask = 0);
4759 control_update_global_event_mask();
4761 /* Unlink everything from the identity map. */
4762 connection_or_clear_identity_map();
4763 connection_or_clear_ext_or_id_map();
4765 /* Clear out our list of broken connections */
4766 clear_broken_connection_map(0);
4768 SMARTLIST_FOREACH(conns, connection_t *, conn, connection_free_(conn));
4770 if (outgoing_addrs) {
4771 SMARTLIST_FOREACH(outgoing_addrs, tor_addr_t *, addr, tor_free(addr));
4772 smartlist_free(outgoing_addrs);
4773 outgoing_addrs = NULL;
4776 tor_free(last_interface_ipv4);
4777 tor_free(last_interface_ipv6);
4780 /** Log a warning, and possibly emit a control event, that <b>received</b> came
4781 * at a skewed time. <b>trusted</b> indicates that the <b>source</b> was one
4782 * that we had more faith in and therefore the warning level should have higher
4783 * severity.
4785 void
4786 clock_skew_warning(const connection_t *conn, long apparent_skew, int trusted,
4787 log_domain_mask_t domain, const char *received,
4788 const char *source)
4790 char dbuf[64];
4791 char *ext_source = NULL;
4792 format_time_interval(dbuf, sizeof(dbuf), apparent_skew);
4793 if (conn)
4794 tor_asprintf(&ext_source, "%s:%s:%d", source, conn->address, conn->port);
4795 else
4796 ext_source = tor_strdup(source);
4797 log_fn(trusted ? LOG_WARN : LOG_INFO, domain,
4798 "Received %s with skewed time (%s): "
4799 "It seems that our clock is %s by %s, or that theirs is %s%s. "
4800 "Tor requires an accurate clock to work: please check your time, "
4801 "timezone, and date settings.", received, ext_source,
4802 apparent_skew > 0 ? "ahead" : "behind", dbuf,
4803 apparent_skew > 0 ? "behind" : "ahead",
4804 (!conn || trusted) ? "" : ", or they are sending us the wrong time");
4805 if (trusted)
4806 control_event_general_status(LOG_WARN, "CLOCK_SKEW SKEW=%ld SOURCE=%s",
4807 apparent_skew, ext_source);
4808 tor_free(ext_source);