pass bucket sizes num_read and num_written around as size_ts
[tor.git] / src / or / connection.c
blobef352f958e9a14fe99e27e0286451883a81df298
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 int connection_init_accepted_conn(connection_t *conn,
22 uint8_t listener_type);
23 static int connection_handle_listener_read(connection_t *conn, int new_type);
24 static int connection_read_bucket_should_increase(or_connection_t *conn);
25 static int connection_finished_flushing(connection_t *conn);
26 static int connection_flushed_some(connection_t *conn);
27 static int connection_finished_connecting(connection_t *conn);
28 static int connection_reached_eof(connection_t *conn);
29 static int connection_read_to_buf(connection_t *conn, int *max_to_read);
30 static int connection_process_inbuf(connection_t *conn, int package_partial);
31 static void client_check_address_changed(int sock);
32 static void set_constrained_socket_buffers(int sock, int size);
34 static uint32_t last_interface_ip = 0;
35 static smartlist_t *outgoing_addrs = NULL;
37 /**************************************************************/
39 /**
40 * Return the human-readable name for the connection type <b>type</b>
42 const char *
43 conn_type_to_string(int type)
45 static char buf[64];
46 switch (type) {
47 case CONN_TYPE_OR_LISTENER: return "OR listener";
48 case CONN_TYPE_OR: return "OR";
49 case CONN_TYPE_EXIT: return "Exit";
50 case CONN_TYPE_AP_LISTENER: return "Socks listener";
51 case CONN_TYPE_AP_TRANS_LISTENER:
52 return "Transparent pf/netfilter listener";
53 case CONN_TYPE_AP_NATD_LISTENER: return "Transparent natd listener";
54 case CONN_TYPE_AP_DNS_LISTENER: return "DNS listener";
55 case CONN_TYPE_AP: return "Socks";
56 case CONN_TYPE_DIR_LISTENER: return "Directory listener";
57 case CONN_TYPE_DIR: return "Directory";
58 case CONN_TYPE_CPUWORKER: return "CPU worker";
59 case CONN_TYPE_CONTROL_LISTENER: return "Control listener";
60 case CONN_TYPE_CONTROL: return "Control";
61 default:
62 log_warn(LD_BUG, "unknown connection type %d", type);
63 tor_snprintf(buf, sizeof(buf), "unknown [%d]", type);
64 return buf;
68 /**
69 * Return the human-readable name for the connection state <b>state</b>
70 * for the connection type <b>type</b>
72 const char *
73 conn_state_to_string(int type, int state)
75 static char buf[96];
76 switch (type) {
77 case CONN_TYPE_OR_LISTENER:
78 case CONN_TYPE_AP_LISTENER:
79 case CONN_TYPE_AP_TRANS_LISTENER:
80 case CONN_TYPE_AP_NATD_LISTENER:
81 case CONN_TYPE_AP_DNS_LISTENER:
82 case CONN_TYPE_DIR_LISTENER:
83 case CONN_TYPE_CONTROL_LISTENER:
84 if (state == LISTENER_STATE_READY)
85 return "ready";
86 break;
87 case CONN_TYPE_OR:
88 switch (state) {
89 case OR_CONN_STATE_CONNECTING: return "connect()ing";
90 case OR_CONN_STATE_PROXY_FLUSHING: return "proxy flushing";
91 case OR_CONN_STATE_PROXY_READING: return "proxy reading";
92 case OR_CONN_STATE_TLS_HANDSHAKING: return "handshaking (TLS)";
93 case OR_CONN_STATE_TLS_CLIENT_RENEGOTIATING:
94 return "renegotiating (TLS)";
95 case OR_CONN_STATE_TLS_SERVER_RENEGOTIATING:
96 return "waiting for renegotiation (TLS)";
97 case OR_CONN_STATE_OR_HANDSHAKING: return "handshaking (Tor)";
98 case OR_CONN_STATE_OPEN: return "open";
100 break;
101 case CONN_TYPE_EXIT:
102 switch (state) {
103 case EXIT_CONN_STATE_RESOLVING: return "waiting for dest info";
104 case EXIT_CONN_STATE_CONNECTING: return "connecting";
105 case EXIT_CONN_STATE_OPEN: return "open";
106 case EXIT_CONN_STATE_RESOLVEFAILED: return "resolve failed";
108 break;
109 case CONN_TYPE_AP:
110 switch (state) {
111 case AP_CONN_STATE_SOCKS_WAIT: return "waiting for socks info";
112 case AP_CONN_STATE_NATD_WAIT: return "waiting for natd dest info";
113 case AP_CONN_STATE_RENDDESC_WAIT: return "waiting for rendezvous desc";
114 case AP_CONN_STATE_CONTROLLER_WAIT: return "waiting for controller";
115 case AP_CONN_STATE_CIRCUIT_WAIT: return "waiting for circuit";
116 case AP_CONN_STATE_CONNECT_WAIT: return "waiting for connect response";
117 case AP_CONN_STATE_RESOLVE_WAIT: return "waiting for resolve response";
118 case AP_CONN_STATE_OPEN: return "open";
120 break;
121 case CONN_TYPE_DIR:
122 switch (state) {
123 case DIR_CONN_STATE_CONNECTING: return "connecting";
124 case DIR_CONN_STATE_CLIENT_SENDING: return "client sending";
125 case DIR_CONN_STATE_CLIENT_READING: return "client reading";
126 case DIR_CONN_STATE_CLIENT_FINISHED: return "client finished";
127 case DIR_CONN_STATE_SERVER_COMMAND_WAIT: return "waiting for command";
128 case DIR_CONN_STATE_SERVER_WRITING: return "writing";
130 break;
131 case CONN_TYPE_CPUWORKER:
132 switch (state) {
133 case CPUWORKER_STATE_IDLE: return "idle";
134 case CPUWORKER_STATE_BUSY_ONION: return "busy with onion";
136 break;
137 case CONN_TYPE_CONTROL:
138 switch (state) {
139 case CONTROL_CONN_STATE_OPEN: return "open (protocol v1)";
140 case CONTROL_CONN_STATE_NEEDAUTH:
141 return "waiting for authentication (protocol v1)";
143 break;
146 log_warn(LD_BUG, "unknown connection state %d (type %d)", state, type);
147 tor_snprintf(buf, sizeof(buf),
148 "unknown state [%d] on unknown [%s] connection",
149 state, conn_type_to_string(type));
150 return buf;
153 /** Allocate space for a new connection_t. This function just initializes
154 * conn; you must call connection_add() to link it into the main array.
156 * Set conn-\>type to <b>type</b>. Set conn-\>s and conn-\>conn_array_index to
157 * -1 to signify they are not yet assigned.
159 * If conn is not a listener type, allocate buffers for it. If it's
160 * an AP type, allocate space to store the socks_request.
162 * Assign a pseudorandom next_circ_id between 0 and 2**15.
164 * Initialize conn's timestamps to now.
166 connection_t *
167 connection_new(int type, int socket_family)
169 static uint32_t n_connections_allocated = 1;
170 connection_t *conn;
171 time_t now = time(NULL);
172 size_t length;
173 uint32_t magic;
175 switch (type) {
176 case CONN_TYPE_OR:
177 length = sizeof(or_connection_t);
178 magic = OR_CONNECTION_MAGIC;
179 break;
180 case CONN_TYPE_EXIT:
181 case CONN_TYPE_AP:
182 length = sizeof(edge_connection_t);
183 magic = EDGE_CONNECTION_MAGIC;
184 break;
185 case CONN_TYPE_DIR:
186 length = sizeof(dir_connection_t);
187 magic = DIR_CONNECTION_MAGIC;
188 break;
189 case CONN_TYPE_CONTROL:
190 length = sizeof(control_connection_t);
191 magic = CONTROL_CONNECTION_MAGIC;
192 break;
193 default:
194 length = sizeof(connection_t);
195 magic = BASE_CONNECTION_MAGIC;
196 break;
199 conn = tor_malloc_zero(length);
200 conn->magic = magic;
201 conn->s = -1; /* give it a default of 'not used' */
202 conn->conn_array_index = -1; /* also default to 'not used' */
204 conn->type = type;
205 conn->socket_family = socket_family;
206 if (!connection_is_listener(conn)) { /* listeners never use their buf */
207 conn->inbuf = buf_new();
208 conn->outbuf = buf_new();
210 if (type == CONN_TYPE_AP) {
211 TO_EDGE_CONN(conn)->socks_request =
212 tor_malloc_zero(sizeof(socks_request_t));
214 if (CONN_IS_EDGE(conn)) {
215 TO_EDGE_CONN(conn)->global_identifier = n_connections_allocated++;
217 if (type == CONN_TYPE_OR) {
218 TO_OR_CONN(conn)->timestamp_last_added_nonpadding = now;
219 TO_OR_CONN(conn)->next_circ_id = crypto_rand_int(1<<15);
222 conn->timestamp_created = now;
223 conn->timestamp_lastread = now;
224 conn->timestamp_lastwritten = now;
226 return conn;
229 /** Create a link between <b>conn_a</b> and <b>conn_b</b>. */
230 void
231 connection_link_connections(connection_t *conn_a, connection_t *conn_b)
233 tor_assert(conn_a->s < 0);
234 tor_assert(conn_b->s < 0);
236 conn_a->linked = 1;
237 conn_b->linked = 1;
238 conn_a->linked_conn = conn_b;
239 conn_b->linked_conn = conn_a;
242 /** Tell libevent that we don't care about <b>conn</b> any more. */
243 void
244 connection_unregister_events(connection_t *conn)
246 if (conn->read_event) {
247 if (event_del(conn->read_event))
248 log_warn(LD_BUG, "Error removing read event for %d", conn->s);
249 tor_free(conn->read_event);
251 if (conn->write_event) {
252 if (event_del(conn->write_event))
253 log_warn(LD_BUG, "Error removing write event for %d", conn->s);
254 tor_free(conn->write_event);
256 if (conn->dns_server_port) {
257 dnsserv_close_listener(conn);
261 /** Deallocate memory used by <b>conn</b>. Deallocate its buffers if
262 * necessary, close its socket if necessary, and mark the directory as dirty
263 * if <b>conn</b> is an OR or OP connection.
265 static void
266 _connection_free(connection_t *conn)
268 void *mem;
269 size_t memlen;
270 switch (conn->type) {
271 case CONN_TYPE_OR:
272 tor_assert(conn->magic == OR_CONNECTION_MAGIC);
273 mem = TO_OR_CONN(conn);
274 memlen = sizeof(or_connection_t);
275 break;
276 case CONN_TYPE_AP:
277 case CONN_TYPE_EXIT:
278 tor_assert(conn->magic == EDGE_CONNECTION_MAGIC);
279 mem = TO_EDGE_CONN(conn);
280 memlen = sizeof(edge_connection_t);
281 break;
282 case CONN_TYPE_DIR:
283 tor_assert(conn->magic == DIR_CONNECTION_MAGIC);
284 mem = TO_DIR_CONN(conn);
285 memlen = sizeof(dir_connection_t);
286 break;
287 case CONN_TYPE_CONTROL:
288 tor_assert(conn->magic == CONTROL_CONNECTION_MAGIC);
289 mem = TO_CONTROL_CONN(conn);
290 memlen = sizeof(control_connection_t);
291 break;
292 default:
293 tor_assert(conn->magic == BASE_CONNECTION_MAGIC);
294 mem = conn;
295 memlen = sizeof(connection_t);
296 break;
299 if (conn->linked) {
300 log_info(LD_GENERAL, "Freeing linked %s connection [%s] with %d "
301 "bytes on inbuf, %d on outbuf.",
302 conn_type_to_string(conn->type),
303 conn_state_to_string(conn->type, conn->state),
304 (int)buf_datalen(conn->inbuf), (int)buf_datalen(conn->outbuf));
307 if (!connection_is_listener(conn)) {
308 buf_free(conn->inbuf);
309 buf_free(conn->outbuf);
310 } else {
311 if (conn->socket_family == AF_UNIX) {
312 /* For now only control ports can be unix domain sockets
313 * and listeners at the same time */
314 tor_assert(conn->type == CONN_TYPE_CONTROL_LISTENER);
316 if (unlink(conn->address) < 0 && errno != ENOENT) {
317 log_warn(LD_NET, "Could not unlink %s: %s", conn->address,
318 strerror(errno));
323 tor_free(conn->address);
325 if (connection_speaks_cells(conn)) {
326 or_connection_t *or_conn = TO_OR_CONN(conn);
327 if (or_conn->tls) {
328 tor_tls_free(or_conn->tls);
329 or_conn->tls = NULL;
331 if (or_conn->handshake_state) {
332 or_handshake_state_free(or_conn->handshake_state);
333 or_conn->handshake_state = NULL;
335 tor_free(or_conn->nickname);
337 if (CONN_IS_EDGE(conn)) {
338 edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
339 tor_free(edge_conn->chosen_exit_name);
340 if (edge_conn->socks_request) {
341 memset(edge_conn->socks_request, 0xcc, sizeof(socks_request_t));
342 tor_free(edge_conn->socks_request);
345 if (conn->type == CONN_TYPE_CONTROL) {
346 control_connection_t *control_conn = TO_CONTROL_CONN(conn);
347 tor_free(control_conn->incoming_cmd);
350 tor_free(conn->read_event); /* Probably already freed by connection_free. */
351 tor_free(conn->write_event); /* Probably already freed by connection_free. */
353 if (conn->type == CONN_TYPE_DIR) {
354 dir_connection_t *dir_conn = TO_DIR_CONN(conn);
355 tor_free(dir_conn->requested_resource);
356 if (dir_conn->zlib_state)
357 tor_zlib_free(dir_conn->zlib_state);
358 if (dir_conn->fingerprint_stack) {
359 SMARTLIST_FOREACH(dir_conn->fingerprint_stack, char *, cp, tor_free(cp));
360 smartlist_free(dir_conn->fingerprint_stack);
362 if (dir_conn->cached_dir)
363 cached_dir_decref(dir_conn->cached_dir);
366 if (conn->s >= 0) {
367 log_debug(LD_NET,"closing fd %d.",conn->s);
368 tor_close_socket(conn->s);
369 conn->s = -1;
372 if (conn->type == CONN_TYPE_OR &&
373 !tor_digest_is_zero(TO_OR_CONN(conn)->identity_digest)) {
374 log_warn(LD_BUG, "called on OR conn with non-zeroed identity_digest");
375 connection_or_remove_from_identity_map(TO_OR_CONN(conn));
378 memset(conn, 0xAA, memlen); /* poison memory */
379 tor_free(mem);
382 /** Make sure <b>conn</b> isn't in any of the global conn lists; then free it.
384 void
385 connection_free(connection_t *conn)
387 tor_assert(conn);
388 tor_assert(!connection_is_on_closeable_list(conn));
389 tor_assert(!connection_in_array(conn));
390 if (conn->linked_conn) {
391 log_err(LD_BUG, "Called with conn->linked_conn still set.");
392 tor_fragile_assert();
393 conn->linked_conn->linked_conn = NULL;
394 if (! conn->linked_conn->marked_for_close &&
395 conn->linked_conn->reading_from_linked_conn)
396 connection_start_reading(conn->linked_conn);
397 conn->linked_conn = NULL;
399 if (connection_speaks_cells(conn)) {
400 if (!tor_digest_is_zero(TO_OR_CONN(conn)->identity_digest)) {
401 connection_or_remove_from_identity_map(TO_OR_CONN(conn));
404 if (conn->type == CONN_TYPE_CONTROL) {
405 TO_CONTROL_CONN(conn)->event_mask = 0;
406 control_update_global_event_mask();
408 connection_unregister_events(conn);
409 _connection_free(conn);
412 /** Call _connection_free() on every connection in our array, and release all
413 * storage helpd by connection.c. This is used by cpuworkers and dnsworkers
414 * when they fork, so they don't keep resources held open (especially
415 * sockets).
417 * Don't do the checks in connection_free(), because they will
418 * fail.
420 void
421 connection_free_all(void)
423 smartlist_t *conns = get_connection_array();
425 /* We don't want to log any messages to controllers. */
426 SMARTLIST_FOREACH(conns, connection_t *, conn,
427 if (conn->type == CONN_TYPE_CONTROL)
428 TO_CONTROL_CONN(conn)->event_mask = 0);
430 control_update_global_event_mask();
432 /* Unlink everything from the identity map. */
433 connection_or_clear_identity_map();
435 SMARTLIST_FOREACH(conns, connection_t *, conn, _connection_free(conn));
437 if (outgoing_addrs) {
438 SMARTLIST_FOREACH(outgoing_addrs, void*, addr, tor_free(addr));
439 smartlist_free(outgoing_addrs);
440 outgoing_addrs = NULL;
444 /** Do any cleanup needed:
445 * - Directory conns that failed to fetch a rendezvous descriptor
446 * need to inform pending rendezvous streams.
447 * - OR conns need to call rep_hist_note_*() to record status.
448 * - AP conns need to send a socks reject if necessary.
449 * - Exit conns need to call connection_dns_remove() if necessary.
450 * - AP and Exit conns need to send an end cell if they can.
451 * - DNS conns need to fail any resolves that are pending on them.
452 * - OR and edge connections need to be unlinked from circuits.
454 void
455 connection_about_to_close_connection(connection_t *conn)
457 circuit_t *circ;
458 dir_connection_t *dir_conn;
459 or_connection_t *or_conn;
460 edge_connection_t *edge_conn;
461 time_t now = time(NULL);
463 tor_assert(conn->marked_for_close);
465 if (CONN_IS_EDGE(conn)) {
466 if (!conn->edge_has_sent_end) {
467 log_warn(LD_BUG, "(Harmless.) Edge connection (marked at %s:%d) "
468 "hasn't sent end yet?",
469 conn->marked_for_close_file, conn->marked_for_close);
470 tor_fragile_assert();
474 switch (conn->type) {
475 case CONN_TYPE_DIR:
476 dir_conn = TO_DIR_CONN(conn);
477 if (conn->state < DIR_CONN_STATE_CLIENT_FINISHED) {
478 /* It's a directory connection and connecting or fetching
479 * failed: forget about this router, and maybe try again. */
480 connection_dir_request_failed(dir_conn);
482 if (conn->purpose == DIR_PURPOSE_FETCH_RENDDESC)
483 rend_client_desc_here(dir_conn->rend_query); /* give it a try */
484 /* If we were trying to fetch a v2 rend desc and did not succeed,
485 * retry as needed. (If a fetch is successful, the connection state
486 * is changed to DIR_PURPOSE_HAS_FETCHED_RENDDESC to mark that
487 * refetching is unnecessary.) */
488 if (conn->purpose == DIR_PURPOSE_FETCH_RENDDESC_V2)
489 rend_client_refetch_v2_renddesc(dir_conn->rend_query);
490 break;
491 case CONN_TYPE_OR:
492 or_conn = TO_OR_CONN(conn);
493 /* Remember why we're closing this connection. */
494 if (conn->state != OR_CONN_STATE_OPEN) {
495 if (connection_or_nonopen_was_started_here(or_conn)) {
496 rep_hist_note_connect_failed(or_conn->identity_digest, now);
497 entry_guard_register_connect_status(or_conn->identity_digest,0,now);
498 router_set_status(or_conn->identity_digest, 0);
499 control_event_or_conn_status(or_conn, OR_CONN_EVENT_FAILED,
500 control_tls_error_to_reason(or_conn->tls_error));
502 /* Inform any pending (not attached) circs that they should
503 * give up. */
504 circuit_n_conn_done(TO_OR_CONN(conn), 0);
505 } else if (conn->hold_open_until_flushed) {
506 /* We only set hold_open_until_flushed when we're intentionally
507 * closing a connection. */
508 rep_hist_note_disconnect(or_conn->identity_digest, now);
509 control_event_or_conn_status(or_conn, OR_CONN_EVENT_CLOSED,
510 control_tls_error_to_reason(or_conn->tls_error));
511 } else if (or_conn->identity_digest) {
512 rep_hist_note_connection_died(or_conn->identity_digest, now);
513 control_event_or_conn_status(or_conn, OR_CONN_EVENT_CLOSED,
514 control_tls_error_to_reason(or_conn->tls_error));
516 /* Now close all the attached circuits on it. */
517 circuit_unlink_all_from_or_conn(TO_OR_CONN(conn),
518 END_CIRC_REASON_OR_CONN_CLOSED);
519 break;
520 case CONN_TYPE_AP:
521 edge_conn = TO_EDGE_CONN(conn);
522 if (edge_conn->socks_request->has_finished == 0) {
523 /* since conn gets removed right after this function finishes,
524 * there's no point trying to send back a reply at this point. */
525 log_warn(LD_BUG,"Closing stream (marked at %s:%d) without sending"
526 " back a socks reply.",
527 conn->marked_for_close_file, conn->marked_for_close);
529 if (!edge_conn->end_reason) {
530 log_warn(LD_BUG,"Closing stream (marked at %s:%d) without having"
531 " set end_reason.",
532 conn->marked_for_close_file, conn->marked_for_close);
534 if (edge_conn->dns_server_request) {
535 log_warn(LD_BUG,"Closing stream (marked at %s:%d) without having"
536 " replied to DNS request.",
537 conn->marked_for_close_file, conn->marked_for_close);
538 dnsserv_reject_request(edge_conn);
540 control_event_stream_status(edge_conn, STREAM_EVENT_CLOSED,
541 edge_conn->end_reason);
542 circ = circuit_get_by_edge_conn(edge_conn);
543 if (circ)
544 circuit_detach_stream(circ, edge_conn);
545 break;
546 case CONN_TYPE_EXIT:
547 edge_conn = TO_EDGE_CONN(conn);
548 circ = circuit_get_by_edge_conn(edge_conn);
549 if (circ)
550 circuit_detach_stream(circ, edge_conn);
551 if (conn->state == EXIT_CONN_STATE_RESOLVING) {
552 connection_dns_remove(edge_conn);
554 break;
558 /** Return true iff connection_close_immediate() has been called on this
559 * connection. */
560 #define CONN_IS_CLOSED(c) \
561 ((c)->linked ? ((c)->linked_conn_is_closed) : ((c)->s < 0))
563 /** Close the underlying socket for <b>conn</b>, so we don't try to
564 * flush it. Must be used in conjunction with (right before)
565 * connection_mark_for_close().
567 void
568 connection_close_immediate(connection_t *conn)
570 assert_connection_ok(conn,0);
571 if (CONN_IS_CLOSED(conn)) {
572 log_err(LD_BUG,"Attempt to close already-closed connection.");
573 tor_fragile_assert();
574 return;
576 if (conn->outbuf_flushlen) {
577 log_info(LD_NET,"fd %d, type %s, state %s, %d bytes on outbuf.",
578 conn->s, conn_type_to_string(conn->type),
579 conn_state_to_string(conn->type, conn->state),
580 (int)conn->outbuf_flushlen);
583 connection_unregister_events(conn);
585 if (conn->s >= 0)
586 tor_close_socket(conn->s);
587 conn->s = -1;
588 if (conn->linked)
589 conn->linked_conn_is_closed = 1;
590 if (!connection_is_listener(conn)) {
591 buf_clear(conn->outbuf);
592 conn->outbuf_flushlen = 0;
596 /** Mark <b>conn</b> to be closed next time we loop through
597 * conn_close_if_marked() in main.c. */
598 void
599 _connection_mark_for_close(connection_t *conn, int line, const char *file)
601 assert_connection_ok(conn,0);
602 tor_assert(line);
603 tor_assert(line < 1<<16); /* marked_for_close can only fit a uint16_t. */
604 tor_assert(file);
606 if (conn->marked_for_close) {
607 log(LOG_WARN,LD_BUG,"Duplicate call to connection_mark_for_close at %s:%d"
608 " (first at %s:%d)", file, line, conn->marked_for_close_file,
609 conn->marked_for_close);
610 tor_fragile_assert();
611 return;
614 conn->marked_for_close = line;
615 conn->marked_for_close_file = file;
616 add_connection_to_closeable_list(conn);
618 /* in case we're going to be held-open-til-flushed, reset
619 * the number of seconds since last successful write, so
620 * we get our whole 15 seconds */
621 conn->timestamp_lastwritten = time(NULL);
624 /** Find each connection that has hold_open_until_flushed set to
625 * 1 but hasn't written in the past 15 seconds, and set
626 * hold_open_until_flushed to 0. This means it will get cleaned
627 * up in the next loop through close_if_marked() in main.c.
629 void
630 connection_expire_held_open(void)
632 time_t now;
633 smartlist_t *conns = get_connection_array();
635 now = time(NULL);
637 SMARTLIST_FOREACH(conns, connection_t *, conn,
639 /* If we've been holding the connection open, but we haven't written
640 * for 15 seconds...
642 if (conn->hold_open_until_flushed) {
643 tor_assert(conn->marked_for_close);
644 if (now - conn->timestamp_lastwritten >= 15) {
645 int severity;
646 if (conn->type == CONN_TYPE_EXIT ||
647 (conn->type == CONN_TYPE_DIR &&
648 conn->purpose == DIR_PURPOSE_SERVER))
649 severity = LOG_INFO;
650 else
651 severity = LOG_NOTICE;
652 log_fn(severity, LD_NET,
653 "Giving up on marked_for_close conn that's been flushing "
654 "for 15s (fd %d, type %s, state %s).",
655 conn->s, conn_type_to_string(conn->type),
656 conn_state_to_string(conn->type, conn->state));
657 conn->hold_open_until_flushed = 0;
663 /** Create an AF_INET listenaddr struct.
664 * <b>listenaddress</b> provides the host and optionally the port information
665 * for the new structure. If no port is provided in <b>listenaddress</b> then
666 * <b>listenport</b> is used.
668 * If not NULL <b>readable_addrress</b> will contain a copy of the host part of
669 * <b>listenaddress</b>.
671 * The listenaddr struct has to be freed by the caller.
673 static struct sockaddr_in *
674 create_inet_sockaddr(const char *listenaddress, uint16_t listenport,
675 char **readable_address) {
676 struct sockaddr_in *listenaddr = NULL;
677 uint32_t addr;
678 uint16_t usePort = 0;
680 if (parse_addr_port(LOG_WARN,
681 listenaddress, readable_address, &addr, &usePort)<0) {
682 log_warn(LD_CONFIG,
683 "Error parsing/resolving ListenAddress %s", listenaddress);
684 goto err;
686 if (usePort==0)
687 usePort = listenport;
689 listenaddr = tor_malloc_zero(sizeof(struct sockaddr_in));
690 listenaddr->sin_addr.s_addr = htonl(addr);
691 listenaddr->sin_family = AF_INET;
692 listenaddr->sin_port = htons((uint16_t) usePort);
694 return listenaddr;
696 err:
697 tor_free(listenaddr);
698 return NULL;
701 #ifdef HAVE_SYS_UN_H
702 /** Create an AF_UNIX listenaddr struct.
703 * <b>listenaddress</b> provides the path to the unix socket.
705 * Eventually <b>listenaddress</b> will also optionally contain user, group,
706 * and file permissions for the new socket. But not yet. XXX
707 * Also, since we do not create the socket here the information doesn't help
708 * here.
710 * If not NULL <b>readable_addrress</b> will contain a copy of the path part of
711 * <b>listenaddress</b>.
713 * The listenaddr struct has to be freed by the caller.
715 static struct sockaddr_un *
716 create_unix_sockaddr(const char *listenaddress, char **readable_address)
718 struct sockaddr_un *sockaddr = NULL;
720 sockaddr = tor_malloc_zero(sizeof(struct sockaddr_un));
721 sockaddr->sun_family = AF_UNIX;
722 strncpy(sockaddr->sun_path, listenaddress, sizeof(sockaddr->sun_path));
724 if (readable_address)
725 *readable_address = tor_strdup(listenaddress);
727 return sockaddr;
729 #else
730 static struct sockaddr *
731 create_unix_sockaddr(const char *listenaddress, char **readable_address)
733 (void)listenaddress;
734 (void)readable_address;
735 log_fn(LOG_ERR, LD_BUG,
736 "Unix domain sockets not supported, yet we tried to create one.");
737 assert(0);
739 #endif /* HAVE_SYS_UN_H */
741 /** Bind a new non-blocking socket listening to the socket described
742 * by <b>listensockaddr</b>.
744 * <b>address</b> is only used for logging purposes and to add the information
745 * to the conn.
747 static connection_t *
748 connection_create_listener(struct sockaddr *listensockaddr, int type,
749 char* address)
751 connection_t *conn;
752 int s; /* the socket we're going to make */
753 uint16_t usePort = 0;
754 int start_reading = 0;
756 if (get_n_open_sockets() >= get_options()->_ConnLimit-1) {
757 int n_conns = get_n_open_sockets();
758 log_warn(LD_NET,"Failing because we have %d connections already. Please "
759 "raise your ulimit -n.", n_conns);
760 control_event_general_status(LOG_WARN, "TOO_MANY_CONNECTIONS CURRENT=%d",
761 n_conns);
762 return NULL;
765 if (listensockaddr->sa_family == AF_INET) {
766 int is_tcp = (type != CONN_TYPE_AP_DNS_LISTENER);
767 #ifndef MS_WINDOWS
768 int one=1;
769 #endif
770 if (is_tcp)
771 start_reading = 1;
773 usePort = ntohs( (uint16_t)
774 ((struct sockaddr_in *)listensockaddr)->sin_port);
776 log_notice(LD_NET, "Opening %s on %s:%d",
777 conn_type_to_string(type), address, usePort);
779 s = tor_open_socket(PF_INET,
780 is_tcp ? SOCK_STREAM : SOCK_DGRAM,
781 is_tcp ? IPPROTO_TCP: IPPROTO_UDP);
782 if (s < 0) {
783 log_warn(LD_NET,"Socket creation failed.");
784 goto err;
787 #ifndef MS_WINDOWS
788 /* REUSEADDR on normal places means you can rebind to the port
789 * right after somebody else has let it go. But REUSEADDR on win32
790 * means you can bind to the port _even when somebody else
791 * already has it bound_. So, don't do that on Win32. */
792 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (void*) &one, sizeof(one));
793 #endif
795 if (bind(s,listensockaddr,sizeof(struct sockaddr_in)) < 0) {
796 const char *helpfulhint = "";
797 int e = tor_socket_errno(s);
798 if (ERRNO_IS_EADDRINUSE(e))
799 helpfulhint = ". Is Tor already running?";
800 log_warn(LD_NET, "Could not bind to %s:%u: %s%s", address, usePort,
801 tor_socket_strerror(e), helpfulhint);
802 tor_close_socket(s);
803 goto err;
806 if (is_tcp) {
807 if (listen(s,SOMAXCONN) < 0) {
808 log_warn(LD_NET, "Could not listen on %s:%u: %s", address, usePort,
809 tor_socket_strerror(tor_socket_errno(s)));
810 tor_close_socket(s);
811 goto err;
814 #ifdef HAVE_SYS_UN_H
815 } else if (listensockaddr->sa_family == AF_UNIX) {
816 int len;
817 start_reading = 1;
819 /* For now only control ports can be unix domain sockets
820 * and listeners at the same time */
821 tor_assert(type == CONN_TYPE_CONTROL_LISTENER);
823 log_notice(LD_NET, "Opening %s on %s",
824 conn_type_to_string(type), address);
826 if (unlink(address) < 0 && errno != ENOENT) {
827 log_warn(LD_NET, "Could not unlink %s: %s", address,
828 strerror(errno));
829 goto err;
831 s = tor_open_socket(AF_UNIX, SOCK_STREAM, 0);
832 if (s < 0) {
833 log_warn(LD_NET,"Socket creation failed: %s.", strerror(errno));
834 goto err;
837 len = strlen(((struct sockaddr_un *)listensockaddr)->sun_path) +
838 sizeof(((struct sockaddr_un *)listensockaddr)->sun_family);
839 if (bind(s, listensockaddr, len) == -1) {
840 log_warn(LD_NET,"Bind to %s failed: %s.", address,
841 tor_socket_strerror(tor_socket_errno(s)));
842 goto err;
845 if (listen(s,SOMAXCONN) < 0) {
846 log_warn(LD_NET, "Could not listen on %s: %s", address,
847 tor_socket_strerror(tor_socket_errno(s)));
848 tor_close_socket(s);
849 goto err;
851 #endif /* HAVE_SYS_UN_H */
852 } else {
853 log_err(LD_BUG,"Got unexpected address family %d.",
854 listensockaddr->sa_family);
855 tor_assert(0);
858 set_socket_nonblocking(s);
860 conn = connection_new(type, listensockaddr->sa_family);
861 conn->socket_family = listensockaddr->sa_family;
862 conn->s = s;
863 conn->address = tor_strdup(address);
864 conn->port = usePort;
866 if (connection_add(conn) < 0) { /* no space, forget it */
867 log_warn(LD_NET,"connection_add for listener failed. Giving up.");
868 connection_free(conn);
869 goto err;
872 log_debug(LD_NET,"%s listening on port %u.",
873 conn_type_to_string(type), usePort);
875 conn->state = LISTENER_STATE_READY;
876 if (start_reading) {
877 connection_start_reading(conn);
878 } else {
879 tor_assert(type == CONN_TYPE_AP_DNS_LISTENER);
880 dnsserv_configure_listener(conn);
883 return conn;
885 err:
886 return NULL;
889 /** Do basic sanity checking on a newly received socket. Return 0
890 * if it looks ok, else return -1. */
891 static int
892 check_sockaddr_in(struct sockaddr *sa, int len, int level)
894 int ok = 1;
895 struct sockaddr_in *sin=(struct sockaddr_in*)sa;
897 if (len != sizeof(struct sockaddr_in)) {
898 log_fn(level, LD_NET, "Length of address not as expected: %d vs %d",
899 len,(int)sizeof(struct sockaddr_in));
900 ok = 0;
902 if (sa->sa_family != AF_INET) {
903 log_fn(level, LD_NET, "Family of address not as expected: %d vs %d",
904 sa->sa_family, AF_INET);
905 ok = 0;
907 if (sin->sin_addr.s_addr == 0 || sin->sin_port == 0) {
908 log_fn(level, LD_NET,
909 "Address for new connection has address/port equal to zero.");
910 ok = 0;
912 return ok ? 0 : -1;
915 /** The listener connection <b>conn</b> told poll() it wanted to read.
916 * Call accept() on conn-\>s, and add the new connection if necessary.
918 static int
919 connection_handle_listener_read(connection_t *conn, int new_type)
921 int news; /* the new socket */
922 connection_t *newconn;
923 /* information about the remote peer when connecting to other routers */
924 struct sockaddr_in remote;
925 char addrbuf[256];
926 /* length of the remote address. Must be whatever accept() needs. */
927 socklen_t remotelen = sizeof(addrbuf);
928 char tmpbuf[INET_NTOA_BUF_LEN];
929 or_options_t *options = get_options();
931 tor_assert((size_t)remotelen >= sizeof(struct sockaddr_in));
932 memset(addrbuf, 0, sizeof(addrbuf));
934 news = tor_accept_socket(conn->s,(struct sockaddr *)&addrbuf,&remotelen);
935 if (news < 0) { /* accept() error */
936 int e = tor_socket_errno(conn->s);
937 if (ERRNO_IS_ACCEPT_EAGAIN(e)) {
938 return 0; /* he hung up before we could accept(). that's fine. */
939 } else if (ERRNO_IS_ACCEPT_RESOURCE_LIMIT(e)) {
940 log_notice(LD_NET,"accept failed: %s. Dropping incoming connection.",
941 tor_socket_strerror(e));
942 return 0;
944 /* else there was a real error. */
945 log_warn(LD_NET,"accept() failed: %s. Closing listener.",
946 tor_socket_strerror(e));
947 connection_mark_for_close(conn);
948 return -1;
950 log_debug(LD_NET,
951 "Connection accepted on socket %d (child of fd %d).",
952 news,conn->s);
954 set_socket_nonblocking(news);
956 if (options->ConstrainedSockets)
957 set_constrained_socket_buffers(news, (int)options->ConstrainedSockSize);
959 if (((struct sockaddr*)addrbuf)->sa_family != conn->socket_family) {
960 /* This is annoying, but can apparently happen on some Darwins. */
961 log_info(LD_BUG, "A listener connection returned a socket with a "
962 "mismatched family. %s for addr_family %d gave us a socket "
963 "with address family %d. Dropping.",
964 conn_type_to_string(conn->type),
965 (int)conn->socket_family,
966 (int)((struct sockaddr*)addrbuf)->sa_family);
967 tor_close_socket(news);
968 return 0;
971 if (conn->socket_family == AF_INET) {
972 if (check_sockaddr_in((struct sockaddr*)addrbuf, remotelen, LOG_INFO)<0) {
973 log_info(LD_NET,
974 "accept() returned a strange address; trying getsockname().");
975 remotelen=256;
976 memset(addrbuf, 0, sizeof(addrbuf));
977 if (getsockname(news, (struct sockaddr*)addrbuf, &remotelen)<0) {
978 int e = tor_socket_errno(news);
979 log_warn(LD_NET, "getsockname() for new connection failed: %s",
980 tor_socket_strerror(e));
981 } else {
982 if (check_sockaddr_in((struct sockaddr*)addrbuf, remotelen,
983 LOG_WARN) < 0) {
984 log_warn(LD_NET,"Something's wrong with this conn. Closing it.");
985 tor_close_socket(news);
986 return 0;
990 memcpy(&remote, addrbuf, sizeof(struct sockaddr_in));
992 /* process entrance policies here, before we even create the connection */
993 if (new_type == CONN_TYPE_AP) {
994 /* check sockspolicy to see if we should accept it */
995 if (socks_policy_permits_address(ntohl(remote.sin_addr.s_addr)) == 0) {
996 tor_inet_ntoa(&remote.sin_addr, tmpbuf, sizeof(tmpbuf));
997 log_notice(LD_APP,
998 "Denying socks connection from untrusted address %s.",
999 tmpbuf);
1000 tor_close_socket(news);
1001 return 0;
1004 if (new_type == CONN_TYPE_DIR) {
1005 /* check dirpolicy to see if we should accept it */
1006 if (dir_policy_permits_address(ntohl(remote.sin_addr.s_addr)) == 0) {
1007 tor_inet_ntoa(&remote.sin_addr, tmpbuf, sizeof(tmpbuf));
1008 log_notice(LD_DIRSERV,"Denying dir connection from address %s.",
1009 tmpbuf);
1010 tor_close_socket(news);
1011 return 0;
1015 newconn = connection_new(new_type, conn->socket_family);
1016 newconn->s = news;
1018 /* remember the remote address */
1019 newconn->addr = ntohl(remote.sin_addr.s_addr);
1020 newconn->port = ntohs(remote.sin_port);
1021 newconn->address = tor_dup_addr(newconn->addr);
1023 } else if (conn->socket_family == AF_UNIX) {
1024 /* For now only control ports can be unix domain sockets
1025 * and listeners at the same time */
1026 tor_assert(conn->type == CONN_TYPE_CONTROL_LISTENER);
1028 newconn = connection_new(new_type, conn->socket_family);
1029 newconn->s = news;
1031 /* remember the remote address -- do we have anything sane to put here? */
1032 newconn->addr = 0;
1033 newconn->port = 1;
1034 newconn->address = tor_strdup(conn->address);
1035 } else {
1036 tor_assert(0);
1039 if (connection_add(newconn) < 0) { /* no space, forget it */
1040 connection_free(newconn);
1041 return 0; /* no need to tear down the parent */
1044 if (connection_init_accepted_conn(newconn, conn->type) < 0) {
1045 connection_mark_for_close(newconn);
1046 return 0;
1048 return 0;
1051 /** Initialize states for newly accepted connection <b>conn</b>.
1052 * If conn is an OR, start the tls handshake.
1053 * If conn is a transparent AP, get its original destination
1054 * and place it in circuit_wait.
1056 static int
1057 connection_init_accepted_conn(connection_t *conn, uint8_t listener_type)
1059 connection_start_reading(conn);
1061 switch (conn->type) {
1062 case CONN_TYPE_OR:
1063 control_event_or_conn_status(TO_OR_CONN(conn), OR_CONN_EVENT_NEW, 0);
1064 return connection_tls_start_handshake(TO_OR_CONN(conn), 1);
1065 case CONN_TYPE_AP:
1066 switch (listener_type) {
1067 case CONN_TYPE_AP_LISTENER:
1068 conn->state = AP_CONN_STATE_SOCKS_WAIT;
1069 break;
1070 case CONN_TYPE_AP_TRANS_LISTENER:
1071 conn->state = AP_CONN_STATE_CIRCUIT_WAIT;
1072 return connection_ap_process_transparent(TO_EDGE_CONN(conn));
1073 case CONN_TYPE_AP_NATD_LISTENER:
1074 conn->state = AP_CONN_STATE_NATD_WAIT;
1075 break;
1077 break;
1078 case CONN_TYPE_DIR:
1079 conn->purpose = DIR_PURPOSE_SERVER;
1080 conn->state = DIR_CONN_STATE_SERVER_COMMAND_WAIT;
1081 break;
1082 case CONN_TYPE_CONTROL:
1083 conn->state = CONTROL_CONN_STATE_NEEDAUTH;
1084 break;
1086 return 0;
1089 /** Take conn, make a nonblocking socket; try to connect to
1090 * addr:port (they arrive in *host order*). If fail, return -1. Else
1091 * assign s to conn-\>s: if connected return 1, if EAGAIN return 0.
1093 * address is used to make the logs useful.
1095 * On success, add conn to the list of polled connections.
1098 connection_connect(connection_t *conn, const char *address,
1099 uint32_t addr, uint16_t port)
1101 int s, inprogress = 0;
1102 struct sockaddr_in dest_addr;
1103 or_options_t *options = get_options();
1105 if (get_n_open_sockets() >= get_options()->_ConnLimit-1) {
1106 int n_conns = get_n_open_sockets();
1107 log_warn(LD_NET,"Failing because we have %d connections already. Please "
1108 "raise your ulimit -n.", n_conns);
1109 control_event_general_status(LOG_WARN, "TOO_MANY_CONNECTIONS CURRENT=%d",
1110 n_conns);
1111 return -1;
1114 s = tor_open_socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
1115 if (s < 0) {
1116 log_warn(LD_NET,"Error creating network socket: %s",
1117 tor_socket_strerror(tor_socket_errno(-1)));
1118 return -1;
1121 if (options->OutboundBindAddress) {
1122 struct sockaddr_in ext_addr;
1124 memset(&ext_addr, 0, sizeof(ext_addr));
1125 ext_addr.sin_family = AF_INET;
1126 ext_addr.sin_port = 0;
1127 if (!tor_inet_aton(options->OutboundBindAddress, &ext_addr.sin_addr)) {
1128 log_warn(LD_CONFIG,"Outbound bind address '%s' didn't parse. Ignoring.",
1129 options->OutboundBindAddress);
1130 } else {
1131 if (bind(s, (struct sockaddr*)&ext_addr, sizeof(ext_addr)) < 0) {
1132 log_warn(LD_NET,"Error binding network socket: %s",
1133 tor_socket_strerror(tor_socket_errno(s)));
1134 tor_close_socket(s);
1135 return -1;
1140 set_socket_nonblocking(s);
1142 if (options->ConstrainedSockets)
1143 set_constrained_socket_buffers(s, (int)options->ConstrainedSockSize);
1145 memset(&dest_addr,0,sizeof(dest_addr));
1146 dest_addr.sin_family = AF_INET;
1147 dest_addr.sin_port = htons(port);
1148 dest_addr.sin_addr.s_addr = htonl(addr);
1150 log_debug(LD_NET,"Connecting to %s:%u.",escaped_safe_str(address),port);
1152 if (connect(s,(struct sockaddr *)&dest_addr,sizeof(dest_addr)) < 0) {
1153 int e = tor_socket_errno(s);
1154 if (!ERRNO_IS_CONN_EINPROGRESS(e)) {
1155 /* yuck. kill it. */
1156 log_info(LD_NET,
1157 "connect() to %s:%u failed: %s",escaped_safe_str(address),
1158 port, tor_socket_strerror(e));
1159 tor_close_socket(s);
1160 return -1;
1161 } else {
1162 inprogress = 1;
1166 if (!server_mode(options))
1167 client_check_address_changed(s);
1169 /* it succeeded. we're connected. */
1170 log_fn(inprogress?LOG_DEBUG:LOG_INFO, LD_NET,
1171 "Connection to %s:%u %s (sock %d).",escaped_safe_str(address),
1172 port, inprogress?"in progress":"established", s);
1173 conn->s = s;
1174 if (connection_add(conn) < 0) /* no space, forget it */
1175 return -1;
1176 return inprogress ? 0 : 1;
1180 * Launch any configured listener connections of type <b>type</b>. (A
1181 * listener is configured if <b>port_option</b> is non-zero. If any
1182 * ListenAddress configuration options are given in <b>cfg</b>, create a
1183 * connection binding to each one. Otherwise, create a single
1184 * connection binding to the address <b>default_addr</b>.)
1186 * Only launch the listeners of this type that are not already open, and
1187 * only close listeners that are no longer wanted. Existing listeners
1188 * that are still configured are not touched.
1190 * If <b>disable_all_conns</b> is set, then never open new conns, and
1191 * close the existing ones.
1193 * Add all old conns that should be closed to <b>replaced_conns</b>.
1194 * Add all new connections to <b>new_conns</b>.
1196 static int
1197 retry_listeners(int type, config_line_t *cfg,
1198 int port_option, const char *default_addr,
1199 smartlist_t *replaced_conns,
1200 smartlist_t *new_conns,
1201 int disable_all_conns,
1202 int socket_family)
1204 smartlist_t *launch = smartlist_create(), *conns;
1205 int free_launch_elts = 1;
1206 int r;
1207 config_line_t *c;
1208 connection_t *conn;
1209 config_line_t *line;
1211 tor_assert(socket_family == AF_INET || socket_family == AF_UNIX);
1213 if (cfg && port_option) {
1214 for (c = cfg; c; c = c->next) {
1215 smartlist_add(launch, c);
1217 free_launch_elts = 0;
1218 } else if (port_option) {
1219 line = tor_malloc_zero(sizeof(config_line_t));
1220 line->key = tor_strdup("");
1221 line->value = tor_strdup(default_addr);
1222 smartlist_add(launch, line);
1226 SMARTLIST_FOREACH(launch, config_line_t *, l,
1227 log_fn(LOG_NOTICE, "#%s#%s", l->key, l->value));
1230 conns = get_connection_array();
1231 SMARTLIST_FOREACH(conns, connection_t *, conn,
1233 if (conn->type != type ||
1234 conn->socket_family != socket_family ||
1235 conn->marked_for_close)
1236 continue;
1237 /* Okay, so this is a listener. Is it configured? */
1238 line = NULL;
1239 SMARTLIST_FOREACH(launch, config_line_t *, wanted,
1241 char *address=NULL;
1242 uint16_t port;
1243 switch (socket_family) {
1244 case AF_INET:
1245 if (!parse_addr_port(LOG_WARN,
1246 wanted->value, &address, NULL, &port)) {
1247 int addr_matches = !strcasecmp(address, conn->address);
1248 tor_free(address);
1249 if (! port)
1250 port = port_option;
1251 if (port == conn->port && addr_matches) {
1252 line = wanted;
1253 break;
1256 break;
1257 case AF_UNIX:
1258 if (!strcasecmp(wanted->value, conn->address)) {
1259 line = wanted;
1260 break;
1262 break;
1263 default:
1264 tor_assert(0);
1267 if (!line || disable_all_conns) {
1268 /* This one isn't configured. Close it. */
1269 log_notice(LD_NET, "Closing no-longer-configured %s on %s:%d",
1270 conn_type_to_string(type), conn->address, conn->port);
1271 if (replaced_conns) {
1272 smartlist_add(replaced_conns, conn);
1273 } else {
1274 connection_close_immediate(conn);
1275 connection_mark_for_close(conn);
1277 } else {
1278 /* It's configured; we don't need to launch it. */
1279 // log_debug(LD_NET, "Already have %s on %s:%d",
1280 // conn_type_to_string(type), conn->address, conn->port);
1281 smartlist_remove(launch, line);
1282 if (free_launch_elts)
1283 config_free_lines(line);
1287 /* Now open all the listeners that are configured but not opened. */
1288 r = 0;
1289 if (!disable_all_conns) {
1290 SMARTLIST_FOREACH(launch, config_line_t *, cfg_line,
1292 char *address = NULL;
1293 struct sockaddr *listensockaddr;
1295 switch (socket_family) {
1296 case AF_INET:
1297 listensockaddr = (struct sockaddr *)
1298 create_inet_sockaddr(cfg_line->value,
1299 (uint16_t) port_option,
1300 &address);
1301 break;
1302 case AF_UNIX:
1303 listensockaddr = (struct sockaddr *)
1304 create_unix_sockaddr(cfg_line->value,
1305 &address);
1306 break;
1307 default:
1308 tor_assert(0);
1311 if (listensockaddr) {
1312 conn = connection_create_listener(listensockaddr, type, address);
1313 tor_free(listensockaddr);
1314 tor_free(address);
1315 } else
1316 conn = NULL;
1318 if (!conn) {
1319 r = -1;
1320 } else {
1321 if (new_conns)
1322 smartlist_add(new_conns, conn);
1327 if (free_launch_elts) {
1328 SMARTLIST_FOREACH(launch, config_line_t *, cfg_line,
1329 config_free_lines(cfg_line));
1331 smartlist_free(launch);
1333 return r;
1336 /** Launch listeners for each port you should have open. Only launch
1337 * listeners who are not already open, and only close listeners we no longer
1338 * want.
1340 * Add all old conns that should be closed to <b>replaced_conns</b>.
1341 * Add all new connections to <b>new_conns</b>.
1344 retry_all_listeners(smartlist_t *replaced_conns,
1345 smartlist_t *new_conns)
1347 or_options_t *options = get_options();
1349 if (retry_listeners(CONN_TYPE_OR_LISTENER, options->ORListenAddress,
1350 options->ORPort, "0.0.0.0",
1351 replaced_conns, new_conns, options->ClientOnly,
1352 AF_INET)<0)
1353 return -1;
1354 if (retry_listeners(CONN_TYPE_DIR_LISTENER, options->DirListenAddress,
1355 options->DirPort, "0.0.0.0",
1356 replaced_conns, new_conns, options->ClientOnly,
1357 AF_INET)<0)
1358 return -1;
1359 if (retry_listeners(CONN_TYPE_AP_LISTENER, options->SocksListenAddress,
1360 options->SocksPort, "127.0.0.1",
1361 replaced_conns, new_conns, 0,
1362 AF_INET)<0)
1363 return -1;
1364 if (retry_listeners(CONN_TYPE_AP_TRANS_LISTENER, options->TransListenAddress,
1365 options->TransPort, "127.0.0.1",
1366 replaced_conns, new_conns, 0,
1367 AF_INET)<0)
1368 return -1;
1369 if (retry_listeners(CONN_TYPE_AP_NATD_LISTENER, options->NatdListenAddress,
1370 options->NatdPort, "127.0.0.1",
1371 replaced_conns, new_conns, 0,
1372 AF_INET)<0)
1373 return -1;
1374 if (retry_listeners(CONN_TYPE_AP_DNS_LISTENER, options->DNSListenAddress,
1375 options->DNSPort, "127.0.0.1",
1376 replaced_conns, new_conns, 0,
1377 AF_INET)<0)
1378 return -1;
1379 if (retry_listeners(CONN_TYPE_CONTROL_LISTENER,
1380 options->ControlListenAddress,
1381 options->ControlPort, "127.0.0.1",
1382 replaced_conns, new_conns, 0,
1383 AF_INET)<0)
1384 return -1;
1385 if (retry_listeners(CONN_TYPE_CONTROL_LISTENER,
1386 options->ControlSocket,
1387 options->ControlSocket ? 1 : 0, NULL,
1388 replaced_conns, new_conns, 0,
1389 AF_UNIX)<0)
1390 return -1;
1392 return 0;
1395 /** Return 1 if we should apply rate limiting to <b>conn</b>,
1396 * and 0 otherwise. Right now this just checks if it's an internal
1397 * IP address or an internal connection. */
1398 static int
1399 connection_is_rate_limited(connection_t *conn)
1401 if (conn->linked || is_internal_IP(conn->addr, 0))
1402 return 0;
1403 else
1404 return 1;
1407 extern int global_read_bucket, global_write_bucket;
1408 extern int global_relayed_read_bucket, global_relayed_write_bucket;
1410 /** Did either global write bucket run dry last second? If so,
1411 * we are likely to run dry again this second, so be stingy with the
1412 * tokens we just put in. */
1413 static int write_buckets_empty_last_second = 0;
1415 /** How many seconds of no active local circuits will make the
1416 * connection revert to the "relayed" bandwidth class? */
1417 #define CLIENT_IDLE_TIME_FOR_PRIORITY 30
1419 /** Return 1 if <b>conn</b> should use tokens from the "relayed"
1420 * bandwidth rates, else 0. Currently, only OR conns with bandwidth
1421 * class 1, and directory conns that are serving data out, count.
1423 static int
1424 connection_counts_as_relayed_traffic(connection_t *conn, time_t now)
1426 if (conn->type == CONN_TYPE_OR &&
1427 TO_OR_CONN(conn)->client_used + CLIENT_IDLE_TIME_FOR_PRIORITY < now)
1428 return 1;
1429 if (conn->type == CONN_TYPE_DIR && DIR_CONN_IS_SERVER(conn))
1430 return 1;
1431 return 0;
1434 /** Helper function to decide how many bytes out of <b>global_bucket</b>
1435 * we're willing to use for this transaction. <b>base</b> is the size
1436 * of a cell on the network; <b>priority</b> says whether we should
1437 * write many of them or just a few; and <b>conn_bucket</b> (if
1438 * non-negative) provides an upper limit for our answer. */
1439 static int
1440 connection_bucket_round_robin(int base, int priority,
1441 int global_bucket, int conn_bucket)
1443 int at_most;
1444 int num_bytes_high = (priority ? 32 : 16) * base;
1445 int num_bytes_low = (priority ? 4 : 2) * base;
1447 /* Do a rudimentary round-robin so one circuit can't hog a connection.
1448 * Pick at most 32 cells, at least 4 cells if possible, and if we're in
1449 * the middle pick 1/8 of the available bandwidth. */
1450 at_most = global_bucket / 8;
1451 at_most -= (at_most % base); /* round down */
1452 if (at_most > num_bytes_high) /* 16 KB, or 8 KB for low-priority */
1453 at_most = num_bytes_high;
1454 else if (at_most < num_bytes_low) /* 2 KB, or 1 KB for low-priority */
1455 at_most = num_bytes_low;
1457 if (at_most > global_bucket)
1458 at_most = global_bucket;
1460 if (conn_bucket >= 0 && at_most > conn_bucket)
1461 at_most = conn_bucket;
1463 if (at_most < 0)
1464 return 0;
1465 return at_most;
1468 /** How many bytes at most can we read onto this connection? */
1469 static int
1470 connection_bucket_read_limit(connection_t *conn, time_t now)
1472 int base = connection_speaks_cells(conn) ?
1473 CELL_NETWORK_SIZE : RELAY_PAYLOAD_SIZE;
1474 int priority = conn->type != CONN_TYPE_DIR;
1475 int conn_bucket = -1;
1476 int global_bucket = global_read_bucket;
1478 if (connection_speaks_cells(conn)) {
1479 or_connection_t *or_conn = TO_OR_CONN(conn);
1480 if (conn->state == OR_CONN_STATE_OPEN)
1481 conn_bucket = or_conn->read_bucket;
1484 if (!connection_is_rate_limited(conn)) {
1485 /* be willing to read on local conns even if our buckets are empty */
1486 return conn_bucket>=0 ? conn_bucket : 1<<14;
1489 if (connection_counts_as_relayed_traffic(conn, now) &&
1490 global_relayed_read_bucket <= global_read_bucket)
1491 global_bucket = global_relayed_read_bucket;
1493 return connection_bucket_round_robin(base, priority,
1494 global_bucket, conn_bucket);
1497 /** How many bytes at most can we write onto this connection? */
1499 connection_bucket_write_limit(connection_t *conn, time_t now)
1501 int base = connection_speaks_cells(conn) ?
1502 CELL_NETWORK_SIZE : RELAY_PAYLOAD_SIZE;
1503 int priority = conn->type != CONN_TYPE_DIR;
1504 int global_bucket = global_write_bucket;
1506 if (!connection_is_rate_limited(conn)) {
1507 /* be willing to write to local conns even if our buckets are empty */
1508 return conn->outbuf_flushlen;
1511 if (connection_counts_as_relayed_traffic(conn, now) &&
1512 global_relayed_write_bucket <= global_write_bucket)
1513 global_bucket = global_relayed_write_bucket;
1515 return connection_bucket_round_robin(base, priority, global_bucket,
1516 conn->outbuf_flushlen);
1519 /** Return 1 if the global write buckets are low enough that we
1520 * shouldn't send <b>attempt</b> bytes of low-priority directory stuff
1521 * out to <b>conn</b>. Else return 0.
1523 * Priority is 1 for v1 requests (directories and running-routers),
1524 * and 2 for v2 requests (statuses and descriptors). But see FFFF in
1525 * directory_handle_command_get() for why we don't use priority 2 yet.
1527 * There are a lot of parameters we could use here:
1528 * - global_relayed_write_bucket. Low is bad.
1529 * - global_write_bucket. Low is bad.
1530 * - bandwidthrate. Low is bad.
1531 * - bandwidthburst. Not a big factor?
1532 * - attempt. High is bad.
1533 * - total bytes queued on outbufs. High is bad. But I'm wary of
1534 * using this, since a few slow-flushing queues will pump up the
1535 * number without meaning what we meant to mean. What we really
1536 * mean is "total directory bytes added to outbufs recently", but
1537 * that's harder to quantify and harder to keep track of.
1540 global_write_bucket_low(connection_t *conn, size_t attempt, int priority)
1542 int smaller_bucket = global_write_bucket < global_relayed_write_bucket ?
1543 global_write_bucket : global_relayed_write_bucket;
1544 if (authdir_mode(get_options()) && priority>1)
1545 return 0; /* there's always room to answer v2 if we're an auth dir */
1547 if (!connection_is_rate_limited(conn))
1548 return 0; /* local conns don't get limited */
1550 if (smaller_bucket < (int)attempt)
1551 return 1; /* not enough space no matter the priority */
1553 if (write_buckets_empty_last_second)
1554 return 1; /* we're already hitting our limits, no more please */
1556 if (priority == 1) { /* old-style v1 query */
1557 /* Could we handle *two* of these requests within the next two seconds? */
1558 or_options_t *options = get_options();
1559 int64_t can_write = (int64_t)smaller_bucket
1560 + 2*(options->RelayBandwidthRate ? options->RelayBandwidthRate :
1561 options->BandwidthRate);
1562 if (can_write < 2*(int64_t)attempt)
1563 return 1;
1564 } else { /* v2 query */
1565 /* no further constraints yet */
1567 return 0;
1570 /** We just read num_read and wrote num_written onto conn.
1571 * Decrement buckets appropriately. */
1572 static void
1573 connection_buckets_decrement(connection_t *conn, time_t now,
1574 size_t num_read, size_t num_written)
1576 if (!connection_is_rate_limited(conn))
1577 return; /* local IPs are free */
1579 if (num_read > 0)
1580 rep_hist_note_bytes_read(num_read, now);
1581 if (num_written > 0)
1582 rep_hist_note_bytes_written(num_written, now);
1584 if (connection_counts_as_relayed_traffic(conn, now)) {
1585 global_relayed_read_bucket -= num_read;
1586 global_relayed_write_bucket -= num_written;
1588 global_read_bucket -= num_read;
1589 global_write_bucket -= num_written;
1590 if (connection_speaks_cells(conn) && conn->state == OR_CONN_STATE_OPEN)
1591 TO_OR_CONN(conn)->read_bucket -= num_read;
1594 /** If we have exhausted our global buckets, or the buckets for conn,
1595 * stop reading. */
1596 static void
1597 connection_consider_empty_read_buckets(connection_t *conn)
1599 const char *reason;
1601 if (global_read_bucket <= 0) {
1602 reason = "global read bucket exhausted. Pausing.";
1603 } else if (connection_counts_as_relayed_traffic(conn, time(NULL)) &&
1604 global_relayed_read_bucket <= 0) {
1605 reason = "global relayed read bucket exhausted. Pausing.";
1606 } else if (connection_speaks_cells(conn) &&
1607 conn->state == OR_CONN_STATE_OPEN &&
1608 TO_OR_CONN(conn)->read_bucket <= 0) {
1609 reason = "connection read bucket exhausted. Pausing.";
1610 } else
1611 return; /* all good, no need to stop it */
1613 LOG_FN_CONN(conn, (LOG_DEBUG, LD_NET, "%s", reason));
1614 conn->read_blocked_on_bw = 1;
1615 connection_stop_reading(conn);
1618 /** If we have exhausted our global buckets, or the buckets for conn,
1619 * stop writing. */
1620 static void
1621 connection_consider_empty_write_buckets(connection_t *conn)
1623 const char *reason;
1625 if (global_write_bucket <= 0) {
1626 reason = "global write bucket exhausted. Pausing.";
1627 } else if (connection_counts_as_relayed_traffic(conn, time(NULL)) &&
1628 global_relayed_write_bucket <= 0) {
1629 reason = "global relayed write bucket exhausted. Pausing.";
1630 #if 0
1631 } else if (connection_speaks_cells(conn) &&
1632 conn->state == OR_CONN_STATE_OPEN &&
1633 TO_OR_CONN(conn)->write_bucket <= 0) {
1634 reason = "connection write bucket exhausted. Pausing.";
1635 #endif
1636 } else
1637 return; /* all good, no need to stop it */
1639 LOG_FN_CONN(conn, (LOG_DEBUG, LD_NET, "%s", reason));
1640 conn->write_blocked_on_bw = 1;
1641 connection_stop_writing(conn);
1644 /** Initialize the global read bucket to options-\>BandwidthBurst. */
1645 void
1646 connection_bucket_init(void)
1648 or_options_t *options = get_options();
1649 /* start it at max traffic */
1650 global_read_bucket = (int)options->BandwidthBurst;
1651 global_write_bucket = (int)options->BandwidthBurst;
1652 if (options->RelayBandwidthRate) {
1653 global_relayed_read_bucket = (int)options->RelayBandwidthBurst;
1654 global_relayed_write_bucket = (int)options->RelayBandwidthBurst;
1655 } else {
1656 global_relayed_read_bucket = (int)options->BandwidthBurst;
1657 global_relayed_write_bucket = (int)options->BandwidthBurst;
1661 /** Refill a single <b>bucket</b> called <b>name</b> with bandwith rate
1662 * <b>rate</b> and bandwidth burst <b>burst</b>, assuming that
1663 * <b>seconds_elapsed</b> seconds have passed since the last call.
1665 static void
1666 connection_bucket_refill_helper(int *bucket, int rate, int burst,
1667 int seconds_elapsed, const char *name)
1669 int starting_bucket = *bucket;
1670 if (starting_bucket < burst && seconds_elapsed) {
1671 if (((burst - starting_bucket)/seconds_elapsed) < rate) {
1672 *bucket = burst; /* We would overflow the bucket; just set it to
1673 * the maximum. */
1674 } else {
1675 int incr = rate*seconds_elapsed;
1676 *bucket += incr;
1677 if (*bucket > burst || *bucket < starting_bucket) {
1678 /* If we overflow the burst, or underflow our starting bucket,
1679 * cap the bucket value to burst. */
1680 /* XXXX021 this might be redundant now, but it doesn't show up
1681 * in profiles. Remove it after analysis. */
1682 *bucket = burst;
1685 log(LOG_DEBUG, LD_NET,"%s now %d.", name, *bucket);
1689 /** A second has rolled over; increment buckets appropriately. */
1690 void
1691 connection_bucket_refill(int seconds_elapsed, time_t now)
1693 or_options_t *options = get_options();
1694 smartlist_t *conns = get_connection_array();
1695 int relayrate, relayburst;
1697 if (options->RelayBandwidthRate) {
1698 relayrate = (int)options->RelayBandwidthRate;
1699 relayburst = (int)options->RelayBandwidthBurst;
1700 } else {
1701 relayrate = (int)options->BandwidthRate;
1702 relayburst = (int)options->BandwidthBurst;
1705 tor_assert(seconds_elapsed >= 0);
1707 write_buckets_empty_last_second =
1708 global_relayed_write_bucket == 0 || global_write_bucket == 0;
1710 /* refill the global buckets */
1711 connection_bucket_refill_helper(&global_read_bucket,
1712 (int)options->BandwidthRate,
1713 (int)options->BandwidthBurst,
1714 seconds_elapsed, "global_read_bucket");
1715 connection_bucket_refill_helper(&global_write_bucket,
1716 (int)options->BandwidthRate,
1717 (int)options->BandwidthBurst,
1718 seconds_elapsed, "global_write_bucket");
1719 connection_bucket_refill_helper(&global_relayed_read_bucket,
1720 relayrate, relayburst, seconds_elapsed,
1721 "global_relayed_read_bucket");
1722 connection_bucket_refill_helper(&global_relayed_write_bucket,
1723 relayrate, relayburst, seconds_elapsed,
1724 "global_relayed_write_bucket");
1726 /* refill the per-connection buckets */
1727 SMARTLIST_FOREACH(conns, connection_t *, conn,
1729 if (connection_speaks_cells(conn)) {
1730 or_connection_t *or_conn = TO_OR_CONN(conn);
1731 if (connection_read_bucket_should_increase(or_conn)) {
1732 connection_bucket_refill_helper(&or_conn->read_bucket,
1733 or_conn->bandwidthrate,
1734 or_conn->bandwidthburst,
1735 seconds_elapsed,
1736 "or_conn->read_bucket");
1737 //log_fn(LOG_DEBUG,"Receiver bucket %d now %d.", i,
1738 // conn->read_bucket);
1742 if (conn->read_blocked_on_bw == 1 /* marked to turn reading back on now */
1743 && global_read_bucket > 0 /* and we're allowed to read */
1744 && (!connection_counts_as_relayed_traffic(conn, now) ||
1745 global_relayed_read_bucket > 0) /* even if we're relayed traffic */
1746 && (!connection_speaks_cells(conn) ||
1747 conn->state != OR_CONN_STATE_OPEN ||
1748 TO_OR_CONN(conn)->read_bucket > 0)) {
1749 /* and either a non-cell conn or a cell conn with non-empty bucket */
1750 LOG_FN_CONN(conn, (LOG_DEBUG,LD_NET,
1751 "waking up conn (fd %d) for read", conn->s));
1752 conn->read_blocked_on_bw = 0;
1753 connection_start_reading(conn);
1756 if (conn->write_blocked_on_bw == 1
1757 && global_write_bucket > 0 /* and we're allowed to write */
1758 && (!connection_counts_as_relayed_traffic(conn, now) ||
1759 global_relayed_write_bucket > 0)) {
1760 /* even if we're relayed traffic */
1761 LOG_FN_CONN(conn, (LOG_DEBUG,LD_NET,
1762 "waking up conn (fd %d) for write", conn->s));
1763 conn->write_blocked_on_bw = 0;
1764 connection_start_writing(conn);
1769 /** Is the receiver bucket for connection <b>conn</b> low enough that we
1770 * should add another pile of tokens to it?
1772 static int
1773 connection_read_bucket_should_increase(or_connection_t *conn)
1775 tor_assert(conn);
1777 if (conn->_base.state != OR_CONN_STATE_OPEN)
1778 return 0; /* only open connections play the rate limiting game */
1779 if (conn->read_bucket >= conn->bandwidthburst)
1780 return 0;
1782 return 1;
1785 /** Read bytes from conn-\>s and process them.
1787 * This function gets called from conn_read() in main.c, either
1788 * when poll() has declared that conn wants to read, or (for OR conns)
1789 * when there are pending TLS bytes.
1791 * It calls connection_read_to_buf() to bring in any new bytes,
1792 * and then calls connection_process_inbuf() to process them.
1794 * Mark the connection and return -1 if you want to close it, else
1795 * return 0.
1798 connection_handle_read(connection_t *conn)
1800 int max_to_read=-1, try_to_read;
1801 size_t before, n_read = 0;
1803 if (conn->marked_for_close)
1804 return 0; /* do nothing */
1806 conn->timestamp_lastread = time(NULL);
1808 switch (conn->type) {
1809 case CONN_TYPE_OR_LISTENER:
1810 return connection_handle_listener_read(conn, CONN_TYPE_OR);
1811 case CONN_TYPE_AP_LISTENER:
1812 case CONN_TYPE_AP_TRANS_LISTENER:
1813 case CONN_TYPE_AP_NATD_LISTENER:
1814 return connection_handle_listener_read(conn, CONN_TYPE_AP);
1815 case CONN_TYPE_DIR_LISTENER:
1816 return connection_handle_listener_read(conn, CONN_TYPE_DIR);
1817 case CONN_TYPE_CONTROL_LISTENER:
1818 return connection_handle_listener_read(conn, CONN_TYPE_CONTROL);
1819 case CONN_TYPE_AP_DNS_LISTENER:
1820 /* This should never happen; eventdns.c handles the reads here. */
1821 tor_fragile_assert();
1822 return 0;
1825 loop_again:
1826 try_to_read = max_to_read;
1827 tor_assert(!conn->marked_for_close);
1829 before = buf_datalen(conn->inbuf);
1830 if (connection_read_to_buf(conn, &max_to_read) < 0) {
1831 /* There's a read error; kill the connection.*/
1832 connection_close_immediate(conn); /* Don't flush; connection is dead. */
1833 if (CONN_IS_EDGE(conn)) {
1834 edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
1835 connection_edge_end_errno(edge_conn);
1836 if (edge_conn->socks_request) /* broken, don't send a socks reply back */
1837 edge_conn->socks_request->has_finished = 1;
1839 connection_mark_for_close(conn);
1840 return -1;
1842 n_read += buf_datalen(conn->inbuf) - before;
1843 if (CONN_IS_EDGE(conn) && try_to_read != max_to_read) {
1844 /* instruct it not to try to package partial cells. */
1845 if (connection_process_inbuf(conn, 0) < 0) {
1846 return -1;
1848 if (!conn->marked_for_close &&
1849 connection_is_reading(conn) &&
1850 !conn->inbuf_reached_eof &&
1851 max_to_read > 0)
1852 goto loop_again; /* try reading again, in case more is here now */
1854 /* one last try, packaging partial cells and all. */
1855 if (!conn->marked_for_close &&
1856 connection_process_inbuf(conn, 1) < 0) {
1857 return -1;
1859 if (conn->linked_conn) {
1860 /* The other side's handle_write will never actually get called, so
1861 * we need to invoke the appropriate callbacks ourself. */
1862 connection_t *linked = conn->linked_conn;
1863 /* XXXX020 Do we need to ensure that this stuff is called even if
1864 * conn dies in a way that causes us to return -1 earlier? */
1866 if (n_read) {
1867 /* Probably a no-op, but hey. */
1868 connection_buckets_decrement(linked, time(NULL), 0, n_read);
1870 if (connection_flushed_some(linked) < 0)
1871 connection_mark_for_close(linked);
1872 if (!connection_wants_to_flush(linked))
1873 connection_finished_flushing(linked);
1876 if (!buf_datalen(linked->outbuf) && conn->active_on_link)
1877 connection_stop_reading_from_linked_conn(conn);
1879 /* If we hit the EOF, call connection_reached_eof. */
1880 if (!conn->marked_for_close &&
1881 conn->inbuf_reached_eof &&
1882 connection_reached_eof(conn) < 0) {
1883 return -1;
1885 return 0;
1888 /** Pull in new bytes from conn-\>s or conn-\>linked_conn onto conn-\>inbuf,
1889 * either directly or via TLS. Reduce the token buckets by the number of bytes
1890 * read.
1892 * If *max_to_read is -1, then decide it ourselves, else go with the
1893 * value passed to us. When returning, if it's changed, subtract the
1894 * number of bytes we read from *max_to_read.
1896 * Return -1 if we want to break conn, else return 0.
1898 static int
1899 connection_read_to_buf(connection_t *conn, int *max_to_read)
1901 int result, at_most = *max_to_read;
1902 size_t slack_in_buf, more_to_read;
1903 size_t n_read = 0, n_written = 0;
1905 if (at_most == -1) { /* we need to initialize it */
1906 /* how many bytes are we allowed to read? */
1907 /* XXXX020 too many calls to time(). Do they hurt? */
1908 at_most = connection_bucket_read_limit(conn, time(NULL));
1911 slack_in_buf = buf_slack(conn->inbuf);
1912 again:
1913 if ((size_t)at_most > slack_in_buf && slack_in_buf >= 1024) {
1914 more_to_read = at_most - slack_in_buf;
1915 at_most = slack_in_buf;
1916 } else {
1917 more_to_read = 0;
1920 if (connection_speaks_cells(conn) &&
1921 conn->state > OR_CONN_STATE_PROXY_READING) {
1922 int pending;
1923 or_connection_t *or_conn = TO_OR_CONN(conn);
1924 size_t initial_size;
1925 if (conn->state == OR_CONN_STATE_TLS_HANDSHAKING ||
1926 conn->state == OR_CONN_STATE_TLS_CLIENT_RENEGOTIATING) {
1927 /* continue handshaking even if global token bucket is empty */
1928 return connection_tls_continue_handshake(or_conn);
1931 log_debug(LD_NET,
1932 "%d: starting, inbuf_datalen %d (%d pending in tls object)."
1933 " at_most %d.",
1934 conn->s,(int)buf_datalen(conn->inbuf),
1935 tor_tls_get_pending_bytes(or_conn->tls), at_most);
1937 initial_size = buf_datalen(conn->inbuf);
1938 /* else open, or closing */
1939 result = read_to_buf_tls(or_conn->tls, at_most, conn->inbuf);
1940 if (TOR_TLS_IS_ERROR(result) || result == TOR_TLS_CLOSE)
1941 or_conn->tls_error = result;
1942 else
1943 or_conn->tls_error = 0;
1945 switch (result) {
1946 case TOR_TLS_CLOSE:
1947 log_info(LD_NET,"TLS connection closed on read. Closing. "
1948 "(Nickname %s, address %s",
1949 or_conn->nickname ? or_conn->nickname : "not set",
1950 conn->address);
1951 return result;
1952 CASE_TOR_TLS_ERROR_ANY:
1953 log_info(LD_NET,"tls error [%s]. breaking (nickname %s, address %s).",
1954 tor_tls_err_to_string(result),
1955 or_conn->nickname ? or_conn->nickname : "not set",
1956 conn->address);
1957 return result;
1958 case TOR_TLS_WANTWRITE:
1959 connection_start_writing(conn);
1960 return 0;
1961 case TOR_TLS_WANTREAD: /* we're already reading */
1962 case TOR_TLS_DONE: /* no data read, so nothing to process */
1963 result = 0;
1964 break; /* so we call bucket_decrement below */
1965 default:
1966 break;
1968 pending = tor_tls_get_pending_bytes(or_conn->tls);
1969 if (pending) {
1970 /* If we have any pending bytes, we read them now. This *can*
1971 * take us over our read allotment, but really we shouldn't be
1972 * believing that SSL bytes are the same as TCP bytes anyway. */
1973 int r2 = read_to_buf_tls(or_conn->tls, pending, conn->inbuf);
1974 if (r2<0) {
1975 log_warn(LD_BUG, "apparently, reading pending bytes can fail.");
1976 return -1;
1979 result = (int)(buf_datalen(conn->inbuf)-initial_size);
1980 tor_tls_get_n_raw_bytes(or_conn->tls, &n_read, &n_written);
1981 log_debug(LD_GENERAL, "After TLS read of %d: %ld read, %ld written",
1982 result, (long)n_read, (long)n_written);
1983 } else if (conn->linked) {
1984 if (conn->linked_conn) {
1985 result = move_buf_to_buf(conn->inbuf, conn->linked_conn->outbuf,
1986 &conn->linked_conn->outbuf_flushlen);
1987 } else {
1988 result = 0;
1990 //log_notice(LD_GENERAL, "Moved %d bytes on an internal link!", result);
1991 /* If the other side has disappeared, or if it's been marked for close and
1992 * we flushed its outbuf, then we should set our inbuf_reached_eof. */
1993 if (!conn->linked_conn ||
1994 (conn->linked_conn->marked_for_close &&
1995 buf_datalen(conn->linked_conn->outbuf) == 0))
1996 conn->inbuf_reached_eof = 1;
1998 n_read = (size_t) result;
1999 } else {
2000 /* !connection_speaks_cells, !conn->linked_conn. */
2001 int reached_eof = 0;
2002 CONN_LOG_PROTECT(conn,
2003 result = read_to_buf(conn->s, at_most, conn->inbuf, &reached_eof));
2004 if (reached_eof)
2005 conn->inbuf_reached_eof = 1;
2007 // log_fn(LOG_DEBUG,"read_to_buf returned %d.",read_result);
2009 if (result < 0)
2010 return -1;
2011 n_read = (size_t) result;
2014 if (n_read > 0) { /* change *max_to_read */
2015 *max_to_read = at_most - n_read;
2018 if (conn->type == CONN_TYPE_AP) {
2019 edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
2020 edge_conn->n_read += n_read;
2023 connection_buckets_decrement(conn, time(NULL), n_read, n_written);
2025 if (more_to_read && result == at_most) {
2026 slack_in_buf = buf_slack(conn->inbuf);
2027 at_most = more_to_read;
2028 goto again;
2031 /* Call even if result is 0, since the global read bucket may
2032 * have reached 0 on a different conn, and this guy needs to
2033 * know to stop reading. */
2034 connection_consider_empty_read_buckets(conn);
2035 if (n_written > 0 && connection_is_writing(conn))
2036 connection_consider_empty_write_buckets(conn);
2038 return 0;
2041 /** A pass-through to fetch_from_buf. */
2043 connection_fetch_from_buf(char *string, size_t len, connection_t *conn)
2045 return fetch_from_buf(string, len, conn->inbuf);
2048 /** Return conn-\>outbuf_flushlen: how many bytes conn wants to flush
2049 * from its outbuf. */
2051 connection_wants_to_flush(connection_t *conn)
2053 return conn->outbuf_flushlen > 0;
2056 /** Are there too many bytes on edge connection <b>conn</b>'s outbuf to
2057 * send back a relay-level sendme yet? Return 1 if so, 0 if not. Used by
2058 * connection_edge_consider_sending_sendme().
2061 connection_outbuf_too_full(connection_t *conn)
2063 return (conn->outbuf_flushlen > 10*CELL_PAYLOAD_SIZE);
2066 /** Try to flush more bytes onto conn-\>s.
2068 * This function gets called either from conn_write() in main.c
2069 * when poll() has declared that conn wants to write, or below
2070 * from connection_write_to_buf() when an entire TLS record is ready.
2072 * Update conn-\>timestamp_lastwritten to now, and call flush_buf
2073 * or flush_buf_tls appropriately. If it succeeds and there are no more
2074 * more bytes on conn->outbuf, then call connection_finished_flushing
2075 * on it too.
2077 * If <b>force</b>, then write as many bytes as possible, ignoring bandwidth
2078 * limits. (Used for flushing messages to controller connections on fatal
2079 * errors.)
2081 * Mark the connection and return -1 if you want to close it, else
2082 * return 0.
2085 connection_handle_write(connection_t *conn, int force)
2087 int e;
2088 socklen_t len=sizeof(e);
2089 int result;
2090 int max_to_write;
2091 time_t now = time(NULL);
2092 size_t n_read = 0, n_written = 0;
2094 tor_assert(!connection_is_listener(conn));
2096 if (conn->marked_for_close || conn->s < 0)
2097 return 0; /* do nothing */
2099 if (conn->in_flushed_some) {
2100 log_warn(LD_BUG, "called recursively from inside conn->in_flushed_some()");
2101 return 0;
2104 conn->timestamp_lastwritten = now;
2106 /* Sometimes, "writable" means "connected". */
2107 if (connection_state_is_connecting(conn)) {
2108 if (getsockopt(conn->s, SOL_SOCKET, SO_ERROR, (void*)&e, &len) < 0) {
2109 log_warn(LD_BUG,
2110 "getsockopt() syscall failed?! Please report to tor-ops.");
2111 if (CONN_IS_EDGE(conn))
2112 connection_edge_end_errno(TO_EDGE_CONN(conn));
2113 connection_mark_for_close(conn);
2114 return -1;
2116 if (e) {
2117 /* some sort of error, but maybe just inprogress still */
2118 if (!ERRNO_IS_CONN_EINPROGRESS(e)) {
2119 log_info(LD_NET,"in-progress connect failed. Removing.");
2120 if (CONN_IS_EDGE(conn))
2121 connection_edge_end_errno(TO_EDGE_CONN(conn));
2123 connection_close_immediate(conn);
2124 connection_mark_for_close(conn);
2125 /* it's safe to pass OPs to router_set_status(), since it just
2126 * ignores unrecognized routers
2128 if (conn->type == CONN_TYPE_OR && !get_options()->HttpsProxy)
2129 router_set_status(TO_OR_CONN(conn)->identity_digest, 0);
2130 return -1;
2131 } else {
2132 return 0; /* no change, see if next time is better */
2135 /* The connection is successful. */
2136 if (connection_finished_connecting(conn)<0)
2137 return -1;
2140 max_to_write = force ? (int)conn->outbuf_flushlen
2141 : connection_bucket_write_limit(conn, now);
2143 if (connection_speaks_cells(conn) &&
2144 conn->state > OR_CONN_STATE_PROXY_READING) {
2145 or_connection_t *or_conn = TO_OR_CONN(conn);
2146 if (conn->state == OR_CONN_STATE_TLS_HANDSHAKING ||
2147 conn->state == OR_CONN_STATE_TLS_CLIENT_RENEGOTIATING) {
2148 connection_stop_writing(conn);
2149 if (connection_tls_continue_handshake(or_conn) < 0) {
2150 /* Don't flush; connection is dead. */
2151 connection_close_immediate(conn);
2152 connection_mark_for_close(conn);
2153 return -1;
2155 return 0;
2156 } else if (conn->state == OR_CONN_STATE_TLS_SERVER_RENEGOTIATING) {
2157 return connection_handle_read(conn);
2160 /* else open, or closing */
2161 result = flush_buf_tls(or_conn->tls, conn->outbuf,
2162 max_to_write, &conn->outbuf_flushlen);
2163 switch (result) {
2164 CASE_TOR_TLS_ERROR_ANY:
2165 case TOR_TLS_CLOSE:
2166 log_info(LD_NET,result!=TOR_TLS_CLOSE?
2167 "tls error. breaking.":"TLS connection closed on flush");
2168 /* Don't flush; connection is dead. */
2169 connection_close_immediate(conn);
2170 connection_mark_for_close(conn);
2171 return -1;
2172 case TOR_TLS_WANTWRITE:
2173 log_debug(LD_NET,"wanted write.");
2174 /* we're already writing */
2175 return 0;
2176 case TOR_TLS_WANTREAD:
2177 /* Make sure to avoid a loop if the receive buckets are empty. */
2178 log_debug(LD_NET,"wanted read.");
2179 if (!connection_is_reading(conn)) {
2180 connection_stop_writing(conn);
2181 conn->write_blocked_on_bw = 1;
2182 /* we'll start reading again when the next second arrives,
2183 * and then also start writing again.
2186 /* else no problem, we're already reading */
2187 return 0;
2188 /* case TOR_TLS_DONE:
2189 * for TOR_TLS_DONE, fall through to check if the flushlen
2190 * is empty, so we can stop writing.
2194 tor_tls_get_n_raw_bytes(or_conn->tls, &n_read, &n_written);
2195 log_debug(LD_GENERAL, "After TLS write of %d: %ld read, %ld written",
2196 result, (long)n_read, (long)n_written);
2197 } else {
2198 CONN_LOG_PROTECT(conn,
2199 result = flush_buf(conn->s, conn->outbuf,
2200 max_to_write, &conn->outbuf_flushlen));
2201 if (result < 0) {
2202 if (CONN_IS_EDGE(conn))
2203 connection_edge_end_errno(TO_EDGE_CONN(conn));
2205 connection_close_immediate(conn); /* Don't flush; connection is dead. */
2206 connection_mark_for_close(conn);
2207 return -1;
2209 n_written = (size_t) result;
2212 if (conn->type == CONN_TYPE_AP) {
2213 edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
2214 edge_conn->n_written += n_written;
2217 connection_buckets_decrement(conn, time(NULL), n_read, n_written);
2219 if (result > 0) {
2220 /* If we wrote any bytes from our buffer, then call the appropriate
2221 * functions. */
2222 if (connection_flushed_some(conn) < 0)
2223 connection_mark_for_close(conn);
2226 if (!connection_wants_to_flush(conn)) { /* it's done flushing */
2227 if (connection_finished_flushing(conn) < 0) {
2228 /* already marked */
2229 return -1;
2231 return 0;
2234 /* Call even if result is 0, since the global write bucket may
2235 * have reached 0 on a different conn, and this guy needs to
2236 * know to stop writing. */
2237 connection_consider_empty_write_buckets(conn);
2238 if (n_read > 0 && connection_is_reading(conn))
2239 connection_consider_empty_read_buckets(conn);
2241 return 0;
2244 /** Openssl TLS record size is 16383; this is close. The goal here is to
2245 * push data out as soon as we know there's enough for a TLS record, so
2246 * during periods of high load we won't read entire megabytes from
2247 * input before pushing any data out. It also has the feature of not
2248 * growing huge outbufs unless something is slow. */
2249 #define MIN_TLS_FLUSHLEN 15872
2251 /** Append <b>len</b> bytes of <b>string</b> onto <b>conn</b>'s
2252 * outbuf, and ask it to start writing.
2254 * If <b>zlib</b> is nonzero, this is a directory connection that should get
2255 * its contents compressed or decompressed as they're written. If zlib is
2256 * negative, this is the last data to be compressed, and the connection's zlib
2257 * state should be flushed.
2259 * If it's an OR conn and an entire TLS record is ready, then try to
2260 * flush the record now. Similarly, if it's a local control connection
2261 * and a 64k chunk is ready, try to flush it all, so we don't end up with
2262 * many megabytes of controller info queued at once.
2264 void
2265 _connection_write_to_buf_impl(const char *string, size_t len,
2266 connection_t *conn, int zlib)
2268 /* XXXX This function really needs to return -1 on failure. */
2269 int r;
2270 size_t old_datalen;
2271 if (!len && !(zlib<0))
2272 return;
2273 /* if it's marked for close, only allow write if we mean to flush it */
2274 if (conn->marked_for_close && !conn->hold_open_until_flushed)
2275 return;
2277 old_datalen = buf_datalen(conn->outbuf);
2278 if (zlib) {
2279 dir_connection_t *dir_conn = TO_DIR_CONN(conn);
2280 int done = zlib < 0;
2281 CONN_LOG_PROTECT(conn, r = write_to_buf_zlib(conn->outbuf,
2282 dir_conn->zlib_state,
2283 string, len, done));
2284 } else {
2285 CONN_LOG_PROTECT(conn, r = write_to_buf(string, len, conn->outbuf));
2287 if (r < 0) {
2288 if (CONN_IS_EDGE(conn)) {
2289 /* if it failed, it means we have our package/delivery windows set
2290 wrong compared to our max outbuf size. close the whole circuit. */
2291 log_warn(LD_NET,
2292 "write_to_buf failed. Closing circuit (fd %d).", conn->s);
2293 circuit_mark_for_close(circuit_get_by_edge_conn(TO_EDGE_CONN(conn)),
2294 END_CIRC_REASON_INTERNAL);
2295 } else {
2296 log_warn(LD_NET,
2297 "write_to_buf failed. Closing connection (fd %d).", conn->s);
2298 connection_mark_for_close(conn);
2300 return;
2303 connection_start_writing(conn);
2304 if (zlib) {
2305 conn->outbuf_flushlen += buf_datalen(conn->outbuf) - old_datalen;
2306 } else {
2307 int extra = 0;
2308 conn->outbuf_flushlen += len;
2310 /* Should we try flushing the outbuf now? */
2311 if (conn->in_flushed_some) {
2312 /* Don't flush the outbuf when the reason we're writing more stuff is
2313 * _because_ we flushed the outbuf. That's unfair. */
2314 return;
2317 if (conn->type == CONN_TYPE_OR &&
2318 conn->outbuf_flushlen-len < MIN_TLS_FLUSHLEN &&
2319 conn->outbuf_flushlen >= MIN_TLS_FLUSHLEN) {
2320 /* We just pushed outbuf_flushlen to MIN_TLS_FLUSHLEN or above;
2321 * we can send out a full TLS frame now if we like. */
2322 extra = conn->outbuf_flushlen - MIN_TLS_FLUSHLEN;
2323 conn->outbuf_flushlen = MIN_TLS_FLUSHLEN;
2324 } else if (conn->type == CONN_TYPE_CONTROL &&
2325 !connection_is_rate_limited(conn) &&
2326 conn->outbuf_flushlen-len < 1<<16 &&
2327 conn->outbuf_flushlen >= 1<<16) {
2328 /* just try to flush all of it */
2329 } else
2330 return; /* no need to try flushing */
2332 if (connection_handle_write(conn, 0) < 0) {
2333 if (!conn->marked_for_close) {
2334 /* this connection is broken. remove it. */
2335 log_warn(LD_BUG, "unhandled error on write for "
2336 "conn (type %d, fd %d); removing",
2337 conn->type, conn->s);
2338 tor_fragile_assert();
2339 /* do a close-immediate here, so we don't try to flush */
2340 connection_close_immediate(conn);
2342 return;
2344 if (extra) {
2345 conn->outbuf_flushlen += extra;
2346 connection_start_writing(conn);
2351 /** Return the conn to addr/port that has the most recent
2352 * timestamp_created, or NULL if no such conn exists. */
2353 or_connection_t *
2354 connection_or_exact_get_by_addr_port(uint32_t addr, uint16_t port)
2356 or_connection_t *best=NULL;
2357 smartlist_t *conns = get_connection_array();
2359 SMARTLIST_FOREACH(conns, connection_t *, conn,
2361 if (conn->type == CONN_TYPE_OR &&
2362 conn->addr == addr &&
2363 conn->port == port &&
2364 !conn->marked_for_close &&
2365 (!best || best->_base.timestamp_created < conn->timestamp_created))
2366 best = TO_OR_CONN(conn);
2368 return best;
2371 /** Return a connection with given type, address, port, and purpose;
2372 * or NULL if no such connection exists. */
2373 connection_t *
2374 connection_get_by_type_addr_port_purpose(int type,
2375 uint32_t addr, uint16_t port,
2376 int purpose)
2378 smartlist_t *conns = get_connection_array();
2379 SMARTLIST_FOREACH(conns, connection_t *, conn,
2381 if (conn->type == type &&
2382 conn->addr == addr &&
2383 conn->port == port &&
2384 conn->purpose == purpose &&
2385 !conn->marked_for_close)
2386 return conn;
2388 return NULL;
2391 /** Return the stream with id <b>id</b> if it is not already marked for
2392 * close.
2394 edge_connection_t *
2395 connection_get_by_global_id(uint32_t id)
2397 smartlist_t *conns = get_connection_array();
2398 SMARTLIST_FOREACH(conns, connection_t *, conn,
2400 if (CONN_IS_EDGE(conn) && TO_EDGE_CONN(conn)->global_identifier == id) {
2401 if (!conn->marked_for_close)
2402 return TO_EDGE_CONN(conn);
2403 else
2404 return NULL;
2407 return NULL;
2410 /** Return a connection of type <b>type</b> that is not marked for close.
2412 connection_t *
2413 connection_get_by_type(int type)
2415 smartlist_t *conns = get_connection_array();
2416 SMARTLIST_FOREACH(conns, connection_t *, conn,
2418 if (conn->type == type && !conn->marked_for_close)
2419 return conn;
2421 return NULL;
2424 /** Return a connection of type <b>type</b> that is in state <b>state</b>,
2425 * and that is not marked for close.
2427 connection_t *
2428 connection_get_by_type_state(int type, int state)
2430 smartlist_t *conns = get_connection_array();
2431 SMARTLIST_FOREACH(conns, connection_t *, conn,
2433 if (conn->type == type && conn->state == state && !conn->marked_for_close)
2434 return conn;
2436 return NULL;
2439 /** Return a connection of type <b>type</b> that has rendquery equal
2440 * to <b>rendquery</b>, and that is not marked for close. If state
2441 * is non-zero, conn must be of that state too. If rendversion is
2442 * nonnegative, conn must be fetching that rendversion, too.
2444 connection_t *
2445 connection_get_by_type_state_rendquery(int type, int state,
2446 const char *rendquery,
2447 int rendversion)
2449 smartlist_t *conns = get_connection_array();
2451 tor_assert(type == CONN_TYPE_DIR ||
2452 type == CONN_TYPE_AP || type == CONN_TYPE_EXIT);
2454 SMARTLIST_FOREACH(conns, connection_t *, conn,
2456 if (conn->type == type &&
2457 !conn->marked_for_close &&
2458 (!state || state == conn->state)) {
2459 if (type == CONN_TYPE_DIR &&
2460 (rendversion < 0 ||
2461 rendversion == TO_DIR_CONN(conn)->rend_version) &&
2462 !rend_cmp_service_ids(rendquery, TO_DIR_CONN(conn)->rend_query))
2463 return conn;
2464 else if (CONN_IS_EDGE(conn) &&
2465 !rend_cmp_service_ids(rendquery, TO_EDGE_CONN(conn)->rend_query))
2466 return conn;
2469 return NULL;
2472 /** Return an open, non-marked connection of a given type and purpose, or NULL
2473 * if no such connection exists. */
2474 connection_t *
2475 connection_get_by_type_purpose(int type, int purpose)
2477 smartlist_t *conns = get_connection_array();
2478 SMARTLIST_FOREACH(conns, connection_t *, conn,
2480 if (conn->type == type &&
2481 !conn->marked_for_close &&
2482 (purpose == conn->purpose))
2483 return conn;
2485 return NULL;
2488 /** Return 1 if <b>conn</b> is a listener conn, else return 0. */
2490 connection_is_listener(connection_t *conn)
2492 if (conn->type == CONN_TYPE_OR_LISTENER ||
2493 conn->type == CONN_TYPE_AP_LISTENER ||
2494 conn->type == CONN_TYPE_AP_TRANS_LISTENER ||
2495 conn->type == CONN_TYPE_AP_DNS_LISTENER ||
2496 conn->type == CONN_TYPE_AP_NATD_LISTENER ||
2497 conn->type == CONN_TYPE_DIR_LISTENER ||
2498 conn->type == CONN_TYPE_CONTROL_LISTENER)
2499 return 1;
2500 return 0;
2503 /** Return 1 if <b>conn</b> is in state "open" and is not marked
2504 * for close, else return 0.
2507 connection_state_is_open(connection_t *conn)
2509 tor_assert(conn);
2511 if (conn->marked_for_close)
2512 return 0;
2514 if ((conn->type == CONN_TYPE_OR && conn->state == OR_CONN_STATE_OPEN) ||
2515 (conn->type == CONN_TYPE_AP && conn->state == AP_CONN_STATE_OPEN) ||
2516 (conn->type == CONN_TYPE_EXIT && conn->state == EXIT_CONN_STATE_OPEN) ||
2517 (conn->type == CONN_TYPE_CONTROL &&
2518 conn->state == CONTROL_CONN_STATE_OPEN))
2519 return 1;
2521 return 0;
2524 /** Return 1 if conn is in 'connecting' state, else return 0. */
2526 connection_state_is_connecting(connection_t *conn)
2528 tor_assert(conn);
2530 if (conn->marked_for_close)
2531 return 0;
2532 switch (conn->type)
2534 case CONN_TYPE_OR:
2535 return conn->state == OR_CONN_STATE_CONNECTING;
2536 case CONN_TYPE_EXIT:
2537 return conn->state == EXIT_CONN_STATE_CONNECTING;
2538 case CONN_TYPE_DIR:
2539 return conn->state == DIR_CONN_STATE_CONNECTING;
2542 return 0;
2545 /** Allocates a base64'ed authenticator for use in http or https
2546 * auth, based on the input string <b>authenticator</b>. Returns it
2547 * if success, else returns NULL. */
2548 char *
2549 alloc_http_authenticator(const char *authenticator)
2551 /* an authenticator in Basic authentication
2552 * is just the string "username:password" */
2553 const int authenticator_length = strlen(authenticator);
2554 /* The base64_encode function needs a minimum buffer length
2555 * of 66 bytes. */
2556 const int base64_authenticator_length = (authenticator_length/48+1)*66;
2557 char *base64_authenticator = tor_malloc(base64_authenticator_length);
2558 if (base64_encode(base64_authenticator, base64_authenticator_length,
2559 authenticator, authenticator_length) < 0) {
2560 tor_free(base64_authenticator); /* free and set to null */
2561 } else {
2562 /* remove extra \n at end of encoding */
2563 base64_authenticator[strlen(base64_authenticator) - 1] = 0;
2565 return base64_authenticator;
2568 /** Given a socket handle, check whether the local address (sockname) of the
2569 * socket is one that we've connected from before. If so, double-check
2570 * whether our address has changed and we need to generate keys. If we do,
2571 * call init_keys().
2573 static void
2574 client_check_address_changed(int sock)
2576 uint32_t iface_ip, ip_out;
2577 struct sockaddr_in out_addr;
2578 socklen_t out_addr_len = sizeof(out_addr);
2579 uint32_t *ip;
2581 if (!last_interface_ip)
2582 get_interface_address(LOG_INFO, &last_interface_ip);
2583 if (!outgoing_addrs)
2584 outgoing_addrs = smartlist_create();
2586 if (getsockname(sock, (struct sockaddr*)&out_addr, &out_addr_len)<0) {
2587 int e = tor_socket_errno(sock);
2588 log_warn(LD_NET, "getsockname() to check for address change failed: %s",
2589 tor_socket_strerror(e));
2590 return;
2593 /* Okay. If we've used this address previously, we're okay. */
2594 ip_out = ntohl(out_addr.sin_addr.s_addr);
2595 SMARTLIST_FOREACH(outgoing_addrs, uint32_t*, ip_ptr,
2596 if (*ip_ptr == ip_out) return;
2599 /* Uh-oh. We haven't connected from this address before. Has the interface
2600 * address changed? */
2601 if (get_interface_address(LOG_INFO, &iface_ip)<0)
2602 return;
2603 ip = tor_malloc(sizeof(uint32_t));
2604 *ip = ip_out;
2606 if (iface_ip == last_interface_ip) {
2607 /* Nope, it hasn't changed. Add this address to the list. */
2608 smartlist_add(outgoing_addrs, ip);
2609 } else {
2610 /* The interface changed. We're a client, so we need to regenerate our
2611 * keys. First, reset the state. */
2612 log(LOG_NOTICE, LD_NET, "Our IP has changed. Rotating keys...");
2613 last_interface_ip = iface_ip;
2614 SMARTLIST_FOREACH(outgoing_addrs, void*, ip_ptr, tor_free(ip_ptr));
2615 smartlist_clear(outgoing_addrs);
2616 smartlist_add(outgoing_addrs, ip);
2617 /* Okay, now change our keys. */
2618 ip_address_changed(1);
2622 /** Some systems have limited system buffers for recv and xmit on
2623 * sockets allocated in a virtual server or similar environment. For a Tor
2624 * server this can produce the "Error creating network socket: No buffer
2625 * space available" error once all available TCP buffer space is consumed.
2626 * This method will attempt to constrain the buffers allocated for the socket
2627 * to the desired size to stay below system TCP buffer limits.
2629 static void
2630 set_constrained_socket_buffers(int sock, int size)
2632 void *sz = (void*)&size;
2633 if (setsockopt(sock, SOL_SOCKET, SO_SNDBUF, sz, sizeof(size)) < 0) {
2634 int e = tor_socket_errno(sock);
2635 log_warn(LD_NET, "setsockopt() to constrain send "
2636 "buffer to %d bytes failed: %s", size, tor_socket_strerror(e));
2638 if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF, sz, sizeof(size)) < 0) {
2639 int e = tor_socket_errno(sock);
2640 log_warn(LD_NET, "setsockopt() to constrain recv "
2641 "buffer to %d bytes failed: %s", size, tor_socket_strerror(e));
2645 /** Process new bytes that have arrived on conn-\>inbuf.
2647 * This function just passes conn to the connection-specific
2648 * connection_*_process_inbuf() function. It also passes in
2649 * package_partial if wanted.
2651 static int
2652 connection_process_inbuf(connection_t *conn, int package_partial)
2654 tor_assert(conn);
2656 switch (conn->type) {
2657 case CONN_TYPE_OR:
2658 return connection_or_process_inbuf(TO_OR_CONN(conn));
2659 case CONN_TYPE_EXIT:
2660 case CONN_TYPE_AP:
2661 return connection_edge_process_inbuf(TO_EDGE_CONN(conn),
2662 package_partial);
2663 case CONN_TYPE_DIR:
2664 return connection_dir_process_inbuf(TO_DIR_CONN(conn));
2665 case CONN_TYPE_CPUWORKER:
2666 return connection_cpu_process_inbuf(conn);
2667 case CONN_TYPE_CONTROL:
2668 return connection_control_process_inbuf(TO_CONTROL_CONN(conn));
2669 default:
2670 log_err(LD_BUG,"got unexpected conn type %d.", conn->type);
2671 tor_fragile_assert();
2672 return -1;
2676 /** Called whenever we've written data on a connection. */
2677 static int
2678 connection_flushed_some(connection_t *conn)
2680 int r = 0;
2681 tor_assert(!conn->in_flushed_some);
2682 conn->in_flushed_some = 1;
2683 if (conn->type == CONN_TYPE_DIR &&
2684 conn->state == DIR_CONN_STATE_SERVER_WRITING) {
2685 r = connection_dirserv_flushed_some(TO_DIR_CONN(conn));
2686 } else if (conn->type == CONN_TYPE_OR) {
2687 r = connection_or_flushed_some(TO_OR_CONN(conn));
2689 conn->in_flushed_some = 0;
2690 return r;
2693 /** We just finished flushing bytes from conn-\>outbuf, and there
2694 * are no more bytes remaining.
2696 * This function just passes conn to the connection-specific
2697 * connection_*_finished_flushing() function.
2699 static int
2700 connection_finished_flushing(connection_t *conn)
2702 tor_assert(conn);
2704 /* If the connection is closed, don't try to do anything more here. */
2705 if (CONN_IS_CLOSED(conn))
2706 return 0;
2708 // log_fn(LOG_DEBUG,"entered. Socket %u.", conn->s);
2710 switch (conn->type) {
2711 case CONN_TYPE_OR:
2712 return connection_or_finished_flushing(TO_OR_CONN(conn));
2713 case CONN_TYPE_AP:
2714 case CONN_TYPE_EXIT:
2715 return connection_edge_finished_flushing(TO_EDGE_CONN(conn));
2716 case CONN_TYPE_DIR:
2717 return connection_dir_finished_flushing(TO_DIR_CONN(conn));
2718 case CONN_TYPE_CPUWORKER:
2719 return connection_cpu_finished_flushing(conn);
2720 case CONN_TYPE_CONTROL:
2721 return connection_control_finished_flushing(TO_CONTROL_CONN(conn));
2722 default:
2723 log_err(LD_BUG,"got unexpected conn type %d.", conn->type);
2724 tor_fragile_assert();
2725 return -1;
2729 /** Called when our attempt to connect() to another server has just
2730 * succeeded.
2732 * This function just passes conn to the connection-specific
2733 * connection_*_finished_connecting() function.
2735 static int
2736 connection_finished_connecting(connection_t *conn)
2738 tor_assert(conn);
2739 switch (conn->type)
2741 case CONN_TYPE_OR:
2742 return connection_or_finished_connecting(TO_OR_CONN(conn));
2743 case CONN_TYPE_EXIT:
2744 return connection_edge_finished_connecting(TO_EDGE_CONN(conn));
2745 case CONN_TYPE_DIR:
2746 return connection_dir_finished_connecting(TO_DIR_CONN(conn));
2747 default:
2748 log_err(LD_BUG,"got unexpected conn type %d.", conn->type);
2749 tor_fragile_assert();
2750 return -1;
2754 /** Callback: invoked when a connection reaches an EOF event. */
2755 static int
2756 connection_reached_eof(connection_t *conn)
2758 switch (conn->type) {
2759 case CONN_TYPE_OR:
2760 return connection_or_reached_eof(TO_OR_CONN(conn));
2761 case CONN_TYPE_AP:
2762 case CONN_TYPE_EXIT:
2763 return connection_edge_reached_eof(TO_EDGE_CONN(conn));
2764 case CONN_TYPE_DIR:
2765 return connection_dir_reached_eof(TO_DIR_CONN(conn));
2766 case CONN_TYPE_CPUWORKER:
2767 return connection_cpu_reached_eof(conn);
2768 case CONN_TYPE_CONTROL:
2769 return connection_control_reached_eof(TO_CONTROL_CONN(conn));
2770 default:
2771 log_err(LD_BUG,"got unexpected conn type %d.", conn->type);
2772 tor_fragile_assert();
2773 return -1;
2777 /** Log how many bytes are used by buffers of different kinds and sizes. */
2778 void
2779 connection_dump_buffer_mem_stats(int severity)
2781 uint64_t used_by_type[_CONN_TYPE_MAX+1];
2782 uint64_t alloc_by_type[_CONN_TYPE_MAX+1];
2783 int n_conns_by_type[_CONN_TYPE_MAX+1];
2784 uint64_t total_alloc = 0;
2785 uint64_t total_used = 0;
2786 int i;
2787 smartlist_t *conns = get_connection_array();
2789 memset(used_by_type, 0, sizeof(used_by_type));
2790 memset(alloc_by_type, 0, sizeof(alloc_by_type));
2791 memset(n_conns_by_type, 0, sizeof(n_conns_by_type));
2793 SMARTLIST_FOREACH(conns, connection_t *, c,
2795 int tp = c->type;
2796 ++n_conns_by_type[tp];
2797 if (c->inbuf) {
2798 used_by_type[tp] += buf_datalen(c->inbuf);
2799 alloc_by_type[tp] += buf_allocation(c->inbuf);
2801 if (c->outbuf) {
2802 used_by_type[tp] += buf_datalen(c->outbuf);
2803 alloc_by_type[tp] += buf_allocation(c->outbuf);
2806 for (i=0; i <= _CONN_TYPE_MAX; ++i) {
2807 total_used += used_by_type[i];
2808 total_alloc += alloc_by_type[i];
2811 log(severity, LD_GENERAL,
2812 "In buffers for %d connections: "U64_FORMAT" used/"U64_FORMAT" allocated",
2813 smartlist_len(conns),
2814 U64_PRINTF_ARG(total_used), U64_PRINTF_ARG(total_alloc));
2815 for (i=_CONN_TYPE_MIN; i <= _CONN_TYPE_MAX; ++i) {
2816 if (!n_conns_by_type[i])
2817 continue;
2818 log(severity, LD_GENERAL,
2819 " For %d %s connections: "U64_FORMAT" used/"U64_FORMAT" allocated",
2820 n_conns_by_type[i], conn_type_to_string(i),
2821 U64_PRINTF_ARG(used_by_type[i]), U64_PRINTF_ARG(alloc_by_type[i]));
2825 /** Verify that connection <b>conn</b> has all of its invariants
2826 * correct. Trigger an assert if anything is invalid.
2828 void
2829 assert_connection_ok(connection_t *conn, time_t now)
2831 (void) now; /* XXXX unused. */
2832 tor_assert(conn);
2833 tor_assert(conn->type >= _CONN_TYPE_MIN);
2834 tor_assert(conn->type <= _CONN_TYPE_MAX);
2835 switch (conn->type) {
2836 case CONN_TYPE_OR:
2837 tor_assert(conn->magic == OR_CONNECTION_MAGIC);
2838 break;
2839 case CONN_TYPE_AP:
2840 case CONN_TYPE_EXIT:
2841 tor_assert(conn->magic == EDGE_CONNECTION_MAGIC);
2842 break;
2843 case CONN_TYPE_DIR:
2844 tor_assert(conn->magic == DIR_CONNECTION_MAGIC);
2845 break;
2846 case CONN_TYPE_CONTROL:
2847 tor_assert(conn->magic == CONTROL_CONNECTION_MAGIC);
2848 break;
2849 default:
2850 tor_assert(conn->magic == BASE_CONNECTION_MAGIC);
2851 break;
2854 if (conn->linked_conn) {
2855 tor_assert(conn->linked_conn->linked_conn == conn);
2856 tor_assert(conn->linked != 0);
2858 if (conn->linked)
2859 tor_assert(conn->s < 0);
2861 if (conn->outbuf_flushlen > 0) {
2862 tor_assert(connection_is_writing(conn) || conn->write_blocked_on_bw ||
2863 conn->edge_blocked_on_circ);
2866 if (conn->hold_open_until_flushed)
2867 tor_assert(conn->marked_for_close);
2869 /* XXXX check: read_blocked_on_bw, write_blocked_on_bw, s, conn_array_index,
2870 * marked_for_close. */
2872 /* buffers */
2873 if (!connection_is_listener(conn)) {
2874 assert_buf_ok(conn->inbuf);
2875 assert_buf_ok(conn->outbuf);
2878 if (conn->chosen_exit_optional) {
2879 tor_assert(conn->type == CONN_TYPE_AP);
2880 tor_assert((TO_EDGE_CONN(conn))->chosen_exit_name);
2883 if (conn->type == CONN_TYPE_OR) {
2884 or_connection_t *or_conn = TO_OR_CONN(conn);
2885 if (conn->state == OR_CONN_STATE_OPEN) {
2886 /* tor_assert(conn->bandwidth > 0); */
2887 /* the above isn't necessarily true: if we just did a TLS
2888 * handshake but we didn't recognize the other peer, or it
2889 * gave a bad cert/etc, then we won't have assigned bandwidth,
2890 * yet it will be open. -RD
2892 // tor_assert(conn->read_bucket >= 0);
2894 // tor_assert(conn->addr && conn->port);
2895 tor_assert(conn->address);
2896 if (conn->state > OR_CONN_STATE_PROXY_READING)
2897 tor_assert(or_conn->tls);
2900 if (CONN_IS_EDGE(conn)) {
2901 edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
2902 /* XXX unchecked: package window, deliver window. */
2903 if (conn->type == CONN_TYPE_AP) {
2905 tor_assert(edge_conn->socks_request);
2906 if (conn->state == AP_CONN_STATE_OPEN) {
2907 tor_assert(edge_conn->socks_request->has_finished != 0);
2908 if (!conn->marked_for_close) {
2909 tor_assert(edge_conn->cpath_layer);
2910 assert_cpath_layer_ok(edge_conn->cpath_layer);
2914 if (conn->type == CONN_TYPE_EXIT) {
2915 tor_assert(conn->purpose == EXIT_PURPOSE_CONNECT ||
2916 conn->purpose == EXIT_PURPOSE_RESOLVE);
2918 } else if (conn->type == CONN_TYPE_DIR) {
2919 } else {
2920 /* Purpose is only used for dir and exit types currently */
2921 tor_assert(!conn->purpose);
2924 switch (conn->type)
2926 case CONN_TYPE_OR_LISTENER:
2927 case CONN_TYPE_AP_LISTENER:
2928 case CONN_TYPE_AP_TRANS_LISTENER:
2929 case CONN_TYPE_AP_NATD_LISTENER:
2930 case CONN_TYPE_DIR_LISTENER:
2931 case CONN_TYPE_CONTROL_LISTENER:
2932 case CONN_TYPE_AP_DNS_LISTENER:
2933 tor_assert(conn->state == LISTENER_STATE_READY);
2934 break;
2935 case CONN_TYPE_OR:
2936 tor_assert(conn->state >= _OR_CONN_STATE_MIN);
2937 tor_assert(conn->state <= _OR_CONN_STATE_MAX);
2938 tor_assert(TO_OR_CONN(conn)->n_circuits >= 0);
2939 break;
2940 case CONN_TYPE_EXIT:
2941 tor_assert(conn->state >= _EXIT_CONN_STATE_MIN);
2942 tor_assert(conn->state <= _EXIT_CONN_STATE_MAX);
2943 tor_assert(conn->purpose >= _EXIT_PURPOSE_MIN);
2944 tor_assert(conn->purpose <= _EXIT_PURPOSE_MAX);
2945 break;
2946 case CONN_TYPE_AP:
2947 tor_assert(conn->state >= _AP_CONN_STATE_MIN);
2948 tor_assert(conn->state <= _AP_CONN_STATE_MAX);
2949 tor_assert(TO_EDGE_CONN(conn)->socks_request);
2950 break;
2951 case CONN_TYPE_DIR:
2952 tor_assert(conn->state >= _DIR_CONN_STATE_MIN);
2953 tor_assert(conn->state <= _DIR_CONN_STATE_MAX);
2954 tor_assert(conn->purpose >= _DIR_PURPOSE_MIN);
2955 tor_assert(conn->purpose <= _DIR_PURPOSE_MAX);
2956 break;
2957 case CONN_TYPE_CPUWORKER:
2958 tor_assert(conn->state >= _CPUWORKER_STATE_MIN);
2959 tor_assert(conn->state <= _CPUWORKER_STATE_MAX);
2960 break;
2961 case CONN_TYPE_CONTROL:
2962 tor_assert(conn->state >= _CONTROL_CONN_STATE_MIN);
2963 tor_assert(conn->state <= _CONTROL_CONN_STATE_MAX);
2964 break;
2965 default:
2966 tor_assert(0);