connection_exit.c 12 KB

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