connection_edge.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  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_ap_handshake_process_socks(connection_t *conn);
  7. static int connection_ap_handshake_send_begin(connection_t *ap_conn, circuit_t *circ,
  8. char *destaddr, uint16_t destport);
  9. static int connection_ap_handshake_socks_reply(connection_t *conn, char *reply,
  10. int replylen, char success);
  11. static int connection_exit_begin_conn(cell_t *cell, circuit_t *circ);
  12. int connection_edge_process_inbuf(connection_t *conn) {
  13. assert(conn);
  14. assert(conn->type == CONN_TYPE_AP || conn->type == CONN_TYPE_EXIT);
  15. if(conn->inbuf_reached_eof) {
  16. #ifdef HALF_OPEN
  17. /* eof reached; we're done reading, but we might want to write more. */
  18. conn->done_receiving = 1;
  19. shutdown(conn->s, 0); /* XXX check return, refactor NM */
  20. if (conn->done_sending)
  21. /*ENDCLOSE*/ conn->marked_for_close = 1;
  22. /* XXX Factor out common logic here and in circuit_about_to_close NM */
  23. connection_edge_send_command(conn, circuit_get_by_conn(conn), RELAY_COMMAND_END,
  24. NULL, 0, conn->cpath_layer);
  25. return 0;
  26. #else
  27. /* eof reached, kill it. */
  28. log_fn(LOG_INFO,"conn (fd %d) reached eof. Closing.", conn->s);
  29. /*ENDCLOSE*/ return -1;
  30. #endif
  31. }
  32. switch(conn->state) {
  33. case AP_CONN_STATE_SOCKS_WAIT:
  34. /*ENDCLOSE*/ return connection_ap_handshake_process_socks(conn);
  35. case AP_CONN_STATE_OPEN:
  36. case EXIT_CONN_STATE_OPEN:
  37. if(connection_package_raw_inbuf(conn) < 0)
  38. /*ENDCLOSE*/ return -1;
  39. return 0;
  40. case EXIT_CONN_STATE_CONNECTING:
  41. log_fn(LOG_INFO,"text from server while in 'connecting' state at exit. Leaving it on buffer.");
  42. return 0;
  43. }
  44. return 0;
  45. }
  46. void connection_edge_send_command(connection_t *fromconn, circuit_t *circ, int relay_command,
  47. void *payload, int payload_len, crypt_path_t *cpath_layer) {
  48. cell_t cell;
  49. int cell_direction;
  50. int is_control_cell=0;
  51. if(!circ) {
  52. log_fn(LOG_WARNING,"no circ. Closing.");
  53. return;
  54. }
  55. if(!fromconn || relay_command == RELAY_COMMAND_BEGIN) /* XXX more */
  56. is_control_cell = 1;
  57. memset(&cell, 0, sizeof(cell_t));
  58. if(fromconn && fromconn->type == CONN_TYPE_AP) {
  59. cell.aci = circ->n_aci;
  60. cell_direction = CELL_DIRECTION_OUT;
  61. } else {
  62. /* NOTE: if !fromconn, we assume that it's heading towards the OP */
  63. cell.aci = circ->p_aci;
  64. cell_direction = CELL_DIRECTION_IN;
  65. }
  66. cell.command = CELL_RELAY;
  67. SET_CELL_RELAY_COMMAND(cell, relay_command);
  68. if(is_control_cell)
  69. SET_CELL_STREAM_ID(cell, ZERO_STREAM);
  70. else
  71. SET_CELL_STREAM_ID(cell, fromconn->stream_id);
  72. cell.length = RELAY_HEADER_SIZE + payload_len;
  73. if(payload_len) {
  74. memcpy(cell.payload+RELAY_HEADER_SIZE,payload,payload_len);
  75. }
  76. log_fn(LOG_INFO,"delivering %d cell %s.", relay_command,
  77. cell_direction == CELL_DIRECTION_OUT ? "forward" : "backward");
  78. if(circuit_deliver_relay_cell(&cell, circ, cell_direction, cpath_layer) < 0) {
  79. log_fn(LOG_WARNING,"circuit_deliver_relay_cell failed. Closing.");
  80. circuit_close(circ);
  81. }
  82. }
  83. /* an incoming relay cell has arrived. return -1 if you want to tear down the
  84. * circuit, else 0. */
  85. int connection_edge_process_relay_cell(cell_t *cell, circuit_t *circ, connection_t *conn,
  86. int edge_type, crypt_path_t *layer_hint) {
  87. int relay_command;
  88. static int num_seen=0;
  89. assert(cell && circ);
  90. relay_command = CELL_RELAY_COMMAND(*cell);
  91. // log_fn(LOG_DEBUG,"command %d stream %d", relay_command, stream_id);
  92. num_seen++;
  93. log_fn(LOG_DEBUG,"Now seen %d relay cells here.", num_seen);
  94. /* either conn is NULL, in which case we've got a control cell, or else
  95. * conn points to the recognized stream. */
  96. if(conn && conn->state != AP_CONN_STATE_OPEN && conn->state != EXIT_CONN_STATE_OPEN) {
  97. if(conn->type == CONN_TYPE_EXIT && relay_command == RELAY_COMMAND_END) {
  98. log_fn(LOG_INFO,"Exit got end before we're connected. Marking for close.");
  99. conn->marked_for_close = 1;
  100. if(conn->state == EXIT_CONN_STATE_RESOLVING) {
  101. log_fn(LOG_INFO,"...and informing resolver we don't want the answer anymore.");
  102. dns_cancel_pending_resolve(conn->address, conn);
  103. }
  104. return 0;
  105. } else {
  106. log_fn(LOG_WARNING,"Got an unexpected relay cell, not in 'open' state. Closing.");
  107. return -1;
  108. }
  109. }
  110. switch(relay_command) {
  111. case RELAY_COMMAND_BEGIN:
  112. if(edge_type == EDGE_AP) {
  113. log_fn(LOG_WARNING,"relay begin request unsupported at AP. Dropping.");
  114. return 0;
  115. }
  116. if(conn) {
  117. log_fn(LOG_WARNING,"begin cell for known stream. Dropping.");
  118. return 0;
  119. }
  120. return connection_exit_begin_conn(cell, circ);
  121. case RELAY_COMMAND_DATA:
  122. ++stats_n_data_cells_received;
  123. if((edge_type == EDGE_AP && --layer_hint->deliver_window < 0) ||
  124. (edge_type == EDGE_EXIT && --circ->deliver_window < 0)) {
  125. log_fn(LOG_WARNING,"(relay data) circ deliver_window below 0. Killing.");
  126. return -1;
  127. }
  128. log_fn(LOG_DEBUG,"circ deliver_window now %d.", edge_type == EDGE_AP ? layer_hint->deliver_window : circ->deliver_window);
  129. if(circuit_consider_sending_sendme(circ, edge_type, layer_hint) < 0)
  130. return -1;
  131. if(!conn) {
  132. log_fn(LOG_INFO,"relay cell dropped, unknown stream %d.",*(int*)conn->stream_id);
  133. return 0;
  134. }
  135. if(--conn->deliver_window < 0) { /* is it below 0 after decrement? */
  136. log_fn(LOG_WARNING,"(relay data) conn deliver_window below 0. Killing.");
  137. return -1; /* somebody's breaking protocol. kill the whole circuit. */
  138. }
  139. // printf("New text for buf (%d bytes): '%s'", cell->length - RELAY_HEADER_SIZE, cell->payload + RELAY_HEADER_SIZE);
  140. stats_n_data_bytes_received += (cell->length - RELAY_HEADER_SIZE);
  141. connection_write_to_buf(cell->payload + RELAY_HEADER_SIZE,
  142. cell->length - RELAY_HEADER_SIZE, conn);
  143. connection_consider_sending_sendme(conn, edge_type);
  144. return 0;
  145. case RELAY_COMMAND_END:
  146. if(!conn) {
  147. log_fn(LOG_INFO,"end cell dropped, unknown stream %d.",*(int*)conn->stream_id);
  148. return 0;
  149. }
  150. log_fn(LOG_INFO,"end cell for stream %d. Removing stream.",*(int*)conn->stream_id);
  151. #ifdef HALF_OPEN
  152. conn->done_sending = 1;
  153. shutdown(conn->s, 1); /* XXX check return; refactor NM */
  154. if (conn->done_receiving)
  155. /*ENDCLOSE*/ conn->marked_for_close = 1;
  156. #endif
  157. /*ENDCLOSE*/ conn->marked_for_close = 1;
  158. break;
  159. case RELAY_COMMAND_EXTEND:
  160. if(conn) {
  161. log_fn(LOG_WARNING,"'extend' for non-zero stream. Dropping.");
  162. return 0;
  163. }
  164. return circuit_extend(cell, circ);
  165. case RELAY_COMMAND_EXTENDED:
  166. if(edge_type == EDGE_EXIT) {
  167. log_fn(LOG_WARNING,"'extended' unsupported at exit. Dropping.");
  168. return 0;
  169. }
  170. log_fn(LOG_DEBUG,"Got an extended cell! Yay.");
  171. if(circuit_finish_handshake(circ, cell->payload+RELAY_HEADER_SIZE) < 0) {
  172. log_fn(LOG_WARNING,"circuit_finish_handshake failed.");
  173. return -1;
  174. }
  175. return circuit_send_next_onion_skin(circ);
  176. case RELAY_COMMAND_TRUNCATE:
  177. if(edge_type == EDGE_AP) {
  178. log_fn(LOG_WARNING,"'truncate' unsupported at AP. Dropping.");
  179. return 0;
  180. }
  181. if(circ->n_conn) {
  182. connection_send_destroy(circ->n_aci, circ->n_conn);
  183. circ->n_conn = NULL;
  184. }
  185. log_fn(LOG_DEBUG, "Processed 'truncate', replying.");
  186. connection_edge_send_command(NULL, circ, RELAY_COMMAND_TRUNCATED,
  187. NULL, 0, NULL);
  188. return 0;
  189. case RELAY_COMMAND_TRUNCATED:
  190. if(edge_type == EDGE_EXIT) {
  191. log_fn(LOG_WARNING,"'truncated' unsupported at exit. Dropping.");
  192. return 0;
  193. }
  194. return circuit_truncated(circ, layer_hint);
  195. case RELAY_COMMAND_CONNECTED:
  196. if(edge_type == EDGE_EXIT) {
  197. log_fn(LOG_WARNING,"'connected' unsupported at exit. Dropping.");
  198. return 0;
  199. }
  200. if(!conn) {
  201. log_fn(LOG_INFO,"connected cell dropped, unknown stream %d.",*(int*)conn->stream_id);
  202. break;
  203. }
  204. log_fn(LOG_INFO,"Connected! Notifying application.");
  205. if(connection_ap_handshake_socks_reply(conn, NULL, 0, 1) < 0) {
  206. /*ENDCLOSE*/ conn->marked_for_close = 1;
  207. }
  208. break;
  209. case RELAY_COMMAND_SENDME:
  210. if(!conn) {
  211. if(edge_type == EDGE_AP) {
  212. assert(layer_hint);
  213. layer_hint->package_window += CIRCWINDOW_INCREMENT;
  214. log_fn(LOG_DEBUG,"circ-level sendme at AP, packagewindow %d.", layer_hint->package_window);
  215. circuit_resume_edge_reading(circ, EDGE_AP, layer_hint);
  216. } else {
  217. assert(!layer_hint);
  218. circ->package_window += CIRCWINDOW_INCREMENT;
  219. log_fn(LOG_DEBUG,"circ-level sendme at exit, packagewindow %d.", circ->package_window);
  220. circuit_resume_edge_reading(circ, EDGE_EXIT, layer_hint);
  221. }
  222. return 0;
  223. }
  224. conn->package_window += STREAMWINDOW_INCREMENT;
  225. log_fn(LOG_DEBUG,"stream-level sendme, packagewindow now %d.", conn->package_window);
  226. connection_start_reading(conn);
  227. connection_package_raw_inbuf(conn); /* handle whatever might still be on the inbuf */
  228. break;
  229. default:
  230. log_fn(LOG_WARNING,"unknown relay command %d.",relay_command);
  231. return -1;
  232. }
  233. return 0;
  234. }
  235. int connection_edge_finished_flushing(connection_t *conn) {
  236. int e, len=sizeof(e);
  237. assert(conn);
  238. assert(conn->type == CONN_TYPE_AP || conn->type == CONN_TYPE_EXIT);
  239. switch(conn->state) {
  240. case EXIT_CONN_STATE_CONNECTING:
  241. if (getsockopt(conn->s, SOL_SOCKET, SO_ERROR, (void*)&e, &len) < 0) { /* not yet */
  242. if(!ERRNO_CONN_EINPROGRESS(errno)) {
  243. /* yuck. kill it. */
  244. log_fn(LOG_DEBUG,"in-progress exit connect failed. Removing.");
  245. return -1;
  246. } else {
  247. log_fn(LOG_DEBUG,"in-progress exit connect still waiting.");
  248. return 0; /* no change, see if next time is better */
  249. }
  250. }
  251. /* the connect has finished. */
  252. log_fn(LOG_INFO,"Exit connection to %s:%u established.",
  253. conn->address,conn->port);
  254. conn->state = EXIT_CONN_STATE_OPEN;
  255. connection_watch_events(conn, POLLIN); /* stop writing, continue reading */
  256. if(connection_wants_to_flush(conn)) /* in case there are any queued relay cells */
  257. connection_start_writing(conn);
  258. /* deliver a 'connected' relay cell back through the circuit. */
  259. connection_edge_send_command(conn, circuit_get_by_conn(conn), RELAY_COMMAND_CONNECTED,
  260. NULL, 0, conn->cpath_layer);
  261. return connection_process_inbuf(conn); /* in case the server has written anything */
  262. case AP_CONN_STATE_OPEN:
  263. case EXIT_CONN_STATE_OPEN:
  264. connection_stop_writing(conn);
  265. connection_consider_sending_sendme(conn, conn->type);
  266. return 0;
  267. case AP_CONN_STATE_SOCKS_WAIT:
  268. connection_stop_writing(conn);
  269. return 0;
  270. default:
  271. log_fn(LOG_WARNING,"BUG: called in unexpected state.");
  272. return -1;
  273. }
  274. return 0;
  275. }
  276. uint64_t stats_n_data_cells_packaged = 0;
  277. uint64_t stats_n_data_bytes_packaged = 0;
  278. uint64_t stats_n_data_cells_received = 0;
  279. uint64_t stats_n_data_bytes_received = 0;
  280. int connection_package_raw_inbuf(connection_t *conn) {
  281. int amount_to_process, length;
  282. char payload[CELL_PAYLOAD_SIZE];
  283. circuit_t *circ;
  284. assert(conn);
  285. assert(!connection_speaks_cells(conn));
  286. repeat_connection_package_raw_inbuf:
  287. circ = circuit_get_by_conn(conn);
  288. if(!circ) {
  289. log_fn(LOG_INFO,"conn has no circuits! Closing.");
  290. return -1;
  291. }
  292. if(circuit_consider_stop_edge_reading(circ, conn->type, conn->cpath_layer))
  293. return 0;
  294. if(conn->package_window <= 0) {
  295. log_fn(LOG_WARNING,"called with package_window %d. Tell Roger.", conn->package_window);
  296. connection_stop_reading(conn);
  297. return 0;
  298. }
  299. amount_to_process = buf_datalen(conn->inbuf);
  300. if(!amount_to_process)
  301. return 0;
  302. if(amount_to_process > CELL_PAYLOAD_SIZE - RELAY_HEADER_SIZE) {
  303. length = CELL_PAYLOAD_SIZE - RELAY_HEADER_SIZE;
  304. } else {
  305. length = amount_to_process;
  306. }
  307. stats_n_data_bytes_packaged += length;
  308. stats_n_data_cells_packaged += 1;
  309. connection_fetch_from_buf(payload, length, conn);
  310. log_fn(LOG_DEBUG,"(%d) Packaging %d bytes (%d waiting).",conn->s,length,
  311. (int)buf_datalen(conn->inbuf));
  312. connection_edge_send_command(conn, circ, RELAY_COMMAND_DATA,
  313. payload, length, conn->cpath_layer);
  314. if(conn->type == CONN_TYPE_EXIT) {
  315. assert(circ->package_window > 0);
  316. circ->package_window--;
  317. } else { /* we're an AP */
  318. assert(conn->type == CONN_TYPE_AP);
  319. assert(conn->cpath_layer->package_window > 0);
  320. conn->cpath_layer->package_window--;
  321. }
  322. if(--conn->package_window <= 0) { /* is it 0 after decrement? */
  323. connection_stop_reading(conn);
  324. log_fn(LOG_DEBUG,"conn->package_window reached 0.");
  325. circuit_consider_stop_edge_reading(circ, conn->type, conn->cpath_layer);
  326. return 0; /* don't process the inbuf any more */
  327. }
  328. log_fn(LOG_DEBUG,"conn->package_window is now %d",conn->package_window);
  329. /* handle more if there's more, or return 0 if there isn't */
  330. goto repeat_connection_package_raw_inbuf;
  331. }
  332. void connection_consider_sending_sendme(connection_t *conn, int edge_type) {
  333. circuit_t *circ;
  334. cell_t cell;
  335. if(connection_outbuf_too_full(conn))
  336. return;
  337. circ = circuit_get_by_conn(conn);
  338. if(!circ) {
  339. /* this can legitimately happen if the destroy has already arrived and torn down the circuit */
  340. log_fn(LOG_INFO,"No circuit associated with conn. Skipping.");
  341. return;
  342. }
  343. memset(&cell, 0, sizeof(cell_t));
  344. cell.command = CELL_RELAY;
  345. SET_CELL_RELAY_COMMAND(cell, RELAY_COMMAND_SENDME);
  346. SET_CELL_STREAM_ID(cell, conn->stream_id);
  347. cell.length += RELAY_HEADER_SIZE;
  348. if(edge_type == EDGE_EXIT)
  349. cell.aci = circ->p_aci;
  350. else
  351. cell.aci = circ->n_aci;
  352. while(conn->deliver_window < STREAMWINDOW_START - STREAMWINDOW_INCREMENT) {
  353. log_fn(LOG_DEBUG,"Outbuf %d, Queueing stream sendme.", conn->outbuf_flushlen);
  354. conn->deliver_window += STREAMWINDOW_INCREMENT;
  355. if(circuit_deliver_relay_cell(&cell, circ, CELL_DIRECTION(edge_type), conn->cpath_layer) < 0) {
  356. log_fn(LOG_WARNING,"circuit_deliver_relay_cell failed. Closing.");
  357. circuit_close(circ);
  358. return;
  359. }
  360. }
  361. }
  362. static int connection_ap_handshake_process_socks(connection_t *conn) {
  363. circuit_t *circ;
  364. char destaddr[200]; /* XXX why 200? but not 256, because it won't fit in a cell */
  365. char reply[256];
  366. uint16_t destport;
  367. int replylen=0;
  368. int sockshere;
  369. assert(conn);
  370. log_fn(LOG_DEBUG,"entered.");
  371. sockshere = fetch_from_buf_socks(conn->inbuf, &conn->socks_version, reply, &replylen,
  372. destaddr, sizeof(destaddr), &destport);
  373. if(sockshere == -1 || sockshere == 0) {
  374. if(replylen) { /* we should send reply back */
  375. log_fn(LOG_DEBUG,"reply is already set for us. Using it.");
  376. connection_ap_handshake_socks_reply(conn, reply, replylen, 0);
  377. } else if(sockshere == -1) { /* send normal reject */
  378. log_fn(LOG_WARNING,"Fetching socks handshake failed. Closing.");
  379. connection_ap_handshake_socks_reply(conn, NULL, 0, 0);
  380. } else {
  381. log_fn(LOG_DEBUG,"socks handshake not all here yet.");
  382. }
  383. return sockshere;
  384. } /* else socks handshake is done, continue processing */
  385. /* find the circuit that we should use, if there is one. */
  386. circ = circuit_get_newest_open();
  387. if(!circ) {
  388. log_fn(LOG_INFO,"No circuit ready. Closing.");
  389. return -1;
  390. }
  391. circ->dirty = 1;
  392. /* add it into the linked list of streams on this circuit */
  393. log_fn(LOG_DEBUG,"attaching new conn to circ. n_aci %d.", circ->n_aci);
  394. conn->next_stream = circ->p_streams;
  395. circ->p_streams = conn;
  396. assert(circ->cpath && circ->cpath->prev);
  397. assert(circ->cpath->prev->state == CPATH_STATE_OPEN);
  398. conn->cpath_layer = circ->cpath->prev;
  399. if(connection_ap_handshake_send_begin(conn, circ, destaddr, destport) < 0) {
  400. circuit_close(circ);
  401. return -1;
  402. }
  403. return 0;
  404. }
  405. /* deliver the destaddr:destport in a relay cell */
  406. static int connection_ap_handshake_send_begin(connection_t *ap_conn, circuit_t *circ,
  407. char *destaddr, uint16_t destport) {
  408. char payload[CELL_PAYLOAD_SIZE];
  409. int payload_len;
  410. if(crypto_pseudo_rand(STREAM_ID_SIZE, ap_conn->stream_id) < 0)
  411. return -1;
  412. /* FIXME check for collisions */
  413. memcpy(payload, ap_conn->stream_id, STREAM_ID_SIZE);
  414. payload_len = STREAM_ID_SIZE + 1 +
  415. snprintf(payload+STREAM_ID_SIZE,CELL_PAYLOAD_SIZE-RELAY_HEADER_SIZE-STREAM_ID_SIZE,
  416. "%s:%d", destaddr, destport);
  417. log_fn(LOG_DEBUG,"Sending relay cell to begin stream %d.",*(int *)ap_conn->stream_id);
  418. connection_edge_send_command(ap_conn, circ, RELAY_COMMAND_BEGIN,
  419. payload, payload_len, ap_conn->cpath_layer);
  420. ap_conn->package_window = STREAMWINDOW_START;
  421. ap_conn->deliver_window = STREAMWINDOW_START;
  422. ap_conn->state = AP_CONN_STATE_OPEN;
  423. log_fn(LOG_INFO,"Address/port sent, ap socket %d, n_aci %d",ap_conn->s,circ->n_aci);
  424. return 0;
  425. }
  426. static int connection_ap_handshake_socks_reply(connection_t *conn, char *reply,
  427. int replylen, char success) {
  428. char buf[256];
  429. if(replylen) { /* we already have a reply in mind */
  430. connection_write_to_buf(reply, replylen, conn);
  431. return connection_flush_buf(conn); /* try to flush it */
  432. }
  433. if(conn->socks_version == 4) {
  434. memset(buf,0,SOCKS4_NETWORK_LEN);
  435. #define SOCKS4_GRANTED 90
  436. #define SOCKS4_REJECT 91
  437. buf[1] = (success ? SOCKS4_GRANTED : SOCKS4_REJECT);
  438. /* leave version, destport, destip zero */
  439. connection_write_to_buf(buf, SOCKS4_NETWORK_LEN, conn);
  440. return connection_flush_buf(conn); /* try to flush it */
  441. }
  442. if(conn->socks_version == 5) {
  443. buf[0] = 5; /* version 5 */
  444. #define SOCKS5_SUCCESS 0
  445. #define SOCKS5_GENERIC_ERROR 1
  446. buf[1] = success ? SOCKS5_SUCCESS : SOCKS5_GENERIC_ERROR;
  447. buf[2] = 0;
  448. buf[3] = 1; /* ipv4 addr */
  449. memset(buf+4,0,6); /* XXX set external addr/port to 0, see what breaks */
  450. connection_write_to_buf(buf,10,conn);
  451. return connection_flush_buf(conn); /* try to flush it */
  452. }
  453. return 0; /* if socks_version isn't 4 or 5, don't send anything */
  454. }
  455. /*ENDCLOSE*/ static int connection_exit_begin_conn(cell_t *cell, circuit_t *circ) {
  456. connection_t *n_stream;
  457. char *colon;
  458. if(!memchr(cell->payload+RELAY_HEADER_SIZE+STREAM_ID_SIZE,0,
  459. cell->length-RELAY_HEADER_SIZE-STREAM_ID_SIZE)) {
  460. log_fn(LOG_WARNING,"relay begin cell has no \\0. Dropping.");
  461. return 0;
  462. }
  463. colon = strchr(cell->payload+RELAY_HEADER_SIZE+STREAM_ID_SIZE, ':');
  464. if(!colon) {
  465. log_fn(LOG_WARNING,"relay begin cell has no colon. Dropping.");
  466. return 0;
  467. }
  468. *colon = 0;
  469. if(!atoi(colon+1)) { /* bad port */
  470. log_fn(LOG_WARNING,"relay begin cell has invalid port. Dropping.");
  471. return 0;
  472. }
  473. log_fn(LOG_DEBUG,"Creating new exit connection.");
  474. n_stream = connection_new(CONN_TYPE_EXIT);
  475. memcpy(n_stream->stream_id, cell->payload + RELAY_HEADER_SIZE, STREAM_ID_SIZE);
  476. n_stream->address = tor_strdup(cell->payload + RELAY_HEADER_SIZE + STREAM_ID_SIZE);
  477. n_stream->port = atoi(colon+1);
  478. n_stream->state = EXIT_CONN_STATE_RESOLVING;
  479. n_stream->s = -1; /* not yet valid */
  480. n_stream->package_window = STREAMWINDOW_START;
  481. n_stream->deliver_window = STREAMWINDOW_START;
  482. if(connection_add(n_stream) < 0) { /* no space, forget it */
  483. log_fn(LOG_WARNING,"connection_add failed. Dropping.");
  484. connection_free(n_stream);
  485. return 0;
  486. }
  487. /* add it into the linked list of streams on this circuit */
  488. n_stream->next_stream = circ->n_streams;
  489. circ->n_streams = n_stream;
  490. /* send it off to the gethostbyname farm */
  491. switch(dns_resolve(n_stream)) {
  492. case 1: /* resolve worked */
  493. if(connection_exit_connect(n_stream) >= 0)
  494. return 0;
  495. /* else fall through */
  496. case -1: /* resolve failed */
  497. log_fn(LOG_WARNING,"Couldn't queue resolve request.");
  498. connection_remove(n_stream);
  499. connection_free(n_stream);
  500. case 0: /* resolve added to pending list */
  501. ;
  502. }
  503. return 0;
  504. }
  505. int connection_exit_connect(connection_t *conn) {
  506. if(router_compare_to_exit_policy(conn) < 0) {
  507. log_fn(LOG_INFO,"%s:%d failed exit policy. Closing.", conn->address, conn->port);
  508. return -1;
  509. }
  510. switch(connection_connect(conn, conn->address, conn->addr, conn->port)) {
  511. case -1:
  512. return -1;
  513. case 0:
  514. connection_set_poll_socket(conn);
  515. conn->state = EXIT_CONN_STATE_CONNECTING;
  516. connection_watch_events(conn, POLLOUT | POLLIN | POLLERR);
  517. /* writable indicates finish, readable indicates broken link,
  518. error indicates broken link in windowsland. */
  519. return 0;
  520. /* case 1: fall through */
  521. }
  522. connection_set_poll_socket(conn);
  523. conn->state = EXIT_CONN_STATE_OPEN;
  524. if(connection_wants_to_flush(conn)) { /* in case there are any queued data cells */
  525. log_fn(LOG_WARNING,"tell roger: newly connected conn had data waiting!");
  526. // connection_start_writing(conn);
  527. }
  528. // connection_process_inbuf(conn);
  529. connection_watch_events(conn, POLLIN);
  530. /* also, deliver a 'connected' cell back through the circuit. */
  531. connection_edge_send_command(conn, circuit_get_by_conn(conn), RELAY_COMMAND_CONNECTED,
  532. NULL, 0, conn->cpath_layer);
  533. return 0;
  534. }
  535. /*
  536. Local Variables:
  537. mode:c
  538. indent-tabs-mode:nil
  539. c-basic-offset:2
  540. End:
  541. */