Simplify the math to round up to the next multiple of some value.
[tor/rransom.git] / src / or / connection.c
blob0897eb4a5ebe01f5cddd7fb2853d3243121baa21
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);
2305 switch (result) {
2306 CASE_TOR_TLS_ERROR_ANY:
2307 case TOR_TLS_CLOSE:
2308 log_info(LD_NET,result!=TOR_TLS_CLOSE?
2309 "tls error. breaking.":"TLS connection closed on flush");
2310 /* Don't flush; connection is dead. */
2311 connection_close_immediate(conn);
2312 connection_mark_for_close(conn);
2313 return -1;
2314 case TOR_TLS_WANTWRITE:
2315 log_debug(LD_NET,"wanted write.");
2316 /* we're already writing */
2317 return 0;
2318 case TOR_TLS_WANTREAD:
2319 /* Make sure to avoid a loop if the receive buckets are empty. */
2320 log_debug(LD_NET,"wanted read.");
2321 if (!connection_is_reading(conn)) {
2322 connection_stop_writing(conn);
2323 conn->write_blocked_on_bw = 1;
2324 /* we'll start reading again when the next second arrives,
2325 * and then also start writing again.
2328 /* else no problem, we're already reading */
2329 return 0;
2330 /* case TOR_TLS_DONE:
2331 * for TOR_TLS_DONE, fall through to check if the flushlen
2332 * is empty, so we can stop writing.
2336 tor_tls_get_n_raw_bytes(or_conn->tls, &n_read, &n_written);
2337 log_debug(LD_GENERAL, "After TLS write of %d: %ld read, %ld written",
2338 result, (long)n_read, (long)n_written);
2339 } else {
2340 CONN_LOG_PROTECT(conn,
2341 result = flush_buf(conn->s, conn->outbuf,
2342 max_to_write, &conn->outbuf_flushlen));
2343 if (result < 0) {
2344 if (CONN_IS_EDGE(conn))
2345 connection_edge_end_errno(TO_EDGE_CONN(conn));
2347 connection_close_immediate(conn); /* Don't flush; connection is dead. */
2348 connection_mark_for_close(conn);
2349 return -1;
2351 n_written = (size_t) result;
2354 if (conn->type == CONN_TYPE_AP) {
2355 edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
2356 /*XXXX021 check for overflow.*/
2357 edge_conn->n_written += (int)n_written;
2360 connection_buckets_decrement(conn, approx_time(), n_read, n_written);
2362 if (result > 0) {
2363 /* If we wrote any bytes from our buffer, then call the appropriate
2364 * functions. */
2365 if (connection_flushed_some(conn) < 0)
2366 connection_mark_for_close(conn);
2369 if (!connection_wants_to_flush(conn)) { /* it's done flushing */
2370 if (connection_finished_flushing(conn) < 0) {
2371 /* already marked */
2372 return -1;
2374 return 0;
2377 /* Call even if result is 0, since the global write bucket may
2378 * have reached 0 on a different conn, and this guy needs to
2379 * know to stop writing. */
2380 connection_consider_empty_write_buckets(conn);
2381 if (n_read > 0 && connection_is_reading(conn))
2382 connection_consider_empty_read_buckets(conn);
2384 return 0;
2387 /** OpenSSL TLS record size is 16383; this is close. The goal here is to
2388 * push data out as soon as we know there's enough for a TLS record, so
2389 * during periods of high load we won't read entire megabytes from
2390 * input before pushing any data out. It also has the feature of not
2391 * growing huge outbufs unless something is slow. */
2392 #define MIN_TLS_FLUSHLEN 15872
2394 /** Append <b>len</b> bytes of <b>string</b> onto <b>conn</b>'s
2395 * outbuf, and ask it to start writing.
2397 * If <b>zlib</b> is nonzero, this is a directory connection that should get
2398 * its contents compressed or decompressed as they're written. If zlib is
2399 * negative, this is the last data to be compressed, and the connection's zlib
2400 * state should be flushed.
2402 * If it's an OR conn and an entire TLS record is ready, then try to
2403 * flush the record now. Similarly, if it's a local control connection
2404 * and a 64k chunk is ready, try to flush it all, so we don't end up with
2405 * many megabytes of controller info queued at once.
2407 void
2408 _connection_write_to_buf_impl(const char *string, size_t len,
2409 connection_t *conn, int zlib)
2411 /* XXXX This function really needs to return -1 on failure. */
2412 int r;
2413 size_t old_datalen;
2414 if (!len && !(zlib<0))
2415 return;
2416 /* if it's marked for close, only allow write if we mean to flush it */
2417 if (conn->marked_for_close && !conn->hold_open_until_flushed)
2418 return;
2420 old_datalen = buf_datalen(conn->outbuf);
2421 if (zlib) {
2422 dir_connection_t *dir_conn = TO_DIR_CONN(conn);
2423 int done = zlib < 0;
2424 CONN_LOG_PROTECT(conn, r = write_to_buf_zlib(conn->outbuf,
2425 dir_conn->zlib_state,
2426 string, len, done));
2427 } else {
2428 CONN_LOG_PROTECT(conn, r = write_to_buf(string, len, conn->outbuf));
2430 if (r < 0) {
2431 if (CONN_IS_EDGE(conn)) {
2432 /* if it failed, it means we have our package/delivery windows set
2433 wrong compared to our max outbuf size. close the whole circuit. */
2434 log_warn(LD_NET,
2435 "write_to_buf failed. Closing circuit (fd %d).", conn->s);
2436 circuit_mark_for_close(circuit_get_by_edge_conn(TO_EDGE_CONN(conn)),
2437 END_CIRC_REASON_INTERNAL);
2438 } else {
2439 log_warn(LD_NET,
2440 "write_to_buf failed. Closing connection (fd %d).", conn->s);
2441 connection_mark_for_close(conn);
2443 return;
2446 connection_start_writing(conn);
2447 if (zlib) {
2448 conn->outbuf_flushlen += buf_datalen(conn->outbuf) - old_datalen;
2449 } else {
2450 ssize_t extra = 0;
2451 conn->outbuf_flushlen += len;
2453 /* Should we try flushing the outbuf now? */
2454 if (conn->in_flushed_some) {
2455 /* Don't flush the outbuf when the reason we're writing more stuff is
2456 * _because_ we flushed the outbuf. That's unfair. */
2457 return;
2460 if (conn->type == CONN_TYPE_OR &&
2461 conn->outbuf_flushlen-len < MIN_TLS_FLUSHLEN &&
2462 conn->outbuf_flushlen >= MIN_TLS_FLUSHLEN) {
2463 /* We just pushed outbuf_flushlen to MIN_TLS_FLUSHLEN or above;
2464 * we can send out a full TLS frame now if we like. */
2465 extra = conn->outbuf_flushlen - MIN_TLS_FLUSHLEN;
2466 conn->outbuf_flushlen = MIN_TLS_FLUSHLEN;
2467 } else if (conn->type == CONN_TYPE_CONTROL &&
2468 !connection_is_rate_limited(conn) &&
2469 conn->outbuf_flushlen-len < 1<<16 &&
2470 conn->outbuf_flushlen >= 1<<16) {
2471 /* just try to flush all of it */
2472 } else
2473 return; /* no need to try flushing */
2475 if (connection_handle_write(conn, 0) < 0) {
2476 if (!conn->marked_for_close) {
2477 /* this connection is broken. remove it. */
2478 log_warn(LD_BUG, "unhandled error on write for "
2479 "conn (type %d, fd %d); removing",
2480 conn->type, conn->s);
2481 tor_fragile_assert();
2482 /* do a close-immediate here, so we don't try to flush */
2483 connection_close_immediate(conn);
2485 return;
2487 if (extra) {
2488 conn->outbuf_flushlen += extra;
2489 connection_start_writing(conn);
2494 /** Return a connection with given type, address, port, and purpose;
2495 * or NULL if no such connection exists. */
2496 connection_t *
2497 connection_get_by_type_addr_port_purpose(int type,
2498 const tor_addr_t *addr, uint16_t port,
2499 int purpose)
2501 smartlist_t *conns = get_connection_array();
2502 SMARTLIST_FOREACH(conns, connection_t *, conn,
2504 if (conn->type == type &&
2505 tor_addr_eq(&conn->addr, addr) &&
2506 conn->port == port &&
2507 conn->purpose == purpose &&
2508 !conn->marked_for_close)
2509 return conn;
2511 return NULL;
2514 /** Return the stream with id <b>id</b> if it is not already marked for
2515 * close.
2517 connection_t *
2518 connection_get_by_global_id(uint64_t id)
2520 smartlist_t *conns = get_connection_array();
2521 SMARTLIST_FOREACH(conns, connection_t *, conn,
2523 if (conn->global_identifier == id)
2524 return conn;
2526 return NULL;
2529 /** Return a connection of type <b>type</b> that is not marked for close.
2531 connection_t *
2532 connection_get_by_type(int type)
2534 smartlist_t *conns = get_connection_array();
2535 SMARTLIST_FOREACH(conns, connection_t *, conn,
2537 if (conn->type == type && !conn->marked_for_close)
2538 return conn;
2540 return NULL;
2543 /** Return a connection of type <b>type</b> that is in state <b>state</b>,
2544 * and that is not marked for close.
2546 connection_t *
2547 connection_get_by_type_state(int type, int state)
2549 smartlist_t *conns = get_connection_array();
2550 SMARTLIST_FOREACH(conns, connection_t *, conn,
2552 if (conn->type == type && conn->state == state && !conn->marked_for_close)
2553 return conn;
2555 return NULL;
2558 /** Return a connection of type <b>type</b> that has rendquery equal
2559 * to <b>rendquery</b>, and that is not marked for close. If state
2560 * is non-zero, conn must be of that state too.
2562 connection_t *
2563 connection_get_by_type_state_rendquery(int type, int state,
2564 const char *rendquery)
2566 smartlist_t *conns = get_connection_array();
2568 tor_assert(type == CONN_TYPE_DIR ||
2569 type == CONN_TYPE_AP || type == CONN_TYPE_EXIT);
2570 tor_assert(rendquery);
2572 SMARTLIST_FOREACH(conns, connection_t *, conn,
2574 if (conn->type == type &&
2575 !conn->marked_for_close &&
2576 (!state || state == conn->state)) {
2577 if (type == CONN_TYPE_DIR &&
2578 TO_DIR_CONN(conn)->rend_data &&
2579 !rend_cmp_service_ids(rendquery,
2580 TO_DIR_CONN(conn)->rend_data->onion_address))
2581 return conn;
2582 else if (CONN_IS_EDGE(conn) &&
2583 TO_EDGE_CONN(conn)->rend_data &&
2584 !rend_cmp_service_ids(rendquery,
2585 TO_EDGE_CONN(conn)->rend_data->onion_address))
2586 return conn;
2589 return NULL;
2592 /** Return an open, non-marked connection of a given type and purpose, or NULL
2593 * if no such connection exists. */
2594 connection_t *
2595 connection_get_by_type_purpose(int type, int purpose)
2597 smartlist_t *conns = get_connection_array();
2598 SMARTLIST_FOREACH(conns, connection_t *, conn,
2600 if (conn->type == type &&
2601 !conn->marked_for_close &&
2602 (purpose == conn->purpose))
2603 return conn;
2605 return NULL;
2608 /** Return 1 if <b>conn</b> is a listener conn, else return 0. */
2610 connection_is_listener(connection_t *conn)
2612 if (conn->type == CONN_TYPE_OR_LISTENER ||
2613 conn->type == CONN_TYPE_AP_LISTENER ||
2614 conn->type == CONN_TYPE_AP_TRANS_LISTENER ||
2615 conn->type == CONN_TYPE_AP_DNS_LISTENER ||
2616 conn->type == CONN_TYPE_AP_NATD_LISTENER ||
2617 conn->type == CONN_TYPE_DIR_LISTENER ||
2618 conn->type == CONN_TYPE_CONTROL_LISTENER)
2619 return 1;
2620 return 0;
2623 /** Return 1 if <b>conn</b> is in state "open" and is not marked
2624 * for close, else return 0.
2627 connection_state_is_open(connection_t *conn)
2629 tor_assert(conn);
2631 if (conn->marked_for_close)
2632 return 0;
2634 if ((conn->type == CONN_TYPE_OR && conn->state == OR_CONN_STATE_OPEN) ||
2635 (conn->type == CONN_TYPE_AP && conn->state == AP_CONN_STATE_OPEN) ||
2636 (conn->type == CONN_TYPE_EXIT && conn->state == EXIT_CONN_STATE_OPEN) ||
2637 (conn->type == CONN_TYPE_CONTROL &&
2638 conn->state == CONTROL_CONN_STATE_OPEN))
2639 return 1;
2641 return 0;
2644 /** Return 1 if conn is in 'connecting' state, else return 0. */
2646 connection_state_is_connecting(connection_t *conn)
2648 tor_assert(conn);
2650 if (conn->marked_for_close)
2651 return 0;
2652 switch (conn->type)
2654 case CONN_TYPE_OR:
2655 return conn->state == OR_CONN_STATE_CONNECTING;
2656 case CONN_TYPE_EXIT:
2657 return conn->state == EXIT_CONN_STATE_CONNECTING;
2658 case CONN_TYPE_DIR:
2659 return conn->state == DIR_CONN_STATE_CONNECTING;
2662 return 0;
2665 /** Allocates a base64'ed authenticator for use in http or https
2666 * auth, based on the input string <b>authenticator</b>. Returns it
2667 * if success, else returns NULL. */
2668 char *
2669 alloc_http_authenticator(const char *authenticator)
2671 /* an authenticator in Basic authentication
2672 * is just the string "username:password" */
2673 const size_t authenticator_length = strlen(authenticator);
2674 /* The base64_encode function needs a minimum buffer length
2675 * of 66 bytes. */
2676 const size_t base64_authenticator_length = (authenticator_length/48+1)*66;
2677 char *base64_authenticator = tor_malloc(base64_authenticator_length);
2678 if (base64_encode(base64_authenticator, base64_authenticator_length,
2679 authenticator, authenticator_length) < 0) {
2680 tor_free(base64_authenticator); /* free and set to null */
2681 } else {
2682 /* remove extra \n at end of encoding */
2683 base64_authenticator[strlen(base64_authenticator) - 1] = 0;
2685 return base64_authenticator;
2688 /** Given a socket handle, check whether the local address (sockname) of the
2689 * socket is one that we've connected from before. If so, double-check
2690 * whether our address has changed and we need to generate keys. If we do,
2691 * call init_keys().
2693 static void
2694 client_check_address_changed(int sock)
2696 uint32_t iface_ip, ip_out;
2697 struct sockaddr_in out_addr;
2698 socklen_t out_addr_len = (socklen_t) sizeof(out_addr);
2699 uint32_t *ip;
2701 if (!last_interface_ip)
2702 get_interface_address(LOG_INFO, &last_interface_ip);
2703 if (!outgoing_addrs)
2704 outgoing_addrs = smartlist_create();
2706 if (getsockname(sock, (struct sockaddr*)&out_addr, &out_addr_len)<0) {
2707 int e = tor_socket_errno(sock);
2708 log_warn(LD_NET, "getsockname() to check for address change failed: %s",
2709 tor_socket_strerror(e));
2710 return;
2713 /* Okay. If we've used this address previously, we're okay. */
2714 ip_out = ntohl(out_addr.sin_addr.s_addr);
2715 SMARTLIST_FOREACH(outgoing_addrs, uint32_t*, ip_ptr,
2716 if (*ip_ptr == ip_out) return;
2719 /* Uh-oh. We haven't connected from this address before. Has the interface
2720 * address changed? */
2721 if (get_interface_address(LOG_INFO, &iface_ip)<0)
2722 return;
2723 ip = tor_malloc(sizeof(uint32_t));
2724 *ip = ip_out;
2726 if (iface_ip == last_interface_ip) {
2727 /* Nope, it hasn't changed. Add this address to the list. */
2728 smartlist_add(outgoing_addrs, ip);
2729 } else {
2730 /* The interface changed. We're a client, so we need to regenerate our
2731 * keys. First, reset the state. */
2732 log(LOG_NOTICE, LD_NET, "Our IP address has changed. Rotating keys...");
2733 last_interface_ip = iface_ip;
2734 SMARTLIST_FOREACH(outgoing_addrs, void*, ip_ptr, tor_free(ip_ptr));
2735 smartlist_clear(outgoing_addrs);
2736 smartlist_add(outgoing_addrs, ip);
2737 /* Okay, now change our keys. */
2738 ip_address_changed(1);
2742 /** Some systems have limited system buffers for recv and xmit on
2743 * sockets allocated in a virtual server or similar environment. For a Tor
2744 * server this can produce the "Error creating network socket: No buffer
2745 * space available" error once all available TCP buffer space is consumed.
2746 * This method will attempt to constrain the buffers allocated for the socket
2747 * to the desired size to stay below system TCP buffer limits.
2749 static void
2750 set_constrained_socket_buffers(int sock, int size)
2752 void *sz = (void*)&size;
2753 socklen_t sz_sz = (socklen_t) sizeof(size);
2754 if (setsockopt(sock, SOL_SOCKET, SO_SNDBUF, sz, sz_sz) < 0) {
2755 int e = tor_socket_errno(sock);
2756 log_warn(LD_NET, "setsockopt() to constrain send "
2757 "buffer to %d bytes failed: %s", size, tor_socket_strerror(e));
2759 if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF, sz, sz_sz) < 0) {
2760 int e = tor_socket_errno(sock);
2761 log_warn(LD_NET, "setsockopt() to constrain recv "
2762 "buffer to %d bytes failed: %s", size, tor_socket_strerror(e));
2766 /** Process new bytes that have arrived on conn-\>inbuf.
2768 * This function just passes conn to the connection-specific
2769 * connection_*_process_inbuf() function. It also passes in
2770 * package_partial if wanted.
2772 static int
2773 connection_process_inbuf(connection_t *conn, int package_partial)
2775 tor_assert(conn);
2777 switch (conn->type) {
2778 case CONN_TYPE_OR:
2779 return connection_or_process_inbuf(TO_OR_CONN(conn));
2780 case CONN_TYPE_EXIT:
2781 case CONN_TYPE_AP:
2782 return connection_edge_process_inbuf(TO_EDGE_CONN(conn),
2783 package_partial);
2784 case CONN_TYPE_DIR:
2785 return connection_dir_process_inbuf(TO_DIR_CONN(conn));
2786 case CONN_TYPE_CPUWORKER:
2787 return connection_cpu_process_inbuf(conn);
2788 case CONN_TYPE_CONTROL:
2789 return connection_control_process_inbuf(TO_CONTROL_CONN(conn));
2790 default:
2791 log_err(LD_BUG,"got unexpected conn type %d.", conn->type);
2792 tor_fragile_assert();
2793 return -1;
2797 /** Called whenever we've written data on a connection. */
2798 static int
2799 connection_flushed_some(connection_t *conn)
2801 int r = 0;
2802 tor_assert(!conn->in_flushed_some);
2803 conn->in_flushed_some = 1;
2804 if (conn->type == CONN_TYPE_DIR &&
2805 conn->state == DIR_CONN_STATE_SERVER_WRITING) {
2806 r = connection_dirserv_flushed_some(TO_DIR_CONN(conn));
2807 } else if (conn->type == CONN_TYPE_OR) {
2808 r = connection_or_flushed_some(TO_OR_CONN(conn));
2810 conn->in_flushed_some = 0;
2811 return r;
2814 /** We just finished flushing bytes from conn-\>outbuf, and there
2815 * are no more bytes remaining.
2817 * This function just passes conn to the connection-specific
2818 * connection_*_finished_flushing() function.
2820 static int
2821 connection_finished_flushing(connection_t *conn)
2823 tor_assert(conn);
2825 /* If the connection is closed, don't try to do anything more here. */
2826 if (CONN_IS_CLOSED(conn))
2827 return 0;
2829 // log_fn(LOG_DEBUG,"entered. Socket %u.", conn->s);
2831 switch (conn->type) {
2832 case CONN_TYPE_OR:
2833 return connection_or_finished_flushing(TO_OR_CONN(conn));
2834 case CONN_TYPE_AP:
2835 case CONN_TYPE_EXIT:
2836 return connection_edge_finished_flushing(TO_EDGE_CONN(conn));
2837 case CONN_TYPE_DIR:
2838 return connection_dir_finished_flushing(TO_DIR_CONN(conn));
2839 case CONN_TYPE_CPUWORKER:
2840 return connection_cpu_finished_flushing(conn);
2841 case CONN_TYPE_CONTROL:
2842 return connection_control_finished_flushing(TO_CONTROL_CONN(conn));
2843 default:
2844 log_err(LD_BUG,"got unexpected conn type %d.", conn->type);
2845 tor_fragile_assert();
2846 return -1;
2850 /** Called when our attempt to connect() to another server has just
2851 * succeeded.
2853 * This function just passes conn to the connection-specific
2854 * connection_*_finished_connecting() function.
2856 static int
2857 connection_finished_connecting(connection_t *conn)
2859 tor_assert(conn);
2860 switch (conn->type)
2862 case CONN_TYPE_OR:
2863 return connection_or_finished_connecting(TO_OR_CONN(conn));
2864 case CONN_TYPE_EXIT:
2865 return connection_edge_finished_connecting(TO_EDGE_CONN(conn));
2866 case CONN_TYPE_DIR:
2867 return connection_dir_finished_connecting(TO_DIR_CONN(conn));
2868 default:
2869 log_err(LD_BUG,"got unexpected conn type %d.", conn->type);
2870 tor_fragile_assert();
2871 return -1;
2875 /** Callback: invoked when a connection reaches an EOF event. */
2876 static int
2877 connection_reached_eof(connection_t *conn)
2879 switch (conn->type) {
2880 case CONN_TYPE_OR:
2881 return connection_or_reached_eof(TO_OR_CONN(conn));
2882 case CONN_TYPE_AP:
2883 case CONN_TYPE_EXIT:
2884 return connection_edge_reached_eof(TO_EDGE_CONN(conn));
2885 case CONN_TYPE_DIR:
2886 return connection_dir_reached_eof(TO_DIR_CONN(conn));
2887 case CONN_TYPE_CPUWORKER:
2888 return connection_cpu_reached_eof(conn);
2889 case CONN_TYPE_CONTROL:
2890 return connection_control_reached_eof(TO_CONTROL_CONN(conn));
2891 default:
2892 log_err(LD_BUG,"got unexpected conn type %d.", conn->type);
2893 tor_fragile_assert();
2894 return -1;
2898 /** Log how many bytes are used by buffers of different kinds and sizes. */
2899 void
2900 connection_dump_buffer_mem_stats(int severity)
2902 uint64_t used_by_type[_CONN_TYPE_MAX+1];
2903 uint64_t alloc_by_type[_CONN_TYPE_MAX+1];
2904 int n_conns_by_type[_CONN_TYPE_MAX+1];
2905 uint64_t total_alloc = 0;
2906 uint64_t total_used = 0;
2907 int i;
2908 smartlist_t *conns = get_connection_array();
2910 memset(used_by_type, 0, sizeof(used_by_type));
2911 memset(alloc_by_type, 0, sizeof(alloc_by_type));
2912 memset(n_conns_by_type, 0, sizeof(n_conns_by_type));
2914 SMARTLIST_FOREACH(conns, connection_t *, c,
2916 int tp = c->type;
2917 ++n_conns_by_type[tp];
2918 if (c->inbuf) {
2919 used_by_type[tp] += buf_datalen(c->inbuf);
2920 alloc_by_type[tp] += buf_allocation(c->inbuf);
2922 if (c->outbuf) {
2923 used_by_type[tp] += buf_datalen(c->outbuf);
2924 alloc_by_type[tp] += buf_allocation(c->outbuf);
2927 for (i=0; i <= _CONN_TYPE_MAX; ++i) {
2928 total_used += used_by_type[i];
2929 total_alloc += alloc_by_type[i];
2932 log(severity, LD_GENERAL,
2933 "In buffers for %d connections: "U64_FORMAT" used/"U64_FORMAT" allocated",
2934 smartlist_len(conns),
2935 U64_PRINTF_ARG(total_used), U64_PRINTF_ARG(total_alloc));
2936 for (i=_CONN_TYPE_MIN; i <= _CONN_TYPE_MAX; ++i) {
2937 if (!n_conns_by_type[i])
2938 continue;
2939 log(severity, LD_GENERAL,
2940 " For %d %s connections: "U64_FORMAT" used/"U64_FORMAT" allocated",
2941 n_conns_by_type[i], conn_type_to_string(i),
2942 U64_PRINTF_ARG(used_by_type[i]), U64_PRINTF_ARG(alloc_by_type[i]));
2946 /** Verify that connection <b>conn</b> has all of its invariants
2947 * correct. Trigger an assert if anything is invalid.
2949 void
2950 assert_connection_ok(connection_t *conn, time_t now)
2952 (void) now; /* XXXX unused. */
2953 tor_assert(conn);
2954 tor_assert(conn->type >= _CONN_TYPE_MIN);
2955 tor_assert(conn->type <= _CONN_TYPE_MAX);
2956 switch (conn->type) {
2957 case CONN_TYPE_OR:
2958 tor_assert(conn->magic == OR_CONNECTION_MAGIC);
2959 break;
2960 case CONN_TYPE_AP:
2961 case CONN_TYPE_EXIT:
2962 tor_assert(conn->magic == EDGE_CONNECTION_MAGIC);
2963 break;
2964 case CONN_TYPE_DIR:
2965 tor_assert(conn->magic == DIR_CONNECTION_MAGIC);
2966 break;
2967 case CONN_TYPE_CONTROL:
2968 tor_assert(conn->magic == CONTROL_CONNECTION_MAGIC);
2969 break;
2970 default:
2971 tor_assert(conn->magic == BASE_CONNECTION_MAGIC);
2972 break;
2975 if (conn->linked_conn) {
2976 tor_assert(conn->linked_conn->linked_conn == conn);
2977 tor_assert(conn->linked);
2979 if (conn->linked)
2980 tor_assert(conn->s < 0);
2982 if (conn->outbuf_flushlen > 0) {
2983 tor_assert(connection_is_writing(conn) || conn->write_blocked_on_bw ||
2984 (CONN_IS_EDGE(conn) && TO_EDGE_CONN(conn)->edge_blocked_on_circ));
2987 if (conn->hold_open_until_flushed)
2988 tor_assert(conn->marked_for_close);
2990 /* XXXX check: read_blocked_on_bw, write_blocked_on_bw, s, conn_array_index,
2991 * marked_for_close. */
2993 /* buffers */
2994 if (!connection_is_listener(conn)) {
2995 assert_buf_ok(conn->inbuf);
2996 assert_buf_ok(conn->outbuf);
2999 if (conn->type == CONN_TYPE_OR) {
3000 or_connection_t *or_conn = TO_OR_CONN(conn);
3001 if (conn->state == OR_CONN_STATE_OPEN) {
3002 /* tor_assert(conn->bandwidth > 0); */
3003 /* the above isn't necessarily true: if we just did a TLS
3004 * handshake but we didn't recognize the other peer, or it
3005 * gave a bad cert/etc, then we won't have assigned bandwidth,
3006 * yet it will be open. -RD
3008 // tor_assert(conn->read_bucket >= 0);
3010 // tor_assert(conn->addr && conn->port);
3011 tor_assert(conn->address);
3012 if (conn->state > OR_CONN_STATE_PROXY_READING)
3013 tor_assert(or_conn->tls);
3016 if (CONN_IS_EDGE(conn)) {
3017 edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
3018 if (edge_conn->chosen_exit_optional || edge_conn->chosen_exit_retries) {
3019 tor_assert(conn->type == CONN_TYPE_AP);
3020 tor_assert(edge_conn->chosen_exit_name);
3023 /* XXX unchecked: package window, deliver window. */
3024 if (conn->type == CONN_TYPE_AP) {
3026 tor_assert(edge_conn->socks_request);
3027 if (conn->state == AP_CONN_STATE_OPEN) {
3028 tor_assert(edge_conn->socks_request->has_finished);
3029 if (!conn->marked_for_close) {
3030 tor_assert(edge_conn->cpath_layer);
3031 assert_cpath_layer_ok(edge_conn->cpath_layer);
3035 if (conn->type == CONN_TYPE_EXIT) {
3036 tor_assert(conn->purpose == EXIT_PURPOSE_CONNECT ||
3037 conn->purpose == EXIT_PURPOSE_RESOLVE);
3039 } else if (conn->type == CONN_TYPE_DIR) {
3040 } else {
3041 /* Purpose is only used for dir and exit types currently */
3042 tor_assert(!conn->purpose);
3045 switch (conn->type)
3047 case CONN_TYPE_OR_LISTENER:
3048 case CONN_TYPE_AP_LISTENER:
3049 case CONN_TYPE_AP_TRANS_LISTENER:
3050 case CONN_TYPE_AP_NATD_LISTENER:
3051 case CONN_TYPE_DIR_LISTENER:
3052 case CONN_TYPE_CONTROL_LISTENER:
3053 case CONN_TYPE_AP_DNS_LISTENER:
3054 tor_assert(conn->state == LISTENER_STATE_READY);
3055 break;
3056 case CONN_TYPE_OR:
3057 tor_assert(conn->state >= _OR_CONN_STATE_MIN);
3058 tor_assert(conn->state <= _OR_CONN_STATE_MAX);
3059 tor_assert(TO_OR_CONN(conn)->n_circuits >= 0);
3060 break;
3061 case CONN_TYPE_EXIT:
3062 tor_assert(conn->state >= _EXIT_CONN_STATE_MIN);
3063 tor_assert(conn->state <= _EXIT_CONN_STATE_MAX);
3064 tor_assert(conn->purpose >= _EXIT_PURPOSE_MIN);
3065 tor_assert(conn->purpose <= _EXIT_PURPOSE_MAX);
3066 break;
3067 case CONN_TYPE_AP:
3068 tor_assert(conn->state >= _AP_CONN_STATE_MIN);
3069 tor_assert(conn->state <= _AP_CONN_STATE_MAX);
3070 tor_assert(TO_EDGE_CONN(conn)->socks_request);
3071 break;
3072 case CONN_TYPE_DIR:
3073 tor_assert(conn->state >= _DIR_CONN_STATE_MIN);
3074 tor_assert(conn->state <= _DIR_CONN_STATE_MAX);
3075 tor_assert(conn->purpose >= _DIR_PURPOSE_MIN);
3076 tor_assert(conn->purpose <= _DIR_PURPOSE_MAX);
3077 break;
3078 case CONN_TYPE_CPUWORKER:
3079 tor_assert(conn->state >= _CPUWORKER_STATE_MIN);
3080 tor_assert(conn->state <= _CPUWORKER_STATE_MAX);
3081 break;
3082 case CONN_TYPE_CONTROL:
3083 tor_assert(conn->state >= _CONTROL_CONN_STATE_MIN);
3084 tor_assert(conn->state <= _CONTROL_CONN_STATE_MAX);
3085 break;
3086 default:
3087 tor_assert(0);