main.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. /* Copyright 2001,2002 Roger Dingledine, Matej Pfajfar. */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. #include "or.h"
  5. /********* START VARIABLES **********/
  6. or_options_t options; /* command-line and config-file options */
  7. int global_role;
  8. static connection_t *connection_array[MAXCONNECTIONS] =
  9. { NULL };
  10. static struct pollfd poll_array[MAXCONNECTIONS];
  11. static int nfds=0; /* number of connections currently active */
  12. static int please_dumpstats=0; /* whether we should dump stats during the loop */
  13. static int please_fetch_directory=0; /* whether we should fetch a new directory */
  14. /* private key */
  15. static crypto_pk_env_t *privatekey;
  16. routerinfo_t *my_routerinfo=NULL;
  17. /********* END VARIABLES ************/
  18. void setprivatekey(crypto_pk_env_t *k) {
  19. privatekey = k;
  20. }
  21. crypto_pk_env_t *getprivatekey(void) {
  22. assert(privatekey);
  23. return privatekey;
  24. }
  25. /****************************************************************************
  26. *
  27. * This section contains accessors and other methods on the connection_array
  28. * and poll_array variables (which are global within this file and unavailable
  29. * outside it).
  30. *
  31. ****************************************************************************/
  32. int connection_add(connection_t *conn) {
  33. if(nfds >= options.MaxConn-1) {
  34. log(LOG_INFO,"connection_add(): failing because nfds is too high.");
  35. return -1;
  36. }
  37. conn->poll_index = nfds;
  38. connection_set_poll_socket(conn);
  39. connection_array[nfds] = conn;
  40. /* zero these out here, because otherwise we'll inherit values from the previously freed one */
  41. poll_array[nfds].events = 0;
  42. poll_array[nfds].revents = 0;
  43. nfds++;
  44. log(LOG_INFO,"connection_add(): new conn type %d, socket %d, nfds %d.",conn->type, conn->s, nfds);
  45. return 0;
  46. }
  47. void connection_set_poll_socket(connection_t *conn) {
  48. poll_array[conn->poll_index].fd = conn->s;
  49. }
  50. int connection_remove(connection_t *conn) {
  51. int current_index;
  52. assert(conn);
  53. assert(nfds>0);
  54. log(LOG_INFO,"connection_remove(): removing socket %d, nfds now %d",conn->s, nfds-1);
  55. circuit_about_to_close_connection(conn); /* if it's an edge conn, remove it from the list
  56. * of conn's on this circuit. If it's not on an edge,
  57. * flush and send destroys for all circuits on this conn
  58. */
  59. current_index = conn->poll_index;
  60. if(current_index == nfds-1) { /* this is the end */
  61. nfds--;
  62. return 0;
  63. }
  64. /* we replace this one with the one at the end, then free it */
  65. nfds--;
  66. poll_array[current_index].fd = poll_array[nfds].fd;
  67. poll_array[current_index].events = poll_array[nfds].events;
  68. poll_array[current_index].revents = poll_array[nfds].revents;
  69. connection_array[current_index] = connection_array[nfds];
  70. connection_array[current_index]->poll_index = current_index;
  71. return 0;
  72. }
  73. connection_t *connection_twin_get_by_addr_port(uint32_t addr, uint16_t port) {
  74. /* Find a connection to the router described by addr and port,
  75. * or alternately any router which knows its key.
  76. * This connection *must* be in 'open' state.
  77. * If not, return NULL.
  78. */
  79. int i;
  80. connection_t *conn;
  81. routerinfo_t *router;
  82. /* first check if it's there exactly */
  83. conn = connection_exact_get_by_addr_port(addr,port);
  84. if(conn && connection_state_is_open(conn)) {
  85. log(LOG_INFO,"connection_twin_get_by_addr_port(): Found exact match.");
  86. return conn;
  87. }
  88. /* now check if any of the other open connections are a twin for this one */
  89. router = router_get_by_addr_port(addr,port);
  90. if(!router)
  91. return NULL;
  92. for(i=0;i<nfds;i++) {
  93. conn = connection_array[i];
  94. assert(conn);
  95. if(connection_state_is_open(conn) && !crypto_pk_cmp_keys(conn->pkey, router->pkey)) {
  96. log(LOG_INFO,"connection_twin_get_by_addr_port(): Found twin (%s).",conn->address);
  97. return conn;
  98. }
  99. }
  100. /* guess not */
  101. return NULL;
  102. }
  103. connection_t *connection_exact_get_by_addr_port(uint32_t addr, uint16_t port) {
  104. int i;
  105. connection_t *conn;
  106. for(i=0;i<nfds;i++) {
  107. conn = connection_array[i];
  108. assert(conn);
  109. if(conn->addr == addr && conn->port == port)
  110. return conn;
  111. }
  112. return NULL;
  113. }
  114. connection_t *connection_get_by_type(int type) {
  115. int i;
  116. connection_t *conn;
  117. for(i=0;i<nfds;i++) {
  118. conn = connection_array[i];
  119. if(conn->type == type)
  120. return conn;
  121. }
  122. return NULL;
  123. }
  124. void connection_watch_events(connection_t *conn, short events) {
  125. assert(conn && conn->poll_index < nfds);
  126. poll_array[conn->poll_index].events = events;
  127. }
  128. void connection_stop_reading(connection_t *conn) {
  129. assert(conn && conn->poll_index < nfds);
  130. log(LOG_DEBUG,"connection_stop_reading() called.");
  131. if(poll_array[conn->poll_index].events & POLLIN)
  132. poll_array[conn->poll_index].events -= POLLIN;
  133. }
  134. void connection_start_reading(connection_t *conn) {
  135. assert(conn && conn->poll_index < nfds);
  136. poll_array[conn->poll_index].events |= POLLIN;
  137. }
  138. void connection_stop_writing(connection_t *conn) {
  139. assert(conn && conn->poll_index < nfds);
  140. if(poll_array[conn->poll_index].events & POLLOUT)
  141. poll_array[conn->poll_index].events -= POLLOUT;
  142. }
  143. void connection_start_writing(connection_t *conn) {
  144. assert(conn && conn->poll_index < nfds);
  145. poll_array[conn->poll_index].events |= POLLOUT;
  146. }
  147. void check_conn_read(int i) {
  148. int retval;
  149. connection_t *conn;
  150. if(poll_array[i].revents & POLLIN) { /* something to read */
  151. conn = connection_array[i];
  152. assert(conn);
  153. // log(LOG_DEBUG,"check_conn_read(): socket %d has something to read.",conn->s);
  154. if (conn->type == CONN_TYPE_OP_LISTENER) {
  155. retval = connection_op_handle_listener_read(conn);
  156. } else if (conn->type == CONN_TYPE_OR_LISTENER) {
  157. retval = connection_or_handle_listener_read(conn);
  158. } else if (conn->type == CONN_TYPE_AP_LISTENER) {
  159. retval = connection_ap_handle_listener_read(conn);
  160. } else if (conn->type == CONN_TYPE_DIR_LISTENER) {
  161. retval = connection_dir_handle_listener_read(conn);
  162. } else {
  163. retval = connection_read_to_buf(conn);
  164. if (retval < 0 && conn->type == CONN_TYPE_DIR && conn->state == DIR_CONN_STATE_CONNECTING) {
  165. /* it's a directory server and connecting failed: forget about this router */
  166. router_forget_router(conn->addr,conn->port); /* FIXME i don't think this function works. */
  167. }
  168. if (retval >= 0) { /* all still well */
  169. retval = connection_process_inbuf(conn);
  170. // log(LOG_DEBUG,"check_conn_read(): connection_process_inbuf returned %d.",retval);
  171. if(retval >= 0 && !connection_state_is_open(conn) && conn->receiver_bucket == 0) {
  172. log(LOG_DEBUG,"check_conn_read(): receiver bucket reached 0 before handshake finished. Closing.");
  173. retval = -1;
  174. }
  175. }
  176. }
  177. if(retval < 0) { /* this connection is broken. remove it */
  178. log(LOG_INFO,"check_conn_read(): Connection broken, removing.");
  179. connection_remove(conn);
  180. connection_free(conn);
  181. if(i<nfds) { /* we just replaced the one at i with a new one.
  182. process it too. */
  183. check_conn_read(i);
  184. }
  185. }
  186. }
  187. }
  188. void check_conn_write(int i) {
  189. int retval;
  190. connection_t *conn;
  191. if(poll_array[i].revents & POLLOUT) { /* something to write */
  192. conn = connection_array[i];
  193. // log(LOG_DEBUG,"check_conn_write(): socket %d wants to write.",conn->s);
  194. if(connection_is_listener(conn)) {
  195. log(LOG_DEBUG,"check_conn_write(): Got a listener socket. Can't happen!");
  196. retval = -1;
  197. } else {
  198. /* else it's an OP, OR, or exit */
  199. retval = connection_flush_buf(conn); /* conns in CONNECTING state will fall through... */
  200. if(retval == 0) { /* it's done flushing */
  201. retval = connection_finished_flushing(conn); /* ...and get handled here. */
  202. }
  203. }
  204. if(retval < 0) { /* this connection is broken. remove it. */
  205. log(LOG_DEBUG,"check_conn_write(): Connection broken, removing.");
  206. connection_remove(conn);
  207. connection_free(conn);
  208. if(i<nfds) { /* we just replaced the one at i with a new one.
  209. process it too. */
  210. check_conn_write(i);
  211. }
  212. }
  213. }
  214. }
  215. void check_conn_marked(int i) {
  216. connection_t *conn;
  217. conn = connection_array[i];
  218. assert(conn);
  219. if(conn->marked_for_close) {
  220. log(LOG_DEBUG,"check_conn_marked(): Cleaning up connection.");
  221. if(conn->s >= 0) { /* might be an incomplete exit connection */
  222. /* FIXME there's got to be a better way to check for this -- and make other checks? */
  223. connection_flush_buf(conn); /* flush it first */
  224. }
  225. connection_remove(conn);
  226. connection_free(conn);
  227. if(i<nfds) { /* we just replaced the one at i with a new one.
  228. process it too. */
  229. check_conn_marked(i);
  230. }
  231. }
  232. }
  233. int prepare_for_poll(int *timeout) {
  234. int i;
  235. // int need_to_wake_soon = 0;
  236. connection_t *conn = NULL;
  237. connection_t *tmpconn;
  238. struct timeval now, soonest;
  239. static long current_second = 0; /* from previous calls to gettimeofday */
  240. static long time_to_rebuild_directory = 0;
  241. static long time_to_fetch_directory = 0;
  242. int ms_until_conn;
  243. cell_t cell;
  244. if(gettimeofday(&now,NULL) < 0)
  245. return -1;
  246. if(options.Role & ROLE_DIR_SERVER) {
  247. if(time_to_rebuild_directory < now.tv_sec) {
  248. /* it's time to rebuild our directory */
  249. if(time_to_rebuild_directory == 0) {
  250. /* we just started up. if we build a directory now it will be meaningless. */
  251. log(LOG_DEBUG,"prepare_for_poll(): Delaying initial dir build for 10 seconds.");
  252. time_to_rebuild_directory = now.tv_sec + 10; /* try in 10 seconds */
  253. } else {
  254. directory_rebuild();
  255. time_to_rebuild_directory = now.tv_sec + options.DirRebuildPeriod;
  256. }
  257. }
  258. *timeout = 1000*(time_to_rebuild_directory - now.tv_sec) + (1000 - (now.tv_usec / 1000));
  259. // log(LOG_DEBUG,"prepare_for_poll(): DirBuild timeout is %d",*timeout);
  260. }
  261. if(!(options.Role & ROLE_DIR_SERVER)) {
  262. if(time_to_fetch_directory < now.tv_sec) {
  263. /* it's time to fetch a new directory */
  264. /* NOTE directory servers do not currently fetch directories.
  265. * Hope this doesn't bite us later.
  266. */
  267. directory_initiate_fetch(router_pick_directory_server());
  268. time_to_fetch_directory = now.tv_sec + options.DirFetchPeriod;
  269. }
  270. *timeout = 1000*(time_to_fetch_directory - now.tv_sec) + (1000 - (now.tv_usec / 1000));
  271. }
  272. /* check connections to see whether we should send a keepalive, expire, or wait */
  273. for(i=0;i<nfds;i++) {
  274. tmpconn = connection_array[i];
  275. if(!connection_speaks_cells(tmpconn))
  276. continue; /* this conn type doesn't send cells */
  277. if(now.tv_sec >= tmpconn->timestamp_lastwritten + options.KeepalivePeriod) {
  278. if((!(options.Role & ROLE_OR_CONNECT_ALL) && !circuit_get_by_conn(tmpconn)) ||
  279. (!connection_state_is_open(tmpconn))) {
  280. /* we're an onion proxy, with no circuits; or our handshake has expired. kill it. */
  281. log(LOG_DEBUG,"prepare_for_poll(): Expiring connection to %d (%s:%d).",
  282. i,tmpconn->address, tmpconn->port);
  283. tmpconn->marked_for_close = 1;
  284. } else {
  285. /* either a full router, or we've got a circuit. send a padding cell. */
  286. // log(LOG_DEBUG,"prepare_for_poll(): Sending keepalive to (%s:%d)",
  287. // tmpconn->address, tmpconn->port);
  288. memset(&cell,0,sizeof(cell_t));
  289. cell.command = CELL_PADDING;
  290. if(connection_write_cell_to_buf(&cell, tmpconn) < 0)
  291. tmpconn->marked_for_close = 1;
  292. }
  293. }
  294. if(!tmpconn->marked_for_close &&
  295. *timeout > 1000*(tmpconn->timestamp_lastwritten + options.KeepalivePeriod - now.tv_sec)) {
  296. *timeout = 1000*(tmpconn->timestamp_lastwritten + options.KeepalivePeriod - now.tv_sec);
  297. }
  298. }
  299. assert(*timeout >= 0);
  300. /* blow away any connections that need to die. can't do this later
  301. * because we might open up a circuit and not realize it we're about to cull it.
  302. */
  303. for(i=0;i<nfds;i++)
  304. check_conn_marked(i);
  305. if(now.tv_sec > current_second) { /* the second has already rolled over! */
  306. for(i=0;i<nfds;i++) {
  307. connection_increment_receiver_bucket(connection_array[i]);
  308. connection_array[i]->onions_handled_this_second = 0;
  309. }
  310. current_second = now.tv_sec; /* remember which second it is, for next time */
  311. }
  312. /* this timeout is definitely sooner than any of the above ones */
  313. *timeout = 1000 - (now.tv_usec / 1000); /* how many milliseconds til the next second? */
  314. if(options.LinkPadding) {
  315. /* now check which conn wants to speak soonest */
  316. for(i=0;i<nfds;i++) {
  317. tmpconn = connection_array[i];
  318. if(!connection_speaks_cells(tmpconn))
  319. continue; /* this conn type doesn't send cells */
  320. if(!connection_state_is_open(tmpconn))
  321. continue; /* only conns in state 'open' have a valid send_timeval */
  322. while(tv_cmp(&tmpconn->send_timeval,&now) <= 0) { /* send_timeval has already passed, let it send a cell */
  323. // log(LOG_DEBUG,"prepare_for_poll(): doing backlogged connection_send_cell on socket %d (%d ms old)",tmpconn->s,
  324. // (now.tv_sec - tmpconn->send_timeval.tv_sec)*1000 +
  325. // (now.tv_usec - tmpconn->send_timeval.tv_usec)/1000
  326. // );
  327. connection_send_cell(tmpconn);
  328. }
  329. if(!conn || tv_cmp(&tmpconn->send_timeval, &soonest) < 0) { /* this is the best choice so far */
  330. // log(LOG_DEBUG,"prepare_for_poll(): chose socket %d as best connection so far",tmpconn->s);
  331. conn = tmpconn;
  332. soonest.tv_sec = conn->send_timeval.tv_sec;
  333. soonest.tv_usec = conn->send_timeval.tv_usec;
  334. }
  335. }
  336. if(conn) { /* we might want to set *timeout sooner */
  337. ms_until_conn = (soonest.tv_sec - now.tv_sec)*1000 +
  338. (soonest.tv_usec - now.tv_usec)/1000;
  339. // log(LOG_DEBUG,"prepare_for_poll(): conn %d times out in %d ms.",conn->s, ms_until_conn);
  340. if(ms_until_conn < *timeout) { /* use the new one */
  341. // log(LOG_DEBUG,"prepare_for_poll(): conn %d soonest, in %d ms.",conn->s,ms_until_conn);
  342. *timeout = ms_until_conn;
  343. }
  344. }
  345. }
  346. if(onion_pending_check()) {
  347. /* there's an onion pending. check for new things to do, but don't wait any time */
  348. *timeout = 0;
  349. }
  350. return 0;
  351. }
  352. int do_main_loop(void) {
  353. int i;
  354. int timeout;
  355. int poll_result;
  356. crypto_pk_env_t *prkey;
  357. /* load the routers file */
  358. if(router_get_list_from_file(options.RouterFile) < 0) {
  359. log(LOG_ERR,"Error loading router list.");
  360. return -1;
  361. }
  362. /* load the private key, if we're supposed to have one */
  363. if(ROLE_IS_OR(global_role)) {
  364. prkey = crypto_new_pk_env(CRYPTO_PK_RSA);
  365. if (!prkey) {
  366. log(LOG_ERR,"Error creating a crypto environment.");
  367. return -1;
  368. }
  369. if (crypto_pk_read_private_key_from_filename(prkey, options.PrivateKeyFile))
  370. {
  371. log(LOG_ERR,"Error loading private key.");
  372. return -1;
  373. }
  374. setprivatekey(prkey);
  375. }
  376. /* start-up the necessary connections based on global_role. This is where we
  377. * try to connect to all the other ORs, and start the listeners */
  378. retry_all_connections(options.Role, options.ORPort,
  379. options.OPPort, options.APPort, options.DirPort);
  380. for(;;) {
  381. if(please_dumpstats) {
  382. dumpstats();
  383. please_dumpstats = 0;
  384. }
  385. if(please_fetch_directory) {
  386. if(options.Role & ROLE_DIR_SERVER) {
  387. if(router_get_list_from_file(options.RouterFile) < 0) {
  388. log(LOG_ERR,"Error reloading router list. Continuing with old list.");
  389. }
  390. } else {
  391. directory_initiate_fetch(router_pick_directory_server());
  392. }
  393. please_fetch_directory = 0;
  394. }
  395. if(prepare_for_poll(&timeout) < 0) {
  396. log(LOG_DEBUG,"do_main_loop(): prepare_for_poll failed, exiting.");
  397. return -1;
  398. }
  399. /* now timeout is the value we'll hand to poll. It's either -1, meaning
  400. * don't timeout, else it indicates the soonest event (either the
  401. * one-second rollover for refilling receiver buckets, or the soonest
  402. * conn that needs to send a cell)
  403. */
  404. /* if the timeout is less than 10, set it to 10 */
  405. if(timeout > 0 && timeout < 10)
  406. timeout = 10;
  407. /* poll until we have an event, or it's time to do something */
  408. poll_result = poll(poll_array, nfds, timeout);
  409. #if 0 /* let catch() handle things like ^c, and otherwise don't worry about it */
  410. if(poll_result < 0) {
  411. log(LOG_ERR,"do_main_loop(): poll failed.");
  412. if(errno != EINTR) /* let the program survive things like ^z */
  413. return -1;
  414. }
  415. #endif
  416. if(poll_result == 0) {
  417. /* poll timed out without anything to do. process a pending onion, if any. */
  418. onion_pending_process_one();
  419. }
  420. if(poll_result > 0) { /* we have at least one connection to deal with */
  421. /* do all the reads first, so we can detect closed sockets */
  422. for(i=0;i<nfds;i++)
  423. check_conn_read(i); /* this also blows away broken connections */
  424. /* then do the writes */
  425. for(i=0;i<nfds;i++)
  426. check_conn_write(i);
  427. /* any of the conns need to be closed now? */
  428. for(i=0;i<nfds;i++)
  429. check_conn_marked(i);
  430. }
  431. /* refilling buckets and sending cells happens at the beginning of the
  432. * next iteration of the loop, inside prepare_for_poll()
  433. */
  434. }
  435. }
  436. static void catch(int the_signal) {
  437. switch(the_signal) {
  438. // case SIGABRT:
  439. case SIGTERM:
  440. case SIGINT:
  441. log(LOG_NOTICE,"Catching signal %d, exiting cleanly.", the_signal);
  442. exit(0);
  443. case SIGHUP:
  444. please_fetch_directory = 1;
  445. break;
  446. case SIGUSR1:
  447. please_dumpstats = 1;
  448. break;
  449. default:
  450. log(LOG_ERR,"Caught signal that we can't handle??");
  451. }
  452. }
  453. void dumpstats (void) { /* dump stats to stdout */
  454. int i;
  455. connection_t *conn;
  456. struct timeval now;
  457. extern char *conn_type_to_string[];
  458. extern char *conn_state_to_string[][15];
  459. printf("Dumping stats:\n");
  460. if(gettimeofday(&now,NULL) < 0)
  461. return ;
  462. for(i=0;i<nfds;i++) {
  463. conn = connection_array[i];
  464. printf("Conn %d (socket %d) type %d (%s), state %d (%s), created %ld secs ago\n",
  465. i, conn->s, conn->type, conn_type_to_string[conn->type],
  466. conn->state, conn_state_to_string[conn->type][conn->state], now.tv_sec - conn->timestamp_created);
  467. if(!connection_is_listener(conn)) {
  468. printf("Conn %d is to '%s:%d'.\n",i,conn->address, conn->port);
  469. printf("Conn %d: %d bytes waiting on inbuf (last read %ld secs ago)\n",i,conn->inbuf_datalen,
  470. now.tv_sec - conn->timestamp_lastread);
  471. printf("Conn %d: %d bytes waiting on outbuf (last written %ld secs ago)\n",i,conn->outbuf_datalen,
  472. now.tv_sec - conn->timestamp_lastwritten);
  473. }
  474. circuit_dump_by_conn(conn); /* dump info about all the circuits using this conn */
  475. printf("\n");
  476. }
  477. }
  478. int dump_router_to_string(char *s, int maxlen, routerinfo_t *router) {
  479. char *pkey;
  480. int pkeylen;
  481. int written;
  482. if(crypto_pk_write_public_key_to_string(router->pkey,&pkey,&pkeylen)<0) {
  483. log(LOG_ERR,"dump_directory_to_string(): write pkey to string failed!");
  484. return 0;
  485. }
  486. written = snprintf(s, maxlen, "%s %d %d %d %d %d\n%s\n",
  487. router->address,
  488. router->or_port,
  489. router->op_port,
  490. router->ap_port,
  491. router->dir_port,
  492. router->bandwidth,
  493. pkey);
  494. free(pkey);
  495. return written;
  496. }
  497. void dump_directory_to_string(char *s, int maxlen) {
  498. int i;
  499. connection_t *conn;
  500. routerinfo_t *router;
  501. int written;
  502. /* first write my own info */
  503. if(my_routerinfo) {
  504. written = dump_router_to_string(s, maxlen, my_routerinfo);
  505. maxlen -= written;
  506. s += written;
  507. }
  508. /* now write info for other routers */
  509. for(i=0;i<nfds;i++) {
  510. conn = connection_array[i];
  511. if(conn->type != CONN_TYPE_OR)
  512. continue; /* we only want to list ORs */
  513. if(conn->state != OR_CONN_STATE_OPEN)
  514. continue; /* we only want to list ones that successfully handshaked */
  515. router = router_get_by_addr_port(conn->addr,conn->port);
  516. if(!router) {
  517. log(LOG_ERR,"dump_directory_to_string(): couldn't find router %d:%d!",conn->addr,conn->port);
  518. continue;
  519. }
  520. written = dump_router_to_string(s, maxlen, router);
  521. if(written < 0 || written > maxlen) {
  522. /* apparently different glibcs do different things on error.. so check both */
  523. log(LOG_ERR,"dump_directory_to_string(): tried to exceed string length.");
  524. s[maxlen-1] = 0; /* make sure it's null terminated */
  525. return;
  526. }
  527. maxlen -= written;
  528. s += written;
  529. }
  530. }
  531. int main(int argc, char *argv[]) {
  532. int retval = 0;
  533. if(getconfig(argc,argv,&options))
  534. exit(1);
  535. log(options.loglevel,NULL); /* assign logging severity level from options */
  536. global_role = options.Role; /* assign global_role from options. FIXME: remove from global namespace later. */
  537. if(options.Role & ROLE_OR_LISTEN) { /* only spawn dns handlers if we're a router */
  538. if(dns_master_start() < 0) {
  539. log(LOG_ERR,"main(): We're running without a dns handler. Bad news.");
  540. }
  541. }
  542. init_tracked_tree(); /* initialize the replay detection tree */
  543. signal (SIGINT, catch); /* catch kills so we can exit cleanly */
  544. signal (SIGTERM, catch);
  545. signal (SIGUSR1, catch); /* to dump stats to stdout */
  546. signal (SIGHUP, catch); /* to reload directory */
  547. crypto_global_init();
  548. retval = do_main_loop();
  549. crypto_global_cleanup();
  550. return retval;
  551. }