Clean up proposal 166 and its implementation.
[tor/rransom.git] / src / or / connection.c
blob48f82785739eef3a51b00338b2610ea9996c2ce1
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-2009, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
7 /**
8 * \file connection.c
9 * \brief General high-level functions to handle reading and writing
10 * on connections.
11 **/
13 #include "or.h"
15 static connection_t *connection_create_listener(
16 struct sockaddr *listensockaddr,
17 socklen_t listensocklen, int type,
18 char* address);
19 static void connection_init(time_t now, connection_t *conn, int type,
20 int socket_family);
21 static int connection_init_accepted_conn(connection_t *conn,
22 uint8_t listener_type);
23 static int connection_handle_listener_read(connection_t *conn, int new_type);
24 static int connection_read_bucket_should_increase(or_connection_t *conn);
25 static int connection_finished_flushing(connection_t *conn);
26 static int connection_flushed_some(connection_t *conn);
27 static int connection_finished_connecting(connection_t *conn);
28 static int connection_reached_eof(connection_t *conn);
29 static int connection_read_to_buf(connection_t *conn, int *max_to_read,
30 int *socket_error);
31 static int connection_process_inbuf(connection_t *conn, int package_partial);
32 static void client_check_address_changed(int sock);
33 static void set_constrained_socket_buffers(int sock, int size);
35 /** The last IPv4 address that our network interface seemed to have been
36 * binding to, in host order. We use this to detect when our IP changes. */
37 static uint32_t last_interface_ip = 0;
38 /** A list of uint32_ts for addresses we've used in outgoing connections.
39 * Used to detect IP address changes. */
40 static smartlist_t *outgoing_addrs = NULL;
42 /**************************************************************/
44 /**
45 * Return the human-readable name for the connection type <b>type</b>
47 const char *
48 conn_type_to_string(int type)
50 static char buf[64];
51 switch (type) {
52 case CONN_TYPE_OR_LISTENER: return "OR listener";
53 case CONN_TYPE_OR: return "OR";
54 case CONN_TYPE_EXIT: return "Exit";
55 case CONN_TYPE_AP_LISTENER: return "Socks listener";
56 case CONN_TYPE_AP_TRANS_LISTENER:
57 return "Transparent pf/netfilter listener";
58 case CONN_TYPE_AP_NATD_LISTENER: return "Transparent natd listener";
59 case CONN_TYPE_AP_DNS_LISTENER: return "DNS listener";
60 case CONN_TYPE_AP: return "Socks";
61 case CONN_TYPE_DIR_LISTENER: return "Directory listener";
62 case CONN_TYPE_DIR: return "Directory";
63 case CONN_TYPE_CPUWORKER: return "CPU worker";
64 case CONN_TYPE_CONTROL_LISTENER: return "Control listener";
65 case CONN_TYPE_CONTROL: return "Control";
66 default:
67 log_warn(LD_BUG, "unknown connection type %d", type);
68 tor_snprintf(buf, sizeof(buf), "unknown [%d]", type);
69 return buf;
73 /**
74 * Return the human-readable name for the connection state <b>state</b>
75 * for the connection type <b>type</b>
77 const char *
78 conn_state_to_string(int type, int state)
80 static char buf[96];
81 switch (type) {
82 case CONN_TYPE_OR_LISTENER:
83 case CONN_TYPE_AP_LISTENER:
84 case CONN_TYPE_AP_TRANS_LISTENER:
85 case CONN_TYPE_AP_NATD_LISTENER:
86 case CONN_TYPE_AP_DNS_LISTENER:
87 case CONN_TYPE_DIR_LISTENER:
88 case CONN_TYPE_CONTROL_LISTENER:
89 if (state == LISTENER_STATE_READY)
90 return "ready";
91 break;
92 case CONN_TYPE_OR:
93 switch (state) {
94 case OR_CONN_STATE_CONNECTING: return "connect()ing";
95 case OR_CONN_STATE_PROXY_FLUSHING: return "proxy flushing";
96 case OR_CONN_STATE_PROXY_READING: return "proxy reading";
97 case OR_CONN_STATE_TLS_HANDSHAKING: return "handshaking (TLS)";
98 case OR_CONN_STATE_TLS_CLIENT_RENEGOTIATING:
99 return "renegotiating (TLS)";
100 case OR_CONN_STATE_TLS_SERVER_RENEGOTIATING:
101 return "waiting for renegotiation (TLS)";
102 case OR_CONN_STATE_OR_HANDSHAKING: return "handshaking (Tor)";
103 case OR_CONN_STATE_OPEN: return "open";
105 break;
106 case CONN_TYPE_EXIT:
107 switch (state) {
108 case EXIT_CONN_STATE_RESOLVING: return "waiting for dest info";
109 case EXIT_CONN_STATE_CONNECTING: return "connecting";
110 case EXIT_CONN_STATE_OPEN: return "open";
111 case EXIT_CONN_STATE_RESOLVEFAILED: return "resolve failed";
113 break;
114 case CONN_TYPE_AP:
115 switch (state) {
116 case AP_CONN_STATE_SOCKS_WAIT: return "waiting for socks info";
117 case AP_CONN_STATE_NATD_WAIT: return "waiting for natd dest info";
118 case AP_CONN_STATE_RENDDESC_WAIT: return "waiting for rendezvous desc";
119 case AP_CONN_STATE_CONTROLLER_WAIT: return "waiting for controller";
120 case AP_CONN_STATE_CIRCUIT_WAIT: return "waiting for circuit";
121 case AP_CONN_STATE_CONNECT_WAIT: return "waiting for connect response";
122 case AP_CONN_STATE_RESOLVE_WAIT: return "waiting for resolve response";
123 case AP_CONN_STATE_OPEN: return "open";
125 break;
126 case CONN_TYPE_DIR:
127 switch (state) {
128 case DIR_CONN_STATE_CONNECTING: return "connecting";
129 case DIR_CONN_STATE_CLIENT_SENDING: return "client sending";
130 case DIR_CONN_STATE_CLIENT_READING: return "client reading";
131 case DIR_CONN_STATE_CLIENT_FINISHED: return "client finished";
132 case DIR_CONN_STATE_SERVER_COMMAND_WAIT: return "waiting for command";
133 case DIR_CONN_STATE_SERVER_WRITING: return "writing";
135 break;
136 case CONN_TYPE_CPUWORKER:
137 switch (state) {
138 case CPUWORKER_STATE_IDLE: return "idle";
139 case CPUWORKER_STATE_BUSY_ONION: return "busy with onion";
141 break;
142 case CONN_TYPE_CONTROL:
143 switch (state) {
144 case CONTROL_CONN_STATE_OPEN: return "open (protocol v1)";
145 case CONTROL_CONN_STATE_NEEDAUTH:
146 return "waiting for authentication (protocol v1)";
148 break;
151 log_warn(LD_BUG, "unknown connection state %d (type %d)", state, type);
152 tor_snprintf(buf, sizeof(buf),
153 "unknown state [%d] on unknown [%s] connection",
154 state, conn_type_to_string(type));
155 return buf;
158 /** Allocate and return a new dir_connection_t, initialized as by
159 * connection_init(). */
160 dir_connection_t *
161 dir_connection_new(int socket_family)
163 dir_connection_t *dir_conn = tor_malloc_zero(sizeof(dir_connection_t));
164 connection_init(time(NULL), TO_CONN(dir_conn), CONN_TYPE_DIR, socket_family);
165 return dir_conn;
168 /** Allocate and return a new or_connection_t, initialized as by
169 * connection_init(). */
170 or_connection_t *
171 or_connection_new(int socket_family)
173 or_connection_t *or_conn = tor_malloc_zero(sizeof(or_connection_t));
174 time_t now = time(NULL);
175 connection_init(now, TO_CONN(or_conn), CONN_TYPE_OR, socket_family);
177 or_conn->timestamp_last_added_nonpadding = time(NULL);
178 or_conn->next_circ_id = crypto_rand_int(1<<15);
180 return or_conn;
183 /** Allocate and return a new edge_connection_t, initialized as by
184 * connection_init(). */
185 edge_connection_t *
186 edge_connection_new(int type, int socket_family)
188 edge_connection_t *edge_conn = tor_malloc_zero(sizeof(edge_connection_t));
189 tor_assert(type == CONN_TYPE_EXIT || type == CONN_TYPE_AP);
190 connection_init(time(NULL), TO_CONN(edge_conn), type, socket_family);
191 if (type == CONN_TYPE_AP)
192 edge_conn->socks_request = tor_malloc_zero(sizeof(socks_request_t));
193 return edge_conn;
196 /** Allocate and return a new control_connection_t, initialized as by
197 * connection_init(). */
198 control_connection_t *
199 control_connection_new(int socket_family)
201 control_connection_t *control_conn =
202 tor_malloc_zero(sizeof(control_connection_t));
203 connection_init(time(NULL),
204 TO_CONN(control_conn), CONN_TYPE_CONTROL, socket_family);
205 return control_conn;
208 /** Allocate, initialize, and return a new connection_t subtype of <b>type</b>
209 * to make or receive connections of address family <b>socket_family</b>. The
210 * type should be one of the CONN_TYPE_* constants. */
211 connection_t *
212 connection_new(int type, int socket_family)
214 switch (type) {
215 case CONN_TYPE_OR:
216 return TO_CONN(or_connection_new(socket_family));
218 case CONN_TYPE_EXIT:
219 case CONN_TYPE_AP:
220 return TO_CONN(edge_connection_new(type, socket_family));
222 case CONN_TYPE_DIR:
223 return TO_CONN(dir_connection_new(socket_family));
225 case CONN_TYPE_CONTROL:
226 return TO_CONN(control_connection_new(socket_family));
228 default: {
229 connection_t *conn = tor_malloc_zero(sizeof(connection_t));
230 connection_init(time(NULL), conn, type, socket_family);
231 return conn;
236 /** Initializes conn. (you must call connection_add() to link it into the main
237 * array).
239 * Set conn-\>type to <b>type</b>. Set conn-\>s and conn-\>conn_array_index to
240 * -1 to signify they are not yet assigned.
242 * If conn is not a listener type, allocate buffers for it. If it's
243 * an AP type, allocate space to store the socks_request.
245 * Assign a pseudorandom next_circ_id between 0 and 2**15.
247 * Initialize conn's timestamps to now.
249 static void
250 connection_init(time_t now, connection_t *conn, int type, int socket_family)
252 static uint64_t n_connections_allocated = 1;
254 switch (type) {
255 case CONN_TYPE_OR:
256 conn->magic = OR_CONNECTION_MAGIC;
257 break;
258 case CONN_TYPE_EXIT:
259 case CONN_TYPE_AP:
260 conn->magic = EDGE_CONNECTION_MAGIC;
261 break;
262 case CONN_TYPE_DIR:
263 conn->magic = DIR_CONNECTION_MAGIC;
264 break;
265 case CONN_TYPE_CONTROL:
266 conn->magic = CONTROL_CONNECTION_MAGIC;
267 break;
268 default:
269 conn->magic = BASE_CONNECTION_MAGIC;
270 break;
273 conn->s = -1; /* give it a default of 'not used' */
274 conn->conn_array_index = -1; /* also default to 'not used' */
275 conn->global_identifier = n_connections_allocated++;
277 conn->type = type;
278 conn->socket_family = socket_family;
279 if (!connection_is_listener(conn)) { /* listeners never use their buf */
280 conn->inbuf = buf_new();
281 conn->outbuf = buf_new();
284 conn->timestamp_created = now;
285 conn->timestamp_lastread = now;
286 conn->timestamp_lastwritten = now;
289 /** Create a link between <b>conn_a</b> and <b>conn_b</b>. */
290 void
291 connection_link_connections(connection_t *conn_a, connection_t *conn_b)
293 tor_assert(conn_a->s < 0);
294 tor_assert(conn_b->s < 0);
296 conn_a->linked = 1;
297 conn_b->linked = 1;
298 conn_a->linked_conn = conn_b;
299 conn_b->linked_conn = conn_a;
302 /** Deallocate memory used by <b>conn</b>. Deallocate its buffers if
303 * necessary, close its socket if necessary, and mark the directory as dirty
304 * if <b>conn</b> is an OR or OP connection.
306 static void
307 _connection_free(connection_t *conn)
309 void *mem;
310 size_t memlen;
311 switch (conn->type) {
312 case CONN_TYPE_OR:
313 tor_assert(conn->magic == OR_CONNECTION_MAGIC);
314 mem = TO_OR_CONN(conn);
315 memlen = sizeof(or_connection_t);
316 break;
317 case CONN_TYPE_AP:
318 case CONN_TYPE_EXIT:
319 tor_assert(conn->magic == EDGE_CONNECTION_MAGIC);
320 mem = TO_EDGE_CONN(conn);
321 memlen = sizeof(edge_connection_t);
322 break;
323 case CONN_TYPE_DIR:
324 tor_assert(conn->magic == DIR_CONNECTION_MAGIC);
325 mem = TO_DIR_CONN(conn);
326 memlen = sizeof(dir_connection_t);
327 break;
328 case CONN_TYPE_CONTROL:
329 tor_assert(conn->magic == CONTROL_CONNECTION_MAGIC);
330 mem = TO_CONTROL_CONN(conn);
331 memlen = sizeof(control_connection_t);
332 break;
333 default:
334 tor_assert(conn->magic == BASE_CONNECTION_MAGIC);
335 mem = conn;
336 memlen = sizeof(connection_t);
337 break;
340 if (conn->linked) {
341 log_info(LD_GENERAL, "Freeing linked %s connection [%s] with %d "
342 "bytes on inbuf, %d on outbuf.",
343 conn_type_to_string(conn->type),
344 conn_state_to_string(conn->type, conn->state),
345 (int)buf_datalen(conn->inbuf), (int)buf_datalen(conn->outbuf));
348 if (!connection_is_listener(conn)) {
349 buf_free(conn->inbuf);
350 buf_free(conn->outbuf);
351 } else {
352 if (conn->socket_family == AF_UNIX) {
353 /* For now only control ports can be Unix domain sockets
354 * and listeners at the same time */
355 tor_assert(conn->type == CONN_TYPE_CONTROL_LISTENER);
357 if (unlink(conn->address) < 0 && errno != ENOENT) {
358 log_warn(LD_NET, "Could not unlink %s: %s", conn->address,
359 strerror(errno));
364 tor_free(conn->address);
366 if (connection_speaks_cells(conn)) {
367 or_connection_t *or_conn = TO_OR_CONN(conn);
368 if (or_conn->tls) {
369 tor_tls_free(or_conn->tls);
370 or_conn->tls = NULL;
372 if (or_conn->handshake_state) {
373 or_handshake_state_free(or_conn->handshake_state);
374 or_conn->handshake_state = NULL;
376 tor_free(or_conn->nickname);
378 if (CONN_IS_EDGE(conn)) {
379 edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
380 tor_free(edge_conn->chosen_exit_name);
381 if (edge_conn->socks_request) {
382 memset(edge_conn->socks_request, 0xcc, sizeof(socks_request_t));
383 tor_free(edge_conn->socks_request);
385 if (edge_conn->rend_data)
386 rend_data_free(edge_conn->rend_data);
388 if (conn->type == CONN_TYPE_CONTROL) {
389 control_connection_t *control_conn = TO_CONTROL_CONN(conn);
390 tor_free(control_conn->incoming_cmd);
393 tor_free(conn->read_event); /* Probably already freed by connection_free. */
394 tor_free(conn->write_event); /* Probably already freed by connection_free. */
396 if (conn->type == CONN_TYPE_DIR) {
397 dir_connection_t *dir_conn = TO_DIR_CONN(conn);
398 tor_free(dir_conn->requested_resource);
399 if (dir_conn->zlib_state)
400 tor_zlib_free(dir_conn->zlib_state);
401 if (dir_conn->fingerprint_stack) {
402 SMARTLIST_FOREACH(dir_conn->fingerprint_stack, char *, cp, tor_free(cp));
403 smartlist_free(dir_conn->fingerprint_stack);
405 if (dir_conn->cached_dir)
406 cached_dir_decref(dir_conn->cached_dir);
407 if (dir_conn->rend_data)
408 rend_data_free(dir_conn->rend_data);
411 if (conn->s >= 0) {
412 log_debug(LD_NET,"closing fd %d.",conn->s);
413 tor_close_socket(conn->s);
414 conn->s = -1;
417 if (conn->type == CONN_TYPE_OR &&
418 !tor_digest_is_zero(TO_OR_CONN(conn)->identity_digest)) {
419 log_warn(LD_BUG, "called on OR conn with non-zeroed identity_digest");
420 connection_or_remove_from_identity_map(TO_OR_CONN(conn));
423 memset(conn, 0xAA, memlen); /* poison memory */
424 tor_free(mem);
427 /** Make sure <b>conn</b> isn't in any of the global conn lists; then free it.
429 void
430 connection_free(connection_t *conn)
432 tor_assert(conn);
433 tor_assert(!connection_is_on_closeable_list(conn));
434 tor_assert(!connection_in_array(conn));
435 if (conn->linked_conn) {
436 log_err(LD_BUG, "Called with conn->linked_conn still set.");
437 tor_fragile_assert();
438 conn->linked_conn->linked_conn = NULL;
439 if (! conn->linked_conn->marked_for_close &&
440 conn->linked_conn->reading_from_linked_conn)
441 connection_start_reading(conn->linked_conn);
442 conn->linked_conn = NULL;
444 if (connection_speaks_cells(conn)) {
445 if (!tor_digest_is_zero(TO_OR_CONN(conn)->identity_digest)) {
446 connection_or_remove_from_identity_map(TO_OR_CONN(conn));
449 if (conn->type == CONN_TYPE_CONTROL) {
450 TO_CONTROL_CONN(conn)->event_mask = 0;
451 control_update_global_event_mask();
453 connection_unregister_events(conn);
454 _connection_free(conn);
457 /** Call _connection_free() on every connection in our array, and release all
458 * storage held by connection.c. This is used by cpuworkers and dnsworkers
459 * when they fork, so they don't keep resources held open (especially
460 * sockets).
462 * Don't do the checks in connection_free(), because they will
463 * fail.
465 void
466 connection_free_all(void)
468 smartlist_t *conns = get_connection_array();
470 /* We don't want to log any messages to controllers. */
471 SMARTLIST_FOREACH(conns, connection_t *, conn,
472 if (conn->type == CONN_TYPE_CONTROL)
473 TO_CONTROL_CONN(conn)->event_mask = 0);
475 control_update_global_event_mask();
477 /* Unlink everything from the identity map. */
478 connection_or_clear_identity_map();
480 SMARTLIST_FOREACH(conns, connection_t *, conn, _connection_free(conn));
482 if (outgoing_addrs) {
483 SMARTLIST_FOREACH(outgoing_addrs, void*, addr, tor_free(addr));
484 smartlist_free(outgoing_addrs);
485 outgoing_addrs = NULL;
489 /** Do any cleanup needed:
490 * - Directory conns that failed to fetch a rendezvous descriptor
491 * need to inform pending rendezvous streams.
492 * - OR conns need to call rep_hist_note_*() to record status.
493 * - AP conns need to send a socks reject if necessary.
494 * - Exit conns need to call connection_dns_remove() if necessary.
495 * - AP and Exit conns need to send an end cell if they can.
496 * - DNS conns need to fail any resolves that are pending on them.
497 * - OR and edge connections need to be unlinked from circuits.
499 void
500 connection_about_to_close_connection(connection_t *conn)
502 circuit_t *circ;
503 dir_connection_t *dir_conn;
504 or_connection_t *or_conn;
505 edge_connection_t *edge_conn;
506 time_t now = time(NULL);
508 tor_assert(conn->marked_for_close);
510 if (CONN_IS_EDGE(conn)) {
511 edge_conn = TO_EDGE_CONN(conn);
512 if (!edge_conn->edge_has_sent_end) {
513 log_warn(LD_BUG, "(Harmless.) Edge connection (marked at %s:%d) "
514 "hasn't sent end yet?",
515 conn->marked_for_close_file, conn->marked_for_close);
516 tor_fragile_assert();
520 switch (conn->type) {
521 case CONN_TYPE_DIR:
522 dir_conn = TO_DIR_CONN(conn);
523 if (conn->state < DIR_CONN_STATE_CLIENT_FINISHED) {
524 /* It's a directory connection and connecting or fetching
525 * failed: forget about this router, and maybe try again. */
526 connection_dir_request_failed(dir_conn);
528 /* If we were trying to fetch a v2 rend desc and did not succeed,
529 * retry as needed. (If a fetch is successful, the connection state
530 * is changed to DIR_PURPOSE_HAS_FETCHED_RENDDESC to mark that
531 * refetching is unnecessary.) */
532 if (conn->purpose == DIR_PURPOSE_FETCH_RENDDESC_V2 &&
533 dir_conn->rend_data &&
534 strlen(dir_conn->rend_data->onion_address) ==
535 REND_SERVICE_ID_LEN_BASE32)
536 rend_client_refetch_v2_renddesc(dir_conn->rend_data);
537 break;
538 case CONN_TYPE_OR:
539 or_conn = TO_OR_CONN(conn);
540 /* Remember why we're closing this connection. */
541 if (conn->state != OR_CONN_STATE_OPEN) {
542 /* Inform any pending (not attached) circs that they should
543 * give up. */
544 circuit_n_conn_done(TO_OR_CONN(conn), 0);
545 /* now mark things down as needed */
546 if (connection_or_nonopen_was_started_here(or_conn)) {
547 or_options_t *options = get_options();
548 rep_hist_note_connect_failed(or_conn->identity_digest, now);
549 entry_guard_register_connect_status(or_conn->identity_digest,0,
550 !options->HttpsProxy, now);
551 if (conn->state >= OR_CONN_STATE_TLS_HANDSHAKING) {
552 int reason = tls_error_to_orconn_end_reason(or_conn->tls_error);
553 control_event_or_conn_status(or_conn, OR_CONN_EVENT_FAILED,
554 reason);
555 if (!authdir_mode_tests_reachability(options))
556 control_event_bootstrap_problem(
557 orconn_end_reason_to_control_string(reason), reason);
560 } else if (conn->hold_open_until_flushed) {
561 /* We only set hold_open_until_flushed when we're intentionally
562 * closing a connection. */
563 rep_hist_note_disconnect(or_conn->identity_digest, now);
564 control_event_or_conn_status(or_conn, OR_CONN_EVENT_CLOSED,
565 tls_error_to_orconn_end_reason(or_conn->tls_error));
566 } else if (or_conn->identity_digest) {
567 rep_hist_note_connection_died(or_conn->identity_digest, now);
568 control_event_or_conn_status(or_conn, OR_CONN_EVENT_CLOSED,
569 tls_error_to_orconn_end_reason(or_conn->tls_error));
571 /* Now close all the attached circuits on it. */
572 circuit_unlink_all_from_or_conn(TO_OR_CONN(conn),
573 END_CIRC_REASON_OR_CONN_CLOSED);
574 break;
575 case CONN_TYPE_AP:
576 edge_conn = TO_EDGE_CONN(conn);
577 if (edge_conn->socks_request->has_finished == 0) {
578 /* since conn gets removed right after this function finishes,
579 * there's no point trying to send back a reply at this point. */
580 log_warn(LD_BUG,"Closing stream (marked at %s:%d) without sending"
581 " back a socks reply.",
582 conn->marked_for_close_file, conn->marked_for_close);
584 if (!edge_conn->end_reason) {
585 log_warn(LD_BUG,"Closing stream (marked at %s:%d) without having"
586 " set end_reason.",
587 conn->marked_for_close_file, conn->marked_for_close);
589 if (edge_conn->dns_server_request) {
590 log_warn(LD_BUG,"Closing stream (marked at %s:%d) without having"
591 " replied to DNS request.",
592 conn->marked_for_close_file, conn->marked_for_close);
593 dnsserv_reject_request(edge_conn);
595 control_event_stream_bandwidth(edge_conn);
596 control_event_stream_status(edge_conn, STREAM_EVENT_CLOSED,
597 edge_conn->end_reason);
598 circ = circuit_get_by_edge_conn(edge_conn);
599 if (circ)
600 circuit_detach_stream(circ, edge_conn);
601 break;
602 case CONN_TYPE_EXIT:
603 edge_conn = TO_EDGE_CONN(conn);
604 circ = circuit_get_by_edge_conn(edge_conn);
605 if (circ)
606 circuit_detach_stream(circ, edge_conn);
607 if (conn->state == EXIT_CONN_STATE_RESOLVING) {
608 connection_dns_remove(edge_conn);
610 break;
614 /** Return true iff connection_close_immediate() has been called on this
615 * connection. */
616 #define CONN_IS_CLOSED(c) \
617 ((c)->linked ? ((c)->linked_conn_is_closed) : ((c)->s < 0))
619 /** Close the underlying socket for <b>conn</b>, so we don't try to
620 * flush it. Must be used in conjunction with (right before)
621 * connection_mark_for_close().
623 void
624 connection_close_immediate(connection_t *conn)
626 assert_connection_ok(conn,0);
627 if (CONN_IS_CLOSED(conn)) {
628 log_err(LD_BUG,"Attempt to close already-closed connection.");
629 tor_fragile_assert();
630 return;
632 if (conn->outbuf_flushlen) {
633 log_info(LD_NET,"fd %d, type %s, state %s, %d bytes on outbuf.",
634 conn->s, conn_type_to_string(conn->type),
635 conn_state_to_string(conn->type, conn->state),
636 (int)conn->outbuf_flushlen);
639 connection_unregister_events(conn);
641 if (conn->s >= 0)
642 tor_close_socket(conn->s);
643 conn->s = -1;
644 if (conn->linked)
645 conn->linked_conn_is_closed = 1;
646 if (!connection_is_listener(conn)) {
647 buf_clear(conn->outbuf);
648 conn->outbuf_flushlen = 0;
652 /** Mark <b>conn</b> to be closed next time we loop through
653 * conn_close_if_marked() in main.c. */
654 void
655 _connection_mark_for_close(connection_t *conn, int line, const char *file)
657 assert_connection_ok(conn,0);
658 tor_assert(line);
659 tor_assert(line < 1<<16); /* marked_for_close can only fit a uint16_t. */
660 tor_assert(file);
662 if (conn->marked_for_close) {
663 log(LOG_WARN,LD_BUG,"Duplicate call to connection_mark_for_close at %s:%d"
664 " (first at %s:%d)", file, line, conn->marked_for_close_file,
665 conn->marked_for_close);
666 tor_fragile_assert();
667 return;
670 conn->marked_for_close = line;
671 conn->marked_for_close_file = file;
672 add_connection_to_closeable_list(conn);
674 /* in case we're going to be held-open-til-flushed, reset
675 * the number of seconds since last successful write, so
676 * we get our whole 15 seconds */
677 conn->timestamp_lastwritten = time(NULL);
680 /** Find each connection that has hold_open_until_flushed set to
681 * 1 but hasn't written in the past 15 seconds, and set
682 * hold_open_until_flushed to 0. This means it will get cleaned
683 * up in the next loop through close_if_marked() in main.c.
685 void
686 connection_expire_held_open(void)
688 time_t now;
689 smartlist_t *conns = get_connection_array();
691 now = time(NULL);
693 SMARTLIST_FOREACH(conns, connection_t *, conn,
695 /* If we've been holding the connection open, but we haven't written
696 * for 15 seconds...
698 if (conn->hold_open_until_flushed) {
699 tor_assert(conn->marked_for_close);
700 if (now - conn->timestamp_lastwritten >= 15) {
701 int severity;
702 if (conn->type == CONN_TYPE_EXIT ||
703 (conn->type == CONN_TYPE_DIR &&
704 conn->purpose == DIR_PURPOSE_SERVER))
705 severity = LOG_INFO;
706 else
707 severity = LOG_NOTICE;
708 log_fn(severity, LD_NET,
709 "Giving up on marked_for_close conn that's been flushing "
710 "for 15s (fd %d, type %s, state %s).",
711 conn->s, conn_type_to_string(conn->type),
712 conn_state_to_string(conn->type, conn->state));
713 conn->hold_open_until_flushed = 0;
719 /** Create an AF_INET listenaddr struct.
720 * <b>listenaddress</b> provides the host and optionally the port information
721 * for the new structure. If no port is provided in <b>listenaddress</b> then
722 * <b>listenport</b> is used.
724 * If not NULL <b>readable_address</b> will contain a copy of the host part of
725 * <b>listenaddress</b>.
727 * The listenaddr struct has to be freed by the caller.
729 static struct sockaddr_in *
730 create_inet_sockaddr(const char *listenaddress, uint16_t listenport,
731 char **readable_address, socklen_t *socklen_out) {
732 struct sockaddr_in *listenaddr = NULL;
733 uint32_t addr;
734 uint16_t usePort = 0;
736 if (parse_addr_port(LOG_WARN,
737 listenaddress, readable_address, &addr, &usePort)<0) {
738 log_warn(LD_CONFIG,
739 "Error parsing/resolving ListenAddress %s", listenaddress);
740 goto err;
742 if (usePort==0)
743 usePort = listenport;
745 listenaddr = tor_malloc_zero(sizeof(struct sockaddr_in));
746 listenaddr->sin_addr.s_addr = htonl(addr);
747 listenaddr->sin_family = AF_INET;
748 listenaddr->sin_port = htons((uint16_t) usePort);
750 *socklen_out = sizeof(struct sockaddr_in);
752 return listenaddr;
754 err:
755 tor_free(listenaddr);
756 return NULL;
759 #ifdef HAVE_SYS_UN_H
760 /** Create an AF_UNIX listenaddr struct.
761 * <b>listenaddress</b> provides the path to the Unix socket.
763 * Eventually <b>listenaddress</b> will also optionally contain user, group,
764 * and file permissions for the new socket. But not yet. XXX
765 * Also, since we do not create the socket here the information doesn't help
766 * here.
768 * If not NULL <b>readable_address</b> will contain a copy of the path part of
769 * <b>listenaddress</b>.
771 * The listenaddr struct has to be freed by the caller.
773 static struct sockaddr_un *
774 create_unix_sockaddr(const char *listenaddress, char **readable_address,
775 socklen_t *len_out)
777 struct sockaddr_un *sockaddr = NULL;
779 sockaddr = tor_malloc_zero(sizeof(struct sockaddr_un));
780 sockaddr->sun_family = AF_UNIX;
781 strncpy(sockaddr->sun_path, listenaddress, sizeof(sockaddr->sun_path));
783 if (readable_address)
784 *readable_address = tor_strdup(listenaddress);
786 *len_out = sizeof(struct sockaddr_un);
787 return sockaddr;
789 #else
790 static struct sockaddr *
791 create_unix_sockaddr(const char *listenaddress, char **readable_address,
792 socklen_t *len_out)
794 (void)listenaddress;
795 (void)readable_address;
796 log_fn(LOG_ERR, LD_BUG,
797 "Unix domain sockets not supported, yet we tried to create one.");
798 *len_out = 0;
799 tor_assert(0);
801 #endif /* HAVE_SYS_UN_H */
803 /** Warn that an accept or a connect has failed because we're running up
804 * against our ulimit. Rate-limit these warnings so that we don't spam
805 * the log. */
806 static void
807 warn_too_many_conns(void)
809 #define WARN_TOO_MANY_CONNS_INTERVAL (6*60*60)
810 static time_t last_warned = 0;
811 time_t now = time(NULL);
812 int n_conns = get_n_open_sockets();
813 if (last_warned + WARN_TOO_MANY_CONNS_INTERVAL < now) {
814 log_warn(LD_NET,"Failing because we have %d connections already. Please "
815 "raise your ulimit -n.", n_conns);
816 last_warned = now;
818 control_event_general_status(LOG_WARN, "TOO_MANY_CONNECTIONS CURRENT=%d",
819 n_conns);
822 /** Bind a new non-blocking socket listening to the socket described
823 * by <b>listensockaddr</b>.
825 * <b>address</b> is only used for logging purposes and to add the information
826 * to the conn.
828 static connection_t *
829 connection_create_listener(struct sockaddr *listensockaddr, socklen_t socklen,
830 int type, char* address)
832 connection_t *conn;
833 int s; /* the socket we're going to make */
834 uint16_t usePort = 0;
835 int start_reading = 0;
837 if (get_n_open_sockets() >= get_options()->_ConnLimit-1) {
838 warn_too_many_conns();
839 return NULL;
842 if (listensockaddr->sa_family == AF_INET) {
843 int is_tcp = (type != CONN_TYPE_AP_DNS_LISTENER);
844 #ifndef MS_WINDOWS
845 int one=1;
846 #endif
847 if (is_tcp)
848 start_reading = 1;
850 usePort = ntohs( (uint16_t)
851 ((struct sockaddr_in *)listensockaddr)->sin_port);
853 log_notice(LD_NET, "Opening %s on %s:%d",
854 conn_type_to_string(type), address, usePort);
856 s = tor_open_socket(PF_INET,
857 is_tcp ? SOCK_STREAM : SOCK_DGRAM,
858 is_tcp ? IPPROTO_TCP: IPPROTO_UDP);
859 if (s < 0) {
860 log_warn(LD_NET,"Socket creation failed.");
861 goto err;
864 #ifndef MS_WINDOWS
865 /* REUSEADDR on normal places means you can rebind to the port
866 * right after somebody else has let it go. But REUSEADDR on win32
867 * means you can bind to the port _even when somebody else
868 * already has it bound_. So, don't do that on Win32. */
869 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (void*) &one,
870 (socklen_t)sizeof(one));
871 #endif
873 if (bind(s,listensockaddr,socklen) < 0) {
874 const char *helpfulhint = "";
875 int e = tor_socket_errno(s);
876 if (ERRNO_IS_EADDRINUSE(e))
877 helpfulhint = ". Is Tor already running?";
878 log_warn(LD_NET, "Could not bind to %s:%u: %s%s", address, usePort,
879 tor_socket_strerror(e), helpfulhint);
880 tor_close_socket(s);
881 goto err;
884 if (is_tcp) {
885 if (listen(s,SOMAXCONN) < 0) {
886 log_warn(LD_NET, "Could not listen on %s:%u: %s", address, usePort,
887 tor_socket_strerror(tor_socket_errno(s)));
888 tor_close_socket(s);
889 goto err;
892 #ifdef HAVE_SYS_UN_H
893 } else if (listensockaddr->sa_family == AF_UNIX) {
894 start_reading = 1;
896 /* For now only control ports can be Unix domain sockets
897 * and listeners at the same time */
898 tor_assert(type == CONN_TYPE_CONTROL_LISTENER);
900 log_notice(LD_NET, "Opening %s on %s",
901 conn_type_to_string(type), address);
903 if (unlink(address) < 0 && errno != ENOENT) {
904 log_warn(LD_NET, "Could not unlink %s: %s", address,
905 strerror(errno));
906 goto err;
908 s = tor_open_socket(AF_UNIX, SOCK_STREAM, 0);
909 if (s < 0) {
910 log_warn(LD_NET,"Socket creation failed: %s.", strerror(errno));
911 goto err;
914 if (bind(s, listensockaddr, (socklen_t)sizeof(struct sockaddr_un)) == -1) {
915 log_warn(LD_NET,"Bind to %s failed: %s.", address,
916 tor_socket_strerror(tor_socket_errno(s)));
917 goto err;
920 if (listen(s,SOMAXCONN) < 0) {
921 log_warn(LD_NET, "Could not listen on %s: %s", address,
922 tor_socket_strerror(tor_socket_errno(s)));
923 tor_close_socket(s);
924 goto err;
926 #endif /* HAVE_SYS_UN_H */
927 } else {
928 log_err(LD_BUG,"Got unexpected address family %d.",
929 listensockaddr->sa_family);
930 tor_assert(0);
933 set_socket_nonblocking(s);
935 conn = connection_new(type, listensockaddr->sa_family);
936 conn->socket_family = listensockaddr->sa_family;
937 conn->s = s;
938 conn->address = tor_strdup(address);
939 conn->port = usePort;
941 if (connection_add(conn) < 0) { /* no space, forget it */
942 log_warn(LD_NET,"connection_add for listener failed. Giving up.");
943 connection_free(conn);
944 goto err;
947 log_debug(LD_NET,"%s listening on port %u.",
948 conn_type_to_string(type), usePort);
950 conn->state = LISTENER_STATE_READY;
951 if (start_reading) {
952 connection_start_reading(conn);
953 } else {
954 tor_assert(type == CONN_TYPE_AP_DNS_LISTENER);
955 dnsserv_configure_listener(conn);
958 return conn;
960 err:
961 return NULL;
964 /** Do basic sanity checking on a newly received socket. Return 0
965 * if it looks ok, else return -1. */
966 static int
967 check_sockaddr(struct sockaddr *sa, int len, int level)
969 int ok = 1;
971 if (sa->sa_family == AF_INET) {
972 struct sockaddr_in *sin=(struct sockaddr_in*)sa;
973 if (len != sizeof(struct sockaddr_in)) {
974 log_fn(level, LD_NET, "Length of address not as expected: %d vs %d",
975 len,(int)sizeof(struct sockaddr_in));
976 ok = 0;
978 if (sin->sin_addr.s_addr == 0 || sin->sin_port == 0) {
979 log_fn(level, LD_NET,
980 "Address for new connection has address/port equal to zero.");
981 ok = 0;
983 } else if (sa->sa_family == AF_INET6) {
984 struct sockaddr_in6 *sin6=(struct sockaddr_in6*)sa;
985 if (len != sizeof(struct sockaddr_in6)) {
986 log_fn(level, LD_NET, "Length of address not as expected: %d vs %d",
987 len,(int)sizeof(struct sockaddr_in6));
988 ok = 0;
990 if (tor_mem_is_zero((void*)sin6->sin6_addr.s6_addr, 16) ||
991 sin6->sin6_port == 0) {
992 log_fn(level, LD_NET,
993 "Address for new connection has address/port equal to zero.");
994 ok = 0;
996 } else {
997 ok = 0;
999 return ok ? 0 : -1;
1002 /** Check whether the socket family from an accepted socket <b>got</b> is the
1003 * same as the one that <b>listener</b> is waiting for. If it isn't, log
1004 * a useful message and return -1. Else return 0.
1006 * This is annoying, but can apparently happen on some Darwins. */
1007 static int
1008 check_sockaddr_family_match(sa_family_t got, connection_t *listener)
1010 if (got != listener->socket_family) {
1011 log_info(LD_BUG, "A listener connection returned a socket with a "
1012 "mismatched family. %s for addr_family %d gave us a socket "
1013 "with address family %d. Dropping.",
1014 conn_type_to_string(listener->type),
1015 (int)listener->socket_family,
1016 (int)got);
1017 return -1;
1019 return 0;
1022 /** The listener connection <b>conn</b> told poll() it wanted to read.
1023 * Call accept() on conn-\>s, and add the new connection if necessary.
1025 static int
1026 connection_handle_listener_read(connection_t *conn, int new_type)
1028 int news; /* the new socket */
1029 connection_t *newconn;
1030 /* information about the remote peer when connecting to other routers */
1031 char addrbuf[256];
1032 struct sockaddr *remote = (struct sockaddr*)addrbuf;
1033 /* length of the remote address. Must be whatever accept() needs. */
1034 socklen_t remotelen = (socklen_t)sizeof(addrbuf);
1035 or_options_t *options = get_options();
1037 tor_assert((size_t)remotelen >= sizeof(struct sockaddr_in));
1038 memset(addrbuf, 0, sizeof(addrbuf));
1040 news = tor_accept_socket(conn->s,remote,&remotelen);
1041 if (news < 0) { /* accept() error */
1042 int e = tor_socket_errno(conn->s);
1043 if (ERRNO_IS_ACCEPT_EAGAIN(e)) {
1044 return 0; /* he hung up before we could accept(). that's fine. */
1045 } else if (ERRNO_IS_ACCEPT_RESOURCE_LIMIT(e)) {
1046 warn_too_many_conns();
1047 return 0;
1049 /* else there was a real error. */
1050 log_warn(LD_NET,"accept() failed: %s. Closing listener.",
1051 tor_socket_strerror(e));
1052 connection_mark_for_close(conn);
1053 return -1;
1055 log_debug(LD_NET,
1056 "Connection accepted on socket %d (child of fd %d).",
1057 news,conn->s);
1059 set_socket_nonblocking(news);
1061 if (options->ConstrainedSockets)
1062 set_constrained_socket_buffers(news, (int)options->ConstrainedSockSize);
1064 if (check_sockaddr_family_match(remote->sa_family, conn) < 0) {
1065 tor_close_socket(news);
1066 return 0;
1069 if (conn->socket_family == AF_INET || conn->socket_family == AF_INET6) {
1070 tor_addr_t addr;
1071 uint16_t port;
1072 if (check_sockaddr(remote, remotelen, LOG_INFO)<0) {
1073 log_info(LD_NET,
1074 "accept() returned a strange address; trying getsockname().");
1075 remotelen=sizeof(addrbuf);
1076 memset(addrbuf, 0, sizeof(addrbuf));
1077 if (getsockname(news, remote, &remotelen)<0) {
1078 int e = tor_socket_errno(news);
1079 log_warn(LD_NET, "getsockname() for new connection failed: %s",
1080 tor_socket_strerror(e));
1081 } else {
1082 if (check_sockaddr((struct sockaddr*)addrbuf, remotelen,
1083 LOG_WARN) < 0) {
1084 log_warn(LD_NET,"Something's wrong with this conn. Closing it.");
1085 tor_close_socket(news);
1086 return 0;
1091 if (check_sockaddr_family_match(remote->sa_family, conn) < 0) {
1092 tor_close_socket(news);
1093 return 0;
1096 tor_addr_from_sockaddr(&addr, remote, &port);
1098 /* process entrance policies here, before we even create the connection */
1099 if (new_type == CONN_TYPE_AP) {
1100 /* check sockspolicy to see if we should accept it */
1101 if (socks_policy_permits_address(&addr) == 0) {
1102 log_notice(LD_APP,
1103 "Denying socks connection from untrusted address %s.",
1104 fmt_addr(&addr));
1105 tor_close_socket(news);
1106 return 0;
1109 if (new_type == CONN_TYPE_DIR) {
1110 /* check dirpolicy to see if we should accept it */
1111 if (dir_policy_permits_address(&addr) == 0) {
1112 log_notice(LD_DIRSERV,"Denying dir connection from address %s.",
1113 fmt_addr(&addr));
1114 tor_close_socket(news);
1115 return 0;
1119 newconn = connection_new(new_type, conn->socket_family);
1120 newconn->s = news;
1122 /* remember the remote address */
1123 tor_addr_copy(&newconn->addr, &addr);
1124 newconn->port = port;
1125 newconn->address = tor_dup_addr(&addr);
1127 } else if (conn->socket_family == AF_UNIX) {
1128 /* For now only control ports can be Unix domain sockets
1129 * and listeners at the same time */
1130 tor_assert(conn->type == CONN_TYPE_CONTROL_LISTENER);
1132 newconn = connection_new(new_type, conn->socket_family);
1133 newconn->s = news;
1135 /* remember the remote address -- do we have anything sane to put here? */
1136 tor_addr_make_unspec(&newconn->addr);
1137 newconn->port = 1;
1138 newconn->address = tor_strdup(conn->address);
1139 } else {
1140 tor_assert(0);
1143 if (connection_add(newconn) < 0) { /* no space, forget it */
1144 connection_free(newconn);
1145 return 0; /* no need to tear down the parent */
1148 if (connection_init_accepted_conn(newconn, conn->type) < 0) {
1149 connection_mark_for_close(newconn);
1150 return 0;
1152 return 0;
1155 /** Initialize states for newly accepted connection <b>conn</b>.
1156 * If conn is an OR, start the TLS handshake.
1157 * If conn is a transparent AP, get its original destination
1158 * and place it in circuit_wait.
1160 static int
1161 connection_init_accepted_conn(connection_t *conn, uint8_t listener_type)
1163 connection_start_reading(conn);
1165 switch (conn->type) {
1166 case CONN_TYPE_OR:
1167 control_event_or_conn_status(TO_OR_CONN(conn), OR_CONN_EVENT_NEW, 0);
1168 return connection_tls_start_handshake(TO_OR_CONN(conn), 1);
1169 case CONN_TYPE_AP:
1170 switch (listener_type) {
1171 case CONN_TYPE_AP_LISTENER:
1172 conn->state = AP_CONN_STATE_SOCKS_WAIT;
1173 break;
1174 case CONN_TYPE_AP_TRANS_LISTENER:
1175 conn->state = AP_CONN_STATE_CIRCUIT_WAIT;
1176 return connection_ap_process_transparent(TO_EDGE_CONN(conn));
1177 case CONN_TYPE_AP_NATD_LISTENER:
1178 conn->state = AP_CONN_STATE_NATD_WAIT;
1179 break;
1181 break;
1182 case CONN_TYPE_DIR:
1183 conn->purpose = DIR_PURPOSE_SERVER;
1184 conn->state = DIR_CONN_STATE_SERVER_COMMAND_WAIT;
1185 break;
1186 case CONN_TYPE_CONTROL:
1187 conn->state = CONTROL_CONN_STATE_NEEDAUTH;
1188 break;
1190 return 0;
1193 /** Take conn, make a nonblocking socket; try to connect to
1194 * addr:port (they arrive in *host order*). If fail, return -1 and if
1195 * applicable put your best guess about errno into *<b>socket_error</b>.
1196 * Else assign s to conn-\>s: if connected return 1, if EAGAIN return 0.
1198 * address is used to make the logs useful.
1200 * On success, add conn to the list of polled connections.
1203 connection_connect(connection_t *conn, const char *address,
1204 const tor_addr_t *addr, uint16_t port, int *socket_error)
1206 int s, inprogress = 0;
1207 char addrbuf[256];
1208 struct sockaddr *dest_addr = (struct sockaddr*) addrbuf;
1209 socklen_t dest_addr_len;
1210 or_options_t *options = get_options();
1211 int protocol_family;
1213 if (get_n_open_sockets() >= get_options()->_ConnLimit-1) {
1214 warn_too_many_conns();
1215 return -1;
1218 if (tor_addr_family(addr) == AF_INET6)
1219 protocol_family = PF_INET6;
1220 else
1221 protocol_family = PF_INET;
1223 s = tor_open_socket(protocol_family,SOCK_STREAM,IPPROTO_TCP);
1224 if (s < 0) {
1225 *socket_error = tor_socket_errno(-1);
1226 log_warn(LD_NET,"Error creating network socket: %s",
1227 tor_socket_strerror(*socket_error));
1228 return -1;
1231 if (options->OutboundBindAddress) {
1232 struct sockaddr_in ext_addr;
1234 memset(&ext_addr, 0, sizeof(ext_addr));
1235 ext_addr.sin_family = AF_INET;
1236 ext_addr.sin_port = 0;
1237 if (!tor_inet_aton(options->OutboundBindAddress, &ext_addr.sin_addr)) {
1238 log_warn(LD_CONFIG,"Outbound bind address '%s' didn't parse. Ignoring.",
1239 options->OutboundBindAddress);
1240 } else {
1241 if (bind(s, (struct sockaddr*)&ext_addr,
1242 (socklen_t)sizeof(ext_addr)) < 0) {
1243 *socket_error = tor_socket_errno(s);
1244 log_warn(LD_NET,"Error binding network socket: %s",
1245 tor_socket_strerror(*socket_error));
1246 tor_close_socket(s);
1247 return -1;
1252 set_socket_nonblocking(s);
1254 if (options->ConstrainedSockets)
1255 set_constrained_socket_buffers(s, (int)options->ConstrainedSockSize);
1257 memset(addrbuf,0,sizeof(addrbuf));
1258 dest_addr = (struct sockaddr*) addrbuf;
1259 dest_addr_len = tor_addr_to_sockaddr(addr, port, dest_addr, sizeof(addrbuf));
1260 tor_assert(dest_addr_len > 0);
1262 log_debug(LD_NET,"Connecting to %s:%u.",escaped_safe_str(address),port);
1264 if (connect(s, dest_addr, dest_addr_len) < 0) {
1265 int e = tor_socket_errno(s);
1266 if (!ERRNO_IS_CONN_EINPROGRESS(e)) {
1267 /* yuck. kill it. */
1268 *socket_error = e;
1269 log_info(LD_NET,
1270 "connect() to %s:%u failed: %s",escaped_safe_str(address),
1271 port, tor_socket_strerror(e));
1272 tor_close_socket(s);
1273 return -1;
1274 } else {
1275 inprogress = 1;
1279 if (!server_mode(options))
1280 client_check_address_changed(s);
1282 /* it succeeded. we're connected. */
1283 log_fn(inprogress?LOG_DEBUG:LOG_INFO, LD_NET,
1284 "Connection to %s:%u %s (sock %d).",escaped_safe_str(address),
1285 port, inprogress?"in progress":"established", s);
1286 conn->s = s;
1287 if (connection_add(conn) < 0) /* no space, forget it */
1288 return -1;
1289 return inprogress ? 0 : 1;
1293 * Launch any configured listener connections of type <b>type</b>. (A
1294 * listener is configured if <b>port_option</b> is non-zero. If any
1295 * ListenAddress configuration options are given in <b>cfg</b>, create a
1296 * connection binding to each one. Otherwise, create a single
1297 * connection binding to the address <b>default_addr</b>.)
1299 * Only launch the listeners of this type that are not already open, and
1300 * only close listeners that are no longer wanted. Existing listeners
1301 * that are still configured are not touched.
1303 * If <b>disable_all_conns</b> is set, then never open new conns, and
1304 * close the existing ones.
1306 * Add all old conns that should be closed to <b>replaced_conns</b>.
1307 * Add all new connections to <b>new_conns</b>.
1309 static int
1310 retry_listeners(int type, config_line_t *cfg,
1311 int port_option, const char *default_addr,
1312 smartlist_t *replaced_conns,
1313 smartlist_t *new_conns,
1314 int disable_all_conns,
1315 int socket_family)
1317 smartlist_t *launch = smartlist_create(), *conns;
1318 int free_launch_elts = 1;
1319 int r;
1320 config_line_t *c;
1321 connection_t *conn;
1322 config_line_t *line;
1324 tor_assert(socket_family == AF_INET || socket_family == AF_UNIX);
1326 if (cfg && port_option) {
1327 for (c = cfg; c; c = c->next) {
1328 smartlist_add(launch, c);
1330 free_launch_elts = 0;
1331 } else if (port_option) {
1332 line = tor_malloc_zero(sizeof(config_line_t));
1333 line->key = tor_strdup("");
1334 line->value = tor_strdup(default_addr);
1335 smartlist_add(launch, line);
1339 SMARTLIST_FOREACH(launch, config_line_t *, l,
1340 log_fn(LOG_NOTICE, "#%s#%s", l->key, l->value));
1343 conns = get_connection_array();
1344 SMARTLIST_FOREACH(conns, connection_t *, conn,
1346 if (conn->type != type ||
1347 conn->socket_family != socket_family ||
1348 conn->marked_for_close)
1349 continue;
1350 /* Okay, so this is a listener. Is it configured? */
1351 line = NULL;
1352 SMARTLIST_FOREACH(launch, config_line_t *, wanted,
1354 char *address=NULL;
1355 uint16_t port;
1356 switch (socket_family) {
1357 case AF_INET:
1358 if (!parse_addr_port(LOG_WARN,
1359 wanted->value, &address, NULL, &port)) {
1360 int addr_matches = !strcasecmp(address, conn->address);
1361 tor_free(address);
1362 if (! port)
1363 port = port_option;
1364 if (port == conn->port && addr_matches) {
1365 line = wanted;
1366 break;
1369 break;
1370 case AF_UNIX:
1371 if (!strcasecmp(wanted->value, conn->address)) {
1372 line = wanted;
1373 break;
1375 break;
1376 default:
1377 tor_assert(0);
1380 if (!line || disable_all_conns) {
1381 /* This one isn't configured. Close it. */
1382 log_notice(LD_NET, "Closing no-longer-configured %s on %s:%d",
1383 conn_type_to_string(type), conn->address, conn->port);
1384 if (replaced_conns) {
1385 smartlist_add(replaced_conns, conn);
1386 } else {
1387 connection_close_immediate(conn);
1388 connection_mark_for_close(conn);
1390 } else {
1391 /* It's configured; we don't need to launch it. */
1392 // log_debug(LD_NET, "Already have %s on %s:%d",
1393 // conn_type_to_string(type), conn->address, conn->port);
1394 smartlist_remove(launch, line);
1395 if (free_launch_elts)
1396 config_free_lines(line);
1400 /* Now open all the listeners that are configured but not opened. */
1401 r = 0;
1402 if (!disable_all_conns) {
1403 SMARTLIST_FOREACH_BEGIN(launch, config_line_t *, cfg_line) {
1404 char *address = NULL;
1405 struct sockaddr *listensockaddr;
1406 socklen_t listensocklen = 0;
1408 switch (socket_family) {
1409 case AF_INET:
1410 listensockaddr = (struct sockaddr *)
1411 create_inet_sockaddr(cfg_line->value,
1412 (uint16_t) port_option,
1413 &address, &listensocklen);
1414 break;
1415 case AF_UNIX:
1416 listensockaddr = (struct sockaddr *)
1417 create_unix_sockaddr(cfg_line->value,
1418 &address, &listensocklen);
1419 break;
1420 default:
1421 tor_assert(0);
1424 if (listensockaddr) {
1425 conn = connection_create_listener(listensockaddr, listensocklen,
1426 type, address);
1427 tor_free(listensockaddr);
1428 tor_free(address);
1429 } else
1430 conn = NULL;
1432 if (!conn) {
1433 r = -1;
1434 } else {
1435 if (new_conns)
1436 smartlist_add(new_conns, conn);
1438 } SMARTLIST_FOREACH_END(cfg_line);
1441 if (free_launch_elts) {
1442 SMARTLIST_FOREACH(launch, config_line_t *, cfg_line,
1443 config_free_lines(cfg_line));
1445 smartlist_free(launch);
1447 return r;
1450 /** Launch listeners for each port you should have open. Only launch
1451 * listeners who are not already open, and only close listeners we no longer
1452 * want.
1454 * Add all old conns that should be closed to <b>replaced_conns</b>.
1455 * Add all new connections to <b>new_conns</b>.
1458 retry_all_listeners(smartlist_t *replaced_conns,
1459 smartlist_t *new_conns)
1461 or_options_t *options = get_options();
1463 if (retry_listeners(CONN_TYPE_OR_LISTENER, options->ORListenAddress,
1464 options->ORPort, "0.0.0.0",
1465 replaced_conns, new_conns, options->ClientOnly,
1466 AF_INET)<0)
1467 return -1;
1468 if (retry_listeners(CONN_TYPE_DIR_LISTENER, options->DirListenAddress,
1469 options->DirPort, "0.0.0.0",
1470 replaced_conns, new_conns, options->ClientOnly,
1471 AF_INET)<0)
1472 return -1;
1473 if (retry_listeners(CONN_TYPE_AP_LISTENER, options->SocksListenAddress,
1474 options->SocksPort, "127.0.0.1",
1475 replaced_conns, new_conns, 0,
1476 AF_INET)<0)
1477 return -1;
1478 if (retry_listeners(CONN_TYPE_AP_TRANS_LISTENER, options->TransListenAddress,
1479 options->TransPort, "127.0.0.1",
1480 replaced_conns, new_conns, 0,
1481 AF_INET)<0)
1482 return -1;
1483 if (retry_listeners(CONN_TYPE_AP_NATD_LISTENER, options->NatdListenAddress,
1484 options->NatdPort, "127.0.0.1",
1485 replaced_conns, new_conns, 0,
1486 AF_INET)<0)
1487 return -1;
1488 if (retry_listeners(CONN_TYPE_AP_DNS_LISTENER, options->DNSListenAddress,
1489 options->DNSPort, "127.0.0.1",
1490 replaced_conns, new_conns, 0,
1491 AF_INET)<0)
1492 return -1;
1493 if (retry_listeners(CONN_TYPE_CONTROL_LISTENER,
1494 options->ControlListenAddress,
1495 options->ControlPort, "127.0.0.1",
1496 replaced_conns, new_conns, 0,
1497 AF_INET)<0)
1498 return -1;
1499 if (retry_listeners(CONN_TYPE_CONTROL_LISTENER,
1500 options->ControlSocket,
1501 options->ControlSocket ? 1 : 0, NULL,
1502 replaced_conns, new_conns, 0,
1503 AF_UNIX)<0)
1504 return -1;
1506 return 0;
1509 /** Return 1 if we should apply rate limiting to <b>conn</b>,
1510 * and 0 otherwise. Right now this just checks if it's an internal
1511 * IP address or an internal connection. */
1512 static int
1513 connection_is_rate_limited(connection_t *conn)
1515 if (conn->linked || /* internal connection */
1516 tor_addr_family(&conn->addr) == AF_UNSPEC || /* no address */
1517 tor_addr_is_internal(&conn->addr, 0)) /* internal address */
1518 return 0;
1519 else
1520 return 1;
1523 extern int global_read_bucket, global_write_bucket;
1524 extern int global_relayed_read_bucket, global_relayed_write_bucket;
1526 /** Did either global write bucket run dry last second? If so,
1527 * we are likely to run dry again this second, so be stingy with the
1528 * tokens we just put in. */
1529 static int write_buckets_empty_last_second = 0;
1531 /** How many seconds of no active local circuits will make the
1532 * connection revert to the "relayed" bandwidth class? */
1533 #define CLIENT_IDLE_TIME_FOR_PRIORITY 30
1535 /** Return 1 if <b>conn</b> should use tokens from the "relayed"
1536 * bandwidth rates, else 0. Currently, only OR conns with bandwidth
1537 * class 1, and directory conns that are serving data out, count.
1539 static int
1540 connection_counts_as_relayed_traffic(connection_t *conn, time_t now)
1542 if (conn->type == CONN_TYPE_OR &&
1543 TO_OR_CONN(conn)->client_used + CLIENT_IDLE_TIME_FOR_PRIORITY < now)
1544 return 1;
1545 if (conn->type == CONN_TYPE_DIR && DIR_CONN_IS_SERVER(conn))
1546 return 1;
1547 return 0;
1550 /** Helper function to decide how many bytes out of <b>global_bucket</b>
1551 * we're willing to use for this transaction. <b>base</b> is the size
1552 * of a cell on the network; <b>priority</b> says whether we should
1553 * write many of them or just a few; and <b>conn_bucket</b> (if
1554 * non-negative) provides an upper limit for our answer. */
1555 static ssize_t
1556 connection_bucket_round_robin(int base, int priority,
1557 ssize_t global_bucket, ssize_t conn_bucket)
1559 ssize_t at_most;
1560 ssize_t num_bytes_high = (priority ? 32 : 16) * base;
1561 ssize_t num_bytes_low = (priority ? 4 : 2) * base;
1563 /* Do a rudimentary round-robin so one circuit can't hog a connection.
1564 * Pick at most 32 cells, at least 4 cells if possible, and if we're in
1565 * the middle pick 1/8 of the available bandwidth. */
1566 at_most = global_bucket / 8;
1567 at_most -= (at_most % base); /* round down */
1568 if (at_most > num_bytes_high) /* 16 KB, or 8 KB for low-priority */
1569 at_most = num_bytes_high;
1570 else if (at_most < num_bytes_low) /* 2 KB, or 1 KB for low-priority */
1571 at_most = num_bytes_low;
1573 if (at_most > global_bucket)
1574 at_most = global_bucket;
1576 if (conn_bucket >= 0 && at_most > conn_bucket)
1577 at_most = conn_bucket;
1579 if (at_most < 0)
1580 return 0;
1581 return at_most;
1584 /** How many bytes at most can we read onto this connection? */
1585 static ssize_t
1586 connection_bucket_read_limit(connection_t *conn, time_t now)
1588 int base = connection_speaks_cells(conn) ?
1589 CELL_NETWORK_SIZE : RELAY_PAYLOAD_SIZE;
1590 int priority = conn->type != CONN_TYPE_DIR;
1591 int conn_bucket = -1;
1592 int global_bucket = global_read_bucket;
1594 if (connection_speaks_cells(conn)) {
1595 or_connection_t *or_conn = TO_OR_CONN(conn);
1596 if (conn->state == OR_CONN_STATE_OPEN)
1597 conn_bucket = or_conn->read_bucket;
1600 if (!connection_is_rate_limited(conn)) {
1601 /* be willing to read on local conns even if our buckets are empty */
1602 return conn_bucket>=0 ? conn_bucket : 1<<14;
1605 if (connection_counts_as_relayed_traffic(conn, now) &&
1606 global_relayed_read_bucket <= global_read_bucket)
1607 global_bucket = global_relayed_read_bucket;
1609 return connection_bucket_round_robin(base, priority,
1610 global_bucket, conn_bucket);
1613 /** How many bytes at most can we write onto this connection? */
1614 ssize_t
1615 connection_bucket_write_limit(connection_t *conn, time_t now)
1617 int base = connection_speaks_cells(conn) ?
1618 CELL_NETWORK_SIZE : RELAY_PAYLOAD_SIZE;
1619 int priority = conn->type != CONN_TYPE_DIR;
1620 int global_bucket = global_write_bucket;
1622 if (!connection_is_rate_limited(conn)) {
1623 /* be willing to write to local conns even if our buckets are empty */
1624 return conn->outbuf_flushlen;
1627 if (connection_counts_as_relayed_traffic(conn, now) &&
1628 global_relayed_write_bucket <= global_write_bucket)
1629 global_bucket = global_relayed_write_bucket;
1631 return connection_bucket_round_robin(base, priority, global_bucket,
1632 conn->outbuf_flushlen);
1635 /** Return 1 if the global write buckets are low enough that we
1636 * shouldn't send <b>attempt</b> bytes of low-priority directory stuff
1637 * out to <b>conn</b>. Else return 0.
1639 * Priority is 1 for v1 requests (directories and running-routers),
1640 * and 2 for v2 requests (statuses and descriptors). But see FFFF in
1641 * directory_handle_command_get() for why we don't use priority 2 yet.
1643 * There are a lot of parameters we could use here:
1644 * - global_relayed_write_bucket. Low is bad.
1645 * - global_write_bucket. Low is bad.
1646 * - bandwidthrate. Low is bad.
1647 * - bandwidthburst. Not a big factor?
1648 * - attempt. High is bad.
1649 * - total bytes queued on outbufs. High is bad. But I'm wary of
1650 * using this, since a few slow-flushing queues will pump up the
1651 * number without meaning what we meant to mean. What we really
1652 * mean is "total directory bytes added to outbufs recently", but
1653 * that's harder to quantify and harder to keep track of.
1656 global_write_bucket_low(connection_t *conn, size_t attempt, int priority)
1658 int smaller_bucket = global_write_bucket < global_relayed_write_bucket ?
1659 global_write_bucket : global_relayed_write_bucket;
1660 if (authdir_mode(get_options()) && priority>1)
1661 return 0; /* there's always room to answer v2 if we're an auth dir */
1663 if (!connection_is_rate_limited(conn))
1664 return 0; /* local conns don't get limited */
1666 if (smaller_bucket < (int)attempt)
1667 return 1; /* not enough space no matter the priority */
1669 if (write_buckets_empty_last_second)
1670 return 1; /* we're already hitting our limits, no more please */
1672 if (priority == 1) { /* old-style v1 query */
1673 /* Could we handle *two* of these requests within the next two seconds? */
1674 or_options_t *options = get_options();
1675 int64_t can_write = (int64_t)smaller_bucket
1676 + 2*(options->RelayBandwidthRate ? options->RelayBandwidthRate :
1677 options->BandwidthRate);
1678 if (can_write < 2*(int64_t)attempt)
1679 return 1;
1680 } else { /* v2 query */
1681 /* no further constraints yet */
1683 return 0;
1686 /** We just read num_read and wrote num_written onto conn.
1687 * Decrement buckets appropriately. */
1688 static void
1689 connection_buckets_decrement(connection_t *conn, time_t now,
1690 size_t num_read, size_t num_written)
1692 if (!connection_is_rate_limited(conn))
1693 return; /* local IPs are free */
1694 if (num_written >= INT_MAX || num_read >= INT_MAX) {
1695 log_err(LD_BUG, "Value out of range. num_read=%lu, num_written=%lu, "
1696 "connection type=%s, state=%s",
1697 (unsigned long)num_read, (unsigned long)num_written,
1698 conn_type_to_string(conn->type),
1699 conn_state_to_string(conn->type, conn->state));
1700 if (num_written >= INT_MAX) num_written = 1;
1701 if (num_read >= INT_MAX) num_read = 1;
1702 tor_fragile_assert();
1705 if (num_read > 0) {
1706 if (conn->type == CONN_TYPE_EXIT)
1707 rep_hist_note_exit_bytes_read(conn->port, num_read, now);
1708 rep_hist_note_bytes_read(num_read, now);
1710 if (num_written > 0) {
1711 if (conn->type == CONN_TYPE_EXIT)
1712 rep_hist_note_exit_bytes_written(conn->port, num_written, now);
1713 rep_hist_note_bytes_written(num_written, now);
1716 if (connection_counts_as_relayed_traffic(conn, now)) {
1717 global_relayed_read_bucket -= (int)num_read;
1718 global_relayed_write_bucket -= (int)num_written;
1720 global_read_bucket -= (int)num_read;
1721 global_write_bucket -= (int)num_written;
1722 if (connection_speaks_cells(conn) && conn->state == OR_CONN_STATE_OPEN)
1723 TO_OR_CONN(conn)->read_bucket -= (int)num_read;
1726 /** If we have exhausted our global buckets, or the buckets for conn,
1727 * stop reading. */
1728 static void
1729 connection_consider_empty_read_buckets(connection_t *conn)
1731 const char *reason;
1733 if (global_read_bucket <= 0) {
1734 reason = "global read bucket exhausted. Pausing.";
1735 } else if (connection_counts_as_relayed_traffic(conn, approx_time()) &&
1736 global_relayed_read_bucket <= 0) {
1737 reason = "global relayed read bucket exhausted. Pausing.";
1738 } else if (connection_speaks_cells(conn) &&
1739 conn->state == OR_CONN_STATE_OPEN &&
1740 TO_OR_CONN(conn)->read_bucket <= 0) {
1741 reason = "connection read bucket exhausted. Pausing.";
1742 } else
1743 return; /* all good, no need to stop it */
1745 LOG_FN_CONN(conn, (LOG_DEBUG, LD_NET, "%s", reason));
1746 conn->read_blocked_on_bw = 1;
1747 connection_stop_reading(conn);
1750 /** If we have exhausted our global buckets, or the buckets for conn,
1751 * stop writing. */
1752 static void
1753 connection_consider_empty_write_buckets(connection_t *conn)
1755 const char *reason;
1757 if (global_write_bucket <= 0) {
1758 reason = "global write bucket exhausted. Pausing.";
1759 } else if (connection_counts_as_relayed_traffic(conn, approx_time()) &&
1760 global_relayed_write_bucket <= 0) {
1761 reason = "global relayed write bucket exhausted. Pausing.";
1762 #if 0
1763 } else if (connection_speaks_cells(conn) &&
1764 conn->state == OR_CONN_STATE_OPEN &&
1765 TO_OR_CONN(conn)->write_bucket <= 0) {
1766 reason = "connection write bucket exhausted. Pausing.";
1767 #endif
1768 } else
1769 return; /* all good, no need to stop it */
1771 LOG_FN_CONN(conn, (LOG_DEBUG, LD_NET, "%s", reason));
1772 conn->write_blocked_on_bw = 1;
1773 connection_stop_writing(conn);
1776 /** Initialize the global read bucket to options-\>BandwidthBurst. */
1777 void
1778 connection_bucket_init(void)
1780 or_options_t *options = get_options();
1781 /* start it at max traffic */
1782 global_read_bucket = (int)options->BandwidthBurst;
1783 global_write_bucket = (int)options->BandwidthBurst;
1784 if (options->RelayBandwidthRate) {
1785 global_relayed_read_bucket = (int)options->RelayBandwidthBurst;
1786 global_relayed_write_bucket = (int)options->RelayBandwidthBurst;
1787 } else {
1788 global_relayed_read_bucket = (int)options->BandwidthBurst;
1789 global_relayed_write_bucket = (int)options->BandwidthBurst;
1793 /** Refill a single <b>bucket</b> called <b>name</b> with bandwidth rate
1794 * <b>rate</b> and bandwidth burst <b>burst</b>, assuming that
1795 * <b>seconds_elapsed</b> seconds have passed since the last call.
1797 static void
1798 connection_bucket_refill_helper(int *bucket, int rate, int burst,
1799 int seconds_elapsed, const char *name)
1801 int starting_bucket = *bucket;
1802 if (starting_bucket < burst && seconds_elapsed) {
1803 if (((burst - starting_bucket)/seconds_elapsed) < rate) {
1804 *bucket = burst; /* We would overflow the bucket; just set it to
1805 * the maximum. */
1806 } else {
1807 int incr = rate*seconds_elapsed;
1808 *bucket += incr;
1809 if (*bucket > burst || *bucket < starting_bucket) {
1810 /* If we overflow the burst, or underflow our starting bucket,
1811 * cap the bucket value to burst. */
1812 /* XXXX this might be redundant now, but it doesn't show up
1813 * in profiles. Remove it after analysis. */
1814 *bucket = burst;
1817 log(LOG_DEBUG, LD_NET,"%s now %d.", name, *bucket);
1821 /** A second has rolled over; increment buckets appropriately. */
1822 void
1823 connection_bucket_refill(int seconds_elapsed, time_t now)
1825 or_options_t *options = get_options();
1826 smartlist_t *conns = get_connection_array();
1827 int relayrate, relayburst;
1829 if (options->RelayBandwidthRate) {
1830 relayrate = (int)options->RelayBandwidthRate;
1831 relayburst = (int)options->RelayBandwidthBurst;
1832 } else {
1833 relayrate = (int)options->BandwidthRate;
1834 relayburst = (int)options->BandwidthBurst;
1837 tor_assert(seconds_elapsed >= 0);
1839 write_buckets_empty_last_second =
1840 global_relayed_write_bucket <= 0 || global_write_bucket <= 0;
1842 /* refill the global buckets */
1843 connection_bucket_refill_helper(&global_read_bucket,
1844 (int)options->BandwidthRate,
1845 (int)options->BandwidthBurst,
1846 seconds_elapsed, "global_read_bucket");
1847 connection_bucket_refill_helper(&global_write_bucket,
1848 (int)options->BandwidthRate,
1849 (int)options->BandwidthBurst,
1850 seconds_elapsed, "global_write_bucket");
1851 connection_bucket_refill_helper(&global_relayed_read_bucket,
1852 relayrate, relayburst, seconds_elapsed,
1853 "global_relayed_read_bucket");
1854 connection_bucket_refill_helper(&global_relayed_write_bucket,
1855 relayrate, relayburst, seconds_elapsed,
1856 "global_relayed_write_bucket");
1858 /* refill the per-connection buckets */
1859 SMARTLIST_FOREACH(conns, connection_t *, conn,
1861 if (connection_speaks_cells(conn)) {
1862 or_connection_t *or_conn = TO_OR_CONN(conn);
1863 if (connection_read_bucket_should_increase(or_conn)) {
1864 connection_bucket_refill_helper(&or_conn->read_bucket,
1865 or_conn->bandwidthrate,
1866 or_conn->bandwidthburst,
1867 seconds_elapsed,
1868 "or_conn->read_bucket");
1869 //log_fn(LOG_DEBUG,"Receiver bucket %d now %d.", i,
1870 // conn->read_bucket);
1874 if (conn->read_blocked_on_bw == 1 /* marked to turn reading back on now */
1875 && global_read_bucket > 0 /* and we're allowed to read */
1876 && (!connection_counts_as_relayed_traffic(conn, now) ||
1877 global_relayed_read_bucket > 0) /* even if we're relayed traffic */
1878 && (!connection_speaks_cells(conn) ||
1879 conn->state != OR_CONN_STATE_OPEN ||
1880 TO_OR_CONN(conn)->read_bucket > 0)) {
1881 /* and either a non-cell conn or a cell conn with non-empty bucket */
1882 LOG_FN_CONN(conn, (LOG_DEBUG,LD_NET,
1883 "waking up conn (fd %d) for read", conn->s));
1884 conn->read_blocked_on_bw = 0;
1885 connection_start_reading(conn);
1888 if (conn->write_blocked_on_bw == 1
1889 && global_write_bucket > 0 /* and we're allowed to write */
1890 && (!connection_counts_as_relayed_traffic(conn, now) ||
1891 global_relayed_write_bucket > 0)) {
1892 /* even if we're relayed traffic */
1893 LOG_FN_CONN(conn, (LOG_DEBUG,LD_NET,
1894 "waking up conn (fd %d) for write", conn->s));
1895 conn->write_blocked_on_bw = 0;
1896 connection_start_writing(conn);
1901 /** Is the receiver bucket for connection <b>conn</b> low enough that we
1902 * should add another pile of tokens to it?
1904 static int
1905 connection_read_bucket_should_increase(or_connection_t *conn)
1907 tor_assert(conn);
1909 if (conn->_base.state != OR_CONN_STATE_OPEN)
1910 return 0; /* only open connections play the rate limiting game */
1911 if (conn->read_bucket >= conn->bandwidthburst)
1912 return 0;
1914 return 1;
1917 /** Read bytes from conn-\>s and process them.
1919 * This function gets called from conn_read() in main.c, either
1920 * when poll() has declared that conn wants to read, or (for OR conns)
1921 * when there are pending TLS bytes.
1923 * It calls connection_read_to_buf() to bring in any new bytes,
1924 * and then calls connection_process_inbuf() to process them.
1926 * Mark the connection and return -1 if you want to close it, else
1927 * return 0.
1930 connection_handle_read(connection_t *conn)
1932 int max_to_read=-1, try_to_read;
1933 size_t before, n_read = 0;
1934 int socket_error = 0;
1936 if (conn->marked_for_close)
1937 return 0; /* do nothing */
1939 conn->timestamp_lastread = approx_time();
1941 switch (conn->type) {
1942 case CONN_TYPE_OR_LISTENER:
1943 return connection_handle_listener_read(conn, CONN_TYPE_OR);
1944 case CONN_TYPE_AP_LISTENER:
1945 case CONN_TYPE_AP_TRANS_LISTENER:
1946 case CONN_TYPE_AP_NATD_LISTENER:
1947 return connection_handle_listener_read(conn, CONN_TYPE_AP);
1948 case CONN_TYPE_DIR_LISTENER:
1949 return connection_handle_listener_read(conn, CONN_TYPE_DIR);
1950 case CONN_TYPE_CONTROL_LISTENER:
1951 return connection_handle_listener_read(conn, CONN_TYPE_CONTROL);
1952 case CONN_TYPE_AP_DNS_LISTENER:
1953 /* This should never happen; eventdns.c handles the reads here. */
1954 tor_fragile_assert();
1955 return 0;
1958 loop_again:
1959 try_to_read = max_to_read;
1960 tor_assert(!conn->marked_for_close);
1962 before = buf_datalen(conn->inbuf);
1963 if (connection_read_to_buf(conn, &max_to_read, &socket_error) < 0) {
1964 /* There's a read error; kill the connection.*/
1965 if (conn->type == CONN_TYPE_OR &&
1966 conn->state == OR_CONN_STATE_CONNECTING) {
1967 connection_or_connect_failed(TO_OR_CONN(conn),
1968 errno_to_orconn_end_reason(socket_error),
1969 tor_socket_strerror(socket_error));
1971 if (CONN_IS_EDGE(conn)) {
1972 edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
1973 connection_edge_end_errno(edge_conn);
1974 if (edge_conn->socks_request) /* broken, don't send a socks reply back */
1975 edge_conn->socks_request->has_finished = 1;
1977 connection_close_immediate(conn); /* Don't flush; connection is dead. */
1978 connection_mark_for_close(conn);
1979 return -1;
1981 n_read += buf_datalen(conn->inbuf) - before;
1982 if (CONN_IS_EDGE(conn) && try_to_read != max_to_read) {
1983 /* instruct it not to try to package partial cells. */
1984 if (connection_process_inbuf(conn, 0) < 0) {
1985 return -1;
1987 if (!conn->marked_for_close &&
1988 connection_is_reading(conn) &&
1989 !conn->inbuf_reached_eof &&
1990 max_to_read > 0)
1991 goto loop_again; /* try reading again, in case more is here now */
1993 /* one last try, packaging partial cells and all. */
1994 if (!conn->marked_for_close &&
1995 connection_process_inbuf(conn, 1) < 0) {
1996 return -1;
1998 if (conn->linked_conn) {
1999 /* The other side's handle_write will never actually get called, so
2000 * we need to invoke the appropriate callbacks ourself. */
2001 connection_t *linked = conn->linked_conn;
2003 if (n_read) {
2004 /* Probably a no-op, but hey. */
2005 connection_buckets_decrement(linked, approx_time(), 0, n_read);
2007 if (connection_flushed_some(linked) < 0)
2008 connection_mark_for_close(linked);
2009 if (!connection_wants_to_flush(linked))
2010 connection_finished_flushing(linked);
2013 if (!buf_datalen(linked->outbuf) && conn->active_on_link)
2014 connection_stop_reading_from_linked_conn(conn);
2016 /* If we hit the EOF, call connection_reached_eof. */
2017 if (!conn->marked_for_close &&
2018 conn->inbuf_reached_eof &&
2019 connection_reached_eof(conn) < 0) {
2020 return -1;
2022 return 0;
2025 /** Pull in new bytes from conn-\>s or conn-\>linked_conn onto conn-\>inbuf,
2026 * either directly or via TLS. Reduce the token buckets by the number of bytes
2027 * read.
2029 * If *max_to_read is -1, then decide it ourselves, else go with the
2030 * value passed to us. When returning, if it's changed, subtract the
2031 * number of bytes we read from *max_to_read.
2033 * Return -1 if we want to break conn, else return 0.
2035 static int
2036 connection_read_to_buf(connection_t *conn, int *max_to_read, int *socket_error)
2038 int result;
2039 ssize_t at_most = *max_to_read;
2040 size_t slack_in_buf, more_to_read;
2041 size_t n_read = 0, n_written = 0;
2043 if (at_most == -1) { /* we need to initialize it */
2044 /* how many bytes are we allowed to read? */
2045 at_most = connection_bucket_read_limit(conn, approx_time());
2048 slack_in_buf = buf_slack(conn->inbuf);
2049 again:
2050 if ((size_t)at_most > slack_in_buf && slack_in_buf >= 1024) {
2051 more_to_read = at_most - slack_in_buf;
2052 at_most = slack_in_buf;
2053 } else {
2054 more_to_read = 0;
2057 if (connection_speaks_cells(conn) &&
2058 conn->state > OR_CONN_STATE_PROXY_READING) {
2059 int pending;
2060 or_connection_t *or_conn = TO_OR_CONN(conn);
2061 size_t initial_size;
2062 if (conn->state == OR_CONN_STATE_TLS_HANDSHAKING ||
2063 conn->state == OR_CONN_STATE_TLS_CLIENT_RENEGOTIATING) {
2064 /* continue handshaking even if global token bucket is empty */
2065 return connection_tls_continue_handshake(or_conn);
2068 log_debug(LD_NET,
2069 "%d: starting, inbuf_datalen %ld (%d pending in tls object)."
2070 " at_most %ld.",
2071 conn->s,(long)buf_datalen(conn->inbuf),
2072 tor_tls_get_pending_bytes(or_conn->tls), (long)at_most);
2074 initial_size = buf_datalen(conn->inbuf);
2075 /* else open, or closing */
2076 result = read_to_buf_tls(or_conn->tls, at_most, conn->inbuf);
2077 if (TOR_TLS_IS_ERROR(result) || result == TOR_TLS_CLOSE)
2078 or_conn->tls_error = result;
2079 else
2080 or_conn->tls_error = 0;
2082 switch (result) {
2083 case TOR_TLS_CLOSE:
2084 case TOR_TLS_ERROR_IO:
2085 log_debug(LD_NET,"TLS connection closed %son read. Closing. "
2086 "(Nickname %s, address %s)",
2087 result == TOR_TLS_CLOSE ? "cleanly " : "",
2088 or_conn->nickname ? or_conn->nickname : "not set",
2089 conn->address);
2090 return result;
2091 CASE_TOR_TLS_ERROR_ANY_NONIO:
2092 log_debug(LD_NET,"tls error [%s]. breaking (nickname %s, address %s).",
2093 tor_tls_err_to_string(result),
2094 or_conn->nickname ? or_conn->nickname : "not set",
2095 conn->address);
2096 return result;
2097 case TOR_TLS_WANTWRITE:
2098 connection_start_writing(conn);
2099 return 0;
2100 case TOR_TLS_WANTREAD: /* we're already reading */
2101 case TOR_TLS_DONE: /* no data read, so nothing to process */
2102 result = 0;
2103 break; /* so we call bucket_decrement below */
2104 default:
2105 break;
2107 pending = tor_tls_get_pending_bytes(or_conn->tls);
2108 if (pending) {
2109 /* If we have any pending bytes, we read them now. This *can*
2110 * take us over our read allotment, but really we shouldn't be
2111 * believing that SSL bytes are the same as TCP bytes anyway. */
2112 int r2 = read_to_buf_tls(or_conn->tls, pending, conn->inbuf);
2113 if (r2<0) {
2114 log_warn(LD_BUG, "apparently, reading pending bytes can fail.");
2115 return -1;
2118 result = (int)(buf_datalen(conn->inbuf)-initial_size);
2119 tor_tls_get_n_raw_bytes(or_conn->tls, &n_read, &n_written);
2120 log_debug(LD_GENERAL, "After TLS read of %d: %ld read, %ld written",
2121 result, (long)n_read, (long)n_written);
2122 } else if (conn->linked) {
2123 if (conn->linked_conn) {
2124 result = move_buf_to_buf(conn->inbuf, conn->linked_conn->outbuf,
2125 &conn->linked_conn->outbuf_flushlen);
2126 } else {
2127 result = 0;
2129 //log_notice(LD_GENERAL, "Moved %d bytes on an internal link!", result);
2130 /* If the other side has disappeared, or if it's been marked for close and
2131 * we flushed its outbuf, then we should set our inbuf_reached_eof. */
2132 if (!conn->linked_conn ||
2133 (conn->linked_conn->marked_for_close &&
2134 buf_datalen(conn->linked_conn->outbuf) == 0))
2135 conn->inbuf_reached_eof = 1;
2137 n_read = (size_t) result;
2138 } else {
2139 /* !connection_speaks_cells, !conn->linked_conn. */
2140 int reached_eof = 0;
2141 CONN_LOG_PROTECT(conn,
2142 result = read_to_buf(conn->s, at_most, conn->inbuf, &reached_eof,
2143 socket_error));
2144 if (reached_eof)
2145 conn->inbuf_reached_eof = 1;
2147 // log_fn(LOG_DEBUG,"read_to_buf returned %d.",read_result);
2149 if (result < 0)
2150 return -1;
2151 n_read = (size_t) result;
2154 if (n_read > 0) { /* change *max_to_read */
2155 /*XXXX021 check for overflow*/
2156 *max_to_read = (int)(at_most - n_read);
2159 if (conn->type == CONN_TYPE_AP) {
2160 edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
2161 /*XXXX021 check for overflow*/
2162 edge_conn->n_read += (int)n_read;
2165 connection_buckets_decrement(conn, approx_time(), n_read, n_written);
2167 if (more_to_read && result == at_most) {
2168 slack_in_buf = buf_slack(conn->inbuf);
2169 at_most = more_to_read;
2170 goto again;
2173 /* Call even if result is 0, since the global read bucket may
2174 * have reached 0 on a different conn, and this guy needs to
2175 * know to stop reading. */
2176 connection_consider_empty_read_buckets(conn);
2177 if (n_written > 0 && connection_is_writing(conn))
2178 connection_consider_empty_write_buckets(conn);
2180 return 0;
2183 /** A pass-through to fetch_from_buf. */
2185 connection_fetch_from_buf(char *string, size_t len, connection_t *conn)
2187 return fetch_from_buf(string, len, conn->inbuf);
2190 /** Return conn-\>outbuf_flushlen: how many bytes conn wants to flush
2191 * from its outbuf. */
2193 connection_wants_to_flush(connection_t *conn)
2195 return conn->outbuf_flushlen > 0;
2198 /** Are there too many bytes on edge connection <b>conn</b>'s outbuf to
2199 * send back a relay-level sendme yet? Return 1 if so, 0 if not. Used by
2200 * connection_edge_consider_sending_sendme().
2203 connection_outbuf_too_full(connection_t *conn)
2205 return (conn->outbuf_flushlen > 10*CELL_PAYLOAD_SIZE);
2208 /** Try to flush more bytes onto conn-\>s.
2210 * This function gets called either from conn_write() in main.c
2211 * when poll() has declared that conn wants to write, or below
2212 * from connection_write_to_buf() when an entire TLS record is ready.
2214 * Update conn-\>timestamp_lastwritten to now, and call flush_buf
2215 * or flush_buf_tls appropriately. If it succeeds and there are no more
2216 * more bytes on conn->outbuf, then call connection_finished_flushing
2217 * on it too.
2219 * If <b>force</b>, then write as many bytes as possible, ignoring bandwidth
2220 * limits. (Used for flushing messages to controller connections on fatal
2221 * errors.)
2223 * Mark the connection and return -1 if you want to close it, else
2224 * return 0.
2227 connection_handle_write(connection_t *conn, int force)
2229 int e;
2230 socklen_t len=(socklen_t)sizeof(e);
2231 int result;
2232 ssize_t max_to_write;
2233 time_t now = approx_time();
2234 size_t n_read = 0, n_written = 0;
2236 tor_assert(!connection_is_listener(conn));
2238 if (conn->marked_for_close || conn->s < 0)
2239 return 0; /* do nothing */
2241 if (conn->in_flushed_some) {
2242 log_warn(LD_BUG, "called recursively from inside conn->in_flushed_some()");
2243 return 0;
2246 conn->timestamp_lastwritten = now;
2248 /* Sometimes, "writable" means "connected". */
2249 if (connection_state_is_connecting(conn)) {
2250 if (getsockopt(conn->s, SOL_SOCKET, SO_ERROR, (void*)&e, &len) < 0) {
2251 log_warn(LD_BUG,
2252 "getsockopt() syscall failed?! Please report to tor-ops.");
2253 if (CONN_IS_EDGE(conn))
2254 connection_edge_end_errno(TO_EDGE_CONN(conn));
2255 connection_mark_for_close(conn);
2256 return -1;
2258 if (e) {
2259 /* some sort of error, but maybe just inprogress still */
2260 if (!ERRNO_IS_CONN_EINPROGRESS(e)) {
2261 log_info(LD_NET,"in-progress connect failed. Removing. (%s)",
2262 tor_socket_strerror(e));
2263 if (CONN_IS_EDGE(conn))
2264 connection_edge_end_errno(TO_EDGE_CONN(conn));
2265 if (conn->type == CONN_TYPE_OR)
2266 connection_or_connect_failed(TO_OR_CONN(conn),
2267 errno_to_orconn_end_reason(e),
2268 tor_socket_strerror(e));
2270 connection_close_immediate(conn);
2271 connection_mark_for_close(conn);
2272 return -1;
2273 } else {
2274 return 0; /* no change, see if next time is better */
2277 /* The connection is successful. */
2278 if (connection_finished_connecting(conn)<0)
2279 return -1;
2282 max_to_write = force ? (ssize_t)conn->outbuf_flushlen
2283 : connection_bucket_write_limit(conn, now);
2285 if (connection_speaks_cells(conn) &&
2286 conn->state > OR_CONN_STATE_PROXY_READING) {
2287 or_connection_t *or_conn = TO_OR_CONN(conn);
2288 if (conn->state == OR_CONN_STATE_TLS_HANDSHAKING ||
2289 conn->state == OR_CONN_STATE_TLS_CLIENT_RENEGOTIATING) {
2290 connection_stop_writing(conn);
2291 if (connection_tls_continue_handshake(or_conn) < 0) {
2292 /* Don't flush; connection is dead. */
2293 connection_close_immediate(conn);
2294 connection_mark_for_close(conn);
2295 return -1;
2297 return 0;
2298 } else if (conn->state == OR_CONN_STATE_TLS_SERVER_RENEGOTIATING) {
2299 return connection_handle_read(conn);
2302 /* else open, or closing */
2303 result = flush_buf_tls(or_conn->tls, conn->outbuf,
2304 max_to_write, &conn->outbuf_flushlen);
2306 /* If we just flushed the last bytes, check if this tunneled dir
2307 * request is done. */
2308 if (buf_datalen(conn->outbuf) == 0 && conn->dirreq_id)
2309 geoip_change_dirreq_state(conn->dirreq_id, DIRREQ_TUNNELED,
2310 DIRREQ_OR_CONN_BUFFER_FLUSHED);
2312 switch (result) {
2313 CASE_TOR_TLS_ERROR_ANY:
2314 case TOR_TLS_CLOSE:
2315 log_info(LD_NET,result!=TOR_TLS_CLOSE?
2316 "tls error. breaking.":"TLS connection closed on flush");
2317 /* Don't flush; connection is dead. */
2318 connection_close_immediate(conn);
2319 connection_mark_for_close(conn);
2320 return -1;
2321 case TOR_TLS_WANTWRITE:
2322 log_debug(LD_NET,"wanted write.");
2323 /* we're already writing */
2324 return 0;
2325 case TOR_TLS_WANTREAD:
2326 /* Make sure to avoid a loop if the receive buckets are empty. */
2327 log_debug(LD_NET,"wanted read.");
2328 if (!connection_is_reading(conn)) {
2329 connection_stop_writing(conn);
2330 conn->write_blocked_on_bw = 1;
2331 /* we'll start reading again when the next second arrives,
2332 * and then also start writing again.
2335 /* else no problem, we're already reading */
2336 return 0;
2337 /* case TOR_TLS_DONE:
2338 * for TOR_TLS_DONE, fall through to check if the flushlen
2339 * is empty, so we can stop writing.
2343 tor_tls_get_n_raw_bytes(or_conn->tls, &n_read, &n_written);
2344 log_debug(LD_GENERAL, "After TLS write of %d: %ld read, %ld written",
2345 result, (long)n_read, (long)n_written);
2346 } else {
2347 CONN_LOG_PROTECT(conn,
2348 result = flush_buf(conn->s, conn->outbuf,
2349 max_to_write, &conn->outbuf_flushlen));
2350 if (result < 0) {
2351 if (CONN_IS_EDGE(conn))
2352 connection_edge_end_errno(TO_EDGE_CONN(conn));
2354 connection_close_immediate(conn); /* Don't flush; connection is dead. */
2355 connection_mark_for_close(conn);
2356 return -1;
2358 n_written = (size_t) result;
2361 if (conn->type == CONN_TYPE_AP) {
2362 edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
2363 /*XXXX021 check for overflow.*/
2364 edge_conn->n_written += (int)n_written;
2367 connection_buckets_decrement(conn, approx_time(), n_read, n_written);
2369 if (result > 0) {
2370 /* If we wrote any bytes from our buffer, then call the appropriate
2371 * functions. */
2372 if (connection_flushed_some(conn) < 0)
2373 connection_mark_for_close(conn);
2376 if (!connection_wants_to_flush(conn)) { /* it's done flushing */
2377 if (connection_finished_flushing(conn) < 0) {
2378 /* already marked */
2379 return -1;
2381 return 0;
2384 /* Call even if result is 0, since the global write bucket may
2385 * have reached 0 on a different conn, and this guy needs to
2386 * know to stop writing. */
2387 connection_consider_empty_write_buckets(conn);
2388 if (n_read > 0 && connection_is_reading(conn))
2389 connection_consider_empty_read_buckets(conn);
2391 return 0;
2394 /** OpenSSL TLS record size is 16383; this is close. The goal here is to
2395 * push data out as soon as we know there's enough for a TLS record, so
2396 * during periods of high load we won't read entire megabytes from
2397 * input before pushing any data out. It also has the feature of not
2398 * growing huge outbufs unless something is slow. */
2399 #define MIN_TLS_FLUSHLEN 15872
2401 /** Append <b>len</b> bytes of <b>string</b> onto <b>conn</b>'s
2402 * outbuf, and ask it to start writing.
2404 * If <b>zlib</b> is nonzero, this is a directory connection that should get
2405 * its contents compressed or decompressed as they're written. If zlib is
2406 * negative, this is the last data to be compressed, and the connection's zlib
2407 * state should be flushed.
2409 * If it's an OR conn and an entire TLS record is ready, then try to
2410 * flush the record now. Similarly, if it's a local control connection
2411 * and a 64k chunk is ready, try to flush it all, so we don't end up with
2412 * many megabytes of controller info queued at once.
2414 void
2415 _connection_write_to_buf_impl(const char *string, size_t len,
2416 connection_t *conn, int zlib)
2418 /* XXXX This function really needs to return -1 on failure. */
2419 int r;
2420 size_t old_datalen;
2421 if (!len && !(zlib<0))
2422 return;
2423 /* if it's marked for close, only allow write if we mean to flush it */
2424 if (conn->marked_for_close && !conn->hold_open_until_flushed)
2425 return;
2427 old_datalen = buf_datalen(conn->outbuf);
2428 if (zlib) {
2429 dir_connection_t *dir_conn = TO_DIR_CONN(conn);
2430 int done = zlib < 0;
2431 CONN_LOG_PROTECT(conn, r = write_to_buf_zlib(conn->outbuf,
2432 dir_conn->zlib_state,
2433 string, len, done));
2434 } else {
2435 CONN_LOG_PROTECT(conn, r = write_to_buf(string, len, conn->outbuf));
2437 if (r < 0) {
2438 if (CONN_IS_EDGE(conn)) {
2439 /* if it failed, it means we have our package/delivery windows set
2440 wrong compared to our max outbuf size. close the whole circuit. */
2441 log_warn(LD_NET,
2442 "write_to_buf failed. Closing circuit (fd %d).", conn->s);
2443 circuit_mark_for_close(circuit_get_by_edge_conn(TO_EDGE_CONN(conn)),
2444 END_CIRC_REASON_INTERNAL);
2445 } else {
2446 log_warn(LD_NET,
2447 "write_to_buf failed. Closing connection (fd %d).", conn->s);
2448 connection_mark_for_close(conn);
2450 return;
2453 connection_start_writing(conn);
2454 if (zlib) {
2455 conn->outbuf_flushlen += buf_datalen(conn->outbuf) - old_datalen;
2456 } else {
2457 ssize_t extra = 0;
2458 conn->outbuf_flushlen += len;
2460 /* Should we try flushing the outbuf now? */
2461 if (conn->in_flushed_some) {
2462 /* Don't flush the outbuf when the reason we're writing more stuff is
2463 * _because_ we flushed the outbuf. That's unfair. */
2464 return;
2467 if (conn->type == CONN_TYPE_OR &&
2468 conn->outbuf_flushlen-len < MIN_TLS_FLUSHLEN &&
2469 conn->outbuf_flushlen >= MIN_TLS_FLUSHLEN) {
2470 /* We just pushed outbuf_flushlen to MIN_TLS_FLUSHLEN or above;
2471 * we can send out a full TLS frame now if we like. */
2472 extra = conn->outbuf_flushlen - MIN_TLS_FLUSHLEN;
2473 conn->outbuf_flushlen = MIN_TLS_FLUSHLEN;
2474 } else if (conn->type == CONN_TYPE_CONTROL &&
2475 !connection_is_rate_limited(conn) &&
2476 conn->outbuf_flushlen-len < 1<<16 &&
2477 conn->outbuf_flushlen >= 1<<16) {
2478 /* just try to flush all of it */
2479 } else
2480 return; /* no need to try flushing */
2482 if (connection_handle_write(conn, 0) < 0) {
2483 if (!conn->marked_for_close) {
2484 /* this connection is broken. remove it. */
2485 log_warn(LD_BUG, "unhandled error on write for "
2486 "conn (type %d, fd %d); removing",
2487 conn->type, conn->s);
2488 tor_fragile_assert();
2489 /* do a close-immediate here, so we don't try to flush */
2490 connection_close_immediate(conn);
2492 return;
2494 if (extra) {
2495 conn->outbuf_flushlen += extra;
2496 connection_start_writing(conn);
2501 /** Return a connection with given type, address, port, and purpose;
2502 * or NULL if no such connection exists. */
2503 connection_t *
2504 connection_get_by_type_addr_port_purpose(int type,
2505 const tor_addr_t *addr, uint16_t port,
2506 int purpose)
2508 smartlist_t *conns = get_connection_array();
2509 SMARTLIST_FOREACH(conns, connection_t *, conn,
2511 if (conn->type == type &&
2512 tor_addr_eq(&conn->addr, addr) &&
2513 conn->port == port &&
2514 conn->purpose == purpose &&
2515 !conn->marked_for_close)
2516 return conn;
2518 return NULL;
2521 /** Return the stream with id <b>id</b> if it is not already marked for
2522 * close.
2524 connection_t *
2525 connection_get_by_global_id(uint64_t id)
2527 smartlist_t *conns = get_connection_array();
2528 SMARTLIST_FOREACH(conns, connection_t *, conn,
2530 if (conn->global_identifier == id)
2531 return conn;
2533 return NULL;
2536 /** Return a connection of type <b>type</b> that is not marked for close.
2538 connection_t *
2539 connection_get_by_type(int type)
2541 smartlist_t *conns = get_connection_array();
2542 SMARTLIST_FOREACH(conns, connection_t *, conn,
2544 if (conn->type == type && !conn->marked_for_close)
2545 return conn;
2547 return NULL;
2550 /** Return a connection of type <b>type</b> that is in state <b>state</b>,
2551 * and that is not marked for close.
2553 connection_t *
2554 connection_get_by_type_state(int type, int state)
2556 smartlist_t *conns = get_connection_array();
2557 SMARTLIST_FOREACH(conns, connection_t *, conn,
2559 if (conn->type == type && conn->state == state && !conn->marked_for_close)
2560 return conn;
2562 return NULL;
2565 /** Return a connection of type <b>type</b> that has rendquery equal
2566 * to <b>rendquery</b>, and that is not marked for close. If state
2567 * is non-zero, conn must be of that state too.
2569 connection_t *
2570 connection_get_by_type_state_rendquery(int type, int state,
2571 const char *rendquery)
2573 smartlist_t *conns = get_connection_array();
2575 tor_assert(type == CONN_TYPE_DIR ||
2576 type == CONN_TYPE_AP || type == CONN_TYPE_EXIT);
2577 tor_assert(rendquery);
2579 SMARTLIST_FOREACH(conns, connection_t *, conn,
2581 if (conn->type == type &&
2582 !conn->marked_for_close &&
2583 (!state || state == conn->state)) {
2584 if (type == CONN_TYPE_DIR &&
2585 TO_DIR_CONN(conn)->rend_data &&
2586 !rend_cmp_service_ids(rendquery,
2587 TO_DIR_CONN(conn)->rend_data->onion_address))
2588 return conn;
2589 else if (CONN_IS_EDGE(conn) &&
2590 TO_EDGE_CONN(conn)->rend_data &&
2591 !rend_cmp_service_ids(rendquery,
2592 TO_EDGE_CONN(conn)->rend_data->onion_address))
2593 return conn;
2596 return NULL;
2599 /** Return an open, non-marked connection of a given type and purpose, or NULL
2600 * if no such connection exists. */
2601 connection_t *
2602 connection_get_by_type_purpose(int type, int purpose)
2604 smartlist_t *conns = get_connection_array();
2605 SMARTLIST_FOREACH(conns, connection_t *, conn,
2607 if (conn->type == type &&
2608 !conn->marked_for_close &&
2609 (purpose == conn->purpose))
2610 return conn;
2612 return NULL;
2615 /** Return 1 if <b>conn</b> is a listener conn, else return 0. */
2617 connection_is_listener(connection_t *conn)
2619 if (conn->type == CONN_TYPE_OR_LISTENER ||
2620 conn->type == CONN_TYPE_AP_LISTENER ||
2621 conn->type == CONN_TYPE_AP_TRANS_LISTENER ||
2622 conn->type == CONN_TYPE_AP_DNS_LISTENER ||
2623 conn->type == CONN_TYPE_AP_NATD_LISTENER ||
2624 conn->type == CONN_TYPE_DIR_LISTENER ||
2625 conn->type == CONN_TYPE_CONTROL_LISTENER)
2626 return 1;
2627 return 0;
2630 /** Return 1 if <b>conn</b> is in state "open" and is not marked
2631 * for close, else return 0.
2634 connection_state_is_open(connection_t *conn)
2636 tor_assert(conn);
2638 if (conn->marked_for_close)
2639 return 0;
2641 if ((conn->type == CONN_TYPE_OR && conn->state == OR_CONN_STATE_OPEN) ||
2642 (conn->type == CONN_TYPE_AP && conn->state == AP_CONN_STATE_OPEN) ||
2643 (conn->type == CONN_TYPE_EXIT && conn->state == EXIT_CONN_STATE_OPEN) ||
2644 (conn->type == CONN_TYPE_CONTROL &&
2645 conn->state == CONTROL_CONN_STATE_OPEN))
2646 return 1;
2648 return 0;
2651 /** Return 1 if conn is in 'connecting' state, else return 0. */
2653 connection_state_is_connecting(connection_t *conn)
2655 tor_assert(conn);
2657 if (conn->marked_for_close)
2658 return 0;
2659 switch (conn->type)
2661 case CONN_TYPE_OR:
2662 return conn->state == OR_CONN_STATE_CONNECTING;
2663 case CONN_TYPE_EXIT:
2664 return conn->state == EXIT_CONN_STATE_CONNECTING;
2665 case CONN_TYPE_DIR:
2666 return conn->state == DIR_CONN_STATE_CONNECTING;
2669 return 0;
2672 /** Allocates a base64'ed authenticator for use in http or https
2673 * auth, based on the input string <b>authenticator</b>. Returns it
2674 * if success, else returns NULL. */
2675 char *
2676 alloc_http_authenticator(const char *authenticator)
2678 /* an authenticator in Basic authentication
2679 * is just the string "username:password" */
2680 const size_t authenticator_length = strlen(authenticator);
2681 /* The base64_encode function needs a minimum buffer length
2682 * of 66 bytes. */
2683 const size_t base64_authenticator_length = (authenticator_length/48+1)*66;
2684 char *base64_authenticator = tor_malloc(base64_authenticator_length);
2685 if (base64_encode(base64_authenticator, base64_authenticator_length,
2686 authenticator, authenticator_length) < 0) {
2687 tor_free(base64_authenticator); /* free and set to null */
2688 } else {
2689 /* remove extra \n at end of encoding */
2690 base64_authenticator[strlen(base64_authenticator) - 1] = 0;
2692 return base64_authenticator;
2695 /** Given a socket handle, check whether the local address (sockname) of the
2696 * socket is one that we've connected from before. If so, double-check
2697 * whether our address has changed and we need to generate keys. If we do,
2698 * call init_keys().
2700 static void
2701 client_check_address_changed(int sock)
2703 uint32_t iface_ip, ip_out;
2704 struct sockaddr_in out_addr;
2705 socklen_t out_addr_len = (socklen_t) sizeof(out_addr);
2706 uint32_t *ip;
2708 if (!last_interface_ip)
2709 get_interface_address(LOG_INFO, &last_interface_ip);
2710 if (!outgoing_addrs)
2711 outgoing_addrs = smartlist_create();
2713 if (getsockname(sock, (struct sockaddr*)&out_addr, &out_addr_len)<0) {
2714 int e = tor_socket_errno(sock);
2715 log_warn(LD_NET, "getsockname() to check for address change failed: %s",
2716 tor_socket_strerror(e));
2717 return;
2720 /* Okay. If we've used this address previously, we're okay. */
2721 ip_out = ntohl(out_addr.sin_addr.s_addr);
2722 SMARTLIST_FOREACH(outgoing_addrs, uint32_t*, ip_ptr,
2723 if (*ip_ptr == ip_out) return;
2726 /* Uh-oh. We haven't connected from this address before. Has the interface
2727 * address changed? */
2728 if (get_interface_address(LOG_INFO, &iface_ip)<0)
2729 return;
2730 ip = tor_malloc(sizeof(uint32_t));
2731 *ip = ip_out;
2733 if (iface_ip == last_interface_ip) {
2734 /* Nope, it hasn't changed. Add this address to the list. */
2735 smartlist_add(outgoing_addrs, ip);
2736 } else {
2737 /* The interface changed. We're a client, so we need to regenerate our
2738 * keys. First, reset the state. */
2739 log(LOG_NOTICE, LD_NET, "Our IP address has changed. Rotating keys...");
2740 last_interface_ip = iface_ip;
2741 SMARTLIST_FOREACH(outgoing_addrs, void*, ip_ptr, tor_free(ip_ptr));
2742 smartlist_clear(outgoing_addrs);
2743 smartlist_add(outgoing_addrs, ip);
2744 /* Okay, now change our keys. */
2745 ip_address_changed(1);
2749 /** Some systems have limited system buffers for recv and xmit on
2750 * sockets allocated in a virtual server or similar environment. For a Tor
2751 * server this can produce the "Error creating network socket: No buffer
2752 * space available" error once all available TCP buffer space is consumed.
2753 * This method will attempt to constrain the buffers allocated for the socket
2754 * to the desired size to stay below system TCP buffer limits.
2756 static void
2757 set_constrained_socket_buffers(int sock, int size)
2759 void *sz = (void*)&size;
2760 socklen_t sz_sz = (socklen_t) sizeof(size);
2761 if (setsockopt(sock, SOL_SOCKET, SO_SNDBUF, sz, sz_sz) < 0) {
2762 int e = tor_socket_errno(sock);
2763 log_warn(LD_NET, "setsockopt() to constrain send "
2764 "buffer to %d bytes failed: %s", size, tor_socket_strerror(e));
2766 if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF, sz, sz_sz) < 0) {
2767 int e = tor_socket_errno(sock);
2768 log_warn(LD_NET, "setsockopt() to constrain recv "
2769 "buffer to %d bytes failed: %s", size, tor_socket_strerror(e));
2773 /** Process new bytes that have arrived on conn-\>inbuf.
2775 * This function just passes conn to the connection-specific
2776 * connection_*_process_inbuf() function. It also passes in
2777 * package_partial if wanted.
2779 static int
2780 connection_process_inbuf(connection_t *conn, int package_partial)
2782 tor_assert(conn);
2784 switch (conn->type) {
2785 case CONN_TYPE_OR:
2786 return connection_or_process_inbuf(TO_OR_CONN(conn));
2787 case CONN_TYPE_EXIT:
2788 case CONN_TYPE_AP:
2789 return connection_edge_process_inbuf(TO_EDGE_CONN(conn),
2790 package_partial);
2791 case CONN_TYPE_DIR:
2792 return connection_dir_process_inbuf(TO_DIR_CONN(conn));
2793 case CONN_TYPE_CPUWORKER:
2794 return connection_cpu_process_inbuf(conn);
2795 case CONN_TYPE_CONTROL:
2796 return connection_control_process_inbuf(TO_CONTROL_CONN(conn));
2797 default:
2798 log_err(LD_BUG,"got unexpected conn type %d.", conn->type);
2799 tor_fragile_assert();
2800 return -1;
2804 /** Called whenever we've written data on a connection. */
2805 static int
2806 connection_flushed_some(connection_t *conn)
2808 int r = 0;
2809 tor_assert(!conn->in_flushed_some);
2810 conn->in_flushed_some = 1;
2811 if (conn->type == CONN_TYPE_DIR &&
2812 conn->state == DIR_CONN_STATE_SERVER_WRITING) {
2813 r = connection_dirserv_flushed_some(TO_DIR_CONN(conn));
2814 } else if (conn->type == CONN_TYPE_OR) {
2815 r = connection_or_flushed_some(TO_OR_CONN(conn));
2817 conn->in_flushed_some = 0;
2818 return r;
2821 /** We just finished flushing bytes from conn-\>outbuf, and there
2822 * are no more bytes remaining.
2824 * This function just passes conn to the connection-specific
2825 * connection_*_finished_flushing() function.
2827 static int
2828 connection_finished_flushing(connection_t *conn)
2830 tor_assert(conn);
2832 /* If the connection is closed, don't try to do anything more here. */
2833 if (CONN_IS_CLOSED(conn))
2834 return 0;
2836 // log_fn(LOG_DEBUG,"entered. Socket %u.", conn->s);
2838 switch (conn->type) {
2839 case CONN_TYPE_OR:
2840 return connection_or_finished_flushing(TO_OR_CONN(conn));
2841 case CONN_TYPE_AP:
2842 case CONN_TYPE_EXIT:
2843 return connection_edge_finished_flushing(TO_EDGE_CONN(conn));
2844 case CONN_TYPE_DIR:
2845 return connection_dir_finished_flushing(TO_DIR_CONN(conn));
2846 case CONN_TYPE_CPUWORKER:
2847 return connection_cpu_finished_flushing(conn);
2848 case CONN_TYPE_CONTROL:
2849 return connection_control_finished_flushing(TO_CONTROL_CONN(conn));
2850 default:
2851 log_err(LD_BUG,"got unexpected conn type %d.", conn->type);
2852 tor_fragile_assert();
2853 return -1;
2857 /** Called when our attempt to connect() to another server has just
2858 * succeeded.
2860 * This function just passes conn to the connection-specific
2861 * connection_*_finished_connecting() function.
2863 static int
2864 connection_finished_connecting(connection_t *conn)
2866 tor_assert(conn);
2867 switch (conn->type)
2869 case CONN_TYPE_OR:
2870 return connection_or_finished_connecting(TO_OR_CONN(conn));
2871 case CONN_TYPE_EXIT:
2872 return connection_edge_finished_connecting(TO_EDGE_CONN(conn));
2873 case CONN_TYPE_DIR:
2874 return connection_dir_finished_connecting(TO_DIR_CONN(conn));
2875 default:
2876 log_err(LD_BUG,"got unexpected conn type %d.", conn->type);
2877 tor_fragile_assert();
2878 return -1;
2882 /** Callback: invoked when a connection reaches an EOF event. */
2883 static int
2884 connection_reached_eof(connection_t *conn)
2886 switch (conn->type) {
2887 case CONN_TYPE_OR:
2888 return connection_or_reached_eof(TO_OR_CONN(conn));
2889 case CONN_TYPE_AP:
2890 case CONN_TYPE_EXIT:
2891 return connection_edge_reached_eof(TO_EDGE_CONN(conn));
2892 case CONN_TYPE_DIR:
2893 return connection_dir_reached_eof(TO_DIR_CONN(conn));
2894 case CONN_TYPE_CPUWORKER:
2895 return connection_cpu_reached_eof(conn);
2896 case CONN_TYPE_CONTROL:
2897 return connection_control_reached_eof(TO_CONTROL_CONN(conn));
2898 default:
2899 log_err(LD_BUG,"got unexpected conn type %d.", conn->type);
2900 tor_fragile_assert();
2901 return -1;
2905 /** Log how many bytes are used by buffers of different kinds and sizes. */
2906 void
2907 connection_dump_buffer_mem_stats(int severity)
2909 uint64_t used_by_type[_CONN_TYPE_MAX+1];
2910 uint64_t alloc_by_type[_CONN_TYPE_MAX+1];
2911 int n_conns_by_type[_CONN_TYPE_MAX+1];
2912 uint64_t total_alloc = 0;
2913 uint64_t total_used = 0;
2914 int i;
2915 smartlist_t *conns = get_connection_array();
2917 memset(used_by_type, 0, sizeof(used_by_type));
2918 memset(alloc_by_type, 0, sizeof(alloc_by_type));
2919 memset(n_conns_by_type, 0, sizeof(n_conns_by_type));
2921 SMARTLIST_FOREACH(conns, connection_t *, c,
2923 int tp = c->type;
2924 ++n_conns_by_type[tp];
2925 if (c->inbuf) {
2926 used_by_type[tp] += buf_datalen(c->inbuf);
2927 alloc_by_type[tp] += buf_allocation(c->inbuf);
2929 if (c->outbuf) {
2930 used_by_type[tp] += buf_datalen(c->outbuf);
2931 alloc_by_type[tp] += buf_allocation(c->outbuf);
2934 for (i=0; i <= _CONN_TYPE_MAX; ++i) {
2935 total_used += used_by_type[i];
2936 total_alloc += alloc_by_type[i];
2939 log(severity, LD_GENERAL,
2940 "In buffers for %d connections: "U64_FORMAT" used/"U64_FORMAT" allocated",
2941 smartlist_len(conns),
2942 U64_PRINTF_ARG(total_used), U64_PRINTF_ARG(total_alloc));
2943 for (i=_CONN_TYPE_MIN; i <= _CONN_TYPE_MAX; ++i) {
2944 if (!n_conns_by_type[i])
2945 continue;
2946 log(severity, LD_GENERAL,
2947 " For %d %s connections: "U64_FORMAT" used/"U64_FORMAT" allocated",
2948 n_conns_by_type[i], conn_type_to_string(i),
2949 U64_PRINTF_ARG(used_by_type[i]), U64_PRINTF_ARG(alloc_by_type[i]));
2953 /** Verify that connection <b>conn</b> has all of its invariants
2954 * correct. Trigger an assert if anything is invalid.
2956 void
2957 assert_connection_ok(connection_t *conn, time_t now)
2959 (void) now; /* XXXX unused. */
2960 tor_assert(conn);
2961 tor_assert(conn->type >= _CONN_TYPE_MIN);
2962 tor_assert(conn->type <= _CONN_TYPE_MAX);
2963 switch (conn->type) {
2964 case CONN_TYPE_OR:
2965 tor_assert(conn->magic == OR_CONNECTION_MAGIC);
2966 break;
2967 case CONN_TYPE_AP:
2968 case CONN_TYPE_EXIT:
2969 tor_assert(conn->magic == EDGE_CONNECTION_MAGIC);
2970 break;
2971 case CONN_TYPE_DIR:
2972 tor_assert(conn->magic == DIR_CONNECTION_MAGIC);
2973 break;
2974 case CONN_TYPE_CONTROL:
2975 tor_assert(conn->magic == CONTROL_CONNECTION_MAGIC);
2976 break;
2977 default:
2978 tor_assert(conn->magic == BASE_CONNECTION_MAGIC);
2979 break;
2982 if (conn->linked_conn) {
2983 tor_assert(conn->linked_conn->linked_conn == conn);
2984 tor_assert(conn->linked);
2986 if (conn->linked)
2987 tor_assert(conn->s < 0);
2989 if (conn->outbuf_flushlen > 0) {
2990 tor_assert(connection_is_writing(conn) || conn->write_blocked_on_bw ||
2991 (CONN_IS_EDGE(conn) && TO_EDGE_CONN(conn)->edge_blocked_on_circ));
2994 if (conn->hold_open_until_flushed)
2995 tor_assert(conn->marked_for_close);
2997 /* XXXX check: read_blocked_on_bw, write_blocked_on_bw, s, conn_array_index,
2998 * marked_for_close. */
3000 /* buffers */
3001 if (!connection_is_listener(conn)) {
3002 assert_buf_ok(conn->inbuf);
3003 assert_buf_ok(conn->outbuf);
3006 if (conn->type == CONN_TYPE_OR) {
3007 or_connection_t *or_conn = TO_OR_CONN(conn);
3008 if (conn->state == OR_CONN_STATE_OPEN) {
3009 /* tor_assert(conn->bandwidth > 0); */
3010 /* the above isn't necessarily true: if we just did a TLS
3011 * handshake but we didn't recognize the other peer, or it
3012 * gave a bad cert/etc, then we won't have assigned bandwidth,
3013 * yet it will be open. -RD
3015 // tor_assert(conn->read_bucket >= 0);
3017 // tor_assert(conn->addr && conn->port);
3018 tor_assert(conn->address);
3019 if (conn->state > OR_CONN_STATE_PROXY_READING)
3020 tor_assert(or_conn->tls);
3023 if (CONN_IS_EDGE(conn)) {
3024 edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
3025 if (edge_conn->chosen_exit_optional || edge_conn->chosen_exit_retries) {
3026 tor_assert(conn->type == CONN_TYPE_AP);
3027 tor_assert(edge_conn->chosen_exit_name);
3030 /* XXX unchecked: package window, deliver window. */
3031 if (conn->type == CONN_TYPE_AP) {
3033 tor_assert(edge_conn->socks_request);
3034 if (conn->state == AP_CONN_STATE_OPEN) {
3035 tor_assert(edge_conn->socks_request->has_finished);
3036 if (!conn->marked_for_close) {
3037 tor_assert(edge_conn->cpath_layer);
3038 assert_cpath_layer_ok(edge_conn->cpath_layer);
3042 if (conn->type == CONN_TYPE_EXIT) {
3043 tor_assert(conn->purpose == EXIT_PURPOSE_CONNECT ||
3044 conn->purpose == EXIT_PURPOSE_RESOLVE);
3046 } else if (conn->type == CONN_TYPE_DIR) {
3047 } else {
3048 /* Purpose is only used for dir and exit types currently */
3049 tor_assert(!conn->purpose);
3052 switch (conn->type)
3054 case CONN_TYPE_OR_LISTENER:
3055 case CONN_TYPE_AP_LISTENER:
3056 case CONN_TYPE_AP_TRANS_LISTENER:
3057 case CONN_TYPE_AP_NATD_LISTENER:
3058 case CONN_TYPE_DIR_LISTENER:
3059 case CONN_TYPE_CONTROL_LISTENER:
3060 case CONN_TYPE_AP_DNS_LISTENER:
3061 tor_assert(conn->state == LISTENER_STATE_READY);
3062 break;
3063 case CONN_TYPE_OR:
3064 tor_assert(conn->state >= _OR_CONN_STATE_MIN);
3065 tor_assert(conn->state <= _OR_CONN_STATE_MAX);
3066 tor_assert(TO_OR_CONN(conn)->n_circuits >= 0);
3067 break;
3068 case CONN_TYPE_EXIT:
3069 tor_assert(conn->state >= _EXIT_CONN_STATE_MIN);
3070 tor_assert(conn->state <= _EXIT_CONN_STATE_MAX);
3071 tor_assert(conn->purpose >= _EXIT_PURPOSE_MIN);
3072 tor_assert(conn->purpose <= _EXIT_PURPOSE_MAX);
3073 break;
3074 case CONN_TYPE_AP:
3075 tor_assert(conn->state >= _AP_CONN_STATE_MIN);
3076 tor_assert(conn->state <= _AP_CONN_STATE_MAX);
3077 tor_assert(TO_EDGE_CONN(conn)->socks_request);
3078 break;
3079 case CONN_TYPE_DIR:
3080 tor_assert(conn->state >= _DIR_CONN_STATE_MIN);
3081 tor_assert(conn->state <= _DIR_CONN_STATE_MAX);
3082 tor_assert(conn->purpose >= _DIR_PURPOSE_MIN);
3083 tor_assert(conn->purpose <= _DIR_PURPOSE_MAX);
3084 break;
3085 case CONN_TYPE_CPUWORKER:
3086 tor_assert(conn->state >= _CPUWORKER_STATE_MIN);
3087 tor_assert(conn->state <= _CPUWORKER_STATE_MAX);
3088 break;
3089 case CONN_TYPE_CONTROL:
3090 tor_assert(conn->state >= _CONTROL_CONN_STATE_MIN);
3091 tor_assert(conn->state <= _CONTROL_CONN_STATE_MAX);
3092 break;
3093 default:
3094 tor_assert(0);