main.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. #include "or.h"
  2. /********* START VARIABLES **********/
  3. /* valid command-line options */
  4. static char *args = "hf:e:n:l:";
  5. int loglevel = LOG_DEBUG;
  6. /* valid config file options */
  7. config_opt_t options[] =
  8. {
  9. {"RouterFile", CONFIG_TYPE_STRING, {0}, 0},
  10. {"PrivateKeyFile", CONFIG_TYPE_STRING, {0}, 0},
  11. {"EntryPort", CONFIG_TYPE_INT, {0}, 0},
  12. {"NetworkPort", CONFIG_TYPE_INT, {0}, 0},
  13. {"MaxConn", CONFIG_TYPE_INT, {0}, 0},
  14. {"MaxConnTimeout", CONFIG_TYPE_INT, {0}, 0},
  15. {"TrafficShaping", CONFIG_TYPE_INT, {0}, 0},
  16. {0}
  17. };
  18. enum opts {
  19. RouterFile=0, PrivateKeyFile, EntryPort, NetworkPort, MaxConn, MaxConnTimeout, TrafficShaping
  20. };
  21. connection_t *connection_array[MAXCONNECTIONS] =
  22. { NULL };
  23. struct pollfd poll_array[MAXCONNECTIONS] =
  24. { [0 ... MAXCONNECTIONS-1] = { -1, 0, 0 } };
  25. int nfds=0; /* number of connections currently active */
  26. /* default logging threshold */
  27. extern int loglevel;
  28. /* private key */
  29. RSA *prkey = NULL;
  30. /* router array */
  31. routerinfo_t **router_array = NULL;
  32. int rarray_len = 0;
  33. /********* END VARIABLES ************/
  34. int connection_add(connection_t *conn) {
  35. if(nfds >= MAXCONNECTIONS-2) { /* 2, for some breathing room. should count the fenceposts. */
  36. /* FIXME should use the 'max connections' option */
  37. log(LOG_DEBUG,"connection_add(): failing because nfds is too high.");
  38. return -1;
  39. }
  40. conn->poll_index = nfds;
  41. connection_set_poll_socket(conn);
  42. connection_array[nfds] = conn;
  43. /* zero these out here, because otherwise we'll inherit values from the previously freed one */
  44. poll_array[nfds].events = 0;
  45. poll_array[nfds].revents = 0;
  46. nfds++;
  47. log(LOG_DEBUG,"connection_add(): new conn type %d, socket %d, nfds %d.",conn->type, conn->s, nfds);
  48. return 0;
  49. }
  50. void connection_set_poll_socket(connection_t *conn) {
  51. poll_array[conn->poll_index].fd = conn->s;
  52. }
  53. int connection_remove(connection_t *conn) {
  54. int current_index;
  55. assert(conn);
  56. assert(nfds>0);
  57. circuit_about_to_close_connection(conn); /* flush and send destroys for all circuits on this conn */
  58. current_index = conn->poll_index;
  59. if(current_index == nfds-1) { /* this is the end */
  60. // connection_free(conn);
  61. nfds--;
  62. log(LOG_DEBUG,"connection_remove(): nfds now %d.",nfds);
  63. return 0;
  64. }
  65. /* we replace this one with the one at the end, then free it */
  66. nfds--;
  67. poll_array[current_index].fd = poll_array[nfds].fd;
  68. poll_array[current_index].events = poll_array[nfds].events;
  69. poll_array[current_index].revents = poll_array[nfds].revents;
  70. connection_array[current_index] = connection_array[nfds];
  71. connection_array[current_index]->poll_index = current_index;
  72. log(LOG_DEBUG,"connection_remove(): nfds now %d.",nfds);
  73. return 0;
  74. }
  75. connection_t *connection_get_by_addr_port(uint32_t addr, uint16_t port) {
  76. int i;
  77. connection_t *conn;
  78. for(i=0;i<nfds;i++) {
  79. conn = connection_array[i];
  80. assert(conn);
  81. if(conn->addr == addr && conn->port == port)
  82. return conn;
  83. }
  84. return NULL;
  85. }
  86. connection_t *connection_get_by_type(int type) {
  87. int i;
  88. connection_t *conn;
  89. for(i=0;i<nfds;i++) {
  90. conn = connection_array[i];
  91. if(conn->type == type)
  92. return conn;
  93. }
  94. return NULL;
  95. }
  96. routerinfo_t *router_get_by_addr_port(uint32_t addr, uint16_t port) {
  97. int i;
  98. routerinfo_t *router;
  99. if (!router_array)
  100. return NULL;
  101. for(i=0;i<rarray_len;i++)
  102. {
  103. router = router_array[i];
  104. if ((router->addr == addr) && (router->port == port))
  105. return router;
  106. }
  107. return NULL;
  108. }
  109. void connection_watch_events(connection_t *conn, short events) {
  110. assert(conn && conn->poll_index < nfds);
  111. poll_array[conn->poll_index].events = events;
  112. }
  113. void check_conn_read(int i) {
  114. int retval;
  115. connection_t *conn;
  116. if(poll_array[i].revents & POLLIN) { /* something to read */
  117. conn = connection_array[i];
  118. assert(conn);
  119. log(LOG_DEBUG,"check_conn_read(): socket %d has something to read.",conn->s);
  120. if (conn->type == CONN_TYPE_OP_LISTENER) {
  121. retval = connection_op_handle_listener_read(conn);
  122. } else if (conn->type == CONN_TYPE_OR_LISTENER) {
  123. retval = connection_or_handle_listener_read(conn);
  124. } else {
  125. /* else it's an OP, OR, or app */
  126. retval = connection_read_to_buf(conn);
  127. if (retval >= 0) { /* all still well */
  128. retval = connection_process_inbuf(conn);
  129. log(LOG_DEBUG,"check_conn_read(): connection_process_inbuf returned %d.",retval);
  130. }
  131. }
  132. if(retval < 0) { /* this connection is broken. remove it */
  133. log(LOG_DEBUG,"check_conn_read(): Connection broken, removing.");
  134. connection_remove(conn);
  135. connection_free(conn);
  136. if(i<nfds) { /* we just replaced the one at i with a new one.
  137. process it too. */
  138. check_conn_read(i);
  139. }
  140. }
  141. }
  142. }
  143. void check_conn_write(int i) {
  144. int retval;
  145. connection_t *conn;
  146. if(poll_array[i].revents & POLLOUT) { /* something to write */
  147. conn = connection_array[i];
  148. log(LOG_DEBUG,"check_conn_write(): socket %d wants to write.",conn->s);
  149. if(conn->type == CONN_TYPE_OP_LISTENER ||
  150. conn->type == CONN_TYPE_OR_LISTENER) {
  151. log(LOG_DEBUG,"check_conn_write(): Got a listener socket. Can't happen!");
  152. retval = -1;
  153. } else {
  154. /* else it's an OP, OR, or app */
  155. retval = connection_flush_buf(conn); /* conns in CONNECTING state will fall through... */
  156. if(retval == 0) { /* it's done flushing */
  157. retval = connection_finished_flushing(conn); /* ...and get handled here. */
  158. }
  159. }
  160. if(retval < 0) { /* this connection is broken. remove it. */
  161. log(LOG_DEBUG,"check_conn_write(): Connection broken, removing.");
  162. connection_remove(conn);
  163. connection_free(conn);
  164. if(i<nfds) { /* we just replaced the one at i with a new one.
  165. process it too. */
  166. check_conn_read(i);
  167. }
  168. }
  169. }
  170. }
  171. void check_conn_marked(int i) {
  172. connection_t *conn;
  173. conn = connection_array[i];
  174. assert(conn);
  175. if(conn->marked_for_close) {
  176. log(LOG_DEBUG,"check_conn_marked(): Cleaning up connection.");
  177. connection_flush_buf(conn); /* flush it first */
  178. connection_remove(conn);
  179. connection_free(conn);
  180. if(i<nfds) { /* we just replaced the one at i with a new one.
  181. process it too. */
  182. check_conn_read(i);
  183. }
  184. }
  185. }
  186. #if 0
  187. void check_conn_hup(int i) {
  188. connection_t *conn;
  189. if(poll_array[i].revents & POLLHUP) { /* they've hung up */
  190. conn = connection_array[i];
  191. log(LOG_DEBUG,"check_conn_hup(): socket %d has hung up.",conn->s);
  192. connection_remove(conn);
  193. connection_free(conn);
  194. if(i<nfds) { /* we just replaced the one at i with a new one.
  195. process it too. */
  196. check_conn_hup(i);
  197. }
  198. }
  199. }
  200. #endif
  201. int do_main_loop(void) {
  202. int i;
  203. /* load the routers file */
  204. router_array = getrouters(options[RouterFile].r.str,&rarray_len);
  205. if (!router_array)
  206. {
  207. log(LOG_ERR,"Error loading router list.");
  208. exit(1);
  209. }
  210. /* load the private key */
  211. ERR_load_crypto_strings();
  212. prkey = load_prkey(options[PrivateKeyFile].r.str);
  213. if (!prkey)
  214. {
  215. log(LOG_ERR,"Error loading private key.");
  216. exit(1);
  217. }
  218. log(LOG_DEBUG,"core : Loaded private key of size %u bytes.",RSA_size(prkey));
  219. ERR_free_strings();
  220. /* try to connect to all the other ORs, and start the listeners */
  221. retry_all_connections(router_array, rarray_len, prkey,
  222. options[NetworkPort].r.i,options[EntryPort].r.i);
  223. for(;;) {
  224. poll(poll_array, nfds, -1); /* poll until we have an event */
  225. /* do all the reads first, so we can detect closed sockets */
  226. for(i=0;i<nfds;i++)
  227. check_conn_read(i);
  228. /* then do the writes */
  229. for(i=0;i<nfds;i++)
  230. check_conn_write(i);
  231. /* any of the conns need to be closed now? */
  232. for(i=0;i<nfds;i++)
  233. check_conn_marked(i);
  234. #if 0 /* no, check_conn_read() takes care of hups. */
  235. /* remove the ones that have disconnected */
  236. for(i=0;i<nfds;i++)
  237. check_conn_hup(i);
  238. #endif
  239. }
  240. }
  241. void catch ()
  242. {
  243. errno = 0; /* netcat does this. it looks fun. */
  244. log(LOG_DEBUG,"Catching ^c, exiting cleanly.");
  245. exit(0);
  246. }
  247. int main(int argc, char *argv[])
  248. {
  249. int retval = 0;
  250. char *conf_filename = NULL; /* configuration file */
  251. signal (SIGINT, catch); /* to catch ^c so we can exit cleanly */
  252. /* get command-line arguments */
  253. retval = getargs(argc,argv,args,&conf_filename,&loglevel);
  254. if (retval == -1)
  255. {
  256. log(LOG_ERR,"Error processing command-line arguments.");
  257. exit(1);
  258. }
  259. /* load config file */
  260. retval = getconfig(conf_filename,options);
  261. if (retval == -1)
  262. {
  263. log(LOG_ERR,"Error loading configuration file.");
  264. exit(1);
  265. }
  266. else if (options[RouterFile].err != 1)
  267. {
  268. log(LOG_ERR,"RouterFile option required, but not found.");
  269. exit(1);
  270. }
  271. else if (options[PrivateKeyFile].err != 1)
  272. {
  273. log(LOG_ERR,"PrivateKeyFile option required but not found.");
  274. exit(1);
  275. }
  276. else if (options[EntryPort].err != 1)
  277. {
  278. log(LOG_ERR,"EntryPort option required but not found.");
  279. exit(1);
  280. }
  281. else if (options[NetworkPort].err != 1)
  282. {
  283. log(LOG_ERR,"NetworkPort option required but not found.");
  284. exit(1);
  285. }
  286. else if (options[MaxConn].err != 1)
  287. {
  288. log(LOG_ERR,"MaxConn option required but not found.");
  289. exit(1);
  290. }
  291. #if 0
  292. else if (options[MaxConnTimeout].err != 1)
  293. {
  294. conn_tout.tv_sec = OR_DEFAULT_CONN_TIMEOUT;
  295. }
  296. else
  297. {
  298. if (!options[MaxConnTimeout].r.i)
  299. conn_toutp = NULL;
  300. else
  301. conn_tout.tv_sec = options[MaxConnTimeout].r.i;
  302. }
  303. conn_tout.tv_usec = 0;
  304. if (!options[TrafficShaping].err)
  305. {
  306. options[TrafficShaping].r.i = DEFAULT_POLICY;
  307. }
  308. else if ((options[TrafficShaping].r.i < 0) || (options[TrafficShaping].r.i > 1))
  309. {
  310. log(LOG_ERR,"Invalid value for the TrafficShaping option.");
  311. exit(1);
  312. }
  313. #endif
  314. ERR_load_crypto_strings();
  315. retval = do_main_loop();
  316. ERR_free_strings();
  317. return retval;
  318. }