1 /* Copyright 2001,2002,2003 Roger Dingledine, Matej Pfajfar. */
2 /* See LICENSE for licensing information */
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
] =
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
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.");
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;
64 log(LOG_INFO
,"connection_add(): new conn type %d, socket %d, nfds %d.",conn
->type
, conn
->s
, nfds
);
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
) {
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 */
96 /* we replace this one with the one at the end, then free it */
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
;
107 void get_connection_array(connection_t
***array
, int *n
) {
108 *array
= connection_array
;
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
)
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? */
177 (poll_array
[i
].revents
& POLLERR
) ||
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
) {
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
)
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
) {
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
);
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.
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
) {
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
))
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
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;
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 */
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
) {
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
385 conn_close_if_marked(i
);
388 static int prepare_for_poll(void) {
389 static long current_second
= 0; /* from previous calls to gettimeofday */
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.");
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.",
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) {
447 if(options
.RunAsDaemon
) {
448 /* XXXX Can we delay this any more? */
452 /* write our pid to the pid file, if we do not have write permissions we will log a warning */
454 write_pidfile(options
.PidFile
);
459 static int do_hup(void) {
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) {
468 if(retry_all_connections() < 0) {
469 log_fn(LOG_ERR
,"Failed to bind one of the listener ports.");
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.");
480 /* fetch a new directory */
481 directory_initiate_command(router_pick_directory_server(), DIR_CONN_STATE_CONNECTING_FETCH
);
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())) {
494 static int do_main_loop(void) {
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.");
506 /* load the private keys, if we're supposed to have them, and set up the
508 if (init_keys() < 0) {
509 log_fn(LOG_ERR
,"Error initializing keys; exiting");
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.");
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;
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
));
555 log_fn(LOG_DEBUG
,"poll interrupted.");
559 /* do all the reads and errors first, so we can detect closed sockets */
561 conn_read(i
); /* this also marks broken connections */
563 /* then do the writes */
567 /* any of the conns need to be closed now? */
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 */
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 */
588 unlink(options
.PidFile
);
594 please_dumpstats
= 1;
597 please_reap_children
= 1;
600 log(LOG_WARN
,"Caught signal %d that we can't handle??", the_signal
);
602 #endif /* signal stuff */
605 static void dumpstats(int severity
) {
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 */
628 "Cells processed: %10lu padding\n"
633 " (%10lu delivered)\n"
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)
667 log_fn(LOG_WARN
,"You are running Tor as root. You don't need to, and you probably shouldn't.");
670 if (options
.RunAsDaemon
) {
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();
692 crypto_global_cleanup();