make code more readable; arrbitrarily change a -1 to a 0.
[tor.git] / src / or / main.c
blob88fe73820d21e2d5fe061500fed05b9ee224b5e6
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 int connection_is_writing(connection_t *conn) {
140 return poll_array[conn->poll_index].events & POLLOUT;
143 void connection_stop_writing(connection_t *conn) {
145 assert(conn && conn->poll_index < nfds);
147 if(poll_array[conn->poll_index].events & POLLOUT)
148 poll_array[conn->poll_index].events -= POLLOUT;
151 void connection_start_writing(connection_t *conn) {
153 assert(conn && conn->poll_index < nfds);
155 poll_array[conn->poll_index].events |= POLLOUT;
158 static void conn_read(int i) {
159 connection_t *conn = connection_array[i];
161 /* see http://www.greenend.org.uk/rjk/2001/06/poll.html for
162 * discussion of POLLIN vs POLLHUP */
163 if(!(poll_array[i].revents & (POLLIN|POLLHUP|POLLERR)))
164 if(!connection_is_reading(conn) ||
165 !connection_has_pending_tls_data(conn))
166 return; /* this conn should not read */
168 if (conn->marked_for_close)
169 return;
170 log_fn(LOG_DEBUG,"socket %d wants to read.",conn->s);
172 assert_connection_ok(conn, time(NULL));
175 /* XXX does POLLHUP also mean it's definitely broken? */
176 #ifdef MS_WINDOWS
177 (poll_array[i].revents & POLLERR) ||
178 #endif
179 connection_handle_read(conn) < 0) {
180 if (!conn->marked_for_close) {
181 /* this connection is broken. remove it */
182 /* XXX This shouldn't ever happen anymore. */
183 log_fn(LOG_ERR,"Unhandled error on read for %s connection (fd %d); removing",
184 conn_type_to_string[conn->type], conn->s);
185 connection_mark_for_close(conn,0);
188 assert_connection_ok(conn, time(NULL));
191 static void conn_write(int i) {
192 connection_t *conn;
194 if(!(poll_array[i].revents & POLLOUT))
195 return; /* this conn doesn't want to write */
197 conn = connection_array[i];
198 if (conn->marked_for_close)
199 return;
200 log_fn(LOG_DEBUG,"socket %d wants to write.",conn->s);
202 assert_connection_ok(conn, time(NULL));
204 if (connection_handle_write(conn) < 0) {
205 if (!conn->marked_for_close) {
206 /* this connection is broken. remove it. */
207 log_fn(LOG_ERR,"Unhandled error on read for %s connection (fd %d); removing",
208 conn_type_to_string[conn->type], conn->s);
209 connection_mark_for_close(conn,0);
212 assert_connection_ok(conn, time(NULL));
215 static void conn_close_if_marked(int i) {
216 connection_t *conn;
218 conn = connection_array[i];
219 assert_connection_ok(conn, time(NULL));
220 if(conn->marked_for_close) {
221 log_fn(LOG_INFO,"Cleaning up connection (fd %d).",conn->s);
222 if(conn->s >= 0) { /* might be an incomplete edge connection */
223 /* FIXME there's got to be a better way to check for this -- and make other checks? */
224 if(connection_speaks_cells(conn)) {
225 if(conn->state == OR_CONN_STATE_OPEN)
226 flush_buf_tls(conn->tls, conn->outbuf, &conn->outbuf_flushlen);
227 } else {
228 flush_buf(conn->s, conn->outbuf, &conn->outbuf_flushlen);
230 if(connection_wants_to_flush(conn) && buf_datalen(conn->outbuf)) {
231 log_fn(LOG_WARN,"Conn (socket %d) still wants to flush. Losing %d bytes!",
232 conn->s, (int)buf_datalen(conn->outbuf));
235 connection_remove(conn);
236 connection_free(conn);
237 if(i<nfds) { /* we just replaced the one at i with a new one.
238 process it too. */
239 conn_close_if_marked(i);
244 /* Perform regular maintenance tasks for a single connection. This
245 * function gets run once per second per connection by run_housekeeping.
247 static void run_connection_housekeeping(int i, time_t now) {
248 cell_t cell;
249 connection_t *conn = connection_array[i];
251 if(connection_receiver_bucket_should_increase(conn)) {
252 conn->receiver_bucket += conn->bandwidth;
253 // log_fn(LOG_DEBUG,"Receiver bucket %d now %d.", i, conn->receiver_bucket);
256 if(conn->wants_to_read == 1 /* it's marked to turn reading back on now */
257 && global_read_bucket > 0 /* and we're allowed to read */
258 && (!connection_speaks_cells(conn) || conn->receiver_bucket > 0)) {
259 /* and either a non-cell conn or a cell conn with non-empty bucket */
260 conn->wants_to_read = 0;
261 connection_start_reading(conn);
262 if(conn->wants_to_write == 1) {
263 conn->wants_to_write = 0;
264 connection_start_writing(conn);
268 /* check connections to see whether we should send a keepalive, expire, or wait */
269 if(!connection_speaks_cells(conn))
270 return;
272 if(now >= conn->timestamp_lastwritten + options.KeepalivePeriod) {
273 if((!options.ORPort && !circuit_get_by_conn(conn)) ||
274 (!connection_state_is_open(conn))) {
275 /* we're an onion proxy, with no circuits; or our handshake has expired. kill it. */
276 log_fn(LOG_INFO,"Expiring connection to %d (%s:%d).",
277 i,conn->address, conn->port);
278 connection_mark_for_close(conn,0); /* Suppress end ??? */
279 /* XXX there's no concept of 'suppressing end' here, because it's an OR
280 * connection, and there's no such thing as an end cell for an OR
281 * connection. -RD */
282 } else {
283 /* either a full router, or we've got a circuit. send a padding cell. */
284 log_fn(LOG_DEBUG,"Sending keepalive to (%s:%d)",
285 conn->address, conn->port);
286 memset(&cell,0,sizeof(cell_t));
287 cell.command = CELL_PADDING;
288 connection_or_write_cell_to_buf(&cell, conn);
293 /* Perform regular maintenance tasks. This function gets run once per
294 * second by prepare_for_poll.
296 static void run_scheduled_events(time_t now) {
297 static long time_to_fetch_directory = 0;
298 static long time_to_new_circuit = 0;
299 circuit_t *circ;
300 int i;
302 /* 1. Every DirFetchPostPeriod seconds, we get a new directory and upload
303 * our descriptor (if any). */
304 if(time_to_fetch_directory < now) {
305 /* it's time to fetch a new directory and/or post our descriptor */
306 if(options.ORPort) {
307 router_rebuild_descriptor();
308 router_upload_desc_to_dirservers();
310 if(!options.DirPort) {
311 /* NOTE directory servers do not currently fetch directories.
312 * Hope this doesn't bite us later. */
313 directory_initiate_command(router_pick_directory_server(),
314 DIR_CONN_STATE_CONNECTING_FETCH);
316 time_to_fetch_directory = now + options.DirFetchPostPeriod;
319 /* 2. Every second, we examine pending circuits and prune the
320 * ones which have been pending for more than 3 seconds.
321 * We do this before step 3, so it can try building more if
322 * it's not comfortable with the number of available circuits.
324 circuit_expire_building();
326 /* 2b. Also look at pending streams and prune the ones that 'began'
327 * a long time ago but haven't gotten a 'connected' yet.
328 * Do this before step 3, so we can put them back into pending
329 * state to be picked up by the new circuit.
331 connection_ap_expire_beginning();
333 /* 3. Every second, we try a new circuit if there are no valid
334 * circuits. Every NewCircuitPeriod seconds, we expire circuits
335 * that became dirty more than NewCircuitPeriod seconds ago,
336 * and we make a new circ if there are no clean circuits.
338 if(options.SocksPort) {
340 /* launch a new circ for any pending streams that need one */
341 connection_ap_attach_pending();
343 circ = circuit_get_newest(NULL, 1);
344 if(time_to_new_circuit < now) {
345 client_dns_clean();
346 circuit_expire_unused_circuits();
347 circuit_reset_failure_count();
348 if(circ && circ->timestamp_dirty) {
349 log_fn(LOG_INFO,"Youngest circuit dirty; launching replacement.");
350 circuit_launch_new(); /* make a new circuit */
352 time_to_new_circuit = now + options.NewCircuitPeriod;
354 #define CIRCUIT_MIN_BUILDING 3
355 if(!circ && circuit_count_building() < CIRCUIT_MIN_BUILDING) {
356 /* if there's no open circ, and less than 3 are on the way,
357 * go ahead and try another.
359 circuit_launch_new();
363 /* 4. Every second, we check how much bandwidth we've consumed and
364 * increment global_read_bucket.
366 stats_n_bytes_read += stats_prev_global_read_bucket-global_read_bucket;
367 if(global_read_bucket < options.BandwidthBurst) {
368 global_read_bucket += options.BandwidthRate;
369 log_fn(LOG_DEBUG,"global_read_bucket now %d.", global_read_bucket);
371 stats_prev_global_read_bucket = global_read_bucket;
373 /* 5. We do housekeeping for each connection... */
374 for(i=0;i<nfds;i++) {
375 run_connection_housekeeping(i, now);
378 /* 6. and blow away any connections that need to die. can't do this later
379 * because we might open up a circuit and not realize we're about to cull
380 * the connection it's running over.
381 * XXX we can remove this step once we audit circuit-building to make sure
382 * it doesn't pick a marked-for-close conn. -RD
384 for(i=0;i<nfds;i++)
385 conn_close_if_marked(i);
388 static int prepare_for_poll(void) {
389 static long current_second = 0; /* from previous calls to gettimeofday */
390 connection_t *conn;
391 struct timeval now;
392 int i;
394 tor_gettimeofday(&now);
396 if(now.tv_sec > current_second) { /* the second has rolled over. check more stuff. */
398 ++stats_n_seconds_reading;
399 run_scheduled_events(now.tv_sec);
401 current_second = now.tv_sec; /* remember which second it is, for next time */
404 for(i=0;i<nfds;i++) {
405 conn = connection_array[i];
406 if(connection_has_pending_tls_data(conn)) {
407 log_fn(LOG_DEBUG,"sock %d has pending bytes.",conn->s);
408 return 0; /* has pending bytes to read; don't let poll wait. */
412 return (1000 - (now.tv_usec / 1000)); /* how many milliseconds til the next second? */
415 static int init_from_config(int argc, char **argv) {
416 if(getconfig(argc,argv,&options)) {
417 log_fn(LOG_ERR,"Reading config failed. For usage, try -h.");
418 return -1;
420 close_logs(); /* we'll close, then open with correct loglevel if necessary */
421 if(!options.LogFile && !options.RunAsDaemon)
422 add_stream_log(options.loglevel, "<stdout>", stdout);
423 if(options.LogFile) {
424 if (add_file_log(options.loglevel, options.LogFile) != 0) {
425 /* opening the log file failed! Use stderr and log a warning */
426 add_stream_log(options.loglevel, "<stderr>", stderr);
427 log_fn(LOG_WARN, "Cannot write to LogFile '%s': %s.", options.LogFile, strerror(errno));
429 log_fn(LOG_WARN, "Successfully opened LogFile '%s', redirecting output.",
430 options.LogFile);
432 if(options.DebugLogFile) {
433 if (add_file_log(LOG_DEBUG, options.DebugLogFile) != 0)
434 log_fn(LOG_WARN, "Cannot write to DebugLogFile '%s': %s.", options.DebugLogFile, strerror(errno));
435 log_fn(LOG_DEBUG, "Successfully opened DebugLogFile '%s'.", options.DebugLogFile);
438 global_read_bucket = options.BandwidthBurst; /* start it at max traffic */
439 stats_prev_global_read_bucket = global_read_bucket;
441 if(options.User || options.Group) {
442 if(switch_id(options.User, options.Group) != 0) {
443 return -1;
447 if(options.RunAsDaemon) {
448 /* XXXX Can we delay this any more? */
449 finish_daemon();
452 /* write our pid to the pid file, if we do not have write permissions we will log a warning */
453 if(options.PidFile)
454 write_pidfile(options.PidFile);
456 return 0;
459 static int do_hup(void) {
460 char keydir[512];
462 log_fn(LOG_WARN,"Received sighup. Reloading config.");
463 /* first, reload config variables, in case they've changed */
464 /* no need to provide argc/v, they've been cached inside init_from_config */
465 if (init_from_config(0, NULL) < 0) {
466 exit(1);
468 if(retry_all_connections() < 0) {
469 log_fn(LOG_ERR,"Failed to bind one of the listener ports.");
470 return -1;
472 if(options.DirPort) {
473 /* reload the approved-routers file */
474 sprintf(keydir,"%s/approved-routers", options.DataDirectory);
475 log_fn(LOG_INFO,"Reloading approved fingerprints from %s...",keydir);
476 if(dirserv_parse_fingerprint_file(keydir) < 0) {
477 log_fn(LOG_WARN, "Error reloading fingerprints. Continuing with old list.");
479 } else {
480 /* fetch a new directory */
481 directory_initiate_command(router_pick_directory_server(), DIR_CONN_STATE_CONNECTING_FETCH);
483 if(options.ORPort) {
484 router_rebuild_descriptor();
485 sprintf(keydir,"%s/router.desc", options.DataDirectory);
486 log_fn(LOG_INFO,"Dumping descriptor to %s...",keydir);
487 if (write_str_to_file(keydir, router_get_my_descriptor())) {
488 return -1;
491 return 0;
494 static int do_main_loop(void) {
495 int i;
496 int timeout;
497 int poll_result;
499 /* load the routers file */
500 if(options.RouterFile &&
501 router_set_routerlist_from_file(options.RouterFile) < 0) {
502 log_fn(LOG_ERR,"Error loading router list.");
503 return -1;
506 /* load the private keys, if we're supposed to have them, and set up the
507 * TLS context. */
508 if (init_keys() < 0) {
509 log_fn(LOG_ERR,"Error initializing keys; exiting");
510 return -1;
513 if(options.ORPort) {
514 cpu_init(); /* launch cpuworkers. Need to do this *after* we've read the onion key. */
515 router_upload_desc_to_dirservers(); /* upload our descriptor to all dirservers */
518 /* start up the necessary connections based on which ports are
519 * non-zero. This is where we try to connect to all the other ORs,
520 * and start the listeners.
522 if(retry_all_connections() < 0) {
523 log_fn(LOG_ERR,"Failed to bind one of the listener ports.");
524 return -1;
527 for(;;) {
528 #ifndef MS_WINDOWS /* do signal stuff only on unix */
529 if(please_dumpstats) {
530 /* prefer to log it at INFO, but make sure we always see it */
531 dumpstats(options.loglevel>LOG_INFO ? options.loglevel : LOG_INFO);
532 please_dumpstats = 0;
534 if(please_reset) {
535 do_hup();
536 please_reset = 0;
538 if(please_reap_children) {
539 while(waitpid(-1,NULL,WNOHANG)) ; /* keep reaping until no more zombies */
540 please_reap_children = 0;
542 #endif /* signal stuff */
544 timeout = prepare_for_poll();
546 /* poll until we have an event, or the second ends */
547 poll_result = tor_poll(poll_array, nfds, timeout);
549 /* let catch() handle things like ^c, and otherwise don't worry about it */
550 if(poll_result < 0) {
551 if(errno != EINTR) { /* let the program survive things like ^z */
552 log_fn(LOG_ERR,"poll failed: %s",strerror(errno));
553 return -1;
554 } else {
555 log_fn(LOG_DEBUG,"poll interrupted.");
559 /* do all the reads and errors first, so we can detect closed sockets */
560 for(i=0;i<nfds;i++)
561 conn_read(i); /* this also marks broken connections */
563 /* then do the writes */
564 for(i=0;i<nfds;i++)
565 conn_write(i);
567 /* any of the conns need to be closed now? */
568 for(i=0;i<nfds;i++)
569 conn_close_if_marked(i);
571 /* refilling buckets and sending cells happens at the beginning of the
572 * next iteration of the loop, inside prepare_for_poll()
577 static void catch(int the_signal) {
579 #ifndef MS_WINDOWS /* do signal stuff only on unix */
580 switch(the_signal) {
581 // case SIGABRT:
582 case SIGTERM:
583 case SIGINT:
584 log(LOG_ERR,"Catching signal %d, exiting cleanly.", the_signal);
585 /* we don't care if there was an error when we unlink, nothing
586 we could do about it anyways */
587 if(options.PidFile)
588 unlink(options.PidFile);
589 exit(0);
590 case SIGHUP:
591 please_reset = 1;
592 break;
593 case SIGUSR1:
594 please_dumpstats = 1;
595 break;
596 case SIGCHLD:
597 please_reap_children = 1;
598 break;
599 default:
600 log(LOG_WARN,"Caught signal %d that we can't handle??", the_signal);
602 #endif /* signal stuff */
605 static void dumpstats(int severity) {
606 int i;
607 connection_t *conn;
608 time_t now = time(NULL);
610 log(severity, "Dumping stats:");
612 for(i=0;i<nfds;i++) {
613 conn = connection_array[i];
614 log(severity, "Conn %d (socket %d) type %d (%s), state %d (%s), created %ld secs ago",
615 i, conn->s, conn->type, conn_type_to_string[conn->type],
616 conn->state, conn_state_to_string[conn->type][conn->state], now - conn->timestamp_created);
617 if(!connection_is_listener(conn)) {
618 log(severity,"Conn %d is to '%s:%d'.",i,conn->address, conn->port);
619 log(severity,"Conn %d: %d bytes waiting on inbuf (last read %ld secs ago)",i,
620 (int)buf_datalen(conn->inbuf),
621 now - conn->timestamp_lastread);
622 log(severity,"Conn %d: %d bytes waiting on outbuf (last written %ld secs ago)",i,
623 (int)buf_datalen(conn->outbuf), now - conn->timestamp_lastwritten);
625 circuit_dump_by_conn(conn, severity); /* dump info about all the circuits using this conn */
627 log(severity,
628 "Cells processed: %10lu padding\n"
629 " %10lu create\n"
630 " %10lu created\n"
631 " %10lu relay\n"
632 " (%10lu relayed)\n"
633 " (%10lu delivered)\n"
634 " %10lu destroy",
635 stats_n_padding_cells_processed,
636 stats_n_create_cells_processed,
637 stats_n_created_cells_processed,
638 stats_n_relay_cells_processed,
639 stats_n_relay_cells_relayed,
640 stats_n_relay_cells_delivered,
641 stats_n_destroy_cells_processed);
642 if (stats_n_data_cells_packaged)
643 log(severity,"Average packaged cell fullness: %2.3f%%",
644 100*(((double)stats_n_data_bytes_packaged) /
645 (stats_n_data_cells_packaged*RELAY_PAYLOAD_SIZE)) );
646 if (stats_n_data_cells_received)
647 log(severity,"Average delivered cell fullness: %2.3f%%",
648 100*(((double)stats_n_data_bytes_received) /
649 (stats_n_data_cells_received*RELAY_PAYLOAD_SIZE)) );
651 if (stats_n_seconds_reading)
652 log(severity,"Average bandwidth used: %d bytes/sec",
653 (int) (stats_n_bytes_read/stats_n_seconds_reading));
656 int tor_main(int argc, char *argv[]) {
658 /* give it somewhere to log to initially */
659 add_stream_log(LOG_INFO, "<stdout>", stdout);
660 log_fn(LOG_WARN,"Tor v%s. This is experimental software. Do not use it if you need anonymity.",VERSION);
662 if (init_from_config(argc,argv) < 0)
663 return -1;
665 #ifndef MS_WINDOWS
666 if(geteuid()==0)
667 log_fn(LOG_WARN,"You are running Tor as root. You don't need to, and you probably shouldn't.");
668 #endif
670 if (options.RunAsDaemon) {
671 start_daemon();
674 if(options.ORPort) { /* only spawn dns handlers if we're a router */
675 dns_init(); /* initialize the dns resolve tree, and spawn workers */
677 if(options.SocksPort) {
678 client_dns_init(); /* init the client dns cache */
681 #ifndef MS_WINDOWS /* do signal stuff only on unix */
682 signal (SIGINT, catch); /* catch kills so we can exit cleanly */
683 signal (SIGTERM, catch);
684 signal (SIGUSR1, catch); /* to dump stats */
685 signal (SIGHUP, catch); /* to reload directory */
686 signal (SIGCHLD, catch); /* for exiting dns/cpu workers */
687 #endif /* signal stuff */
689 crypto_global_init();
690 crypto_seed_rng();
691 do_main_loop();
692 crypto_global_cleanup();
693 return -1;
697 Local Variables:
698 mode:c
699 indent-tabs-mode:nil
700 c-basic-offset:2
701 End: