connection_exit.c 13 KB

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