connection_ap.c 13 KB

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