main.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  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. // connection_t *conn = NULL;
  236. connection_t *tmpconn;
  237. struct timeval now; //soonest;
  238. static long current_second = 0; /* from previous calls to gettimeofday */
  239. static long time_to_rebuild_directory = 0;
  240. static long time_to_fetch_directory = 0;
  241. // int ms_until_conn;
  242. cell_t cell;
  243. if(gettimeofday(&now,NULL) < 0)
  244. return -1;
  245. if(now.tv_sec > current_second) { /* the second has rolled over. check more stuff. */
  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. } else {
  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. }
  268. /* do housekeeping for each connection */
  269. for(i=0;i<nfds;i++) {
  270. tmpconn = connection_array[i];
  271. connection_increment_receiver_bucket(tmpconn);
  272. connection_array[i]->onions_handled_this_second = 0;
  273. /* check connections to see whether we should send a keepalive, expire, or wait */
  274. if(!connection_speaks_cells(tmpconn))
  275. continue; /* this conn type doesn't send cells */
  276. if(now.tv_sec >= tmpconn->timestamp_lastwritten + options.KeepalivePeriod) {
  277. if((!(options.Role & ROLE_OR_CONNECT_ALL) && !circuit_get_by_conn(tmpconn)) ||
  278. (!connection_state_is_open(tmpconn))) {
  279. /* we're an onion proxy, with no circuits; or our handshake has expired. kill it. */
  280. log(LOG_DEBUG,"prepare_for_poll(): Expiring connection to %d (%s:%d).",
  281. i,tmpconn->address, tmpconn->port);
  282. tmpconn->marked_for_close = 1;
  283. } else {
  284. /* either a full router, or we've got a circuit. send a padding cell. */
  285. // log(LOG_DEBUG,"prepare_for_poll(): Sending keepalive to (%s:%d)",
  286. // tmpconn->address, tmpconn->port);
  287. // memset(&cell,0,sizeof(cell_t));
  288. cell.command = CELL_PADDING;
  289. if(connection_write_cell_to_buf(&cell, tmpconn) < 0)
  290. tmpconn->marked_for_close = 1;
  291. }
  292. }
  293. }
  294. /* blow away any connections that need to die. can't do this later
  295. * because we might open up a circuit and not realize it we're about to cull it.
  296. */
  297. for(i=0;i<nfds;i++)
  298. check_conn_marked(i);
  299. current_second = now.tv_sec; /* remember which second it is, for next time */
  300. }
  301. if(onion_pending_check()) {
  302. /* there's an onion pending. check for new things to do, but don't wait any time */
  303. *timeout = 0;
  304. } else {
  305. *timeout = 1000 - (now.tv_usec / 1000); /* how many milliseconds til the next second? */
  306. }
  307. return 0;
  308. }
  309. /* Link padding stuff left here for fun. Not used now. */
  310. #if 0
  311. if(options.LinkPadding) {
  312. /* now check which conn wants to speak soonest */
  313. for(i=0;i<nfds;i++) {
  314. tmpconn = connection_array[i];
  315. if(!connection_speaks_cells(tmpconn))
  316. continue; /* this conn type doesn't send cells */
  317. if(!connection_state_is_open(tmpconn))
  318. continue; /* only conns in state 'open' have a valid send_timeval */
  319. while(tv_cmp(&tmpconn->send_timeval,&now) <= 0) { /* send_timeval has already passed, let it send a cell */
  320. connection_send_cell(tmpconn);
  321. }
  322. if(!conn || tv_cmp(&tmpconn->send_timeval, &soonest) < 0) { /* this is the best choice so far */
  323. conn = tmpconn;
  324. soonest.tv_sec = conn->send_timeval.tv_sec;
  325. soonest.tv_usec = conn->send_timeval.tv_usec;
  326. }
  327. }
  328. if(conn) { /* we might want to set *timeout sooner */
  329. ms_until_conn = (soonest.tv_sec - now.tv_sec)*1000 +
  330. (soonest.tv_usec - now.tv_usec)/1000;
  331. // log(LOG_DEBUG,"prepare_for_poll(): conn %d times out in %d ms.",conn->s, ms_until_conn);
  332. if(ms_until_conn < *timeout) { /* use the new one */
  333. // log(LOG_DEBUG,"prepare_for_poll(): conn %d soonest, in %d ms.",conn->s,ms_until_conn);
  334. *timeout = ms_until_conn;
  335. }
  336. }
  337. }
  338. #endif
  339. int do_main_loop(void) {
  340. int i;
  341. int timeout;
  342. int poll_result;
  343. crypto_pk_env_t *prkey;
  344. /* load the routers file */
  345. if(router_get_list_from_file(options.RouterFile) < 0) {
  346. log(LOG_ERR,"Error loading router list.");
  347. return -1;
  348. }
  349. /* load the private key, if we're supposed to have one */
  350. if(ROLE_IS_OR(global_role)) {
  351. prkey = crypto_new_pk_env(CRYPTO_PK_RSA);
  352. if (!prkey) {
  353. log(LOG_ERR,"Error creating a crypto environment.");
  354. return -1;
  355. }
  356. if (crypto_pk_read_private_key_from_filename(prkey, options.PrivateKeyFile))
  357. {
  358. log(LOG_ERR,"Error loading private key.");
  359. return -1;
  360. }
  361. setprivatekey(prkey);
  362. }
  363. /* start-up the necessary connections based on global_role. This is where we
  364. * try to connect to all the other ORs, and start the listeners */
  365. retry_all_connections(options.Role, options.ORPort,
  366. options.OPPort, options.APPort, options.DirPort);
  367. for(;;) {
  368. if(please_dumpstats) {
  369. dumpstats();
  370. please_dumpstats = 0;
  371. }
  372. if(please_fetch_directory) {
  373. if(options.Role & ROLE_DIR_SERVER) {
  374. if(router_get_list_from_file(options.RouterFile) < 0) {
  375. log(LOG_ERR,"Error reloading router list. Continuing with old list.");
  376. }
  377. } else {
  378. directory_initiate_fetch(router_pick_directory_server());
  379. }
  380. please_fetch_directory = 0;
  381. }
  382. if(prepare_for_poll(&timeout) < 0) {
  383. log(LOG_DEBUG,"do_main_loop(): prepare_for_poll failed, exiting.");
  384. return -1;
  385. }
  386. /* now timeout is the value we'll hand to poll. It's either -1, meaning
  387. * don't timeout, else it indicates the soonest event (either the
  388. * one-second rollover for refilling receiver buckets, or the soonest
  389. * conn that needs to send a cell)
  390. */
  391. /* poll until we have an event, or it's time to do something */
  392. poll_result = poll(poll_array, nfds, timeout);
  393. #if 0 /* let catch() handle things like ^c, and otherwise don't worry about it */
  394. if(poll_result < 0) {
  395. log(LOG_ERR,"do_main_loop(): poll failed.");
  396. if(errno != EINTR) /* let the program survive things like ^z */
  397. return -1;
  398. }
  399. #endif
  400. if(poll_result == 0) {
  401. /* poll timed out without anything to do. process a pending onion, if any. */
  402. onion_pending_process_one();
  403. }
  404. if(poll_result > 0) { /* we have at least one connection to deal with */
  405. /* do all the reads first, so we can detect closed sockets */
  406. for(i=0;i<nfds;i++)
  407. check_conn_read(i); /* this also blows away broken connections */
  408. /* then do the writes */
  409. for(i=0;i<nfds;i++)
  410. check_conn_write(i);
  411. /* any of the conns need to be closed now? */
  412. for(i=0;i<nfds;i++)
  413. check_conn_marked(i);
  414. }
  415. /* refilling buckets and sending cells happens at the beginning of the
  416. * next iteration of the loop, inside prepare_for_poll()
  417. */
  418. }
  419. }
  420. static void catch(int the_signal) {
  421. switch(the_signal) {
  422. // case SIGABRT:
  423. case SIGTERM:
  424. case SIGINT:
  425. log(LOG_NOTICE,"Catching signal %d, exiting cleanly.", the_signal);
  426. exit(0);
  427. case SIGHUP:
  428. please_fetch_directory = 1;
  429. break;
  430. case SIGUSR1:
  431. please_dumpstats = 1;
  432. break;
  433. default:
  434. log(LOG_ERR,"Caught signal that we can't handle??");
  435. }
  436. }
  437. void dumpstats (void) { /* dump stats to stdout */
  438. int i;
  439. connection_t *conn;
  440. struct timeval now;
  441. extern char *conn_type_to_string[];
  442. extern char *conn_state_to_string[][15];
  443. printf("Dumping stats:\n");
  444. if(gettimeofday(&now,NULL) < 0)
  445. return ;
  446. for(i=0;i<nfds;i++) {
  447. conn = connection_array[i];
  448. printf("Conn %d (socket %d) type %d (%s), state %d (%s), created %ld secs ago\n",
  449. i, conn->s, conn->type, conn_type_to_string[conn->type],
  450. conn->state, conn_state_to_string[conn->type][conn->state], now.tv_sec - conn->timestamp_created);
  451. if(!connection_is_listener(conn)) {
  452. printf("Conn %d is to '%s:%d'.\n",i,conn->address, conn->port);
  453. printf("Conn %d: %d bytes waiting on inbuf (last read %ld secs ago)\n",i,conn->inbuf_datalen,
  454. now.tv_sec - conn->timestamp_lastread);
  455. printf("Conn %d: %d bytes waiting on outbuf (last written %ld secs ago)\n",i,conn->outbuf_datalen,
  456. now.tv_sec - conn->timestamp_lastwritten);
  457. }
  458. circuit_dump_by_conn(conn); /* dump info about all the circuits using this conn */
  459. printf("\n");
  460. }
  461. }
  462. int dump_router_to_string(char *s, int maxlen, routerinfo_t *router) {
  463. char *pkey;
  464. int pkeylen;
  465. int written;
  466. if(crypto_pk_write_public_key_to_string(router->pkey,&pkey,&pkeylen)<0) {
  467. log(LOG_ERR,"dump_directory_to_string(): write pkey to string failed!");
  468. return 0;
  469. }
  470. written = snprintf(s, maxlen, "%s %d %d %d %d %d\n%s\n",
  471. router->address,
  472. router->or_port,
  473. router->op_port,
  474. router->ap_port,
  475. router->dir_port,
  476. router->bandwidth,
  477. pkey);
  478. free(pkey);
  479. return written;
  480. }
  481. void dump_directory_to_string(char *s, int maxlen) {
  482. int i;
  483. connection_t *conn;
  484. routerinfo_t *router;
  485. int written;
  486. /* first write my own info */
  487. if(my_routerinfo) {
  488. written = dump_router_to_string(s, maxlen, my_routerinfo);
  489. maxlen -= written;
  490. s += written;
  491. }
  492. /* now write info for other routers */
  493. for(i=0;i<nfds;i++) {
  494. conn = connection_array[i];
  495. if(conn->type != CONN_TYPE_OR)
  496. continue; /* we only want to list ORs */
  497. if(conn->state != OR_CONN_STATE_OPEN)
  498. continue; /* we only want to list ones that successfully handshaked */
  499. router = router_get_by_addr_port(conn->addr,conn->port);
  500. if(!router) {
  501. log(LOG_ERR,"dump_directory_to_string(): couldn't find router %d:%d!",conn->addr,conn->port);
  502. continue;
  503. }
  504. written = dump_router_to_string(s, maxlen, router);
  505. if(written < 0 || written > maxlen) {
  506. /* apparently different glibcs do different things on error.. so check both */
  507. log(LOG_ERR,"dump_directory_to_string(): tried to exceed string length.");
  508. s[maxlen-1] = 0; /* make sure it's null terminated */
  509. return;
  510. }
  511. maxlen -= written;
  512. s += written;
  513. }
  514. }
  515. int main(int argc, char *argv[]) {
  516. int retval = 0;
  517. if(getconfig(argc,argv,&options))
  518. exit(1);
  519. log(options.loglevel,NULL); /* assign logging severity level from options */
  520. global_role = options.Role; /* assign global_role from options. FIXME: remove from global namespace later. */
  521. if(options.Role & ROLE_OR_LISTEN) { /* only spawn dns handlers if we're a router */
  522. if(dns_master_start() < 0) {
  523. log(LOG_ERR,"main(): We're running without a dns handler. Bad news.");
  524. }
  525. }
  526. init_tracked_tree(); /* initialize the replay detection tree */
  527. init_cache_tree(); /* initialize the dns resolve tree */
  528. signal (SIGINT, catch); /* catch kills so we can exit cleanly */
  529. signal (SIGTERM, catch);
  530. signal (SIGUSR1, catch); /* to dump stats to stdout */
  531. signal (SIGHUP, catch); /* to reload directory */
  532. crypto_global_init();
  533. retval = do_main_loop();
  534. crypto_global_cleanup();
  535. return retval;
  536. }