Create circuitbuild.h
[tor/rransom.git] / src / or / connection.c
blobc910c7c5d10b30577dd61675d44b727e729d02ff
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 "dnsserv.h"
17 #include "geoip.h"
18 #include "rendclient.h"
19 #include "rendcommon.h"
20 #include "router.h"
22 static connection_t *connection_create_listener(
23 struct sockaddr *listensockaddr,
24 socklen_t listensocklen, int type,
25 char* address);
26 static void connection_init(time_t now, connection_t *conn, int type,
27 int socket_family);
28 static int connection_init_accepted_conn(connection_t *conn,
29 uint8_t listener_type);
30 static int connection_handle_listener_read(connection_t *conn, int new_type);
31 static int connection_bucket_should_increase(int bucket,
32 or_connection_t *conn);
33 static int connection_finished_flushing(connection_t *conn);
34 static int connection_flushed_some(connection_t *conn);
35 static int connection_finished_connecting(connection_t *conn);
36 static int connection_reached_eof(connection_t *conn);
37 static int connection_read_to_buf(connection_t *conn, int *max_to_read,
38 int *socket_error);
39 static int connection_process_inbuf(connection_t *conn, int package_partial);
40 static void client_check_address_changed(int sock);
41 static void set_constrained_socket_buffers(int sock, int size);
43 static const char *connection_proxy_state_to_string(int state);
44 static int connection_read_https_proxy_response(connection_t *conn);
45 static void connection_send_socks5_connect(connection_t *conn);
47 /** The last IPv4 address that our network interface seemed to have been
48 * binding to, in host order. We use this to detect when our IP changes. */
49 static uint32_t last_interface_ip = 0;
50 /** A list of uint32_ts for addresses we've used in outgoing connections.
51 * Used to detect IP address changes. */
52 static smartlist_t *outgoing_addrs = NULL;
54 /**************************************************************/
56 /**
57 * Return the human-readable name for the connection type <b>type</b>
59 const char *
60 conn_type_to_string(int type)
62 static char buf[64];
63 switch (type) {
64 case CONN_TYPE_OR_LISTENER: return "OR listener";
65 case CONN_TYPE_OR: return "OR";
66 case CONN_TYPE_EXIT: return "Exit";
67 case CONN_TYPE_AP_LISTENER: return "Socks listener";
68 case CONN_TYPE_AP_TRANS_LISTENER:
69 return "Transparent pf/netfilter listener";
70 case CONN_TYPE_AP_NATD_LISTENER: return "Transparent natd listener";
71 case CONN_TYPE_AP_DNS_LISTENER: return "DNS listener";
72 case CONN_TYPE_AP: return "Socks";
73 case CONN_TYPE_DIR_LISTENER: return "Directory listener";
74 case CONN_TYPE_DIR: return "Directory";
75 case CONN_TYPE_CPUWORKER: return "CPU worker";
76 case CONN_TYPE_CONTROL_LISTENER: return "Control listener";
77 case CONN_TYPE_CONTROL: return "Control";
78 default:
79 log_warn(LD_BUG, "unknown connection type %d", type);
80 tor_snprintf(buf, sizeof(buf), "unknown [%d]", type);
81 return buf;
85 /**
86 * Return the human-readable name for the connection state <b>state</b>
87 * for the connection type <b>type</b>
89 const char *
90 conn_state_to_string(int type, int state)
92 static char buf[96];
93 switch (type) {
94 case CONN_TYPE_OR_LISTENER:
95 case CONN_TYPE_AP_LISTENER:
96 case CONN_TYPE_AP_TRANS_LISTENER:
97 case CONN_TYPE_AP_NATD_LISTENER:
98 case CONN_TYPE_AP_DNS_LISTENER:
99 case CONN_TYPE_DIR_LISTENER:
100 case CONN_TYPE_CONTROL_LISTENER:
101 if (state == LISTENER_STATE_READY)
102 return "ready";
103 break;
104 case CONN_TYPE_OR:
105 switch (state) {
106 case OR_CONN_STATE_CONNECTING: return "connect()ing";
107 case OR_CONN_STATE_PROXY_HANDSHAKING: return "handshaking (proxy)";
108 case OR_CONN_STATE_TLS_HANDSHAKING: return "handshaking (TLS)";
109 case OR_CONN_STATE_TLS_CLIENT_RENEGOTIATING:
110 return "renegotiating (TLS)";
111 case OR_CONN_STATE_TLS_SERVER_RENEGOTIATING:
112 return "waiting for renegotiation (TLS)";
113 case OR_CONN_STATE_OR_HANDSHAKING: return "handshaking (Tor)";
114 case OR_CONN_STATE_OPEN: return "open";
116 break;
117 case CONN_TYPE_EXIT:
118 switch (state) {
119 case EXIT_CONN_STATE_RESOLVING: return "waiting for dest info";
120 case EXIT_CONN_STATE_CONNECTING: return "connecting";
121 case EXIT_CONN_STATE_OPEN: return "open";
122 case EXIT_CONN_STATE_RESOLVEFAILED: return "resolve failed";
124 break;
125 case CONN_TYPE_AP:
126 switch (state) {
127 case AP_CONN_STATE_SOCKS_WAIT: return "waiting for socks info";
128 case AP_CONN_STATE_NATD_WAIT: return "waiting for natd dest info";
129 case AP_CONN_STATE_RENDDESC_WAIT: return "waiting for rendezvous desc";
130 case AP_CONN_STATE_CONTROLLER_WAIT: return "waiting for controller";
131 case AP_CONN_STATE_CIRCUIT_WAIT: return "waiting for circuit";
132 case AP_CONN_STATE_CONNECT_WAIT: return "waiting for connect response";
133 case AP_CONN_STATE_RESOLVE_WAIT: return "waiting for resolve response";
134 case AP_CONN_STATE_OPEN: return "open";
136 break;
137 case CONN_TYPE_DIR:
138 switch (state) {
139 case DIR_CONN_STATE_CONNECTING: return "connecting";
140 case DIR_CONN_STATE_CLIENT_SENDING: return "client sending";
141 case DIR_CONN_STATE_CLIENT_READING: return "client reading";
142 case DIR_CONN_STATE_CLIENT_FINISHED: return "client finished";
143 case DIR_CONN_STATE_SERVER_COMMAND_WAIT: return "waiting for command";
144 case DIR_CONN_STATE_SERVER_WRITING: return "writing";
146 break;
147 case CONN_TYPE_CPUWORKER:
148 switch (state) {
149 case CPUWORKER_STATE_IDLE: return "idle";
150 case CPUWORKER_STATE_BUSY_ONION: return "busy with onion";
152 break;
153 case CONN_TYPE_CONTROL:
154 switch (state) {
155 case CONTROL_CONN_STATE_OPEN: return "open (protocol v1)";
156 case CONTROL_CONN_STATE_NEEDAUTH:
157 return "waiting for authentication (protocol v1)";
159 break;
162 log_warn(LD_BUG, "unknown connection state %d (type %d)", state, type);
163 tor_snprintf(buf, sizeof(buf),
164 "unknown state [%d] on unknown [%s] connection",
165 state, conn_type_to_string(type));
166 return buf;
169 /** Allocate and return a new dir_connection_t, initialized as by
170 * connection_init(). */
171 dir_connection_t *
172 dir_connection_new(int socket_family)
174 dir_connection_t *dir_conn = tor_malloc_zero(sizeof(dir_connection_t));
175 connection_init(time(NULL), TO_CONN(dir_conn), CONN_TYPE_DIR, socket_family);
176 return dir_conn;
179 /** Allocate and return a new or_connection_t, initialized as by
180 * connection_init(). */
181 or_connection_t *
182 or_connection_new(int socket_family)
184 or_connection_t *or_conn = tor_malloc_zero(sizeof(or_connection_t));
185 time_t now = time(NULL);
186 connection_init(now, TO_CONN(or_conn), CONN_TYPE_OR, socket_family);
188 or_conn->timestamp_last_added_nonpadding = time(NULL);
189 or_conn->next_circ_id = crypto_rand_int(1<<15);
191 or_conn->active_circuit_pqueue = smartlist_create();
192 or_conn->active_circuit_pqueue_last_recalibrated = cell_ewma_get_tick();
194 return or_conn;
197 /** Allocate and return a new edge_connection_t, initialized as by
198 * connection_init(). */
199 edge_connection_t *
200 edge_connection_new(int type, int socket_family)
202 edge_connection_t *edge_conn = tor_malloc_zero(sizeof(edge_connection_t));
203 tor_assert(type == CONN_TYPE_EXIT || type == CONN_TYPE_AP);
204 connection_init(time(NULL), TO_CONN(edge_conn), type, socket_family);
205 if (type == CONN_TYPE_AP)
206 edge_conn->socks_request = tor_malloc_zero(sizeof(socks_request_t));
207 return edge_conn;
210 /** Allocate and return a new control_connection_t, initialized as by
211 * connection_init(). */
212 control_connection_t *
213 control_connection_new(int socket_family)
215 control_connection_t *control_conn =
216 tor_malloc_zero(sizeof(control_connection_t));
217 connection_init(time(NULL),
218 TO_CONN(control_conn), CONN_TYPE_CONTROL, socket_family);
219 log_notice(LD_CONTROL, "New control connection opened.");
220 return control_conn;
223 /** Allocate, initialize, and return a new connection_t subtype of <b>type</b>
224 * to make or receive connections of address family <b>socket_family</b>. The
225 * type should be one of the CONN_TYPE_* constants. */
226 connection_t *
227 connection_new(int type, int socket_family)
229 switch (type) {
230 case CONN_TYPE_OR:
231 return TO_CONN(or_connection_new(socket_family));
233 case CONN_TYPE_EXIT:
234 case CONN_TYPE_AP:
235 return TO_CONN(edge_connection_new(type, socket_family));
237 case CONN_TYPE_DIR:
238 return TO_CONN(dir_connection_new(socket_family));
240 case CONN_TYPE_CONTROL:
241 return TO_CONN(control_connection_new(socket_family));
243 default: {
244 connection_t *conn = tor_malloc_zero(sizeof(connection_t));
245 connection_init(time(NULL), conn, type, socket_family);
246 return conn;
251 /** Initializes conn. (you must call connection_add() to link it into the main
252 * array).
254 * Set conn-\>type to <b>type</b>. Set conn-\>s and conn-\>conn_array_index to
255 * -1 to signify they are not yet assigned.
257 * If conn is not a listener type, allocate buffers for it. If it's
258 * an AP type, allocate space to store the socks_request.
260 * Assign a pseudorandom next_circ_id between 0 and 2**15.
262 * Initialize conn's timestamps to now.
264 static void
265 connection_init(time_t now, connection_t *conn, int type, int socket_family)
267 static uint64_t n_connections_allocated = 1;
269 switch (type) {
270 case CONN_TYPE_OR:
271 conn->magic = OR_CONNECTION_MAGIC;
272 break;
273 case CONN_TYPE_EXIT:
274 case CONN_TYPE_AP:
275 conn->magic = EDGE_CONNECTION_MAGIC;
276 break;
277 case CONN_TYPE_DIR:
278 conn->magic = DIR_CONNECTION_MAGIC;
279 break;
280 case CONN_TYPE_CONTROL:
281 conn->magic = CONTROL_CONNECTION_MAGIC;
282 break;
283 default:
284 conn->magic = BASE_CONNECTION_MAGIC;
285 break;
288 conn->s = -1; /* give it a default of 'not used' */
289 conn->conn_array_index = -1; /* also default to 'not used' */
290 conn->global_identifier = n_connections_allocated++;
292 conn->type = type;
293 conn->socket_family = socket_family;
294 if (!connection_is_listener(conn)) { /* listeners never use their buf */
295 conn->inbuf = buf_new();
296 conn->outbuf = buf_new();
299 conn->timestamp_created = now;
300 conn->timestamp_lastread = now;
301 conn->timestamp_lastwritten = now;
304 /** Create a link between <b>conn_a</b> and <b>conn_b</b>. */
305 void
306 connection_link_connections(connection_t *conn_a, connection_t *conn_b)
308 tor_assert(conn_a->s < 0);
309 tor_assert(conn_b->s < 0);
311 conn_a->linked = 1;
312 conn_b->linked = 1;
313 conn_a->linked_conn = conn_b;
314 conn_b->linked_conn = conn_a;
317 /** Deallocate memory used by <b>conn</b>. Deallocate its buffers if
318 * necessary, close its socket if necessary, and mark the directory as dirty
319 * if <b>conn</b> is an OR or OP connection.
321 static void
322 _connection_free(connection_t *conn)
324 void *mem;
325 size_t memlen;
326 if (!conn)
327 return;
329 switch (conn->type) {
330 case CONN_TYPE_OR:
331 tor_assert(conn->magic == OR_CONNECTION_MAGIC);
332 mem = TO_OR_CONN(conn);
333 memlen = sizeof(or_connection_t);
334 break;
335 case CONN_TYPE_AP:
336 case CONN_TYPE_EXIT:
337 tor_assert(conn->magic == EDGE_CONNECTION_MAGIC);
338 mem = TO_EDGE_CONN(conn);
339 memlen = sizeof(edge_connection_t);
340 break;
341 case CONN_TYPE_DIR:
342 tor_assert(conn->magic == DIR_CONNECTION_MAGIC);
343 mem = TO_DIR_CONN(conn);
344 memlen = sizeof(dir_connection_t);
345 break;
346 case CONN_TYPE_CONTROL:
347 tor_assert(conn->magic == CONTROL_CONNECTION_MAGIC);
348 mem = TO_CONTROL_CONN(conn);
349 memlen = sizeof(control_connection_t);
350 break;
351 default:
352 tor_assert(conn->magic == BASE_CONNECTION_MAGIC);
353 mem = conn;
354 memlen = sizeof(connection_t);
355 break;
358 if (conn->linked) {
359 log_info(LD_GENERAL, "Freeing linked %s connection [%s] with %d "
360 "bytes on inbuf, %d on outbuf.",
361 conn_type_to_string(conn->type),
362 conn_state_to_string(conn->type, conn->state),
363 (int)buf_datalen(conn->inbuf), (int)buf_datalen(conn->outbuf));
366 if (!connection_is_listener(conn)) {
367 buf_free(conn->inbuf);
368 buf_free(conn->outbuf);
369 } else {
370 if (conn->socket_family == AF_UNIX) {
371 /* For now only control ports can be Unix domain sockets
372 * and listeners at the same time */
373 tor_assert(conn->type == CONN_TYPE_CONTROL_LISTENER);
375 if (unlink(conn->address) < 0 && errno != ENOENT) {
376 log_warn(LD_NET, "Could not unlink %s: %s", conn->address,
377 strerror(errno));
382 tor_free(conn->address);
384 if (connection_speaks_cells(conn)) {
385 or_connection_t *or_conn = TO_OR_CONN(conn);
386 tor_tls_free(or_conn->tls);
387 or_conn->tls = NULL;
388 or_handshake_state_free(or_conn->handshake_state);
389 or_conn->handshake_state = NULL;
390 smartlist_free(or_conn->active_circuit_pqueue);
391 tor_free(or_conn->nickname);
393 if (CONN_IS_EDGE(conn)) {
394 edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
395 tor_free(edge_conn->chosen_exit_name);
396 if (edge_conn->socks_request) {
397 memset(edge_conn->socks_request, 0xcc, sizeof(socks_request_t));
398 tor_free(edge_conn->socks_request);
401 rend_data_free(edge_conn->rend_data);
403 if (conn->type == CONN_TYPE_CONTROL) {
404 control_connection_t *control_conn = TO_CONTROL_CONN(conn);
405 tor_free(control_conn->incoming_cmd);
408 tor_free(conn->read_event); /* Probably already freed by connection_free. */
409 tor_free(conn->write_event); /* Probably already freed by connection_free. */
411 if (conn->type == CONN_TYPE_DIR) {
412 dir_connection_t *dir_conn = TO_DIR_CONN(conn);
413 tor_free(dir_conn->requested_resource);
415 tor_zlib_free(dir_conn->zlib_state);
416 if (dir_conn->fingerprint_stack) {
417 SMARTLIST_FOREACH(dir_conn->fingerprint_stack, char *, cp, tor_free(cp));
418 smartlist_free(dir_conn->fingerprint_stack);
421 cached_dir_decref(dir_conn->cached_dir);
422 rend_data_free(dir_conn->rend_data);
425 if (conn->s >= 0) {
426 log_debug(LD_NET,"closing fd %d.",conn->s);
427 tor_close_socket(conn->s);
428 conn->s = -1;
431 if (conn->type == CONN_TYPE_OR &&
432 !tor_digest_is_zero(TO_OR_CONN(conn)->identity_digest)) {
433 log_warn(LD_BUG, "called on OR conn with non-zeroed identity_digest");
434 connection_or_remove_from_identity_map(TO_OR_CONN(conn));
437 memset(mem, 0xCC, memlen); /* poison memory */
438 tor_free(mem);
441 /** Make sure <b>conn</b> isn't in any of the global conn lists; then free it.
443 void
444 connection_free(connection_t *conn)
446 if (!conn)
447 return;
448 tor_assert(!connection_is_on_closeable_list(conn));
449 tor_assert(!connection_in_array(conn));
450 if (conn->linked_conn) {
451 log_err(LD_BUG, "Called with conn->linked_conn still set.");
452 tor_fragile_assert();
453 conn->linked_conn->linked_conn = NULL;
454 if (! conn->linked_conn->marked_for_close &&
455 conn->linked_conn->reading_from_linked_conn)
456 connection_start_reading(conn->linked_conn);
457 conn->linked_conn = NULL;
459 if (connection_speaks_cells(conn)) {
460 if (!tor_digest_is_zero(TO_OR_CONN(conn)->identity_digest)) {
461 connection_or_remove_from_identity_map(TO_OR_CONN(conn));
464 if (conn->type == CONN_TYPE_CONTROL) {
465 TO_CONTROL_CONN(conn)->event_mask = 0;
466 control_update_global_event_mask();
468 connection_unregister_events(conn);
469 _connection_free(conn);
472 /** Call _connection_free() on every connection in our array, and release all
473 * storage held by connection.c. This is used by cpuworkers and dnsworkers
474 * when they fork, so they don't keep resources held open (especially
475 * sockets).
477 * Don't do the checks in connection_free(), because they will
478 * fail.
480 void
481 connection_free_all(void)
483 smartlist_t *conns = get_connection_array();
485 /* We don't want to log any messages to controllers. */
486 SMARTLIST_FOREACH(conns, connection_t *, conn,
487 if (conn->type == CONN_TYPE_CONTROL)
488 TO_CONTROL_CONN(conn)->event_mask = 0);
490 control_update_global_event_mask();
492 /* Unlink everything from the identity map. */
493 connection_or_clear_identity_map();
495 SMARTLIST_FOREACH(conns, connection_t *, conn, _connection_free(conn));
497 if (outgoing_addrs) {
498 SMARTLIST_FOREACH(outgoing_addrs, void*, addr, tor_free(addr));
499 smartlist_free(outgoing_addrs);
500 outgoing_addrs = NULL;
504 /** Do any cleanup needed:
505 * - Directory conns that failed to fetch a rendezvous descriptor
506 * need to inform pending rendezvous streams.
507 * - OR conns need to call rep_hist_note_*() to record status.
508 * - AP conns need to send a socks reject if necessary.
509 * - Exit conns need to call connection_dns_remove() if necessary.
510 * - AP and Exit conns need to send an end cell if they can.
511 * - DNS conns need to fail any resolves that are pending on them.
512 * - OR and edge connections need to be unlinked from circuits.
514 void
515 connection_about_to_close_connection(connection_t *conn)
517 circuit_t *circ;
518 dir_connection_t *dir_conn;
519 or_connection_t *or_conn;
520 edge_connection_t *edge_conn;
521 time_t now = time(NULL);
523 tor_assert(conn->marked_for_close);
525 if (CONN_IS_EDGE(conn)) {
526 edge_conn = TO_EDGE_CONN(conn);
527 if (!edge_conn->edge_has_sent_end) {
528 log_warn(LD_BUG, "(Harmless.) Edge connection (marked at %s:%d) "
529 "hasn't sent end yet?",
530 conn->marked_for_close_file, conn->marked_for_close);
531 tor_fragile_assert();
535 switch (conn->type) {
536 case CONN_TYPE_DIR:
537 dir_conn = TO_DIR_CONN(conn);
538 if (conn->state < DIR_CONN_STATE_CLIENT_FINISHED) {
539 /* It's a directory connection and connecting or fetching
540 * failed: forget about this router, and maybe try again. */
541 connection_dir_request_failed(dir_conn);
543 /* If we were trying to fetch a v2 rend desc and did not succeed,
544 * retry as needed. (If a fetch is successful, the connection state
545 * is changed to DIR_PURPOSE_HAS_FETCHED_RENDDESC to mark that
546 * refetching is unnecessary.) */
547 if (conn->purpose == DIR_PURPOSE_FETCH_RENDDESC_V2 &&
548 dir_conn->rend_data &&
549 strlen(dir_conn->rend_data->onion_address) ==
550 REND_SERVICE_ID_LEN_BASE32)
551 rend_client_refetch_v2_renddesc(dir_conn->rend_data);
552 break;
553 case CONN_TYPE_OR:
554 or_conn = TO_OR_CONN(conn);
555 /* Remember why we're closing this connection. */
556 if (conn->state != OR_CONN_STATE_OPEN) {
557 /* Inform any pending (not attached) circs that they should
558 * give up. */
559 circuit_n_conn_done(TO_OR_CONN(conn), 0);
560 /* now mark things down as needed */
561 if (connection_or_nonopen_was_started_here(or_conn)) {
562 or_options_t *options = get_options();
563 rep_hist_note_connect_failed(or_conn->identity_digest, now);
564 entry_guard_register_connect_status(or_conn->identity_digest,0,
565 !options->HttpsProxy, now);
566 if (conn->state >= OR_CONN_STATE_TLS_HANDSHAKING) {
567 int reason = tls_error_to_orconn_end_reason(or_conn->tls_error);
568 control_event_or_conn_status(or_conn, OR_CONN_EVENT_FAILED,
569 reason);
570 if (!authdir_mode_tests_reachability(options))
571 control_event_bootstrap_problem(
572 orconn_end_reason_to_control_string(reason), reason);
575 } else if (conn->hold_open_until_flushed) {
576 /* We only set hold_open_until_flushed when we're intentionally
577 * closing a connection. */
578 rep_hist_note_disconnect(or_conn->identity_digest, now);
579 control_event_or_conn_status(or_conn, OR_CONN_EVENT_CLOSED,
580 tls_error_to_orconn_end_reason(or_conn->tls_error));
581 } else if (!tor_digest_is_zero(or_conn->identity_digest)) {
582 rep_hist_note_connection_died(or_conn->identity_digest, now);
583 control_event_or_conn_status(or_conn, OR_CONN_EVENT_CLOSED,
584 tls_error_to_orconn_end_reason(or_conn->tls_error));
586 /* Now close all the attached circuits on it. */
587 circuit_unlink_all_from_or_conn(TO_OR_CONN(conn),
588 END_CIRC_REASON_OR_CONN_CLOSED);
589 break;
590 case CONN_TYPE_AP:
591 edge_conn = TO_EDGE_CONN(conn);
592 if (edge_conn->socks_request->has_finished == 0) {
593 /* since conn gets removed right after this function finishes,
594 * there's no point trying to send back a reply at this point. */
595 log_warn(LD_BUG,"Closing stream (marked at %s:%d) without sending"
596 " back a socks reply.",
597 conn->marked_for_close_file, conn->marked_for_close);
599 if (!edge_conn->end_reason) {
600 log_warn(LD_BUG,"Closing stream (marked at %s:%d) without having"
601 " set end_reason.",
602 conn->marked_for_close_file, conn->marked_for_close);
604 if (edge_conn->dns_server_request) {
605 log_warn(LD_BUG,"Closing stream (marked at %s:%d) without having"
606 " replied to DNS request.",
607 conn->marked_for_close_file, conn->marked_for_close);
608 dnsserv_reject_request(edge_conn);
610 control_event_stream_bandwidth(edge_conn);
611 control_event_stream_status(edge_conn, STREAM_EVENT_CLOSED,
612 edge_conn->end_reason);
613 circ = circuit_get_by_edge_conn(edge_conn);
614 if (circ)
615 circuit_detach_stream(circ, edge_conn);
616 break;
617 case CONN_TYPE_EXIT:
618 edge_conn = TO_EDGE_CONN(conn);
619 circ = circuit_get_by_edge_conn(edge_conn);
620 if (circ)
621 circuit_detach_stream(circ, edge_conn);
622 if (conn->state == EXIT_CONN_STATE_RESOLVING) {
623 connection_dns_remove(edge_conn);
625 break;
629 /** Return true iff connection_close_immediate() has been called on this
630 * connection. */
631 #define CONN_IS_CLOSED(c) \
632 ((c)->linked ? ((c)->linked_conn_is_closed) : ((c)->s < 0))
634 /** Close the underlying socket for <b>conn</b>, so we don't try to
635 * flush it. Must be used in conjunction with (right before)
636 * connection_mark_for_close().
638 void
639 connection_close_immediate(connection_t *conn)
641 assert_connection_ok(conn,0);
642 if (CONN_IS_CLOSED(conn)) {
643 log_err(LD_BUG,"Attempt to close already-closed connection.");
644 tor_fragile_assert();
645 return;
647 if (conn->outbuf_flushlen) {
648 log_info(LD_NET,"fd %d, type %s, state %s, %d bytes on outbuf.",
649 conn->s, conn_type_to_string(conn->type),
650 conn_state_to_string(conn->type, conn->state),
651 (int)conn->outbuf_flushlen);
654 connection_unregister_events(conn);
656 if (conn->s >= 0)
657 tor_close_socket(conn->s);
658 conn->s = -1;
659 if (conn->linked)
660 conn->linked_conn_is_closed = 1;
661 if (!connection_is_listener(conn)) {
662 buf_clear(conn->outbuf);
663 conn->outbuf_flushlen = 0;
667 /** Mark <b>conn</b> to be closed next time we loop through
668 * conn_close_if_marked() in main.c. */
669 void
670 _connection_mark_for_close(connection_t *conn, int line, const char *file)
672 assert_connection_ok(conn,0);
673 tor_assert(line);
674 tor_assert(line < 1<<16); /* marked_for_close can only fit a uint16_t. */
675 tor_assert(file);
677 if (conn->marked_for_close) {
678 log(LOG_WARN,LD_BUG,"Duplicate call to connection_mark_for_close at %s:%d"
679 " (first at %s:%d)", file, line, conn->marked_for_close_file,
680 conn->marked_for_close);
681 tor_fragile_assert();
682 return;
685 conn->marked_for_close = line;
686 conn->marked_for_close_file = file;
687 add_connection_to_closeable_list(conn);
689 /* in case we're going to be held-open-til-flushed, reset
690 * the number of seconds since last successful write, so
691 * we get our whole 15 seconds */
692 conn->timestamp_lastwritten = time(NULL);
695 /** Find each connection that has hold_open_until_flushed set to
696 * 1 but hasn't written in the past 15 seconds, and set
697 * hold_open_until_flushed to 0. This means it will get cleaned
698 * up in the next loop through close_if_marked() in main.c.
700 void
701 connection_expire_held_open(void)
703 time_t now;
704 smartlist_t *conns = get_connection_array();
706 now = time(NULL);
708 SMARTLIST_FOREACH(conns, connection_t *, conn,
710 /* If we've been holding the connection open, but we haven't written
711 * for 15 seconds...
713 if (conn->hold_open_until_flushed) {
714 tor_assert(conn->marked_for_close);
715 if (now - conn->timestamp_lastwritten >= 15) {
716 int severity;
717 if (conn->type == CONN_TYPE_EXIT ||
718 (conn->type == CONN_TYPE_DIR &&
719 conn->purpose == DIR_PURPOSE_SERVER))
720 severity = LOG_INFO;
721 else
722 severity = LOG_NOTICE;
723 log_fn(severity, LD_NET,
724 "Giving up on marked_for_close conn that's been flushing "
725 "for 15s (fd %d, type %s, state %s).",
726 conn->s, conn_type_to_string(conn->type),
727 conn_state_to_string(conn->type, conn->state));
728 conn->hold_open_until_flushed = 0;
734 /** Create an AF_INET listenaddr struct.
735 * <b>listenaddress</b> provides the host and optionally the port information
736 * for the new structure. If no port is provided in <b>listenaddress</b> then
737 * <b>listenport</b> is used.
739 * If not NULL <b>readable_address</b> will contain a copy of the host part of
740 * <b>listenaddress</b>.
742 * The listenaddr struct has to be freed by the caller.
744 static struct sockaddr_in *
745 create_inet_sockaddr(const char *listenaddress, uint16_t listenport,
746 char **readable_address, socklen_t *socklen_out) {
747 struct sockaddr_in *listenaddr = NULL;
748 uint32_t addr;
749 uint16_t usePort = 0;
751 if (parse_addr_port(LOG_WARN,
752 listenaddress, readable_address, &addr, &usePort)<0) {
753 log_warn(LD_CONFIG,
754 "Error parsing/resolving ListenAddress %s", listenaddress);
755 goto err;
757 if (usePort==0)
758 usePort = listenport;
760 listenaddr = tor_malloc_zero(sizeof(struct sockaddr_in));
761 listenaddr->sin_addr.s_addr = htonl(addr);
762 listenaddr->sin_family = AF_INET;
763 listenaddr->sin_port = htons((uint16_t) usePort);
765 *socklen_out = sizeof(struct sockaddr_in);
767 return listenaddr;
769 err:
770 tor_free(listenaddr);
771 return NULL;
774 #ifdef HAVE_SYS_UN_H
775 /** Create an AF_UNIX listenaddr struct.
776 * <b>listenaddress</b> provides the path to the Unix socket.
778 * Eventually <b>listenaddress</b> will also optionally contain user, group,
779 * and file permissions for the new socket. But not yet. XXX
780 * Also, since we do not create the socket here the information doesn't help
781 * here.
783 * If not NULL <b>readable_address</b> will contain a copy of the path part of
784 * <b>listenaddress</b>.
786 * The listenaddr struct has to be freed by the caller.
788 static struct sockaddr_un *
789 create_unix_sockaddr(const char *listenaddress, char **readable_address,
790 socklen_t *len_out)
792 struct sockaddr_un *sockaddr = NULL;
794 sockaddr = tor_malloc_zero(sizeof(struct sockaddr_un));
795 sockaddr->sun_family = AF_UNIX;
796 strncpy(sockaddr->sun_path, listenaddress, sizeof(sockaddr->sun_path));
798 if (readable_address)
799 *readable_address = tor_strdup(listenaddress);
801 *len_out = sizeof(struct sockaddr_un);
802 return sockaddr;
804 #else
805 static struct sockaddr *
806 create_unix_sockaddr(const char *listenaddress, char **readable_address,
807 socklen_t *len_out)
809 (void)listenaddress;
810 (void)readable_address;
811 log_fn(LOG_ERR, LD_BUG,
812 "Unix domain sockets not supported, yet we tried to create one.");
813 *len_out = 0;
814 tor_assert(0);
816 #endif /* HAVE_SYS_UN_H */
818 /** Warn that an accept or a connect has failed because we're running up
819 * against our ulimit. Rate-limit these warnings so that we don't spam
820 * the log. */
821 static void
822 warn_too_many_conns(void)
824 #define WARN_TOO_MANY_CONNS_INTERVAL (6*60*60)
825 static time_t last_warned = 0;
826 time_t now = time(NULL);
827 int n_conns = get_n_open_sockets();
828 if (last_warned + WARN_TOO_MANY_CONNS_INTERVAL < now) {
829 log_warn(LD_NET,"Failing because we have %d connections already. Please "
830 "raise your ulimit -n.", n_conns);
831 last_warned = now;
832 control_event_general_status(LOG_WARN, "TOO_MANY_CONNECTIONS CURRENT=%d",
833 n_conns);
837 /** Bind a new non-blocking socket listening to the socket described
838 * by <b>listensockaddr</b>.
840 * <b>address</b> is only used for logging purposes and to add the information
841 * to the conn.
843 static connection_t *
844 connection_create_listener(struct sockaddr *listensockaddr, socklen_t socklen,
845 int type, char* address)
847 connection_t *conn;
848 int s; /* the socket we're going to make */
849 uint16_t usePort = 0;
850 int start_reading = 0;
852 if (get_n_open_sockets() >= get_options()->_ConnLimit-1) {
853 warn_too_many_conns();
854 return NULL;
857 if (listensockaddr->sa_family == AF_INET) {
858 int is_tcp = (type != CONN_TYPE_AP_DNS_LISTENER);
859 #ifndef MS_WINDOWS
860 int one=1;
861 #endif
862 if (is_tcp)
863 start_reading = 1;
865 usePort = ntohs( (uint16_t)
866 ((struct sockaddr_in *)listensockaddr)->sin_port);
868 log_notice(LD_NET, "Opening %s on %s:%d",
869 conn_type_to_string(type), address, usePort);
871 s = tor_open_socket(PF_INET,
872 is_tcp ? SOCK_STREAM : SOCK_DGRAM,
873 is_tcp ? IPPROTO_TCP: IPPROTO_UDP);
874 if (s < 0) {
875 log_warn(LD_NET,"Socket creation failed.");
876 goto err;
879 #ifndef MS_WINDOWS
880 /* REUSEADDR on normal places means you can rebind to the port
881 * right after somebody else has let it go. But REUSEADDR on win32
882 * means you can bind to the port _even when somebody else
883 * already has it bound_. So, don't do that on Win32. */
884 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (void*) &one,
885 (socklen_t)sizeof(one));
886 #endif
888 if (bind(s,listensockaddr,socklen) < 0) {
889 const char *helpfulhint = "";
890 int e = tor_socket_errno(s);
891 if (ERRNO_IS_EADDRINUSE(e))
892 helpfulhint = ". Is Tor already running?";
893 log_warn(LD_NET, "Could not bind to %s:%u: %s%s", address, usePort,
894 tor_socket_strerror(e), helpfulhint);
895 tor_close_socket(s);
896 goto err;
899 if (is_tcp) {
900 if (listen(s,SOMAXCONN) < 0) {
901 log_warn(LD_NET, "Could not listen on %s:%u: %s", address, usePort,
902 tor_socket_strerror(tor_socket_errno(s)));
903 tor_close_socket(s);
904 goto err;
907 #ifdef HAVE_SYS_UN_H
908 } else if (listensockaddr->sa_family == AF_UNIX) {
909 start_reading = 1;
911 /* For now only control ports can be Unix domain sockets
912 * and listeners at the same time */
913 tor_assert(type == CONN_TYPE_CONTROL_LISTENER);
915 log_notice(LD_NET, "Opening %s on %s",
916 conn_type_to_string(type), address);
918 if (unlink(address) < 0 && errno != ENOENT) {
919 log_warn(LD_NET, "Could not unlink %s: %s", address,
920 strerror(errno));
921 goto err;
923 s = tor_open_socket(AF_UNIX, SOCK_STREAM, 0);
924 if (s < 0) {
925 log_warn(LD_NET,"Socket creation failed: %s.", strerror(errno));
926 goto err;
929 if (bind(s, listensockaddr, (socklen_t)sizeof(struct sockaddr_un)) == -1) {
930 log_warn(LD_NET,"Bind to %s failed: %s.", address,
931 tor_socket_strerror(tor_socket_errno(s)));
932 goto err;
935 if (listen(s,SOMAXCONN) < 0) {
936 log_warn(LD_NET, "Could not listen on %s: %s", address,
937 tor_socket_strerror(tor_socket_errno(s)));
938 tor_close_socket(s);
939 goto err;
941 #endif /* HAVE_SYS_UN_H */
942 } else {
943 log_err(LD_BUG,"Got unexpected address family %d.",
944 listensockaddr->sa_family);
945 tor_assert(0);
948 set_socket_nonblocking(s);
950 conn = connection_new(type, listensockaddr->sa_family);
951 conn->socket_family = listensockaddr->sa_family;
952 conn->s = s;
953 conn->address = tor_strdup(address);
954 conn->port = usePort;
956 if (connection_add(conn) < 0) { /* no space, forget it */
957 log_warn(LD_NET,"connection_add for listener failed. Giving up.");
958 connection_free(conn);
959 goto err;
962 log_debug(LD_NET,"%s listening on port %u.",
963 conn_type_to_string(type), usePort);
965 conn->state = LISTENER_STATE_READY;
966 if (start_reading) {
967 connection_start_reading(conn);
968 } else {
969 tor_assert(type == CONN_TYPE_AP_DNS_LISTENER);
970 dnsserv_configure_listener(conn);
973 return conn;
975 err:
976 return NULL;
979 /** Do basic sanity checking on a newly received socket. Return 0
980 * if it looks ok, else return -1. */
981 static int
982 check_sockaddr(struct sockaddr *sa, int len, int level)
984 int ok = 1;
986 if (sa->sa_family == AF_INET) {
987 struct sockaddr_in *sin=(struct sockaddr_in*)sa;
988 if (len != sizeof(struct sockaddr_in)) {
989 log_fn(level, LD_NET, "Length of address not as expected: %d vs %d",
990 len,(int)sizeof(struct sockaddr_in));
991 ok = 0;
993 if (sin->sin_addr.s_addr == 0 || sin->sin_port == 0) {
994 log_fn(level, LD_NET,
995 "Address for new connection has address/port equal to zero.");
996 ok = 0;
998 } else if (sa->sa_family == AF_INET6) {
999 struct sockaddr_in6 *sin6=(struct sockaddr_in6*)sa;
1000 if (len != sizeof(struct sockaddr_in6)) {
1001 log_fn(level, LD_NET, "Length of address not as expected: %d vs %d",
1002 len,(int)sizeof(struct sockaddr_in6));
1003 ok = 0;
1005 if (tor_mem_is_zero((void*)sin6->sin6_addr.s6_addr, 16) ||
1006 sin6->sin6_port == 0) {
1007 log_fn(level, LD_NET,
1008 "Address for new connection has address/port equal to zero.");
1009 ok = 0;
1011 } else {
1012 ok = 0;
1014 return ok ? 0 : -1;
1017 /** Check whether the socket family from an accepted socket <b>got</b> is the
1018 * same as the one that <b>listener</b> is waiting for. If it isn't, log
1019 * a useful message and return -1. Else return 0.
1021 * This is annoying, but can apparently happen on some Darwins. */
1022 static int
1023 check_sockaddr_family_match(sa_family_t got, connection_t *listener)
1025 if (got != listener->socket_family) {
1026 log_info(LD_BUG, "A listener connection returned a socket with a "
1027 "mismatched family. %s for addr_family %d gave us a socket "
1028 "with address family %d. Dropping.",
1029 conn_type_to_string(listener->type),
1030 (int)listener->socket_family,
1031 (int)got);
1032 return -1;
1034 return 0;
1037 /** The listener connection <b>conn</b> told poll() it wanted to read.
1038 * Call accept() on conn-\>s, and add the new connection if necessary.
1040 static int
1041 connection_handle_listener_read(connection_t *conn, int new_type)
1043 int news; /* the new socket */
1044 connection_t *newconn;
1045 /* information about the remote peer when connecting to other routers */
1046 char addrbuf[256];
1047 struct sockaddr *remote = (struct sockaddr*)addrbuf;
1048 /* length of the remote address. Must be whatever accept() needs. */
1049 socklen_t remotelen = (socklen_t)sizeof(addrbuf);
1050 or_options_t *options = get_options();
1052 tor_assert((size_t)remotelen >= sizeof(struct sockaddr_in));
1053 memset(addrbuf, 0, sizeof(addrbuf));
1055 news = tor_accept_socket(conn->s,remote,&remotelen);
1056 if (news < 0) { /* accept() error */
1057 int e = tor_socket_errno(conn->s);
1058 if (ERRNO_IS_ACCEPT_EAGAIN(e)) {
1059 return 0; /* he hung up before we could accept(). that's fine. */
1060 } else if (ERRNO_IS_ACCEPT_RESOURCE_LIMIT(e)) {
1061 warn_too_many_conns();
1062 return 0;
1064 /* else there was a real error. */
1065 log_warn(LD_NET,"accept() failed: %s. Closing listener.",
1066 tor_socket_strerror(e));
1067 connection_mark_for_close(conn);
1068 return -1;
1070 log_debug(LD_NET,
1071 "Connection accepted on socket %d (child of fd %d).",
1072 news,conn->s);
1074 set_socket_nonblocking(news);
1076 if (options->ConstrainedSockets)
1077 set_constrained_socket_buffers(news, (int)options->ConstrainedSockSize);
1079 if (check_sockaddr_family_match(remote->sa_family, conn) < 0) {
1080 tor_close_socket(news);
1081 return 0;
1084 if (conn->socket_family == AF_INET || conn->socket_family == AF_INET6) {
1085 tor_addr_t addr;
1086 uint16_t port;
1087 if (check_sockaddr(remote, remotelen, LOG_INFO)<0) {
1088 log_info(LD_NET,
1089 "accept() returned a strange address; trying getsockname().");
1090 remotelen=sizeof(addrbuf);
1091 memset(addrbuf, 0, sizeof(addrbuf));
1092 if (getsockname(news, remote, &remotelen)<0) {
1093 int e = tor_socket_errno(news);
1094 log_warn(LD_NET, "getsockname() for new connection failed: %s",
1095 tor_socket_strerror(e));
1096 } else {
1097 if (check_sockaddr((struct sockaddr*)addrbuf, remotelen,
1098 LOG_WARN) < 0) {
1099 log_warn(LD_NET,"Something's wrong with this conn. Closing it.");
1100 tor_close_socket(news);
1101 return 0;
1106 if (check_sockaddr_family_match(remote->sa_family, conn) < 0) {
1107 tor_close_socket(news);
1108 return 0;
1111 tor_addr_from_sockaddr(&addr, remote, &port);
1113 /* process entrance policies here, before we even create the connection */
1114 if (new_type == CONN_TYPE_AP) {
1115 /* check sockspolicy to see if we should accept it */
1116 if (socks_policy_permits_address(&addr) == 0) {
1117 log_notice(LD_APP,
1118 "Denying socks connection from untrusted address %s.",
1119 fmt_addr(&addr));
1120 tor_close_socket(news);
1121 return 0;
1124 if (new_type == CONN_TYPE_DIR) {
1125 /* check dirpolicy to see if we should accept it */
1126 if (dir_policy_permits_address(&addr) == 0) {
1127 log_notice(LD_DIRSERV,"Denying dir connection from address %s.",
1128 fmt_addr(&addr));
1129 tor_close_socket(news);
1130 return 0;
1134 newconn = connection_new(new_type, conn->socket_family);
1135 newconn->s = news;
1137 /* remember the remote address */
1138 tor_addr_copy(&newconn->addr, &addr);
1139 newconn->port = port;
1140 newconn->address = tor_dup_addr(&addr);
1142 } else if (conn->socket_family == AF_UNIX) {
1143 /* For now only control ports can be Unix domain sockets
1144 * and listeners at the same time */
1145 tor_assert(conn->type == CONN_TYPE_CONTROL_LISTENER);
1147 newconn = connection_new(new_type, conn->socket_family);
1148 newconn->s = news;
1150 /* remember the remote address -- do we have anything sane to put here? */
1151 tor_addr_make_unspec(&newconn->addr);
1152 newconn->port = 1;
1153 newconn->address = tor_strdup(conn->address);
1154 } else {
1155 tor_assert(0);
1158 if (connection_add(newconn) < 0) { /* no space, forget it */
1159 connection_free(newconn);
1160 return 0; /* no need to tear down the parent */
1163 if (connection_init_accepted_conn(newconn, conn->type) < 0) {
1164 connection_mark_for_close(newconn);
1165 return 0;
1167 return 0;
1170 /** Initialize states for newly accepted connection <b>conn</b>.
1171 * If conn is an OR, start the TLS handshake.
1172 * If conn is a transparent AP, get its original destination
1173 * and place it in circuit_wait.
1175 static int
1176 connection_init_accepted_conn(connection_t *conn, uint8_t listener_type)
1178 connection_start_reading(conn);
1180 switch (conn->type) {
1181 case CONN_TYPE_OR:
1182 control_event_or_conn_status(TO_OR_CONN(conn), OR_CONN_EVENT_NEW, 0);
1183 return connection_tls_start_handshake(TO_OR_CONN(conn), 1);
1184 case CONN_TYPE_AP:
1185 switch (listener_type) {
1186 case CONN_TYPE_AP_LISTENER:
1187 conn->state = AP_CONN_STATE_SOCKS_WAIT;
1188 break;
1189 case CONN_TYPE_AP_TRANS_LISTENER:
1190 conn->state = AP_CONN_STATE_CIRCUIT_WAIT;
1191 return connection_ap_process_transparent(TO_EDGE_CONN(conn));
1192 case CONN_TYPE_AP_NATD_LISTENER:
1193 conn->state = AP_CONN_STATE_NATD_WAIT;
1194 break;
1196 break;
1197 case CONN_TYPE_DIR:
1198 conn->purpose = DIR_PURPOSE_SERVER;
1199 conn->state = DIR_CONN_STATE_SERVER_COMMAND_WAIT;
1200 break;
1201 case CONN_TYPE_CONTROL:
1202 conn->state = CONTROL_CONN_STATE_NEEDAUTH;
1203 break;
1205 return 0;
1208 /** Take conn, make a nonblocking socket; try to connect to
1209 * addr:port (they arrive in *host order*). If fail, return -1 and if
1210 * applicable put your best guess about errno into *<b>socket_error</b>.
1211 * Else assign s to conn-\>s: if connected return 1, if EAGAIN return 0.
1213 * address is used to make the logs useful.
1215 * On success, add conn to the list of polled connections.
1218 connection_connect(connection_t *conn, const char *address,
1219 const tor_addr_t *addr, uint16_t port, int *socket_error)
1221 int s, inprogress = 0;
1222 char addrbuf[256];
1223 struct sockaddr *dest_addr = (struct sockaddr*) addrbuf;
1224 socklen_t dest_addr_len;
1225 or_options_t *options = get_options();
1226 int protocol_family;
1228 if (get_n_open_sockets() >= get_options()->_ConnLimit-1) {
1229 warn_too_many_conns();
1230 return -1;
1233 if (tor_addr_family(addr) == AF_INET6)
1234 protocol_family = PF_INET6;
1235 else
1236 protocol_family = PF_INET;
1238 s = tor_open_socket(protocol_family,SOCK_STREAM,IPPROTO_TCP);
1239 if (s < 0) {
1240 *socket_error = tor_socket_errno(-1);
1241 log_warn(LD_NET,"Error creating network socket: %s",
1242 tor_socket_strerror(*socket_error));
1243 return -1;
1246 if (options->OutboundBindAddress && !tor_addr_is_loopback(addr)) {
1247 struct sockaddr_in ext_addr;
1249 memset(&ext_addr, 0, sizeof(ext_addr));
1250 ext_addr.sin_family = AF_INET;
1251 ext_addr.sin_port = 0;
1252 if (!tor_inet_aton(options->OutboundBindAddress, &ext_addr.sin_addr)) {
1253 log_warn(LD_CONFIG,"Outbound bind address '%s' didn't parse. Ignoring.",
1254 options->OutboundBindAddress);
1255 } else {
1256 if (bind(s, (struct sockaddr*)&ext_addr,
1257 (socklen_t)sizeof(ext_addr)) < 0) {
1258 *socket_error = tor_socket_errno(s);
1259 log_warn(LD_NET,"Error binding network socket: %s",
1260 tor_socket_strerror(*socket_error));
1261 tor_close_socket(s);
1262 return -1;
1267 set_socket_nonblocking(s);
1269 if (options->ConstrainedSockets)
1270 set_constrained_socket_buffers(s, (int)options->ConstrainedSockSize);
1272 memset(addrbuf,0,sizeof(addrbuf));
1273 dest_addr = (struct sockaddr*) addrbuf;
1274 dest_addr_len = tor_addr_to_sockaddr(addr, port, dest_addr, sizeof(addrbuf));
1275 tor_assert(dest_addr_len > 0);
1277 log_debug(LD_NET, "Connecting to %s:%u.",
1278 escaped_safe_str_client(address), port);
1280 if (connect(s, dest_addr, dest_addr_len) < 0) {
1281 int e = tor_socket_errno(s);
1282 if (!ERRNO_IS_CONN_EINPROGRESS(e)) {
1283 /* yuck. kill it. */
1284 *socket_error = e;
1285 log_info(LD_NET,
1286 "connect() to %s:%u failed: %s",
1287 escaped_safe_str_client(address),
1288 port, tor_socket_strerror(e));
1289 tor_close_socket(s);
1290 return -1;
1291 } else {
1292 inprogress = 1;
1296 if (!server_mode(options))
1297 client_check_address_changed(s);
1299 /* it succeeded. we're connected. */
1300 log_fn(inprogress?LOG_DEBUG:LOG_INFO, LD_NET,
1301 "Connection to %s:%u %s (sock %d).",
1302 escaped_safe_str_client(address),
1303 port, inprogress?"in progress":"established", s);
1304 conn->s = s;
1305 if (connection_add(conn) < 0) /* no space, forget it */
1306 return -1;
1307 return inprogress ? 0 : 1;
1310 /** Convert state number to string representation for logging purposes.
1312 static const char *
1313 connection_proxy_state_to_string(int state)
1315 static const char *unknown = "???";
1316 static const char *states[] = {
1317 "PROXY_NONE",
1318 "PROXY_HTTPS_WANT_CONNECT_OK",
1319 "PROXY_SOCKS4_WANT_CONNECT_OK",
1320 "PROXY_SOCKS5_WANT_AUTH_METHOD_NONE",
1321 "PROXY_SOCKS5_WANT_AUTH_METHOD_RFC1929",
1322 "PROXY_SOCKS5_WANT_AUTH_RFC1929_OK",
1323 "PROXY_SOCKS5_WANT_CONNECT_OK",
1324 "PROXY_CONNECTED",
1327 if (state < PROXY_NONE || state > PROXY_CONNECTED)
1328 return unknown;
1330 return states[state];
1333 /** Write a proxy request of <b>type</b> (socks4, socks5, https) to conn
1334 * for conn->addr:conn->port, authenticating with the auth details given
1335 * in the configuration (if available). SOCKS 5 and HTTP CONNECT proxies
1336 * support authentication.
1338 * Returns -1 if conn->addr is incompatible with the proxy protocol, and
1339 * 0 otherwise.
1341 * Use connection_read_proxy_handshake() to complete the handshake.
1344 connection_proxy_connect(connection_t *conn, int type)
1346 or_options_t *options;
1348 tor_assert(conn);
1350 options = get_options();
1352 switch (type) {
1353 case PROXY_CONNECT: {
1354 char buf[1024];
1355 char *base64_authenticator=NULL;
1356 const char *authenticator = options->HttpsProxyAuthenticator;
1358 /* Send HTTP CONNECT and authentication (if available) in
1359 * one request */
1361 if (authenticator) {
1362 base64_authenticator = alloc_http_authenticator(authenticator);
1363 if (!base64_authenticator)
1364 log_warn(LD_OR, "Encoding https authenticator failed");
1367 if (base64_authenticator) {
1368 tor_snprintf(buf, sizeof(buf), "CONNECT %s:%d HTTP/1.1\r\n"
1369 "Proxy-Authorization: Basic %s\r\n\r\n",
1370 fmt_addr(&conn->addr),
1371 conn->port, base64_authenticator);
1372 tor_free(base64_authenticator);
1373 } else {
1374 tor_snprintf(buf, sizeof(buf), "CONNECT %s:%d HTTP/1.0\r\n\r\n",
1375 fmt_addr(&conn->addr), conn->port);
1378 connection_write_to_buf(buf, strlen(buf), conn);
1379 conn->proxy_state = PROXY_HTTPS_WANT_CONNECT_OK;
1380 break;
1383 case PROXY_SOCKS4: {
1384 unsigned char buf[9];
1385 uint16_t portn;
1386 uint32_t ip4addr;
1388 /* Send a SOCKS4 connect request with empty user id */
1390 if (tor_addr_family(&conn->addr) != AF_INET) {
1391 log_warn(LD_NET, "SOCKS4 client is incompatible with IPv6");
1392 return -1;
1395 ip4addr = tor_addr_to_ipv4n(&conn->addr);
1396 portn = htons(conn->port);
1398 buf[0] = 4; /* version */
1399 buf[1] = SOCKS_COMMAND_CONNECT; /* command */
1400 memcpy(buf + 2, &portn, 2); /* port */
1401 memcpy(buf + 4, &ip4addr, 4); /* addr */
1402 buf[8] = 0; /* userid (empty) */
1404 connection_write_to_buf((char *)buf, sizeof(buf), conn);
1405 conn->proxy_state = PROXY_SOCKS4_WANT_CONNECT_OK;
1406 break;
1409 case PROXY_SOCKS5: {
1410 unsigned char buf[4]; /* fields: vers, num methods, method list */
1412 /* Send a SOCKS5 greeting (connect request must wait) */
1414 buf[0] = 5; /* version */
1416 /* number of auth methods */
1417 if (options->Socks5ProxyUsername) {
1418 buf[1] = 2;
1419 buf[2] = 0x00; /* no authentication */
1420 buf[3] = 0x02; /* rfc1929 Username/Passwd auth */
1421 conn->proxy_state = PROXY_SOCKS5_WANT_AUTH_METHOD_RFC1929;
1422 } else {
1423 buf[1] = 1;
1424 buf[2] = 0x00; /* no authentication */
1425 conn->proxy_state = PROXY_SOCKS5_WANT_AUTH_METHOD_NONE;
1428 connection_write_to_buf((char *)buf, 2 + buf[1], conn);
1429 break;
1432 default:
1433 log_err(LD_BUG, "Invalid proxy protocol, %d", type);
1434 tor_fragile_assert();
1435 return -1;
1438 log_debug(LD_NET, "set state %s",
1439 connection_proxy_state_to_string(conn->proxy_state));
1441 return 0;
1444 /** Read conn's inbuf. If the http response from the proxy is all
1445 * here, make sure it's good news, then return 1. If it's bad news,
1446 * return -1. Else return 0 and hope for better luck next time.
1448 static int
1449 connection_read_https_proxy_response(connection_t *conn)
1451 char *headers;
1452 char *reason=NULL;
1453 int status_code;
1454 time_t date_header;
1456 switch (fetch_from_buf_http(conn->inbuf,
1457 &headers, MAX_HEADERS_SIZE,
1458 NULL, NULL, 10000, 0)) {
1459 case -1: /* overflow */
1460 log_warn(LD_PROTOCOL,
1461 "Your https proxy sent back an oversized response. Closing.");
1462 return -1;
1463 case 0:
1464 log_info(LD_NET,"https proxy response not all here yet. Waiting.");
1465 return 0;
1466 /* case 1, fall through */
1469 if (parse_http_response(headers, &status_code, &date_header,
1470 NULL, &reason) < 0) {
1471 log_warn(LD_NET,
1472 "Unparseable headers from proxy (connecting to '%s'). Closing.",
1473 conn->address);
1474 tor_free(headers);
1475 return -1;
1477 if (!reason) reason = tor_strdup("[no reason given]");
1479 if (status_code == 200) {
1480 log_info(LD_NET,
1481 "HTTPS connect to '%s' successful! (200 %s) Starting TLS.",
1482 conn->address, escaped(reason));
1483 tor_free(reason);
1484 return 1;
1486 /* else, bad news on the status code */
1487 log_warn(LD_NET,
1488 "The https proxy sent back an unexpected status code %d (%s). "
1489 "Closing.",
1490 status_code, escaped(reason));
1491 tor_free(reason);
1492 return -1;
1495 /** Send SOCKS5 CONNECT command to <b>conn</b>, copying <b>conn->addr</b>
1496 * and <b>conn->port</b> into the request.
1498 static void
1499 connection_send_socks5_connect(connection_t *conn)
1501 unsigned char buf[1024];
1502 size_t reqsize = 6;
1503 uint16_t port = htons(conn->port);
1505 buf[0] = 5; /* version */
1506 buf[1] = SOCKS_COMMAND_CONNECT; /* command */
1507 buf[2] = 0; /* reserved */
1509 if (tor_addr_family(&conn->addr) == AF_INET) {
1510 uint32_t addr = tor_addr_to_ipv4n(&conn->addr);
1512 buf[3] = 1;
1513 reqsize += 4;
1514 memcpy(buf + 4, &addr, 4);
1515 memcpy(buf + 8, &port, 2);
1516 } else { /* AF_INET6 */
1517 buf[3] = 4;
1518 reqsize += 16;
1519 memcpy(buf + 4, tor_addr_to_in6(&conn->addr), 16);
1520 memcpy(buf + 20, &port, 2);
1523 connection_write_to_buf((char *)buf, reqsize, conn);
1525 conn->proxy_state = PROXY_SOCKS5_WANT_CONNECT_OK;
1528 /** Call this from connection_*_process_inbuf() to advance the proxy
1529 * handshake.
1531 * No matter what proxy protocol is used, if this function returns 1, the
1532 * handshake is complete, and the data remaining on inbuf may contain the
1533 * start of the communication with the requested server.
1535 * Returns 0 if the current buffer contains an incomplete response, and -1
1536 * on error.
1539 connection_read_proxy_handshake(connection_t *conn)
1541 int ret = 0;
1542 char *reason = NULL;
1544 log_debug(LD_NET, "enter state %s",
1545 connection_proxy_state_to_string(conn->proxy_state));
1547 switch (conn->proxy_state) {
1548 case PROXY_HTTPS_WANT_CONNECT_OK:
1549 ret = connection_read_https_proxy_response(conn);
1550 if (ret == 1)
1551 conn->proxy_state = PROXY_CONNECTED;
1552 break;
1554 case PROXY_SOCKS4_WANT_CONNECT_OK:
1555 ret = fetch_from_buf_socks_client(conn->inbuf,
1556 conn->proxy_state,
1557 &reason);
1558 if (ret == 1)
1559 conn->proxy_state = PROXY_CONNECTED;
1560 break;
1562 case PROXY_SOCKS5_WANT_AUTH_METHOD_NONE:
1563 ret = fetch_from_buf_socks_client(conn->inbuf,
1564 conn->proxy_state,
1565 &reason);
1566 /* no auth needed, do connect */
1567 if (ret == 1) {
1568 connection_send_socks5_connect(conn);
1569 ret = 0;
1571 break;
1573 case PROXY_SOCKS5_WANT_AUTH_METHOD_RFC1929:
1574 ret = fetch_from_buf_socks_client(conn->inbuf,
1575 conn->proxy_state,
1576 &reason);
1578 /* send auth if needed, otherwise do connect */
1579 if (ret == 1) {
1580 connection_send_socks5_connect(conn);
1581 ret = 0;
1582 } else if (ret == 2) {
1583 unsigned char buf[1024];
1584 size_t reqsize, usize, psize;
1585 const char *user, *pass;
1587 user = get_options()->Socks5ProxyUsername;
1588 pass = get_options()->Socks5ProxyPassword;
1589 tor_assert(user && pass);
1591 /* XXX len of user and pass must be <= 255 !!! */
1592 usize = strlen(user);
1593 psize = strlen(pass);
1594 tor_assert(usize <= 255 && psize <= 255);
1595 reqsize = 3 + usize + psize;
1597 buf[0] = 1; /* negotiation version */
1598 buf[1] = usize;
1599 memcpy(buf + 2, user, usize);
1600 buf[2 + usize] = psize;
1601 memcpy(buf + 3 + usize, pass, psize);
1603 connection_write_to_buf((char *)buf, reqsize, conn);
1605 conn->proxy_state = PROXY_SOCKS5_WANT_AUTH_RFC1929_OK;
1606 ret = 0;
1608 break;
1610 case PROXY_SOCKS5_WANT_AUTH_RFC1929_OK:
1611 ret = fetch_from_buf_socks_client(conn->inbuf,
1612 conn->proxy_state,
1613 &reason);
1614 /* send the connect request */
1615 if (ret == 1) {
1616 connection_send_socks5_connect(conn);
1617 ret = 0;
1619 break;
1621 case PROXY_SOCKS5_WANT_CONNECT_OK:
1622 ret = fetch_from_buf_socks_client(conn->inbuf,
1623 conn->proxy_state,
1624 &reason);
1625 if (ret == 1)
1626 conn->proxy_state = PROXY_CONNECTED;
1627 break;
1629 default:
1630 log_err(LD_BUG, "Invalid proxy_state for reading, %d",
1631 conn->proxy_state);
1632 tor_fragile_assert();
1633 ret = -1;
1634 break;
1637 log_debug(LD_NET, "leaving state %s",
1638 connection_proxy_state_to_string(conn->proxy_state));
1640 if (ret < 0) {
1641 if (reason) {
1642 log_warn(LD_NET, "Proxy Client: unable to connect to %s:%d (%s)",
1643 conn->address, conn->port, escaped(reason));
1644 tor_free(reason);
1645 } else {
1646 log_warn(LD_NET, "Proxy Client: unable to connect to %s:%d",
1647 conn->address, conn->port);
1649 } else if (ret == 1) {
1650 log_info(LD_NET, "Proxy Client: connection to %s:%d successful",
1651 conn->address, conn->port);
1654 return ret;
1658 * Launch any configured listener connections of type <b>type</b>. (A
1659 * listener is configured if <b>port_option</b> is non-zero. If any
1660 * ListenAddress configuration options are given in <b>cfg</b>, create a
1661 * connection binding to each one. Otherwise, create a single
1662 * connection binding to the address <b>default_addr</b>.)
1664 * Only launch the listeners of this type that are not already open, and
1665 * only close listeners that are no longer wanted. Existing listeners
1666 * that are still configured are not touched.
1668 * If <b>disable_all_conns</b> is set, then never open new conns, and
1669 * close the existing ones.
1671 * Add all old conns that should be closed to <b>replaced_conns</b>.
1672 * Add all new connections to <b>new_conns</b>.
1674 static int
1675 retry_listeners(int type, config_line_t *cfg,
1676 int port_option, const char *default_addr,
1677 smartlist_t *replaced_conns,
1678 smartlist_t *new_conns,
1679 int disable_all_conns,
1680 int socket_family)
1682 smartlist_t *launch = smartlist_create(), *conns;
1683 int free_launch_elts = 1;
1684 int r;
1685 config_line_t *c;
1686 connection_t *conn;
1687 config_line_t *line;
1689 tor_assert(socket_family == AF_INET || socket_family == AF_UNIX);
1691 if (cfg && port_option) {
1692 for (c = cfg; c; c = c->next) {
1693 smartlist_add(launch, c);
1695 free_launch_elts = 0;
1696 } else if (port_option) {
1697 line = tor_malloc_zero(sizeof(config_line_t));
1698 line->key = tor_strdup("");
1699 line->value = tor_strdup(default_addr);
1700 smartlist_add(launch, line);
1704 SMARTLIST_FOREACH(launch, config_line_t *, l,
1705 log_fn(LOG_NOTICE, "#%s#%s", l->key, l->value));
1708 conns = get_connection_array();
1709 SMARTLIST_FOREACH(conns, connection_t *, conn,
1711 if (conn->type != type ||
1712 conn->socket_family != socket_family ||
1713 conn->marked_for_close)
1714 continue;
1715 /* Okay, so this is a listener. Is it configured? */
1716 line = NULL;
1717 SMARTLIST_FOREACH(launch, config_line_t *, wanted,
1719 char *address=NULL;
1720 uint16_t port;
1721 switch (socket_family) {
1722 case AF_INET:
1723 if (!parse_addr_port(LOG_WARN,
1724 wanted->value, &address, NULL, &port)) {
1725 int addr_matches = !strcasecmp(address, conn->address);
1726 tor_free(address);
1727 if (! port)
1728 port = port_option;
1729 if (port == conn->port && addr_matches) {
1730 line = wanted;
1731 break;
1734 break;
1735 case AF_UNIX:
1736 if (!strcasecmp(wanted->value, conn->address)) {
1737 line = wanted;
1738 break;
1740 break;
1741 default:
1742 tor_assert(0);
1745 if (!line || disable_all_conns) {
1746 /* This one isn't configured. Close it. */
1747 log_notice(LD_NET, "Closing no-longer-configured %s on %s:%d",
1748 conn_type_to_string(type), conn->address, conn->port);
1749 if (replaced_conns) {
1750 smartlist_add(replaced_conns, conn);
1751 } else {
1752 connection_close_immediate(conn);
1753 connection_mark_for_close(conn);
1755 } else {
1756 /* It's configured; we don't need to launch it. */
1757 // log_debug(LD_NET, "Already have %s on %s:%d",
1758 // conn_type_to_string(type), conn->address, conn->port);
1759 smartlist_remove(launch, line);
1760 if (free_launch_elts)
1761 config_free_lines(line);
1765 /* Now open all the listeners that are configured but not opened. */
1766 r = 0;
1767 if (!disable_all_conns) {
1768 SMARTLIST_FOREACH_BEGIN(launch, config_line_t *, cfg_line) {
1769 char *address = NULL;
1770 struct sockaddr *listensockaddr;
1771 socklen_t listensocklen = 0;
1773 switch (socket_family) {
1774 case AF_INET:
1775 listensockaddr = (struct sockaddr *)
1776 create_inet_sockaddr(cfg_line->value,
1777 (uint16_t) port_option,
1778 &address, &listensocklen);
1779 break;
1780 case AF_UNIX:
1781 listensockaddr = (struct sockaddr *)
1782 create_unix_sockaddr(cfg_line->value,
1783 &address, &listensocklen);
1784 break;
1785 default:
1786 tor_assert(0);
1789 if (listensockaddr) {
1790 conn = connection_create_listener(listensockaddr, listensocklen,
1791 type, address);
1792 tor_free(listensockaddr);
1793 tor_free(address);
1794 } else
1795 conn = NULL;
1797 if (!conn) {
1798 r = -1;
1799 } else {
1800 if (new_conns)
1801 smartlist_add(new_conns, conn);
1803 } SMARTLIST_FOREACH_END(cfg_line);
1806 if (free_launch_elts) {
1807 SMARTLIST_FOREACH(launch, config_line_t *, cfg_line,
1808 config_free_lines(cfg_line));
1810 smartlist_free(launch);
1812 return r;
1815 /** Launch listeners for each port you should have open. Only launch
1816 * listeners who are not already open, and only close listeners we no longer
1817 * want.
1819 * Add all old conns that should be closed to <b>replaced_conns</b>.
1820 * Add all new connections to <b>new_conns</b>.
1823 retry_all_listeners(smartlist_t *replaced_conns,
1824 smartlist_t *new_conns)
1826 or_options_t *options = get_options();
1828 if (retry_listeners(CONN_TYPE_OR_LISTENER, options->ORListenAddress,
1829 options->ORPort, "0.0.0.0",
1830 replaced_conns, new_conns, options->ClientOnly,
1831 AF_INET)<0)
1832 return -1;
1833 if (retry_listeners(CONN_TYPE_DIR_LISTENER, options->DirListenAddress,
1834 options->DirPort, "0.0.0.0",
1835 replaced_conns, new_conns, options->ClientOnly,
1836 AF_INET)<0)
1837 return -1;
1838 if (retry_listeners(CONN_TYPE_AP_LISTENER, options->SocksListenAddress,
1839 options->SocksPort, "127.0.0.1",
1840 replaced_conns, new_conns, 0,
1841 AF_INET)<0)
1842 return -1;
1843 if (retry_listeners(CONN_TYPE_AP_TRANS_LISTENER, options->TransListenAddress,
1844 options->TransPort, "127.0.0.1",
1845 replaced_conns, new_conns, 0,
1846 AF_INET)<0)
1847 return -1;
1848 if (retry_listeners(CONN_TYPE_AP_NATD_LISTENER, options->NatdListenAddress,
1849 options->NatdPort, "127.0.0.1",
1850 replaced_conns, new_conns, 0,
1851 AF_INET)<0)
1852 return -1;
1853 if (retry_listeners(CONN_TYPE_AP_DNS_LISTENER, options->DNSListenAddress,
1854 options->DNSPort, "127.0.0.1",
1855 replaced_conns, new_conns, 0,
1856 AF_INET)<0)
1857 return -1;
1858 if (retry_listeners(CONN_TYPE_CONTROL_LISTENER,
1859 options->ControlListenAddress,
1860 options->ControlPort, "127.0.0.1",
1861 replaced_conns, new_conns, 0,
1862 AF_INET)<0)
1863 return -1;
1864 if (retry_listeners(CONN_TYPE_CONTROL_LISTENER,
1865 options->ControlSocket,
1866 options->ControlSocket ? 1 : 0, NULL,
1867 replaced_conns, new_conns, 0,
1868 AF_UNIX)<0)
1869 return -1;
1871 return 0;
1874 /** Return 1 if we should apply rate limiting to <b>conn</b>,
1875 * and 0 otherwise. Right now this just checks if it's an internal
1876 * IP address or an internal connection. */
1877 static int
1878 connection_is_rate_limited(connection_t *conn)
1880 if (conn->linked || /* internal connection */
1881 tor_addr_family(&conn->addr) == AF_UNSPEC || /* no address */
1882 tor_addr_is_internal(&conn->addr, 0)) /* internal address */
1883 return 0;
1884 else
1885 return 1;
1888 extern int global_read_bucket, global_write_bucket;
1889 extern int global_relayed_read_bucket, global_relayed_write_bucket;
1891 /** Did either global write bucket run dry last second? If so,
1892 * we are likely to run dry again this second, so be stingy with the
1893 * tokens we just put in. */
1894 static int write_buckets_empty_last_second = 0;
1896 /** How many seconds of no active local circuits will make the
1897 * connection revert to the "relayed" bandwidth class? */
1898 #define CLIENT_IDLE_TIME_FOR_PRIORITY 30
1900 /** Return 1 if <b>conn</b> should use tokens from the "relayed"
1901 * bandwidth rates, else 0. Currently, only OR conns with bandwidth
1902 * class 1, and directory conns that are serving data out, count.
1904 static int
1905 connection_counts_as_relayed_traffic(connection_t *conn, time_t now)
1907 if (conn->type == CONN_TYPE_OR &&
1908 TO_OR_CONN(conn)->client_used + CLIENT_IDLE_TIME_FOR_PRIORITY < now)
1909 return 1;
1910 if (conn->type == CONN_TYPE_DIR && DIR_CONN_IS_SERVER(conn))
1911 return 1;
1912 return 0;
1915 /** Helper function to decide how many bytes out of <b>global_bucket</b>
1916 * we're willing to use for this transaction. <b>base</b> is the size
1917 * of a cell on the network; <b>priority</b> says whether we should
1918 * write many of them or just a few; and <b>conn_bucket</b> (if
1919 * non-negative) provides an upper limit for our answer. */
1920 static ssize_t
1921 connection_bucket_round_robin(int base, int priority,
1922 ssize_t global_bucket, ssize_t conn_bucket)
1924 ssize_t at_most;
1925 ssize_t num_bytes_high = (priority ? 32 : 16) * base;
1926 ssize_t num_bytes_low = (priority ? 4 : 2) * base;
1928 /* Do a rudimentary round-robin so one circuit can't hog a connection.
1929 * Pick at most 32 cells, at least 4 cells if possible, and if we're in
1930 * the middle pick 1/8 of the available bandwidth. */
1931 at_most = global_bucket / 8;
1932 at_most -= (at_most % base); /* round down */
1933 if (at_most > num_bytes_high) /* 16 KB, or 8 KB for low-priority */
1934 at_most = num_bytes_high;
1935 else if (at_most < num_bytes_low) /* 2 KB, or 1 KB for low-priority */
1936 at_most = num_bytes_low;
1938 if (at_most > global_bucket)
1939 at_most = global_bucket;
1941 if (conn_bucket >= 0 && at_most > conn_bucket)
1942 at_most = conn_bucket;
1944 if (at_most < 0)
1945 return 0;
1946 return at_most;
1949 /** How many bytes at most can we read onto this connection? */
1950 static ssize_t
1951 connection_bucket_read_limit(connection_t *conn, time_t now)
1953 int base = connection_speaks_cells(conn) ?
1954 CELL_NETWORK_SIZE : RELAY_PAYLOAD_SIZE;
1955 int priority = conn->type != CONN_TYPE_DIR;
1956 int conn_bucket = -1;
1957 int global_bucket = global_read_bucket;
1959 if (connection_speaks_cells(conn)) {
1960 or_connection_t *or_conn = TO_OR_CONN(conn);
1961 if (conn->state == OR_CONN_STATE_OPEN)
1962 conn_bucket = or_conn->read_bucket;
1965 if (!connection_is_rate_limited(conn)) {
1966 /* be willing to read on local conns even if our buckets are empty */
1967 return conn_bucket>=0 ? conn_bucket : 1<<14;
1970 if (connection_counts_as_relayed_traffic(conn, now) &&
1971 global_relayed_read_bucket <= global_read_bucket)
1972 global_bucket = global_relayed_read_bucket;
1974 return connection_bucket_round_robin(base, priority,
1975 global_bucket, conn_bucket);
1978 /** How many bytes at most can we write onto this connection? */
1979 ssize_t
1980 connection_bucket_write_limit(connection_t *conn, time_t now)
1982 int base = connection_speaks_cells(conn) ?
1983 CELL_NETWORK_SIZE : RELAY_PAYLOAD_SIZE;
1984 int priority = conn->type != CONN_TYPE_DIR;
1985 int conn_bucket = (int)conn->outbuf_flushlen;
1986 int global_bucket = global_write_bucket;
1988 if (!connection_is_rate_limited(conn)) {
1989 /* be willing to write to local conns even if our buckets are empty */
1990 return conn->outbuf_flushlen;
1993 if (connection_speaks_cells(conn)) {
1994 /* use the per-conn write limit if it's lower, but if it's less
1995 * than zero just use zero */
1996 or_connection_t *or_conn = TO_OR_CONN(conn);
1997 if (conn->state == OR_CONN_STATE_OPEN)
1998 if (or_conn->write_bucket < conn_bucket)
1999 conn_bucket = or_conn->write_bucket >= 0 ?
2000 or_conn->write_bucket : 0;
2003 if (connection_counts_as_relayed_traffic(conn, now) &&
2004 global_relayed_write_bucket <= global_write_bucket)
2005 global_bucket = global_relayed_write_bucket;
2007 return connection_bucket_round_robin(base, priority,
2008 global_bucket, conn_bucket);
2011 /** Return 1 if the global write buckets are low enough that we
2012 * shouldn't send <b>attempt</b> bytes of low-priority directory stuff
2013 * out to <b>conn</b>. Else return 0.
2015 * Priority is 1 for v1 requests (directories and running-routers),
2016 * and 2 for v2 requests (statuses and descriptors). But see FFFF in
2017 * directory_handle_command_get() for why we don't use priority 2 yet.
2019 * There are a lot of parameters we could use here:
2020 * - global_relayed_write_bucket. Low is bad.
2021 * - global_write_bucket. Low is bad.
2022 * - bandwidthrate. Low is bad.
2023 * - bandwidthburst. Not a big factor?
2024 * - attempt. High is bad.
2025 * - total bytes queued on outbufs. High is bad. But I'm wary of
2026 * using this, since a few slow-flushing queues will pump up the
2027 * number without meaning what we meant to mean. What we really
2028 * mean is "total directory bytes added to outbufs recently", but
2029 * that's harder to quantify and harder to keep track of.
2032 global_write_bucket_low(connection_t *conn, size_t attempt, int priority)
2034 int smaller_bucket = global_write_bucket < global_relayed_write_bucket ?
2035 global_write_bucket : global_relayed_write_bucket;
2036 if (authdir_mode(get_options()) && priority>1)
2037 return 0; /* there's always room to answer v2 if we're an auth dir */
2039 if (!connection_is_rate_limited(conn))
2040 return 0; /* local conns don't get limited */
2042 if (smaller_bucket < (int)attempt)
2043 return 1; /* not enough space no matter the priority */
2045 if (write_buckets_empty_last_second)
2046 return 1; /* we're already hitting our limits, no more please */
2048 if (priority == 1) { /* old-style v1 query */
2049 /* Could we handle *two* of these requests within the next two seconds? */
2050 or_options_t *options = get_options();
2051 int64_t can_write = (int64_t)smaller_bucket
2052 + 2*(options->RelayBandwidthRate ? options->RelayBandwidthRate :
2053 options->BandwidthRate);
2054 if (can_write < 2*(int64_t)attempt)
2055 return 1;
2056 } else { /* v2 query */
2057 /* no further constraints yet */
2059 return 0;
2062 /** We just read <b>num_read</b> and wrote <b>num_written</b> bytes
2063 * onto <b>conn</b>. Decrement buckets appropriately. */
2064 static void
2065 connection_buckets_decrement(connection_t *conn, time_t now,
2066 size_t num_read, size_t num_written)
2068 if (!connection_is_rate_limited(conn))
2069 return; /* local IPs are free */
2070 if (num_written >= INT_MAX || num_read >= INT_MAX) {
2071 log_err(LD_BUG, "Value out of range. num_read=%lu, num_written=%lu, "
2072 "connection type=%s, state=%s",
2073 (unsigned long)num_read, (unsigned long)num_written,
2074 conn_type_to_string(conn->type),
2075 conn_state_to_string(conn->type, conn->state));
2076 if (num_written >= INT_MAX) num_written = 1;
2077 if (num_read >= INT_MAX) num_read = 1;
2078 tor_fragile_assert();
2081 if (num_read > 0) {
2082 if (conn->type == CONN_TYPE_EXIT)
2083 rep_hist_note_exit_bytes_read(conn->port, num_read);
2084 rep_hist_note_bytes_read(num_read, now);
2086 if (num_written > 0) {
2087 if (conn->type == CONN_TYPE_EXIT)
2088 rep_hist_note_exit_bytes_written(conn->port, num_written);
2089 rep_hist_note_bytes_written(num_written, now);
2092 if (connection_counts_as_relayed_traffic(conn, now)) {
2093 global_relayed_read_bucket -= (int)num_read;
2094 global_relayed_write_bucket -= (int)num_written;
2096 global_read_bucket -= (int)num_read;
2097 global_write_bucket -= (int)num_written;
2098 if (connection_speaks_cells(conn) && conn->state == OR_CONN_STATE_OPEN) {
2099 TO_OR_CONN(conn)->read_bucket -= (int)num_read;
2100 TO_OR_CONN(conn)->write_bucket -= (int)num_written;
2104 /** If we have exhausted our global buckets, or the buckets for conn,
2105 * stop reading. */
2106 static void
2107 connection_consider_empty_read_buckets(connection_t *conn)
2109 const char *reason;
2111 if (global_read_bucket <= 0) {
2112 reason = "global read bucket exhausted. Pausing.";
2113 } else if (connection_counts_as_relayed_traffic(conn, approx_time()) &&
2114 global_relayed_read_bucket <= 0) {
2115 reason = "global relayed read bucket exhausted. Pausing.";
2116 } else if (connection_speaks_cells(conn) &&
2117 conn->state == OR_CONN_STATE_OPEN &&
2118 TO_OR_CONN(conn)->read_bucket <= 0) {
2119 reason = "connection read bucket exhausted. Pausing.";
2120 } else
2121 return; /* all good, no need to stop it */
2123 LOG_FN_CONN(conn, (LOG_DEBUG, LD_NET, "%s", reason));
2124 conn->read_blocked_on_bw = 1;
2125 connection_stop_reading(conn);
2128 /** If we have exhausted our global buckets, or the buckets for conn,
2129 * stop writing. */
2130 static void
2131 connection_consider_empty_write_buckets(connection_t *conn)
2133 const char *reason;
2135 if (global_write_bucket <= 0) {
2136 reason = "global write bucket exhausted. Pausing.";
2137 } else if (connection_counts_as_relayed_traffic(conn, approx_time()) &&
2138 global_relayed_write_bucket <= 0) {
2139 reason = "global relayed write bucket exhausted. Pausing.";
2140 } else if (connection_speaks_cells(conn) &&
2141 conn->state == OR_CONN_STATE_OPEN &&
2142 TO_OR_CONN(conn)->write_bucket <= 0) {
2143 reason = "connection write bucket exhausted. Pausing.";
2144 } else
2145 return; /* all good, no need to stop it */
2147 LOG_FN_CONN(conn, (LOG_DEBUG, LD_NET, "%s", reason));
2148 conn->write_blocked_on_bw = 1;
2149 connection_stop_writing(conn);
2152 /** Initialize the global read bucket to options-\>BandwidthBurst. */
2153 void
2154 connection_bucket_init(void)
2156 or_options_t *options = get_options();
2157 /* start it at max traffic */
2158 global_read_bucket = (int)options->BandwidthBurst;
2159 global_write_bucket = (int)options->BandwidthBurst;
2160 if (options->RelayBandwidthRate) {
2161 global_relayed_read_bucket = (int)options->RelayBandwidthBurst;
2162 global_relayed_write_bucket = (int)options->RelayBandwidthBurst;
2163 } else {
2164 global_relayed_read_bucket = (int)options->BandwidthBurst;
2165 global_relayed_write_bucket = (int)options->BandwidthBurst;
2169 /** Refill a single <b>bucket</b> called <b>name</b> with bandwidth rate
2170 * <b>rate</b> and bandwidth burst <b>burst</b>, assuming that
2171 * <b>seconds_elapsed</b> seconds have passed since the last call.
2173 static void
2174 connection_bucket_refill_helper(int *bucket, int rate, int burst,
2175 int seconds_elapsed, const char *name)
2177 int starting_bucket = *bucket;
2178 if (starting_bucket < burst && seconds_elapsed) {
2179 if (((burst - starting_bucket)/seconds_elapsed) < rate) {
2180 *bucket = burst; /* We would overflow the bucket; just set it to
2181 * the maximum. */
2182 } else {
2183 int incr = rate*seconds_elapsed;
2184 *bucket += incr;
2185 if (*bucket > burst || *bucket < starting_bucket) {
2186 /* If we overflow the burst, or underflow our starting bucket,
2187 * cap the bucket value to burst. */
2188 /* XXXX this might be redundant now, but it doesn't show up
2189 * in profiles. Remove it after analysis. */
2190 *bucket = burst;
2193 log(LOG_DEBUG, LD_NET,"%s now %d.", name, *bucket);
2197 /** A second has rolled over; increment buckets appropriately. */
2198 void
2199 connection_bucket_refill(int seconds_elapsed, time_t now)
2201 or_options_t *options = get_options();
2202 smartlist_t *conns = get_connection_array();
2203 int relayrate, relayburst;
2205 if (options->RelayBandwidthRate) {
2206 relayrate = (int)options->RelayBandwidthRate;
2207 relayburst = (int)options->RelayBandwidthBurst;
2208 } else {
2209 relayrate = (int)options->BandwidthRate;
2210 relayburst = (int)options->BandwidthBurst;
2213 tor_assert(seconds_elapsed >= 0);
2215 write_buckets_empty_last_second =
2216 global_relayed_write_bucket <= 0 || global_write_bucket <= 0;
2218 /* refill the global buckets */
2219 connection_bucket_refill_helper(&global_read_bucket,
2220 (int)options->BandwidthRate,
2221 (int)options->BandwidthBurst,
2222 seconds_elapsed, "global_read_bucket");
2223 connection_bucket_refill_helper(&global_write_bucket,
2224 (int)options->BandwidthRate,
2225 (int)options->BandwidthBurst,
2226 seconds_elapsed, "global_write_bucket");
2227 connection_bucket_refill_helper(&global_relayed_read_bucket,
2228 relayrate, relayburst, seconds_elapsed,
2229 "global_relayed_read_bucket");
2230 connection_bucket_refill_helper(&global_relayed_write_bucket,
2231 relayrate, relayburst, seconds_elapsed,
2232 "global_relayed_write_bucket");
2234 /* refill the per-connection buckets */
2235 SMARTLIST_FOREACH(conns, connection_t *, conn,
2237 if (connection_speaks_cells(conn)) {
2238 or_connection_t *or_conn = TO_OR_CONN(conn);
2239 if (connection_bucket_should_increase(or_conn->read_bucket, or_conn)) {
2240 connection_bucket_refill_helper(&or_conn->read_bucket,
2241 or_conn->bandwidthrate,
2242 or_conn->bandwidthburst,
2243 seconds_elapsed,
2244 "or_conn->read_bucket");
2246 if (connection_bucket_should_increase(or_conn->write_bucket, or_conn)) {
2247 connection_bucket_refill_helper(&or_conn->write_bucket,
2248 or_conn->bandwidthrate,
2249 or_conn->bandwidthburst,
2250 seconds_elapsed,
2251 "or_conn->write_bucket");
2255 if (conn->read_blocked_on_bw == 1 /* marked to turn reading back on now */
2256 && global_read_bucket > 0 /* and we're allowed to read */
2257 && (!connection_counts_as_relayed_traffic(conn, now) ||
2258 global_relayed_read_bucket > 0) /* even if we're relayed traffic */
2259 && (!connection_speaks_cells(conn) ||
2260 conn->state != OR_CONN_STATE_OPEN ||
2261 TO_OR_CONN(conn)->read_bucket > 0)) {
2262 /* and either a non-cell conn or a cell conn with non-empty bucket */
2263 LOG_FN_CONN(conn, (LOG_DEBUG,LD_NET,
2264 "waking up conn (fd %d) for read", conn->s));
2265 conn->read_blocked_on_bw = 0;
2266 connection_start_reading(conn);
2269 if (conn->write_blocked_on_bw == 1
2270 && global_write_bucket > 0 /* and we're allowed to write */
2271 && (!connection_counts_as_relayed_traffic(conn, now) ||
2272 global_relayed_write_bucket > 0) /* even if it's relayed traffic */
2273 && (!connection_speaks_cells(conn) ||
2274 conn->state != OR_CONN_STATE_OPEN ||
2275 TO_OR_CONN(conn)->write_bucket > 0)) {
2276 LOG_FN_CONN(conn, (LOG_DEBUG,LD_NET,
2277 "waking up conn (fd %d) for write", conn->s));
2278 conn->write_blocked_on_bw = 0;
2279 connection_start_writing(conn);
2284 /** Is the <b>bucket</b> for connection <b>conn</b> low enough that we
2285 * should add another pile of tokens to it?
2287 static int
2288 connection_bucket_should_increase(int bucket, or_connection_t *conn)
2290 tor_assert(conn);
2292 if (conn->_base.state != OR_CONN_STATE_OPEN)
2293 return 0; /* only open connections play the rate limiting game */
2294 if (bucket >= conn->bandwidthburst)
2295 return 0;
2297 return 1;
2300 /** Read bytes from conn-\>s and process them.
2302 * This function gets called from conn_read() in main.c, either
2303 * when poll() has declared that conn wants to read, or (for OR conns)
2304 * when there are pending TLS bytes.
2306 * It calls connection_read_to_buf() to bring in any new bytes,
2307 * and then calls connection_process_inbuf() to process them.
2309 * Mark the connection and return -1 if you want to close it, else
2310 * return 0.
2312 static int
2313 connection_handle_read_impl(connection_t *conn)
2315 int max_to_read=-1, try_to_read;
2316 size_t before, n_read = 0;
2317 int socket_error = 0;
2319 if (conn->marked_for_close)
2320 return 0; /* do nothing */
2322 conn->timestamp_lastread = approx_time();
2324 switch (conn->type) {
2325 case CONN_TYPE_OR_LISTENER:
2326 return connection_handle_listener_read(conn, CONN_TYPE_OR);
2327 case CONN_TYPE_AP_LISTENER:
2328 case CONN_TYPE_AP_TRANS_LISTENER:
2329 case CONN_TYPE_AP_NATD_LISTENER:
2330 return connection_handle_listener_read(conn, CONN_TYPE_AP);
2331 case CONN_TYPE_DIR_LISTENER:
2332 return connection_handle_listener_read(conn, CONN_TYPE_DIR);
2333 case CONN_TYPE_CONTROL_LISTENER:
2334 return connection_handle_listener_read(conn, CONN_TYPE_CONTROL);
2335 case CONN_TYPE_AP_DNS_LISTENER:
2336 /* This should never happen; eventdns.c handles the reads here. */
2337 tor_fragile_assert();
2338 return 0;
2341 loop_again:
2342 try_to_read = max_to_read;
2343 tor_assert(!conn->marked_for_close);
2345 before = buf_datalen(conn->inbuf);
2346 if (connection_read_to_buf(conn, &max_to_read, &socket_error) < 0) {
2347 /* There's a read error; kill the connection.*/
2348 if (conn->type == CONN_TYPE_OR &&
2349 conn->state == OR_CONN_STATE_CONNECTING) {
2350 connection_or_connect_failed(TO_OR_CONN(conn),
2351 errno_to_orconn_end_reason(socket_error),
2352 tor_socket_strerror(socket_error));
2354 if (CONN_IS_EDGE(conn)) {
2355 edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
2356 connection_edge_end_errno(edge_conn);
2357 if (edge_conn->socks_request) /* broken, don't send a socks reply back */
2358 edge_conn->socks_request->has_finished = 1;
2360 connection_close_immediate(conn); /* Don't flush; connection is dead. */
2361 connection_mark_for_close(conn);
2362 return -1;
2364 n_read += buf_datalen(conn->inbuf) - before;
2365 if (CONN_IS_EDGE(conn) && try_to_read != max_to_read) {
2366 /* instruct it not to try to package partial cells. */
2367 if (connection_process_inbuf(conn, 0) < 0) {
2368 return -1;
2370 if (!conn->marked_for_close &&
2371 connection_is_reading(conn) &&
2372 !conn->inbuf_reached_eof &&
2373 max_to_read > 0)
2374 goto loop_again; /* try reading again, in case more is here now */
2376 /* one last try, packaging partial cells and all. */
2377 if (!conn->marked_for_close &&
2378 connection_process_inbuf(conn, 1) < 0) {
2379 return -1;
2381 if (conn->linked_conn) {
2382 /* The other side's handle_write() will never actually get called, so
2383 * we need to invoke the appropriate callbacks ourself. */
2384 connection_t *linked = conn->linked_conn;
2386 if (n_read) {
2387 /* Probably a no-op, but hey. */
2388 connection_buckets_decrement(linked, approx_time(), n_read, 0);
2390 if (connection_flushed_some(linked) < 0)
2391 connection_mark_for_close(linked);
2392 if (!connection_wants_to_flush(linked))
2393 connection_finished_flushing(linked);
2396 if (!buf_datalen(linked->outbuf) && conn->active_on_link)
2397 connection_stop_reading_from_linked_conn(conn);
2399 /* If we hit the EOF, call connection_reached_eof(). */
2400 if (!conn->marked_for_close &&
2401 conn->inbuf_reached_eof &&
2402 connection_reached_eof(conn) < 0) {
2403 return -1;
2405 return 0;
2409 connection_handle_read(connection_t *conn)
2411 int res;
2413 tor_gettimeofday_cache_clear();
2414 res = connection_handle_read_impl(conn);
2415 return res;
2418 /** Pull in new bytes from conn-\>s or conn-\>linked_conn onto conn-\>inbuf,
2419 * either directly or via TLS. Reduce the token buckets by the number of bytes
2420 * read.
2422 * If *max_to_read is -1, then decide it ourselves, else go with the
2423 * value passed to us. When returning, if it's changed, subtract the
2424 * number of bytes we read from *max_to_read.
2426 * Return -1 if we want to break conn, else return 0.
2428 static int
2429 connection_read_to_buf(connection_t *conn, int *max_to_read, int *socket_error)
2431 int result;
2432 ssize_t at_most = *max_to_read;
2433 size_t slack_in_buf, more_to_read;
2434 size_t n_read = 0, n_written = 0;
2436 if (at_most == -1) { /* we need to initialize it */
2437 /* how many bytes are we allowed to read? */
2438 at_most = connection_bucket_read_limit(conn, approx_time());
2441 slack_in_buf = buf_slack(conn->inbuf);
2442 again:
2443 if ((size_t)at_most > slack_in_buf && slack_in_buf >= 1024) {
2444 more_to_read = at_most - slack_in_buf;
2445 at_most = slack_in_buf;
2446 } else {
2447 more_to_read = 0;
2450 if (connection_speaks_cells(conn) &&
2451 conn->state > OR_CONN_STATE_PROXY_HANDSHAKING) {
2452 int pending;
2453 or_connection_t *or_conn = TO_OR_CONN(conn);
2454 size_t initial_size;
2455 if (conn->state == OR_CONN_STATE_TLS_HANDSHAKING ||
2456 conn->state == OR_CONN_STATE_TLS_CLIENT_RENEGOTIATING) {
2457 /* continue handshaking even if global token bucket is empty */
2458 return connection_tls_continue_handshake(or_conn);
2461 log_debug(LD_NET,
2462 "%d: starting, inbuf_datalen %ld (%d pending in tls object)."
2463 " at_most %ld.",
2464 conn->s,(long)buf_datalen(conn->inbuf),
2465 tor_tls_get_pending_bytes(or_conn->tls), (long)at_most);
2467 initial_size = buf_datalen(conn->inbuf);
2468 /* else open, or closing */
2469 result = read_to_buf_tls(or_conn->tls, at_most, conn->inbuf);
2470 if (TOR_TLS_IS_ERROR(result) || result == TOR_TLS_CLOSE)
2471 or_conn->tls_error = result;
2472 else
2473 or_conn->tls_error = 0;
2475 switch (result) {
2476 case TOR_TLS_CLOSE:
2477 case TOR_TLS_ERROR_IO:
2478 log_debug(LD_NET,"TLS connection closed %son read. Closing. "
2479 "(Nickname %s, address %s)",
2480 result == TOR_TLS_CLOSE ? "cleanly " : "",
2481 or_conn->nickname ? or_conn->nickname : "not set",
2482 conn->address);
2483 return result;
2484 CASE_TOR_TLS_ERROR_ANY_NONIO:
2485 log_debug(LD_NET,"tls error [%s]. breaking (nickname %s, address %s).",
2486 tor_tls_err_to_string(result),
2487 or_conn->nickname ? or_conn->nickname : "not set",
2488 conn->address);
2489 return result;
2490 case TOR_TLS_WANTWRITE:
2491 connection_start_writing(conn);
2492 return 0;
2493 case TOR_TLS_WANTREAD: /* we're already reading */
2494 case TOR_TLS_DONE: /* no data read, so nothing to process */
2495 result = 0;
2496 break; /* so we call bucket_decrement below */
2497 default:
2498 break;
2500 pending = tor_tls_get_pending_bytes(or_conn->tls);
2501 if (pending) {
2502 /* If we have any pending bytes, we read them now. This *can*
2503 * take us over our read allotment, but really we shouldn't be
2504 * believing that SSL bytes are the same as TCP bytes anyway. */
2505 int r2 = read_to_buf_tls(or_conn->tls, pending, conn->inbuf);
2506 if (r2<0) {
2507 log_warn(LD_BUG, "apparently, reading pending bytes can fail.");
2508 return -1;
2511 result = (int)(buf_datalen(conn->inbuf)-initial_size);
2512 tor_tls_get_n_raw_bytes(or_conn->tls, &n_read, &n_written);
2513 log_debug(LD_GENERAL, "After TLS read of %d: %ld read, %ld written",
2514 result, (long)n_read, (long)n_written);
2515 } else if (conn->linked) {
2516 if (conn->linked_conn) {
2517 result = move_buf_to_buf(conn->inbuf, conn->linked_conn->outbuf,
2518 &conn->linked_conn->outbuf_flushlen);
2519 } else {
2520 result = 0;
2522 //log_notice(LD_GENERAL, "Moved %d bytes on an internal link!", result);
2523 /* If the other side has disappeared, or if it's been marked for close and
2524 * we flushed its outbuf, then we should set our inbuf_reached_eof. */
2525 if (!conn->linked_conn ||
2526 (conn->linked_conn->marked_for_close &&
2527 buf_datalen(conn->linked_conn->outbuf) == 0))
2528 conn->inbuf_reached_eof = 1;
2530 n_read = (size_t) result;
2531 } else {
2532 /* !connection_speaks_cells, !conn->linked_conn. */
2533 int reached_eof = 0;
2534 CONN_LOG_PROTECT(conn,
2535 result = read_to_buf(conn->s, at_most, conn->inbuf, &reached_eof,
2536 socket_error));
2537 if (reached_eof)
2538 conn->inbuf_reached_eof = 1;
2540 // log_fn(LOG_DEBUG,"read_to_buf returned %d.",read_result);
2542 if (result < 0)
2543 return -1;
2544 n_read = (size_t) result;
2547 if (n_read > 0) { /* change *max_to_read */
2548 /*XXXX021 check for overflow*/
2549 *max_to_read = (int)(at_most - n_read);
2552 if (conn->type == CONN_TYPE_AP) {
2553 edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
2554 /*XXXX021 check for overflow*/
2555 edge_conn->n_read += (int)n_read;
2558 connection_buckets_decrement(conn, approx_time(), n_read, n_written);
2560 if (more_to_read && result == at_most) {
2561 slack_in_buf = buf_slack(conn->inbuf);
2562 at_most = more_to_read;
2563 goto again;
2566 /* Call even if result is 0, since the global read bucket may
2567 * have reached 0 on a different conn, and this guy needs to
2568 * know to stop reading. */
2569 connection_consider_empty_read_buckets(conn);
2570 if (n_written > 0 && connection_is_writing(conn))
2571 connection_consider_empty_write_buckets(conn);
2573 return 0;
2576 /** A pass-through to fetch_from_buf. */
2578 connection_fetch_from_buf(char *string, size_t len, connection_t *conn)
2580 return fetch_from_buf(string, len, conn->inbuf);
2583 /** Return conn-\>outbuf_flushlen: how many bytes conn wants to flush
2584 * from its outbuf. */
2586 connection_wants_to_flush(connection_t *conn)
2588 return conn->outbuf_flushlen > 0;
2591 /** Are there too many bytes on edge connection <b>conn</b>'s outbuf to
2592 * send back a relay-level sendme yet? Return 1 if so, 0 if not. Used by
2593 * connection_edge_consider_sending_sendme().
2596 connection_outbuf_too_full(connection_t *conn)
2598 return (conn->outbuf_flushlen > 10*CELL_PAYLOAD_SIZE);
2601 /** Try to flush more bytes onto conn-\>s.
2603 * This function gets called either from conn_write() in main.c
2604 * when poll() has declared that conn wants to write, or below
2605 * from connection_write_to_buf() when an entire TLS record is ready.
2607 * Update conn-\>timestamp_lastwritten to now, and call flush_buf
2608 * or flush_buf_tls appropriately. If it succeeds and there are no more
2609 * more bytes on conn->outbuf, then call connection_finished_flushing
2610 * on it too.
2612 * If <b>force</b>, then write as many bytes as possible, ignoring bandwidth
2613 * limits. (Used for flushing messages to controller connections on fatal
2614 * errors.)
2616 * Mark the connection and return -1 if you want to close it, else
2617 * return 0.
2619 static int
2620 connection_handle_write_impl(connection_t *conn, int force)
2622 int e;
2623 socklen_t len=(socklen_t)sizeof(e);
2624 int result;
2625 ssize_t max_to_write;
2626 time_t now = approx_time();
2627 size_t n_read = 0, n_written = 0;
2629 tor_assert(!connection_is_listener(conn));
2631 if (conn->marked_for_close || conn->s < 0)
2632 return 0; /* do nothing */
2634 if (conn->in_flushed_some) {
2635 log_warn(LD_BUG, "called recursively from inside conn->in_flushed_some");
2636 return 0;
2639 conn->timestamp_lastwritten = now;
2641 /* Sometimes, "writable" means "connected". */
2642 if (connection_state_is_connecting(conn)) {
2643 if (getsockopt(conn->s, SOL_SOCKET, SO_ERROR, (void*)&e, &len) < 0) {
2644 log_warn(LD_BUG,
2645 "getsockopt() syscall failed?! Please report to tor-ops.");
2646 if (CONN_IS_EDGE(conn))
2647 connection_edge_end_errno(TO_EDGE_CONN(conn));
2648 connection_mark_for_close(conn);
2649 return -1;
2651 if (e) {
2652 /* some sort of error, but maybe just inprogress still */
2653 if (!ERRNO_IS_CONN_EINPROGRESS(e)) {
2654 log_info(LD_NET,"in-progress connect failed. Removing. (%s)",
2655 tor_socket_strerror(e));
2656 if (CONN_IS_EDGE(conn))
2657 connection_edge_end_errno(TO_EDGE_CONN(conn));
2658 if (conn->type == CONN_TYPE_OR)
2659 connection_or_connect_failed(TO_OR_CONN(conn),
2660 errno_to_orconn_end_reason(e),
2661 tor_socket_strerror(e));
2663 connection_close_immediate(conn);
2664 connection_mark_for_close(conn);
2665 return -1;
2666 } else {
2667 return 0; /* no change, see if next time is better */
2670 /* The connection is successful. */
2671 if (connection_finished_connecting(conn)<0)
2672 return -1;
2675 max_to_write = force ? (ssize_t)conn->outbuf_flushlen
2676 : connection_bucket_write_limit(conn, now);
2678 if (connection_speaks_cells(conn) &&
2679 conn->state > OR_CONN_STATE_PROXY_HANDSHAKING) {
2680 or_connection_t *or_conn = TO_OR_CONN(conn);
2681 if (conn->state == OR_CONN_STATE_TLS_HANDSHAKING ||
2682 conn->state == OR_CONN_STATE_TLS_CLIENT_RENEGOTIATING) {
2683 connection_stop_writing(conn);
2684 if (connection_tls_continue_handshake(or_conn) < 0) {
2685 /* Don't flush; connection is dead. */
2686 connection_close_immediate(conn);
2687 connection_mark_for_close(conn);
2688 return -1;
2690 return 0;
2691 } else if (conn->state == OR_CONN_STATE_TLS_SERVER_RENEGOTIATING) {
2692 return connection_handle_read(conn);
2695 /* else open, or closing */
2696 result = flush_buf_tls(or_conn->tls, conn->outbuf,
2697 max_to_write, &conn->outbuf_flushlen);
2699 /* If we just flushed the last bytes, check if this tunneled dir
2700 * request is done. */
2701 if (buf_datalen(conn->outbuf) == 0 && conn->dirreq_id)
2702 geoip_change_dirreq_state(conn->dirreq_id, DIRREQ_TUNNELED,
2703 DIRREQ_OR_CONN_BUFFER_FLUSHED);
2705 switch (result) {
2706 CASE_TOR_TLS_ERROR_ANY:
2707 case TOR_TLS_CLOSE:
2708 log_info(LD_NET,result!=TOR_TLS_CLOSE?
2709 "tls error. breaking.":"TLS connection closed on flush");
2710 /* Don't flush; connection is dead. */
2711 connection_close_immediate(conn);
2712 connection_mark_for_close(conn);
2713 return -1;
2714 case TOR_TLS_WANTWRITE:
2715 log_debug(LD_NET,"wanted write.");
2716 /* we're already writing */
2717 return 0;
2718 case TOR_TLS_WANTREAD:
2719 /* Make sure to avoid a loop if the receive buckets are empty. */
2720 log_debug(LD_NET,"wanted read.");
2721 if (!connection_is_reading(conn)) {
2722 connection_stop_writing(conn);
2723 conn->write_blocked_on_bw = 1;
2724 /* we'll start reading again when we get more tokens in our
2725 * read bucket; then we'll start writing again too.
2728 /* else no problem, we're already reading */
2729 return 0;
2730 /* case TOR_TLS_DONE:
2731 * for TOR_TLS_DONE, fall through to check if the flushlen
2732 * is empty, so we can stop writing.
2736 tor_tls_get_n_raw_bytes(or_conn->tls, &n_read, &n_written);
2737 log_debug(LD_GENERAL, "After TLS write of %d: %ld read, %ld written",
2738 result, (long)n_read, (long)n_written);
2739 } else {
2740 CONN_LOG_PROTECT(conn,
2741 result = flush_buf(conn->s, conn->outbuf,
2742 max_to_write, &conn->outbuf_flushlen));
2743 if (result < 0) {
2744 if (CONN_IS_EDGE(conn))
2745 connection_edge_end_errno(TO_EDGE_CONN(conn));
2747 connection_close_immediate(conn); /* Don't flush; connection is dead. */
2748 connection_mark_for_close(conn);
2749 return -1;
2751 n_written = (size_t) result;
2754 if (conn->type == CONN_TYPE_AP) {
2755 edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
2756 /*XXXX021 check for overflow.*/
2757 edge_conn->n_written += (int)n_written;
2760 connection_buckets_decrement(conn, approx_time(), n_read, n_written);
2762 if (result > 0) {
2763 /* If we wrote any bytes from our buffer, then call the appropriate
2764 * functions. */
2765 if (connection_flushed_some(conn) < 0)
2766 connection_mark_for_close(conn);
2769 if (!connection_wants_to_flush(conn)) { /* it's done flushing */
2770 if (connection_finished_flushing(conn) < 0) {
2771 /* already marked */
2772 return -1;
2774 return 0;
2777 /* Call even if result is 0, since the global write bucket may
2778 * have reached 0 on a different conn, and this guy needs to
2779 * know to stop writing. */
2780 connection_consider_empty_write_buckets(conn);
2781 if (n_read > 0 && connection_is_reading(conn))
2782 connection_consider_empty_read_buckets(conn);
2784 return 0;
2788 connection_handle_write(connection_t *conn, int force)
2790 int res;
2791 tor_gettimeofday_cache_clear();
2792 res = connection_handle_write_impl(conn, force);
2793 return res;
2796 /** OpenSSL TLS record size is 16383; this is close. The goal here is to
2797 * push data out as soon as we know there's enough for a TLS record, so
2798 * during periods of high load we won't read entire megabytes from
2799 * input before pushing any data out. It also has the feature of not
2800 * growing huge outbufs unless something is slow. */
2801 #define MIN_TLS_FLUSHLEN 15872
2803 /** Append <b>len</b> bytes of <b>string</b> onto <b>conn</b>'s
2804 * outbuf, and ask it to start writing.
2806 * If <b>zlib</b> is nonzero, this is a directory connection that should get
2807 * its contents compressed or decompressed as they're written. If zlib is
2808 * negative, this is the last data to be compressed, and the connection's zlib
2809 * state should be flushed.
2811 * If it's an OR conn and an entire TLS record is ready, then try to
2812 * flush the record now. Similarly, if it's a local control connection
2813 * and a 64k chunk is ready, try to flush it all, so we don't end up with
2814 * many megabytes of controller info queued at once.
2816 void
2817 _connection_write_to_buf_impl(const char *string, size_t len,
2818 connection_t *conn, int zlib)
2820 /* XXXX This function really needs to return -1 on failure. */
2821 int r;
2822 size_t old_datalen;
2823 if (!len && !(zlib<0))
2824 return;
2825 /* if it's marked for close, only allow write if we mean to flush it */
2826 if (conn->marked_for_close && !conn->hold_open_until_flushed)
2827 return;
2829 old_datalen = buf_datalen(conn->outbuf);
2830 if (zlib) {
2831 dir_connection_t *dir_conn = TO_DIR_CONN(conn);
2832 int done = zlib < 0;
2833 CONN_LOG_PROTECT(conn, r = write_to_buf_zlib(conn->outbuf,
2834 dir_conn->zlib_state,
2835 string, len, done));
2836 } else {
2837 CONN_LOG_PROTECT(conn, r = write_to_buf(string, len, conn->outbuf));
2839 if (r < 0) {
2840 if (CONN_IS_EDGE(conn)) {
2841 /* if it failed, it means we have our package/delivery windows set
2842 wrong compared to our max outbuf size. close the whole circuit. */
2843 log_warn(LD_NET,
2844 "write_to_buf failed. Closing circuit (fd %d).", conn->s);
2845 circuit_mark_for_close(circuit_get_by_edge_conn(TO_EDGE_CONN(conn)),
2846 END_CIRC_REASON_INTERNAL);
2847 } else {
2848 log_warn(LD_NET,
2849 "write_to_buf failed. Closing connection (fd %d).", conn->s);
2850 connection_mark_for_close(conn);
2852 return;
2855 connection_start_writing(conn);
2856 if (zlib) {
2857 conn->outbuf_flushlen += buf_datalen(conn->outbuf) - old_datalen;
2858 } else {
2859 ssize_t extra = 0;
2860 conn->outbuf_flushlen += len;
2862 /* Should we try flushing the outbuf now? */
2863 if (conn->in_flushed_some) {
2864 /* Don't flush the outbuf when the reason we're writing more stuff is
2865 * _because_ we flushed the outbuf. That's unfair. */
2866 return;
2869 if (conn->type == CONN_TYPE_OR &&
2870 conn->outbuf_flushlen-len < MIN_TLS_FLUSHLEN &&
2871 conn->outbuf_flushlen >= MIN_TLS_FLUSHLEN) {
2872 /* We just pushed outbuf_flushlen to MIN_TLS_FLUSHLEN or above;
2873 * we can send out a full TLS frame now if we like. */
2874 extra = conn->outbuf_flushlen - MIN_TLS_FLUSHLEN;
2875 conn->outbuf_flushlen = MIN_TLS_FLUSHLEN;
2876 } else if (conn->type == CONN_TYPE_CONTROL &&
2877 !connection_is_rate_limited(conn) &&
2878 conn->outbuf_flushlen-len < 1<<16 &&
2879 conn->outbuf_flushlen >= 1<<16) {
2880 /* just try to flush all of it */
2881 } else
2882 return; /* no need to try flushing */
2884 if (connection_handle_write(conn, 0) < 0) {
2885 if (!conn->marked_for_close) {
2886 /* this connection is broken. remove it. */
2887 log_warn(LD_BUG, "unhandled error on write for "
2888 "conn (type %d, fd %d); removing",
2889 conn->type, conn->s);
2890 tor_fragile_assert();
2891 /* do a close-immediate here, so we don't try to flush */
2892 connection_close_immediate(conn);
2894 return;
2896 if (extra) {
2897 conn->outbuf_flushlen += extra;
2898 connection_start_writing(conn);
2903 /** Return a connection with given type, address, port, and purpose;
2904 * or NULL if no such connection exists. */
2905 connection_t *
2906 connection_get_by_type_addr_port_purpose(int type,
2907 const tor_addr_t *addr, uint16_t port,
2908 int purpose)
2910 smartlist_t *conns = get_connection_array();
2911 SMARTLIST_FOREACH(conns, connection_t *, conn,
2913 if (conn->type == type &&
2914 tor_addr_eq(&conn->addr, addr) &&
2915 conn->port == port &&
2916 conn->purpose == purpose &&
2917 !conn->marked_for_close)
2918 return conn;
2920 return NULL;
2923 /** Return the stream with id <b>id</b> if it is not already marked for
2924 * close.
2926 connection_t *
2927 connection_get_by_global_id(uint64_t id)
2929 smartlist_t *conns = get_connection_array();
2930 SMARTLIST_FOREACH(conns, connection_t *, conn,
2932 if (conn->global_identifier == id)
2933 return conn;
2935 return NULL;
2938 /** Return a connection of type <b>type</b> that is not marked for close.
2940 connection_t *
2941 connection_get_by_type(int type)
2943 smartlist_t *conns = get_connection_array();
2944 SMARTLIST_FOREACH(conns, connection_t *, conn,
2946 if (conn->type == type && !conn->marked_for_close)
2947 return conn;
2949 return NULL;
2952 /** Return a connection of type <b>type</b> that is in state <b>state</b>,
2953 * and that is not marked for close.
2955 connection_t *
2956 connection_get_by_type_state(int type, int state)
2958 smartlist_t *conns = get_connection_array();
2959 SMARTLIST_FOREACH(conns, connection_t *, conn,
2961 if (conn->type == type && conn->state == state && !conn->marked_for_close)
2962 return conn;
2964 return NULL;
2967 /** Return a connection of type <b>type</b> that has rendquery equal
2968 * to <b>rendquery</b>, and that is not marked for close. If state
2969 * is non-zero, conn must be of that state too.
2971 connection_t *
2972 connection_get_by_type_state_rendquery(int type, int state,
2973 const char *rendquery)
2975 smartlist_t *conns = get_connection_array();
2977 tor_assert(type == CONN_TYPE_DIR ||
2978 type == CONN_TYPE_AP || type == CONN_TYPE_EXIT);
2979 tor_assert(rendquery);
2981 SMARTLIST_FOREACH(conns, connection_t *, conn,
2983 if (conn->type == type &&
2984 !conn->marked_for_close &&
2985 (!state || state == conn->state)) {
2986 if (type == CONN_TYPE_DIR &&
2987 TO_DIR_CONN(conn)->rend_data &&
2988 !rend_cmp_service_ids(rendquery,
2989 TO_DIR_CONN(conn)->rend_data->onion_address))
2990 return conn;
2991 else if (CONN_IS_EDGE(conn) &&
2992 TO_EDGE_CONN(conn)->rend_data &&
2993 !rend_cmp_service_ids(rendquery,
2994 TO_EDGE_CONN(conn)->rend_data->onion_address))
2995 return conn;
2998 return NULL;
3001 /** Return an open, non-marked connection of a given type and purpose, or NULL
3002 * if no such connection exists. */
3003 connection_t *
3004 connection_get_by_type_purpose(int type, int purpose)
3006 smartlist_t *conns = get_connection_array();
3007 SMARTLIST_FOREACH(conns, connection_t *, conn,
3009 if (conn->type == type &&
3010 !conn->marked_for_close &&
3011 (purpose == conn->purpose))
3012 return conn;
3014 return NULL;
3017 /** Return 1 if <b>conn</b> is a listener conn, else return 0. */
3019 connection_is_listener(connection_t *conn)
3021 if (conn->type == CONN_TYPE_OR_LISTENER ||
3022 conn->type == CONN_TYPE_AP_LISTENER ||
3023 conn->type == CONN_TYPE_AP_TRANS_LISTENER ||
3024 conn->type == CONN_TYPE_AP_DNS_LISTENER ||
3025 conn->type == CONN_TYPE_AP_NATD_LISTENER ||
3026 conn->type == CONN_TYPE_DIR_LISTENER ||
3027 conn->type == CONN_TYPE_CONTROL_LISTENER)
3028 return 1;
3029 return 0;
3032 /** Return 1 if <b>conn</b> is in state "open" and is not marked
3033 * for close, else return 0.
3036 connection_state_is_open(connection_t *conn)
3038 tor_assert(conn);
3040 if (conn->marked_for_close)
3041 return 0;
3043 if ((conn->type == CONN_TYPE_OR && conn->state == OR_CONN_STATE_OPEN) ||
3044 (conn->type == CONN_TYPE_AP && conn->state == AP_CONN_STATE_OPEN) ||
3045 (conn->type == CONN_TYPE_EXIT && conn->state == EXIT_CONN_STATE_OPEN) ||
3046 (conn->type == CONN_TYPE_CONTROL &&
3047 conn->state == CONTROL_CONN_STATE_OPEN))
3048 return 1;
3050 return 0;
3053 /** Return 1 if conn is in 'connecting' state, else return 0. */
3055 connection_state_is_connecting(connection_t *conn)
3057 tor_assert(conn);
3059 if (conn->marked_for_close)
3060 return 0;
3061 switch (conn->type)
3063 case CONN_TYPE_OR:
3064 return conn->state == OR_CONN_STATE_CONNECTING;
3065 case CONN_TYPE_EXIT:
3066 return conn->state == EXIT_CONN_STATE_CONNECTING;
3067 case CONN_TYPE_DIR:
3068 return conn->state == DIR_CONN_STATE_CONNECTING;
3071 return 0;
3074 /** Allocates a base64'ed authenticator for use in http or https
3075 * auth, based on the input string <b>authenticator</b>. Returns it
3076 * if success, else returns NULL. */
3077 char *
3078 alloc_http_authenticator(const char *authenticator)
3080 /* an authenticator in Basic authentication
3081 * is just the string "username:password" */
3082 const size_t authenticator_length = strlen(authenticator);
3083 /* The base64_encode function needs a minimum buffer length
3084 * of 66 bytes. */
3085 const size_t base64_authenticator_length = (authenticator_length/48+1)*66;
3086 char *base64_authenticator = tor_malloc(base64_authenticator_length);
3087 if (base64_encode(base64_authenticator, base64_authenticator_length,
3088 authenticator, authenticator_length) < 0) {
3089 tor_free(base64_authenticator); /* free and set to null */
3090 } else {
3091 /* remove extra \n at end of encoding */
3092 base64_authenticator[strlen(base64_authenticator) - 1] = 0;
3094 return base64_authenticator;
3097 /** Given a socket handle, check whether the local address (sockname) of the
3098 * socket is one that we've connected from before. If so, double-check
3099 * whether our address has changed and we need to generate keys. If we do,
3100 * call init_keys().
3102 static void
3103 client_check_address_changed(int sock)
3105 uint32_t iface_ip, ip_out; /* host order */
3106 struct sockaddr_in out_addr;
3107 socklen_t out_addr_len = (socklen_t) sizeof(out_addr);
3108 uint32_t *ip; /* host order */
3110 if (!last_interface_ip)
3111 get_interface_address(LOG_INFO, &last_interface_ip);
3112 if (!outgoing_addrs)
3113 outgoing_addrs = smartlist_create();
3115 if (getsockname(sock, (struct sockaddr*)&out_addr, &out_addr_len)<0) {
3116 int e = tor_socket_errno(sock);
3117 log_warn(LD_NET, "getsockname() to check for address change failed: %s",
3118 tor_socket_strerror(e));
3119 return;
3122 /* If we've used this address previously, we're okay. */
3123 ip_out = ntohl(out_addr.sin_addr.s_addr);
3124 SMARTLIST_FOREACH(outgoing_addrs, uint32_t*, ip_ptr,
3125 if (*ip_ptr == ip_out) return;
3128 /* Uh-oh. We haven't connected from this address before. Has the interface
3129 * address changed? */
3130 if (get_interface_address(LOG_INFO, &iface_ip)<0)
3131 return;
3132 ip = tor_malloc(sizeof(uint32_t));
3133 *ip = ip_out;
3135 if (iface_ip == last_interface_ip) {
3136 /* Nope, it hasn't changed. Add this address to the list. */
3137 smartlist_add(outgoing_addrs, ip);
3138 } else {
3139 /* The interface changed. We're a client, so we need to regenerate our
3140 * keys. First, reset the state. */
3141 log(LOG_NOTICE, LD_NET, "Our IP address has changed. Rotating keys...");
3142 last_interface_ip = iface_ip;
3143 SMARTLIST_FOREACH(outgoing_addrs, void*, ip_ptr, tor_free(ip_ptr));
3144 smartlist_clear(outgoing_addrs);
3145 smartlist_add(outgoing_addrs, ip);
3146 /* Okay, now change our keys. */
3147 ip_address_changed(1);
3151 /** Some systems have limited system buffers for recv and xmit on
3152 * sockets allocated in a virtual server or similar environment. For a Tor
3153 * server this can produce the "Error creating network socket: No buffer
3154 * space available" error once all available TCP buffer space is consumed.
3155 * This method will attempt to constrain the buffers allocated for the socket
3156 * to the desired size to stay below system TCP buffer limits.
3158 static void
3159 set_constrained_socket_buffers(int sock, int size)
3161 void *sz = (void*)&size;
3162 socklen_t sz_sz = (socklen_t) sizeof(size);
3163 if (setsockopt(sock, SOL_SOCKET, SO_SNDBUF, sz, sz_sz) < 0) {
3164 int e = tor_socket_errno(sock);
3165 log_warn(LD_NET, "setsockopt() to constrain send "
3166 "buffer to %d bytes failed: %s", size, tor_socket_strerror(e));
3168 if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF, sz, sz_sz) < 0) {
3169 int e = tor_socket_errno(sock);
3170 log_warn(LD_NET, "setsockopt() to constrain recv "
3171 "buffer to %d bytes failed: %s", size, tor_socket_strerror(e));
3175 /** Process new bytes that have arrived on conn-\>inbuf.
3177 * This function just passes conn to the connection-specific
3178 * connection_*_process_inbuf() function. It also passes in
3179 * package_partial if wanted.
3181 static int
3182 connection_process_inbuf(connection_t *conn, int package_partial)
3184 tor_assert(conn);
3186 switch (conn->type) {
3187 case CONN_TYPE_OR:
3188 return connection_or_process_inbuf(TO_OR_CONN(conn));
3189 case CONN_TYPE_EXIT:
3190 case CONN_TYPE_AP:
3191 return connection_edge_process_inbuf(TO_EDGE_CONN(conn),
3192 package_partial);
3193 case CONN_TYPE_DIR:
3194 return connection_dir_process_inbuf(TO_DIR_CONN(conn));
3195 case CONN_TYPE_CPUWORKER:
3196 return connection_cpu_process_inbuf(conn);
3197 case CONN_TYPE_CONTROL:
3198 return connection_control_process_inbuf(TO_CONTROL_CONN(conn));
3199 default:
3200 log_err(LD_BUG,"got unexpected conn type %d.", conn->type);
3201 tor_fragile_assert();
3202 return -1;
3206 /** Called whenever we've written data on a connection. */
3207 static int
3208 connection_flushed_some(connection_t *conn)
3210 int r = 0;
3211 tor_assert(!conn->in_flushed_some);
3212 conn->in_flushed_some = 1;
3213 if (conn->type == CONN_TYPE_DIR &&
3214 conn->state == DIR_CONN_STATE_SERVER_WRITING) {
3215 r = connection_dirserv_flushed_some(TO_DIR_CONN(conn));
3216 } else if (conn->type == CONN_TYPE_OR) {
3217 r = connection_or_flushed_some(TO_OR_CONN(conn));
3219 conn->in_flushed_some = 0;
3220 return r;
3223 /** We just finished flushing bytes from conn-\>outbuf, and there
3224 * are no more bytes remaining.
3226 * This function just passes conn to the connection-specific
3227 * connection_*_finished_flushing() function.
3229 static int
3230 connection_finished_flushing(connection_t *conn)
3232 tor_assert(conn);
3234 /* If the connection is closed, don't try to do anything more here. */
3235 if (CONN_IS_CLOSED(conn))
3236 return 0;
3238 // log_fn(LOG_DEBUG,"entered. Socket %u.", conn->s);
3240 switch (conn->type) {
3241 case CONN_TYPE_OR:
3242 return connection_or_finished_flushing(TO_OR_CONN(conn));
3243 case CONN_TYPE_AP:
3244 case CONN_TYPE_EXIT:
3245 return connection_edge_finished_flushing(TO_EDGE_CONN(conn));
3246 case CONN_TYPE_DIR:
3247 return connection_dir_finished_flushing(TO_DIR_CONN(conn));
3248 case CONN_TYPE_CPUWORKER:
3249 return connection_cpu_finished_flushing(conn);
3250 case CONN_TYPE_CONTROL:
3251 return connection_control_finished_flushing(TO_CONTROL_CONN(conn));
3252 default:
3253 log_err(LD_BUG,"got unexpected conn type %d.", conn->type);
3254 tor_fragile_assert();
3255 return -1;
3259 /** Called when our attempt to connect() to another server has just
3260 * succeeded.
3262 * This function just passes conn to the connection-specific
3263 * connection_*_finished_connecting() function.
3265 static int
3266 connection_finished_connecting(connection_t *conn)
3268 tor_assert(conn);
3269 switch (conn->type)
3271 case CONN_TYPE_OR:
3272 return connection_or_finished_connecting(TO_OR_CONN(conn));
3273 case CONN_TYPE_EXIT:
3274 return connection_edge_finished_connecting(TO_EDGE_CONN(conn));
3275 case CONN_TYPE_DIR:
3276 return connection_dir_finished_connecting(TO_DIR_CONN(conn));
3277 default:
3278 log_err(LD_BUG,"got unexpected conn type %d.", conn->type);
3279 tor_fragile_assert();
3280 return -1;
3284 /** Callback: invoked when a connection reaches an EOF event. */
3285 static int
3286 connection_reached_eof(connection_t *conn)
3288 switch (conn->type) {
3289 case CONN_TYPE_OR:
3290 return connection_or_reached_eof(TO_OR_CONN(conn));
3291 case CONN_TYPE_AP:
3292 case CONN_TYPE_EXIT:
3293 return connection_edge_reached_eof(TO_EDGE_CONN(conn));
3294 case CONN_TYPE_DIR:
3295 return connection_dir_reached_eof(TO_DIR_CONN(conn));
3296 case CONN_TYPE_CPUWORKER:
3297 return connection_cpu_reached_eof(conn);
3298 case CONN_TYPE_CONTROL:
3299 return connection_control_reached_eof(TO_CONTROL_CONN(conn));
3300 default:
3301 log_err(LD_BUG,"got unexpected conn type %d.", conn->type);
3302 tor_fragile_assert();
3303 return -1;
3307 /** Log how many bytes are used by buffers of different kinds and sizes. */
3308 void
3309 connection_dump_buffer_mem_stats(int severity)
3311 uint64_t used_by_type[_CONN_TYPE_MAX+1];
3312 uint64_t alloc_by_type[_CONN_TYPE_MAX+1];
3313 int n_conns_by_type[_CONN_TYPE_MAX+1];
3314 uint64_t total_alloc = 0;
3315 uint64_t total_used = 0;
3316 int i;
3317 smartlist_t *conns = get_connection_array();
3319 memset(used_by_type, 0, sizeof(used_by_type));
3320 memset(alloc_by_type, 0, sizeof(alloc_by_type));
3321 memset(n_conns_by_type, 0, sizeof(n_conns_by_type));
3323 SMARTLIST_FOREACH(conns, connection_t *, c,
3325 int tp = c->type;
3326 ++n_conns_by_type[tp];
3327 if (c->inbuf) {
3328 used_by_type[tp] += buf_datalen(c->inbuf);
3329 alloc_by_type[tp] += buf_allocation(c->inbuf);
3331 if (c->outbuf) {
3332 used_by_type[tp] += buf_datalen(c->outbuf);
3333 alloc_by_type[tp] += buf_allocation(c->outbuf);
3336 for (i=0; i <= _CONN_TYPE_MAX; ++i) {
3337 total_used += used_by_type[i];
3338 total_alloc += alloc_by_type[i];
3341 log(severity, LD_GENERAL,
3342 "In buffers for %d connections: "U64_FORMAT" used/"U64_FORMAT" allocated",
3343 smartlist_len(conns),
3344 U64_PRINTF_ARG(total_used), U64_PRINTF_ARG(total_alloc));
3345 for (i=_CONN_TYPE_MIN; i <= _CONN_TYPE_MAX; ++i) {
3346 if (!n_conns_by_type[i])
3347 continue;
3348 log(severity, LD_GENERAL,
3349 " For %d %s connections: "U64_FORMAT" used/"U64_FORMAT" allocated",
3350 n_conns_by_type[i], conn_type_to_string(i),
3351 U64_PRINTF_ARG(used_by_type[i]), U64_PRINTF_ARG(alloc_by_type[i]));
3355 /** Verify that connection <b>conn</b> has all of its invariants
3356 * correct. Trigger an assert if anything is invalid.
3358 void
3359 assert_connection_ok(connection_t *conn, time_t now)
3361 (void) now; /* XXXX unused. */
3362 tor_assert(conn);
3363 tor_assert(conn->type >= _CONN_TYPE_MIN);
3364 tor_assert(conn->type <= _CONN_TYPE_MAX);
3365 switch (conn->type) {
3366 case CONN_TYPE_OR:
3367 tor_assert(conn->magic == OR_CONNECTION_MAGIC);
3368 break;
3369 case CONN_TYPE_AP:
3370 case CONN_TYPE_EXIT:
3371 tor_assert(conn->magic == EDGE_CONNECTION_MAGIC);
3372 break;
3373 case CONN_TYPE_DIR:
3374 tor_assert(conn->magic == DIR_CONNECTION_MAGIC);
3375 break;
3376 case CONN_TYPE_CONTROL:
3377 tor_assert(conn->magic == CONTROL_CONNECTION_MAGIC);
3378 break;
3379 default:
3380 tor_assert(conn->magic == BASE_CONNECTION_MAGIC);
3381 break;
3384 if (conn->linked_conn) {
3385 tor_assert(conn->linked_conn->linked_conn == conn);
3386 tor_assert(conn->linked);
3388 if (conn->linked)
3389 tor_assert(conn->s < 0);
3391 if (conn->outbuf_flushlen > 0) {
3392 tor_assert(connection_is_writing(conn) || conn->write_blocked_on_bw ||
3393 (CONN_IS_EDGE(conn) && TO_EDGE_CONN(conn)->edge_blocked_on_circ));
3396 if (conn->hold_open_until_flushed)
3397 tor_assert(conn->marked_for_close);
3399 /* XXXX check: read_blocked_on_bw, write_blocked_on_bw, s, conn_array_index,
3400 * marked_for_close. */
3402 /* buffers */
3403 if (!connection_is_listener(conn)) {
3404 assert_buf_ok(conn->inbuf);
3405 assert_buf_ok(conn->outbuf);
3408 if (conn->type == CONN_TYPE_OR) {
3409 or_connection_t *or_conn = TO_OR_CONN(conn);
3410 if (conn->state == OR_CONN_STATE_OPEN) {
3411 /* tor_assert(conn->bandwidth > 0); */
3412 /* the above isn't necessarily true: if we just did a TLS
3413 * handshake but we didn't recognize the other peer, or it
3414 * gave a bad cert/etc, then we won't have assigned bandwidth,
3415 * yet it will be open. -RD
3417 // tor_assert(conn->read_bucket >= 0);
3419 // tor_assert(conn->addr && conn->port);
3420 tor_assert(conn->address);
3421 if (conn->state > OR_CONN_STATE_PROXY_HANDSHAKING)
3422 tor_assert(or_conn->tls);
3425 if (CONN_IS_EDGE(conn)) {
3426 edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
3427 if (edge_conn->chosen_exit_optional || edge_conn->chosen_exit_retries) {
3428 tor_assert(conn->type == CONN_TYPE_AP);
3429 tor_assert(edge_conn->chosen_exit_name);
3432 /* XXX unchecked: package window, deliver window. */
3433 if (conn->type == CONN_TYPE_AP) {
3435 tor_assert(edge_conn->socks_request);
3436 if (conn->state == AP_CONN_STATE_OPEN) {
3437 tor_assert(edge_conn->socks_request->has_finished);
3438 if (!conn->marked_for_close) {
3439 tor_assert(edge_conn->cpath_layer);
3440 assert_cpath_layer_ok(edge_conn->cpath_layer);
3444 if (conn->type == CONN_TYPE_EXIT) {
3445 tor_assert(conn->purpose == EXIT_PURPOSE_CONNECT ||
3446 conn->purpose == EXIT_PURPOSE_RESOLVE);
3448 } else if (conn->type == CONN_TYPE_DIR) {
3449 } else {
3450 /* Purpose is only used for dir and exit types currently */
3451 tor_assert(!conn->purpose);
3454 switch (conn->type)
3456 case CONN_TYPE_OR_LISTENER:
3457 case CONN_TYPE_AP_LISTENER:
3458 case CONN_TYPE_AP_TRANS_LISTENER:
3459 case CONN_TYPE_AP_NATD_LISTENER:
3460 case CONN_TYPE_DIR_LISTENER:
3461 case CONN_TYPE_CONTROL_LISTENER:
3462 case CONN_TYPE_AP_DNS_LISTENER:
3463 tor_assert(conn->state == LISTENER_STATE_READY);
3464 break;
3465 case CONN_TYPE_OR:
3466 tor_assert(conn->state >= _OR_CONN_STATE_MIN);
3467 tor_assert(conn->state <= _OR_CONN_STATE_MAX);
3468 tor_assert(TO_OR_CONN(conn)->n_circuits >= 0);
3469 break;
3470 case CONN_TYPE_EXIT:
3471 tor_assert(conn->state >= _EXIT_CONN_STATE_MIN);
3472 tor_assert(conn->state <= _EXIT_CONN_STATE_MAX);
3473 tor_assert(conn->purpose >= _EXIT_PURPOSE_MIN);
3474 tor_assert(conn->purpose <= _EXIT_PURPOSE_MAX);
3475 break;
3476 case CONN_TYPE_AP:
3477 tor_assert(conn->state >= _AP_CONN_STATE_MIN);
3478 tor_assert(conn->state <= _AP_CONN_STATE_MAX);
3479 tor_assert(TO_EDGE_CONN(conn)->socks_request);
3480 break;
3481 case CONN_TYPE_DIR:
3482 tor_assert(conn->state >= _DIR_CONN_STATE_MIN);
3483 tor_assert(conn->state <= _DIR_CONN_STATE_MAX);
3484 tor_assert(conn->purpose >= _DIR_PURPOSE_MIN);
3485 tor_assert(conn->purpose <= _DIR_PURPOSE_MAX);
3486 break;
3487 case CONN_TYPE_CPUWORKER:
3488 tor_assert(conn->state >= _CPUWORKER_STATE_MIN);
3489 tor_assert(conn->state <= _CPUWORKER_STATE_MAX);
3490 break;
3491 case CONN_TYPE_CONTROL:
3492 tor_assert(conn->state >= _CONTROL_CONN_STATE_MIN);
3493 tor_assert(conn->state <= _CONTROL_CONN_STATE_MAX);
3494 break;
3495 default:
3496 tor_assert(0);