Detect and disallow compression bombs
[tor/rransom.git] / src / or / connection.c
blob4869a2439ac3739dc860a7a4790ed0e4cd154764
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-2011, 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 /** Tell libevent that we don't care about <b>conn</b> any more. */
303 void
304 connection_unregister_events(connection_t *conn)
306 if (conn->read_event) {
307 if (event_del(conn->read_event))
308 log_warn(LD_BUG, "Error removing read event for %d", conn->s);
309 tor_free(conn->read_event);
311 if (conn->write_event) {
312 if (event_del(conn->write_event))
313 log_warn(LD_BUG, "Error removing write event for %d", conn->s);
314 tor_free(conn->write_event);
316 if (conn->dns_server_port) {
317 dnsserv_close_listener(conn);
321 /** Deallocate memory used by <b>conn</b>. Deallocate its buffers if
322 * necessary, close its socket if necessary, and mark the directory as dirty
323 * if <b>conn</b> is an OR or OP connection.
325 static void
326 _connection_free(connection_t *conn)
328 void *mem;
329 size_t memlen;
330 switch (conn->type) {
331 case CONN_TYPE_OR:
332 tor_assert(conn->magic == OR_CONNECTION_MAGIC);
333 mem = TO_OR_CONN(conn);
334 memlen = sizeof(or_connection_t);
335 break;
336 case CONN_TYPE_AP:
337 case CONN_TYPE_EXIT:
338 tor_assert(conn->magic == EDGE_CONNECTION_MAGIC);
339 mem = TO_EDGE_CONN(conn);
340 memlen = sizeof(edge_connection_t);
341 break;
342 case CONN_TYPE_DIR:
343 tor_assert(conn->magic == DIR_CONNECTION_MAGIC);
344 mem = TO_DIR_CONN(conn);
345 memlen = sizeof(dir_connection_t);
346 break;
347 case CONN_TYPE_CONTROL:
348 tor_assert(conn->magic == CONTROL_CONNECTION_MAGIC);
349 mem = TO_CONTROL_CONN(conn);
350 memlen = sizeof(control_connection_t);
351 break;
352 default:
353 tor_assert(conn->magic == BASE_CONNECTION_MAGIC);
354 mem = conn;
355 memlen = sizeof(connection_t);
356 break;
359 if (conn->linked) {
360 log_info(LD_GENERAL, "Freeing linked %s connection [%s] with %d "
361 "bytes on inbuf, %d on outbuf.",
362 conn_type_to_string(conn->type),
363 conn_state_to_string(conn->type, conn->state),
364 (int)buf_datalen(conn->inbuf), (int)buf_datalen(conn->outbuf));
367 if (!connection_is_listener(conn)) {
368 buf_free(conn->inbuf);
369 buf_free(conn->outbuf);
370 } else {
371 if (conn->socket_family == AF_UNIX) {
372 /* For now only control ports can be Unix domain sockets
373 * and listeners at the same time */
374 tor_assert(conn->type == CONN_TYPE_CONTROL_LISTENER);
376 if (unlink(conn->address) < 0 && errno != ENOENT) {
377 log_warn(LD_NET, "Could not unlink %s: %s", conn->address,
378 strerror(errno));
383 tor_free(conn->address);
385 if (connection_speaks_cells(conn)) {
386 or_connection_t *or_conn = TO_OR_CONN(conn);
387 if (or_conn->tls) {
388 tor_tls_free(or_conn->tls);
389 or_conn->tls = NULL;
391 if (or_conn->handshake_state) {
392 or_handshake_state_free(or_conn->handshake_state);
393 or_conn->handshake_state = NULL;
395 tor_free(or_conn->nickname);
397 if (CONN_IS_EDGE(conn)) {
398 edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
399 tor_free(edge_conn->chosen_exit_name);
400 if (edge_conn->socks_request) {
401 memset(edge_conn->socks_request, 0xcc, sizeof(socks_request_t));
402 tor_free(edge_conn->socks_request);
404 if (edge_conn->rend_data)
405 rend_data_free(edge_conn->rend_data);
407 if (conn->type == CONN_TYPE_CONTROL) {
408 control_connection_t *control_conn = TO_CONTROL_CONN(conn);
409 tor_free(control_conn->incoming_cmd);
412 tor_free(conn->read_event); /* Probably already freed by connection_free. */
413 tor_free(conn->write_event); /* Probably already freed by connection_free. */
415 if (conn->type == CONN_TYPE_DIR) {
416 dir_connection_t *dir_conn = TO_DIR_CONN(conn);
417 tor_free(dir_conn->requested_resource);
418 if (dir_conn->zlib_state)
419 tor_zlib_free(dir_conn->zlib_state);
420 if (dir_conn->fingerprint_stack) {
421 SMARTLIST_FOREACH(dir_conn->fingerprint_stack, char *, cp, tor_free(cp));
422 smartlist_free(dir_conn->fingerprint_stack);
424 if (dir_conn->cached_dir)
425 cached_dir_decref(dir_conn->cached_dir);
426 if (dir_conn->rend_data)
427 rend_data_free(dir_conn->rend_data);
430 if (conn->s >= 0) {
431 log_debug(LD_NET,"closing fd %d.",conn->s);
432 tor_close_socket(conn->s);
433 conn->s = -1;
436 if (conn->type == CONN_TYPE_OR &&
437 !tor_digest_is_zero(TO_OR_CONN(conn)->identity_digest)) {
438 log_warn(LD_BUG, "called on OR conn with non-zeroed identity_digest");
439 connection_or_remove_from_identity_map(TO_OR_CONN(conn));
442 memset(conn, 0xAA, memlen); /* poison memory */
443 tor_free(mem);
446 /** Make sure <b>conn</b> isn't in any of the global conn lists; then free it.
448 void
449 connection_free(connection_t *conn)
451 tor_assert(conn);
452 tor_assert(!connection_is_on_closeable_list(conn));
453 tor_assert(!connection_in_array(conn));
454 if (conn->linked_conn) {
455 log_err(LD_BUG, "Called with conn->linked_conn still set.");
456 tor_fragile_assert();
457 conn->linked_conn->linked_conn = NULL;
458 if (! conn->linked_conn->marked_for_close &&
459 conn->linked_conn->reading_from_linked_conn)
460 connection_start_reading(conn->linked_conn);
461 conn->linked_conn = NULL;
463 if (connection_speaks_cells(conn)) {
464 if (!tor_digest_is_zero(TO_OR_CONN(conn)->identity_digest)) {
465 connection_or_remove_from_identity_map(TO_OR_CONN(conn));
468 if (conn->type == CONN_TYPE_CONTROL) {
469 TO_CONTROL_CONN(conn)->event_mask = 0;
470 control_update_global_event_mask();
472 connection_unregister_events(conn);
473 _connection_free(conn);
476 /** Call _connection_free() on every connection in our array, and release all
477 * storage held by connection.c. This is used by cpuworkers and dnsworkers
478 * when they fork, so they don't keep resources held open (especially
479 * sockets).
481 * Don't do the checks in connection_free(), because they will
482 * fail.
484 void
485 connection_free_all(void)
487 smartlist_t *conns = get_connection_array();
489 /* We don't want to log any messages to controllers. */
490 SMARTLIST_FOREACH(conns, connection_t *, conn,
491 if (conn->type == CONN_TYPE_CONTROL)
492 TO_CONTROL_CONN(conn)->event_mask = 0);
494 control_update_global_event_mask();
496 /* Unlink everything from the identity map. */
497 connection_or_clear_identity_map();
499 SMARTLIST_FOREACH(conns, connection_t *, conn, _connection_free(conn));
501 if (outgoing_addrs) {
502 SMARTLIST_FOREACH(outgoing_addrs, void*, addr, tor_free(addr));
503 smartlist_free(outgoing_addrs);
504 outgoing_addrs = NULL;
508 /** Do any cleanup needed:
509 * - Directory conns that failed to fetch a rendezvous descriptor
510 * need to inform pending rendezvous streams.
511 * - OR conns need to call rep_hist_note_*() to record status.
512 * - AP conns need to send a socks reject if necessary.
513 * - Exit conns need to call connection_dns_remove() if necessary.
514 * - AP and Exit conns need to send an end cell if they can.
515 * - DNS conns need to fail any resolves that are pending on them.
516 * - OR and edge connections need to be unlinked from circuits.
518 void
519 connection_about_to_close_connection(connection_t *conn)
521 circuit_t *circ;
522 dir_connection_t *dir_conn;
523 or_connection_t *or_conn;
524 edge_connection_t *edge_conn;
525 time_t now = time(NULL);
527 tor_assert(conn->marked_for_close);
529 if (CONN_IS_EDGE(conn)) {
530 edge_conn = TO_EDGE_CONN(conn);
531 if (!edge_conn->edge_has_sent_end) {
532 log_warn(LD_BUG, "(Harmless.) Edge connection (marked at %s:%d) "
533 "hasn't sent end yet?",
534 conn->marked_for_close_file, conn->marked_for_close);
535 tor_fragile_assert();
539 switch (conn->type) {
540 case CONN_TYPE_DIR:
541 dir_conn = TO_DIR_CONN(conn);
542 if (conn->state < DIR_CONN_STATE_CLIENT_FINISHED) {
543 /* It's a directory connection and connecting or fetching
544 * failed: forget about this router, and maybe try again. */
545 connection_dir_request_failed(dir_conn);
547 if (conn->purpose == DIR_PURPOSE_FETCH_RENDDESC && dir_conn->rend_data) {
548 /* Give it a try. However, there is no re-fetching for v0 rend
549 * descriptors; if the response is empty or the descriptor is
550 * unusable, close pending connections (unless a v2 request is
551 * still in progress). */
552 rend_client_desc_trynow(dir_conn->rend_data->onion_address, 0);
554 /* If we were trying to fetch a v2 rend desc and did not succeed,
555 * retry as needed. (If a fetch is successful, the connection state
556 * is changed to DIR_PURPOSE_HAS_FETCHED_RENDDESC to mark that
557 * refetching is unnecessary.) */
558 if (conn->purpose == DIR_PURPOSE_FETCH_RENDDESC_V2 &&
559 dir_conn->rend_data &&
560 strlen(dir_conn->rend_data->onion_address) ==
561 REND_SERVICE_ID_LEN_BASE32)
562 rend_client_refetch_v2_renddesc(dir_conn->rend_data);
563 break;
564 case CONN_TYPE_OR:
565 or_conn = TO_OR_CONN(conn);
566 /* Remember why we're closing this connection. */
567 if (conn->state != OR_CONN_STATE_OPEN) {
568 /* Inform any pending (not attached) circs that they should
569 * give up. */
570 circuit_n_conn_done(TO_OR_CONN(conn), 0);
571 /* now mark things down as needed */
572 if (connection_or_nonopen_was_started_here(or_conn)) {
573 or_options_t *options = get_options();
574 rep_hist_note_connect_failed(or_conn->identity_digest, now);
575 entry_guard_register_connect_status(or_conn->identity_digest,0,
576 !options->HttpsProxy, now);
577 if (conn->state >= OR_CONN_STATE_TLS_HANDSHAKING) {
578 int reason = tls_error_to_orconn_end_reason(or_conn->tls_error);
579 control_event_or_conn_status(or_conn, OR_CONN_EVENT_FAILED,
580 reason);
581 if (!authdir_mode_tests_reachability(options))
582 control_event_bootstrap_problem(
583 orconn_end_reason_to_control_string(reason), reason);
586 } else if (conn->hold_open_until_flushed) {
587 /* We only set hold_open_until_flushed when we're intentionally
588 * closing a connection. */
589 rep_hist_note_disconnect(or_conn->identity_digest, now);
590 control_event_or_conn_status(or_conn, OR_CONN_EVENT_CLOSED,
591 tls_error_to_orconn_end_reason(or_conn->tls_error));
592 } else if (or_conn->identity_digest) {
593 rep_hist_note_connection_died(or_conn->identity_digest, now);
594 control_event_or_conn_status(or_conn, OR_CONN_EVENT_CLOSED,
595 tls_error_to_orconn_end_reason(or_conn->tls_error));
597 /* Now close all the attached circuits on it. */
598 circuit_unlink_all_from_or_conn(TO_OR_CONN(conn),
599 END_CIRC_REASON_OR_CONN_CLOSED);
600 break;
601 case CONN_TYPE_AP:
602 edge_conn = TO_EDGE_CONN(conn);
603 if (edge_conn->socks_request->has_finished == 0) {
604 /* since conn gets removed right after this function finishes,
605 * there's no point trying to send back a reply at this point. */
606 log_warn(LD_BUG,"Closing stream (marked at %s:%d) without sending"
607 " back a socks reply.",
608 conn->marked_for_close_file, conn->marked_for_close);
610 if (!edge_conn->end_reason) {
611 log_warn(LD_BUG,"Closing stream (marked at %s:%d) without having"
612 " set end_reason.",
613 conn->marked_for_close_file, conn->marked_for_close);
615 if (edge_conn->dns_server_request) {
616 log_warn(LD_BUG,"Closing stream (marked at %s:%d) without having"
617 " replied to DNS request.",
618 conn->marked_for_close_file, conn->marked_for_close);
619 dnsserv_reject_request(edge_conn);
621 control_event_stream_bandwidth(edge_conn);
622 control_event_stream_status(edge_conn, STREAM_EVENT_CLOSED,
623 edge_conn->end_reason);
624 circ = circuit_get_by_edge_conn(edge_conn);
625 if (circ)
626 circuit_detach_stream(circ, edge_conn);
627 break;
628 case CONN_TYPE_EXIT:
629 edge_conn = TO_EDGE_CONN(conn);
630 circ = circuit_get_by_edge_conn(edge_conn);
631 if (circ)
632 circuit_detach_stream(circ, edge_conn);
633 if (conn->state == EXIT_CONN_STATE_RESOLVING) {
634 connection_dns_remove(edge_conn);
636 break;
640 /** Return true iff connection_close_immediate() has been called on this
641 * connection. */
642 #define CONN_IS_CLOSED(c) \
643 ((c)->linked ? ((c)->linked_conn_is_closed) : ((c)->s < 0))
645 /** Close the underlying socket for <b>conn</b>, so we don't try to
646 * flush it. Must be used in conjunction with (right before)
647 * connection_mark_for_close().
649 void
650 connection_close_immediate(connection_t *conn)
652 assert_connection_ok(conn,0);
653 if (CONN_IS_CLOSED(conn)) {
654 log_err(LD_BUG,"Attempt to close already-closed connection.");
655 tor_fragile_assert();
656 return;
658 if (conn->outbuf_flushlen) {
659 log_info(LD_NET,"fd %d, type %s, state %s, %d bytes on outbuf.",
660 conn->s, conn_type_to_string(conn->type),
661 conn_state_to_string(conn->type, conn->state),
662 (int)conn->outbuf_flushlen);
665 connection_unregister_events(conn);
667 if (conn->s >= 0)
668 tor_close_socket(conn->s);
669 conn->s = -1;
670 if (conn->linked)
671 conn->linked_conn_is_closed = 1;
672 if (!connection_is_listener(conn)) {
673 buf_clear(conn->outbuf);
674 conn->outbuf_flushlen = 0;
678 /** Mark <b>conn</b> to be closed next time we loop through
679 * conn_close_if_marked() in main.c. */
680 void
681 _connection_mark_for_close(connection_t *conn, int line, const char *file)
683 assert_connection_ok(conn,0);
684 tor_assert(line);
685 tor_assert(line < 1<<16); /* marked_for_close can only fit a uint16_t. */
686 tor_assert(file);
688 if (conn->marked_for_close) {
689 log(LOG_WARN,LD_BUG,"Duplicate call to connection_mark_for_close at %s:%d"
690 " (first at %s:%d)", file, line, conn->marked_for_close_file,
691 conn->marked_for_close);
692 tor_fragile_assert();
693 return;
696 conn->marked_for_close = line;
697 conn->marked_for_close_file = file;
698 add_connection_to_closeable_list(conn);
700 /* in case we're going to be held-open-til-flushed, reset
701 * the number of seconds since last successful write, so
702 * we get our whole 15 seconds */
703 conn->timestamp_lastwritten = time(NULL);
706 /** Find each connection that has hold_open_until_flushed set to
707 * 1 but hasn't written in the past 15 seconds, and set
708 * hold_open_until_flushed to 0. This means it will get cleaned
709 * up in the next loop through close_if_marked() in main.c.
711 void
712 connection_expire_held_open(void)
714 time_t now;
715 smartlist_t *conns = get_connection_array();
717 now = time(NULL);
719 SMARTLIST_FOREACH(conns, connection_t *, conn,
721 /* If we've been holding the connection open, but we haven't written
722 * for 15 seconds...
724 if (conn->hold_open_until_flushed) {
725 tor_assert(conn->marked_for_close);
726 if (now - conn->timestamp_lastwritten >= 15) {
727 int severity;
728 if (conn->type == CONN_TYPE_EXIT ||
729 (conn->type == CONN_TYPE_DIR &&
730 conn->purpose == DIR_PURPOSE_SERVER))
731 severity = LOG_INFO;
732 else
733 severity = LOG_NOTICE;
734 log_fn(severity, LD_NET,
735 "Giving up on marked_for_close conn that's been flushing "
736 "for 15s (fd %d, type %s, state %s).",
737 conn->s, conn_type_to_string(conn->type),
738 conn_state_to_string(conn->type, conn->state));
739 conn->hold_open_until_flushed = 0;
745 /** Create an AF_INET listenaddr struct.
746 * <b>listenaddress</b> provides the host and optionally the port information
747 * for the new structure. If no port is provided in <b>listenaddress</b> then
748 * <b>listenport</b> is used.
750 * If not NULL <b>readable_address</b> will contain a copy of the host part of
751 * <b>listenaddress</b>.
753 * The listenaddr struct has to be freed by the caller.
755 static struct sockaddr_in *
756 create_inet_sockaddr(const char *listenaddress, uint16_t listenport,
757 char **readable_address, socklen_t *socklen_out) {
758 struct sockaddr_in *listenaddr = NULL;
759 uint32_t addr;
760 uint16_t usePort = 0;
762 if (parse_addr_port(LOG_WARN,
763 listenaddress, readable_address, &addr, &usePort)<0) {
764 log_warn(LD_CONFIG,
765 "Error parsing/resolving ListenAddress %s", listenaddress);
766 goto err;
768 if (usePort==0)
769 usePort = listenport;
771 listenaddr = tor_malloc_zero(sizeof(struct sockaddr_in));
772 listenaddr->sin_addr.s_addr = htonl(addr);
773 listenaddr->sin_family = AF_INET;
774 listenaddr->sin_port = htons((uint16_t) usePort);
776 *socklen_out = sizeof(struct sockaddr_in);
778 return listenaddr;
780 err:
781 tor_free(listenaddr);
782 return NULL;
785 #ifdef HAVE_SYS_UN_H
786 /** Create an AF_UNIX listenaddr struct.
787 * <b>listenaddress</b> provides the path to the Unix socket.
789 * Eventually <b>listenaddress</b> will also optionally contain user, group,
790 * and file permissions for the new socket. But not yet. XXX
791 * Also, since we do not create the socket here the information doesn't help
792 * here.
794 * If not NULL <b>readable_address</b> will contain a copy of the path part of
795 * <b>listenaddress</b>.
797 * The listenaddr struct has to be freed by the caller.
799 static struct sockaddr_un *
800 create_unix_sockaddr(const char *listenaddress, char **readable_address,
801 socklen_t *len_out)
803 struct sockaddr_un *sockaddr = NULL;
805 sockaddr = tor_malloc_zero(sizeof(struct sockaddr_un));
806 sockaddr->sun_family = AF_UNIX;
807 strncpy(sockaddr->sun_path, listenaddress, sizeof(sockaddr->sun_path));
809 if (readable_address)
810 *readable_address = tor_strdup(listenaddress);
812 *len_out = sizeof(struct sockaddr_un);
813 return sockaddr;
815 #else
816 static struct sockaddr *
817 create_unix_sockaddr(const char *listenaddress, char **readable_address,
818 socklen_t *len_out)
820 (void)listenaddress;
821 (void)readable_address;
822 log_fn(LOG_ERR, LD_BUG,
823 "Unix domain sockets not supported, yet we tried to create one.");
824 *len_out = 0;
825 tor_assert(0);
827 #endif /* HAVE_SYS_UN_H */
829 /** Warn that an accept or a connect has failed because we're running up
830 * against our ulimit. Rate-limit these warnings so that we don't spam
831 * the log. */
832 static void
833 warn_too_many_conns(void)
835 #define WARN_TOO_MANY_CONNS_INTERVAL (6*60*60)
836 static time_t last_warned = 0;
837 time_t now = time(NULL);
838 int n_conns = get_n_open_sockets();
839 if (last_warned + WARN_TOO_MANY_CONNS_INTERVAL < now) {
840 log_warn(LD_NET,"Failing because we have %d connections already. Please "
841 "raise your ulimit -n.", n_conns);
842 last_warned = now;
844 control_event_general_status(LOG_WARN, "TOO_MANY_CONNECTIONS CURRENT=%d",
845 n_conns);
848 /** Bind a new non-blocking socket listening to the socket described
849 * by <b>listensockaddr</b>.
851 * <b>address</b> is only used for logging purposes and to add the information
852 * to the conn.
854 static connection_t *
855 connection_create_listener(struct sockaddr *listensockaddr, socklen_t socklen,
856 int type, char* address)
858 connection_t *conn;
859 int s; /* the socket we're going to make */
860 uint16_t usePort = 0;
861 int start_reading = 0;
863 if (get_n_open_sockets() >= get_options()->_ConnLimit-1) {
864 warn_too_many_conns();
865 return NULL;
868 if (listensockaddr->sa_family == AF_INET) {
869 int is_tcp = (type != CONN_TYPE_AP_DNS_LISTENER);
870 #ifndef MS_WINDOWS
871 int one=1;
872 #endif
873 if (is_tcp)
874 start_reading = 1;
876 usePort = ntohs( (uint16_t)
877 ((struct sockaddr_in *)listensockaddr)->sin_port);
879 log_notice(LD_NET, "Opening %s on %s:%d",
880 conn_type_to_string(type), address, usePort);
882 s = tor_open_socket(PF_INET,
883 is_tcp ? SOCK_STREAM : SOCK_DGRAM,
884 is_tcp ? IPPROTO_TCP: IPPROTO_UDP);
885 if (s < 0) {
886 log_warn(LD_NET,"Socket creation failed.");
887 goto err;
890 #ifndef MS_WINDOWS
891 /* REUSEADDR on normal places means you can rebind to the port
892 * right after somebody else has let it go. But REUSEADDR on win32
893 * means you can bind to the port _even when somebody else
894 * already has it bound_. So, don't do that on Win32. */
895 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (void*) &one,
896 (socklen_t)sizeof(one));
897 #endif
899 if (bind(s,listensockaddr,socklen) < 0) {
900 const char *helpfulhint = "";
901 int e = tor_socket_errno(s);
902 if (ERRNO_IS_EADDRINUSE(e))
903 helpfulhint = ". Is Tor already running?";
904 log_warn(LD_NET, "Could not bind to %s:%u: %s%s", address, usePort,
905 tor_socket_strerror(e), helpfulhint);
906 tor_close_socket(s);
907 goto err;
910 if (is_tcp) {
911 if (listen(s,SOMAXCONN) < 0) {
912 log_warn(LD_NET, "Could not listen on %s:%u: %s", address, usePort,
913 tor_socket_strerror(tor_socket_errno(s)));
914 tor_close_socket(s);
915 goto err;
918 #ifdef HAVE_SYS_UN_H
919 } else if (listensockaddr->sa_family == AF_UNIX) {
920 start_reading = 1;
922 /* For now only control ports can be Unix domain sockets
923 * and listeners at the same time */
924 tor_assert(type == CONN_TYPE_CONTROL_LISTENER);
926 log_notice(LD_NET, "Opening %s on %s",
927 conn_type_to_string(type), address);
929 if (unlink(address) < 0 && errno != ENOENT) {
930 log_warn(LD_NET, "Could not unlink %s: %s", address,
931 strerror(errno));
932 goto err;
934 s = tor_open_socket(AF_UNIX, SOCK_STREAM, 0);
935 if (s < 0) {
936 log_warn(LD_NET,"Socket creation failed: %s.", strerror(errno));
937 goto err;
940 if (bind(s, listensockaddr, (socklen_t)sizeof(struct sockaddr_un)) == -1) {
941 log_warn(LD_NET,"Bind to %s failed: %s.", address,
942 tor_socket_strerror(tor_socket_errno(s)));
943 goto err;
946 if (listen(s,SOMAXCONN) < 0) {
947 log_warn(LD_NET, "Could not listen on %s: %s", address,
948 tor_socket_strerror(tor_socket_errno(s)));
949 tor_close_socket(s);
950 goto err;
952 #endif /* HAVE_SYS_UN_H */
953 } else {
954 log_err(LD_BUG,"Got unexpected address family %d.",
955 listensockaddr->sa_family);
956 tor_assert(0);
959 set_socket_nonblocking(s);
961 conn = connection_new(type, listensockaddr->sa_family);
962 conn->socket_family = listensockaddr->sa_family;
963 conn->s = s;
964 conn->address = tor_strdup(address);
965 conn->port = usePort;
967 if (connection_add(conn) < 0) { /* no space, forget it */
968 log_warn(LD_NET,"connection_add for listener failed. Giving up.");
969 connection_free(conn);
970 goto err;
973 log_debug(LD_NET,"%s listening on port %u.",
974 conn_type_to_string(type), usePort);
976 conn->state = LISTENER_STATE_READY;
977 if (start_reading) {
978 connection_start_reading(conn);
979 } else {
980 tor_assert(type == CONN_TYPE_AP_DNS_LISTENER);
981 dnsserv_configure_listener(conn);
984 return conn;
986 err:
987 return NULL;
990 /** Do basic sanity checking on a newly received socket. Return 0
991 * if it looks ok, else return -1. */
992 static int
993 check_sockaddr(struct sockaddr *sa, int len, int level)
995 int ok = 1;
997 if (sa->sa_family == AF_INET) {
998 struct sockaddr_in *sin=(struct sockaddr_in*)sa;
999 if (len != sizeof(struct sockaddr_in)) {
1000 log_fn(level, LD_NET, "Length of address not as expected: %d vs %d",
1001 len,(int)sizeof(struct sockaddr_in));
1002 ok = 0;
1004 if (sin->sin_addr.s_addr == 0 || sin->sin_port == 0) {
1005 log_fn(level, LD_NET,
1006 "Address for new connection has address/port equal to zero.");
1007 ok = 0;
1009 } else if (sa->sa_family == AF_INET6) {
1010 struct sockaddr_in6 *sin6=(struct sockaddr_in6*)sa;
1011 if (len != sizeof(struct sockaddr_in6)) {
1012 log_fn(level, LD_NET, "Length of address not as expected: %d vs %d",
1013 len,(int)sizeof(struct sockaddr_in6));
1014 ok = 0;
1016 if (tor_mem_is_zero((void*)sin6->sin6_addr.s6_addr, 16) ||
1017 sin6->sin6_port == 0) {
1018 log_fn(level, LD_NET,
1019 "Address for new connection has address/port equal to zero.");
1020 ok = 0;
1022 } else {
1023 ok = 0;
1025 return ok ? 0 : -1;
1028 /** Check whether the socket family from an accepted socket <b>got</b> is the
1029 * same as the one that <b>listener</b> is waiting for. If it isn't, log
1030 * a useful message and return -1. Else return 0.
1032 * This is annoying, but can apparently happen on some Darwins. */
1033 static int
1034 check_sockaddr_family_match(sa_family_t got, connection_t *listener)
1036 if (got != listener->socket_family) {
1037 log_info(LD_BUG, "A listener connection returned a socket with a "
1038 "mismatched family. %s for addr_family %d gave us a socket "
1039 "with address family %d. Dropping.",
1040 conn_type_to_string(listener->type),
1041 (int)listener->socket_family,
1042 (int)got);
1043 return -1;
1045 return 0;
1048 /** The listener connection <b>conn</b> told poll() it wanted to read.
1049 * Call accept() on conn-\>s, and add the new connection if necessary.
1051 static int
1052 connection_handle_listener_read(connection_t *conn, int new_type)
1054 int news; /* the new socket */
1055 connection_t *newconn;
1056 /* information about the remote peer when connecting to other routers */
1057 char addrbuf[256];
1058 struct sockaddr *remote = (struct sockaddr*)addrbuf;
1059 /* length of the remote address. Must be whatever accept() needs. */
1060 socklen_t remotelen = (socklen_t)sizeof(addrbuf);
1061 or_options_t *options = get_options();
1063 tor_assert((size_t)remotelen >= sizeof(struct sockaddr_in));
1064 memset(addrbuf, 0, sizeof(addrbuf));
1066 news = tor_accept_socket(conn->s,remote,&remotelen);
1067 if (news < 0) { /* accept() error */
1068 int e = tor_socket_errno(conn->s);
1069 if (ERRNO_IS_ACCEPT_EAGAIN(e)) {
1070 return 0; /* he hung up before we could accept(). that's fine. */
1071 } else if (ERRNO_IS_ACCEPT_RESOURCE_LIMIT(e)) {
1072 warn_too_many_conns();
1073 return 0;
1075 /* else there was a real error. */
1076 log_warn(LD_NET,"accept() failed: %s. Closing listener.",
1077 tor_socket_strerror(e));
1078 connection_mark_for_close(conn);
1079 return -1;
1081 log_debug(LD_NET,
1082 "Connection accepted on socket %d (child of fd %d).",
1083 news,conn->s);
1085 set_socket_nonblocking(news);
1087 if (options->ConstrainedSockets)
1088 set_constrained_socket_buffers(news, (int)options->ConstrainedSockSize);
1090 if (check_sockaddr_family_match(remote->sa_family, conn) < 0) {
1091 tor_close_socket(news);
1092 return 0;
1095 if (conn->socket_family == AF_INET || conn->socket_family == AF_INET6) {
1096 tor_addr_t addr;
1097 uint16_t port;
1098 if (check_sockaddr(remote, remotelen, LOG_INFO)<0) {
1099 log_info(LD_NET,
1100 "accept() returned a strange address; trying getsockname().");
1101 remotelen=sizeof(addrbuf);
1102 memset(addrbuf, 0, sizeof(addrbuf));
1103 if (getsockname(news, remote, &remotelen)<0) {
1104 int e = tor_socket_errno(news);
1105 log_warn(LD_NET, "getsockname() for new connection failed: %s",
1106 tor_socket_strerror(e));
1107 } else {
1108 if (check_sockaddr((struct sockaddr*)addrbuf, remotelen,
1109 LOG_WARN) < 0) {
1110 log_warn(LD_NET,"Something's wrong with this conn. Closing it.");
1111 tor_close_socket(news);
1112 return 0;
1117 if (check_sockaddr_family_match(remote->sa_family, conn) < 0) {
1118 tor_close_socket(news);
1119 return 0;
1122 tor_addr_from_sockaddr(&addr, remote, &port);
1124 /* process entrance policies here, before we even create the connection */
1125 if (new_type == CONN_TYPE_AP) {
1126 /* check sockspolicy to see if we should accept it */
1127 if (socks_policy_permits_address(&addr) == 0) {
1128 log_notice(LD_APP,
1129 "Denying socks connection from untrusted address %s.",
1130 fmt_addr(&addr));
1131 tor_close_socket(news);
1132 return 0;
1135 if (new_type == CONN_TYPE_DIR) {
1136 /* check dirpolicy to see if we should accept it */
1137 if (dir_policy_permits_address(&addr) == 0) {
1138 log_notice(LD_DIRSERV,"Denying dir connection from address %s.",
1139 fmt_addr(&addr));
1140 tor_close_socket(news);
1141 return 0;
1145 newconn = connection_new(new_type, conn->socket_family);
1146 newconn->s = news;
1148 /* remember the remote address */
1149 tor_addr_copy(&newconn->addr, &addr);
1150 newconn->port = port;
1151 newconn->address = tor_dup_addr(&addr);
1153 } else if (conn->socket_family == AF_UNIX) {
1154 /* For now only control ports can be Unix domain sockets
1155 * and listeners at the same time */
1156 tor_assert(conn->type == CONN_TYPE_CONTROL_LISTENER);
1158 newconn = connection_new(new_type, conn->socket_family);
1159 newconn->s = news;
1161 /* remember the remote address -- do we have anything sane to put here? */
1162 tor_addr_make_unspec(&newconn->addr);
1163 newconn->port = 1;
1164 newconn->address = tor_strdup(conn->address);
1165 } else {
1166 tor_assert(0);
1169 if (connection_add(newconn) < 0) { /* no space, forget it */
1170 connection_free(newconn);
1171 return 0; /* no need to tear down the parent */
1174 if (connection_init_accepted_conn(newconn, conn->type) < 0) {
1175 connection_mark_for_close(newconn);
1176 return 0;
1178 return 0;
1181 /** Initialize states for newly accepted connection <b>conn</b>.
1182 * If conn is an OR, start the TLS handshake.
1183 * If conn is a transparent AP, get its original destination
1184 * and place it in circuit_wait.
1186 static int
1187 connection_init_accepted_conn(connection_t *conn, uint8_t listener_type)
1189 connection_start_reading(conn);
1191 switch (conn->type) {
1192 case CONN_TYPE_OR:
1193 control_event_or_conn_status(TO_OR_CONN(conn), OR_CONN_EVENT_NEW, 0);
1194 return connection_tls_start_handshake(TO_OR_CONN(conn), 1);
1195 case CONN_TYPE_AP:
1196 switch (listener_type) {
1197 case CONN_TYPE_AP_LISTENER:
1198 conn->state = AP_CONN_STATE_SOCKS_WAIT;
1199 break;
1200 case CONN_TYPE_AP_TRANS_LISTENER:
1201 conn->state = AP_CONN_STATE_CIRCUIT_WAIT;
1202 return connection_ap_process_transparent(TO_EDGE_CONN(conn));
1203 case CONN_TYPE_AP_NATD_LISTENER:
1204 conn->state = AP_CONN_STATE_NATD_WAIT;
1205 break;
1207 break;
1208 case CONN_TYPE_DIR:
1209 conn->purpose = DIR_PURPOSE_SERVER;
1210 conn->state = DIR_CONN_STATE_SERVER_COMMAND_WAIT;
1211 break;
1212 case CONN_TYPE_CONTROL:
1213 conn->state = CONTROL_CONN_STATE_NEEDAUTH;
1214 break;
1216 return 0;
1219 /** Take conn, make a nonblocking socket; try to connect to
1220 * addr:port (they arrive in *host order*). If fail, return -1 and if
1221 * applicable put your best guess about errno into *<b>socket_error</b>.
1222 * Else assign s to conn-\>s: if connected return 1, if EAGAIN return 0.
1224 * address is used to make the logs useful.
1226 * On success, add conn to the list of polled connections.
1229 connection_connect(connection_t *conn, const char *address,
1230 const tor_addr_t *addr, uint16_t port, int *socket_error)
1232 int s, inprogress = 0;
1233 char addrbuf[256];
1234 struct sockaddr *dest_addr = (struct sockaddr*) addrbuf;
1235 socklen_t dest_addr_len;
1236 or_options_t *options = get_options();
1237 int protocol_family;
1239 if (get_n_open_sockets() >= get_options()->_ConnLimit-1) {
1240 warn_too_many_conns();
1241 return -1;
1244 if (tor_addr_family(addr) == AF_INET6)
1245 protocol_family = PF_INET6;
1246 else
1247 protocol_family = PF_INET;
1249 s = tor_open_socket(protocol_family,SOCK_STREAM,IPPROTO_TCP);
1250 if (s < 0) {
1251 *socket_error = tor_socket_errno(-1);
1252 log_warn(LD_NET,"Error creating network socket: %s",
1253 tor_socket_strerror(*socket_error));
1254 return -1;
1257 if (options->OutboundBindAddress) {
1258 struct sockaddr_in ext_addr;
1260 memset(&ext_addr, 0, sizeof(ext_addr));
1261 ext_addr.sin_family = AF_INET;
1262 ext_addr.sin_port = 0;
1263 if (!tor_inet_aton(options->OutboundBindAddress, &ext_addr.sin_addr)) {
1264 log_warn(LD_CONFIG,"Outbound bind address '%s' didn't parse. Ignoring.",
1265 options->OutboundBindAddress);
1266 } else {
1267 if (bind(s, (struct sockaddr*)&ext_addr,
1268 (socklen_t)sizeof(ext_addr)) < 0) {
1269 *socket_error = tor_socket_errno(s);
1270 log_warn(LD_NET,"Error binding network socket: %s",
1271 tor_socket_strerror(*socket_error));
1272 tor_close_socket(s);
1273 return -1;
1278 set_socket_nonblocking(s);
1280 if (options->ConstrainedSockets)
1281 set_constrained_socket_buffers(s, (int)options->ConstrainedSockSize);
1283 memset(addrbuf,0,sizeof(addrbuf));
1284 dest_addr = (struct sockaddr*) addrbuf;
1285 dest_addr_len = tor_addr_to_sockaddr(addr, port, dest_addr, sizeof(addrbuf));
1286 tor_assert(dest_addr_len > 0);
1288 log_debug(LD_NET,"Connecting to %s:%u.",escaped_safe_str(address),port);
1290 if (connect(s, dest_addr, dest_addr_len) < 0) {
1291 int e = tor_socket_errno(s);
1292 if (!ERRNO_IS_CONN_EINPROGRESS(e)) {
1293 /* yuck. kill it. */
1294 *socket_error = e;
1295 log_info(LD_NET,
1296 "connect() to %s:%u failed: %s",escaped_safe_str(address),
1297 port, tor_socket_strerror(e));
1298 tor_close_socket(s);
1299 return -1;
1300 } else {
1301 inprogress = 1;
1305 if (!server_mode(options))
1306 client_check_address_changed(s);
1308 /* it succeeded. we're connected. */
1309 log_fn(inprogress?LOG_DEBUG:LOG_INFO, LD_NET,
1310 "Connection to %s:%u %s (sock %d).",escaped_safe_str(address),
1311 port, inprogress?"in progress":"established", s);
1312 conn->s = s;
1313 if (connection_add(conn) < 0) /* no space, forget it */
1314 return -1;
1315 return inprogress ? 0 : 1;
1319 * Launch any configured listener connections of type <b>type</b>. (A
1320 * listener is configured if <b>port_option</b> is non-zero. If any
1321 * ListenAddress configuration options are given in <b>cfg</b>, create a
1322 * connection binding to each one. Otherwise, create a single
1323 * connection binding to the address <b>default_addr</b>.)
1325 * Only launch the listeners of this type that are not already open, and
1326 * only close listeners that are no longer wanted. Existing listeners
1327 * that are still configured are not touched.
1329 * If <b>disable_all_conns</b> is set, then never open new conns, and
1330 * close the existing ones.
1332 * Add all old conns that should be closed to <b>replaced_conns</b>.
1333 * Add all new connections to <b>new_conns</b>.
1335 static int
1336 retry_listeners(int type, config_line_t *cfg,
1337 int port_option, const char *default_addr,
1338 smartlist_t *replaced_conns,
1339 smartlist_t *new_conns,
1340 int disable_all_conns,
1341 int socket_family)
1343 smartlist_t *launch = smartlist_create(), *conns;
1344 int free_launch_elts = 1;
1345 int r;
1346 config_line_t *c;
1347 connection_t *conn;
1348 config_line_t *line;
1350 tor_assert(socket_family == AF_INET || socket_family == AF_UNIX);
1352 if (cfg && port_option) {
1353 for (c = cfg; c; c = c->next) {
1354 smartlist_add(launch, c);
1356 free_launch_elts = 0;
1357 } else if (port_option) {
1358 line = tor_malloc_zero(sizeof(config_line_t));
1359 line->key = tor_strdup("");
1360 line->value = tor_strdup(default_addr);
1361 smartlist_add(launch, line);
1365 SMARTLIST_FOREACH(launch, config_line_t *, l,
1366 log_fn(LOG_NOTICE, "#%s#%s", l->key, l->value));
1369 conns = get_connection_array();
1370 SMARTLIST_FOREACH(conns, connection_t *, conn,
1372 if (conn->type != type ||
1373 conn->socket_family != socket_family ||
1374 conn->marked_for_close)
1375 continue;
1376 /* Okay, so this is a listener. Is it configured? */
1377 line = NULL;
1378 SMARTLIST_FOREACH(launch, config_line_t *, wanted,
1380 char *address=NULL;
1381 uint16_t port;
1382 switch (socket_family) {
1383 case AF_INET:
1384 if (!parse_addr_port(LOG_WARN,
1385 wanted->value, &address, NULL, &port)) {
1386 int addr_matches = !strcasecmp(address, conn->address);
1387 tor_free(address);
1388 if (! port)
1389 port = port_option;
1390 if (port == conn->port && addr_matches) {
1391 line = wanted;
1392 break;
1395 break;
1396 case AF_UNIX:
1397 if (!strcasecmp(wanted->value, conn->address)) {
1398 line = wanted;
1399 break;
1401 break;
1402 default:
1403 tor_assert(0);
1406 if (!line || disable_all_conns) {
1407 /* This one isn't configured. Close it. */
1408 log_notice(LD_NET, "Closing no-longer-configured %s on %s:%d",
1409 conn_type_to_string(type), conn->address, conn->port);
1410 if (replaced_conns) {
1411 smartlist_add(replaced_conns, conn);
1412 } else {
1413 connection_close_immediate(conn);
1414 connection_mark_for_close(conn);
1416 } else {
1417 /* It's configured; we don't need to launch it. */
1418 // log_debug(LD_NET, "Already have %s on %s:%d",
1419 // conn_type_to_string(type), conn->address, conn->port);
1420 smartlist_remove(launch, line);
1421 if (free_launch_elts)
1422 config_free_lines(line);
1426 /* Now open all the listeners that are configured but not opened. */
1427 r = 0;
1428 if (!disable_all_conns) {
1429 SMARTLIST_FOREACH_BEGIN(launch, config_line_t *, cfg_line) {
1430 char *address = NULL;
1431 struct sockaddr *listensockaddr;
1432 socklen_t listensocklen = 0;
1434 switch (socket_family) {
1435 case AF_INET:
1436 listensockaddr = (struct sockaddr *)
1437 create_inet_sockaddr(cfg_line->value,
1438 (uint16_t) port_option,
1439 &address, &listensocklen);
1440 break;
1441 case AF_UNIX:
1442 listensockaddr = (struct sockaddr *)
1443 create_unix_sockaddr(cfg_line->value,
1444 &address, &listensocklen);
1445 break;
1446 default:
1447 tor_assert(0);
1450 if (listensockaddr) {
1451 conn = connection_create_listener(listensockaddr, listensocklen,
1452 type, address);
1453 tor_free(listensockaddr);
1454 tor_free(address);
1455 } else
1456 conn = NULL;
1458 if (!conn) {
1459 r = -1;
1460 } else {
1461 if (new_conns)
1462 smartlist_add(new_conns, conn);
1464 } SMARTLIST_FOREACH_END(cfg_line);
1467 if (free_launch_elts) {
1468 SMARTLIST_FOREACH(launch, config_line_t *, cfg_line,
1469 config_free_lines(cfg_line));
1471 smartlist_free(launch);
1473 return r;
1476 /** Launch listeners for each port you should have open. Only launch
1477 * listeners who are not already open, and only close listeners we no longer
1478 * want.
1480 * Add all old conns that should be closed to <b>replaced_conns</b>.
1481 * Add all new connections to <b>new_conns</b>.
1484 retry_all_listeners(smartlist_t *replaced_conns,
1485 smartlist_t *new_conns)
1487 or_options_t *options = get_options();
1489 if (retry_listeners(CONN_TYPE_OR_LISTENER, options->ORListenAddress,
1490 options->ORPort, "0.0.0.0",
1491 replaced_conns, new_conns, options->ClientOnly,
1492 AF_INET)<0)
1493 return -1;
1494 if (retry_listeners(CONN_TYPE_DIR_LISTENER, options->DirListenAddress,
1495 options->DirPort, "0.0.0.0",
1496 replaced_conns, new_conns, options->ClientOnly,
1497 AF_INET)<0)
1498 return -1;
1499 if (retry_listeners(CONN_TYPE_AP_LISTENER, options->SocksListenAddress,
1500 options->SocksPort, "127.0.0.1",
1501 replaced_conns, new_conns, 0,
1502 AF_INET)<0)
1503 return -1;
1504 if (retry_listeners(CONN_TYPE_AP_TRANS_LISTENER, options->TransListenAddress,
1505 options->TransPort, "127.0.0.1",
1506 replaced_conns, new_conns, 0,
1507 AF_INET)<0)
1508 return -1;
1509 if (retry_listeners(CONN_TYPE_AP_NATD_LISTENER, options->NatdListenAddress,
1510 options->NatdPort, "127.0.0.1",
1511 replaced_conns, new_conns, 0,
1512 AF_INET)<0)
1513 return -1;
1514 if (retry_listeners(CONN_TYPE_AP_DNS_LISTENER, options->DNSListenAddress,
1515 options->DNSPort, "127.0.0.1",
1516 replaced_conns, new_conns, 0,
1517 AF_INET)<0)
1518 return -1;
1519 if (retry_listeners(CONN_TYPE_CONTROL_LISTENER,
1520 options->ControlListenAddress,
1521 options->ControlPort, "127.0.0.1",
1522 replaced_conns, new_conns, 0,
1523 AF_INET)<0)
1524 return -1;
1525 if (retry_listeners(CONN_TYPE_CONTROL_LISTENER,
1526 options->ControlSocket,
1527 options->ControlSocket ? 1 : 0, NULL,
1528 replaced_conns, new_conns, 0,
1529 AF_UNIX)<0)
1530 return -1;
1532 return 0;
1535 /** Return 1 if we should apply rate limiting to <b>conn</b>,
1536 * and 0 otherwise. Right now this just checks if it's an internal
1537 * IP address or an internal connection. */
1538 static int
1539 connection_is_rate_limited(connection_t *conn)
1541 if (conn->linked || /* internal connection */
1542 tor_addr_family(&conn->addr) == AF_UNSPEC || /* no address */
1543 tor_addr_is_internal(&conn->addr, 0)) /* internal address */
1544 return 0;
1545 else
1546 return 1;
1549 extern int global_read_bucket, global_write_bucket;
1550 extern int global_relayed_read_bucket, global_relayed_write_bucket;
1552 /** Did either global write bucket run dry last second? If so,
1553 * we are likely to run dry again this second, so be stingy with the
1554 * tokens we just put in. */
1555 static int write_buckets_empty_last_second = 0;
1557 /** How many seconds of no active local circuits will make the
1558 * connection revert to the "relayed" bandwidth class? */
1559 #define CLIENT_IDLE_TIME_FOR_PRIORITY 30
1561 /** Return 1 if <b>conn</b> should use tokens from the "relayed"
1562 * bandwidth rates, else 0. Currently, only OR conns with bandwidth
1563 * class 1, and directory conns that are serving data out, count.
1565 static int
1566 connection_counts_as_relayed_traffic(connection_t *conn, time_t now)
1568 if (conn->type == CONN_TYPE_OR &&
1569 TO_OR_CONN(conn)->client_used + CLIENT_IDLE_TIME_FOR_PRIORITY < now)
1570 return 1;
1571 if (conn->type == CONN_TYPE_DIR && DIR_CONN_IS_SERVER(conn))
1572 return 1;
1573 return 0;
1576 /** Helper function to decide how many bytes out of <b>global_bucket</b>
1577 * we're willing to use for this transaction. <b>base</b> is the size
1578 * of a cell on the network; <b>priority</b> says whether we should
1579 * write many of them or just a few; and <b>conn_bucket</b> (if
1580 * non-negative) provides an upper limit for our answer. */
1581 static ssize_t
1582 connection_bucket_round_robin(int base, int priority,
1583 ssize_t global_bucket, ssize_t conn_bucket)
1585 ssize_t at_most;
1586 ssize_t num_bytes_high = (priority ? 32 : 16) * base;
1587 ssize_t num_bytes_low = (priority ? 4 : 2) * base;
1589 /* Do a rudimentary round-robin so one circuit can't hog a connection.
1590 * Pick at most 32 cells, at least 4 cells if possible, and if we're in
1591 * the middle pick 1/8 of the available bandwidth. */
1592 at_most = global_bucket / 8;
1593 at_most -= (at_most % base); /* round down */
1594 if (at_most > num_bytes_high) /* 16 KB, or 8 KB for low-priority */
1595 at_most = num_bytes_high;
1596 else if (at_most < num_bytes_low) /* 2 KB, or 1 KB for low-priority */
1597 at_most = num_bytes_low;
1599 if (at_most > global_bucket)
1600 at_most = global_bucket;
1602 if (conn_bucket >= 0 && at_most > conn_bucket)
1603 at_most = conn_bucket;
1605 if (at_most < 0)
1606 return 0;
1607 return at_most;
1610 /** How many bytes at most can we read onto this connection? */
1611 static ssize_t
1612 connection_bucket_read_limit(connection_t *conn, time_t now)
1614 int base = connection_speaks_cells(conn) ?
1615 CELL_NETWORK_SIZE : RELAY_PAYLOAD_SIZE;
1616 int priority = conn->type != CONN_TYPE_DIR;
1617 int conn_bucket = -1;
1618 int global_bucket = global_read_bucket;
1620 if (connection_speaks_cells(conn)) {
1621 or_connection_t *or_conn = TO_OR_CONN(conn);
1622 if (conn->state == OR_CONN_STATE_OPEN)
1623 conn_bucket = or_conn->read_bucket;
1626 if (!connection_is_rate_limited(conn)) {
1627 /* be willing to read on local conns even if our buckets are empty */
1628 return conn_bucket>=0 ? conn_bucket : 1<<14;
1631 if (connection_counts_as_relayed_traffic(conn, now) &&
1632 global_relayed_read_bucket <= global_read_bucket)
1633 global_bucket = global_relayed_read_bucket;
1635 return connection_bucket_round_robin(base, priority,
1636 global_bucket, conn_bucket);
1639 /** How many bytes at most can we write onto this connection? */
1640 ssize_t
1641 connection_bucket_write_limit(connection_t *conn, time_t now)
1643 int base = connection_speaks_cells(conn) ?
1644 CELL_NETWORK_SIZE : RELAY_PAYLOAD_SIZE;
1645 int priority = conn->type != CONN_TYPE_DIR;
1646 int global_bucket = global_write_bucket;
1648 if (!connection_is_rate_limited(conn)) {
1649 /* be willing to write to local conns even if our buckets are empty */
1650 return conn->outbuf_flushlen;
1653 if (connection_counts_as_relayed_traffic(conn, now) &&
1654 global_relayed_write_bucket <= global_write_bucket)
1655 global_bucket = global_relayed_write_bucket;
1657 return connection_bucket_round_robin(base, priority, global_bucket,
1658 conn->outbuf_flushlen);
1661 /** Return 1 if the global write buckets are low enough that we
1662 * shouldn't send <b>attempt</b> bytes of low-priority directory stuff
1663 * out to <b>conn</b>. Else return 0.
1665 * Priority is 1 for v1 requests (directories and running-routers),
1666 * and 2 for v2 requests (statuses and descriptors). But see FFFF in
1667 * directory_handle_command_get() for why we don't use priority 2 yet.
1669 * There are a lot of parameters we could use here:
1670 * - global_relayed_write_bucket. Low is bad.
1671 * - global_write_bucket. Low is bad.
1672 * - bandwidthrate. Low is bad.
1673 * - bandwidthburst. Not a big factor?
1674 * - attempt. High is bad.
1675 * - total bytes queued on outbufs. High is bad. But I'm wary of
1676 * using this, since a few slow-flushing queues will pump up the
1677 * number without meaning what we meant to mean. What we really
1678 * mean is "total directory bytes added to outbufs recently", but
1679 * that's harder to quantify and harder to keep track of.
1682 global_write_bucket_low(connection_t *conn, size_t attempt, int priority)
1684 int smaller_bucket = global_write_bucket < global_relayed_write_bucket ?
1685 global_write_bucket : global_relayed_write_bucket;
1686 if (authdir_mode(get_options()) && priority>1)
1687 return 0; /* there's always room to answer v2 if we're an auth dir */
1689 if (!connection_is_rate_limited(conn))
1690 return 0; /* local conns don't get limited */
1692 if (smaller_bucket < (int)attempt)
1693 return 1; /* not enough space no matter the priority */
1695 if (write_buckets_empty_last_second)
1696 return 1; /* we're already hitting our limits, no more please */
1698 if (priority == 1) { /* old-style v1 query */
1699 /* Could we handle *two* of these requests within the next two seconds? */
1700 or_options_t *options = get_options();
1701 int64_t can_write = (int64_t)smaller_bucket
1702 + 2*(options->RelayBandwidthRate ? options->RelayBandwidthRate :
1703 options->BandwidthRate);
1704 if (can_write < 2*(int64_t)attempt)
1705 return 1;
1706 } else { /* v2 query */
1707 /* no further constraints yet */
1709 return 0;
1712 /** We just read num_read and wrote num_written onto conn.
1713 * Decrement buckets appropriately. */
1714 static void
1715 connection_buckets_decrement(connection_t *conn, time_t now,
1716 size_t num_read, size_t num_written)
1718 if (!connection_is_rate_limited(conn))
1719 return; /* local IPs are free */
1720 if (num_written >= INT_MAX || num_read >= INT_MAX) {
1721 log_err(LD_BUG, "Value out of range. num_read=%lu, num_written=%lu, "
1722 "connection type=%s, state=%s",
1723 (unsigned long)num_read, (unsigned long)num_written,
1724 conn_type_to_string(conn->type),
1725 conn_state_to_string(conn->type, conn->state));
1726 if (num_written >= INT_MAX) num_written = 1;
1727 if (num_read >= INT_MAX) num_read = 1;
1728 tor_fragile_assert();
1731 if (num_read > 0)
1732 rep_hist_note_bytes_read(num_read, now);
1733 if (num_written > 0)
1734 rep_hist_note_bytes_written(num_written, now);
1736 if (connection_counts_as_relayed_traffic(conn, now)) {
1737 global_relayed_read_bucket -= (int)num_read;
1738 global_relayed_write_bucket -= (int)num_written;
1740 global_read_bucket -= (int)num_read;
1741 global_write_bucket -= (int)num_written;
1742 if (connection_speaks_cells(conn) && conn->state == OR_CONN_STATE_OPEN)
1743 TO_OR_CONN(conn)->read_bucket -= (int)num_read;
1746 /** If we have exhausted our global buckets, or the buckets for conn,
1747 * stop reading. */
1748 static void
1749 connection_consider_empty_read_buckets(connection_t *conn)
1751 const char *reason;
1753 if (global_read_bucket <= 0) {
1754 reason = "global read bucket exhausted. Pausing.";
1755 } else if (connection_counts_as_relayed_traffic(conn, approx_time()) &&
1756 global_relayed_read_bucket <= 0) {
1757 reason = "global relayed read bucket exhausted. Pausing.";
1758 } else if (connection_speaks_cells(conn) &&
1759 conn->state == OR_CONN_STATE_OPEN &&
1760 TO_OR_CONN(conn)->read_bucket <= 0) {
1761 reason = "connection read bucket exhausted. Pausing.";
1762 } else
1763 return; /* all good, no need to stop it */
1765 LOG_FN_CONN(conn, (LOG_DEBUG, LD_NET, "%s", reason));
1766 conn->read_blocked_on_bw = 1;
1767 connection_stop_reading(conn);
1770 /** If we have exhausted our global buckets, or the buckets for conn,
1771 * stop writing. */
1772 static void
1773 connection_consider_empty_write_buckets(connection_t *conn)
1775 const char *reason;
1777 if (global_write_bucket <= 0) {
1778 reason = "global write bucket exhausted. Pausing.";
1779 } else if (connection_counts_as_relayed_traffic(conn, approx_time()) &&
1780 global_relayed_write_bucket <= 0) {
1781 reason = "global relayed write bucket exhausted. Pausing.";
1782 #if 0
1783 } else if (connection_speaks_cells(conn) &&
1784 conn->state == OR_CONN_STATE_OPEN &&
1785 TO_OR_CONN(conn)->write_bucket <= 0) {
1786 reason = "connection write bucket exhausted. Pausing.";
1787 #endif
1788 } else
1789 return; /* all good, no need to stop it */
1791 LOG_FN_CONN(conn, (LOG_DEBUG, LD_NET, "%s", reason));
1792 conn->write_blocked_on_bw = 1;
1793 connection_stop_writing(conn);
1796 /** Initialize the global read bucket to options-\>BandwidthBurst. */
1797 void
1798 connection_bucket_init(void)
1800 or_options_t *options = get_options();
1801 /* start it at max traffic */
1802 global_read_bucket = (int)options->BandwidthBurst;
1803 global_write_bucket = (int)options->BandwidthBurst;
1804 if (options->RelayBandwidthRate) {
1805 global_relayed_read_bucket = (int)options->RelayBandwidthBurst;
1806 global_relayed_write_bucket = (int)options->RelayBandwidthBurst;
1807 } else {
1808 global_relayed_read_bucket = (int)options->BandwidthBurst;
1809 global_relayed_write_bucket = (int)options->BandwidthBurst;
1813 /** Refill a single <b>bucket</b> called <b>name</b> with bandwidth rate
1814 * <b>rate</b> and bandwidth burst <b>burst</b>, assuming that
1815 * <b>seconds_elapsed</b> seconds have passed since the last call.
1817 static void
1818 connection_bucket_refill_helper(int *bucket, int rate, int burst,
1819 int seconds_elapsed, const char *name)
1821 int starting_bucket = *bucket;
1822 if (starting_bucket < burst && seconds_elapsed) {
1823 if (((burst - starting_bucket)/seconds_elapsed) < rate) {
1824 *bucket = burst; /* We would overflow the bucket; just set it to
1825 * the maximum. */
1826 } else {
1827 int incr = rate*seconds_elapsed;
1828 *bucket += incr;
1829 if (*bucket > burst || *bucket < starting_bucket) {
1830 /* If we overflow the burst, or underflow our starting bucket,
1831 * cap the bucket value to burst. */
1832 /* XXXX this might be redundant now, but it doesn't show up
1833 * in profiles. Remove it after analysis. */
1834 *bucket = burst;
1837 log(LOG_DEBUG, LD_NET,"%s now %d.", name, *bucket);
1841 /** A second has rolled over; increment buckets appropriately. */
1842 void
1843 connection_bucket_refill(int seconds_elapsed, time_t now)
1845 or_options_t *options = get_options();
1846 smartlist_t *conns = get_connection_array();
1847 int relayrate, relayburst;
1849 if (options->RelayBandwidthRate) {
1850 relayrate = (int)options->RelayBandwidthRate;
1851 relayburst = (int)options->RelayBandwidthBurst;
1852 } else {
1853 relayrate = (int)options->BandwidthRate;
1854 relayburst = (int)options->BandwidthBurst;
1857 tor_assert(seconds_elapsed >= 0);
1859 write_buckets_empty_last_second =
1860 global_relayed_write_bucket <= 0 || global_write_bucket <= 0;
1862 /* refill the global buckets */
1863 connection_bucket_refill_helper(&global_read_bucket,
1864 (int)options->BandwidthRate,
1865 (int)options->BandwidthBurst,
1866 seconds_elapsed, "global_read_bucket");
1867 connection_bucket_refill_helper(&global_write_bucket,
1868 (int)options->BandwidthRate,
1869 (int)options->BandwidthBurst,
1870 seconds_elapsed, "global_write_bucket");
1871 connection_bucket_refill_helper(&global_relayed_read_bucket,
1872 relayrate, relayburst, seconds_elapsed,
1873 "global_relayed_read_bucket");
1874 connection_bucket_refill_helper(&global_relayed_write_bucket,
1875 relayrate, relayburst, seconds_elapsed,
1876 "global_relayed_write_bucket");
1878 /* refill the per-connection buckets */
1879 SMARTLIST_FOREACH(conns, connection_t *, conn,
1881 if (connection_speaks_cells(conn)) {
1882 or_connection_t *or_conn = TO_OR_CONN(conn);
1883 if (connection_read_bucket_should_increase(or_conn)) {
1884 connection_bucket_refill_helper(&or_conn->read_bucket,
1885 or_conn->bandwidthrate,
1886 or_conn->bandwidthburst,
1887 seconds_elapsed,
1888 "or_conn->read_bucket");
1889 //log_fn(LOG_DEBUG,"Receiver bucket %d now %d.", i,
1890 // conn->read_bucket);
1894 if (conn->read_blocked_on_bw == 1 /* marked to turn reading back on now */
1895 && global_read_bucket > 0 /* and we're allowed to read */
1896 && (!connection_counts_as_relayed_traffic(conn, now) ||
1897 global_relayed_read_bucket > 0) /* even if we're relayed traffic */
1898 && (!connection_speaks_cells(conn) ||
1899 conn->state != OR_CONN_STATE_OPEN ||
1900 TO_OR_CONN(conn)->read_bucket > 0)) {
1901 /* and either a non-cell conn or a cell conn with non-empty bucket */
1902 LOG_FN_CONN(conn, (LOG_DEBUG,LD_NET,
1903 "waking up conn (fd %d) for read", conn->s));
1904 conn->read_blocked_on_bw = 0;
1905 connection_start_reading(conn);
1908 if (conn->write_blocked_on_bw == 1
1909 && global_write_bucket > 0 /* and we're allowed to write */
1910 && (!connection_counts_as_relayed_traffic(conn, now) ||
1911 global_relayed_write_bucket > 0)) {
1912 /* even if we're relayed traffic */
1913 LOG_FN_CONN(conn, (LOG_DEBUG,LD_NET,
1914 "waking up conn (fd %d) for write", conn->s));
1915 conn->write_blocked_on_bw = 0;
1916 connection_start_writing(conn);
1921 /** Is the receiver bucket for connection <b>conn</b> low enough that we
1922 * should add another pile of tokens to it?
1924 static int
1925 connection_read_bucket_should_increase(or_connection_t *conn)
1927 tor_assert(conn);
1929 if (conn->_base.state != OR_CONN_STATE_OPEN)
1930 return 0; /* only open connections play the rate limiting game */
1931 if (conn->read_bucket >= conn->bandwidthburst)
1932 return 0;
1934 return 1;
1937 /** Read bytes from conn-\>s and process them.
1939 * This function gets called from conn_read() in main.c, either
1940 * when poll() has declared that conn wants to read, or (for OR conns)
1941 * when there are pending TLS bytes.
1943 * It calls connection_read_to_buf() to bring in any new bytes,
1944 * and then calls connection_process_inbuf() to process them.
1946 * Mark the connection and return -1 if you want to close it, else
1947 * return 0.
1950 connection_handle_read(connection_t *conn)
1952 int max_to_read=-1, try_to_read;
1953 size_t before, n_read = 0;
1954 int socket_error = 0;
1956 if (conn->marked_for_close)
1957 return 0; /* do nothing */
1959 conn->timestamp_lastread = approx_time();
1961 switch (conn->type) {
1962 case CONN_TYPE_OR_LISTENER:
1963 return connection_handle_listener_read(conn, CONN_TYPE_OR);
1964 case CONN_TYPE_AP_LISTENER:
1965 case CONN_TYPE_AP_TRANS_LISTENER:
1966 case CONN_TYPE_AP_NATD_LISTENER:
1967 return connection_handle_listener_read(conn, CONN_TYPE_AP);
1968 case CONN_TYPE_DIR_LISTENER:
1969 return connection_handle_listener_read(conn, CONN_TYPE_DIR);
1970 case CONN_TYPE_CONTROL_LISTENER:
1971 return connection_handle_listener_read(conn, CONN_TYPE_CONTROL);
1972 case CONN_TYPE_AP_DNS_LISTENER:
1973 /* This should never happen; eventdns.c handles the reads here. */
1974 tor_fragile_assert();
1975 return 0;
1978 loop_again:
1979 try_to_read = max_to_read;
1980 tor_assert(!conn->marked_for_close);
1982 before = buf_datalen(conn->inbuf);
1983 if (connection_read_to_buf(conn, &max_to_read, &socket_error) < 0) {
1984 /* There's a read error; kill the connection.*/
1985 if (conn->type == CONN_TYPE_OR &&
1986 conn->state == OR_CONN_STATE_CONNECTING) {
1987 connection_or_connect_failed(TO_OR_CONN(conn),
1988 errno_to_orconn_end_reason(socket_error),
1989 tor_socket_strerror(socket_error));
1991 if (CONN_IS_EDGE(conn)) {
1992 edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
1993 connection_edge_end_errno(edge_conn);
1994 if (edge_conn->socks_request) /* broken, don't send a socks reply back */
1995 edge_conn->socks_request->has_finished = 1;
1997 connection_close_immediate(conn); /* Don't flush; connection is dead. */
1998 connection_mark_for_close(conn);
1999 return -1;
2001 n_read += buf_datalen(conn->inbuf) - before;
2002 if (CONN_IS_EDGE(conn) && try_to_read != max_to_read) {
2003 /* instruct it not to try to package partial cells. */
2004 if (connection_process_inbuf(conn, 0) < 0) {
2005 return -1;
2007 if (!conn->marked_for_close &&
2008 connection_is_reading(conn) &&
2009 !conn->inbuf_reached_eof &&
2010 max_to_read > 0)
2011 goto loop_again; /* try reading again, in case more is here now */
2013 /* one last try, packaging partial cells and all. */
2014 if (!conn->marked_for_close &&
2015 connection_process_inbuf(conn, 1) < 0) {
2016 return -1;
2018 if (conn->linked_conn) {
2019 /* The other side's handle_write will never actually get called, so
2020 * we need to invoke the appropriate callbacks ourself. */
2021 connection_t *linked = conn->linked_conn;
2023 if (n_read) {
2024 /* Probably a no-op, but hey. */
2025 connection_buckets_decrement(linked, approx_time(), 0, n_read);
2027 if (connection_flushed_some(linked) < 0)
2028 connection_mark_for_close(linked);
2029 if (!connection_wants_to_flush(linked))
2030 connection_finished_flushing(linked);
2033 if (!buf_datalen(linked->outbuf) && conn->active_on_link)
2034 connection_stop_reading_from_linked_conn(conn);
2036 /* If we hit the EOF, call connection_reached_eof. */
2037 if (!conn->marked_for_close &&
2038 conn->inbuf_reached_eof &&
2039 connection_reached_eof(conn) < 0) {
2040 return -1;
2042 return 0;
2045 /** Pull in new bytes from conn-\>s or conn-\>linked_conn onto conn-\>inbuf,
2046 * either directly or via TLS. Reduce the token buckets by the number of bytes
2047 * read.
2049 * If *max_to_read is -1, then decide it ourselves, else go with the
2050 * value passed to us. When returning, if it's changed, subtract the
2051 * number of bytes we read from *max_to_read.
2053 * Return -1 if we want to break conn, else return 0.
2055 static int
2056 connection_read_to_buf(connection_t *conn, int *max_to_read, int *socket_error)
2058 int result;
2059 ssize_t at_most = *max_to_read;
2060 size_t slack_in_buf, more_to_read;
2061 size_t n_read = 0, n_written = 0;
2063 if (at_most == -1) { /* we need to initialize it */
2064 /* how many bytes are we allowed to read? */
2065 at_most = connection_bucket_read_limit(conn, approx_time());
2068 slack_in_buf = buf_slack(conn->inbuf);
2069 again:
2070 if ((size_t)at_most > slack_in_buf && slack_in_buf >= 1024) {
2071 more_to_read = at_most - slack_in_buf;
2072 at_most = slack_in_buf;
2073 } else {
2074 more_to_read = 0;
2077 if (connection_speaks_cells(conn) &&
2078 conn->state > OR_CONN_STATE_PROXY_READING) {
2079 int pending;
2080 or_connection_t *or_conn = TO_OR_CONN(conn);
2081 size_t initial_size;
2082 if (conn->state == OR_CONN_STATE_TLS_HANDSHAKING ||
2083 conn->state == OR_CONN_STATE_TLS_CLIENT_RENEGOTIATING) {
2084 /* continue handshaking even if global token bucket is empty */
2085 return connection_tls_continue_handshake(or_conn);
2088 log_debug(LD_NET,
2089 "%d: starting, inbuf_datalen %ld (%d pending in tls object)."
2090 " at_most %ld.",
2091 conn->s,(long)buf_datalen(conn->inbuf),
2092 tor_tls_get_pending_bytes(or_conn->tls), (long)at_most);
2094 initial_size = buf_datalen(conn->inbuf);
2095 /* else open, or closing */
2096 result = read_to_buf_tls(or_conn->tls, at_most, conn->inbuf);
2097 if (TOR_TLS_IS_ERROR(result) || result == TOR_TLS_CLOSE)
2098 or_conn->tls_error = result;
2099 else
2100 or_conn->tls_error = 0;
2102 switch (result) {
2103 case TOR_TLS_CLOSE:
2104 case TOR_TLS_ERROR_IO:
2105 log_debug(LD_NET,"TLS connection closed %son read. Closing. "
2106 "(Nickname %s, address %s)",
2107 result == TOR_TLS_CLOSE ? "cleanly " : "",
2108 or_conn->nickname ? or_conn->nickname : "not set",
2109 conn->address);
2110 return result;
2111 CASE_TOR_TLS_ERROR_ANY_NONIO:
2112 log_debug(LD_NET,"tls error [%s]. breaking (nickname %s, address %s).",
2113 tor_tls_err_to_string(result),
2114 or_conn->nickname ? or_conn->nickname : "not set",
2115 conn->address);
2116 return result;
2117 case TOR_TLS_WANTWRITE:
2118 connection_start_writing(conn);
2119 return 0;
2120 case TOR_TLS_WANTREAD: /* we're already reading */
2121 case TOR_TLS_DONE: /* no data read, so nothing to process */
2122 result = 0;
2123 break; /* so we call bucket_decrement below */
2124 default:
2125 break;
2127 pending = tor_tls_get_pending_bytes(or_conn->tls);
2128 if (pending) {
2129 /* If we have any pending bytes, we read them now. This *can*
2130 * take us over our read allotment, but really we shouldn't be
2131 * believing that SSL bytes are the same as TCP bytes anyway. */
2132 int r2 = read_to_buf_tls(or_conn->tls, pending, conn->inbuf);
2133 if (r2<0) {
2134 log_warn(LD_BUG, "apparently, reading pending bytes can fail.");
2135 return -1;
2138 result = (int)(buf_datalen(conn->inbuf)-initial_size);
2139 tor_tls_get_n_raw_bytes(or_conn->tls, &n_read, &n_written);
2140 log_debug(LD_GENERAL, "After TLS read of %d: %ld read, %ld written",
2141 result, (long)n_read, (long)n_written);
2142 } else if (conn->linked) {
2143 if (conn->linked_conn) {
2144 result = move_buf_to_buf(conn->inbuf, conn->linked_conn->outbuf,
2145 &conn->linked_conn->outbuf_flushlen);
2146 } else {
2147 result = 0;
2149 //log_notice(LD_GENERAL, "Moved %d bytes on an internal link!", result);
2150 /* If the other side has disappeared, or if it's been marked for close and
2151 * we flushed its outbuf, then we should set our inbuf_reached_eof. */
2152 if (!conn->linked_conn ||
2153 (conn->linked_conn->marked_for_close &&
2154 buf_datalen(conn->linked_conn->outbuf) == 0))
2155 conn->inbuf_reached_eof = 1;
2157 n_read = (size_t) result;
2158 } else {
2159 /* !connection_speaks_cells, !conn->linked_conn. */
2160 int reached_eof = 0;
2161 CONN_LOG_PROTECT(conn,
2162 result = read_to_buf(conn->s, at_most, conn->inbuf, &reached_eof,
2163 socket_error));
2164 if (reached_eof)
2165 conn->inbuf_reached_eof = 1;
2167 // log_fn(LOG_DEBUG,"read_to_buf returned %d.",read_result);
2169 if (result < 0)
2170 return -1;
2171 n_read = (size_t) result;
2174 if (n_read > 0) { /* change *max_to_read */
2175 /*XXXX021 check for overflow*/
2176 *max_to_read = (int)(at_most - n_read);
2179 if (conn->type == CONN_TYPE_AP) {
2180 edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
2181 /*XXXX021 check for overflow*/
2182 edge_conn->n_read += (int)n_read;
2185 connection_buckets_decrement(conn, approx_time(), n_read, n_written);
2187 if (more_to_read && result == at_most) {
2188 slack_in_buf = buf_slack(conn->inbuf);
2189 at_most = more_to_read;
2190 goto again;
2193 /* Call even if result is 0, since the global read bucket may
2194 * have reached 0 on a different conn, and this guy needs to
2195 * know to stop reading. */
2196 connection_consider_empty_read_buckets(conn);
2197 if (n_written > 0 && connection_is_writing(conn))
2198 connection_consider_empty_write_buckets(conn);
2200 return 0;
2203 /** A pass-through to fetch_from_buf. */
2205 connection_fetch_from_buf(char *string, size_t len, connection_t *conn)
2207 return fetch_from_buf(string, len, conn->inbuf);
2210 /** Return conn-\>outbuf_flushlen: how many bytes conn wants to flush
2211 * from its outbuf. */
2213 connection_wants_to_flush(connection_t *conn)
2215 return conn->outbuf_flushlen > 0;
2218 /** Are there too many bytes on edge connection <b>conn</b>'s outbuf to
2219 * send back a relay-level sendme yet? Return 1 if so, 0 if not. Used by
2220 * connection_edge_consider_sending_sendme().
2223 connection_outbuf_too_full(connection_t *conn)
2225 return (conn->outbuf_flushlen > 10*CELL_PAYLOAD_SIZE);
2228 /** Try to flush more bytes onto conn-\>s.
2230 * This function gets called either from conn_write() in main.c
2231 * when poll() has declared that conn wants to write, or below
2232 * from connection_write_to_buf() when an entire TLS record is ready.
2234 * Update conn-\>timestamp_lastwritten to now, and call flush_buf
2235 * or flush_buf_tls appropriately. If it succeeds and there are no more
2236 * more bytes on conn->outbuf, then call connection_finished_flushing
2237 * on it too.
2239 * If <b>force</b>, then write as many bytes as possible, ignoring bandwidth
2240 * limits. (Used for flushing messages to controller connections on fatal
2241 * errors.)
2243 * Mark the connection and return -1 if you want to close it, else
2244 * return 0.
2247 connection_handle_write(connection_t *conn, int force)
2249 int e;
2250 socklen_t len=(socklen_t)sizeof(e);
2251 int result;
2252 ssize_t max_to_write;
2253 time_t now = approx_time();
2254 size_t n_read = 0, n_written = 0;
2256 tor_assert(!connection_is_listener(conn));
2258 if (conn->marked_for_close || conn->s < 0)
2259 return 0; /* do nothing */
2261 if (conn->in_flushed_some) {
2262 log_warn(LD_BUG, "called recursively from inside conn->in_flushed_some()");
2263 return 0;
2266 conn->timestamp_lastwritten = now;
2268 /* Sometimes, "writable" means "connected". */
2269 if (connection_state_is_connecting(conn)) {
2270 if (getsockopt(conn->s, SOL_SOCKET, SO_ERROR, (void*)&e, &len) < 0) {
2271 log_warn(LD_BUG,
2272 "getsockopt() syscall failed?! Please report to tor-ops.");
2273 if (CONN_IS_EDGE(conn))
2274 connection_edge_end_errno(TO_EDGE_CONN(conn));
2275 connection_mark_for_close(conn);
2276 return -1;
2278 if (e) {
2279 /* some sort of error, but maybe just inprogress still */
2280 if (!ERRNO_IS_CONN_EINPROGRESS(e)) {
2281 log_info(LD_NET,"in-progress connect failed. Removing. (%s)",
2282 tor_socket_strerror(e));
2283 if (CONN_IS_EDGE(conn))
2284 connection_edge_end_errno(TO_EDGE_CONN(conn));
2285 if (conn->type == CONN_TYPE_OR)
2286 connection_or_connect_failed(TO_OR_CONN(conn),
2287 errno_to_orconn_end_reason(e),
2288 tor_socket_strerror(e));
2290 connection_close_immediate(conn);
2291 connection_mark_for_close(conn);
2292 return -1;
2293 } else {
2294 return 0; /* no change, see if next time is better */
2297 /* The connection is successful. */
2298 if (connection_finished_connecting(conn)<0)
2299 return -1;
2302 max_to_write = force ? (ssize_t)conn->outbuf_flushlen
2303 : connection_bucket_write_limit(conn, now);
2305 if (connection_speaks_cells(conn) &&
2306 conn->state > OR_CONN_STATE_PROXY_READING) {
2307 or_connection_t *or_conn = TO_OR_CONN(conn);
2308 if (conn->state == OR_CONN_STATE_TLS_HANDSHAKING ||
2309 conn->state == OR_CONN_STATE_TLS_CLIENT_RENEGOTIATING) {
2310 connection_stop_writing(conn);
2311 if (connection_tls_continue_handshake(or_conn) < 0) {
2312 /* Don't flush; connection is dead. */
2313 connection_close_immediate(conn);
2314 connection_mark_for_close(conn);
2315 return -1;
2317 return 0;
2318 } else if (conn->state == OR_CONN_STATE_TLS_SERVER_RENEGOTIATING) {
2319 return connection_handle_read(conn);
2322 /* else open, or closing */
2323 result = flush_buf_tls(or_conn->tls, conn->outbuf,
2324 max_to_write, &conn->outbuf_flushlen);
2325 switch (result) {
2326 CASE_TOR_TLS_ERROR_ANY:
2327 case TOR_TLS_CLOSE:
2328 log_info(LD_NET,result!=TOR_TLS_CLOSE?
2329 "tls error. breaking.":"TLS connection closed on flush");
2330 /* Don't flush; connection is dead. */
2331 connection_close_immediate(conn);
2332 connection_mark_for_close(conn);
2333 return -1;
2334 case TOR_TLS_WANTWRITE:
2335 log_debug(LD_NET,"wanted write.");
2336 /* we're already writing */
2337 return 0;
2338 case TOR_TLS_WANTREAD:
2339 /* Make sure to avoid a loop if the receive buckets are empty. */
2340 log_debug(LD_NET,"wanted read.");
2341 if (!connection_is_reading(conn)) {
2342 connection_stop_writing(conn);
2343 conn->write_blocked_on_bw = 1;
2344 /* we'll start reading again when the next second arrives,
2345 * and then also start writing again.
2348 /* else no problem, we're already reading */
2349 return 0;
2350 /* case TOR_TLS_DONE:
2351 * for TOR_TLS_DONE, fall through to check if the flushlen
2352 * is empty, so we can stop writing.
2356 tor_tls_get_n_raw_bytes(or_conn->tls, &n_read, &n_written);
2357 log_debug(LD_GENERAL, "After TLS write of %d: %ld read, %ld written",
2358 result, (long)n_read, (long)n_written);
2359 } else {
2360 CONN_LOG_PROTECT(conn,
2361 result = flush_buf(conn->s, conn->outbuf,
2362 max_to_write, &conn->outbuf_flushlen));
2363 if (result < 0) {
2364 if (CONN_IS_EDGE(conn))
2365 connection_edge_end_errno(TO_EDGE_CONN(conn));
2367 connection_close_immediate(conn); /* Don't flush; connection is dead. */
2368 connection_mark_for_close(conn);
2369 return -1;
2371 n_written = (size_t) result;
2374 if (conn->type == CONN_TYPE_AP) {
2375 edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
2376 /*XXXX021 check for overflow.*/
2377 edge_conn->n_written += (int)n_written;
2380 connection_buckets_decrement(conn, approx_time(), n_read, n_written);
2382 if (result > 0) {
2383 /* If we wrote any bytes from our buffer, then call the appropriate
2384 * functions. */
2385 if (connection_flushed_some(conn) < 0)
2386 connection_mark_for_close(conn);
2389 if (!connection_wants_to_flush(conn)) { /* it's done flushing */
2390 if (connection_finished_flushing(conn) < 0) {
2391 /* already marked */
2392 return -1;
2394 return 0;
2397 /* Call even if result is 0, since the global write bucket may
2398 * have reached 0 on a different conn, and this guy needs to
2399 * know to stop writing. */
2400 connection_consider_empty_write_buckets(conn);
2401 if (n_read > 0 && connection_is_reading(conn))
2402 connection_consider_empty_read_buckets(conn);
2404 return 0;
2407 /** OpenSSL TLS record size is 16383; this is close. The goal here is to
2408 * push data out as soon as we know there's enough for a TLS record, so
2409 * during periods of high load we won't read entire megabytes from
2410 * input before pushing any data out. It also has the feature of not
2411 * growing huge outbufs unless something is slow. */
2412 #define MIN_TLS_FLUSHLEN 15872
2414 /** Append <b>len</b> bytes of <b>string</b> onto <b>conn</b>'s
2415 * outbuf, and ask it to start writing.
2417 * If <b>zlib</b> is nonzero, this is a directory connection that should get
2418 * its contents compressed or decompressed as they're written. If zlib is
2419 * negative, this is the last data to be compressed, and the connection's zlib
2420 * state should be flushed.
2422 * If it's an OR conn and an entire TLS record is ready, then try to
2423 * flush the record now. Similarly, if it's a local control connection
2424 * and a 64k chunk is ready, try to flush it all, so we don't end up with
2425 * many megabytes of controller info queued at once.
2427 void
2428 _connection_write_to_buf_impl(const char *string, size_t len,
2429 connection_t *conn, int zlib)
2431 /* XXXX This function really needs to return -1 on failure. */
2432 int r;
2433 size_t old_datalen;
2434 if (!len && !(zlib<0))
2435 return;
2436 /* if it's marked for close, only allow write if we mean to flush it */
2437 if (conn->marked_for_close && !conn->hold_open_until_flushed)
2438 return;
2440 old_datalen = buf_datalen(conn->outbuf);
2441 if (zlib) {
2442 dir_connection_t *dir_conn = TO_DIR_CONN(conn);
2443 int done = zlib < 0;
2444 CONN_LOG_PROTECT(conn, r = write_to_buf_zlib(conn->outbuf,
2445 dir_conn->zlib_state,
2446 string, len, done));
2447 } else {
2448 CONN_LOG_PROTECT(conn, r = write_to_buf(string, len, conn->outbuf));
2450 if (r < 0) {
2451 if (CONN_IS_EDGE(conn)) {
2452 /* if it failed, it means we have our package/delivery windows set
2453 wrong compared to our max outbuf size. close the whole circuit. */
2454 log_warn(LD_NET,
2455 "write_to_buf failed. Closing circuit (fd %d).", conn->s);
2456 circuit_mark_for_close(circuit_get_by_edge_conn(TO_EDGE_CONN(conn)),
2457 END_CIRC_REASON_INTERNAL);
2458 } else {
2459 log_warn(LD_NET,
2460 "write_to_buf failed. Closing connection (fd %d).", conn->s);
2461 connection_mark_for_close(conn);
2463 return;
2466 connection_start_writing(conn);
2467 if (zlib) {
2468 conn->outbuf_flushlen += buf_datalen(conn->outbuf) - old_datalen;
2469 } else {
2470 ssize_t extra = 0;
2471 conn->outbuf_flushlen += len;
2473 /* Should we try flushing the outbuf now? */
2474 if (conn->in_flushed_some) {
2475 /* Don't flush the outbuf when the reason we're writing more stuff is
2476 * _because_ we flushed the outbuf. That's unfair. */
2477 return;
2480 if (conn->type == CONN_TYPE_OR &&
2481 conn->outbuf_flushlen-len < MIN_TLS_FLUSHLEN &&
2482 conn->outbuf_flushlen >= MIN_TLS_FLUSHLEN) {
2483 /* We just pushed outbuf_flushlen to MIN_TLS_FLUSHLEN or above;
2484 * we can send out a full TLS frame now if we like. */
2485 extra = conn->outbuf_flushlen - MIN_TLS_FLUSHLEN;
2486 conn->outbuf_flushlen = MIN_TLS_FLUSHLEN;
2487 } else if (conn->type == CONN_TYPE_CONTROL &&
2488 !connection_is_rate_limited(conn) &&
2489 conn->outbuf_flushlen-len < 1<<16 &&
2490 conn->outbuf_flushlen >= 1<<16) {
2491 /* just try to flush all of it */
2492 } else
2493 return; /* no need to try flushing */
2495 if (connection_handle_write(conn, 0) < 0) {
2496 if (!conn->marked_for_close) {
2497 /* this connection is broken. remove it. */
2498 log_warn(LD_BUG, "unhandled error on write for "
2499 "conn (type %d, fd %d); removing",
2500 conn->type, conn->s);
2501 tor_fragile_assert();
2502 /* do a close-immediate here, so we don't try to flush */
2503 connection_close_immediate(conn);
2505 return;
2507 if (extra) {
2508 conn->outbuf_flushlen += extra;
2509 connection_start_writing(conn);
2514 /** Return a connection with given type, address, port, and purpose;
2515 * or NULL if no such connection exists. */
2516 connection_t *
2517 connection_get_by_type_addr_port_purpose(int type,
2518 const tor_addr_t *addr, uint16_t port,
2519 int purpose)
2521 smartlist_t *conns = get_connection_array();
2522 SMARTLIST_FOREACH(conns, connection_t *, conn,
2524 if (conn->type == type &&
2525 tor_addr_eq(&conn->addr, addr) &&
2526 conn->port == port &&
2527 conn->purpose == purpose &&
2528 !conn->marked_for_close)
2529 return conn;
2531 return NULL;
2534 /** Return the stream with id <b>id</b> if it is not already marked for
2535 * close.
2537 connection_t *
2538 connection_get_by_global_id(uint64_t id)
2540 smartlist_t *conns = get_connection_array();
2541 SMARTLIST_FOREACH(conns, connection_t *, conn,
2543 if (conn->global_identifier == id)
2544 return conn;
2546 return NULL;
2549 /** Return a connection of type <b>type</b> that is not marked for close.
2551 connection_t *
2552 connection_get_by_type(int type)
2554 smartlist_t *conns = get_connection_array();
2555 SMARTLIST_FOREACH(conns, connection_t *, conn,
2557 if (conn->type == type && !conn->marked_for_close)
2558 return conn;
2560 return NULL;
2563 /** Return a connection of type <b>type</b> that is in state <b>state</b>,
2564 * and that is not marked for close.
2566 connection_t *
2567 connection_get_by_type_state(int type, int state)
2569 smartlist_t *conns = get_connection_array();
2570 SMARTLIST_FOREACH(conns, connection_t *, conn,
2572 if (conn->type == type && conn->state == state && !conn->marked_for_close)
2573 return conn;
2575 return NULL;
2578 /** Return a connection of type <b>type</b> that has rendquery equal
2579 * to <b>rendquery</b>, and that is not marked for close. If state
2580 * is non-zero, conn must be of that state too. If rendversion is
2581 * nonnegative, conn must be fetching that rendversion, too.
2583 connection_t *
2584 connection_get_by_type_state_rendquery(int type, int state,
2585 const char *rendquery,
2586 int rendversion)
2588 smartlist_t *conns = get_connection_array();
2590 tor_assert(type == CONN_TYPE_DIR ||
2591 type == CONN_TYPE_AP || type == CONN_TYPE_EXIT);
2592 tor_assert(rendquery);
2594 SMARTLIST_FOREACH(conns, connection_t *, conn,
2596 if (conn->type == type &&
2597 !conn->marked_for_close &&
2598 (!state || state == conn->state)) {
2599 if (type == CONN_TYPE_DIR &&
2600 TO_DIR_CONN(conn)->rend_data &&
2601 (rendversion < 0 ||
2602 rendversion == TO_DIR_CONN(conn)->rend_data->rend_desc_version) &&
2603 !rend_cmp_service_ids(rendquery,
2604 TO_DIR_CONN(conn)->rend_data->onion_address))
2605 return conn;
2606 else if (CONN_IS_EDGE(conn) &&
2607 TO_EDGE_CONN(conn)->rend_data &&
2608 !rend_cmp_service_ids(rendquery,
2609 TO_EDGE_CONN(conn)->rend_data->onion_address))
2610 return conn;
2613 return NULL;
2616 /** Return an open, non-marked connection of a given type and purpose, or NULL
2617 * if no such connection exists. */
2618 connection_t *
2619 connection_get_by_type_purpose(int type, int purpose)
2621 smartlist_t *conns = get_connection_array();
2622 SMARTLIST_FOREACH(conns, connection_t *, conn,
2624 if (conn->type == type &&
2625 !conn->marked_for_close &&
2626 (purpose == conn->purpose))
2627 return conn;
2629 return NULL;
2632 /** Return 1 if <b>conn</b> is a listener conn, else return 0. */
2634 connection_is_listener(connection_t *conn)
2636 if (conn->type == CONN_TYPE_OR_LISTENER ||
2637 conn->type == CONN_TYPE_AP_LISTENER ||
2638 conn->type == CONN_TYPE_AP_TRANS_LISTENER ||
2639 conn->type == CONN_TYPE_AP_DNS_LISTENER ||
2640 conn->type == CONN_TYPE_AP_NATD_LISTENER ||
2641 conn->type == CONN_TYPE_DIR_LISTENER ||
2642 conn->type == CONN_TYPE_CONTROL_LISTENER)
2643 return 1;
2644 return 0;
2647 /** Return 1 if <b>conn</b> is in state "open" and is not marked
2648 * for close, else return 0.
2651 connection_state_is_open(connection_t *conn)
2653 tor_assert(conn);
2655 if (conn->marked_for_close)
2656 return 0;
2658 if ((conn->type == CONN_TYPE_OR && conn->state == OR_CONN_STATE_OPEN) ||
2659 (conn->type == CONN_TYPE_AP && conn->state == AP_CONN_STATE_OPEN) ||
2660 (conn->type == CONN_TYPE_EXIT && conn->state == EXIT_CONN_STATE_OPEN) ||
2661 (conn->type == CONN_TYPE_CONTROL &&
2662 conn->state == CONTROL_CONN_STATE_OPEN))
2663 return 1;
2665 return 0;
2668 /** Return 1 if conn is in 'connecting' state, else return 0. */
2670 connection_state_is_connecting(connection_t *conn)
2672 tor_assert(conn);
2674 if (conn->marked_for_close)
2675 return 0;
2676 switch (conn->type)
2678 case CONN_TYPE_OR:
2679 return conn->state == OR_CONN_STATE_CONNECTING;
2680 case CONN_TYPE_EXIT:
2681 return conn->state == EXIT_CONN_STATE_CONNECTING;
2682 case CONN_TYPE_DIR:
2683 return conn->state == DIR_CONN_STATE_CONNECTING;
2686 return 0;
2689 /** Allocates a base64'ed authenticator for use in http or https
2690 * auth, based on the input string <b>authenticator</b>. Returns it
2691 * if success, else returns NULL. */
2692 char *
2693 alloc_http_authenticator(const char *authenticator)
2695 /* an authenticator in Basic authentication
2696 * is just the string "username:password" */
2697 const size_t authenticator_length = strlen(authenticator);
2698 /* The base64_encode function needs a minimum buffer length
2699 * of 66 bytes. */
2700 const size_t base64_authenticator_length = (authenticator_length/48+1)*66;
2701 char *base64_authenticator = tor_malloc(base64_authenticator_length);
2702 if (base64_encode(base64_authenticator, base64_authenticator_length,
2703 authenticator, authenticator_length) < 0) {
2704 tor_free(base64_authenticator); /* free and set to null */
2705 } else {
2706 /* remove extra \n at end of encoding */
2707 base64_authenticator[strlen(base64_authenticator) - 1] = 0;
2709 return base64_authenticator;
2712 /** Given a socket handle, check whether the local address (sockname) of the
2713 * socket is one that we've connected from before. If so, double-check
2714 * whether our address has changed and we need to generate keys. If we do,
2715 * call init_keys().
2717 static void
2718 client_check_address_changed(int sock)
2720 uint32_t iface_ip, ip_out;
2721 struct sockaddr_in out_addr;
2722 socklen_t out_addr_len = (socklen_t) sizeof(out_addr);
2723 uint32_t *ip;
2725 if (!last_interface_ip)
2726 get_interface_address(LOG_INFO, &last_interface_ip);
2727 if (!outgoing_addrs)
2728 outgoing_addrs = smartlist_create();
2730 if (getsockname(sock, (struct sockaddr*)&out_addr, &out_addr_len)<0) {
2731 int e = tor_socket_errno(sock);
2732 log_warn(LD_NET, "getsockname() to check for address change failed: %s",
2733 tor_socket_strerror(e));
2734 return;
2737 /* Okay. If we've used this address previously, we're okay. */
2738 ip_out = ntohl(out_addr.sin_addr.s_addr);
2739 SMARTLIST_FOREACH(outgoing_addrs, uint32_t*, ip_ptr,
2740 if (*ip_ptr == ip_out) return;
2743 /* Uh-oh. We haven't connected from this address before. Has the interface
2744 * address changed? */
2745 if (get_interface_address(LOG_INFO, &iface_ip)<0)
2746 return;
2747 ip = tor_malloc(sizeof(uint32_t));
2748 *ip = ip_out;
2750 if (iface_ip == last_interface_ip) {
2751 /* Nope, it hasn't changed. Add this address to the list. */
2752 smartlist_add(outgoing_addrs, ip);
2753 } else {
2754 /* The interface changed. We're a client, so we need to regenerate our
2755 * keys. First, reset the state. */
2756 log(LOG_NOTICE, LD_NET, "Our IP address has changed. Rotating keys...");
2757 last_interface_ip = iface_ip;
2758 SMARTLIST_FOREACH(outgoing_addrs, void*, ip_ptr, tor_free(ip_ptr));
2759 smartlist_clear(outgoing_addrs);
2760 smartlist_add(outgoing_addrs, ip);
2761 /* Okay, now change our keys. */
2762 ip_address_changed(1);
2766 /** Some systems have limited system buffers for recv and xmit on
2767 * sockets allocated in a virtual server or similar environment. For a Tor
2768 * server this can produce the "Error creating network socket: No buffer
2769 * space available" error once all available TCP buffer space is consumed.
2770 * This method will attempt to constrain the buffers allocated for the socket
2771 * to the desired size to stay below system TCP buffer limits.
2773 static void
2774 set_constrained_socket_buffers(int sock, int size)
2776 void *sz = (void*)&size;
2777 socklen_t sz_sz = (socklen_t) sizeof(size);
2778 if (setsockopt(sock, SOL_SOCKET, SO_SNDBUF, sz, sz_sz) < 0) {
2779 int e = tor_socket_errno(sock);
2780 log_warn(LD_NET, "setsockopt() to constrain send "
2781 "buffer to %d bytes failed: %s", size, tor_socket_strerror(e));
2783 if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF, sz, sz_sz) < 0) {
2784 int e = tor_socket_errno(sock);
2785 log_warn(LD_NET, "setsockopt() to constrain recv "
2786 "buffer to %d bytes failed: %s", size, tor_socket_strerror(e));
2790 /** Process new bytes that have arrived on conn-\>inbuf.
2792 * This function just passes conn to the connection-specific
2793 * connection_*_process_inbuf() function. It also passes in
2794 * package_partial if wanted.
2796 static int
2797 connection_process_inbuf(connection_t *conn, int package_partial)
2799 tor_assert(conn);
2801 switch (conn->type) {
2802 case CONN_TYPE_OR:
2803 return connection_or_process_inbuf(TO_OR_CONN(conn));
2804 case CONN_TYPE_EXIT:
2805 case CONN_TYPE_AP:
2806 return connection_edge_process_inbuf(TO_EDGE_CONN(conn),
2807 package_partial);
2808 case CONN_TYPE_DIR:
2809 return connection_dir_process_inbuf(TO_DIR_CONN(conn));
2810 case CONN_TYPE_CPUWORKER:
2811 return connection_cpu_process_inbuf(conn);
2812 case CONN_TYPE_CONTROL:
2813 return connection_control_process_inbuf(TO_CONTROL_CONN(conn));
2814 default:
2815 log_err(LD_BUG,"got unexpected conn type %d.", conn->type);
2816 tor_fragile_assert();
2817 return -1;
2821 /** Called whenever we've written data on a connection. */
2822 static int
2823 connection_flushed_some(connection_t *conn)
2825 int r = 0;
2826 tor_assert(!conn->in_flushed_some);
2827 conn->in_flushed_some = 1;
2828 if (conn->type == CONN_TYPE_DIR &&
2829 conn->state == DIR_CONN_STATE_SERVER_WRITING) {
2830 r = connection_dirserv_flushed_some(TO_DIR_CONN(conn));
2831 } else if (conn->type == CONN_TYPE_OR) {
2832 r = connection_or_flushed_some(TO_OR_CONN(conn));
2834 conn->in_flushed_some = 0;
2835 return r;
2838 /** We just finished flushing bytes from conn-\>outbuf, and there
2839 * are no more bytes remaining.
2841 * This function just passes conn to the connection-specific
2842 * connection_*_finished_flushing() function.
2844 static int
2845 connection_finished_flushing(connection_t *conn)
2847 tor_assert(conn);
2849 /* If the connection is closed, don't try to do anything more here. */
2850 if (CONN_IS_CLOSED(conn))
2851 return 0;
2853 // log_fn(LOG_DEBUG,"entered. Socket %u.", conn->s);
2855 switch (conn->type) {
2856 case CONN_TYPE_OR:
2857 return connection_or_finished_flushing(TO_OR_CONN(conn));
2858 case CONN_TYPE_AP:
2859 case CONN_TYPE_EXIT:
2860 return connection_edge_finished_flushing(TO_EDGE_CONN(conn));
2861 case CONN_TYPE_DIR:
2862 return connection_dir_finished_flushing(TO_DIR_CONN(conn));
2863 case CONN_TYPE_CPUWORKER:
2864 return connection_cpu_finished_flushing(conn);
2865 case CONN_TYPE_CONTROL:
2866 return connection_control_finished_flushing(TO_CONTROL_CONN(conn));
2867 default:
2868 log_err(LD_BUG,"got unexpected conn type %d.", conn->type);
2869 tor_fragile_assert();
2870 return -1;
2874 /** Called when our attempt to connect() to another server has just
2875 * succeeded.
2877 * This function just passes conn to the connection-specific
2878 * connection_*_finished_connecting() function.
2880 static int
2881 connection_finished_connecting(connection_t *conn)
2883 tor_assert(conn);
2884 switch (conn->type)
2886 case CONN_TYPE_OR:
2887 return connection_or_finished_connecting(TO_OR_CONN(conn));
2888 case CONN_TYPE_EXIT:
2889 return connection_edge_finished_connecting(TO_EDGE_CONN(conn));
2890 case CONN_TYPE_DIR:
2891 return connection_dir_finished_connecting(TO_DIR_CONN(conn));
2892 default:
2893 log_err(LD_BUG,"got unexpected conn type %d.", conn->type);
2894 tor_fragile_assert();
2895 return -1;
2899 /** Callback: invoked when a connection reaches an EOF event. */
2900 static int
2901 connection_reached_eof(connection_t *conn)
2903 switch (conn->type) {
2904 case CONN_TYPE_OR:
2905 return connection_or_reached_eof(TO_OR_CONN(conn));
2906 case CONN_TYPE_AP:
2907 case CONN_TYPE_EXIT:
2908 return connection_edge_reached_eof(TO_EDGE_CONN(conn));
2909 case CONN_TYPE_DIR:
2910 return connection_dir_reached_eof(TO_DIR_CONN(conn));
2911 case CONN_TYPE_CPUWORKER:
2912 return connection_cpu_reached_eof(conn);
2913 case CONN_TYPE_CONTROL:
2914 return connection_control_reached_eof(TO_CONTROL_CONN(conn));
2915 default:
2916 log_err(LD_BUG,"got unexpected conn type %d.", conn->type);
2917 tor_fragile_assert();
2918 return -1;
2922 /** Log how many bytes are used by buffers of different kinds and sizes. */
2923 void
2924 connection_dump_buffer_mem_stats(int severity)
2926 uint64_t used_by_type[_CONN_TYPE_MAX+1];
2927 uint64_t alloc_by_type[_CONN_TYPE_MAX+1];
2928 int n_conns_by_type[_CONN_TYPE_MAX+1];
2929 uint64_t total_alloc = 0;
2930 uint64_t total_used = 0;
2931 int i;
2932 smartlist_t *conns = get_connection_array();
2934 memset(used_by_type, 0, sizeof(used_by_type));
2935 memset(alloc_by_type, 0, sizeof(alloc_by_type));
2936 memset(n_conns_by_type, 0, sizeof(n_conns_by_type));
2938 SMARTLIST_FOREACH(conns, connection_t *, c,
2940 int tp = c->type;
2941 ++n_conns_by_type[tp];
2942 if (c->inbuf) {
2943 used_by_type[tp] += buf_datalen(c->inbuf);
2944 alloc_by_type[tp] += buf_allocation(c->inbuf);
2946 if (c->outbuf) {
2947 used_by_type[tp] += buf_datalen(c->outbuf);
2948 alloc_by_type[tp] += buf_allocation(c->outbuf);
2951 for (i=0; i <= _CONN_TYPE_MAX; ++i) {
2952 total_used += used_by_type[i];
2953 total_alloc += alloc_by_type[i];
2956 log(severity, LD_GENERAL,
2957 "In buffers for %d connections: "U64_FORMAT" used/"U64_FORMAT" allocated",
2958 smartlist_len(conns),
2959 U64_PRINTF_ARG(total_used), U64_PRINTF_ARG(total_alloc));
2960 for (i=_CONN_TYPE_MIN; i <= _CONN_TYPE_MAX; ++i) {
2961 if (!n_conns_by_type[i])
2962 continue;
2963 log(severity, LD_GENERAL,
2964 " For %d %s connections: "U64_FORMAT" used/"U64_FORMAT" allocated",
2965 n_conns_by_type[i], conn_type_to_string(i),
2966 U64_PRINTF_ARG(used_by_type[i]), U64_PRINTF_ARG(alloc_by_type[i]));
2970 /** Verify that connection <b>conn</b> has all of its invariants
2971 * correct. Trigger an assert if anything is invalid.
2973 void
2974 assert_connection_ok(connection_t *conn, time_t now)
2976 (void) now; /* XXXX unused. */
2977 tor_assert(conn);
2978 tor_assert(conn->type >= _CONN_TYPE_MIN);
2979 tor_assert(conn->type <= _CONN_TYPE_MAX);
2980 switch (conn->type) {
2981 case CONN_TYPE_OR:
2982 tor_assert(conn->magic == OR_CONNECTION_MAGIC);
2983 break;
2984 case CONN_TYPE_AP:
2985 case CONN_TYPE_EXIT:
2986 tor_assert(conn->magic == EDGE_CONNECTION_MAGIC);
2987 break;
2988 case CONN_TYPE_DIR:
2989 tor_assert(conn->magic == DIR_CONNECTION_MAGIC);
2990 break;
2991 case CONN_TYPE_CONTROL:
2992 tor_assert(conn->magic == CONTROL_CONNECTION_MAGIC);
2993 break;
2994 default:
2995 tor_assert(conn->magic == BASE_CONNECTION_MAGIC);
2996 break;
2999 if (conn->linked_conn) {
3000 tor_assert(conn->linked_conn->linked_conn == conn);
3001 tor_assert(conn->linked);
3003 if (conn->linked)
3004 tor_assert(conn->s < 0);
3006 if (conn->outbuf_flushlen > 0) {
3007 tor_assert(connection_is_writing(conn) || conn->write_blocked_on_bw ||
3008 (CONN_IS_EDGE(conn) && TO_EDGE_CONN(conn)->edge_blocked_on_circ));
3011 if (conn->hold_open_until_flushed)
3012 tor_assert(conn->marked_for_close);
3014 /* XXXX check: read_blocked_on_bw, write_blocked_on_bw, s, conn_array_index,
3015 * marked_for_close. */
3017 /* buffers */
3018 if (!connection_is_listener(conn)) {
3019 assert_buf_ok(conn->inbuf);
3020 assert_buf_ok(conn->outbuf);
3023 if (conn->type == CONN_TYPE_OR) {
3024 or_connection_t *or_conn = TO_OR_CONN(conn);
3025 if (conn->state == OR_CONN_STATE_OPEN) {
3026 /* tor_assert(conn->bandwidth > 0); */
3027 /* the above isn't necessarily true: if we just did a TLS
3028 * handshake but we didn't recognize the other peer, or it
3029 * gave a bad cert/etc, then we won't have assigned bandwidth,
3030 * yet it will be open. -RD
3032 // tor_assert(conn->read_bucket >= 0);
3034 // tor_assert(conn->addr && conn->port);
3035 tor_assert(conn->address);
3036 if (conn->state > OR_CONN_STATE_PROXY_READING)
3037 tor_assert(or_conn->tls);
3040 if (CONN_IS_EDGE(conn)) {
3041 edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
3042 if (edge_conn->chosen_exit_optional || edge_conn->chosen_exit_retries) {
3043 tor_assert(conn->type == CONN_TYPE_AP);
3044 tor_assert(edge_conn->chosen_exit_name);
3047 /* XXX unchecked: package window, deliver window. */
3048 if (conn->type == CONN_TYPE_AP) {
3050 tor_assert(edge_conn->socks_request);
3051 if (conn->state == AP_CONN_STATE_OPEN) {
3052 tor_assert(edge_conn->socks_request->has_finished);
3053 if (!conn->marked_for_close) {
3054 tor_assert(edge_conn->cpath_layer);
3055 assert_cpath_layer_ok(edge_conn->cpath_layer);
3059 if (conn->type == CONN_TYPE_EXIT) {
3060 tor_assert(conn->purpose == EXIT_PURPOSE_CONNECT ||
3061 conn->purpose == EXIT_PURPOSE_RESOLVE);
3063 } else if (conn->type == CONN_TYPE_DIR) {
3064 } else {
3065 /* Purpose is only used for dir and exit types currently */
3066 tor_assert(!conn->purpose);
3069 switch (conn->type)
3071 case CONN_TYPE_OR_LISTENER:
3072 case CONN_TYPE_AP_LISTENER:
3073 case CONN_TYPE_AP_TRANS_LISTENER:
3074 case CONN_TYPE_AP_NATD_LISTENER:
3075 case CONN_TYPE_DIR_LISTENER:
3076 case CONN_TYPE_CONTROL_LISTENER:
3077 case CONN_TYPE_AP_DNS_LISTENER:
3078 tor_assert(conn->state == LISTENER_STATE_READY);
3079 break;
3080 case CONN_TYPE_OR:
3081 tor_assert(conn->state >= _OR_CONN_STATE_MIN);
3082 tor_assert(conn->state <= _OR_CONN_STATE_MAX);
3083 tor_assert(TO_OR_CONN(conn)->n_circuits >= 0);
3084 break;
3085 case CONN_TYPE_EXIT:
3086 tor_assert(conn->state >= _EXIT_CONN_STATE_MIN);
3087 tor_assert(conn->state <= _EXIT_CONN_STATE_MAX);
3088 tor_assert(conn->purpose >= _EXIT_PURPOSE_MIN);
3089 tor_assert(conn->purpose <= _EXIT_PURPOSE_MAX);
3090 break;
3091 case CONN_TYPE_AP:
3092 tor_assert(conn->state >= _AP_CONN_STATE_MIN);
3093 tor_assert(conn->state <= _AP_CONN_STATE_MAX);
3094 tor_assert(TO_EDGE_CONN(conn)->socks_request);
3095 break;
3096 case CONN_TYPE_DIR:
3097 tor_assert(conn->state >= _DIR_CONN_STATE_MIN);
3098 tor_assert(conn->state <= _DIR_CONN_STATE_MAX);
3099 tor_assert(conn->purpose >= _DIR_PURPOSE_MIN);
3100 tor_assert(conn->purpose <= _DIR_PURPOSE_MAX);
3101 break;
3102 case CONN_TYPE_CPUWORKER:
3103 tor_assert(conn->state >= _CPUWORKER_STATE_MIN);
3104 tor_assert(conn->state <= _CPUWORKER_STATE_MAX);
3105 break;
3106 case CONN_TYPE_CONTROL:
3107 tor_assert(conn->state >= _CONTROL_CONN_STATE_MIN);
3108 tor_assert(conn->state <= _CONTROL_CONN_STATE_MAX);
3109 break;
3110 default:
3111 tor_assert(0);