connection_edge.c 21 KB

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