main.c 10 KB

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