connection.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. #include "or.h"
  2. /********* START VARIABLES **********/
  3. char *conn_type_to_string[] = {
  4. "OP listener", /* 0 */
  5. "OP", /* 1 */
  6. "OR listener", /* 2 */
  7. "OR", /* 3 */
  8. "App" /* 4 */
  9. };
  10. char *conn_state_to_string[][10] = {
  11. { "ready" }, /* op listener, 0 */
  12. { "awaiting keys", /* op, 0 */
  13. "open", /* 1 */
  14. "close", /* 2 */
  15. "close_wait" }, /* 3 */
  16. { "ready" }, /* or listener, 0 */
  17. { "connecting (as client)", /* or, 0 */
  18. "sending auth (as client)", /* 1 */
  19. "waiting for auth (as client)", /* 2 */
  20. "sending nonce (as client)", /* 3 */
  21. "waiting for auth (as server)", /* 4 */
  22. "sending auth (as server)", /* 5 */
  23. "waiting for nonce (as server)",/* 6 */
  24. "open" }, /* 7 */
  25. { "connecting", /* app, 0 */
  26. "open", /* 1 */
  27. "waiting for dest info", /* 2 */
  28. "flushing buffer, then will close",/* 3 */
  29. "close_wait" } /* 4 */
  30. };
  31. /********* END VARIABLES ************/
  32. connection_t *connection_new(int type) {
  33. connection_t *conn;
  34. conn = (connection_t *)malloc(sizeof(connection_t));
  35. if(!conn)
  36. return NULL;
  37. memset(conn,0,sizeof(connection_t)); /* zero it out to start */
  38. conn->type = type;
  39. buf_new(&conn->inbuf, &conn->inbuflen, &conn->inbuf_datalen);
  40. buf_new(&conn->outbuf, &conn->outbuflen, &conn->outbuf_datalen);
  41. return conn;
  42. }
  43. void connection_free(connection_t *conn) {
  44. assert(conn);
  45. buf_free(conn->inbuf);
  46. buf_free(conn->outbuf);
  47. if(conn->address)
  48. free(conn->address);
  49. /* FIXME should we do these for all connections, or just ORs, or what */
  50. if(conn->type == CONN_TYPE_OR ||
  51. conn->type == CONN_TYPE_OP) {
  52. // EVP_CIPHER_CTX_cleanup(&conn->f_ctx);
  53. // EVP_CIPHER_CTX_cleanup(&conn->b_ctx);
  54. }
  55. if(conn->s > 0)
  56. close(conn->s);
  57. free(conn);
  58. }
  59. int connection_create_listener(RSA *prkey, struct sockaddr_in *local, int type) {
  60. connection_t *conn;
  61. int s;
  62. int one=1;
  63. s = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
  64. if (s < 0)
  65. {
  66. log(LOG_ERR,"connection_create_listener(): Socket creation failed.");
  67. return -1;
  68. }
  69. setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
  70. if(bind(s,(struct sockaddr *)local,sizeof(*local)) < 0) {
  71. perror("bind ");
  72. log(LOG_ERR,"Could not bind to local port %u.",ntohs(local->sin_port));
  73. return -1;
  74. }
  75. /* start local server */
  76. if(listen(s,SOMAXCONN) < 0) {
  77. log(LOG_ERR,"Could not listen on local port %u.",ntohs(local->sin_port));
  78. return -1;
  79. }
  80. fcntl(s, F_SETFL, O_NONBLOCK); /* set s to non-blocking */
  81. conn = connection_new(type);
  82. conn->s = s;
  83. if(connection_add(conn) < 0) { /* no space, forget it */
  84. connection_free(conn);
  85. return -1;
  86. }
  87. /* remember things so you can tell the baby sockets */
  88. memcpy(&conn->local,local,sizeof(struct sockaddr_in));
  89. conn->prkey = prkey;
  90. log(LOG_DEBUG,"connection_create_listener(): Listening on local port %u.",ntohs(local->sin_port));
  91. conn->state = LISTENER_STATE_READY;
  92. connection_watch_events(conn, POLLIN);
  93. return 0;
  94. }
  95. int connection_handle_listener_read(connection_t *conn, int new_type, int new_state) {
  96. int news; /* the new socket */
  97. connection_t *newconn;
  98. struct sockaddr_in remote; /* information about the remote peer when connecting to other routers */
  99. int remotelen = sizeof(struct sockaddr_in); /* length of the remote address */
  100. news = accept(conn->s,(struct sockaddr *)&remote,&remotelen);
  101. if (news == -1) { /* accept() error */
  102. if(errno==EAGAIN)
  103. return 0; /* he hung up before we could accept(). that's fine. */
  104. /* else there was a real error. */
  105. log(LOG_ERR,"connection_handle_listener_read(): accept() failed. Closing.");
  106. return -1;
  107. }
  108. log(LOG_DEBUG,"Connection accepted on socket %d.",news);
  109. newconn = connection_new(new_type);
  110. newconn->s = news;
  111. /* learn things from parent, so we can perform auth */
  112. memcpy(&newconn->local,&conn->local,sizeof(struct sockaddr_in));
  113. newconn->prkey = conn->prkey;
  114. // newconn->address = strdup(get_string_from_remote()) FIXME ;
  115. if(connection_add(newconn) < 0) { /* no space, forget it */
  116. connection_free(newconn);
  117. return -1;
  118. }
  119. log(LOG_DEBUG,"connection_handle_listener_read(): socket %d entered state %d.",newconn->s, new_state);
  120. newconn->state = new_state;
  121. connection_watch_events(newconn, POLLIN);
  122. return 0;
  123. }
  124. int retry_all_connections(routerinfo_t **router_array, int rarray_len,
  125. RSA *prkey, uint16_t or_port, uint16_t op_port) {
  126. /* start all connections that should be up but aren't */
  127. routerinfo_t *router;
  128. int i;
  129. /* local host information */
  130. char localhostname[512];
  131. struct hostent *localhost;
  132. struct sockaddr_in local; /* local address */
  133. /* obtain local host information */
  134. if(gethostname(localhostname,512) < 0) {
  135. log(LOG_ERR,"Error obtaining local hostname.");
  136. return -1;
  137. }
  138. localhost = gethostbyname(localhostname);
  139. if (!localhost) {
  140. log(LOG_ERR,"Error obtaining local host info.");
  141. return -1;
  142. }
  143. memset((void *)&local,0,sizeof(local));
  144. local.sin_family = AF_INET;
  145. local.sin_addr.s_addr = INADDR_ANY;
  146. local.sin_port = htons(or_port);
  147. memcpy((void *)&local.sin_addr,(void *)localhost->h_addr,sizeof(struct in_addr));
  148. for (i=0;i<rarray_len;i++) {
  149. router = router_array[i];
  150. if(!connection_get_by_addr_port(router->addr,router->port)) { /* not in the list */
  151. connect_to_router(router, prkey, &local);
  152. }
  153. }
  154. if(!connection_get_by_type(CONN_TYPE_OR_LISTENER)) {
  155. connection_or_create_listener(prkey, &local);
  156. }
  157. local.sin_port = htons(op_port);
  158. if(!connection_get_by_type(CONN_TYPE_OP_LISTENER)) {
  159. connection_op_create_listener(prkey, &local);
  160. }
  161. return 0;
  162. }
  163. int connection_read_to_buf(connection_t *conn) {
  164. return read_to_buf(conn->s, &conn->inbuf, &conn->inbuflen, &conn->inbuf_datalen, &conn->inbuf_reached_eof);
  165. }
  166. int connection_fetch_from_buf(char *string, int len, connection_t *conn) {
  167. return fetch_from_buf(string, len, &conn->inbuf, &conn->inbuflen, &conn->inbuf_datalen);
  168. }
  169. int connection_flush_buf(connection_t *conn) {
  170. return flush_buf(conn->s, &conn->outbuf, &conn->outbuflen, &conn->outbuf_datalen);
  171. }
  172. int connection_write_to_buf(char *string, int len, connection_t *conn) {
  173. if(!len)
  174. return 0;
  175. connection_watch_events(conn, POLLOUT | POLLIN);
  176. return write_to_buf(string, len, &conn->outbuf, &conn->outbuflen, &conn->outbuf_datalen);
  177. }
  178. int connection_send_destroy(aci_t aci, connection_t *conn) {
  179. cell_t cell;
  180. if(!conn)
  181. return -1;
  182. if(conn->type == CONN_TYPE_OP ||
  183. conn->type == CONN_TYPE_APP) {
  184. log(LOG_DEBUG,"connection_send_destroy(): At an edge. Marking connection for close.");
  185. conn->marked_for_close = 1;
  186. return 0;
  187. }
  188. cell.aci = aci;
  189. cell.command = CELL_DESTROY;
  190. log(LOG_DEBUG,"connection_send_destroy(): Sending destroy (aci %d).",aci);
  191. return connection_write_cell_to_buf(&cell, conn);
  192. }
  193. int connection_write_cell_to_buf(cell_t *cellp, connection_t *conn) {
  194. /* FIXME in the future, we should modify windows, etc, here */
  195. if(connection_encrypt_cell_header(cellp,conn)<0) {
  196. return -1;
  197. }
  198. return connection_write_to_buf((char *)cellp, sizeof(cell_t), conn);
  199. }
  200. int connection_encrypt_cell_header(cell_t *cellp, connection_t *conn) {
  201. char newheader[8];
  202. int newsize;
  203. int x;
  204. char *px;
  205. printf("Sending: Cell header plaintext: ");
  206. px = (char *)cellp;
  207. for(x=0;x<8;x++) {
  208. printf("%u ",px[x]);
  209. }
  210. printf("\n");
  211. if(!EVP_EncryptUpdate(&conn->f_ctx, newheader, &newsize, (char *)cellp, 8)) {
  212. log(LOG_ERR,"Could not encrypt data for connection %s:%u.",conn->address,ntohs(conn->port));
  213. return -1;
  214. }
  215. printf("Sending: Cell header crypttext: ");
  216. for(x=0;x<8;x++) {
  217. printf("%u ",newheader[x]);
  218. }
  219. printf("\n");
  220. memcpy(cellp,newheader,8);
  221. return 0;
  222. }
  223. int connection_process_inbuf(connection_t *conn) {
  224. assert(conn);
  225. switch(conn->type) {
  226. case CONN_TYPE_OP:
  227. return connection_op_process_inbuf(conn);
  228. case CONN_TYPE_OR:
  229. return connection_or_process_inbuf(conn);
  230. case CONN_TYPE_APP:
  231. return connection_app_process_inbuf(conn);
  232. default:
  233. log(LOG_DEBUG,"connection_process_inbuf() got unexpected conn->type.");
  234. return -1;
  235. }
  236. }
  237. int connection_finished_flushing(connection_t *conn) {
  238. assert(conn);
  239. log(LOG_DEBUG,"connection_finished_flushing() entered. Socket %u.", conn->s);
  240. switch(conn->type) {
  241. case CONN_TYPE_OP:
  242. return connection_op_finished_flushing(conn);
  243. case CONN_TYPE_OR:
  244. return connection_or_finished_flushing(conn);
  245. case CONN_TYPE_APP:
  246. return connection_app_finished_flushing(conn);
  247. default:
  248. log(LOG_DEBUG,"connection_finished_flushing() got unexpected conn->type.");
  249. return -1;
  250. }
  251. }
  252. int connection_process_cell_from_inbuf(connection_t *conn) {
  253. /* check if there's a whole cell there.
  254. * if yes, pull it off, decrypt it, and process it.
  255. */
  256. char crypted[128];
  257. char outbuf[1024];
  258. int outlen;
  259. int x;
  260. cell_t *cellp;
  261. if(conn->inbuf_datalen < 128) /* entire response available? */
  262. return 0; /* not yet */
  263. if(connection_fetch_from_buf(crypted,128,conn) < 0) {
  264. return -1;
  265. }
  266. printf("Cell header crypttext: ");
  267. for(x=0;x<8;x++) {
  268. printf("%u ",crypted[x]);
  269. }
  270. printf("\n");
  271. /* decrypt */
  272. if(!EVP_DecryptUpdate(&conn->b_ctx,(unsigned char *)outbuf,&outlen,crypted,8)) {
  273. log(LOG_ERR,"connection_process_cell_from_inbuf(): Decryption failed, dropping.");
  274. return connection_process_inbuf(conn); /* process the remainder of the buffer */
  275. }
  276. log(LOG_DEBUG,"connection_process_cell_from_inbuf(): Cell decrypted (%d bytes).",outlen);
  277. printf("Cell header plaintext: ");
  278. for(x=0;x<8;x++) {
  279. printf("%u ",outbuf[x]);
  280. }
  281. printf("\n");
  282. /* copy the rest of the cell */
  283. memcpy((char *)outbuf+8, (char *)crypted+8, sizeof(cell_t)-8);
  284. cellp = (cell_t *)outbuf;
  285. log(LOG_DEBUG,"connection_process_cell_from_inbuf(): Decrypted cell is of type %u (ACI %u).",cellp->command,cellp->aci);
  286. command_process_cell(cellp, conn);
  287. return connection_process_inbuf(conn); /* process the remainder of the buffer */
  288. }