Optimize cell-ewma circuit priority algorithm.
[tor.git] / src / or / connection.c
bloba95850b9e504b8130f829fadb531ce28f1147eed
1 /* Copyright (c) 2001 Matej Pfajfar.
2 * Copyright (c) 2001-2004, Roger Dingledine.
3 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
4 * Copyright (c) 2007-2009, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
7 /**
8 * \file connection.c
9 * \brief General high-level functions to handle reading and writing
10 * on connections.
11 **/
13 #include "or.h"
15 static connection_t *connection_create_listener(
16 struct sockaddr *listensockaddr,
17 socklen_t listensocklen, int type,
18 char* address);
19 static void connection_init(time_t now, connection_t *conn, int type,
20 int socket_family);
21 static int connection_init_accepted_conn(connection_t *conn,
22 uint8_t listener_type);
23 static int connection_handle_listener_read(connection_t *conn, int new_type);
24 static int connection_read_bucket_should_increase(or_connection_t *conn);
25 static int connection_finished_flushing(connection_t *conn);
26 static int connection_flushed_some(connection_t *conn);
27 static int connection_finished_connecting(connection_t *conn);
28 static int connection_reached_eof(connection_t *conn);
29 static int connection_read_to_buf(connection_t *conn, int *max_to_read,
30 int *socket_error);
31 static int connection_process_inbuf(connection_t *conn, int package_partial);
32 static void client_check_address_changed(int sock);
33 static void set_constrained_socket_buffers(int sock, int size);
35 static const char *connection_proxy_state_to_string(int state);
36 static int connection_read_https_proxy_response(connection_t *conn);
37 static void connection_send_socks5_connect(connection_t *conn);
39 /** The last IPv4 address that our network interface seemed to have been
40 * binding to, in host order. We use this to detect when our IP changes. */
41 static uint32_t last_interface_ip = 0;
42 /** A list of uint32_ts for addresses we've used in outgoing connections.
43 * Used to detect IP address changes. */
44 static smartlist_t *outgoing_addrs = NULL;
46 /**************************************************************/
48 /**
49 * Return the human-readable name for the connection type <b>type</b>
51 const char *
52 conn_type_to_string(int type)
54 static char buf[64];
55 switch (type) {
56 case CONN_TYPE_OR_LISTENER: return "OR listener";
57 case CONN_TYPE_OR: return "OR";
58 case CONN_TYPE_EXIT: return "Exit";
59 case CONN_TYPE_AP_LISTENER: return "Socks listener";
60 case CONN_TYPE_AP_TRANS_LISTENER:
61 return "Transparent pf/netfilter listener";
62 case CONN_TYPE_AP_NATD_LISTENER: return "Transparent natd listener";
63 case CONN_TYPE_AP_DNS_LISTENER: return "DNS listener";
64 case CONN_TYPE_AP: return "Socks";
65 case CONN_TYPE_DIR_LISTENER: return "Directory listener";
66 case CONN_TYPE_DIR: return "Directory";
67 case CONN_TYPE_CPUWORKER: return "CPU worker";
68 case CONN_TYPE_CONTROL_LISTENER: return "Control listener";
69 case CONN_TYPE_CONTROL: return "Control";
70 default:
71 log_warn(LD_BUG, "unknown connection type %d", type);
72 tor_snprintf(buf, sizeof(buf), "unknown [%d]", type);
73 return buf;
77 /**
78 * Return the human-readable name for the connection state <b>state</b>
79 * for the connection type <b>type</b>
81 const char *
82 conn_state_to_string(int type, int state)
84 static char buf[96];
85 switch (type) {
86 case CONN_TYPE_OR_LISTENER:
87 case CONN_TYPE_AP_LISTENER:
88 case CONN_TYPE_AP_TRANS_LISTENER:
89 case CONN_TYPE_AP_NATD_LISTENER:
90 case CONN_TYPE_AP_DNS_LISTENER:
91 case CONN_TYPE_DIR_LISTENER:
92 case CONN_TYPE_CONTROL_LISTENER:
93 if (state == LISTENER_STATE_READY)
94 return "ready";
95 break;
96 case CONN_TYPE_OR:
97 switch (state) {
98 case OR_CONN_STATE_CONNECTING: return "connect()ing";
99 case OR_CONN_STATE_PROXY_HANDSHAKING: return "handshaking (proxy)";
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 or_conn->active_circuit_pqueue = smartlist_create();
184 or_conn->active_circuit_pqueue_last_recalibrated = cell_ewma_get_tick();
186 return or_conn;
189 /** Allocate and return a new edge_connection_t, initialized as by
190 * connection_init(). */
191 edge_connection_t *
192 edge_connection_new(int type, int socket_family)
194 edge_connection_t *edge_conn = tor_malloc_zero(sizeof(edge_connection_t));
195 tor_assert(type == CONN_TYPE_EXIT || type == CONN_TYPE_AP);
196 connection_init(time(NULL), TO_CONN(edge_conn), type, socket_family);
197 if (type == CONN_TYPE_AP)
198 edge_conn->socks_request = tor_malloc_zero(sizeof(socks_request_t));
199 return edge_conn;
202 /** Allocate and return a new control_connection_t, initialized as by
203 * connection_init(). */
204 control_connection_t *
205 control_connection_new(int socket_family)
207 control_connection_t *control_conn =
208 tor_malloc_zero(sizeof(control_connection_t));
209 connection_init(time(NULL),
210 TO_CONN(control_conn), CONN_TYPE_CONTROL, socket_family);
211 return control_conn;
214 /** Allocate, initialize, and return a new connection_t subtype of <b>type</b>
215 * to make or receive connections of address family <b>socket_family</b>. The
216 * type should be one of the CONN_TYPE_* constants. */
217 connection_t *
218 connection_new(int type, int socket_family)
220 switch (type) {
221 case CONN_TYPE_OR:
222 return TO_CONN(or_connection_new(socket_family));
224 case CONN_TYPE_EXIT:
225 case CONN_TYPE_AP:
226 return TO_CONN(edge_connection_new(type, socket_family));
228 case CONN_TYPE_DIR:
229 return TO_CONN(dir_connection_new(socket_family));
231 case CONN_TYPE_CONTROL:
232 return TO_CONN(control_connection_new(socket_family));
234 default: {
235 connection_t *conn = tor_malloc_zero(sizeof(connection_t));
236 connection_init(time(NULL), conn, type, socket_family);
237 return conn;
242 /** Initializes conn. (you must call connection_add() to link it into the main
243 * array).
245 * Set conn-\>type to <b>type</b>. Set conn-\>s and conn-\>conn_array_index to
246 * -1 to signify they are not yet assigned.
248 * If conn is not a listener type, allocate buffers for it. If it's
249 * an AP type, allocate space to store the socks_request.
251 * Assign a pseudorandom next_circ_id between 0 and 2**15.
253 * Initialize conn's timestamps to now.
255 static void
256 connection_init(time_t now, connection_t *conn, int type, int socket_family)
258 static uint64_t n_connections_allocated = 1;
260 switch (type) {
261 case CONN_TYPE_OR:
262 conn->magic = OR_CONNECTION_MAGIC;
263 break;
264 case CONN_TYPE_EXIT:
265 case CONN_TYPE_AP:
266 conn->magic = EDGE_CONNECTION_MAGIC;
267 break;
268 case CONN_TYPE_DIR:
269 conn->magic = DIR_CONNECTION_MAGIC;
270 break;
271 case CONN_TYPE_CONTROL:
272 conn->magic = CONTROL_CONNECTION_MAGIC;
273 break;
274 default:
275 conn->magic = BASE_CONNECTION_MAGIC;
276 break;
279 conn->s = -1; /* give it a default of 'not used' */
280 conn->conn_array_index = -1; /* also default to 'not used' */
281 conn->global_identifier = n_connections_allocated++;
283 conn->type = type;
284 conn->socket_family = socket_family;
285 if (!connection_is_listener(conn)) { /* listeners never use their buf */
286 conn->inbuf = buf_new();
287 conn->outbuf = buf_new();
290 conn->timestamp_created = now;
291 conn->timestamp_lastread = now;
292 conn->timestamp_lastwritten = now;
295 /** Create a link between <b>conn_a</b> and <b>conn_b</b>. */
296 void
297 connection_link_connections(connection_t *conn_a, connection_t *conn_b)
299 tor_assert(conn_a->s < 0);
300 tor_assert(conn_b->s < 0);
302 conn_a->linked = 1;
303 conn_b->linked = 1;
304 conn_a->linked_conn = conn_b;
305 conn_b->linked_conn = conn_a;
308 /** Deallocate memory used by <b>conn</b>. Deallocate its buffers if
309 * necessary, close its socket if necessary, and mark the directory as dirty
310 * if <b>conn</b> is an OR or OP connection.
312 static void
313 _connection_free(connection_t *conn)
315 void *mem;
316 size_t memlen;
317 if (!conn)
318 return;
320 switch (conn->type) {
321 case CONN_TYPE_OR:
322 tor_assert(conn->magic == OR_CONNECTION_MAGIC);
323 mem = TO_OR_CONN(conn);
324 memlen = sizeof(or_connection_t);
325 break;
326 case CONN_TYPE_AP:
327 case CONN_TYPE_EXIT:
328 tor_assert(conn->magic == EDGE_CONNECTION_MAGIC);
329 mem = TO_EDGE_CONN(conn);
330 memlen = sizeof(edge_connection_t);
331 break;
332 case CONN_TYPE_DIR:
333 tor_assert(conn->magic == DIR_CONNECTION_MAGIC);
334 mem = TO_DIR_CONN(conn);
335 memlen = sizeof(dir_connection_t);
336 break;
337 case CONN_TYPE_CONTROL:
338 tor_assert(conn->magic == CONTROL_CONNECTION_MAGIC);
339 mem = TO_CONTROL_CONN(conn);
340 memlen = sizeof(control_connection_t);
341 break;
342 default:
343 tor_assert(conn->magic == BASE_CONNECTION_MAGIC);
344 mem = conn;
345 memlen = sizeof(connection_t);
346 break;
349 if (conn->linked) {
350 log_info(LD_GENERAL, "Freeing linked %s connection [%s] with %d "
351 "bytes on inbuf, %d on outbuf.",
352 conn_type_to_string(conn->type),
353 conn_state_to_string(conn->type, conn->state),
354 (int)buf_datalen(conn->inbuf), (int)buf_datalen(conn->outbuf));
357 if (!connection_is_listener(conn)) {
358 buf_free(conn->inbuf);
359 buf_free(conn->outbuf);
360 } else {
361 if (conn->socket_family == AF_UNIX) {
362 /* For now only control ports can be Unix domain sockets
363 * and listeners at the same time */
364 tor_assert(conn->type == CONN_TYPE_CONTROL_LISTENER);
366 if (unlink(conn->address) < 0 && errno != ENOENT) {
367 log_warn(LD_NET, "Could not unlink %s: %s", conn->address,
368 strerror(errno));
373 tor_free(conn->address);
375 if (connection_speaks_cells(conn)) {
376 or_connection_t *or_conn = TO_OR_CONN(conn);
377 tor_tls_free(or_conn->tls);
378 or_conn->tls = NULL;
379 or_handshake_state_free(or_conn->handshake_state);
380 or_conn->handshake_state = NULL;
381 smartlist_free(or_conn->active_circuit_pqueue);
382 tor_free(or_conn->nickname);
384 if (CONN_IS_EDGE(conn)) {
385 edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
386 tor_free(edge_conn->chosen_exit_name);
387 if (edge_conn->socks_request) {
388 memset(edge_conn->socks_request, 0xcc, sizeof(socks_request_t));
389 tor_free(edge_conn->socks_request);
392 rend_data_free(edge_conn->rend_data);
394 if (conn->type == CONN_TYPE_CONTROL) {
395 control_connection_t *control_conn = TO_CONTROL_CONN(conn);
396 tor_free(control_conn->incoming_cmd);
399 tor_free(conn->read_event); /* Probably already freed by connection_free. */
400 tor_free(conn->write_event); /* Probably already freed by connection_free. */
402 if (conn->type == CONN_TYPE_DIR) {
403 dir_connection_t *dir_conn = TO_DIR_CONN(conn);
404 tor_free(dir_conn->requested_resource);
406 tor_zlib_free(dir_conn->zlib_state);
407 if (dir_conn->fingerprint_stack) {
408 SMARTLIST_FOREACH(dir_conn->fingerprint_stack, char *, cp, tor_free(cp));
409 smartlist_free(dir_conn->fingerprint_stack);
412 cached_dir_decref(dir_conn->cached_dir);
413 rend_data_free(dir_conn->rend_data);
416 if (conn->s >= 0) {
417 log_debug(LD_NET,"closing fd %d.",conn->s);
418 tor_close_socket(conn->s);
419 conn->s = -1;
422 if (conn->type == CONN_TYPE_OR &&
423 !tor_digest_is_zero(TO_OR_CONN(conn)->identity_digest)) {
424 log_warn(LD_BUG, "called on OR conn with non-zeroed identity_digest");
425 connection_or_remove_from_identity_map(TO_OR_CONN(conn));
428 memset(mem, 0xCC, memlen); /* poison memory */
429 tor_free(mem);
432 /** Make sure <b>conn</b> isn't in any of the global conn lists; then free it.
434 void
435 connection_free(connection_t *conn)
437 if (!conn)
438 return;
439 tor_assert(!connection_is_on_closeable_list(conn));
440 tor_assert(!connection_in_array(conn));
441 if (conn->linked_conn) {
442 log_err(LD_BUG, "Called with conn->linked_conn still set.");
443 tor_fragile_assert();
444 conn->linked_conn->linked_conn = NULL;
445 if (! conn->linked_conn->marked_for_close &&
446 conn->linked_conn->reading_from_linked_conn)
447 connection_start_reading(conn->linked_conn);
448 conn->linked_conn = NULL;
450 if (connection_speaks_cells(conn)) {
451 if (!tor_digest_is_zero(TO_OR_CONN(conn)->identity_digest)) {
452 connection_or_remove_from_identity_map(TO_OR_CONN(conn));
455 if (conn->type == CONN_TYPE_CONTROL) {
456 TO_CONTROL_CONN(conn)->event_mask = 0;
457 control_update_global_event_mask();
459 connection_unregister_events(conn);
460 _connection_free(conn);
463 /** Call _connection_free() on every connection in our array, and release all
464 * storage held by connection.c. This is used by cpuworkers and dnsworkers
465 * when they fork, so they don't keep resources held open (especially
466 * sockets).
468 * Don't do the checks in connection_free(), because they will
469 * fail.
471 void
472 connection_free_all(void)
474 smartlist_t *conns = get_connection_array();
476 /* We don't want to log any messages to controllers. */
477 SMARTLIST_FOREACH(conns, connection_t *, conn,
478 if (conn->type == CONN_TYPE_CONTROL)
479 TO_CONTROL_CONN(conn)->event_mask = 0);
481 control_update_global_event_mask();
483 /* Unlink everything from the identity map. */
484 connection_or_clear_identity_map();
486 SMARTLIST_FOREACH(conns, connection_t *, conn, _connection_free(conn));
488 if (outgoing_addrs) {
489 SMARTLIST_FOREACH(outgoing_addrs, void*, addr, tor_free(addr));
490 smartlist_free(outgoing_addrs);
491 outgoing_addrs = NULL;
495 /** Do any cleanup needed:
496 * - Directory conns that failed to fetch a rendezvous descriptor
497 * need to inform pending rendezvous streams.
498 * - OR conns need to call rep_hist_note_*() to record status.
499 * - AP conns need to send a socks reject if necessary.
500 * - Exit conns need to call connection_dns_remove() if necessary.
501 * - AP and Exit conns need to send an end cell if they can.
502 * - DNS conns need to fail any resolves that are pending on them.
503 * - OR and edge connections need to be unlinked from circuits.
505 void
506 connection_about_to_close_connection(connection_t *conn)
508 circuit_t *circ;
509 dir_connection_t *dir_conn;
510 or_connection_t *or_conn;
511 edge_connection_t *edge_conn;
512 time_t now = time(NULL);
514 tor_assert(conn->marked_for_close);
516 if (CONN_IS_EDGE(conn)) {
517 edge_conn = TO_EDGE_CONN(conn);
518 if (!edge_conn->edge_has_sent_end) {
519 log_warn(LD_BUG, "(Harmless.) Edge connection (marked at %s:%d) "
520 "hasn't sent end yet?",
521 conn->marked_for_close_file, conn->marked_for_close);
522 tor_fragile_assert();
526 switch (conn->type) {
527 case CONN_TYPE_DIR:
528 dir_conn = TO_DIR_CONN(conn);
529 if (conn->state < DIR_CONN_STATE_CLIENT_FINISHED) {
530 /* It's a directory connection and connecting or fetching
531 * failed: forget about this router, and maybe try again. */
532 connection_dir_request_failed(dir_conn);
534 /* If we were trying to fetch a v2 rend desc and did not succeed,
535 * retry as needed. (If a fetch is successful, the connection state
536 * is changed to DIR_PURPOSE_HAS_FETCHED_RENDDESC to mark that
537 * refetching is unnecessary.) */
538 if (conn->purpose == DIR_PURPOSE_FETCH_RENDDESC_V2 &&
539 dir_conn->rend_data &&
540 strlen(dir_conn->rend_data->onion_address) ==
541 REND_SERVICE_ID_LEN_BASE32)
542 rend_client_refetch_v2_renddesc(dir_conn->rend_data);
543 break;
544 case CONN_TYPE_OR:
545 or_conn = TO_OR_CONN(conn);
546 /* Remember why we're closing this connection. */
547 if (conn->state != OR_CONN_STATE_OPEN) {
548 /* Inform any pending (not attached) circs that they should
549 * give up. */
550 circuit_n_conn_done(TO_OR_CONN(conn), 0);
551 /* now mark things down as needed */
552 if (connection_or_nonopen_was_started_here(or_conn)) {
553 or_options_t *options = get_options();
554 rep_hist_note_connect_failed(or_conn->identity_digest, now);
555 entry_guard_register_connect_status(or_conn->identity_digest,0,
556 !options->HttpsProxy, now);
557 if (conn->state >= OR_CONN_STATE_TLS_HANDSHAKING) {
558 int reason = tls_error_to_orconn_end_reason(or_conn->tls_error);
559 control_event_or_conn_status(or_conn, OR_CONN_EVENT_FAILED,
560 reason);
561 if (!authdir_mode_tests_reachability(options))
562 control_event_bootstrap_problem(
563 orconn_end_reason_to_control_string(reason), reason);
566 } else if (conn->hold_open_until_flushed) {
567 /* We only set hold_open_until_flushed when we're intentionally
568 * closing a connection. */
569 rep_hist_note_disconnect(or_conn->identity_digest, now);
570 control_event_or_conn_status(or_conn, OR_CONN_EVENT_CLOSED,
571 tls_error_to_orconn_end_reason(or_conn->tls_error));
572 } else if (!tor_digest_is_zero(or_conn->identity_digest)) {
573 rep_hist_note_connection_died(or_conn->identity_digest, now);
574 control_event_or_conn_status(or_conn, OR_CONN_EVENT_CLOSED,
575 tls_error_to_orconn_end_reason(or_conn->tls_error));
577 /* Now close all the attached circuits on it. */
578 circuit_unlink_all_from_or_conn(TO_OR_CONN(conn),
579 END_CIRC_REASON_OR_CONN_CLOSED);
580 break;
581 case CONN_TYPE_AP:
582 edge_conn = TO_EDGE_CONN(conn);
583 if (edge_conn->socks_request->has_finished == 0) {
584 /* since conn gets removed right after this function finishes,
585 * there's no point trying to send back a reply at this point. */
586 log_warn(LD_BUG,"Closing stream (marked at %s:%d) without sending"
587 " back a socks reply.",
588 conn->marked_for_close_file, conn->marked_for_close);
590 if (!edge_conn->end_reason) {
591 log_warn(LD_BUG,"Closing stream (marked at %s:%d) without having"
592 " set end_reason.",
593 conn->marked_for_close_file, conn->marked_for_close);
595 if (edge_conn->dns_server_request) {
596 log_warn(LD_BUG,"Closing stream (marked at %s:%d) without having"
597 " replied to DNS request.",
598 conn->marked_for_close_file, conn->marked_for_close);
599 dnsserv_reject_request(edge_conn);
601 control_event_stream_bandwidth(edge_conn);
602 control_event_stream_status(edge_conn, STREAM_EVENT_CLOSED,
603 edge_conn->end_reason);
604 circ = circuit_get_by_edge_conn(edge_conn);
605 if (circ)
606 circuit_detach_stream(circ, edge_conn);
607 break;
608 case CONN_TYPE_EXIT:
609 edge_conn = TO_EDGE_CONN(conn);
610 circ = circuit_get_by_edge_conn(edge_conn);
611 if (circ)
612 circuit_detach_stream(circ, edge_conn);
613 if (conn->state == EXIT_CONN_STATE_RESOLVING) {
614 connection_dns_remove(edge_conn);
616 break;
620 /** Return true iff connection_close_immediate() has been called on this
621 * connection. */
622 #define CONN_IS_CLOSED(c) \
623 ((c)->linked ? ((c)->linked_conn_is_closed) : ((c)->s < 0))
625 /** Close the underlying socket for <b>conn</b>, so we don't try to
626 * flush it. Must be used in conjunction with (right before)
627 * connection_mark_for_close().
629 void
630 connection_close_immediate(connection_t *conn)
632 assert_connection_ok(conn,0);
633 if (CONN_IS_CLOSED(conn)) {
634 log_err(LD_BUG,"Attempt to close already-closed connection.");
635 tor_fragile_assert();
636 return;
638 if (conn->outbuf_flushlen) {
639 log_info(LD_NET,"fd %d, type %s, state %s, %d bytes on outbuf.",
640 conn->s, conn_type_to_string(conn->type),
641 conn_state_to_string(conn->type, conn->state),
642 (int)conn->outbuf_flushlen);
645 connection_unregister_events(conn);
647 if (conn->s >= 0)
648 tor_close_socket(conn->s);
649 conn->s = -1;
650 if (conn->linked)
651 conn->linked_conn_is_closed = 1;
652 if (!connection_is_listener(conn)) {
653 buf_clear(conn->outbuf);
654 conn->outbuf_flushlen = 0;
658 /** Mark <b>conn</b> to be closed next time we loop through
659 * conn_close_if_marked() in main.c. */
660 void
661 _connection_mark_for_close(connection_t *conn, int line, const char *file)
663 assert_connection_ok(conn,0);
664 tor_assert(line);
665 tor_assert(line < 1<<16); /* marked_for_close can only fit a uint16_t. */
666 tor_assert(file);
668 if (conn->marked_for_close) {
669 log(LOG_WARN,LD_BUG,"Duplicate call to connection_mark_for_close at %s:%d"
670 " (first at %s:%d)", file, line, conn->marked_for_close_file,
671 conn->marked_for_close);
672 tor_fragile_assert();
673 return;
676 conn->marked_for_close = line;
677 conn->marked_for_close_file = file;
678 add_connection_to_closeable_list(conn);
680 /* in case we're going to be held-open-til-flushed, reset
681 * the number of seconds since last successful write, so
682 * we get our whole 15 seconds */
683 conn->timestamp_lastwritten = time(NULL);
686 /** Find each connection that has hold_open_until_flushed set to
687 * 1 but hasn't written in the past 15 seconds, and set
688 * hold_open_until_flushed to 0. This means it will get cleaned
689 * up in the next loop through close_if_marked() in main.c.
691 void
692 connection_expire_held_open(void)
694 time_t now;
695 smartlist_t *conns = get_connection_array();
697 now = time(NULL);
699 SMARTLIST_FOREACH(conns, connection_t *, conn,
701 /* If we've been holding the connection open, but we haven't written
702 * for 15 seconds...
704 if (conn->hold_open_until_flushed) {
705 tor_assert(conn->marked_for_close);
706 if (now - conn->timestamp_lastwritten >= 15) {
707 int severity;
708 if (conn->type == CONN_TYPE_EXIT ||
709 (conn->type == CONN_TYPE_DIR &&
710 conn->purpose == DIR_PURPOSE_SERVER))
711 severity = LOG_INFO;
712 else
713 severity = LOG_NOTICE;
714 log_fn(severity, LD_NET,
715 "Giving up on marked_for_close conn that's been flushing "
716 "for 15s (fd %d, type %s, state %s).",
717 conn->s, conn_type_to_string(conn->type),
718 conn_state_to_string(conn->type, conn->state));
719 conn->hold_open_until_flushed = 0;
725 /** Create an AF_INET listenaddr struct.
726 * <b>listenaddress</b> provides the host and optionally the port information
727 * for the new structure. If no port is provided in <b>listenaddress</b> then
728 * <b>listenport</b> is used.
730 * If not NULL <b>readable_address</b> will contain a copy of the host part of
731 * <b>listenaddress</b>.
733 * The listenaddr struct has to be freed by the caller.
735 static struct sockaddr_in *
736 create_inet_sockaddr(const char *listenaddress, uint16_t listenport,
737 char **readable_address, socklen_t *socklen_out) {
738 struct sockaddr_in *listenaddr = NULL;
739 uint32_t addr;
740 uint16_t usePort = 0;
742 if (parse_addr_port(LOG_WARN,
743 listenaddress, readable_address, &addr, &usePort)<0) {
744 log_warn(LD_CONFIG,
745 "Error parsing/resolving ListenAddress %s", listenaddress);
746 goto err;
748 if (usePort==0)
749 usePort = listenport;
751 listenaddr = tor_malloc_zero(sizeof(struct sockaddr_in));
752 listenaddr->sin_addr.s_addr = htonl(addr);
753 listenaddr->sin_family = AF_INET;
754 listenaddr->sin_port = htons((uint16_t) usePort);
756 *socklen_out = sizeof(struct sockaddr_in);
758 return listenaddr;
760 err:
761 tor_free(listenaddr);
762 return NULL;
765 #ifdef HAVE_SYS_UN_H
766 /** Create an AF_UNIX listenaddr struct.
767 * <b>listenaddress</b> provides the path to the Unix socket.
769 * Eventually <b>listenaddress</b> will also optionally contain user, group,
770 * and file permissions for the new socket. But not yet. XXX
771 * Also, since we do not create the socket here the information doesn't help
772 * here.
774 * If not NULL <b>readable_address</b> will contain a copy of the path part of
775 * <b>listenaddress</b>.
777 * The listenaddr struct has to be freed by the caller.
779 static struct sockaddr_un *
780 create_unix_sockaddr(const char *listenaddress, char **readable_address,
781 socklen_t *len_out)
783 struct sockaddr_un *sockaddr = NULL;
785 sockaddr = tor_malloc_zero(sizeof(struct sockaddr_un));
786 sockaddr->sun_family = AF_UNIX;
787 strncpy(sockaddr->sun_path, listenaddress, sizeof(sockaddr->sun_path));
789 if (readable_address)
790 *readable_address = tor_strdup(listenaddress);
792 *len_out = sizeof(struct sockaddr_un);
793 return sockaddr;
795 #else
796 static struct sockaddr *
797 create_unix_sockaddr(const char *listenaddress, char **readable_address,
798 socklen_t *len_out)
800 (void)listenaddress;
801 (void)readable_address;
802 log_fn(LOG_ERR, LD_BUG,
803 "Unix domain sockets not supported, yet we tried to create one.");
804 *len_out = 0;
805 tor_assert(0);
807 #endif /* HAVE_SYS_UN_H */
809 /** Warn that an accept or a connect has failed because we're running up
810 * against our ulimit. Rate-limit these warnings so that we don't spam
811 * the log. */
812 static void
813 warn_too_many_conns(void)
815 #define WARN_TOO_MANY_CONNS_INTERVAL (6*60*60)
816 static time_t last_warned = 0;
817 time_t now = time(NULL);
818 int n_conns = get_n_open_sockets();
819 if (last_warned + WARN_TOO_MANY_CONNS_INTERVAL < now) {
820 log_warn(LD_NET,"Failing because we have %d connections already. Please "
821 "raise your ulimit -n.", n_conns);
822 last_warned = now;
824 control_event_general_status(LOG_WARN, "TOO_MANY_CONNECTIONS CURRENT=%d",
825 n_conns);
828 /** Bind a new non-blocking socket listening to the socket described
829 * by <b>listensockaddr</b>.
831 * <b>address</b> is only used for logging purposes and to add the information
832 * to the conn.
834 static connection_t *
835 connection_create_listener(struct sockaddr *listensockaddr, socklen_t socklen,
836 int type, char* address)
838 connection_t *conn;
839 int s; /* the socket we're going to make */
840 uint16_t usePort = 0;
841 int start_reading = 0;
843 if (get_n_open_sockets() >= get_options()->_ConnLimit-1) {
844 warn_too_many_conns();
845 return NULL;
848 if (listensockaddr->sa_family == AF_INET) {
849 int is_tcp = (type != CONN_TYPE_AP_DNS_LISTENER);
850 #ifndef MS_WINDOWS
851 int one=1;
852 #endif
853 if (is_tcp)
854 start_reading = 1;
856 usePort = ntohs( (uint16_t)
857 ((struct sockaddr_in *)listensockaddr)->sin_port);
859 log_notice(LD_NET, "Opening %s on %s:%d",
860 conn_type_to_string(type), address, usePort);
862 s = tor_open_socket(PF_INET,
863 is_tcp ? SOCK_STREAM : SOCK_DGRAM,
864 is_tcp ? IPPROTO_TCP: IPPROTO_UDP);
865 if (s < 0) {
866 log_warn(LD_NET,"Socket creation failed.");
867 goto err;
870 #ifndef MS_WINDOWS
871 /* REUSEADDR on normal places means you can rebind to the port
872 * right after somebody else has let it go. But REUSEADDR on win32
873 * means you can bind to the port _even when somebody else
874 * already has it bound_. So, don't do that on Win32. */
875 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (void*) &one,
876 (socklen_t)sizeof(one));
877 #endif
879 if (bind(s,listensockaddr,socklen) < 0) {
880 const char *helpfulhint = "";
881 int e = tor_socket_errno(s);
882 if (ERRNO_IS_EADDRINUSE(e))
883 helpfulhint = ". Is Tor already running?";
884 log_warn(LD_NET, "Could not bind to %s:%u: %s%s", address, usePort,
885 tor_socket_strerror(e), helpfulhint);
886 tor_close_socket(s);
887 goto err;
890 if (is_tcp) {
891 if (listen(s,SOMAXCONN) < 0) {
892 log_warn(LD_NET, "Could not listen on %s:%u: %s", address, usePort,
893 tor_socket_strerror(tor_socket_errno(s)));
894 tor_close_socket(s);
895 goto err;
898 #ifdef HAVE_SYS_UN_H
899 } else if (listensockaddr->sa_family == AF_UNIX) {
900 start_reading = 1;
902 /* For now only control ports can be Unix domain sockets
903 * and listeners at the same time */
904 tor_assert(type == CONN_TYPE_CONTROL_LISTENER);
906 log_notice(LD_NET, "Opening %s on %s",
907 conn_type_to_string(type), address);
909 if (unlink(address) < 0 && errno != ENOENT) {
910 log_warn(LD_NET, "Could not unlink %s: %s", address,
911 strerror(errno));
912 goto err;
914 s = tor_open_socket(AF_UNIX, SOCK_STREAM, 0);
915 if (s < 0) {
916 log_warn(LD_NET,"Socket creation failed: %s.", strerror(errno));
917 goto err;
920 if (bind(s, listensockaddr, (socklen_t)sizeof(struct sockaddr_un)) == -1) {
921 log_warn(LD_NET,"Bind to %s failed: %s.", address,
922 tor_socket_strerror(tor_socket_errno(s)));
923 goto err;
926 if (listen(s,SOMAXCONN) < 0) {
927 log_warn(LD_NET, "Could not listen on %s: %s", address,
928 tor_socket_strerror(tor_socket_errno(s)));
929 tor_close_socket(s);
930 goto err;
932 #endif /* HAVE_SYS_UN_H */
933 } else {
934 log_err(LD_BUG,"Got unexpected address family %d.",
935 listensockaddr->sa_family);
936 tor_assert(0);
939 set_socket_nonblocking(s);
941 conn = connection_new(type, listensockaddr->sa_family);
942 conn->socket_family = listensockaddr->sa_family;
943 conn->s = s;
944 conn->address = tor_strdup(address);
945 conn->port = usePort;
947 if (connection_add(conn) < 0) { /* no space, forget it */
948 log_warn(LD_NET,"connection_add for listener failed. Giving up.");
949 connection_free(conn);
950 goto err;
953 log_debug(LD_NET,"%s listening on port %u.",
954 conn_type_to_string(type), usePort);
956 conn->state = LISTENER_STATE_READY;
957 if (start_reading) {
958 connection_start_reading(conn);
959 } else {
960 tor_assert(type == CONN_TYPE_AP_DNS_LISTENER);
961 dnsserv_configure_listener(conn);
964 return conn;
966 err:
967 return NULL;
970 /** Do basic sanity checking on a newly received socket. Return 0
971 * if it looks ok, else return -1. */
972 static int
973 check_sockaddr(struct sockaddr *sa, int len, int level)
975 int ok = 1;
977 if (sa->sa_family == AF_INET) {
978 struct sockaddr_in *sin=(struct sockaddr_in*)sa;
979 if (len != sizeof(struct sockaddr_in)) {
980 log_fn(level, LD_NET, "Length of address not as expected: %d vs %d",
981 len,(int)sizeof(struct sockaddr_in));
982 ok = 0;
984 if (sin->sin_addr.s_addr == 0 || sin->sin_port == 0) {
985 log_fn(level, LD_NET,
986 "Address for new connection has address/port equal to zero.");
987 ok = 0;
989 } else if (sa->sa_family == AF_INET6) {
990 struct sockaddr_in6 *sin6=(struct sockaddr_in6*)sa;
991 if (len != sizeof(struct sockaddr_in6)) {
992 log_fn(level, LD_NET, "Length of address not as expected: %d vs %d",
993 len,(int)sizeof(struct sockaddr_in6));
994 ok = 0;
996 if (tor_mem_is_zero((void*)sin6->sin6_addr.s6_addr, 16) ||
997 sin6->sin6_port == 0) {
998 log_fn(level, LD_NET,
999 "Address for new connection has address/port equal to zero.");
1000 ok = 0;
1002 } else {
1003 ok = 0;
1005 return ok ? 0 : -1;
1008 /** Check whether the socket family from an accepted socket <b>got</b> is the
1009 * same as the one that <b>listener</b> is waiting for. If it isn't, log
1010 * a useful message and return -1. Else return 0.
1012 * This is annoying, but can apparently happen on some Darwins. */
1013 static int
1014 check_sockaddr_family_match(sa_family_t got, connection_t *listener)
1016 if (got != listener->socket_family) {
1017 log_info(LD_BUG, "A listener connection returned a socket with a "
1018 "mismatched family. %s for addr_family %d gave us a socket "
1019 "with address family %d. Dropping.",
1020 conn_type_to_string(listener->type),
1021 (int)listener->socket_family,
1022 (int)got);
1023 return -1;
1025 return 0;
1028 /** The listener connection <b>conn</b> told poll() it wanted to read.
1029 * Call accept() on conn-\>s, and add the new connection if necessary.
1031 static int
1032 connection_handle_listener_read(connection_t *conn, int new_type)
1034 int news; /* the new socket */
1035 connection_t *newconn;
1036 /* information about the remote peer when connecting to other routers */
1037 char addrbuf[256];
1038 struct sockaddr *remote = (struct sockaddr*)addrbuf;
1039 /* length of the remote address. Must be whatever accept() needs. */
1040 socklen_t remotelen = (socklen_t)sizeof(addrbuf);
1041 or_options_t *options = get_options();
1043 tor_assert((size_t)remotelen >= sizeof(struct sockaddr_in));
1044 memset(addrbuf, 0, sizeof(addrbuf));
1046 news = tor_accept_socket(conn->s,remote,&remotelen);
1047 if (news < 0) { /* accept() error */
1048 int e = tor_socket_errno(conn->s);
1049 if (ERRNO_IS_ACCEPT_EAGAIN(e)) {
1050 return 0; /* he hung up before we could accept(). that's fine. */
1051 } else if (ERRNO_IS_ACCEPT_RESOURCE_LIMIT(e)) {
1052 warn_too_many_conns();
1053 return 0;
1055 /* else there was a real error. */
1056 log_warn(LD_NET,"accept() failed: %s. Closing listener.",
1057 tor_socket_strerror(e));
1058 connection_mark_for_close(conn);
1059 return -1;
1061 log_debug(LD_NET,
1062 "Connection accepted on socket %d (child of fd %d).",
1063 news,conn->s);
1065 set_socket_nonblocking(news);
1067 if (options->ConstrainedSockets)
1068 set_constrained_socket_buffers(news, (int)options->ConstrainedSockSize);
1070 if (check_sockaddr_family_match(remote->sa_family, conn) < 0) {
1071 tor_close_socket(news);
1072 return 0;
1075 if (conn->socket_family == AF_INET || conn->socket_family == AF_INET6) {
1076 tor_addr_t addr;
1077 uint16_t port;
1078 if (check_sockaddr(remote, remotelen, LOG_INFO)<0) {
1079 log_info(LD_NET,
1080 "accept() returned a strange address; trying getsockname().");
1081 remotelen=sizeof(addrbuf);
1082 memset(addrbuf, 0, sizeof(addrbuf));
1083 if (getsockname(news, remote, &remotelen)<0) {
1084 int e = tor_socket_errno(news);
1085 log_warn(LD_NET, "getsockname() for new connection failed: %s",
1086 tor_socket_strerror(e));
1087 } else {
1088 if (check_sockaddr((struct sockaddr*)addrbuf, remotelen,
1089 LOG_WARN) < 0) {
1090 log_warn(LD_NET,"Something's wrong with this conn. Closing it.");
1091 tor_close_socket(news);
1092 return 0;
1097 if (check_sockaddr_family_match(remote->sa_family, conn) < 0) {
1098 tor_close_socket(news);
1099 return 0;
1102 tor_addr_from_sockaddr(&addr, remote, &port);
1104 /* process entrance policies here, before we even create the connection */
1105 if (new_type == CONN_TYPE_AP) {
1106 /* check sockspolicy to see if we should accept it */
1107 if (socks_policy_permits_address(&addr) == 0) {
1108 log_notice(LD_APP,
1109 "Denying socks connection from untrusted address %s.",
1110 fmt_addr(&addr));
1111 tor_close_socket(news);
1112 return 0;
1115 if (new_type == CONN_TYPE_DIR) {
1116 /* check dirpolicy to see if we should accept it */
1117 if (dir_policy_permits_address(&addr) == 0) {
1118 log_notice(LD_DIRSERV,"Denying dir connection from address %s.",
1119 fmt_addr(&addr));
1120 tor_close_socket(news);
1121 return 0;
1125 newconn = connection_new(new_type, conn->socket_family);
1126 newconn->s = news;
1128 /* remember the remote address */
1129 tor_addr_copy(&newconn->addr, &addr);
1130 newconn->port = port;
1131 newconn->address = tor_dup_addr(&addr);
1133 } else if (conn->socket_family == AF_UNIX) {
1134 /* For now only control ports can be Unix domain sockets
1135 * and listeners at the same time */
1136 tor_assert(conn->type == CONN_TYPE_CONTROL_LISTENER);
1138 newconn = connection_new(new_type, conn->socket_family);
1139 newconn->s = news;
1141 /* remember the remote address -- do we have anything sane to put here? */
1142 tor_addr_make_unspec(&newconn->addr);
1143 newconn->port = 1;
1144 newconn->address = tor_strdup(conn->address);
1145 } else {
1146 tor_assert(0);
1149 if (connection_add(newconn) < 0) { /* no space, forget it */
1150 connection_free(newconn);
1151 return 0; /* no need to tear down the parent */
1154 if (connection_init_accepted_conn(newconn, conn->type) < 0) {
1155 connection_mark_for_close(newconn);
1156 return 0;
1158 return 0;
1161 /** Initialize states for newly accepted connection <b>conn</b>.
1162 * If conn is an OR, start the TLS handshake.
1163 * If conn is a transparent AP, get its original destination
1164 * and place it in circuit_wait.
1166 static int
1167 connection_init_accepted_conn(connection_t *conn, uint8_t listener_type)
1169 connection_start_reading(conn);
1171 switch (conn->type) {
1172 case CONN_TYPE_OR:
1173 control_event_or_conn_status(TO_OR_CONN(conn), OR_CONN_EVENT_NEW, 0);
1174 return connection_tls_start_handshake(TO_OR_CONN(conn), 1);
1175 case CONN_TYPE_AP:
1176 switch (listener_type) {
1177 case CONN_TYPE_AP_LISTENER:
1178 conn->state = AP_CONN_STATE_SOCKS_WAIT;
1179 break;
1180 case CONN_TYPE_AP_TRANS_LISTENER:
1181 conn->state = AP_CONN_STATE_CIRCUIT_WAIT;
1182 return connection_ap_process_transparent(TO_EDGE_CONN(conn));
1183 case CONN_TYPE_AP_NATD_LISTENER:
1184 conn->state = AP_CONN_STATE_NATD_WAIT;
1185 break;
1187 break;
1188 case CONN_TYPE_DIR:
1189 conn->purpose = DIR_PURPOSE_SERVER;
1190 conn->state = DIR_CONN_STATE_SERVER_COMMAND_WAIT;
1191 break;
1192 case CONN_TYPE_CONTROL:
1193 conn->state = CONTROL_CONN_STATE_NEEDAUTH;
1194 break;
1196 return 0;
1199 /** Take conn, make a nonblocking socket; try to connect to
1200 * addr:port (they arrive in *host order*). If fail, return -1 and if
1201 * applicable put your best guess about errno into *<b>socket_error</b>.
1202 * Else assign s to conn-\>s: if connected return 1, if EAGAIN return 0.
1204 * address is used to make the logs useful.
1206 * On success, add conn to the list of polled connections.
1209 connection_connect(connection_t *conn, const char *address,
1210 const tor_addr_t *addr, uint16_t port, int *socket_error)
1212 int s, inprogress = 0;
1213 char addrbuf[256];
1214 struct sockaddr *dest_addr = (struct sockaddr*) addrbuf;
1215 socklen_t dest_addr_len;
1216 or_options_t *options = get_options();
1217 int protocol_family;
1219 if (get_n_open_sockets() >= get_options()->_ConnLimit-1) {
1220 warn_too_many_conns();
1221 return -1;
1224 if (tor_addr_family(addr) == AF_INET6)
1225 protocol_family = PF_INET6;
1226 else
1227 protocol_family = PF_INET;
1229 s = tor_open_socket(protocol_family,SOCK_STREAM,IPPROTO_TCP);
1230 if (s < 0) {
1231 *socket_error = tor_socket_errno(-1);
1232 log_warn(LD_NET,"Error creating network socket: %s",
1233 tor_socket_strerror(*socket_error));
1234 return -1;
1237 if (options->OutboundBindAddress) {
1238 struct sockaddr_in ext_addr;
1240 memset(&ext_addr, 0, sizeof(ext_addr));
1241 ext_addr.sin_family = AF_INET;
1242 ext_addr.sin_port = 0;
1243 if (!tor_inet_aton(options->OutboundBindAddress, &ext_addr.sin_addr)) {
1244 log_warn(LD_CONFIG,"Outbound bind address '%s' didn't parse. Ignoring.",
1245 options->OutboundBindAddress);
1246 } else {
1247 if (bind(s, (struct sockaddr*)&ext_addr,
1248 (socklen_t)sizeof(ext_addr)) < 0) {
1249 *socket_error = tor_socket_errno(s);
1250 log_warn(LD_NET,"Error binding network socket: %s",
1251 tor_socket_strerror(*socket_error));
1252 tor_close_socket(s);
1253 return -1;
1258 set_socket_nonblocking(s);
1260 if (options->ConstrainedSockets)
1261 set_constrained_socket_buffers(s, (int)options->ConstrainedSockSize);
1263 memset(addrbuf,0,sizeof(addrbuf));
1264 dest_addr = (struct sockaddr*) addrbuf;
1265 dest_addr_len = tor_addr_to_sockaddr(addr, port, dest_addr, sizeof(addrbuf));
1266 tor_assert(dest_addr_len > 0);
1268 log_debug(LD_NET,"Connecting to %s:%u.",escaped_safe_str(address),port);
1270 if (connect(s, dest_addr, dest_addr_len) < 0) {
1271 int e = tor_socket_errno(s);
1272 if (!ERRNO_IS_CONN_EINPROGRESS(e)) {
1273 /* yuck. kill it. */
1274 *socket_error = e;
1275 log_info(LD_NET,
1276 "connect() to %s:%u failed: %s",escaped_safe_str(address),
1277 port, tor_socket_strerror(e));
1278 tor_close_socket(s);
1279 return -1;
1280 } else {
1281 inprogress = 1;
1285 if (!server_mode(options))
1286 client_check_address_changed(s);
1288 /* it succeeded. we're connected. */
1289 log_fn(inprogress?LOG_DEBUG:LOG_INFO, LD_NET,
1290 "Connection to %s:%u %s (sock %d).",escaped_safe_str(address),
1291 port, inprogress?"in progress":"established", s);
1292 conn->s = s;
1293 if (connection_add(conn) < 0) /* no space, forget it */
1294 return -1;
1295 return inprogress ? 0 : 1;
1298 /** Convert state number to string representation for logging purposes.
1300 static const char *
1301 connection_proxy_state_to_string(int state)
1303 static const char *unknown = "???";
1304 static const char *states[] = {
1305 "PROXY_NONE",
1306 "PROXY_HTTPS_WANT_CONNECT_OK",
1307 "PROXY_SOCKS4_WANT_CONNECT_OK",
1308 "PROXY_SOCKS5_WANT_AUTH_METHOD_NONE",
1309 "PROXY_SOCKS5_WANT_AUTH_METHOD_RFC1929",
1310 "PROXY_SOCKS5_WANT_AUTH_RFC1929_OK",
1311 "PROXY_SOCKS5_WANT_CONNECT_OK",
1312 "PROXY_CONNECTED",
1315 if (state < PROXY_NONE || state > PROXY_CONNECTED)
1316 return unknown;
1318 return states[state];
1321 /** Write a proxy request of <b>type</b> (socks4, socks5, https) to conn
1322 * for conn->addr:conn->port, authenticating with the auth details given
1323 * in the configuration (if available). SOCKS 5 and HTTP CONNECT proxies
1324 * support authentication.
1326 * Returns -1 if conn->addr is incompatible with the proxy protocol, and
1327 * 0 otherwise.
1329 * Use connection_read_proxy_handshake() to complete the handshake.
1332 connection_proxy_connect(connection_t *conn, int type)
1334 or_options_t *options;
1336 tor_assert(conn);
1338 options = get_options();
1340 switch (type) {
1341 case PROXY_CONNECT: {
1342 char buf[1024];
1343 char *base64_authenticator=NULL;
1344 const char *authenticator = options->HttpsProxyAuthenticator;
1346 /* Send HTTP CONNECT and authentication (if available) in
1347 * one request */
1349 if (authenticator) {
1350 base64_authenticator = alloc_http_authenticator(authenticator);
1351 if (!base64_authenticator)
1352 log_warn(LD_OR, "Encoding https authenticator failed");
1355 if (base64_authenticator) {
1356 tor_snprintf(buf, sizeof(buf), "CONNECT %s:%d HTTP/1.1\r\n"
1357 "Proxy-Authorization: Basic %s\r\n\r\n",
1358 fmt_addr(&conn->addr),
1359 conn->port, base64_authenticator);
1360 tor_free(base64_authenticator);
1361 } else {
1362 tor_snprintf(buf, sizeof(buf), "CONNECT %s:%d HTTP/1.0\r\n\r\n",
1363 fmt_addr(&conn->addr), conn->port);
1366 connection_write_to_buf(buf, strlen(buf), conn);
1367 conn->proxy_state = PROXY_HTTPS_WANT_CONNECT_OK;
1368 break;
1371 case PROXY_SOCKS4: {
1372 unsigned char buf[9];
1373 uint16_t portn;
1374 uint32_t ip4addr;
1376 /* Send a SOCKS4 connect request with empty user id */
1378 if (tor_addr_family(&conn->addr) != AF_INET) {
1379 log_warn(LD_NET, "SOCKS4 client is incompatible with with IPv6");
1380 return -1;
1383 ip4addr = tor_addr_to_ipv4n(&conn->addr);
1384 portn = htons(conn->port);
1386 buf[0] = 4; /* version */
1387 buf[1] = SOCKS_COMMAND_CONNECT; /* command */
1388 memcpy(buf + 2, &portn, 2); /* port */
1389 memcpy(buf + 4, &ip4addr, 4); /* addr */
1390 buf[8] = 0; /* userid (empty) */
1392 connection_write_to_buf((char *)buf, sizeof(buf), conn);
1393 conn->proxy_state = PROXY_SOCKS4_WANT_CONNECT_OK;
1394 break;
1397 case PROXY_SOCKS5: {
1398 unsigned char buf[4]; /* fields: vers, num methods, method list */
1400 /* Send a SOCKS5 greeting (connect request must wait) */
1402 buf[0] = 5; /* version */
1404 /* number of auth methods */
1405 if (options->Socks5ProxyUsername) {
1406 buf[1] = 2;
1407 buf[2] = 0x00; /* no authentication */
1408 buf[3] = 0x02; /* rfc1929 Username/Passwd auth */
1409 conn->proxy_state = PROXY_SOCKS5_WANT_AUTH_METHOD_RFC1929;
1410 } else {
1411 buf[1] = 1;
1412 buf[2] = 0x00; /* no authentication */
1413 conn->proxy_state = PROXY_SOCKS5_WANT_AUTH_METHOD_NONE;
1416 connection_write_to_buf((char *)buf, 2 + buf[1], conn);
1417 break;
1420 default:
1421 log_err(LD_BUG, "Invalid proxy protocol, %d", type);
1422 tor_fragile_assert();
1423 return -1;
1426 log_debug(LD_NET, "set state %s",
1427 connection_proxy_state_to_string(conn->proxy_state));
1429 return 0;
1432 /** Read conn's inbuf. If the http response from the proxy is all
1433 * here, make sure it's good news, then return 1. If it's bad news,
1434 * return -1. Else return 0 and hope for better luck next time.
1436 static int
1437 connection_read_https_proxy_response(connection_t *conn)
1439 char *headers;
1440 char *reason=NULL;
1441 int status_code;
1442 time_t date_header;
1444 switch (fetch_from_buf_http(conn->inbuf,
1445 &headers, MAX_HEADERS_SIZE,
1446 NULL, NULL, 10000, 0)) {
1447 case -1: /* overflow */
1448 log_warn(LD_PROTOCOL,
1449 "Your https proxy sent back an oversized response. Closing.");
1450 return -1;
1451 case 0:
1452 log_info(LD_NET,"https proxy response not all here yet. Waiting.");
1453 return 0;
1454 /* case 1, fall through */
1457 if (parse_http_response(headers, &status_code, &date_header,
1458 NULL, &reason) < 0) {
1459 log_warn(LD_NET,
1460 "Unparseable headers from proxy (connecting to '%s'). Closing.",
1461 conn->address);
1462 tor_free(headers);
1463 return -1;
1465 if (!reason) reason = tor_strdup("[no reason given]");
1467 if (status_code == 200) {
1468 log_info(LD_NET,
1469 "HTTPS connect to '%s' successful! (200 %s) Starting TLS.",
1470 conn->address, escaped(reason));
1471 tor_free(reason);
1472 return 1;
1474 /* else, bad news on the status code */
1475 log_warn(LD_NET,
1476 "The https proxy sent back an unexpected status code %d (%s). "
1477 "Closing.",
1478 status_code, escaped(reason));
1479 tor_free(reason);
1480 return -1;
1483 /** Send SOCKS5 CONNECT command to <b>conn</b>, copying <b>conn->addr</b>
1484 * and <b>conn->port</b> into the request.
1486 static void
1487 connection_send_socks5_connect(connection_t *conn)
1489 unsigned char buf[1024];
1490 size_t reqsize = 6;
1491 uint16_t port = htons(conn->port);
1493 buf[0] = 5; /* version */
1494 buf[1] = SOCKS_COMMAND_CONNECT; /* command */
1495 buf[2] = 0; /* reserved */
1497 if (tor_addr_family(&conn->addr) == AF_INET) {
1498 uint32_t addr = tor_addr_to_ipv4n(&conn->addr);
1500 buf[3] = 1;
1501 reqsize += 4;
1502 memcpy(buf + 4, &addr, 4);
1503 memcpy(buf + 8, &port, 2);
1504 } else { /* AF_INET6 */
1505 buf[3] = 4;
1506 reqsize += 16;
1507 memcpy(buf + 4, tor_addr_to_in6(&conn->addr), 16);
1508 memcpy(buf + 20, &port, 2);
1511 connection_write_to_buf((char *)buf, reqsize, conn);
1513 conn->proxy_state = PROXY_SOCKS5_WANT_CONNECT_OK;
1516 /** Call this from connection_*_process_inbuf() to advance the proxy
1517 * handshake.
1519 * No matter what proxy protocol is used, if this function returns 1, the
1520 * handshake is complete, and the data remaining on inbuf may contain the
1521 * start of the communication with the requested server.
1523 * Returns 0 if the current buffer contains an incomplete response, and -1
1524 * on error.
1527 connection_read_proxy_handshake(connection_t *conn)
1529 int ret = 0;
1530 char *reason = NULL;
1532 log_debug(LD_NET, "enter state %s",
1533 connection_proxy_state_to_string(conn->proxy_state));
1535 switch (conn->proxy_state) {
1536 case PROXY_HTTPS_WANT_CONNECT_OK:
1537 ret = connection_read_https_proxy_response(conn);
1538 if (ret == 1)
1539 conn->proxy_state = PROXY_CONNECTED;
1540 break;
1542 case PROXY_SOCKS4_WANT_CONNECT_OK:
1543 ret = fetch_from_buf_socks_client(conn->inbuf,
1544 conn->proxy_state,
1545 &reason);
1546 if (ret == 1)
1547 conn->proxy_state = PROXY_CONNECTED;
1548 break;
1550 case PROXY_SOCKS5_WANT_AUTH_METHOD_NONE:
1551 ret = fetch_from_buf_socks_client(conn->inbuf,
1552 conn->proxy_state,
1553 &reason);
1554 /* no auth needed, do connect */
1555 if (ret == 1) {
1556 connection_send_socks5_connect(conn);
1557 ret = 0;
1559 break;
1561 case PROXY_SOCKS5_WANT_AUTH_METHOD_RFC1929:
1562 ret = fetch_from_buf_socks_client(conn->inbuf,
1563 conn->proxy_state,
1564 &reason);
1566 /* send auth if needed, otherwise do connect */
1567 if (ret == 1) {
1568 connection_send_socks5_connect(conn);
1569 ret = 0;
1570 } else if (ret == 2) {
1571 unsigned char buf[1024];
1572 size_t reqsize, usize, psize;
1573 const char *user, *pass;
1575 user = get_options()->Socks5ProxyUsername;
1576 pass = get_options()->Socks5ProxyPassword;
1577 tor_assert(user && pass);
1579 /* XXX len of user and pass must be <= 255 !!! */
1580 usize = strlen(user);
1581 psize = strlen(pass);
1582 tor_assert(usize <= 255 && psize <= 255);
1583 reqsize = 3 + usize + psize;
1585 buf[0] = 1; /* negotiation version */
1586 buf[1] = usize;
1587 memcpy(buf + 2, user, usize);
1588 buf[2 + usize] = psize;
1589 memcpy(buf + 3 + usize, pass, psize);
1591 connection_write_to_buf((char *)buf, reqsize, conn);
1593 conn->proxy_state = PROXY_SOCKS5_WANT_AUTH_RFC1929_OK;
1594 ret = 0;
1596 break;
1598 case PROXY_SOCKS5_WANT_AUTH_RFC1929_OK:
1599 ret = fetch_from_buf_socks_client(conn->inbuf,
1600 conn->proxy_state,
1601 &reason);
1602 /* send the connect request */
1603 if (ret == 1) {
1604 connection_send_socks5_connect(conn);
1605 ret = 0;
1607 break;
1609 case PROXY_SOCKS5_WANT_CONNECT_OK:
1610 ret = fetch_from_buf_socks_client(conn->inbuf,
1611 conn->proxy_state,
1612 &reason);
1613 if (ret == 1)
1614 conn->proxy_state = PROXY_CONNECTED;
1615 break;
1617 default:
1618 log_err(LD_BUG, "Invalid proxy_state for reading, %d",
1619 conn->proxy_state);
1620 tor_fragile_assert();
1621 ret = -1;
1622 break;
1625 log_debug(LD_NET, "leaving state %s",
1626 connection_proxy_state_to_string(conn->proxy_state));
1628 if (ret < 0) {
1629 if (reason) {
1630 log_warn(LD_NET, "Proxy Client: unable to connect to %s:%d (%s)",
1631 conn->address, conn->port, escaped(reason));
1632 tor_free(reason);
1633 } else {
1634 log_warn(LD_NET, "Proxy Client: unable to connect to %s:%d",
1635 conn->address, conn->port);
1637 } else if (ret == 1) {
1638 log_info(LD_NET, "Proxy Client: connection to %s:%d successful",
1639 conn->address, conn->port);
1642 return ret;
1646 * Launch any configured listener connections of type <b>type</b>. (A
1647 * listener is configured if <b>port_option</b> is non-zero. If any
1648 * ListenAddress configuration options are given in <b>cfg</b>, create a
1649 * connection binding to each one. Otherwise, create a single
1650 * connection binding to the address <b>default_addr</b>.)
1652 * Only launch the listeners of this type that are not already open, and
1653 * only close listeners that are no longer wanted. Existing listeners
1654 * that are still configured are not touched.
1656 * If <b>disable_all_conns</b> is set, then never open new conns, and
1657 * close the existing ones.
1659 * Add all old conns that should be closed to <b>replaced_conns</b>.
1660 * Add all new connections to <b>new_conns</b>.
1662 static int
1663 retry_listeners(int type, config_line_t *cfg,
1664 int port_option, const char *default_addr,
1665 smartlist_t *replaced_conns,
1666 smartlist_t *new_conns,
1667 int disable_all_conns,
1668 int socket_family)
1670 smartlist_t *launch = smartlist_create(), *conns;
1671 int free_launch_elts = 1;
1672 int r;
1673 config_line_t *c;
1674 connection_t *conn;
1675 config_line_t *line;
1677 tor_assert(socket_family == AF_INET || socket_family == AF_UNIX);
1679 if (cfg && port_option) {
1680 for (c = cfg; c; c = c->next) {
1681 smartlist_add(launch, c);
1683 free_launch_elts = 0;
1684 } else if (port_option) {
1685 line = tor_malloc_zero(sizeof(config_line_t));
1686 line->key = tor_strdup("");
1687 line->value = tor_strdup(default_addr);
1688 smartlist_add(launch, line);
1692 SMARTLIST_FOREACH(launch, config_line_t *, l,
1693 log_fn(LOG_NOTICE, "#%s#%s", l->key, l->value));
1696 conns = get_connection_array();
1697 SMARTLIST_FOREACH(conns, connection_t *, conn,
1699 if (conn->type != type ||
1700 conn->socket_family != socket_family ||
1701 conn->marked_for_close)
1702 continue;
1703 /* Okay, so this is a listener. Is it configured? */
1704 line = NULL;
1705 SMARTLIST_FOREACH(launch, config_line_t *, wanted,
1707 char *address=NULL;
1708 uint16_t port;
1709 switch (socket_family) {
1710 case AF_INET:
1711 if (!parse_addr_port(LOG_WARN,
1712 wanted->value, &address, NULL, &port)) {
1713 int addr_matches = !strcasecmp(address, conn->address);
1714 tor_free(address);
1715 if (! port)
1716 port = port_option;
1717 if (port == conn->port && addr_matches) {
1718 line = wanted;
1719 break;
1722 break;
1723 case AF_UNIX:
1724 if (!strcasecmp(wanted->value, conn->address)) {
1725 line = wanted;
1726 break;
1728 break;
1729 default:
1730 tor_assert(0);
1733 if (!line || disable_all_conns) {
1734 /* This one isn't configured. Close it. */
1735 log_notice(LD_NET, "Closing no-longer-configured %s on %s:%d",
1736 conn_type_to_string(type), conn->address, conn->port);
1737 if (replaced_conns) {
1738 smartlist_add(replaced_conns, conn);
1739 } else {
1740 connection_close_immediate(conn);
1741 connection_mark_for_close(conn);
1743 } else {
1744 /* It's configured; we don't need to launch it. */
1745 // log_debug(LD_NET, "Already have %s on %s:%d",
1746 // conn_type_to_string(type), conn->address, conn->port);
1747 smartlist_remove(launch, line);
1748 if (free_launch_elts)
1749 config_free_lines(line);
1753 /* Now open all the listeners that are configured but not opened. */
1754 r = 0;
1755 if (!disable_all_conns) {
1756 SMARTLIST_FOREACH_BEGIN(launch, config_line_t *, cfg_line) {
1757 char *address = NULL;
1758 struct sockaddr *listensockaddr;
1759 socklen_t listensocklen = 0;
1761 switch (socket_family) {
1762 case AF_INET:
1763 listensockaddr = (struct sockaddr *)
1764 create_inet_sockaddr(cfg_line->value,
1765 (uint16_t) port_option,
1766 &address, &listensocklen);
1767 break;
1768 case AF_UNIX:
1769 listensockaddr = (struct sockaddr *)
1770 create_unix_sockaddr(cfg_line->value,
1771 &address, &listensocklen);
1772 break;
1773 default:
1774 tor_assert(0);
1777 if (listensockaddr) {
1778 conn = connection_create_listener(listensockaddr, listensocklen,
1779 type, address);
1780 tor_free(listensockaddr);
1781 tor_free(address);
1782 } else
1783 conn = NULL;
1785 if (!conn) {
1786 r = -1;
1787 } else {
1788 if (new_conns)
1789 smartlist_add(new_conns, conn);
1791 } SMARTLIST_FOREACH_END(cfg_line);
1794 if (free_launch_elts) {
1795 SMARTLIST_FOREACH(launch, config_line_t *, cfg_line,
1796 config_free_lines(cfg_line));
1798 smartlist_free(launch);
1800 return r;
1803 /** Launch listeners for each port you should have open. Only launch
1804 * listeners who are not already open, and only close listeners we no longer
1805 * want.
1807 * Add all old conns that should be closed to <b>replaced_conns</b>.
1808 * Add all new connections to <b>new_conns</b>.
1811 retry_all_listeners(smartlist_t *replaced_conns,
1812 smartlist_t *new_conns)
1814 or_options_t *options = get_options();
1816 if (retry_listeners(CONN_TYPE_OR_LISTENER, options->ORListenAddress,
1817 options->ORPort, "0.0.0.0",
1818 replaced_conns, new_conns, options->ClientOnly,
1819 AF_INET)<0)
1820 return -1;
1821 if (retry_listeners(CONN_TYPE_DIR_LISTENER, options->DirListenAddress,
1822 options->DirPort, "0.0.0.0",
1823 replaced_conns, new_conns, options->ClientOnly,
1824 AF_INET)<0)
1825 return -1;
1826 if (retry_listeners(CONN_TYPE_AP_LISTENER, options->SocksListenAddress,
1827 options->SocksPort, "127.0.0.1",
1828 replaced_conns, new_conns, 0,
1829 AF_INET)<0)
1830 return -1;
1831 if (retry_listeners(CONN_TYPE_AP_TRANS_LISTENER, options->TransListenAddress,
1832 options->TransPort, "127.0.0.1",
1833 replaced_conns, new_conns, 0,
1834 AF_INET)<0)
1835 return -1;
1836 if (retry_listeners(CONN_TYPE_AP_NATD_LISTENER, options->NatdListenAddress,
1837 options->NatdPort, "127.0.0.1",
1838 replaced_conns, new_conns, 0,
1839 AF_INET)<0)
1840 return -1;
1841 if (retry_listeners(CONN_TYPE_AP_DNS_LISTENER, options->DNSListenAddress,
1842 options->DNSPort, "127.0.0.1",
1843 replaced_conns, new_conns, 0,
1844 AF_INET)<0)
1845 return -1;
1846 if (retry_listeners(CONN_TYPE_CONTROL_LISTENER,
1847 options->ControlListenAddress,
1848 options->ControlPort, "127.0.0.1",
1849 replaced_conns, new_conns, 0,
1850 AF_INET)<0)
1851 return -1;
1852 if (retry_listeners(CONN_TYPE_CONTROL_LISTENER,
1853 options->ControlSocket,
1854 options->ControlSocket ? 1 : 0, NULL,
1855 replaced_conns, new_conns, 0,
1856 AF_UNIX)<0)
1857 return -1;
1859 return 0;
1862 /** Return 1 if we should apply rate limiting to <b>conn</b>,
1863 * and 0 otherwise. Right now this just checks if it's an internal
1864 * IP address or an internal connection. */
1865 static int
1866 connection_is_rate_limited(connection_t *conn)
1868 if (conn->linked || /* internal connection */
1869 tor_addr_family(&conn->addr) == AF_UNSPEC || /* no address */
1870 tor_addr_is_internal(&conn->addr, 0)) /* internal address */
1871 return 0;
1872 else
1873 return 1;
1876 extern int global_read_bucket, global_write_bucket;
1877 extern int global_relayed_read_bucket, global_relayed_write_bucket;
1879 /** Did either global write bucket run dry last second? If so,
1880 * we are likely to run dry again this second, so be stingy with the
1881 * tokens we just put in. */
1882 static int write_buckets_empty_last_second = 0;
1884 /** How many seconds of no active local circuits will make the
1885 * connection revert to the "relayed" bandwidth class? */
1886 #define CLIENT_IDLE_TIME_FOR_PRIORITY 30
1888 /** Return 1 if <b>conn</b> should use tokens from the "relayed"
1889 * bandwidth rates, else 0. Currently, only OR conns with bandwidth
1890 * class 1, and directory conns that are serving data out, count.
1892 static int
1893 connection_counts_as_relayed_traffic(connection_t *conn, time_t now)
1895 if (conn->type == CONN_TYPE_OR &&
1896 TO_OR_CONN(conn)->client_used + CLIENT_IDLE_TIME_FOR_PRIORITY < now)
1897 return 1;
1898 if (conn->type == CONN_TYPE_DIR && DIR_CONN_IS_SERVER(conn))
1899 return 1;
1900 return 0;
1903 /** Helper function to decide how many bytes out of <b>global_bucket</b>
1904 * we're willing to use for this transaction. <b>base</b> is the size
1905 * of a cell on the network; <b>priority</b> says whether we should
1906 * write many of them or just a few; and <b>conn_bucket</b> (if
1907 * non-negative) provides an upper limit for our answer. */
1908 static ssize_t
1909 connection_bucket_round_robin(int base, int priority,
1910 ssize_t global_bucket, ssize_t conn_bucket)
1912 ssize_t at_most;
1913 ssize_t num_bytes_high = (priority ? 32 : 16) * base;
1914 ssize_t num_bytes_low = (priority ? 4 : 2) * base;
1916 /* Do a rudimentary round-robin so one circuit can't hog a connection.
1917 * Pick at most 32 cells, at least 4 cells if possible, and if we're in
1918 * the middle pick 1/8 of the available bandwidth. */
1919 at_most = global_bucket / 8;
1920 at_most -= (at_most % base); /* round down */
1921 if (at_most > num_bytes_high) /* 16 KB, or 8 KB for low-priority */
1922 at_most = num_bytes_high;
1923 else if (at_most < num_bytes_low) /* 2 KB, or 1 KB for low-priority */
1924 at_most = num_bytes_low;
1926 if (at_most > global_bucket)
1927 at_most = global_bucket;
1929 if (conn_bucket >= 0 && at_most > conn_bucket)
1930 at_most = conn_bucket;
1932 if (at_most < 0)
1933 return 0;
1934 return at_most;
1937 /** How many bytes at most can we read onto this connection? */
1938 static ssize_t
1939 connection_bucket_read_limit(connection_t *conn, time_t now)
1941 int base = connection_speaks_cells(conn) ?
1942 CELL_NETWORK_SIZE : RELAY_PAYLOAD_SIZE;
1943 int priority = conn->type != CONN_TYPE_DIR;
1944 int conn_bucket = -1;
1945 int global_bucket = global_read_bucket;
1947 if (connection_speaks_cells(conn)) {
1948 or_connection_t *or_conn = TO_OR_CONN(conn);
1949 if (conn->state == OR_CONN_STATE_OPEN)
1950 conn_bucket = or_conn->read_bucket;
1953 if (!connection_is_rate_limited(conn)) {
1954 /* be willing to read on local conns even if our buckets are empty */
1955 return conn_bucket>=0 ? conn_bucket : 1<<14;
1958 if (connection_counts_as_relayed_traffic(conn, now) &&
1959 global_relayed_read_bucket <= global_read_bucket)
1960 global_bucket = global_relayed_read_bucket;
1962 return connection_bucket_round_robin(base, priority,
1963 global_bucket, conn_bucket);
1966 /** How many bytes at most can we write onto this connection? */
1967 ssize_t
1968 connection_bucket_write_limit(connection_t *conn, time_t now)
1970 int base = connection_speaks_cells(conn) ?
1971 CELL_NETWORK_SIZE : RELAY_PAYLOAD_SIZE;
1972 int priority = conn->type != CONN_TYPE_DIR;
1973 int global_bucket = global_write_bucket;
1975 if (!connection_is_rate_limited(conn)) {
1976 /* be willing to write to local conns even if our buckets are empty */
1977 return conn->outbuf_flushlen;
1980 if (connection_counts_as_relayed_traffic(conn, now) &&
1981 global_relayed_write_bucket <= global_write_bucket)
1982 global_bucket = global_relayed_write_bucket;
1984 return connection_bucket_round_robin(base, priority, global_bucket,
1985 conn->outbuf_flushlen);
1988 /** Return 1 if the global write buckets are low enough that we
1989 * shouldn't send <b>attempt</b> bytes of low-priority directory stuff
1990 * out to <b>conn</b>. Else return 0.
1992 * Priority is 1 for v1 requests (directories and running-routers),
1993 * and 2 for v2 requests (statuses and descriptors). But see FFFF in
1994 * directory_handle_command_get() for why we don't use priority 2 yet.
1996 * There are a lot of parameters we could use here:
1997 * - global_relayed_write_bucket. Low is bad.
1998 * - global_write_bucket. Low is bad.
1999 * - bandwidthrate. Low is bad.
2000 * - bandwidthburst. Not a big factor?
2001 * - attempt. High is bad.
2002 * - total bytes queued on outbufs. High is bad. But I'm wary of
2003 * using this, since a few slow-flushing queues will pump up the
2004 * number without meaning what we meant to mean. What we really
2005 * mean is "total directory bytes added to outbufs recently", but
2006 * that's harder to quantify and harder to keep track of.
2009 global_write_bucket_low(connection_t *conn, size_t attempt, int priority)
2011 int smaller_bucket = global_write_bucket < global_relayed_write_bucket ?
2012 global_write_bucket : global_relayed_write_bucket;
2013 if (authdir_mode(get_options()) && priority>1)
2014 return 0; /* there's always room to answer v2 if we're an auth dir */
2016 if (!connection_is_rate_limited(conn))
2017 return 0; /* local conns don't get limited */
2019 if (smaller_bucket < (int)attempt)
2020 return 1; /* not enough space no matter the priority */
2022 if (write_buckets_empty_last_second)
2023 return 1; /* we're already hitting our limits, no more please */
2025 if (priority == 1) { /* old-style v1 query */
2026 /* Could we handle *two* of these requests within the next two seconds? */
2027 or_options_t *options = get_options();
2028 int64_t can_write = (int64_t)smaller_bucket
2029 + 2*(options->RelayBandwidthRate ? options->RelayBandwidthRate :
2030 options->BandwidthRate);
2031 if (can_write < 2*(int64_t)attempt)
2032 return 1;
2033 } else { /* v2 query */
2034 /* no further constraints yet */
2036 return 0;
2039 /** We just read num_read and wrote num_written onto conn.
2040 * Decrement buckets appropriately. */
2041 static void
2042 connection_buckets_decrement(connection_t *conn, time_t now,
2043 size_t num_read, size_t num_written)
2045 if (!connection_is_rate_limited(conn))
2046 return; /* local IPs are free */
2047 if (num_written >= INT_MAX || num_read >= INT_MAX) {
2048 log_err(LD_BUG, "Value out of range. num_read=%lu, num_written=%lu, "
2049 "connection type=%s, state=%s",
2050 (unsigned long)num_read, (unsigned long)num_written,
2051 conn_type_to_string(conn->type),
2052 conn_state_to_string(conn->type, conn->state));
2053 if (num_written >= INT_MAX) num_written = 1;
2054 if (num_read >= INT_MAX) num_read = 1;
2055 tor_fragile_assert();
2058 if (num_read > 0) {
2059 if (conn->type == CONN_TYPE_EXIT)
2060 rep_hist_note_exit_bytes_read(conn->port, num_read);
2061 rep_hist_note_bytes_read(num_read, now);
2063 if (num_written > 0) {
2064 if (conn->type == CONN_TYPE_EXIT)
2065 rep_hist_note_exit_bytes_written(conn->port, num_written);
2066 rep_hist_note_bytes_written(num_written, now);
2069 if (connection_counts_as_relayed_traffic(conn, now)) {
2070 global_relayed_read_bucket -= (int)num_read;
2071 global_relayed_write_bucket -= (int)num_written;
2073 global_read_bucket -= (int)num_read;
2074 global_write_bucket -= (int)num_written;
2075 if (connection_speaks_cells(conn) && conn->state == OR_CONN_STATE_OPEN)
2076 TO_OR_CONN(conn)->read_bucket -= (int)num_read;
2079 /** If we have exhausted our global buckets, or the buckets for conn,
2080 * stop reading. */
2081 static void
2082 connection_consider_empty_read_buckets(connection_t *conn)
2084 const char *reason;
2086 if (global_read_bucket <= 0) {
2087 reason = "global read bucket exhausted. Pausing.";
2088 } else if (connection_counts_as_relayed_traffic(conn, approx_time()) &&
2089 global_relayed_read_bucket <= 0) {
2090 reason = "global relayed read bucket exhausted. Pausing.";
2091 } else if (connection_speaks_cells(conn) &&
2092 conn->state == OR_CONN_STATE_OPEN &&
2093 TO_OR_CONN(conn)->read_bucket <= 0) {
2094 reason = "connection read bucket exhausted. Pausing.";
2095 } else
2096 return; /* all good, no need to stop it */
2098 LOG_FN_CONN(conn, (LOG_DEBUG, LD_NET, "%s", reason));
2099 conn->read_blocked_on_bw = 1;
2100 connection_stop_reading(conn);
2103 /** If we have exhausted our global buckets, or the buckets for conn,
2104 * stop writing. */
2105 static void
2106 connection_consider_empty_write_buckets(connection_t *conn)
2108 const char *reason;
2110 if (global_write_bucket <= 0) {
2111 reason = "global write bucket exhausted. Pausing.";
2112 } else if (connection_counts_as_relayed_traffic(conn, approx_time()) &&
2113 global_relayed_write_bucket <= 0) {
2114 reason = "global relayed write bucket exhausted. Pausing.";
2115 #if 0
2116 } else if (connection_speaks_cells(conn) &&
2117 conn->state == OR_CONN_STATE_OPEN &&
2118 TO_OR_CONN(conn)->write_bucket <= 0) {
2119 reason = "connection write bucket exhausted. Pausing.";
2120 #endif
2121 } else
2122 return; /* all good, no need to stop it */
2124 LOG_FN_CONN(conn, (LOG_DEBUG, LD_NET, "%s", reason));
2125 conn->write_blocked_on_bw = 1;
2126 connection_stop_writing(conn);
2129 /** Initialize the global read bucket to options-\>BandwidthBurst. */
2130 void
2131 connection_bucket_init(void)
2133 or_options_t *options = get_options();
2134 /* start it at max traffic */
2135 global_read_bucket = (int)options->BandwidthBurst;
2136 global_write_bucket = (int)options->BandwidthBurst;
2137 if (options->RelayBandwidthRate) {
2138 global_relayed_read_bucket = (int)options->RelayBandwidthBurst;
2139 global_relayed_write_bucket = (int)options->RelayBandwidthBurst;
2140 } else {
2141 global_relayed_read_bucket = (int)options->BandwidthBurst;
2142 global_relayed_write_bucket = (int)options->BandwidthBurst;
2146 /** Refill a single <b>bucket</b> called <b>name</b> with bandwidth rate
2147 * <b>rate</b> and bandwidth burst <b>burst</b>, assuming that
2148 * <b>seconds_elapsed</b> seconds have passed since the last call.
2150 static void
2151 connection_bucket_refill_helper(int *bucket, int rate, int burst,
2152 int seconds_elapsed, const char *name)
2154 int starting_bucket = *bucket;
2155 if (starting_bucket < burst && seconds_elapsed) {
2156 if (((burst - starting_bucket)/seconds_elapsed) < rate) {
2157 *bucket = burst; /* We would overflow the bucket; just set it to
2158 * the maximum. */
2159 } else {
2160 int incr = rate*seconds_elapsed;
2161 *bucket += incr;
2162 if (*bucket > burst || *bucket < starting_bucket) {
2163 /* If we overflow the burst, or underflow our starting bucket,
2164 * cap the bucket value to burst. */
2165 /* XXXX this might be redundant now, but it doesn't show up
2166 * in profiles. Remove it after analysis. */
2167 *bucket = burst;
2170 log(LOG_DEBUG, LD_NET,"%s now %d.", name, *bucket);
2174 /** A second has rolled over; increment buckets appropriately. */
2175 void
2176 connection_bucket_refill(int seconds_elapsed, time_t now)
2178 or_options_t *options = get_options();
2179 smartlist_t *conns = get_connection_array();
2180 int relayrate, relayburst;
2182 if (options->RelayBandwidthRate) {
2183 relayrate = (int)options->RelayBandwidthRate;
2184 relayburst = (int)options->RelayBandwidthBurst;
2185 } else {
2186 relayrate = (int)options->BandwidthRate;
2187 relayburst = (int)options->BandwidthBurst;
2190 tor_assert(seconds_elapsed >= 0);
2192 write_buckets_empty_last_second =
2193 global_relayed_write_bucket <= 0 || global_write_bucket <= 0;
2195 /* refill the global buckets */
2196 connection_bucket_refill_helper(&global_read_bucket,
2197 (int)options->BandwidthRate,
2198 (int)options->BandwidthBurst,
2199 seconds_elapsed, "global_read_bucket");
2200 connection_bucket_refill_helper(&global_write_bucket,
2201 (int)options->BandwidthRate,
2202 (int)options->BandwidthBurst,
2203 seconds_elapsed, "global_write_bucket");
2204 connection_bucket_refill_helper(&global_relayed_read_bucket,
2205 relayrate, relayburst, seconds_elapsed,
2206 "global_relayed_read_bucket");
2207 connection_bucket_refill_helper(&global_relayed_write_bucket,
2208 relayrate, relayburst, seconds_elapsed,
2209 "global_relayed_write_bucket");
2211 /* refill the per-connection buckets */
2212 SMARTLIST_FOREACH(conns, connection_t *, conn,
2214 if (connection_speaks_cells(conn)) {
2215 or_connection_t *or_conn = TO_OR_CONN(conn);
2216 if (connection_read_bucket_should_increase(or_conn)) {
2217 connection_bucket_refill_helper(&or_conn->read_bucket,
2218 or_conn->bandwidthrate,
2219 or_conn->bandwidthburst,
2220 seconds_elapsed,
2221 "or_conn->read_bucket");
2222 //log_fn(LOG_DEBUG,"Receiver bucket %d now %d.", i,
2223 // conn->read_bucket);
2227 if (conn->read_blocked_on_bw == 1 /* marked to turn reading back on now */
2228 && global_read_bucket > 0 /* and we're allowed to read */
2229 && (!connection_counts_as_relayed_traffic(conn, now) ||
2230 global_relayed_read_bucket > 0) /* even if we're relayed traffic */
2231 && (!connection_speaks_cells(conn) ||
2232 conn->state != OR_CONN_STATE_OPEN ||
2233 TO_OR_CONN(conn)->read_bucket > 0)) {
2234 /* and either a non-cell conn or a cell conn with non-empty bucket */
2235 LOG_FN_CONN(conn, (LOG_DEBUG,LD_NET,
2236 "waking up conn (fd %d) for read", conn->s));
2237 conn->read_blocked_on_bw = 0;
2238 connection_start_reading(conn);
2241 if (conn->write_blocked_on_bw == 1
2242 && global_write_bucket > 0 /* and we're allowed to write */
2243 && (!connection_counts_as_relayed_traffic(conn, now) ||
2244 global_relayed_write_bucket > 0)) {
2245 /* even if we're relayed traffic */
2246 LOG_FN_CONN(conn, (LOG_DEBUG,LD_NET,
2247 "waking up conn (fd %d) for write", conn->s));
2248 conn->write_blocked_on_bw = 0;
2249 connection_start_writing(conn);
2254 /** Is the receiver bucket for connection <b>conn</b> low enough that we
2255 * should add another pile of tokens to it?
2257 static int
2258 connection_read_bucket_should_increase(or_connection_t *conn)
2260 tor_assert(conn);
2262 if (conn->_base.state != OR_CONN_STATE_OPEN)
2263 return 0; /* only open connections play the rate limiting game */
2264 if (conn->read_bucket >= conn->bandwidthburst)
2265 return 0;
2267 return 1;
2270 /** Read bytes from conn-\>s and process them.
2272 * This function gets called from conn_read() in main.c, either
2273 * when poll() has declared that conn wants to read, or (for OR conns)
2274 * when there are pending TLS bytes.
2276 * It calls connection_read_to_buf() to bring in any new bytes,
2277 * and then calls connection_process_inbuf() to process them.
2279 * Mark the connection and return -1 if you want to close it, else
2280 * return 0.
2282 static int
2283 connection_handle_read_impl(connection_t *conn)
2285 int max_to_read=-1, try_to_read;
2286 size_t before, n_read = 0;
2287 int socket_error = 0;
2289 if (conn->marked_for_close)
2290 return 0; /* do nothing */
2292 conn->timestamp_lastread = approx_time();
2294 switch (conn->type) {
2295 case CONN_TYPE_OR_LISTENER:
2296 return connection_handle_listener_read(conn, CONN_TYPE_OR);
2297 case CONN_TYPE_AP_LISTENER:
2298 case CONN_TYPE_AP_TRANS_LISTENER:
2299 case CONN_TYPE_AP_NATD_LISTENER:
2300 return connection_handle_listener_read(conn, CONN_TYPE_AP);
2301 case CONN_TYPE_DIR_LISTENER:
2302 return connection_handle_listener_read(conn, CONN_TYPE_DIR);
2303 case CONN_TYPE_CONTROL_LISTENER:
2304 return connection_handle_listener_read(conn, CONN_TYPE_CONTROL);
2305 case CONN_TYPE_AP_DNS_LISTENER:
2306 /* This should never happen; eventdns.c handles the reads here. */
2307 tor_fragile_assert();
2308 return 0;
2311 loop_again:
2312 try_to_read = max_to_read;
2313 tor_assert(!conn->marked_for_close);
2315 before = buf_datalen(conn->inbuf);
2316 if (connection_read_to_buf(conn, &max_to_read, &socket_error) < 0) {
2317 /* There's a read error; kill the connection.*/
2318 if (conn->type == CONN_TYPE_OR &&
2319 conn->state == OR_CONN_STATE_CONNECTING) {
2320 connection_or_connect_failed(TO_OR_CONN(conn),
2321 errno_to_orconn_end_reason(socket_error),
2322 tor_socket_strerror(socket_error));
2324 if (CONN_IS_EDGE(conn)) {
2325 edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
2326 connection_edge_end_errno(edge_conn);
2327 if (edge_conn->socks_request) /* broken, don't send a socks reply back */
2328 edge_conn->socks_request->has_finished = 1;
2330 connection_close_immediate(conn); /* Don't flush; connection is dead. */
2331 connection_mark_for_close(conn);
2332 return -1;
2334 n_read += buf_datalen(conn->inbuf) - before;
2335 if (CONN_IS_EDGE(conn) && try_to_read != max_to_read) {
2336 /* instruct it not to try to package partial cells. */
2337 if (connection_process_inbuf(conn, 0) < 0) {
2338 return -1;
2340 if (!conn->marked_for_close &&
2341 connection_is_reading(conn) &&
2342 !conn->inbuf_reached_eof &&
2343 max_to_read > 0)
2344 goto loop_again; /* try reading again, in case more is here now */
2346 /* one last try, packaging partial cells and all. */
2347 if (!conn->marked_for_close &&
2348 connection_process_inbuf(conn, 1) < 0) {
2349 return -1;
2351 if (conn->linked_conn) {
2352 /* The other side's handle_write() will never actually get called, so
2353 * we need to invoke the appropriate callbacks ourself. */
2354 connection_t *linked = conn->linked_conn;
2356 if (n_read) {
2357 /* Probably a no-op, but hey. */
2358 connection_buckets_decrement(linked, approx_time(), n_read, 0);
2360 if (connection_flushed_some(linked) < 0)
2361 connection_mark_for_close(linked);
2362 if (!connection_wants_to_flush(linked))
2363 connection_finished_flushing(linked);
2366 if (!buf_datalen(linked->outbuf) && conn->active_on_link)
2367 connection_stop_reading_from_linked_conn(conn);
2369 /* If we hit the EOF, call connection_reached_eof(). */
2370 if (!conn->marked_for_close &&
2371 conn->inbuf_reached_eof &&
2372 connection_reached_eof(conn) < 0) {
2373 return -1;
2375 return 0;
2379 connection_handle_read(connection_t *conn)
2381 int res;
2383 tor_gettimeofday_cache_clear();
2384 res = connection_handle_read_impl(conn);
2385 return res;
2388 /** Pull in new bytes from conn-\>s or conn-\>linked_conn onto conn-\>inbuf,
2389 * either directly or via TLS. Reduce the token buckets by the number of bytes
2390 * read.
2392 * If *max_to_read is -1, then decide it ourselves, else go with the
2393 * value passed to us. When returning, if it's changed, subtract the
2394 * number of bytes we read from *max_to_read.
2396 * Return -1 if we want to break conn, else return 0.
2398 static int
2399 connection_read_to_buf(connection_t *conn, int *max_to_read, int *socket_error)
2401 int result;
2402 ssize_t at_most = *max_to_read;
2403 size_t slack_in_buf, more_to_read;
2404 size_t n_read = 0, n_written = 0;
2406 if (at_most == -1) { /* we need to initialize it */
2407 /* how many bytes are we allowed to read? */
2408 at_most = connection_bucket_read_limit(conn, approx_time());
2411 slack_in_buf = buf_slack(conn->inbuf);
2412 again:
2413 if ((size_t)at_most > slack_in_buf && slack_in_buf >= 1024) {
2414 more_to_read = at_most - slack_in_buf;
2415 at_most = slack_in_buf;
2416 } else {
2417 more_to_read = 0;
2420 if (connection_speaks_cells(conn) &&
2421 conn->state > OR_CONN_STATE_PROXY_HANDSHAKING) {
2422 int pending;
2423 or_connection_t *or_conn = TO_OR_CONN(conn);
2424 size_t initial_size;
2425 if (conn->state == OR_CONN_STATE_TLS_HANDSHAKING ||
2426 conn->state == OR_CONN_STATE_TLS_CLIENT_RENEGOTIATING) {
2427 /* continue handshaking even if global token bucket is empty */
2428 return connection_tls_continue_handshake(or_conn);
2431 log_debug(LD_NET,
2432 "%d: starting, inbuf_datalen %ld (%d pending in tls object)."
2433 " at_most %ld.",
2434 conn->s,(long)buf_datalen(conn->inbuf),
2435 tor_tls_get_pending_bytes(or_conn->tls), (long)at_most);
2437 initial_size = buf_datalen(conn->inbuf);
2438 /* else open, or closing */
2439 result = read_to_buf_tls(or_conn->tls, at_most, conn->inbuf);
2440 if (TOR_TLS_IS_ERROR(result) || result == TOR_TLS_CLOSE)
2441 or_conn->tls_error = result;
2442 else
2443 or_conn->tls_error = 0;
2445 switch (result) {
2446 case TOR_TLS_CLOSE:
2447 case TOR_TLS_ERROR_IO:
2448 log_debug(LD_NET,"TLS connection closed %son read. Closing. "
2449 "(Nickname %s, address %s)",
2450 result == TOR_TLS_CLOSE ? "cleanly " : "",
2451 or_conn->nickname ? or_conn->nickname : "not set",
2452 conn->address);
2453 return result;
2454 CASE_TOR_TLS_ERROR_ANY_NONIO:
2455 log_debug(LD_NET,"tls error [%s]. breaking (nickname %s, address %s).",
2456 tor_tls_err_to_string(result),
2457 or_conn->nickname ? or_conn->nickname : "not set",
2458 conn->address);
2459 return result;
2460 case TOR_TLS_WANTWRITE:
2461 connection_start_writing(conn);
2462 return 0;
2463 case TOR_TLS_WANTREAD: /* we're already reading */
2464 case TOR_TLS_DONE: /* no data read, so nothing to process */
2465 result = 0;
2466 break; /* so we call bucket_decrement below */
2467 default:
2468 break;
2470 pending = tor_tls_get_pending_bytes(or_conn->tls);
2471 if (pending) {
2472 /* If we have any pending bytes, we read them now. This *can*
2473 * take us over our read allotment, but really we shouldn't be
2474 * believing that SSL bytes are the same as TCP bytes anyway. */
2475 int r2 = read_to_buf_tls(or_conn->tls, pending, conn->inbuf);
2476 if (r2<0) {
2477 log_warn(LD_BUG, "apparently, reading pending bytes can fail.");
2478 return -1;
2481 result = (int)(buf_datalen(conn->inbuf)-initial_size);
2482 tor_tls_get_n_raw_bytes(or_conn->tls, &n_read, &n_written);
2483 log_debug(LD_GENERAL, "After TLS read of %d: %ld read, %ld written",
2484 result, (long)n_read, (long)n_written);
2485 } else if (conn->linked) {
2486 if (conn->linked_conn) {
2487 result = move_buf_to_buf(conn->inbuf, conn->linked_conn->outbuf,
2488 &conn->linked_conn->outbuf_flushlen);
2489 } else {
2490 result = 0;
2492 //log_notice(LD_GENERAL, "Moved %d bytes on an internal link!", result);
2493 /* If the other side has disappeared, or if it's been marked for close and
2494 * we flushed its outbuf, then we should set our inbuf_reached_eof. */
2495 if (!conn->linked_conn ||
2496 (conn->linked_conn->marked_for_close &&
2497 buf_datalen(conn->linked_conn->outbuf) == 0))
2498 conn->inbuf_reached_eof = 1;
2500 n_read = (size_t) result;
2501 } else {
2502 /* !connection_speaks_cells, !conn->linked_conn. */
2503 int reached_eof = 0;
2504 CONN_LOG_PROTECT(conn,
2505 result = read_to_buf(conn->s, at_most, conn->inbuf, &reached_eof,
2506 socket_error));
2507 if (reached_eof)
2508 conn->inbuf_reached_eof = 1;
2510 // log_fn(LOG_DEBUG,"read_to_buf returned %d.",read_result);
2512 if (result < 0)
2513 return -1;
2514 n_read = (size_t) result;
2517 if (n_read > 0) { /* change *max_to_read */
2518 /*XXXX021 check for overflow*/
2519 *max_to_read = (int)(at_most - n_read);
2522 if (conn->type == CONN_TYPE_AP) {
2523 edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
2524 /*XXXX021 check for overflow*/
2525 edge_conn->n_read += (int)n_read;
2528 connection_buckets_decrement(conn, approx_time(), n_read, n_written);
2530 if (more_to_read && result == at_most) {
2531 slack_in_buf = buf_slack(conn->inbuf);
2532 at_most = more_to_read;
2533 goto again;
2536 /* Call even if result is 0, since the global read bucket may
2537 * have reached 0 on a different conn, and this guy needs to
2538 * know to stop reading. */
2539 connection_consider_empty_read_buckets(conn);
2540 if (n_written > 0 && connection_is_writing(conn))
2541 connection_consider_empty_write_buckets(conn);
2543 return 0;
2546 /** A pass-through to fetch_from_buf. */
2548 connection_fetch_from_buf(char *string, size_t len, connection_t *conn)
2550 return fetch_from_buf(string, len, conn->inbuf);
2553 /** Return conn-\>outbuf_flushlen: how many bytes conn wants to flush
2554 * from its outbuf. */
2556 connection_wants_to_flush(connection_t *conn)
2558 return conn->outbuf_flushlen > 0;
2561 /** Are there too many bytes on edge connection <b>conn</b>'s outbuf to
2562 * send back a relay-level sendme yet? Return 1 if so, 0 if not. Used by
2563 * connection_edge_consider_sending_sendme().
2566 connection_outbuf_too_full(connection_t *conn)
2568 return (conn->outbuf_flushlen > 10*CELL_PAYLOAD_SIZE);
2571 /** Try to flush more bytes onto conn-\>s.
2573 * This function gets called either from conn_write() in main.c
2574 * when poll() has declared that conn wants to write, or below
2575 * from connection_write_to_buf() when an entire TLS record is ready.
2577 * Update conn-\>timestamp_lastwritten to now, and call flush_buf
2578 * or flush_buf_tls appropriately. If it succeeds and there are no more
2579 * more bytes on conn->outbuf, then call connection_finished_flushing
2580 * on it too.
2582 * If <b>force</b>, then write as many bytes as possible, ignoring bandwidth
2583 * limits. (Used for flushing messages to controller connections on fatal
2584 * errors.)
2586 * Mark the connection and return -1 if you want to close it, else
2587 * return 0.
2589 static int
2590 connection_handle_write_impl(connection_t *conn, int force)
2592 int e;
2593 socklen_t len=(socklen_t)sizeof(e);
2594 int result;
2595 ssize_t max_to_write;
2596 time_t now = approx_time();
2597 size_t n_read = 0, n_written = 0;
2599 tor_assert(!connection_is_listener(conn));
2601 if (conn->marked_for_close || conn->s < 0)
2602 return 0; /* do nothing */
2604 if (conn->in_flushed_some) {
2605 log_warn(LD_BUG, "called recursively from inside conn->in_flushed_some");
2606 return 0;
2609 conn->timestamp_lastwritten = now;
2611 /* Sometimes, "writable" means "connected". */
2612 if (connection_state_is_connecting(conn)) {
2613 if (getsockopt(conn->s, SOL_SOCKET, SO_ERROR, (void*)&e, &len) < 0) {
2614 log_warn(LD_BUG,
2615 "getsockopt() syscall failed?! Please report to tor-ops.");
2616 if (CONN_IS_EDGE(conn))
2617 connection_edge_end_errno(TO_EDGE_CONN(conn));
2618 connection_mark_for_close(conn);
2619 return -1;
2621 if (e) {
2622 /* some sort of error, but maybe just inprogress still */
2623 if (!ERRNO_IS_CONN_EINPROGRESS(e)) {
2624 log_info(LD_NET,"in-progress connect failed. Removing. (%s)",
2625 tor_socket_strerror(e));
2626 if (CONN_IS_EDGE(conn))
2627 connection_edge_end_errno(TO_EDGE_CONN(conn));
2628 if (conn->type == CONN_TYPE_OR)
2629 connection_or_connect_failed(TO_OR_CONN(conn),
2630 errno_to_orconn_end_reason(e),
2631 tor_socket_strerror(e));
2633 connection_close_immediate(conn);
2634 connection_mark_for_close(conn);
2635 return -1;
2636 } else {
2637 return 0; /* no change, see if next time is better */
2640 /* The connection is successful. */
2641 if (connection_finished_connecting(conn)<0)
2642 return -1;
2645 max_to_write = force ? (ssize_t)conn->outbuf_flushlen
2646 : connection_bucket_write_limit(conn, now);
2648 if (connection_speaks_cells(conn) &&
2649 conn->state > OR_CONN_STATE_PROXY_HANDSHAKING) {
2650 or_connection_t *or_conn = TO_OR_CONN(conn);
2651 if (conn->state == OR_CONN_STATE_TLS_HANDSHAKING ||
2652 conn->state == OR_CONN_STATE_TLS_CLIENT_RENEGOTIATING) {
2653 connection_stop_writing(conn);
2654 if (connection_tls_continue_handshake(or_conn) < 0) {
2655 /* Don't flush; connection is dead. */
2656 connection_close_immediate(conn);
2657 connection_mark_for_close(conn);
2658 return -1;
2660 return 0;
2661 } else if (conn->state == OR_CONN_STATE_TLS_SERVER_RENEGOTIATING) {
2662 return connection_handle_read(conn);
2665 /* else open, or closing */
2666 result = flush_buf_tls(or_conn->tls, conn->outbuf,
2667 max_to_write, &conn->outbuf_flushlen);
2669 /* If we just flushed the last bytes, check if this tunneled dir
2670 * request is done. */
2671 if (buf_datalen(conn->outbuf) == 0 && conn->dirreq_id)
2672 geoip_change_dirreq_state(conn->dirreq_id, DIRREQ_TUNNELED,
2673 DIRREQ_OR_CONN_BUFFER_FLUSHED);
2675 switch (result) {
2676 CASE_TOR_TLS_ERROR_ANY:
2677 case TOR_TLS_CLOSE:
2678 log_info(LD_NET,result!=TOR_TLS_CLOSE?
2679 "tls error. breaking.":"TLS connection closed on flush");
2680 /* Don't flush; connection is dead. */
2681 connection_close_immediate(conn);
2682 connection_mark_for_close(conn);
2683 return -1;
2684 case TOR_TLS_WANTWRITE:
2685 log_debug(LD_NET,"wanted write.");
2686 /* we're already writing */
2687 return 0;
2688 case TOR_TLS_WANTREAD:
2689 /* Make sure to avoid a loop if the receive buckets are empty. */
2690 log_debug(LD_NET,"wanted read.");
2691 if (!connection_is_reading(conn)) {
2692 connection_stop_writing(conn);
2693 conn->write_blocked_on_bw = 1;
2694 /* we'll start reading again when we get more tokens in our
2695 * read bucket; then we'll start writing again too.
2698 /* else no problem, we're already reading */
2699 return 0;
2700 /* case TOR_TLS_DONE:
2701 * for TOR_TLS_DONE, fall through to check if the flushlen
2702 * is empty, so we can stop writing.
2706 tor_tls_get_n_raw_bytes(or_conn->tls, &n_read, &n_written);
2707 log_debug(LD_GENERAL, "After TLS write of %d: %ld read, %ld written",
2708 result, (long)n_read, (long)n_written);
2709 } else {
2710 CONN_LOG_PROTECT(conn,
2711 result = flush_buf(conn->s, conn->outbuf,
2712 max_to_write, &conn->outbuf_flushlen));
2713 if (result < 0) {
2714 if (CONN_IS_EDGE(conn))
2715 connection_edge_end_errno(TO_EDGE_CONN(conn));
2717 connection_close_immediate(conn); /* Don't flush; connection is dead. */
2718 connection_mark_for_close(conn);
2719 return -1;
2721 n_written = (size_t) result;
2724 if (conn->type == CONN_TYPE_AP) {
2725 edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
2726 /*XXXX021 check for overflow.*/
2727 edge_conn->n_written += (int)n_written;
2730 connection_buckets_decrement(conn, approx_time(), n_read, n_written);
2732 if (result > 0) {
2733 /* If we wrote any bytes from our buffer, then call the appropriate
2734 * functions. */
2735 if (connection_flushed_some(conn) < 0)
2736 connection_mark_for_close(conn);
2739 if (!connection_wants_to_flush(conn)) { /* it's done flushing */
2740 if (connection_finished_flushing(conn) < 0) {
2741 /* already marked */
2742 return -1;
2744 return 0;
2747 /* Call even if result is 0, since the global write bucket may
2748 * have reached 0 on a different conn, and this guy needs to
2749 * know to stop writing. */
2750 connection_consider_empty_write_buckets(conn);
2751 if (n_read > 0 && connection_is_reading(conn))
2752 connection_consider_empty_read_buckets(conn);
2754 return 0;
2758 connection_handle_write(connection_t *conn, int force)
2760 int res;
2761 tor_gettimeofday_cache_clear();
2762 res = connection_handle_write_impl(conn, force);
2763 return res;
2766 /** OpenSSL TLS record size is 16383; this is close. The goal here is to
2767 * push data out as soon as we know there's enough for a TLS record, so
2768 * during periods of high load we won't read entire megabytes from
2769 * input before pushing any data out. It also has the feature of not
2770 * growing huge outbufs unless something is slow. */
2771 #define MIN_TLS_FLUSHLEN 15872
2773 /** Append <b>len</b> bytes of <b>string</b> onto <b>conn</b>'s
2774 * outbuf, and ask it to start writing.
2776 * If <b>zlib</b> is nonzero, this is a directory connection that should get
2777 * its contents compressed or decompressed as they're written. If zlib is
2778 * negative, this is the last data to be compressed, and the connection's zlib
2779 * state should be flushed.
2781 * If it's an OR conn and an entire TLS record is ready, then try to
2782 * flush the record now. Similarly, if it's a local control connection
2783 * and a 64k chunk is ready, try to flush it all, so we don't end up with
2784 * many megabytes of controller info queued at once.
2786 void
2787 _connection_write_to_buf_impl(const char *string, size_t len,
2788 connection_t *conn, int zlib)
2790 /* XXXX This function really needs to return -1 on failure. */
2791 int r;
2792 size_t old_datalen;
2793 if (!len && !(zlib<0))
2794 return;
2795 /* if it's marked for close, only allow write if we mean to flush it */
2796 if (conn->marked_for_close && !conn->hold_open_until_flushed)
2797 return;
2799 old_datalen = buf_datalen(conn->outbuf);
2800 if (zlib) {
2801 dir_connection_t *dir_conn = TO_DIR_CONN(conn);
2802 int done = zlib < 0;
2803 CONN_LOG_PROTECT(conn, r = write_to_buf_zlib(conn->outbuf,
2804 dir_conn->zlib_state,
2805 string, len, done));
2806 } else {
2807 CONN_LOG_PROTECT(conn, r = write_to_buf(string, len, conn->outbuf));
2809 if (r < 0) {
2810 if (CONN_IS_EDGE(conn)) {
2811 /* if it failed, it means we have our package/delivery windows set
2812 wrong compared to our max outbuf size. close the whole circuit. */
2813 log_warn(LD_NET,
2814 "write_to_buf failed. Closing circuit (fd %d).", conn->s);
2815 circuit_mark_for_close(circuit_get_by_edge_conn(TO_EDGE_CONN(conn)),
2816 END_CIRC_REASON_INTERNAL);
2817 } else {
2818 log_warn(LD_NET,
2819 "write_to_buf failed. Closing connection (fd %d).", conn->s);
2820 connection_mark_for_close(conn);
2822 return;
2825 connection_start_writing(conn);
2826 if (zlib) {
2827 conn->outbuf_flushlen += buf_datalen(conn->outbuf) - old_datalen;
2828 } else {
2829 ssize_t extra = 0;
2830 conn->outbuf_flushlen += len;
2832 /* Should we try flushing the outbuf now? */
2833 if (conn->in_flushed_some) {
2834 /* Don't flush the outbuf when the reason we're writing more stuff is
2835 * _because_ we flushed the outbuf. That's unfair. */
2836 return;
2839 if (conn->type == CONN_TYPE_OR &&
2840 conn->outbuf_flushlen-len < MIN_TLS_FLUSHLEN &&
2841 conn->outbuf_flushlen >= MIN_TLS_FLUSHLEN) {
2842 /* We just pushed outbuf_flushlen to MIN_TLS_FLUSHLEN or above;
2843 * we can send out a full TLS frame now if we like. */
2844 extra = conn->outbuf_flushlen - MIN_TLS_FLUSHLEN;
2845 conn->outbuf_flushlen = MIN_TLS_FLUSHLEN;
2846 } else if (conn->type == CONN_TYPE_CONTROL &&
2847 !connection_is_rate_limited(conn) &&
2848 conn->outbuf_flushlen-len < 1<<16 &&
2849 conn->outbuf_flushlen >= 1<<16) {
2850 /* just try to flush all of it */
2851 } else
2852 return; /* no need to try flushing */
2854 if (connection_handle_write(conn, 0) < 0) {
2855 if (!conn->marked_for_close) {
2856 /* this connection is broken. remove it. */
2857 log_warn(LD_BUG, "unhandled error on write for "
2858 "conn (type %d, fd %d); removing",
2859 conn->type, conn->s);
2860 tor_fragile_assert();
2861 /* do a close-immediate here, so we don't try to flush */
2862 connection_close_immediate(conn);
2864 return;
2866 if (extra) {
2867 conn->outbuf_flushlen += extra;
2868 connection_start_writing(conn);
2873 /** Return a connection with given type, address, port, and purpose;
2874 * or NULL if no such connection exists. */
2875 connection_t *
2876 connection_get_by_type_addr_port_purpose(int type,
2877 const tor_addr_t *addr, uint16_t port,
2878 int purpose)
2880 smartlist_t *conns = get_connection_array();
2881 SMARTLIST_FOREACH(conns, connection_t *, conn,
2883 if (conn->type == type &&
2884 tor_addr_eq(&conn->addr, addr) &&
2885 conn->port == port &&
2886 conn->purpose == purpose &&
2887 !conn->marked_for_close)
2888 return conn;
2890 return NULL;
2893 /** Return the stream with id <b>id</b> if it is not already marked for
2894 * close.
2896 connection_t *
2897 connection_get_by_global_id(uint64_t id)
2899 smartlist_t *conns = get_connection_array();
2900 SMARTLIST_FOREACH(conns, connection_t *, conn,
2902 if (conn->global_identifier == id)
2903 return conn;
2905 return NULL;
2908 /** Return a connection of type <b>type</b> that is not marked for close.
2910 connection_t *
2911 connection_get_by_type(int type)
2913 smartlist_t *conns = get_connection_array();
2914 SMARTLIST_FOREACH(conns, connection_t *, conn,
2916 if (conn->type == type && !conn->marked_for_close)
2917 return conn;
2919 return NULL;
2922 /** Return a connection of type <b>type</b> that is in state <b>state</b>,
2923 * and that is not marked for close.
2925 connection_t *
2926 connection_get_by_type_state(int type, int state)
2928 smartlist_t *conns = get_connection_array();
2929 SMARTLIST_FOREACH(conns, connection_t *, conn,
2931 if (conn->type == type && conn->state == state && !conn->marked_for_close)
2932 return conn;
2934 return NULL;
2937 /** Return a connection of type <b>type</b> that has rendquery equal
2938 * to <b>rendquery</b>, and that is not marked for close. If state
2939 * is non-zero, conn must be of that state too.
2941 connection_t *
2942 connection_get_by_type_state_rendquery(int type, int state,
2943 const char *rendquery)
2945 smartlist_t *conns = get_connection_array();
2947 tor_assert(type == CONN_TYPE_DIR ||
2948 type == CONN_TYPE_AP || type == CONN_TYPE_EXIT);
2949 tor_assert(rendquery);
2951 SMARTLIST_FOREACH(conns, connection_t *, conn,
2953 if (conn->type == type &&
2954 !conn->marked_for_close &&
2955 (!state || state == conn->state)) {
2956 if (type == CONN_TYPE_DIR &&
2957 TO_DIR_CONN(conn)->rend_data &&
2958 !rend_cmp_service_ids(rendquery,
2959 TO_DIR_CONN(conn)->rend_data->onion_address))
2960 return conn;
2961 else if (CONN_IS_EDGE(conn) &&
2962 TO_EDGE_CONN(conn)->rend_data &&
2963 !rend_cmp_service_ids(rendquery,
2964 TO_EDGE_CONN(conn)->rend_data->onion_address))
2965 return conn;
2968 return NULL;
2971 /** Return an open, non-marked connection of a given type and purpose, or NULL
2972 * if no such connection exists. */
2973 connection_t *
2974 connection_get_by_type_purpose(int type, int purpose)
2976 smartlist_t *conns = get_connection_array();
2977 SMARTLIST_FOREACH(conns, connection_t *, conn,
2979 if (conn->type == type &&
2980 !conn->marked_for_close &&
2981 (purpose == conn->purpose))
2982 return conn;
2984 return NULL;
2987 /** Return 1 if <b>conn</b> is a listener conn, else return 0. */
2989 connection_is_listener(connection_t *conn)
2991 if (conn->type == CONN_TYPE_OR_LISTENER ||
2992 conn->type == CONN_TYPE_AP_LISTENER ||
2993 conn->type == CONN_TYPE_AP_TRANS_LISTENER ||
2994 conn->type == CONN_TYPE_AP_DNS_LISTENER ||
2995 conn->type == CONN_TYPE_AP_NATD_LISTENER ||
2996 conn->type == CONN_TYPE_DIR_LISTENER ||
2997 conn->type == CONN_TYPE_CONTROL_LISTENER)
2998 return 1;
2999 return 0;
3002 /** Return 1 if <b>conn</b> is in state "open" and is not marked
3003 * for close, else return 0.
3006 connection_state_is_open(connection_t *conn)
3008 tor_assert(conn);
3010 if (conn->marked_for_close)
3011 return 0;
3013 if ((conn->type == CONN_TYPE_OR && conn->state == OR_CONN_STATE_OPEN) ||
3014 (conn->type == CONN_TYPE_AP && conn->state == AP_CONN_STATE_OPEN) ||
3015 (conn->type == CONN_TYPE_EXIT && conn->state == EXIT_CONN_STATE_OPEN) ||
3016 (conn->type == CONN_TYPE_CONTROL &&
3017 conn->state == CONTROL_CONN_STATE_OPEN))
3018 return 1;
3020 return 0;
3023 /** Return 1 if conn is in 'connecting' state, else return 0. */
3025 connection_state_is_connecting(connection_t *conn)
3027 tor_assert(conn);
3029 if (conn->marked_for_close)
3030 return 0;
3031 switch (conn->type)
3033 case CONN_TYPE_OR:
3034 return conn->state == OR_CONN_STATE_CONNECTING;
3035 case CONN_TYPE_EXIT:
3036 return conn->state == EXIT_CONN_STATE_CONNECTING;
3037 case CONN_TYPE_DIR:
3038 return conn->state == DIR_CONN_STATE_CONNECTING;
3041 return 0;
3044 /** Allocates a base64'ed authenticator for use in http or https
3045 * auth, based on the input string <b>authenticator</b>. Returns it
3046 * if success, else returns NULL. */
3047 char *
3048 alloc_http_authenticator(const char *authenticator)
3050 /* an authenticator in Basic authentication
3051 * is just the string "username:password" */
3052 const size_t authenticator_length = strlen(authenticator);
3053 /* The base64_encode function needs a minimum buffer length
3054 * of 66 bytes. */
3055 const size_t base64_authenticator_length = (authenticator_length/48+1)*66;
3056 char *base64_authenticator = tor_malloc(base64_authenticator_length);
3057 if (base64_encode(base64_authenticator, base64_authenticator_length,
3058 authenticator, authenticator_length) < 0) {
3059 tor_free(base64_authenticator); /* free and set to null */
3060 } else {
3061 /* remove extra \n at end of encoding */
3062 base64_authenticator[strlen(base64_authenticator) - 1] = 0;
3064 return base64_authenticator;
3067 /** Given a socket handle, check whether the local address (sockname) of the
3068 * socket is one that we've connected from before. If so, double-check
3069 * whether our address has changed and we need to generate keys. If we do,
3070 * call init_keys().
3072 static void
3073 client_check_address_changed(int sock)
3075 uint32_t iface_ip, ip_out;
3076 struct sockaddr_in out_addr;
3077 socklen_t out_addr_len = (socklen_t) sizeof(out_addr);
3078 uint32_t *ip;
3080 if (!last_interface_ip)
3081 get_interface_address(LOG_INFO, &last_interface_ip);
3082 if (!outgoing_addrs)
3083 outgoing_addrs = smartlist_create();
3085 if (getsockname(sock, (struct sockaddr*)&out_addr, &out_addr_len)<0) {
3086 int e = tor_socket_errno(sock);
3087 log_warn(LD_NET, "getsockname() to check for address change failed: %s",
3088 tor_socket_strerror(e));
3089 return;
3092 /* If we've used this address previously, we're okay. */
3093 ip_out = ntohl(out_addr.sin_addr.s_addr);
3094 SMARTLIST_FOREACH(outgoing_addrs, uint32_t*, ip_ptr,
3095 if (*ip_ptr == ip_out) return;
3098 /* Uh-oh. We haven't connected from this address before. Has the interface
3099 * address changed? */
3100 if (get_interface_address(LOG_INFO, &iface_ip)<0)
3101 return;
3102 ip = tor_malloc(sizeof(uint32_t));
3103 *ip = ip_out;
3105 if (iface_ip == last_interface_ip) {
3106 /* Nope, it hasn't changed. Add this address to the list. */
3107 smartlist_add(outgoing_addrs, ip);
3108 } else {
3109 /* The interface changed. We're a client, so we need to regenerate our
3110 * keys. First, reset the state. */
3111 log(LOG_NOTICE, LD_NET, "Our IP address has changed. Rotating keys...");
3112 last_interface_ip = iface_ip;
3113 SMARTLIST_FOREACH(outgoing_addrs, void*, ip_ptr, tor_free(ip_ptr));
3114 smartlist_clear(outgoing_addrs);
3115 smartlist_add(outgoing_addrs, ip);
3116 /* Okay, now change our keys. */
3117 ip_address_changed(1);
3121 /** Some systems have limited system buffers for recv and xmit on
3122 * sockets allocated in a virtual server or similar environment. For a Tor
3123 * server this can produce the "Error creating network socket: No buffer
3124 * space available" error once all available TCP buffer space is consumed.
3125 * This method will attempt to constrain the buffers allocated for the socket
3126 * to the desired size to stay below system TCP buffer limits.
3128 static void
3129 set_constrained_socket_buffers(int sock, int size)
3131 void *sz = (void*)&size;
3132 socklen_t sz_sz = (socklen_t) sizeof(size);
3133 if (setsockopt(sock, SOL_SOCKET, SO_SNDBUF, sz, sz_sz) < 0) {
3134 int e = tor_socket_errno(sock);
3135 log_warn(LD_NET, "setsockopt() to constrain send "
3136 "buffer to %d bytes failed: %s", size, tor_socket_strerror(e));
3138 if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF, sz, sz_sz) < 0) {
3139 int e = tor_socket_errno(sock);
3140 log_warn(LD_NET, "setsockopt() to constrain recv "
3141 "buffer to %d bytes failed: %s", size, tor_socket_strerror(e));
3145 /** Process new bytes that have arrived on conn-\>inbuf.
3147 * This function just passes conn to the connection-specific
3148 * connection_*_process_inbuf() function. It also passes in
3149 * package_partial if wanted.
3151 static int
3152 connection_process_inbuf(connection_t *conn, int package_partial)
3154 tor_assert(conn);
3156 switch (conn->type) {
3157 case CONN_TYPE_OR:
3158 return connection_or_process_inbuf(TO_OR_CONN(conn));
3159 case CONN_TYPE_EXIT:
3160 case CONN_TYPE_AP:
3161 return connection_edge_process_inbuf(TO_EDGE_CONN(conn),
3162 package_partial);
3163 case CONN_TYPE_DIR:
3164 return connection_dir_process_inbuf(TO_DIR_CONN(conn));
3165 case CONN_TYPE_CPUWORKER:
3166 return connection_cpu_process_inbuf(conn);
3167 case CONN_TYPE_CONTROL:
3168 return connection_control_process_inbuf(TO_CONTROL_CONN(conn));
3169 default:
3170 log_err(LD_BUG,"got unexpected conn type %d.", conn->type);
3171 tor_fragile_assert();
3172 return -1;
3176 /** Called whenever we've written data on a connection. */
3177 static int
3178 connection_flushed_some(connection_t *conn)
3180 int r = 0;
3181 tor_assert(!conn->in_flushed_some);
3182 conn->in_flushed_some = 1;
3183 if (conn->type == CONN_TYPE_DIR &&
3184 conn->state == DIR_CONN_STATE_SERVER_WRITING) {
3185 r = connection_dirserv_flushed_some(TO_DIR_CONN(conn));
3186 } else if (conn->type == CONN_TYPE_OR) {
3187 r = connection_or_flushed_some(TO_OR_CONN(conn));
3189 conn->in_flushed_some = 0;
3190 return r;
3193 /** We just finished flushing bytes from conn-\>outbuf, and there
3194 * are no more bytes remaining.
3196 * This function just passes conn to the connection-specific
3197 * connection_*_finished_flushing() function.
3199 static int
3200 connection_finished_flushing(connection_t *conn)
3202 tor_assert(conn);
3204 /* If the connection is closed, don't try to do anything more here. */
3205 if (CONN_IS_CLOSED(conn))
3206 return 0;
3208 // log_fn(LOG_DEBUG,"entered. Socket %u.", conn->s);
3210 switch (conn->type) {
3211 case CONN_TYPE_OR:
3212 return connection_or_finished_flushing(TO_OR_CONN(conn));
3213 case CONN_TYPE_AP:
3214 case CONN_TYPE_EXIT:
3215 return connection_edge_finished_flushing(TO_EDGE_CONN(conn));
3216 case CONN_TYPE_DIR:
3217 return connection_dir_finished_flushing(TO_DIR_CONN(conn));
3218 case CONN_TYPE_CPUWORKER:
3219 return connection_cpu_finished_flushing(conn);
3220 case CONN_TYPE_CONTROL:
3221 return connection_control_finished_flushing(TO_CONTROL_CONN(conn));
3222 default:
3223 log_err(LD_BUG,"got unexpected conn type %d.", conn->type);
3224 tor_fragile_assert();
3225 return -1;
3229 /** Called when our attempt to connect() to another server has just
3230 * succeeded.
3232 * This function just passes conn to the connection-specific
3233 * connection_*_finished_connecting() function.
3235 static int
3236 connection_finished_connecting(connection_t *conn)
3238 tor_assert(conn);
3239 switch (conn->type)
3241 case CONN_TYPE_OR:
3242 return connection_or_finished_connecting(TO_OR_CONN(conn));
3243 case CONN_TYPE_EXIT:
3244 return connection_edge_finished_connecting(TO_EDGE_CONN(conn));
3245 case CONN_TYPE_DIR:
3246 return connection_dir_finished_connecting(TO_DIR_CONN(conn));
3247 default:
3248 log_err(LD_BUG,"got unexpected conn type %d.", conn->type);
3249 tor_fragile_assert();
3250 return -1;
3254 /** Callback: invoked when a connection reaches an EOF event. */
3255 static int
3256 connection_reached_eof(connection_t *conn)
3258 switch (conn->type) {
3259 case CONN_TYPE_OR:
3260 return connection_or_reached_eof(TO_OR_CONN(conn));
3261 case CONN_TYPE_AP:
3262 case CONN_TYPE_EXIT:
3263 return connection_edge_reached_eof(TO_EDGE_CONN(conn));
3264 case CONN_TYPE_DIR:
3265 return connection_dir_reached_eof(TO_DIR_CONN(conn));
3266 case CONN_TYPE_CPUWORKER:
3267 return connection_cpu_reached_eof(conn);
3268 case CONN_TYPE_CONTROL:
3269 return connection_control_reached_eof(TO_CONTROL_CONN(conn));
3270 default:
3271 log_err(LD_BUG,"got unexpected conn type %d.", conn->type);
3272 tor_fragile_assert();
3273 return -1;
3277 /** Log how many bytes are used by buffers of different kinds and sizes. */
3278 void
3279 connection_dump_buffer_mem_stats(int severity)
3281 uint64_t used_by_type[_CONN_TYPE_MAX+1];
3282 uint64_t alloc_by_type[_CONN_TYPE_MAX+1];
3283 int n_conns_by_type[_CONN_TYPE_MAX+1];
3284 uint64_t total_alloc = 0;
3285 uint64_t total_used = 0;
3286 int i;
3287 smartlist_t *conns = get_connection_array();
3289 memset(used_by_type, 0, sizeof(used_by_type));
3290 memset(alloc_by_type, 0, sizeof(alloc_by_type));
3291 memset(n_conns_by_type, 0, sizeof(n_conns_by_type));
3293 SMARTLIST_FOREACH(conns, connection_t *, c,
3295 int tp = c->type;
3296 ++n_conns_by_type[tp];
3297 if (c->inbuf) {
3298 used_by_type[tp] += buf_datalen(c->inbuf);
3299 alloc_by_type[tp] += buf_allocation(c->inbuf);
3301 if (c->outbuf) {
3302 used_by_type[tp] += buf_datalen(c->outbuf);
3303 alloc_by_type[tp] += buf_allocation(c->outbuf);
3306 for (i=0; i <= _CONN_TYPE_MAX; ++i) {
3307 total_used += used_by_type[i];
3308 total_alloc += alloc_by_type[i];
3311 log(severity, LD_GENERAL,
3312 "In buffers for %d connections: "U64_FORMAT" used/"U64_FORMAT" allocated",
3313 smartlist_len(conns),
3314 U64_PRINTF_ARG(total_used), U64_PRINTF_ARG(total_alloc));
3315 for (i=_CONN_TYPE_MIN; i <= _CONN_TYPE_MAX; ++i) {
3316 if (!n_conns_by_type[i])
3317 continue;
3318 log(severity, LD_GENERAL,
3319 " For %d %s connections: "U64_FORMAT" used/"U64_FORMAT" allocated",
3320 n_conns_by_type[i], conn_type_to_string(i),
3321 U64_PRINTF_ARG(used_by_type[i]), U64_PRINTF_ARG(alloc_by_type[i]));
3325 /** Verify that connection <b>conn</b> has all of its invariants
3326 * correct. Trigger an assert if anything is invalid.
3328 void
3329 assert_connection_ok(connection_t *conn, time_t now)
3331 (void) now; /* XXXX unused. */
3332 tor_assert(conn);
3333 tor_assert(conn->type >= _CONN_TYPE_MIN);
3334 tor_assert(conn->type <= _CONN_TYPE_MAX);
3335 switch (conn->type) {
3336 case CONN_TYPE_OR:
3337 tor_assert(conn->magic == OR_CONNECTION_MAGIC);
3338 break;
3339 case CONN_TYPE_AP:
3340 case CONN_TYPE_EXIT:
3341 tor_assert(conn->magic == EDGE_CONNECTION_MAGIC);
3342 break;
3343 case CONN_TYPE_DIR:
3344 tor_assert(conn->magic == DIR_CONNECTION_MAGIC);
3345 break;
3346 case CONN_TYPE_CONTROL:
3347 tor_assert(conn->magic == CONTROL_CONNECTION_MAGIC);
3348 break;
3349 default:
3350 tor_assert(conn->magic == BASE_CONNECTION_MAGIC);
3351 break;
3354 if (conn->linked_conn) {
3355 tor_assert(conn->linked_conn->linked_conn == conn);
3356 tor_assert(conn->linked);
3358 if (conn->linked)
3359 tor_assert(conn->s < 0);
3361 if (conn->outbuf_flushlen > 0) {
3362 tor_assert(connection_is_writing(conn) || conn->write_blocked_on_bw ||
3363 (CONN_IS_EDGE(conn) && TO_EDGE_CONN(conn)->edge_blocked_on_circ));
3366 if (conn->hold_open_until_flushed)
3367 tor_assert(conn->marked_for_close);
3369 /* XXXX check: read_blocked_on_bw, write_blocked_on_bw, s, conn_array_index,
3370 * marked_for_close. */
3372 /* buffers */
3373 if (!connection_is_listener(conn)) {
3374 assert_buf_ok(conn->inbuf);
3375 assert_buf_ok(conn->outbuf);
3378 if (conn->type == CONN_TYPE_OR) {
3379 or_connection_t *or_conn = TO_OR_CONN(conn);
3380 if (conn->state == OR_CONN_STATE_OPEN) {
3381 /* tor_assert(conn->bandwidth > 0); */
3382 /* the above isn't necessarily true: if we just did a TLS
3383 * handshake but we didn't recognize the other peer, or it
3384 * gave a bad cert/etc, then we won't have assigned bandwidth,
3385 * yet it will be open. -RD
3387 // tor_assert(conn->read_bucket >= 0);
3389 // tor_assert(conn->addr && conn->port);
3390 tor_assert(conn->address);
3391 if (conn->state > OR_CONN_STATE_PROXY_HANDSHAKING)
3392 tor_assert(or_conn->tls);
3395 if (CONN_IS_EDGE(conn)) {
3396 edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
3397 if (edge_conn->chosen_exit_optional || edge_conn->chosen_exit_retries) {
3398 tor_assert(conn->type == CONN_TYPE_AP);
3399 tor_assert(edge_conn->chosen_exit_name);
3402 /* XXX unchecked: package window, deliver window. */
3403 if (conn->type == CONN_TYPE_AP) {
3405 tor_assert(edge_conn->socks_request);
3406 if (conn->state == AP_CONN_STATE_OPEN) {
3407 tor_assert(edge_conn->socks_request->has_finished);
3408 if (!conn->marked_for_close) {
3409 tor_assert(edge_conn->cpath_layer);
3410 assert_cpath_layer_ok(edge_conn->cpath_layer);
3414 if (conn->type == CONN_TYPE_EXIT) {
3415 tor_assert(conn->purpose == EXIT_PURPOSE_CONNECT ||
3416 conn->purpose == EXIT_PURPOSE_RESOLVE);
3418 } else if (conn->type == CONN_TYPE_DIR) {
3419 } else {
3420 /* Purpose is only used for dir and exit types currently */
3421 tor_assert(!conn->purpose);
3424 switch (conn->type)
3426 case CONN_TYPE_OR_LISTENER:
3427 case CONN_TYPE_AP_LISTENER:
3428 case CONN_TYPE_AP_TRANS_LISTENER:
3429 case CONN_TYPE_AP_NATD_LISTENER:
3430 case CONN_TYPE_DIR_LISTENER:
3431 case CONN_TYPE_CONTROL_LISTENER:
3432 case CONN_TYPE_AP_DNS_LISTENER:
3433 tor_assert(conn->state == LISTENER_STATE_READY);
3434 break;
3435 case CONN_TYPE_OR:
3436 tor_assert(conn->state >= _OR_CONN_STATE_MIN);
3437 tor_assert(conn->state <= _OR_CONN_STATE_MAX);
3438 tor_assert(TO_OR_CONN(conn)->n_circuits >= 0);
3439 break;
3440 case CONN_TYPE_EXIT:
3441 tor_assert(conn->state >= _EXIT_CONN_STATE_MIN);
3442 tor_assert(conn->state <= _EXIT_CONN_STATE_MAX);
3443 tor_assert(conn->purpose >= _EXIT_PURPOSE_MIN);
3444 tor_assert(conn->purpose <= _EXIT_PURPOSE_MAX);
3445 break;
3446 case CONN_TYPE_AP:
3447 tor_assert(conn->state >= _AP_CONN_STATE_MIN);
3448 tor_assert(conn->state <= _AP_CONN_STATE_MAX);
3449 tor_assert(TO_EDGE_CONN(conn)->socks_request);
3450 break;
3451 case CONN_TYPE_DIR:
3452 tor_assert(conn->state >= _DIR_CONN_STATE_MIN);
3453 tor_assert(conn->state <= _DIR_CONN_STATE_MAX);
3454 tor_assert(conn->purpose >= _DIR_PURPOSE_MIN);
3455 tor_assert(conn->purpose <= _DIR_PURPOSE_MAX);
3456 break;
3457 case CONN_TYPE_CPUWORKER:
3458 tor_assert(conn->state >= _CPUWORKER_STATE_MIN);
3459 tor_assert(conn->state <= _CPUWORKER_STATE_MAX);
3460 break;
3461 case CONN_TYPE_CONTROL:
3462 tor_assert(conn->state >= _CONTROL_CONN_STATE_MIN);
3463 tor_assert(conn->state <= _CONTROL_CONN_STATE_MAX);
3464 break;
3465 default:
3466 tor_assert(0);