main.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  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) && buf_datalen(conn->outbuf)) {
  168. log_fn(LOG_WARN,"Conn (socket %d) still wants to flush. Losing %d bytes!",
  169. conn->s, (int)buf_datalen(conn->outbuf));
  170. }
  171. }
  172. connection_remove(conn);
  173. connection_free(conn);
  174. if(i<nfds) { /* we just replaced the one at i with a new one.
  175. process it too. */
  176. conn_close_if_marked(i);
  177. }
  178. }
  179. }
  180. /* Perform regular maintenance tasks for a single connection. This
  181. * function gets run once per second per connection by run_housekeeping.
  182. */
  183. static void run_connection_housekeeping(int i, time_t now) {
  184. cell_t cell;
  185. connection_t *conn = connection_array[i];
  186. if(connection_receiver_bucket_should_increase(conn)) {
  187. conn->receiver_bucket += conn->bandwidth;
  188. // log_fn(LOG_DEBUG,"Receiver bucket %d now %d.", i, conn->receiver_bucket);
  189. }
  190. if(conn->wants_to_read == 1 /* it's marked to turn reading back on now */
  191. && global_read_bucket > 0 /* and we're allowed to read */
  192. && (!connection_speaks_cells(conn) || conn->receiver_bucket > 0)) {
  193. /* and either a non-cell conn or a cell conn with non-empty bucket */
  194. conn->wants_to_read = 0;
  195. connection_start_reading(conn);
  196. if(conn->wants_to_write == 1) {
  197. conn->wants_to_write = 0;
  198. connection_start_writing(conn);
  199. }
  200. }
  201. /* check connections to see whether we should send a keepalive, expire, or wait */
  202. if(!connection_speaks_cells(conn))
  203. return;
  204. if(now >= conn->timestamp_lastwritten + options.KeepalivePeriod) {
  205. if((!options.ORPort && !circuit_get_by_conn(conn)) ||
  206. (!connection_state_is_open(conn))) {
  207. /* we're an onion proxy, with no circuits; or our handshake has expired. kill it. */
  208. log_fn(LOG_INFO,"Expiring connection to %d (%s:%d).",
  209. i,conn->address, conn->port);
  210. conn->marked_for_close = 1;
  211. } else {
  212. /* either a full router, or we've got a circuit. send a padding cell. */
  213. log_fn(LOG_DEBUG,"Sending keepalive to (%s:%d)",
  214. conn->address, conn->port);
  215. memset(&cell,0,sizeof(cell_t));
  216. cell.command = CELL_PADDING;
  217. connection_or_write_cell_to_buf(&cell, conn);
  218. }
  219. }
  220. }
  221. /* Perform regular maintenance tasks. This function gets run once per
  222. * second by prepare_for_poll.
  223. */
  224. static void run_scheduled_events(time_t now) {
  225. static long time_to_fetch_directory = 0;
  226. static long time_to_new_circuit = 0;
  227. circuit_t *circ;
  228. int i;
  229. /* 1. Every DirFetchPostPeriod seconds, we get a new directory and upload
  230. * our descriptor (if any). */
  231. if(time_to_fetch_directory < now) {
  232. /* it's time to fetch a new directory and/or post our descriptor */
  233. if(options.ORPort) {
  234. router_rebuild_descriptor();
  235. router_upload_desc_to_dirservers();
  236. }
  237. if(!options.DirPort) {
  238. /* NOTE directory servers do not currently fetch directories.
  239. * Hope this doesn't bite us later. */
  240. directory_initiate_command(router_pick_directory_server(),
  241. DIR_CONN_STATE_CONNECTING_FETCH);
  242. }
  243. time_to_fetch_directory = now + options.DirFetchPostPeriod;
  244. }
  245. /* 2. Every second, we examine pending circuits and prune the
  246. * ones which have been pending for more than 3 seconds.
  247. * We do this before step 3, so it can try building more if
  248. * it's not comfortable with the number of available circuits.
  249. */
  250. circuit_expire_building();
  251. /* 2b. Also look at pending streams and prune the ones that 'began'
  252. * a long time ago but haven't gotten a 'connected' yet.
  253. * Do this before step 3, so we can put them back into pending
  254. * state to be picked up by the new circuit.
  255. */
  256. connection_ap_expire_beginning();
  257. /* 3. Every second, we try a new circuit if there are no valid
  258. * circuits. Every NewCircuitPeriod seconds, we expire circuits
  259. * that became dirty more than NewCircuitPeriod seconds ago,
  260. * and we make a new circ if there are no clean circuits.
  261. */
  262. if(options.SocksPort) {
  263. /* launch a new circ for any pending streams that need one */
  264. connection_ap_attach_pending();
  265. circ = circuit_get_newest(NULL, 1);
  266. if(time_to_new_circuit < now) {
  267. client_dns_clean();
  268. circuit_expire_unused_circuits();
  269. circuit_reset_failure_count();
  270. if(circ && circ->timestamp_dirty) {
  271. log_fn(LOG_INFO,"Youngest circuit dirty; launching replacement.");
  272. circuit_launch_new(); /* make a new circuit */
  273. }
  274. time_to_new_circuit = now + options.NewCircuitPeriod;
  275. }
  276. #define CIRCUIT_MIN_BUILDING 3
  277. if(!circ && circuit_count_building() < CIRCUIT_MIN_BUILDING) {
  278. /* if there's no open circ, and less than 3 are on the way,
  279. * go ahead and try another.
  280. */
  281. circuit_launch_new();
  282. }
  283. }
  284. /* 4. Every second, we check how much bandwidth we've consumed and
  285. * increment global_read_bucket.
  286. */
  287. stats_n_bytes_read += stats_prev_global_read_bucket-global_read_bucket;
  288. if(global_read_bucket < options.BandwidthBurst) {
  289. global_read_bucket += options.BandwidthRate;
  290. log_fn(LOG_DEBUG,"global_read_bucket now %d.", global_read_bucket);
  291. }
  292. stats_prev_global_read_bucket = global_read_bucket;
  293. /* 5. We do housekeeping for each connection... */
  294. for(i=0;i<nfds;i++) {
  295. run_connection_housekeeping(i, now);
  296. }
  297. /* 6. and blow away any connections that need to die. can't do this later
  298. * because we might open up a circuit and not realize we're about to cull
  299. * the connection it's running over.
  300. * XXX we can remove this step once we audit circuit-building to make sure
  301. * it doesn't pick a marked-for-close conn. -RD
  302. */
  303. for(i=0;i<nfds;i++)
  304. conn_close_if_marked(i);
  305. }
  306. static int prepare_for_poll(void) {
  307. static long current_second = 0; /* from previous calls to gettimeofday */
  308. connection_t *conn;
  309. struct timeval now;
  310. int i;
  311. tor_gettimeofday(&now);
  312. if(now.tv_sec > current_second) { /* the second has rolled over. check more stuff. */
  313. ++stats_n_seconds_reading;
  314. run_scheduled_events(now.tv_sec);
  315. current_second = now.tv_sec; /* remember which second it is, for next time */
  316. }
  317. for(i=0;i<nfds;i++) {
  318. conn = connection_array[i];
  319. if(connection_has_pending_tls_data(conn)) {
  320. log_fn(LOG_DEBUG,"sock %d has pending bytes.",conn->s);
  321. return 0; /* has pending bytes to read; don't let poll wait. */
  322. }
  323. }
  324. return (1000 - (now.tv_usec / 1000)); /* how many milliseconds til the next second? */
  325. }
  326. static int init_from_config(int argc, char **argv) {
  327. if(getconfig(argc,argv,&options)) {
  328. log_fn(LOG_ERR,"Reading config failed. For usage, try -h.");
  329. return -1;
  330. }
  331. close_logs(); /* we'll close, then open with correct loglevel if necessary */
  332. if(!options.LogFile && !options.RunAsDaemon)
  333. add_stream_log(options.loglevel, "<stdout>", stdout);
  334. if(options.LogFile) {
  335. if (add_file_log(options.loglevel, options.LogFile) != 0) {
  336. /* opening the log file failed! Use stderr and log a warning */
  337. add_stream_log(options.loglevel, "<stderr>", stderr);
  338. log_fn(LOG_WARN, "Cannot write to LogFile '%s': %s.", options.LogFile, strerror(errno));
  339. }
  340. log_fn(LOG_WARN, "Successfully opened LogFile '%s', redirecting output.",
  341. options.LogFile);
  342. }
  343. if(options.DebugLogFile) {
  344. if (add_file_log(LOG_DEBUG, options.DebugLogFile) != 0)
  345. log_fn(LOG_WARN, "Cannot write to DebugLogFile '%s': %s.", options.DebugLogFile, strerror(errno));
  346. log_fn(LOG_DEBUG, "Successfully opened DebugLogFile '%s'.", options.DebugLogFile);
  347. }
  348. global_read_bucket = options.BandwidthBurst; /* start it at max traffic */
  349. stats_prev_global_read_bucket = global_read_bucket;
  350. if(options.User || options.Group) {
  351. if(switch_id(options.User, options.Group) != 0) {
  352. return -1;
  353. }
  354. }
  355. if(options.RunAsDaemon) {
  356. /* XXXX Can we delay this any more? */
  357. finish_daemon();
  358. }
  359. /* write our pid to the pid file, if we do not have write permissions we will log a warning */
  360. if(options.PidFile)
  361. write_pidfile(options.PidFile);
  362. return 0;
  363. }
  364. static int do_main_loop(void) {
  365. int i;
  366. int timeout;
  367. int poll_result;
  368. /* load the routers file */
  369. if(options.RouterFile &&
  370. router_set_routerlist_from_file(options.RouterFile) < 0) {
  371. log_fn(LOG_ERR,"Error loading router list.");
  372. return -1;
  373. }
  374. /* load the private keys, if we're supposed to have them, and set up the
  375. * TLS context. */
  376. if (init_keys() < 0) {
  377. log_fn(LOG_ERR,"Error initializing keys; exiting");
  378. return -1;
  379. }
  380. if(options.ORPort) {
  381. cpu_init(); /* launch cpuworkers. Need to do this *after* we've read the onion key. */
  382. router_upload_desc_to_dirservers(); /* upload our descriptor to all dirservers */
  383. }
  384. /* start up the necessary connections based on which ports are
  385. * non-zero. This is where we try to connect to all the other ORs,
  386. * and start the listeners.
  387. */
  388. if(retry_all_connections() < 0) {
  389. log_fn(LOG_ERR,"Failed to bind one of the listener ports.");
  390. return -1;
  391. }
  392. for(;;) {
  393. #ifndef MS_WINDOWS /* do signal stuff only on unix */
  394. if(please_dumpstats) {
  395. /* prefer to log it at INFO, but make sure we always see it */
  396. dumpstats(options.loglevel>LOG_INFO ? options.loglevel : LOG_INFO);
  397. please_dumpstats = 0;
  398. }
  399. if(please_reset) {
  400. log_fn(LOG_WARN,"Received sighup. Reloading config.");
  401. /* first, reload config variables, in case they've changed */
  402. if (init_from_config(0, NULL) < 0) {
  403. /* no need to provide argc/v, they've been cached inside init_from_config */
  404. exit(1);
  405. }
  406. if(retry_all_connections() < 0) {
  407. log_fn(LOG_ERR,"Failed to bind one of the listener ports.");
  408. return -1;
  409. }
  410. if(options.DirPort) {
  411. /* reload the approved-routers file */
  412. char keydir[512];
  413. sprintf(keydir,"%s/approved-routers", options.DataDirectory);
  414. log_fn(LOG_INFO,"Reloading approved fingerprints from %s...",keydir);
  415. if(dirserv_parse_fingerprint_file(keydir) < 0) {
  416. log_fn(LOG_WARN, "Error reloading fingerprints. Continuing with old list.");
  417. }
  418. } else {
  419. /* fetch a new directory */
  420. directory_initiate_command(router_pick_directory_server(), DIR_CONN_STATE_CONNECTING_FETCH);
  421. }
  422. please_reset = 0;
  423. }
  424. if(please_reap_children) {
  425. while(waitpid(-1,NULL,WNOHANG)) ; /* keep reaping until no more zombies */
  426. please_reap_children = 0;
  427. }
  428. #endif /* signal stuff */
  429. timeout = prepare_for_poll();
  430. /* poll until we have an event, or the second ends */
  431. poll_result = tor_poll(poll_array, nfds, timeout);
  432. /* let catch() handle things like ^c, and otherwise don't worry about it */
  433. if(poll_result < 0) {
  434. if(errno != EINTR) { /* let the program survive things like ^z */
  435. log_fn(LOG_ERR,"poll failed.");
  436. return -1;
  437. } else {
  438. log_fn(LOG_DEBUG,"poll interrupted.");
  439. }
  440. }
  441. /* do all the reads and errors first, so we can detect closed sockets */
  442. for(i=0;i<nfds;i++)
  443. conn_read(i); /* this also blows away broken connections */
  444. /* then do the writes */
  445. for(i=0;i<nfds;i++)
  446. conn_write(i);
  447. /* any of the conns need to be closed now? */
  448. for(i=0;i<nfds;i++)
  449. conn_close_if_marked(i);
  450. /* refilling buckets and sending cells happens at the beginning of the
  451. * next iteration of the loop, inside prepare_for_poll()
  452. */
  453. }
  454. }
  455. static void catch(int the_signal) {
  456. #ifndef MS_WINDOWS /* do signal stuff only on unix */
  457. switch(the_signal) {
  458. // case SIGABRT:
  459. case SIGTERM:
  460. case SIGINT:
  461. log(LOG_ERR,"Catching signal %d, exiting cleanly.", the_signal);
  462. /* we don't care if there was an error when we unlink, nothing
  463. we could do about it anyways */
  464. if(options.PidFile)
  465. unlink(options.PidFile);
  466. exit(0);
  467. case SIGHUP:
  468. please_reset = 1;
  469. break;
  470. case SIGUSR1:
  471. please_dumpstats = 1;
  472. break;
  473. case SIGCHLD:
  474. please_reap_children = 1;
  475. break;
  476. default:
  477. log(LOG_WARN,"Caught signal %d that we can't handle??", the_signal);
  478. }
  479. #endif /* signal stuff */
  480. }
  481. static void dumpstats(int severity) {
  482. int i;
  483. connection_t *conn;
  484. time_t now = time(NULL);
  485. log(severity, "Dumping stats:");
  486. for(i=0;i<nfds;i++) {
  487. conn = connection_array[i];
  488. log(severity, "Conn %d (socket %d) type %d (%s), state %d (%s), created %ld secs ago",
  489. i, conn->s, conn->type, conn_type_to_string[conn->type],
  490. conn->state, conn_state_to_string[conn->type][conn->state], now - conn->timestamp_created);
  491. if(!connection_is_listener(conn)) {
  492. log(severity,"Conn %d is to '%s:%d'.",i,conn->address, conn->port);
  493. log(severity,"Conn %d: %d bytes waiting on inbuf (last read %ld secs ago)",i,
  494. (int)buf_datalen(conn->inbuf),
  495. now - conn->timestamp_lastread);
  496. log(severity,"Conn %d: %d bytes waiting on outbuf (last written %ld secs ago)",i,
  497. (int)buf_datalen(conn->outbuf), now - conn->timestamp_lastwritten);
  498. }
  499. circuit_dump_by_conn(conn, severity); /* dump info about all the circuits using this conn */
  500. }
  501. log(severity,
  502. "Cells processed: %10lu padding\n"
  503. " %10lu create\n"
  504. " %10lu created\n"
  505. " %10lu relay\n"
  506. " (%10lu relayed)\n"
  507. " (%10lu delivered)\n"
  508. " %10lu destroy",
  509. stats_n_padding_cells_processed,
  510. stats_n_create_cells_processed,
  511. stats_n_created_cells_processed,
  512. stats_n_relay_cells_processed,
  513. stats_n_relay_cells_relayed,
  514. stats_n_relay_cells_delivered,
  515. stats_n_destroy_cells_processed);
  516. if (stats_n_data_cells_packaged)
  517. log(severity,"Average packaged cell fullness: %2.3f%%",
  518. 100*(((double)stats_n_data_bytes_packaged) /
  519. (stats_n_data_cells_packaged*RELAY_PAYLOAD_SIZE)) );
  520. if (stats_n_data_cells_received)
  521. log(severity,"Average delivered cell fullness: %2.3f%%",
  522. 100*(((double)stats_n_data_bytes_received) /
  523. (stats_n_data_cells_received*RELAY_PAYLOAD_SIZE)) );
  524. if (stats_n_seconds_reading)
  525. log(severity,"Average bandwidth used: %d bytes/sec",
  526. (int) (stats_n_bytes_read/stats_n_seconds_reading));
  527. }
  528. int tor_main(int argc, char *argv[]) {
  529. /* give it somewhere to log to initially */
  530. add_stream_log(LOG_INFO, "<stdout>", stdout);
  531. log_fn(LOG_WARN,"Tor v%s. This is experimental software. Do not use it if you need anonymity.",VERSION);
  532. if (init_from_config(argc,argv) < 0)
  533. return -1;
  534. #ifndef MS_WINDOWS
  535. if(geteuid()==0)
  536. log_fn(LOG_WARN,"You are running Tor as root. You don't need to, and you probably shouldn't.");
  537. #endif
  538. if (options.RunAsDaemon) {
  539. start_daemon();
  540. }
  541. if(options.ORPort) { /* only spawn dns handlers if we're a router */
  542. dns_init(); /* initialize the dns resolve tree, and spawn workers */
  543. }
  544. if(options.SocksPort) {
  545. client_dns_init(); /* init the client dns cache */
  546. }
  547. #ifndef MS_WINDOWS /* do signal stuff only on unix */
  548. signal (SIGINT, catch); /* catch kills so we can exit cleanly */
  549. signal (SIGTERM, catch);
  550. signal (SIGUSR1, catch); /* to dump stats */
  551. signal (SIGHUP, catch); /* to reload directory */
  552. signal (SIGCHLD, catch); /* for exiting dns/cpu workers */
  553. #endif /* signal stuff */
  554. crypto_global_init();
  555. crypto_seed_rng();
  556. do_main_loop();
  557. crypto_global_cleanup();
  558. return -1;
  559. }
  560. /*
  561. Local Variables:
  562. mode:c
  563. indent-tabs-mode:nil
  564. c-basic-offset:2
  565. End:
  566. */