connection_ap.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /* Copyright 2001,2002 Roger Dingledine, Matej Pfajfar. */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. #include "or.h"
  5. extern int global_role; /* from main.c */
  6. int connection_ap_process_inbuf(connection_t *conn) {
  7. assert(conn && conn->type == CONN_TYPE_AP);
  8. if(conn->inbuf_reached_eof) {
  9. /* eof reached, kill it. */
  10. log(LOG_DEBUG,"connection_ap_process_inbuf(): conn reached eof. Closing.");
  11. return -1;
  12. }
  13. // log(LOG_DEBUG,"connection_ap_process_inbuf(): state %d.",conn->state);
  14. switch(conn->state) {
  15. case AP_CONN_STATE_SS_WAIT:
  16. return ap_handshake_process_ss(conn);
  17. case AP_CONN_STATE_OPEN:
  18. return connection_package_raw_inbuf(conn);
  19. default:
  20. log(LOG_DEBUG,"connection_ap_process_inbuf() called in state where I'm waiting. Ignoring buf for now.");
  21. }
  22. return 0;
  23. }
  24. int ap_handshake_process_ss(connection_t *conn) {
  25. uint16_t len;
  26. assert(conn);
  27. log(LOG_DEBUG,"ap_handshake_process_ss() entered.");
  28. if(!conn->ss_received) { /* try to pull it in */
  29. if(conn->inbuf_datalen < sizeof(ss_t)) /* entire ss available? */
  30. return 0; /* not yet */
  31. if(connection_fetch_from_buf((char *)&conn->ss,sizeof(ss_t),conn) < 0)
  32. return -1;
  33. conn->ss_received = sizeof(ss_t);
  34. log(LOG_DEBUG,"ap_handshake_process_ss(): Successfully read ss.");
  35. if ((conn->ss.version == 0) || (conn->ss.version != VERSION)) { /* unsupported version */
  36. log(LOG_DEBUG,"ap_handshake_process_ss(): ss: Unsupported version.");
  37. return -1;
  38. }
  39. if (conn->ss.addr_fmt != SS_ADDR_FMT_ASCII_HOST_PORT) { /* unrecognized address format */
  40. log(LOG_DEBUG,"ap_handshake_process_ss(): ss: Unrecognized address format.");
  41. return -1;
  42. }
  43. }
  44. if(!conn->dest_addr) { /* no dest_addr found yet */
  45. if(conn->inbuf_datalen < sizeof(uint16_t))
  46. return 0; /* not yet */
  47. if(connection_fetch_from_buf((char *)&len,sizeof(uint16_t),conn) < 0)
  48. return -1;
  49. len = ntohs(len);
  50. if(len > 512) {
  51. log(LOG_DEBUG,"ap_handshake_process_ss(): Addr length %d too high.",len);
  52. return -1;
  53. }
  54. conn->dest_addr = malloc(len+1);
  55. if(!conn->dest_addr) {
  56. log(LOG_DEBUG,"ap_handshake_process_ss(): Addr malloc failed");
  57. return -1;
  58. }
  59. conn->dest_addr[len] = 0; /* null terminate it */
  60. conn->dest_addr_len = len;
  61. log(LOG_DEBUG,"Preparing a dest_addr of %d+1 bytes.",len);
  62. }
  63. if(conn->dest_addr_len != conn->dest_addr_received) { /* try to fetch it all in */
  64. if(conn->inbuf_datalen < conn->dest_addr_len)
  65. return 0; /* not yet */
  66. if(connection_fetch_from_buf(conn->dest_addr,conn->dest_addr_len,conn) < 0)
  67. return -1;
  68. log(LOG_DEBUG,"ap_handshake_process_ss(): Read dest_addr '%s'.",conn->dest_addr);
  69. conn->dest_addr_received = conn->dest_addr_len;
  70. }
  71. /* now do the same thing for port */
  72. if(!conn->dest_port) { /* no dest_port found yet */
  73. if(conn->inbuf_datalen < sizeof(uint16_t))
  74. return 0; /* not yet */
  75. if(connection_fetch_from_buf((char *)&len,sizeof(uint16_t),conn) < 0)
  76. return -1;
  77. len = ntohs(len);
  78. if(len > 10) {
  79. log(LOG_DEBUG,"ap_handshake_process_ss(): Port length %d too high.",len);
  80. return -1;
  81. }
  82. conn->dest_port = malloc(len+1);
  83. if(!conn->dest_port) {
  84. log(LOG_DEBUG,"ap_handshake_process_ss(): Port malloc failed");
  85. return -1;
  86. }
  87. conn->dest_port[len] = 0; /* null terminate it */
  88. conn->dest_port_len = len;
  89. log(LOG_DEBUG,"Preparing a dest_port of %d+1 bytes.",len);
  90. }
  91. if(conn->dest_port_len != conn->dest_port_received) { /* try to fetch it all in */
  92. if(conn->inbuf_datalen < conn->dest_port_len)
  93. return 0; /* not yet */
  94. if(connection_fetch_from_buf(conn->dest_port,conn->dest_port_len,conn) < 0)
  95. return -1;
  96. log(LOG_DEBUG,"ap_handshake_process_ss(): Read dest_port '%s'.",conn->dest_port);
  97. conn->dest_port_received = conn->dest_port_len;
  98. }
  99. /* now we're all ready to make an onion, etc */
  100. return ap_handshake_create_onion(conn);
  101. }
  102. int ap_handshake_create_onion(connection_t *conn) {
  103. int i;
  104. int routelen = 0; /* length of the route */
  105. unsigned int *route = NULL; /* hops in the route as an array of indexes into rarray */
  106. unsigned char *onion = NULL; /* holds the onion */
  107. int onionlen = 0; /* onion length in host order */
  108. crypt_path_t **cpath = NULL; /* defines the crypt operations that need to be performed on incoming/outgoing data */
  109. assert(conn);
  110. /* choose a route */
  111. route = (unsigned int *)router_new_route(&routelen);
  112. if (!route) {
  113. log(LOG_ERR,"ap_handshake_create_onion(): Error choosing a route through the OR network.");
  114. return -1;
  115. }
  116. log(LOG_DEBUG,"ap_handshake_create_onion(): Chosen a route of length %u : ",routelen);
  117. #if 0
  118. for (i=routelen-1;i>=0;i--)
  119. {
  120. log(LOG_DEBUG,"ap_handshake_process_ss() : %u : %s:%u, %u",routelen-i,(routerarray[route[i]])->address,ntohs((routerarray[route[i]])->port),RSA_size((routerarray[route[i]])->pkey));
  121. }
  122. #endif
  123. /* allocate memory for the crypt path */
  124. cpath = malloc(routelen * sizeof(crypt_path_t *));
  125. if (!cpath) {
  126. log(LOG_ERR,"ap_handshake_create_onion(): Error allocating memory for cpath.");
  127. free(route);
  128. return -1;
  129. }
  130. /* create an onion and calculate crypto keys */
  131. onion = router_create_onion(route,routelen,&onionlen,cpath);
  132. if (!onion) {
  133. log(LOG_ERR,"ap_handshake_create_onion(): Error creating an onion.");
  134. free(route);
  135. free(cpath); /* it's got nothing in it, since !onion */
  136. return -1;
  137. }
  138. log(LOG_DEBUG,"ap_handshake_create_onion(): Created an onion of size %u bytes.",onionlen);
  139. log(LOG_DEBUG,"ap_handshake_create_onion(): Crypt path :");
  140. for (i=0;i<routelen;i++) {
  141. log(LOG_DEBUG,"ap_handshake_create_onion() : %u/%u",(cpath[i])->forwf, (cpath[i])->backf);
  142. }
  143. return ap_handshake_establish_circuit(conn, route, routelen, onion, onionlen, cpath);
  144. }
  145. int ap_handshake_establish_circuit(connection_t *conn, unsigned int *route, int routelen, char *onion,
  146. int onionlen, crypt_path_t **cpath) {
  147. routerinfo_t *firsthop;
  148. connection_t *n_conn;
  149. circuit_t *circ;
  150. /* now see if we're already connected to the first OR in 'route' */
  151. firsthop = router_get_first_in_route(route, routelen);
  152. assert(firsthop); /* should always be defined */
  153. free(route); /* we don't need it anymore */
  154. circ = circuit_new(0, conn); /* sets circ->p_aci and circ->p_conn */
  155. circ->state = CIRCUIT_STATE_OR_WAIT;
  156. circ->onion = onion;
  157. circ->onionlen = onionlen;
  158. circ->cpath = cpath;
  159. circ->cpathlen = routelen;
  160. log(LOG_DEBUG,"ap_handshake_establish_circuit(): Looking for firsthop '%s:%u'",
  161. firsthop->address,firsthop->or_port);
  162. n_conn = connection_twin_get_by_addr_port(firsthop->addr,firsthop->or_port);
  163. if(!n_conn) { /* not currently connected */
  164. circ->n_addr = firsthop->addr;
  165. circ->n_port = firsthop->or_port;
  166. if(global_role & ROLE_OR_CONNECT_ALL) { /* we would be connected if he were up. but he's not. */
  167. log(LOG_DEBUG,"ap_handshake_establish_circuit(): Route's firsthop isn't connected.");
  168. circuit_close(circ);
  169. return -1;
  170. }
  171. /* ok, launch the connection */
  172. n_conn = connect_to_router_as_op(firsthop);
  173. if(!n_conn) { /* connect failed, forget the whole thing */
  174. log(LOG_DEBUG,"ap_handshake_establish_circuit(): connect to firsthop failed. Closing.");
  175. circuit_close(circ);
  176. return -1;
  177. }
  178. conn->state = AP_CONN_STATE_OR_WAIT;
  179. connection_stop_reading(conn); /* Stop listening for input from the AP! */
  180. return 0; /* return success. The onion/circuit/etc will be taken care of automatically
  181. * (may already have been) whenever n_conn reaches OR_CONN_STATE_OPEN.
  182. */
  183. } else { /* it (or a twin) is already open. use it. */
  184. circ->n_addr = n_conn->addr;
  185. circ->n_port = n_conn->port;
  186. return ap_handshake_send_onion(conn, n_conn, circ);
  187. }
  188. }
  189. /* find the circ that's waiting on me, if any, and get it to send its onion */
  190. int ap_handshake_n_conn_open(connection_t *or_conn) {
  191. circuit_t *circ;
  192. log(LOG_DEBUG,"ap_handshake_n_conn_open(): Starting.");
  193. circ = circuit_get_by_naddr_nport(or_conn->addr, or_conn->port);
  194. if(!circ)
  195. return 0; /* i'm ok with that. no need to close the connection or anything. */
  196. if(circ->p_conn->state != AP_CONN_STATE_OR_WAIT) {
  197. log(LOG_DEBUG,"Bug: ap_handshake_n_conn_open() got an ap_conn not in OR_WAIT state.");
  198. }
  199. connection_start_reading(circ->p_conn); /* resume listening for reads */
  200. log(LOG_DEBUG,"ap_handshake_n_conn_open(): Found circ, sending onion.");
  201. return ap_handshake_send_onion(circ->p_conn, or_conn, circ);
  202. }
  203. int ap_handshake_send_onion(connection_t *ap_conn, connection_t *n_conn, circuit_t *circ) {
  204. cell_t cell;
  205. int tmpbuflen, dataleft;
  206. char *tmpbuf;
  207. char zero=0;
  208. circ->n_aci = get_unique_aci_by_addr_port(circ->n_addr, circ->n_port, ACI_TYPE_BOTH);
  209. circ->n_conn = n_conn;
  210. log(LOG_DEBUG,"ap_handshake_send_onion(): n_conn is %s:%u",n_conn->address,n_conn->port);
  211. /* deliver the onion as one or more create cells */
  212. cell.command = CELL_CREATE;
  213. cell.aci = circ->n_aci;
  214. tmpbuflen = circ->onionlen+4;
  215. tmpbuf = malloc(tmpbuflen);
  216. if(!tmpbuf)
  217. return -1;
  218. *(uint32_t*)tmpbuf = htonl(circ->onionlen);
  219. memcpy(tmpbuf+4, circ->onion, circ->onionlen);
  220. dataleft = tmpbuflen;
  221. while(dataleft) {
  222. cell.command = CELL_CREATE;
  223. cell.aci = circ->n_aci;
  224. log(LOG_DEBUG,"ap_handshake_send_onion(): Sending a create cell for the onion...");
  225. if(dataleft >= CELL_PAYLOAD_SIZE) {
  226. cell.length = CELL_PAYLOAD_SIZE;
  227. memcpy(cell.payload, tmpbuf + tmpbuflen - dataleft, CELL_PAYLOAD_SIZE);
  228. connection_write_cell_to_buf(&cell, n_conn); /* clobbers cell */
  229. dataleft -= CELL_PAYLOAD_SIZE;
  230. } else { /* last cell */
  231. cell.length = dataleft;
  232. memcpy(cell.payload, tmpbuf + tmpbuflen - dataleft, dataleft);
  233. connection_write_cell_to_buf(&cell, n_conn); /* clobbers cell */
  234. dataleft = 0;
  235. }
  236. }
  237. free(tmpbuf);
  238. /* deliver the ss in a data cell */
  239. cell.command = CELL_DATA;
  240. cell.aci = circ->n_aci;
  241. cell.length = sizeof(ss_t);
  242. memcpy(cell.payload, &ap_conn->ss, sizeof(ss_t));
  243. log(LOG_DEBUG,"ap_handshake_send_onion(): Sending a data cell for ss...");
  244. if(circuit_deliver_data_cell(&cell, circ, circ->n_conn, 'e') < 0) {
  245. log(LOG_DEBUG,"ap_handshake_send_onion(): failed to deliver ss cell. Closing.");
  246. circuit_close(circ);
  247. return -1;
  248. }
  249. /* deliver the dest_addr in a data cell */
  250. cell.command = CELL_DATA;
  251. cell.aci = circ->n_aci;
  252. cell.length = ap_conn->dest_addr_len+1;
  253. strncpy(cell.payload, ap_conn->dest_addr, ap_conn->dest_addr_len+1);
  254. log(LOG_DEBUG,"ap_handshake_send_onion(): Sending a data cell for addr...");
  255. if(circuit_deliver_data_cell(&cell, circ, circ->n_conn, 'e') < 0) {
  256. log(LOG_DEBUG,"ap_handshake_send_onion(): failed to deliver addr cell. Closing.");
  257. circuit_close(circ);
  258. return -1;
  259. }
  260. /* deliver the dest_port in a data cell */
  261. cell.command = CELL_DATA;
  262. cell.aci = circ->n_aci;
  263. cell.length = ap_conn->dest_port_len+1;
  264. strncpy(cell.payload, ap_conn->dest_port, ap_conn->dest_port_len+1);
  265. log(LOG_DEBUG,"ap_handshake_send_onion(): Sending a data cell for port...");
  266. if(circuit_deliver_data_cell(&cell, circ, circ->n_conn, 'e') < 0) {
  267. log(LOG_DEBUG,"ap_handshake_send_onion(): failed to deliver port cell. Closing.");
  268. circuit_close(circ);
  269. return -1;
  270. }
  271. circ->state = CIRCUIT_STATE_OPEN;
  272. ap_conn->state = AP_CONN_STATE_OPEN;
  273. log(LOG_INFO,"ap_handshake_send_onion(): Address/port sent, ap socket %d, n_aci %d",ap_conn->s,circ->n_aci);
  274. /* FIXME should set circ->expire to something here */
  275. /* now we want to give the AP a "0" byte, because it wants to hear
  276. * back from us */
  277. connection_write_to_buf(&zero, 1, ap_conn); /* this does connection_start_writing() too */
  278. return 0;
  279. }
  280. int connection_ap_process_data_cell(cell_t *cell, connection_t *conn) {
  281. /* an incoming data cell has arrived */
  282. assert(conn && conn->type == CONN_TYPE_AP);
  283. if(conn->state != AP_CONN_STATE_OPEN) {
  284. /* we should not have gotten this cell */
  285. log(LOG_DEBUG,"connection_ap_process_data_cell(): Got a data cell when not in 'open' state. Closing.");
  286. return -1;
  287. }
  288. // log(LOG_DEBUG,"connection_ap_process_data_cell(): In state 'open', writing to buf.");
  289. if(connection_write_to_buf(cell->payload, cell->length, conn) < 0)
  290. return -1;
  291. return connection_consider_sending_sendme(conn);
  292. }
  293. int connection_ap_finished_flushing(connection_t *conn) {
  294. assert(conn && conn->type == CONN_TYPE_AP);
  295. switch(conn->state) {
  296. case AP_CONN_STATE_OPEN:
  297. /* FIXME down the road, we'll clear out circuits that are pending to close */
  298. connection_stop_writing(conn);
  299. return connection_consider_sending_sendme(conn);
  300. default:
  301. log(LOG_DEBUG,"Bug: connection_ap_finished_flushing() called in unexpected state.");
  302. return 0;
  303. }
  304. return 0;
  305. }
  306. int connection_ap_create_listener(crypto_pk_env_t *prkey, struct sockaddr_in *local) {
  307. log(LOG_DEBUG,"connection_create_ap_listener starting");
  308. return connection_create_listener(prkey, local, CONN_TYPE_AP_LISTENER);
  309. }
  310. int connection_ap_handle_listener_read(connection_t *conn) {
  311. log(LOG_NOTICE,"AP: Received a connection request. Waiting for SS.");
  312. return connection_handle_listener_read(conn, CONN_TYPE_AP, AP_CONN_STATE_SS_WAIT);
  313. }