connection_or.c 8.7 KB

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