connection_or.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /* Copyright 2001,2002 Roger Dingledine, Matej Pfajfar. */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. #include "or.h"
  5. extern or_options_t options; /* command-line and config-file options */
  6. static int connection_tls_finish_handshake(connection_t *conn);
  7. /**************************************************************/
  8. static void cell_pack(char *dest, const cell_t *src) {
  9. *(uint16_t*)dest = htons(src->aci);
  10. *(uint8_t*)(dest+2) = src->command;
  11. *(uint8_t*)(dest+3) = src->length;
  12. *(uint32_t*)(dest+4) = 0; /* Reserved */
  13. memcpy(dest+8, src->payload, CELL_PAYLOAD_SIZE);
  14. }
  15. static void cell_unpack(cell_t *dest, const char *src) {
  16. dest->aci = ntohs(*(uint16_t*)(src));
  17. dest->command = *(uint8_t*)(src+2);
  18. dest->length = *(uint8_t*)(src+3);
  19. dest->seq = ntohl(*(uint32_t*)(src+4));
  20. memcpy(dest->payload, src+8, CELL_PAYLOAD_SIZE);
  21. }
  22. /**************************************************************/
  23. int connection_or_process_inbuf(connection_t *conn) {
  24. assert(conn && conn->type == CONN_TYPE_OR);
  25. if(conn->inbuf_reached_eof) {
  26. log_fn(LOG_INFO,"conn reached eof. Closing.");
  27. return -1;
  28. }
  29. if(conn->state != OR_CONN_STATE_OPEN)
  30. return 0; /* don't do anything */
  31. return connection_process_cell_from_inbuf(conn);
  32. }
  33. int connection_or_finished_flushing(connection_t *conn) {
  34. int e, len=sizeof(e);
  35. assert(conn && conn->type == CONN_TYPE_OR);
  36. switch(conn->state) {
  37. case OR_CONN_STATE_CONNECTING:
  38. if (getsockopt(conn->s, SOL_SOCKET, SO_ERROR, (void*)&e, &len) < 0) { /* not yet */
  39. if(!ERRNO_CONN_EINPROGRESS(errno)){
  40. log_fn(LOG_DEBUG,"in-progress connect failed. Removing.");
  41. return -1;
  42. } else {
  43. return 0; /* no change, see if next time is better */
  44. }
  45. }
  46. /* the connect has finished. */
  47. log_fn(LOG_INFO,"OR connect() to router %s:%u finished.",
  48. conn->address,conn->port);
  49. if(connection_tls_start_handshake(conn, 0) < 0)
  50. return -1;
  51. return 0;
  52. case OR_CONN_STATE_OPEN:
  53. connection_stop_writing(conn);
  54. return 0;
  55. default:
  56. log_fn(LOG_WARNING,"BUG: called in unexpected state.");
  57. return 0;
  58. }
  59. }
  60. /*********************/
  61. void connection_or_init_conn_from_router(connection_t *conn, routerinfo_t *router) {
  62. conn->addr = router->addr;
  63. conn->port = router->or_port;
  64. conn->receiver_bucket = conn->bandwidth = router->bandwidth;
  65. conn->onion_pkey = crypto_pk_dup_key(router->onion_pkey);
  66. conn->link_pkey = crypto_pk_dup_key(router->link_pkey);
  67. conn->identity_pkey = crypto_pk_dup_key(router->identity_pkey);
  68. conn->nickname = strdup(router->nickname);
  69. if(conn->address)
  70. free(conn->address);
  71. conn->address = strdup(router->address);
  72. }
  73. connection_t *connection_or_connect(routerinfo_t *router) {
  74. connection_t *conn;
  75. assert(router);
  76. if(router_is_me(router->addr, router->or_port)) {
  77. /* this is me! don't connect to me. XXX use nickname/key */
  78. log(LOG_DEBUG,"connection_or_connect(): This is me. Skipping.");
  79. return NULL;
  80. }
  81. /* this function should never be called if we're already connected to router, but */
  82. /* check first to be sure */
  83. conn = connection_exact_get_by_addr_port(router->addr,router->or_port);
  84. if(conn)
  85. return conn;
  86. conn = connection_new(CONN_TYPE_OR);
  87. /* set up conn so it's got all the data we need to remember */
  88. connection_or_init_conn_from_router(conn, router);
  89. if(connection_add(conn) < 0) { /* no space, forget it */
  90. connection_free(conn);
  91. return NULL;
  92. }
  93. switch(connection_connect(conn, router->address, router->addr, router->or_port)) {
  94. case -1:
  95. connection_remove(conn);
  96. connection_free(conn);
  97. return NULL;
  98. case 0:
  99. connection_set_poll_socket(conn);
  100. connection_watch_events(conn, POLLIN | POLLOUT | POLLERR);
  101. /* writable indicates finish, readable indicates broken link,
  102. error indicates broken link on windows */
  103. conn->state = OR_CONN_STATE_CONNECTING;
  104. return conn;
  105. /* case 1: fall through */
  106. }
  107. connection_set_poll_socket(conn);
  108. if(connection_tls_start_handshake(conn, 0) >= 0)
  109. return conn;
  110. /* failure */
  111. connection_remove(conn);
  112. connection_free(conn);
  113. return NULL;
  114. }
  115. /* ********************************** */
  116. int connection_tls_start_handshake(connection_t *conn, int receiving) {
  117. conn->state = OR_CONN_STATE_HANDSHAKING;
  118. conn->tls = tor_tls_new(conn->s, receiving);
  119. if(!conn->tls) {
  120. log_fn(LOG_WARNING,"tor_tls_new failed. Closing.");
  121. return -1;
  122. }
  123. connection_start_reading(conn);
  124. log_fn(LOG_DEBUG,"starting the handshake");
  125. if(connection_tls_continue_handshake(conn) < 0)
  126. return -1;
  127. return 0;
  128. }
  129. int connection_tls_continue_handshake(connection_t *conn) {
  130. switch(tor_tls_handshake(conn->tls)) {
  131. case TOR_TLS_ERROR:
  132. case TOR_TLS_CLOSE:
  133. log_fn(LOG_INFO,"tls error. breaking.");
  134. return -1;
  135. case TOR_TLS_DONE:
  136. return connection_tls_finish_handshake(conn);
  137. case TOR_TLS_WANTWRITE:
  138. connection_start_writing(conn);
  139. log_fn(LOG_DEBUG,"wanted write");
  140. return 0;
  141. case TOR_TLS_WANTREAD: /* handshaking conns are *always* reading */
  142. log_fn(LOG_DEBUG,"wanted read");
  143. return 0;
  144. }
  145. return 0;
  146. }
  147. static int connection_tls_finish_handshake(connection_t *conn) {
  148. crypto_pk_env_t *pk;
  149. routerinfo_t *router;
  150. conn->state = OR_CONN_STATE_OPEN;
  151. directory_set_dirty();
  152. connection_watch_events(conn, POLLIN);
  153. log_fn(LOG_DEBUG,"tls handshake done. verifying.");
  154. if(options.OnionRouter) { /* I'm an OR */
  155. if(tor_tls_peer_has_cert(conn->tls)) { /* it's another OR */
  156. pk = tor_tls_verify(conn->tls);
  157. if(!pk) {
  158. log_fn(LOG_WARNING,"Other side has a cert but it's invalid. Closing.");
  159. return -1;
  160. }
  161. router = router_get_by_link_pk(pk);
  162. if (!router) {
  163. log_fn(LOG_WARNING,"Unrecognized public key from peer. Closing.");
  164. crypto_free_pk_env(pk);
  165. return -1;
  166. }
  167. if(conn->link_pkey) { /* I initiated this connection. */
  168. if(crypto_pk_cmp_keys(conn->link_pkey, pk)) {
  169. log_fn(LOG_WARNING,"We connected to '%s' but he gave us a different key. Closing.",
  170. router->nickname);
  171. crypto_free_pk_env(pk);
  172. return -1;
  173. }
  174. log_fn(LOG_DEBUG,"The router's pk matches the one we meant to connect to. Good.");
  175. } else {
  176. if(connection_exact_get_by_addr_port(router->addr,router->or_port)) {
  177. log_fn(LOG_INFO,"Router %s is already connected. Dropping.", router->nickname);
  178. return -1;
  179. }
  180. connection_or_init_conn_from_router(conn, router);
  181. }
  182. crypto_free_pk_env(pk);
  183. } else { /* it's an OP */
  184. conn->receiver_bucket = conn->bandwidth = DEFAULT_BANDWIDTH_OP;
  185. }
  186. } else { /* I'm a client */
  187. if(!tor_tls_peer_has_cert(conn->tls)) { /* it's a client too?! */
  188. log_fn(LOG_WARNING,"Neither peer sent a cert! Closing.");
  189. return -1;
  190. }
  191. pk = tor_tls_verify(conn->tls);
  192. if(!pk) {
  193. log_fn(LOG_WARNING,"Other side has a cert but it's invalid. Closing.");
  194. return -1;
  195. }
  196. router = router_get_by_link_pk(pk);
  197. if (!router) {
  198. log_fn(LOG_WARNING,"Unrecognized public key from peer. Closing.");
  199. crypto_free_pk_env(pk);
  200. return -1;
  201. }
  202. if(crypto_pk_cmp_keys(conn->link_pkey, pk)) {
  203. log_fn(LOG_WARNING,"We connected to '%s' but he gave us a different key. Closing.",
  204. router->nickname);
  205. crypto_free_pk_env(pk);
  206. return -1;
  207. }
  208. log_fn(LOG_DEBUG,"The router's pk matches the one we meant to connect to. Good.");
  209. crypto_free_pk_env(pk);
  210. conn->receiver_bucket = conn->bandwidth = DEFAULT_BANDWIDTH_OP;
  211. circuit_n_conn_open(conn); /* send the pending create */
  212. }
  213. return 0;
  214. }
  215. /* ********************************** */
  216. int connection_write_cell_to_buf(const cell_t *cellp, connection_t *conn) {
  217. char networkcell[CELL_NETWORK_SIZE];
  218. char *n = networkcell;
  219. cell_pack(n, cellp);
  220. return connection_write_to_buf(n, CELL_NETWORK_SIZE, conn);
  221. }
  222. /* if there's a whole cell there, pull it off and process it. */
  223. int connection_process_cell_from_inbuf(connection_t *conn) {
  224. char buf[CELL_NETWORK_SIZE];
  225. cell_t cell;
  226. log_fn(LOG_DEBUG,"%d: starting, inbuf_datalen %d (%d pending in tls object).",
  227. conn->s,buf_datalen(conn->inbuf),tor_tls_get_pending_bytes(conn->tls));
  228. if(buf_datalen(conn->inbuf) < CELL_NETWORK_SIZE) /* entire response available? */
  229. return 0; /* not yet */
  230. connection_fetch_from_buf(buf, CELL_NETWORK_SIZE, conn);
  231. /* retrieve cell info from buf (create the host-order struct from the network-order string) */
  232. cell_unpack(&cell, buf);
  233. command_process_cell(&cell, conn);
  234. return connection_process_inbuf(conn); /* process the remainder of the buffer */
  235. }
  236. /*
  237. Local Variables:
  238. mode:c
  239. indent-tabs-mode:nil
  240. c-basic-offset:2
  241. End:
  242. */