TOR: update to v0.2.5.12
[tomato.git] / release / src / router / tor / src / or / connection.c
blob276dca28188fccf38c575d78f628381df92e2154
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-2013, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
7 /**
8 * \file connection.c
9 * \brief General high-level functions to handle reading and writing
10 * on connections.
11 **/
13 #define CONNECTION_PRIVATE
14 #include "or.h"
15 #include "buffers.h"
17 * Define this so we get channel internal functions, since we're implementing
18 * part of a subclass (channel_tls_t).
20 #define TOR_CHANNEL_INTERNAL_
21 #define CONNECTION_PRIVATE
22 #include "channel.h"
23 #include "channeltls.h"
24 #include "circuitbuild.h"
25 #include "circuitlist.h"
26 #include "circuituse.h"
27 #include "config.h"
28 #include "connection.h"
29 #include "connection_edge.h"
30 #include "connection_or.h"
31 #include "control.h"
32 #include "cpuworker.h"
33 #include "directory.h"
34 #include "dirserv.h"
35 #include "dns.h"
36 #include "dnsserv.h"
37 #include "entrynodes.h"
38 #include "ext_orport.h"
39 #include "geoip.h"
40 #include "main.h"
41 #include "policies.h"
42 #include "reasons.h"
43 #include "relay.h"
44 #include "rendclient.h"
45 #include "rendcommon.h"
46 #include "rephist.h"
47 #include "router.h"
48 #include "transports.h"
49 #include "routerparse.h"
50 #include "transports.h"
52 #ifdef USE_BUFFEREVENTS
53 #include <event2/event.h>
54 #endif
56 #ifdef HAVE_PWD_H
57 #include <pwd.h>
58 #endif
60 static connection_t *connection_listener_new(
61 const struct sockaddr *listensockaddr,
62 socklen_t listensocklen, int type,
63 const char *address,
64 const port_cfg_t *portcfg);
65 static void connection_init(time_t now, connection_t *conn, int type,
66 int socket_family);
67 static int connection_init_accepted_conn(connection_t *conn,
68 const listener_connection_t *listener);
69 static int connection_handle_listener_read(connection_t *conn, int new_type);
70 #ifndef USE_BUFFEREVENTS
71 static int connection_bucket_should_increase(int bucket,
72 or_connection_t *conn);
73 #endif
74 static int connection_finished_flushing(connection_t *conn);
75 static int connection_flushed_some(connection_t *conn);
76 static int connection_finished_connecting(connection_t *conn);
77 static int connection_reached_eof(connection_t *conn);
78 static int connection_read_to_buf(connection_t *conn, ssize_t *max_to_read,
79 int *socket_error);
80 static int connection_process_inbuf(connection_t *conn, int package_partial);
81 static void client_check_address_changed(tor_socket_t sock);
82 static void set_constrained_socket_buffers(tor_socket_t sock, int size);
84 static const char *connection_proxy_state_to_string(int state);
85 static int connection_read_https_proxy_response(connection_t *conn);
86 static void connection_send_socks5_connect(connection_t *conn);
87 static const char *proxy_type_to_string(int proxy_type);
88 static int get_proxy_type(void);
90 /** The last addresses that our network interface seemed to have been
91 * binding to. We use this as one way to detect when our IP changes.
93 * XXX024 We should really use the entire list of interfaces here.
94 **/
95 static tor_addr_t *last_interface_ipv4 = NULL;
96 /* DOCDOC last_interface_ipv6 */
97 static tor_addr_t *last_interface_ipv6 = NULL;
98 /** A list of tor_addr_t for addresses we've used in outgoing connections.
99 * Used to detect IP address changes. */
100 static smartlist_t *outgoing_addrs = NULL;
102 #define CASE_ANY_LISTENER_TYPE \
103 case CONN_TYPE_OR_LISTENER: \
104 case CONN_TYPE_EXT_OR_LISTENER: \
105 case CONN_TYPE_AP_LISTENER: \
106 case CONN_TYPE_DIR_LISTENER: \
107 case CONN_TYPE_CONTROL_LISTENER: \
108 case CONN_TYPE_AP_TRANS_LISTENER: \
109 case CONN_TYPE_AP_NATD_LISTENER: \
110 case CONN_TYPE_AP_DNS_LISTENER
112 /**************************************************************/
115 * Return the human-readable name for the connection type <b>type</b>
117 const char *
118 conn_type_to_string(int type)
120 static char buf[64];
121 switch (type) {
122 case CONN_TYPE_OR_LISTENER: return "OR listener";
123 case CONN_TYPE_OR: return "OR";
124 case CONN_TYPE_EXIT: return "Exit";
125 case CONN_TYPE_AP_LISTENER: return "Socks listener";
126 case CONN_TYPE_AP_TRANS_LISTENER:
127 return "Transparent pf/netfilter listener";
128 case CONN_TYPE_AP_NATD_LISTENER: return "Transparent natd listener";
129 case CONN_TYPE_AP_DNS_LISTENER: return "DNS listener";
130 case CONN_TYPE_AP: return "Socks";
131 case CONN_TYPE_DIR_LISTENER: return "Directory listener";
132 case CONN_TYPE_DIR: return "Directory";
133 case CONN_TYPE_CPUWORKER: return "CPU worker";
134 case CONN_TYPE_CONTROL_LISTENER: return "Control listener";
135 case CONN_TYPE_CONTROL: return "Control";
136 case CONN_TYPE_EXT_OR: return "Extended OR";
137 case CONN_TYPE_EXT_OR_LISTENER: return "Extended OR listener";
138 default:
139 log_warn(LD_BUG, "unknown connection type %d", type);
140 tor_snprintf(buf, sizeof(buf), "unknown [%d]", type);
141 return buf;
146 * Return the human-readable name for the connection state <b>state</b>
147 * for the connection type <b>type</b>
149 const char *
150 conn_state_to_string(int type, int state)
152 static char buf[96];
153 switch (type) {
154 CASE_ANY_LISTENER_TYPE:
155 if (state == LISTENER_STATE_READY)
156 return "ready";
157 break;
158 case CONN_TYPE_OR:
159 switch (state) {
160 case OR_CONN_STATE_CONNECTING: return "connect()ing";
161 case OR_CONN_STATE_PROXY_HANDSHAKING: return "handshaking (proxy)";
162 case OR_CONN_STATE_TLS_HANDSHAKING: return "handshaking (TLS)";
163 case OR_CONN_STATE_TLS_CLIENT_RENEGOTIATING:
164 return "renegotiating (TLS, v2 handshake)";
165 case OR_CONN_STATE_TLS_SERVER_RENEGOTIATING:
166 return "waiting for renegotiation or V3 handshake";
167 case OR_CONN_STATE_OR_HANDSHAKING_V2:
168 return "handshaking (Tor, v2 handshake)";
169 case OR_CONN_STATE_OR_HANDSHAKING_V3:
170 return "handshaking (Tor, v3 handshake)";
171 case OR_CONN_STATE_OPEN: return "open";
173 break;
174 case CONN_TYPE_EXT_OR:
175 switch (state) {
176 case EXT_OR_CONN_STATE_AUTH_WAIT_AUTH_TYPE:
177 return "waiting for authentication type";
178 case EXT_OR_CONN_STATE_AUTH_WAIT_CLIENT_NONCE:
179 return "waiting for client nonce";
180 case EXT_OR_CONN_STATE_AUTH_WAIT_CLIENT_HASH:
181 return "waiting for client hash";
182 case EXT_OR_CONN_STATE_OPEN: return "open";
183 case EXT_OR_CONN_STATE_FLUSHING: return "flushing final OKAY";
185 break;
186 case CONN_TYPE_EXIT:
187 switch (state) {
188 case EXIT_CONN_STATE_RESOLVING: return "waiting for dest info";
189 case EXIT_CONN_STATE_CONNECTING: return "connecting";
190 case EXIT_CONN_STATE_OPEN: return "open";
191 case EXIT_CONN_STATE_RESOLVEFAILED: return "resolve failed";
193 break;
194 case CONN_TYPE_AP:
195 switch (state) {
196 case AP_CONN_STATE_SOCKS_WAIT: return "waiting for socks info";
197 case AP_CONN_STATE_NATD_WAIT: return "waiting for natd dest info";
198 case AP_CONN_STATE_RENDDESC_WAIT: return "waiting for rendezvous desc";
199 case AP_CONN_STATE_CONTROLLER_WAIT: return "waiting for controller";
200 case AP_CONN_STATE_CIRCUIT_WAIT: return "waiting for circuit";
201 case AP_CONN_STATE_CONNECT_WAIT: return "waiting for connect response";
202 case AP_CONN_STATE_RESOLVE_WAIT: return "waiting for resolve response";
203 case AP_CONN_STATE_OPEN: return "open";
205 break;
206 case CONN_TYPE_DIR:
207 switch (state) {
208 case DIR_CONN_STATE_CONNECTING: return "connecting";
209 case DIR_CONN_STATE_CLIENT_SENDING: return "client sending";
210 case DIR_CONN_STATE_CLIENT_READING: return "client reading";
211 case DIR_CONN_STATE_CLIENT_FINISHED: return "client finished";
212 case DIR_CONN_STATE_SERVER_COMMAND_WAIT: return "waiting for command";
213 case DIR_CONN_STATE_SERVER_WRITING: return "writing";
215 break;
216 case CONN_TYPE_CPUWORKER:
217 switch (state) {
218 case CPUWORKER_STATE_IDLE: return "idle";
219 case CPUWORKER_STATE_BUSY_ONION: return "busy with onion";
221 break;
222 case CONN_TYPE_CONTROL:
223 switch (state) {
224 case CONTROL_CONN_STATE_OPEN: return "open (protocol v1)";
225 case CONTROL_CONN_STATE_NEEDAUTH:
226 return "waiting for authentication (protocol v1)";
228 break;
231 log_warn(LD_BUG, "unknown connection state %d (type %d)", state, type);
232 tor_snprintf(buf, sizeof(buf),
233 "unknown state [%d] on unknown [%s] connection",
234 state, conn_type_to_string(type));
235 return buf;
238 #ifdef USE_BUFFEREVENTS
239 /** Return true iff the connection's type is one that can use a
240 bufferevent-based implementation. */
242 connection_type_uses_bufferevent(connection_t *conn)
244 switch (conn->type) {
245 case CONN_TYPE_AP:
246 case CONN_TYPE_EXIT:
247 case CONN_TYPE_DIR:
248 case CONN_TYPE_CONTROL:
249 case CONN_TYPE_OR:
250 case CONN_TYPE_EXT_OR:
251 case CONN_TYPE_CPUWORKER:
252 return 1;
253 default:
254 return 0;
257 #endif
259 /** Allocate and return a new dir_connection_t, initialized as by
260 * connection_init(). */
261 dir_connection_t *
262 dir_connection_new(int socket_family)
264 dir_connection_t *dir_conn = tor_malloc_zero(sizeof(dir_connection_t));
265 connection_init(time(NULL), TO_CONN(dir_conn), CONN_TYPE_DIR, socket_family);
266 return dir_conn;
269 /** Allocate and return a new or_connection_t, initialized as by
270 * connection_init().
272 * Initialize active_circuit_pqueue.
274 * Set active_circuit_pqueue_last_recalibrated to current cell_ewma tick.
276 or_connection_t *
277 or_connection_new(int type, int socket_family)
279 or_connection_t *or_conn = tor_malloc_zero(sizeof(or_connection_t));
280 time_t now = time(NULL);
281 tor_assert(type == CONN_TYPE_OR || type == CONN_TYPE_EXT_OR);
282 connection_init(now, TO_CONN(or_conn), type, socket_family);
284 connection_or_set_canonical(or_conn, 0);
286 if (type == CONN_TYPE_EXT_OR)
287 connection_or_set_ext_or_identifier(or_conn);
289 return or_conn;
292 /** Allocate and return a new entry_connection_t, initialized as by
293 * connection_init().
295 * Allocate space to store the socks_request.
297 entry_connection_t *
298 entry_connection_new(int type, int socket_family)
300 entry_connection_t *entry_conn = tor_malloc_zero(sizeof(entry_connection_t));
301 tor_assert(type == CONN_TYPE_AP);
302 connection_init(time(NULL), ENTRY_TO_CONN(entry_conn), type, socket_family);
303 entry_conn->socks_request = socks_request_new();
304 /* If this is coming from a listener, we'll set it up based on the listener
305 * in a little while. Otherwise, we're doing this as a linked connection
306 * of some kind, and we should set it up here based on the socket family */
307 if (socket_family == AF_INET)
308 entry_conn->ipv4_traffic_ok = 1;
309 else if (socket_family == AF_INET6)
310 entry_conn->ipv6_traffic_ok = 1;
311 return entry_conn;
314 /** Allocate and return a new edge_connection_t, initialized as by
315 * connection_init(). */
316 edge_connection_t *
317 edge_connection_new(int type, int socket_family)
319 edge_connection_t *edge_conn = tor_malloc_zero(sizeof(edge_connection_t));
320 tor_assert(type == CONN_TYPE_EXIT);
321 connection_init(time(NULL), TO_CONN(edge_conn), type, socket_family);
322 return edge_conn;
325 /** Allocate and return a new control_connection_t, initialized as by
326 * connection_init(). */
327 control_connection_t *
328 control_connection_new(int socket_family)
330 control_connection_t *control_conn =
331 tor_malloc_zero(sizeof(control_connection_t));
332 connection_init(time(NULL),
333 TO_CONN(control_conn), CONN_TYPE_CONTROL, socket_family);
334 return control_conn;
337 /** Allocate and return a new listener_connection_t, initialized as by
338 * connection_init(). */
339 listener_connection_t *
340 listener_connection_new(int type, int socket_family)
342 listener_connection_t *listener_conn =
343 tor_malloc_zero(sizeof(listener_connection_t));
344 connection_init(time(NULL), TO_CONN(listener_conn), type, socket_family);
345 return listener_conn;
348 /** Allocate, initialize, and return a new connection_t subtype of <b>type</b>
349 * to make or receive connections of address family <b>socket_family</b>. The
350 * type should be one of the CONN_TYPE_* constants. */
351 connection_t *
352 connection_new(int type, int socket_family)
354 switch (type) {
355 case CONN_TYPE_OR:
356 case CONN_TYPE_EXT_OR:
357 return TO_CONN(or_connection_new(type, socket_family));
359 case CONN_TYPE_EXIT:
360 return TO_CONN(edge_connection_new(type, socket_family));
362 case CONN_TYPE_AP:
363 return ENTRY_TO_CONN(entry_connection_new(type, socket_family));
365 case CONN_TYPE_DIR:
366 return TO_CONN(dir_connection_new(socket_family));
368 case CONN_TYPE_CONTROL:
369 return TO_CONN(control_connection_new(socket_family));
371 CASE_ANY_LISTENER_TYPE:
372 return TO_CONN(listener_connection_new(type, socket_family));
374 default: {
375 connection_t *conn = tor_malloc_zero(sizeof(connection_t));
376 connection_init(time(NULL), conn, type, socket_family);
377 return conn;
382 /** Initializes conn. (you must call connection_add() to link it into the main
383 * array).
385 * Set conn-\>magic to the correct value.
387 * Set conn-\>type to <b>type</b>. Set conn-\>s and conn-\>conn_array_index to
388 * -1 to signify they are not yet assigned.
390 * Initialize conn's timestamps to now.
392 static void
393 connection_init(time_t now, connection_t *conn, int type, int socket_family)
395 static uint64_t n_connections_allocated = 1;
397 switch (type) {
398 case CONN_TYPE_OR:
399 case CONN_TYPE_EXT_OR:
400 conn->magic = OR_CONNECTION_MAGIC;
401 break;
402 case CONN_TYPE_EXIT:
403 conn->magic = EDGE_CONNECTION_MAGIC;
404 break;
405 case CONN_TYPE_AP:
406 conn->magic = ENTRY_CONNECTION_MAGIC;
407 break;
408 case CONN_TYPE_DIR:
409 conn->magic = DIR_CONNECTION_MAGIC;
410 break;
411 case CONN_TYPE_CONTROL:
412 conn->magic = CONTROL_CONNECTION_MAGIC;
413 break;
414 CASE_ANY_LISTENER_TYPE:
415 conn->magic = LISTENER_CONNECTION_MAGIC;
416 break;
417 default:
418 conn->magic = BASE_CONNECTION_MAGIC;
419 break;
422 conn->s = TOR_INVALID_SOCKET; /* give it a default of 'not used' */
423 conn->conn_array_index = -1; /* also default to 'not used' */
424 conn->global_identifier = n_connections_allocated++;
426 conn->type = type;
427 conn->socket_family = socket_family;
428 #ifndef USE_BUFFEREVENTS
429 if (!connection_is_listener(conn)) {
430 /* listeners never use their buf */
431 conn->inbuf = buf_new();
432 conn->outbuf = buf_new();
434 #endif
436 conn->timestamp_created = now;
437 conn->timestamp_lastread = now;
438 conn->timestamp_lastwritten = now;
441 /** Create a link between <b>conn_a</b> and <b>conn_b</b>. */
442 void
443 connection_link_connections(connection_t *conn_a, connection_t *conn_b)
445 tor_assert(! SOCKET_OK(conn_a->s));
446 tor_assert(! SOCKET_OK(conn_b->s));
448 conn_a->linked = 1;
449 conn_b->linked = 1;
450 conn_a->linked_conn = conn_b;
451 conn_b->linked_conn = conn_a;
454 /** Deallocate memory used by <b>conn</b>. Deallocate its buffers if
455 * necessary, close its socket if necessary, and mark the directory as dirty
456 * if <b>conn</b> is an OR or OP connection.
458 STATIC void
459 connection_free_(connection_t *conn)
461 void *mem;
462 size_t memlen;
463 if (!conn)
464 return;
466 switch (conn->type) {
467 case CONN_TYPE_OR:
468 case CONN_TYPE_EXT_OR:
469 tor_assert(conn->magic == OR_CONNECTION_MAGIC);
470 mem = TO_OR_CONN(conn);
471 memlen = sizeof(or_connection_t);
472 break;
473 case CONN_TYPE_AP:
474 tor_assert(conn->magic == ENTRY_CONNECTION_MAGIC);
475 mem = TO_ENTRY_CONN(conn);
476 memlen = sizeof(entry_connection_t);
477 break;
478 case CONN_TYPE_EXIT:
479 tor_assert(conn->magic == EDGE_CONNECTION_MAGIC);
480 mem = TO_EDGE_CONN(conn);
481 memlen = sizeof(edge_connection_t);
482 break;
483 case CONN_TYPE_DIR:
484 tor_assert(conn->magic == DIR_CONNECTION_MAGIC);
485 mem = TO_DIR_CONN(conn);
486 memlen = sizeof(dir_connection_t);
487 break;
488 case CONN_TYPE_CONTROL:
489 tor_assert(conn->magic == CONTROL_CONNECTION_MAGIC);
490 mem = TO_CONTROL_CONN(conn);
491 memlen = sizeof(control_connection_t);
492 break;
493 CASE_ANY_LISTENER_TYPE:
494 tor_assert(conn->magic == LISTENER_CONNECTION_MAGIC);
495 mem = TO_LISTENER_CONN(conn);
496 memlen = sizeof(listener_connection_t);
497 break;
498 default:
499 tor_assert(conn->magic == BASE_CONNECTION_MAGIC);
500 mem = conn;
501 memlen = sizeof(connection_t);
502 break;
505 if (conn->linked) {
506 log_info(LD_GENERAL, "Freeing linked %s connection [%s] with %d "
507 "bytes on inbuf, %d on outbuf.",
508 conn_type_to_string(conn->type),
509 conn_state_to_string(conn->type, conn->state),
510 (int)connection_get_inbuf_len(conn),
511 (int)connection_get_outbuf_len(conn));
514 if (!connection_is_listener(conn)) {
515 buf_free(conn->inbuf);
516 buf_free(conn->outbuf);
517 } else {
518 if (conn->socket_family == AF_UNIX) {
519 /* For now only control ports can be Unix domain sockets
520 * and listeners at the same time */
521 tor_assert(conn->type == CONN_TYPE_CONTROL_LISTENER);
523 if (unlink(conn->address) < 0 && errno != ENOENT) {
524 log_warn(LD_NET, "Could not unlink %s: %s", conn->address,
525 strerror(errno));
530 tor_free(conn->address);
532 if (connection_speaks_cells(conn)) {
533 or_connection_t *or_conn = TO_OR_CONN(conn);
534 tor_tls_free(or_conn->tls);
535 or_conn->tls = NULL;
536 or_handshake_state_free(or_conn->handshake_state);
537 or_conn->handshake_state = NULL;
538 tor_free(or_conn->nickname);
539 if (or_conn->chan) {
540 /* Owww, this shouldn't happen, but... */
541 log_info(LD_CHANNEL,
542 "Freeing orconn at %p, saw channel %p with ID "
543 U64_FORMAT " left un-NULLed",
544 or_conn, TLS_CHAN_TO_BASE(or_conn->chan),
545 U64_PRINTF_ARG(
546 TLS_CHAN_TO_BASE(or_conn->chan)->global_identifier));
547 if (!(TLS_CHAN_TO_BASE(or_conn->chan)->state == CHANNEL_STATE_CLOSED ||
548 TLS_CHAN_TO_BASE(or_conn->chan)->state == CHANNEL_STATE_ERROR)) {
549 channel_close_for_error(TLS_CHAN_TO_BASE(or_conn->chan));
552 or_conn->chan->conn = NULL;
553 or_conn->chan = NULL;
556 if (conn->type == CONN_TYPE_AP) {
557 entry_connection_t *entry_conn = TO_ENTRY_CONN(conn);
558 tor_free(entry_conn->chosen_exit_name);
559 tor_free(entry_conn->original_dest_address);
560 if (entry_conn->socks_request)
561 socks_request_free(entry_conn->socks_request);
562 if (entry_conn->pending_optimistic_data) {
563 generic_buffer_free(entry_conn->pending_optimistic_data);
565 if (entry_conn->sending_optimistic_data) {
566 generic_buffer_free(entry_conn->sending_optimistic_data);
569 if (CONN_IS_EDGE(conn)) {
570 rend_data_free(TO_EDGE_CONN(conn)->rend_data);
572 if (conn->type == CONN_TYPE_CONTROL) {
573 control_connection_t *control_conn = TO_CONTROL_CONN(conn);
574 tor_free(control_conn->safecookie_client_hash);
575 tor_free(control_conn->incoming_cmd);
578 tor_free(conn->read_event); /* Probably already freed by connection_free. */
579 tor_free(conn->write_event); /* Probably already freed by connection_free. */
580 IF_HAS_BUFFEREVENT(conn, {
581 /* This was a workaround to handle bugs in some old versions of libevent
582 * where callbacks can occur after calling bufferevent_free(). Setting
583 * the callbacks to NULL prevented this. It shouldn't be necessary any
584 * more, but let's not tempt fate for now. */
585 bufferevent_setcb(conn->bufev, NULL, NULL, NULL, NULL);
586 bufferevent_free(conn->bufev);
587 conn->bufev = NULL;
590 if (conn->type == CONN_TYPE_DIR) {
591 dir_connection_t *dir_conn = TO_DIR_CONN(conn);
592 tor_free(dir_conn->requested_resource);
594 tor_zlib_free(dir_conn->zlib_state);
595 if (dir_conn->fingerprint_stack) {
596 SMARTLIST_FOREACH(dir_conn->fingerprint_stack, char *, cp, tor_free(cp));
597 smartlist_free(dir_conn->fingerprint_stack);
600 cached_dir_decref(dir_conn->cached_dir);
601 rend_data_free(dir_conn->rend_data);
604 if (SOCKET_OK(conn->s)) {
605 log_debug(LD_NET,"closing fd %d.",(int)conn->s);
606 tor_close_socket(conn->s);
607 conn->s = TOR_INVALID_SOCKET;
610 if (conn->type == CONN_TYPE_OR &&
611 !tor_digest_is_zero(TO_OR_CONN(conn)->identity_digest)) {
612 log_warn(LD_BUG, "called on OR conn with non-zeroed identity_digest");
613 connection_or_remove_from_identity_map(TO_OR_CONN(conn));
615 if (conn->type == CONN_TYPE_OR || conn->type == CONN_TYPE_EXT_OR) {
616 connection_or_remove_from_ext_or_id_map(TO_OR_CONN(conn));
617 tor_free(TO_OR_CONN(conn)->ext_or_conn_id);
618 tor_free(TO_OR_CONN(conn)->ext_or_auth_correct_client_hash);
619 tor_free(TO_OR_CONN(conn)->ext_or_transport);
622 #ifdef USE_BUFFEREVENTS
623 if (conn->type == CONN_TYPE_OR && TO_OR_CONN(conn)->bucket_cfg) {
624 ev_token_bucket_cfg_free(TO_OR_CONN(conn)->bucket_cfg);
625 TO_OR_CONN(conn)->bucket_cfg = NULL;
627 #endif
629 memwipe(mem, 0xCC, memlen); /* poison memory */
630 tor_free(mem);
633 /** Make sure <b>conn</b> isn't in any of the global conn lists; then free it.
635 void
636 connection_free(connection_t *conn)
638 if (!conn)
639 return;
640 tor_assert(!connection_is_on_closeable_list(conn));
641 tor_assert(!connection_in_array(conn));
642 if (conn->linked_conn) {
643 log_err(LD_BUG, "Called with conn->linked_conn still set.");
644 tor_fragile_assert();
645 conn->linked_conn->linked_conn = NULL;
646 if (! conn->linked_conn->marked_for_close &&
647 conn->linked_conn->reading_from_linked_conn)
648 connection_start_reading(conn->linked_conn);
649 conn->linked_conn = NULL;
651 if (connection_speaks_cells(conn)) {
652 if (!tor_digest_is_zero(TO_OR_CONN(conn)->identity_digest)) {
653 connection_or_remove_from_identity_map(TO_OR_CONN(conn));
656 if (conn->type == CONN_TYPE_CONTROL) {
657 connection_control_closed(TO_CONTROL_CONN(conn));
659 connection_unregister_events(conn);
660 connection_free_(conn);
664 * Called when we're about to finally unlink and free a connection:
665 * perform necessary accounting and cleanup
666 * - Directory conns that failed to fetch a rendezvous descriptor
667 * need to inform pending rendezvous streams.
668 * - OR conns need to call rep_hist_note_*() to record status.
669 * - AP conns need to send a socks reject if necessary.
670 * - Exit conns need to call connection_dns_remove() if necessary.
671 * - AP and Exit conns need to send an end cell if they can.
672 * - DNS conns need to fail any resolves that are pending on them.
673 * - OR and edge connections need to be unlinked from circuits.
675 void
676 connection_about_to_close_connection(connection_t *conn)
678 tor_assert(conn->marked_for_close);
680 switch (conn->type) {
681 case CONN_TYPE_DIR:
682 connection_dir_about_to_close(TO_DIR_CONN(conn));
683 break;
684 case CONN_TYPE_OR:
685 case CONN_TYPE_EXT_OR:
686 connection_or_about_to_close(TO_OR_CONN(conn));
687 break;
688 case CONN_TYPE_AP:
689 connection_ap_about_to_close(TO_ENTRY_CONN(conn));
690 break;
691 case CONN_TYPE_EXIT:
692 connection_exit_about_to_close(TO_EDGE_CONN(conn));
693 break;
697 /** Return true iff connection_close_immediate() has been called on this
698 * connection. */
699 #define CONN_IS_CLOSED(c) \
700 ((c)->linked ? ((c)->linked_conn_is_closed) : (! SOCKET_OK(c->s)))
702 /** Close the underlying socket for <b>conn</b>, so we don't try to
703 * flush it. Must be used in conjunction with (right before)
704 * connection_mark_for_close().
706 void
707 connection_close_immediate(connection_t *conn)
709 assert_connection_ok(conn,0);
710 if (CONN_IS_CLOSED(conn)) {
711 log_err(LD_BUG,"Attempt to close already-closed connection.");
712 tor_fragile_assert();
713 return;
715 if (conn->outbuf_flushlen) {
716 log_info(LD_NET,"fd %d, type %s, state %s, %d bytes on outbuf.",
717 (int)conn->s, conn_type_to_string(conn->type),
718 conn_state_to_string(conn->type, conn->state),
719 (int)conn->outbuf_flushlen);
722 connection_unregister_events(conn);
724 if (SOCKET_OK(conn->s))
725 tor_close_socket(conn->s);
726 conn->s = TOR_INVALID_SOCKET;
727 if (conn->linked)
728 conn->linked_conn_is_closed = 1;
729 if (conn->outbuf)
730 buf_clear(conn->outbuf);
731 conn->outbuf_flushlen = 0;
734 /** Mark <b>conn</b> to be closed next time we loop through
735 * conn_close_if_marked() in main.c. */
736 void
737 connection_mark_for_close_(connection_t *conn, int line, const char *file)
739 assert_connection_ok(conn,0);
740 tor_assert(line);
741 tor_assert(line < 1<<16); /* marked_for_close can only fit a uint16_t. */
742 tor_assert(file);
744 if (conn->type == CONN_TYPE_OR) {
746 * An or_connection should have been closed through one of the channel-
747 * aware functions in connection_or.c. We'll assume this is an error
748 * close and do that, and log a bug warning.
750 log_warn(LD_CHANNEL | LD_BUG,
751 "Something tried to close an or_connection_t without going "
752 "through channels at %s:%d",
753 file, line);
754 connection_or_close_for_error(TO_OR_CONN(conn), 0);
755 } else {
756 /* Pass it down to the real function */
757 connection_mark_for_close_internal_(conn, line, file);
761 /** Mark <b>conn</b> to be closed next time we loop through
762 * conn_close_if_marked() in main.c; the _internal version bypasses the
763 * CONN_TYPE_OR checks; this should be called when you either are sure that
764 * if this is an or_connection_t the controlling channel has been notified
765 * (e.g. with connection_or_notify_error()), or you actually are the
766 * connection_or_close_for_error() or connection_or_close_normally function.
767 * For all other cases, use connection_mark_and_flush() instead, which
768 * checks for or_connection_t properly, instead. See below.
770 void
771 connection_mark_for_close_internal_(connection_t *conn,
772 int line, const char *file)
774 assert_connection_ok(conn,0);
775 tor_assert(line);
776 tor_assert(line < 1<<16); /* marked_for_close can only fit a uint16_t. */
777 tor_assert(file);
779 if (conn->marked_for_close) {
780 log_warn(LD_BUG,"Duplicate call to connection_mark_for_close at %s:%d"
781 " (first at %s:%d)", file, line, conn->marked_for_close_file,
782 conn->marked_for_close);
783 tor_fragile_assert();
784 return;
787 if (conn->type == CONN_TYPE_OR) {
789 * Bad news if this happens without telling the controlling channel; do
790 * this so we can find things that call this wrongly when the asserts hit.
792 log_debug(LD_CHANNEL,
793 "Calling connection_mark_for_close_internal_() on an OR conn "
794 "at %s:%d",
795 file, line);
798 conn->marked_for_close = line;
799 conn->marked_for_close_file = file;
800 add_connection_to_closeable_list(conn);
802 /* in case we're going to be held-open-til-flushed, reset
803 * the number of seconds since last successful write, so
804 * we get our whole 15 seconds */
805 conn->timestamp_lastwritten = time(NULL);
808 /** Find each connection that has hold_open_until_flushed set to
809 * 1 but hasn't written in the past 15 seconds, and set
810 * hold_open_until_flushed to 0. This means it will get cleaned
811 * up in the next loop through close_if_marked() in main.c.
813 void
814 connection_expire_held_open(void)
816 time_t now;
817 smartlist_t *conns = get_connection_array();
819 now = time(NULL);
821 SMARTLIST_FOREACH_BEGIN(conns, connection_t *, conn) {
822 /* If we've been holding the connection open, but we haven't written
823 * for 15 seconds...
825 if (conn->hold_open_until_flushed) {
826 tor_assert(conn->marked_for_close);
827 if (now - conn->timestamp_lastwritten >= 15) {
828 int severity;
829 if (conn->type == CONN_TYPE_EXIT ||
830 (conn->type == CONN_TYPE_DIR &&
831 conn->purpose == DIR_PURPOSE_SERVER))
832 severity = LOG_INFO;
833 else
834 severity = LOG_NOTICE;
835 log_fn(severity, LD_NET,
836 "Giving up on marked_for_close conn that's been flushing "
837 "for 15s (fd %d, type %s, state %s).",
838 (int)conn->s, conn_type_to_string(conn->type),
839 conn_state_to_string(conn->type, conn->state));
840 conn->hold_open_until_flushed = 0;
843 } SMARTLIST_FOREACH_END(conn);
846 #if defined(HAVE_SYS_UN_H) || defined(RUNNING_DOXYGEN)
847 /** Create an AF_UNIX listenaddr struct.
848 * <b>listenaddress</b> provides the path to the Unix socket.
850 * Eventually <b>listenaddress</b> will also optionally contain user, group,
851 * and file permissions for the new socket. But not yet. XXX
852 * Also, since we do not create the socket here the information doesn't help
853 * here.
855 * If not NULL <b>readable_address</b> will contain a copy of the path part of
856 * <b>listenaddress</b>.
858 * The listenaddr struct has to be freed by the caller.
860 static struct sockaddr_un *
861 create_unix_sockaddr(const char *listenaddress, char **readable_address,
862 socklen_t *len_out)
864 struct sockaddr_un *sockaddr = NULL;
866 sockaddr = tor_malloc_zero(sizeof(struct sockaddr_un));
867 sockaddr->sun_family = AF_UNIX;
868 if (strlcpy(sockaddr->sun_path, listenaddress, sizeof(sockaddr->sun_path))
869 >= sizeof(sockaddr->sun_path)) {
870 log_warn(LD_CONFIG, "Unix socket path '%s' is too long to fit.",
871 escaped(listenaddress));
872 tor_free(sockaddr);
873 return NULL;
876 if (readable_address)
877 *readable_address = tor_strdup(listenaddress);
879 *len_out = sizeof(struct sockaddr_un);
880 return sockaddr;
882 #else
883 static struct sockaddr *
884 create_unix_sockaddr(const char *listenaddress, char **readable_address,
885 socklen_t *len_out)
887 (void)listenaddress;
888 (void)readable_address;
889 log_fn(LOG_ERR, LD_BUG,
890 "Unix domain sockets not supported, yet we tried to create one.");
891 *len_out = 0;
892 tor_fragile_assert();
893 return NULL;
895 #endif /* HAVE_SYS_UN_H */
897 /** Warn that an accept or a connect has failed because we're running up
898 * against our ulimit. Rate-limit these warnings so that we don't spam
899 * the log. */
900 static void
901 warn_too_many_conns(void)
903 #define WARN_TOO_MANY_CONNS_INTERVAL (6*60*60)
904 static ratelim_t last_warned = RATELIM_INIT(WARN_TOO_MANY_CONNS_INTERVAL);
905 char *m;
906 if ((m = rate_limit_log(&last_warned, approx_time()))) {
907 int n_conns = get_n_open_sockets();
908 log_warn(LD_NET,"Failing because we have %d connections already. Please "
909 "raise your ulimit -n.%s", n_conns, m);
910 tor_free(m);
911 control_event_general_status(LOG_WARN, "TOO_MANY_CONNECTIONS CURRENT=%d",
912 n_conns);
916 #ifdef HAVE_SYS_UN_H
917 /** Check whether we should be willing to open an AF_UNIX socket in
918 * <b>path</b>. Return 0 if we should go ahead and -1 if we shouldn't. */
919 static int
920 check_location_for_unix_socket(const or_options_t *options, const char *path)
922 int r = -1;
923 char *p = tor_strdup(path);
924 cpd_check_t flags = CPD_CHECK_MODE_ONLY;
925 if (get_parent_directory(p)<0 || p[0] != '/') {
926 log_warn(LD_GENERAL, "Bad unix socket address '%s'. Tor does not support "
927 "relative paths for unix sockets.", path);
928 goto done;
931 if (options->ControlSocketsGroupWritable)
932 flags |= CPD_GROUP_OK;
934 if (check_private_dir(p, flags, options->User) < 0) {
935 char *escpath, *escdir;
936 escpath = esc_for_log(path);
937 escdir = esc_for_log(p);
938 log_warn(LD_GENERAL, "Before Tor can create a control socket in %s, the "
939 "directory %s needs to exist, and to be accessible only by the "
940 "user%s account that is running Tor. (On some Unix systems, "
941 "anybody who can list a socket can connect to it, so Tor is "
942 "being careful.)", escpath, escdir,
943 options->ControlSocketsGroupWritable ? " and group" : "");
944 tor_free(escpath);
945 tor_free(escdir);
946 goto done;
949 r = 0;
950 done:
951 tor_free(p);
952 return r;
954 #endif
956 /** Tell the TCP stack that it shouldn't wait for a long time after
957 * <b>sock</b> has closed before reusing its port. Return 0 on success,
958 * -1 on failure. */
959 static int
960 make_socket_reuseable(tor_socket_t sock)
962 #ifdef _WIN32
963 (void) sock;
964 return 0;
965 #else
966 int one=1;
968 /* REUSEADDR on normal places means you can rebind to the port
969 * right after somebody else has let it go. But REUSEADDR on win32
970 * means you can bind to the port _even when somebody else
971 * already has it bound_. So, don't do that on Win32. */
972 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void*) &one,
973 (socklen_t)sizeof(one)) == -1) {
974 return -1;
976 return 0;
977 #endif
980 /** Max backlog to pass to listen. We start at */
981 static int listen_limit = INT_MAX;
983 /* Listen on <b>fd</b> with appropriate backlog. Return as for listen. */
984 static int
985 tor_listen(tor_socket_t fd)
987 int r;
989 if ((r = listen(fd, listen_limit)) < 0) {
990 if (listen_limit == SOMAXCONN)
991 return r;
992 if ((r = listen(fd, SOMAXCONN)) == 0) {
993 listen_limit = SOMAXCONN;
994 log_warn(LD_NET, "Setting listen backlog to INT_MAX connections "
995 "didn't work, but SOMAXCONN did. Lowering backlog limit.");
998 return r;
1001 /** Bind a new non-blocking socket listening to the socket described
1002 * by <b>listensockaddr</b>.
1004 * <b>address</b> is only used for logging purposes and to add the information
1005 * to the conn.
1007 static connection_t *
1008 connection_listener_new(const struct sockaddr *listensockaddr,
1009 socklen_t socklen,
1010 int type, const char *address,
1011 const port_cfg_t *port_cfg)
1013 listener_connection_t *lis_conn;
1014 connection_t *conn = NULL;
1015 tor_socket_t s = TOR_INVALID_SOCKET; /* the socket we're going to make */
1016 or_options_t const *options = get_options();
1017 #if defined(HAVE_PWD_H) && defined(HAVE_SYS_UN_H)
1018 const struct passwd *pw = NULL;
1019 #endif
1020 uint16_t usePort = 0, gotPort = 0;
1021 int start_reading = 0;
1022 static int global_next_session_group = SESSION_GROUP_FIRST_AUTO;
1023 tor_addr_t addr;
1025 if (get_n_open_sockets() >= get_options()->ConnLimit_-1) {
1026 warn_too_many_conns();
1027 return NULL;
1030 if (listensockaddr->sa_family == AF_INET ||
1031 listensockaddr->sa_family == AF_INET6) {
1032 int is_tcp = (type != CONN_TYPE_AP_DNS_LISTENER);
1033 if (is_tcp)
1034 start_reading = 1;
1036 tor_addr_from_sockaddr(&addr, listensockaddr, &usePort);
1038 log_notice(LD_NET, "Opening %s on %s",
1039 conn_type_to_string(type), fmt_addrport(&addr, usePort));
1041 s = tor_open_socket_nonblocking(tor_addr_family(&addr),
1042 is_tcp ? SOCK_STREAM : SOCK_DGRAM,
1043 is_tcp ? IPPROTO_TCP: IPPROTO_UDP);
1044 if (!SOCKET_OK(s)) {
1045 log_warn(LD_NET,"Socket creation failed: %s",
1046 tor_socket_strerror(tor_socket_errno(-1)));
1047 goto err;
1050 if (make_socket_reuseable(s) < 0) {
1051 log_warn(LD_NET, "Error setting SO_REUSEADDR flag on %s: %s",
1052 conn_type_to_string(type),
1053 tor_socket_strerror(errno));
1056 #if defined USE_TRANSPARENT && defined(IP_TRANSPARENT)
1057 if (options->TransProxyType_parsed == TPT_TPROXY &&
1058 type == CONN_TYPE_AP_TRANS_LISTENER) {
1059 int one = 1;
1060 if (setsockopt(s, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) < 0) {
1061 const char *extra = "";
1062 int e = tor_socket_errno(s);
1063 if (e == EPERM)
1064 extra = "TransTPROXY requires root privileges or similar"
1065 " capabilities.";
1066 log_warn(LD_NET, "Error setting IP_TRANSPARENT flag: %s.%s",
1067 tor_socket_strerror(e), extra);
1070 #endif
1072 #ifdef IPV6_V6ONLY
1073 if (listensockaddr->sa_family == AF_INET6) {
1074 #ifdef _WIN32
1075 /* In Redmond, this kind of thing passes for standards-conformance. */
1076 DWORD one = 1;
1077 #else
1078 int one = 1;
1079 #endif
1080 /* We need to set IPV6_V6ONLY so that this socket can't get used for
1081 * IPv4 connections. */
1082 if (setsockopt(s,IPPROTO_IPV6, IPV6_V6ONLY,
1083 (void*)&one, sizeof(one)) < 0) {
1084 int e = tor_socket_errno(s);
1085 log_warn(LD_NET, "Error setting IPV6_V6ONLY flag: %s",
1086 tor_socket_strerror(e));
1087 /* Keep going; probably not harmful. */
1090 #endif
1092 if (bind(s,listensockaddr,socklen) < 0) {
1093 const char *helpfulhint = "";
1094 int e = tor_socket_errno(s);
1095 if (ERRNO_IS_EADDRINUSE(e))
1096 helpfulhint = ". Is Tor already running?";
1097 log_warn(LD_NET, "Could not bind to %s:%u: %s%s", address, usePort,
1098 tor_socket_strerror(e), helpfulhint);
1099 goto err;
1102 if (is_tcp) {
1103 if (tor_listen(s) < 0) {
1104 log_warn(LD_NET, "Could not listen on %s:%u: %s", address, usePort,
1105 tor_socket_strerror(tor_socket_errno(s)));
1106 goto err;
1110 if (usePort != 0) {
1111 gotPort = usePort;
1112 } else {
1113 tor_addr_t addr2;
1114 struct sockaddr_storage ss;
1115 socklen_t ss_len=sizeof(ss);
1116 if (getsockname(s, (struct sockaddr*)&ss, &ss_len)<0) {
1117 log_warn(LD_NET, "getsockname() couldn't learn address for %s: %s",
1118 conn_type_to_string(type),
1119 tor_socket_strerror(tor_socket_errno(s)));
1120 gotPort = 0;
1122 tor_addr_from_sockaddr(&addr2, (struct sockaddr*)&ss, &gotPort);
1124 #ifdef HAVE_SYS_UN_H
1125 } else if (listensockaddr->sa_family == AF_UNIX) {
1126 start_reading = 1;
1128 /* For now only control ports can be Unix domain sockets
1129 * and listeners at the same time */
1130 tor_assert(type == CONN_TYPE_CONTROL_LISTENER);
1132 if (check_location_for_unix_socket(options, address) < 0)
1133 goto err;
1135 log_notice(LD_NET, "Opening %s on %s",
1136 conn_type_to_string(type), address);
1138 tor_addr_make_unspec(&addr);
1140 if (unlink(address) < 0 && errno != ENOENT) {
1141 log_warn(LD_NET, "Could not unlink %s: %s", address,
1142 strerror(errno));
1143 goto err;
1145 s = tor_open_socket_nonblocking(AF_UNIX, SOCK_STREAM, 0);
1146 if (! SOCKET_OK(s)) {
1147 log_warn(LD_NET,"Socket creation failed: %s.", strerror(errno));
1148 goto err;
1151 if (bind(s, listensockaddr, (socklen_t)sizeof(struct sockaddr_un)) == -1) {
1152 log_warn(LD_NET,"Bind to %s failed: %s.", address,
1153 tor_socket_strerror(tor_socket_errno(s)));
1154 goto err;
1156 #ifdef HAVE_PWD_H
1157 if (options->User) {
1158 pw = tor_getpwnam(options->User);
1159 if (pw == NULL) {
1160 log_warn(LD_NET,"Unable to chown() %s socket: user %s not found.",
1161 address, options->User);
1162 goto err;
1163 } else if (chown(address, pw->pw_uid, pw->pw_gid) < 0) {
1164 log_warn(LD_NET,"Unable to chown() %s socket: %s.",
1165 address, strerror(errno));
1166 goto err;
1169 #endif
1170 if (options->ControlSocketsGroupWritable) {
1171 /* We need to use chmod; fchmod doesn't work on sockets on all
1172 * platforms. */
1173 if (chmod(address, 0660) < 0) {
1174 log_warn(LD_FS,"Unable to make %s group-writable.", address);
1175 goto err;
1179 if (listen(s, SOMAXCONN) < 0) {
1180 log_warn(LD_NET, "Could not listen on %s: %s", address,
1181 tor_socket_strerror(tor_socket_errno(s)));
1182 goto err;
1184 #else
1185 (void)options;
1186 #endif /* HAVE_SYS_UN_H */
1187 } else {
1188 log_err(LD_BUG, "Got unexpected address family %d.",
1189 listensockaddr->sa_family);
1190 tor_assert(0);
1193 lis_conn = listener_connection_new(type, listensockaddr->sa_family);
1194 conn = TO_CONN(lis_conn);
1195 conn->socket_family = listensockaddr->sa_family;
1196 conn->s = s;
1197 s = TOR_INVALID_SOCKET; /* Prevent double-close */
1198 conn->address = tor_strdup(address);
1199 conn->port = gotPort;
1200 tor_addr_copy(&conn->addr, &addr);
1202 if (port_cfg->isolation_flags) {
1203 lis_conn->isolation_flags = port_cfg->isolation_flags;
1204 if (port_cfg->session_group >= 0) {
1205 lis_conn->session_group = port_cfg->session_group;
1206 } else {
1207 /* This can wrap after around INT_MAX listeners are opened. But I don't
1208 * believe that matters, since you would need to open a ridiculous
1209 * number of listeners while keeping the early ones open before you ever
1210 * hit this. An OR with a dozen ports open, for example, would have to
1211 * close and re-open its listeners every second for 4 years nonstop.
1213 lis_conn->session_group = global_next_session_group--;
1216 if (type == CONN_TYPE_AP_LISTENER) {
1217 lis_conn->socks_ipv4_traffic = port_cfg->ipv4_traffic;
1218 lis_conn->socks_ipv6_traffic = port_cfg->ipv6_traffic;
1219 lis_conn->socks_prefer_ipv6 = port_cfg->prefer_ipv6;
1220 } else {
1221 lis_conn->socks_ipv4_traffic = 1;
1222 lis_conn->socks_ipv6_traffic = 1;
1224 lis_conn->cache_ipv4_answers = port_cfg->cache_ipv4_answers;
1225 lis_conn->cache_ipv6_answers = port_cfg->cache_ipv6_answers;
1226 lis_conn->use_cached_ipv4_answers = port_cfg->use_cached_ipv4_answers;
1227 lis_conn->use_cached_ipv6_answers = port_cfg->use_cached_ipv6_answers;
1228 lis_conn->prefer_ipv6_virtaddr = port_cfg->prefer_ipv6_virtaddr;
1229 lis_conn->socks_prefer_no_auth = port_cfg->socks_prefer_no_auth;
1231 if (connection_add(conn) < 0) { /* no space, forget it */
1232 log_warn(LD_NET,"connection_add for listener failed. Giving up.");
1233 goto err;
1236 log_fn(usePort==gotPort ? LOG_DEBUG : LOG_NOTICE, LD_NET,
1237 "%s listening on port %u.",
1238 conn_type_to_string(type), gotPort);
1240 conn->state = LISTENER_STATE_READY;
1241 if (start_reading) {
1242 connection_start_reading(conn);
1243 } else {
1244 tor_assert(type == CONN_TYPE_AP_DNS_LISTENER);
1245 dnsserv_configure_listener(conn);
1248 return conn;
1250 err:
1251 if (SOCKET_OK(s))
1252 tor_close_socket(s);
1253 if (conn)
1254 connection_free(conn);
1256 return NULL;
1259 /** Do basic sanity checking on a newly received socket. Return 0
1260 * if it looks ok, else return -1.
1262 * Notably, some TCP stacks can erroneously have accept() return successfully
1263 * with socklen 0, when the client sends an RST before the accept call (as
1264 * nmap does). We want to detect that, and not go on with the connection.
1266 static int
1267 check_sockaddr(const struct sockaddr *sa, int len, int level)
1269 int ok = 1;
1271 if (sa->sa_family == AF_INET) {
1272 struct sockaddr_in *sin=(struct sockaddr_in*)sa;
1273 if (len != sizeof(struct sockaddr_in)) {
1274 log_fn(level, LD_NET, "Length of address not as expected: %d vs %d",
1275 len,(int)sizeof(struct sockaddr_in));
1276 ok = 0;
1278 if (sin->sin_addr.s_addr == 0 || sin->sin_port == 0) {
1279 log_fn(level, LD_NET,
1280 "Address for new connection has address/port equal to zero.");
1281 ok = 0;
1283 } else if (sa->sa_family == AF_INET6) {
1284 struct sockaddr_in6 *sin6=(struct sockaddr_in6*)sa;
1285 if (len != sizeof(struct sockaddr_in6)) {
1286 log_fn(level, LD_NET, "Length of address not as expected: %d vs %d",
1287 len,(int)sizeof(struct sockaddr_in6));
1288 ok = 0;
1290 if (tor_mem_is_zero((void*)sin6->sin6_addr.s6_addr, 16) ||
1291 sin6->sin6_port == 0) {
1292 log_fn(level, LD_NET,
1293 "Address for new connection has address/port equal to zero.");
1294 ok = 0;
1296 } else {
1297 ok = 0;
1299 return ok ? 0 : -1;
1302 /** Check whether the socket family from an accepted socket <b>got</b> is the
1303 * same as the one that <b>listener</b> is waiting for. If it isn't, log
1304 * a useful message and return -1. Else return 0.
1306 * This is annoying, but can apparently happen on some Darwins. */
1307 static int
1308 check_sockaddr_family_match(sa_family_t got, connection_t *listener)
1310 if (got != listener->socket_family) {
1311 log_info(LD_BUG, "A listener connection returned a socket with a "
1312 "mismatched family. %s for addr_family %d gave us a socket "
1313 "with address family %d. Dropping.",
1314 conn_type_to_string(listener->type),
1315 (int)listener->socket_family,
1316 (int)got);
1317 return -1;
1319 return 0;
1322 /** The listener connection <b>conn</b> told poll() it wanted to read.
1323 * Call accept() on conn-\>s, and add the new connection if necessary.
1325 static int
1326 connection_handle_listener_read(connection_t *conn, int new_type)
1328 tor_socket_t news; /* the new socket */
1329 connection_t *newconn;
1330 /* information about the remote peer when connecting to other routers */
1331 struct sockaddr_storage addrbuf;
1332 struct sockaddr *remote = (struct sockaddr*)&addrbuf;
1333 /* length of the remote address. Must be whatever accept() needs. */
1334 socklen_t remotelen = (socklen_t)sizeof(addrbuf);
1335 const or_options_t *options = get_options();
1337 tor_assert((size_t)remotelen >= sizeof(struct sockaddr_in));
1338 memset(&addrbuf, 0, sizeof(addrbuf));
1340 news = tor_accept_socket_nonblocking(conn->s,remote,&remotelen);
1341 if (!SOCKET_OK(news)) { /* accept() error */
1342 int e = tor_socket_errno(conn->s);
1343 if (ERRNO_IS_ACCEPT_EAGAIN(e)) {
1344 return 0; /* he hung up before we could accept(). that's fine. */
1345 } else if (ERRNO_IS_ACCEPT_RESOURCE_LIMIT(e)) {
1346 warn_too_many_conns();
1347 return 0;
1349 /* else there was a real error. */
1350 log_warn(LD_NET,"accept() failed: %s. Closing listener.",
1351 tor_socket_strerror(e));
1352 connection_mark_for_close(conn);
1353 return -1;
1355 log_debug(LD_NET,
1356 "Connection accepted on socket %d (child of fd %d).",
1357 (int)news,(int)conn->s);
1359 if (make_socket_reuseable(news) < 0) {
1360 if (tor_socket_errno(news) == EINVAL) {
1361 /* This can happen on OSX if we get a badly timed shutdown. */
1362 log_debug(LD_NET, "make_socket_reuseable returned EINVAL");
1363 } else {
1364 log_warn(LD_NET, "Error setting SO_REUSEADDR flag on %s: %s",
1365 conn_type_to_string(new_type),
1366 tor_socket_strerror(errno));
1368 tor_close_socket(news);
1369 return 0;
1372 if (options->ConstrainedSockets)
1373 set_constrained_socket_buffers(news, (int)options->ConstrainedSockSize);
1375 if (check_sockaddr_family_match(remote->sa_family, conn) < 0) {
1376 tor_close_socket(news);
1377 return 0;
1380 if (conn->socket_family == AF_INET || conn->socket_family == AF_INET6) {
1381 tor_addr_t addr;
1382 uint16_t port;
1383 if (check_sockaddr(remote, remotelen, LOG_INFO)<0) {
1384 log_info(LD_NET,
1385 "accept() returned a strange address; closing connection.");
1386 tor_close_socket(news);
1387 return 0;
1390 tor_addr_from_sockaddr(&addr, remote, &port);
1392 /* process entrance policies here, before we even create the connection */
1393 if (new_type == CONN_TYPE_AP) {
1394 /* check sockspolicy to see if we should accept it */
1395 if (socks_policy_permits_address(&addr) == 0) {
1396 log_notice(LD_APP,
1397 "Denying socks connection from untrusted address %s.",
1398 fmt_and_decorate_addr(&addr));
1399 tor_close_socket(news);
1400 return 0;
1403 if (new_type == CONN_TYPE_DIR) {
1404 /* check dirpolicy to see if we should accept it */
1405 if (dir_policy_permits_address(&addr) == 0) {
1406 log_notice(LD_DIRSERV,"Denying dir connection from address %s.",
1407 fmt_and_decorate_addr(&addr));
1408 tor_close_socket(news);
1409 return 0;
1413 newconn = connection_new(new_type, conn->socket_family);
1414 newconn->s = news;
1416 /* remember the remote address */
1417 tor_addr_copy(&newconn->addr, &addr);
1418 newconn->port = port;
1419 newconn->address = tor_dup_addr(&addr);
1421 if (new_type == CONN_TYPE_AP) {
1422 TO_ENTRY_CONN(newconn)->socks_request->socks_prefer_no_auth =
1423 TO_LISTENER_CONN(conn)->socks_prefer_no_auth;
1425 if (new_type == CONN_TYPE_CONTROL) {
1426 log_notice(LD_CONTROL, "New control connection opened from %s.",
1427 fmt_and_decorate_addr(&addr));
1430 } else if (conn->socket_family == AF_UNIX) {
1431 /* For now only control ports can be Unix domain sockets
1432 * and listeners at the same time */
1433 tor_assert(conn->type == CONN_TYPE_CONTROL_LISTENER);
1434 tor_assert(new_type == CONN_TYPE_CONTROL);
1435 log_notice(LD_CONTROL, "New control connection opened.");
1437 newconn = connection_new(new_type, conn->socket_family);
1438 newconn->s = news;
1440 /* remember the remote address -- do we have anything sane to put here? */
1441 tor_addr_make_unspec(&newconn->addr);
1442 newconn->port = 1;
1443 newconn->address = tor_strdup(conn->address);
1444 } else {
1445 tor_assert(0);
1448 if (connection_add(newconn) < 0) { /* no space, forget it */
1449 connection_free(newconn);
1450 return 0; /* no need to tear down the parent */
1453 if (connection_init_accepted_conn(newconn, TO_LISTENER_CONN(conn)) < 0) {
1454 if (! newconn->marked_for_close)
1455 connection_mark_for_close(newconn);
1456 return 0;
1458 return 0;
1461 /** Initialize states for newly accepted connection <b>conn</b>.
1462 * If conn is an OR, start the TLS handshake.
1463 * If conn is a transparent AP, get its original destination
1464 * and place it in circuit_wait.
1466 static int
1467 connection_init_accepted_conn(connection_t *conn,
1468 const listener_connection_t *listener)
1470 int rv;
1472 connection_start_reading(conn);
1474 switch (conn->type) {
1475 case CONN_TYPE_EXT_OR:
1476 /* Initiate Extended ORPort authentication. */
1477 return connection_ext_or_start_auth(TO_OR_CONN(conn));
1478 case CONN_TYPE_OR:
1479 control_event_or_conn_status(TO_OR_CONN(conn), OR_CONN_EVENT_NEW, 0);
1480 rv = connection_tls_start_handshake(TO_OR_CONN(conn), 1);
1481 if (rv < 0) {
1482 connection_or_close_for_error(TO_OR_CONN(conn), 0);
1484 return rv;
1485 break;
1486 case CONN_TYPE_AP:
1487 TO_ENTRY_CONN(conn)->isolation_flags = listener->isolation_flags;
1488 TO_ENTRY_CONN(conn)->session_group = listener->session_group;
1489 TO_ENTRY_CONN(conn)->nym_epoch = get_signewnym_epoch();
1490 TO_ENTRY_CONN(conn)->socks_request->listener_type = listener->base_.type;
1491 TO_ENTRY_CONN(conn)->ipv4_traffic_ok = listener->socks_ipv4_traffic;
1492 TO_ENTRY_CONN(conn)->ipv6_traffic_ok = listener->socks_ipv6_traffic;
1493 TO_ENTRY_CONN(conn)->prefer_ipv6_traffic = listener->socks_prefer_ipv6;
1494 TO_ENTRY_CONN(conn)->cache_ipv4_answers = listener->cache_ipv4_answers;
1495 TO_ENTRY_CONN(conn)->cache_ipv6_answers = listener->cache_ipv6_answers;
1496 TO_ENTRY_CONN(conn)->use_cached_ipv4_answers =
1497 listener->use_cached_ipv4_answers;
1498 TO_ENTRY_CONN(conn)->use_cached_ipv6_answers =
1499 listener->use_cached_ipv6_answers;
1500 TO_ENTRY_CONN(conn)->prefer_ipv6_virtaddr =
1501 listener->prefer_ipv6_virtaddr;
1503 switch (TO_CONN(listener)->type) {
1504 case CONN_TYPE_AP_LISTENER:
1505 conn->state = AP_CONN_STATE_SOCKS_WAIT;
1506 break;
1507 case CONN_TYPE_AP_TRANS_LISTENER:
1508 TO_ENTRY_CONN(conn)->is_transparent_ap = 1;
1509 conn->state = AP_CONN_STATE_CIRCUIT_WAIT;
1510 return connection_ap_process_transparent(TO_ENTRY_CONN(conn));
1511 case CONN_TYPE_AP_NATD_LISTENER:
1512 TO_ENTRY_CONN(conn)->is_transparent_ap = 1;
1513 conn->state = AP_CONN_STATE_NATD_WAIT;
1514 break;
1516 break;
1517 case CONN_TYPE_DIR:
1518 conn->purpose = DIR_PURPOSE_SERVER;
1519 conn->state = DIR_CONN_STATE_SERVER_COMMAND_WAIT;
1520 break;
1521 case CONN_TYPE_CONTROL:
1522 conn->state = CONTROL_CONN_STATE_NEEDAUTH;
1523 break;
1525 return 0;
1528 /** Take conn, make a nonblocking socket; try to connect to
1529 * addr:port (they arrive in *host order*). If fail, return -1 and if
1530 * applicable put your best guess about errno into *<b>socket_error</b>.
1531 * Else assign s to conn-\>s: if connected return 1, if EAGAIN return 0.
1533 * address is used to make the logs useful.
1535 * On success, add conn to the list of polled connections.
1538 connection_connect(connection_t *conn, const char *address,
1539 const tor_addr_t *addr, uint16_t port, int *socket_error)
1541 tor_socket_t s;
1542 int inprogress = 0;
1543 struct sockaddr_storage addrbuf;
1544 struct sockaddr *dest_addr;
1545 int dest_addr_len;
1546 const or_options_t *options = get_options();
1547 int protocol_family;
1549 if (get_n_open_sockets() >= get_options()->ConnLimit_-1) {
1550 warn_too_many_conns();
1551 *socket_error = SOCK_ERRNO(ENOBUFS);
1552 return -1;
1555 if (tor_addr_family(addr) == AF_INET6)
1556 protocol_family = PF_INET6;
1557 else
1558 protocol_family = PF_INET;
1560 if (get_options()->DisableNetwork) {
1561 /* We should never even try to connect anyplace if DisableNetwork is set.
1562 * Warn if we do, and refuse to make the connection. */
1563 static ratelim_t disablenet_violated = RATELIM_INIT(30*60);
1564 *socket_error = SOCK_ERRNO(ENETUNREACH);
1565 log_fn_ratelim(&disablenet_violated, LOG_WARN, LD_BUG,
1566 "Tried to open a socket with DisableNetwork set.");
1567 tor_fragile_assert();
1568 return -1;
1571 s = tor_open_socket_nonblocking(protocol_family,SOCK_STREAM,IPPROTO_TCP);
1572 if (! SOCKET_OK(s)) {
1573 *socket_error = tor_socket_errno(-1);
1574 log_warn(LD_NET,"Error creating network socket: %s",
1575 tor_socket_strerror(*socket_error));
1576 return -1;
1579 if (make_socket_reuseable(s) < 0) {
1580 log_warn(LD_NET, "Error setting SO_REUSEADDR flag on new connection: %s",
1581 tor_socket_strerror(errno));
1584 if (!tor_addr_is_loopback(addr)) {
1585 const tor_addr_t *ext_addr = NULL;
1586 if (protocol_family == AF_INET &&
1587 !tor_addr_is_null(&options->OutboundBindAddressIPv4_))
1588 ext_addr = &options->OutboundBindAddressIPv4_;
1589 else if (protocol_family == AF_INET6 &&
1590 !tor_addr_is_null(&options->OutboundBindAddressIPv6_))
1591 ext_addr = &options->OutboundBindAddressIPv6_;
1592 if (ext_addr) {
1593 struct sockaddr_storage ext_addr_sa;
1594 socklen_t ext_addr_len = 0;
1595 memset(&ext_addr_sa, 0, sizeof(ext_addr_sa));
1596 ext_addr_len = tor_addr_to_sockaddr(ext_addr, 0,
1597 (struct sockaddr *) &ext_addr_sa,
1598 sizeof(ext_addr_sa));
1599 if (ext_addr_len == 0) {
1600 log_warn(LD_NET,
1601 "Error converting OutboundBindAddress %s into sockaddr. "
1602 "Ignoring.", fmt_and_decorate_addr(ext_addr));
1603 } else {
1604 if (bind(s, (struct sockaddr *) &ext_addr_sa, ext_addr_len) < 0) {
1605 *socket_error = tor_socket_errno(s);
1606 log_warn(LD_NET,"Error binding network socket to %s: %s",
1607 fmt_and_decorate_addr(ext_addr),
1608 tor_socket_strerror(*socket_error));
1609 tor_close_socket(s);
1610 return -1;
1616 tor_assert(options);
1617 if (options->ConstrainedSockets)
1618 set_constrained_socket_buffers(s, (int)options->ConstrainedSockSize);
1620 memset(&addrbuf,0,sizeof(addrbuf));
1621 dest_addr = (struct sockaddr*) &addrbuf;
1622 dest_addr_len = tor_addr_to_sockaddr(addr, port, dest_addr, sizeof(addrbuf));
1623 tor_assert(dest_addr_len > 0);
1625 log_debug(LD_NET, "Connecting to %s:%u.",
1626 escaped_safe_str_client(address), port);
1628 if (connect(s, dest_addr, (socklen_t)dest_addr_len) < 0) {
1629 int e = tor_socket_errno(s);
1630 if (!ERRNO_IS_CONN_EINPROGRESS(e)) {
1631 /* yuck. kill it. */
1632 *socket_error = e;
1633 log_info(LD_NET,
1634 "connect() to %s:%u failed: %s",
1635 escaped_safe_str_client(address),
1636 port, tor_socket_strerror(e));
1637 tor_close_socket(s);
1638 return -1;
1639 } else {
1640 inprogress = 1;
1644 /* it succeeded. we're connected. */
1645 log_fn(inprogress?LOG_DEBUG:LOG_INFO, LD_NET,
1646 "Connection to %s:%u %s (sock "TOR_SOCKET_T_FORMAT").",
1647 escaped_safe_str_client(address),
1648 port, inprogress?"in progress":"established", s);
1649 conn->s = s;
1650 if (connection_add_connecting(conn) < 0) {
1651 /* no space, forget it */
1652 *socket_error = SOCK_ERRNO(ENOBUFS);
1653 return -1;
1655 return inprogress ? 0 : 1;
1658 /** Convert state number to string representation for logging purposes.
1660 static const char *
1661 connection_proxy_state_to_string(int state)
1663 static const char *unknown = "???";
1664 static const char *states[] = {
1665 "PROXY_NONE",
1666 "PROXY_INFANT",
1667 "PROXY_HTTPS_WANT_CONNECT_OK",
1668 "PROXY_SOCKS4_WANT_CONNECT_OK",
1669 "PROXY_SOCKS5_WANT_AUTH_METHOD_NONE",
1670 "PROXY_SOCKS5_WANT_AUTH_METHOD_RFC1929",
1671 "PROXY_SOCKS5_WANT_AUTH_RFC1929_OK",
1672 "PROXY_SOCKS5_WANT_CONNECT_OK",
1673 "PROXY_CONNECTED",
1676 if (state < PROXY_NONE || state > PROXY_CONNECTED)
1677 return unknown;
1679 return states[state];
1682 /** Returns the global proxy type used by tor. Use this function for
1683 * logging or high-level purposes, don't use it to fill the
1684 * <b>proxy_type</b> field of or_connection_t; use the actual proxy
1685 * protocol instead.*/
1686 static int
1687 get_proxy_type(void)
1689 const or_options_t *options = get_options();
1691 if (options->HTTPSProxy)
1692 return PROXY_CONNECT;
1693 else if (options->Socks4Proxy)
1694 return PROXY_SOCKS4;
1695 else if (options->Socks5Proxy)
1696 return PROXY_SOCKS5;
1697 else if (options->ClientTransportPlugin)
1698 return PROXY_PLUGGABLE;
1699 else
1700 return PROXY_NONE;
1703 /* One byte for the version, one for the command, two for the
1704 port, and four for the addr... and, one more for the
1705 username NUL: */
1706 #define SOCKS4_STANDARD_BUFFER_SIZE (1 + 1 + 2 + 4 + 1)
1708 /** Write a proxy request of <b>type</b> (socks4, socks5, https) to conn
1709 * for conn->addr:conn->port, authenticating with the auth details given
1710 * in the configuration (if available). SOCKS 5 and HTTP CONNECT proxies
1711 * support authentication.
1713 * Returns -1 if conn->addr is incompatible with the proxy protocol, and
1714 * 0 otherwise.
1716 * Use connection_read_proxy_handshake() to complete the handshake.
1719 connection_proxy_connect(connection_t *conn, int type)
1721 const or_options_t *options;
1723 tor_assert(conn);
1725 options = get_options();
1727 switch (type) {
1728 case PROXY_CONNECT: {
1729 char buf[1024];
1730 char *base64_authenticator=NULL;
1731 const char *authenticator = options->HTTPSProxyAuthenticator;
1733 /* Send HTTP CONNECT and authentication (if available) in
1734 * one request */
1736 if (authenticator) {
1737 base64_authenticator = alloc_http_authenticator(authenticator);
1738 if (!base64_authenticator)
1739 log_warn(LD_OR, "Encoding https authenticator failed");
1742 if (base64_authenticator) {
1743 const char *addrport = fmt_addrport(&conn->addr, conn->port);
1744 tor_snprintf(buf, sizeof(buf), "CONNECT %s HTTP/1.1\r\n"
1745 "Host: %s\r\n"
1746 "Proxy-Authorization: Basic %s\r\n\r\n",
1747 addrport,
1748 addrport,
1749 base64_authenticator);
1750 tor_free(base64_authenticator);
1751 } else {
1752 tor_snprintf(buf, sizeof(buf), "CONNECT %s HTTP/1.0\r\n\r\n",
1753 fmt_addrport(&conn->addr, conn->port));
1756 connection_write_to_buf(buf, strlen(buf), conn);
1757 conn->proxy_state = PROXY_HTTPS_WANT_CONNECT_OK;
1758 break;
1761 case PROXY_SOCKS4: {
1762 unsigned char *buf;
1763 uint16_t portn;
1764 uint32_t ip4addr;
1765 size_t buf_size = 0;
1766 char *socks_args_string = NULL;
1768 /* Send a SOCKS4 connect request */
1770 if (tor_addr_family(&conn->addr) != AF_INET) {
1771 log_warn(LD_NET, "SOCKS4 client is incompatible with IPv6");
1772 return -1;
1775 { /* If we are here because we are trying to connect to a
1776 pluggable transport proxy, check if we have any SOCKS
1777 arguments to transmit. If we do, compress all arguments to
1778 a single string in 'socks_args_string': */
1780 if (get_proxy_type() == PROXY_PLUGGABLE) {
1781 socks_args_string =
1782 pt_get_socks_args_for_proxy_addrport(&conn->addr, conn->port);
1783 if (socks_args_string)
1784 log_debug(LD_NET, "Sending out '%s' as our SOCKS argument string.",
1785 socks_args_string);
1789 { /* Figure out the buffer size we need for the SOCKS message: */
1791 buf_size = SOCKS4_STANDARD_BUFFER_SIZE;
1793 /* If we have a SOCKS argument string, consider its size when
1794 calculating the buffer size: */
1795 if (socks_args_string)
1796 buf_size += strlen(socks_args_string);
1799 buf = tor_malloc_zero(buf_size);
1801 ip4addr = tor_addr_to_ipv4n(&conn->addr);
1802 portn = htons(conn->port);
1804 buf[0] = 4; /* version */
1805 buf[1] = SOCKS_COMMAND_CONNECT; /* command */
1806 memcpy(buf + 2, &portn, 2); /* port */
1807 memcpy(buf + 4, &ip4addr, 4); /* addr */
1809 /* Next packet field is the userid. If we have pluggable
1810 transport SOCKS arguments, we have to embed them
1811 there. Otherwise, we use an empty userid. */
1812 if (socks_args_string) { /* place the SOCKS args string: */
1813 tor_assert(strlen(socks_args_string) > 0);
1814 tor_assert(buf_size >=
1815 SOCKS4_STANDARD_BUFFER_SIZE + strlen(socks_args_string));
1816 strlcpy((char *)buf + 8, socks_args_string, buf_size - 8);
1817 tor_free(socks_args_string);
1818 } else {
1819 buf[8] = 0; /* no userid */
1822 connection_write_to_buf((char *)buf, buf_size, conn);
1823 tor_free(buf);
1825 conn->proxy_state = PROXY_SOCKS4_WANT_CONNECT_OK;
1826 break;
1829 case PROXY_SOCKS5: {
1830 unsigned char buf[4]; /* fields: vers, num methods, method list */
1832 /* Send a SOCKS5 greeting (connect request must wait) */
1834 buf[0] = 5; /* version */
1836 /* We have to use SOCKS5 authentication, if we have a
1837 Socks5ProxyUsername or if we want to pass arguments to our
1838 pluggable transport proxy: */
1839 if ((options->Socks5ProxyUsername) ||
1840 (get_proxy_type() == PROXY_PLUGGABLE &&
1841 (get_socks_args_by_bridge_addrport(&conn->addr, conn->port)))) {
1842 /* number of auth methods */
1843 buf[1] = 2;
1844 buf[2] = 0x00; /* no authentication */
1845 buf[3] = 0x02; /* rfc1929 Username/Passwd auth */
1846 conn->proxy_state = PROXY_SOCKS5_WANT_AUTH_METHOD_RFC1929;
1847 } else {
1848 buf[1] = 1;
1849 buf[2] = 0x00; /* no authentication */
1850 conn->proxy_state = PROXY_SOCKS5_WANT_AUTH_METHOD_NONE;
1853 connection_write_to_buf((char *)buf, 2 + buf[1], conn);
1854 break;
1857 default:
1858 log_err(LD_BUG, "Invalid proxy protocol, %d", type);
1859 tor_fragile_assert();
1860 return -1;
1863 log_debug(LD_NET, "set state %s",
1864 connection_proxy_state_to_string(conn->proxy_state));
1866 return 0;
1869 /** Read conn's inbuf. If the http response from the proxy is all
1870 * here, make sure it's good news, then return 1. If it's bad news,
1871 * return -1. Else return 0 and hope for better luck next time.
1873 static int
1874 connection_read_https_proxy_response(connection_t *conn)
1876 char *headers;
1877 char *reason=NULL;
1878 int status_code;
1879 time_t date_header;
1881 switch (fetch_from_buf_http(conn->inbuf,
1882 &headers, MAX_HEADERS_SIZE,
1883 NULL, NULL, 10000, 0)) {
1884 case -1: /* overflow */
1885 log_warn(LD_PROTOCOL,
1886 "Your https proxy sent back an oversized response. Closing.");
1887 return -1;
1888 case 0:
1889 log_info(LD_NET,"https proxy response not all here yet. Waiting.");
1890 return 0;
1891 /* case 1, fall through */
1894 if (parse_http_response(headers, &status_code, &date_header,
1895 NULL, &reason) < 0) {
1896 log_warn(LD_NET,
1897 "Unparseable headers from proxy (connecting to '%s'). Closing.",
1898 conn->address);
1899 tor_free(headers);
1900 return -1;
1902 tor_free(headers);
1903 if (!reason) reason = tor_strdup("[no reason given]");
1905 if (status_code == 200) {
1906 log_info(LD_NET,
1907 "HTTPS connect to '%s' successful! (200 %s) Starting TLS.",
1908 conn->address, escaped(reason));
1909 tor_free(reason);
1910 return 1;
1912 /* else, bad news on the status code */
1913 switch (status_code) {
1914 case 403:
1915 log_warn(LD_NET,
1916 "The https proxy refused to allow connection to %s "
1917 "(status code %d, %s). Closing.",
1918 conn->address, status_code, escaped(reason));
1919 break;
1920 default:
1921 log_warn(LD_NET,
1922 "The https proxy sent back an unexpected status code %d (%s). "
1923 "Closing.",
1924 status_code, escaped(reason));
1925 break;
1927 tor_free(reason);
1928 return -1;
1931 /** Send SOCKS5 CONNECT command to <b>conn</b>, copying <b>conn->addr</b>
1932 * and <b>conn->port</b> into the request.
1934 static void
1935 connection_send_socks5_connect(connection_t *conn)
1937 unsigned char buf[1024];
1938 size_t reqsize = 6;
1939 uint16_t port = htons(conn->port);
1941 buf[0] = 5; /* version */
1942 buf[1] = SOCKS_COMMAND_CONNECT; /* command */
1943 buf[2] = 0; /* reserved */
1945 if (tor_addr_family(&conn->addr) == AF_INET) {
1946 uint32_t addr = tor_addr_to_ipv4n(&conn->addr);
1948 buf[3] = 1;
1949 reqsize += 4;
1950 memcpy(buf + 4, &addr, 4);
1951 memcpy(buf + 8, &port, 2);
1952 } else { /* AF_INET6 */
1953 buf[3] = 4;
1954 reqsize += 16;
1955 memcpy(buf + 4, tor_addr_to_in6(&conn->addr), 16);
1956 memcpy(buf + 20, &port, 2);
1959 connection_write_to_buf((char *)buf, reqsize, conn);
1961 conn->proxy_state = PROXY_SOCKS5_WANT_CONNECT_OK;
1964 /** Wrapper around fetch_from_(buf/evbuffer)_socks_client: see those functions
1965 * for documentation of its behavior. */
1966 static int
1967 connection_fetch_from_buf_socks_client(connection_t *conn,
1968 int state, char **reason)
1970 IF_HAS_BUFFEREVENT(conn, {
1971 struct evbuffer *input = bufferevent_get_input(conn->bufev);
1972 return fetch_from_evbuffer_socks_client(input, state, reason);
1973 }) ELSE_IF_NO_BUFFEREVENT {
1974 return fetch_from_buf_socks_client(conn->inbuf, state, reason);
1978 /** Call this from connection_*_process_inbuf() to advance the proxy
1979 * handshake.
1981 * No matter what proxy protocol is used, if this function returns 1, the
1982 * handshake is complete, and the data remaining on inbuf may contain the
1983 * start of the communication with the requested server.
1985 * Returns 0 if the current buffer contains an incomplete response, and -1
1986 * on error.
1989 connection_read_proxy_handshake(connection_t *conn)
1991 int ret = 0;
1992 char *reason = NULL;
1994 log_debug(LD_NET, "enter state %s",
1995 connection_proxy_state_to_string(conn->proxy_state));
1997 switch (conn->proxy_state) {
1998 case PROXY_HTTPS_WANT_CONNECT_OK:
1999 ret = connection_read_https_proxy_response(conn);
2000 if (ret == 1)
2001 conn->proxy_state = PROXY_CONNECTED;
2002 break;
2004 case PROXY_SOCKS4_WANT_CONNECT_OK:
2005 ret = connection_fetch_from_buf_socks_client(conn,
2006 conn->proxy_state,
2007 &reason);
2008 if (ret == 1)
2009 conn->proxy_state = PROXY_CONNECTED;
2010 break;
2012 case PROXY_SOCKS5_WANT_AUTH_METHOD_NONE:
2013 ret = connection_fetch_from_buf_socks_client(conn,
2014 conn->proxy_state,
2015 &reason);
2016 /* no auth needed, do connect */
2017 if (ret == 1) {
2018 connection_send_socks5_connect(conn);
2019 ret = 0;
2021 break;
2023 case PROXY_SOCKS5_WANT_AUTH_METHOD_RFC1929:
2024 ret = connection_fetch_from_buf_socks_client(conn,
2025 conn->proxy_state,
2026 &reason);
2028 /* send auth if needed, otherwise do connect */
2029 if (ret == 1) {
2030 connection_send_socks5_connect(conn);
2031 ret = 0;
2032 } else if (ret == 2) {
2033 unsigned char buf[1024];
2034 size_t reqsize, usize, psize;
2035 const char *user, *pass;
2036 char *socks_args_string = NULL;
2038 if (get_proxy_type() == PROXY_PLUGGABLE) {
2039 socks_args_string =
2040 pt_get_socks_args_for_proxy_addrport(&conn->addr, conn->port);
2041 if (!socks_args_string) {
2042 log_warn(LD_NET, "Could not create SOCKS args string.");
2043 ret = -1;
2044 break;
2047 log_debug(LD_NET, "SOCKS5 arguments: %s", socks_args_string);
2048 tor_assert(strlen(socks_args_string) > 0);
2049 tor_assert(strlen(socks_args_string) <= MAX_SOCKS5_AUTH_SIZE_TOTAL);
2051 if (strlen(socks_args_string) > MAX_SOCKS5_AUTH_FIELD_SIZE) {
2052 user = socks_args_string;
2053 usize = MAX_SOCKS5_AUTH_FIELD_SIZE;
2054 pass = socks_args_string + MAX_SOCKS5_AUTH_FIELD_SIZE;
2055 psize = strlen(socks_args_string) - MAX_SOCKS5_AUTH_FIELD_SIZE;
2056 } else {
2057 user = socks_args_string;
2058 usize = strlen(socks_args_string);
2059 pass = "\0";
2060 psize = 1;
2062 } else if (get_options()->Socks5ProxyUsername) {
2063 user = get_options()->Socks5ProxyUsername;
2064 pass = get_options()->Socks5ProxyPassword;
2065 tor_assert(user && pass);
2066 usize = strlen(user);
2067 psize = strlen(pass);
2068 } else {
2069 log_err(LD_BUG, "We entered %s for no reason!", __func__);
2070 tor_fragile_assert();
2071 ret = -1;
2072 break;
2075 /* Username and password lengths should have been checked
2076 above and during torrc parsing. */
2077 tor_assert(usize <= MAX_SOCKS5_AUTH_FIELD_SIZE &&
2078 psize <= MAX_SOCKS5_AUTH_FIELD_SIZE);
2079 reqsize = 3 + usize + psize;
2081 buf[0] = 1; /* negotiation version */
2082 buf[1] = usize;
2083 memcpy(buf + 2, user, usize);
2084 buf[2 + usize] = psize;
2085 memcpy(buf + 3 + usize, pass, psize);
2087 if (socks_args_string)
2088 tor_free(socks_args_string);
2090 connection_write_to_buf((char *)buf, reqsize, conn);
2092 conn->proxy_state = PROXY_SOCKS5_WANT_AUTH_RFC1929_OK;
2093 ret = 0;
2095 break;
2097 case PROXY_SOCKS5_WANT_AUTH_RFC1929_OK:
2098 ret = connection_fetch_from_buf_socks_client(conn,
2099 conn->proxy_state,
2100 &reason);
2101 /* send the connect request */
2102 if (ret == 1) {
2103 connection_send_socks5_connect(conn);
2104 ret = 0;
2106 break;
2108 case PROXY_SOCKS5_WANT_CONNECT_OK:
2109 ret = connection_fetch_from_buf_socks_client(conn,
2110 conn->proxy_state,
2111 &reason);
2112 if (ret == 1)
2113 conn->proxy_state = PROXY_CONNECTED;
2114 break;
2116 default:
2117 log_err(LD_BUG, "Invalid proxy_state for reading, %d",
2118 conn->proxy_state);
2119 tor_fragile_assert();
2120 ret = -1;
2121 break;
2124 log_debug(LD_NET, "leaving state %s",
2125 connection_proxy_state_to_string(conn->proxy_state));
2127 if (ret < 0) {
2128 if (reason) {
2129 log_warn(LD_NET, "Proxy Client: unable to connect to %s:%d (%s)",
2130 conn->address, conn->port, escaped(reason));
2131 tor_free(reason);
2132 } else {
2133 log_warn(LD_NET, "Proxy Client: unable to connect to %s:%d",
2134 conn->address, conn->port);
2136 } else if (ret == 1) {
2137 log_info(LD_NET, "Proxy Client: connection to %s:%d successful",
2138 conn->address, conn->port);
2141 return ret;
2144 /** Given a list of listener connections in <b>old_conns</b>, and list of
2145 * port_cfg_t entries in <b>ports</b>, open a new listener for every port in
2146 * <b>ports</b> that does not already have a listener in <b>old_conns</b>.
2148 * Remove from <b>old_conns</b> every connection that has a corresponding
2149 * entry in <b>ports</b>. Add to <b>new_conns</b> new every connection we
2150 * launch.
2152 * If <b>control_listeners_only</b> is true, then we only open control
2153 * listeners, and we do not remove any noncontrol listeners from old_conns.
2155 * Return 0 on success, -1 on failure.
2157 static int
2158 retry_listener_ports(smartlist_t *old_conns,
2159 const smartlist_t *ports,
2160 smartlist_t *new_conns,
2161 int control_listeners_only)
2163 smartlist_t *launch = smartlist_new();
2164 int r = 0;
2166 if (control_listeners_only) {
2167 SMARTLIST_FOREACH(ports, port_cfg_t *, p, {
2168 if (p->type == CONN_TYPE_CONTROL_LISTENER)
2169 smartlist_add(launch, p);
2171 } else {
2172 smartlist_add_all(launch, ports);
2175 /* Iterate through old_conns, comparing it to launch: remove from both lists
2176 * each pair of elements that corresponds to the same port. */
2177 SMARTLIST_FOREACH_BEGIN(old_conns, connection_t *, conn) {
2178 const port_cfg_t *found_port = NULL;
2180 /* Okay, so this is a listener. Is it configured? */
2181 SMARTLIST_FOREACH_BEGIN(launch, const port_cfg_t *, wanted) {
2182 if (conn->type != wanted->type)
2183 continue;
2184 if ((conn->socket_family != AF_UNIX && wanted->is_unix_addr) ||
2185 (conn->socket_family == AF_UNIX && ! wanted->is_unix_addr))
2186 continue;
2188 if (wanted->no_listen)
2189 continue; /* We don't want to open a listener for this one */
2191 if (wanted->is_unix_addr) {
2192 if (conn->socket_family == AF_UNIX &&
2193 !strcmp(wanted->unix_addr, conn->address)) {
2194 found_port = wanted;
2195 break;
2197 } else {
2198 int port_matches;
2199 if (wanted->port == CFG_AUTO_PORT) {
2200 port_matches = 1;
2201 } else {
2202 port_matches = (wanted->port == conn->port);
2204 if (port_matches && tor_addr_eq(&wanted->addr, &conn->addr)) {
2205 found_port = wanted;
2206 break;
2209 } SMARTLIST_FOREACH_END(wanted);
2211 if (found_port) {
2212 /* This listener is already running; we don't need to launch it. */
2213 //log_debug(LD_NET, "Already have %s on %s:%d",
2214 // conn_type_to_string(found_port->type), conn->address, conn->port);
2215 smartlist_remove(launch, found_port);
2216 /* And we can remove the connection from old_conns too. */
2217 SMARTLIST_DEL_CURRENT(old_conns, conn);
2219 } SMARTLIST_FOREACH_END(conn);
2221 /* Now open all the listeners that are configured but not opened. */
2222 SMARTLIST_FOREACH_BEGIN(launch, const port_cfg_t *, port) {
2223 struct sockaddr *listensockaddr;
2224 socklen_t listensocklen = 0;
2225 char *address=NULL;
2226 connection_t *conn;
2227 int real_port = port->port == CFG_AUTO_PORT ? 0 : port->port;
2228 tor_assert(real_port <= UINT16_MAX);
2229 if (port->no_listen)
2230 continue;
2232 if (port->is_unix_addr) {
2233 listensockaddr = (struct sockaddr *)
2234 create_unix_sockaddr(port->unix_addr,
2235 &address, &listensocklen);
2236 } else {
2237 listensockaddr = tor_malloc(sizeof(struct sockaddr_storage));
2238 listensocklen = tor_addr_to_sockaddr(&port->addr,
2239 real_port,
2240 listensockaddr,
2241 sizeof(struct sockaddr_storage));
2242 address = tor_dup_addr(&port->addr);
2245 if (listensockaddr) {
2246 conn = connection_listener_new(listensockaddr, listensocklen,
2247 port->type, address, port);
2248 tor_free(listensockaddr);
2249 tor_free(address);
2250 } else {
2251 conn = NULL;
2254 if (!conn) {
2255 r = -1;
2256 } else {
2257 if (new_conns)
2258 smartlist_add(new_conns, conn);
2260 } SMARTLIST_FOREACH_END(port);
2262 smartlist_free(launch);
2264 return r;
2267 /** Launch listeners for each port you should have open. Only launch
2268 * listeners who are not already open, and only close listeners we no longer
2269 * want.
2271 * Add all old conns that should be closed to <b>replaced_conns</b>.
2272 * Add all new connections to <b>new_conns</b>.
2274 * If <b>close_all_noncontrol</b> is true, then we only open control
2275 * listeners, and we close all other listeners.
2278 retry_all_listeners(smartlist_t *replaced_conns,
2279 smartlist_t *new_conns, int close_all_noncontrol)
2281 smartlist_t *listeners = smartlist_new();
2282 const or_options_t *options = get_options();
2283 int retval = 0;
2284 const uint16_t old_or_port = router_get_advertised_or_port(options);
2285 const uint16_t old_or_port_ipv6 =
2286 router_get_advertised_or_port_by_af(options,AF_INET6);
2287 const uint16_t old_dir_port = router_get_advertised_dir_port(options, 0);
2289 SMARTLIST_FOREACH_BEGIN(get_connection_array(), connection_t *, conn) {
2290 if (connection_is_listener(conn) && !conn->marked_for_close)
2291 smartlist_add(listeners, conn);
2292 } SMARTLIST_FOREACH_END(conn);
2294 if (retry_listener_ports(listeners,
2295 get_configured_ports(),
2296 new_conns,
2297 close_all_noncontrol) < 0)
2298 retval = -1;
2300 /* Any members that were still in 'listeners' don't correspond to
2301 * any configured port. Kill 'em. */
2302 SMARTLIST_FOREACH_BEGIN(listeners, connection_t *, conn) {
2303 log_notice(LD_NET, "Closing no-longer-configured %s on %s:%d",
2304 conn_type_to_string(conn->type), conn->address, conn->port);
2305 if (replaced_conns) {
2306 smartlist_add(replaced_conns, conn);
2307 } else {
2308 connection_close_immediate(conn);
2309 connection_mark_for_close(conn);
2311 } SMARTLIST_FOREACH_END(conn);
2313 smartlist_free(listeners);
2315 if (old_or_port != router_get_advertised_or_port(options) ||
2316 old_or_port_ipv6 != router_get_advertised_or_port_by_af(options,
2317 AF_INET6) ||
2318 old_dir_port != router_get_advertised_dir_port(options, 0)) {
2319 /* Our chosen ORPort or DirPort is not what it used to be: the
2320 * descriptor we had (if any) should be regenerated. (We won't
2321 * automatically notice this because of changes in the option,
2322 * since the value could be "auto".) */
2323 mark_my_descriptor_dirty("Chosen Or/DirPort changed");
2326 return retval;
2329 /** Mark every listener of type other than CONTROL_LISTENER to be closed. */
2330 void
2331 connection_mark_all_noncontrol_listeners(void)
2333 SMARTLIST_FOREACH_BEGIN(get_connection_array(), connection_t *, conn) {
2334 if (conn->marked_for_close)
2335 continue;
2336 if (conn->type == CONN_TYPE_CONTROL_LISTENER)
2337 continue;
2338 if (connection_is_listener(conn))
2339 connection_mark_for_close(conn);
2340 } SMARTLIST_FOREACH_END(conn);
2343 /** Mark every external connection not used for controllers for close. */
2344 void
2345 connection_mark_all_noncontrol_connections(void)
2347 SMARTLIST_FOREACH_BEGIN(get_connection_array(), connection_t *, conn) {
2348 if (conn->marked_for_close)
2349 continue;
2350 switch (conn->type) {
2351 case CONN_TYPE_CPUWORKER:
2352 case CONN_TYPE_CONTROL_LISTENER:
2353 case CONN_TYPE_CONTROL:
2354 break;
2355 case CONN_TYPE_AP:
2356 connection_mark_unattached_ap(TO_ENTRY_CONN(conn),
2357 END_STREAM_REASON_HIBERNATING);
2358 break;
2359 case CONN_TYPE_OR:
2361 or_connection_t *orconn = TO_OR_CONN(conn);
2362 if (orconn->chan) {
2363 connection_or_close_normally(orconn, 0);
2364 } else {
2366 * There should have been one, but mark for close and hope
2367 * for the best..
2369 connection_mark_for_close(conn);
2372 break;
2373 default:
2374 connection_mark_for_close(conn);
2375 break;
2377 } SMARTLIST_FOREACH_END(conn);
2380 /** Return 1 if we should apply rate limiting to <b>conn</b>, and 0
2381 * otherwise.
2382 * Right now this just checks if it's an internal IP address or an
2383 * internal connection. We also should, but don't, check if the connection
2384 * uses pluggable transports, since we should then limit it even if it
2385 * comes from an internal IP address. */
2386 static int
2387 connection_is_rate_limited(connection_t *conn)
2389 const or_options_t *options = get_options();
2390 if (conn->linked)
2391 return 0; /* Internal connection */
2392 else if (! options->CountPrivateBandwidth &&
2393 (tor_addr_family(&conn->addr) == AF_UNSPEC || /* no address */
2394 tor_addr_is_internal(&conn->addr, 0)))
2395 return 0; /* Internal address */
2396 else
2397 return 1;
2400 #ifdef USE_BUFFEREVENTS
2401 static struct bufferevent_rate_limit_group *global_rate_limit = NULL;
2402 #else
2403 extern int global_read_bucket, global_write_bucket;
2404 extern int global_relayed_read_bucket, global_relayed_write_bucket;
2406 /** Did either global write bucket run dry last second? If so,
2407 * we are likely to run dry again this second, so be stingy with the
2408 * tokens we just put in. */
2409 static int write_buckets_empty_last_second = 0;
2410 #endif
2412 /** How many seconds of no active local circuits will make the
2413 * connection revert to the "relayed" bandwidth class? */
2414 #define CLIENT_IDLE_TIME_FOR_PRIORITY 30
2416 #ifndef USE_BUFFEREVENTS
2417 /** Return 1 if <b>conn</b> should use tokens from the "relayed"
2418 * bandwidth rates, else 0. Currently, only OR conns with bandwidth
2419 * class 1, and directory conns that are serving data out, count.
2421 static int
2422 connection_counts_as_relayed_traffic(connection_t *conn, time_t now)
2424 if (conn->type == CONN_TYPE_OR &&
2425 connection_or_client_used(TO_OR_CONN(conn)) +
2426 CLIENT_IDLE_TIME_FOR_PRIORITY < now)
2427 return 1;
2428 if (conn->type == CONN_TYPE_DIR && DIR_CONN_IS_SERVER(conn))
2429 return 1;
2430 return 0;
2433 /** Helper function to decide how many bytes out of <b>global_bucket</b>
2434 * we're willing to use for this transaction. <b>base</b> is the size
2435 * of a cell on the network; <b>priority</b> says whether we should
2436 * write many of them or just a few; and <b>conn_bucket</b> (if
2437 * non-negative) provides an upper limit for our answer. */
2438 static ssize_t
2439 connection_bucket_round_robin(int base, int priority,
2440 ssize_t global_bucket, ssize_t conn_bucket)
2442 ssize_t at_most;
2443 ssize_t num_bytes_high = (priority ? 32 : 16) * base;
2444 ssize_t num_bytes_low = (priority ? 4 : 2) * base;
2446 /* Do a rudimentary round-robin so one circuit can't hog a connection.
2447 * Pick at most 32 cells, at least 4 cells if possible, and if we're in
2448 * the middle pick 1/8 of the available bandwidth. */
2449 at_most = global_bucket / 8;
2450 at_most -= (at_most % base); /* round down */
2451 if (at_most > num_bytes_high) /* 16 KB, or 8 KB for low-priority */
2452 at_most = num_bytes_high;
2453 else if (at_most < num_bytes_low) /* 2 KB, or 1 KB for low-priority */
2454 at_most = num_bytes_low;
2456 if (at_most > global_bucket)
2457 at_most = global_bucket;
2459 if (conn_bucket >= 0 && at_most > conn_bucket)
2460 at_most = conn_bucket;
2462 if (at_most < 0)
2463 return 0;
2464 return at_most;
2467 /** How many bytes at most can we read onto this connection? */
2468 static ssize_t
2469 connection_bucket_read_limit(connection_t *conn, time_t now)
2471 int base = RELAY_PAYLOAD_SIZE;
2472 int priority = conn->type != CONN_TYPE_DIR;
2473 int conn_bucket = -1;
2474 int global_bucket = global_read_bucket;
2476 if (connection_speaks_cells(conn)) {
2477 or_connection_t *or_conn = TO_OR_CONN(conn);
2478 if (conn->state == OR_CONN_STATE_OPEN)
2479 conn_bucket = or_conn->read_bucket;
2480 base = get_cell_network_size(or_conn->wide_circ_ids);
2483 if (!connection_is_rate_limited(conn)) {
2484 /* be willing to read on local conns even if our buckets are empty */
2485 return conn_bucket>=0 ? conn_bucket : 1<<14;
2488 if (connection_counts_as_relayed_traffic(conn, now) &&
2489 global_relayed_read_bucket <= global_read_bucket)
2490 global_bucket = global_relayed_read_bucket;
2492 return connection_bucket_round_robin(base, priority,
2493 global_bucket, conn_bucket);
2496 /** How many bytes at most can we write onto this connection? */
2497 ssize_t
2498 connection_bucket_write_limit(connection_t *conn, time_t now)
2500 int base = RELAY_PAYLOAD_SIZE;
2501 int priority = conn->type != CONN_TYPE_DIR;
2502 int conn_bucket = (int)conn->outbuf_flushlen;
2503 int global_bucket = global_write_bucket;
2505 if (!connection_is_rate_limited(conn)) {
2506 /* be willing to write to local conns even if our buckets are empty */
2507 return conn->outbuf_flushlen;
2510 if (connection_speaks_cells(conn)) {
2511 /* use the per-conn write limit if it's lower, but if it's less
2512 * than zero just use zero */
2513 or_connection_t *or_conn = TO_OR_CONN(conn);
2514 if (conn->state == OR_CONN_STATE_OPEN)
2515 if (or_conn->write_bucket < conn_bucket)
2516 conn_bucket = or_conn->write_bucket >= 0 ?
2517 or_conn->write_bucket : 0;
2518 base = get_cell_network_size(or_conn->wide_circ_ids);
2521 if (connection_counts_as_relayed_traffic(conn, now) &&
2522 global_relayed_write_bucket <= global_write_bucket)
2523 global_bucket = global_relayed_write_bucket;
2525 return connection_bucket_round_robin(base, priority,
2526 global_bucket, conn_bucket);
2528 #else
2529 static ssize_t
2530 connection_bucket_read_limit(connection_t *conn, time_t now)
2532 (void) now;
2533 return bufferevent_get_max_to_read(conn->bufev);
2535 ssize_t
2536 connection_bucket_write_limit(connection_t *conn, time_t now)
2538 (void) now;
2539 return bufferevent_get_max_to_write(conn->bufev);
2541 #endif
2543 /** Return 1 if the global write buckets are low enough that we
2544 * shouldn't send <b>attempt</b> bytes of low-priority directory stuff
2545 * out to <b>conn</b>. Else return 0.
2547 * Priority was 1 for v1 requests (directories and running-routers),
2548 * and 2 for v2 requests and later (statuses and descriptors).
2550 * There are a lot of parameters we could use here:
2551 * - global_relayed_write_bucket. Low is bad.
2552 * - global_write_bucket. Low is bad.
2553 * - bandwidthrate. Low is bad.
2554 * - bandwidthburst. Not a big factor?
2555 * - attempt. High is bad.
2556 * - total bytes queued on outbufs. High is bad. But I'm wary of
2557 * using this, since a few slow-flushing queues will pump up the
2558 * number without meaning what we meant to mean. What we really
2559 * mean is "total directory bytes added to outbufs recently", but
2560 * that's harder to quantify and harder to keep track of.
2563 global_write_bucket_low(connection_t *conn, size_t attempt, int priority)
2565 #ifdef USE_BUFFEREVENTS
2566 ssize_t smaller_bucket = bufferevent_get_max_to_write(conn->bufev);
2567 #else
2568 int smaller_bucket = global_write_bucket < global_relayed_write_bucket ?
2569 global_write_bucket : global_relayed_write_bucket;
2570 #endif
2571 if (authdir_mode(get_options()) && priority>1)
2572 return 0; /* there's always room to answer v2 if we're an auth dir */
2574 if (!connection_is_rate_limited(conn))
2575 return 0; /* local conns don't get limited */
2577 if (smaller_bucket < (int)attempt)
2578 return 1; /* not enough space no matter the priority */
2580 #ifndef USE_BUFFEREVENTS
2581 if (write_buckets_empty_last_second)
2582 return 1; /* we're already hitting our limits, no more please */
2583 #endif
2585 if (priority == 1) { /* old-style v1 query */
2586 /* Could we handle *two* of these requests within the next two seconds? */
2587 const or_options_t *options = get_options();
2588 int64_t can_write = (int64_t)smaller_bucket
2589 + 2*(options->RelayBandwidthRate ? options->RelayBandwidthRate :
2590 options->BandwidthRate);
2591 if (can_write < 2*(int64_t)attempt)
2592 return 1;
2593 } else { /* v2 query */
2594 /* no further constraints yet */
2596 return 0;
2599 /** Helper: adjusts our bandwidth history and informs the controller as
2600 * appropriate, given that we have just read <b>num_read</b> bytes and written
2601 * <b>num_written</b> bytes on <b>conn</b>. */
2602 static void
2603 record_num_bytes_transferred_impl(connection_t *conn,
2604 time_t now, size_t num_read, size_t num_written)
2606 /* Count bytes of answering direct and tunneled directory requests */
2607 if (conn->type == CONN_TYPE_DIR && conn->purpose == DIR_PURPOSE_SERVER) {
2608 if (num_read > 0)
2609 rep_hist_note_dir_bytes_read(num_read, now);
2610 if (num_written > 0)
2611 rep_hist_note_dir_bytes_written(num_written, now);
2614 if (!connection_is_rate_limited(conn))
2615 return; /* local IPs are free */
2617 if (conn->type == CONN_TYPE_OR)
2618 rep_hist_note_or_conn_bytes(conn->global_identifier, num_read,
2619 num_written, now);
2621 if (num_read > 0) {
2622 rep_hist_note_bytes_read(num_read, now);
2624 if (num_written > 0) {
2625 rep_hist_note_bytes_written(num_written, now);
2627 if (conn->type == CONN_TYPE_EXIT)
2628 rep_hist_note_exit_bytes(conn->port, num_written, num_read);
2631 #ifdef USE_BUFFEREVENTS
2632 /** Wrapper around fetch_from_(buf/evbuffer)_socks_client: see those functions
2633 * for documentation of its behavior. */
2634 static void
2635 record_num_bytes_transferred(connection_t *conn,
2636 time_t now, size_t num_read, size_t num_written)
2638 /* XXX024 check if this is necessary */
2639 if (num_written >= INT_MAX || num_read >= INT_MAX) {
2640 log_err(LD_BUG, "Value out of range. num_read=%lu, num_written=%lu, "
2641 "connection type=%s, state=%s",
2642 (unsigned long)num_read, (unsigned long)num_written,
2643 conn_type_to_string(conn->type),
2644 conn_state_to_string(conn->type, conn->state));
2645 if (num_written >= INT_MAX) num_written = 1;
2646 if (num_read >= INT_MAX) num_read = 1;
2647 tor_fragile_assert();
2650 record_num_bytes_transferred_impl(conn,now,num_read,num_written);
2652 #endif
2654 /** Helper: convert given <b>tvnow</b> time value to milliseconds since
2655 * midnight. */
2656 static uint32_t
2657 msec_since_midnight(const struct timeval *tvnow)
2659 return (uint32_t)(((tvnow->tv_sec % 86400L) * 1000L) +
2660 ((uint32_t)tvnow->tv_usec / (uint32_t)1000L));
2663 /** Helper: return the time in milliseconds since <b>last_empty_time</b>
2664 * when a bucket ran empty that previously had <b>tokens_before</b> tokens
2665 * now has <b>tokens_after</b> tokens after refilling at timestamp
2666 * <b>tvnow</b>, capped at <b>milliseconds_elapsed</b> milliseconds since
2667 * last refilling that bucket. Return 0 if the bucket has not been empty
2668 * since the last refill or has not been refilled. */
2669 uint32_t
2670 bucket_millis_empty(int tokens_before, uint32_t last_empty_time,
2671 int tokens_after, int milliseconds_elapsed,
2672 const struct timeval *tvnow)
2674 uint32_t result = 0, refilled;
2675 if (tokens_before <= 0 && tokens_after > tokens_before) {
2676 refilled = msec_since_midnight(tvnow);
2677 result = (uint32_t)((refilled + 86400L * 1000L - last_empty_time) %
2678 (86400L * 1000L));
2679 if (result > (uint32_t)milliseconds_elapsed)
2680 result = (uint32_t)milliseconds_elapsed;
2682 return result;
2685 /** Check if a bucket which had <b>tokens_before</b> tokens and which got
2686 * <b>tokens_removed</b> tokens removed at timestamp <b>tvnow</b> has run
2687 * out of tokens, and if so, note the milliseconds since midnight in
2688 * <b>timestamp_var</b> for the next TB_EMPTY event. */
2689 void
2690 connection_buckets_note_empty_ts(uint32_t *timestamp_var,
2691 int tokens_before, size_t tokens_removed,
2692 const struct timeval *tvnow)
2694 if (tokens_before > 0 && (uint32_t)tokens_before <= tokens_removed)
2695 *timestamp_var = msec_since_midnight(tvnow);
2698 #ifndef USE_BUFFEREVENTS
2699 /** Last time at which the global or relay buckets were emptied in msec
2700 * since midnight. */
2701 static uint32_t global_relayed_read_emptied = 0,
2702 global_relayed_write_emptied = 0,
2703 global_read_emptied = 0,
2704 global_write_emptied = 0;
2706 /** We just read <b>num_read</b> and wrote <b>num_written</b> bytes
2707 * onto <b>conn</b>. Decrement buckets appropriately. */
2708 static void
2709 connection_buckets_decrement(connection_t *conn, time_t now,
2710 size_t num_read, size_t num_written)
2712 if (num_written >= INT_MAX || num_read >= INT_MAX) {
2713 log_err(LD_BUG, "Value out of range. num_read=%lu, num_written=%lu, "
2714 "connection type=%s, state=%s",
2715 (unsigned long)num_read, (unsigned long)num_written,
2716 conn_type_to_string(conn->type),
2717 conn_state_to_string(conn->type, conn->state));
2718 if (num_written >= INT_MAX) num_written = 1;
2719 if (num_read >= INT_MAX) num_read = 1;
2720 tor_fragile_assert();
2723 record_num_bytes_transferred_impl(conn, now, num_read, num_written);
2725 if (!connection_is_rate_limited(conn))
2726 return; /* local IPs are free */
2728 /* If one or more of our token buckets ran dry just now, note the
2729 * timestamp for TB_EMPTY events. */
2730 if (get_options()->TestingEnableTbEmptyEvent) {
2731 struct timeval tvnow;
2732 tor_gettimeofday_cached(&tvnow);
2733 if (connection_counts_as_relayed_traffic(conn, now)) {
2734 connection_buckets_note_empty_ts(&global_relayed_read_emptied,
2735 global_relayed_read_bucket, num_read, &tvnow);
2736 connection_buckets_note_empty_ts(&global_relayed_write_emptied,
2737 global_relayed_write_bucket, num_written, &tvnow);
2739 connection_buckets_note_empty_ts(&global_read_emptied,
2740 global_read_bucket, num_read, &tvnow);
2741 connection_buckets_note_empty_ts(&global_write_emptied,
2742 global_write_bucket, num_written, &tvnow);
2743 if (connection_speaks_cells(conn) && conn->state == OR_CONN_STATE_OPEN) {
2744 or_connection_t *or_conn = TO_OR_CONN(conn);
2745 connection_buckets_note_empty_ts(&or_conn->read_emptied_time,
2746 or_conn->read_bucket, num_read, &tvnow);
2747 connection_buckets_note_empty_ts(&or_conn->write_emptied_time,
2748 or_conn->write_bucket, num_written, &tvnow);
2752 if (connection_counts_as_relayed_traffic(conn, now)) {
2753 global_relayed_read_bucket -= (int)num_read;
2754 global_relayed_write_bucket -= (int)num_written;
2756 global_read_bucket -= (int)num_read;
2757 global_write_bucket -= (int)num_written;
2758 if (connection_speaks_cells(conn) && conn->state == OR_CONN_STATE_OPEN) {
2759 TO_OR_CONN(conn)->read_bucket -= (int)num_read;
2760 TO_OR_CONN(conn)->write_bucket -= (int)num_written;
2764 /** If we have exhausted our global buckets, or the buckets for conn,
2765 * stop reading. */
2766 static void
2767 connection_consider_empty_read_buckets(connection_t *conn)
2769 const char *reason;
2771 if (!connection_is_rate_limited(conn))
2772 return; /* Always okay. */
2774 if (global_read_bucket <= 0) {
2775 reason = "global read bucket exhausted. Pausing.";
2776 } else if (connection_counts_as_relayed_traffic(conn, approx_time()) &&
2777 global_relayed_read_bucket <= 0) {
2778 reason = "global relayed read bucket exhausted. Pausing.";
2779 } else if (connection_speaks_cells(conn) &&
2780 conn->state == OR_CONN_STATE_OPEN &&
2781 TO_OR_CONN(conn)->read_bucket <= 0) {
2782 reason = "connection read bucket exhausted. Pausing.";
2783 } else
2784 return; /* all good, no need to stop it */
2786 LOG_FN_CONN(conn, (LOG_DEBUG, LD_NET, "%s", reason));
2787 conn->read_blocked_on_bw = 1;
2788 connection_stop_reading(conn);
2791 /** If we have exhausted our global buckets, or the buckets for conn,
2792 * stop writing. */
2793 static void
2794 connection_consider_empty_write_buckets(connection_t *conn)
2796 const char *reason;
2798 if (!connection_is_rate_limited(conn))
2799 return; /* Always okay. */
2801 if (global_write_bucket <= 0) {
2802 reason = "global write bucket exhausted. Pausing.";
2803 } else if (connection_counts_as_relayed_traffic(conn, approx_time()) &&
2804 global_relayed_write_bucket <= 0) {
2805 reason = "global relayed write bucket exhausted. Pausing.";
2806 } else if (connection_speaks_cells(conn) &&
2807 conn->state == OR_CONN_STATE_OPEN &&
2808 TO_OR_CONN(conn)->write_bucket <= 0) {
2809 reason = "connection write bucket exhausted. Pausing.";
2810 } else
2811 return; /* all good, no need to stop it */
2813 LOG_FN_CONN(conn, (LOG_DEBUG, LD_NET, "%s", reason));
2814 conn->write_blocked_on_bw = 1;
2815 connection_stop_writing(conn);
2818 /** Initialize the global read bucket to options-\>BandwidthBurst. */
2819 void
2820 connection_bucket_init(void)
2822 const or_options_t *options = get_options();
2823 /* start it at max traffic */
2824 global_read_bucket = (int)options->BandwidthBurst;
2825 global_write_bucket = (int)options->BandwidthBurst;
2826 if (options->RelayBandwidthRate) {
2827 global_relayed_read_bucket = (int)options->RelayBandwidthBurst;
2828 global_relayed_write_bucket = (int)options->RelayBandwidthBurst;
2829 } else {
2830 global_relayed_read_bucket = (int)options->BandwidthBurst;
2831 global_relayed_write_bucket = (int)options->BandwidthBurst;
2835 /** Refill a single <b>bucket</b> called <b>name</b> with bandwidth rate per
2836 * second <b>rate</b> and bandwidth burst <b>burst</b>, assuming that
2837 * <b>milliseconds_elapsed</b> milliseconds have passed since the last
2838 * call. */
2839 static void
2840 connection_bucket_refill_helper(int *bucket, int rate, int burst,
2841 int milliseconds_elapsed,
2842 const char *name)
2844 int starting_bucket = *bucket;
2845 if (starting_bucket < burst && milliseconds_elapsed > 0) {
2846 int64_t incr = (((int64_t)rate) * milliseconds_elapsed) / 1000;
2847 if ((burst - starting_bucket) < incr) {
2848 *bucket = burst; /* We would overflow the bucket; just set it to
2849 * the maximum. */
2850 } else {
2851 *bucket += (int)incr;
2852 if (*bucket > burst || *bucket < starting_bucket) {
2853 /* If we overflow the burst, or underflow our starting bucket,
2854 * cap the bucket value to burst. */
2855 /* XXXX this might be redundant now, but it doesn't show up
2856 * in profiles. Remove it after analysis. */
2857 *bucket = burst;
2860 log_debug(LD_NET,"%s now %d.", name, *bucket);
2864 /** Time has passed; increment buckets appropriately. */
2865 void
2866 connection_bucket_refill(int milliseconds_elapsed, time_t now)
2868 const or_options_t *options = get_options();
2869 smartlist_t *conns = get_connection_array();
2870 int bandwidthrate, bandwidthburst, relayrate, relayburst;
2872 int prev_global_read = global_read_bucket;
2873 int prev_global_write = global_write_bucket;
2874 int prev_relay_read = global_relayed_read_bucket;
2875 int prev_relay_write = global_relayed_write_bucket;
2876 struct timeval tvnow; /*< Only used if TB_EMPTY events are enabled. */
2878 bandwidthrate = (int)options->BandwidthRate;
2879 bandwidthburst = (int)options->BandwidthBurst;
2881 if (options->RelayBandwidthRate) {
2882 relayrate = (int)options->RelayBandwidthRate;
2883 relayburst = (int)options->RelayBandwidthBurst;
2884 } else {
2885 relayrate = bandwidthrate;
2886 relayburst = bandwidthburst;
2889 tor_assert(milliseconds_elapsed >= 0);
2891 write_buckets_empty_last_second =
2892 global_relayed_write_bucket <= 0 || global_write_bucket <= 0;
2894 /* refill the global buckets */
2895 connection_bucket_refill_helper(&global_read_bucket,
2896 bandwidthrate, bandwidthburst,
2897 milliseconds_elapsed,
2898 "global_read_bucket");
2899 connection_bucket_refill_helper(&global_write_bucket,
2900 bandwidthrate, bandwidthburst,
2901 milliseconds_elapsed,
2902 "global_write_bucket");
2903 connection_bucket_refill_helper(&global_relayed_read_bucket,
2904 relayrate, relayburst,
2905 milliseconds_elapsed,
2906 "global_relayed_read_bucket");
2907 connection_bucket_refill_helper(&global_relayed_write_bucket,
2908 relayrate, relayburst,
2909 milliseconds_elapsed,
2910 "global_relayed_write_bucket");
2912 /* If buckets were empty before and have now been refilled, tell any
2913 * interested controllers. */
2914 if (get_options()->TestingEnableTbEmptyEvent) {
2915 uint32_t global_read_empty_time, global_write_empty_time,
2916 relay_read_empty_time, relay_write_empty_time;
2917 tor_gettimeofday_cached(&tvnow);
2918 global_read_empty_time = bucket_millis_empty(prev_global_read,
2919 global_read_emptied, global_read_bucket,
2920 milliseconds_elapsed, &tvnow);
2921 global_write_empty_time = bucket_millis_empty(prev_global_write,
2922 global_write_emptied, global_write_bucket,
2923 milliseconds_elapsed, &tvnow);
2924 control_event_tb_empty("GLOBAL", global_read_empty_time,
2925 global_write_empty_time, milliseconds_elapsed);
2926 relay_read_empty_time = bucket_millis_empty(prev_relay_read,
2927 global_relayed_read_emptied,
2928 global_relayed_read_bucket,
2929 milliseconds_elapsed, &tvnow);
2930 relay_write_empty_time = bucket_millis_empty(prev_relay_write,
2931 global_relayed_write_emptied,
2932 global_relayed_write_bucket,
2933 milliseconds_elapsed, &tvnow);
2934 control_event_tb_empty("RELAY", relay_read_empty_time,
2935 relay_write_empty_time, milliseconds_elapsed);
2938 /* refill the per-connection buckets */
2939 SMARTLIST_FOREACH_BEGIN(conns, connection_t *, conn) {
2940 if (connection_speaks_cells(conn)) {
2941 or_connection_t *or_conn = TO_OR_CONN(conn);
2942 int orbandwidthrate = or_conn->bandwidthrate;
2943 int orbandwidthburst = or_conn->bandwidthburst;
2945 int prev_conn_read = or_conn->read_bucket;
2946 int prev_conn_write = or_conn->write_bucket;
2948 if (connection_bucket_should_increase(or_conn->read_bucket, or_conn)) {
2949 connection_bucket_refill_helper(&or_conn->read_bucket,
2950 orbandwidthrate,
2951 orbandwidthburst,
2952 milliseconds_elapsed,
2953 "or_conn->read_bucket");
2955 if (connection_bucket_should_increase(or_conn->write_bucket, or_conn)) {
2956 connection_bucket_refill_helper(&or_conn->write_bucket,
2957 orbandwidthrate,
2958 orbandwidthburst,
2959 milliseconds_elapsed,
2960 "or_conn->write_bucket");
2963 /* If buckets were empty before and have now been refilled, tell any
2964 * interested controllers. */
2965 if (get_options()->TestingEnableTbEmptyEvent) {
2966 char *bucket;
2967 uint32_t conn_read_empty_time, conn_write_empty_time;
2968 tor_asprintf(&bucket, "ORCONN ID="U64_FORMAT,
2969 U64_PRINTF_ARG(or_conn->base_.global_identifier));
2970 conn_read_empty_time = bucket_millis_empty(prev_conn_read,
2971 or_conn->read_emptied_time,
2972 or_conn->read_bucket,
2973 milliseconds_elapsed, &tvnow);
2974 conn_write_empty_time = bucket_millis_empty(prev_conn_write,
2975 or_conn->write_emptied_time,
2976 or_conn->write_bucket,
2977 milliseconds_elapsed, &tvnow);
2978 control_event_tb_empty(bucket, conn_read_empty_time,
2979 conn_write_empty_time,
2980 milliseconds_elapsed);
2981 tor_free(bucket);
2985 if (conn->read_blocked_on_bw == 1 /* marked to turn reading back on now */
2986 && global_read_bucket > 0 /* and we're allowed to read */
2987 && (!connection_counts_as_relayed_traffic(conn, now) ||
2988 global_relayed_read_bucket > 0) /* even if we're relayed traffic */
2989 && (!connection_speaks_cells(conn) ||
2990 conn->state != OR_CONN_STATE_OPEN ||
2991 TO_OR_CONN(conn)->read_bucket > 0)) {
2992 /* and either a non-cell conn or a cell conn with non-empty bucket */
2993 LOG_FN_CONN(conn, (LOG_DEBUG,LD_NET,
2994 "waking up conn (fd %d) for read", (int)conn->s));
2995 conn->read_blocked_on_bw = 0;
2996 connection_start_reading(conn);
2999 if (conn->write_blocked_on_bw == 1
3000 && global_write_bucket > 0 /* and we're allowed to write */
3001 && (!connection_counts_as_relayed_traffic(conn, now) ||
3002 global_relayed_write_bucket > 0) /* even if it's relayed traffic */
3003 && (!connection_speaks_cells(conn) ||
3004 conn->state != OR_CONN_STATE_OPEN ||
3005 TO_OR_CONN(conn)->write_bucket > 0)) {
3006 LOG_FN_CONN(conn, (LOG_DEBUG,LD_NET,
3007 "waking up conn (fd %d) for write", (int)conn->s));
3008 conn->write_blocked_on_bw = 0;
3009 connection_start_writing(conn);
3011 } SMARTLIST_FOREACH_END(conn);
3014 /** Is the <b>bucket</b> for connection <b>conn</b> low enough that we
3015 * should add another pile of tokens to it?
3017 static int
3018 connection_bucket_should_increase(int bucket, or_connection_t *conn)
3020 tor_assert(conn);
3022 if (conn->base_.state != OR_CONN_STATE_OPEN)
3023 return 0; /* only open connections play the rate limiting game */
3024 if (bucket >= conn->bandwidthburst)
3025 return 0;
3027 return 1;
3029 #else
3030 static void
3031 connection_buckets_decrement(connection_t *conn, time_t now,
3032 size_t num_read, size_t num_written)
3034 (void) conn;
3035 (void) now;
3036 (void) num_read;
3037 (void) num_written;
3038 /* Libevent does this for us. */
3041 void
3042 connection_bucket_refill(int seconds_elapsed, time_t now)
3044 (void) seconds_elapsed;
3045 (void) now;
3046 /* Libevent does this for us. */
3048 void
3049 connection_bucket_init(void)
3051 const or_options_t *options = get_options();
3052 const struct timeval *tick = tor_libevent_get_one_tick_timeout();
3053 struct ev_token_bucket_cfg *bucket_cfg;
3055 uint64_t rate, burst;
3056 if (options->RelayBandwidthRate) {
3057 rate = options->RelayBandwidthRate;
3058 burst = options->RelayBandwidthBurst;
3059 } else {
3060 rate = options->BandwidthRate;
3061 burst = options->BandwidthBurst;
3064 /* This can't overflow, since TokenBucketRefillInterval <= 1000,
3065 * and rate started out less than INT32_MAX. */
3066 rate = (rate * options->TokenBucketRefillInterval) / 1000;
3068 bucket_cfg = ev_token_bucket_cfg_new((uint32_t)rate, (uint32_t)burst,
3069 (uint32_t)rate, (uint32_t)burst,
3070 tick);
3072 if (!global_rate_limit) {
3073 global_rate_limit =
3074 bufferevent_rate_limit_group_new(tor_libevent_get_base(), bucket_cfg);
3075 } else {
3076 bufferevent_rate_limit_group_set_cfg(global_rate_limit, bucket_cfg);
3078 ev_token_bucket_cfg_free(bucket_cfg);
3081 void
3082 connection_get_rate_limit_totals(uint64_t *read_out, uint64_t *written_out)
3084 if (global_rate_limit == NULL) {
3085 *read_out = *written_out = 0;
3086 } else {
3087 bufferevent_rate_limit_group_get_totals(
3088 global_rate_limit, read_out, written_out);
3092 /** Perform whatever operations are needed on <b>conn</b> to enable
3093 * rate-limiting. */
3094 void
3095 connection_enable_rate_limiting(connection_t *conn)
3097 if (conn->bufev) {
3098 if (!global_rate_limit)
3099 connection_bucket_init();
3100 tor_add_bufferevent_to_rate_limit_group(conn->bufev, global_rate_limit);
3104 static void
3105 connection_consider_empty_write_buckets(connection_t *conn)
3107 (void) conn;
3109 static void
3110 connection_consider_empty_read_buckets(connection_t *conn)
3112 (void) conn;
3114 #endif
3116 /** Read bytes from conn-\>s and process them.
3118 * It calls connection_read_to_buf() to bring in any new bytes,
3119 * and then calls connection_process_inbuf() to process them.
3121 * Mark the connection and return -1 if you want to close it, else
3122 * return 0.
3124 static int
3125 connection_handle_read_impl(connection_t *conn)
3127 ssize_t max_to_read=-1, try_to_read;
3128 size_t before, n_read = 0;
3129 int socket_error = 0;
3131 if (conn->marked_for_close)
3132 return 0; /* do nothing */
3134 conn->timestamp_lastread = approx_time();
3136 switch (conn->type) {
3137 case CONN_TYPE_OR_LISTENER:
3138 return connection_handle_listener_read(conn, CONN_TYPE_OR);
3139 case CONN_TYPE_EXT_OR_LISTENER:
3140 return connection_handle_listener_read(conn, CONN_TYPE_EXT_OR);
3141 case CONN_TYPE_AP_LISTENER:
3142 case CONN_TYPE_AP_TRANS_LISTENER:
3143 case CONN_TYPE_AP_NATD_LISTENER:
3144 return connection_handle_listener_read(conn, CONN_TYPE_AP);
3145 case CONN_TYPE_DIR_LISTENER:
3146 return connection_handle_listener_read(conn, CONN_TYPE_DIR);
3147 case CONN_TYPE_CONTROL_LISTENER:
3148 return connection_handle_listener_read(conn, CONN_TYPE_CONTROL);
3149 case CONN_TYPE_AP_DNS_LISTENER:
3150 /* This should never happen; eventdns.c handles the reads here. */
3151 tor_fragile_assert();
3152 return 0;
3155 loop_again:
3156 try_to_read = max_to_read;
3157 tor_assert(!conn->marked_for_close);
3159 before = buf_datalen(conn->inbuf);
3160 if (connection_read_to_buf(conn, &max_to_read, &socket_error) < 0) {
3161 /* There's a read error; kill the connection.*/
3162 if (conn->type == CONN_TYPE_OR) {
3163 connection_or_notify_error(TO_OR_CONN(conn),
3164 socket_error != 0 ?
3165 errno_to_orconn_end_reason(socket_error) :
3166 END_OR_CONN_REASON_CONNRESET,
3167 socket_error != 0 ?
3168 tor_socket_strerror(socket_error) :
3169 "(unknown, errno was 0)");
3171 if (CONN_IS_EDGE(conn)) {
3172 edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
3173 connection_edge_end_errno(edge_conn);
3174 if (conn->type == CONN_TYPE_AP && TO_ENTRY_CONN(conn)->socks_request) {
3175 /* broken, don't send a socks reply back */
3176 TO_ENTRY_CONN(conn)->socks_request->has_finished = 1;
3179 connection_close_immediate(conn); /* Don't flush; connection is dead. */
3181 * This can bypass normal channel checking since we did
3182 * connection_or_notify_error() above.
3184 connection_mark_for_close_internal(conn);
3185 return -1;
3187 n_read += buf_datalen(conn->inbuf) - before;
3188 if (CONN_IS_EDGE(conn) && try_to_read != max_to_read) {
3189 /* instruct it not to try to package partial cells. */
3190 if (connection_process_inbuf(conn, 0) < 0) {
3191 return -1;
3193 if (!conn->marked_for_close &&
3194 connection_is_reading(conn) &&
3195 !conn->inbuf_reached_eof &&
3196 max_to_read > 0)
3197 goto loop_again; /* try reading again, in case more is here now */
3199 /* one last try, packaging partial cells and all. */
3200 if (!conn->marked_for_close &&
3201 connection_process_inbuf(conn, 1) < 0) {
3202 return -1;
3204 if (conn->linked_conn) {
3205 /* The other side's handle_write() will never actually get called, so
3206 * we need to invoke the appropriate callbacks ourself. */
3207 connection_t *linked = conn->linked_conn;
3209 if (n_read) {
3210 /* Probably a no-op, since linked conns typically don't count for
3211 * bandwidth rate limiting. But do it anyway so we can keep stats
3212 * accurately. Note that since we read the bytes from conn, and
3213 * we're writing the bytes onto the linked connection, we count
3214 * these as <i>written</i> bytes. */
3215 connection_buckets_decrement(linked, approx_time(), 0, n_read);
3217 if (connection_flushed_some(linked) < 0)
3218 connection_mark_for_close(linked);
3219 if (!connection_wants_to_flush(linked))
3220 connection_finished_flushing(linked);
3223 if (!buf_datalen(linked->outbuf) && conn->active_on_link)
3224 connection_stop_reading_from_linked_conn(conn);
3226 /* If we hit the EOF, call connection_reached_eof(). */
3227 if (!conn->marked_for_close &&
3228 conn->inbuf_reached_eof &&
3229 connection_reached_eof(conn) < 0) {
3230 return -1;
3232 return 0;
3235 /* DOCDOC connection_handle_read */
3237 connection_handle_read(connection_t *conn)
3239 int res;
3241 tor_gettimeofday_cache_clear();
3242 res = connection_handle_read_impl(conn);
3243 return res;
3246 /** Pull in new bytes from conn-\>s or conn-\>linked_conn onto conn-\>inbuf,
3247 * either directly or via TLS. Reduce the token buckets by the number of bytes
3248 * read.
3250 * If *max_to_read is -1, then decide it ourselves, else go with the
3251 * value passed to us. When returning, if it's changed, subtract the
3252 * number of bytes we read from *max_to_read.
3254 * Return -1 if we want to break conn, else return 0.
3256 static int
3257 connection_read_to_buf(connection_t *conn, ssize_t *max_to_read,
3258 int *socket_error)
3260 int result;
3261 ssize_t at_most = *max_to_read;
3262 size_t slack_in_buf, more_to_read;
3263 size_t n_read = 0, n_written = 0;
3265 if (at_most == -1) { /* we need to initialize it */
3266 /* how many bytes are we allowed to read? */
3267 at_most = connection_bucket_read_limit(conn, approx_time());
3270 slack_in_buf = buf_slack(conn->inbuf);
3271 again:
3272 if ((size_t)at_most > slack_in_buf && slack_in_buf >= 1024) {
3273 more_to_read = at_most - slack_in_buf;
3274 at_most = slack_in_buf;
3275 } else {
3276 more_to_read = 0;
3279 if (connection_speaks_cells(conn) &&
3280 conn->state > OR_CONN_STATE_PROXY_HANDSHAKING) {
3281 int pending;
3282 or_connection_t *or_conn = TO_OR_CONN(conn);
3283 size_t initial_size;
3284 if (conn->state == OR_CONN_STATE_TLS_HANDSHAKING ||
3285 conn->state == OR_CONN_STATE_TLS_CLIENT_RENEGOTIATING) {
3286 /* continue handshaking even if global token bucket is empty */
3287 return connection_tls_continue_handshake(or_conn);
3290 log_debug(LD_NET,
3291 "%d: starting, inbuf_datalen %ld (%d pending in tls object)."
3292 " at_most %ld.",
3293 (int)conn->s,(long)buf_datalen(conn->inbuf),
3294 tor_tls_get_pending_bytes(or_conn->tls), (long)at_most);
3296 initial_size = buf_datalen(conn->inbuf);
3297 /* else open, or closing */
3298 result = read_to_buf_tls(or_conn->tls, at_most, conn->inbuf);
3299 if (TOR_TLS_IS_ERROR(result) || result == TOR_TLS_CLOSE)
3300 or_conn->tls_error = result;
3301 else
3302 or_conn->tls_error = 0;
3304 switch (result) {
3305 case TOR_TLS_CLOSE:
3306 case TOR_TLS_ERROR_IO:
3307 log_debug(LD_NET,"TLS connection closed %son read. Closing. "
3308 "(Nickname %s, address %s)",
3309 result == TOR_TLS_CLOSE ? "cleanly " : "",
3310 or_conn->nickname ? or_conn->nickname : "not set",
3311 conn->address);
3312 return result;
3313 CASE_TOR_TLS_ERROR_ANY_NONIO:
3314 log_debug(LD_NET,"tls error [%s]. breaking (nickname %s, address %s).",
3315 tor_tls_err_to_string(result),
3316 or_conn->nickname ? or_conn->nickname : "not set",
3317 conn->address);
3318 return result;
3319 case TOR_TLS_WANTWRITE:
3320 connection_start_writing(conn);
3321 return 0;
3322 case TOR_TLS_WANTREAD:
3323 if (conn->in_connection_handle_write) {
3324 /* We've been invoked from connection_handle_write, because we're
3325 * waiting for a TLS renegotiation, the renegotiation started, and
3326 * SSL_read returned WANTWRITE. But now SSL_read is saying WANTREAD
3327 * again. Stop waiting for write events now, or else we'll
3328 * busy-loop until data arrives for us to read. */
3329 connection_stop_writing(conn);
3330 if (!connection_is_reading(conn))
3331 connection_start_reading(conn);
3333 /* we're already reading, one hopes */
3334 result = 0;
3335 break;
3336 case TOR_TLS_DONE: /* no data read, so nothing to process */
3337 result = 0;
3338 break; /* so we call bucket_decrement below */
3339 default:
3340 break;
3342 pending = tor_tls_get_pending_bytes(or_conn->tls);
3343 if (pending) {
3344 /* If we have any pending bytes, we read them now. This *can*
3345 * take us over our read allotment, but really we shouldn't be
3346 * believing that SSL bytes are the same as TCP bytes anyway. */
3347 int r2 = read_to_buf_tls(or_conn->tls, pending, conn->inbuf);
3348 if (r2<0) {
3349 log_warn(LD_BUG, "apparently, reading pending bytes can fail.");
3350 return -1;
3353 result = (int)(buf_datalen(conn->inbuf)-initial_size);
3354 tor_tls_get_n_raw_bytes(or_conn->tls, &n_read, &n_written);
3355 log_debug(LD_GENERAL, "After TLS read of %d: %ld read, %ld written",
3356 result, (long)n_read, (long)n_written);
3357 } else if (conn->linked) {
3358 if (conn->linked_conn) {
3359 result = move_buf_to_buf(conn->inbuf, conn->linked_conn->outbuf,
3360 &conn->linked_conn->outbuf_flushlen);
3361 } else {
3362 result = 0;
3364 //log_notice(LD_GENERAL, "Moved %d bytes on an internal link!", result);
3365 /* If the other side has disappeared, or if it's been marked for close and
3366 * we flushed its outbuf, then we should set our inbuf_reached_eof. */
3367 if (!conn->linked_conn ||
3368 (conn->linked_conn->marked_for_close &&
3369 buf_datalen(conn->linked_conn->outbuf) == 0))
3370 conn->inbuf_reached_eof = 1;
3372 n_read = (size_t) result;
3373 } else {
3374 /* !connection_speaks_cells, !conn->linked_conn. */
3375 int reached_eof = 0;
3376 CONN_LOG_PROTECT(conn,
3377 result = read_to_buf(conn->s, at_most, conn->inbuf, &reached_eof,
3378 socket_error));
3379 if (reached_eof)
3380 conn->inbuf_reached_eof = 1;
3382 // log_fn(LOG_DEBUG,"read_to_buf returned %d.",read_result);
3384 if (result < 0)
3385 return -1;
3386 n_read = (size_t) result;
3389 if (n_read > 0) {
3390 /* change *max_to_read */
3391 *max_to_read = at_most - n_read;
3393 /* Update edge_conn->n_read and ocirc->n_read_circ_bw */
3394 if (conn->type == CONN_TYPE_AP) {
3395 edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
3396 circuit_t *circ = circuit_get_by_edge_conn(edge_conn);
3397 origin_circuit_t *ocirc;
3399 /* Check for overflow: */
3400 if (PREDICT_LIKELY(UINT32_MAX - edge_conn->n_read > n_read))
3401 edge_conn->n_read += (int)n_read;
3402 else
3403 edge_conn->n_read = UINT32_MAX;
3405 if (circ && CIRCUIT_IS_ORIGIN(circ)) {
3406 ocirc = TO_ORIGIN_CIRCUIT(circ);
3407 if (PREDICT_LIKELY(UINT32_MAX - ocirc->n_read_circ_bw > n_read))
3408 ocirc->n_read_circ_bw += (int)n_read;
3409 else
3410 ocirc->n_read_circ_bw = UINT32_MAX;
3414 /* If CONN_BW events are enabled, update conn->n_read_conn_bw for
3415 * OR/DIR/EXIT connections, checking for overflow. */
3416 if (get_options()->TestingEnableConnBwEvent &&
3417 (conn->type == CONN_TYPE_OR ||
3418 conn->type == CONN_TYPE_DIR ||
3419 conn->type == CONN_TYPE_EXIT)) {
3420 if (PREDICT_LIKELY(UINT32_MAX - conn->n_read_conn_bw > n_read))
3421 conn->n_read_conn_bw += (int)n_read;
3422 else
3423 conn->n_read_conn_bw = UINT32_MAX;
3427 connection_buckets_decrement(conn, approx_time(), n_read, n_written);
3429 if (more_to_read && result == at_most) {
3430 slack_in_buf = buf_slack(conn->inbuf);
3431 at_most = more_to_read;
3432 goto again;
3435 /* Call even if result is 0, since the global read bucket may
3436 * have reached 0 on a different conn, and this guy needs to
3437 * know to stop reading. */
3438 connection_consider_empty_read_buckets(conn);
3439 if (n_written > 0 && connection_is_writing(conn))
3440 connection_consider_empty_write_buckets(conn);
3442 return 0;
3445 #ifdef USE_BUFFEREVENTS
3446 /* XXXX These generic versions could be simplified by making them
3447 type-specific */
3449 /** Callback: Invoked whenever bytes are added to or drained from an input
3450 * evbuffer. Used to track the number of bytes read. */
3451 static void
3452 evbuffer_inbuf_callback(struct evbuffer *buf,
3453 const struct evbuffer_cb_info *info, void *arg)
3455 connection_t *conn = arg;
3456 (void) buf;
3457 /* XXXX These need to get real counts on the non-nested TLS case. - NM */
3458 if (info->n_added) {
3459 time_t now = approx_time();
3460 conn->timestamp_lastread = now;
3461 record_num_bytes_transferred(conn, now, info->n_added, 0);
3462 connection_consider_empty_read_buckets(conn);
3463 if (conn->type == CONN_TYPE_AP) {
3464 edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
3465 /*XXXX024 check for overflow*/
3466 edge_conn->n_read += (int)info->n_added;
3471 /** Callback: Invoked whenever bytes are added to or drained from an output
3472 * evbuffer. Used to track the number of bytes written. */
3473 static void
3474 evbuffer_outbuf_callback(struct evbuffer *buf,
3475 const struct evbuffer_cb_info *info, void *arg)
3477 connection_t *conn = arg;
3478 (void)buf;
3479 if (info->n_deleted) {
3480 time_t now = approx_time();
3481 conn->timestamp_lastwritten = now;
3482 record_num_bytes_transferred(conn, now, 0, info->n_deleted);
3483 connection_consider_empty_write_buckets(conn);
3484 if (conn->type == CONN_TYPE_AP) {
3485 edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
3486 /*XXXX024 check for overflow*/
3487 edge_conn->n_written += (int)info->n_deleted;
3492 /** Callback: invoked whenever a bufferevent has read data. */
3493 void
3494 connection_handle_read_cb(struct bufferevent *bufev, void *arg)
3496 connection_t *conn = arg;
3497 (void) bufev;
3498 if (!conn->marked_for_close) {
3499 if (connection_process_inbuf(conn, 1)<0) /* XXXX Always 1? */
3500 if (!conn->marked_for_close)
3501 connection_mark_for_close(conn);
3505 /** Callback: invoked whenever a bufferevent has written data. */
3506 void
3507 connection_handle_write_cb(struct bufferevent *bufev, void *arg)
3509 connection_t *conn = arg;
3510 struct evbuffer *output;
3511 if (connection_flushed_some(conn)<0) {
3512 if (!conn->marked_for_close)
3513 connection_mark_for_close(conn);
3514 return;
3517 output = bufferevent_get_output(bufev);
3518 if (!evbuffer_get_length(output)) {
3519 connection_finished_flushing(conn);
3520 if (conn->marked_for_close && conn->hold_open_until_flushed) {
3521 conn->hold_open_until_flushed = 0;
3522 if (conn->linked) {
3523 /* send eof */
3524 bufferevent_flush(conn->bufev, EV_WRITE, BEV_FINISHED);
3530 /** Callback: invoked whenever a bufferevent has had an event (like a
3531 * connection, or an eof, or an error) occur. */
3532 void
3533 connection_handle_event_cb(struct bufferevent *bufev, short event, void *arg)
3535 connection_t *conn = arg;
3536 (void) bufev;
3537 if (conn->marked_for_close)
3538 return;
3540 if (event & BEV_EVENT_CONNECTED) {
3541 tor_assert(connection_state_is_connecting(conn));
3542 if (connection_finished_connecting(conn)<0)
3543 return;
3545 if (event & BEV_EVENT_EOF) {
3546 if (!conn->marked_for_close) {
3547 conn->inbuf_reached_eof = 1;
3548 if (connection_reached_eof(conn)<0)
3549 return;
3552 if (event & BEV_EVENT_ERROR) {
3553 int socket_error = evutil_socket_geterror(conn->s);
3554 if (conn->type == CONN_TYPE_OR &&
3555 conn->state == OR_CONN_STATE_CONNECTING) {
3556 connection_or_connect_failed(TO_OR_CONN(conn),
3557 errno_to_orconn_end_reason(socket_error),
3558 tor_socket_strerror(socket_error));
3559 } else if (CONN_IS_EDGE(conn)) {
3560 edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
3561 if (!edge_conn->edge_has_sent_end)
3562 connection_edge_end_errno(edge_conn);
3563 if (conn->type == CONN_TYPE_AP && TO_ENTRY_CONN(conn)->socks_request) {
3564 /* broken, don't send a socks reply back */
3565 TO_ENTRY_CONN(conn)->socks_request->has_finished = 1;
3568 connection_close_immediate(conn); /* Connection is dead. */
3569 if (!conn->marked_for_close)
3570 connection_mark_for_close(conn);
3574 /** Set up the generic callbacks for the bufferevent on <b>conn</b>. */
3575 void
3576 connection_configure_bufferevent_callbacks(connection_t *conn)
3578 struct bufferevent *bufev;
3579 struct evbuffer *input, *output;
3580 tor_assert(conn->bufev);
3581 bufev = conn->bufev;
3582 bufferevent_setcb(bufev,
3583 connection_handle_read_cb,
3584 connection_handle_write_cb,
3585 connection_handle_event_cb,
3586 conn);
3587 /* Set a fairly high write low-watermark so that we get the write callback
3588 called whenever data is written to bring us under 128K. Leave the
3589 high-watermark at 0.
3591 bufferevent_setwatermark(bufev, EV_WRITE, 128*1024, 0);
3593 input = bufferevent_get_input(bufev);
3594 output = bufferevent_get_output(bufev);
3595 evbuffer_add_cb(input, evbuffer_inbuf_callback, conn);
3596 evbuffer_add_cb(output, evbuffer_outbuf_callback, conn);
3598 #endif
3600 /** A pass-through to fetch_from_buf. */
3602 connection_fetch_from_buf(char *string, size_t len, connection_t *conn)
3604 IF_HAS_BUFFEREVENT(conn, {
3605 /* XXX overflow -seb */
3606 return (int)bufferevent_read(conn->bufev, string, len);
3607 }) ELSE_IF_NO_BUFFEREVENT {
3608 return fetch_from_buf(string, len, conn->inbuf);
3612 /** As fetch_from_buf_line(), but read from a connection's input buffer. */
3614 connection_fetch_from_buf_line(connection_t *conn, char *data,
3615 size_t *data_len)
3617 IF_HAS_BUFFEREVENT(conn, {
3618 int r;
3619 size_t eol_len=0;
3620 struct evbuffer *input = bufferevent_get_input(conn->bufev);
3621 struct evbuffer_ptr ptr =
3622 evbuffer_search_eol(input, NULL, &eol_len, EVBUFFER_EOL_LF);
3623 if (ptr.pos == -1)
3624 return 0; /* No EOL found. */
3625 if ((size_t)ptr.pos+eol_len >= *data_len) {
3626 return -1; /* Too long */
3628 *data_len = ptr.pos+eol_len;
3629 r = evbuffer_remove(input, data, ptr.pos+eol_len);
3630 tor_assert(r >= 0);
3631 data[ptr.pos+eol_len] = '\0';
3632 return 1;
3633 }) ELSE_IF_NO_BUFFEREVENT {
3634 return fetch_from_buf_line(conn->inbuf, data, data_len);
3638 /** As fetch_from_buf_http, but fetches from a conncetion's input buffer_t or
3639 * its bufferevent as appropriate. */
3641 connection_fetch_from_buf_http(connection_t *conn,
3642 char **headers_out, size_t max_headerlen,
3643 char **body_out, size_t *body_used,
3644 size_t max_bodylen, int force_complete)
3646 IF_HAS_BUFFEREVENT(conn, {
3647 struct evbuffer *input = bufferevent_get_input(conn->bufev);
3648 return fetch_from_evbuffer_http(input, headers_out, max_headerlen,
3649 body_out, body_used, max_bodylen, force_complete);
3650 }) ELSE_IF_NO_BUFFEREVENT {
3651 return fetch_from_buf_http(conn->inbuf, headers_out, max_headerlen,
3652 body_out, body_used, max_bodylen, force_complete);
3656 /** Return conn-\>outbuf_flushlen: how many bytes conn wants to flush
3657 * from its outbuf. */
3659 connection_wants_to_flush(connection_t *conn)
3661 return conn->outbuf_flushlen > 0;
3664 /** Are there too many bytes on edge connection <b>conn</b>'s outbuf to
3665 * send back a relay-level sendme yet? Return 1 if so, 0 if not. Used by
3666 * connection_edge_consider_sending_sendme().
3669 connection_outbuf_too_full(connection_t *conn)
3671 return (conn->outbuf_flushlen > 10*CELL_PAYLOAD_SIZE);
3674 /** Try to flush more bytes onto <b>conn</b>-\>s.
3676 * This function gets called either from conn_write_callback() in main.c
3677 * when libevent tells us that conn wants to write, or below
3678 * from connection_write_to_buf() when an entire TLS record is ready.
3680 * Update <b>conn</b>-\>timestamp_lastwritten to now, and call flush_buf
3681 * or flush_buf_tls appropriately. If it succeeds and there are no more
3682 * more bytes on <b>conn</b>-\>outbuf, then call connection_finished_flushing
3683 * on it too.
3685 * If <b>force</b>, then write as many bytes as possible, ignoring bandwidth
3686 * limits. (Used for flushing messages to controller connections on fatal
3687 * errors.)
3689 * Mark the connection and return -1 if you want to close it, else
3690 * return 0.
3692 static int
3693 connection_handle_write_impl(connection_t *conn, int force)
3695 int e;
3696 socklen_t len=(socklen_t)sizeof(e);
3697 int result;
3698 ssize_t max_to_write;
3699 time_t now = approx_time();
3700 size_t n_read = 0, n_written = 0;
3701 int dont_stop_writing = 0;
3703 tor_assert(!connection_is_listener(conn));
3705 if (conn->marked_for_close || !SOCKET_OK(conn->s))
3706 return 0; /* do nothing */
3708 if (conn->in_flushed_some) {
3709 log_warn(LD_BUG, "called recursively from inside conn->in_flushed_some");
3710 return 0;
3713 conn->timestamp_lastwritten = now;
3715 /* Sometimes, "writable" means "connected". */
3716 if (connection_state_is_connecting(conn)) {
3717 if (getsockopt(conn->s, SOL_SOCKET, SO_ERROR, (void*)&e, &len) < 0) {
3718 log_warn(LD_BUG, "getsockopt() syscall failed");
3719 if (CONN_IS_EDGE(conn))
3720 connection_edge_end_errno(TO_EDGE_CONN(conn));
3721 connection_mark_for_close(conn);
3722 return -1;
3724 if (e) {
3725 /* some sort of error, but maybe just inprogress still */
3726 if (!ERRNO_IS_CONN_EINPROGRESS(e)) {
3727 log_info(LD_NET,"in-progress connect failed. Removing. (%s)",
3728 tor_socket_strerror(e));
3729 if (CONN_IS_EDGE(conn))
3730 connection_edge_end_errno(TO_EDGE_CONN(conn));
3731 if (conn->type == CONN_TYPE_OR)
3732 connection_or_notify_error(TO_OR_CONN(conn),
3733 errno_to_orconn_end_reason(e),
3734 tor_socket_strerror(e));
3736 connection_close_immediate(conn);
3738 * This can bypass normal channel checking since we did
3739 * connection_or_notify_error() above.
3741 connection_mark_for_close_internal(conn);
3742 return -1;
3743 } else {
3744 return 0; /* no change, see if next time is better */
3747 /* The connection is successful. */
3748 if (connection_finished_connecting(conn)<0)
3749 return -1;
3752 max_to_write = force ? (ssize_t)conn->outbuf_flushlen
3753 : connection_bucket_write_limit(conn, now);
3755 if (connection_speaks_cells(conn) &&
3756 conn->state > OR_CONN_STATE_PROXY_HANDSHAKING) {
3757 or_connection_t *or_conn = TO_OR_CONN(conn);
3758 size_t initial_size;
3759 if (conn->state == OR_CONN_STATE_TLS_HANDSHAKING ||
3760 conn->state == OR_CONN_STATE_TLS_CLIENT_RENEGOTIATING) {
3761 connection_stop_writing(conn);
3762 if (connection_tls_continue_handshake(or_conn) < 0) {
3763 /* Don't flush; connection is dead. */
3764 connection_or_notify_error(or_conn,
3765 END_OR_CONN_REASON_MISC,
3766 "TLS error in connection_tls_"
3767 "continue_handshake()");
3768 connection_close_immediate(conn);
3770 * This can bypass normal channel checking since we did
3771 * connection_or_notify_error() above.
3773 connection_mark_for_close_internal(conn);
3774 return -1;
3776 return 0;
3777 } else if (conn->state == OR_CONN_STATE_TLS_SERVER_RENEGOTIATING) {
3778 return connection_handle_read(conn);
3781 /* else open, or closing */
3782 initial_size = buf_datalen(conn->outbuf);
3783 result = flush_buf_tls(or_conn->tls, conn->outbuf,
3784 max_to_write, &conn->outbuf_flushlen);
3786 /* If we just flushed the last bytes, tell the channel on the
3787 * or_conn to check if it needs to geoip_change_dirreq_state() */
3788 /* XXXX move this to flushed_some or finished_flushing -NM */
3789 if (buf_datalen(conn->outbuf) == 0 && or_conn->chan)
3790 channel_notify_flushed(TLS_CHAN_TO_BASE(or_conn->chan));
3792 switch (result) {
3793 CASE_TOR_TLS_ERROR_ANY:
3794 case TOR_TLS_CLOSE:
3795 log_info(LD_NET, result != TOR_TLS_CLOSE ?
3796 "tls error. breaking.":"TLS connection closed on flush");
3797 /* Don't flush; connection is dead. */
3798 connection_or_notify_error(or_conn,
3799 END_OR_CONN_REASON_MISC,
3800 result != TOR_TLS_CLOSE ?
3801 "TLS error in during flush" :
3802 "TLS closed during flush");
3803 connection_close_immediate(conn);
3805 * This can bypass normal channel checking since we did
3806 * connection_or_notify_error() above.
3808 connection_mark_for_close_internal(conn);
3809 return -1;
3810 case TOR_TLS_WANTWRITE:
3811 log_debug(LD_NET,"wanted write.");
3812 /* we're already writing */
3813 dont_stop_writing = 1;
3814 break;
3815 case TOR_TLS_WANTREAD:
3816 /* Make sure to avoid a loop if the receive buckets are empty. */
3817 log_debug(LD_NET,"wanted read.");
3818 if (!connection_is_reading(conn)) {
3819 connection_stop_writing(conn);
3820 conn->write_blocked_on_bw = 1;
3821 /* we'll start reading again when we get more tokens in our
3822 * read bucket; then we'll start writing again too.
3825 /* else no problem, we're already reading */
3826 return 0;
3827 /* case TOR_TLS_DONE:
3828 * for TOR_TLS_DONE, fall through to check if the flushlen
3829 * is empty, so we can stop writing.
3833 tor_tls_get_n_raw_bytes(or_conn->tls, &n_read, &n_written);
3834 log_debug(LD_GENERAL, "After TLS write of %d: %ld read, %ld written",
3835 result, (long)n_read, (long)n_written);
3836 /* So we notice bytes were written even on error */
3837 /* XXXX024 This cast is safe since we can never write INT_MAX bytes in a
3838 * single set of TLS operations. But it looks kinda ugly. If we refactor
3839 * the *_buf_tls functions, we should make them return ssize_t or size_t
3840 * or something. */
3841 result = (int)(initial_size-buf_datalen(conn->outbuf));
3842 } else {
3843 CONN_LOG_PROTECT(conn,
3844 result = flush_buf(conn->s, conn->outbuf,
3845 max_to_write, &conn->outbuf_flushlen));
3846 if (result < 0) {
3847 if (CONN_IS_EDGE(conn))
3848 connection_edge_end_errno(TO_EDGE_CONN(conn));
3849 if (conn->type == CONN_TYPE_AP) {
3850 /* writing failed; we couldn't send a SOCKS reply if we wanted to */
3851 TO_ENTRY_CONN(conn)->socks_request->has_finished = 1;
3854 connection_close_immediate(conn); /* Don't flush; connection is dead. */
3855 connection_mark_for_close(conn);
3856 return -1;
3858 n_written = (size_t) result;
3861 if (n_written && conn->type == CONN_TYPE_AP) {
3862 edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
3863 circuit_t *circ = circuit_get_by_edge_conn(edge_conn);
3864 origin_circuit_t *ocirc;
3866 /* Check for overflow: */
3867 if (PREDICT_LIKELY(UINT32_MAX - edge_conn->n_written > n_written))
3868 edge_conn->n_written += (int)n_written;
3869 else
3870 edge_conn->n_written = UINT32_MAX;
3872 if (circ && CIRCUIT_IS_ORIGIN(circ)) {
3873 ocirc = TO_ORIGIN_CIRCUIT(circ);
3874 if (PREDICT_LIKELY(UINT32_MAX - ocirc->n_written_circ_bw > n_written))
3875 ocirc->n_written_circ_bw += (int)n_written;
3876 else
3877 ocirc->n_written_circ_bw = UINT32_MAX;
3881 /* If CONN_BW events are enabled, update conn->n_written_conn_bw for
3882 * OR/DIR/EXIT connections, checking for overflow. */
3883 if (n_written && get_options()->TestingEnableConnBwEvent &&
3884 (conn->type == CONN_TYPE_OR ||
3885 conn->type == CONN_TYPE_DIR ||
3886 conn->type == CONN_TYPE_EXIT)) {
3887 if (PREDICT_LIKELY(UINT32_MAX - conn->n_written_conn_bw > n_written))
3888 conn->n_written_conn_bw += (int)n_written;
3889 else
3890 conn->n_written_conn_bw = UINT32_MAX;
3893 connection_buckets_decrement(conn, approx_time(), n_read, n_written);
3895 if (result > 0) {
3896 /* If we wrote any bytes from our buffer, then call the appropriate
3897 * functions. */
3898 if (connection_flushed_some(conn) < 0) {
3899 if (connection_speaks_cells(conn)) {
3900 connection_or_notify_error(TO_OR_CONN(conn),
3901 END_OR_CONN_REASON_MISC,
3902 "Got error back from "
3903 "connection_flushed_some()");
3907 * This can bypass normal channel checking since we did
3908 * connection_or_notify_error() above.
3910 connection_mark_for_close_internal(conn);
3914 if (!connection_wants_to_flush(conn) &&
3915 !dont_stop_writing) { /* it's done flushing */
3916 if (connection_finished_flushing(conn) < 0) {
3917 /* already marked */
3918 return -1;
3920 return 0;
3923 /* Call even if result is 0, since the global write bucket may
3924 * have reached 0 on a different conn, and this guy needs to
3925 * know to stop writing. */
3926 connection_consider_empty_write_buckets(conn);
3927 if (n_read > 0 && connection_is_reading(conn))
3928 connection_consider_empty_read_buckets(conn);
3930 return 0;
3933 /* DOCDOC connection_handle_write */
3935 connection_handle_write(connection_t *conn, int force)
3937 int res;
3938 tor_gettimeofday_cache_clear();
3939 conn->in_connection_handle_write = 1;
3940 res = connection_handle_write_impl(conn, force);
3941 conn->in_connection_handle_write = 0;
3942 return res;
3946 * Try to flush data that's waiting for a write on <b>conn</b>. Return
3947 * -1 on failure, 0 on success.
3949 * Don't use this function for regular writing; the buffers/bufferevents
3950 * system should be good enough at scheduling writes there. Instead, this
3951 * function is for cases when we're about to exit or something and we want
3952 * to report it right away.
3955 connection_flush(connection_t *conn)
3957 IF_HAS_BUFFEREVENT(conn, {
3958 int r = bufferevent_flush(conn->bufev, EV_WRITE, BEV_FLUSH);
3959 return (r < 0) ? -1 : 0;
3961 return connection_handle_write(conn, 1);
3964 /** Append <b>len</b> bytes of <b>string</b> onto <b>conn</b>'s
3965 * outbuf, and ask it to start writing.
3967 * If <b>zlib</b> is nonzero, this is a directory connection that should get
3968 * its contents compressed or decompressed as they're written. If zlib is
3969 * negative, this is the last data to be compressed, and the connection's zlib
3970 * state should be flushed.
3972 * If it's a local control connection and a 64k chunk is ready, try to flush
3973 * it all, so we don't end up with many megabytes of controller info queued at
3974 * once.
3976 MOCK_IMPL(void,
3977 connection_write_to_buf_impl_,(const char *string, size_t len,
3978 connection_t *conn, int zlib))
3980 /* XXXX This function really needs to return -1 on failure. */
3981 int r;
3982 size_t old_datalen;
3983 if (!len && !(zlib<0))
3984 return;
3985 /* if it's marked for close, only allow write if we mean to flush it */
3986 if (conn->marked_for_close && !conn->hold_open_until_flushed)
3987 return;
3989 IF_HAS_BUFFEREVENT(conn, {
3990 if (zlib) {
3991 int done = zlib < 0;
3992 r = write_to_evbuffer_zlib(bufferevent_get_output(conn->bufev),
3993 TO_DIR_CONN(conn)->zlib_state,
3994 string, len, done);
3995 } else {
3996 r = bufferevent_write(conn->bufev, string, len);
3998 if (r < 0) {
3999 /* XXXX mark for close? */
4000 log_warn(LD_NET, "bufferevent_write failed! That shouldn't happen.");
4002 return;
4005 old_datalen = buf_datalen(conn->outbuf);
4006 if (zlib) {
4007 dir_connection_t *dir_conn = TO_DIR_CONN(conn);
4008 int done = zlib < 0;
4009 CONN_LOG_PROTECT(conn, r = write_to_buf_zlib(conn->outbuf,
4010 dir_conn->zlib_state,
4011 string, len, done));
4012 } else {
4013 CONN_LOG_PROTECT(conn, r = write_to_buf(string, len, conn->outbuf));
4015 if (r < 0) {
4016 if (CONN_IS_EDGE(conn)) {
4017 /* if it failed, it means we have our package/delivery windows set
4018 wrong compared to our max outbuf size. close the whole circuit. */
4019 log_warn(LD_NET,
4020 "write_to_buf failed. Closing circuit (fd %d).", (int)conn->s);
4021 circuit_mark_for_close(circuit_get_by_edge_conn(TO_EDGE_CONN(conn)),
4022 END_CIRC_REASON_INTERNAL);
4023 } else if (conn->type == CONN_TYPE_OR) {
4024 or_connection_t *orconn = TO_OR_CONN(conn);
4025 log_warn(LD_NET,
4026 "write_to_buf failed on an orconn; notifying of error "
4027 "(fd %d)", (int)(conn->s));
4028 connection_or_close_for_error(orconn, 0);
4029 } else {
4030 log_warn(LD_NET,
4031 "write_to_buf failed. Closing connection (fd %d).",
4032 (int)conn->s);
4033 connection_mark_for_close(conn);
4035 return;
4038 /* If we receive optimistic data in the EXIT_CONN_STATE_RESOLVING
4039 * state, we don't want to try to write it right away, since
4040 * conn->write_event won't be set yet. Otherwise, write data from
4041 * this conn as the socket is available. */
4042 if (conn->write_event) {
4043 connection_start_writing(conn);
4045 if (zlib) {
4046 conn->outbuf_flushlen += buf_datalen(conn->outbuf) - old_datalen;
4047 } else {
4048 conn->outbuf_flushlen += len;
4050 /* Should we try flushing the outbuf now? */
4051 if (conn->in_flushed_some) {
4052 /* Don't flush the outbuf when the reason we're writing more stuff is
4053 * _because_ we flushed the outbuf. That's unfair. */
4054 return;
4057 if (conn->type == CONN_TYPE_CONTROL &&
4058 !connection_is_rate_limited(conn) &&
4059 conn->outbuf_flushlen-len < 1<<16 &&
4060 conn->outbuf_flushlen >= 1<<16) {
4061 /* just try to flush all of it */
4062 } else
4063 return; /* no need to try flushing */
4065 if (connection_handle_write(conn, 0) < 0) {
4066 if (!conn->marked_for_close) {
4067 /* this connection is broken. remove it. */
4068 log_warn(LD_BUG, "unhandled error on write for "
4069 "conn (type %d, fd %d); removing",
4070 conn->type, (int)conn->s);
4071 tor_fragile_assert();
4072 /* do a close-immediate here, so we don't try to flush */
4073 connection_close_immediate(conn);
4075 return;
4080 /** Return a connection with given type, address, port, and purpose;
4081 * or NULL if no such connection exists. */
4082 connection_t *
4083 connection_get_by_type_addr_port_purpose(int type,
4084 const tor_addr_t *addr, uint16_t port,
4085 int purpose)
4087 smartlist_t *conns = get_connection_array();
4088 SMARTLIST_FOREACH(conns, connection_t *, conn,
4090 if (conn->type == type &&
4091 tor_addr_eq(&conn->addr, addr) &&
4092 conn->port == port &&
4093 conn->purpose == purpose &&
4094 !conn->marked_for_close)
4095 return conn;
4097 return NULL;
4100 /** Return the stream with id <b>id</b> if it is not already marked for
4101 * close.
4103 connection_t *
4104 connection_get_by_global_id(uint64_t id)
4106 smartlist_t *conns = get_connection_array();
4107 SMARTLIST_FOREACH(conns, connection_t *, conn,
4109 if (conn->global_identifier == id)
4110 return conn;
4112 return NULL;
4115 /** Return a connection of type <b>type</b> that is not marked for close.
4117 connection_t *
4118 connection_get_by_type(int type)
4120 smartlist_t *conns = get_connection_array();
4121 SMARTLIST_FOREACH(conns, connection_t *, conn,
4123 if (conn->type == type && !conn->marked_for_close)
4124 return conn;
4126 return NULL;
4129 /** Return a connection of type <b>type</b> that is in state <b>state</b>,
4130 * and that is not marked for close.
4132 connection_t *
4133 connection_get_by_type_state(int type, int state)
4135 smartlist_t *conns = get_connection_array();
4136 SMARTLIST_FOREACH(conns, connection_t *, conn,
4138 if (conn->type == type && conn->state == state && !conn->marked_for_close)
4139 return conn;
4141 return NULL;
4144 /** Return a connection of type <b>type</b> that has rendquery equal
4145 * to <b>rendquery</b>, and that is not marked for close. If state
4146 * is non-zero, conn must be of that state too.
4148 connection_t *
4149 connection_get_by_type_state_rendquery(int type, int state,
4150 const char *rendquery)
4152 smartlist_t *conns = get_connection_array();
4154 tor_assert(type == CONN_TYPE_DIR ||
4155 type == CONN_TYPE_AP || type == CONN_TYPE_EXIT);
4156 tor_assert(rendquery);
4158 SMARTLIST_FOREACH_BEGIN(conns, connection_t *, conn) {
4159 if (conn->type == type &&
4160 !conn->marked_for_close &&
4161 (!state || state == conn->state)) {
4162 if (type == CONN_TYPE_DIR &&
4163 TO_DIR_CONN(conn)->rend_data &&
4164 !rend_cmp_service_ids(rendquery,
4165 TO_DIR_CONN(conn)->rend_data->onion_address))
4166 return conn;
4167 else if (CONN_IS_EDGE(conn) &&
4168 TO_EDGE_CONN(conn)->rend_data &&
4169 !rend_cmp_service_ids(rendquery,
4170 TO_EDGE_CONN(conn)->rend_data->onion_address))
4171 return conn;
4173 } SMARTLIST_FOREACH_END(conn);
4174 return NULL;
4177 /** Return a directory connection (if any one exists) that is fetching
4178 * the item described by <b>state</b>/<b>resource</b> */
4179 dir_connection_t *
4180 connection_dir_get_by_purpose_and_resource(int purpose,
4181 const char *resource)
4183 smartlist_t *conns = get_connection_array();
4185 SMARTLIST_FOREACH_BEGIN(conns, connection_t *, conn) {
4186 dir_connection_t *dirconn;
4187 if (conn->type != CONN_TYPE_DIR || conn->marked_for_close ||
4188 conn->purpose != purpose)
4189 continue;
4190 dirconn = TO_DIR_CONN(conn);
4191 if (dirconn->requested_resource == NULL) {
4192 if (resource == NULL)
4193 return dirconn;
4194 } else if (resource) {
4195 if (0 == strcmp(resource, dirconn->requested_resource))
4196 return dirconn;
4198 } SMARTLIST_FOREACH_END(conn);
4200 return NULL;
4203 /** Return 1 if there are any active OR connections apart from
4204 * <b>this_conn</b>.
4206 * We use this to guess if we should tell the controller that we
4207 * didn't manage to connect to any of our bridges. */
4209 any_other_active_or_conns(const or_connection_t *this_conn)
4211 smartlist_t *conns = get_connection_array();
4212 SMARTLIST_FOREACH_BEGIN(conns, connection_t *, conn) {
4213 if (conn == TO_CONN(this_conn)) { /* don't consider this conn */
4214 continue;
4217 if (conn->type == CONN_TYPE_OR &&
4218 !conn->marked_for_close) {
4219 log_debug(LD_DIR, "%s: Found an OR connection: %s",
4220 __func__, conn->address);
4221 return 1;
4223 } SMARTLIST_FOREACH_END(conn);
4225 return 0;
4228 /** Return 1 if <b>conn</b> is a listener conn, else return 0. */
4230 connection_is_listener(connection_t *conn)
4232 if (conn->type == CONN_TYPE_OR_LISTENER ||
4233 conn->type == CONN_TYPE_EXT_OR_LISTENER ||
4234 conn->type == CONN_TYPE_AP_LISTENER ||
4235 conn->type == CONN_TYPE_AP_TRANS_LISTENER ||
4236 conn->type == CONN_TYPE_AP_DNS_LISTENER ||
4237 conn->type == CONN_TYPE_AP_NATD_LISTENER ||
4238 conn->type == CONN_TYPE_DIR_LISTENER ||
4239 conn->type == CONN_TYPE_CONTROL_LISTENER)
4240 return 1;
4241 return 0;
4244 /** Return 1 if <b>conn</b> is in state "open" and is not marked
4245 * for close, else return 0.
4248 connection_state_is_open(connection_t *conn)
4250 tor_assert(conn);
4252 if (conn->marked_for_close)
4253 return 0;
4255 if ((conn->type == CONN_TYPE_OR && conn->state == OR_CONN_STATE_OPEN) ||
4256 (conn->type == CONN_TYPE_EXT_OR) ||
4257 (conn->type == CONN_TYPE_AP && conn->state == AP_CONN_STATE_OPEN) ||
4258 (conn->type == CONN_TYPE_EXIT && conn->state == EXIT_CONN_STATE_OPEN) ||
4259 (conn->type == CONN_TYPE_CONTROL &&
4260 conn->state == CONTROL_CONN_STATE_OPEN))
4261 return 1;
4263 return 0;
4266 /** Return 1 if conn is in 'connecting' state, else return 0. */
4268 connection_state_is_connecting(connection_t *conn)
4270 tor_assert(conn);
4272 if (conn->marked_for_close)
4273 return 0;
4274 switch (conn->type)
4276 case CONN_TYPE_OR:
4277 return conn->state == OR_CONN_STATE_CONNECTING;
4278 case CONN_TYPE_EXIT:
4279 return conn->state == EXIT_CONN_STATE_CONNECTING;
4280 case CONN_TYPE_DIR:
4281 return conn->state == DIR_CONN_STATE_CONNECTING;
4284 return 0;
4287 /** Allocates a base64'ed authenticator for use in http or https
4288 * auth, based on the input string <b>authenticator</b>. Returns it
4289 * if success, else returns NULL. */
4290 char *
4291 alloc_http_authenticator(const char *authenticator)
4293 /* an authenticator in Basic authentication
4294 * is just the string "username:password" */
4295 const size_t authenticator_length = strlen(authenticator);
4296 /* The base64_encode function needs a minimum buffer length
4297 * of 66 bytes. */
4298 const size_t base64_authenticator_length = (authenticator_length/48+1)*66;
4299 char *base64_authenticator = tor_malloc(base64_authenticator_length);
4300 if (base64_encode(base64_authenticator, base64_authenticator_length,
4301 authenticator, authenticator_length) < 0) {
4302 tor_free(base64_authenticator); /* free and set to null */
4303 } else {
4304 int i = 0, j = 0;
4305 ssize_t len = strlen(base64_authenticator);
4307 /* remove all newline occurrences within the string */
4308 for (i=0; i < len; ++i) {
4309 if ('\n' != base64_authenticator[i]) {
4310 base64_authenticator[j] = base64_authenticator[i];
4311 ++j;
4314 base64_authenticator[j]='\0';
4316 return base64_authenticator;
4319 /** Given a socket handle, check whether the local address (sockname) of the
4320 * socket is one that we've connected from before. If so, double-check
4321 * whether our address has changed and we need to generate keys. If we do,
4322 * call init_keys().
4324 static void
4325 client_check_address_changed(tor_socket_t sock)
4327 struct sockaddr_storage out_sockaddr;
4328 socklen_t out_addr_len = (socklen_t) sizeof(out_sockaddr);
4329 tor_addr_t out_addr, iface_addr;
4330 tor_addr_t **last_interface_ip_ptr;
4331 sa_family_t family;
4333 if (!outgoing_addrs)
4334 outgoing_addrs = smartlist_new();
4336 if (getsockname(sock, (struct sockaddr*)&out_sockaddr, &out_addr_len)<0) {
4337 int e = tor_socket_errno(sock);
4338 log_warn(LD_NET, "getsockname() to check for address change failed: %s",
4339 tor_socket_strerror(e));
4340 return;
4342 tor_addr_from_sockaddr(&out_addr, (struct sockaddr*)&out_sockaddr, NULL);
4343 family = tor_addr_family(&out_addr);
4345 if (family == AF_INET)
4346 last_interface_ip_ptr = &last_interface_ipv4;
4347 else if (family == AF_INET6)
4348 last_interface_ip_ptr = &last_interface_ipv6;
4349 else
4350 return;
4352 if (! *last_interface_ip_ptr) {
4353 tor_addr_t *a = tor_malloc_zero(sizeof(tor_addr_t));
4354 if (get_interface_address6(LOG_INFO, family, a)==0) {
4355 *last_interface_ip_ptr = a;
4356 } else {
4357 tor_free(a);
4361 /* If we've used this address previously, we're okay. */
4362 SMARTLIST_FOREACH(outgoing_addrs, const tor_addr_t *, a_ptr,
4363 if (tor_addr_eq(a_ptr, &out_addr))
4364 return;
4367 /* Uh-oh. We haven't connected from this address before. Has the interface
4368 * address changed? */
4369 if (get_interface_address6(LOG_INFO, family, &iface_addr)<0)
4370 return;
4372 if (tor_addr_eq(&iface_addr, *last_interface_ip_ptr)) {
4373 /* Nope, it hasn't changed. Add this address to the list. */
4374 smartlist_add(outgoing_addrs, tor_memdup(&out_addr, sizeof(tor_addr_t)));
4375 } else {
4376 /* The interface changed. We're a client, so we need to regenerate our
4377 * keys. First, reset the state. */
4378 log_notice(LD_NET, "Our IP address has changed. Rotating keys...");
4379 tor_addr_copy(*last_interface_ip_ptr, &iface_addr);
4380 SMARTLIST_FOREACH(outgoing_addrs, tor_addr_t*, a_ptr, tor_free(a_ptr));
4381 smartlist_clear(outgoing_addrs);
4382 smartlist_add(outgoing_addrs, tor_memdup(&out_addr, sizeof(tor_addr_t)));
4383 /* Okay, now change our keys. */
4384 ip_address_changed(1);
4388 /** Some systems have limited system buffers for recv and xmit on
4389 * sockets allocated in a virtual server or similar environment. For a Tor
4390 * server this can produce the "Error creating network socket: No buffer
4391 * space available" error once all available TCP buffer space is consumed.
4392 * This method will attempt to constrain the buffers allocated for the socket
4393 * to the desired size to stay below system TCP buffer limits.
4395 static void
4396 set_constrained_socket_buffers(tor_socket_t sock, int size)
4398 void *sz = (void*)&size;
4399 socklen_t sz_sz = (socklen_t) sizeof(size);
4400 if (setsockopt(sock, SOL_SOCKET, SO_SNDBUF, sz, sz_sz) < 0) {
4401 int e = tor_socket_errno(sock);
4402 log_warn(LD_NET, "setsockopt() to constrain send "
4403 "buffer to %d bytes failed: %s", size, tor_socket_strerror(e));
4405 if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF, sz, sz_sz) < 0) {
4406 int e = tor_socket_errno(sock);
4407 log_warn(LD_NET, "setsockopt() to constrain recv "
4408 "buffer to %d bytes failed: %s", size, tor_socket_strerror(e));
4412 /** Process new bytes that have arrived on conn-\>inbuf.
4414 * This function just passes conn to the connection-specific
4415 * connection_*_process_inbuf() function. It also passes in
4416 * package_partial if wanted.
4418 static int
4419 connection_process_inbuf(connection_t *conn, int package_partial)
4421 tor_assert(conn);
4423 switch (conn->type) {
4424 case CONN_TYPE_OR:
4425 return connection_or_process_inbuf(TO_OR_CONN(conn));
4426 case CONN_TYPE_EXT_OR:
4427 return connection_ext_or_process_inbuf(TO_OR_CONN(conn));
4428 case CONN_TYPE_EXIT:
4429 case CONN_TYPE_AP:
4430 return connection_edge_process_inbuf(TO_EDGE_CONN(conn),
4431 package_partial);
4432 case CONN_TYPE_DIR:
4433 return connection_dir_process_inbuf(TO_DIR_CONN(conn));
4434 case CONN_TYPE_CPUWORKER:
4435 return connection_cpu_process_inbuf(conn);
4436 case CONN_TYPE_CONTROL:
4437 return connection_control_process_inbuf(TO_CONTROL_CONN(conn));
4438 default:
4439 log_err(LD_BUG,"got unexpected conn type %d.", conn->type);
4440 tor_fragile_assert();
4441 return -1;
4445 /** Called whenever we've written data on a connection. */
4446 static int
4447 connection_flushed_some(connection_t *conn)
4449 int r = 0;
4450 tor_assert(!conn->in_flushed_some);
4451 conn->in_flushed_some = 1;
4452 if (conn->type == CONN_TYPE_DIR &&
4453 conn->state == DIR_CONN_STATE_SERVER_WRITING) {
4454 r = connection_dirserv_flushed_some(TO_DIR_CONN(conn));
4455 } else if (conn->type == CONN_TYPE_OR) {
4456 r = connection_or_flushed_some(TO_OR_CONN(conn));
4457 } else if (CONN_IS_EDGE(conn)) {
4458 r = connection_edge_flushed_some(TO_EDGE_CONN(conn));
4460 conn->in_flushed_some = 0;
4461 return r;
4464 /** We just finished flushing bytes to the appropriately low network layer,
4465 * and there are no more bytes remaining in conn-\>outbuf, conn-\>bev, or
4466 * conn-\>tls to be flushed.
4468 * This function just passes conn to the connection-specific
4469 * connection_*_finished_flushing() function.
4471 static int
4472 connection_finished_flushing(connection_t *conn)
4474 tor_assert(conn);
4476 /* If the connection is closed, don't try to do anything more here. */
4477 if (CONN_IS_CLOSED(conn))
4478 return 0;
4480 // log_fn(LOG_DEBUG,"entered. Socket %u.", conn->s);
4482 IF_HAS_NO_BUFFEREVENT(conn)
4483 connection_stop_writing(conn);
4485 switch (conn->type) {
4486 case CONN_TYPE_OR:
4487 return connection_or_finished_flushing(TO_OR_CONN(conn));
4488 case CONN_TYPE_EXT_OR:
4489 return connection_ext_or_finished_flushing(TO_OR_CONN(conn));
4490 case CONN_TYPE_AP:
4491 case CONN_TYPE_EXIT:
4492 return connection_edge_finished_flushing(TO_EDGE_CONN(conn));
4493 case CONN_TYPE_DIR:
4494 return connection_dir_finished_flushing(TO_DIR_CONN(conn));
4495 case CONN_TYPE_CPUWORKER:
4496 return connection_cpu_finished_flushing(conn);
4497 case CONN_TYPE_CONTROL:
4498 return connection_control_finished_flushing(TO_CONTROL_CONN(conn));
4499 default:
4500 log_err(LD_BUG,"got unexpected conn type %d.", conn->type);
4501 tor_fragile_assert();
4502 return -1;
4506 /** Called when our attempt to connect() to another server has just
4507 * succeeded.
4509 * This function just passes conn to the connection-specific
4510 * connection_*_finished_connecting() function.
4512 static int
4513 connection_finished_connecting(connection_t *conn)
4515 tor_assert(conn);
4517 if (!server_mode(get_options())) {
4518 /* See whether getsockname() says our address changed. We need to do this
4519 * now that the connection has finished, because getsockname() on Windows
4520 * won't work until then. */
4521 client_check_address_changed(conn->s);
4524 switch (conn->type)
4526 case CONN_TYPE_OR:
4527 return connection_or_finished_connecting(TO_OR_CONN(conn));
4528 case CONN_TYPE_EXIT:
4529 return connection_edge_finished_connecting(TO_EDGE_CONN(conn));
4530 case CONN_TYPE_DIR:
4531 return connection_dir_finished_connecting(TO_DIR_CONN(conn));
4532 default:
4533 log_err(LD_BUG,"got unexpected conn type %d.", conn->type);
4534 tor_fragile_assert();
4535 return -1;
4539 /** Callback: invoked when a connection reaches an EOF event. */
4540 static int
4541 connection_reached_eof(connection_t *conn)
4543 switch (conn->type) {
4544 case CONN_TYPE_OR:
4545 case CONN_TYPE_EXT_OR:
4546 return connection_or_reached_eof(TO_OR_CONN(conn));
4547 case CONN_TYPE_AP:
4548 case CONN_TYPE_EXIT:
4549 return connection_edge_reached_eof(TO_EDGE_CONN(conn));
4550 case CONN_TYPE_DIR:
4551 return connection_dir_reached_eof(TO_DIR_CONN(conn));
4552 case CONN_TYPE_CPUWORKER:
4553 return connection_cpu_reached_eof(conn);
4554 case CONN_TYPE_CONTROL:
4555 return connection_control_reached_eof(TO_CONTROL_CONN(conn));
4556 default:
4557 log_err(LD_BUG,"got unexpected conn type %d.", conn->type);
4558 tor_fragile_assert();
4559 return -1;
4563 /** Log how many bytes are used by buffers of different kinds and sizes. */
4564 void
4565 connection_dump_buffer_mem_stats(int severity)
4567 uint64_t used_by_type[CONN_TYPE_MAX_+1];
4568 uint64_t alloc_by_type[CONN_TYPE_MAX_+1];
4569 int n_conns_by_type[CONN_TYPE_MAX_+1];
4570 uint64_t total_alloc = 0;
4571 uint64_t total_used = 0;
4572 int i;
4573 smartlist_t *conns = get_connection_array();
4575 memset(used_by_type, 0, sizeof(used_by_type));
4576 memset(alloc_by_type, 0, sizeof(alloc_by_type));
4577 memset(n_conns_by_type, 0, sizeof(n_conns_by_type));
4579 SMARTLIST_FOREACH_BEGIN(conns, connection_t *, c) {
4580 int tp = c->type;
4581 ++n_conns_by_type[tp];
4582 if (c->inbuf) {
4583 used_by_type[tp] += buf_datalen(c->inbuf);
4584 alloc_by_type[tp] += buf_allocation(c->inbuf);
4586 if (c->outbuf) {
4587 used_by_type[tp] += buf_datalen(c->outbuf);
4588 alloc_by_type[tp] += buf_allocation(c->outbuf);
4590 } SMARTLIST_FOREACH_END(c);
4591 for (i=0; i <= CONN_TYPE_MAX_; ++i) {
4592 total_used += used_by_type[i];
4593 total_alloc += alloc_by_type[i];
4596 tor_log(severity, LD_GENERAL,
4597 "In buffers for %d connections: "U64_FORMAT" used/"U64_FORMAT" allocated",
4598 smartlist_len(conns),
4599 U64_PRINTF_ARG(total_used), U64_PRINTF_ARG(total_alloc));
4600 for (i=CONN_TYPE_MIN_; i <= CONN_TYPE_MAX_; ++i) {
4601 if (!n_conns_by_type[i])
4602 continue;
4603 tor_log(severity, LD_GENERAL,
4604 " For %d %s connections: "U64_FORMAT" used/"U64_FORMAT" allocated",
4605 n_conns_by_type[i], conn_type_to_string(i),
4606 U64_PRINTF_ARG(used_by_type[i]), U64_PRINTF_ARG(alloc_by_type[i]));
4610 /** Verify that connection <b>conn</b> has all of its invariants
4611 * correct. Trigger an assert if anything is invalid.
4613 void
4614 assert_connection_ok(connection_t *conn, time_t now)
4616 (void) now; /* XXXX unused. */
4617 tor_assert(conn);
4618 tor_assert(conn->type >= CONN_TYPE_MIN_);
4619 tor_assert(conn->type <= CONN_TYPE_MAX_);
4621 #ifdef USE_BUFFEREVENTS
4622 if (conn->bufev) {
4623 tor_assert(conn->read_event == NULL);
4624 tor_assert(conn->write_event == NULL);
4625 tor_assert(conn->inbuf == NULL);
4626 tor_assert(conn->outbuf == NULL);
4628 #endif
4630 switch (conn->type) {
4631 case CONN_TYPE_OR:
4632 case CONN_TYPE_EXT_OR:
4633 tor_assert(conn->magic == OR_CONNECTION_MAGIC);
4634 break;
4635 case CONN_TYPE_AP:
4636 tor_assert(conn->magic == ENTRY_CONNECTION_MAGIC);
4637 break;
4638 case CONN_TYPE_EXIT:
4639 tor_assert(conn->magic == EDGE_CONNECTION_MAGIC);
4640 break;
4641 case CONN_TYPE_DIR:
4642 tor_assert(conn->magic == DIR_CONNECTION_MAGIC);
4643 break;
4644 case CONN_TYPE_CONTROL:
4645 tor_assert(conn->magic == CONTROL_CONNECTION_MAGIC);
4646 break;
4647 CASE_ANY_LISTENER_TYPE:
4648 tor_assert(conn->magic == LISTENER_CONNECTION_MAGIC);
4649 break;
4650 default:
4651 tor_assert(conn->magic == BASE_CONNECTION_MAGIC);
4652 break;
4655 if (conn->linked_conn) {
4656 tor_assert(conn->linked_conn->linked_conn == conn);
4657 tor_assert(conn->linked);
4659 if (conn->linked)
4660 tor_assert(!SOCKET_OK(conn->s));
4662 if (conn->outbuf_flushlen > 0) {
4663 /* With optimistic data, we may have queued data in
4664 * EXIT_CONN_STATE_RESOLVING while the conn is not yet marked to writing.
4665 * */
4666 tor_assert((conn->type == CONN_TYPE_EXIT &&
4667 conn->state == EXIT_CONN_STATE_RESOLVING) ||
4668 connection_is_writing(conn) ||
4669 conn->write_blocked_on_bw ||
4670 (CONN_IS_EDGE(conn) &&
4671 TO_EDGE_CONN(conn)->edge_blocked_on_circ));
4674 if (conn->hold_open_until_flushed)
4675 tor_assert(conn->marked_for_close);
4677 /* XXXX check: read_blocked_on_bw, write_blocked_on_bw, s, conn_array_index,
4678 * marked_for_close. */
4680 /* buffers */
4681 if (conn->inbuf)
4682 assert_buf_ok(conn->inbuf);
4683 if (conn->outbuf)
4684 assert_buf_ok(conn->outbuf);
4686 if (conn->type == CONN_TYPE_OR) {
4687 or_connection_t *or_conn = TO_OR_CONN(conn);
4688 if (conn->state == OR_CONN_STATE_OPEN) {
4689 /* tor_assert(conn->bandwidth > 0); */
4690 /* the above isn't necessarily true: if we just did a TLS
4691 * handshake but we didn't recognize the other peer, or it
4692 * gave a bad cert/etc, then we won't have assigned bandwidth,
4693 * yet it will be open. -RD
4695 // tor_assert(conn->read_bucket >= 0);
4697 // tor_assert(conn->addr && conn->port);
4698 tor_assert(conn->address);
4699 if (conn->state > OR_CONN_STATE_PROXY_HANDSHAKING)
4700 tor_assert(or_conn->tls);
4703 if (CONN_IS_EDGE(conn)) {
4704 /* XXX unchecked: package window, deliver window. */
4705 if (conn->type == CONN_TYPE_AP) {
4706 entry_connection_t *entry_conn = TO_ENTRY_CONN(conn);
4707 if (entry_conn->chosen_exit_optional || entry_conn->chosen_exit_retries)
4708 tor_assert(entry_conn->chosen_exit_name);
4710 tor_assert(entry_conn->socks_request);
4711 if (conn->state == AP_CONN_STATE_OPEN) {
4712 tor_assert(entry_conn->socks_request->has_finished);
4713 if (!conn->marked_for_close) {
4714 tor_assert(ENTRY_TO_EDGE_CONN(entry_conn)->cpath_layer);
4715 assert_cpath_layer_ok(ENTRY_TO_EDGE_CONN(entry_conn)->cpath_layer);
4719 if (conn->type == CONN_TYPE_EXIT) {
4720 tor_assert(conn->purpose == EXIT_PURPOSE_CONNECT ||
4721 conn->purpose == EXIT_PURPOSE_RESOLVE);
4723 } else if (conn->type == CONN_TYPE_DIR) {
4724 } else {
4725 /* Purpose is only used for dir and exit types currently */
4726 tor_assert(!conn->purpose);
4729 switch (conn->type)
4731 CASE_ANY_LISTENER_TYPE:
4732 tor_assert(conn->state == LISTENER_STATE_READY);
4733 break;
4734 case CONN_TYPE_OR:
4735 tor_assert(conn->state >= OR_CONN_STATE_MIN_);
4736 tor_assert(conn->state <= OR_CONN_STATE_MAX_);
4737 break;
4738 case CONN_TYPE_EXT_OR:
4739 tor_assert(conn->state >= EXT_OR_CONN_STATE_MIN_);
4740 tor_assert(conn->state <= EXT_OR_CONN_STATE_MAX_);
4741 break;
4742 case CONN_TYPE_EXIT:
4743 tor_assert(conn->state >= EXIT_CONN_STATE_MIN_);
4744 tor_assert(conn->state <= EXIT_CONN_STATE_MAX_);
4745 tor_assert(conn->purpose >= EXIT_PURPOSE_MIN_);
4746 tor_assert(conn->purpose <= EXIT_PURPOSE_MAX_);
4747 break;
4748 case CONN_TYPE_AP:
4749 tor_assert(conn->state >= AP_CONN_STATE_MIN_);
4750 tor_assert(conn->state <= AP_CONN_STATE_MAX_);
4751 tor_assert(TO_ENTRY_CONN(conn)->socks_request);
4752 break;
4753 case CONN_TYPE_DIR:
4754 tor_assert(conn->state >= DIR_CONN_STATE_MIN_);
4755 tor_assert(conn->state <= DIR_CONN_STATE_MAX_);
4756 tor_assert(conn->purpose >= DIR_PURPOSE_MIN_);
4757 tor_assert(conn->purpose <= DIR_PURPOSE_MAX_);
4758 break;
4759 case CONN_TYPE_CPUWORKER:
4760 tor_assert(conn->state >= CPUWORKER_STATE_MIN_);
4761 tor_assert(conn->state <= CPUWORKER_STATE_MAX_);
4762 break;
4763 case CONN_TYPE_CONTROL:
4764 tor_assert(conn->state >= CONTROL_CONN_STATE_MIN_);
4765 tor_assert(conn->state <= CONTROL_CONN_STATE_MAX_);
4766 break;
4767 default:
4768 tor_assert(0);
4772 /** Fills <b>addr</b> and <b>port</b> with the details of the global
4773 * proxy server we are using.
4774 * <b>conn</b> contains the connection we are using the proxy for.
4776 * Return 0 on success, -1 on failure.
4779 get_proxy_addrport(tor_addr_t *addr, uint16_t *port, int *proxy_type,
4780 const connection_t *conn)
4782 const or_options_t *options = get_options();
4784 if (options->HTTPSProxy) {
4785 tor_addr_copy(addr, &options->HTTPSProxyAddr);
4786 *port = options->HTTPSProxyPort;
4787 *proxy_type = PROXY_CONNECT;
4788 return 0;
4789 } else if (options->Socks4Proxy) {
4790 tor_addr_copy(addr, &options->Socks4ProxyAddr);
4791 *port = options->Socks4ProxyPort;
4792 *proxy_type = PROXY_SOCKS4;
4793 return 0;
4794 } else if (options->Socks5Proxy) {
4795 tor_addr_copy(addr, &options->Socks5ProxyAddr);
4796 *port = options->Socks5ProxyPort;
4797 *proxy_type = PROXY_SOCKS5;
4798 return 0;
4799 } else if (options->ClientTransportPlugin ||
4800 options->Bridges) {
4801 const transport_t *transport = NULL;
4802 int r;
4803 r = get_transport_by_bridge_addrport(&conn->addr, conn->port, &transport);
4804 if (r<0)
4805 return -1;
4806 if (transport) { /* transport found */
4807 tor_addr_copy(addr, &transport->addr);
4808 *port = transport->port;
4809 *proxy_type = transport->socks_version;
4810 return 0;
4814 tor_addr_make_unspec(addr);
4815 *port = 0;
4816 *proxy_type = PROXY_NONE;
4817 return 0;
4820 /** Log a failed connection to a proxy server.
4821 * <b>conn</b> is the connection we use the proxy server for. */
4822 void
4823 log_failed_proxy_connection(connection_t *conn)
4825 tor_addr_t proxy_addr;
4826 uint16_t proxy_port;
4827 int proxy_type;
4829 if (get_proxy_addrport(&proxy_addr, &proxy_port, &proxy_type, conn) != 0)
4830 return; /* if we have no proxy set up, leave this function. */
4832 log_warn(LD_NET,
4833 "The connection to the %s proxy server at %s just failed. "
4834 "Make sure that the proxy server is up and running.",
4835 proxy_type_to_string(get_proxy_type()),
4836 fmt_addrport(&proxy_addr, proxy_port));
4839 /** Return string representation of <b>proxy_type</b>. */
4840 static const char *
4841 proxy_type_to_string(int proxy_type)
4843 switch (proxy_type) {
4844 case PROXY_CONNECT: return "HTTP";
4845 case PROXY_SOCKS4: return "SOCKS4";
4846 case PROXY_SOCKS5: return "SOCKS5";
4847 case PROXY_PLUGGABLE: return "pluggable transports SOCKS";
4848 case PROXY_NONE: return "NULL";
4849 default: tor_assert(0);
4851 return NULL; /*Unreached*/
4854 /** Call connection_free_() on every connection in our array, and release all
4855 * storage held by connection.c. This is used by cpuworkers and dnsworkers
4856 * when they fork, so they don't keep resources held open (especially
4857 * sockets).
4859 * Don't do the checks in connection_free(), because they will
4860 * fail.
4862 void
4863 connection_free_all(void)
4865 smartlist_t *conns = get_connection_array();
4867 /* We don't want to log any messages to controllers. */
4868 SMARTLIST_FOREACH(conns, connection_t *, conn,
4869 if (conn->type == CONN_TYPE_CONTROL)
4870 TO_CONTROL_CONN(conn)->event_mask = 0);
4872 control_update_global_event_mask();
4874 /* Unlink everything from the identity map. */
4875 connection_or_clear_identity_map();
4876 connection_or_clear_ext_or_id_map();
4878 /* Clear out our list of broken connections */
4879 clear_broken_connection_map(0);
4881 SMARTLIST_FOREACH(conns, connection_t *, conn, connection_free_(conn));
4883 if (outgoing_addrs) {
4884 SMARTLIST_FOREACH(outgoing_addrs, tor_addr_t *, addr, tor_free(addr));
4885 smartlist_free(outgoing_addrs);
4886 outgoing_addrs = NULL;
4889 tor_free(last_interface_ipv4);
4890 tor_free(last_interface_ipv6);
4892 #ifdef USE_BUFFEREVENTS
4893 if (global_rate_limit)
4894 bufferevent_rate_limit_group_free(global_rate_limit);
4895 #endif