connection_edge.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  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_fn(LOG_WARNING,"(fd %d) circuit_deliver_relay_cell failed. Closing.", conn->s);
  37. circuit_close(circ);
  38. }
  39. return 0;
  40. #else
  41. /* eof reached, kill it. */
  42. log_fn(LOG_INFO,"conn (fd %d) reached eof. Closing.", conn->s);
  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_INFO,"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_WARNING,"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_WARNING,"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_WARNING,"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_WARNING,"relay begin request unsupported at AP. Dropping.");
  121. return 0;
  122. }
  123. if(conn) {
  124. log_fn(LOG_WARNING,"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_WARNING,"(relay data) 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_INFO,"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_WARNING,"(relay data) 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_INFO,"end cell dropped, unknown stream %d.",*(int*)conn->stream_id);
  157. return 0;
  158. }
  159. log_fn(LOG_INFO,"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_WARNING,"'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_WARNING,"'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_WARNING,"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_WARNING,"'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_WARNING,"'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_WARNING,"'connected' unsupported at exit. Dropping.");
  205. return 0;
  206. }
  207. if(!conn) {
  208. log_fn(LOG_INFO,"connected cell dropped, unknown stream %d.",*(int*)conn->stream_id);
  209. break;
  210. }
  211. log_fn(LOG_INFO,"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_WARNING,"unknown relay command %d.",relay_command);
  238. return -1;
  239. }
  240. return 0;
  241. }
  242. int connection_edge_finished_flushing(connection_t *conn) {
  243. int e, len=sizeof(e);
  244. assert(conn);
  245. assert(conn->type == CONN_TYPE_AP || conn->type == CONN_TYPE_EXIT);
  246. switch(conn->state) {
  247. case EXIT_CONN_STATE_CONNECTING:
  248. if (getsockopt(conn->s, SOL_SOCKET, SO_ERROR, (void*)&e, &len) < 0) { /* not yet */
  249. if(!ERRNO_CONN_EINPROGRESS(errno)) {
  250. /* yuck. kill it. */
  251. log_fn(LOG_DEBUG,"in-progress exit connect failed. Removing.");
  252. return -1;
  253. } else {
  254. log_fn(LOG_DEBUG,"in-progress exit connect still waiting.");
  255. return 0; /* no change, see if next time is better */
  256. }
  257. }
  258. /* the connect has finished. */
  259. log_fn(LOG_INFO,"Exit connection to %s:%u established.",
  260. conn->address,conn->port);
  261. conn->state = EXIT_CONN_STATE_OPEN;
  262. connection_watch_events(conn, POLLIN); /* stop writing, continue reading */
  263. if(connection_wants_to_flush(conn)) /* in case there are any queued relay cells */
  264. connection_start_writing(conn);
  265. return
  266. connection_edge_send_command(conn, circuit_get_by_conn(conn), RELAY_COMMAND_CONNECTED) || /* deliver a 'connected' relay cell back through the circuit. */
  267. connection_process_inbuf(conn); /* in case the server has written anything */
  268. case AP_CONN_STATE_OPEN:
  269. case EXIT_CONN_STATE_OPEN:
  270. connection_stop_writing(conn);
  271. return connection_consider_sending_sendme(conn, conn->type);
  272. default:
  273. log_fn(LOG_WARNING,"BUG: called in unexpected state.");
  274. return -1;
  275. }
  276. return 0;
  277. }
  278. int connection_package_raw_inbuf(connection_t *conn) {
  279. int amount_to_process;
  280. cell_t cell;
  281. circuit_t *circ;
  282. assert(conn);
  283. assert(!connection_speaks_cells(conn));
  284. repeat_connection_package_raw_inbuf:
  285. circ = circuit_get_by_conn(conn);
  286. if(!circ) {
  287. log_fn(LOG_INFO,"conn has no circuits! Closing.");
  288. return -1;
  289. }
  290. if(circuit_consider_stop_edge_reading(circ, conn->type, conn->cpath_layer))
  291. return 0;
  292. if(conn->package_window <= 0) {
  293. log_fn(LOG_WARNING,"called with package_window 0. Tell Roger.");
  294. connection_stop_reading(conn);
  295. return 0;
  296. }
  297. amount_to_process = buf_datalen(conn->inbuf);
  298. if(!amount_to_process)
  299. return 0;
  300. /* Initialize the cell with 0's */
  301. memset(&cell, 0, sizeof(cell_t));
  302. if(amount_to_process > CELL_PAYLOAD_SIZE - RELAY_HEADER_SIZE) {
  303. cell.length = CELL_PAYLOAD_SIZE - RELAY_HEADER_SIZE;
  304. } else {
  305. cell.length = amount_to_process;
  306. }
  307. connection_fetch_from_buf(cell.payload+RELAY_HEADER_SIZE, cell.length, conn);
  308. log_fn(LOG_DEBUG,"(%d) Packaging %d bytes (%d waiting).",conn->s,cell.length,
  309. (int)buf_datalen(conn->inbuf));
  310. cell.command = CELL_RELAY;
  311. SET_CELL_RELAY_COMMAND(cell, RELAY_COMMAND_DATA);
  312. SET_CELL_STREAM_ID(cell, conn->stream_id);
  313. cell.length += RELAY_HEADER_SIZE;
  314. if(conn->type == CONN_TYPE_EXIT) {
  315. cell.aci = circ->p_aci;
  316. if(circuit_deliver_relay_cell(&cell, circ, CELL_DIRECTION_IN, NULL) < 0) {
  317. log_fn(LOG_WARNING,"circuit_deliver_relay_cell (backward) failed. Closing.");
  318. circuit_close(circ);
  319. return 0;
  320. }
  321. assert(circ->package_window > 0);
  322. circ->package_window--;
  323. } else { /* send it forward. we're an AP */
  324. assert(conn->type == CONN_TYPE_AP);
  325. cell.aci = circ->n_aci;
  326. if(circuit_deliver_relay_cell(&cell, circ, CELL_DIRECTION_OUT, conn->cpath_layer) < 0) {
  327. log_fn(LOG_WARNING,"circuit_deliver_relay_cell (forward) failed. Closing.");
  328. circuit_close(circ);
  329. return 0;
  330. }
  331. assert(conn->cpath_layer->package_window > 0);
  332. conn->cpath_layer->package_window--;
  333. }
  334. assert(conn->package_window > 0);
  335. if(--conn->package_window <= 0) { /* is it 0 after decrement? */
  336. connection_stop_reading(conn);
  337. log_fn(LOG_DEBUG,"conn->package_window reached 0.");
  338. circuit_consider_stop_edge_reading(circ, conn->type, conn->cpath_layer);
  339. return 0; /* don't process the inbuf any more */
  340. }
  341. log_fn(LOG_DEBUG,"conn->package_window is now %d",conn->package_window);
  342. /* handle more if there's more, or return 0 if there isn't */
  343. goto repeat_connection_package_raw_inbuf;
  344. }
  345. int connection_consider_sending_sendme(connection_t *conn, int edge_type) {
  346. circuit_t *circ;
  347. cell_t cell;
  348. if(connection_outbuf_too_full(conn))
  349. return 0;
  350. circ = circuit_get_by_conn(conn);
  351. if(!circ) {
  352. /* this can legitimately happen if the destroy has already arrived and torn down the circuit */
  353. log_fn(LOG_INFO,"No circuit associated with conn. Skipping.");
  354. return 0;
  355. }
  356. memset(&cell, 0, sizeof(cell_t));
  357. cell.command = CELL_RELAY;
  358. SET_CELL_RELAY_COMMAND(cell, RELAY_COMMAND_SENDME);
  359. SET_CELL_STREAM_ID(cell, conn->stream_id);
  360. cell.length += RELAY_HEADER_SIZE;
  361. if(edge_type == EDGE_EXIT)
  362. cell.aci = circ->p_aci;
  363. else
  364. cell.aci = circ->n_aci;
  365. while(conn->deliver_window < STREAMWINDOW_START - STREAMWINDOW_INCREMENT) {
  366. log_fn(LOG_DEBUG,"Outbuf %d, Queueing stream sendme.", conn->outbuf_flushlen);
  367. conn->deliver_window += STREAMWINDOW_INCREMENT;
  368. if(circuit_deliver_relay_cell(&cell, circ, CELL_DIRECTION(edge_type), conn->cpath_layer) < 0) {
  369. log_fn(LOG_WARNING,"circuit_deliver_relay_cell failed. Closing.");
  370. circuit_close(circ);
  371. return 0;
  372. }
  373. }
  374. return 0;
  375. }
  376. static int connection_ap_handshake_process_socks(connection_t *conn) {
  377. circuit_t *circ;
  378. char destaddr[200]; /* XXX why 200? but not 256, because it won't fit in a cell */
  379. uint16_t destport;
  380. assert(conn);
  381. log_fn(LOG_DEBUG,"entered.");
  382. switch(fetch_from_buf_socks(conn->inbuf,
  383. destaddr, sizeof(destaddr), &destport)) {
  384. case -1:
  385. log_fn(LOG_WARNING,"Fetching socks handshake failed. Closing.");
  386. connection_ap_handshake_socks_reply(conn, SOCKS4_REQUEST_REJECT);
  387. return -1;
  388. case 0:
  389. log_fn(LOG_DEBUG,"Fetching socks handshake, not all here yet. Ignoring.");
  390. return 0;
  391. /* case 1, fall through */
  392. }
  393. /* find the circuit that we should use, if there is one. */
  394. circ = circuit_get_newest_open();
  395. if(!circ) {
  396. log_fn(LOG_INFO,"No circuit ready. Closing.");
  397. return -1;
  398. }
  399. circ->dirty = 1;
  400. /* add it into the linked list of streams on this circuit */
  401. log_fn(LOG_DEBUG,"attaching new conn to circ. n_aci %d.", circ->n_aci);
  402. conn->next_stream = circ->p_streams;
  403. circ->p_streams = conn;
  404. assert(circ->cpath && circ->cpath->prev);
  405. assert(circ->cpath->prev->state == CPATH_STATE_OPEN);
  406. conn->cpath_layer = circ->cpath->prev;
  407. if(connection_ap_handshake_send_begin(conn, circ, destaddr, destport) < 0) {
  408. circuit_close(circ);
  409. return -1;
  410. }
  411. return 0;
  412. }
  413. /* deliver the destaddr:destport in a relay cell */
  414. static int connection_ap_handshake_send_begin(connection_t *ap_conn, circuit_t *circ,
  415. char *destaddr, uint16_t destport) {
  416. cell_t cell;
  417. memset(&cell, 0, sizeof(cell_t));
  418. cell.command = CELL_RELAY;
  419. cell.aci = circ->n_aci;
  420. SET_CELL_RELAY_COMMAND(cell, RELAY_COMMAND_BEGIN);
  421. if(crypto_pseudo_rand(STREAM_ID_SIZE, ap_conn->stream_id) < 0)
  422. return -1;
  423. /* FIXME check for collisions */
  424. SET_CELL_STREAM_ID(cell, ZERO_STREAM);
  425. memcpy(cell.payload+RELAY_HEADER_SIZE, ap_conn->stream_id, STREAM_ID_SIZE);
  426. cell.length =
  427. snprintf(cell.payload+RELAY_HEADER_SIZE+STREAM_ID_SIZE, CELL_PAYLOAD_SIZE-RELAY_HEADER_SIZE-STREAM_ID_SIZE,
  428. "%s:%d", destaddr, destport) +
  429. 1 + STREAM_ID_SIZE + RELAY_HEADER_SIZE;
  430. log_fn(LOG_DEBUG,"Sending relay cell (id %d) to begin stream %d.", *(int *)(cell.payload+1),*(int *)ap_conn->stream_id);
  431. if(circuit_deliver_relay_cell(&cell, circ, CELL_DIRECTION_OUT, ap_conn->cpath_layer) < 0) {
  432. log_fn(LOG_WARNING,"failed to deliver begin cell. Closing.");
  433. return -1;
  434. }
  435. ap_conn->package_window = STREAMWINDOW_START;
  436. ap_conn->deliver_window = STREAMWINDOW_START;
  437. ap_conn->state = AP_CONN_STATE_OPEN;
  438. log_fn(LOG_INFO,"Address/port sent, ap socket %d, n_aci %d",ap_conn->s,circ->n_aci);
  439. return 0;
  440. }
  441. static int connection_ap_handshake_socks_reply(connection_t *conn, char result) {
  442. char buf[SOCKS4_NETWORK_LEN];
  443. assert(conn);
  444. /* an inlined socks4_pack() */
  445. memset(buf,0,sizeof(buf));
  446. buf[1] = result; /* command */
  447. /* leave version, destport, destip zero */
  448. if(connection_write_to_buf(buf, sizeof(buf), conn) < 0)
  449. return -1;
  450. return connection_flush_buf(conn); /* try to flush it, in case we're about to close the conn */
  451. }
  452. static int connection_exit_begin_conn(cell_t *cell, circuit_t *circ) {
  453. connection_t *n_stream;
  454. char *colon;
  455. if(!memchr(cell->payload+RELAY_HEADER_SIZE+STREAM_ID_SIZE,0,cell->length-RELAY_HEADER_SIZE-STREAM_ID_SIZE)) {
  456. log_fn(LOG_WARNING,"relay begin cell has no \\0. Dropping.");
  457. return 0;
  458. }
  459. colon = strchr(cell->payload+RELAY_HEADER_SIZE+STREAM_ID_SIZE, ':');
  460. if(!colon) {
  461. log_fn(LOG_WARNING,"relay begin cell has no colon. Dropping.");
  462. return 0;
  463. }
  464. *colon = 0;
  465. if(!atoi(colon+1)) { /* bad port */
  466. log_fn(LOG_WARNING,"relay begin cell has invalid port. Dropping.");
  467. return 0;
  468. }
  469. log_fn(LOG_DEBUG,"Creating new exit connection.");
  470. n_stream = connection_new(CONN_TYPE_EXIT);
  471. if(!n_stream) {
  472. log_fn(LOG_WARNING,"connection_new failed. Dropping.");
  473. return 0;
  474. }
  475. memcpy(n_stream->stream_id, cell->payload + RELAY_HEADER_SIZE, STREAM_ID_SIZE);
  476. n_stream->address = 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->receiver_bucket = -1; /* edge connections don't do receiver buckets */
  480. n_stream->bandwidth = -1;
  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_WARNING,"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_WARNING,"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_WARNING,"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. return connection_edge_send_command(conn, circuit_get_by_conn(conn), RELAY_COMMAND_CONNECTED);
  534. }
  535. /*
  536. Local Variables:
  537. mode:c
  538. indent-tabs-mode:nil
  539. c-basic-offset:2
  540. End:
  541. */