break routers.c into router.c for stuff the router does,
[tor.git] / src / or / main.c
blobf630a8a4091365f80b3fd69d8a55ecc424f9738d
1 /* Copyright 2001,2002,2003 Roger Dingledine, Matej Pfajfar. */
2 /* See LICENSE for licensing information */
3 /* $Id$ */
5 #include "or.h"
7 /********* START PROTOTYPES **********/
9 static void dumpstats(int severity); /* log stats */
10 static int init_from_config(int argc, char **argv);
12 /********* START VARIABLES **********/
14 extern char *conn_type_to_string[];
15 extern char *conn_state_to_string[][_CONN_TYPE_MAX+1];
17 or_options_t options; /* command-line and config-file options */
18 int global_read_bucket; /* max number of bytes I can read this second */
20 static int stats_prev_global_read_bucket;
21 static uint64_t stats_n_bytes_read = 0;
22 static long stats_n_seconds_reading = 0;
24 static connection_t *connection_array[MAXCONNECTIONS] =
25 { NULL };
27 static struct pollfd poll_array[MAXCONNECTIONS];
29 static int nfds=0; /* number of connections currently active */
31 #ifndef MS_WINDOWS /* do signal stuff only on unix */
32 static int please_dumpstats=0; /* whether we should dump stats during the loop */
33 static int please_reset=0; /* whether we just got a sighup */
34 static int please_reap_children=0; /* whether we should waitpid for exited children */
35 #endif /* signal stuff */
37 /********* END VARIABLES ************/
39 /****************************************************************************
41 * This section contains accessors and other methods on the connection_array
42 * and poll_array variables (which are global within this file and unavailable
43 * outside it).
45 ****************************************************************************/
47 int connection_add(connection_t *conn) {
49 if(nfds >= options.MaxConn-1) {
50 log(LOG_WARN,"connection_add(): failing because nfds is too high.");
51 return -1;
54 conn->poll_index = nfds;
55 connection_set_poll_socket(conn);
56 connection_array[nfds] = conn;
58 /* zero these out here, because otherwise we'll inherit values from the previously freed one */
59 poll_array[nfds].events = 0;
60 poll_array[nfds].revents = 0;
62 nfds++;
64 log(LOG_INFO,"connection_add(): new conn type %d, socket %d, nfds %d.",conn->type, conn->s, nfds);
66 return 0;
69 void connection_set_poll_socket(connection_t *conn) {
70 poll_array[conn->poll_index].fd = conn->s;
73 /* Remove the connection from the global list, and remove the
74 * corresponding poll entry. Calling this function will shift the last
75 * connection (if any) into the position occupied by conn.
77 int connection_remove(connection_t *conn) {
78 int current_index;
80 assert(conn);
81 assert(nfds>0);
83 log(LOG_INFO,"connection_remove(): removing socket %d, nfds now %d",conn->s, nfds-1);
84 /* if it's an edge conn, remove it from the list
85 * of conn's on this circuit. If it's not on an edge,
86 * flush and send destroys for all circuits on this conn
88 circuit_about_to_close_connection(conn);
90 current_index = conn->poll_index;
91 if(current_index == nfds-1) { /* this is the end */
92 nfds--;
93 return 0;
96 /* we replace this one with the one at the end, then free it */
97 nfds--;
98 poll_array[current_index].fd = poll_array[nfds].fd;
99 poll_array[current_index].events = poll_array[nfds].events;
100 poll_array[current_index].revents = poll_array[nfds].revents;
101 connection_array[current_index] = connection_array[nfds];
102 connection_array[current_index]->poll_index = current_index;
104 return 0;
107 void get_connection_array(connection_t ***array, int *n) {
108 *array = connection_array;
109 *n = nfds;
112 void connection_watch_events(connection_t *conn, short events) {
114 assert(conn && conn->poll_index < nfds);
116 poll_array[conn->poll_index].events = events;
119 int connection_is_reading(connection_t *conn) {
120 return poll_array[conn->poll_index].events & POLLIN;
123 void connection_stop_reading(connection_t *conn) {
125 assert(conn && conn->poll_index < nfds);
127 log(LOG_DEBUG,"connection_stop_reading() called.");
128 if(poll_array[conn->poll_index].events & POLLIN)
129 poll_array[conn->poll_index].events -= POLLIN;
132 void connection_start_reading(connection_t *conn) {
134 assert(conn && conn->poll_index < nfds);
136 poll_array[conn->poll_index].events |= POLLIN;
139 void connection_stop_writing(connection_t *conn) {
141 assert(conn && conn->poll_index < nfds);
143 if(poll_array[conn->poll_index].events & POLLOUT)
144 poll_array[conn->poll_index].events -= POLLOUT;
147 void connection_start_writing(connection_t *conn) {
149 assert(conn && conn->poll_index < nfds);
151 poll_array[conn->poll_index].events |= POLLOUT;
154 static void conn_read(int i) {
155 connection_t *conn = connection_array[i];
157 /* see http://www.greenend.org.uk/rjk/2001/06/poll.html for
158 * discussion of POLLIN vs POLLHUP */
159 if(!(poll_array[i].revents & (POLLIN|POLLHUP|POLLERR)))
160 if(!connection_is_reading(conn) ||
161 !connection_has_pending_tls_data(conn))
162 return; /* this conn should not read */
164 log_fn(LOG_DEBUG,"socket %d wants to read.",conn->s);
166 assert_connection_ok(conn, time(NULL));
169 /* XXX does POLLHUP also mean it's definitely broken? */
170 #ifdef MS_WINDOWS
171 (poll_array[i].revents & POLLERR) ||
172 #endif
173 connection_handle_read(conn) < 0)
175 /* this connection is broken. remove it */
176 log_fn(LOG_INFO,"%s connection broken, removing.",
177 conn_type_to_string[conn->type]);
178 connection_remove(conn);
179 connection_free(conn);
180 if(i<nfds) {
181 /* we just replaced the one at i with a new one. process it too. */
182 conn_read(i);
184 } else assert_connection_ok(conn, time(NULL));
187 static void conn_write(int i) {
188 connection_t *conn;
190 if(!(poll_array[i].revents & POLLOUT))
191 return; /* this conn doesn't want to write */
193 conn = connection_array[i];
194 log_fn(LOG_DEBUG,"socket %d wants to write.",conn->s);
196 assert_connection_ok(conn, time(NULL));
198 if(connection_handle_write(conn) < 0) { /* this connection is broken. remove it. */
199 log_fn(LOG_INFO,"%s connection broken, removing.", conn_type_to_string[conn->type]);
200 connection_remove(conn);
201 connection_free(conn);
202 if(i<nfds) { /* we just replaced the one at i with a new one. process it too. */
203 conn_write(i);
205 } else assert_connection_ok(conn, time(NULL));
208 static void conn_close_if_marked(int i) {
209 connection_t *conn;
211 conn = connection_array[i];
212 assert_connection_ok(conn, time(NULL));
213 if(conn->marked_for_close) {
214 log_fn(LOG_INFO,"Cleaning up connection (fd %d).",conn->s);
215 if(conn->s >= 0) { /* might be an incomplete edge connection */
216 /* FIXME there's got to be a better way to check for this -- and make other checks? */
217 if(connection_speaks_cells(conn)) {
218 if(conn->state == OR_CONN_STATE_OPEN)
219 flush_buf_tls(conn->tls, conn->outbuf, &conn->outbuf_flushlen);
220 } else {
221 flush_buf(conn->s, conn->outbuf, &conn->outbuf_flushlen);
223 if(connection_wants_to_flush(conn)) /* not done flushing */
224 log_fn(LOG_WARN,"Conn (socket %d) still wants to flush. Losing %d bytes!",conn->s, (int)buf_datalen(conn->inbuf));
226 connection_remove(conn);
227 connection_free(conn);
228 if(i<nfds) { /* we just replaced the one at i with a new one.
229 process it too. */
230 conn_close_if_marked(i);
235 /* Perform regular maintenance tasks for a single connection. This
236 * function gets run once per second per connection by run_housekeeping.
238 static void run_connection_housekeeping(int i, time_t now) {
239 cell_t cell;
240 connection_t *conn = connection_array[i];
242 if(connection_receiver_bucket_should_increase(conn)) {
243 conn->receiver_bucket += conn->bandwidth;
244 // log_fn(LOG_DEBUG,"Receiver bucket %d now %d.", i, conn->receiver_bucket);
247 if(conn->wants_to_read == 1 /* it's marked to turn reading back on now */
248 && global_read_bucket > 0 /* and we're allowed to read */
249 && (!connection_speaks_cells(conn) || conn->receiver_bucket > 0)) {
250 /* and either a non-cell conn or a cell conn with non-empty bucket */
251 conn->wants_to_read = 0;
252 connection_start_reading(conn);
253 if(conn->wants_to_write == 1) {
254 conn->wants_to_write = 0;
255 connection_start_writing(conn);
259 /* check connections to see whether we should send a keepalive, expire, or wait */
260 if(!connection_speaks_cells(conn))
261 return;
263 if(now >= conn->timestamp_lastwritten + options.KeepalivePeriod) {
264 if((!options.ORPort && !circuit_get_by_conn(conn)) ||
265 (!connection_state_is_open(conn))) {
266 /* we're an onion proxy, with no circuits; or our handshake has expired. kill it. */
267 log_fn(LOG_INFO,"Expiring connection to %d (%s:%d).",
268 i,conn->address, conn->port);
269 conn->marked_for_close = 1;
270 } else {
271 /* either a full router, or we've got a circuit. send a padding cell. */
272 log_fn(LOG_DEBUG,"Sending keepalive to (%s:%d)",
273 conn->address, conn->port);
274 memset(&cell,0,sizeof(cell_t));
275 cell.command = CELL_PADDING;
276 connection_or_write_cell_to_buf(&cell, conn);
281 /* Perform regular maintenance tasks. This function gets run once per
282 * second by prepare_for_poll.
284 static void run_scheduled_events(time_t now) {
285 static long time_to_fetch_directory = 0;
286 static long time_to_new_circuit = 0;
287 circuit_t *circ;
288 int i;
290 /* 1. Every DirFetchPostPeriod seconds, we get a new directory and upload
291 * our descriptor (if any). */
292 if(time_to_fetch_directory < now) {
293 /* it's time to fetch a new directory and/or post our descriptor */
294 if(options.ORPort) {
295 router_rebuild_descriptor();
296 router_upload_desc_to_dirservers();
298 if(!options.DirPort) {
299 /* NOTE directory servers do not currently fetch directories.
300 * Hope this doesn't bite us later. */
301 directory_initiate_command(router_pick_directory_server(),
302 DIR_CONN_STATE_CONNECTING_FETCH);
304 time_to_fetch_directory = now + options.DirFetchPostPeriod;
307 /* 2. Every second, we examine pending circuits and prune the
308 * ones which have been pending for more than 3 seconds.
309 * We do this before step 3, so it can try building more if
310 * it's not comfortable with the number of available circuits.
312 circuit_expire_building();
314 /* 3. Every second, we try a new circuit if there are no valid
315 * circuits. Every NewCircuitPeriod seconds, we expire circuits
316 * that became dirty more than NewCircuitPeriod seconds ago,
317 * and we make a new circ if there are no clean circuits.
319 if(options.SocksPort) {
321 /* launch a new circ for any pending streams that need one */
322 connection_ap_attach_pending();
324 circ = circuit_get_newest(NULL, 1);
325 if(time_to_new_circuit < now) {
326 client_dns_clean();
327 circuit_expire_unused_circuits();
328 circuit_reset_failure_count();
329 if(circ && circ->timestamp_dirty) {
330 log_fn(LOG_INFO,"Youngest circuit dirty; launching replacement.");
331 circuit_launch_new(); /* make a new circuit */
333 time_to_new_circuit = now + options.NewCircuitPeriod;
335 #define CIRCUIT_MIN_BUILDING 2
336 if(!circ && circuit_count_building() < CIRCUIT_MIN_BUILDING) {
337 /* if there's no open circ, and less than 2 are on the way,
338 * go ahead and try another.
340 circuit_launch_new();
344 /* 4. Every second, we check how much bandwidth we've consumed and
345 * increment global_read_bucket.
347 stats_n_bytes_read += stats_prev_global_read_bucket-global_read_bucket;
348 if(global_read_bucket < 9*options.TotalBandwidth) {
349 global_read_bucket += options.TotalBandwidth;
350 log_fn(LOG_DEBUG,"global_read_bucket now %d.", global_read_bucket);
352 stats_prev_global_read_bucket = global_read_bucket;
354 /* 5. We do housekeeping for each connection... */
355 for(i=0;i<nfds;i++) {
356 run_connection_housekeeping(i, now);
359 /* 6. and blow away any connections that need to die. can't do this later
360 * because we might open up a circuit and not realize we're about to cull
361 * the connection it's running over.
362 * XXX we can remove this step once we audit circuit-building to make sure
363 * it doesn't pick a marked-for-close conn. -RD
365 for(i=0;i<nfds;i++)
366 conn_close_if_marked(i);
369 static int prepare_for_poll(void) {
370 static long current_second = 0; /* from previous calls to gettimeofday */
371 connection_t *conn;
372 struct timeval now;
373 int i;
375 tor_gettimeofday(&now);
377 if(now.tv_sec > current_second) { /* the second has rolled over. check more stuff. */
379 ++stats_n_seconds_reading;
380 run_scheduled_events(now.tv_sec);
382 current_second = now.tv_sec; /* remember which second it is, for next time */
385 for(i=0;i<nfds;i++) {
386 conn = connection_array[i];
387 if(connection_has_pending_tls_data(conn)) {
388 log_fn(LOG_DEBUG,"sock %d has pending bytes.",conn->s);
389 return 0; /* has pending bytes to read; don't let poll wait. */
393 return (1000 - (now.tv_usec / 1000)); /* how many milliseconds til the next second? */
396 static int init_from_config(int argc, char **argv) {
397 static int have_daemonized=0;
399 if(getconfig(argc,argv,&options)) {
400 log_fn(LOG_ERR,"Reading config failed. For usage, try -h.");
401 return -1;
403 log_set_severity(options.loglevel); /* assign logging severity level from options */
404 close_logs(); /* we'll close, then open with correct loglevel if necessary */
405 if(!options.LogFile && !options.RunAsDaemon)
406 add_stream_log(options.loglevel, "<stdout>", stdout);
407 if(options.LogFile)
408 if (add_file_log(options.loglevel, options.LogFile) != 0) {
409 /* opening the log file failed! Use stderr and log a warning */
410 add_stream_log(options.loglevel, "<stderr>", stderr);
411 log_fn(LOG_WARN, "Cannot write to LogFile '%s': %s.", options.LogFile, strerror(errno));
413 if(options.DebugLogFile)
414 if (add_file_log(LOG_DEBUG, options.DebugLogFile) != 0)
415 log_fn(LOG_WARN, "Cannot write to DebugLogFile '%s': %s.", options.LogFile, strerror(errno));
417 global_read_bucket = options.TotalBandwidth; /* start it at 1 second of traffic */
418 stats_prev_global_read_bucket = global_read_bucket;
420 if(options.User || options.Group) {
421 if(switch_id(options.User, options.Group) != 0) {
422 return -1;
426 if(options.RunAsDaemon && !have_daemonized) {
427 daemonize();
428 have_daemonized = 1;
431 /* write our pid to the pid file, if we do not have write permissions we will log a warning */
432 if(options.PidFile)
433 write_pidfile(options.PidFile);
435 return 0;
438 static int do_main_loop(void) {
439 int i;
440 int timeout;
441 int poll_result;
443 /* load the routers file */
444 if(router_set_routerlist_from_file(options.RouterFile) < 0) {
445 log_fn(LOG_ERR,"Error loading router list.");
446 return -1;
449 /* load the private keys, if we're supposed to have them, and set up the
450 * TLS context. */
451 if (init_keys() < 0) {
452 log_fn(LOG_ERR,"Error initializing keys; exiting");
453 return -1;
456 if(options.ORPort) {
457 cpu_init(); /* launch cpuworkers. Need to do this *after* we've read the onion key. */
458 router_upload_desc_to_dirservers(); /* upload our descriptor to all dirservers */
461 /* start up the necessary connections based on which ports are
462 * non-zero. This is where we try to connect to all the other ORs,
463 * and start the listeners.
465 if(retry_all_connections() < 0) {
466 log_fn(LOG_ERR,"Failed to bind one of the listener ports.");
467 return -1;
470 for(;;) {
471 #ifndef MS_WINDOWS /* do signal stuff only on unix */
472 if(please_dumpstats) {
473 /* prefer to log it at INFO, but make sure we always see it */
474 dumpstats(options.loglevel>LOG_INFO ? options.loglevel : LOG_INFO);
475 please_dumpstats = 0;
477 if(please_reset) {
478 log_fn(LOG_WARN,"Received sighup. Reloading config.");
479 /* first, reload config variables, in case they've changed */
480 if (init_from_config(0, NULL) < 0) {
481 /* no need to provide argc/v, they've been cached inside init_from_config */
482 exit(1);
485 if(options.DirPort) {
486 /* reload the fingerprint file */
487 char keydir[512];
488 sprintf(keydir,"%s/approved-routers", options.DataDirectory);
489 log_fn(LOG_INFO,"Reloading approved fingerprints from %s...",keydir);
490 if(dirserv_parse_fingerprint_file(keydir) < 0) {
491 log_fn(LOG_WARN, "Error reloading fingerprints. Continuing with old list.");
494 /* XXX do we really want to be resetting the routerlist here? */
495 if(router_set_routerlist_from_file(options.RouterFile) < 0) {
496 log(LOG_WARN,"Error reloading router list. Continuing with old list.");
498 } else {
499 /* fetch a new directory */
500 directory_initiate_command(router_pick_directory_server(), DIR_CONN_STATE_CONNECTING_FETCH);
503 please_reset = 0;
505 if(please_reap_children) {
506 while(waitpid(-1,NULL,WNOHANG)) ; /* keep reaping until no more zombies */
507 please_reap_children = 0;
509 #endif /* signal stuff */
511 timeout = prepare_for_poll();
513 /* poll until we have an event, or the second ends */
514 poll_result = poll(poll_array, nfds, timeout);
516 /* let catch() handle things like ^c, and otherwise don't worry about it */
517 if(poll_result < 0) {
518 if(errno != EINTR) { /* let the program survive things like ^z */
519 log_fn(LOG_ERR,"poll failed.");
520 return -1;
521 } else {
522 log_fn(LOG_DEBUG,"poll interrupted.");
526 /* do all the reads and errors first, so we can detect closed sockets */
527 for(i=0;i<nfds;i++)
528 conn_read(i); /* this also blows away broken connections */
530 /* then do the writes */
531 for(i=0;i<nfds;i++)
532 conn_write(i);
534 /* any of the conns need to be closed now? */
535 for(i=0;i<nfds;i++)
536 conn_close_if_marked(i);
538 /* refilling buckets and sending cells happens at the beginning of the
539 * next iteration of the loop, inside prepare_for_poll()
544 static void catch(int the_signal) {
546 #ifndef MS_WINDOWS /* do signal stuff only on unix */
547 switch(the_signal) {
548 // case SIGABRT:
549 case SIGTERM:
550 case SIGINT:
551 log(LOG_ERR,"Catching signal %d, exiting cleanly.", the_signal);
552 /* we don't care if there was an error when we unlink, nothing
553 we could do about it anyways */
554 if(options.PidFile)
555 unlink(options.PidFile);
556 exit(0);
557 case SIGHUP:
558 please_reset = 1;
559 break;
560 case SIGUSR1:
561 please_dumpstats = 1;
562 break;
563 case SIGCHLD:
564 please_reap_children = 1;
565 break;
566 default:
567 log(LOG_WARN,"Caught signal %d that we can't handle??", the_signal);
569 #endif /* signal stuff */
572 static void dumpstats(int severity) {
573 int i;
574 connection_t *conn;
575 time_t now = time(NULL);
577 log(severity, "Dumping stats:");
579 for(i=0;i<nfds;i++) {
580 conn = connection_array[i];
581 log(severity, "Conn %d (socket %d) type %d (%s), state %d (%s), created %ld secs ago",
582 i, conn->s, conn->type, conn_type_to_string[conn->type],
583 conn->state, conn_state_to_string[conn->type][conn->state], now - conn->timestamp_created);
584 if(!connection_is_listener(conn)) {
585 log(severity,"Conn %d is to '%s:%d'.",i,conn->address, conn->port);
586 log(severity,"Conn %d: %d bytes waiting on inbuf (last read %ld secs ago)",i,
587 (int)buf_datalen(conn->inbuf),
588 now - conn->timestamp_lastread);
589 log(severity,"Conn %d: %d bytes waiting on outbuf (last written %ld secs ago)",i,
590 (int)buf_datalen(conn->outbuf), now - conn->timestamp_lastwritten);
592 circuit_dump_by_conn(conn, severity); /* dump info about all the circuits using this conn */
594 log(severity,
595 "Cells processed: %10lu padding\n"
596 " %10lu create\n"
597 " %10lu created\n"
598 " %10lu relay\n"
599 " (%10lu relayed)\n"
600 " (%10lu delivered)\n"
601 " %10lud destroy",
602 stats_n_padding_cells_processed,
603 stats_n_create_cells_processed,
604 stats_n_created_cells_processed,
605 stats_n_relay_cells_processed,
606 stats_n_relay_cells_relayed,
607 stats_n_relay_cells_delivered,
608 stats_n_destroy_cells_processed);
609 if (stats_n_data_cells_packaged)
610 log(severity,"Average outgoing cell fullness: %2.3f%%",
611 100*(((double)stats_n_data_bytes_packaged) /
612 (stats_n_data_cells_packaged*(CELL_PAYLOAD_SIZE-RELAY_HEADER_SIZE))) );
613 if (stats_n_data_cells_received)
614 log(severity,"Average incoming cell fullness: %2.3f%%",
615 100*(((double)stats_n_data_bytes_received) /
616 (stats_n_data_cells_received*(CELL_PAYLOAD_SIZE-RELAY_HEADER_SIZE))) );
618 if (stats_n_seconds_reading)
619 log(severity,"Average bandwidth used: %d bytes/sec",
620 (int) (stats_n_bytes_read/stats_n_seconds_reading));
623 int tor_main(int argc, char *argv[]) {
625 /* give it somewhere to log to initially */
626 add_stream_log(LOG_INFO, "<stdout>", stdout);
627 log_fn(LOG_WARN,"Tor v%s. This is experimental software. Do not use it if you need anonymity.",VERSION);
629 if (init_from_config(argc,argv) < 0)
630 return -1;
632 if(options.ORPort) { /* only spawn dns handlers if we're a router */
633 dns_init(); /* initialize the dns resolve tree, and spawn workers */
635 if(options.SocksPort) {
636 client_dns_init(); /* init the client dns cache */
639 #ifndef MS_WINDOWS /* do signal stuff only on unix */
640 signal (SIGINT, catch); /* catch kills so we can exit cleanly */
641 signal (SIGTERM, catch);
642 signal (SIGUSR1, catch); /* to dump stats */
643 signal (SIGHUP, catch); /* to reload directory */
644 signal (SIGCHLD, catch); /* for exiting dns/cpu workers */
645 #endif /* signal stuff */
647 crypto_global_init();
648 crypto_seed_rng();
649 do_main_loop();
650 crypto_global_cleanup();
651 return -1;
655 Local Variables:
656 mode:c
657 indent-tabs-mode:nil
658 c-basic-offset:2
659 End: