connection_edge.c 22 KB

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