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