Count bytes we spend on answering directory requests.
[tor/rransom.git] / src / or / connection.c
blob5438cf7d0dd1f3d1c4e6e61b86a9ad67baa093dd
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-2010, 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"
14 #include "buffers.h"
15 #include "circuitbuild.h"
16 #include "circuitlist.h"
17 #include "circuituse.h"
18 #include "config.h"
19 #include "connection.h"
20 #include "connection_edge.h"
21 #include "connection_or.h"
22 #include "control.h"
23 #include "cpuworker.h"
24 #include "directory.h"
25 #include "dirserv.h"
26 #include "dns.h"
27 #include "dnsserv.h"
28 #include "geoip.h"
29 #include "main.h"
30 #include "policies.h"
31 #include "reasons.h"
32 #include "relay.h"
33 #include "rendclient.h"
34 #include "rendcommon.h"
35 #include "rephist.h"
36 #include "router.h"
37 #include "routerparse.h"
39 static connection_t *connection_create_listener(
40 struct sockaddr *listensockaddr,
41 socklen_t listensocklen, int type,
42 char* address);
43 static void connection_init(time_t now, connection_t *conn, int type,
44 int socket_family);
45 static int connection_init_accepted_conn(connection_t *conn,
46 uint8_t listener_type);
47 static int connection_handle_listener_read(connection_t *conn, int new_type);
48 static int connection_bucket_should_increase(int bucket,
49 or_connection_t *conn);
50 static int connection_finished_flushing(connection_t *conn);
51 static int connection_flushed_some(connection_t *conn);
52 static int connection_finished_connecting(connection_t *conn);
53 static int connection_reached_eof(connection_t *conn);
54 static int connection_read_to_buf(connection_t *conn, int *max_to_read,
55 int *socket_error);
56 static int connection_process_inbuf(connection_t *conn, int package_partial);
57 static void client_check_address_changed(int sock);
58 static void set_constrained_socket_buffers(int sock, int size);
60 static const char *connection_proxy_state_to_string(int state);
61 static int connection_read_https_proxy_response(connection_t *conn);
62 static void connection_send_socks5_connect(connection_t *conn);
64 /** The last IPv4 address that our network interface seemed to have been
65 * binding to, in host order. We use this to detect when our IP changes. */
66 static uint32_t last_interface_ip = 0;
67 /** A list of uint32_ts for addresses we've used in outgoing connections.
68 * Used to detect IP address changes. */
69 static smartlist_t *outgoing_addrs = NULL;
71 /**************************************************************/
73 /**
74 * Return the human-readable name for the connection type <b>type</b>
76 const char *
77 conn_type_to_string(int type)
79 static char buf[64];
80 switch (type) {
81 case CONN_TYPE_OR_LISTENER: return "OR listener";
82 case CONN_TYPE_OR: return "OR";
83 case CONN_TYPE_EXIT: return "Exit";
84 case CONN_TYPE_AP_LISTENER: return "Socks listener";
85 case CONN_TYPE_AP_TRANS_LISTENER:
86 return "Transparent pf/netfilter listener";
87 case CONN_TYPE_AP_NATD_LISTENER: return "Transparent natd listener";
88 case CONN_TYPE_AP_DNS_LISTENER: return "DNS listener";
89 case CONN_TYPE_AP: return "Socks";
90 case CONN_TYPE_DIR_LISTENER: return "Directory listener";
91 case CONN_TYPE_DIR: return "Directory";
92 case CONN_TYPE_CPUWORKER: return "CPU worker";
93 case CONN_TYPE_CONTROL_LISTENER: return "Control listener";
94 case CONN_TYPE_CONTROL: return "Control";
95 default:
96 log_warn(LD_BUG, "unknown connection type %d", type);
97 tor_snprintf(buf, sizeof(buf), "unknown [%d]", type);
98 return buf;
103 * Return the human-readable name for the connection state <b>state</b>
104 * for the connection type <b>type</b>
106 const char *
107 conn_state_to_string(int type, int state)
109 static char buf[96];
110 switch (type) {
111 case CONN_TYPE_OR_LISTENER:
112 case CONN_TYPE_AP_LISTENER:
113 case CONN_TYPE_AP_TRANS_LISTENER:
114 case CONN_TYPE_AP_NATD_LISTENER:
115 case CONN_TYPE_AP_DNS_LISTENER:
116 case CONN_TYPE_DIR_LISTENER:
117 case CONN_TYPE_CONTROL_LISTENER:
118 if (state == LISTENER_STATE_READY)
119 return "ready";
120 break;
121 case CONN_TYPE_OR:
122 switch (state) {
123 case OR_CONN_STATE_CONNECTING: return "connect()ing";
124 case OR_CONN_STATE_PROXY_HANDSHAKING: return "handshaking (proxy)";
125 case OR_CONN_STATE_TLS_HANDSHAKING: return "handshaking (TLS)";
126 case OR_CONN_STATE_TLS_CLIENT_RENEGOTIATING:
127 return "renegotiating (TLS)";
128 case OR_CONN_STATE_TLS_SERVER_RENEGOTIATING:
129 return "waiting for renegotiation (TLS)";
130 case OR_CONN_STATE_OR_HANDSHAKING: return "handshaking (Tor)";
131 case OR_CONN_STATE_OPEN: return "open";
133 break;
134 case CONN_TYPE_EXIT:
135 switch (state) {
136 case EXIT_CONN_STATE_RESOLVING: return "waiting for dest info";
137 case EXIT_CONN_STATE_CONNECTING: return "connecting";
138 case EXIT_CONN_STATE_OPEN: return "open";
139 case EXIT_CONN_STATE_RESOLVEFAILED: return "resolve failed";
141 break;
142 case CONN_TYPE_AP:
143 switch (state) {
144 case AP_CONN_STATE_SOCKS_WAIT: return "waiting for socks info";
145 case AP_CONN_STATE_NATD_WAIT: return "waiting for natd dest info";
146 case AP_CONN_STATE_RENDDESC_WAIT: return "waiting for rendezvous desc";
147 case AP_CONN_STATE_CONTROLLER_WAIT: return "waiting for controller";
148 case AP_CONN_STATE_CIRCUIT_WAIT: return "waiting for circuit";
149 case AP_CONN_STATE_CONNECT_WAIT: return "waiting for connect response";
150 case AP_CONN_STATE_RESOLVE_WAIT: return "waiting for resolve response";
151 case AP_CONN_STATE_OPEN: return "open";
153 break;
154 case CONN_TYPE_DIR:
155 switch (state) {
156 case DIR_CONN_STATE_CONNECTING: return "connecting";
157 case DIR_CONN_STATE_CLIENT_SENDING: return "client sending";
158 case DIR_CONN_STATE_CLIENT_READING: return "client reading";
159 case DIR_CONN_STATE_CLIENT_FINISHED: return "client finished";
160 case DIR_CONN_STATE_SERVER_COMMAND_WAIT: return "waiting for command";
161 case DIR_CONN_STATE_SERVER_WRITING: return "writing";
163 break;
164 case CONN_TYPE_CPUWORKER:
165 switch (state) {
166 case CPUWORKER_STATE_IDLE: return "idle";
167 case CPUWORKER_STATE_BUSY_ONION: return "busy with onion";
169 break;
170 case CONN_TYPE_CONTROL:
171 switch (state) {
172 case CONTROL_CONN_STATE_OPEN: return "open (protocol v1)";
173 case CONTROL_CONN_STATE_NEEDAUTH:
174 return "waiting for authentication (protocol v1)";
176 break;
179 log_warn(LD_BUG, "unknown connection state %d (type %d)", state, type);
180 tor_snprintf(buf, sizeof(buf),
181 "unknown state [%d] on unknown [%s] connection",
182 state, conn_type_to_string(type));
183 return buf;
186 /** Allocate and return a new dir_connection_t, initialized as by
187 * connection_init(). */
188 dir_connection_t *
189 dir_connection_new(int socket_family)
191 dir_connection_t *dir_conn = tor_malloc_zero(sizeof(dir_connection_t));
192 connection_init(time(NULL), TO_CONN(dir_conn), CONN_TYPE_DIR, socket_family);
193 return dir_conn;
196 /** Allocate and return a new or_connection_t, initialized as by
197 * connection_init(). */
198 or_connection_t *
199 or_connection_new(int socket_family)
201 or_connection_t *or_conn = tor_malloc_zero(sizeof(or_connection_t));
202 time_t now = time(NULL);
203 connection_init(now, TO_CONN(or_conn), CONN_TYPE_OR, socket_family);
205 or_conn->timestamp_last_added_nonpadding = time(NULL);
206 or_conn->next_circ_id = crypto_rand_int(1<<15);
208 or_conn->active_circuit_pqueue = smartlist_create();
209 or_conn->active_circuit_pqueue_last_recalibrated = cell_ewma_get_tick();
211 return or_conn;
214 /** Allocate and return a new edge_connection_t, initialized as by
215 * connection_init(). */
216 edge_connection_t *
217 edge_connection_new(int type, int socket_family)
219 edge_connection_t *edge_conn = tor_malloc_zero(sizeof(edge_connection_t));
220 tor_assert(type == CONN_TYPE_EXIT || type == CONN_TYPE_AP);
221 connection_init(time(NULL), TO_CONN(edge_conn), type, socket_family);
222 if (type == CONN_TYPE_AP)
223 edge_conn->socks_request = tor_malloc_zero(sizeof(socks_request_t));
224 return edge_conn;
227 /** Allocate and return a new control_connection_t, initialized as by
228 * connection_init(). */
229 control_connection_t *
230 control_connection_new(int socket_family)
232 control_connection_t *control_conn =
233 tor_malloc_zero(sizeof(control_connection_t));
234 connection_init(time(NULL),
235 TO_CONN(control_conn), CONN_TYPE_CONTROL, socket_family);
236 log_notice(LD_CONTROL, "New control connection opened.");
237 return control_conn;
240 /** Allocate, initialize, and return a new connection_t subtype of <b>type</b>
241 * to make or receive connections of address family <b>socket_family</b>. The
242 * type should be one of the CONN_TYPE_* constants. */
243 connection_t *
244 connection_new(int type, int socket_family)
246 switch (type) {
247 case CONN_TYPE_OR:
248 return TO_CONN(or_connection_new(socket_family));
250 case CONN_TYPE_EXIT:
251 case CONN_TYPE_AP:
252 return TO_CONN(edge_connection_new(type, socket_family));
254 case CONN_TYPE_DIR:
255 return TO_CONN(dir_connection_new(socket_family));
257 case CONN_TYPE_CONTROL:
258 return TO_CONN(control_connection_new(socket_family));
260 default: {
261 connection_t *conn = tor_malloc_zero(sizeof(connection_t));
262 connection_init(time(NULL), conn, type, socket_family);
263 return conn;
268 /** Initializes conn. (you must call connection_add() to link it into the main
269 * array).
271 * Set conn-\>type to <b>type</b>. Set conn-\>s and conn-\>conn_array_index to
272 * -1 to signify they are not yet assigned.
274 * If conn is not a listener type, allocate buffers for it. If it's
275 * an AP type, allocate space to store the socks_request.
277 * Assign a pseudorandom next_circ_id between 0 and 2**15.
279 * Initialize conn's timestamps to now.
281 static void
282 connection_init(time_t now, connection_t *conn, int type, int socket_family)
284 static uint64_t n_connections_allocated = 1;
286 switch (type) {
287 case CONN_TYPE_OR:
288 conn->magic = OR_CONNECTION_MAGIC;
289 break;
290 case CONN_TYPE_EXIT:
291 case CONN_TYPE_AP:
292 conn->magic = EDGE_CONNECTION_MAGIC;
293 break;
294 case CONN_TYPE_DIR:
295 conn->magic = DIR_CONNECTION_MAGIC;
296 break;
297 case CONN_TYPE_CONTROL:
298 conn->magic = CONTROL_CONNECTION_MAGIC;
299 break;
300 default:
301 conn->magic = BASE_CONNECTION_MAGIC;
302 break;
305 conn->s = -1; /* give it a default of 'not used' */
306 conn->conn_array_index = -1; /* also default to 'not used' */
307 conn->global_identifier = n_connections_allocated++;
309 conn->type = type;
310 conn->socket_family = socket_family;
311 if (!connection_is_listener(conn)) { /* listeners never use their buf */
312 conn->inbuf = buf_new();
313 conn->outbuf = buf_new();
316 conn->timestamp_created = now;
317 conn->timestamp_lastread = now;
318 conn->timestamp_lastwritten = now;
321 /** Create a link between <b>conn_a</b> and <b>conn_b</b>. */
322 void
323 connection_link_connections(connection_t *conn_a, connection_t *conn_b)
325 tor_assert(conn_a->s < 0);
326 tor_assert(conn_b->s < 0);
328 conn_a->linked = 1;
329 conn_b->linked = 1;
330 conn_a->linked_conn = conn_b;
331 conn_b->linked_conn = conn_a;
334 /** Deallocate memory used by <b>conn</b>. Deallocate its buffers if
335 * necessary, close its socket if necessary, and mark the directory as dirty
336 * if <b>conn</b> is an OR or OP connection.
338 static void
339 _connection_free(connection_t *conn)
341 void *mem;
342 size_t memlen;
343 if (!conn)
344 return;
346 switch (conn->type) {
347 case CONN_TYPE_OR:
348 tor_assert(conn->magic == OR_CONNECTION_MAGIC);
349 mem = TO_OR_CONN(conn);
350 memlen = sizeof(or_connection_t);
351 break;
352 case CONN_TYPE_AP:
353 case CONN_TYPE_EXIT:
354 tor_assert(conn->magic == EDGE_CONNECTION_MAGIC);
355 mem = TO_EDGE_CONN(conn);
356 memlen = sizeof(edge_connection_t);
357 break;
358 case CONN_TYPE_DIR:
359 tor_assert(conn->magic == DIR_CONNECTION_MAGIC);
360 mem = TO_DIR_CONN(conn);
361 memlen = sizeof(dir_connection_t);
362 break;
363 case CONN_TYPE_CONTROL:
364 tor_assert(conn->magic == CONTROL_CONNECTION_MAGIC);
365 mem = TO_CONTROL_CONN(conn);
366 memlen = sizeof(control_connection_t);
367 break;
368 default:
369 tor_assert(conn->magic == BASE_CONNECTION_MAGIC);
370 mem = conn;
371 memlen = sizeof(connection_t);
372 break;
375 if (conn->linked) {
376 log_info(LD_GENERAL, "Freeing linked %s connection [%s] with %d "
377 "bytes on inbuf, %d on outbuf.",
378 conn_type_to_string(conn->type),
379 conn_state_to_string(conn->type, conn->state),
380 (int)buf_datalen(conn->inbuf), (int)buf_datalen(conn->outbuf));
383 if (!connection_is_listener(conn)) {
384 buf_free(conn->inbuf);
385 buf_free(conn->outbuf);
386 } else {
387 if (conn->socket_family == AF_UNIX) {
388 /* For now only control ports can be Unix domain sockets
389 * and listeners at the same time */
390 tor_assert(conn->type == CONN_TYPE_CONTROL_LISTENER);
392 if (unlink(conn->address) < 0 && errno != ENOENT) {
393 log_warn(LD_NET, "Could not unlink %s: %s", conn->address,
394 strerror(errno));
399 tor_free(conn->address);
401 if (connection_speaks_cells(conn)) {
402 or_connection_t *or_conn = TO_OR_CONN(conn);
403 tor_tls_free(or_conn->tls);
404 or_conn->tls = NULL;
405 or_handshake_state_free(or_conn->handshake_state);
406 or_conn->handshake_state = NULL;
407 smartlist_free(or_conn->active_circuit_pqueue);
408 tor_free(or_conn->nickname);
410 if (CONN_IS_EDGE(conn)) {
411 edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
412 tor_free(edge_conn->chosen_exit_name);
413 if (edge_conn->socks_request) {
414 memset(edge_conn->socks_request, 0xcc, sizeof(socks_request_t));
415 tor_free(edge_conn->socks_request);
418 rend_data_free(edge_conn->rend_data);
420 if (conn->type == CONN_TYPE_CONTROL) {
421 control_connection_t *control_conn = TO_CONTROL_CONN(conn);
422 tor_free(control_conn->incoming_cmd);
425 tor_free(conn->read_event); /* Probably already freed by connection_free. */
426 tor_free(conn->write_event); /* Probably already freed by connection_free. */
428 if (conn->type == CONN_TYPE_DIR) {
429 dir_connection_t *dir_conn = TO_DIR_CONN(conn);
430 tor_free(dir_conn->requested_resource);
432 tor_zlib_free(dir_conn->zlib_state);
433 if (dir_conn->fingerprint_stack) {
434 SMARTLIST_FOREACH(dir_conn->fingerprint_stack, char *, cp, tor_free(cp));
435 smartlist_free(dir_conn->fingerprint_stack);
438 cached_dir_decref(dir_conn->cached_dir);
439 rend_data_free(dir_conn->rend_data);
442 if (conn->s >= 0) {
443 log_debug(LD_NET,"closing fd %d.",conn->s);
444 tor_close_socket(conn->s);
445 conn->s = -1;
448 if (conn->type == CONN_TYPE_OR &&
449 !tor_digest_is_zero(TO_OR_CONN(conn)->identity_digest)) {
450 log_warn(LD_BUG, "called on OR conn with non-zeroed identity_digest");
451 connection_or_remove_from_identity_map(TO_OR_CONN(conn));
454 memset(mem, 0xCC, memlen); /* poison memory */
455 tor_free(mem);
458 /** Make sure <b>conn</b> isn't in any of the global conn lists; then free it.
460 void
461 connection_free(connection_t *conn)
463 if (!conn)
464 return;
465 tor_assert(!connection_is_on_closeable_list(conn));
466 tor_assert(!connection_in_array(conn));
467 if (conn->linked_conn) {
468 log_err(LD_BUG, "Called with conn->linked_conn still set.");
469 tor_fragile_assert();
470 conn->linked_conn->linked_conn = NULL;
471 if (! conn->linked_conn->marked_for_close &&
472 conn->linked_conn->reading_from_linked_conn)
473 connection_start_reading(conn->linked_conn);
474 conn->linked_conn = NULL;
476 if (connection_speaks_cells(conn)) {
477 if (!tor_digest_is_zero(TO_OR_CONN(conn)->identity_digest)) {
478 connection_or_remove_from_identity_map(TO_OR_CONN(conn));
481 if (conn->type == CONN_TYPE_CONTROL) {
482 TO_CONTROL_CONN(conn)->event_mask = 0;
483 control_update_global_event_mask();
485 connection_unregister_events(conn);
486 _connection_free(conn);
489 /** Call _connection_free() on every connection in our array, and release all
490 * storage held by connection.c. This is used by cpuworkers and dnsworkers
491 * when they fork, so they don't keep resources held open (especially
492 * sockets).
494 * Don't do the checks in connection_free(), because they will
495 * fail.
497 void
498 connection_free_all(void)
500 smartlist_t *conns = get_connection_array();
502 /* We don't want to log any messages to controllers. */
503 SMARTLIST_FOREACH(conns, connection_t *, conn,
504 if (conn->type == CONN_TYPE_CONTROL)
505 TO_CONTROL_CONN(conn)->event_mask = 0);
507 control_update_global_event_mask();
509 /* Unlink everything from the identity map. */
510 connection_or_clear_identity_map();
512 SMARTLIST_FOREACH(conns, connection_t *, conn, _connection_free(conn));
514 if (outgoing_addrs) {
515 SMARTLIST_FOREACH(outgoing_addrs, void*, addr, tor_free(addr));
516 smartlist_free(outgoing_addrs);
517 outgoing_addrs = NULL;
521 /** Do any cleanup needed:
522 * - Directory conns that failed to fetch a rendezvous descriptor
523 * need to inform pending rendezvous streams.
524 * - OR conns need to call rep_hist_note_*() to record status.
525 * - AP conns need to send a socks reject if necessary.
526 * - Exit conns need to call connection_dns_remove() if necessary.
527 * - AP and Exit conns need to send an end cell if they can.
528 * - DNS conns need to fail any resolves that are pending on them.
529 * - OR and edge connections need to be unlinked from circuits.
531 void
532 connection_about_to_close_connection(connection_t *conn)
534 circuit_t *circ;
535 dir_connection_t *dir_conn;
536 or_connection_t *or_conn;
537 edge_connection_t *edge_conn;
538 time_t now = time(NULL);
540 tor_assert(conn->marked_for_close);
542 if (CONN_IS_EDGE(conn)) {
543 edge_conn = TO_EDGE_CONN(conn);
544 if (!edge_conn->edge_has_sent_end) {
545 log_warn(LD_BUG, "(Harmless.) Edge connection (marked at %s:%d) "
546 "hasn't sent end yet?",
547 conn->marked_for_close_file, conn->marked_for_close);
548 tor_fragile_assert();
552 switch (conn->type) {
553 case CONN_TYPE_DIR:
554 dir_conn = TO_DIR_CONN(conn);
555 if (conn->state < DIR_CONN_STATE_CLIENT_FINISHED) {
556 /* It's a directory connection and connecting or fetching
557 * failed: forget about this router, and maybe try again. */
558 connection_dir_request_failed(dir_conn);
560 /* If we were trying to fetch a v2 rend desc and did not succeed,
561 * retry as needed. (If a fetch is successful, the connection state
562 * is changed to DIR_PURPOSE_HAS_FETCHED_RENDDESC to mark that
563 * refetching is unnecessary.) */
564 if (conn->purpose == DIR_PURPOSE_FETCH_RENDDESC_V2 &&
565 dir_conn->rend_data &&
566 strlen(dir_conn->rend_data->onion_address) ==
567 REND_SERVICE_ID_LEN_BASE32)
568 rend_client_refetch_v2_renddesc(dir_conn->rend_data);
569 break;
570 case CONN_TYPE_OR:
571 or_conn = TO_OR_CONN(conn);
572 /* Remember why we're closing this connection. */
573 if (conn->state != OR_CONN_STATE_OPEN) {
574 /* Inform any pending (not attached) circs that they should
575 * give up. */
576 circuit_n_conn_done(TO_OR_CONN(conn), 0);
577 /* now mark things down as needed */
578 if (connection_or_nonopen_was_started_here(or_conn)) {
579 or_options_t *options = get_options();
580 rep_hist_note_connect_failed(or_conn->identity_digest, now);
581 entry_guard_register_connect_status(or_conn->identity_digest,0,
582 !options->HttpsProxy, now);
583 if (conn->state >= OR_CONN_STATE_TLS_HANDSHAKING) {
584 int reason = tls_error_to_orconn_end_reason(or_conn->tls_error);
585 control_event_or_conn_status(or_conn, OR_CONN_EVENT_FAILED,
586 reason);
587 if (!authdir_mode_tests_reachability(options))
588 control_event_bootstrap_problem(
589 orconn_end_reason_to_control_string(reason), reason);
592 } else if (conn->hold_open_until_flushed) {
593 /* We only set hold_open_until_flushed when we're intentionally
594 * closing a connection. */
595 rep_hist_note_disconnect(or_conn->identity_digest, now);
596 control_event_or_conn_status(or_conn, OR_CONN_EVENT_CLOSED,
597 tls_error_to_orconn_end_reason(or_conn->tls_error));
598 } else if (!tor_digest_is_zero(or_conn->identity_digest)) {
599 rep_hist_note_connection_died(or_conn->identity_digest, now);
600 control_event_or_conn_status(or_conn, OR_CONN_EVENT_CLOSED,
601 tls_error_to_orconn_end_reason(or_conn->tls_error));
603 /* Now close all the attached circuits on it. */
604 circuit_unlink_all_from_or_conn(TO_OR_CONN(conn),
605 END_CIRC_REASON_OR_CONN_CLOSED);
606 break;
607 case CONN_TYPE_AP:
608 edge_conn = TO_EDGE_CONN(conn);
609 if (edge_conn->socks_request->has_finished == 0) {
610 /* since conn gets removed right after this function finishes,
611 * there's no point trying to send back a reply at this point. */
612 log_warn(LD_BUG,"Closing stream (marked at %s:%d) without sending"
613 " back a socks reply.",
614 conn->marked_for_close_file, conn->marked_for_close);
616 if (!edge_conn->end_reason) {
617 log_warn(LD_BUG,"Closing stream (marked at %s:%d) without having"
618 " set end_reason.",
619 conn->marked_for_close_file, conn->marked_for_close);
621 if (edge_conn->dns_server_request) {
622 log_warn(LD_BUG,"Closing stream (marked at %s:%d) without having"
623 " replied to DNS request.",
624 conn->marked_for_close_file, conn->marked_for_close);
625 dnsserv_reject_request(edge_conn);
627 control_event_stream_bandwidth(edge_conn);
628 control_event_stream_status(edge_conn, STREAM_EVENT_CLOSED,
629 edge_conn->end_reason);
630 circ = circuit_get_by_edge_conn(edge_conn);
631 if (circ)
632 circuit_detach_stream(circ, edge_conn);
633 break;
634 case CONN_TYPE_EXIT:
635 edge_conn = TO_EDGE_CONN(conn);
636 circ = circuit_get_by_edge_conn(edge_conn);
637 if (circ)
638 circuit_detach_stream(circ, edge_conn);
639 if (conn->state == EXIT_CONN_STATE_RESOLVING) {
640 connection_dns_remove(edge_conn);
642 break;
646 /** Return true iff connection_close_immediate() has been called on this
647 * connection. */
648 #define CONN_IS_CLOSED(c) \
649 ((c)->linked ? ((c)->linked_conn_is_closed) : ((c)->s < 0))
651 /** Close the underlying socket for <b>conn</b>, so we don't try to
652 * flush it. Must be used in conjunction with (right before)
653 * connection_mark_for_close().
655 void
656 connection_close_immediate(connection_t *conn)
658 assert_connection_ok(conn,0);
659 if (CONN_IS_CLOSED(conn)) {
660 log_err(LD_BUG,"Attempt to close already-closed connection.");
661 tor_fragile_assert();
662 return;
664 if (conn->outbuf_flushlen) {
665 log_info(LD_NET,"fd %d, type %s, state %s, %d bytes on outbuf.",
666 conn->s, conn_type_to_string(conn->type),
667 conn_state_to_string(conn->type, conn->state),
668 (int)conn->outbuf_flushlen);
671 connection_unregister_events(conn);
673 if (conn->s >= 0)
674 tor_close_socket(conn->s);
675 conn->s = -1;
676 if (conn->linked)
677 conn->linked_conn_is_closed = 1;
678 if (!connection_is_listener(conn)) {
679 buf_clear(conn->outbuf);
680 conn->outbuf_flushlen = 0;
684 /** Mark <b>conn</b> to be closed next time we loop through
685 * conn_close_if_marked() in main.c. */
686 void
687 _connection_mark_for_close(connection_t *conn, int line, const char *file)
689 assert_connection_ok(conn,0);
690 tor_assert(line);
691 tor_assert(line < 1<<16); /* marked_for_close can only fit a uint16_t. */
692 tor_assert(file);
694 if (conn->marked_for_close) {
695 log(LOG_WARN,LD_BUG,"Duplicate call to connection_mark_for_close at %s:%d"
696 " (first at %s:%d)", file, line, conn->marked_for_close_file,
697 conn->marked_for_close);
698 tor_fragile_assert();
699 return;
702 conn->marked_for_close = line;
703 conn->marked_for_close_file = file;
704 add_connection_to_closeable_list(conn);
706 /* in case we're going to be held-open-til-flushed, reset
707 * the number of seconds since last successful write, so
708 * we get our whole 15 seconds */
709 conn->timestamp_lastwritten = time(NULL);
712 /** Find each connection that has hold_open_until_flushed set to
713 * 1 but hasn't written in the past 15 seconds, and set
714 * hold_open_until_flushed to 0. This means it will get cleaned
715 * up in the next loop through close_if_marked() in main.c.
717 void
718 connection_expire_held_open(void)
720 time_t now;
721 smartlist_t *conns = get_connection_array();
723 now = time(NULL);
725 SMARTLIST_FOREACH(conns, connection_t *, conn,
727 /* If we've been holding the connection open, but we haven't written
728 * for 15 seconds...
730 if (conn->hold_open_until_flushed) {
731 tor_assert(conn->marked_for_close);
732 if (now - conn->timestamp_lastwritten >= 15) {
733 int severity;
734 if (conn->type == CONN_TYPE_EXIT ||
735 (conn->type == CONN_TYPE_DIR &&
736 conn->purpose == DIR_PURPOSE_SERVER))
737 severity = LOG_INFO;
738 else
739 severity = LOG_NOTICE;
740 log_fn(severity, LD_NET,
741 "Giving up on marked_for_close conn that's been flushing "
742 "for 15s (fd %d, type %s, state %s).",
743 conn->s, conn_type_to_string(conn->type),
744 conn_state_to_string(conn->type, conn->state));
745 conn->hold_open_until_flushed = 0;
751 /** Create an AF_INET listenaddr struct.
752 * <b>listenaddress</b> provides the host and optionally the port information
753 * for the new structure. If no port is provided in <b>listenaddress</b> then
754 * <b>listenport</b> is used.
756 * If not NULL <b>readable_address</b> will contain a copy of the host part of
757 * <b>listenaddress</b>.
759 * The listenaddr struct has to be freed by the caller.
761 static struct sockaddr_in *
762 create_inet_sockaddr(const char *listenaddress, uint16_t listenport,
763 char **readable_address, socklen_t *socklen_out) {
764 struct sockaddr_in *listenaddr = NULL;
765 uint32_t addr;
766 uint16_t usePort = 0;
768 if (parse_addr_port(LOG_WARN,
769 listenaddress, readable_address, &addr, &usePort)<0) {
770 log_warn(LD_CONFIG,
771 "Error parsing/resolving ListenAddress %s", listenaddress);
772 goto err;
774 if (usePort==0)
775 usePort = listenport;
777 listenaddr = tor_malloc_zero(sizeof(struct sockaddr_in));
778 listenaddr->sin_addr.s_addr = htonl(addr);
779 listenaddr->sin_family = AF_INET;
780 listenaddr->sin_port = htons((uint16_t) usePort);
782 *socklen_out = sizeof(struct sockaddr_in);
784 return listenaddr;
786 err:
787 tor_free(listenaddr);
788 return NULL;
791 #ifdef HAVE_SYS_UN_H
792 /** Create an AF_UNIX listenaddr struct.
793 * <b>listenaddress</b> provides the path to the Unix socket.
795 * Eventually <b>listenaddress</b> will also optionally contain user, group,
796 * and file permissions for the new socket. But not yet. XXX
797 * Also, since we do not create the socket here the information doesn't help
798 * here.
800 * If not NULL <b>readable_address</b> will contain a copy of the path part of
801 * <b>listenaddress</b>.
803 * The listenaddr struct has to be freed by the caller.
805 static struct sockaddr_un *
806 create_unix_sockaddr(const char *listenaddress, char **readable_address,
807 socklen_t *len_out)
809 struct sockaddr_un *sockaddr = NULL;
811 sockaddr = tor_malloc_zero(sizeof(struct sockaddr_un));
812 sockaddr->sun_family = AF_UNIX;
813 strncpy(sockaddr->sun_path, listenaddress, sizeof(sockaddr->sun_path));
815 if (readable_address)
816 *readable_address = tor_strdup(listenaddress);
818 *len_out = sizeof(struct sockaddr_un);
819 return sockaddr;
821 #else
822 static struct sockaddr *
823 create_unix_sockaddr(const char *listenaddress, char **readable_address,
824 socklen_t *len_out)
826 (void)listenaddress;
827 (void)readable_address;
828 log_fn(LOG_ERR, LD_BUG,
829 "Unix domain sockets not supported, yet we tried to create one.");
830 *len_out = 0;
831 tor_assert(0);
833 #endif /* HAVE_SYS_UN_H */
835 /** Warn that an accept or a connect has failed because we're running up
836 * against our ulimit. Rate-limit these warnings so that we don't spam
837 * the log. */
838 static void
839 warn_too_many_conns(void)
841 #define WARN_TOO_MANY_CONNS_INTERVAL (6*60*60)
842 static time_t last_warned = 0;
843 time_t now = time(NULL);
844 int n_conns = get_n_open_sockets();
845 if (last_warned + WARN_TOO_MANY_CONNS_INTERVAL < now) {
846 log_warn(LD_NET,"Failing because we have %d connections already. Please "
847 "raise your ulimit -n.", n_conns);
848 last_warned = now;
849 control_event_general_status(LOG_WARN, "TOO_MANY_CONNECTIONS CURRENT=%d",
850 n_conns);
854 /** Bind a new non-blocking socket listening to the socket described
855 * by <b>listensockaddr</b>.
857 * <b>address</b> is only used for logging purposes and to add the information
858 * to the conn.
860 static connection_t *
861 connection_create_listener(struct sockaddr *listensockaddr, socklen_t socklen,
862 int type, char* address)
864 connection_t *conn;
865 int s; /* the socket we're going to make */
866 uint16_t usePort = 0;
867 int start_reading = 0;
869 if (get_n_open_sockets() >= get_options()->_ConnLimit-1) {
870 warn_too_many_conns();
871 return NULL;
874 if (listensockaddr->sa_family == AF_INET) {
875 int is_tcp = (type != CONN_TYPE_AP_DNS_LISTENER);
876 #ifndef MS_WINDOWS
877 int one=1;
878 #endif
879 if (is_tcp)
880 start_reading = 1;
882 usePort = ntohs( (uint16_t)
883 ((struct sockaddr_in *)listensockaddr)->sin_port);
885 log_notice(LD_NET, "Opening %s on %s:%d",
886 conn_type_to_string(type), address, usePort);
888 s = tor_open_socket(PF_INET,
889 is_tcp ? SOCK_STREAM : SOCK_DGRAM,
890 is_tcp ? IPPROTO_TCP: IPPROTO_UDP);
891 if (s < 0) {
892 log_warn(LD_NET,"Socket creation failed.");
893 goto err;
896 #ifndef MS_WINDOWS
897 /* REUSEADDR on normal places means you can rebind to the port
898 * right after somebody else has let it go. But REUSEADDR on win32
899 * means you can bind to the port _even when somebody else
900 * already has it bound_. So, don't do that on Win32. */
901 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (void*) &one,
902 (socklen_t)sizeof(one));
903 #endif
905 if (bind(s,listensockaddr,socklen) < 0) {
906 const char *helpfulhint = "";
907 int e = tor_socket_errno(s);
908 if (ERRNO_IS_EADDRINUSE(e))
909 helpfulhint = ". Is Tor already running?";
910 log_warn(LD_NET, "Could not bind to %s:%u: %s%s", address, usePort,
911 tor_socket_strerror(e), helpfulhint);
912 tor_close_socket(s);
913 goto err;
916 if (is_tcp) {
917 if (listen(s,SOMAXCONN) < 0) {
918 log_warn(LD_NET, "Could not listen on %s:%u: %s", address, usePort,
919 tor_socket_strerror(tor_socket_errno(s)));
920 tor_close_socket(s);
921 goto err;
924 #ifdef HAVE_SYS_UN_H
925 } else if (listensockaddr->sa_family == AF_UNIX) {
926 start_reading = 1;
928 /* For now only control ports can be Unix domain sockets
929 * and listeners at the same time */
930 tor_assert(type == CONN_TYPE_CONTROL_LISTENER);
932 log_notice(LD_NET, "Opening %s on %s",
933 conn_type_to_string(type), address);
935 if (unlink(address) < 0 && errno != ENOENT) {
936 log_warn(LD_NET, "Could not unlink %s: %s", address,
937 strerror(errno));
938 goto err;
940 s = tor_open_socket(AF_UNIX, SOCK_STREAM, 0);
941 if (s < 0) {
942 log_warn(LD_NET,"Socket creation failed: %s.", strerror(errno));
943 goto err;
946 if (bind(s, listensockaddr, (socklen_t)sizeof(struct sockaddr_un)) == -1) {
947 log_warn(LD_NET,"Bind to %s failed: %s.", address,
948 tor_socket_strerror(tor_socket_errno(s)));
949 goto err;
952 if (listen(s,SOMAXCONN) < 0) {
953 log_warn(LD_NET, "Could not listen on %s: %s", address,
954 tor_socket_strerror(tor_socket_errno(s)));
955 tor_close_socket(s);
956 goto err;
958 #endif /* HAVE_SYS_UN_H */
959 } else {
960 log_err(LD_BUG,"Got unexpected address family %d.",
961 listensockaddr->sa_family);
962 tor_assert(0);
965 set_socket_nonblocking(s);
967 conn = connection_new(type, listensockaddr->sa_family);
968 conn->socket_family = listensockaddr->sa_family;
969 conn->s = s;
970 conn->address = tor_strdup(address);
971 conn->port = usePort;
973 if (connection_add(conn) < 0) { /* no space, forget it */
974 log_warn(LD_NET,"connection_add for listener failed. Giving up.");
975 connection_free(conn);
976 goto err;
979 log_debug(LD_NET,"%s listening on port %u.",
980 conn_type_to_string(type), usePort);
982 conn->state = LISTENER_STATE_READY;
983 if (start_reading) {
984 connection_start_reading(conn);
985 } else {
986 tor_assert(type == CONN_TYPE_AP_DNS_LISTENER);
987 dnsserv_configure_listener(conn);
990 return conn;
992 err:
993 return NULL;
996 /** Do basic sanity checking on a newly received socket. Return 0
997 * if it looks ok, else return -1. */
998 static int
999 check_sockaddr(struct sockaddr *sa, int len, int level)
1001 int ok = 1;
1003 if (sa->sa_family == AF_INET) {
1004 struct sockaddr_in *sin=(struct sockaddr_in*)sa;
1005 if (len != sizeof(struct sockaddr_in)) {
1006 log_fn(level, LD_NET, "Length of address not as expected: %d vs %d",
1007 len,(int)sizeof(struct sockaddr_in));
1008 ok = 0;
1010 if (sin->sin_addr.s_addr == 0 || sin->sin_port == 0) {
1011 log_fn(level, LD_NET,
1012 "Address for new connection has address/port equal to zero.");
1013 ok = 0;
1015 } else if (sa->sa_family == AF_INET6) {
1016 struct sockaddr_in6 *sin6=(struct sockaddr_in6*)sa;
1017 if (len != sizeof(struct sockaddr_in6)) {
1018 log_fn(level, LD_NET, "Length of address not as expected: %d vs %d",
1019 len,(int)sizeof(struct sockaddr_in6));
1020 ok = 0;
1022 if (tor_mem_is_zero((void*)sin6->sin6_addr.s6_addr, 16) ||
1023 sin6->sin6_port == 0) {
1024 log_fn(level, LD_NET,
1025 "Address for new connection has address/port equal to zero.");
1026 ok = 0;
1028 } else {
1029 ok = 0;
1031 return ok ? 0 : -1;
1034 /** Check whether the socket family from an accepted socket <b>got</b> is the
1035 * same as the one that <b>listener</b> is waiting for. If it isn't, log
1036 * a useful message and return -1. Else return 0.
1038 * This is annoying, but can apparently happen on some Darwins. */
1039 static int
1040 check_sockaddr_family_match(sa_family_t got, connection_t *listener)
1042 if (got != listener->socket_family) {
1043 log_info(LD_BUG, "A listener connection returned a socket with a "
1044 "mismatched family. %s for addr_family %d gave us a socket "
1045 "with address family %d. Dropping.",
1046 conn_type_to_string(listener->type),
1047 (int)listener->socket_family,
1048 (int)got);
1049 return -1;
1051 return 0;
1054 /** The listener connection <b>conn</b> told poll() it wanted to read.
1055 * Call accept() on conn-\>s, and add the new connection if necessary.
1057 static int
1058 connection_handle_listener_read(connection_t *conn, int new_type)
1060 int news; /* the new socket */
1061 connection_t *newconn;
1062 /* information about the remote peer when connecting to other routers */
1063 char addrbuf[256];
1064 struct sockaddr *remote = (struct sockaddr*)addrbuf;
1065 /* length of the remote address. Must be whatever accept() needs. */
1066 socklen_t remotelen = (socklen_t)sizeof(addrbuf);
1067 or_options_t *options = get_options();
1069 tor_assert((size_t)remotelen >= sizeof(struct sockaddr_in));
1070 memset(addrbuf, 0, sizeof(addrbuf));
1072 news = tor_accept_socket(conn->s,remote,&remotelen);
1073 if (news < 0) { /* accept() error */
1074 int e = tor_socket_errno(conn->s);
1075 if (ERRNO_IS_ACCEPT_EAGAIN(e)) {
1076 return 0; /* he hung up before we could accept(). that's fine. */
1077 } else if (ERRNO_IS_ACCEPT_RESOURCE_LIMIT(e)) {
1078 warn_too_many_conns();
1079 return 0;
1081 /* else there was a real error. */
1082 log_warn(LD_NET,"accept() failed: %s. Closing listener.",
1083 tor_socket_strerror(e));
1084 connection_mark_for_close(conn);
1085 return -1;
1087 log_debug(LD_NET,
1088 "Connection accepted on socket %d (child of fd %d).",
1089 news,conn->s);
1091 set_socket_nonblocking(news);
1093 if (options->ConstrainedSockets)
1094 set_constrained_socket_buffers(news, (int)options->ConstrainedSockSize);
1096 if (check_sockaddr_family_match(remote->sa_family, conn) < 0) {
1097 tor_close_socket(news);
1098 return 0;
1101 if (conn->socket_family == AF_INET || conn->socket_family == AF_INET6) {
1102 tor_addr_t addr;
1103 uint16_t port;
1104 if (check_sockaddr(remote, remotelen, LOG_INFO)<0) {
1105 log_info(LD_NET,
1106 "accept() returned a strange address; trying getsockname().");
1107 remotelen=sizeof(addrbuf);
1108 memset(addrbuf, 0, sizeof(addrbuf));
1109 if (getsockname(news, remote, &remotelen)<0) {
1110 int e = tor_socket_errno(news);
1111 log_warn(LD_NET, "getsockname() for new connection failed: %s",
1112 tor_socket_strerror(e));
1113 } else {
1114 if (check_sockaddr((struct sockaddr*)addrbuf, remotelen,
1115 LOG_WARN) < 0) {
1116 log_warn(LD_NET,"Something's wrong with this conn. Closing it.");
1117 tor_close_socket(news);
1118 return 0;
1123 if (check_sockaddr_family_match(remote->sa_family, conn) < 0) {
1124 tor_close_socket(news);
1125 return 0;
1128 tor_addr_from_sockaddr(&addr, remote, &port);
1130 /* process entrance policies here, before we even create the connection */
1131 if (new_type == CONN_TYPE_AP) {
1132 /* check sockspolicy to see if we should accept it */
1133 if (socks_policy_permits_address(&addr) == 0) {
1134 log_notice(LD_APP,
1135 "Denying socks connection from untrusted address %s.",
1136 fmt_addr(&addr));
1137 tor_close_socket(news);
1138 return 0;
1141 if (new_type == CONN_TYPE_DIR) {
1142 /* check dirpolicy to see if we should accept it */
1143 if (dir_policy_permits_address(&addr) == 0) {
1144 log_notice(LD_DIRSERV,"Denying dir connection from address %s.",
1145 fmt_addr(&addr));
1146 tor_close_socket(news);
1147 return 0;
1151 newconn = connection_new(new_type, conn->socket_family);
1152 newconn->s = news;
1154 /* remember the remote address */
1155 tor_addr_copy(&newconn->addr, &addr);
1156 newconn->port = port;
1157 newconn->address = tor_dup_addr(&addr);
1159 } else if (conn->socket_family == AF_UNIX) {
1160 /* For now only control ports can be Unix domain sockets
1161 * and listeners at the same time */
1162 tor_assert(conn->type == CONN_TYPE_CONTROL_LISTENER);
1164 newconn = connection_new(new_type, conn->socket_family);
1165 newconn->s = news;
1167 /* remember the remote address -- do we have anything sane to put here? */
1168 tor_addr_make_unspec(&newconn->addr);
1169 newconn->port = 1;
1170 newconn->address = tor_strdup(conn->address);
1171 } else {
1172 tor_assert(0);
1175 if (connection_add(newconn) < 0) { /* no space, forget it */
1176 connection_free(newconn);
1177 return 0; /* no need to tear down the parent */
1180 if (connection_init_accepted_conn(newconn, conn->type) < 0) {
1181 connection_mark_for_close(newconn);
1182 return 0;
1184 return 0;
1187 /** Initialize states for newly accepted connection <b>conn</b>.
1188 * If conn is an OR, start the TLS handshake.
1189 * If conn is a transparent AP, get its original destination
1190 * and place it in circuit_wait.
1192 static int
1193 connection_init_accepted_conn(connection_t *conn, uint8_t listener_type)
1195 connection_start_reading(conn);
1197 switch (conn->type) {
1198 case CONN_TYPE_OR:
1199 control_event_or_conn_status(TO_OR_CONN(conn), OR_CONN_EVENT_NEW, 0);
1200 return connection_tls_start_handshake(TO_OR_CONN(conn), 1);
1201 case CONN_TYPE_AP:
1202 switch (listener_type) {
1203 case CONN_TYPE_AP_LISTENER:
1204 conn->state = AP_CONN_STATE_SOCKS_WAIT;
1205 break;
1206 case CONN_TYPE_AP_TRANS_LISTENER:
1207 conn->state = AP_CONN_STATE_CIRCUIT_WAIT;
1208 return connection_ap_process_transparent(TO_EDGE_CONN(conn));
1209 case CONN_TYPE_AP_NATD_LISTENER:
1210 conn->state = AP_CONN_STATE_NATD_WAIT;
1211 break;
1213 break;
1214 case CONN_TYPE_DIR:
1215 conn->purpose = DIR_PURPOSE_SERVER;
1216 conn->state = DIR_CONN_STATE_SERVER_COMMAND_WAIT;
1217 break;
1218 case CONN_TYPE_CONTROL:
1219 conn->state = CONTROL_CONN_STATE_NEEDAUTH;
1220 break;
1222 return 0;
1225 /** Take conn, make a nonblocking socket; try to connect to
1226 * addr:port (they arrive in *host order*). If fail, return -1 and if
1227 * applicable put your best guess about errno into *<b>socket_error</b>.
1228 * Else assign s to conn-\>s: if connected return 1, if EAGAIN return 0.
1230 * address is used to make the logs useful.
1232 * On success, add conn to the list of polled connections.
1235 connection_connect(connection_t *conn, const char *address,
1236 const tor_addr_t *addr, uint16_t port, int *socket_error)
1238 int s, inprogress = 0;
1239 char addrbuf[256];
1240 struct sockaddr *dest_addr = (struct sockaddr*) addrbuf;
1241 socklen_t dest_addr_len;
1242 or_options_t *options = get_options();
1243 int protocol_family;
1245 if (get_n_open_sockets() >= get_options()->_ConnLimit-1) {
1246 warn_too_many_conns();
1247 return -1;
1250 if (tor_addr_family(addr) == AF_INET6)
1251 protocol_family = PF_INET6;
1252 else
1253 protocol_family = PF_INET;
1255 s = tor_open_socket(protocol_family,SOCK_STREAM,IPPROTO_TCP);
1256 if (s < 0) {
1257 *socket_error = tor_socket_errno(-1);
1258 log_warn(LD_NET,"Error creating network socket: %s",
1259 tor_socket_strerror(*socket_error));
1260 return -1;
1263 if (options->OutboundBindAddress && !tor_addr_is_loopback(addr)) {
1264 struct sockaddr_in ext_addr;
1266 memset(&ext_addr, 0, sizeof(ext_addr));
1267 ext_addr.sin_family = AF_INET;
1268 ext_addr.sin_port = 0;
1269 if (!tor_inet_aton(options->OutboundBindAddress, &ext_addr.sin_addr)) {
1270 log_warn(LD_CONFIG,"Outbound bind address '%s' didn't parse. Ignoring.",
1271 options->OutboundBindAddress);
1272 } else {
1273 if (bind(s, (struct sockaddr*)&ext_addr,
1274 (socklen_t)sizeof(ext_addr)) < 0) {
1275 *socket_error = tor_socket_errno(s);
1276 log_warn(LD_NET,"Error binding network socket: %s",
1277 tor_socket_strerror(*socket_error));
1278 tor_close_socket(s);
1279 return -1;
1284 set_socket_nonblocking(s);
1286 if (options->ConstrainedSockets)
1287 set_constrained_socket_buffers(s, (int)options->ConstrainedSockSize);
1289 memset(addrbuf,0,sizeof(addrbuf));
1290 dest_addr = (struct sockaddr*) addrbuf;
1291 dest_addr_len = tor_addr_to_sockaddr(addr, port, dest_addr, sizeof(addrbuf));
1292 tor_assert(dest_addr_len > 0);
1294 log_debug(LD_NET, "Connecting to %s:%u.",
1295 escaped_safe_str_client(address), port);
1297 if (connect(s, dest_addr, dest_addr_len) < 0) {
1298 int e = tor_socket_errno(s);
1299 if (!ERRNO_IS_CONN_EINPROGRESS(e)) {
1300 /* yuck. kill it. */
1301 *socket_error = e;
1302 log_info(LD_NET,
1303 "connect() to %s:%u failed: %s",
1304 escaped_safe_str_client(address),
1305 port, tor_socket_strerror(e));
1306 tor_close_socket(s);
1307 return -1;
1308 } else {
1309 inprogress = 1;
1313 if (!server_mode(options))
1314 client_check_address_changed(s);
1316 /* it succeeded. we're connected. */
1317 log_fn(inprogress?LOG_DEBUG:LOG_INFO, LD_NET,
1318 "Connection to %s:%u %s (sock %d).",
1319 escaped_safe_str_client(address),
1320 port, inprogress?"in progress":"established", s);
1321 conn->s = s;
1322 if (connection_add(conn) < 0) /* no space, forget it */
1323 return -1;
1324 return inprogress ? 0 : 1;
1327 /** Convert state number to string representation for logging purposes.
1329 static const char *
1330 connection_proxy_state_to_string(int state)
1332 static const char *unknown = "???";
1333 static const char *states[] = {
1334 "PROXY_NONE",
1335 "PROXY_HTTPS_WANT_CONNECT_OK",
1336 "PROXY_SOCKS4_WANT_CONNECT_OK",
1337 "PROXY_SOCKS5_WANT_AUTH_METHOD_NONE",
1338 "PROXY_SOCKS5_WANT_AUTH_METHOD_RFC1929",
1339 "PROXY_SOCKS5_WANT_AUTH_RFC1929_OK",
1340 "PROXY_SOCKS5_WANT_CONNECT_OK",
1341 "PROXY_CONNECTED",
1344 if (state < PROXY_NONE || state > PROXY_CONNECTED)
1345 return unknown;
1347 return states[state];
1350 /** Write a proxy request of <b>type</b> (socks4, socks5, https) to conn
1351 * for conn->addr:conn->port, authenticating with the auth details given
1352 * in the configuration (if available). SOCKS 5 and HTTP CONNECT proxies
1353 * support authentication.
1355 * Returns -1 if conn->addr is incompatible with the proxy protocol, and
1356 * 0 otherwise.
1358 * Use connection_read_proxy_handshake() to complete the handshake.
1361 connection_proxy_connect(connection_t *conn, int type)
1363 or_options_t *options;
1365 tor_assert(conn);
1367 options = get_options();
1369 switch (type) {
1370 case PROXY_CONNECT: {
1371 char buf[1024];
1372 char *base64_authenticator=NULL;
1373 const char *authenticator = options->HttpsProxyAuthenticator;
1375 /* Send HTTP CONNECT and authentication (if available) in
1376 * one request */
1378 if (authenticator) {
1379 base64_authenticator = alloc_http_authenticator(authenticator);
1380 if (!base64_authenticator)
1381 log_warn(LD_OR, "Encoding https authenticator failed");
1384 if (base64_authenticator) {
1385 tor_snprintf(buf, sizeof(buf), "CONNECT %s:%d HTTP/1.1\r\n"
1386 "Proxy-Authorization: Basic %s\r\n\r\n",
1387 fmt_addr(&conn->addr),
1388 conn->port, base64_authenticator);
1389 tor_free(base64_authenticator);
1390 } else {
1391 tor_snprintf(buf, sizeof(buf), "CONNECT %s:%d HTTP/1.0\r\n\r\n",
1392 fmt_addr(&conn->addr), conn->port);
1395 connection_write_to_buf(buf, strlen(buf), conn);
1396 conn->proxy_state = PROXY_HTTPS_WANT_CONNECT_OK;
1397 break;
1400 case PROXY_SOCKS4: {
1401 unsigned char buf[9];
1402 uint16_t portn;
1403 uint32_t ip4addr;
1405 /* Send a SOCKS4 connect request with empty user id */
1407 if (tor_addr_family(&conn->addr) != AF_INET) {
1408 log_warn(LD_NET, "SOCKS4 client is incompatible with IPv6");
1409 return -1;
1412 ip4addr = tor_addr_to_ipv4n(&conn->addr);
1413 portn = htons(conn->port);
1415 buf[0] = 4; /* version */
1416 buf[1] = SOCKS_COMMAND_CONNECT; /* command */
1417 memcpy(buf + 2, &portn, 2); /* port */
1418 memcpy(buf + 4, &ip4addr, 4); /* addr */
1419 buf[8] = 0; /* userid (empty) */
1421 connection_write_to_buf((char *)buf, sizeof(buf), conn);
1422 conn->proxy_state = PROXY_SOCKS4_WANT_CONNECT_OK;
1423 break;
1426 case PROXY_SOCKS5: {
1427 unsigned char buf[4]; /* fields: vers, num methods, method list */
1429 /* Send a SOCKS5 greeting (connect request must wait) */
1431 buf[0] = 5; /* version */
1433 /* number of auth methods */
1434 if (options->Socks5ProxyUsername) {
1435 buf[1] = 2;
1436 buf[2] = 0x00; /* no authentication */
1437 buf[3] = 0x02; /* rfc1929 Username/Passwd auth */
1438 conn->proxy_state = PROXY_SOCKS5_WANT_AUTH_METHOD_RFC1929;
1439 } else {
1440 buf[1] = 1;
1441 buf[2] = 0x00; /* no authentication */
1442 conn->proxy_state = PROXY_SOCKS5_WANT_AUTH_METHOD_NONE;
1445 connection_write_to_buf((char *)buf, 2 + buf[1], conn);
1446 break;
1449 default:
1450 log_err(LD_BUG, "Invalid proxy protocol, %d", type);
1451 tor_fragile_assert();
1452 return -1;
1455 log_debug(LD_NET, "set state %s",
1456 connection_proxy_state_to_string(conn->proxy_state));
1458 return 0;
1461 /** Read conn's inbuf. If the http response from the proxy is all
1462 * here, make sure it's good news, then return 1. If it's bad news,
1463 * return -1. Else return 0 and hope for better luck next time.
1465 static int
1466 connection_read_https_proxy_response(connection_t *conn)
1468 char *headers;
1469 char *reason=NULL;
1470 int status_code;
1471 time_t date_header;
1473 switch (fetch_from_buf_http(conn->inbuf,
1474 &headers, MAX_HEADERS_SIZE,
1475 NULL, NULL, 10000, 0)) {
1476 case -1: /* overflow */
1477 log_warn(LD_PROTOCOL,
1478 "Your https proxy sent back an oversized response. Closing.");
1479 return -1;
1480 case 0:
1481 log_info(LD_NET,"https proxy response not all here yet. Waiting.");
1482 return 0;
1483 /* case 1, fall through */
1486 if (parse_http_response(headers, &status_code, &date_header,
1487 NULL, &reason) < 0) {
1488 log_warn(LD_NET,
1489 "Unparseable headers from proxy (connecting to '%s'). Closing.",
1490 conn->address);
1491 tor_free(headers);
1492 return -1;
1494 if (!reason) reason = tor_strdup("[no reason given]");
1496 if (status_code == 200) {
1497 log_info(LD_NET,
1498 "HTTPS connect to '%s' successful! (200 %s) Starting TLS.",
1499 conn->address, escaped(reason));
1500 tor_free(reason);
1501 return 1;
1503 /* else, bad news on the status code */
1504 log_warn(LD_NET,
1505 "The https proxy sent back an unexpected status code %d (%s). "
1506 "Closing.",
1507 status_code, escaped(reason));
1508 tor_free(reason);
1509 return -1;
1512 /** Send SOCKS5 CONNECT command to <b>conn</b>, copying <b>conn->addr</b>
1513 * and <b>conn->port</b> into the request.
1515 static void
1516 connection_send_socks5_connect(connection_t *conn)
1518 unsigned char buf[1024];
1519 size_t reqsize = 6;
1520 uint16_t port = htons(conn->port);
1522 buf[0] = 5; /* version */
1523 buf[1] = SOCKS_COMMAND_CONNECT; /* command */
1524 buf[2] = 0; /* reserved */
1526 if (tor_addr_family(&conn->addr) == AF_INET) {
1527 uint32_t addr = tor_addr_to_ipv4n(&conn->addr);
1529 buf[3] = 1;
1530 reqsize += 4;
1531 memcpy(buf + 4, &addr, 4);
1532 memcpy(buf + 8, &port, 2);
1533 } else { /* AF_INET6 */
1534 buf[3] = 4;
1535 reqsize += 16;
1536 memcpy(buf + 4, tor_addr_to_in6(&conn->addr), 16);
1537 memcpy(buf + 20, &port, 2);
1540 connection_write_to_buf((char *)buf, reqsize, conn);
1542 conn->proxy_state = PROXY_SOCKS5_WANT_CONNECT_OK;
1545 /** Call this from connection_*_process_inbuf() to advance the proxy
1546 * handshake.
1548 * No matter what proxy protocol is used, if this function returns 1, the
1549 * handshake is complete, and the data remaining on inbuf may contain the
1550 * start of the communication with the requested server.
1552 * Returns 0 if the current buffer contains an incomplete response, and -1
1553 * on error.
1556 connection_read_proxy_handshake(connection_t *conn)
1558 int ret = 0;
1559 char *reason = NULL;
1561 log_debug(LD_NET, "enter state %s",
1562 connection_proxy_state_to_string(conn->proxy_state));
1564 switch (conn->proxy_state) {
1565 case PROXY_HTTPS_WANT_CONNECT_OK:
1566 ret = connection_read_https_proxy_response(conn);
1567 if (ret == 1)
1568 conn->proxy_state = PROXY_CONNECTED;
1569 break;
1571 case PROXY_SOCKS4_WANT_CONNECT_OK:
1572 ret = fetch_from_buf_socks_client(conn->inbuf,
1573 conn->proxy_state,
1574 &reason);
1575 if (ret == 1)
1576 conn->proxy_state = PROXY_CONNECTED;
1577 break;
1579 case PROXY_SOCKS5_WANT_AUTH_METHOD_NONE:
1580 ret = fetch_from_buf_socks_client(conn->inbuf,
1581 conn->proxy_state,
1582 &reason);
1583 /* no auth needed, do connect */
1584 if (ret == 1) {
1585 connection_send_socks5_connect(conn);
1586 ret = 0;
1588 break;
1590 case PROXY_SOCKS5_WANT_AUTH_METHOD_RFC1929:
1591 ret = fetch_from_buf_socks_client(conn->inbuf,
1592 conn->proxy_state,
1593 &reason);
1595 /* send auth if needed, otherwise do connect */
1596 if (ret == 1) {
1597 connection_send_socks5_connect(conn);
1598 ret = 0;
1599 } else if (ret == 2) {
1600 unsigned char buf[1024];
1601 size_t reqsize, usize, psize;
1602 const char *user, *pass;
1604 user = get_options()->Socks5ProxyUsername;
1605 pass = get_options()->Socks5ProxyPassword;
1606 tor_assert(user && pass);
1608 /* XXX len of user and pass must be <= 255 !!! */
1609 usize = strlen(user);
1610 psize = strlen(pass);
1611 tor_assert(usize <= 255 && psize <= 255);
1612 reqsize = 3 + usize + psize;
1614 buf[0] = 1; /* negotiation version */
1615 buf[1] = usize;
1616 memcpy(buf + 2, user, usize);
1617 buf[2 + usize] = psize;
1618 memcpy(buf + 3 + usize, pass, psize);
1620 connection_write_to_buf((char *)buf, reqsize, conn);
1622 conn->proxy_state = PROXY_SOCKS5_WANT_AUTH_RFC1929_OK;
1623 ret = 0;
1625 break;
1627 case PROXY_SOCKS5_WANT_AUTH_RFC1929_OK:
1628 ret = fetch_from_buf_socks_client(conn->inbuf,
1629 conn->proxy_state,
1630 &reason);
1631 /* send the connect request */
1632 if (ret == 1) {
1633 connection_send_socks5_connect(conn);
1634 ret = 0;
1636 break;
1638 case PROXY_SOCKS5_WANT_CONNECT_OK:
1639 ret = fetch_from_buf_socks_client(conn->inbuf,
1640 conn->proxy_state,
1641 &reason);
1642 if (ret == 1)
1643 conn->proxy_state = PROXY_CONNECTED;
1644 break;
1646 default:
1647 log_err(LD_BUG, "Invalid proxy_state for reading, %d",
1648 conn->proxy_state);
1649 tor_fragile_assert();
1650 ret = -1;
1651 break;
1654 log_debug(LD_NET, "leaving state %s",
1655 connection_proxy_state_to_string(conn->proxy_state));
1657 if (ret < 0) {
1658 if (reason) {
1659 log_warn(LD_NET, "Proxy Client: unable to connect to %s:%d (%s)",
1660 conn->address, conn->port, escaped(reason));
1661 tor_free(reason);
1662 } else {
1663 log_warn(LD_NET, "Proxy Client: unable to connect to %s:%d",
1664 conn->address, conn->port);
1666 } else if (ret == 1) {
1667 log_info(LD_NET, "Proxy Client: connection to %s:%d successful",
1668 conn->address, conn->port);
1671 return ret;
1675 * Launch any configured listener connections of type <b>type</b>. (A
1676 * listener is configured if <b>port_option</b> is non-zero. If any
1677 * ListenAddress configuration options are given in <b>cfg</b>, create a
1678 * connection binding to each one. Otherwise, create a single
1679 * connection binding to the address <b>default_addr</b>.)
1681 * Only launch the listeners of this type that are not already open, and
1682 * only close listeners that are no longer wanted. Existing listeners
1683 * that are still configured are not touched.
1685 * If <b>disable_all_conns</b> is set, then never open new conns, and
1686 * close the existing ones.
1688 * Add all old conns that should be closed to <b>replaced_conns</b>.
1689 * Add all new connections to <b>new_conns</b>.
1691 static int
1692 retry_listeners(int type, config_line_t *cfg,
1693 int port_option, const char *default_addr,
1694 smartlist_t *replaced_conns,
1695 smartlist_t *new_conns,
1696 int disable_all_conns,
1697 int socket_family)
1699 smartlist_t *launch = smartlist_create(), *conns;
1700 int free_launch_elts = 1;
1701 int r;
1702 config_line_t *c;
1703 connection_t *conn;
1704 config_line_t *line;
1706 tor_assert(socket_family == AF_INET || socket_family == AF_UNIX);
1708 if (cfg && port_option) {
1709 for (c = cfg; c; c = c->next) {
1710 smartlist_add(launch, c);
1712 free_launch_elts = 0;
1713 } else if (port_option) {
1714 line = tor_malloc_zero(sizeof(config_line_t));
1715 line->key = tor_strdup("");
1716 line->value = tor_strdup(default_addr);
1717 smartlist_add(launch, line);
1721 SMARTLIST_FOREACH(launch, config_line_t *, l,
1722 log_fn(LOG_NOTICE, "#%s#%s", l->key, l->value));
1725 conns = get_connection_array();
1726 SMARTLIST_FOREACH(conns, connection_t *, conn,
1728 if (conn->type != type ||
1729 conn->socket_family != socket_family ||
1730 conn->marked_for_close)
1731 continue;
1732 /* Okay, so this is a listener. Is it configured? */
1733 line = NULL;
1734 SMARTLIST_FOREACH(launch, config_line_t *, wanted,
1736 char *address=NULL;
1737 uint16_t port;
1738 switch (socket_family) {
1739 case AF_INET:
1740 if (!parse_addr_port(LOG_WARN,
1741 wanted->value, &address, NULL, &port)) {
1742 int addr_matches = !strcasecmp(address, conn->address);
1743 tor_free(address);
1744 if (! port)
1745 port = port_option;
1746 if (port == conn->port && addr_matches) {
1747 line = wanted;
1748 break;
1751 break;
1752 case AF_UNIX:
1753 if (!strcasecmp(wanted->value, conn->address)) {
1754 line = wanted;
1755 break;
1757 break;
1758 default:
1759 tor_assert(0);
1762 if (!line || disable_all_conns) {
1763 /* This one isn't configured. Close it. */
1764 log_notice(LD_NET, "Closing no-longer-configured %s on %s:%d",
1765 conn_type_to_string(type), conn->address, conn->port);
1766 if (replaced_conns) {
1767 smartlist_add(replaced_conns, conn);
1768 } else {
1769 connection_close_immediate(conn);
1770 connection_mark_for_close(conn);
1772 } else {
1773 /* It's configured; we don't need to launch it. */
1774 // log_debug(LD_NET, "Already have %s on %s:%d",
1775 // conn_type_to_string(type), conn->address, conn->port);
1776 smartlist_remove(launch, line);
1777 if (free_launch_elts)
1778 config_free_lines(line);
1782 /* Now open all the listeners that are configured but not opened. */
1783 r = 0;
1784 if (!disable_all_conns) {
1785 SMARTLIST_FOREACH_BEGIN(launch, config_line_t *, cfg_line) {
1786 char *address = NULL;
1787 struct sockaddr *listensockaddr;
1788 socklen_t listensocklen = 0;
1790 switch (socket_family) {
1791 case AF_INET:
1792 listensockaddr = (struct sockaddr *)
1793 create_inet_sockaddr(cfg_line->value,
1794 (uint16_t) port_option,
1795 &address, &listensocklen);
1796 break;
1797 case AF_UNIX:
1798 listensockaddr = (struct sockaddr *)
1799 create_unix_sockaddr(cfg_line->value,
1800 &address, &listensocklen);
1801 break;
1802 default:
1803 tor_assert(0);
1806 if (listensockaddr) {
1807 conn = connection_create_listener(listensockaddr, listensocklen,
1808 type, address);
1809 tor_free(listensockaddr);
1810 tor_free(address);
1811 } else
1812 conn = NULL;
1814 if (!conn) {
1815 r = -1;
1816 } else {
1817 if (new_conns)
1818 smartlist_add(new_conns, conn);
1820 } SMARTLIST_FOREACH_END(cfg_line);
1823 if (free_launch_elts) {
1824 SMARTLIST_FOREACH(launch, config_line_t *, cfg_line,
1825 config_free_lines(cfg_line));
1827 smartlist_free(launch);
1829 return r;
1832 /** Launch listeners for each port you should have open. Only launch
1833 * listeners who are not already open, and only close listeners we no longer
1834 * want.
1836 * Add all old conns that should be closed to <b>replaced_conns</b>.
1837 * Add all new connections to <b>new_conns</b>.
1840 retry_all_listeners(smartlist_t *replaced_conns,
1841 smartlist_t *new_conns)
1843 or_options_t *options = get_options();
1845 if (retry_listeners(CONN_TYPE_OR_LISTENER, options->ORListenAddress,
1846 options->ORPort, "0.0.0.0",
1847 replaced_conns, new_conns, options->ClientOnly,
1848 AF_INET)<0)
1849 return -1;
1850 if (retry_listeners(CONN_TYPE_DIR_LISTENER, options->DirListenAddress,
1851 options->DirPort, "0.0.0.0",
1852 replaced_conns, new_conns, options->ClientOnly,
1853 AF_INET)<0)
1854 return -1;
1855 if (retry_listeners(CONN_TYPE_AP_LISTENER, options->SocksListenAddress,
1856 options->SocksPort, "127.0.0.1",
1857 replaced_conns, new_conns, 0,
1858 AF_INET)<0)
1859 return -1;
1860 if (retry_listeners(CONN_TYPE_AP_TRANS_LISTENER, options->TransListenAddress,
1861 options->TransPort, "127.0.0.1",
1862 replaced_conns, new_conns, 0,
1863 AF_INET)<0)
1864 return -1;
1865 if (retry_listeners(CONN_TYPE_AP_NATD_LISTENER, options->NatdListenAddress,
1866 options->NatdPort, "127.0.0.1",
1867 replaced_conns, new_conns, 0,
1868 AF_INET)<0)
1869 return -1;
1870 if (retry_listeners(CONN_TYPE_AP_DNS_LISTENER, options->DNSListenAddress,
1871 options->DNSPort, "127.0.0.1",
1872 replaced_conns, new_conns, 0,
1873 AF_INET)<0)
1874 return -1;
1875 if (retry_listeners(CONN_TYPE_CONTROL_LISTENER,
1876 options->ControlListenAddress,
1877 options->ControlPort, "127.0.0.1",
1878 replaced_conns, new_conns, 0,
1879 AF_INET)<0)
1880 return -1;
1881 if (retry_listeners(CONN_TYPE_CONTROL_LISTENER,
1882 options->ControlSocket,
1883 options->ControlSocket ? 1 : 0, NULL,
1884 replaced_conns, new_conns, 0,
1885 AF_UNIX)<0)
1886 return -1;
1888 return 0;
1891 /** Return 1 if we should apply rate limiting to <b>conn</b>,
1892 * and 0 otherwise. Right now this just checks if it's an internal
1893 * IP address or an internal connection. */
1894 static int
1895 connection_is_rate_limited(connection_t *conn)
1897 if (conn->linked || /* internal connection */
1898 tor_addr_family(&conn->addr) == AF_UNSPEC || /* no address */
1899 tor_addr_is_internal(&conn->addr, 0)) /* internal address */
1900 return 0;
1901 else
1902 return 1;
1905 extern int global_read_bucket, global_write_bucket;
1906 extern int global_relayed_read_bucket, global_relayed_write_bucket;
1908 /** Did either global write bucket run dry last second? If so,
1909 * we are likely to run dry again this second, so be stingy with the
1910 * tokens we just put in. */
1911 static int write_buckets_empty_last_second = 0;
1913 /** How many seconds of no active local circuits will make the
1914 * connection revert to the "relayed" bandwidth class? */
1915 #define CLIENT_IDLE_TIME_FOR_PRIORITY 30
1917 /** Return 1 if <b>conn</b> should use tokens from the "relayed"
1918 * bandwidth rates, else 0. Currently, only OR conns with bandwidth
1919 * class 1, and directory conns that are serving data out, count.
1921 static int
1922 connection_counts_as_relayed_traffic(connection_t *conn, time_t now)
1924 if (conn->type == CONN_TYPE_OR &&
1925 TO_OR_CONN(conn)->client_used + CLIENT_IDLE_TIME_FOR_PRIORITY < now)
1926 return 1;
1927 if (conn->type == CONN_TYPE_DIR && DIR_CONN_IS_SERVER(conn))
1928 return 1;
1929 return 0;
1932 /** Helper function to decide how many bytes out of <b>global_bucket</b>
1933 * we're willing to use for this transaction. <b>base</b> is the size
1934 * of a cell on the network; <b>priority</b> says whether we should
1935 * write many of them or just a few; and <b>conn_bucket</b> (if
1936 * non-negative) provides an upper limit for our answer. */
1937 static ssize_t
1938 connection_bucket_round_robin(int base, int priority,
1939 ssize_t global_bucket, ssize_t conn_bucket)
1941 ssize_t at_most;
1942 ssize_t num_bytes_high = (priority ? 32 : 16) * base;
1943 ssize_t num_bytes_low = (priority ? 4 : 2) * base;
1945 /* Do a rudimentary round-robin so one circuit can't hog a connection.
1946 * Pick at most 32 cells, at least 4 cells if possible, and if we're in
1947 * the middle pick 1/8 of the available bandwidth. */
1948 at_most = global_bucket / 8;
1949 at_most -= (at_most % base); /* round down */
1950 if (at_most > num_bytes_high) /* 16 KB, or 8 KB for low-priority */
1951 at_most = num_bytes_high;
1952 else if (at_most < num_bytes_low) /* 2 KB, or 1 KB for low-priority */
1953 at_most = num_bytes_low;
1955 if (at_most > global_bucket)
1956 at_most = global_bucket;
1958 if (conn_bucket >= 0 && at_most > conn_bucket)
1959 at_most = conn_bucket;
1961 if (at_most < 0)
1962 return 0;
1963 return at_most;
1966 /** How many bytes at most can we read onto this connection? */
1967 static ssize_t
1968 connection_bucket_read_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 conn_bucket = -1;
1974 int global_bucket = global_read_bucket;
1976 if (connection_speaks_cells(conn)) {
1977 or_connection_t *or_conn = TO_OR_CONN(conn);
1978 if (conn->state == OR_CONN_STATE_OPEN)
1979 conn_bucket = or_conn->read_bucket;
1982 if (!connection_is_rate_limited(conn)) {
1983 /* be willing to read on local conns even if our buckets are empty */
1984 return conn_bucket>=0 ? conn_bucket : 1<<14;
1987 if (connection_counts_as_relayed_traffic(conn, now) &&
1988 global_relayed_read_bucket <= global_read_bucket)
1989 global_bucket = global_relayed_read_bucket;
1991 return connection_bucket_round_robin(base, priority,
1992 global_bucket, conn_bucket);
1995 /** How many bytes at most can we write onto this connection? */
1996 ssize_t
1997 connection_bucket_write_limit(connection_t *conn, time_t now)
1999 int base = connection_speaks_cells(conn) ?
2000 CELL_NETWORK_SIZE : RELAY_PAYLOAD_SIZE;
2001 int priority = conn->type != CONN_TYPE_DIR;
2002 int conn_bucket = (int)conn->outbuf_flushlen;
2003 int global_bucket = global_write_bucket;
2005 if (!connection_is_rate_limited(conn)) {
2006 /* be willing to write to local conns even if our buckets are empty */
2007 return conn->outbuf_flushlen;
2010 if (connection_speaks_cells(conn)) {
2011 /* use the per-conn write limit if it's lower, but if it's less
2012 * than zero just use zero */
2013 or_connection_t *or_conn = TO_OR_CONN(conn);
2014 if (conn->state == OR_CONN_STATE_OPEN)
2015 if (or_conn->write_bucket < conn_bucket)
2016 conn_bucket = or_conn->write_bucket >= 0 ?
2017 or_conn->write_bucket : 0;
2020 if (connection_counts_as_relayed_traffic(conn, now) &&
2021 global_relayed_write_bucket <= global_write_bucket)
2022 global_bucket = global_relayed_write_bucket;
2024 return connection_bucket_round_robin(base, priority,
2025 global_bucket, conn_bucket);
2028 /** Return 1 if the global write buckets are low enough that we
2029 * shouldn't send <b>attempt</b> bytes of low-priority directory stuff
2030 * out to <b>conn</b>. Else return 0.
2032 * Priority is 1 for v1 requests (directories and running-routers),
2033 * and 2 for v2 requests (statuses and descriptors). But see FFFF in
2034 * directory_handle_command_get() for why we don't use priority 2 yet.
2036 * There are a lot of parameters we could use here:
2037 * - global_relayed_write_bucket. Low is bad.
2038 * - global_write_bucket. Low is bad.
2039 * - bandwidthrate. Low is bad.
2040 * - bandwidthburst. Not a big factor?
2041 * - attempt. High is bad.
2042 * - total bytes queued on outbufs. High is bad. But I'm wary of
2043 * using this, since a few slow-flushing queues will pump up the
2044 * number without meaning what we meant to mean. What we really
2045 * mean is "total directory bytes added to outbufs recently", but
2046 * that's harder to quantify and harder to keep track of.
2049 global_write_bucket_low(connection_t *conn, size_t attempt, int priority)
2051 int smaller_bucket = global_write_bucket < global_relayed_write_bucket ?
2052 global_write_bucket : global_relayed_write_bucket;
2053 if (authdir_mode(get_options()) && priority>1)
2054 return 0; /* there's always room to answer v2 if we're an auth dir */
2056 if (!connection_is_rate_limited(conn))
2057 return 0; /* local conns don't get limited */
2059 if (smaller_bucket < (int)attempt)
2060 return 1; /* not enough space no matter the priority */
2062 if (write_buckets_empty_last_second)
2063 return 1; /* we're already hitting our limits, no more please */
2065 if (priority == 1) { /* old-style v1 query */
2066 /* Could we handle *two* of these requests within the next two seconds? */
2067 or_options_t *options = get_options();
2068 int64_t can_write = (int64_t)smaller_bucket
2069 + 2*(options->RelayBandwidthRate ? options->RelayBandwidthRate :
2070 options->BandwidthRate);
2071 if (can_write < 2*(int64_t)attempt)
2072 return 1;
2073 } else { /* v2 query */
2074 /* no further constraints yet */
2076 return 0;
2079 /** We just read <b>num_read</b> and wrote <b>num_written</b> bytes
2080 * onto <b>conn</b>. Decrement buckets appropriately. */
2081 static void
2082 connection_buckets_decrement(connection_t *conn, time_t now,
2083 size_t num_read, size_t num_written)
2085 if (num_written >= INT_MAX || num_read >= INT_MAX) {
2086 log_err(LD_BUG, "Value out of range. num_read=%lu, num_written=%lu, "
2087 "connection type=%s, state=%s",
2088 (unsigned long)num_read, (unsigned long)num_written,
2089 conn_type_to_string(conn->type),
2090 conn_state_to_string(conn->type, conn->state));
2091 if (num_written >= INT_MAX) num_written = 1;
2092 if (num_read >= INT_MAX) num_read = 1;
2093 tor_fragile_assert();
2096 /* Count bytes of answering direct and tunneled directory requests */
2097 if (conn->type == CONN_TYPE_DIR && conn->purpose == DIR_PURPOSE_SERVER) {
2098 if (num_read > 0)
2099 rep_hist_note_dir_bytes_read(num_read, now);
2100 if (num_written > 0)
2101 rep_hist_note_dir_bytes_written(num_written, now);
2104 if (!connection_is_rate_limited(conn))
2105 return; /* local IPs are free */
2106 if (num_read > 0) {
2107 if (conn->type == CONN_TYPE_EXIT)
2108 rep_hist_note_exit_bytes_read(conn->port, num_read);
2109 rep_hist_note_bytes_read(num_read, now);
2111 if (num_written > 0) {
2112 if (conn->type == CONN_TYPE_EXIT)
2113 rep_hist_note_exit_bytes_written(conn->port, num_written);
2114 rep_hist_note_bytes_written(num_written, now);
2117 if (connection_counts_as_relayed_traffic(conn, now)) {
2118 global_relayed_read_bucket -= (int)num_read;
2119 global_relayed_write_bucket -= (int)num_written;
2121 global_read_bucket -= (int)num_read;
2122 global_write_bucket -= (int)num_written;
2123 if (connection_speaks_cells(conn) && conn->state == OR_CONN_STATE_OPEN) {
2124 TO_OR_CONN(conn)->read_bucket -= (int)num_read;
2125 TO_OR_CONN(conn)->write_bucket -= (int)num_written;
2129 /** If we have exhausted our global buckets, or the buckets for conn,
2130 * stop reading. */
2131 static void
2132 connection_consider_empty_read_buckets(connection_t *conn)
2134 const char *reason;
2136 if (global_read_bucket <= 0) {
2137 reason = "global read bucket exhausted. Pausing.";
2138 } else if (connection_counts_as_relayed_traffic(conn, approx_time()) &&
2139 global_relayed_read_bucket <= 0) {
2140 reason = "global relayed read bucket exhausted. Pausing.";
2141 } else if (connection_speaks_cells(conn) &&
2142 conn->state == OR_CONN_STATE_OPEN &&
2143 TO_OR_CONN(conn)->read_bucket <= 0) {
2144 reason = "connection read bucket exhausted. Pausing.";
2145 } else
2146 return; /* all good, no need to stop it */
2148 LOG_FN_CONN(conn, (LOG_DEBUG, LD_NET, "%s", reason));
2149 conn->read_blocked_on_bw = 1;
2150 connection_stop_reading(conn);
2153 /** If we have exhausted our global buckets, or the buckets for conn,
2154 * stop writing. */
2155 static void
2156 connection_consider_empty_write_buckets(connection_t *conn)
2158 const char *reason;
2160 if (global_write_bucket <= 0) {
2161 reason = "global write bucket exhausted. Pausing.";
2162 } else if (connection_counts_as_relayed_traffic(conn, approx_time()) &&
2163 global_relayed_write_bucket <= 0) {
2164 reason = "global relayed write bucket exhausted. Pausing.";
2165 } else if (connection_speaks_cells(conn) &&
2166 conn->state == OR_CONN_STATE_OPEN &&
2167 TO_OR_CONN(conn)->write_bucket <= 0) {
2168 reason = "connection write bucket exhausted. Pausing.";
2169 } else
2170 return; /* all good, no need to stop it */
2172 LOG_FN_CONN(conn, (LOG_DEBUG, LD_NET, "%s", reason));
2173 conn->write_blocked_on_bw = 1;
2174 connection_stop_writing(conn);
2177 /** Initialize the global read bucket to options-\>BandwidthBurst. */
2178 void
2179 connection_bucket_init(void)
2181 or_options_t *options = get_options();
2182 /* start it at max traffic */
2183 global_read_bucket = (int)options->BandwidthBurst;
2184 global_write_bucket = (int)options->BandwidthBurst;
2185 if (options->RelayBandwidthRate) {
2186 global_relayed_read_bucket = (int)options->RelayBandwidthBurst;
2187 global_relayed_write_bucket = (int)options->RelayBandwidthBurst;
2188 } else {
2189 global_relayed_read_bucket = (int)options->BandwidthBurst;
2190 global_relayed_write_bucket = (int)options->BandwidthBurst;
2194 /** Refill a single <b>bucket</b> called <b>name</b> with bandwidth rate
2195 * <b>rate</b> and bandwidth burst <b>burst</b>, assuming that
2196 * <b>seconds_elapsed</b> seconds have passed since the last call.
2198 static void
2199 connection_bucket_refill_helper(int *bucket, int rate, int burst,
2200 int seconds_elapsed, const char *name)
2202 int starting_bucket = *bucket;
2203 if (starting_bucket < burst && seconds_elapsed) {
2204 if (((burst - starting_bucket)/seconds_elapsed) < rate) {
2205 *bucket = burst; /* We would overflow the bucket; just set it to
2206 * the maximum. */
2207 } else {
2208 int incr = rate*seconds_elapsed;
2209 *bucket += incr;
2210 if (*bucket > burst || *bucket < starting_bucket) {
2211 /* If we overflow the burst, or underflow our starting bucket,
2212 * cap the bucket value to burst. */
2213 /* XXXX this might be redundant now, but it doesn't show up
2214 * in profiles. Remove it after analysis. */
2215 *bucket = burst;
2218 log(LOG_DEBUG, LD_NET,"%s now %d.", name, *bucket);
2222 /** A second has rolled over; increment buckets appropriately. */
2223 void
2224 connection_bucket_refill(int seconds_elapsed, time_t now)
2226 or_options_t *options = get_options();
2227 smartlist_t *conns = get_connection_array();
2228 int relayrate, relayburst;
2230 if (options->RelayBandwidthRate) {
2231 relayrate = (int)options->RelayBandwidthRate;
2232 relayburst = (int)options->RelayBandwidthBurst;
2233 } else {
2234 relayrate = (int)options->BandwidthRate;
2235 relayburst = (int)options->BandwidthBurst;
2238 tor_assert(seconds_elapsed >= 0);
2240 write_buckets_empty_last_second =
2241 global_relayed_write_bucket <= 0 || global_write_bucket <= 0;
2243 /* refill the global buckets */
2244 connection_bucket_refill_helper(&global_read_bucket,
2245 (int)options->BandwidthRate,
2246 (int)options->BandwidthBurst,
2247 seconds_elapsed, "global_read_bucket");
2248 connection_bucket_refill_helper(&global_write_bucket,
2249 (int)options->BandwidthRate,
2250 (int)options->BandwidthBurst,
2251 seconds_elapsed, "global_write_bucket");
2252 connection_bucket_refill_helper(&global_relayed_read_bucket,
2253 relayrate, relayburst, seconds_elapsed,
2254 "global_relayed_read_bucket");
2255 connection_bucket_refill_helper(&global_relayed_write_bucket,
2256 relayrate, relayburst, seconds_elapsed,
2257 "global_relayed_write_bucket");
2259 /* refill the per-connection buckets */
2260 SMARTLIST_FOREACH(conns, connection_t *, conn,
2262 if (connection_speaks_cells(conn)) {
2263 or_connection_t *or_conn = TO_OR_CONN(conn);
2264 if (connection_bucket_should_increase(or_conn->read_bucket, or_conn)) {
2265 connection_bucket_refill_helper(&or_conn->read_bucket,
2266 or_conn->bandwidthrate,
2267 or_conn->bandwidthburst,
2268 seconds_elapsed,
2269 "or_conn->read_bucket");
2271 if (connection_bucket_should_increase(or_conn->write_bucket, or_conn)) {
2272 connection_bucket_refill_helper(&or_conn->write_bucket,
2273 or_conn->bandwidthrate,
2274 or_conn->bandwidthburst,
2275 seconds_elapsed,
2276 "or_conn->write_bucket");
2280 if (conn->read_blocked_on_bw == 1 /* marked to turn reading back on now */
2281 && global_read_bucket > 0 /* and we're allowed to read */
2282 && (!connection_counts_as_relayed_traffic(conn, now) ||
2283 global_relayed_read_bucket > 0) /* even if we're relayed traffic */
2284 && (!connection_speaks_cells(conn) ||
2285 conn->state != OR_CONN_STATE_OPEN ||
2286 TO_OR_CONN(conn)->read_bucket > 0)) {
2287 /* and either a non-cell conn or a cell conn with non-empty bucket */
2288 LOG_FN_CONN(conn, (LOG_DEBUG,LD_NET,
2289 "waking up conn (fd %d) for read", conn->s));
2290 conn->read_blocked_on_bw = 0;
2291 connection_start_reading(conn);
2294 if (conn->write_blocked_on_bw == 1
2295 && global_write_bucket > 0 /* and we're allowed to write */
2296 && (!connection_counts_as_relayed_traffic(conn, now) ||
2297 global_relayed_write_bucket > 0) /* even if it's relayed traffic */
2298 && (!connection_speaks_cells(conn) ||
2299 conn->state != OR_CONN_STATE_OPEN ||
2300 TO_OR_CONN(conn)->write_bucket > 0)) {
2301 LOG_FN_CONN(conn, (LOG_DEBUG,LD_NET,
2302 "waking up conn (fd %d) for write", conn->s));
2303 conn->write_blocked_on_bw = 0;
2304 connection_start_writing(conn);
2309 /** Is the <b>bucket</b> for connection <b>conn</b> low enough that we
2310 * should add another pile of tokens to it?
2312 static int
2313 connection_bucket_should_increase(int bucket, or_connection_t *conn)
2315 tor_assert(conn);
2317 if (conn->_base.state != OR_CONN_STATE_OPEN)
2318 return 0; /* only open connections play the rate limiting game */
2319 if (bucket >= conn->bandwidthburst)
2320 return 0;
2322 return 1;
2325 /** Read bytes from conn-\>s and process them.
2327 * This function gets called from conn_read() in main.c, either
2328 * when poll() has declared that conn wants to read, or (for OR conns)
2329 * when there are pending TLS bytes.
2331 * It calls connection_read_to_buf() to bring in any new bytes,
2332 * and then calls connection_process_inbuf() to process them.
2334 * Mark the connection and return -1 if you want to close it, else
2335 * return 0.
2337 static int
2338 connection_handle_read_impl(connection_t *conn)
2340 int max_to_read=-1, try_to_read;
2341 size_t before, n_read = 0;
2342 int socket_error = 0;
2344 if (conn->marked_for_close)
2345 return 0; /* do nothing */
2347 conn->timestamp_lastread = approx_time();
2349 switch (conn->type) {
2350 case CONN_TYPE_OR_LISTENER:
2351 return connection_handle_listener_read(conn, CONN_TYPE_OR);
2352 case CONN_TYPE_AP_LISTENER:
2353 case CONN_TYPE_AP_TRANS_LISTENER:
2354 case CONN_TYPE_AP_NATD_LISTENER:
2355 return connection_handle_listener_read(conn, CONN_TYPE_AP);
2356 case CONN_TYPE_DIR_LISTENER:
2357 return connection_handle_listener_read(conn, CONN_TYPE_DIR);
2358 case CONN_TYPE_CONTROL_LISTENER:
2359 return connection_handle_listener_read(conn, CONN_TYPE_CONTROL);
2360 case CONN_TYPE_AP_DNS_LISTENER:
2361 /* This should never happen; eventdns.c handles the reads here. */
2362 tor_fragile_assert();
2363 return 0;
2366 loop_again:
2367 try_to_read = max_to_read;
2368 tor_assert(!conn->marked_for_close);
2370 before = buf_datalen(conn->inbuf);
2371 if (connection_read_to_buf(conn, &max_to_read, &socket_error) < 0) {
2372 /* There's a read error; kill the connection.*/
2373 if (conn->type == CONN_TYPE_OR &&
2374 conn->state == OR_CONN_STATE_CONNECTING) {
2375 connection_or_connect_failed(TO_OR_CONN(conn),
2376 errno_to_orconn_end_reason(socket_error),
2377 tor_socket_strerror(socket_error));
2379 if (CONN_IS_EDGE(conn)) {
2380 edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
2381 connection_edge_end_errno(edge_conn);
2382 if (edge_conn->socks_request) /* broken, don't send a socks reply back */
2383 edge_conn->socks_request->has_finished = 1;
2385 connection_close_immediate(conn); /* Don't flush; connection is dead. */
2386 connection_mark_for_close(conn);
2387 return -1;
2389 n_read += buf_datalen(conn->inbuf) - before;
2390 if (CONN_IS_EDGE(conn) && try_to_read != max_to_read) {
2391 /* instruct it not to try to package partial cells. */
2392 if (connection_process_inbuf(conn, 0) < 0) {
2393 return -1;
2395 if (!conn->marked_for_close &&
2396 connection_is_reading(conn) &&
2397 !conn->inbuf_reached_eof &&
2398 max_to_read > 0)
2399 goto loop_again; /* try reading again, in case more is here now */
2401 /* one last try, packaging partial cells and all. */
2402 if (!conn->marked_for_close &&
2403 connection_process_inbuf(conn, 1) < 0) {
2404 return -1;
2406 if (conn->linked_conn) {
2407 /* The other side's handle_write() will never actually get called, so
2408 * we need to invoke the appropriate callbacks ourself. */
2409 connection_t *linked = conn->linked_conn;
2411 if (n_read) {
2412 /* Probably a no-op, but hey. */
2413 connection_buckets_decrement(linked, approx_time(), n_read, 0);
2415 if (connection_flushed_some(linked) < 0)
2416 connection_mark_for_close(linked);
2417 if (!connection_wants_to_flush(linked))
2418 connection_finished_flushing(linked);
2421 if (!buf_datalen(linked->outbuf) && conn->active_on_link)
2422 connection_stop_reading_from_linked_conn(conn);
2424 /* If we hit the EOF, call connection_reached_eof(). */
2425 if (!conn->marked_for_close &&
2426 conn->inbuf_reached_eof &&
2427 connection_reached_eof(conn) < 0) {
2428 return -1;
2430 return 0;
2434 connection_handle_read(connection_t *conn)
2436 int res;
2438 tor_gettimeofday_cache_clear();
2439 res = connection_handle_read_impl(conn);
2440 return res;
2443 /** Pull in new bytes from conn-\>s or conn-\>linked_conn onto conn-\>inbuf,
2444 * either directly or via TLS. Reduce the token buckets by the number of bytes
2445 * read.
2447 * If *max_to_read is -1, then decide it ourselves, else go with the
2448 * value passed to us. When returning, if it's changed, subtract the
2449 * number of bytes we read from *max_to_read.
2451 * Return -1 if we want to break conn, else return 0.
2453 static int
2454 connection_read_to_buf(connection_t *conn, int *max_to_read, int *socket_error)
2456 int result;
2457 ssize_t at_most = *max_to_read;
2458 size_t slack_in_buf, more_to_read;
2459 size_t n_read = 0, n_written = 0;
2461 if (at_most == -1) { /* we need to initialize it */
2462 /* how many bytes are we allowed to read? */
2463 at_most = connection_bucket_read_limit(conn, approx_time());
2466 slack_in_buf = buf_slack(conn->inbuf);
2467 again:
2468 if ((size_t)at_most > slack_in_buf && slack_in_buf >= 1024) {
2469 more_to_read = at_most - slack_in_buf;
2470 at_most = slack_in_buf;
2471 } else {
2472 more_to_read = 0;
2475 if (connection_speaks_cells(conn) &&
2476 conn->state > OR_CONN_STATE_PROXY_HANDSHAKING) {
2477 int pending;
2478 or_connection_t *or_conn = TO_OR_CONN(conn);
2479 size_t initial_size;
2480 if (conn->state == OR_CONN_STATE_TLS_HANDSHAKING ||
2481 conn->state == OR_CONN_STATE_TLS_CLIENT_RENEGOTIATING) {
2482 /* continue handshaking even if global token bucket is empty */
2483 return connection_tls_continue_handshake(or_conn);
2486 log_debug(LD_NET,
2487 "%d: starting, inbuf_datalen %ld (%d pending in tls object)."
2488 " at_most %ld.",
2489 conn->s,(long)buf_datalen(conn->inbuf),
2490 tor_tls_get_pending_bytes(or_conn->tls), (long)at_most);
2492 initial_size = buf_datalen(conn->inbuf);
2493 /* else open, or closing */
2494 result = read_to_buf_tls(or_conn->tls, at_most, conn->inbuf);
2495 if (TOR_TLS_IS_ERROR(result) || result == TOR_TLS_CLOSE)
2496 or_conn->tls_error = result;
2497 else
2498 or_conn->tls_error = 0;
2500 switch (result) {
2501 case TOR_TLS_CLOSE:
2502 case TOR_TLS_ERROR_IO:
2503 log_debug(LD_NET,"TLS connection closed %son read. Closing. "
2504 "(Nickname %s, address %s)",
2505 result == TOR_TLS_CLOSE ? "cleanly " : "",
2506 or_conn->nickname ? or_conn->nickname : "not set",
2507 conn->address);
2508 return result;
2509 CASE_TOR_TLS_ERROR_ANY_NONIO:
2510 log_debug(LD_NET,"tls error [%s]. breaking (nickname %s, address %s).",
2511 tor_tls_err_to_string(result),
2512 or_conn->nickname ? or_conn->nickname : "not set",
2513 conn->address);
2514 return result;
2515 case TOR_TLS_WANTWRITE:
2516 connection_start_writing(conn);
2517 return 0;
2518 case TOR_TLS_WANTREAD: /* we're already reading */
2519 case TOR_TLS_DONE: /* no data read, so nothing to process */
2520 result = 0;
2521 break; /* so we call bucket_decrement below */
2522 default:
2523 break;
2525 pending = tor_tls_get_pending_bytes(or_conn->tls);
2526 if (pending) {
2527 /* If we have any pending bytes, we read them now. This *can*
2528 * take us over our read allotment, but really we shouldn't be
2529 * believing that SSL bytes are the same as TCP bytes anyway. */
2530 int r2 = read_to_buf_tls(or_conn->tls, pending, conn->inbuf);
2531 if (r2<0) {
2532 log_warn(LD_BUG, "apparently, reading pending bytes can fail.");
2533 return -1;
2536 result = (int)(buf_datalen(conn->inbuf)-initial_size);
2537 tor_tls_get_n_raw_bytes(or_conn->tls, &n_read, &n_written);
2538 log_debug(LD_GENERAL, "After TLS read of %d: %ld read, %ld written",
2539 result, (long)n_read, (long)n_written);
2540 } else if (conn->linked) {
2541 if (conn->linked_conn) {
2542 result = move_buf_to_buf(conn->inbuf, conn->linked_conn->outbuf,
2543 &conn->linked_conn->outbuf_flushlen);
2544 } else {
2545 result = 0;
2547 //log_notice(LD_GENERAL, "Moved %d bytes on an internal link!", result);
2548 /* If the other side has disappeared, or if it's been marked for close and
2549 * we flushed its outbuf, then we should set our inbuf_reached_eof. */
2550 if (!conn->linked_conn ||
2551 (conn->linked_conn->marked_for_close &&
2552 buf_datalen(conn->linked_conn->outbuf) == 0))
2553 conn->inbuf_reached_eof = 1;
2555 n_read = (size_t) result;
2556 } else {
2557 /* !connection_speaks_cells, !conn->linked_conn. */
2558 int reached_eof = 0;
2559 CONN_LOG_PROTECT(conn,
2560 result = read_to_buf(conn->s, at_most, conn->inbuf, &reached_eof,
2561 socket_error));
2562 if (reached_eof)
2563 conn->inbuf_reached_eof = 1;
2565 // log_fn(LOG_DEBUG,"read_to_buf returned %d.",read_result);
2567 if (result < 0)
2568 return -1;
2569 n_read = (size_t) result;
2572 if (n_read > 0) { /* change *max_to_read */
2573 /*XXXX021 check for overflow*/
2574 *max_to_read = (int)(at_most - n_read);
2577 if (conn->type == CONN_TYPE_AP) {
2578 edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
2579 /*XXXX021 check for overflow*/
2580 edge_conn->n_read += (int)n_read;
2583 connection_buckets_decrement(conn, approx_time(), n_read, n_written);
2585 if (more_to_read && result == at_most) {
2586 slack_in_buf = buf_slack(conn->inbuf);
2587 at_most = more_to_read;
2588 goto again;
2591 /* Call even if result is 0, since the global read bucket may
2592 * have reached 0 on a different conn, and this guy needs to
2593 * know to stop reading. */
2594 connection_consider_empty_read_buckets(conn);
2595 if (n_written > 0 && connection_is_writing(conn))
2596 connection_consider_empty_write_buckets(conn);
2598 return 0;
2601 /** A pass-through to fetch_from_buf. */
2603 connection_fetch_from_buf(char *string, size_t len, connection_t *conn)
2605 return fetch_from_buf(string, len, conn->inbuf);
2608 /** Return conn-\>outbuf_flushlen: how many bytes conn wants to flush
2609 * from its outbuf. */
2611 connection_wants_to_flush(connection_t *conn)
2613 return conn->outbuf_flushlen > 0;
2616 /** Are there too many bytes on edge connection <b>conn</b>'s outbuf to
2617 * send back a relay-level sendme yet? Return 1 if so, 0 if not. Used by
2618 * connection_edge_consider_sending_sendme().
2621 connection_outbuf_too_full(connection_t *conn)
2623 return (conn->outbuf_flushlen > 10*CELL_PAYLOAD_SIZE);
2626 /** Try to flush more bytes onto conn-\>s.
2628 * This function gets called either from conn_write() in main.c
2629 * when poll() has declared that conn wants to write, or below
2630 * from connection_write_to_buf() when an entire TLS record is ready.
2632 * Update conn-\>timestamp_lastwritten to now, and call flush_buf
2633 * or flush_buf_tls appropriately. If it succeeds and there are no more
2634 * more bytes on conn->outbuf, then call connection_finished_flushing
2635 * on it too.
2637 * If <b>force</b>, then write as many bytes as possible, ignoring bandwidth
2638 * limits. (Used for flushing messages to controller connections on fatal
2639 * errors.)
2641 * Mark the connection and return -1 if you want to close it, else
2642 * return 0.
2644 static int
2645 connection_handle_write_impl(connection_t *conn, int force)
2647 int e;
2648 socklen_t len=(socklen_t)sizeof(e);
2649 int result;
2650 ssize_t max_to_write;
2651 time_t now = approx_time();
2652 size_t n_read = 0, n_written = 0;
2654 tor_assert(!connection_is_listener(conn));
2656 if (conn->marked_for_close || conn->s < 0)
2657 return 0; /* do nothing */
2659 if (conn->in_flushed_some) {
2660 log_warn(LD_BUG, "called recursively from inside conn->in_flushed_some");
2661 return 0;
2664 conn->timestamp_lastwritten = now;
2666 /* Sometimes, "writable" means "connected". */
2667 if (connection_state_is_connecting(conn)) {
2668 if (getsockopt(conn->s, SOL_SOCKET, SO_ERROR, (void*)&e, &len) < 0) {
2669 log_warn(LD_BUG,
2670 "getsockopt() syscall failed?! Please report to tor-ops.");
2671 if (CONN_IS_EDGE(conn))
2672 connection_edge_end_errno(TO_EDGE_CONN(conn));
2673 connection_mark_for_close(conn);
2674 return -1;
2676 if (e) {
2677 /* some sort of error, but maybe just inprogress still */
2678 if (!ERRNO_IS_CONN_EINPROGRESS(e)) {
2679 log_info(LD_NET,"in-progress connect failed. Removing. (%s)",
2680 tor_socket_strerror(e));
2681 if (CONN_IS_EDGE(conn))
2682 connection_edge_end_errno(TO_EDGE_CONN(conn));
2683 if (conn->type == CONN_TYPE_OR)
2684 connection_or_connect_failed(TO_OR_CONN(conn),
2685 errno_to_orconn_end_reason(e),
2686 tor_socket_strerror(e));
2688 connection_close_immediate(conn);
2689 connection_mark_for_close(conn);
2690 return -1;
2691 } else {
2692 return 0; /* no change, see if next time is better */
2695 /* The connection is successful. */
2696 if (connection_finished_connecting(conn)<0)
2697 return -1;
2700 max_to_write = force ? (ssize_t)conn->outbuf_flushlen
2701 : connection_bucket_write_limit(conn, now);
2703 if (connection_speaks_cells(conn) &&
2704 conn->state > OR_CONN_STATE_PROXY_HANDSHAKING) {
2705 or_connection_t *or_conn = TO_OR_CONN(conn);
2706 if (conn->state == OR_CONN_STATE_TLS_HANDSHAKING ||
2707 conn->state == OR_CONN_STATE_TLS_CLIENT_RENEGOTIATING) {
2708 connection_stop_writing(conn);
2709 if (connection_tls_continue_handshake(or_conn) < 0) {
2710 /* Don't flush; connection is dead. */
2711 connection_close_immediate(conn);
2712 connection_mark_for_close(conn);
2713 return -1;
2715 return 0;
2716 } else if (conn->state == OR_CONN_STATE_TLS_SERVER_RENEGOTIATING) {
2717 return connection_handle_read(conn);
2720 /* else open, or closing */
2721 result = flush_buf_tls(or_conn->tls, conn->outbuf,
2722 max_to_write, &conn->outbuf_flushlen);
2724 /* If we just flushed the last bytes, check if this tunneled dir
2725 * request is done. */
2726 if (buf_datalen(conn->outbuf) == 0 && conn->dirreq_id)
2727 geoip_change_dirreq_state(conn->dirreq_id, DIRREQ_TUNNELED,
2728 DIRREQ_OR_CONN_BUFFER_FLUSHED);
2730 switch (result) {
2731 CASE_TOR_TLS_ERROR_ANY:
2732 case TOR_TLS_CLOSE:
2733 log_info(LD_NET,result!=TOR_TLS_CLOSE?
2734 "tls error. breaking.":"TLS connection closed on flush");
2735 /* Don't flush; connection is dead. */
2736 connection_close_immediate(conn);
2737 connection_mark_for_close(conn);
2738 return -1;
2739 case TOR_TLS_WANTWRITE:
2740 log_debug(LD_NET,"wanted write.");
2741 /* we're already writing */
2742 return 0;
2743 case TOR_TLS_WANTREAD:
2744 /* Make sure to avoid a loop if the receive buckets are empty. */
2745 log_debug(LD_NET,"wanted read.");
2746 if (!connection_is_reading(conn)) {
2747 connection_stop_writing(conn);
2748 conn->write_blocked_on_bw = 1;
2749 /* we'll start reading again when we get more tokens in our
2750 * read bucket; then we'll start writing again too.
2753 /* else no problem, we're already reading */
2754 return 0;
2755 /* case TOR_TLS_DONE:
2756 * for TOR_TLS_DONE, fall through to check if the flushlen
2757 * is empty, so we can stop writing.
2761 tor_tls_get_n_raw_bytes(or_conn->tls, &n_read, &n_written);
2762 log_debug(LD_GENERAL, "After TLS write of %d: %ld read, %ld written",
2763 result, (long)n_read, (long)n_written);
2764 } else {
2765 CONN_LOG_PROTECT(conn,
2766 result = flush_buf(conn->s, conn->outbuf,
2767 max_to_write, &conn->outbuf_flushlen));
2768 if (result < 0) {
2769 if (CONN_IS_EDGE(conn))
2770 connection_edge_end_errno(TO_EDGE_CONN(conn));
2772 connection_close_immediate(conn); /* Don't flush; connection is dead. */
2773 connection_mark_for_close(conn);
2774 return -1;
2776 n_written = (size_t) result;
2779 if (conn->type == CONN_TYPE_AP) {
2780 edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
2781 /*XXXX021 check for overflow.*/
2782 edge_conn->n_written += (int)n_written;
2785 connection_buckets_decrement(conn, approx_time(), n_read, n_written);
2787 if (result > 0) {
2788 /* If we wrote any bytes from our buffer, then call the appropriate
2789 * functions. */
2790 if (connection_flushed_some(conn) < 0)
2791 connection_mark_for_close(conn);
2794 if (!connection_wants_to_flush(conn)) { /* it's done flushing */
2795 if (connection_finished_flushing(conn) < 0) {
2796 /* already marked */
2797 return -1;
2799 return 0;
2802 /* Call even if result is 0, since the global write bucket may
2803 * have reached 0 on a different conn, and this guy needs to
2804 * know to stop writing. */
2805 connection_consider_empty_write_buckets(conn);
2806 if (n_read > 0 && connection_is_reading(conn))
2807 connection_consider_empty_read_buckets(conn);
2809 return 0;
2813 connection_handle_write(connection_t *conn, int force)
2815 int res;
2816 tor_gettimeofday_cache_clear();
2817 res = connection_handle_write_impl(conn, force);
2818 return res;
2821 /** OpenSSL TLS record size is 16383; this is close. The goal here is to
2822 * push data out as soon as we know there's enough for a TLS record, so
2823 * during periods of high load we won't read entire megabytes from
2824 * input before pushing any data out. It also has the feature of not
2825 * growing huge outbufs unless something is slow. */
2826 #define MIN_TLS_FLUSHLEN 15872
2828 /** Append <b>len</b> bytes of <b>string</b> onto <b>conn</b>'s
2829 * outbuf, and ask it to start writing.
2831 * If <b>zlib</b> is nonzero, this is a directory connection that should get
2832 * its contents compressed or decompressed as they're written. If zlib is
2833 * negative, this is the last data to be compressed, and the connection's zlib
2834 * state should be flushed.
2836 * If it's an OR conn and an entire TLS record is ready, then try to
2837 * flush the record now. Similarly, if it's a local control connection
2838 * and a 64k chunk is ready, try to flush it all, so we don't end up with
2839 * many megabytes of controller info queued at once.
2841 void
2842 _connection_write_to_buf_impl(const char *string, size_t len,
2843 connection_t *conn, int zlib)
2845 /* XXXX This function really needs to return -1 on failure. */
2846 int r;
2847 size_t old_datalen;
2848 if (!len && !(zlib<0))
2849 return;
2850 /* if it's marked for close, only allow write if we mean to flush it */
2851 if (conn->marked_for_close && !conn->hold_open_until_flushed)
2852 return;
2854 old_datalen = buf_datalen(conn->outbuf);
2855 if (zlib) {
2856 dir_connection_t *dir_conn = TO_DIR_CONN(conn);
2857 int done = zlib < 0;
2858 CONN_LOG_PROTECT(conn, r = write_to_buf_zlib(conn->outbuf,
2859 dir_conn->zlib_state,
2860 string, len, done));
2861 } else {
2862 CONN_LOG_PROTECT(conn, r = write_to_buf(string, len, conn->outbuf));
2864 if (r < 0) {
2865 if (CONN_IS_EDGE(conn)) {
2866 /* if it failed, it means we have our package/delivery windows set
2867 wrong compared to our max outbuf size. close the whole circuit. */
2868 log_warn(LD_NET,
2869 "write_to_buf failed. Closing circuit (fd %d).", conn->s);
2870 circuit_mark_for_close(circuit_get_by_edge_conn(TO_EDGE_CONN(conn)),
2871 END_CIRC_REASON_INTERNAL);
2872 } else {
2873 log_warn(LD_NET,
2874 "write_to_buf failed. Closing connection (fd %d).", conn->s);
2875 connection_mark_for_close(conn);
2877 return;
2880 connection_start_writing(conn);
2881 if (zlib) {
2882 conn->outbuf_flushlen += buf_datalen(conn->outbuf) - old_datalen;
2883 } else {
2884 ssize_t extra = 0;
2885 conn->outbuf_flushlen += len;
2887 /* Should we try flushing the outbuf now? */
2888 if (conn->in_flushed_some) {
2889 /* Don't flush the outbuf when the reason we're writing more stuff is
2890 * _because_ we flushed the outbuf. That's unfair. */
2891 return;
2894 if (conn->type == CONN_TYPE_OR &&
2895 conn->outbuf_flushlen-len < MIN_TLS_FLUSHLEN &&
2896 conn->outbuf_flushlen >= MIN_TLS_FLUSHLEN) {
2897 /* We just pushed outbuf_flushlen to MIN_TLS_FLUSHLEN or above;
2898 * we can send out a full TLS frame now if we like. */
2899 extra = conn->outbuf_flushlen - MIN_TLS_FLUSHLEN;
2900 conn->outbuf_flushlen = MIN_TLS_FLUSHLEN;
2901 } else if (conn->type == CONN_TYPE_CONTROL &&
2902 !connection_is_rate_limited(conn) &&
2903 conn->outbuf_flushlen-len < 1<<16 &&
2904 conn->outbuf_flushlen >= 1<<16) {
2905 /* just try to flush all of it */
2906 } else
2907 return; /* no need to try flushing */
2909 if (connection_handle_write(conn, 0) < 0) {
2910 if (!conn->marked_for_close) {
2911 /* this connection is broken. remove it. */
2912 log_warn(LD_BUG, "unhandled error on write for "
2913 "conn (type %d, fd %d); removing",
2914 conn->type, conn->s);
2915 tor_fragile_assert();
2916 /* do a close-immediate here, so we don't try to flush */
2917 connection_close_immediate(conn);
2919 return;
2921 if (extra) {
2922 conn->outbuf_flushlen += extra;
2923 connection_start_writing(conn);
2928 /** Return a connection with given type, address, port, and purpose;
2929 * or NULL if no such connection exists. */
2930 connection_t *
2931 connection_get_by_type_addr_port_purpose(int type,
2932 const tor_addr_t *addr, uint16_t port,
2933 int purpose)
2935 smartlist_t *conns = get_connection_array();
2936 SMARTLIST_FOREACH(conns, connection_t *, conn,
2938 if (conn->type == type &&
2939 tor_addr_eq(&conn->addr, addr) &&
2940 conn->port == port &&
2941 conn->purpose == purpose &&
2942 !conn->marked_for_close)
2943 return conn;
2945 return NULL;
2948 /** Return the stream with id <b>id</b> if it is not already marked for
2949 * close.
2951 connection_t *
2952 connection_get_by_global_id(uint64_t id)
2954 smartlist_t *conns = get_connection_array();
2955 SMARTLIST_FOREACH(conns, connection_t *, conn,
2957 if (conn->global_identifier == id)
2958 return conn;
2960 return NULL;
2963 /** Return a connection of type <b>type</b> that is not marked for close.
2965 connection_t *
2966 connection_get_by_type(int type)
2968 smartlist_t *conns = get_connection_array();
2969 SMARTLIST_FOREACH(conns, connection_t *, conn,
2971 if (conn->type == type && !conn->marked_for_close)
2972 return conn;
2974 return NULL;
2977 /** Return a connection of type <b>type</b> that is in state <b>state</b>,
2978 * and that is not marked for close.
2980 connection_t *
2981 connection_get_by_type_state(int type, int state)
2983 smartlist_t *conns = get_connection_array();
2984 SMARTLIST_FOREACH(conns, connection_t *, conn,
2986 if (conn->type == type && conn->state == state && !conn->marked_for_close)
2987 return conn;
2989 return NULL;
2992 /** Return a connection of type <b>type</b> that has rendquery equal
2993 * to <b>rendquery</b>, and that is not marked for close. If state
2994 * is non-zero, conn must be of that state too.
2996 connection_t *
2997 connection_get_by_type_state_rendquery(int type, int state,
2998 const char *rendquery)
3000 smartlist_t *conns = get_connection_array();
3002 tor_assert(type == CONN_TYPE_DIR ||
3003 type == CONN_TYPE_AP || type == CONN_TYPE_EXIT);
3004 tor_assert(rendquery);
3006 SMARTLIST_FOREACH(conns, connection_t *, conn,
3008 if (conn->type == type &&
3009 !conn->marked_for_close &&
3010 (!state || state == conn->state)) {
3011 if (type == CONN_TYPE_DIR &&
3012 TO_DIR_CONN(conn)->rend_data &&
3013 !rend_cmp_service_ids(rendquery,
3014 TO_DIR_CONN(conn)->rend_data->onion_address))
3015 return conn;
3016 else if (CONN_IS_EDGE(conn) &&
3017 TO_EDGE_CONN(conn)->rend_data &&
3018 !rend_cmp_service_ids(rendquery,
3019 TO_EDGE_CONN(conn)->rend_data->onion_address))
3020 return conn;
3023 return NULL;
3026 /** Return an open, non-marked connection of a given type and purpose, or NULL
3027 * if no such connection exists. */
3028 connection_t *
3029 connection_get_by_type_purpose(int type, int purpose)
3031 smartlist_t *conns = get_connection_array();
3032 SMARTLIST_FOREACH(conns, connection_t *, conn,
3034 if (conn->type == type &&
3035 !conn->marked_for_close &&
3036 (purpose == conn->purpose))
3037 return conn;
3039 return NULL;
3042 /** Return 1 if <b>conn</b> is a listener conn, else return 0. */
3044 connection_is_listener(connection_t *conn)
3046 if (conn->type == CONN_TYPE_OR_LISTENER ||
3047 conn->type == CONN_TYPE_AP_LISTENER ||
3048 conn->type == CONN_TYPE_AP_TRANS_LISTENER ||
3049 conn->type == CONN_TYPE_AP_DNS_LISTENER ||
3050 conn->type == CONN_TYPE_AP_NATD_LISTENER ||
3051 conn->type == CONN_TYPE_DIR_LISTENER ||
3052 conn->type == CONN_TYPE_CONTROL_LISTENER)
3053 return 1;
3054 return 0;
3057 /** Return 1 if <b>conn</b> is in state "open" and is not marked
3058 * for close, else return 0.
3061 connection_state_is_open(connection_t *conn)
3063 tor_assert(conn);
3065 if (conn->marked_for_close)
3066 return 0;
3068 if ((conn->type == CONN_TYPE_OR && conn->state == OR_CONN_STATE_OPEN) ||
3069 (conn->type == CONN_TYPE_AP && conn->state == AP_CONN_STATE_OPEN) ||
3070 (conn->type == CONN_TYPE_EXIT && conn->state == EXIT_CONN_STATE_OPEN) ||
3071 (conn->type == CONN_TYPE_CONTROL &&
3072 conn->state == CONTROL_CONN_STATE_OPEN))
3073 return 1;
3075 return 0;
3078 /** Return 1 if conn is in 'connecting' state, else return 0. */
3080 connection_state_is_connecting(connection_t *conn)
3082 tor_assert(conn);
3084 if (conn->marked_for_close)
3085 return 0;
3086 switch (conn->type)
3088 case CONN_TYPE_OR:
3089 return conn->state == OR_CONN_STATE_CONNECTING;
3090 case CONN_TYPE_EXIT:
3091 return conn->state == EXIT_CONN_STATE_CONNECTING;
3092 case CONN_TYPE_DIR:
3093 return conn->state == DIR_CONN_STATE_CONNECTING;
3096 return 0;
3099 /** Allocates a base64'ed authenticator for use in http or https
3100 * auth, based on the input string <b>authenticator</b>. Returns it
3101 * if success, else returns NULL. */
3102 char *
3103 alloc_http_authenticator(const char *authenticator)
3105 /* an authenticator in Basic authentication
3106 * is just the string "username:password" */
3107 const size_t authenticator_length = strlen(authenticator);
3108 /* The base64_encode function needs a minimum buffer length
3109 * of 66 bytes. */
3110 const size_t base64_authenticator_length = (authenticator_length/48+1)*66;
3111 char *base64_authenticator = tor_malloc(base64_authenticator_length);
3112 if (base64_encode(base64_authenticator, base64_authenticator_length,
3113 authenticator, authenticator_length) < 0) {
3114 tor_free(base64_authenticator); /* free and set to null */
3115 } else {
3116 /* remove extra \n at end of encoding */
3117 base64_authenticator[strlen(base64_authenticator) - 1] = 0;
3119 return base64_authenticator;
3122 /** Given a socket handle, check whether the local address (sockname) of the
3123 * socket is one that we've connected from before. If so, double-check
3124 * whether our address has changed and we need to generate keys. If we do,
3125 * call init_keys().
3127 static void
3128 client_check_address_changed(int sock)
3130 uint32_t iface_ip, ip_out; /* host order */
3131 struct sockaddr_in out_addr;
3132 socklen_t out_addr_len = (socklen_t) sizeof(out_addr);
3133 uint32_t *ip; /* host order */
3135 if (!last_interface_ip)
3136 get_interface_address(LOG_INFO, &last_interface_ip);
3137 if (!outgoing_addrs)
3138 outgoing_addrs = smartlist_create();
3140 if (getsockname(sock, (struct sockaddr*)&out_addr, &out_addr_len)<0) {
3141 int e = tor_socket_errno(sock);
3142 log_warn(LD_NET, "getsockname() to check for address change failed: %s",
3143 tor_socket_strerror(e));
3144 return;
3147 /* If we've used this address previously, we're okay. */
3148 ip_out = ntohl(out_addr.sin_addr.s_addr);
3149 SMARTLIST_FOREACH(outgoing_addrs, uint32_t*, ip_ptr,
3150 if (*ip_ptr == ip_out) return;
3153 /* Uh-oh. We haven't connected from this address before. Has the interface
3154 * address changed? */
3155 if (get_interface_address(LOG_INFO, &iface_ip)<0)
3156 return;
3157 ip = tor_malloc(sizeof(uint32_t));
3158 *ip = ip_out;
3160 if (iface_ip == last_interface_ip) {
3161 /* Nope, it hasn't changed. Add this address to the list. */
3162 smartlist_add(outgoing_addrs, ip);
3163 } else {
3164 /* The interface changed. We're a client, so we need to regenerate our
3165 * keys. First, reset the state. */
3166 log(LOG_NOTICE, LD_NET, "Our IP address has changed. Rotating keys...");
3167 last_interface_ip = iface_ip;
3168 SMARTLIST_FOREACH(outgoing_addrs, void*, ip_ptr, tor_free(ip_ptr));
3169 smartlist_clear(outgoing_addrs);
3170 smartlist_add(outgoing_addrs, ip);
3171 /* Okay, now change our keys. */
3172 ip_address_changed(1);
3176 /** Some systems have limited system buffers for recv and xmit on
3177 * sockets allocated in a virtual server or similar environment. For a Tor
3178 * server this can produce the "Error creating network socket: No buffer
3179 * space available" error once all available TCP buffer space is consumed.
3180 * This method will attempt to constrain the buffers allocated for the socket
3181 * to the desired size to stay below system TCP buffer limits.
3183 static void
3184 set_constrained_socket_buffers(int sock, int size)
3186 void *sz = (void*)&size;
3187 socklen_t sz_sz = (socklen_t) sizeof(size);
3188 if (setsockopt(sock, SOL_SOCKET, SO_SNDBUF, sz, sz_sz) < 0) {
3189 int e = tor_socket_errno(sock);
3190 log_warn(LD_NET, "setsockopt() to constrain send "
3191 "buffer to %d bytes failed: %s", size, tor_socket_strerror(e));
3193 if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF, sz, sz_sz) < 0) {
3194 int e = tor_socket_errno(sock);
3195 log_warn(LD_NET, "setsockopt() to constrain recv "
3196 "buffer to %d bytes failed: %s", size, tor_socket_strerror(e));
3200 /** Process new bytes that have arrived on conn-\>inbuf.
3202 * This function just passes conn to the connection-specific
3203 * connection_*_process_inbuf() function. It also passes in
3204 * package_partial if wanted.
3206 static int
3207 connection_process_inbuf(connection_t *conn, int package_partial)
3209 tor_assert(conn);
3211 switch (conn->type) {
3212 case CONN_TYPE_OR:
3213 return connection_or_process_inbuf(TO_OR_CONN(conn));
3214 case CONN_TYPE_EXIT:
3215 case CONN_TYPE_AP:
3216 return connection_edge_process_inbuf(TO_EDGE_CONN(conn),
3217 package_partial);
3218 case CONN_TYPE_DIR:
3219 return connection_dir_process_inbuf(TO_DIR_CONN(conn));
3220 case CONN_TYPE_CPUWORKER:
3221 return connection_cpu_process_inbuf(conn);
3222 case CONN_TYPE_CONTROL:
3223 return connection_control_process_inbuf(TO_CONTROL_CONN(conn));
3224 default:
3225 log_err(LD_BUG,"got unexpected conn type %d.", conn->type);
3226 tor_fragile_assert();
3227 return -1;
3231 /** Called whenever we've written data on a connection. */
3232 static int
3233 connection_flushed_some(connection_t *conn)
3235 int r = 0;
3236 tor_assert(!conn->in_flushed_some);
3237 conn->in_flushed_some = 1;
3238 if (conn->type == CONN_TYPE_DIR &&
3239 conn->state == DIR_CONN_STATE_SERVER_WRITING) {
3240 r = connection_dirserv_flushed_some(TO_DIR_CONN(conn));
3241 } else if (conn->type == CONN_TYPE_OR) {
3242 r = connection_or_flushed_some(TO_OR_CONN(conn));
3244 conn->in_flushed_some = 0;
3245 return r;
3248 /** We just finished flushing bytes from conn-\>outbuf, and there
3249 * are no more bytes remaining.
3251 * This function just passes conn to the connection-specific
3252 * connection_*_finished_flushing() function.
3254 static int
3255 connection_finished_flushing(connection_t *conn)
3257 tor_assert(conn);
3259 /* If the connection is closed, don't try to do anything more here. */
3260 if (CONN_IS_CLOSED(conn))
3261 return 0;
3263 // log_fn(LOG_DEBUG,"entered. Socket %u.", conn->s);
3265 switch (conn->type) {
3266 case CONN_TYPE_OR:
3267 return connection_or_finished_flushing(TO_OR_CONN(conn));
3268 case CONN_TYPE_AP:
3269 case CONN_TYPE_EXIT:
3270 return connection_edge_finished_flushing(TO_EDGE_CONN(conn));
3271 case CONN_TYPE_DIR:
3272 return connection_dir_finished_flushing(TO_DIR_CONN(conn));
3273 case CONN_TYPE_CPUWORKER:
3274 return connection_cpu_finished_flushing(conn);
3275 case CONN_TYPE_CONTROL:
3276 return connection_control_finished_flushing(TO_CONTROL_CONN(conn));
3277 default:
3278 log_err(LD_BUG,"got unexpected conn type %d.", conn->type);
3279 tor_fragile_assert();
3280 return -1;
3284 /** Called when our attempt to connect() to another server has just
3285 * succeeded.
3287 * This function just passes conn to the connection-specific
3288 * connection_*_finished_connecting() function.
3290 static int
3291 connection_finished_connecting(connection_t *conn)
3293 tor_assert(conn);
3294 switch (conn->type)
3296 case CONN_TYPE_OR:
3297 return connection_or_finished_connecting(TO_OR_CONN(conn));
3298 case CONN_TYPE_EXIT:
3299 return connection_edge_finished_connecting(TO_EDGE_CONN(conn));
3300 case CONN_TYPE_DIR:
3301 return connection_dir_finished_connecting(TO_DIR_CONN(conn));
3302 default:
3303 log_err(LD_BUG,"got unexpected conn type %d.", conn->type);
3304 tor_fragile_assert();
3305 return -1;
3309 /** Callback: invoked when a connection reaches an EOF event. */
3310 static int
3311 connection_reached_eof(connection_t *conn)
3313 switch (conn->type) {
3314 case CONN_TYPE_OR:
3315 return connection_or_reached_eof(TO_OR_CONN(conn));
3316 case CONN_TYPE_AP:
3317 case CONN_TYPE_EXIT:
3318 return connection_edge_reached_eof(TO_EDGE_CONN(conn));
3319 case CONN_TYPE_DIR:
3320 return connection_dir_reached_eof(TO_DIR_CONN(conn));
3321 case CONN_TYPE_CPUWORKER:
3322 return connection_cpu_reached_eof(conn);
3323 case CONN_TYPE_CONTROL:
3324 return connection_control_reached_eof(TO_CONTROL_CONN(conn));
3325 default:
3326 log_err(LD_BUG,"got unexpected conn type %d.", conn->type);
3327 tor_fragile_assert();
3328 return -1;
3332 /** Log how many bytes are used by buffers of different kinds and sizes. */
3333 void
3334 connection_dump_buffer_mem_stats(int severity)
3336 uint64_t used_by_type[_CONN_TYPE_MAX+1];
3337 uint64_t alloc_by_type[_CONN_TYPE_MAX+1];
3338 int n_conns_by_type[_CONN_TYPE_MAX+1];
3339 uint64_t total_alloc = 0;
3340 uint64_t total_used = 0;
3341 int i;
3342 smartlist_t *conns = get_connection_array();
3344 memset(used_by_type, 0, sizeof(used_by_type));
3345 memset(alloc_by_type, 0, sizeof(alloc_by_type));
3346 memset(n_conns_by_type, 0, sizeof(n_conns_by_type));
3348 SMARTLIST_FOREACH(conns, connection_t *, c,
3350 int tp = c->type;
3351 ++n_conns_by_type[tp];
3352 if (c->inbuf) {
3353 used_by_type[tp] += buf_datalen(c->inbuf);
3354 alloc_by_type[tp] += buf_allocation(c->inbuf);
3356 if (c->outbuf) {
3357 used_by_type[tp] += buf_datalen(c->outbuf);
3358 alloc_by_type[tp] += buf_allocation(c->outbuf);
3361 for (i=0; i <= _CONN_TYPE_MAX; ++i) {
3362 total_used += used_by_type[i];
3363 total_alloc += alloc_by_type[i];
3366 log(severity, LD_GENERAL,
3367 "In buffers for %d connections: "U64_FORMAT" used/"U64_FORMAT" allocated",
3368 smartlist_len(conns),
3369 U64_PRINTF_ARG(total_used), U64_PRINTF_ARG(total_alloc));
3370 for (i=_CONN_TYPE_MIN; i <= _CONN_TYPE_MAX; ++i) {
3371 if (!n_conns_by_type[i])
3372 continue;
3373 log(severity, LD_GENERAL,
3374 " For %d %s connections: "U64_FORMAT" used/"U64_FORMAT" allocated",
3375 n_conns_by_type[i], conn_type_to_string(i),
3376 U64_PRINTF_ARG(used_by_type[i]), U64_PRINTF_ARG(alloc_by_type[i]));
3380 /** Verify that connection <b>conn</b> has all of its invariants
3381 * correct. Trigger an assert if anything is invalid.
3383 void
3384 assert_connection_ok(connection_t *conn, time_t now)
3386 (void) now; /* XXXX unused. */
3387 tor_assert(conn);
3388 tor_assert(conn->type >= _CONN_TYPE_MIN);
3389 tor_assert(conn->type <= _CONN_TYPE_MAX);
3390 switch (conn->type) {
3391 case CONN_TYPE_OR:
3392 tor_assert(conn->magic == OR_CONNECTION_MAGIC);
3393 break;
3394 case CONN_TYPE_AP:
3395 case CONN_TYPE_EXIT:
3396 tor_assert(conn->magic == EDGE_CONNECTION_MAGIC);
3397 break;
3398 case CONN_TYPE_DIR:
3399 tor_assert(conn->magic == DIR_CONNECTION_MAGIC);
3400 break;
3401 case CONN_TYPE_CONTROL:
3402 tor_assert(conn->magic == CONTROL_CONNECTION_MAGIC);
3403 break;
3404 default:
3405 tor_assert(conn->magic == BASE_CONNECTION_MAGIC);
3406 break;
3409 if (conn->linked_conn) {
3410 tor_assert(conn->linked_conn->linked_conn == conn);
3411 tor_assert(conn->linked);
3413 if (conn->linked)
3414 tor_assert(conn->s < 0);
3416 if (conn->outbuf_flushlen > 0) {
3417 tor_assert(connection_is_writing(conn) || conn->write_blocked_on_bw ||
3418 (CONN_IS_EDGE(conn) && TO_EDGE_CONN(conn)->edge_blocked_on_circ));
3421 if (conn->hold_open_until_flushed)
3422 tor_assert(conn->marked_for_close);
3424 /* XXXX check: read_blocked_on_bw, write_blocked_on_bw, s, conn_array_index,
3425 * marked_for_close. */
3427 /* buffers */
3428 if (!connection_is_listener(conn)) {
3429 assert_buf_ok(conn->inbuf);
3430 assert_buf_ok(conn->outbuf);
3433 if (conn->type == CONN_TYPE_OR) {
3434 or_connection_t *or_conn = TO_OR_CONN(conn);
3435 if (conn->state == OR_CONN_STATE_OPEN) {
3436 /* tor_assert(conn->bandwidth > 0); */
3437 /* the above isn't necessarily true: if we just did a TLS
3438 * handshake but we didn't recognize the other peer, or it
3439 * gave a bad cert/etc, then we won't have assigned bandwidth,
3440 * yet it will be open. -RD
3442 // tor_assert(conn->read_bucket >= 0);
3444 // tor_assert(conn->addr && conn->port);
3445 tor_assert(conn->address);
3446 if (conn->state > OR_CONN_STATE_PROXY_HANDSHAKING)
3447 tor_assert(or_conn->tls);
3450 if (CONN_IS_EDGE(conn)) {
3451 edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
3452 if (edge_conn->chosen_exit_optional || edge_conn->chosen_exit_retries) {
3453 tor_assert(conn->type == CONN_TYPE_AP);
3454 tor_assert(edge_conn->chosen_exit_name);
3457 /* XXX unchecked: package window, deliver window. */
3458 if (conn->type == CONN_TYPE_AP) {
3460 tor_assert(edge_conn->socks_request);
3461 if (conn->state == AP_CONN_STATE_OPEN) {
3462 tor_assert(edge_conn->socks_request->has_finished);
3463 if (!conn->marked_for_close) {
3464 tor_assert(edge_conn->cpath_layer);
3465 assert_cpath_layer_ok(edge_conn->cpath_layer);
3469 if (conn->type == CONN_TYPE_EXIT) {
3470 tor_assert(conn->purpose == EXIT_PURPOSE_CONNECT ||
3471 conn->purpose == EXIT_PURPOSE_RESOLVE);
3473 } else if (conn->type == CONN_TYPE_DIR) {
3474 } else {
3475 /* Purpose is only used for dir and exit types currently */
3476 tor_assert(!conn->purpose);
3479 switch (conn->type)
3481 case CONN_TYPE_OR_LISTENER:
3482 case CONN_TYPE_AP_LISTENER:
3483 case CONN_TYPE_AP_TRANS_LISTENER:
3484 case CONN_TYPE_AP_NATD_LISTENER:
3485 case CONN_TYPE_DIR_LISTENER:
3486 case CONN_TYPE_CONTROL_LISTENER:
3487 case CONN_TYPE_AP_DNS_LISTENER:
3488 tor_assert(conn->state == LISTENER_STATE_READY);
3489 break;
3490 case CONN_TYPE_OR:
3491 tor_assert(conn->state >= _OR_CONN_STATE_MIN);
3492 tor_assert(conn->state <= _OR_CONN_STATE_MAX);
3493 tor_assert(TO_OR_CONN(conn)->n_circuits >= 0);
3494 break;
3495 case CONN_TYPE_EXIT:
3496 tor_assert(conn->state >= _EXIT_CONN_STATE_MIN);
3497 tor_assert(conn->state <= _EXIT_CONN_STATE_MAX);
3498 tor_assert(conn->purpose >= _EXIT_PURPOSE_MIN);
3499 tor_assert(conn->purpose <= _EXIT_PURPOSE_MAX);
3500 break;
3501 case CONN_TYPE_AP:
3502 tor_assert(conn->state >= _AP_CONN_STATE_MIN);
3503 tor_assert(conn->state <= _AP_CONN_STATE_MAX);
3504 tor_assert(TO_EDGE_CONN(conn)->socks_request);
3505 break;
3506 case CONN_TYPE_DIR:
3507 tor_assert(conn->state >= _DIR_CONN_STATE_MIN);
3508 tor_assert(conn->state <= _DIR_CONN_STATE_MAX);
3509 tor_assert(conn->purpose >= _DIR_PURPOSE_MIN);
3510 tor_assert(conn->purpose <= _DIR_PURPOSE_MAX);
3511 break;
3512 case CONN_TYPE_CPUWORKER:
3513 tor_assert(conn->state >= _CPUWORKER_STATE_MIN);
3514 tor_assert(conn->state <= _CPUWORKER_STATE_MAX);
3515 break;
3516 case CONN_TYPE_CONTROL:
3517 tor_assert(conn->state >= _CONTROL_CONN_STATE_MIN);
3518 tor_assert(conn->state <= _CONTROL_CONN_STATE_MAX);
3519 break;
3520 default:
3521 tor_assert(0);