main.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. /* Copyright 2001,2002,2003 Roger Dingledine, Matej Pfajfar. */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. #include "or.h"
  5. /********* START PROTOTYPES **********/
  6. static void dumpstats(int severity); /* log stats */
  7. static int init_from_config(int argc, char **argv);
  8. /********* START VARIABLES **********/
  9. extern char *conn_type_to_string[];
  10. extern char *conn_state_to_string[][_CONN_TYPE_MAX+1];
  11. or_options_t options; /* command-line and config-file options */
  12. int global_read_bucket; /* max number of bytes I can read this second */
  13. static int stats_prev_global_read_bucket;
  14. static uint64_t stats_n_bytes_read = 0;
  15. static long stats_n_seconds_reading = 0;
  16. static connection_t *connection_array[MAXCONNECTIONS] =
  17. { NULL };
  18. static struct pollfd poll_array[MAXCONNECTIONS];
  19. static int nfds=0; /* number of connections currently active */
  20. #ifndef MS_WINDOWS /* do signal stuff only on unix */
  21. static int please_dumpstats=0; /* whether we should dump stats during the loop */
  22. static int please_reset=0; /* whether we just got a sighup */
  23. static int please_reap_children=0; /* whether we should waitpid for exited children */
  24. #endif /* signal stuff */
  25. /********* END VARIABLES ************/
  26. /****************************************************************************
  27. *
  28. * This section contains accessors and other methods on the connection_array
  29. * and poll_array variables (which are global within this file and unavailable
  30. * outside it).
  31. *
  32. ****************************************************************************/
  33. int connection_add(connection_t *conn) {
  34. if(nfds >= options.MaxConn-1) {
  35. log(LOG_WARN,"connection_add(): failing because nfds is too high.");
  36. return -1;
  37. }
  38. conn->poll_index = nfds;
  39. connection_set_poll_socket(conn);
  40. connection_array[nfds] = conn;
  41. /* zero these out here, because otherwise we'll inherit values from the previously freed one */
  42. poll_array[nfds].events = 0;
  43. poll_array[nfds].revents = 0;
  44. nfds++;
  45. log(LOG_INFO,"connection_add(): new conn type %d, socket %d, nfds %d.",conn->type, conn->s, nfds);
  46. return 0;
  47. }
  48. void connection_set_poll_socket(connection_t *conn) {
  49. poll_array[conn->poll_index].fd = conn->s;
  50. }
  51. /* Remove the connection from the global list, and remove the
  52. * corresponding poll entry. Calling this function will shift the last
  53. * connection (if any) into the position occupied by conn.
  54. */
  55. int connection_remove(connection_t *conn) {
  56. int current_index;
  57. assert(conn);
  58. assert(nfds>0);
  59. log(LOG_INFO,"connection_remove(): removing socket %d, nfds now %d",conn->s, nfds-1);
  60. /* if it's an edge conn, remove it from the list
  61. * of conn's on this circuit. If it's not on an edge,
  62. * flush and send destroys for all circuits on this conn
  63. */
  64. circuit_about_to_close_connection(conn);
  65. current_index = conn->poll_index;
  66. if(current_index == nfds-1) { /* this is the end */
  67. nfds--;
  68. return 0;
  69. }
  70. /* we replace this one with the one at the end, then free it */
  71. nfds--;
  72. poll_array[current_index].fd = poll_array[nfds].fd;
  73. poll_array[current_index].events = poll_array[nfds].events;
  74. poll_array[current_index].revents = poll_array[nfds].revents;
  75. connection_array[current_index] = connection_array[nfds];
  76. connection_array[current_index]->poll_index = current_index;
  77. return 0;
  78. }
  79. void get_connection_array(connection_t ***array, int *n) {
  80. *array = connection_array;
  81. *n = nfds;
  82. }
  83. void connection_watch_events(connection_t *conn, short events) {
  84. assert(conn && conn->poll_index < nfds);
  85. poll_array[conn->poll_index].events = events;
  86. }
  87. int connection_is_reading(connection_t *conn) {
  88. return poll_array[conn->poll_index].events & POLLIN;
  89. }
  90. void connection_stop_reading(connection_t *conn) {
  91. assert(conn && conn->poll_index < nfds);
  92. log(LOG_DEBUG,"connection_stop_reading() called.");
  93. if(poll_array[conn->poll_index].events & POLLIN)
  94. poll_array[conn->poll_index].events -= POLLIN;
  95. }
  96. void connection_start_reading(connection_t *conn) {
  97. assert(conn && conn->poll_index < nfds);
  98. poll_array[conn->poll_index].events |= POLLIN;
  99. }
  100. void connection_stop_writing(connection_t *conn) {
  101. assert(conn && conn->poll_index < nfds);
  102. if(poll_array[conn->poll_index].events & POLLOUT)
  103. poll_array[conn->poll_index].events -= POLLOUT;
  104. }
  105. void connection_start_writing(connection_t *conn) {
  106. assert(conn && conn->poll_index < nfds);
  107. poll_array[conn->poll_index].events |= POLLOUT;
  108. }
  109. static void conn_read(int i) {
  110. connection_t *conn = connection_array[i];
  111. /* see http://www.greenend.org.uk/rjk/2001/06/poll.html for
  112. * discussion of POLLIN vs POLLHUP */
  113. if(!(poll_array[i].revents & (POLLIN|POLLHUP|POLLERR)))
  114. if(!connection_is_reading(conn) ||
  115. !connection_has_pending_tls_data(conn))
  116. return; /* this conn should not read */
  117. log_fn(LOG_DEBUG,"socket %d wants to read.",conn->s);
  118. assert_connection_ok(conn, time(NULL));
  119. if(
  120. /* XXX does POLLHUP also mean it's definitely broken? */
  121. #ifdef MS_WINDOWS
  122. (poll_array[i].revents & POLLERR) ||
  123. #endif
  124. connection_handle_read(conn) < 0)
  125. {
  126. /* this connection is broken. remove it */
  127. log_fn(LOG_INFO,"%s connection broken, removing.",
  128. conn_type_to_string[conn->type]);
  129. connection_remove(conn);
  130. connection_free(conn);
  131. if(i<nfds) {
  132. /* we just replaced the one at i with a new one. process it too. */
  133. conn_read(i);
  134. }
  135. } else assert_connection_ok(conn, time(NULL));
  136. }
  137. static void conn_write(int i) {
  138. connection_t *conn;
  139. if(!(poll_array[i].revents & POLLOUT))
  140. return; /* this conn doesn't want to write */
  141. conn = connection_array[i];
  142. log_fn(LOG_DEBUG,"socket %d wants to write.",conn->s);
  143. assert_connection_ok(conn, time(NULL));
  144. if(connection_handle_write(conn) < 0) { /* this connection is broken. remove it. */
  145. log_fn(LOG_INFO,"%s connection broken, removing.", conn_type_to_string[conn->type]);
  146. connection_remove(conn);
  147. connection_free(conn);
  148. if(i<nfds) { /* we just replaced the one at i with a new one. process it too. */
  149. conn_write(i);
  150. }
  151. } else assert_connection_ok(conn, time(NULL));
  152. }
  153. static void conn_close_if_marked(int i) {
  154. connection_t *conn;
  155. conn = connection_array[i];
  156. assert_connection_ok(conn, time(NULL));
  157. if(conn->marked_for_close) {
  158. log_fn(LOG_INFO,"Cleaning up connection (fd %d).",conn->s);
  159. if(conn->s >= 0) { /* might be an incomplete edge connection */
  160. /* FIXME there's got to be a better way to check for this -- and make other checks? */
  161. if(connection_speaks_cells(conn)) {
  162. if(conn->state == OR_CONN_STATE_OPEN)
  163. flush_buf_tls(conn->tls, conn->outbuf, &conn->outbuf_flushlen);
  164. } else {
  165. flush_buf(conn->s, conn->outbuf, &conn->outbuf_flushlen);
  166. }
  167. if(connection_wants_to_flush(conn)) /* not done flushing */
  168. log_fn(LOG_WARN,"Conn (socket %d) still wants to flush. Losing %d bytes!",conn->s, (int)buf_datalen(conn->inbuf));
  169. }
  170. connection_remove(conn);
  171. connection_free(conn);
  172. if(i<nfds) { /* we just replaced the one at i with a new one.
  173. process it too. */
  174. conn_close_if_marked(i);
  175. }
  176. }
  177. }
  178. /* Perform regular maintenance tasks for a single connection. This
  179. * function gets run once per second per connection by run_housekeeping.
  180. */
  181. static void run_connection_housekeeping(int i, time_t now) {
  182. cell_t cell;
  183. connection_t *conn = connection_array[i];
  184. if(connection_receiver_bucket_should_increase(conn)) {
  185. conn->receiver_bucket += conn->bandwidth;
  186. // log_fn(LOG_DEBUG,"Receiver bucket %d now %d.", i, conn->receiver_bucket);
  187. }
  188. if(conn->wants_to_read == 1 /* it's marked to turn reading back on now */
  189. && global_read_bucket > 0 /* and we're allowed to read */
  190. && (!connection_speaks_cells(conn) || conn->receiver_bucket > 0)) {
  191. /* and either a non-cell conn or a cell conn with non-empty bucket */
  192. conn->wants_to_read = 0;
  193. connection_start_reading(conn);
  194. if(conn->wants_to_write == 1) {
  195. conn->wants_to_write = 0;
  196. connection_start_writing(conn);
  197. }
  198. }
  199. /* check connections to see whether we should send a keepalive, expire, or wait */
  200. if(!connection_speaks_cells(conn))
  201. return;
  202. if(now >= conn->timestamp_lastwritten + options.KeepalivePeriod) {
  203. if((!options.ORPort && !circuit_get_by_conn(conn)) ||
  204. (!connection_state_is_open(conn))) {
  205. /* we're an onion proxy, with no circuits; or our handshake has expired. kill it. */
  206. log_fn(LOG_INFO,"Expiring connection to %d (%s:%d).",
  207. i,conn->address, conn->port);
  208. conn->marked_for_close = 1;
  209. } else {
  210. /* either a full router, or we've got a circuit. send a padding cell. */
  211. log_fn(LOG_DEBUG,"Sending keepalive to (%s:%d)",
  212. conn->address, conn->port);
  213. memset(&cell,0,sizeof(cell_t));
  214. cell.command = CELL_PADDING;
  215. connection_or_write_cell_to_buf(&cell, conn);
  216. }
  217. }
  218. }
  219. /* Perform regular maintenance tasks. This function gets run once per
  220. * second by prepare_for_poll.
  221. */
  222. static void run_scheduled_events(time_t now) {
  223. static long time_to_fetch_directory = 0;
  224. static long time_to_new_circuit = 0;
  225. circuit_t *circ;
  226. int i;
  227. /* 1. Every DirFetchPostPeriod seconds, we get a new directory and upload
  228. * our descriptor (if any). */
  229. if(time_to_fetch_directory < now) {
  230. /* it's time to fetch a new directory and/or post our descriptor */
  231. if(options.ORPort) {
  232. router_rebuild_descriptor();
  233. router_upload_desc_to_dirservers();
  234. }
  235. if(!options.DirPort) {
  236. /* NOTE directory servers do not currently fetch directories.
  237. * Hope this doesn't bite us later. */
  238. directory_initiate_command(router_pick_directory_server(),
  239. DIR_CONN_STATE_CONNECTING_FETCH);
  240. }
  241. time_to_fetch_directory = now + options.DirFetchPostPeriod;
  242. }
  243. /* 2. Every second, we examine pending circuits and prune the
  244. * ones which have been pending for more than 3 seconds.
  245. * We do this before step 3, so it can try building more if
  246. * it's not comfortable with the number of available circuits.
  247. */
  248. circuit_expire_building();
  249. /* 3. Every second, we try a new circuit if there are no valid
  250. * circuits. Every NewCircuitPeriod seconds, we expire circuits
  251. * that became dirty more than NewCircuitPeriod seconds ago,
  252. * and we make a new circ if there are no clean circuits.
  253. */
  254. if(options.SocksPort) {
  255. /* launch a new circ for any pending streams that need one */
  256. connection_ap_attach_pending();
  257. circ = circuit_get_newest(NULL, 1);
  258. if(time_to_new_circuit < now) {
  259. client_dns_clean();
  260. circuit_expire_unused_circuits();
  261. circuit_reset_failure_count();
  262. if(circ && circ->timestamp_dirty) {
  263. log_fn(LOG_INFO,"Youngest circuit dirty; launching replacement.");
  264. circuit_launch_new(); /* make a new circuit */
  265. }
  266. time_to_new_circuit = now + options.NewCircuitPeriod;
  267. }
  268. #define CIRCUIT_MIN_BUILDING 2
  269. if(!circ && circuit_count_building() < CIRCUIT_MIN_BUILDING) {
  270. /* if there's no open circ, and less than 2 are on the way,
  271. * go ahead and try another.
  272. */
  273. circuit_launch_new();
  274. }
  275. }
  276. /* 4. Every second, we check how much bandwidth we've consumed and
  277. * increment global_read_bucket.
  278. */
  279. stats_n_bytes_read += stats_prev_global_read_bucket-global_read_bucket;
  280. if(global_read_bucket < 9*options.TotalBandwidth) {
  281. global_read_bucket += options.TotalBandwidth;
  282. log_fn(LOG_DEBUG,"global_read_bucket now %d.", global_read_bucket);
  283. }
  284. stats_prev_global_read_bucket = global_read_bucket;
  285. /* 5. We do housekeeping for each connection... */
  286. for(i=0;i<nfds;i++) {
  287. run_connection_housekeeping(i, now);
  288. }
  289. /* 6. and blow away any connections that need to die. can't do this later
  290. * because we might open up a circuit and not realize we're about to cull
  291. * the connection it's running over.
  292. * XXX we can remove this step once we audit circuit-building to make sure
  293. * it doesn't pick a marked-for-close conn. -RD
  294. */
  295. for(i=0;i<nfds;i++)
  296. conn_close_if_marked(i);
  297. }
  298. static int prepare_for_poll(void) {
  299. static long current_second = 0; /* from previous calls to gettimeofday */
  300. connection_t *conn;
  301. struct timeval now;
  302. int i;
  303. tor_gettimeofday(&now);
  304. if(now.tv_sec > current_second) { /* the second has rolled over. check more stuff. */
  305. ++stats_n_seconds_reading;
  306. run_scheduled_events(now.tv_sec);
  307. current_second = now.tv_sec; /* remember which second it is, for next time */
  308. }
  309. for(i=0;i<nfds;i++) {
  310. conn = connection_array[i];
  311. if(connection_has_pending_tls_data(conn)) {
  312. log_fn(LOG_DEBUG,"sock %d has pending bytes.",conn->s);
  313. return 0; /* has pending bytes to read; don't let poll wait. */
  314. }
  315. }
  316. return (1000 - (now.tv_usec / 1000)); /* how many milliseconds til the next second? */
  317. }
  318. static int init_from_config(int argc, char **argv) {
  319. static int have_daemonized=0;
  320. if(getconfig(argc,argv,&options)) {
  321. log_fn(LOG_ERR,"Reading config failed. For usage, try -h.");
  322. return -1;
  323. }
  324. log_set_severity(options.loglevel); /* assign logging severity level from options */
  325. close_logs(); /* we'll close, then open with correct loglevel if necessary */
  326. if(!options.LogFile && !options.RunAsDaemon)
  327. add_stream_log(options.loglevel, "<stdout>", stdout);
  328. if(options.LogFile)
  329. if (add_file_log(options.loglevel, options.LogFile) != 0) {
  330. /* opening the log file failed! Use stderr and log a warning */
  331. add_stream_log(options.loglevel, "<stderr>", stderr);
  332. log_fn(LOG_WARN, "Cannot write to LogFile '%s': %s.", options.LogFile, strerror(errno));
  333. }
  334. if(options.DebugLogFile)
  335. if (add_file_log(LOG_DEBUG, options.DebugLogFile) != 0)
  336. log_fn(LOG_WARN, "Cannot write to DebugLogFile '%s': %s.", options.LogFile, strerror(errno));
  337. global_read_bucket = options.TotalBandwidth; /* start it at 1 second of traffic */
  338. stats_prev_global_read_bucket = global_read_bucket;
  339. if(options.User || options.Group) {
  340. if(switch_id(options.User, options.Group) != 0) {
  341. return -1;
  342. }
  343. }
  344. if(options.RunAsDaemon && !have_daemonized) {
  345. daemonize();
  346. have_daemonized = 1;
  347. }
  348. /* write our pid to the pid file, if we do not have write permissions we will log a warning */
  349. if(options.PidFile)
  350. write_pidfile(options.PidFile);
  351. return 0;
  352. }
  353. static int do_main_loop(void) {
  354. int i;
  355. int timeout;
  356. int poll_result;
  357. /* load the routers file */
  358. if(router_set_routerlist_from_file(options.RouterFile) < 0) {
  359. log_fn(LOG_ERR,"Error loading router list.");
  360. return -1;
  361. }
  362. /* load the private keys, if we're supposed to have them, and set up the
  363. * TLS context. */
  364. if (init_keys() < 0) {
  365. log_fn(LOG_ERR,"Error initializing keys; exiting");
  366. return -1;
  367. }
  368. if(options.ORPort) {
  369. cpu_init(); /* launch cpuworkers. Need to do this *after* we've read the onion key. */
  370. router_upload_desc_to_dirservers(); /* upload our descriptor to all dirservers */
  371. }
  372. /* start up the necessary connections based on which ports are
  373. * non-zero. This is where we try to connect to all the other ORs,
  374. * and start the listeners.
  375. */
  376. if(retry_all_connections() < 0) {
  377. log_fn(LOG_ERR,"Failed to bind one of the listener ports.");
  378. return -1;
  379. }
  380. for(;;) {
  381. #ifndef MS_WINDOWS /* do signal stuff only on unix */
  382. if(please_dumpstats) {
  383. /* prefer to log it at INFO, but make sure we always see it */
  384. dumpstats(options.loglevel>LOG_INFO ? options.loglevel : LOG_INFO);
  385. please_dumpstats = 0;
  386. }
  387. if(please_reset) {
  388. log_fn(LOG_WARN,"Received sighup. Reloading config.");
  389. /* first, reload config variables, in case they've changed */
  390. if (init_from_config(0, NULL) < 0) {
  391. /* no need to provide argc/v, they've been cached inside init_from_config */
  392. exit(1);
  393. }
  394. if(options.DirPort) {
  395. /* reload the fingerprint file */
  396. char keydir[512];
  397. sprintf(keydir,"%s/approved-routers", options.DataDirectory);
  398. log_fn(LOG_INFO,"Reloading approved fingerprints from %s...",keydir);
  399. if(dirserv_parse_fingerprint_file(keydir) < 0) {
  400. log_fn(LOG_WARN, "Error reloading fingerprints. Continuing with old list.");
  401. }
  402. /* XXX do we really want to be resetting the routerlist here? */
  403. if(router_set_routerlist_from_file(options.RouterFile) < 0) {
  404. log(LOG_WARN,"Error reloading router list. Continuing with old list.");
  405. }
  406. } else {
  407. /* fetch a new directory */
  408. directory_initiate_command(router_pick_directory_server(), DIR_CONN_STATE_CONNECTING_FETCH);
  409. }
  410. please_reset = 0;
  411. }
  412. if(please_reap_children) {
  413. while(waitpid(-1,NULL,WNOHANG)) ; /* keep reaping until no more zombies */
  414. please_reap_children = 0;
  415. }
  416. #endif /* signal stuff */
  417. timeout = prepare_for_poll();
  418. /* poll until we have an event, or the second ends */
  419. poll_result = poll(poll_array, nfds, timeout);
  420. /* let catch() handle things like ^c, and otherwise don't worry about it */
  421. if(poll_result < 0) {
  422. if(errno != EINTR) { /* let the program survive things like ^z */
  423. log_fn(LOG_ERR,"poll failed.");
  424. return -1;
  425. } else {
  426. log_fn(LOG_DEBUG,"poll interrupted.");
  427. }
  428. }
  429. /* do all the reads and errors first, so we can detect closed sockets */
  430. for(i=0;i<nfds;i++)
  431. conn_read(i); /* this also blows away broken connections */
  432. /* then do the writes */
  433. for(i=0;i<nfds;i++)
  434. conn_write(i);
  435. /* any of the conns need to be closed now? */
  436. for(i=0;i<nfds;i++)
  437. conn_close_if_marked(i);
  438. /* refilling buckets and sending cells happens at the beginning of the
  439. * next iteration of the loop, inside prepare_for_poll()
  440. */
  441. }
  442. }
  443. static void catch(int the_signal) {
  444. #ifndef MS_WINDOWS /* do signal stuff only on unix */
  445. switch(the_signal) {
  446. // case SIGABRT:
  447. case SIGTERM:
  448. case SIGINT:
  449. log(LOG_ERR,"Catching signal %d, exiting cleanly.", the_signal);
  450. /* we don't care if there was an error when we unlink, nothing
  451. we could do about it anyways */
  452. if(options.PidFile)
  453. unlink(options.PidFile);
  454. exit(0);
  455. case SIGHUP:
  456. please_reset = 1;
  457. break;
  458. case SIGUSR1:
  459. please_dumpstats = 1;
  460. break;
  461. case SIGCHLD:
  462. please_reap_children = 1;
  463. break;
  464. default:
  465. log(LOG_WARN,"Caught signal %d that we can't handle??", the_signal);
  466. }
  467. #endif /* signal stuff */
  468. }
  469. static void dumpstats(int severity) {
  470. int i;
  471. connection_t *conn;
  472. time_t now = time(NULL);
  473. log(severity, "Dumping stats:");
  474. for(i=0;i<nfds;i++) {
  475. conn = connection_array[i];
  476. log(severity, "Conn %d (socket %d) type %d (%s), state %d (%s), created %ld secs ago",
  477. i, conn->s, conn->type, conn_type_to_string[conn->type],
  478. conn->state, conn_state_to_string[conn->type][conn->state], now - conn->timestamp_created);
  479. if(!connection_is_listener(conn)) {
  480. log(severity,"Conn %d is to '%s:%d'.",i,conn->address, conn->port);
  481. log(severity,"Conn %d: %d bytes waiting on inbuf (last read %ld secs ago)",i,
  482. (int)buf_datalen(conn->inbuf),
  483. now - conn->timestamp_lastread);
  484. log(severity,"Conn %d: %d bytes waiting on outbuf (last written %ld secs ago)",i,
  485. (int)buf_datalen(conn->outbuf), now - conn->timestamp_lastwritten);
  486. }
  487. circuit_dump_by_conn(conn, severity); /* dump info about all the circuits using this conn */
  488. }
  489. log(severity,
  490. "Cells processed: %10lu padding\n"
  491. " %10lu create\n"
  492. " %10lu created\n"
  493. " %10lu relay\n"
  494. " (%10lu relayed)\n"
  495. " (%10lu delivered)\n"
  496. " %10lud destroy",
  497. stats_n_padding_cells_processed,
  498. stats_n_create_cells_processed,
  499. stats_n_created_cells_processed,
  500. stats_n_relay_cells_processed,
  501. stats_n_relay_cells_relayed,
  502. stats_n_relay_cells_delivered,
  503. stats_n_destroy_cells_processed);
  504. if (stats_n_data_cells_packaged)
  505. log(severity,"Average outgoing cell fullness: %2.3f%%",
  506. 100*(((double)stats_n_data_bytes_packaged) /
  507. (stats_n_data_cells_packaged*(CELL_PAYLOAD_SIZE-RELAY_HEADER_SIZE))) );
  508. if (stats_n_data_cells_received)
  509. log(severity,"Average incoming cell fullness: %2.3f%%",
  510. 100*(((double)stats_n_data_bytes_received) /
  511. (stats_n_data_cells_received*(CELL_PAYLOAD_SIZE-RELAY_HEADER_SIZE))) );
  512. if (stats_n_seconds_reading)
  513. log(severity,"Average bandwidth used: %d bytes/sec",
  514. (int) (stats_n_bytes_read/stats_n_seconds_reading));
  515. }
  516. int tor_main(int argc, char *argv[]) {
  517. /* give it somewhere to log to initially */
  518. add_stream_log(LOG_INFO, "<stdout>", stdout);
  519. log_fn(LOG_WARN,"Tor v%s. This is experimental software. Do not use it if you need anonymity.",VERSION);
  520. if (init_from_config(argc,argv) < 0)
  521. return -1;
  522. if(options.ORPort) { /* only spawn dns handlers if we're a router */
  523. dns_init(); /* initialize the dns resolve tree, and spawn workers */
  524. }
  525. if(options.SocksPort) {
  526. client_dns_init(); /* init the client dns cache */
  527. }
  528. #ifndef MS_WINDOWS /* do signal stuff only on unix */
  529. signal (SIGINT, catch); /* catch kills so we can exit cleanly */
  530. signal (SIGTERM, catch);
  531. signal (SIGUSR1, catch); /* to dump stats */
  532. signal (SIGHUP, catch); /* to reload directory */
  533. signal (SIGCHLD, catch); /* for exiting dns/cpu workers */
  534. #endif /* signal stuff */
  535. crypto_global_init();
  536. crypto_seed_rng();
  537. do_main_loop();
  538. crypto_global_cleanup();
  539. return -1;
  540. }
  541. /*
  542. Local Variables:
  543. mode:c
  544. indent-tabs-mode:nil
  545. c-basic-offset:2
  546. End:
  547. */