Downgrade some xxx021s, comment more on others, etc
[tor/rransom.git] / src / or / connection.c
blob2587e28f969141cb38977da29b48fb396f3225e0
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-2008, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
6 /* $Id$ */
7 const char connection_c_id[] =
8 "$Id$";
10 /**
11 * \file connection.c
12 * \brief General high-level functions to handle reading and writing
13 * on connections.
14 **/
16 #include "or.h"
18 static connection_t *connection_create_listener(
19 struct sockaddr *listensockaddr,
20 socklen_t listensocklen, int type,
21 char* address);
22 static void connection_init(time_t now, connection_t *conn, int type,
23 int socket_family);
24 static int connection_init_accepted_conn(connection_t *conn,
25 uint8_t listener_type);
26 static int connection_handle_listener_read(connection_t *conn, int new_type);
27 static int connection_read_bucket_should_increase(or_connection_t *conn);
28 static int connection_finished_flushing(connection_t *conn);
29 static int connection_flushed_some(connection_t *conn);
30 static int connection_finished_connecting(connection_t *conn);
31 static int connection_reached_eof(connection_t *conn);
32 static int connection_read_to_buf(connection_t *conn, int *max_to_read,
33 int *socket_error);
34 static int connection_process_inbuf(connection_t *conn, int package_partial);
35 static void client_check_address_changed(int sock);
36 static void set_constrained_socket_buffers(int sock, int size);
38 /** The last IPv4 address that our network interface seemed to have been
39 * binding to, in host order. We use this to detect when our IP changes. */
40 static uint32_t last_interface_ip = 0;
41 /** A list of uint32_ts for addresses we've used in outgoing connections.
42 * Used to detect IP address changes. */
43 static smartlist_t *outgoing_addrs = NULL;
45 /**************************************************************/
47 /**
48 * Return the human-readable name for the connection type <b>type</b>
50 const char *
51 conn_type_to_string(int type)
53 static char buf[64];
54 switch (type) {
55 case CONN_TYPE_OR_LISTENER: return "OR listener";
56 case CONN_TYPE_OR: return "OR";
57 case CONN_TYPE_EXIT: return "Exit";
58 case CONN_TYPE_AP_LISTENER: return "Socks listener";
59 case CONN_TYPE_AP_TRANS_LISTENER:
60 return "Transparent pf/netfilter listener";
61 case CONN_TYPE_AP_NATD_LISTENER: return "Transparent natd listener";
62 case CONN_TYPE_AP_DNS_LISTENER: return "DNS listener";
63 case CONN_TYPE_AP: return "Socks";
64 case CONN_TYPE_DIR_LISTENER: return "Directory listener";
65 case CONN_TYPE_DIR: return "Directory";
66 case CONN_TYPE_CPUWORKER: return "CPU worker";
67 case CONN_TYPE_CONTROL_LISTENER: return "Control listener";
68 case CONN_TYPE_CONTROL: return "Control";
69 default:
70 log_warn(LD_BUG, "unknown connection type %d", type);
71 tor_snprintf(buf, sizeof(buf), "unknown [%d]", type);
72 return buf;
76 /**
77 * Return the human-readable name for the connection state <b>state</b>
78 * for the connection type <b>type</b>
80 const char *
81 conn_state_to_string(int type, int state)
83 static char buf[96];
84 switch (type) {
85 case CONN_TYPE_OR_LISTENER:
86 case CONN_TYPE_AP_LISTENER:
87 case CONN_TYPE_AP_TRANS_LISTENER:
88 case CONN_TYPE_AP_NATD_LISTENER:
89 case CONN_TYPE_AP_DNS_LISTENER:
90 case CONN_TYPE_DIR_LISTENER:
91 case CONN_TYPE_CONTROL_LISTENER:
92 if (state == LISTENER_STATE_READY)
93 return "ready";
94 break;
95 case CONN_TYPE_OR:
96 switch (state) {
97 case OR_CONN_STATE_CONNECTING: return "connect()ing";
98 case OR_CONN_STATE_PROXY_FLUSHING: return "proxy flushing";
99 case OR_CONN_STATE_PROXY_READING: return "proxy reading";
100 case OR_CONN_STATE_TLS_HANDSHAKING: return "handshaking (TLS)";
101 case OR_CONN_STATE_TLS_CLIENT_RENEGOTIATING:
102 return "renegotiating (TLS)";
103 case OR_CONN_STATE_TLS_SERVER_RENEGOTIATING:
104 return "waiting for renegotiation (TLS)";
105 case OR_CONN_STATE_OR_HANDSHAKING: return "handshaking (Tor)";
106 case OR_CONN_STATE_OPEN: return "open";
108 break;
109 case CONN_TYPE_EXIT:
110 switch (state) {
111 case EXIT_CONN_STATE_RESOLVING: return "waiting for dest info";
112 case EXIT_CONN_STATE_CONNECTING: return "connecting";
113 case EXIT_CONN_STATE_OPEN: return "open";
114 case EXIT_CONN_STATE_RESOLVEFAILED: return "resolve failed";
116 break;
117 case CONN_TYPE_AP:
118 switch (state) {
119 case AP_CONN_STATE_SOCKS_WAIT: return "waiting for socks info";
120 case AP_CONN_STATE_NATD_WAIT: return "waiting for natd dest info";
121 case AP_CONN_STATE_RENDDESC_WAIT: return "waiting for rendezvous desc";
122 case AP_CONN_STATE_CONTROLLER_WAIT: return "waiting for controller";
123 case AP_CONN_STATE_CIRCUIT_WAIT: return "waiting for circuit";
124 case AP_CONN_STATE_CONNECT_WAIT: return "waiting for connect response";
125 case AP_CONN_STATE_RESOLVE_WAIT: return "waiting for resolve response";
126 case AP_CONN_STATE_OPEN: return "open";
128 break;
129 case CONN_TYPE_DIR:
130 switch (state) {
131 case DIR_CONN_STATE_CONNECTING: return "connecting";
132 case DIR_CONN_STATE_CLIENT_SENDING: return "client sending";
133 case DIR_CONN_STATE_CLIENT_READING: return "client reading";
134 case DIR_CONN_STATE_CLIENT_FINISHED: return "client finished";
135 case DIR_CONN_STATE_SERVER_COMMAND_WAIT: return "waiting for command";
136 case DIR_CONN_STATE_SERVER_WRITING: return "writing";
138 break;
139 case CONN_TYPE_CPUWORKER:
140 switch (state) {
141 case CPUWORKER_STATE_IDLE: return "idle";
142 case CPUWORKER_STATE_BUSY_ONION: return "busy with onion";
144 break;
145 case CONN_TYPE_CONTROL:
146 switch (state) {
147 case CONTROL_CONN_STATE_OPEN: return "open (protocol v1)";
148 case CONTROL_CONN_STATE_NEEDAUTH:
149 return "waiting for authentication (protocol v1)";
151 break;
154 log_warn(LD_BUG, "unknown connection state %d (type %d)", state, type);
155 tor_snprintf(buf, sizeof(buf),
156 "unknown state [%d] on unknown [%s] connection",
157 state, conn_type_to_string(type));
158 return buf;
161 /** Allocate and return a new dir_connection_t, initialized as by
162 * connection_init(). */
163 dir_connection_t *
164 dir_connection_new(int socket_family)
166 dir_connection_t *dir_conn = tor_malloc_zero(sizeof(dir_connection_t));
167 connection_init(time(NULL), TO_CONN(dir_conn), CONN_TYPE_DIR, socket_family);
168 return dir_conn;
171 /** Allocate and return a new or_connection_t, initialized as by
172 * connection_init(). */
173 or_connection_t *
174 or_connection_new(int socket_family)
176 or_connection_t *or_conn = tor_malloc_zero(sizeof(or_connection_t));
177 time_t now = time(NULL);
178 connection_init(now, TO_CONN(or_conn), CONN_TYPE_OR, socket_family);
180 or_conn->timestamp_last_added_nonpadding = time(NULL);
181 or_conn->next_circ_id = crypto_rand_int(1<<15);
183 return or_conn;
186 /** Allocate and return a new edge_connection_t, initialized as by
187 * connection_init(). */
188 edge_connection_t *
189 edge_connection_new(int type, int socket_family)
191 edge_connection_t *edge_conn = tor_malloc_zero(sizeof(edge_connection_t));
192 tor_assert(type == CONN_TYPE_EXIT || type == CONN_TYPE_AP);
193 connection_init(time(NULL), TO_CONN(edge_conn), type, socket_family);
194 if (type == CONN_TYPE_AP)
195 edge_conn->socks_request = tor_malloc_zero(sizeof(socks_request_t));
196 return edge_conn;
199 /** Allocate and return a new control_connection_t, initialized as by
200 * connection_init(). */
201 control_connection_t *
202 control_connection_new(int socket_family)
204 control_connection_t *control_conn =
205 tor_malloc_zero(sizeof(control_connection_t));
206 connection_init(time(NULL),
207 TO_CONN(control_conn), CONN_TYPE_CONTROL, socket_family);
208 return control_conn;
211 /** Allocate, initialize, and return a new connection_t subtype of <b>type</b>
212 * to make or receive connections of address family <b>socket_family</b>. The
213 * type should be one of the CONN_TYPE_* constants. */
214 connection_t *
215 connection_new(int type, int socket_family)
217 switch (type) {
218 case CONN_TYPE_OR:
219 return TO_CONN(or_connection_new(socket_family));
221 case CONN_TYPE_EXIT:
222 case CONN_TYPE_AP:
223 return TO_CONN(edge_connection_new(type, socket_family));
225 case CONN_TYPE_DIR:
226 return TO_CONN(dir_connection_new(socket_family));
228 case CONN_TYPE_CONTROL:
229 return TO_CONN(control_connection_new(socket_family));
231 default: {
232 connection_t *conn = tor_malloc_zero(sizeof(connection_t));
233 connection_init(time(NULL), conn, type, socket_family);
234 return conn;
239 /** Initializes conn. (you must call connection_add() to link it into the main
240 * array).
242 * Set conn-\>type to <b>type</b>. Set conn-\>s and conn-\>conn_array_index to
243 * -1 to signify they are not yet assigned.
245 * If conn is not a listener type, allocate buffers for it. If it's
246 * an AP type, allocate space to store the socks_request.
248 * Assign a pseudorandom next_circ_id between 0 and 2**15.
250 * Initialize conn's timestamps to now.
252 static void
253 connection_init(time_t now, connection_t *conn, int type, int socket_family)
255 static uint64_t n_connections_allocated = 1;
257 switch (type) {
258 case CONN_TYPE_OR:
259 conn->magic = OR_CONNECTION_MAGIC;
260 break;
261 case CONN_TYPE_EXIT:
262 case CONN_TYPE_AP:
263 conn->magic = EDGE_CONNECTION_MAGIC;
264 break;
265 case CONN_TYPE_DIR:
266 conn->magic = DIR_CONNECTION_MAGIC;
267 break;
268 case CONN_TYPE_CONTROL:
269 conn->magic = CONTROL_CONNECTION_MAGIC;
270 break;
271 default:
272 conn->magic = BASE_CONNECTION_MAGIC;
273 break;
276 conn->s = -1; /* give it a default of 'not used' */
277 conn->conn_array_index = -1; /* also default to 'not used' */
278 conn->global_identifier = n_connections_allocated++;
280 conn->type = type;
281 conn->socket_family = socket_family;
282 if (!connection_is_listener(conn)) { /* listeners never use their buf */
283 conn->inbuf = buf_new();
284 conn->outbuf = buf_new();
287 conn->timestamp_created = now;
288 conn->timestamp_lastread = now;
289 conn->timestamp_lastwritten = now;
292 /** Create a link between <b>conn_a</b> and <b>conn_b</b>. */
293 void
294 connection_link_connections(connection_t *conn_a, connection_t *conn_b)
296 tor_assert(conn_a->s < 0);
297 tor_assert(conn_b->s < 0);
299 conn_a->linked = 1;
300 conn_b->linked = 1;
301 conn_a->linked_conn = conn_b;
302 conn_b->linked_conn = conn_a;
305 /** Tell libevent that we don't care about <b>conn</b> any more. */
306 void
307 connection_unregister_events(connection_t *conn)
309 if (conn->read_event) {
310 if (event_del(conn->read_event))
311 log_warn(LD_BUG, "Error removing read event for %d", conn->s);
312 tor_free(conn->read_event);
314 if (conn->write_event) {
315 if (event_del(conn->write_event))
316 log_warn(LD_BUG, "Error removing write event for %d", conn->s);
317 tor_free(conn->write_event);
319 if (conn->dns_server_port) {
320 dnsserv_close_listener(conn);
324 /** Deallocate memory used by <b>conn</b>. Deallocate its buffers if
325 * necessary, close its socket if necessary, and mark the directory as dirty
326 * if <b>conn</b> is an OR or OP connection.
328 static void
329 _connection_free(connection_t *conn)
331 void *mem;
332 size_t memlen;
333 switch (conn->type) {
334 case CONN_TYPE_OR:
335 tor_assert(conn->magic == OR_CONNECTION_MAGIC);
336 mem = TO_OR_CONN(conn);
337 memlen = sizeof(or_connection_t);
338 break;
339 case CONN_TYPE_AP:
340 case CONN_TYPE_EXIT:
341 tor_assert(conn->magic == EDGE_CONNECTION_MAGIC);
342 mem = TO_EDGE_CONN(conn);
343 memlen = sizeof(edge_connection_t);
344 break;
345 case CONN_TYPE_DIR:
346 tor_assert(conn->magic == DIR_CONNECTION_MAGIC);
347 mem = TO_DIR_CONN(conn);
348 memlen = sizeof(dir_connection_t);
349 break;
350 case CONN_TYPE_CONTROL:
351 tor_assert(conn->magic == CONTROL_CONNECTION_MAGIC);
352 mem = TO_CONTROL_CONN(conn);
353 memlen = sizeof(control_connection_t);
354 break;
355 default:
356 tor_assert(conn->magic == BASE_CONNECTION_MAGIC);
357 mem = conn;
358 memlen = sizeof(connection_t);
359 break;
362 if (conn->linked) {
363 log_info(LD_GENERAL, "Freeing linked %s connection [%s] with %d "
364 "bytes on inbuf, %d on outbuf.",
365 conn_type_to_string(conn->type),
366 conn_state_to_string(conn->type, conn->state),
367 (int)buf_datalen(conn->inbuf), (int)buf_datalen(conn->outbuf));
370 if (!connection_is_listener(conn)) {
371 buf_free(conn->inbuf);
372 buf_free(conn->outbuf);
373 } else {
374 if (conn->socket_family == AF_UNIX) {
375 /* For now only control ports can be unix domain sockets
376 * and listeners at the same time */
377 tor_assert(conn->type == CONN_TYPE_CONTROL_LISTENER);
379 if (unlink(conn->address) < 0 && errno != ENOENT) {
380 log_warn(LD_NET, "Could not unlink %s: %s", conn->address,
381 strerror(errno));
386 tor_free(conn->address);
388 if (connection_speaks_cells(conn)) {
389 or_connection_t *or_conn = TO_OR_CONN(conn);
390 if (or_conn->tls) {
391 tor_tls_free(or_conn->tls);
392 or_conn->tls = NULL;
394 if (or_conn->handshake_state) {
395 or_handshake_state_free(or_conn->handshake_state);
396 or_conn->handshake_state = NULL;
398 tor_free(or_conn->nickname);
400 if (CONN_IS_EDGE(conn)) {
401 edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
402 tor_free(edge_conn->chosen_exit_name);
403 if (edge_conn->socks_request) {
404 memset(edge_conn->socks_request, 0xcc, sizeof(socks_request_t));
405 tor_free(edge_conn->socks_request);
407 if (edge_conn->rend_data)
408 rend_data_free(edge_conn->rend_data);
410 if (conn->type == CONN_TYPE_CONTROL) {
411 control_connection_t *control_conn = TO_CONTROL_CONN(conn);
412 tor_free(control_conn->incoming_cmd);
415 tor_free(conn->read_event); /* Probably already freed by connection_free. */
416 tor_free(conn->write_event); /* Probably already freed by connection_free. */
418 if (conn->type == CONN_TYPE_DIR) {
419 dir_connection_t *dir_conn = TO_DIR_CONN(conn);
420 tor_free(dir_conn->requested_resource);
421 if (dir_conn->zlib_state)
422 tor_zlib_free(dir_conn->zlib_state);
423 if (dir_conn->fingerprint_stack) {
424 SMARTLIST_FOREACH(dir_conn->fingerprint_stack, char *, cp, tor_free(cp));
425 smartlist_free(dir_conn->fingerprint_stack);
427 if (dir_conn->cached_dir)
428 cached_dir_decref(dir_conn->cached_dir);
429 if (dir_conn->rend_data)
430 rend_data_free(dir_conn->rend_data);
433 if (conn->s >= 0) {
434 log_debug(LD_NET,"closing fd %d.",conn->s);
435 tor_close_socket(conn->s);
436 conn->s = -1;
439 if (conn->type == CONN_TYPE_OR &&
440 !tor_digest_is_zero(TO_OR_CONN(conn)->identity_digest)) {
441 log_warn(LD_BUG, "called on OR conn with non-zeroed identity_digest");
442 connection_or_remove_from_identity_map(TO_OR_CONN(conn));
445 memset(conn, 0xAA, memlen); /* poison memory */
446 tor_free(mem);
449 /** Make sure <b>conn</b> isn't in any of the global conn lists; then free it.
451 void
452 connection_free(connection_t *conn)
454 tor_assert(conn);
455 tor_assert(!connection_is_on_closeable_list(conn));
456 tor_assert(!connection_in_array(conn));
457 if (conn->linked_conn) {
458 log_err(LD_BUG, "Called with conn->linked_conn still set.");
459 tor_fragile_assert();
460 conn->linked_conn->linked_conn = NULL;
461 if (! conn->linked_conn->marked_for_close &&
462 conn->linked_conn->reading_from_linked_conn)
463 connection_start_reading(conn->linked_conn);
464 conn->linked_conn = NULL;
466 if (connection_speaks_cells(conn)) {
467 if (!tor_digest_is_zero(TO_OR_CONN(conn)->identity_digest)) {
468 connection_or_remove_from_identity_map(TO_OR_CONN(conn));
471 if (conn->type == CONN_TYPE_CONTROL) {
472 TO_CONTROL_CONN(conn)->event_mask = 0;
473 control_update_global_event_mask();
475 connection_unregister_events(conn);
476 _connection_free(conn);
479 /** Call _connection_free() on every connection in our array, and release all
480 * storage helpd by connection.c. This is used by cpuworkers and dnsworkers
481 * when they fork, so they don't keep resources held open (especially
482 * sockets).
484 * Don't do the checks in connection_free(), because they will
485 * fail.
487 void
488 connection_free_all(void)
490 smartlist_t *conns = get_connection_array();
492 /* We don't want to log any messages to controllers. */
493 SMARTLIST_FOREACH(conns, connection_t *, conn,
494 if (conn->type == CONN_TYPE_CONTROL)
495 TO_CONTROL_CONN(conn)->event_mask = 0);
497 control_update_global_event_mask();
499 /* Unlink everything from the identity map. */
500 connection_or_clear_identity_map();
502 SMARTLIST_FOREACH(conns, connection_t *, conn, _connection_free(conn));
504 if (outgoing_addrs) {
505 SMARTLIST_FOREACH(outgoing_addrs, void*, addr, tor_free(addr));
506 smartlist_free(outgoing_addrs);
507 outgoing_addrs = NULL;
511 /** Do any cleanup needed:
512 * - Directory conns that failed to fetch a rendezvous descriptor
513 * need to inform pending rendezvous streams.
514 * - OR conns need to call rep_hist_note_*() to record status.
515 * - AP conns need to send a socks reject if necessary.
516 * - Exit conns need to call connection_dns_remove() if necessary.
517 * - AP and Exit conns need to send an end cell if they can.
518 * - DNS conns need to fail any resolves that are pending on them.
519 * - OR and edge connections need to be unlinked from circuits.
521 void
522 connection_about_to_close_connection(connection_t *conn)
524 circuit_t *circ;
525 dir_connection_t *dir_conn;
526 or_connection_t *or_conn;
527 edge_connection_t *edge_conn;
528 time_t now = time(NULL);
530 tor_assert(conn->marked_for_close);
532 if (CONN_IS_EDGE(conn)) {
533 edge_conn = TO_EDGE_CONN(conn);
534 if (!edge_conn->edge_has_sent_end) {
535 log_warn(LD_BUG, "(Harmless.) Edge connection (marked at %s:%d) "
536 "hasn't sent end yet?",
537 conn->marked_for_close_file, conn->marked_for_close);
538 tor_fragile_assert();
542 switch (conn->type) {
543 case CONN_TYPE_DIR:
544 dir_conn = TO_DIR_CONN(conn);
545 if (conn->state < DIR_CONN_STATE_CLIENT_FINISHED) {
546 /* It's a directory connection and connecting or fetching
547 * failed: forget about this router, and maybe try again. */
548 connection_dir_request_failed(dir_conn);
550 if (conn->purpose == DIR_PURPOSE_FETCH_RENDDESC && dir_conn->rend_data) {
551 /* Give it a try. However, there is no re-fetching for v0 rend
552 * descriptors; if the response is empty or the descriptor is
553 * unusable, close pending connections (unless a v2 request is
554 * still in progress). */
555 rend_client_desc_trynow(dir_conn->rend_data->onion_address, 0);
557 /* If we were trying to fetch a v2 rend desc and did not succeed,
558 * retry as needed. (If a fetch is successful, the connection state
559 * is changed to DIR_PURPOSE_HAS_FETCHED_RENDDESC to mark that
560 * refetching is unnecessary.) */
561 if (conn->purpose == DIR_PURPOSE_FETCH_RENDDESC_V2 &&
562 dir_conn->rend_data &&
563 strlen(dir_conn->rend_data->onion_address) ==
564 REND_SERVICE_ID_LEN_BASE32)
565 rend_client_refetch_v2_renddesc(dir_conn->rend_data);
566 break;
567 case CONN_TYPE_OR:
568 or_conn = TO_OR_CONN(conn);
569 /* Remember why we're closing this connection. */
570 if (conn->state != OR_CONN_STATE_OPEN) {
571 /* Inform any pending (not attached) circs that they should
572 * give up. */
573 circuit_n_conn_done(TO_OR_CONN(conn), 0);
574 /* now mark things down as needed */
575 if (connection_or_nonopen_was_started_here(or_conn)) {
576 or_options_t *options = get_options();
577 rep_hist_note_connect_failed(or_conn->identity_digest, now);
578 entry_guard_register_connect_status(or_conn->identity_digest,0,now);
579 if (!options->HttpsProxy)
580 router_set_status(or_conn->identity_digest, 0);
581 if (conn->state >= OR_CONN_STATE_TLS_HANDSHAKING) {
582 int reason = tls_error_to_orconn_end_reason(or_conn->tls_error);
583 control_event_or_conn_status(or_conn, OR_CONN_EVENT_FAILED,
584 reason);
585 if (!authdir_mode_tests_reachability(options))
586 control_event_bootstrap_problem(
587 orconn_end_reason_to_control_string(reason), reason);
590 } else if (conn->hold_open_until_flushed) {
591 /* We only set hold_open_until_flushed when we're intentionally
592 * closing a connection. */
593 rep_hist_note_disconnect(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));
596 } else if (or_conn->identity_digest) {
597 rep_hist_note_connection_died(or_conn->identity_digest, now);
598 control_event_or_conn_status(or_conn, OR_CONN_EVENT_CLOSED,
599 tls_error_to_orconn_end_reason(or_conn->tls_error));
601 /* Now close all the attached circuits on it. */
602 circuit_unlink_all_from_or_conn(TO_OR_CONN(conn),
603 END_CIRC_REASON_OR_CONN_CLOSED);
604 break;
605 case CONN_TYPE_AP:
606 edge_conn = TO_EDGE_CONN(conn);
607 if (edge_conn->socks_request->has_finished == 0) {
608 /* since conn gets removed right after this function finishes,
609 * there's no point trying to send back a reply at this point. */
610 log_warn(LD_BUG,"Closing stream (marked at %s:%d) without sending"
611 " back a socks reply.",
612 conn->marked_for_close_file, conn->marked_for_close);
614 if (!edge_conn->end_reason) {
615 log_warn(LD_BUG,"Closing stream (marked at %s:%d) without having"
616 " set end_reason.",
617 conn->marked_for_close_file, conn->marked_for_close);
619 if (edge_conn->dns_server_request) {
620 log_warn(LD_BUG,"Closing stream (marked at %s:%d) without having"
621 " replied to DNS request.",
622 conn->marked_for_close_file, conn->marked_for_close);
623 dnsserv_reject_request(edge_conn);
625 control_event_stream_status(edge_conn, STREAM_EVENT_CLOSED,
626 edge_conn->end_reason);
627 circ = circuit_get_by_edge_conn(edge_conn);
628 if (circ)
629 circuit_detach_stream(circ, edge_conn);
630 break;
631 case CONN_TYPE_EXIT:
632 edge_conn = TO_EDGE_CONN(conn);
633 circ = circuit_get_by_edge_conn(edge_conn);
634 if (circ)
635 circuit_detach_stream(circ, edge_conn);
636 if (conn->state == EXIT_CONN_STATE_RESOLVING) {
637 connection_dns_remove(edge_conn);
639 break;
643 /** Return true iff connection_close_immediate() has been called on this
644 * connection. */
645 #define CONN_IS_CLOSED(c) \
646 ((c)->linked ? ((c)->linked_conn_is_closed) : ((c)->s < 0))
648 /** Close the underlying socket for <b>conn</b>, so we don't try to
649 * flush it. Must be used in conjunction with (right before)
650 * connection_mark_for_close().
652 void
653 connection_close_immediate(connection_t *conn)
655 assert_connection_ok(conn,0);
656 if (CONN_IS_CLOSED(conn)) {
657 log_err(LD_BUG,"Attempt to close already-closed connection.");
658 tor_fragile_assert();
659 return;
661 if (conn->outbuf_flushlen) {
662 log_info(LD_NET,"fd %d, type %s, state %s, %d bytes on outbuf.",
663 conn->s, conn_type_to_string(conn->type),
664 conn_state_to_string(conn->type, conn->state),
665 (int)conn->outbuf_flushlen);
668 connection_unregister_events(conn);
670 if (conn->s >= 0)
671 tor_close_socket(conn->s);
672 conn->s = -1;
673 if (conn->linked)
674 conn->linked_conn_is_closed = 1;
675 if (!connection_is_listener(conn)) {
676 buf_clear(conn->outbuf);
677 conn->outbuf_flushlen = 0;
681 /** Mark <b>conn</b> to be closed next time we loop through
682 * conn_close_if_marked() in main.c. */
683 void
684 _connection_mark_for_close(connection_t *conn, int line, const char *file)
686 assert_connection_ok(conn,0);
687 tor_assert(line);
688 tor_assert(line < 1<<16); /* marked_for_close can only fit a uint16_t. */
689 tor_assert(file);
691 if (conn->marked_for_close) {
692 log(LOG_WARN,LD_BUG,"Duplicate call to connection_mark_for_close at %s:%d"
693 " (first at %s:%d)", file, line, conn->marked_for_close_file,
694 conn->marked_for_close);
695 tor_fragile_assert();
696 return;
699 conn->marked_for_close = line;
700 conn->marked_for_close_file = file;
701 add_connection_to_closeable_list(conn);
703 /* in case we're going to be held-open-til-flushed, reset
704 * the number of seconds since last successful write, so
705 * we get our whole 15 seconds */
706 conn->timestamp_lastwritten = time(NULL);
709 /** Find each connection that has hold_open_until_flushed set to
710 * 1 but hasn't written in the past 15 seconds, and set
711 * hold_open_until_flushed to 0. This means it will get cleaned
712 * up in the next loop through close_if_marked() in main.c.
714 void
715 connection_expire_held_open(void)
717 time_t now;
718 smartlist_t *conns = get_connection_array();
720 now = time(NULL);
722 SMARTLIST_FOREACH(conns, connection_t *, conn,
724 /* If we've been holding the connection open, but we haven't written
725 * for 15 seconds...
727 if (conn->hold_open_until_flushed) {
728 tor_assert(conn->marked_for_close);
729 if (now - conn->timestamp_lastwritten >= 15) {
730 int severity;
731 if (conn->type == CONN_TYPE_EXIT ||
732 (conn->type == CONN_TYPE_DIR &&
733 conn->purpose == DIR_PURPOSE_SERVER))
734 severity = LOG_INFO;
735 else
736 severity = LOG_NOTICE;
737 log_fn(severity, LD_NET,
738 "Giving up on marked_for_close conn that's been flushing "
739 "for 15s (fd %d, type %s, state %s).",
740 conn->s, conn_type_to_string(conn->type),
741 conn_state_to_string(conn->type, conn->state));
742 conn->hold_open_until_flushed = 0;
748 /** Create an AF_INET listenaddr struct.
749 * <b>listenaddress</b> provides the host and optionally the port information
750 * for the new structure. If no port is provided in <b>listenaddress</b> then
751 * <b>listenport</b> is used.
753 * If not NULL <b>readable_addrress</b> will contain a copy of the host part of
754 * <b>listenaddress</b>.
756 * The listenaddr struct has to be freed by the caller.
758 static struct sockaddr_in *
759 create_inet_sockaddr(const char *listenaddress, uint16_t listenport,
760 char **readable_address, socklen_t *socklen_out) {
761 struct sockaddr_in *listenaddr = NULL;
762 uint32_t addr;
763 uint16_t usePort = 0;
765 if (parse_addr_port(LOG_WARN,
766 listenaddress, readable_address, &addr, &usePort)<0) {
767 log_warn(LD_CONFIG,
768 "Error parsing/resolving ListenAddress %s", listenaddress);
769 goto err;
771 if (usePort==0)
772 usePort = listenport;
774 listenaddr = tor_malloc_zero(sizeof(struct sockaddr_in));
775 listenaddr->sin_addr.s_addr = htonl(addr);
776 listenaddr->sin_family = AF_INET;
777 listenaddr->sin_port = htons((uint16_t) usePort);
779 *socklen_out = sizeof(struct sockaddr_in);
781 return listenaddr;
783 err:
784 tor_free(listenaddr);
785 return NULL;
788 #ifdef HAVE_SYS_UN_H
789 /** Create an AF_UNIX listenaddr struct.
790 * <b>listenaddress</b> provides the path to the unix socket.
792 * Eventually <b>listenaddress</b> will also optionally contain user, group,
793 * and file permissions for the new socket. But not yet. XXX
794 * Also, since we do not create the socket here the information doesn't help
795 * here.
797 * If not NULL <b>readable_addrress</b> will contain a copy of the path part of
798 * <b>listenaddress</b>.
800 * The listenaddr struct has to be freed by the caller.
802 static struct sockaddr_un *
803 create_unix_sockaddr(const char *listenaddress, char **readable_address,
804 socklen_t *len_out)
806 struct sockaddr_un *sockaddr = NULL;
808 sockaddr = tor_malloc_zero(sizeof(struct sockaddr_un));
809 sockaddr->sun_family = AF_UNIX;
810 strncpy(sockaddr->sun_path, listenaddress, sizeof(sockaddr->sun_path));
812 if (readable_address)
813 *readable_address = tor_strdup(listenaddress);
815 *len_out = sizeof(struct sockaddr_un);
816 return sockaddr;
818 #else
819 static struct sockaddr *
820 create_unix_sockaddr(const char *listenaddress, char **readable_address,
821 socklen_t *len_out)
823 (void)listenaddress;
824 (void)readable_address;
825 log_fn(LOG_ERR, LD_BUG,
826 "Unix domain sockets not supported, yet we tried to create one.");
827 *len_out = 0;
828 tor_assert(0);
830 #endif /* HAVE_SYS_UN_H */
832 /** Warn that an accept or a connect has failed because we're running up
833 * against our ulimit. Rate-limit these warnings so that we don't spam
834 * the log. */
835 static void
836 warn_too_many_conns(void)
838 #define WARN_TOO_MANY_CONNS_INTERVAL (6*60*60)
839 static time_t last_warned = 0;
840 time_t now = time(NULL);
841 int n_conns = get_n_open_sockets();
842 if (last_warned + WARN_TOO_MANY_CONNS_INTERVAL < now) {
843 log_warn(LD_NET,"Failing because we have %d connections already. Please "
844 "raise your ulimit -n.", n_conns);
845 last_warned = now;
847 control_event_general_status(LOG_WARN, "TOO_MANY_CONNECTIONS CURRENT=%d",
848 n_conns);
851 /** Bind a new non-blocking socket listening to the socket described
852 * by <b>listensockaddr</b>.
854 * <b>address</b> is only used for logging purposes and to add the information
855 * to the conn.
857 static connection_t *
858 connection_create_listener(struct sockaddr *listensockaddr, socklen_t socklen,
859 int type, char* address)
861 connection_t *conn;
862 int s; /* the socket we're going to make */
863 uint16_t usePort = 0;
864 int start_reading = 0;
866 if (get_n_open_sockets() >= get_options()->_ConnLimit-1) {
867 warn_too_many_conns();
868 return NULL;
871 if (listensockaddr->sa_family == AF_INET) {
872 int is_tcp = (type != CONN_TYPE_AP_DNS_LISTENER);
873 #ifndef MS_WINDOWS
874 int one=1;
875 #endif
876 if (is_tcp)
877 start_reading = 1;
879 usePort = ntohs( (uint16_t)
880 ((struct sockaddr_in *)listensockaddr)->sin_port);
882 log_notice(LD_NET, "Opening %s on %s:%d",
883 conn_type_to_string(type), address, usePort);
885 s = tor_open_socket(PF_INET,
886 is_tcp ? SOCK_STREAM : SOCK_DGRAM,
887 is_tcp ? IPPROTO_TCP: IPPROTO_UDP);
888 if (s < 0) {
889 log_warn(LD_NET,"Socket creation failed.");
890 goto err;
893 #ifndef MS_WINDOWS
894 /* REUSEADDR on normal places means you can rebind to the port
895 * right after somebody else has let it go. But REUSEADDR on win32
896 * means you can bind to the port _even when somebody else
897 * already has it bound_. So, don't do that on Win32. */
898 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (void*) &one,
899 (socklen_t)sizeof(one));
900 #endif
902 if (bind(s,listensockaddr,socklen) < 0) {
903 const char *helpfulhint = "";
904 int e = tor_socket_errno(s);
905 if (ERRNO_IS_EADDRINUSE(e))
906 helpfulhint = ". Is Tor already running?";
907 log_warn(LD_NET, "Could not bind to %s:%u: %s%s", address, usePort,
908 tor_socket_strerror(e), helpfulhint);
909 tor_close_socket(s);
910 goto err;
913 if (is_tcp) {
914 if (listen(s,SOMAXCONN) < 0) {
915 log_warn(LD_NET, "Could not listen on %s:%u: %s", address, usePort,
916 tor_socket_strerror(tor_socket_errno(s)));
917 tor_close_socket(s);
918 goto err;
921 #ifdef HAVE_SYS_UN_H
922 } else if (listensockaddr->sa_family == AF_UNIX) {
923 start_reading = 1;
925 /* For now only control ports can be unix domain sockets
926 * and listeners at the same time */
927 tor_assert(type == CONN_TYPE_CONTROL_LISTENER);
929 log_notice(LD_NET, "Opening %s on %s",
930 conn_type_to_string(type), address);
932 if (unlink(address) < 0 && errno != ENOENT) {
933 log_warn(LD_NET, "Could not unlink %s: %s", address,
934 strerror(errno));
935 goto err;
937 s = tor_open_socket(AF_UNIX, SOCK_STREAM, 0);
938 if (s < 0) {
939 log_warn(LD_NET,"Socket creation failed: %s.", strerror(errno));
940 goto err;
943 if (bind(s, listensockaddr, (socklen_t)sizeof(struct sockaddr_un)) == -1) {
944 log_warn(LD_NET,"Bind to %s failed: %s.", address,
945 tor_socket_strerror(tor_socket_errno(s)));
946 goto err;
949 if (listen(s,SOMAXCONN) < 0) {
950 log_warn(LD_NET, "Could not listen on %s: %s", address,
951 tor_socket_strerror(tor_socket_errno(s)));
952 tor_close_socket(s);
953 goto err;
955 #endif /* HAVE_SYS_UN_H */
956 } else {
957 log_err(LD_BUG,"Got unexpected address family %d.",
958 listensockaddr->sa_family);
959 tor_assert(0);
962 set_socket_nonblocking(s);
964 conn = connection_new(type, listensockaddr->sa_family);
965 conn->socket_family = listensockaddr->sa_family;
966 conn->s = s;
967 conn->address = tor_strdup(address);
968 conn->port = usePort;
970 if (connection_add(conn) < 0) { /* no space, forget it */
971 log_warn(LD_NET,"connection_add for listener failed. Giving up.");
972 connection_free(conn);
973 goto err;
976 log_debug(LD_NET,"%s listening on port %u.",
977 conn_type_to_string(type), usePort);
979 conn->state = LISTENER_STATE_READY;
980 if (start_reading) {
981 connection_start_reading(conn);
982 } else {
983 tor_assert(type == CONN_TYPE_AP_DNS_LISTENER);
984 dnsserv_configure_listener(conn);
987 return conn;
989 err:
990 return NULL;
993 /** Do basic sanity checking on a newly received socket. Return 0
994 * if it looks ok, else return -1. */
995 static int
996 check_sockaddr(struct sockaddr *sa, int len, int level)
998 int ok = 1;
1000 if (sa->sa_family == AF_INET) {
1001 struct sockaddr_in *sin=(struct sockaddr_in*)sa;
1002 if (len != sizeof(struct sockaddr_in)) {
1003 log_fn(level, LD_NET, "Length of address not as expected: %d vs %d",
1004 len,(int)sizeof(struct sockaddr_in));
1005 ok = 0;
1007 if (sin->sin_addr.s_addr == 0 || sin->sin_port == 0) {
1008 log_fn(level, LD_NET,
1009 "Address for new connection has address/port equal to zero.");
1010 ok = 0;
1012 } else if (sa->sa_family == AF_INET6) {
1013 struct sockaddr_in6 *sin6=(struct sockaddr_in6*)sa;
1014 if (len != sizeof(struct sockaddr_in6)) {
1015 log_fn(level, LD_NET, "Length of address not as expected: %d vs %d",
1016 len,(int)sizeof(struct sockaddr_in6));
1017 ok = 0;
1019 if (tor_mem_is_zero((void*)sin6->sin6_addr.s6_addr, 16) ||
1020 sin6->sin6_port == 0) {
1021 log_fn(level, LD_NET,
1022 "Address for new connection has address/port equal to zero.");
1023 ok = 0;
1025 } else {
1026 ok = 0;
1028 return ok ? 0 : -1;
1031 /** Check whether the socket family from an accepted socket <b>got</b> is the
1032 * same as the one that <b>listener</b> is waiting for. If it isn't, log
1033 * a useful message and return -1. Else return 0.
1035 * This is annoying, but can apparently happen on some Darwins. */
1036 static int
1037 check_sockaddr_family_match(sa_family_t got, connection_t *listener)
1039 if (got != listener->socket_family) {
1040 log_info(LD_BUG, "A listener connection returned a socket with a "
1041 "mismatched family. %s for addr_family %d gave us a socket "
1042 "with address family %d. Dropping.",
1043 conn_type_to_string(listener->type),
1044 (int)listener->socket_family,
1045 (int)got);
1046 return -1;
1048 return 0;
1051 /** The listener connection <b>conn</b> told poll() it wanted to read.
1052 * Call accept() on conn-\>s, and add the new connection if necessary.
1054 static int
1055 connection_handle_listener_read(connection_t *conn, int new_type)
1057 int news; /* the new socket */
1058 connection_t *newconn;
1059 /* information about the remote peer when connecting to other routers */
1060 char addrbuf[256];
1061 struct sockaddr *remote = (struct sockaddr*)addrbuf;
1062 /* length of the remote address. Must be whatever accept() needs. */
1063 socklen_t remotelen = (socklen_t)sizeof(addrbuf);
1064 or_options_t *options = get_options();
1066 tor_assert((size_t)remotelen >= sizeof(struct sockaddr_in));
1067 memset(addrbuf, 0, sizeof(addrbuf));
1069 news = tor_accept_socket(conn->s,remote,&remotelen);
1070 if (news < 0) { /* accept() error */
1071 int e = tor_socket_errno(conn->s);
1072 if (ERRNO_IS_ACCEPT_EAGAIN(e)) {
1073 return 0; /* he hung up before we could accept(). that's fine. */
1074 } else if (ERRNO_IS_ACCEPT_RESOURCE_LIMIT(e)) {
1075 warn_too_many_conns();
1076 return 0;
1078 /* else there was a real error. */
1079 log_warn(LD_NET,"accept() failed: %s. Closing listener.",
1080 tor_socket_strerror(e));
1081 connection_mark_for_close(conn);
1082 return -1;
1084 log_debug(LD_NET,
1085 "Connection accepted on socket %d (child of fd %d).",
1086 news,conn->s);
1088 set_socket_nonblocking(news);
1090 if (options->ConstrainedSockets)
1091 set_constrained_socket_buffers(news, (int)options->ConstrainedSockSize);
1093 if (check_sockaddr_family_match(remote->sa_family, conn) < 0) {
1094 tor_close_socket(news);
1095 return 0;
1098 if (conn->socket_family == AF_INET || conn->socket_family == AF_INET6) {
1099 tor_addr_t addr;
1100 uint16_t port;
1101 if (check_sockaddr(remote, remotelen, LOG_INFO)<0) {
1102 log_info(LD_NET,
1103 "accept() returned a strange address; trying getsockname().");
1104 remotelen=sizeof(addrbuf);
1105 memset(addrbuf, 0, sizeof(addrbuf));
1106 if (getsockname(news, remote, &remotelen)<0) {
1107 int e = tor_socket_errno(news);
1108 log_warn(LD_NET, "getsockname() for new connection failed: %s",
1109 tor_socket_strerror(e));
1110 } else {
1111 if (check_sockaddr((struct sockaddr*)addrbuf, remotelen,
1112 LOG_WARN) < 0) {
1113 log_warn(LD_NET,"Something's wrong with this conn. Closing it.");
1114 tor_close_socket(news);
1115 return 0;
1120 if (check_sockaddr_family_match(remote->sa_family, conn) < 0) {
1121 tor_close_socket(news);
1122 return 0;
1125 tor_addr_from_sockaddr(&addr, remote, &port);
1127 /* process entrance policies here, before we even create the connection */
1128 if (new_type == CONN_TYPE_AP) {
1129 /* check sockspolicy to see if we should accept it */
1130 if (socks_policy_permits_address(&addr) == 0) {
1131 log_notice(LD_APP,
1132 "Denying socks connection from untrusted address %s.",
1133 fmt_addr(&addr));
1134 tor_close_socket(news);
1135 return 0;
1138 if (new_type == CONN_TYPE_DIR) {
1139 /* check dirpolicy to see if we should accept it */
1140 if (dir_policy_permits_address(&addr) == 0) {
1141 log_notice(LD_DIRSERV,"Denying dir connection from address %s.",
1142 fmt_addr(&addr));
1143 tor_close_socket(news);
1144 return 0;
1148 newconn = connection_new(new_type, conn->socket_family);
1149 newconn->s = news;
1151 /* remember the remote address */
1152 tor_addr_copy(&newconn->addr, &addr);
1153 newconn->port = port;
1154 newconn->address = tor_dup_addr(&addr);
1156 } else if (conn->socket_family == AF_UNIX) {
1157 /* For now only control ports can be unix domain sockets
1158 * and listeners at the same time */
1159 tor_assert(conn->type == CONN_TYPE_CONTROL_LISTENER);
1161 newconn = connection_new(new_type, conn->socket_family);
1162 newconn->s = news;
1164 /* remember the remote address -- do we have anything sane to put here? */
1165 tor_addr_make_unspec(&newconn->addr);
1166 newconn->port = 1;
1167 newconn->address = tor_strdup(conn->address);
1168 } else {
1169 tor_assert(0);
1172 if (connection_add(newconn) < 0) { /* no space, forget it */
1173 connection_free(newconn);
1174 return 0; /* no need to tear down the parent */
1177 if (connection_init_accepted_conn(newconn, conn->type) < 0) {
1178 connection_mark_for_close(newconn);
1179 return 0;
1181 return 0;
1184 /** Initialize states for newly accepted connection <b>conn</b>.
1185 * If conn is an OR, start the tls handshake.
1186 * If conn is a transparent AP, get its original destination
1187 * and place it in circuit_wait.
1189 static int
1190 connection_init_accepted_conn(connection_t *conn, uint8_t listener_type)
1192 connection_start_reading(conn);
1194 switch (conn->type) {
1195 case CONN_TYPE_OR:
1196 control_event_or_conn_status(TO_OR_CONN(conn), OR_CONN_EVENT_NEW, 0);
1197 return connection_tls_start_handshake(TO_OR_CONN(conn), 1);
1198 case CONN_TYPE_AP:
1199 switch (listener_type) {
1200 case CONN_TYPE_AP_LISTENER:
1201 conn->state = AP_CONN_STATE_SOCKS_WAIT;
1202 break;
1203 case CONN_TYPE_AP_TRANS_LISTENER:
1204 conn->state = AP_CONN_STATE_CIRCUIT_WAIT;
1205 return connection_ap_process_transparent(TO_EDGE_CONN(conn));
1206 case CONN_TYPE_AP_NATD_LISTENER:
1207 conn->state = AP_CONN_STATE_NATD_WAIT;
1208 break;
1210 break;
1211 case CONN_TYPE_DIR:
1212 conn->purpose = DIR_PURPOSE_SERVER;
1213 conn->state = DIR_CONN_STATE_SERVER_COMMAND_WAIT;
1214 break;
1215 case CONN_TYPE_CONTROL:
1216 conn->state = CONTROL_CONN_STATE_NEEDAUTH;
1217 break;
1219 return 0;
1222 /** Take conn, make a nonblocking socket; try to connect to
1223 * addr:port (they arrive in *host order*). If fail, return -1 and if
1224 * applicable put your best guess about errno into *<b>socket_error</b>.
1225 * Else assign s to conn-\>s: if connected return 1, if EAGAIN return 0.
1227 * address is used to make the logs useful.
1229 * On success, add conn to the list of polled connections.
1232 connection_connect(connection_t *conn, const char *address,
1233 const tor_addr_t *addr, uint16_t port, int *socket_error)
1235 int s, inprogress = 0;
1236 char addrbuf[256];
1237 struct sockaddr *dest_addr = (struct sockaddr*) addrbuf;
1238 socklen_t dest_addr_len;
1239 or_options_t *options = get_options();
1240 int protocol_family;
1242 if (get_n_open_sockets() >= get_options()->_ConnLimit-1) {
1243 warn_too_many_conns();
1244 return -1;
1247 if (tor_addr_family(addr) == AF_INET6)
1248 protocol_family = PF_INET6;
1249 else
1250 protocol_family = PF_INET;
1252 s = tor_open_socket(protocol_family,SOCK_STREAM,IPPROTO_TCP);
1253 if (s < 0) {
1254 *socket_error = tor_socket_errno(-1);
1255 log_warn(LD_NET,"Error creating network socket: %s",
1256 tor_socket_strerror(*socket_error));
1257 return -1;
1260 if (options->OutboundBindAddress) {
1261 struct sockaddr_in ext_addr;
1263 memset(&ext_addr, 0, sizeof(ext_addr));
1264 ext_addr.sin_family = AF_INET;
1265 ext_addr.sin_port = 0;
1266 if (!tor_inet_aton(options->OutboundBindAddress, &ext_addr.sin_addr)) {
1267 log_warn(LD_CONFIG,"Outbound bind address '%s' didn't parse. Ignoring.",
1268 options->OutboundBindAddress);
1269 } else {
1270 if (bind(s, (struct sockaddr*)&ext_addr,
1271 (socklen_t)sizeof(ext_addr)) < 0) {
1272 *socket_error = tor_socket_errno(s);
1273 log_warn(LD_NET,"Error binding network socket: %s",
1274 tor_socket_strerror(*socket_error));
1275 tor_close_socket(s);
1276 return -1;
1281 set_socket_nonblocking(s);
1283 if (options->ConstrainedSockets)
1284 set_constrained_socket_buffers(s, (int)options->ConstrainedSockSize);
1286 memset(addrbuf,0,sizeof(addrbuf));
1287 dest_addr = (struct sockaddr*) addrbuf;
1288 dest_addr_len = tor_addr_to_sockaddr(addr, port, dest_addr, sizeof(addrbuf));
1289 tor_assert(dest_addr_len > 0);
1291 log_debug(LD_NET,"Connecting to %s:%u.",escaped_safe_str(address),port);
1293 if (connect(s, dest_addr, dest_addr_len) < 0) {
1294 int e = tor_socket_errno(s);
1295 if (!ERRNO_IS_CONN_EINPROGRESS(e)) {
1296 /* yuck. kill it. */
1297 *socket_error = e;
1298 log_info(LD_NET,
1299 "connect() to %s:%u failed: %s",escaped_safe_str(address),
1300 port, tor_socket_strerror(e));
1301 tor_close_socket(s);
1302 return -1;
1303 } else {
1304 inprogress = 1;
1308 if (!server_mode(options))
1309 client_check_address_changed(s);
1311 /* it succeeded. we're connected. */
1312 log_fn(inprogress?LOG_DEBUG:LOG_INFO, LD_NET,
1313 "Connection to %s:%u %s (sock %d).",escaped_safe_str(address),
1314 port, inprogress?"in progress":"established", s);
1315 conn->s = s;
1316 if (connection_add(conn) < 0) /* no space, forget it */
1317 return -1;
1318 return inprogress ? 0 : 1;
1322 * Launch any configured listener connections of type <b>type</b>. (A
1323 * listener is configured if <b>port_option</b> is non-zero. If any
1324 * ListenAddress configuration options are given in <b>cfg</b>, create a
1325 * connection binding to each one. Otherwise, create a single
1326 * connection binding to the address <b>default_addr</b>.)
1328 * Only launch the listeners of this type that are not already open, and
1329 * only close listeners that are no longer wanted. Existing listeners
1330 * that are still configured are not touched.
1332 * If <b>disable_all_conns</b> is set, then never open new conns, and
1333 * close the existing ones.
1335 * Add all old conns that should be closed to <b>replaced_conns</b>.
1336 * Add all new connections to <b>new_conns</b>.
1338 static int
1339 retry_listeners(int type, config_line_t *cfg,
1340 int port_option, const char *default_addr,
1341 smartlist_t *replaced_conns,
1342 smartlist_t *new_conns,
1343 int disable_all_conns,
1344 int socket_family)
1346 smartlist_t *launch = smartlist_create(), *conns;
1347 int free_launch_elts = 1;
1348 int r;
1349 config_line_t *c;
1350 connection_t *conn;
1351 config_line_t *line;
1353 tor_assert(socket_family == AF_INET || socket_family == AF_UNIX);
1355 if (cfg && port_option) {
1356 for (c = cfg; c; c = c->next) {
1357 smartlist_add(launch, c);
1359 free_launch_elts = 0;
1360 } else if (port_option) {
1361 line = tor_malloc_zero(sizeof(config_line_t));
1362 line->key = tor_strdup("");
1363 line->value = tor_strdup(default_addr);
1364 smartlist_add(launch, line);
1368 SMARTLIST_FOREACH(launch, config_line_t *, l,
1369 log_fn(LOG_NOTICE, "#%s#%s", l->key, l->value));
1372 conns = get_connection_array();
1373 SMARTLIST_FOREACH(conns, connection_t *, conn,
1375 if (conn->type != type ||
1376 conn->socket_family != socket_family ||
1377 conn->marked_for_close)
1378 continue;
1379 /* Okay, so this is a listener. Is it configured? */
1380 line = NULL;
1381 SMARTLIST_FOREACH(launch, config_line_t *, wanted,
1383 char *address=NULL;
1384 uint16_t port;
1385 switch (socket_family) {
1386 case AF_INET:
1387 if (!parse_addr_port(LOG_WARN,
1388 wanted->value, &address, NULL, &port)) {
1389 int addr_matches = !strcasecmp(address, conn->address);
1390 tor_free(address);
1391 if (! port)
1392 port = port_option;
1393 if (port == conn->port && addr_matches) {
1394 line = wanted;
1395 break;
1398 break;
1399 case AF_UNIX:
1400 if (!strcasecmp(wanted->value, conn->address)) {
1401 line = wanted;
1402 break;
1404 break;
1405 default:
1406 tor_assert(0);
1409 if (!line || disable_all_conns) {
1410 /* This one isn't configured. Close it. */
1411 log_notice(LD_NET, "Closing no-longer-configured %s on %s:%d",
1412 conn_type_to_string(type), conn->address, conn->port);
1413 if (replaced_conns) {
1414 smartlist_add(replaced_conns, conn);
1415 } else {
1416 connection_close_immediate(conn);
1417 connection_mark_for_close(conn);
1419 } else {
1420 /* It's configured; we don't need to launch it. */
1421 // log_debug(LD_NET, "Already have %s on %s:%d",
1422 // conn_type_to_string(type), conn->address, conn->port);
1423 smartlist_remove(launch, line);
1424 if (free_launch_elts)
1425 config_free_lines(line);
1429 /* Now open all the listeners that are configured but not opened. */
1430 r = 0;
1431 if (!disable_all_conns) {
1432 SMARTLIST_FOREACH_BEGIN(launch, config_line_t *, cfg_line) {
1433 char *address = NULL;
1434 struct sockaddr *listensockaddr;
1435 socklen_t listensocklen = 0;
1437 switch (socket_family) {
1438 case AF_INET:
1439 listensockaddr = (struct sockaddr *)
1440 create_inet_sockaddr(cfg_line->value,
1441 (uint16_t) port_option,
1442 &address, &listensocklen);
1443 break;
1444 case AF_UNIX:
1445 listensockaddr = (struct sockaddr *)
1446 create_unix_sockaddr(cfg_line->value,
1447 &address, &listensocklen);
1448 break;
1449 default:
1450 tor_assert(0);
1453 if (listensockaddr) {
1454 conn = connection_create_listener(listensockaddr, listensocklen,
1455 type, address);
1456 tor_free(listensockaddr);
1457 tor_free(address);
1458 } else
1459 conn = NULL;
1461 if (!conn) {
1462 r = -1;
1463 } else {
1464 if (new_conns)
1465 smartlist_add(new_conns, conn);
1467 } SMARTLIST_FOREACH_END(cfg_line);
1470 if (free_launch_elts) {
1471 SMARTLIST_FOREACH(launch, config_line_t *, cfg_line,
1472 config_free_lines(cfg_line));
1474 smartlist_free(launch);
1476 return r;
1479 /** Launch listeners for each port you should have open. Only launch
1480 * listeners who are not already open, and only close listeners we no longer
1481 * want.
1483 * Add all old conns that should be closed to <b>replaced_conns</b>.
1484 * Add all new connections to <b>new_conns</b>.
1487 retry_all_listeners(smartlist_t *replaced_conns,
1488 smartlist_t *new_conns)
1490 or_options_t *options = get_options();
1492 if (retry_listeners(CONN_TYPE_OR_LISTENER, options->ORListenAddress,
1493 options->ORPort, "0.0.0.0",
1494 replaced_conns, new_conns, options->ClientOnly,
1495 AF_INET)<0)
1496 return -1;
1497 if (retry_listeners(CONN_TYPE_DIR_LISTENER, options->DirListenAddress,
1498 options->DirPort, "0.0.0.0",
1499 replaced_conns, new_conns, options->ClientOnly,
1500 AF_INET)<0)
1501 return -1;
1502 if (retry_listeners(CONN_TYPE_AP_LISTENER, options->SocksListenAddress,
1503 options->SocksPort, "127.0.0.1",
1504 replaced_conns, new_conns, 0,
1505 AF_INET)<0)
1506 return -1;
1507 if (retry_listeners(CONN_TYPE_AP_TRANS_LISTENER, options->TransListenAddress,
1508 options->TransPort, "127.0.0.1",
1509 replaced_conns, new_conns, 0,
1510 AF_INET)<0)
1511 return -1;
1512 if (retry_listeners(CONN_TYPE_AP_NATD_LISTENER, options->NatdListenAddress,
1513 options->NatdPort, "127.0.0.1",
1514 replaced_conns, new_conns, 0,
1515 AF_INET)<0)
1516 return -1;
1517 if (retry_listeners(CONN_TYPE_AP_DNS_LISTENER, options->DNSListenAddress,
1518 options->DNSPort, "127.0.0.1",
1519 replaced_conns, new_conns, 0,
1520 AF_INET)<0)
1521 return -1;
1522 if (retry_listeners(CONN_TYPE_CONTROL_LISTENER,
1523 options->ControlListenAddress,
1524 options->ControlPort, "127.0.0.1",
1525 replaced_conns, new_conns, 0,
1526 AF_INET)<0)
1527 return -1;
1528 if (retry_listeners(CONN_TYPE_CONTROL_LISTENER,
1529 options->ControlSocket,
1530 options->ControlSocket ? 1 : 0, NULL,
1531 replaced_conns, new_conns, 0,
1532 AF_UNIX)<0)
1533 return -1;
1535 return 0;
1538 /** Return 1 if we should apply rate limiting to <b>conn</b>,
1539 * and 0 otherwise. Right now this just checks if it's an internal
1540 * IP address or an internal connection. */
1541 static int
1542 connection_is_rate_limited(connection_t *conn)
1544 if (conn->linked || /* internal connection */
1545 tor_addr_family(&conn->addr) == AF_UNSPEC || /* no address */
1546 tor_addr_is_internal(&conn->addr, 0)) /* internal address */
1547 return 0;
1548 else
1549 return 1;
1552 extern int global_read_bucket, global_write_bucket;
1553 extern int global_relayed_read_bucket, global_relayed_write_bucket;
1555 /** Did either global write bucket run dry last second? If so,
1556 * we are likely to run dry again this second, so be stingy with the
1557 * tokens we just put in. */
1558 static int write_buckets_empty_last_second = 0;
1560 /** How many seconds of no active local circuits will make the
1561 * connection revert to the "relayed" bandwidth class? */
1562 #define CLIENT_IDLE_TIME_FOR_PRIORITY 30
1564 /** Return 1 if <b>conn</b> should use tokens from the "relayed"
1565 * bandwidth rates, else 0. Currently, only OR conns with bandwidth
1566 * class 1, and directory conns that are serving data out, count.
1568 static int
1569 connection_counts_as_relayed_traffic(connection_t *conn, time_t now)
1571 if (conn->type == CONN_TYPE_OR &&
1572 TO_OR_CONN(conn)->client_used + CLIENT_IDLE_TIME_FOR_PRIORITY < now)
1573 return 1;
1574 if (conn->type == CONN_TYPE_DIR && DIR_CONN_IS_SERVER(conn))
1575 return 1;
1576 return 0;
1579 /** Helper function to decide how many bytes out of <b>global_bucket</b>
1580 * we're willing to use for this transaction. <b>base</b> is the size
1581 * of a cell on the network; <b>priority</b> says whether we should
1582 * write many of them or just a few; and <b>conn_bucket</b> (if
1583 * non-negative) provides an upper limit for our answer. */
1584 static ssize_t
1585 connection_bucket_round_robin(int base, int priority,
1586 ssize_t global_bucket, ssize_t conn_bucket)
1588 ssize_t at_most;
1589 ssize_t num_bytes_high = (priority ? 32 : 16) * base;
1590 ssize_t num_bytes_low = (priority ? 4 : 2) * base;
1592 /* Do a rudimentary round-robin so one circuit can't hog a connection.
1593 * Pick at most 32 cells, at least 4 cells if possible, and if we're in
1594 * the middle pick 1/8 of the available bandwidth. */
1595 at_most = global_bucket / 8;
1596 at_most -= (at_most % base); /* round down */
1597 if (at_most > num_bytes_high) /* 16 KB, or 8 KB for low-priority */
1598 at_most = num_bytes_high;
1599 else if (at_most < num_bytes_low) /* 2 KB, or 1 KB for low-priority */
1600 at_most = num_bytes_low;
1602 if (at_most > global_bucket)
1603 at_most = global_bucket;
1605 if (conn_bucket >= 0 && at_most > conn_bucket)
1606 at_most = conn_bucket;
1608 if (at_most < 0)
1609 return 0;
1610 return at_most;
1613 /** How many bytes at most can we read onto this connection? */
1614 static ssize_t
1615 connection_bucket_read_limit(connection_t *conn, time_t now)
1617 int base = connection_speaks_cells(conn) ?
1618 CELL_NETWORK_SIZE : RELAY_PAYLOAD_SIZE;
1619 int priority = conn->type != CONN_TYPE_DIR;
1620 int conn_bucket = -1;
1621 int global_bucket = global_read_bucket;
1623 if (connection_speaks_cells(conn)) {
1624 or_connection_t *or_conn = TO_OR_CONN(conn);
1625 if (conn->state == OR_CONN_STATE_OPEN)
1626 conn_bucket = or_conn->read_bucket;
1629 if (!connection_is_rate_limited(conn)) {
1630 /* be willing to read on local conns even if our buckets are empty */
1631 return conn_bucket>=0 ? conn_bucket : 1<<14;
1634 if (connection_counts_as_relayed_traffic(conn, now) &&
1635 global_relayed_read_bucket <= global_read_bucket)
1636 global_bucket = global_relayed_read_bucket;
1638 return connection_bucket_round_robin(base, priority,
1639 global_bucket, conn_bucket);
1642 /** How many bytes at most can we write onto this connection? */
1643 ssize_t
1644 connection_bucket_write_limit(connection_t *conn, time_t now)
1646 int base = connection_speaks_cells(conn) ?
1647 CELL_NETWORK_SIZE : RELAY_PAYLOAD_SIZE;
1648 int priority = conn->type != CONN_TYPE_DIR;
1649 int global_bucket = global_write_bucket;
1651 if (!connection_is_rate_limited(conn)) {
1652 /* be willing to write to local conns even if our buckets are empty */
1653 return conn->outbuf_flushlen;
1656 if (connection_counts_as_relayed_traffic(conn, now) &&
1657 global_relayed_write_bucket <= global_write_bucket)
1658 global_bucket = global_relayed_write_bucket;
1660 return connection_bucket_round_robin(base, priority, global_bucket,
1661 conn->outbuf_flushlen);
1664 /** Return 1 if the global write buckets are low enough that we
1665 * shouldn't send <b>attempt</b> bytes of low-priority directory stuff
1666 * out to <b>conn</b>. Else return 0.
1668 * Priority is 1 for v1 requests (directories and running-routers),
1669 * and 2 for v2 requests (statuses and descriptors). But see FFFF in
1670 * directory_handle_command_get() for why we don't use priority 2 yet.
1672 * There are a lot of parameters we could use here:
1673 * - global_relayed_write_bucket. Low is bad.
1674 * - global_write_bucket. Low is bad.
1675 * - bandwidthrate. Low is bad.
1676 * - bandwidthburst. Not a big factor?
1677 * - attempt. High is bad.
1678 * - total bytes queued on outbufs. High is bad. But I'm wary of
1679 * using this, since a few slow-flushing queues will pump up the
1680 * number without meaning what we meant to mean. What we really
1681 * mean is "total directory bytes added to outbufs recently", but
1682 * that's harder to quantify and harder to keep track of.
1685 global_write_bucket_low(connection_t *conn, size_t attempt, int priority)
1687 int smaller_bucket = global_write_bucket < global_relayed_write_bucket ?
1688 global_write_bucket : global_relayed_write_bucket;
1689 if (authdir_mode(get_options()) && priority>1)
1690 return 0; /* there's always room to answer v2 if we're an auth dir */
1692 if (!connection_is_rate_limited(conn))
1693 return 0; /* local conns don't get limited */
1695 if (smaller_bucket < (int)attempt)
1696 return 1; /* not enough space no matter the priority */
1698 if (write_buckets_empty_last_second)
1699 return 1; /* we're already hitting our limits, no more please */
1701 if (priority == 1) { /* old-style v1 query */
1702 /* Could we handle *two* of these requests within the next two seconds? */
1703 or_options_t *options = get_options();
1704 int64_t can_write = (int64_t)smaller_bucket
1705 + 2*(options->RelayBandwidthRate ? options->RelayBandwidthRate :
1706 options->BandwidthRate);
1707 if (can_write < 2*(int64_t)attempt)
1708 return 1;
1709 } else { /* v2 query */
1710 /* no further constraints yet */
1712 return 0;
1715 /** We just read num_read and wrote num_written onto conn.
1716 * Decrement buckets appropriately. */
1717 static void
1718 connection_buckets_decrement(connection_t *conn, time_t now,
1719 size_t num_read, size_t num_written)
1721 if (!connection_is_rate_limited(conn))
1722 return; /* local IPs are free */
1723 if (num_written >= INT_MAX || num_read >= INT_MAX) {
1724 log_err(LD_BUG, "Value out of range. num_read=%lu, num_written=%lu, "
1725 "connection type=%s, state=%s",
1726 (unsigned long)num_read, (unsigned long)num_written,
1727 conn_type_to_string(conn->type),
1728 conn_state_to_string(conn->type, conn->state));
1729 if (num_written >= INT_MAX) num_written = 1;
1730 if (num_read >= INT_MAX) num_read = 1;
1731 tor_fragile_assert();
1734 if (num_read > 0)
1735 rep_hist_note_bytes_read(num_read, now);
1736 if (num_written > 0)
1737 rep_hist_note_bytes_written(num_written, now);
1739 if (connection_counts_as_relayed_traffic(conn, now)) {
1740 global_relayed_read_bucket -= (int)num_read;
1741 global_relayed_write_bucket -= (int)num_written;
1743 global_read_bucket -= (int)num_read;
1744 global_write_bucket -= (int)num_written;
1745 if (connection_speaks_cells(conn) && conn->state == OR_CONN_STATE_OPEN)
1746 TO_OR_CONN(conn)->read_bucket -= (int)num_read;
1749 /** If we have exhausted our global buckets, or the buckets for conn,
1750 * stop reading. */
1751 static void
1752 connection_consider_empty_read_buckets(connection_t *conn)
1754 const char *reason;
1756 if (global_read_bucket <= 0) {
1757 reason = "global read bucket exhausted. Pausing.";
1758 } else if (connection_counts_as_relayed_traffic(conn, approx_time()) &&
1759 global_relayed_read_bucket <= 0) {
1760 reason = "global relayed read bucket exhausted. Pausing.";
1761 } else if (connection_speaks_cells(conn) &&
1762 conn->state == OR_CONN_STATE_OPEN &&
1763 TO_OR_CONN(conn)->read_bucket <= 0) {
1764 reason = "connection read bucket exhausted. Pausing.";
1765 } else
1766 return; /* all good, no need to stop it */
1768 LOG_FN_CONN(conn, (LOG_DEBUG, LD_NET, "%s", reason));
1769 conn->read_blocked_on_bw = 1;
1770 connection_stop_reading(conn);
1773 /** If we have exhausted our global buckets, or the buckets for conn,
1774 * stop writing. */
1775 static void
1776 connection_consider_empty_write_buckets(connection_t *conn)
1778 const char *reason;
1780 if (global_write_bucket <= 0) {
1781 reason = "global write bucket exhausted. Pausing.";
1782 } else if (connection_counts_as_relayed_traffic(conn, approx_time()) &&
1783 global_relayed_write_bucket <= 0) {
1784 reason = "global relayed write bucket exhausted. Pausing.";
1785 #if 0
1786 } else if (connection_speaks_cells(conn) &&
1787 conn->state == OR_CONN_STATE_OPEN &&
1788 TO_OR_CONN(conn)->write_bucket <= 0) {
1789 reason = "connection write bucket exhausted. Pausing.";
1790 #endif
1791 } else
1792 return; /* all good, no need to stop it */
1794 LOG_FN_CONN(conn, (LOG_DEBUG, LD_NET, "%s", reason));
1795 conn->write_blocked_on_bw = 1;
1796 connection_stop_writing(conn);
1799 /** Initialize the global read bucket to options-\>BandwidthBurst. */
1800 void
1801 connection_bucket_init(void)
1803 or_options_t *options = get_options();
1804 /* start it at max traffic */
1805 global_read_bucket = (int)options->BandwidthBurst;
1806 global_write_bucket = (int)options->BandwidthBurst;
1807 if (options->RelayBandwidthRate) {
1808 global_relayed_read_bucket = (int)options->RelayBandwidthBurst;
1809 global_relayed_write_bucket = (int)options->RelayBandwidthBurst;
1810 } else {
1811 global_relayed_read_bucket = (int)options->BandwidthBurst;
1812 global_relayed_write_bucket = (int)options->BandwidthBurst;
1816 /** Refill a single <b>bucket</b> called <b>name</b> with bandwith rate
1817 * <b>rate</b> and bandwidth burst <b>burst</b>, assuming that
1818 * <b>seconds_elapsed</b> seconds have passed since the last call.
1820 static void
1821 connection_bucket_refill_helper(int *bucket, int rate, int burst,
1822 int seconds_elapsed, const char *name)
1824 int starting_bucket = *bucket;
1825 if (starting_bucket < burst && seconds_elapsed) {
1826 if (((burst - starting_bucket)/seconds_elapsed) < rate) {
1827 *bucket = burst; /* We would overflow the bucket; just set it to
1828 * the maximum. */
1829 } else {
1830 int incr = rate*seconds_elapsed;
1831 *bucket += incr;
1832 if (*bucket > burst || *bucket < starting_bucket) {
1833 /* If we overflow the burst, or underflow our starting bucket,
1834 * cap the bucket value to burst. */
1835 /* XXXX this might be redundant now, but it doesn't show up
1836 * in profiles. Remove it after analysis. */
1837 *bucket = burst;
1840 log(LOG_DEBUG, LD_NET,"%s now %d.", name, *bucket);
1844 /** A second has rolled over; increment buckets appropriately. */
1845 void
1846 connection_bucket_refill(int seconds_elapsed, time_t now)
1848 or_options_t *options = get_options();
1849 smartlist_t *conns = get_connection_array();
1850 int relayrate, relayburst;
1852 if (options->RelayBandwidthRate) {
1853 relayrate = (int)options->RelayBandwidthRate;
1854 relayburst = (int)options->RelayBandwidthBurst;
1855 } else {
1856 relayrate = (int)options->BandwidthRate;
1857 relayburst = (int)options->BandwidthBurst;
1860 tor_assert(seconds_elapsed >= 0);
1862 write_buckets_empty_last_second =
1863 global_relayed_write_bucket <= 0 || global_write_bucket <= 0;
1865 /* refill the global buckets */
1866 connection_bucket_refill_helper(&global_read_bucket,
1867 (int)options->BandwidthRate,
1868 (int)options->BandwidthBurst,
1869 seconds_elapsed, "global_read_bucket");
1870 connection_bucket_refill_helper(&global_write_bucket,
1871 (int)options->BandwidthRate,
1872 (int)options->BandwidthBurst,
1873 seconds_elapsed, "global_write_bucket");
1874 connection_bucket_refill_helper(&global_relayed_read_bucket,
1875 relayrate, relayburst, seconds_elapsed,
1876 "global_relayed_read_bucket");
1877 connection_bucket_refill_helper(&global_relayed_write_bucket,
1878 relayrate, relayburst, seconds_elapsed,
1879 "global_relayed_write_bucket");
1881 /* refill the per-connection buckets */
1882 SMARTLIST_FOREACH(conns, connection_t *, conn,
1884 if (connection_speaks_cells(conn)) {
1885 or_connection_t *or_conn = TO_OR_CONN(conn);
1886 if (connection_read_bucket_should_increase(or_conn)) {
1887 connection_bucket_refill_helper(&or_conn->read_bucket,
1888 or_conn->bandwidthrate,
1889 or_conn->bandwidthburst,
1890 seconds_elapsed,
1891 "or_conn->read_bucket");
1892 //log_fn(LOG_DEBUG,"Receiver bucket %d now %d.", i,
1893 // conn->read_bucket);
1897 if (conn->read_blocked_on_bw == 1 /* marked to turn reading back on now */
1898 && global_read_bucket > 0 /* and we're allowed to read */
1899 && (!connection_counts_as_relayed_traffic(conn, now) ||
1900 global_relayed_read_bucket > 0) /* even if we're relayed traffic */
1901 && (!connection_speaks_cells(conn) ||
1902 conn->state != OR_CONN_STATE_OPEN ||
1903 TO_OR_CONN(conn)->read_bucket > 0)) {
1904 /* and either a non-cell conn or a cell conn with non-empty bucket */
1905 LOG_FN_CONN(conn, (LOG_DEBUG,LD_NET,
1906 "waking up conn (fd %d) for read", conn->s));
1907 conn->read_blocked_on_bw = 0;
1908 connection_start_reading(conn);
1911 if (conn->write_blocked_on_bw == 1
1912 && global_write_bucket > 0 /* and we're allowed to write */
1913 && (!connection_counts_as_relayed_traffic(conn, now) ||
1914 global_relayed_write_bucket > 0)) {
1915 /* even if we're relayed traffic */
1916 LOG_FN_CONN(conn, (LOG_DEBUG,LD_NET,
1917 "waking up conn (fd %d) for write", conn->s));
1918 conn->write_blocked_on_bw = 0;
1919 connection_start_writing(conn);
1924 /** Is the receiver bucket for connection <b>conn</b> low enough that we
1925 * should add another pile of tokens to it?
1927 static int
1928 connection_read_bucket_should_increase(or_connection_t *conn)
1930 tor_assert(conn);
1932 if (conn->_base.state != OR_CONN_STATE_OPEN)
1933 return 0; /* only open connections play the rate limiting game */
1934 if (conn->read_bucket >= conn->bandwidthburst)
1935 return 0;
1937 return 1;
1940 /** Read bytes from conn-\>s and process them.
1942 * This function gets called from conn_read() in main.c, either
1943 * when poll() has declared that conn wants to read, or (for OR conns)
1944 * when there are pending TLS bytes.
1946 * It calls connection_read_to_buf() to bring in any new bytes,
1947 * and then calls connection_process_inbuf() to process them.
1949 * Mark the connection and return -1 if you want to close it, else
1950 * return 0.
1953 connection_handle_read(connection_t *conn)
1955 int max_to_read=-1, try_to_read;
1956 size_t before, n_read = 0;
1957 int socket_error = 0;
1959 if (conn->marked_for_close)
1960 return 0; /* do nothing */
1962 conn->timestamp_lastread = approx_time();
1964 switch (conn->type) {
1965 case CONN_TYPE_OR_LISTENER:
1966 return connection_handle_listener_read(conn, CONN_TYPE_OR);
1967 case CONN_TYPE_AP_LISTENER:
1968 case CONN_TYPE_AP_TRANS_LISTENER:
1969 case CONN_TYPE_AP_NATD_LISTENER:
1970 return connection_handle_listener_read(conn, CONN_TYPE_AP);
1971 case CONN_TYPE_DIR_LISTENER:
1972 return connection_handle_listener_read(conn, CONN_TYPE_DIR);
1973 case CONN_TYPE_CONTROL_LISTENER:
1974 return connection_handle_listener_read(conn, CONN_TYPE_CONTROL);
1975 case CONN_TYPE_AP_DNS_LISTENER:
1976 /* This should never happen; eventdns.c handles the reads here. */
1977 tor_fragile_assert();
1978 return 0;
1981 loop_again:
1982 try_to_read = max_to_read;
1983 tor_assert(!conn->marked_for_close);
1985 before = buf_datalen(conn->inbuf);
1986 if (connection_read_to_buf(conn, &max_to_read, &socket_error) < 0) {
1987 /* There's a read error; kill the connection.*/
1988 if (conn->type == CONN_TYPE_OR &&
1989 conn->state == OR_CONN_STATE_CONNECTING) {
1990 connection_or_connect_failed(TO_OR_CONN(conn),
1991 errno_to_orconn_end_reason(socket_error),
1992 tor_socket_strerror(socket_error));
1994 if (CONN_IS_EDGE(conn)) {
1995 edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
1996 connection_edge_end_errno(edge_conn);
1997 if (edge_conn->socks_request) /* broken, don't send a socks reply back */
1998 edge_conn->socks_request->has_finished = 1;
2000 connection_close_immediate(conn); /* Don't flush; connection is dead. */
2001 connection_mark_for_close(conn);
2002 return -1;
2004 n_read += buf_datalen(conn->inbuf) - before;
2005 if (CONN_IS_EDGE(conn) && try_to_read != max_to_read) {
2006 /* instruct it not to try to package partial cells. */
2007 if (connection_process_inbuf(conn, 0) < 0) {
2008 return -1;
2010 if (!conn->marked_for_close &&
2011 connection_is_reading(conn) &&
2012 !conn->inbuf_reached_eof &&
2013 max_to_read > 0)
2014 goto loop_again; /* try reading again, in case more is here now */
2016 /* one last try, packaging partial cells and all. */
2017 if (!conn->marked_for_close &&
2018 connection_process_inbuf(conn, 1) < 0) {
2019 return -1;
2021 if (conn->linked_conn) {
2022 /* The other side's handle_write will never actually get called, so
2023 * we need to invoke the appropriate callbacks ourself. */
2024 connection_t *linked = conn->linked_conn;
2026 if (n_read) {
2027 /* Probably a no-op, but hey. */
2028 connection_buckets_decrement(linked, approx_time(), 0, n_read);
2030 if (connection_flushed_some(linked) < 0)
2031 connection_mark_for_close(linked);
2032 if (!connection_wants_to_flush(linked))
2033 connection_finished_flushing(linked);
2036 if (!buf_datalen(linked->outbuf) && conn->active_on_link)
2037 connection_stop_reading_from_linked_conn(conn);
2039 /* If we hit the EOF, call connection_reached_eof. */
2040 if (!conn->marked_for_close &&
2041 conn->inbuf_reached_eof &&
2042 connection_reached_eof(conn) < 0) {
2043 return -1;
2045 return 0;
2048 /** Pull in new bytes from conn-\>s or conn-\>linked_conn onto conn-\>inbuf,
2049 * either directly or via TLS. Reduce the token buckets by the number of bytes
2050 * read.
2052 * If *max_to_read is -1, then decide it ourselves, else go with the
2053 * value passed to us. When returning, if it's changed, subtract the
2054 * number of bytes we read from *max_to_read.
2056 * Return -1 if we want to break conn, else return 0.
2058 static int
2059 connection_read_to_buf(connection_t *conn, int *max_to_read, int *socket_error)
2061 int result;
2062 ssize_t at_most = *max_to_read;
2063 size_t slack_in_buf, more_to_read;
2064 size_t n_read = 0, n_written = 0;
2066 if (at_most == -1) { /* we need to initialize it */
2067 /* how many bytes are we allowed to read? */
2068 at_most = connection_bucket_read_limit(conn, approx_time());
2071 slack_in_buf = buf_slack(conn->inbuf);
2072 again:
2073 if ((size_t)at_most > slack_in_buf && slack_in_buf >= 1024) {
2074 more_to_read = at_most - slack_in_buf;
2075 at_most = slack_in_buf;
2076 } else {
2077 more_to_read = 0;
2080 if (connection_speaks_cells(conn) &&
2081 conn->state > OR_CONN_STATE_PROXY_READING) {
2082 int pending;
2083 or_connection_t *or_conn = TO_OR_CONN(conn);
2084 size_t initial_size;
2085 if (conn->state == OR_CONN_STATE_TLS_HANDSHAKING ||
2086 conn->state == OR_CONN_STATE_TLS_CLIENT_RENEGOTIATING) {
2087 /* continue handshaking even if global token bucket is empty */
2088 return connection_tls_continue_handshake(or_conn);
2091 log_debug(LD_NET,
2092 "%d: starting, inbuf_datalen %ld (%d pending in tls object)."
2093 " at_most %ld.",
2094 conn->s,(long)buf_datalen(conn->inbuf),
2095 tor_tls_get_pending_bytes(or_conn->tls), (long)at_most);
2097 initial_size = buf_datalen(conn->inbuf);
2098 /* else open, or closing */
2099 result = read_to_buf_tls(or_conn->tls, at_most, conn->inbuf);
2100 if (TOR_TLS_IS_ERROR(result) || result == TOR_TLS_CLOSE)
2101 or_conn->tls_error = result;
2102 else
2103 or_conn->tls_error = 0;
2105 switch (result) {
2106 case TOR_TLS_CLOSE:
2107 case TOR_TLS_ERROR_IO:
2108 log_debug(LD_NET,"TLS connection closed %son read. Closing. "
2109 "(Nickname %s, address %s)",
2110 result == TOR_TLS_CLOSE ? "cleanly " : "",
2111 or_conn->nickname ? or_conn->nickname : "not set",
2112 conn->address);
2113 return result;
2114 CASE_TOR_TLS_ERROR_ANY_NONIO:
2115 log_debug(LD_NET,"tls error [%s]. breaking (nickname %s, address %s).",
2116 tor_tls_err_to_string(result),
2117 or_conn->nickname ? or_conn->nickname : "not set",
2118 conn->address);
2119 return result;
2120 case TOR_TLS_WANTWRITE:
2121 connection_start_writing(conn);
2122 return 0;
2123 case TOR_TLS_WANTREAD: /* we're already reading */
2124 case TOR_TLS_DONE: /* no data read, so nothing to process */
2125 result = 0;
2126 break; /* so we call bucket_decrement below */
2127 default:
2128 break;
2130 pending = tor_tls_get_pending_bytes(or_conn->tls);
2131 if (pending) {
2132 /* If we have any pending bytes, we read them now. This *can*
2133 * take us over our read allotment, but really we shouldn't be
2134 * believing that SSL bytes are the same as TCP bytes anyway. */
2135 int r2 = read_to_buf_tls(or_conn->tls, pending, conn->inbuf);
2136 if (r2<0) {
2137 log_warn(LD_BUG, "apparently, reading pending bytes can fail.");
2138 return -1;
2141 result = (int)(buf_datalen(conn->inbuf)-initial_size);
2142 tor_tls_get_n_raw_bytes(or_conn->tls, &n_read, &n_written);
2143 log_debug(LD_GENERAL, "After TLS read of %d: %ld read, %ld written",
2144 result, (long)n_read, (long)n_written);
2145 } else if (conn->linked) {
2146 if (conn->linked_conn) {
2147 result = move_buf_to_buf(conn->inbuf, conn->linked_conn->outbuf,
2148 &conn->linked_conn->outbuf_flushlen);
2149 } else {
2150 result = 0;
2152 //log_notice(LD_GENERAL, "Moved %d bytes on an internal link!", result);
2153 /* If the other side has disappeared, or if it's been marked for close and
2154 * we flushed its outbuf, then we should set our inbuf_reached_eof. */
2155 if (!conn->linked_conn ||
2156 (conn->linked_conn->marked_for_close &&
2157 buf_datalen(conn->linked_conn->outbuf) == 0))
2158 conn->inbuf_reached_eof = 1;
2160 n_read = (size_t) result;
2161 } else {
2162 /* !connection_speaks_cells, !conn->linked_conn. */
2163 int reached_eof = 0;
2164 CONN_LOG_PROTECT(conn,
2165 result = read_to_buf(conn->s, at_most, conn->inbuf, &reached_eof,
2166 socket_error));
2167 if (reached_eof)
2168 conn->inbuf_reached_eof = 1;
2170 // log_fn(LOG_DEBUG,"read_to_buf returned %d.",read_result);
2172 if (result < 0)
2173 return -1;
2174 n_read = (size_t) result;
2177 if (n_read > 0) { /* change *max_to_read */
2178 /*XXXX021 check for overflow*/
2179 *max_to_read = (int)(at_most - n_read);
2182 if (conn->type == CONN_TYPE_AP) {
2183 edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
2184 /*XXXX021 check for overflow*/
2185 edge_conn->n_read += (int)n_read;
2188 connection_buckets_decrement(conn, approx_time(), n_read, n_written);
2190 if (more_to_read && result == at_most) {
2191 slack_in_buf = buf_slack(conn->inbuf);
2192 at_most = more_to_read;
2193 goto again;
2196 /* Call even if result is 0, since the global read bucket may
2197 * have reached 0 on a different conn, and this guy needs to
2198 * know to stop reading. */
2199 connection_consider_empty_read_buckets(conn);
2200 if (n_written > 0 && connection_is_writing(conn))
2201 connection_consider_empty_write_buckets(conn);
2203 return 0;
2206 /** A pass-through to fetch_from_buf. */
2208 connection_fetch_from_buf(char *string, size_t len, connection_t *conn)
2210 return fetch_from_buf(string, len, conn->inbuf);
2213 /** Return conn-\>outbuf_flushlen: how many bytes conn wants to flush
2214 * from its outbuf. */
2216 connection_wants_to_flush(connection_t *conn)
2218 return conn->outbuf_flushlen > 0;
2221 /** Are there too many bytes on edge connection <b>conn</b>'s outbuf to
2222 * send back a relay-level sendme yet? Return 1 if so, 0 if not. Used by
2223 * connection_edge_consider_sending_sendme().
2226 connection_outbuf_too_full(connection_t *conn)
2228 return (conn->outbuf_flushlen > 10*CELL_PAYLOAD_SIZE);
2231 /** Try to flush more bytes onto conn-\>s.
2233 * This function gets called either from conn_write() in main.c
2234 * when poll() has declared that conn wants to write, or below
2235 * from connection_write_to_buf() when an entire TLS record is ready.
2237 * Update conn-\>timestamp_lastwritten to now, and call flush_buf
2238 * or flush_buf_tls appropriately. If it succeeds and there are no more
2239 * more bytes on conn->outbuf, then call connection_finished_flushing
2240 * on it too.
2242 * If <b>force</b>, then write as many bytes as possible, ignoring bandwidth
2243 * limits. (Used for flushing messages to controller connections on fatal
2244 * errors.)
2246 * Mark the connection and return -1 if you want to close it, else
2247 * return 0.
2250 connection_handle_write(connection_t *conn, int force)
2252 int e;
2253 socklen_t len=(socklen_t)sizeof(e);
2254 int result;
2255 ssize_t max_to_write;
2256 time_t now = approx_time();
2257 size_t n_read = 0, n_written = 0;
2259 tor_assert(!connection_is_listener(conn));
2261 if (conn->marked_for_close || conn->s < 0)
2262 return 0; /* do nothing */
2264 if (conn->in_flushed_some) {
2265 log_warn(LD_BUG, "called recursively from inside conn->in_flushed_some()");
2266 return 0;
2269 conn->timestamp_lastwritten = now;
2271 /* Sometimes, "writable" means "connected". */
2272 if (connection_state_is_connecting(conn)) {
2273 if (getsockopt(conn->s, SOL_SOCKET, SO_ERROR, (void*)&e, &len) < 0) {
2274 log_warn(LD_BUG,
2275 "getsockopt() syscall failed?! Please report to tor-ops.");
2276 if (CONN_IS_EDGE(conn))
2277 connection_edge_end_errno(TO_EDGE_CONN(conn));
2278 connection_mark_for_close(conn);
2279 return -1;
2281 if (e) {
2282 /* some sort of error, but maybe just inprogress still */
2283 if (!ERRNO_IS_CONN_EINPROGRESS(e)) {
2284 log_info(LD_NET,"in-progress connect failed. Removing. (%s)",
2285 tor_socket_strerror(e));
2286 if (CONN_IS_EDGE(conn))
2287 connection_edge_end_errno(TO_EDGE_CONN(conn));
2288 if (conn->type == CONN_TYPE_OR)
2289 connection_or_connect_failed(TO_OR_CONN(conn),
2290 errno_to_orconn_end_reason(e),
2291 tor_socket_strerror(e));
2293 connection_close_immediate(conn);
2294 connection_mark_for_close(conn);
2295 return -1;
2296 } else {
2297 return 0; /* no change, see if next time is better */
2300 /* The connection is successful. */
2301 if (connection_finished_connecting(conn)<0)
2302 return -1;
2305 max_to_write = force ? (ssize_t)conn->outbuf_flushlen
2306 : connection_bucket_write_limit(conn, now);
2308 if (connection_speaks_cells(conn) &&
2309 conn->state > OR_CONN_STATE_PROXY_READING) {
2310 or_connection_t *or_conn = TO_OR_CONN(conn);
2311 if (conn->state == OR_CONN_STATE_TLS_HANDSHAKING ||
2312 conn->state == OR_CONN_STATE_TLS_CLIENT_RENEGOTIATING) {
2313 connection_stop_writing(conn);
2314 if (connection_tls_continue_handshake(or_conn) < 0) {
2315 /* Don't flush; connection is dead. */
2316 connection_close_immediate(conn);
2317 connection_mark_for_close(conn);
2318 return -1;
2320 return 0;
2321 } else if (conn->state == OR_CONN_STATE_TLS_SERVER_RENEGOTIATING) {
2322 return connection_handle_read(conn);
2325 /* else open, or closing */
2326 result = flush_buf_tls(or_conn->tls, conn->outbuf,
2327 max_to_write, &conn->outbuf_flushlen);
2328 switch (result) {
2329 CASE_TOR_TLS_ERROR_ANY:
2330 case TOR_TLS_CLOSE:
2331 log_info(LD_NET,result!=TOR_TLS_CLOSE?
2332 "tls error. breaking.":"TLS connection closed on flush");
2333 /* Don't flush; connection is dead. */
2334 connection_close_immediate(conn);
2335 connection_mark_for_close(conn);
2336 return -1;
2337 case TOR_TLS_WANTWRITE:
2338 log_debug(LD_NET,"wanted write.");
2339 /* we're already writing */
2340 return 0;
2341 case TOR_TLS_WANTREAD:
2342 /* Make sure to avoid a loop if the receive buckets are empty. */
2343 log_debug(LD_NET,"wanted read.");
2344 if (!connection_is_reading(conn)) {
2345 connection_stop_writing(conn);
2346 conn->write_blocked_on_bw = 1;
2347 /* we'll start reading again when the next second arrives,
2348 * and then also start writing again.
2351 /* else no problem, we're already reading */
2352 return 0;
2353 /* case TOR_TLS_DONE:
2354 * for TOR_TLS_DONE, fall through to check if the flushlen
2355 * is empty, so we can stop writing.
2359 tor_tls_get_n_raw_bytes(or_conn->tls, &n_read, &n_written);
2360 log_debug(LD_GENERAL, "After TLS write of %d: %ld read, %ld written",
2361 result, (long)n_read, (long)n_written);
2362 } else {
2363 CONN_LOG_PROTECT(conn,
2364 result = flush_buf(conn->s, conn->outbuf,
2365 max_to_write, &conn->outbuf_flushlen));
2366 if (result < 0) {
2367 if (CONN_IS_EDGE(conn))
2368 connection_edge_end_errno(TO_EDGE_CONN(conn));
2370 connection_close_immediate(conn); /* Don't flush; connection is dead. */
2371 connection_mark_for_close(conn);
2372 return -1;
2374 n_written = (size_t) result;
2377 if (conn->type == CONN_TYPE_AP) {
2378 edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
2379 /*XXXX021 check for overflow.*/
2380 edge_conn->n_written += (int)n_written;
2383 connection_buckets_decrement(conn, approx_time(), n_read, n_written);
2385 if (result > 0) {
2386 /* If we wrote any bytes from our buffer, then call the appropriate
2387 * functions. */
2388 if (connection_flushed_some(conn) < 0)
2389 connection_mark_for_close(conn);
2392 if (!connection_wants_to_flush(conn)) { /* it's done flushing */
2393 if (connection_finished_flushing(conn) < 0) {
2394 /* already marked */
2395 return -1;
2397 return 0;
2400 /* Call even if result is 0, since the global write bucket may
2401 * have reached 0 on a different conn, and this guy needs to
2402 * know to stop writing. */
2403 connection_consider_empty_write_buckets(conn);
2404 if (n_read > 0 && connection_is_reading(conn))
2405 connection_consider_empty_read_buckets(conn);
2407 return 0;
2410 /** OpenSSL TLS record size is 16383; this is close. The goal here is to
2411 * push data out as soon as we know there's enough for a TLS record, so
2412 * during periods of high load we won't read entire megabytes from
2413 * input before pushing any data out. It also has the feature of not
2414 * growing huge outbufs unless something is slow. */
2415 #define MIN_TLS_FLUSHLEN 15872
2417 /** Append <b>len</b> bytes of <b>string</b> onto <b>conn</b>'s
2418 * outbuf, and ask it to start writing.
2420 * If <b>zlib</b> is nonzero, this is a directory connection that should get
2421 * its contents compressed or decompressed as they're written. If zlib is
2422 * negative, this is the last data to be compressed, and the connection's zlib
2423 * state should be flushed.
2425 * If it's an OR conn and an entire TLS record is ready, then try to
2426 * flush the record now. Similarly, if it's a local control connection
2427 * and a 64k chunk is ready, try to flush it all, so we don't end up with
2428 * many megabytes of controller info queued at once.
2430 void
2431 _connection_write_to_buf_impl(const char *string, size_t len,
2432 connection_t *conn, int zlib)
2434 /* XXXX This function really needs to return -1 on failure. */
2435 int r;
2436 size_t old_datalen;
2437 if (!len && !(zlib<0))
2438 return;
2439 /* if it's marked for close, only allow write if we mean to flush it */
2440 if (conn->marked_for_close && !conn->hold_open_until_flushed)
2441 return;
2443 old_datalen = buf_datalen(conn->outbuf);
2444 if (zlib) {
2445 dir_connection_t *dir_conn = TO_DIR_CONN(conn);
2446 int done = zlib < 0;
2447 CONN_LOG_PROTECT(conn, r = write_to_buf_zlib(conn->outbuf,
2448 dir_conn->zlib_state,
2449 string, len, done));
2450 } else {
2451 CONN_LOG_PROTECT(conn, r = write_to_buf(string, len, conn->outbuf));
2453 if (r < 0) {
2454 if (CONN_IS_EDGE(conn)) {
2455 /* if it failed, it means we have our package/delivery windows set
2456 wrong compared to our max outbuf size. close the whole circuit. */
2457 log_warn(LD_NET,
2458 "write_to_buf failed. Closing circuit (fd %d).", conn->s);
2459 circuit_mark_for_close(circuit_get_by_edge_conn(TO_EDGE_CONN(conn)),
2460 END_CIRC_REASON_INTERNAL);
2461 } else {
2462 log_warn(LD_NET,
2463 "write_to_buf failed. Closing connection (fd %d).", conn->s);
2464 connection_mark_for_close(conn);
2466 return;
2469 connection_start_writing(conn);
2470 if (zlib) {
2471 conn->outbuf_flushlen += buf_datalen(conn->outbuf) - old_datalen;
2472 } else {
2473 ssize_t extra = 0;
2474 conn->outbuf_flushlen += len;
2476 /* Should we try flushing the outbuf now? */
2477 if (conn->in_flushed_some) {
2478 /* Don't flush the outbuf when the reason we're writing more stuff is
2479 * _because_ we flushed the outbuf. That's unfair. */
2480 return;
2483 if (conn->type == CONN_TYPE_OR &&
2484 conn->outbuf_flushlen-len < MIN_TLS_FLUSHLEN &&
2485 conn->outbuf_flushlen >= MIN_TLS_FLUSHLEN) {
2486 /* We just pushed outbuf_flushlen to MIN_TLS_FLUSHLEN or above;
2487 * we can send out a full TLS frame now if we like. */
2488 extra = conn->outbuf_flushlen - MIN_TLS_FLUSHLEN;
2489 conn->outbuf_flushlen = MIN_TLS_FLUSHLEN;
2490 } else if (conn->type == CONN_TYPE_CONTROL &&
2491 !connection_is_rate_limited(conn) &&
2492 conn->outbuf_flushlen-len < 1<<16 &&
2493 conn->outbuf_flushlen >= 1<<16) {
2494 /* just try to flush all of it */
2495 } else
2496 return; /* no need to try flushing */
2498 if (connection_handle_write(conn, 0) < 0) {
2499 if (!conn->marked_for_close) {
2500 /* this connection is broken. remove it. */
2501 log_warn(LD_BUG, "unhandled error on write for "
2502 "conn (type %d, fd %d); removing",
2503 conn->type, conn->s);
2504 tor_fragile_assert();
2505 /* do a close-immediate here, so we don't try to flush */
2506 connection_close_immediate(conn);
2508 return;
2510 if (extra) {
2511 conn->outbuf_flushlen += extra;
2512 connection_start_writing(conn);
2517 /** Return a connection with given type, address, port, and purpose;
2518 * or NULL if no such connection exists. */
2519 connection_t *
2520 connection_get_by_type_addr_port_purpose(int type,
2521 const tor_addr_t *addr, uint16_t port,
2522 int purpose)
2524 smartlist_t *conns = get_connection_array();
2525 SMARTLIST_FOREACH(conns, connection_t *, conn,
2527 if (conn->type == type &&
2528 tor_addr_eq(&conn->addr, addr) &&
2529 conn->port == port &&
2530 conn->purpose == purpose &&
2531 !conn->marked_for_close)
2532 return conn;
2534 return NULL;
2537 /** Return the stream with id <b>id</b> if it is not already marked for
2538 * close.
2540 connection_t *
2541 connection_get_by_global_id(uint64_t id)
2543 smartlist_t *conns = get_connection_array();
2544 SMARTLIST_FOREACH(conns, connection_t *, conn,
2546 if (conn->global_identifier == id)
2547 return conn;
2549 return NULL;
2552 /** Return a connection of type <b>type</b> that is not marked for close.
2554 connection_t *
2555 connection_get_by_type(int type)
2557 smartlist_t *conns = get_connection_array();
2558 SMARTLIST_FOREACH(conns, connection_t *, conn,
2560 if (conn->type == type && !conn->marked_for_close)
2561 return conn;
2563 return NULL;
2566 /** Return a connection of type <b>type</b> that is in state <b>state</b>,
2567 * and that is not marked for close.
2569 connection_t *
2570 connection_get_by_type_state(int type, int state)
2572 smartlist_t *conns = get_connection_array();
2573 SMARTLIST_FOREACH(conns, connection_t *, conn,
2575 if (conn->type == type && conn->state == state && !conn->marked_for_close)
2576 return conn;
2578 return NULL;
2581 /** Return a connection of type <b>type</b> that has rendquery equal
2582 * to <b>rendquery</b>, and that is not marked for close. If state
2583 * is non-zero, conn must be of that state too. If rendversion is
2584 * nonnegative, conn must be fetching that rendversion, too.
2586 connection_t *
2587 connection_get_by_type_state_rendquery(int type, int state,
2588 const char *rendquery,
2589 int rendversion)
2591 smartlist_t *conns = get_connection_array();
2593 tor_assert(type == CONN_TYPE_DIR ||
2594 type == CONN_TYPE_AP || type == CONN_TYPE_EXIT);
2595 tor_assert(rendquery);
2597 SMARTLIST_FOREACH(conns, connection_t *, conn,
2599 if (conn->type == type &&
2600 !conn->marked_for_close &&
2601 (!state || state == conn->state)) {
2602 if (type == CONN_TYPE_DIR &&
2603 TO_DIR_CONN(conn)->rend_data &&
2604 (rendversion < 0 ||
2605 rendversion == TO_DIR_CONN(conn)->rend_data->rend_desc_version) &&
2606 !rend_cmp_service_ids(rendquery,
2607 TO_DIR_CONN(conn)->rend_data->onion_address))
2608 return conn;
2609 else if (CONN_IS_EDGE(conn) &&
2610 TO_EDGE_CONN(conn)->rend_data &&
2611 !rend_cmp_service_ids(rendquery,
2612 TO_EDGE_CONN(conn)->rend_data->onion_address))
2613 return conn;
2616 return NULL;
2619 /** Return an open, non-marked connection of a given type and purpose, or NULL
2620 * if no such connection exists. */
2621 connection_t *
2622 connection_get_by_type_purpose(int type, int purpose)
2624 smartlist_t *conns = get_connection_array();
2625 SMARTLIST_FOREACH(conns, connection_t *, conn,
2627 if (conn->type == type &&
2628 !conn->marked_for_close &&
2629 (purpose == conn->purpose))
2630 return conn;
2632 return NULL;
2635 /** Return 1 if <b>conn</b> is a listener conn, else return 0. */
2637 connection_is_listener(connection_t *conn)
2639 if (conn->type == CONN_TYPE_OR_LISTENER ||
2640 conn->type == CONN_TYPE_AP_LISTENER ||
2641 conn->type == CONN_TYPE_AP_TRANS_LISTENER ||
2642 conn->type == CONN_TYPE_AP_DNS_LISTENER ||
2643 conn->type == CONN_TYPE_AP_NATD_LISTENER ||
2644 conn->type == CONN_TYPE_DIR_LISTENER ||
2645 conn->type == CONN_TYPE_CONTROL_LISTENER)
2646 return 1;
2647 return 0;
2650 /** Return 1 if <b>conn</b> is in state "open" and is not marked
2651 * for close, else return 0.
2654 connection_state_is_open(connection_t *conn)
2656 tor_assert(conn);
2658 if (conn->marked_for_close)
2659 return 0;
2661 if ((conn->type == CONN_TYPE_OR && conn->state == OR_CONN_STATE_OPEN) ||
2662 (conn->type == CONN_TYPE_AP && conn->state == AP_CONN_STATE_OPEN) ||
2663 (conn->type == CONN_TYPE_EXIT && conn->state == EXIT_CONN_STATE_OPEN) ||
2664 (conn->type == CONN_TYPE_CONTROL &&
2665 conn->state == CONTROL_CONN_STATE_OPEN))
2666 return 1;
2668 return 0;
2671 /** Return 1 if conn is in 'connecting' state, else return 0. */
2673 connection_state_is_connecting(connection_t *conn)
2675 tor_assert(conn);
2677 if (conn->marked_for_close)
2678 return 0;
2679 switch (conn->type)
2681 case CONN_TYPE_OR:
2682 return conn->state == OR_CONN_STATE_CONNECTING;
2683 case CONN_TYPE_EXIT:
2684 return conn->state == EXIT_CONN_STATE_CONNECTING;
2685 case CONN_TYPE_DIR:
2686 return conn->state == DIR_CONN_STATE_CONNECTING;
2689 return 0;
2692 /** Allocates a base64'ed authenticator for use in http or https
2693 * auth, based on the input string <b>authenticator</b>. Returns it
2694 * if success, else returns NULL. */
2695 char *
2696 alloc_http_authenticator(const char *authenticator)
2698 /* an authenticator in Basic authentication
2699 * is just the string "username:password" */
2700 const size_t authenticator_length = strlen(authenticator);
2701 /* The base64_encode function needs a minimum buffer length
2702 * of 66 bytes. */
2703 const size_t base64_authenticator_length = (authenticator_length/48+1)*66;
2704 char *base64_authenticator = tor_malloc(base64_authenticator_length);
2705 if (base64_encode(base64_authenticator, base64_authenticator_length,
2706 authenticator, authenticator_length) < 0) {
2707 tor_free(base64_authenticator); /* free and set to null */
2708 } else {
2709 /* remove extra \n at end of encoding */
2710 base64_authenticator[strlen(base64_authenticator) - 1] = 0;
2712 return base64_authenticator;
2715 /** Given a socket handle, check whether the local address (sockname) of the
2716 * socket is one that we've connected from before. If so, double-check
2717 * whether our address has changed and we need to generate keys. If we do,
2718 * call init_keys().
2720 static void
2721 client_check_address_changed(int sock)
2723 uint32_t iface_ip, ip_out;
2724 struct sockaddr_in out_addr;
2725 socklen_t out_addr_len = (socklen_t) sizeof(out_addr);
2726 uint32_t *ip;
2728 if (!last_interface_ip)
2729 get_interface_address(LOG_INFO, &last_interface_ip);
2730 if (!outgoing_addrs)
2731 outgoing_addrs = smartlist_create();
2733 if (getsockname(sock, (struct sockaddr*)&out_addr, &out_addr_len)<0) {
2734 int e = tor_socket_errno(sock);
2735 log_warn(LD_NET, "getsockname() to check for address change failed: %s",
2736 tor_socket_strerror(e));
2737 return;
2740 /* Okay. If we've used this address previously, we're okay. */
2741 ip_out = ntohl(out_addr.sin_addr.s_addr);
2742 SMARTLIST_FOREACH(outgoing_addrs, uint32_t*, ip_ptr,
2743 if (*ip_ptr == ip_out) return;
2746 /* Uh-oh. We haven't connected from this address before. Has the interface
2747 * address changed? */
2748 if (get_interface_address(LOG_INFO, &iface_ip)<0)
2749 return;
2750 ip = tor_malloc(sizeof(uint32_t));
2751 *ip = ip_out;
2753 if (iface_ip == last_interface_ip) {
2754 /* Nope, it hasn't changed. Add this address to the list. */
2755 smartlist_add(outgoing_addrs, ip);
2756 } else {
2757 /* The interface changed. We're a client, so we need to regenerate our
2758 * keys. First, reset the state. */
2759 log(LOG_NOTICE, LD_NET, "Our IP address has changed. Rotating keys...");
2760 last_interface_ip = iface_ip;
2761 SMARTLIST_FOREACH(outgoing_addrs, void*, ip_ptr, tor_free(ip_ptr));
2762 smartlist_clear(outgoing_addrs);
2763 smartlist_add(outgoing_addrs, ip);
2764 /* Okay, now change our keys. */
2765 ip_address_changed(1);
2769 /** Some systems have limited system buffers for recv and xmit on
2770 * sockets allocated in a virtual server or similar environment. For a Tor
2771 * server this can produce the "Error creating network socket: No buffer
2772 * space available" error once all available TCP buffer space is consumed.
2773 * This method will attempt to constrain the buffers allocated for the socket
2774 * to the desired size to stay below system TCP buffer limits.
2776 static void
2777 set_constrained_socket_buffers(int sock, int size)
2779 void *sz = (void*)&size;
2780 socklen_t sz_sz = (socklen_t) sizeof(size);
2781 if (setsockopt(sock, SOL_SOCKET, SO_SNDBUF, sz, sz_sz) < 0) {
2782 int e = tor_socket_errno(sock);
2783 log_warn(LD_NET, "setsockopt() to constrain send "
2784 "buffer to %d bytes failed: %s", size, tor_socket_strerror(e));
2786 if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF, sz, sz_sz) < 0) {
2787 int e = tor_socket_errno(sock);
2788 log_warn(LD_NET, "setsockopt() to constrain recv "
2789 "buffer to %d bytes failed: %s", size, tor_socket_strerror(e));
2793 /** Process new bytes that have arrived on conn-\>inbuf.
2795 * This function just passes conn to the connection-specific
2796 * connection_*_process_inbuf() function. It also passes in
2797 * package_partial if wanted.
2799 static int
2800 connection_process_inbuf(connection_t *conn, int package_partial)
2802 tor_assert(conn);
2804 switch (conn->type) {
2805 case CONN_TYPE_OR:
2806 return connection_or_process_inbuf(TO_OR_CONN(conn));
2807 case CONN_TYPE_EXIT:
2808 case CONN_TYPE_AP:
2809 return connection_edge_process_inbuf(TO_EDGE_CONN(conn),
2810 package_partial);
2811 case CONN_TYPE_DIR:
2812 return connection_dir_process_inbuf(TO_DIR_CONN(conn));
2813 case CONN_TYPE_CPUWORKER:
2814 return connection_cpu_process_inbuf(conn);
2815 case CONN_TYPE_CONTROL:
2816 return connection_control_process_inbuf(TO_CONTROL_CONN(conn));
2817 default:
2818 log_err(LD_BUG,"got unexpected conn type %d.", conn->type);
2819 tor_fragile_assert();
2820 return -1;
2824 /** Called whenever we've written data on a connection. */
2825 static int
2826 connection_flushed_some(connection_t *conn)
2828 int r = 0;
2829 tor_assert(!conn->in_flushed_some);
2830 conn->in_flushed_some = 1;
2831 if (conn->type == CONN_TYPE_DIR &&
2832 conn->state == DIR_CONN_STATE_SERVER_WRITING) {
2833 r = connection_dirserv_flushed_some(TO_DIR_CONN(conn));
2834 } else if (conn->type == CONN_TYPE_OR) {
2835 r = connection_or_flushed_some(TO_OR_CONN(conn));
2837 conn->in_flushed_some = 0;
2838 return r;
2841 /** We just finished flushing bytes from conn-\>outbuf, and there
2842 * are no more bytes remaining.
2844 * This function just passes conn to the connection-specific
2845 * connection_*_finished_flushing() function.
2847 static int
2848 connection_finished_flushing(connection_t *conn)
2850 tor_assert(conn);
2852 /* If the connection is closed, don't try to do anything more here. */
2853 if (CONN_IS_CLOSED(conn))
2854 return 0;
2856 // log_fn(LOG_DEBUG,"entered. Socket %u.", conn->s);
2858 switch (conn->type) {
2859 case CONN_TYPE_OR:
2860 return connection_or_finished_flushing(TO_OR_CONN(conn));
2861 case CONN_TYPE_AP:
2862 case CONN_TYPE_EXIT:
2863 return connection_edge_finished_flushing(TO_EDGE_CONN(conn));
2864 case CONN_TYPE_DIR:
2865 return connection_dir_finished_flushing(TO_DIR_CONN(conn));
2866 case CONN_TYPE_CPUWORKER:
2867 return connection_cpu_finished_flushing(conn);
2868 case CONN_TYPE_CONTROL:
2869 return connection_control_finished_flushing(TO_CONTROL_CONN(conn));
2870 default:
2871 log_err(LD_BUG,"got unexpected conn type %d.", conn->type);
2872 tor_fragile_assert();
2873 return -1;
2877 /** Called when our attempt to connect() to another server has just
2878 * succeeded.
2880 * This function just passes conn to the connection-specific
2881 * connection_*_finished_connecting() function.
2883 static int
2884 connection_finished_connecting(connection_t *conn)
2886 tor_assert(conn);
2887 switch (conn->type)
2889 case CONN_TYPE_OR:
2890 return connection_or_finished_connecting(TO_OR_CONN(conn));
2891 case CONN_TYPE_EXIT:
2892 return connection_edge_finished_connecting(TO_EDGE_CONN(conn));
2893 case CONN_TYPE_DIR:
2894 return connection_dir_finished_connecting(TO_DIR_CONN(conn));
2895 default:
2896 log_err(LD_BUG,"got unexpected conn type %d.", conn->type);
2897 tor_fragile_assert();
2898 return -1;
2902 /** Callback: invoked when a connection reaches an EOF event. */
2903 static int
2904 connection_reached_eof(connection_t *conn)
2906 switch (conn->type) {
2907 case CONN_TYPE_OR:
2908 return connection_or_reached_eof(TO_OR_CONN(conn));
2909 case CONN_TYPE_AP:
2910 case CONN_TYPE_EXIT:
2911 return connection_edge_reached_eof(TO_EDGE_CONN(conn));
2912 case CONN_TYPE_DIR:
2913 return connection_dir_reached_eof(TO_DIR_CONN(conn));
2914 case CONN_TYPE_CPUWORKER:
2915 return connection_cpu_reached_eof(conn);
2916 case CONN_TYPE_CONTROL:
2917 return connection_control_reached_eof(TO_CONTROL_CONN(conn));
2918 default:
2919 log_err(LD_BUG,"got unexpected conn type %d.", conn->type);
2920 tor_fragile_assert();
2921 return -1;
2925 /** Log how many bytes are used by buffers of different kinds and sizes. */
2926 void
2927 connection_dump_buffer_mem_stats(int severity)
2929 uint64_t used_by_type[_CONN_TYPE_MAX+1];
2930 uint64_t alloc_by_type[_CONN_TYPE_MAX+1];
2931 int n_conns_by_type[_CONN_TYPE_MAX+1];
2932 uint64_t total_alloc = 0;
2933 uint64_t total_used = 0;
2934 int i;
2935 smartlist_t *conns = get_connection_array();
2937 memset(used_by_type, 0, sizeof(used_by_type));
2938 memset(alloc_by_type, 0, sizeof(alloc_by_type));
2939 memset(n_conns_by_type, 0, sizeof(n_conns_by_type));
2941 SMARTLIST_FOREACH(conns, connection_t *, c,
2943 int tp = c->type;
2944 ++n_conns_by_type[tp];
2945 if (c->inbuf) {
2946 used_by_type[tp] += buf_datalen(c->inbuf);
2947 alloc_by_type[tp] += buf_allocation(c->inbuf);
2949 if (c->outbuf) {
2950 used_by_type[tp] += buf_datalen(c->outbuf);
2951 alloc_by_type[tp] += buf_allocation(c->outbuf);
2954 for (i=0; i <= _CONN_TYPE_MAX; ++i) {
2955 total_used += used_by_type[i];
2956 total_alloc += alloc_by_type[i];
2959 log(severity, LD_GENERAL,
2960 "In buffers for %d connections: "U64_FORMAT" used/"U64_FORMAT" allocated",
2961 smartlist_len(conns),
2962 U64_PRINTF_ARG(total_used), U64_PRINTF_ARG(total_alloc));
2963 for (i=_CONN_TYPE_MIN; i <= _CONN_TYPE_MAX; ++i) {
2964 if (!n_conns_by_type[i])
2965 continue;
2966 log(severity, LD_GENERAL,
2967 " For %d %s connections: "U64_FORMAT" used/"U64_FORMAT" allocated",
2968 n_conns_by_type[i], conn_type_to_string(i),
2969 U64_PRINTF_ARG(used_by_type[i]), U64_PRINTF_ARG(alloc_by_type[i]));
2973 /** Verify that connection <b>conn</b> has all of its invariants
2974 * correct. Trigger an assert if anything is invalid.
2976 void
2977 assert_connection_ok(connection_t *conn, time_t now)
2979 (void) now; /* XXXX unused. */
2980 tor_assert(conn);
2981 tor_assert(conn->type >= _CONN_TYPE_MIN);
2982 tor_assert(conn->type <= _CONN_TYPE_MAX);
2983 switch (conn->type) {
2984 case CONN_TYPE_OR:
2985 tor_assert(conn->magic == OR_CONNECTION_MAGIC);
2986 break;
2987 case CONN_TYPE_AP:
2988 case CONN_TYPE_EXIT:
2989 tor_assert(conn->magic == EDGE_CONNECTION_MAGIC);
2990 break;
2991 case CONN_TYPE_DIR:
2992 tor_assert(conn->magic == DIR_CONNECTION_MAGIC);
2993 break;
2994 case CONN_TYPE_CONTROL:
2995 tor_assert(conn->magic == CONTROL_CONNECTION_MAGIC);
2996 break;
2997 default:
2998 tor_assert(conn->magic == BASE_CONNECTION_MAGIC);
2999 break;
3002 if (conn->linked_conn) {
3003 tor_assert(conn->linked_conn->linked_conn == conn);
3004 tor_assert(conn->linked);
3006 if (conn->linked)
3007 tor_assert(conn->s < 0);
3009 if (conn->outbuf_flushlen > 0) {
3010 tor_assert(connection_is_writing(conn) || conn->write_blocked_on_bw ||
3011 (CONN_IS_EDGE(conn) && TO_EDGE_CONN(conn)->edge_blocked_on_circ));
3014 if (conn->hold_open_until_flushed)
3015 tor_assert(conn->marked_for_close);
3017 /* XXXX check: read_blocked_on_bw, write_blocked_on_bw, s, conn_array_index,
3018 * marked_for_close. */
3020 /* buffers */
3021 if (!connection_is_listener(conn)) {
3022 assert_buf_ok(conn->inbuf);
3023 assert_buf_ok(conn->outbuf);
3026 if (conn->type == CONN_TYPE_OR) {
3027 or_connection_t *or_conn = TO_OR_CONN(conn);
3028 if (conn->state == OR_CONN_STATE_OPEN) {
3029 /* tor_assert(conn->bandwidth > 0); */
3030 /* the above isn't necessarily true: if we just did a TLS
3031 * handshake but we didn't recognize the other peer, or it
3032 * gave a bad cert/etc, then we won't have assigned bandwidth,
3033 * yet it will be open. -RD
3035 // tor_assert(conn->read_bucket >= 0);
3037 // tor_assert(conn->addr && conn->port);
3038 tor_assert(conn->address);
3039 if (conn->state > OR_CONN_STATE_PROXY_READING)
3040 tor_assert(or_conn->tls);
3043 if (CONN_IS_EDGE(conn)) {
3044 edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
3045 if (edge_conn->chosen_exit_optional || edge_conn->chosen_exit_retries) {
3046 tor_assert(conn->type == CONN_TYPE_AP);
3047 tor_assert(edge_conn->chosen_exit_name);
3050 /* XXX unchecked: package window, deliver window. */
3051 if (conn->type == CONN_TYPE_AP) {
3053 tor_assert(edge_conn->socks_request);
3054 if (conn->state == AP_CONN_STATE_OPEN) {
3055 tor_assert(edge_conn->socks_request->has_finished);
3056 if (!conn->marked_for_close) {
3057 tor_assert(edge_conn->cpath_layer);
3058 assert_cpath_layer_ok(edge_conn->cpath_layer);
3062 if (conn->type == CONN_TYPE_EXIT) {
3063 tor_assert(conn->purpose == EXIT_PURPOSE_CONNECT ||
3064 conn->purpose == EXIT_PURPOSE_RESOLVE);
3066 } else if (conn->type == CONN_TYPE_DIR) {
3067 } else {
3068 /* Purpose is only used for dir and exit types currently */
3069 tor_assert(!conn->purpose);
3072 switch (conn->type)
3074 case CONN_TYPE_OR_LISTENER:
3075 case CONN_TYPE_AP_LISTENER:
3076 case CONN_TYPE_AP_TRANS_LISTENER:
3077 case CONN_TYPE_AP_NATD_LISTENER:
3078 case CONN_TYPE_DIR_LISTENER:
3079 case CONN_TYPE_CONTROL_LISTENER:
3080 case CONN_TYPE_AP_DNS_LISTENER:
3081 tor_assert(conn->state == LISTENER_STATE_READY);
3082 break;
3083 case CONN_TYPE_OR:
3084 tor_assert(conn->state >= _OR_CONN_STATE_MIN);
3085 tor_assert(conn->state <= _OR_CONN_STATE_MAX);
3086 tor_assert(TO_OR_CONN(conn)->n_circuits >= 0);
3087 break;
3088 case CONN_TYPE_EXIT:
3089 tor_assert(conn->state >= _EXIT_CONN_STATE_MIN);
3090 tor_assert(conn->state <= _EXIT_CONN_STATE_MAX);
3091 tor_assert(conn->purpose >= _EXIT_PURPOSE_MIN);
3092 tor_assert(conn->purpose <= _EXIT_PURPOSE_MAX);
3093 break;
3094 case CONN_TYPE_AP:
3095 tor_assert(conn->state >= _AP_CONN_STATE_MIN);
3096 tor_assert(conn->state <= _AP_CONN_STATE_MAX);
3097 tor_assert(TO_EDGE_CONN(conn)->socks_request);
3098 break;
3099 case CONN_TYPE_DIR:
3100 tor_assert(conn->state >= _DIR_CONN_STATE_MIN);
3101 tor_assert(conn->state <= _DIR_CONN_STATE_MAX);
3102 tor_assert(conn->purpose >= _DIR_PURPOSE_MIN);
3103 tor_assert(conn->purpose <= _DIR_PURPOSE_MAX);
3104 break;
3105 case CONN_TYPE_CPUWORKER:
3106 tor_assert(conn->state >= _CPUWORKER_STATE_MIN);
3107 tor_assert(conn->state <= _CPUWORKER_STATE_MAX);
3108 break;
3109 case CONN_TYPE_CONTROL:
3110 tor_assert(conn->state >= _CONTROL_CONN_STATE_MIN);
3111 tor_assert(conn->state <= _CONTROL_CONN_STATE_MAX);
3112 break;
3113 default:
3114 tor_assert(0);