main.c 21 KB

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