Document the purpose argument of circuit_find_to_cannibalize
[tor/rransom.git] / src / or / connection.c
blobca688052fc19cafe68b00c51c830aa42a14473f1
1 /* Copyright (c) 2001 Matej Pfajfar.
2 * Copyright (c) 2001-2004, Roger Dingledine.
3 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
4 * Copyright (c) 2007-2008, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
6 /* $Id$ */
7 const char connection_c_id[] =
8 "$Id$";
10 /**
11 * \file connection.c
12 * \brief General high-level functions to handle reading and writing
13 * on connections.
14 **/
16 #include "or.h"
18 static connection_t *connection_create_listener(
19 struct sockaddr *listensockaddr, int type,
20 char* address);
21 static void connection_init(time_t now, connection_t *conn, int type,
22 int socket_family);
23 static int connection_init_accepted_conn(connection_t *conn,
24 uint8_t listener_type);
25 static int connection_handle_listener_read(connection_t *conn, int new_type);
26 static int connection_read_bucket_should_increase(or_connection_t *conn);
27 static int connection_finished_flushing(connection_t *conn);
28 static int connection_flushed_some(connection_t *conn);
29 static int connection_finished_connecting(connection_t *conn);
30 static int connection_reached_eof(connection_t *conn);
31 static int connection_read_to_buf(connection_t *conn, int *max_to_read,
32 int *socket_error);
33 static int connection_process_inbuf(connection_t *conn, int package_partial);
34 static void client_check_address_changed(int sock);
35 static void set_constrained_socket_buffers(int sock, int size);
37 static uint32_t last_interface_ip = 0;
38 static smartlist_t *outgoing_addrs = NULL;
40 /**************************************************************/
42 /**
43 * Return the human-readable name for the connection type <b>type</b>
45 const char *
46 conn_type_to_string(int type)
48 static char buf[64];
49 switch (type) {
50 case CONN_TYPE_OR_LISTENER: return "OR listener";
51 case CONN_TYPE_OR: return "OR";
52 case CONN_TYPE_EXIT: return "Exit";
53 case CONN_TYPE_AP_LISTENER: return "Socks listener";
54 case CONN_TYPE_AP_TRANS_LISTENER:
55 return "Transparent pf/netfilter listener";
56 case CONN_TYPE_AP_NATD_LISTENER: return "Transparent natd listener";
57 case CONN_TYPE_AP_DNS_LISTENER: return "DNS listener";
58 case CONN_TYPE_AP: return "Socks";
59 case CONN_TYPE_DIR_LISTENER: return "Directory listener";
60 case CONN_TYPE_DIR: return "Directory";
61 case CONN_TYPE_CPUWORKER: return "CPU worker";
62 case CONN_TYPE_CONTROL_LISTENER: return "Control listener";
63 case CONN_TYPE_CONTROL: return "Control";
64 default:
65 log_warn(LD_BUG, "unknown connection type %d", type);
66 tor_snprintf(buf, sizeof(buf), "unknown [%d]", type);
67 return buf;
71 /**
72 * Return the human-readable name for the connection state <b>state</b>
73 * for the connection type <b>type</b>
75 const char *
76 conn_state_to_string(int type, int state)
78 static char buf[96];
79 switch (type) {
80 case CONN_TYPE_OR_LISTENER:
81 case CONN_TYPE_AP_LISTENER:
82 case CONN_TYPE_AP_TRANS_LISTENER:
83 case CONN_TYPE_AP_NATD_LISTENER:
84 case CONN_TYPE_AP_DNS_LISTENER:
85 case CONN_TYPE_DIR_LISTENER:
86 case CONN_TYPE_CONTROL_LISTENER:
87 if (state == LISTENER_STATE_READY)
88 return "ready";
89 break;
90 case CONN_TYPE_OR:
91 switch (state) {
92 case OR_CONN_STATE_CONNECTING: return "connect()ing";
93 case OR_CONN_STATE_PROXY_FLUSHING: return "proxy flushing";
94 case OR_CONN_STATE_PROXY_READING: return "proxy reading";
95 case OR_CONN_STATE_TLS_HANDSHAKING: return "handshaking (TLS)";
96 case OR_CONN_STATE_TLS_CLIENT_RENEGOTIATING:
97 return "renegotiating (TLS)";
98 case OR_CONN_STATE_TLS_SERVER_RENEGOTIATING:
99 return "waiting for renegotiation (TLS)";
100 case OR_CONN_STATE_OR_HANDSHAKING: return "handshaking (Tor)";
101 case OR_CONN_STATE_OPEN: return "open";
103 break;
104 case CONN_TYPE_EXIT:
105 switch (state) {
106 case EXIT_CONN_STATE_RESOLVING: return "waiting for dest info";
107 case EXIT_CONN_STATE_CONNECTING: return "connecting";
108 case EXIT_CONN_STATE_OPEN: return "open";
109 case EXIT_CONN_STATE_RESOLVEFAILED: return "resolve failed";
111 break;
112 case CONN_TYPE_AP:
113 switch (state) {
114 case AP_CONN_STATE_SOCKS_WAIT: return "waiting for socks info";
115 case AP_CONN_STATE_NATD_WAIT: return "waiting for natd dest info";
116 case AP_CONN_STATE_RENDDESC_WAIT: return "waiting for rendezvous desc";
117 case AP_CONN_STATE_CONTROLLER_WAIT: return "waiting for controller";
118 case AP_CONN_STATE_CIRCUIT_WAIT: return "waiting for circuit";
119 case AP_CONN_STATE_CONNECT_WAIT: return "waiting for connect response";
120 case AP_CONN_STATE_RESOLVE_WAIT: return "waiting for resolve response";
121 case AP_CONN_STATE_OPEN: return "open";
123 break;
124 case CONN_TYPE_DIR:
125 switch (state) {
126 case DIR_CONN_STATE_CONNECTING: return "connecting";
127 case DIR_CONN_STATE_CLIENT_SENDING: return "client sending";
128 case DIR_CONN_STATE_CLIENT_READING: return "client reading";
129 case DIR_CONN_STATE_CLIENT_FINISHED: return "client finished";
130 case DIR_CONN_STATE_SERVER_COMMAND_WAIT: return "waiting for command";
131 case DIR_CONN_STATE_SERVER_WRITING: return "writing";
133 break;
134 case CONN_TYPE_CPUWORKER:
135 switch (state) {
136 case CPUWORKER_STATE_IDLE: return "idle";
137 case CPUWORKER_STATE_BUSY_ONION: return "busy with onion";
139 break;
140 case CONN_TYPE_CONTROL:
141 switch (state) {
142 case CONTROL_CONN_STATE_OPEN: return "open (protocol v1)";
143 case CONTROL_CONN_STATE_NEEDAUTH:
144 return "waiting for authentication (protocol v1)";
146 break;
149 log_warn(LD_BUG, "unknown connection state %d (type %d)", state, type);
150 tor_snprintf(buf, sizeof(buf),
151 "unknown state [%d] on unknown [%s] connection",
152 state, conn_type_to_string(type));
153 return buf;
156 dir_connection_t *
157 dir_connection_new(int socket_family)
159 dir_connection_t *dir_conn = tor_malloc_zero(sizeof(dir_connection_t));
160 connection_init(time(NULL), TO_CONN(dir_conn), CONN_TYPE_DIR, socket_family);
161 return dir_conn;
163 or_connection_t *
164 or_connection_new(int socket_family)
166 or_connection_t *or_conn = tor_malloc_zero(sizeof(or_connection_t));
167 time_t now = time(NULL);
168 connection_init(now, TO_CONN(or_conn), CONN_TYPE_OR, socket_family);
170 or_conn->timestamp_last_added_nonpadding = time(NULL);
171 or_conn->next_circ_id = crypto_rand_int(1<<15);
173 return or_conn;
175 edge_connection_t *
176 edge_connection_new(int type, int socket_family)
178 edge_connection_t *edge_conn = tor_malloc_zero(sizeof(edge_connection_t));
179 tor_assert(type == CONN_TYPE_EXIT || type == CONN_TYPE_AP);
180 connection_init(time(NULL), TO_CONN(edge_conn), type, socket_family);
181 if (type == CONN_TYPE_AP)
182 edge_conn->socks_request = tor_malloc_zero(sizeof(socks_request_t));
183 return edge_conn;
185 control_connection_t *
186 control_connection_new(int socket_family)
188 control_connection_t *control_conn =
189 tor_malloc_zero(sizeof(control_connection_t));
190 connection_init(time(NULL),
191 TO_CONN(control_conn), CONN_TYPE_CONTROL, socket_family);
192 return control_conn;
195 connection_t *
196 connection_new(int type, int socket_family)
198 switch (type) {
199 case CONN_TYPE_OR:
200 return TO_CONN(or_connection_new(socket_family));
202 case CONN_TYPE_EXIT:
203 case CONN_TYPE_AP:
204 return TO_CONN(edge_connection_new(type, socket_family));
206 case CONN_TYPE_DIR:
207 return TO_CONN(dir_connection_new(socket_family));
209 case CONN_TYPE_CONTROL:
210 return TO_CONN(control_connection_new(socket_family));
212 default: {
213 connection_t *conn = tor_malloc_zero(sizeof(connection_t));
214 connection_init(time(NULL), conn, type, socket_family);
215 return conn;
220 /** Initializes conn. (you must call connection_add() to link it into the main
221 * array).
223 * Set conn-\>type to <b>type</b>. Set conn-\>s and conn-\>conn_array_index to
224 * -1 to signify they are not yet assigned.
226 * If conn is not a listener type, allocate buffers for it. If it's
227 * an AP type, allocate space to store the socks_request.
229 * Assign a pseudorandom next_circ_id between 0 and 2**15.
231 * Initialize conn's timestamps to now.
233 static void
234 connection_init(time_t now, connection_t *conn, int type, int socket_family)
236 static uint64_t n_connections_allocated = 1;
238 switch (type) {
239 case CONN_TYPE_OR:
240 conn->magic = OR_CONNECTION_MAGIC;
241 break;
242 case CONN_TYPE_EXIT:
243 case CONN_TYPE_AP:
244 conn->magic = EDGE_CONNECTION_MAGIC;
245 break;
246 case CONN_TYPE_DIR:
247 conn->magic = DIR_CONNECTION_MAGIC;
248 break;
249 case CONN_TYPE_CONTROL:
250 conn->magic = CONTROL_CONNECTION_MAGIC;
251 break;
252 default:
253 conn->magic = BASE_CONNECTION_MAGIC;
254 break;
257 conn->s = -1; /* give it a default of 'not used' */
258 conn->conn_array_index = -1; /* also default to 'not used' */
259 conn->global_identifier = n_connections_allocated++;
261 conn->type = type;
262 conn->socket_family = socket_family;
263 if (!connection_is_listener(conn)) { /* listeners never use their buf */
264 conn->inbuf = buf_new();
265 conn->outbuf = buf_new();
268 conn->timestamp_created = now;
269 conn->timestamp_lastread = now;
270 conn->timestamp_lastwritten = now;
273 /** Create a link between <b>conn_a</b> and <b>conn_b</b>. */
274 void
275 connection_link_connections(connection_t *conn_a, connection_t *conn_b)
277 tor_assert(conn_a->s < 0);
278 tor_assert(conn_b->s < 0);
280 conn_a->linked = 1;
281 conn_b->linked = 1;
282 conn_a->linked_conn = conn_b;
283 conn_b->linked_conn = conn_a;
286 /** Tell libevent that we don't care about <b>conn</b> any more. */
287 void
288 connection_unregister_events(connection_t *conn)
290 if (conn->read_event) {
291 if (event_del(conn->read_event))
292 log_warn(LD_BUG, "Error removing read event for %d", conn->s);
293 tor_free(conn->read_event);
295 if (conn->write_event) {
296 if (event_del(conn->write_event))
297 log_warn(LD_BUG, "Error removing write event for %d", conn->s);
298 tor_free(conn->write_event);
300 if (conn->dns_server_port) {
301 dnsserv_close_listener(conn);
305 /** Deallocate memory used by <b>conn</b>. Deallocate its buffers if
306 * necessary, close its socket if necessary, and mark the directory as dirty
307 * if <b>conn</b> is an OR or OP connection.
309 static void
310 _connection_free(connection_t *conn)
312 void *mem;
313 size_t memlen;
314 switch (conn->type) {
315 case CONN_TYPE_OR:
316 tor_assert(conn->magic == OR_CONNECTION_MAGIC);
317 mem = TO_OR_CONN(conn);
318 memlen = sizeof(or_connection_t);
319 break;
320 case CONN_TYPE_AP:
321 case CONN_TYPE_EXIT:
322 tor_assert(conn->magic == EDGE_CONNECTION_MAGIC);
323 mem = TO_EDGE_CONN(conn);
324 memlen = sizeof(edge_connection_t);
325 break;
326 case CONN_TYPE_DIR:
327 tor_assert(conn->magic == DIR_CONNECTION_MAGIC);
328 mem = TO_DIR_CONN(conn);
329 memlen = sizeof(dir_connection_t);
330 break;
331 case CONN_TYPE_CONTROL:
332 tor_assert(conn->magic == CONTROL_CONNECTION_MAGIC);
333 mem = TO_CONTROL_CONN(conn);
334 memlen = sizeof(control_connection_t);
335 break;
336 default:
337 tor_assert(conn->magic == BASE_CONNECTION_MAGIC);
338 mem = conn;
339 memlen = sizeof(connection_t);
340 break;
343 if (conn->linked) {
344 log_info(LD_GENERAL, "Freeing linked %s connection [%s] with %d "
345 "bytes on inbuf, %d on outbuf.",
346 conn_type_to_string(conn->type),
347 conn_state_to_string(conn->type, conn->state),
348 (int)buf_datalen(conn->inbuf), (int)buf_datalen(conn->outbuf));
351 if (!connection_is_listener(conn)) {
352 buf_free(conn->inbuf);
353 buf_free(conn->outbuf);
354 } else {
355 if (conn->socket_family == AF_UNIX) {
356 /* For now only control ports can be unix domain sockets
357 * and listeners at the same time */
358 tor_assert(conn->type == CONN_TYPE_CONTROL_LISTENER);
360 if (unlink(conn->address) < 0 && errno != ENOENT) {
361 log_warn(LD_NET, "Could not unlink %s: %s", conn->address,
362 strerror(errno));
367 tor_free(conn->address);
369 if (connection_speaks_cells(conn)) {
370 or_connection_t *or_conn = TO_OR_CONN(conn);
371 if (or_conn->tls) {
372 tor_tls_free(or_conn->tls);
373 or_conn->tls = NULL;
375 if (or_conn->handshake_state) {
376 or_handshake_state_free(or_conn->handshake_state);
377 or_conn->handshake_state = NULL;
379 tor_free(or_conn->nickname);
381 if (CONN_IS_EDGE(conn)) {
382 edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
383 tor_free(edge_conn->chosen_exit_name);
384 if (edge_conn->socks_request) {
385 memset(edge_conn->socks_request, 0xcc, sizeof(socks_request_t));
386 tor_free(edge_conn->socks_request);
388 if (edge_conn->rend_data)
389 rend_data_free(edge_conn->rend_data);
391 if (conn->type == CONN_TYPE_CONTROL) {
392 control_connection_t *control_conn = TO_CONTROL_CONN(conn);
393 tor_free(control_conn->incoming_cmd);
396 tor_free(conn->read_event); /* Probably already freed by connection_free. */
397 tor_free(conn->write_event); /* Probably already freed by connection_free. */
399 if (conn->type == CONN_TYPE_DIR) {
400 dir_connection_t *dir_conn = TO_DIR_CONN(conn);
401 tor_free(dir_conn->requested_resource);
402 if (dir_conn->zlib_state)
403 tor_zlib_free(dir_conn->zlib_state);
404 if (dir_conn->fingerprint_stack) {
405 SMARTLIST_FOREACH(dir_conn->fingerprint_stack, char *, cp, tor_free(cp));
406 smartlist_free(dir_conn->fingerprint_stack);
408 if (dir_conn->cached_dir)
409 cached_dir_decref(dir_conn->cached_dir);
410 if (dir_conn->rend_data)
411 rend_data_free(dir_conn->rend_data);
414 if (conn->s >= 0) {
415 log_debug(LD_NET,"closing fd %d.",conn->s);
416 tor_close_socket(conn->s);
417 conn->s = -1;
420 if (conn->type == CONN_TYPE_OR &&
421 !tor_digest_is_zero(TO_OR_CONN(conn)->identity_digest)) {
422 log_warn(LD_BUG, "called on OR conn with non-zeroed identity_digest");
423 connection_or_remove_from_identity_map(TO_OR_CONN(conn));
426 memset(conn, 0xAA, memlen); /* poison memory */
427 tor_free(mem);
430 /** Make sure <b>conn</b> isn't in any of the global conn lists; then free it.
432 void
433 connection_free(connection_t *conn)
435 tor_assert(conn);
436 tor_assert(!connection_is_on_closeable_list(conn));
437 tor_assert(!connection_in_array(conn));
438 if (conn->linked_conn) {
439 log_err(LD_BUG, "Called with conn->linked_conn still set.");
440 tor_fragile_assert();
441 conn->linked_conn->linked_conn = NULL;
442 if (! conn->linked_conn->marked_for_close &&
443 conn->linked_conn->reading_from_linked_conn)
444 connection_start_reading(conn->linked_conn);
445 conn->linked_conn = NULL;
447 if (connection_speaks_cells(conn)) {
448 if (!tor_digest_is_zero(TO_OR_CONN(conn)->identity_digest)) {
449 connection_or_remove_from_identity_map(TO_OR_CONN(conn));
452 if (conn->type == CONN_TYPE_CONTROL) {
453 TO_CONTROL_CONN(conn)->event_mask = 0;
454 control_update_global_event_mask();
456 connection_unregister_events(conn);
457 _connection_free(conn);
460 /** Call _connection_free() on every connection in our array, and release all
461 * storage helpd by connection.c. This is used by cpuworkers and dnsworkers
462 * when they fork, so they don't keep resources held open (especially
463 * sockets).
465 * Don't do the checks in connection_free(), because they will
466 * fail.
468 void
469 connection_free_all(void)
471 smartlist_t *conns = get_connection_array();
473 /* We don't want to log any messages to controllers. */
474 SMARTLIST_FOREACH(conns, connection_t *, conn,
475 if (conn->type == CONN_TYPE_CONTROL)
476 TO_CONTROL_CONN(conn)->event_mask = 0);
478 control_update_global_event_mask();
480 /* Unlink everything from the identity map. */
481 connection_or_clear_identity_map();
483 SMARTLIST_FOREACH(conns, connection_t *, conn, _connection_free(conn));
485 if (outgoing_addrs) {
486 SMARTLIST_FOREACH(outgoing_addrs, void*, addr, tor_free(addr));
487 smartlist_free(outgoing_addrs);
488 outgoing_addrs = NULL;
492 /** Do any cleanup needed:
493 * - Directory conns that failed to fetch a rendezvous descriptor
494 * need to inform pending rendezvous streams.
495 * - OR conns need to call rep_hist_note_*() to record status.
496 * - AP conns need to send a socks reject if necessary.
497 * - Exit conns need to call connection_dns_remove() if necessary.
498 * - AP and Exit conns need to send an end cell if they can.
499 * - DNS conns need to fail any resolves that are pending on them.
500 * - OR and edge connections need to be unlinked from circuits.
502 void
503 connection_about_to_close_connection(connection_t *conn)
505 circuit_t *circ;
506 dir_connection_t *dir_conn;
507 or_connection_t *or_conn;
508 edge_connection_t *edge_conn;
509 time_t now = time(NULL);
511 tor_assert(conn->marked_for_close);
513 if (CONN_IS_EDGE(conn)) {
514 edge_conn = TO_EDGE_CONN(conn);
515 if (!edge_conn->edge_has_sent_end) {
516 log_warn(LD_BUG, "(Harmless.) Edge connection (marked at %s:%d) "
517 "hasn't sent end yet?",
518 conn->marked_for_close_file, conn->marked_for_close);
519 tor_fragile_assert();
523 switch (conn->type) {
524 case CONN_TYPE_DIR:
525 dir_conn = TO_DIR_CONN(conn);
526 if (conn->state < DIR_CONN_STATE_CLIENT_FINISHED) {
527 /* It's a directory connection and connecting or fetching
528 * failed: forget about this router, and maybe try again. */
529 connection_dir_request_failed(dir_conn);
531 if (conn->purpose == DIR_PURPOSE_FETCH_RENDDESC && dir_conn->rend_data) {
532 /* Give it a try. However, there is no re-fetching for v0 rend
533 * descriptors; if the response is empty or the descriptor is
534 * unusable, close pending connections (unless a v2 request is
535 * still in progress). */
536 rend_client_desc_trynow(dir_conn->rend_data->onion_address, 0);
538 /* If we were trying to fetch a v2 rend desc and did not succeed,
539 * retry as needed. (If a fetch is successful, the connection state
540 * is changed to DIR_PURPOSE_HAS_FETCHED_RENDDESC to mark that
541 * refetching is unnecessary.) */
542 if (conn->purpose == DIR_PURPOSE_FETCH_RENDDESC_V2 &&
543 dir_conn->rend_data &&
544 strlen(dir_conn->rend_data->onion_address) ==
545 REND_SERVICE_ID_LEN_BASE32)
546 rend_client_refetch_v2_renddesc(dir_conn->rend_data);
547 break;
548 case CONN_TYPE_OR:
549 or_conn = TO_OR_CONN(conn);
550 /* Remember why we're closing this connection. */
551 if (conn->state != OR_CONN_STATE_OPEN) {
552 /* Inform any pending (not attached) circs that they should
553 * give up. */
554 circuit_n_conn_done(TO_OR_CONN(conn), 0);
555 /* now mark things down as needed */
556 if (connection_or_nonopen_was_started_here(or_conn)) {
557 or_options_t *options = get_options();
558 rep_hist_note_connect_failed(or_conn->identity_digest, now);
559 entry_guard_register_connect_status(or_conn->identity_digest,0,now);
560 if (!options->HttpsProxy)
561 router_set_status(or_conn->identity_digest, 0);
562 if (conn->state >= OR_CONN_STATE_TLS_HANDSHAKING) {
563 int reason = tls_error_to_orconn_end_reason(or_conn->tls_error);
564 control_event_or_conn_status(or_conn, OR_CONN_EVENT_FAILED,
565 reason);
566 /* XXX021 come up with a better string for the first arg -RD */
567 /* What did you have in mind? -NM */
568 if (!authdir_mode_tests_reachability(options))
569 control_event_bootstrap_problem(
570 orconn_end_reason_to_control_string(reason), reason);
573 } else if (conn->hold_open_until_flushed) {
574 /* We only set hold_open_until_flushed when we're intentionally
575 * closing a connection. */
576 rep_hist_note_disconnect(or_conn->identity_digest, now);
577 control_event_or_conn_status(or_conn, OR_CONN_EVENT_CLOSED,
578 tls_error_to_orconn_end_reason(or_conn->tls_error));
579 } else if (or_conn->identity_digest) {
580 rep_hist_note_connection_died(or_conn->identity_digest, now);
581 control_event_or_conn_status(or_conn, OR_CONN_EVENT_CLOSED,
582 tls_error_to_orconn_end_reason(or_conn->tls_error));
584 /* Now close all the attached circuits on it. */
585 circuit_unlink_all_from_or_conn(TO_OR_CONN(conn),
586 END_CIRC_REASON_OR_CONN_CLOSED);
587 break;
588 case CONN_TYPE_AP:
589 edge_conn = TO_EDGE_CONN(conn);
590 if (edge_conn->socks_request->has_finished == 0) {
591 /* since conn gets removed right after this function finishes,
592 * there's no point trying to send back a reply at this point. */
593 log_warn(LD_BUG,"Closing stream (marked at %s:%d) without sending"
594 " back a socks reply.",
595 conn->marked_for_close_file, conn->marked_for_close);
597 if (!edge_conn->end_reason) {
598 log_warn(LD_BUG,"Closing stream (marked at %s:%d) without having"
599 " set end_reason.",
600 conn->marked_for_close_file, conn->marked_for_close);
602 if (edge_conn->dns_server_request) {
603 log_warn(LD_BUG,"Closing stream (marked at %s:%d) without having"
604 " replied to DNS request.",
605 conn->marked_for_close_file, conn->marked_for_close);
606 dnsserv_reject_request(edge_conn);
608 control_event_stream_status(edge_conn, STREAM_EVENT_CLOSED,
609 edge_conn->end_reason);
610 circ = circuit_get_by_edge_conn(edge_conn);
611 if (circ)
612 circuit_detach_stream(circ, edge_conn);
613 break;
614 case CONN_TYPE_EXIT:
615 edge_conn = TO_EDGE_CONN(conn);
616 circ = circuit_get_by_edge_conn(edge_conn);
617 if (circ)
618 circuit_detach_stream(circ, edge_conn);
619 if (conn->state == EXIT_CONN_STATE_RESOLVING) {
620 connection_dns_remove(edge_conn);
622 break;
626 /** Return true iff connection_close_immediate() has been called on this
627 * connection. */
628 #define CONN_IS_CLOSED(c) \
629 ((c)->linked ? ((c)->linked_conn_is_closed) : ((c)->s < 0))
631 /** Close the underlying socket for <b>conn</b>, so we don't try to
632 * flush it. Must be used in conjunction with (right before)
633 * connection_mark_for_close().
635 void
636 connection_close_immediate(connection_t *conn)
638 assert_connection_ok(conn,0);
639 if (CONN_IS_CLOSED(conn)) {
640 log_err(LD_BUG,"Attempt to close already-closed connection.");
641 tor_fragile_assert();
642 return;
644 if (conn->outbuf_flushlen) {
645 log_info(LD_NET,"fd %d, type %s, state %s, %d bytes on outbuf.",
646 conn->s, conn_type_to_string(conn->type),
647 conn_state_to_string(conn->type, conn->state),
648 (int)conn->outbuf_flushlen);
651 connection_unregister_events(conn);
653 if (conn->s >= 0)
654 tor_close_socket(conn->s);
655 conn->s = -1;
656 if (conn->linked)
657 conn->linked_conn_is_closed = 1;
658 if (!connection_is_listener(conn)) {
659 buf_clear(conn->outbuf);
660 conn->outbuf_flushlen = 0;
664 /** Mark <b>conn</b> to be closed next time we loop through
665 * conn_close_if_marked() in main.c. */
666 void
667 _connection_mark_for_close(connection_t *conn, int line, const char *file)
669 assert_connection_ok(conn,0);
670 tor_assert(line);
671 tor_assert(line < 1<<16); /* marked_for_close can only fit a uint16_t. */
672 tor_assert(file);
674 if (conn->marked_for_close) {
675 log(LOG_WARN,LD_BUG,"Duplicate call to connection_mark_for_close at %s:%d"
676 " (first at %s:%d)", file, line, conn->marked_for_close_file,
677 conn->marked_for_close);
678 tor_fragile_assert();
679 return;
682 conn->marked_for_close = line;
683 conn->marked_for_close_file = file;
684 add_connection_to_closeable_list(conn);
686 /* in case we're going to be held-open-til-flushed, reset
687 * the number of seconds since last successful write, so
688 * we get our whole 15 seconds */
689 conn->timestamp_lastwritten = time(NULL);
692 /** Find each connection that has hold_open_until_flushed set to
693 * 1 but hasn't written in the past 15 seconds, and set
694 * hold_open_until_flushed to 0. This means it will get cleaned
695 * up in the next loop through close_if_marked() in main.c.
697 void
698 connection_expire_held_open(void)
700 time_t now;
701 smartlist_t *conns = get_connection_array();
703 now = time(NULL);
705 SMARTLIST_FOREACH(conns, connection_t *, conn,
707 /* If we've been holding the connection open, but we haven't written
708 * for 15 seconds...
710 if (conn->hold_open_until_flushed) {
711 tor_assert(conn->marked_for_close);
712 if (now - conn->timestamp_lastwritten >= 15) {
713 int severity;
714 if (conn->type == CONN_TYPE_EXIT ||
715 (conn->type == CONN_TYPE_DIR &&
716 conn->purpose == DIR_PURPOSE_SERVER))
717 severity = LOG_INFO;
718 else
719 severity = LOG_NOTICE;
720 log_fn(severity, LD_NET,
721 "Giving up on marked_for_close conn that's been flushing "
722 "for 15s (fd %d, type %s, state %s).",
723 conn->s, conn_type_to_string(conn->type),
724 conn_state_to_string(conn->type, conn->state));
725 conn->hold_open_until_flushed = 0;
731 /** Create an AF_INET listenaddr struct.
732 * <b>listenaddress</b> provides the host and optionally the port information
733 * for the new structure. If no port is provided in <b>listenaddress</b> then
734 * <b>listenport</b> is used.
736 * If not NULL <b>readable_addrress</b> will contain a copy of the host part of
737 * <b>listenaddress</b>.
739 * The listenaddr struct has to be freed by the caller.
741 static struct sockaddr_in *
742 create_inet_sockaddr(const char *listenaddress, uint16_t listenport,
743 char **readable_address) {
744 struct sockaddr_in *listenaddr = NULL;
745 uint32_t addr;
746 uint16_t usePort = 0;
748 if (parse_addr_port(LOG_WARN,
749 listenaddress, readable_address, &addr, &usePort)<0) {
750 log_warn(LD_CONFIG,
751 "Error parsing/resolving ListenAddress %s", listenaddress);
752 goto err;
754 if (usePort==0)
755 usePort = listenport;
757 listenaddr = tor_malloc_zero(sizeof(struct sockaddr_in));
758 listenaddr->sin_addr.s_addr = htonl(addr);
759 listenaddr->sin_family = AF_INET;
760 listenaddr->sin_port = htons((uint16_t) usePort);
762 return listenaddr;
764 err:
765 tor_free(listenaddr);
766 return NULL;
769 #ifdef HAVE_SYS_UN_H
770 /** Create an AF_UNIX listenaddr struct.
771 * <b>listenaddress</b> provides the path to the unix socket.
773 * Eventually <b>listenaddress</b> will also optionally contain user, group,
774 * and file permissions for the new socket. But not yet. XXX
775 * Also, since we do not create the socket here the information doesn't help
776 * here.
778 * If not NULL <b>readable_addrress</b> will contain a copy of the path part of
779 * <b>listenaddress</b>.
781 * The listenaddr struct has to be freed by the caller.
783 static struct sockaddr_un *
784 create_unix_sockaddr(const char *listenaddress, char **readable_address)
786 struct sockaddr_un *sockaddr = NULL;
788 sockaddr = tor_malloc_zero(sizeof(struct sockaddr_un));
789 sockaddr->sun_family = AF_UNIX;
790 strncpy(sockaddr->sun_path, listenaddress, sizeof(sockaddr->sun_path));
792 if (readable_address)
793 *readable_address = tor_strdup(listenaddress);
795 return sockaddr;
797 #else
798 static struct sockaddr *
799 create_unix_sockaddr(const char *listenaddress, char **readable_address)
801 (void)listenaddress;
802 (void)readable_address;
803 log_fn(LOG_ERR, LD_BUG,
804 "Unix domain sockets not supported, yet we tried to create one.");
805 tor_assert(0);
807 #endif /* HAVE_SYS_UN_H */
809 static void
810 warn_too_many_conns(void)
812 #define WARN_TOO_MANY_CONNS_INTERVAL (6*60*60)
813 static time_t last_warned = 0;
814 time_t now = time(NULL);
815 int n_conns = get_n_open_sockets();
816 if (last_warned + WARN_TOO_MANY_CONNS_INTERVAL < now) {
817 log_warn(LD_NET,"Failing because we have %d connections already. Please "
818 "raise your ulimit -n.", n_conns);
819 last_warned = now;
821 control_event_general_status(LOG_WARN, "TOO_MANY_CONNECTIONS CURRENT=%d",
822 n_conns);
825 /** Bind a new non-blocking socket listening to the socket described
826 * by <b>listensockaddr</b>.
828 * <b>address</b> is only used for logging purposes and to add the information
829 * to the conn.
831 static connection_t *
832 connection_create_listener(struct sockaddr *listensockaddr, int type,
833 char* address)
835 /*XXXX021 this function should take a socklen too. */
836 connection_t *conn;
837 int s; /* the socket we're going to make */
838 uint16_t usePort = 0;
839 int start_reading = 0;
841 if (get_n_open_sockets() >= get_options()->_ConnLimit-1) {
842 warn_too_many_conns();
843 return NULL;
846 if (listensockaddr->sa_family == AF_INET) {
847 int is_tcp = (type != CONN_TYPE_AP_DNS_LISTENER);
848 #ifndef MS_WINDOWS
849 int one=1;
850 #endif
851 if (is_tcp)
852 start_reading = 1;
854 usePort = ntohs( (uint16_t)
855 ((struct sockaddr_in *)listensockaddr)->sin_port);
857 log_notice(LD_NET, "Opening %s on %s:%d",
858 conn_type_to_string(type), address, usePort);
860 s = tor_open_socket(PF_INET,
861 is_tcp ? SOCK_STREAM : SOCK_DGRAM,
862 is_tcp ? IPPROTO_TCP: IPPROTO_UDP);
863 if (s < 0) {
864 log_warn(LD_NET,"Socket creation failed.");
865 goto err;
868 #ifndef MS_WINDOWS
869 /* REUSEADDR on normal places means you can rebind to the port
870 * right after somebody else has let it go. But REUSEADDR on win32
871 * means you can bind to the port _even when somebody else
872 * already has it bound_. So, don't do that on Win32. */
873 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (void*) &one,
874 (socklen_t)sizeof(one));
875 #endif
877 if (bind(s,listensockaddr,(socklen_t)sizeof(struct sockaddr_in)) < 0) {
878 const char *helpfulhint = "";
879 int e = tor_socket_errno(s);
880 if (ERRNO_IS_EADDRINUSE(e))
881 helpfulhint = ". Is Tor already running?";
882 log_warn(LD_NET, "Could not bind to %s:%u: %s%s", address, usePort,
883 tor_socket_strerror(e), helpfulhint);
884 tor_close_socket(s);
885 goto err;
888 if (is_tcp) {
889 if (listen(s,SOMAXCONN) < 0) {
890 log_warn(LD_NET, "Could not listen on %s:%u: %s", address, usePort,
891 tor_socket_strerror(tor_socket_errno(s)));
892 tor_close_socket(s);
893 goto err;
896 #ifdef HAVE_SYS_UN_H
897 } else if (listensockaddr->sa_family == AF_UNIX) {
898 start_reading = 1;
900 /* For now only control ports can be unix domain sockets
901 * and listeners at the same time */
902 tor_assert(type == CONN_TYPE_CONTROL_LISTENER);
904 log_notice(LD_NET, "Opening %s on %s",
905 conn_type_to_string(type), address);
907 if (unlink(address) < 0 && errno != ENOENT) {
908 log_warn(LD_NET, "Could not unlink %s: %s", address,
909 strerror(errno));
910 goto err;
912 s = tor_open_socket(AF_UNIX, SOCK_STREAM, 0);
913 if (s < 0) {
914 log_warn(LD_NET,"Socket creation failed: %s.", strerror(errno));
915 goto err;
918 if (bind(s, listensockaddr, (socklen_t)sizeof(struct sockaddr_un)) == -1) {
919 log_warn(LD_NET,"Bind to %s failed: %s.", address,
920 tor_socket_strerror(tor_socket_errno(s)));
921 goto err;
924 if (listen(s,SOMAXCONN) < 0) {
925 log_warn(LD_NET, "Could not listen on %s: %s", address,
926 tor_socket_strerror(tor_socket_errno(s)));
927 tor_close_socket(s);
928 goto err;
930 #endif /* HAVE_SYS_UN_H */
931 } else {
932 log_err(LD_BUG,"Got unexpected address family %d.",
933 listensockaddr->sa_family);
934 tor_assert(0);
937 set_socket_nonblocking(s);
939 conn = connection_new(type, listensockaddr->sa_family);
940 conn->socket_family = listensockaddr->sa_family;
941 conn->s = s;
942 conn->address = tor_strdup(address);
943 conn->port = usePort;
945 if (connection_add(conn) < 0) { /* no space, forget it */
946 log_warn(LD_NET,"connection_add for listener failed. Giving up.");
947 connection_free(conn);
948 goto err;
951 log_debug(LD_NET,"%s listening on port %u.",
952 conn_type_to_string(type), usePort);
954 conn->state = LISTENER_STATE_READY;
955 if (start_reading) {
956 connection_start_reading(conn);
957 } else {
958 tor_assert(type == CONN_TYPE_AP_DNS_LISTENER);
959 dnsserv_configure_listener(conn);
962 return conn;
964 err:
965 return NULL;
968 /** Do basic sanity checking on a newly received socket. Return 0
969 * if it looks ok, else return -1. */
970 static int
971 check_sockaddr(struct sockaddr *sa, int len, int level)
973 int ok = 1;
975 if (sa->sa_family == AF_INET) {
976 struct sockaddr_in *sin=(struct sockaddr_in*)sa;
977 if (len != sizeof(struct sockaddr_in)) {
978 log_fn(level, LD_NET, "Length of address not as expected: %d vs %d",
979 len,(int)sizeof(struct sockaddr_in));
980 ok = 0;
982 if (sin->sin_addr.s_addr == 0 || sin->sin_port == 0) {
983 log_fn(level, LD_NET,
984 "Address for new connection has address/port equal to zero.");
985 ok = 0;
987 } else if (sa->sa_family == AF_INET6) {
988 struct sockaddr_in6 *sin6=(struct sockaddr_in6*)sa;
989 if (len != sizeof(struct sockaddr_in6)) {
990 log_fn(level, LD_NET, "Length of address not as expected: %d vs %d",
991 len,(int)sizeof(struct sockaddr_in6));
992 ok = 0;
994 if (tor_mem_is_zero((void*)sin6->sin6_addr.s6_addr, 16) ||
995 sin6->sin6_port == 0) {
996 log_fn(level, LD_NET,
997 "Address for new connection has address/port equal to zero.");
998 ok = 0;
1000 } else {
1001 ok = 0;
1003 return ok ? 0 : -1;
1006 /** The listener connection <b>conn</b> told poll() it wanted to read.
1007 * Call accept() on conn-\>s, and add the new connection if necessary.
1009 static int
1010 connection_handle_listener_read(connection_t *conn, int new_type)
1012 int news; /* the new socket */
1013 connection_t *newconn;
1014 /* information about the remote peer when connecting to other routers */
1015 char addrbuf[256];
1016 struct sockaddr *remote = (struct sockaddr*)addrbuf;
1017 /* length of the remote address. Must be whatever accept() needs. */
1018 socklen_t remotelen = (socklen_t)sizeof(addrbuf);
1019 or_options_t *options = get_options();
1021 tor_assert((size_t)remotelen >= sizeof(struct sockaddr_in));
1022 memset(addrbuf, 0, sizeof(addrbuf));
1024 news = tor_accept_socket(conn->s,remote,&remotelen);
1025 if (news < 0) { /* accept() error */
1026 int e = tor_socket_errno(conn->s);
1027 if (ERRNO_IS_ACCEPT_EAGAIN(e)) {
1028 return 0; /* he hung up before we could accept(). that's fine. */
1029 } else if (ERRNO_IS_ACCEPT_RESOURCE_LIMIT(e)) {
1030 warn_too_many_conns();
1031 return 0;
1033 /* else there was a real error. */
1034 log_warn(LD_NET,"accept() failed: %s. Closing listener.",
1035 tor_socket_strerror(e));
1036 connection_mark_for_close(conn);
1037 return -1;
1039 log_debug(LD_NET,
1040 "Connection accepted on socket %d (child of fd %d).",
1041 news,conn->s);
1043 set_socket_nonblocking(news);
1045 if (options->ConstrainedSockets)
1046 set_constrained_socket_buffers(news, (int)options->ConstrainedSockSize);
1048 if (remote->sa_family != conn->socket_family) {
1049 /* This is annoying, but can apparently happen on some Darwins. */
1050 log_info(LD_BUG, "A listener connection returned a socket with a "
1051 "mismatched family. %s for addr_family %d gave us a socket "
1052 "with address family %d. Dropping.",
1053 conn_type_to_string(conn->type),
1054 (int)conn->socket_family,
1055 (int)remote->sa_family);
1056 tor_close_socket(news);
1057 return 0;
1060 if (conn->socket_family == AF_INET || conn->socket_family == AF_INET6) {
1061 tor_addr_t addr;
1062 uint16_t port;
1063 if (check_sockaddr(remote, remotelen, LOG_INFO)<0) {
1064 log_info(LD_NET,
1065 "accept() returned a strange address; trying getsockname().");
1066 remotelen=sizeof(addrbuf);
1067 memset(addrbuf, 0, sizeof(addrbuf));
1068 if (getsockname(news, remote, &remotelen)<0) {
1069 int e = tor_socket_errno(news);
1070 log_warn(LD_NET, "getsockname() for new connection failed: %s",
1071 tor_socket_strerror(e));
1072 } else {
1073 if (check_sockaddr((struct sockaddr*)addrbuf, remotelen,
1074 LOG_WARN) < 0) {
1075 log_warn(LD_NET,"Something's wrong with this conn. Closing it.");
1076 tor_close_socket(news);
1077 return 0;
1082 /* Duplicate code. XXXX021 */
1083 if (remote->sa_family != conn->socket_family) {
1084 /* This is annoying, but can apparently happen on some Darwins. */
1085 log_info(LD_BUG, "A listener connection returned a socket with a "
1086 "mismatched family. %s for addr_family %d gave us a socket "
1087 "with address family %d. Dropping.",
1088 conn_type_to_string(conn->type),
1089 (int)conn->socket_family,
1090 (int)remote->sa_family);
1091 tor_close_socket(news);
1092 return 0;
1095 tor_addr_from_sockaddr(&addr, remote, &port);
1097 /* process entrance policies here, before we even create the connection */
1098 if (new_type == CONN_TYPE_AP) {
1099 /* check sockspolicy to see if we should accept it */
1100 if (socks_policy_permits_address(&addr) == 0) {
1101 log_notice(LD_APP,
1102 "Denying socks connection from untrusted address %s.",
1103 fmt_addr(&addr));
1104 tor_close_socket(news);
1105 return 0;
1108 if (new_type == CONN_TYPE_DIR) {
1109 /* check dirpolicy to see if we should accept it */
1110 if (dir_policy_permits_address(&addr) == 0) {
1111 log_notice(LD_DIRSERV,"Denying dir connection from address %s.",
1112 fmt_addr(&addr));
1113 tor_close_socket(news);
1114 return 0;
1118 newconn = connection_new(new_type, conn->socket_family);
1119 newconn->s = news;
1121 /* remember the remote address */
1122 tor_addr_copy(&newconn->addr, &addr);
1123 newconn->port = port;
1124 newconn->address = tor_dup_addr(&addr);
1126 } else if (conn->socket_family == AF_UNIX) {
1127 /* For now only control ports can be unix domain sockets
1128 * and listeners at the same time */
1129 tor_assert(conn->type == CONN_TYPE_CONTROL_LISTENER);
1131 newconn = connection_new(new_type, conn->socket_family);
1132 newconn->s = news;
1134 /* remember the remote address -- do we have anything sane to put here? */
1135 tor_addr_make_unspec(&newconn->addr);
1136 newconn->port = 1;
1137 newconn->address = tor_strdup(conn->address);
1138 } else {
1139 tor_assert(0);
1142 if (connection_add(newconn) < 0) { /* no space, forget it */
1143 connection_free(newconn);
1144 return 0; /* no need to tear down the parent */
1147 if (connection_init_accepted_conn(newconn, conn->type) < 0) {
1148 connection_mark_for_close(newconn);
1149 return 0;
1151 return 0;
1154 /** Initialize states for newly accepted connection <b>conn</b>.
1155 * If conn is an OR, start the tls handshake.
1156 * If conn is a transparent AP, get its original destination
1157 * and place it in circuit_wait.
1159 static int
1160 connection_init_accepted_conn(connection_t *conn, uint8_t listener_type)
1162 connection_start_reading(conn);
1164 switch (conn->type) {
1165 case CONN_TYPE_OR:
1166 control_event_or_conn_status(TO_OR_CONN(conn), OR_CONN_EVENT_NEW, 0);
1167 return connection_tls_start_handshake(TO_OR_CONN(conn), 1);
1168 case CONN_TYPE_AP:
1169 switch (listener_type) {
1170 case CONN_TYPE_AP_LISTENER:
1171 conn->state = AP_CONN_STATE_SOCKS_WAIT;
1172 break;
1173 case CONN_TYPE_AP_TRANS_LISTENER:
1174 conn->state = AP_CONN_STATE_CIRCUIT_WAIT;
1175 return connection_ap_process_transparent(TO_EDGE_CONN(conn));
1176 case CONN_TYPE_AP_NATD_LISTENER:
1177 conn->state = AP_CONN_STATE_NATD_WAIT;
1178 break;
1180 break;
1181 case CONN_TYPE_DIR:
1182 conn->purpose = DIR_PURPOSE_SERVER;
1183 conn->state = DIR_CONN_STATE_SERVER_COMMAND_WAIT;
1184 break;
1185 case CONN_TYPE_CONTROL:
1186 conn->state = CONTROL_CONN_STATE_NEEDAUTH;
1187 break;
1189 return 0;
1192 /** Take conn, make a nonblocking socket; try to connect to
1193 * addr:port (they arrive in *host order*). If fail, return -1 and if
1194 * applicable put your best guess about errno into *<b>socket_error</b>.
1195 * Else assign s to conn-\>s: if connected return 1, if EAGAIN return 0.
1197 * address is used to make the logs useful.
1199 * On success, add conn to the list of polled connections.
1202 connection_connect(connection_t *conn, const char *address,
1203 const tor_addr_t *addr, uint16_t port, int *socket_error)
1205 int s, inprogress = 0;
1206 char addrbuf[256];
1207 struct sockaddr *dest_addr = (struct sockaddr*) addrbuf;
1208 socklen_t dest_addr_len;
1209 or_options_t *options = get_options();
1210 int protocol_family;
1212 if (get_n_open_sockets() >= get_options()->_ConnLimit-1) {
1213 warn_too_many_conns();
1214 return -1;
1217 if (tor_addr_family(addr) == AF_INET6)
1218 protocol_family = PF_INET6;
1219 else
1220 protocol_family = PF_INET;
1222 s = tor_open_socket(protocol_family,SOCK_STREAM,IPPROTO_TCP);
1223 if (s < 0) {
1224 *socket_error = tor_socket_errno(-1);
1225 log_warn(LD_NET,"Error creating network socket: %s",
1226 tor_socket_strerror(*socket_error));
1227 return -1;
1230 if (options->OutboundBindAddress) {
1231 struct sockaddr_in ext_addr;
1233 memset(&ext_addr, 0, sizeof(ext_addr));
1234 ext_addr.sin_family = AF_INET;
1235 ext_addr.sin_port = 0;
1236 if (!tor_inet_aton(options->OutboundBindAddress, &ext_addr.sin_addr)) {
1237 log_warn(LD_CONFIG,"Outbound bind address '%s' didn't parse. Ignoring.",
1238 options->OutboundBindAddress);
1239 } else {
1240 if (bind(s, (struct sockaddr*)&ext_addr,
1241 (socklen_t)sizeof(ext_addr)) < 0) {
1242 *socket_error = tor_socket_errno(s);
1243 log_warn(LD_NET,"Error binding network socket: %s",
1244 tor_socket_strerror(*socket_error));
1245 tor_close_socket(s);
1246 return -1;
1251 set_socket_nonblocking(s);
1253 if (options->ConstrainedSockets)
1254 set_constrained_socket_buffers(s, (int)options->ConstrainedSockSize);
1256 memset(addrbuf,0,sizeof(addrbuf));
1257 dest_addr = (struct sockaddr*) addrbuf;
1258 dest_addr_len = tor_addr_to_sockaddr(addr, port, dest_addr, sizeof(addrbuf));
1259 tor_assert(dest_addr_len > 0);
1261 log_debug(LD_NET,"Connecting to %s:%u.",escaped_safe_str(address),port);
1263 if (connect(s, dest_addr, dest_addr_len) < 0) {
1264 int e = tor_socket_errno(s);
1265 if (!ERRNO_IS_CONN_EINPROGRESS(e)) {
1266 /* yuck. kill it. */
1267 *socket_error = e;
1268 log_info(LD_NET,
1269 "connect() to %s:%u failed: %s",escaped_safe_str(address),
1270 port, tor_socket_strerror(e));
1271 tor_close_socket(s);
1272 return -1;
1273 } else {
1274 inprogress = 1;
1278 if (!server_mode(options))
1279 client_check_address_changed(s);
1281 /* it succeeded. we're connected. */
1282 log_fn(inprogress?LOG_DEBUG:LOG_INFO, LD_NET,
1283 "Connection to %s:%u %s (sock %d).",escaped_safe_str(address),
1284 port, inprogress?"in progress":"established", s);
1285 conn->s = s;
1286 if (connection_add(conn) < 0) /* no space, forget it */
1287 return -1;
1288 return inprogress ? 0 : 1;
1292 * Launch any configured listener connections of type <b>type</b>. (A
1293 * listener is configured if <b>port_option</b> is non-zero. If any
1294 * ListenAddress configuration options are given in <b>cfg</b>, create a
1295 * connection binding to each one. Otherwise, create a single
1296 * connection binding to the address <b>default_addr</b>.)
1298 * Only launch the listeners of this type that are not already open, and
1299 * only close listeners that are no longer wanted. Existing listeners
1300 * that are still configured are not touched.
1302 * If <b>disable_all_conns</b> is set, then never open new conns, and
1303 * close the existing ones.
1305 * Add all old conns that should be closed to <b>replaced_conns</b>.
1306 * Add all new connections to <b>new_conns</b>.
1308 static int
1309 retry_listeners(int type, config_line_t *cfg,
1310 int port_option, const char *default_addr,
1311 smartlist_t *replaced_conns,
1312 smartlist_t *new_conns,
1313 int disable_all_conns,
1314 int socket_family)
1316 smartlist_t *launch = smartlist_create(), *conns;
1317 int free_launch_elts = 1;
1318 int r;
1319 config_line_t *c;
1320 connection_t *conn;
1321 config_line_t *line;
1323 tor_assert(socket_family == AF_INET || socket_family == AF_UNIX);
1325 if (cfg && port_option) {
1326 for (c = cfg; c; c = c->next) {
1327 smartlist_add(launch, c);
1329 free_launch_elts = 0;
1330 } else if (port_option) {
1331 line = tor_malloc_zero(sizeof(config_line_t));
1332 line->key = tor_strdup("");
1333 line->value = tor_strdup(default_addr);
1334 smartlist_add(launch, line);
1338 SMARTLIST_FOREACH(launch, config_line_t *, l,
1339 log_fn(LOG_NOTICE, "#%s#%s", l->key, l->value));
1342 conns = get_connection_array();
1343 SMARTLIST_FOREACH(conns, connection_t *, conn,
1345 if (conn->type != type ||
1346 conn->socket_family != socket_family ||
1347 conn->marked_for_close)
1348 continue;
1349 /* Okay, so this is a listener. Is it configured? */
1350 line = NULL;
1351 SMARTLIST_FOREACH(launch, config_line_t *, wanted,
1353 char *address=NULL;
1354 uint16_t port;
1355 switch (socket_family) {
1356 case AF_INET:
1357 if (!parse_addr_port(LOG_WARN,
1358 wanted->value, &address, NULL, &port)) {
1359 int addr_matches = !strcasecmp(address, conn->address);
1360 tor_free(address);
1361 if (! port)
1362 port = port_option;
1363 if (port == conn->port && addr_matches) {
1364 line = wanted;
1365 break;
1368 break;
1369 case AF_UNIX:
1370 if (!strcasecmp(wanted->value, conn->address)) {
1371 line = wanted;
1372 break;
1374 break;
1375 default:
1376 tor_assert(0);
1379 if (!line || disable_all_conns) {
1380 /* This one isn't configured. Close it. */
1381 log_notice(LD_NET, "Closing no-longer-configured %s on %s:%d",
1382 conn_type_to_string(type), conn->address, conn->port);
1383 if (replaced_conns) {
1384 smartlist_add(replaced_conns, conn);
1385 } else {
1386 connection_close_immediate(conn);
1387 connection_mark_for_close(conn);
1389 } else {
1390 /* It's configured; we don't need to launch it. */
1391 // log_debug(LD_NET, "Already have %s on %s:%d",
1392 // conn_type_to_string(type), conn->address, conn->port);
1393 smartlist_remove(launch, line);
1394 if (free_launch_elts)
1395 config_free_lines(line);
1399 /* Now open all the listeners that are configured but not opened. */
1400 r = 0;
1401 if (!disable_all_conns) {
1402 SMARTLIST_FOREACH(launch, config_line_t *, cfg_line,
1404 char *address = NULL;
1405 struct sockaddr *listensockaddr;
1407 switch (socket_family) {
1408 case AF_INET:
1409 listensockaddr = (struct sockaddr *)
1410 create_inet_sockaddr(cfg_line->value,
1411 (uint16_t) port_option,
1412 &address);
1413 break;
1414 case AF_UNIX:
1415 listensockaddr = (struct sockaddr *)
1416 create_unix_sockaddr(cfg_line->value,
1417 &address);
1418 break;
1419 default:
1420 tor_assert(0);
1423 if (listensockaddr) {
1424 conn = connection_create_listener(listensockaddr, type, address);
1425 tor_free(listensockaddr);
1426 tor_free(address);
1427 } else
1428 conn = NULL;
1430 if (!conn) {
1431 r = -1;
1432 } else {
1433 if (new_conns)
1434 smartlist_add(new_conns, conn);
1439 if (free_launch_elts) {
1440 SMARTLIST_FOREACH(launch, config_line_t *, cfg_line,
1441 config_free_lines(cfg_line));
1443 smartlist_free(launch);
1445 return r;
1448 /** Launch listeners for each port you should have open. Only launch
1449 * listeners who are not already open, and only close listeners we no longer
1450 * want.
1452 * Add all old conns that should be closed to <b>replaced_conns</b>.
1453 * Add all new connections to <b>new_conns</b>.
1456 retry_all_listeners(smartlist_t *replaced_conns,
1457 smartlist_t *new_conns)
1459 or_options_t *options = get_options();
1461 if (retry_listeners(CONN_TYPE_OR_LISTENER, options->ORListenAddress,
1462 options->ORPort, "0.0.0.0",
1463 replaced_conns, new_conns, options->ClientOnly,
1464 AF_INET)<0)
1465 return -1;
1466 if (retry_listeners(CONN_TYPE_DIR_LISTENER, options->DirListenAddress,
1467 options->DirPort, "0.0.0.0",
1468 replaced_conns, new_conns, options->ClientOnly,
1469 AF_INET)<0)
1470 return -1;
1471 if (retry_listeners(CONN_TYPE_AP_LISTENER, options->SocksListenAddress,
1472 options->SocksPort, "127.0.0.1",
1473 replaced_conns, new_conns, 0,
1474 AF_INET)<0)
1475 return -1;
1476 if (retry_listeners(CONN_TYPE_AP_TRANS_LISTENER, options->TransListenAddress,
1477 options->TransPort, "127.0.0.1",
1478 replaced_conns, new_conns, 0,
1479 AF_INET)<0)
1480 return -1;
1481 if (retry_listeners(CONN_TYPE_AP_NATD_LISTENER, options->NatdListenAddress,
1482 options->NatdPort, "127.0.0.1",
1483 replaced_conns, new_conns, 0,
1484 AF_INET)<0)
1485 return -1;
1486 if (retry_listeners(CONN_TYPE_AP_DNS_LISTENER, options->DNSListenAddress,
1487 options->DNSPort, "127.0.0.1",
1488 replaced_conns, new_conns, 0,
1489 AF_INET)<0)
1490 return -1;
1491 if (retry_listeners(CONN_TYPE_CONTROL_LISTENER,
1492 options->ControlListenAddress,
1493 options->ControlPort, "127.0.0.1",
1494 replaced_conns, new_conns, 0,
1495 AF_INET)<0)
1496 return -1;
1497 if (retry_listeners(CONN_TYPE_CONTROL_LISTENER,
1498 options->ControlSocket,
1499 options->ControlSocket ? 1 : 0, NULL,
1500 replaced_conns, new_conns, 0,
1501 AF_UNIX)<0)
1502 return -1;
1504 return 0;
1507 /** Return 1 if we should apply rate limiting to <b>conn</b>,
1508 * and 0 otherwise. Right now this just checks if it's an internal
1509 * IP address or an internal connection. */
1510 static int
1511 connection_is_rate_limited(connection_t *conn)
1513 if (conn->linked || /* internal connection */
1514 tor_addr_family(&conn->addr) == AF_UNSPEC || /* no address */
1515 tor_addr_is_internal(&conn->addr, 0)) /* internal address */
1516 return 0;
1517 else
1518 return 1;
1521 extern int global_read_bucket, global_write_bucket;
1522 extern int global_relayed_read_bucket, global_relayed_write_bucket;
1524 /** Did either global write bucket run dry last second? If so,
1525 * we are likely to run dry again this second, so be stingy with the
1526 * tokens we just put in. */
1527 static int write_buckets_empty_last_second = 0;
1529 /** How many seconds of no active local circuits will make the
1530 * connection revert to the "relayed" bandwidth class? */
1531 #define CLIENT_IDLE_TIME_FOR_PRIORITY 30
1533 /** Return 1 if <b>conn</b> should use tokens from the "relayed"
1534 * bandwidth rates, else 0. Currently, only OR conns with bandwidth
1535 * class 1, and directory conns that are serving data out, count.
1537 static int
1538 connection_counts_as_relayed_traffic(connection_t *conn, time_t now)
1540 if (conn->type == CONN_TYPE_OR &&
1541 TO_OR_CONN(conn)->client_used + CLIENT_IDLE_TIME_FOR_PRIORITY < now)
1542 return 1;
1543 if (conn->type == CONN_TYPE_DIR && DIR_CONN_IS_SERVER(conn))
1544 return 1;
1545 return 0;
1548 /** Helper function to decide how many bytes out of <b>global_bucket</b>
1549 * we're willing to use for this transaction. <b>base</b> is the size
1550 * of a cell on the network; <b>priority</b> says whether we should
1551 * write many of them or just a few; and <b>conn_bucket</b> (if
1552 * non-negative) provides an upper limit for our answer. */
1553 static ssize_t
1554 connection_bucket_round_robin(int base, int priority,
1555 ssize_t global_bucket, ssize_t conn_bucket)
1557 ssize_t at_most;
1558 ssize_t num_bytes_high = (priority ? 32 : 16) * base;
1559 ssize_t num_bytes_low = (priority ? 4 : 2) * base;
1561 /* Do a rudimentary round-robin so one circuit can't hog a connection.
1562 * Pick at most 32 cells, at least 4 cells if possible, and if we're in
1563 * the middle pick 1/8 of the available bandwidth. */
1564 at_most = global_bucket / 8;
1565 at_most -= (at_most % base); /* round down */
1566 if (at_most > num_bytes_high) /* 16 KB, or 8 KB for low-priority */
1567 at_most = num_bytes_high;
1568 else if (at_most < num_bytes_low) /* 2 KB, or 1 KB for low-priority */
1569 at_most = num_bytes_low;
1571 if (at_most > global_bucket)
1572 at_most = global_bucket;
1574 if (conn_bucket >= 0 && at_most > conn_bucket)
1575 at_most = conn_bucket;
1577 if (at_most < 0)
1578 return 0;
1579 return at_most;
1582 /** How many bytes at most can we read onto this connection? */
1583 static ssize_t
1584 connection_bucket_read_limit(connection_t *conn, time_t now)
1586 int base = connection_speaks_cells(conn) ?
1587 CELL_NETWORK_SIZE : RELAY_PAYLOAD_SIZE;
1588 int priority = conn->type != CONN_TYPE_DIR;
1589 int conn_bucket = -1;
1590 int global_bucket = global_read_bucket;
1592 if (connection_speaks_cells(conn)) {
1593 or_connection_t *or_conn = TO_OR_CONN(conn);
1594 if (conn->state == OR_CONN_STATE_OPEN)
1595 conn_bucket = or_conn->read_bucket;
1598 if (!connection_is_rate_limited(conn)) {
1599 /* be willing to read on local conns even if our buckets are empty */
1600 return conn_bucket>=0 ? conn_bucket : 1<<14;
1603 if (connection_counts_as_relayed_traffic(conn, now) &&
1604 global_relayed_read_bucket <= global_read_bucket)
1605 global_bucket = global_relayed_read_bucket;
1607 return connection_bucket_round_robin(base, priority,
1608 global_bucket, conn_bucket);
1611 /** How many bytes at most can we write onto this connection? */
1612 ssize_t
1613 connection_bucket_write_limit(connection_t *conn, time_t now)
1615 int base = connection_speaks_cells(conn) ?
1616 CELL_NETWORK_SIZE : RELAY_PAYLOAD_SIZE;
1617 int priority = conn->type != CONN_TYPE_DIR;
1618 int global_bucket = global_write_bucket;
1620 if (!connection_is_rate_limited(conn)) {
1621 /* be willing to write to local conns even if our buckets are empty */
1622 return conn->outbuf_flushlen;
1625 if (connection_counts_as_relayed_traffic(conn, now) &&
1626 global_relayed_write_bucket <= global_write_bucket)
1627 global_bucket = global_relayed_write_bucket;
1629 return connection_bucket_round_robin(base, priority, global_bucket,
1630 conn->outbuf_flushlen);
1633 /** Return 1 if the global write buckets are low enough that we
1634 * shouldn't send <b>attempt</b> bytes of low-priority directory stuff
1635 * out to <b>conn</b>. Else return 0.
1637 * Priority is 1 for v1 requests (directories and running-routers),
1638 * and 2 for v2 requests (statuses and descriptors). But see FFFF in
1639 * directory_handle_command_get() for why we don't use priority 2 yet.
1641 * There are a lot of parameters we could use here:
1642 * - global_relayed_write_bucket. Low is bad.
1643 * - global_write_bucket. Low is bad.
1644 * - bandwidthrate. Low is bad.
1645 * - bandwidthburst. Not a big factor?
1646 * - attempt. High is bad.
1647 * - total bytes queued on outbufs. High is bad. But I'm wary of
1648 * using this, since a few slow-flushing queues will pump up the
1649 * number without meaning what we meant to mean. What we really
1650 * mean is "total directory bytes added to outbufs recently", but
1651 * that's harder to quantify and harder to keep track of.
1654 global_write_bucket_low(connection_t *conn, size_t attempt, int priority)
1656 int smaller_bucket = global_write_bucket < global_relayed_write_bucket ?
1657 global_write_bucket : global_relayed_write_bucket;
1658 if (authdir_mode(get_options()) && priority>1)
1659 return 0; /* there's always room to answer v2 if we're an auth dir */
1661 if (!connection_is_rate_limited(conn))
1662 return 0; /* local conns don't get limited */
1664 if (smaller_bucket < (int)attempt)
1665 return 1; /* not enough space no matter the priority */
1667 if (write_buckets_empty_last_second)
1668 return 1; /* we're already hitting our limits, no more please */
1670 if (priority == 1) { /* old-style v1 query */
1671 /* Could we handle *two* of these requests within the next two seconds? */
1672 or_options_t *options = get_options();
1673 int64_t can_write = (int64_t)smaller_bucket
1674 + 2*(options->RelayBandwidthRate ? options->RelayBandwidthRate :
1675 options->BandwidthRate);
1676 if (can_write < 2*(int64_t)attempt)
1677 return 1;
1678 } else { /* v2 query */
1679 /* no further constraints yet */
1681 return 0;
1684 /** We just read num_read and wrote num_written onto conn.
1685 * Decrement buckets appropriately. */
1686 static void
1687 connection_buckets_decrement(connection_t *conn, time_t now,
1688 size_t num_read, size_t num_written)
1690 if (!connection_is_rate_limited(conn))
1691 return; /* local IPs are free */
1692 if (num_written >= INT_MAX || num_read >= INT_MAX) {
1693 log_err(LD_BUG, "Value out of range. num_read=%lu, num_written=%lu, "
1694 "connection type=%s, state=%s",
1695 (unsigned long)num_read, (unsigned long)num_written,
1696 conn_type_to_string(conn->type),
1697 conn_state_to_string(conn->type, conn->state));
1698 if (num_written >= INT_MAX) num_written = 1;
1699 if (num_read >= INT_MAX) num_read = 1;
1700 tor_fragile_assert();
1703 if (num_read > 0)
1704 rep_hist_note_bytes_read(num_read, now);
1705 if (num_written > 0)
1706 rep_hist_note_bytes_written(num_written, now);
1708 if (connection_counts_as_relayed_traffic(conn, now)) {
1709 global_relayed_read_bucket -= (int)num_read;
1710 global_relayed_write_bucket -= (int)num_written;
1712 global_read_bucket -= (int)num_read;
1713 global_write_bucket -= (int)num_written;
1714 if (connection_speaks_cells(conn) && conn->state == OR_CONN_STATE_OPEN)
1715 TO_OR_CONN(conn)->read_bucket -= (int)num_read;
1718 /** If we have exhausted our global buckets, or the buckets for conn,
1719 * stop reading. */
1720 static void
1721 connection_consider_empty_read_buckets(connection_t *conn)
1723 const char *reason;
1725 if (global_read_bucket <= 0) {
1726 reason = "global read bucket exhausted. Pausing.";
1727 } else if (connection_counts_as_relayed_traffic(conn, time(NULL)) &&
1728 global_relayed_read_bucket <= 0) {
1729 reason = "global relayed read bucket exhausted. Pausing.";
1730 } else if (connection_speaks_cells(conn) &&
1731 conn->state == OR_CONN_STATE_OPEN &&
1732 TO_OR_CONN(conn)->read_bucket <= 0) {
1733 reason = "connection read bucket exhausted. Pausing.";
1734 } else
1735 return; /* all good, no need to stop it */
1737 LOG_FN_CONN(conn, (LOG_DEBUG, LD_NET, "%s", reason));
1738 conn->read_blocked_on_bw = 1;
1739 connection_stop_reading(conn);
1742 /** If we have exhausted our global buckets, or the buckets for conn,
1743 * stop writing. */
1744 static void
1745 connection_consider_empty_write_buckets(connection_t *conn)
1747 const char *reason;
1749 if (global_write_bucket <= 0) {
1750 reason = "global write bucket exhausted. Pausing.";
1751 } else if (connection_counts_as_relayed_traffic(conn, time(NULL)) &&
1752 global_relayed_write_bucket <= 0) {
1753 reason = "global relayed write bucket exhausted. Pausing.";
1754 #if 0
1755 } else if (connection_speaks_cells(conn) &&
1756 conn->state == OR_CONN_STATE_OPEN &&
1757 TO_OR_CONN(conn)->write_bucket <= 0) {
1758 reason = "connection write bucket exhausted. Pausing.";
1759 #endif
1760 } else
1761 return; /* all good, no need to stop it */
1763 LOG_FN_CONN(conn, (LOG_DEBUG, LD_NET, "%s", reason));
1764 conn->write_blocked_on_bw = 1;
1765 connection_stop_writing(conn);
1768 /** Initialize the global read bucket to options-\>BandwidthBurst. */
1769 void
1770 connection_bucket_init(void)
1772 or_options_t *options = get_options();
1773 /* start it at max traffic */
1774 global_read_bucket = (int)options->BandwidthBurst;
1775 global_write_bucket = (int)options->BandwidthBurst;
1776 if (options->RelayBandwidthRate) {
1777 global_relayed_read_bucket = (int)options->RelayBandwidthBurst;
1778 global_relayed_write_bucket = (int)options->RelayBandwidthBurst;
1779 } else {
1780 global_relayed_read_bucket = (int)options->BandwidthBurst;
1781 global_relayed_write_bucket = (int)options->BandwidthBurst;
1785 /** Refill a single <b>bucket</b> called <b>name</b> with bandwith rate
1786 * <b>rate</b> and bandwidth burst <b>burst</b>, assuming that
1787 * <b>seconds_elapsed</b> seconds have passed since the last call.
1789 static void
1790 connection_bucket_refill_helper(int *bucket, int rate, int burst,
1791 int seconds_elapsed, const char *name)
1793 int starting_bucket = *bucket;
1794 if (starting_bucket < burst && seconds_elapsed) {
1795 if (((burst - starting_bucket)/seconds_elapsed) < rate) {
1796 *bucket = burst; /* We would overflow the bucket; just set it to
1797 * the maximum. */
1798 } else {
1799 int incr = rate*seconds_elapsed;
1800 *bucket += incr;
1801 if (*bucket > burst || *bucket < starting_bucket) {
1802 /* If we overflow the burst, or underflow our starting bucket,
1803 * cap the bucket value to burst. */
1804 /* XXXX this might be redundant now, but it doesn't show up
1805 * in profiles. Remove it after analysis. */
1806 *bucket = burst;
1809 log(LOG_DEBUG, LD_NET,"%s now %d.", name, *bucket);
1813 /** A second has rolled over; increment buckets appropriately. */
1814 void
1815 connection_bucket_refill(int seconds_elapsed, time_t now)
1817 or_options_t *options = get_options();
1818 smartlist_t *conns = get_connection_array();
1819 int relayrate, relayburst;
1821 if (options->RelayBandwidthRate) {
1822 relayrate = (int)options->RelayBandwidthRate;
1823 relayburst = (int)options->RelayBandwidthBurst;
1824 } else {
1825 relayrate = (int)options->BandwidthRate;
1826 relayburst = (int)options->BandwidthBurst;
1829 tor_assert(seconds_elapsed >= 0);
1831 write_buckets_empty_last_second =
1832 global_relayed_write_bucket <= 0 || global_write_bucket <= 0;
1834 /* refill the global buckets */
1835 connection_bucket_refill_helper(&global_read_bucket,
1836 (int)options->BandwidthRate,
1837 (int)options->BandwidthBurst,
1838 seconds_elapsed, "global_read_bucket");
1839 connection_bucket_refill_helper(&global_write_bucket,
1840 (int)options->BandwidthRate,
1841 (int)options->BandwidthBurst,
1842 seconds_elapsed, "global_write_bucket");
1843 connection_bucket_refill_helper(&global_relayed_read_bucket,
1844 relayrate, relayburst, seconds_elapsed,
1845 "global_relayed_read_bucket");
1846 connection_bucket_refill_helper(&global_relayed_write_bucket,
1847 relayrate, relayburst, seconds_elapsed,
1848 "global_relayed_write_bucket");
1850 /* refill the per-connection buckets */
1851 SMARTLIST_FOREACH(conns, connection_t *, conn,
1853 if (connection_speaks_cells(conn)) {
1854 or_connection_t *or_conn = TO_OR_CONN(conn);
1855 if (connection_read_bucket_should_increase(or_conn)) {
1856 connection_bucket_refill_helper(&or_conn->read_bucket,
1857 or_conn->bandwidthrate,
1858 or_conn->bandwidthburst,
1859 seconds_elapsed,
1860 "or_conn->read_bucket");
1861 //log_fn(LOG_DEBUG,"Receiver bucket %d now %d.", i,
1862 // conn->read_bucket);
1866 if (conn->read_blocked_on_bw == 1 /* marked to turn reading back on now */
1867 && global_read_bucket > 0 /* and we're allowed to read */
1868 && (!connection_counts_as_relayed_traffic(conn, now) ||
1869 global_relayed_read_bucket > 0) /* even if we're relayed traffic */
1870 && (!connection_speaks_cells(conn) ||
1871 conn->state != OR_CONN_STATE_OPEN ||
1872 TO_OR_CONN(conn)->read_bucket > 0)) {
1873 /* and either a non-cell conn or a cell conn with non-empty bucket */
1874 LOG_FN_CONN(conn, (LOG_DEBUG,LD_NET,
1875 "waking up conn (fd %d) for read", conn->s));
1876 conn->read_blocked_on_bw = 0;
1877 connection_start_reading(conn);
1880 if (conn->write_blocked_on_bw == 1
1881 && global_write_bucket > 0 /* and we're allowed to write */
1882 && (!connection_counts_as_relayed_traffic(conn, now) ||
1883 global_relayed_write_bucket > 0)) {
1884 /* even if we're relayed traffic */
1885 LOG_FN_CONN(conn, (LOG_DEBUG,LD_NET,
1886 "waking up conn (fd %d) for write", conn->s));
1887 conn->write_blocked_on_bw = 0;
1888 connection_start_writing(conn);
1893 /** Is the receiver bucket for connection <b>conn</b> low enough that we
1894 * should add another pile of tokens to it?
1896 static int
1897 connection_read_bucket_should_increase(or_connection_t *conn)
1899 tor_assert(conn);
1901 if (conn->_base.state != OR_CONN_STATE_OPEN)
1902 return 0; /* only open connections play the rate limiting game */
1903 if (conn->read_bucket >= conn->bandwidthburst)
1904 return 0;
1906 return 1;
1909 /** Read bytes from conn-\>s and process them.
1911 * This function gets called from conn_read() in main.c, either
1912 * when poll() has declared that conn wants to read, or (for OR conns)
1913 * when there are pending TLS bytes.
1915 * It calls connection_read_to_buf() to bring in any new bytes,
1916 * and then calls connection_process_inbuf() to process them.
1918 * Mark the connection and return -1 if you want to close it, else
1919 * return 0.
1922 connection_handle_read(connection_t *conn)
1924 int max_to_read=-1, try_to_read;
1925 size_t before, n_read = 0;
1926 int socket_error = 0;
1928 if (conn->marked_for_close)
1929 return 0; /* do nothing */
1931 conn->timestamp_lastread = time(NULL);
1933 switch (conn->type) {
1934 case CONN_TYPE_OR_LISTENER:
1935 return connection_handle_listener_read(conn, CONN_TYPE_OR);
1936 case CONN_TYPE_AP_LISTENER:
1937 case CONN_TYPE_AP_TRANS_LISTENER:
1938 case CONN_TYPE_AP_NATD_LISTENER:
1939 return connection_handle_listener_read(conn, CONN_TYPE_AP);
1940 case CONN_TYPE_DIR_LISTENER:
1941 return connection_handle_listener_read(conn, CONN_TYPE_DIR);
1942 case CONN_TYPE_CONTROL_LISTENER:
1943 return connection_handle_listener_read(conn, CONN_TYPE_CONTROL);
1944 case CONN_TYPE_AP_DNS_LISTENER:
1945 /* This should never happen; eventdns.c handles the reads here. */
1946 tor_fragile_assert();
1947 return 0;
1950 loop_again:
1951 try_to_read = max_to_read;
1952 tor_assert(!conn->marked_for_close);
1954 before = buf_datalen(conn->inbuf);
1955 if (connection_read_to_buf(conn, &max_to_read, &socket_error) < 0) {
1956 /* There's a read error; kill the connection.*/
1957 if (conn->type == CONN_TYPE_OR &&
1958 conn->state == OR_CONN_STATE_CONNECTING) {
1959 connection_or_connect_failed(TO_OR_CONN(conn),
1960 errno_to_orconn_end_reason(socket_error),
1961 tor_socket_strerror(socket_error));
1963 if (CONN_IS_EDGE(conn)) {
1964 edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
1965 connection_edge_end_errno(edge_conn);
1966 if (edge_conn->socks_request) /* broken, don't send a socks reply back */
1967 edge_conn->socks_request->has_finished = 1;
1969 connection_close_immediate(conn); /* Don't flush; connection is dead. */
1970 connection_mark_for_close(conn);
1971 return -1;
1973 n_read += buf_datalen(conn->inbuf) - before;
1974 if (CONN_IS_EDGE(conn) && try_to_read != max_to_read) {
1975 /* instruct it not to try to package partial cells. */
1976 if (connection_process_inbuf(conn, 0) < 0) {
1977 return -1;
1979 if (!conn->marked_for_close &&
1980 connection_is_reading(conn) &&
1981 !conn->inbuf_reached_eof &&
1982 max_to_read > 0)
1983 goto loop_again; /* try reading again, in case more is here now */
1985 /* one last try, packaging partial cells and all. */
1986 if (!conn->marked_for_close &&
1987 connection_process_inbuf(conn, 1) < 0) {
1988 return -1;
1990 if (conn->linked_conn) {
1991 /* The other side's handle_write will never actually get called, so
1992 * we need to invoke the appropriate callbacks ourself. */
1993 connection_t *linked = conn->linked_conn;
1995 if (n_read) {
1996 /* Probably a no-op, but hey. */
1997 connection_buckets_decrement(linked, time(NULL), 0, n_read);
1999 if (connection_flushed_some(linked) < 0)
2000 connection_mark_for_close(linked);
2001 if (!connection_wants_to_flush(linked))
2002 connection_finished_flushing(linked);
2005 if (!buf_datalen(linked->outbuf) && conn->active_on_link)
2006 connection_stop_reading_from_linked_conn(conn);
2008 /* If we hit the EOF, call connection_reached_eof. */
2009 if (!conn->marked_for_close &&
2010 conn->inbuf_reached_eof &&
2011 connection_reached_eof(conn) < 0) {
2012 return -1;
2014 return 0;
2017 /** Pull in new bytes from conn-\>s or conn-\>linked_conn onto conn-\>inbuf,
2018 * either directly or via TLS. Reduce the token buckets by the number of bytes
2019 * read.
2021 * If *max_to_read is -1, then decide it ourselves, else go with the
2022 * value passed to us. When returning, if it's changed, subtract the
2023 * number of bytes we read from *max_to_read.
2025 * Return -1 if we want to break conn, else return 0.
2027 static int
2028 connection_read_to_buf(connection_t *conn, int *max_to_read, int *socket_error)
2030 int result;
2031 ssize_t at_most = *max_to_read;
2032 size_t slack_in_buf, more_to_read;
2033 size_t n_read = 0, n_written = 0;
2035 if (at_most == -1) { /* we need to initialize it */
2036 /* how many bytes are we allowed to read? */
2037 /* XXXX021 too many calls to time(). Do they hurt? */
2038 at_most = connection_bucket_read_limit(conn, time(NULL));
2041 slack_in_buf = buf_slack(conn->inbuf);
2042 again:
2043 if ((size_t)at_most > slack_in_buf && slack_in_buf >= 1024) {
2044 more_to_read = at_most - slack_in_buf;
2045 at_most = slack_in_buf;
2046 } else {
2047 more_to_read = 0;
2050 if (connection_speaks_cells(conn) &&
2051 conn->state > OR_CONN_STATE_PROXY_READING) {
2052 int pending;
2053 or_connection_t *or_conn = TO_OR_CONN(conn);
2054 size_t initial_size;
2055 if (conn->state == OR_CONN_STATE_TLS_HANDSHAKING ||
2056 conn->state == OR_CONN_STATE_TLS_CLIENT_RENEGOTIATING) {
2057 /* continue handshaking even if global token bucket is empty */
2058 return connection_tls_continue_handshake(or_conn);
2061 log_debug(LD_NET,
2062 "%d: starting, inbuf_datalen %ld (%d pending in tls object)."
2063 " at_most %ld.",
2064 conn->s,(long)buf_datalen(conn->inbuf),
2065 tor_tls_get_pending_bytes(or_conn->tls), (long)at_most);
2067 initial_size = buf_datalen(conn->inbuf);
2068 /* else open, or closing */
2069 result = read_to_buf_tls(or_conn->tls, at_most, conn->inbuf);
2070 if (TOR_TLS_IS_ERROR(result) || result == TOR_TLS_CLOSE)
2071 or_conn->tls_error = result;
2072 else
2073 or_conn->tls_error = 0;
2075 switch (result) {
2076 case TOR_TLS_CLOSE:
2077 case TOR_TLS_ERROR_IO:
2078 log_debug(LD_NET,"TLS connection closed %son read. Closing. "
2079 "(Nickname %s, address %s)",
2080 result == TOR_TLS_CLOSE ? "cleanly " : "",
2081 or_conn->nickname ? or_conn->nickname : "not set",
2082 conn->address);
2083 return result;
2084 CASE_TOR_TLS_ERROR_ANY_NONIO:
2085 log_debug(LD_NET,"tls error [%s]. breaking (nickname %s, address %s).",
2086 tor_tls_err_to_string(result),
2087 or_conn->nickname ? or_conn->nickname : "not set",
2088 conn->address);
2089 return result;
2090 case TOR_TLS_WANTWRITE:
2091 connection_start_writing(conn);
2092 return 0;
2093 case TOR_TLS_WANTREAD: /* we're already reading */
2094 case TOR_TLS_DONE: /* no data read, so nothing to process */
2095 result = 0;
2096 break; /* so we call bucket_decrement below */
2097 default:
2098 break;
2100 pending = tor_tls_get_pending_bytes(or_conn->tls);
2101 if (pending) {
2102 /* If we have any pending bytes, we read them now. This *can*
2103 * take us over our read allotment, but really we shouldn't be
2104 * believing that SSL bytes are the same as TCP bytes anyway. */
2105 int r2 = read_to_buf_tls(or_conn->tls, pending, conn->inbuf);
2106 if (r2<0) {
2107 log_warn(LD_BUG, "apparently, reading pending bytes can fail.");
2108 return -1;
2111 result = (int)(buf_datalen(conn->inbuf)-initial_size);
2112 tor_tls_get_n_raw_bytes(or_conn->tls, &n_read, &n_written);
2113 log_debug(LD_GENERAL, "After TLS read of %d: %ld read, %ld written",
2114 result, (long)n_read, (long)n_written);
2115 } else if (conn->linked) {
2116 if (conn->linked_conn) {
2117 result = move_buf_to_buf(conn->inbuf, conn->linked_conn->outbuf,
2118 &conn->linked_conn->outbuf_flushlen);
2119 } else {
2120 result = 0;
2122 //log_notice(LD_GENERAL, "Moved %d bytes on an internal link!", result);
2123 /* If the other side has disappeared, or if it's been marked for close and
2124 * we flushed its outbuf, then we should set our inbuf_reached_eof. */
2125 if (!conn->linked_conn ||
2126 (conn->linked_conn->marked_for_close &&
2127 buf_datalen(conn->linked_conn->outbuf) == 0))
2128 conn->inbuf_reached_eof = 1;
2130 n_read = (size_t) result;
2131 } else {
2132 /* !connection_speaks_cells, !conn->linked_conn. */
2133 int reached_eof = 0;
2134 CONN_LOG_PROTECT(conn,
2135 result = read_to_buf(conn->s, at_most, conn->inbuf, &reached_eof,
2136 socket_error));
2137 if (reached_eof)
2138 conn->inbuf_reached_eof = 1;
2140 // log_fn(LOG_DEBUG,"read_to_buf returned %d.",read_result);
2142 if (result < 0)
2143 return -1;
2144 n_read = (size_t) result;
2147 if (n_read > 0) { /* change *max_to_read */
2148 /*XXXX021 check for overflow*/
2149 *max_to_read = (int)(at_most - n_read);
2152 if (conn->type == CONN_TYPE_AP) {
2153 edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
2154 /*XXXX021 check for overflow*/
2155 edge_conn->n_read += (int)n_read;
2158 connection_buckets_decrement(conn, time(NULL), n_read, n_written);
2160 if (more_to_read && result == at_most) {
2161 slack_in_buf = buf_slack(conn->inbuf);
2162 at_most = more_to_read;
2163 goto again;
2166 /* Call even if result is 0, since the global read bucket may
2167 * have reached 0 on a different conn, and this guy needs to
2168 * know to stop reading. */
2169 connection_consider_empty_read_buckets(conn);
2170 if (n_written > 0 && connection_is_writing(conn))
2171 connection_consider_empty_write_buckets(conn);
2173 return 0;
2176 /** A pass-through to fetch_from_buf. */
2178 connection_fetch_from_buf(char *string, size_t len, connection_t *conn)
2180 return fetch_from_buf(string, len, conn->inbuf);
2183 /** Return conn-\>outbuf_flushlen: how many bytes conn wants to flush
2184 * from its outbuf. */
2186 connection_wants_to_flush(connection_t *conn)
2188 return conn->outbuf_flushlen > 0;
2191 /** Are there too many bytes on edge connection <b>conn</b>'s outbuf to
2192 * send back a relay-level sendme yet? Return 1 if so, 0 if not. Used by
2193 * connection_edge_consider_sending_sendme().
2196 connection_outbuf_too_full(connection_t *conn)
2198 return (conn->outbuf_flushlen > 10*CELL_PAYLOAD_SIZE);
2201 /** Try to flush more bytes onto conn-\>s.
2203 * This function gets called either from conn_write() in main.c
2204 * when poll() has declared that conn wants to write, or below
2205 * from connection_write_to_buf() when an entire TLS record is ready.
2207 * Update conn-\>timestamp_lastwritten to now, and call flush_buf
2208 * or flush_buf_tls appropriately. If it succeeds and there are no more
2209 * more bytes on conn->outbuf, then call connection_finished_flushing
2210 * on it too.
2212 * If <b>force</b>, then write as many bytes as possible, ignoring bandwidth
2213 * limits. (Used for flushing messages to controller connections on fatal
2214 * errors.)
2216 * Mark the connection and return -1 if you want to close it, else
2217 * return 0.
2220 connection_handle_write(connection_t *conn, int force)
2222 int e;
2223 socklen_t len=(socklen_t)sizeof(e);
2224 int result;
2225 ssize_t max_to_write;
2226 time_t now = time(NULL);
2227 size_t n_read = 0, n_written = 0;
2229 tor_assert(!connection_is_listener(conn));
2231 if (conn->marked_for_close || conn->s < 0)
2232 return 0; /* do nothing */
2234 if (conn->in_flushed_some) {
2235 log_warn(LD_BUG, "called recursively from inside conn->in_flushed_some()");
2236 return 0;
2239 conn->timestamp_lastwritten = now;
2241 /* Sometimes, "writable" means "connected". */
2242 if (connection_state_is_connecting(conn)) {
2243 if (getsockopt(conn->s, SOL_SOCKET, SO_ERROR, (void*)&e, &len) < 0) {
2244 log_warn(LD_BUG,
2245 "getsockopt() syscall failed?! Please report to tor-ops.");
2246 if (CONN_IS_EDGE(conn))
2247 connection_edge_end_errno(TO_EDGE_CONN(conn));
2248 connection_mark_for_close(conn);
2249 return -1;
2251 if (e) {
2252 /* some sort of error, but maybe just inprogress still */
2253 if (!ERRNO_IS_CONN_EINPROGRESS(e)) {
2254 log_info(LD_NET,"in-progress connect failed. Removing. (%s)",
2255 tor_socket_strerror(e));
2256 if (CONN_IS_EDGE(conn))
2257 connection_edge_end_errno(TO_EDGE_CONN(conn));
2258 if (conn->type == CONN_TYPE_OR)
2259 connection_or_connect_failed(TO_OR_CONN(conn),
2260 errno_to_orconn_end_reason(e),
2261 tor_socket_strerror(e));
2263 connection_close_immediate(conn);
2264 connection_mark_for_close(conn);
2265 return -1;
2266 } else {
2267 return 0; /* no change, see if next time is better */
2270 /* The connection is successful. */
2271 if (connection_finished_connecting(conn)<0)
2272 return -1;
2275 max_to_write = force ? (ssize_t)conn->outbuf_flushlen
2276 : connection_bucket_write_limit(conn, now);
2278 if (connection_speaks_cells(conn) &&
2279 conn->state > OR_CONN_STATE_PROXY_READING) {
2280 or_connection_t *or_conn = TO_OR_CONN(conn);
2281 if (conn->state == OR_CONN_STATE_TLS_HANDSHAKING ||
2282 conn->state == OR_CONN_STATE_TLS_CLIENT_RENEGOTIATING) {
2283 connection_stop_writing(conn);
2284 if (connection_tls_continue_handshake(or_conn) < 0) {
2285 /* Don't flush; connection is dead. */
2286 connection_close_immediate(conn);
2287 connection_mark_for_close(conn);
2288 return -1;
2290 return 0;
2291 } else if (conn->state == OR_CONN_STATE_TLS_SERVER_RENEGOTIATING) {
2292 return connection_handle_read(conn);
2295 /* else open, or closing */
2296 result = flush_buf_tls(or_conn->tls, conn->outbuf,
2297 max_to_write, &conn->outbuf_flushlen);
2298 switch (result) {
2299 CASE_TOR_TLS_ERROR_ANY:
2300 case TOR_TLS_CLOSE:
2301 log_info(LD_NET,result!=TOR_TLS_CLOSE?
2302 "tls error. breaking.":"TLS connection closed on flush");
2303 /* Don't flush; connection is dead. */
2304 connection_close_immediate(conn);
2305 connection_mark_for_close(conn);
2306 return -1;
2307 case TOR_TLS_WANTWRITE:
2308 log_debug(LD_NET,"wanted write.");
2309 /* we're already writing */
2310 return 0;
2311 case TOR_TLS_WANTREAD:
2312 /* Make sure to avoid a loop if the receive buckets are empty. */
2313 log_debug(LD_NET,"wanted read.");
2314 if (!connection_is_reading(conn)) {
2315 connection_stop_writing(conn);
2316 conn->write_blocked_on_bw = 1;
2317 /* we'll start reading again when the next second arrives,
2318 * and then also start writing again.
2321 /* else no problem, we're already reading */
2322 return 0;
2323 /* case TOR_TLS_DONE:
2324 * for TOR_TLS_DONE, fall through to check if the flushlen
2325 * is empty, so we can stop writing.
2329 tor_tls_get_n_raw_bytes(or_conn->tls, &n_read, &n_written);
2330 log_debug(LD_GENERAL, "After TLS write of %d: %ld read, %ld written",
2331 result, (long)n_read, (long)n_written);
2332 } else {
2333 CONN_LOG_PROTECT(conn,
2334 result = flush_buf(conn->s, conn->outbuf,
2335 max_to_write, &conn->outbuf_flushlen));
2336 if (result < 0) {
2337 if (CONN_IS_EDGE(conn))
2338 connection_edge_end_errno(TO_EDGE_CONN(conn));
2340 connection_close_immediate(conn); /* Don't flush; connection is dead. */
2341 connection_mark_for_close(conn);
2342 return -1;
2344 n_written = (size_t) result;
2347 if (conn->type == CONN_TYPE_AP) {
2348 edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
2349 /*XXXX021 check for overflow.*/
2350 edge_conn->n_written += (int)n_written;
2353 connection_buckets_decrement(conn, time(NULL), n_read, n_written);
2355 if (result > 0) {
2356 /* If we wrote any bytes from our buffer, then call the appropriate
2357 * functions. */
2358 if (connection_flushed_some(conn) < 0)
2359 connection_mark_for_close(conn);
2362 if (!connection_wants_to_flush(conn)) { /* it's done flushing */
2363 if (connection_finished_flushing(conn) < 0) {
2364 /* already marked */
2365 return -1;
2367 return 0;
2370 /* Call even if result is 0, since the global write bucket may
2371 * have reached 0 on a different conn, and this guy needs to
2372 * know to stop writing. */
2373 connection_consider_empty_write_buckets(conn);
2374 if (n_read > 0 && connection_is_reading(conn))
2375 connection_consider_empty_read_buckets(conn);
2377 return 0;
2380 /** OpenSSL TLS record size is 16383; this is close. The goal here is to
2381 * push data out as soon as we know there's enough for a TLS record, so
2382 * during periods of high load we won't read entire megabytes from
2383 * input before pushing any data out. It also has the feature of not
2384 * growing huge outbufs unless something is slow. */
2385 #define MIN_TLS_FLUSHLEN 15872
2387 /** Append <b>len</b> bytes of <b>string</b> onto <b>conn</b>'s
2388 * outbuf, and ask it to start writing.
2390 * If <b>zlib</b> is nonzero, this is a directory connection that should get
2391 * its contents compressed or decompressed as they're written. If zlib is
2392 * negative, this is the last data to be compressed, and the connection's zlib
2393 * state should be flushed.
2395 * If it's an OR conn and an entire TLS record is ready, then try to
2396 * flush the record now. Similarly, if it's a local control connection
2397 * and a 64k chunk is ready, try to flush it all, so we don't end up with
2398 * many megabytes of controller info queued at once.
2400 void
2401 _connection_write_to_buf_impl(const char *string, size_t len,
2402 connection_t *conn, int zlib)
2404 /* XXXX This function really needs to return -1 on failure. */
2405 int r;
2406 size_t old_datalen;
2407 if (!len && !(zlib<0))
2408 return;
2409 /* if it's marked for close, only allow write if we mean to flush it */
2410 if (conn->marked_for_close && !conn->hold_open_until_flushed)
2411 return;
2413 old_datalen = buf_datalen(conn->outbuf);
2414 if (zlib) {
2415 dir_connection_t *dir_conn = TO_DIR_CONN(conn);
2416 int done = zlib < 0;
2417 CONN_LOG_PROTECT(conn, r = write_to_buf_zlib(conn->outbuf,
2418 dir_conn->zlib_state,
2419 string, len, done));
2420 } else {
2421 CONN_LOG_PROTECT(conn, r = write_to_buf(string, len, conn->outbuf));
2423 if (r < 0) {
2424 if (CONN_IS_EDGE(conn)) {
2425 /* if it failed, it means we have our package/delivery windows set
2426 wrong compared to our max outbuf size. close the whole circuit. */
2427 log_warn(LD_NET,
2428 "write_to_buf failed. Closing circuit (fd %d).", conn->s);
2429 circuit_mark_for_close(circuit_get_by_edge_conn(TO_EDGE_CONN(conn)),
2430 END_CIRC_REASON_INTERNAL);
2431 } else {
2432 log_warn(LD_NET,
2433 "write_to_buf failed. Closing connection (fd %d).", conn->s);
2434 connection_mark_for_close(conn);
2436 return;
2439 connection_start_writing(conn);
2440 if (zlib) {
2441 conn->outbuf_flushlen += buf_datalen(conn->outbuf) - old_datalen;
2442 } else {
2443 ssize_t extra = 0;
2444 conn->outbuf_flushlen += len;
2446 /* Should we try flushing the outbuf now? */
2447 if (conn->in_flushed_some) {
2448 /* Don't flush the outbuf when the reason we're writing more stuff is
2449 * _because_ we flushed the outbuf. That's unfair. */
2450 return;
2453 if (conn->type == CONN_TYPE_OR &&
2454 conn->outbuf_flushlen-len < MIN_TLS_FLUSHLEN &&
2455 conn->outbuf_flushlen >= MIN_TLS_FLUSHLEN) {
2456 /* We just pushed outbuf_flushlen to MIN_TLS_FLUSHLEN or above;
2457 * we can send out a full TLS frame now if we like. */
2458 extra = conn->outbuf_flushlen - MIN_TLS_FLUSHLEN;
2459 conn->outbuf_flushlen = MIN_TLS_FLUSHLEN;
2460 } else if (conn->type == CONN_TYPE_CONTROL &&
2461 !connection_is_rate_limited(conn) &&
2462 conn->outbuf_flushlen-len < 1<<16 &&
2463 conn->outbuf_flushlen >= 1<<16) {
2464 /* just try to flush all of it */
2465 } else
2466 return; /* no need to try flushing */
2468 if (connection_handle_write(conn, 0) < 0) {
2469 if (!conn->marked_for_close) {
2470 /* this connection is broken. remove it. */
2471 log_warn(LD_BUG, "unhandled error on write for "
2472 "conn (type %d, fd %d); removing",
2473 conn->type, conn->s);
2474 tor_fragile_assert();
2475 /* do a close-immediate here, so we don't try to flush */
2476 connection_close_immediate(conn);
2478 return;
2480 if (extra) {
2481 conn->outbuf_flushlen += extra;
2482 connection_start_writing(conn);
2487 /** Return a connection with given type, address, port, and purpose;
2488 * or NULL if no such connection exists. */
2489 connection_t *
2490 connection_get_by_type_addr_port_purpose(int type,
2491 const tor_addr_t *addr, uint16_t port,
2492 int purpose)
2494 smartlist_t *conns = get_connection_array();
2495 SMARTLIST_FOREACH(conns, connection_t *, conn,
2497 if (conn->type == type &&
2498 tor_addr_eq(&conn->addr, addr) &&
2499 conn->port == port &&
2500 conn->purpose == purpose &&
2501 !conn->marked_for_close)
2502 return conn;
2504 return NULL;
2507 /** Return the stream with id <b>id</b> if it is not already marked for
2508 * close.
2510 connection_t *
2511 connection_get_by_global_id(uint64_t id)
2513 smartlist_t *conns = get_connection_array();
2514 SMARTLIST_FOREACH(conns, connection_t *, conn,
2516 if (conn->global_identifier == id)
2517 return conn;
2519 return NULL;
2522 /** Return a connection of type <b>type</b> that is not marked for close.
2524 connection_t *
2525 connection_get_by_type(int type)
2527 smartlist_t *conns = get_connection_array();
2528 SMARTLIST_FOREACH(conns, connection_t *, conn,
2530 if (conn->type == type && !conn->marked_for_close)
2531 return conn;
2533 return NULL;
2536 /** Return a connection of type <b>type</b> that is in state <b>state</b>,
2537 * and that is not marked for close.
2539 connection_t *
2540 connection_get_by_type_state(int type, int state)
2542 smartlist_t *conns = get_connection_array();
2543 SMARTLIST_FOREACH(conns, connection_t *, conn,
2545 if (conn->type == type && conn->state == state && !conn->marked_for_close)
2546 return conn;
2548 return NULL;
2551 /** Return a connection of type <b>type</b> that has rendquery equal
2552 * to <b>rendquery</b>, and that is not marked for close. If state
2553 * is non-zero, conn must be of that state too. If rendversion is
2554 * nonnegative, conn must be fetching that rendversion, too.
2556 connection_t *
2557 connection_get_by_type_state_rendquery(int type, int state,
2558 const char *rendquery,
2559 int rendversion)
2561 smartlist_t *conns = get_connection_array();
2563 tor_assert(type == CONN_TYPE_DIR ||
2564 type == CONN_TYPE_AP || type == CONN_TYPE_EXIT);
2565 tor_assert(rendquery);
2567 SMARTLIST_FOREACH(conns, connection_t *, conn,
2569 if (conn->type == type &&
2570 !conn->marked_for_close &&
2571 (!state || state == conn->state)) {
2572 if (type == CONN_TYPE_DIR &&
2573 TO_DIR_CONN(conn)->rend_data &&
2574 (rendversion < 0 ||
2575 rendversion == TO_DIR_CONN(conn)->rend_data->rend_desc_version) &&
2576 !rend_cmp_service_ids(rendquery,
2577 TO_DIR_CONN(conn)->rend_data->onion_address))
2578 return conn;
2579 else if (CONN_IS_EDGE(conn) &&
2580 TO_EDGE_CONN(conn)->rend_data &&
2581 !rend_cmp_service_ids(rendquery,
2582 TO_EDGE_CONN(conn)->rend_data->onion_address))
2583 return conn;
2586 return NULL;
2589 /** Return an open, non-marked connection of a given type and purpose, or NULL
2590 * if no such connection exists. */
2591 connection_t *
2592 connection_get_by_type_purpose(int type, int purpose)
2594 smartlist_t *conns = get_connection_array();
2595 SMARTLIST_FOREACH(conns, connection_t *, conn,
2597 if (conn->type == type &&
2598 !conn->marked_for_close &&
2599 (purpose == conn->purpose))
2600 return conn;
2602 return NULL;
2605 /** Return 1 if <b>conn</b> is a listener conn, else return 0. */
2607 connection_is_listener(connection_t *conn)
2609 if (conn->type == CONN_TYPE_OR_LISTENER ||
2610 conn->type == CONN_TYPE_AP_LISTENER ||
2611 conn->type == CONN_TYPE_AP_TRANS_LISTENER ||
2612 conn->type == CONN_TYPE_AP_DNS_LISTENER ||
2613 conn->type == CONN_TYPE_AP_NATD_LISTENER ||
2614 conn->type == CONN_TYPE_DIR_LISTENER ||
2615 conn->type == CONN_TYPE_CONTROL_LISTENER)
2616 return 1;
2617 return 0;
2620 /** Return 1 if <b>conn</b> is in state "open" and is not marked
2621 * for close, else return 0.
2624 connection_state_is_open(connection_t *conn)
2626 tor_assert(conn);
2628 if (conn->marked_for_close)
2629 return 0;
2631 if ((conn->type == CONN_TYPE_OR && conn->state == OR_CONN_STATE_OPEN) ||
2632 (conn->type == CONN_TYPE_AP && conn->state == AP_CONN_STATE_OPEN) ||
2633 (conn->type == CONN_TYPE_EXIT && conn->state == EXIT_CONN_STATE_OPEN) ||
2634 (conn->type == CONN_TYPE_CONTROL &&
2635 conn->state == CONTROL_CONN_STATE_OPEN))
2636 return 1;
2638 return 0;
2641 /** Return 1 if conn is in 'connecting' state, else return 0. */
2643 connection_state_is_connecting(connection_t *conn)
2645 tor_assert(conn);
2647 if (conn->marked_for_close)
2648 return 0;
2649 switch (conn->type)
2651 case CONN_TYPE_OR:
2652 return conn->state == OR_CONN_STATE_CONNECTING;
2653 case CONN_TYPE_EXIT:
2654 return conn->state == EXIT_CONN_STATE_CONNECTING;
2655 case CONN_TYPE_DIR:
2656 return conn->state == DIR_CONN_STATE_CONNECTING;
2659 return 0;
2662 /** Allocates a base64'ed authenticator for use in http or https
2663 * auth, based on the input string <b>authenticator</b>. Returns it
2664 * if success, else returns NULL. */
2665 char *
2666 alloc_http_authenticator(const char *authenticator)
2668 /* an authenticator in Basic authentication
2669 * is just the string "username:password" */
2670 const size_t authenticator_length = strlen(authenticator);
2671 /* The base64_encode function needs a minimum buffer length
2672 * of 66 bytes. */
2673 const size_t base64_authenticator_length = (authenticator_length/48+1)*66;
2674 char *base64_authenticator = tor_malloc(base64_authenticator_length);
2675 if (base64_encode(base64_authenticator, base64_authenticator_length,
2676 authenticator, authenticator_length) < 0) {
2677 tor_free(base64_authenticator); /* free and set to null */
2678 } else {
2679 /* remove extra \n at end of encoding */
2680 base64_authenticator[strlen(base64_authenticator) - 1] = 0;
2682 return base64_authenticator;
2685 /** Given a socket handle, check whether the local address (sockname) of the
2686 * socket is one that we've connected from before. If so, double-check
2687 * whether our address has changed and we need to generate keys. If we do,
2688 * call init_keys().
2690 static void
2691 client_check_address_changed(int sock)
2693 uint32_t iface_ip, ip_out;
2694 struct sockaddr_in out_addr;
2695 socklen_t out_addr_len = (socklen_t) sizeof(out_addr);
2696 uint32_t *ip;
2698 if (!last_interface_ip)
2699 get_interface_address(LOG_INFO, &last_interface_ip);
2700 if (!outgoing_addrs)
2701 outgoing_addrs = smartlist_create();
2703 if (getsockname(sock, (struct sockaddr*)&out_addr, &out_addr_len)<0) {
2704 int e = tor_socket_errno(sock);
2705 log_warn(LD_NET, "getsockname() to check for address change failed: %s",
2706 tor_socket_strerror(e));
2707 return;
2710 /* Okay. If we've used this address previously, we're okay. */
2711 ip_out = ntohl(out_addr.sin_addr.s_addr);
2712 SMARTLIST_FOREACH(outgoing_addrs, uint32_t*, ip_ptr,
2713 if (*ip_ptr == ip_out) return;
2716 /* Uh-oh. We haven't connected from this address before. Has the interface
2717 * address changed? */
2718 if (get_interface_address(LOG_INFO, &iface_ip)<0)
2719 return;
2720 ip = tor_malloc(sizeof(uint32_t));
2721 *ip = ip_out;
2723 if (iface_ip == last_interface_ip) {
2724 /* Nope, it hasn't changed. Add this address to the list. */
2725 smartlist_add(outgoing_addrs, ip);
2726 } else {
2727 /* The interface changed. We're a client, so we need to regenerate our
2728 * keys. First, reset the state. */
2729 log(LOG_NOTICE, LD_NET, "Our IP address has changed. Rotating keys...");
2730 last_interface_ip = iface_ip;
2731 SMARTLIST_FOREACH(outgoing_addrs, void*, ip_ptr, tor_free(ip_ptr));
2732 smartlist_clear(outgoing_addrs);
2733 smartlist_add(outgoing_addrs, ip);
2734 /* Okay, now change our keys. */
2735 ip_address_changed(1);
2739 /** Some systems have limited system buffers for recv and xmit on
2740 * sockets allocated in a virtual server or similar environment. For a Tor
2741 * server this can produce the "Error creating network socket: No buffer
2742 * space available" error once all available TCP buffer space is consumed.
2743 * This method will attempt to constrain the buffers allocated for the socket
2744 * to the desired size to stay below system TCP buffer limits.
2746 static void
2747 set_constrained_socket_buffers(int sock, int size)
2749 void *sz = (void*)&size;
2750 socklen_t sz_sz = (socklen_t) sizeof(size);
2751 if (setsockopt(sock, SOL_SOCKET, SO_SNDBUF, sz, sz_sz) < 0) {
2752 int e = tor_socket_errno(sock);
2753 log_warn(LD_NET, "setsockopt() to constrain send "
2754 "buffer to %d bytes failed: %s", size, tor_socket_strerror(e));
2756 if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF, sz, sz_sz) < 0) {
2757 int e = tor_socket_errno(sock);
2758 log_warn(LD_NET, "setsockopt() to constrain recv "
2759 "buffer to %d bytes failed: %s", size, tor_socket_strerror(e));
2763 /** Process new bytes that have arrived on conn-\>inbuf.
2765 * This function just passes conn to the connection-specific
2766 * connection_*_process_inbuf() function. It also passes in
2767 * package_partial if wanted.
2769 static int
2770 connection_process_inbuf(connection_t *conn, int package_partial)
2772 tor_assert(conn);
2774 switch (conn->type) {
2775 case CONN_TYPE_OR:
2776 return connection_or_process_inbuf(TO_OR_CONN(conn));
2777 case CONN_TYPE_EXIT:
2778 case CONN_TYPE_AP:
2779 return connection_edge_process_inbuf(TO_EDGE_CONN(conn),
2780 package_partial);
2781 case CONN_TYPE_DIR:
2782 return connection_dir_process_inbuf(TO_DIR_CONN(conn));
2783 case CONN_TYPE_CPUWORKER:
2784 return connection_cpu_process_inbuf(conn);
2785 case CONN_TYPE_CONTROL:
2786 return connection_control_process_inbuf(TO_CONTROL_CONN(conn));
2787 default:
2788 log_err(LD_BUG,"got unexpected conn type %d.", conn->type);
2789 tor_fragile_assert();
2790 return -1;
2794 /** Called whenever we've written data on a connection. */
2795 static int
2796 connection_flushed_some(connection_t *conn)
2798 int r = 0;
2799 tor_assert(!conn->in_flushed_some);
2800 conn->in_flushed_some = 1;
2801 if (conn->type == CONN_TYPE_DIR &&
2802 conn->state == DIR_CONN_STATE_SERVER_WRITING) {
2803 r = connection_dirserv_flushed_some(TO_DIR_CONN(conn));
2804 } else if (conn->type == CONN_TYPE_OR) {
2805 r = connection_or_flushed_some(TO_OR_CONN(conn));
2807 conn->in_flushed_some = 0;
2808 return r;
2811 /** We just finished flushing bytes from conn-\>outbuf, and there
2812 * are no more bytes remaining.
2814 * This function just passes conn to the connection-specific
2815 * connection_*_finished_flushing() function.
2817 static int
2818 connection_finished_flushing(connection_t *conn)
2820 tor_assert(conn);
2822 /* If the connection is closed, don't try to do anything more here. */
2823 if (CONN_IS_CLOSED(conn))
2824 return 0;
2826 // log_fn(LOG_DEBUG,"entered. Socket %u.", conn->s);
2828 switch (conn->type) {
2829 case CONN_TYPE_OR:
2830 return connection_or_finished_flushing(TO_OR_CONN(conn));
2831 case CONN_TYPE_AP:
2832 case CONN_TYPE_EXIT:
2833 return connection_edge_finished_flushing(TO_EDGE_CONN(conn));
2834 case CONN_TYPE_DIR:
2835 return connection_dir_finished_flushing(TO_DIR_CONN(conn));
2836 case CONN_TYPE_CPUWORKER:
2837 return connection_cpu_finished_flushing(conn);
2838 case CONN_TYPE_CONTROL:
2839 return connection_control_finished_flushing(TO_CONTROL_CONN(conn));
2840 default:
2841 log_err(LD_BUG,"got unexpected conn type %d.", conn->type);
2842 tor_fragile_assert();
2843 return -1;
2847 /** Called when our attempt to connect() to another server has just
2848 * succeeded.
2850 * This function just passes conn to the connection-specific
2851 * connection_*_finished_connecting() function.
2853 static int
2854 connection_finished_connecting(connection_t *conn)
2856 tor_assert(conn);
2857 switch (conn->type)
2859 case CONN_TYPE_OR:
2860 return connection_or_finished_connecting(TO_OR_CONN(conn));
2861 case CONN_TYPE_EXIT:
2862 return connection_edge_finished_connecting(TO_EDGE_CONN(conn));
2863 case CONN_TYPE_DIR:
2864 return connection_dir_finished_connecting(TO_DIR_CONN(conn));
2865 default:
2866 log_err(LD_BUG,"got unexpected conn type %d.", conn->type);
2867 tor_fragile_assert();
2868 return -1;
2872 /** Callback: invoked when a connection reaches an EOF event. */
2873 static int
2874 connection_reached_eof(connection_t *conn)
2876 switch (conn->type) {
2877 case CONN_TYPE_OR:
2878 return connection_or_reached_eof(TO_OR_CONN(conn));
2879 case CONN_TYPE_AP:
2880 case CONN_TYPE_EXIT:
2881 return connection_edge_reached_eof(TO_EDGE_CONN(conn));
2882 case CONN_TYPE_DIR:
2883 return connection_dir_reached_eof(TO_DIR_CONN(conn));
2884 case CONN_TYPE_CPUWORKER:
2885 return connection_cpu_reached_eof(conn);
2886 case CONN_TYPE_CONTROL:
2887 return connection_control_reached_eof(TO_CONTROL_CONN(conn));
2888 default:
2889 log_err(LD_BUG,"got unexpected conn type %d.", conn->type);
2890 tor_fragile_assert();
2891 return -1;
2895 /** Log how many bytes are used by buffers of different kinds and sizes. */
2896 void
2897 connection_dump_buffer_mem_stats(int severity)
2899 uint64_t used_by_type[_CONN_TYPE_MAX+1];
2900 uint64_t alloc_by_type[_CONN_TYPE_MAX+1];
2901 int n_conns_by_type[_CONN_TYPE_MAX+1];
2902 uint64_t total_alloc = 0;
2903 uint64_t total_used = 0;
2904 int i;
2905 smartlist_t *conns = get_connection_array();
2907 memset(used_by_type, 0, sizeof(used_by_type));
2908 memset(alloc_by_type, 0, sizeof(alloc_by_type));
2909 memset(n_conns_by_type, 0, sizeof(n_conns_by_type));
2911 SMARTLIST_FOREACH(conns, connection_t *, c,
2913 int tp = c->type;
2914 ++n_conns_by_type[tp];
2915 if (c->inbuf) {
2916 used_by_type[tp] += buf_datalen(c->inbuf);
2917 alloc_by_type[tp] += buf_allocation(c->inbuf);
2919 if (c->outbuf) {
2920 used_by_type[tp] += buf_datalen(c->outbuf);
2921 alloc_by_type[tp] += buf_allocation(c->outbuf);
2924 for (i=0; i <= _CONN_TYPE_MAX; ++i) {
2925 total_used += used_by_type[i];
2926 total_alloc += alloc_by_type[i];
2929 log(severity, LD_GENERAL,
2930 "In buffers for %d connections: "U64_FORMAT" used/"U64_FORMAT" allocated",
2931 smartlist_len(conns),
2932 U64_PRINTF_ARG(total_used), U64_PRINTF_ARG(total_alloc));
2933 for (i=_CONN_TYPE_MIN; i <= _CONN_TYPE_MAX; ++i) {
2934 if (!n_conns_by_type[i])
2935 continue;
2936 log(severity, LD_GENERAL,
2937 " For %d %s connections: "U64_FORMAT" used/"U64_FORMAT" allocated",
2938 n_conns_by_type[i], conn_type_to_string(i),
2939 U64_PRINTF_ARG(used_by_type[i]), U64_PRINTF_ARG(alloc_by_type[i]));
2943 /** Verify that connection <b>conn</b> has all of its invariants
2944 * correct. Trigger an assert if anything is invalid.
2946 void
2947 assert_connection_ok(connection_t *conn, time_t now)
2949 (void) now; /* XXXX unused. */
2950 tor_assert(conn);
2951 tor_assert(conn->type >= _CONN_TYPE_MIN);
2952 tor_assert(conn->type <= _CONN_TYPE_MAX);
2953 switch (conn->type) {
2954 case CONN_TYPE_OR:
2955 tor_assert(conn->magic == OR_CONNECTION_MAGIC);
2956 break;
2957 case CONN_TYPE_AP:
2958 case CONN_TYPE_EXIT:
2959 tor_assert(conn->magic == EDGE_CONNECTION_MAGIC);
2960 break;
2961 case CONN_TYPE_DIR:
2962 tor_assert(conn->magic == DIR_CONNECTION_MAGIC);
2963 break;
2964 case CONN_TYPE_CONTROL:
2965 tor_assert(conn->magic == CONTROL_CONNECTION_MAGIC);
2966 break;
2967 default:
2968 tor_assert(conn->magic == BASE_CONNECTION_MAGIC);
2969 break;
2972 if (conn->linked_conn) {
2973 tor_assert(conn->linked_conn->linked_conn == conn);
2974 tor_assert(conn->linked);
2976 if (conn->linked)
2977 tor_assert(conn->s < 0);
2979 if (conn->outbuf_flushlen > 0) {
2980 tor_assert(connection_is_writing(conn) || conn->write_blocked_on_bw ||
2981 (CONN_IS_EDGE(conn) && TO_EDGE_CONN(conn)->edge_blocked_on_circ));
2984 if (conn->hold_open_until_flushed)
2985 tor_assert(conn->marked_for_close);
2987 /* XXXX check: read_blocked_on_bw, write_blocked_on_bw, s, conn_array_index,
2988 * marked_for_close. */
2990 /* buffers */
2991 if (!connection_is_listener(conn)) {
2992 assert_buf_ok(conn->inbuf);
2993 assert_buf_ok(conn->outbuf);
2996 if (conn->type == CONN_TYPE_OR) {
2997 or_connection_t *or_conn = TO_OR_CONN(conn);
2998 if (conn->state == OR_CONN_STATE_OPEN) {
2999 /* tor_assert(conn->bandwidth > 0); */
3000 /* the above isn't necessarily true: if we just did a TLS
3001 * handshake but we didn't recognize the other peer, or it
3002 * gave a bad cert/etc, then we won't have assigned bandwidth,
3003 * yet it will be open. -RD
3005 // tor_assert(conn->read_bucket >= 0);
3007 // tor_assert(conn->addr && conn->port);
3008 tor_assert(conn->address);
3009 if (conn->state > OR_CONN_STATE_PROXY_READING)
3010 tor_assert(or_conn->tls);
3013 if (CONN_IS_EDGE(conn)) {
3014 edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
3015 if (edge_conn->chosen_exit_optional || edge_conn->chosen_exit_retries) {
3016 tor_assert(conn->type == CONN_TYPE_AP);
3017 tor_assert(edge_conn->chosen_exit_name);
3020 /* XXX unchecked: package window, deliver window. */
3021 if (conn->type == CONN_TYPE_AP) {
3023 tor_assert(edge_conn->socks_request);
3024 if (conn->state == AP_CONN_STATE_OPEN) {
3025 tor_assert(edge_conn->socks_request->has_finished);
3026 if (!conn->marked_for_close) {
3027 tor_assert(edge_conn->cpath_layer);
3028 assert_cpath_layer_ok(edge_conn->cpath_layer);
3032 if (conn->type == CONN_TYPE_EXIT) {
3033 tor_assert(conn->purpose == EXIT_PURPOSE_CONNECT ||
3034 conn->purpose == EXIT_PURPOSE_RESOLVE);
3036 } else if (conn->type == CONN_TYPE_DIR) {
3037 } else {
3038 /* Purpose is only used for dir and exit types currently */
3039 tor_assert(!conn->purpose);
3042 switch (conn->type)
3044 case CONN_TYPE_OR_LISTENER:
3045 case CONN_TYPE_AP_LISTENER:
3046 case CONN_TYPE_AP_TRANS_LISTENER:
3047 case CONN_TYPE_AP_NATD_LISTENER:
3048 case CONN_TYPE_DIR_LISTENER:
3049 case CONN_TYPE_CONTROL_LISTENER:
3050 case CONN_TYPE_AP_DNS_LISTENER:
3051 tor_assert(conn->state == LISTENER_STATE_READY);
3052 break;
3053 case CONN_TYPE_OR:
3054 tor_assert(conn->state >= _OR_CONN_STATE_MIN);
3055 tor_assert(conn->state <= _OR_CONN_STATE_MAX);
3056 tor_assert(TO_OR_CONN(conn)->n_circuits >= 0);
3057 break;
3058 case CONN_TYPE_EXIT:
3059 tor_assert(conn->state >= _EXIT_CONN_STATE_MIN);
3060 tor_assert(conn->state <= _EXIT_CONN_STATE_MAX);
3061 tor_assert(conn->purpose >= _EXIT_PURPOSE_MIN);
3062 tor_assert(conn->purpose <= _EXIT_PURPOSE_MAX);
3063 break;
3064 case CONN_TYPE_AP:
3065 tor_assert(conn->state >= _AP_CONN_STATE_MIN);
3066 tor_assert(conn->state <= _AP_CONN_STATE_MAX);
3067 tor_assert(TO_EDGE_CONN(conn)->socks_request);
3068 break;
3069 case CONN_TYPE_DIR:
3070 tor_assert(conn->state >= _DIR_CONN_STATE_MIN);
3071 tor_assert(conn->state <= _DIR_CONN_STATE_MAX);
3072 tor_assert(conn->purpose >= _DIR_PURPOSE_MIN);
3073 tor_assert(conn->purpose <= _DIR_PURPOSE_MAX);
3074 break;
3075 case CONN_TYPE_CPUWORKER:
3076 tor_assert(conn->state >= _CPUWORKER_STATE_MIN);
3077 tor_assert(conn->state <= _CPUWORKER_STATE_MAX);
3078 break;
3079 case CONN_TYPE_CONTROL:
3080 tor_assert(conn->state >= _CONTROL_CONN_STATE_MIN);
3081 tor_assert(conn->state <= _CONTROL_CONN_STATE_MAX);
3082 break;
3083 default:
3084 tor_assert(0);