connection_edge.c 22 KB

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