main.c 22 KB

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