connection_edge.c 25 KB

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