clarify the bandwidthburst and bandwidthrate are in bytes
[tor.git] / src / or / connection.c
blobc996d638410cdb4582d608cc5ba16f9d9438c21c
1 /* Copyright 2001,2002,2003 Roger Dingledine, Matej Pfajfar. */
2 /* See LICENSE for licensing information */
3 /* $Id$ */
5 /**
6 * \file connection.c
7 * \brief General high-level functions to handle reading and writing
8 * on connections.
9 **/
11 #include "or.h"
13 /********* START VARIABLES **********/
15 extern or_options_t options; /* command-line and config-file options */
17 /** Array of strings to make conn-\>type human-readable. */
18 const char *conn_type_to_string[] = {
19 "", /* 0 */
20 "OP listener", /* 1 */
21 "OP", /* 2 */
22 "OR listener", /* 3 */
23 "OR", /* 4 */
24 "Exit", /* 5 */
25 "App listener",/* 6 */
26 "App", /* 7 */
27 "Dir listener",/* 8 */
28 "Dir", /* 9 */
29 "DNS worker", /* 10 */
30 "CPU worker", /* 11 */
31 "Control listener", /* 12 */
32 "Control", /* 13 */
35 /** Array of string arrays to make {conn-\>type,conn-\>state} human-readable. */
36 const char *conn_state_to_string[][_CONN_TYPE_MAX+1] = {
37 { NULL }, /* no type associated with 0 */
38 { NULL }, /* op listener, obsolete */
39 { NULL }, /* op, obsolete */
40 { "ready" }, /* or listener, 0 */
41 { "", /* OR, 0 */
42 "connect()ing", /* 1 */
43 "handshaking", /* 2 */
44 "open" }, /* 3 */
45 { "", /* exit, 0 */
46 "waiting for dest info", /* 1 */
47 "connecting", /* 2 */
48 "open", /* 3 */
49 "resolve failed" }, /* 4 */
50 { "ready" }, /* app listener, 0 */
51 { "", /* 0 */
52 "", /* 1 */
53 "", /* 2 */
54 "", /* 3 */
55 "", /* 4 */
56 "awaiting dest info", /* app, 5 */
57 "waiting for rendezvous desc", /* 6 */
58 "waiting for safe circuit", /* 7 */
59 "waiting for connected", /* 8 */
60 "open" }, /* 9 */
61 { "ready" }, /* dir listener, 0 */
62 { "", /* dir, 0 */
63 "connecting", /* 1 */
64 "client sending", /* 2 */
65 "client reading", /* 3 */
66 "awaiting command", /* 4 */
67 "writing" }, /* 5 */
68 { "", /* dns worker, 0 */
69 "idle", /* 1 */
70 "busy" }, /* 2 */
71 { "", /* cpu worker, 0 */
72 "idle", /* 1 */
73 "busy with onion", /* 2 */
74 "busy with handshake" }, /* 3 */
75 { "ready" }, /* control listener, 0 */
76 { "", /* control, 0 */
77 "ready", /* 1 */
78 "waiting for authentication", }, /* 2 */
81 /********* END VARIABLES ************/
83 static int connection_create_listener(const char *bindaddress,
84 uint16_t bindport, int type);
85 static int connection_init_accepted_conn(connection_t *conn);
86 static int connection_handle_listener_read(connection_t *conn, int new_type);
87 static int connection_receiver_bucket_should_increase(connection_t *conn);
88 static int connection_finished_flushing(connection_t *conn);
89 static int connection_finished_connecting(connection_t *conn);
90 static int connection_read_to_buf(connection_t *conn);
91 static int connection_process_inbuf(connection_t *conn);
92 static int connection_bucket_read_limit(connection_t *conn);
94 /**************************************************************/
96 /** Allocate space for a new connection_t. This function just initializes
97 * conn; you must call connection_add() to link it into the main array.
99 * Set conn-\>type to <b>type</b>. Set conn-\>s and conn-\>poll_index to
100 * -1 to signify they are not yet assigned.
102 * If conn is not a listener type, allocate buffers for it. If it's
103 * an AP type, allocate space to store the socks_request.
105 * Assign a pseudorandom next_circ_id between 0 and 2**15.
107 * Initialize conn's timestamps to now.
109 connection_t *connection_new(int type) {
110 connection_t *conn;
111 time_t now = time(NULL);
113 conn = tor_malloc_zero(sizeof(connection_t));
114 conn->magic = CONNECTION_MAGIC;
115 conn->s = -1; /* give it a default of 'not used' */
116 conn->poll_index = -1; /* also default to 'not used' */
118 conn->type = type;
119 if(!connection_is_listener(conn)) { /* listeners never use their buf */
120 conn->inbuf = buf_new();
121 conn->outbuf = buf_new();
123 if (type == CONN_TYPE_AP) {
124 conn->socks_request = tor_malloc_zero(sizeof(socks_request_t));
127 conn->next_circ_id = crypto_pseudo_rand_int(1<<15);
129 conn->timestamp_created = now;
130 conn->timestamp_lastread = now;
131 conn->timestamp_lastwritten = now;
133 return conn;
136 /** Deallocate memory used by <b>conn</b>. Deallocate its buffers if necessary,
137 * close its socket if necessary, and mark the directory as dirty if <b>conn</b>
138 * is an OR or OP connection.
140 void connection_free(connection_t *conn) {
141 tor_assert(conn);
142 tor_assert(conn->magic == CONNECTION_MAGIC);
144 if(!connection_is_listener(conn)) {
145 buf_free(conn->inbuf);
146 buf_free(conn->outbuf);
148 tor_free(conn->address);
150 if(connection_speaks_cells(conn)) {
151 if(conn->state == OR_CONN_STATE_OPEN)
152 directory_set_dirty();
153 if (conn->tls)
154 tor_tls_free(conn->tls);
157 if (conn->identity_pkey)
158 crypto_free_pk_env(conn->identity_pkey);
159 tor_free(conn->nickname);
160 tor_free(conn->socks_request);
162 if(conn->s >= 0) {
163 log_fn(LOG_INFO,"closing fd %d.",conn->s);
164 tor_close_socket(conn->s);
166 memset(conn, 0xAA, sizeof(connection_t)); /* poison memory */
167 tor_free(conn);
170 /** Call connection_free() on every connection in our array.
171 * This is used by cpuworkers and dnsworkers when they fork,
172 * so they don't keep resources held open (especially sockets).
174 void connection_free_all(void) {
175 int i, n;
176 connection_t **carray;
178 get_connection_array(&carray,&n);
179 for(i=0;i<n;i++)
180 connection_free(carray[i]);
183 void connection_about_to_close_connection(connection_t *conn)
186 assert(conn->marked_for_close);
188 if(conn->type == CONN_TYPE_AP || conn->type == CONN_TYPE_EXIT) {
189 if(!conn->has_sent_end)
190 log_fn(LOG_WARN,"Edge connection hasn't sent end yet? Bug.");
193 switch(conn->type) {
194 case CONN_TYPE_DIR:
195 if(conn->purpose == DIR_PURPOSE_FETCH_RENDDESC)
196 rend_client_desc_fetched(conn->rend_query, 0);
197 break;
198 case CONN_TYPE_OR:
199 /* Remember why we're closing this connection. */
200 if (conn->state != OR_CONN_STATE_OPEN) {
201 if(connection_or_nonopen_was_started_here(conn)) {
202 rep_hist_note_connect_failed(conn->identity_digest, time(NULL));
203 control_event_or_conn_status(conn, OR_CONN_EVENT_FAILED);
205 } else if (0) { // XXX reason == CLOSE_REASON_UNUSED_OR_CONN) {
206 rep_hist_note_disconnect(conn->identity_digest, time(NULL));
207 } else if(conn->identity_digest) {
208 rep_hist_note_connection_died(conn->identity_digest, time(NULL));
209 control_event_or_conn_status(conn, OR_CONN_EVENT_CLOSED);
211 break;
212 case CONN_TYPE_AP:
213 if (conn->socks_request->has_finished == 0) {
214 log_fn(LOG_INFO,"Cleaning up AP -- sending socks reject.");
215 connection_ap_handshake_socks_reply(conn, NULL, 0, -1);
216 conn->socks_request->has_finished = 1;
217 conn->hold_open_until_flushed = 1;
218 } else {
219 control_event_stream_status(conn, STREAM_EVENT_CLOSED);
221 break;
222 case CONN_TYPE_EXIT:
223 if (conn->state == EXIT_CONN_STATE_RESOLVING) {
224 circuit_detach_stream(circuit_get_by_conn(conn), conn);
225 connection_dns_remove(conn);
227 break;
228 case CONN_TYPE_DNSWORKER:
229 if (conn->state == DNSWORKER_STATE_BUSY) {
230 dns_cancel_pending_resolve(conn->address);
232 break;
236 /** Close the underlying socket for <b>conn</b>, so we don't try to
237 * flush it. Must be used in conjunction with (right before)
238 * connection_mark_for_close().
240 void connection_close_immediate(connection_t *conn)
242 assert_connection_ok(conn,0);
243 if (conn->s < 0) {
244 log_fn(LOG_WARN,"Attempt to close already-closed connection.");
245 return;
247 if (conn->outbuf_flushlen) {
248 log_fn(LOG_INFO,"fd %d, type %s, state %d, %d bytes on outbuf.",
249 conn->s, CONN_TYPE_TO_STRING(conn->type),
250 conn->state, (int)conn->outbuf_flushlen);
252 tor_close_socket(conn->s);
253 conn->s = -1;
254 if(!connection_is_listener(conn)) {
255 buf_clear(conn->outbuf);
256 conn->outbuf_flushlen = 0;
260 /** Mark <b>conn</b> to be closed next time we loop through
261 * conn_close_if_marked() in main.c. Do any cleanup needed:
262 * - Directory conns that fail to fetch a rendezvous descriptor need
263 * to inform pending rendezvous streams.
264 * - OR conns need to call rep_hist_note_*() to record status.
265 * - AP conns need to send a socks reject if necessary.
266 * - Exit conns need to call connection_dns_remove() if necessary.
267 * - AP and Exit conns need to send an end cell if they can.
268 * - DNS conns need to fail any resolves that are pending on them.
271 _connection_mark_for_close(connection_t *conn)
273 assert_connection_ok(conn,0);
275 if (conn->marked_for_close) {
276 log(LOG_WARN, "Double mark-for-close on connection.");
277 return -1;
280 conn->marked_for_close = 1;
282 /* in case we're going to be held-open-til-flushed, reset
283 * the number of seconds since last successful write, so
284 * we get our whole 15 seconds */
285 conn->timestamp_lastwritten = time(NULL);
287 return 0;
290 /** Find each connection that has hold_open_until_flushed set to
291 * 1 but hasn't written in the past 15 seconds, and set
292 * hold_open_until_flushed to 0. This means it will get cleaned
293 * up in the next loop through close_if_marked() in main.c.
295 void connection_expire_held_open(void)
297 connection_t **carray, *conn;
298 int n, i;
299 time_t now;
301 now = time(NULL);
303 get_connection_array(&carray, &n);
304 for (i = 0; i < n; ++i) {
305 conn = carray[i];
306 /* If we've been holding the connection open, but we haven't written
307 * for 15 seconds...
309 if (conn->hold_open_until_flushed) {
310 tor_assert(conn->marked_for_close);
311 if (now - conn->timestamp_lastwritten >= 15) {
312 log_fn(LOG_WARN,"Giving up on marked_for_close conn that's been flushing for 15s (fd %d, type %s, state %d).",
313 conn->s, CONN_TYPE_TO_STRING(conn->type), conn->state);
314 conn->hold_open_until_flushed = 0;
320 /** Bind a new non-blocking socket listening to
321 * <b>bindaddress</b>:<b>bindport</b>, and add this new connection
322 * (of type <b>type</b>) to the connection array.
324 * If <b>bindaddress</b> includes a port, we bind on that port; otherwise, we
325 * use bindport.
327 static int connection_create_listener(const char *bindaddress, uint16_t bindport, int type) {
328 struct sockaddr_in bindaddr; /* where to bind */
329 connection_t *conn;
330 uint16_t usePort;
331 uint32_t addr;
332 int s; /* the socket we're going to make */
333 int one=1;
335 memset(&bindaddr,0,sizeof(struct sockaddr_in));
336 if (parse_addr_port(bindaddress, NULL, &addr, &usePort)<0) {
337 log_fn(LOG_WARN, "Error parsing/resolving BindAddress %s",bindaddress);
338 return -1;
341 if (usePort==0)
342 usePort = bindport;
343 bindaddr.sin_addr.s_addr = htonl(addr);
344 bindaddr.sin_family = AF_INET;
345 bindaddr.sin_port = htons((uint16_t) usePort);
347 s = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
348 if (s < 0) {
349 log_fn(LOG_WARN,"Socket creation failed.");
350 return -1;
353 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (void*) &one, sizeof(one));
355 if(bind(s,(struct sockaddr *)&bindaddr,sizeof(bindaddr)) < 0) {
356 log_fn(LOG_WARN,"Could not bind to port %u: %s",usePort,
357 tor_socket_strerror(tor_socket_errno(s)));
358 return -1;
361 if(listen(s,SOMAXCONN) < 0) {
362 log_fn(LOG_WARN,"Could not listen on port %u: %s",usePort,
363 tor_socket_strerror(tor_socket_errno(s)));
364 return -1;
367 set_socket_nonblocking(s);
369 conn = connection_new(type);
370 conn->s = s;
372 if(connection_add(conn) < 0) { /* no space, forget it */
373 log_fn(LOG_WARN,"connection_add failed. Giving up.");
374 connection_free(conn);
375 return -1;
378 log_fn(LOG_DEBUG,"%s listening on port %u.",conn_type_to_string[type], usePort);
380 conn->state = LISTENER_STATE_READY;
381 connection_start_reading(conn);
383 return 0;
386 /** The listener connection <b>conn</b> told poll() it wanted to read.
387 * Call accept() on conn-\>s, and add the new connection if necessary.
389 static int connection_handle_listener_read(connection_t *conn, int new_type) {
390 int news; /* the new socket */
391 connection_t *newconn;
392 /* information about the remote peer when connecting to other routers */
393 struct sockaddr_in remote;
394 /* length of the remote address. Must be an int, since accept() needs that. */
395 int remotelen = sizeof(struct sockaddr_in);
397 news = accept(conn->s,(struct sockaddr *)&remote,&remotelen);
398 if (news == -1) { /* accept() error */
399 int e = tor_socket_errno(conn->s);
400 if (ERRNO_IS_ACCEPT_EAGAIN(e)) {
401 return 0; /* he hung up before we could accept(). that's fine. */
402 } else if (ERRNO_IS_ACCEPT_RESOURCE_LIMIT(e)) {
403 log_fn(LOG_WARN,"accept failed: %s. Dropping incoming connection.",
404 tor_socket_strerror(e));
405 return 0;
407 /* else there was a real error. */
408 log_fn(LOG_WARN,"accept() failed: %s. Closing listener.",
409 tor_socket_strerror(e));
410 connection_mark_for_close(conn);
411 return -1;
413 log(LOG_INFO,"Connection accepted on socket %d (child of fd %d).",news, conn->s);
415 set_socket_nonblocking(news);
417 /* process entrance policies here, before we even create the connection */
418 if(new_type == CONN_TYPE_AP) {
419 /* check sockspolicy to see if we should accept it */
420 if(socks_policy_permits_address(ntohl(remote.sin_addr.s_addr)) == 0) {
421 log_fn(LOG_WARN,"Denying socks connection from untrusted address %s.",
422 inet_ntoa(remote.sin_addr));
423 tor_close_socket(news);
424 return 0;
427 if(new_type == CONN_TYPE_DIR) {
428 /* check dirpolicy to see if we should accept it */
429 if(dir_policy_permits_address(ntohl(remote.sin_addr.s_addr)) == 0) {
430 log_fn(LOG_WARN,"Denying dir connection from address %s.",
431 inet_ntoa(remote.sin_addr));
432 tor_close_socket(news);
433 return 0;
437 newconn = connection_new(new_type);
438 newconn->s = news;
440 newconn->address = tor_strdup(inet_ntoa(remote.sin_addr)); /* remember the remote address */
441 newconn->addr = ntohl(remote.sin_addr.s_addr);
442 newconn->port = ntohs(remote.sin_port);
444 if(connection_add(newconn) < 0) { /* no space, forget it */
445 connection_free(newconn);
446 return 0; /* no need to tear down the parent */
449 if(connection_init_accepted_conn(newconn) < 0) {
450 connection_mark_for_close(newconn);
451 return 0;
453 return 0;
456 /** Initialize states for newly accepted connection <b>conn</b>.
457 * If conn is an OR, start the tls handshake.
459 static int connection_init_accepted_conn(connection_t *conn) {
461 connection_start_reading(conn);
463 switch(conn->type) {
464 case CONN_TYPE_OR:
465 return connection_tls_start_handshake(conn, 1);
466 case CONN_TYPE_AP:
467 conn->state = AP_CONN_STATE_SOCKS_WAIT;
468 break;
469 case CONN_TYPE_DIR:
470 conn->purpose = DIR_PURPOSE_SERVER;
471 conn->state = DIR_CONN_STATE_SERVER_COMMAND_WAIT;
472 break;
473 case CONN_TYPE_CONTROL:
474 /* XXXX009 NM control */
475 break;
477 return 0;
480 /** Take conn, make a nonblocking socket; try to connect to
481 * addr:port (they arrive in *host order*). If fail, return -1. Else
482 * assign s to conn->\s: if connected return 1, if EAGAIN return 0.
484 * address is used to make the logs useful.
486 * On success, add conn to the list of polled connections.
488 int connection_connect(connection_t *conn, char *address, uint32_t addr, uint16_t port) {
489 int s;
490 struct sockaddr_in dest_addr;
492 s=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
493 if (s < 0) {
494 log_fn(LOG_WARN,"Error creating network socket: %s",
495 tor_socket_strerror(tor_socket_errno(-1)));
496 return -1;
499 if (options.OutboundBindAddress) {
500 struct sockaddr_in ext_addr;
502 memset(&ext_addr, 0, sizeof(ext_addr));
503 ext_addr.sin_family = AF_INET;
504 ext_addr.sin_port = 0;
505 if (!tor_inet_aton(options.OutboundBindAddress, &ext_addr.sin_addr)) {
506 log_fn(LOG_WARN,"Outbound bind address '%s' didn't parse. Ignoring.",
507 options.OutboundBindAddress);
508 } else {
509 if(bind(s, (struct sockaddr*)&ext_addr, sizeof(ext_addr)) < 0) {
510 log_fn(LOG_WARN,"Error binding network socket: %s",
511 tor_socket_strerror(tor_socket_errno(s)));
512 return -1;
517 set_socket_nonblocking(s);
519 memset(&dest_addr,0,sizeof(dest_addr));
520 dest_addr.sin_family = AF_INET;
521 dest_addr.sin_port = htons(port);
522 dest_addr.sin_addr.s_addr = htonl(addr);
524 log_fn(LOG_DEBUG,"Connecting to %s:%u.",address,port);
526 if(connect(s,(struct sockaddr *)&dest_addr,sizeof(dest_addr)) < 0) {
527 if(!ERRNO_IS_CONN_EINPROGRESS(tor_socket_errno(s))) {
528 /* yuck. kill it. */
529 log_fn(LOG_INFO,"Connect() to %s:%u failed: %s",address,port,
530 tor_socket_strerror(tor_socket_errno(s)));
531 tor_close_socket(s);
532 return -1;
533 } else {
534 /* it's in progress. set state appropriately and return. */
535 conn->s = s;
536 if(connection_add(conn) < 0) /* no space, forget it */
537 return -1;
538 log_fn(LOG_DEBUG,"connect in progress, socket %d.",s);
539 return 0;
543 /* it succeeded. we're connected. */
544 log_fn(LOG_INFO,"Connection to %s:%u established.",address,port);
545 conn->s = s;
546 if(connection_add(conn) < 0) /* no space, forget it */
547 return -1;
548 return 1;
551 /** If there exist any listeners of type <b>type</b> in the connection
552 * array, mark them for close.
554 static void listener_close_if_present(int type) {
555 connection_t *conn;
556 connection_t **carray;
557 int i,n;
558 tor_assert(type == CONN_TYPE_OR_LISTENER ||
559 type == CONN_TYPE_AP_LISTENER ||
560 type == CONN_TYPE_DIR_LISTENER ||
561 type == CONN_TYPE_CONTROL_LISTENER);
562 get_connection_array(&carray,&n);
563 for(i=0;i<n;i++) {
564 conn = carray[i];
565 if (conn->type == type && !conn->marked_for_close) {
566 connection_close_immediate(conn);
567 connection_mark_for_close(conn);
572 /**
573 * Launch any configured listener connections of type <b>type</b>. (A
574 * listener is configured if <b>port_option</b> is non-zero. If any
575 * BindAddress configuration options are given in <b>cfg</b>, create a
576 * connection binding to each one. Otherwise, create a single
577 * connection binding to the address <b>default_addr</b>.)
579 * If <b>force</b> is true, close and re-open all listener connections.
580 * Otherwise, only relaunch the listeners of this type if the number of
581 * existing connections is not as configured (e.g., because one died).
583 static int retry_listeners(int type, struct config_line_t *cfg,
584 int port_option, const char *default_addr,
585 int force)
587 if (!force) {
588 int want, have, n_conn, i;
589 struct config_line_t *c;
590 connection_t *conn;
591 connection_t **carray;
592 /* How many should there be? */
593 if (cfg && port_option) {
594 want = 0;
595 for (c = cfg; c; c = c->next)
596 ++want;
597 } else if (port_option) {
598 want = 1;
599 } else {
600 want = 0;
603 /* How many are there actually? */
604 have = 0;
605 get_connection_array(&carray,&n_conn);
606 for(i=0;i<n_conn;i++) {
607 conn = carray[i];
608 if (conn->type == type && !conn->marked_for_close)
609 ++have;
612 /* If we have the right number of listeners, do nothing. */
613 if (have == want)
614 return 0;
616 /* Otherwise, warn the user and relaunch. */
617 log_fn(LOG_WARN,"We have %d %s(s) open, but we want %d; relaunching.",
618 have, conn_type_to_string[type], want);
621 listener_close_if_present(type);
622 if (port_option) {
623 if (!cfg) {
624 if (connection_create_listener(default_addr, (uint16_t) port_option,
625 type)<0)
626 return -1;
627 } else {
628 for ( ; cfg; cfg = cfg->next) {
629 if (connection_create_listener(cfg->value, (uint16_t) port_option,
630 type)<0)
631 return -1;
635 return 0;
638 /** (Re)launch listeners for each port you should have open. If
639 * <b>force</b> is true, close and relaunch all listeners. If <b>force</b>
640 * is false, then only relaunch listeners when we have the wrong number of
641 * connections for a given type.
643 int retry_all_listeners(int force) {
645 if (retry_listeners(CONN_TYPE_OR_LISTENER, options.ORBindAddress,
646 options.ORPort, "0.0.0.0", force)<0)
647 return -1;
648 if (retry_listeners(CONN_TYPE_DIR_LISTENER, options.DirBindAddress,
649 options.DirPort, "0.0.0.0", force)<0)
650 return -1;
651 if (retry_listeners(CONN_TYPE_AP_LISTENER, options.SocksBindAddress,
652 options.SocksPort, "127.0.0.1", force)<0)
653 return -1;
654 /* XXXX009 control NM */
656 return 0;
659 extern int global_read_bucket, global_write_bucket;
661 /** How many bytes at most can we read onto this connection? */
662 static int connection_bucket_read_limit(connection_t *conn) {
663 int at_most;
665 /* do a rudimentary round-robin so one circuit can't hog a connection */
666 if(connection_speaks_cells(conn)) {
667 at_most = 32*(CELL_NETWORK_SIZE);
668 } else {
669 at_most = 32*(RELAY_PAYLOAD_SIZE);
672 if(at_most > global_read_bucket)
673 at_most = global_read_bucket;
675 if(connection_speaks_cells(conn) && conn->state == OR_CONN_STATE_OPEN)
676 if(at_most > conn->receiver_bucket)
677 at_most = conn->receiver_bucket;
679 return at_most;
682 /** We just read num_read onto conn. Decrement buckets appropriately. */
683 static void connection_read_bucket_decrement(connection_t *conn, int num_read) {
684 global_read_bucket -= num_read; tor_assert(global_read_bucket >= 0);
685 if(connection_speaks_cells(conn) && conn->state == OR_CONN_STATE_OPEN) {
686 conn->receiver_bucket -= num_read; tor_assert(conn->receiver_bucket >= 0);
688 if(global_read_bucket == 0) {
689 log_fn(LOG_DEBUG,"global bucket exhausted. Pausing.");
690 conn->wants_to_read = 1;
691 connection_stop_reading(conn);
692 return;
694 if(connection_speaks_cells(conn) &&
695 conn->state == OR_CONN_STATE_OPEN &&
696 conn->receiver_bucket == 0) {
697 log_fn(LOG_DEBUG,"receiver bucket exhausted. Pausing.");
698 conn->wants_to_read = 1;
699 connection_stop_reading(conn);
703 /** Initiatialize the global read bucket to options.BandwidthBurstBytes,
704 * and current_time to the current time. */
705 void connection_bucket_init(void) {
706 global_read_bucket = options.BandwidthBurstBytes; /* start it at max traffic */
707 global_write_bucket = options.BandwidthBurstBytes; /* start it at max traffic */
710 /** A second has rolled over; increment buckets appropriately. */
711 void connection_bucket_refill(struct timeval *now) {
712 int i, n;
713 connection_t *conn;
714 connection_t **carray;
716 /* refill the global buckets */
717 if(global_read_bucket < options.BandwidthBurstBytes) {
718 global_read_bucket += options.BandwidthRateBytes;
719 log_fn(LOG_DEBUG,"global_read_bucket now %d.", global_read_bucket);
721 if(global_write_bucket < options.BandwidthBurstBytes) {
722 global_write_bucket += options.BandwidthRateBytes;
723 log_fn(LOG_DEBUG,"global_write_bucket now %d.", global_write_bucket);
726 /* refill the per-connection buckets */
727 get_connection_array(&carray,&n);
728 for(i=0;i<n;i++) {
729 conn = carray[i];
731 if(connection_receiver_bucket_should_increase(conn)) {
732 conn->receiver_bucket += conn->bandwidth;
733 //log_fn(LOG_DEBUG,"Receiver bucket %d now %d.", i, conn->receiver_bucket);
736 if(conn->wants_to_read == 1 /* it's marked to turn reading back on now */
737 && global_read_bucket > 0 /* and we're allowed to read */
738 && global_write_bucket > 0 /* and we're allowed to write (XXXX,
739 * not the best place to check this.) */
740 && (!connection_speaks_cells(conn) ||
741 conn->state != OR_CONN_STATE_OPEN ||
742 conn->receiver_bucket > 0)) {
743 /* and either a non-cell conn or a cell conn with non-empty bucket */
744 log_fn(LOG_DEBUG,"waking up conn (fd %d)",conn->s);
745 conn->wants_to_read = 0;
746 connection_start_reading(conn);
747 if(conn->wants_to_write == 1) {
748 conn->wants_to_write = 0;
749 connection_start_writing(conn);
755 /** Is the receiver bucket for connection <b>conn</b> low enough that we
756 * should add another pile of tokens to it?
758 static int connection_receiver_bucket_should_increase(connection_t *conn) {
759 tor_assert(conn);
761 if(!connection_speaks_cells(conn))
762 return 0; /* edge connections don't use receiver_buckets */
763 if(conn->state != OR_CONN_STATE_OPEN)
764 return 0; /* only open connections play the rate limiting game */
766 tor_assert(conn->bandwidth > 0);
767 if(conn->receiver_bucket > 9*conn->bandwidth)
768 return 0;
770 return 1;
773 /** Read bytes from conn->\s and process them.
775 * This function gets called from conn_read() in main.c, either
776 * when poll() has declared that conn wants to read, or (for OR conns)
777 * when there are pending TLS bytes.
779 * It calls connection_read_to_buf() to bring in any new bytes,
780 * and then calls connection_process_inbuf() to process them.
782 * Mark the connection and return -1 if you want to close it, else
783 * return 0.
785 int connection_handle_read(connection_t *conn) {
787 conn->timestamp_lastread = time(NULL);
789 switch(conn->type) {
790 case CONN_TYPE_OR_LISTENER:
791 return connection_handle_listener_read(conn, CONN_TYPE_OR);
792 case CONN_TYPE_AP_LISTENER:
793 return connection_handle_listener_read(conn, CONN_TYPE_AP);
794 case CONN_TYPE_DIR_LISTENER:
795 return connection_handle_listener_read(conn, CONN_TYPE_DIR);
796 case CONN_TYPE_CONTROL_LISTENER:
797 return connection_handle_listener_read(conn, CONN_TYPE_CONTROL);
800 if(connection_read_to_buf(conn) < 0) {
801 /* There's a read error; kill the connection.*/
802 connection_close_immediate(conn); /* Don't flush; connection is dead. */
803 if(conn->type == CONN_TYPE_AP || conn->type == CONN_TYPE_EXIT) {
804 connection_edge_end(conn, (char)(connection_state_is_open(conn) ?
805 END_STREAM_REASON_MISC : END_STREAM_REASON_CONNECTFAILED),
806 conn->cpath_layer);
808 connection_mark_for_close(conn);
809 if(conn->type == CONN_TYPE_DIR &&
810 conn->state == DIR_CONN_STATE_CONNECTING) {
811 /* it's a directory server and connecting failed: forget about this router */
812 /* XXX I suspect pollerr may make Windows not get to this point. :( */
813 router_mark_as_down(conn->identity_digest);
814 if(conn->purpose == DIR_PURPOSE_FETCH_DIR &&
815 !all_trusted_directory_servers_down()) {
816 log_fn(LOG_INFO,"Giving up on dirserver %s; trying another.", conn->address);
817 directory_get_from_dirserver(DIR_PURPOSE_FETCH_DIR, NULL, 0);
820 return -1;
822 if(connection_process_inbuf(conn) < 0) {
823 // log_fn(LOG_DEBUG,"connection_process_inbuf returned -1.");
824 return -1;
826 return 0;
829 /** Pull in new bytes from conn-\>s onto conn-\>inbuf, either
830 * directly or via TLS. Reduce the token buckets by the number of
831 * bytes read.
833 * Return -1 if we want to break conn, else return 0.
835 static int connection_read_to_buf(connection_t *conn) {
836 int result;
837 int at_most;
839 /* how many bytes are we allowed to read? */
840 at_most = connection_bucket_read_limit(conn);
842 if(connection_speaks_cells(conn) && conn->state != OR_CONN_STATE_CONNECTING) {
843 if(conn->state == OR_CONN_STATE_HANDSHAKING) {
844 /* continue handshaking even if global token bucket is empty */
845 return connection_tls_continue_handshake(conn);
848 log_fn(LOG_DEBUG,"%d: starting, inbuf_datalen %d (%d pending in tls object). at_most %d.",
849 conn->s,(int)buf_datalen(conn->inbuf),tor_tls_get_pending_bytes(conn->tls), at_most);
851 /* else open, or closing */
852 result = read_to_buf_tls(conn->tls, at_most, conn->inbuf);
854 switch(result) {
855 case TOR_TLS_ERROR:
856 case TOR_TLS_CLOSE:
857 log_fn(LOG_INFO,"tls error. breaking (nickname %s, address %s).",
858 conn->nickname ? conn->nickname : "not set", conn->address);
859 return -1; /* XXX deal with close better */
860 case TOR_TLS_WANTWRITE:
861 connection_start_writing(conn);
862 return 0;
863 case TOR_TLS_WANTREAD: /* we're already reading */
864 case TOR_TLS_DONE: /* no data read, so nothing to process */
865 result = 0;
866 break; /* so we call bucket_decrement below */
868 } else {
869 result = read_to_buf(conn->s, at_most, conn->inbuf,
870 &conn->inbuf_reached_eof);
872 // log(LOG_DEBUG,"connection_read_to_buf(): read_to_buf returned %d.",read_result);
874 if(result < 0)
875 return -1;
878 if(result > 0 && !is_local_IP(conn->addr)) { /* remember it */
879 rep_hist_note_bytes_read(result, time(NULL));
882 /* Call even if result is 0, since the global read bucket may
883 * have reached 0 on a different conn, and this guy needs to
884 * know to stop reading. */
885 /* Longer-term, we should separate this out to read_bucket_decrement
886 * and consider_empty_buckets, and just call the second one always. */
887 connection_read_bucket_decrement(conn, result);
889 return 0;
892 /** A pass-through to fetch_from_buf. */
893 int connection_fetch_from_buf(char *string, size_t len, connection_t *conn) {
894 return fetch_from_buf(string, len, conn->inbuf);
897 /** Return conn-\>outbuf_flushlen: how many bytes conn wants to flush
898 * from its outbuf. */
899 int connection_wants_to_flush(connection_t *conn) {
900 return conn->outbuf_flushlen;
903 /** Are there too many bytes on edge connection <b>conn</b>'s outbuf to
904 * send back a relay-level sendme yet? Return 1 if so, 0 if not. Used by
905 * connection_edge_consider_sending_sendme().
907 int connection_outbuf_too_full(connection_t *conn) {
908 return (conn->outbuf_flushlen > 10*CELL_PAYLOAD_SIZE);
911 /** Try to flush more bytes onto conn-\>s.
913 * This function gets called either from conn_write() in main.c
914 * when poll() has declared that conn wants to write, or below
915 * from connection_write_to_buf() when an entire TLS record is ready.
917 * Update conn-\>timestamp_lastwritten to now, and call flush_buf
918 * or flush_buf_tls appropriately. If it succeeds and there no more
919 * more bytes on conn->outbuf, then call connection_finished_flushing
920 * on it too.
922 * Mark the connection and return -1 if you want to close it, else
923 * return 0.
925 int connection_handle_write(connection_t *conn) {
926 int e, len=sizeof(e);
927 int result;
928 time_t now = time(NULL);
930 tor_assert(!connection_is_listener(conn));
932 conn->timestamp_lastwritten = now;
934 /* Sometimes, "writeable" means "connected". */
935 if (connection_state_is_connecting(conn)) {
936 if (getsockopt(conn->s, SOL_SOCKET, SO_ERROR, (void*)&e, &len) < 0) {
937 log_fn(LOG_WARN,"getsockopt() syscall failed?! Please report to tor-ops.");
938 connection_close_immediate(conn);
939 connection_mark_for_close(conn);
940 return -1;
942 if(e) {
943 /* some sort of error, but maybe just inprogress still */
944 errno = e; /* XXX008 this is a kludge. maybe we should rearrange
945 our error-hunting functions? E.g. pass errno to
946 tor_socket_errno(). */
947 if(!ERRNO_IS_CONN_EINPROGRESS(tor_socket_errno(conn->s))) {
948 log_fn(LOG_INFO,"in-progress connect failed. Removing.");
949 connection_close_immediate(conn);
950 connection_mark_for_close(conn);
951 /* it's safe to pass OPs to router_mark_as_down(), since it just
952 * ignores unrecognized routers
954 if (conn->type == CONN_TYPE_OR)
955 router_mark_as_down(conn->identity_digest);
956 return -1;
957 } else {
958 return 0; /* no change, see if next time is better */
961 /* The connection is successful. */
962 return connection_finished_connecting(conn);
965 if (connection_speaks_cells(conn)) {
966 if (conn->state == OR_CONN_STATE_HANDSHAKING) {
967 connection_stop_writing(conn);
968 if(connection_tls_continue_handshake(conn) < 0) {
969 connection_close_immediate(conn); /* Don't flush; connection is dead. */
970 connection_mark_for_close(conn);
971 return -1;
973 return 0;
976 /* else open, or closing */
977 result = flush_buf_tls(conn->tls, conn->outbuf, &conn->outbuf_flushlen);
978 switch(result) {
979 case TOR_TLS_ERROR:
980 case TOR_TLS_CLOSE:
981 log_fn(LOG_INFO,"tls error. breaking.");
982 connection_close_immediate(conn); /* Don't flush; connection is dead. */
983 connection_mark_for_close(conn);
984 return -1; /* XXX deal with close better */
985 case TOR_TLS_WANTWRITE:
986 log_fn(LOG_DEBUG,"wanted write.");
987 /* we're already writing */
988 return 0;
989 case TOR_TLS_WANTREAD:
990 /* Make sure to avoid a loop if the receive buckets are empty. */
991 log_fn(LOG_DEBUG,"wanted read.");
992 if(!connection_is_reading(conn)) {
993 connection_stop_writing(conn);
994 conn->wants_to_write = 1;
995 /* we'll start reading again when the next second arrives,
996 * and then also start writing again.
999 /* else no problem, we're already reading */
1000 return 0;
1001 /* case TOR_TLS_DONE:
1002 * for TOR_TLS_DONE, fall through to check if the flushlen
1003 * is empty, so we can stop writing.
1006 } else {
1007 result = flush_buf(conn->s, conn->outbuf, &conn->outbuf_flushlen);
1008 if (result < 0) {
1009 connection_close_immediate(conn); /* Don't flush; connection is dead. */
1010 conn->has_sent_end = 1;
1011 connection_mark_for_close(conn);
1012 return -1;
1016 if(result > 0 && !is_local_IP(conn->addr)) { /* remember it */
1017 rep_hist_note_bytes_written(result, now);
1018 global_write_bucket -= result;
1021 if(!connection_wants_to_flush(conn)) { /* it's done flushing */
1022 if(connection_finished_flushing(conn) < 0) {
1023 /* already marked */
1024 return -1;
1028 return 0;
1031 /** Append <b>len</b> bytes of <b>string</b> onto <b>conn</b>'s
1032 * outbuf, and ask it to start writing.
1034 void connection_write_to_buf(const char *string, size_t len, connection_t *conn) {
1036 if(!len || conn->marked_for_close)
1037 return;
1039 if(write_to_buf(string, len, conn->outbuf) < 0) {
1040 if(conn->type == CONN_TYPE_AP || conn->type == CONN_TYPE_EXIT) {
1041 /* if it failed, it means we have our package/delivery windows set
1042 wrong compared to our max outbuf size. close the whole circuit. */
1043 log_fn(LOG_WARN,"write_to_buf failed. Closing circuit (fd %d).", conn->s);
1044 circuit_mark_for_close(circuit_get_by_conn(conn));
1045 } else {
1046 log_fn(LOG_WARN,"write_to_buf failed. Closing connection (fd %d).", conn->s);
1047 connection_mark_for_close(conn);
1049 return;
1052 connection_start_writing(conn);
1053 conn->outbuf_flushlen += len;
1056 /** Return the conn to addr/port that has the most recent
1057 * timestamp_created, or NULL if no such conn exists. */
1058 connection_t *connection_exact_get_by_addr_port(uint32_t addr, uint16_t port) {
1059 int i, n;
1060 connection_t *conn, *best=NULL;
1061 connection_t **carray;
1063 get_connection_array(&carray,&n);
1064 for(i=0;i<n;i++) {
1065 conn = carray[i];
1066 if(conn->addr == addr && conn->port == port && !conn->marked_for_close &&
1067 (!best || best->timestamp_created < conn->timestamp_created))
1068 best = conn;
1070 return best;
1073 connection_t *connection_get_by_identity_digest(const char *digest, int type)
1075 int i, n;
1076 connection_t *conn, *best=NULL;
1077 connection_t **carray;
1079 get_connection_array(&carray,&n);
1080 for(i=0;i<n;i++) {
1081 conn = carray[i];
1082 if (conn->type != type)
1083 continue;
1084 if (!memcmp(conn->identity_digest, digest, DIGEST_LEN)
1085 && !conn->marked_for_close
1086 && (!best || best->timestamp_created < conn->timestamp_created))
1087 best = conn;
1089 return best;
1092 /** Return a connection of type <b>type</b> that is not marked for
1093 * close.
1095 connection_t *connection_get_by_type(int type) {
1096 int i, n;
1097 connection_t *conn;
1098 connection_t **carray;
1100 get_connection_array(&carray,&n);
1101 for(i=0;i<n;i++) {
1102 conn = carray[i];
1103 if(conn->type == type && !conn->marked_for_close)
1104 return conn;
1106 return NULL;
1109 /** Return a connection of type <b>type</b> that is in state <b>state</b>,
1110 * and that is not marked for close.
1112 connection_t *connection_get_by_type_state(int type, int state) {
1113 int i, n;
1114 connection_t *conn;
1115 connection_t **carray;
1117 get_connection_array(&carray,&n);
1118 for(i=0;i<n;i++) {
1119 conn = carray[i];
1120 if(conn->type == type && conn->state == state && !conn->marked_for_close)
1121 return conn;
1123 return NULL;
1126 /** Return the connection of type <b>type</b> that is in state
1127 * <b>state</b>, that was written to least recently, and that is not
1128 * marked for close.
1130 connection_t *connection_get_by_type_state_lastwritten(int type, int state) {
1131 int i, n;
1132 connection_t *conn, *best=NULL;
1133 connection_t **carray;
1135 get_connection_array(&carray,&n);
1136 for(i=0;i<n;i++) {
1137 conn = carray[i];
1138 if(conn->type == type && conn->state == state && !conn->marked_for_close)
1139 if(!best || conn->timestamp_lastwritten < best->timestamp_lastwritten)
1140 best = conn;
1142 return best;
1145 /** Return a connection of type <b>type</b> that has rendquery equal
1146 * to <b>rendquery</b>, and that is not marked for close.
1148 connection_t *connection_get_by_type_rendquery(int type, const char *rendquery) {
1149 int i, n;
1150 connection_t *conn;
1151 connection_t **carray;
1153 get_connection_array(&carray,&n);
1154 for(i=0;i<n;i++) {
1155 conn = carray[i];
1156 if(conn->type == type &&
1157 !conn->marked_for_close &&
1158 !rend_cmp_service_ids(rendquery, conn->rend_query))
1159 return conn;
1161 return NULL;
1164 /** Return 1 if <b>conn</b> is a listener conn, else return 0. */
1165 int connection_is_listener(connection_t *conn) {
1166 if(conn->type == CONN_TYPE_OR_LISTENER ||
1167 conn->type == CONN_TYPE_AP_LISTENER ||
1168 conn->type == CONN_TYPE_DIR_LISTENER ||
1169 conn->type == CONN_TYPE_CONTROL_LISTENER)
1170 return 1;
1171 return 0;
1174 /** Return 1 if <b>conn</b> is in state "open" and is not marked
1175 * for close, else return 0.
1177 int connection_state_is_open(connection_t *conn) {
1178 tor_assert(conn);
1180 if(conn->marked_for_close)
1181 return 0;
1183 if((conn->type == CONN_TYPE_OR && conn->state == OR_CONN_STATE_OPEN) ||
1184 (conn->type == CONN_TYPE_AP && conn->state == AP_CONN_STATE_OPEN) ||
1185 (conn->type == CONN_TYPE_EXIT && conn->state == EXIT_CONN_STATE_OPEN) ||
1186 (conn->type == CONN_TYPE_CONTROL && conn->state ==CONTROL_CONN_STATE_OPEN))
1187 return 1;
1189 return 0;
1192 /** Return 1 if conn is in 'connecting' state, else return 0. */
1193 int connection_state_is_connecting(connection_t *conn) {
1194 tor_assert(conn);
1196 if (conn->marked_for_close)
1197 return 0;
1198 switch (conn->type)
1200 case CONN_TYPE_OR:
1201 return conn->state == OR_CONN_STATE_CONNECTING;
1202 case CONN_TYPE_EXIT:
1203 return conn->state == EXIT_CONN_STATE_CONNECTING;
1204 case CONN_TYPE_DIR:
1205 return conn->state == DIR_CONN_STATE_CONNECTING;
1208 return 0;
1211 /** Write a destroy cell with circ ID <b>circ_id</b> onto OR connection
1212 * <b>conn</b>.
1214 * Return 0.
1216 int connection_send_destroy(uint16_t circ_id, connection_t *conn) {
1217 cell_t cell;
1219 tor_assert(conn);
1220 tor_assert(connection_speaks_cells(conn));
1222 memset(&cell, 0, sizeof(cell_t));
1223 cell.circ_id = circ_id;
1224 cell.command = CELL_DESTROY;
1225 log_fn(LOG_INFO,"Sending destroy (circID %d).", circ_id);
1226 connection_or_write_cell_to_buf(&cell, conn);
1227 return 0;
1230 /** Process new bytes that have arrived on conn-\>inbuf.
1232 * This function just passes conn to the connection-specific
1233 * connection_*_process_inbuf() function.
1235 static int connection_process_inbuf(connection_t *conn) {
1237 tor_assert(conn);
1239 switch(conn->type) {
1240 case CONN_TYPE_OR:
1241 return connection_or_process_inbuf(conn);
1242 case CONN_TYPE_EXIT:
1243 case CONN_TYPE_AP:
1244 return connection_edge_process_inbuf(conn);
1245 case CONN_TYPE_DIR:
1246 return connection_dir_process_inbuf(conn);
1247 case CONN_TYPE_DNSWORKER:
1248 return connection_dns_process_inbuf(conn);
1249 case CONN_TYPE_CPUWORKER:
1250 return connection_cpu_process_inbuf(conn);
1251 case CONN_TYPE_CONTROL:
1252 return connection_control_process_inbuf(conn);
1253 default:
1254 log_fn(LOG_WARN,"got unexpected conn->type %d.", conn->type);
1255 return -1;
1259 /** We just finished flushing bytes from conn-\>outbuf, and there
1260 * are no more bytes remaining.
1262 * This function just passes conn to the connection-specific
1263 * connection_*_finished_flushing() function.
1265 static int connection_finished_flushing(connection_t *conn) {
1267 tor_assert(conn);
1269 // log_fn(LOG_DEBUG,"entered. Socket %u.", conn->s);
1271 switch(conn->type) {
1272 case CONN_TYPE_OR:
1273 return connection_or_finished_flushing(conn);
1274 case CONN_TYPE_AP:
1275 case CONN_TYPE_EXIT:
1276 return connection_edge_finished_flushing(conn);
1277 case CONN_TYPE_DIR:
1278 return connection_dir_finished_flushing(conn);
1279 case CONN_TYPE_DNSWORKER:
1280 return connection_dns_finished_flushing(conn);
1281 case CONN_TYPE_CPUWORKER:
1282 return connection_cpu_finished_flushing(conn);
1283 case CONN_TYPE_CONTROL:
1284 return connection_control_finished_flushing(conn);
1285 default:
1286 log_fn(LOG_WARN,"got unexpected conn->type %d.", conn->type);
1287 return -1;
1291 /** Called when our attempt to connect() to another server has just
1292 * succeeded.
1294 * This function just passes conn to the connection-specific
1295 * connection_*_finished_connecting() function.
1297 static int connection_finished_connecting(connection_t *conn)
1299 tor_assert(conn);
1300 switch (conn->type)
1302 case CONN_TYPE_OR:
1303 return connection_or_finished_connecting(conn);
1304 case CONN_TYPE_EXIT:
1305 return connection_edge_finished_connecting(conn);
1306 case CONN_TYPE_DIR:
1307 return connection_dir_finished_connecting(conn);
1308 default:
1309 tor_assert(0);
1310 return -1;
1314 /** Verify that connection <b>conn</b> has all of its invariants
1315 * correct. Trigger an assert if anything is invalid.
1317 void assert_connection_ok(connection_t *conn, time_t now)
1319 tor_assert(conn);
1320 tor_assert(conn->magic == CONNECTION_MAGIC);
1321 tor_assert(conn->type >= _CONN_TYPE_MIN);
1322 tor_assert(conn->type <= _CONN_TYPE_MAX);
1324 if(conn->outbuf_flushlen > 0) {
1325 tor_assert(connection_is_writing(conn) || conn->wants_to_write);
1328 if(conn->hold_open_until_flushed)
1329 tor_assert(conn->marked_for_close);
1331 /* XXX check: wants_to_read, wants_to_write, s, poll_index,
1332 * marked_for_close. */
1334 /* buffers */
1335 if (!connection_is_listener(conn)) {
1336 assert_buf_ok(conn->inbuf);
1337 assert_buf_ok(conn->outbuf);
1340 #if 0 /* computers often go back in time; no way to know */
1341 tor_assert(!now || conn->timestamp_lastread <= now);
1342 tor_assert(!now || conn->timestamp_lastwritten <= now);
1343 tor_assert(conn->timestamp_created <= conn->timestamp_lastread);
1344 tor_assert(conn->timestamp_created <= conn->timestamp_lastwritten);
1345 #endif
1347 /* XXX Fix this; no longer so.*/
1348 #if 0
1349 if(conn->type != CONN_TYPE_OR && conn->type != CONN_TYPE_DIR)
1350 tor_assert(!conn->pkey);
1351 /* pkey is set if we're a dir client, or if we're an OR in state OPEN
1352 * connected to another OR.
1354 #endif
1356 if (conn->type != CONN_TYPE_OR) {
1357 tor_assert(!conn->tls);
1358 } else {
1359 if(conn->state == OR_CONN_STATE_OPEN) {
1360 /* tor_assert(conn->bandwidth > 0); */
1361 /* the above isn't necessarily true: if we just did a TLS
1362 * handshake but we didn't recognize the other peer, or it
1363 * gave a bad cert/etc, then we won't have assigned bandwidth,
1364 * yet it will be open. -RD
1366 tor_assert(conn->receiver_bucket >= 0);
1368 tor_assert(conn->addr && conn->port);
1369 tor_assert(conn->address);
1370 if (conn->state != OR_CONN_STATE_CONNECTING)
1371 tor_assert(conn->tls);
1374 if (conn->type != CONN_TYPE_EXIT && conn->type != CONN_TYPE_AP) {
1375 tor_assert(!conn->stream_id);
1376 tor_assert(!conn->next_stream);
1377 tor_assert(!conn->cpath_layer);
1378 tor_assert(!conn->package_window);
1379 tor_assert(!conn->deliver_window);
1380 tor_assert(!conn->done_sending);
1381 tor_assert(!conn->done_receiving);
1382 } else {
1383 /* XXX unchecked: package window, deliver window. */
1385 if (conn->type == CONN_TYPE_AP) {
1386 tor_assert(conn->socks_request);
1387 if (conn->state == AP_CONN_STATE_OPEN) {
1388 tor_assert(conn->socks_request->has_finished);
1389 tor_assert(conn->cpath_layer);
1390 assert_cpath_layer_ok(conn->cpath_layer);
1392 } else {
1393 tor_assert(!conn->socks_request);
1395 if (conn->type == CONN_TYPE_EXIT) {
1396 tor_assert(conn->purpose == EXIT_PURPOSE_CONNECT ||
1397 conn->purpose == EXIT_PURPOSE_RESOLVE);
1398 } else if(conn->type != CONN_TYPE_DIR) {
1399 tor_assert(!conn->purpose); /* only used for dir types currently */
1402 switch(conn->type)
1404 case CONN_TYPE_OR_LISTENER:
1405 case CONN_TYPE_AP_LISTENER:
1406 case CONN_TYPE_DIR_LISTENER:
1407 case CONN_TYPE_CONTROL_LISTENER:
1408 tor_assert(conn->state == LISTENER_STATE_READY);
1409 break;
1410 case CONN_TYPE_OR:
1411 tor_assert(conn->state >= _OR_CONN_STATE_MIN);
1412 tor_assert(conn->state <= _OR_CONN_STATE_MAX);
1413 break;
1414 case CONN_TYPE_EXIT:
1415 tor_assert(conn->state >= _EXIT_CONN_STATE_MIN);
1416 tor_assert(conn->state <= _EXIT_CONN_STATE_MAX);
1417 break;
1418 case CONN_TYPE_AP:
1419 tor_assert(conn->state >= _AP_CONN_STATE_MIN);
1420 tor_assert(conn->state <= _AP_CONN_STATE_MAX);
1421 tor_assert(conn->socks_request);
1422 break;
1423 case CONN_TYPE_DIR:
1424 tor_assert(conn->state >= _DIR_CONN_STATE_MIN);
1425 tor_assert(conn->state <= _DIR_CONN_STATE_MAX);
1426 tor_assert(conn->purpose >= _DIR_PURPOSE_MIN);
1427 tor_assert(conn->purpose <= _DIR_PURPOSE_MAX);
1428 break;
1429 case CONN_TYPE_DNSWORKER:
1430 tor_assert(conn->state == DNSWORKER_STATE_IDLE ||
1431 conn->state == DNSWORKER_STATE_BUSY);
1432 break;
1433 case CONN_TYPE_CPUWORKER:
1434 tor_assert(conn->state >= _CPUWORKER_STATE_MIN);
1435 tor_assert(conn->state <= _CPUWORKER_STATE_MAX);
1436 break;
1437 case CONN_TYPE_CONTROL:
1438 tor_assert(conn->state >= _CONTROL_CONN_STATE_MIN);
1439 tor_assert(conn->state <= _CONTROL_CONN_STATE_MAX);
1440 /* XXXX009 NM */
1441 break;
1442 default:
1443 tor_assert(0);
1448 Local Variables:
1449 mode:c
1450 indent-tabs-mode:nil
1451 c-basic-offset:2
1452 End: