connection_exit.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /* Copyright 2001,2002 Roger Dingledine, Matej Pfajfar. */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. #include "or.h"
  5. int connection_exit_process_inbuf(connection_t *conn) {
  6. circuit_t *circ;
  7. cell_t cell;
  8. assert(conn && conn->type == CONN_TYPE_EXIT);
  9. if(conn->inbuf_reached_eof) {
  10. #ifdef HALF_OPEN
  11. /* eof reached; we're done reading, but we might want to write more. */
  12. conn->done_receiving = 1;
  13. shutdown(conn->s, 0); /* XXX check return, refactor NM */
  14. if (conn->done_sending)
  15. conn->marked_for_close = 1;
  16. /* XXX Factor out common logic here and in circuit_about_to_close NM */
  17. circ = circuit_get_by_conn(conn);
  18. if (!circ)
  19. return -1;
  20. memset(&cell, 0, sizeof(cell_t));
  21. cell.command = CELL_DATA;
  22. cell.length = TOPIC_HEADER_SIZE;
  23. *(uint16_t *)(cell.payload+2) = htons(conn->topic_id);
  24. *cell.payload = TOPIC_COMMAND_END;
  25. cell.aci = circ->p_aci;
  26. if (circuit_deliver_data_cell_from_edge(&cell, circ, EDGE_EXIT) < 0) {
  27. log(LOG_DEBUG,"connection_exit_process_inbuf: circuit_deliver_data_cell_from_edge failed. Closing");
  28. circuit_close(circ);
  29. }
  30. return 0;
  31. #else
  32. /* eof reached, kill it. */
  33. log(LOG_DEBUG,"connection_exit_process_inbuf(): conn reached eof. Closing.");
  34. return -1;
  35. #endif
  36. }
  37. log(LOG_DEBUG,"connection_exit_process_inbuf(): state %d.",conn->state);
  38. switch(conn->state) {
  39. case EXIT_CONN_STATE_CONNECTING:
  40. log(LOG_DEBUG,"connection_exit_process_inbuf(): text from server while in 'connecting' state. Leaving it on buffer.");
  41. return 0;
  42. case EXIT_CONN_STATE_OPEN:
  43. if(connection_package_raw_inbuf(conn) < 0)
  44. return -1;
  45. circuit_consider_stop_edge_reading(circuit_get_by_conn(conn), EDGE_EXIT);
  46. return 0;
  47. }
  48. return 0;
  49. }
  50. int connection_exit_finished_flushing(connection_t *conn) {
  51. int e, len=sizeof(e);
  52. assert(conn && conn->type == CONN_TYPE_EXIT);
  53. switch(conn->state) {
  54. case EXIT_CONN_STATE_CONNECTING:
  55. if (getsockopt(conn->s, SOL_SOCKET, SO_ERROR, &e, &len) < 0) { /* not yet */
  56. if(errno != EINPROGRESS){
  57. /* yuck. kill it. */
  58. log(LOG_DEBUG,"connection_exit_finished_flushing(): in-progress connect failed. Removing.");
  59. return -1;
  60. } else {
  61. log(LOG_DEBUG,"connection_exit_finished_flushing(): in-progress connect still waiting.");
  62. return 0; /* no change, see if next time is better */
  63. }
  64. }
  65. /* the connect has finished. */
  66. log(LOG_DEBUG,"connection_exit_finished_flushing(): Connection to %s:%u established.",
  67. conn->address,conn->port);
  68. conn->state = EXIT_CONN_STATE_OPEN;
  69. connection_watch_events(conn, POLLIN); /* stop writing, continue reading */
  70. if(connection_wants_to_flush(conn)) /* in case there are any queued data cells */
  71. connection_start_writing(conn);
  72. return
  73. connection_exit_send_connected(conn) || /* deliver a 'connected' data cell back through the circuit. */
  74. connection_process_inbuf(conn); /* in case the server has written anything */
  75. case EXIT_CONN_STATE_OPEN:
  76. /* FIXME down the road, we'll clear out circuits that are pending to close */
  77. log(LOG_DEBUG,"connection_exit_finished_flushing(): finished flushing.");
  78. connection_stop_writing(conn);
  79. #ifdef USE_ZLIB
  80. if (connection_decompress_to_buf(NULL, 0, conn, Z_SYNC_FLUSH) < 0)
  81. return -1;
  82. #endif
  83. connection_consider_sending_sendme(conn, EDGE_EXIT);
  84. return 0;
  85. default:
  86. log(LOG_DEBUG,"Bug: connection_exit_finished_flushing() called in unexpected state.");
  87. return 0;
  88. }
  89. return 0;
  90. }
  91. int connection_exit_send_connected(connection_t *conn) {
  92. circuit_t *circ;
  93. cell_t cell;
  94. assert(conn);
  95. circ = circuit_get_by_conn(conn);
  96. if(!circ) {
  97. log(LOG_DEBUG,"connection_exit_send_connected(): client-side sent destroy just as we completed server connection. Closing.");
  98. return -1;
  99. }
  100. memset(&cell, 0, sizeof(cell_t));
  101. cell.aci = circ->p_aci;
  102. cell.command = CELL_DATA;
  103. *(uint16_t *)(cell.payload+2) = htons(conn->topic_id);
  104. *cell.payload = TOPIC_COMMAND_CONNECTED;
  105. cell.length = TOPIC_HEADER_SIZE;
  106. log(LOG_INFO,"connection_exit_send_connected(): passing back cell (aci %d).",circ->p_aci);
  107. if(circuit_deliver_data_cell_from_edge(&cell, circ, EDGE_EXIT) < 0) {
  108. log(LOG_DEBUG,"connection_exit_send_connected(): circuit_deliver_data_cell (backward) failed. Closing.");
  109. circuit_close(circ);
  110. return 0;
  111. }
  112. return 0;
  113. }
  114. int connection_exit_begin_conn(cell_t *cell, circuit_t *circ) {
  115. connection_t *n_conn;
  116. char *colon;
  117. if(!memchr(cell->payload + TOPIC_HEADER_SIZE,0,cell->length - TOPIC_HEADER_SIZE)) {
  118. log(LOG_WARNING,"connection_exit_begin_conn(): topic begin cell has no \\0. Dropping.");
  119. return 0;
  120. }
  121. colon = strchr(cell->payload + TOPIC_HEADER_SIZE, ':');
  122. if(!colon) {
  123. log(LOG_WARNING,"connection_exit_begin_conn(): topic begin cell has no colon. Dropping.");
  124. return 0;
  125. }
  126. *colon = 0;
  127. if(!atoi(colon+1)) { /* bad port */
  128. log(LOG_DEBUG,"connection_exit_begin_conn(): topic begin cell has invalid port. Dropping.");
  129. return 0;
  130. }
  131. log(LOG_DEBUG,"connection_exit_begin_conn(): Creating new exit connection.");
  132. n_conn = connection_new(CONN_TYPE_EXIT);
  133. if(!n_conn) {
  134. log(LOG_DEBUG,"connection_exit_begin_conn(): connection_new failed. Dropping.");
  135. return 0;
  136. }
  137. cell->payload[0] = 0;
  138. n_conn->topic_id = ntohs(*(uint16_t *)(cell->payload+2));
  139. n_conn->address = strdup(cell->payload + TOPIC_HEADER_SIZE);
  140. n_conn->port = atoi(colon+1);
  141. n_conn->state = EXIT_CONN_STATE_RESOLVING;
  142. n_conn->receiver_bucket = -1; /* edge connections don't do receiver buckets */
  143. n_conn->bandwidth = -1;
  144. n_conn->s = -1; /* not yet valid */
  145. n_conn->n_receive_topicwindow = TOPICWINDOW_START;
  146. n_conn->p_receive_topicwindow = TOPICWINDOW_START;
  147. if(connection_add(n_conn) < 0) { /* no space, forget it */
  148. log(LOG_DEBUG,"connection_exit_begin_conn(): connection_add failed. Dropping.");
  149. connection_free(n_conn);
  150. return 0;
  151. }
  152. /* add it into the linked list of topics on this circuit */
  153. n_conn->next_topic = circ->n_conn;
  154. circ->n_conn = n_conn;
  155. /* send it off to the gethostbyname farm */
  156. if(dns_resolve(n_conn) < 0) {
  157. log(LOG_DEBUG,"connection_exit_begin_conn(): Couldn't queue resolve request.");
  158. connection_remove(n_conn);
  159. connection_free(n_conn);
  160. return 0;
  161. }
  162. return 0;
  163. }
  164. int connection_exit_process_data_cell(cell_t *cell, circuit_t *circ) {
  165. connection_t *conn;
  166. int topic_command;
  167. int topic_id;
  168. int len;
  169. static int num_seen=0;
  170. /* an outgoing data cell has arrived */
  171. assert(cell && circ);
  172. topic_command = *cell->payload;
  173. topic_id = ntohs(*(uint16_t *)(cell->payload+2));
  174. log(LOG_DEBUG,"connection_exit_process_data_cell(): command %d topic %d", topic_command, topic_id);
  175. num_seen++;
  176. log(LOG_DEBUG,"connection_exit_process_data_cell(): Now seen %d data cells here.", num_seen);
  177. circuit_consider_sending_sendme(circ, EDGE_EXIT);
  178. for(conn = circ->n_conn; conn && conn->topic_id != topic_id; conn = conn->next_topic) ;
  179. /* now conn is either NULL, in which case we don't recognize the topic_id, or
  180. * it is set, in which case cell is talking about this conn.
  181. */
  182. if(conn && conn->state != EXIT_CONN_STATE_OPEN) {
  183. if(topic_command == TOPIC_COMMAND_END) {
  184. log(LOG_ERR,"connection_exit_process_data_cell(): Got an end before we're connected. Marking for close.");
  185. conn->marked_for_close = 1;
  186. return 0;
  187. } else {
  188. log(LOG_INFO,"connection_exit_process_data_cell(): Got a non-end data cell when not in 'open' state. Dropping.");
  189. return 0;
  190. }
  191. }
  192. switch(topic_command) {
  193. case TOPIC_COMMAND_BEGIN:
  194. if(conn) {
  195. log(LOG_INFO,"connection_exit_process_data_cell(): begin cell for known topic. Dropping.");
  196. return 0;
  197. }
  198. return connection_exit_begin_conn(cell, circ);
  199. case TOPIC_COMMAND_DATA:
  200. if(!conn) {
  201. log(LOG_INFO,"connection_exit_process_data_cell(): data cell for unknown topic. Dropping.");
  202. return 0;
  203. }
  204. if(--conn->p_receive_topicwindow < 0) { /* is it below 0 after decrement? */
  205. log(LOG_DEBUG,"connection_exit_process_data_cell(): receive_topicwindow at exit below 0. Killing.");
  206. return -1; /* AP breaking protocol. kill the whole circuit. */
  207. }
  208. log(LOG_DEBUG,"connection_exit_process_data_cell(): willing to receive %d more cells from circ",conn->p_receive_topicwindow);
  209. if(conn->state != EXIT_CONN_STATE_OPEN) {
  210. log(LOG_DEBUG,"connection_exit_process_data_cell(): data received while resolving/connecting. Queueing.");
  211. }
  212. #ifdef USE_ZLIB
  213. log(LOG_DEBUG,"connection_exit_process_data_cell(): uncompressing %d bytes onto outbuf...",cell->length - TOPIC_HEADER_SIZE);
  214. len = connection_decompress_to_buf(cell->payload + TOPIC_HEADER_SIZE,
  215. cell->length - TOPIC_HEADER_SIZE,
  216. conn, Z_SYNC_FLUSH);
  217. log(LOG_DEBUG,"%d bytes written", len);
  218. if (len<0) {
  219. log(LOG_INFO,"connection_exit_process_data_cell(): write to buf failed. Marking for close.");
  220. conn->marked_for_close = 1;
  221. return 0;
  222. }
  223. #else
  224. log(LOG_DEBUG,"connection_exit_process_data_cell(): put %d bytes on outbuf.",cell->length - TOPIC_HEADER_SIZE);
  225. if(connection_write_to_buf(cell->payload + TOPIC_HEADER_SIZE,
  226. cell->length - TOPIC_HEADER_SIZE, conn) < 0) {
  227. log(LOG_INFO,"connection_exit_process_data_cell(): write to buf failed. Marking for close.");
  228. conn->marked_for_close = 1;
  229. return 0;
  230. }
  231. #endif
  232. if(connection_consider_sending_sendme(conn, EDGE_EXIT) < 0)
  233. conn->marked_for_close = 1;
  234. return 0;
  235. case TOPIC_COMMAND_END:
  236. if(!conn) {
  237. log(LOG_DEBUG,"connection_exit_process_data_cell(): end cell dropped, unknown topic %d.",topic_id);
  238. return 0;
  239. }
  240. log(LOG_DEBUG,"connection_exit_process_data_cell(): end cell for topic %d. Removing topic.",topic_id);
  241. #if 0
  242. /* go through and identify who points to conn. remove conn from the list. */
  243. if(conn == circ->n_conn) {
  244. circ->n_conn = conn->next_topic;
  245. }
  246. for(prevconn = circ->n_conn; prevconn->next_topic != conn; prevconn = prevconn->next_topic) ;
  247. prevconn->next_topic = conn->next_topic;
  248. #endif
  249. #ifdef HALF_OPEN
  250. conn->done_sending = 1;
  251. shutdown(conn->s, 1); /* XXX check return; refactor NM */
  252. if (conn->done_receiving)
  253. conn->marked_for_close = 1;
  254. #endif
  255. conn->marked_for_close = 1;
  256. break;
  257. case TOPIC_COMMAND_CONNECTED:
  258. log(LOG_INFO,"connection_exit_process_data_cell(): topic connected request unsupported. Dropping.");
  259. break;
  260. case TOPIC_COMMAND_SENDME:
  261. if(!conn) {
  262. log(LOG_DEBUG,"connection_exit_process_data_cell(): sendme cell dropped, unknown topic %d.",topic_id);
  263. return 0;
  264. }
  265. conn->n_receive_topicwindow += TOPICWINDOW_INCREMENT;
  266. connection_start_reading(conn);
  267. connection_package_raw_inbuf(conn); /* handle whatever might still be on the inbuf */
  268. circuit_consider_stop_edge_reading(circ, EDGE_EXIT);
  269. break;
  270. default:
  271. log(LOG_DEBUG,"connection_exit_process_data_cell(): unknown topic command %d.",topic_command);
  272. }
  273. return 0;
  274. }
  275. #if 0
  276. static uint32_t address_to_addr(char *address) {
  277. struct hostent *rent;
  278. uint32_t addr;
  279. char *caddr;
  280. rent = gethostbyname(address);
  281. if (!rent) {
  282. log(LOG_ERR,"address_to_addr(): Could not resolve dest addr %s.",address);
  283. return 0;
  284. }
  285. memcpy(&addr, rent->h_addr,rent->h_length);
  286. addr = ntohl(addr); /* get it back to host order */
  287. caddr = (char *)&addr;
  288. log(LOG_DEBUG,"address_to_addr(): addr is %d %d %d %d",
  289. caddr[0], caddr[1], caddr[2], caddr[3]);
  290. return addr;
  291. }
  292. #endif
  293. int connection_exit_connect(connection_t *conn) {
  294. int s; /* for the new socket */
  295. struct sockaddr_in dest_addr;
  296. /* all the necessary info is here. Start the connect() */
  297. s=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
  298. if (s < 0) {
  299. log(LOG_ERR,"connection_exit_connect(): Error creating network socket.");
  300. return -1;
  301. }
  302. fcntl(s, F_SETFL, O_NONBLOCK); /* set s to non-blocking */
  303. memset((void *)&dest_addr,0,sizeof(dest_addr));
  304. dest_addr.sin_family = AF_INET;
  305. dest_addr.sin_port = htons(conn->port);
  306. dest_addr.sin_addr.s_addr = htonl(conn->addr);
  307. log(LOG_DEBUG,"connection_exit_connect(): Connecting to %s:%u.",conn->address,conn->port);
  308. if(connect(s,(struct sockaddr *)&dest_addr,sizeof(dest_addr)) < 0) {
  309. if(errno != EINPROGRESS){
  310. /* yuck. kill it. */
  311. perror("connect");
  312. log(LOG_DEBUG,"connection_exit_connect(): Connect failed.");
  313. return -1;
  314. } else {
  315. /* it's in progress. set state appropriately and return. */
  316. conn->s = s;
  317. connection_set_poll_socket(conn);
  318. conn->state = EXIT_CONN_STATE_CONNECTING;
  319. log(LOG_DEBUG,"connection_exit_connect(): connect in progress, socket %d.",s);
  320. connection_watch_events(conn, POLLOUT | POLLIN);
  321. return 0;
  322. }
  323. }
  324. /* it succeeded. we're connected. */
  325. log(LOG_DEBUG,"connection_exit_connect(): Connection to %s:%u established.",conn->address,conn->port);
  326. conn->s = s;
  327. connection_set_poll_socket(conn);
  328. conn->state = EXIT_CONN_STATE_OPEN;
  329. if(connection_wants_to_flush(conn)) { /* in case there are any queued data cells */
  330. log(LOG_ERR,"connection_exit_connect(): tell roger: newly connected conn had data waiting!");
  331. // connection_start_writing(conn);
  332. }
  333. // connection_process_inbuf(conn);
  334. connection_watch_events(conn, POLLIN);
  335. /* also, deliver a 'connected' cell back through the circuit. */
  336. return connection_exit_send_connected(conn);
  337. }
  338. /*
  339. Local Variables:
  340. mode:c
  341. indent-tabs-mode:nil
  342. c-basic-offset:2
  343. End:
  344. */