connection_edge.c 25 KB

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