connection_edge.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /* Copyright 2001,2002 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. int connection_edge_process_inbuf(connection_t *conn) {
  7. assert(conn);
  8. assert(conn->type == CONN_TYPE_AP || 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_RELAY;
  22. cell.length = RELAY_HEADER_SIZE;
  23. SET_CELL_RELAY_COMMAND(cell, RELAY_COMMAND_END);
  24. SET_CELL_STREAM_ID(cell, conn->stream_id);
  25. cell.aci = circ->n_aci;
  26. if (circuit_deliver_relay_cell(&cell, circ, CELL_DIRECTION(conn->type), conn->cpath_layer) < 0) {
  27. log(LOG_DEBUG,"circuit_deliver_relay_cell failed. Closing.");
  28. circuit_close(circ);
  29. }
  30. return 0;
  31. #else
  32. /* eof reached, kill it. */
  33. log_fn(LOG_DEBUG,"conn reached eof. Closing.");
  34. return -1;
  35. #endif
  36. }
  37. switch(conn->state) {
  38. case AP_CONN_STATE_SOCKS_WAIT:
  39. return ap_handshake_process_socks(conn);
  40. case AP_CONN_STATE_OPEN:
  41. case EXIT_CONN_STATE_OPEN:
  42. if(connection_package_raw_inbuf(conn) < 0)
  43. return -1;
  44. return 0;
  45. case EXIT_CONN_STATE_CONNECTING:
  46. log_fn(LOG_DEBUG,"text from server while in 'connecting' state at exit. Leaving it on buffer.");
  47. return 0;
  48. }
  49. return 0;
  50. }
  51. int connection_edge_send_command(connection_t *fromconn, circuit_t *circ, int relay_command) {
  52. cell_t cell;
  53. int cell_direction;
  54. if(!circ) {
  55. log_fn(LOG_DEBUG,"no circ. Closing.");
  56. return -1;
  57. }
  58. memset(&cell, 0, sizeof(cell_t));
  59. if(fromconn && fromconn->type == CONN_TYPE_AP) {
  60. cell.aci = circ->n_aci;
  61. cell_direction = CELL_DIRECTION_OUT;
  62. } else {
  63. /* NOTE: if !fromconn, we assume that it's heading towards the OP */
  64. cell.aci = circ->p_aci;
  65. cell_direction = CELL_DIRECTION_IN;
  66. }
  67. cell.command = CELL_RELAY;
  68. SET_CELL_RELAY_COMMAND(cell, relay_command);
  69. if(fromconn)
  70. SET_CELL_STREAM_ID(cell, fromconn->stream_id);
  71. else
  72. SET_CELL_STREAM_ID(cell, ZERO_STREAM);
  73. cell.length = RELAY_HEADER_SIZE;
  74. log_fn(LOG_INFO,"delivering %d cell %s.", relay_command, cell_direction == CELL_DIRECTION_OUT ? "forward" : "backward");
  75. if(circuit_deliver_relay_cell(&cell, circ, cell_direction, fromconn ? fromconn->cpath_layer : NULL) < 0) {
  76. log_fn(LOG_DEBUG,"circuit_deliver_relay_cell failed. Closing.");
  77. circuit_close(circ);
  78. return 0;
  79. }
  80. return 0;
  81. }
  82. int connection_edge_process_relay_cell(cell_t *cell, circuit_t *circ, connection_t *conn,
  83. int edge_type, crypt_path_t *layer_hint) {
  84. int relay_command;
  85. static int num_seen=0;
  86. /* an incoming relay cell has arrived */
  87. assert(cell && circ);
  88. relay_command = CELL_RELAY_COMMAND(*cell);
  89. // log_fn(LOG_DEBUG,"command %d stream %d", relay_command, stream_id);
  90. num_seen++;
  91. log_fn(LOG_DEBUG,"Now seen %d relay cells here.", num_seen);
  92. /* either conn is NULL, in which case we've got a control cell, or else
  93. * conn points to the recognized stream. */
  94. if(conn && conn->state != AP_CONN_STATE_OPEN && conn->state != EXIT_CONN_STATE_OPEN) {
  95. if(conn->type == CONN_TYPE_EXIT && relay_command == RELAY_COMMAND_END) {
  96. log_fn(LOG_INFO,"Exit got end before we're connected. Marking for close.");
  97. conn->marked_for_close = 1;
  98. if(conn->state == EXIT_CONN_STATE_RESOLVING) {
  99. log_fn(LOG_INFO,"...and informing resolver we don't want the answer anymore.");
  100. dns_cancel_pending_resolve(conn->address, conn);
  101. }
  102. } else {
  103. log_fn(LOG_DEBUG,"Got an unexpected relay cell, not in 'open' state. Dropping.");
  104. }
  105. return 0;
  106. }
  107. switch(relay_command) {
  108. case RELAY_COMMAND_BEGIN:
  109. if(edge_type == EDGE_AP) {
  110. log_fn(LOG_INFO,"relay begin request unsupported. Dropping.");
  111. return 0;
  112. }
  113. if(conn) {
  114. log_fn(LOG_INFO,"begin cell for known stream. Dropping.");
  115. return 0;
  116. }
  117. return connection_exit_begin_conn(cell, circ);
  118. case RELAY_COMMAND_DATA:
  119. if((edge_type == EDGE_AP && --layer_hint->deliver_window < 0) ||
  120. (edge_type == EDGE_EXIT && --circ->deliver_window < 0)) {
  121. log_fn(LOG_DEBUG,"circ deliver_window below 0. Killing.");
  122. return -1; /* XXX kill the whole circ? */
  123. }
  124. log_fn(LOG_DEBUG,"circ deliver_window now %d.", edge_type == EDGE_AP ? layer_hint->deliver_window : circ->deliver_window);
  125. if(circuit_consider_sending_sendme(circ, edge_type, layer_hint) < 0)
  126. return -1;
  127. if(!conn) {
  128. log_fn(LOG_DEBUG,"relay cell dropped, unknown stream %d.",*(int*)conn->stream_id);
  129. return 0;
  130. }
  131. if(--conn->deliver_window < 0) { /* is it below 0 after decrement? */
  132. log_fn(LOG_DEBUG,"conn deliver_window below 0. Killing.");
  133. return -1; /* somebody's breaking protocol. kill the whole circuit. */
  134. }
  135. if(connection_write_to_buf(cell->payload + RELAY_HEADER_SIZE,
  136. cell->length - RELAY_HEADER_SIZE, conn) < 0) {
  137. conn->marked_for_close = 1;
  138. return 0;
  139. }
  140. if(connection_consider_sending_sendme(conn, edge_type) < 0)
  141. conn->marked_for_close = 1;
  142. return 0;
  143. case RELAY_COMMAND_END:
  144. if(!conn) {
  145. log_fn(LOG_DEBUG,"end cell dropped, unknown stream %d.",*(int*)conn->stream_id);
  146. return 0;
  147. }
  148. log_fn(LOG_DEBUG,"end cell for stream %d. Removing stream.",*(int*)conn->stream_id);
  149. #ifdef HALF_OPEN
  150. conn->done_sending = 1;
  151. shutdown(conn->s, 1); /* XXX check return; refactor NM */
  152. if (conn->done_receiving)
  153. conn->marked_for_close = 1;
  154. #endif
  155. conn->marked_for_close = 1;
  156. break;
  157. case RELAY_COMMAND_EXTEND:
  158. if(conn) {
  159. log_fn(LOG_INFO,"'extend' for non-zero stream. Dropping.");
  160. return 0;
  161. }
  162. return circuit_extend(cell, circ);
  163. case RELAY_COMMAND_EXTENDED:
  164. if(edge_type == EDGE_EXIT) {
  165. log_fn(LOG_INFO,"'extended' unsupported at exit. Dropping.");
  166. return 0;
  167. }
  168. log_fn(LOG_DEBUG,"Got an extended cell! Yay.");
  169. if(circuit_finish_handshake(circ, cell->payload+RELAY_HEADER_SIZE) < 0) {
  170. log_fn(LOG_INFO,"circuit_finish_handshake failed.");
  171. return -1;
  172. }
  173. return circuit_send_next_onion_skin(circ);
  174. case RELAY_COMMAND_TRUNCATE:
  175. if(edge_type == EDGE_AP) {
  176. log_fn(LOG_INFO,"'truncate' unsupported at AP. Dropping.");
  177. return 0;
  178. }
  179. if(circ->n_conn) {
  180. connection_send_destroy(circ->n_aci, circ->n_conn);
  181. circ->n_conn = NULL;
  182. }
  183. log_fn(LOG_DEBUG, "Processed 'truncate', replying.");
  184. return connection_edge_send_command(NULL, circ, RELAY_COMMAND_TRUNCATED);
  185. case RELAY_COMMAND_TRUNCATED:
  186. if(edge_type == EDGE_EXIT) {
  187. log_fn(LOG_INFO,"'truncated' unsupported at exit. Dropping.");
  188. return 0;
  189. }
  190. return circuit_truncated(circ, layer_hint);
  191. case RELAY_COMMAND_CONNECTED:
  192. if(edge_type == EDGE_EXIT) {
  193. log_fn(LOG_INFO,"'connected' unsupported at exit. Dropping.");
  194. return 0;
  195. }
  196. if(!conn) {
  197. log_fn(LOG_DEBUG,"connected cell dropped, unknown stream %d.",*(int*)conn->stream_id);
  198. break;
  199. }
  200. log_fn(LOG_DEBUG,"Connected! Notifying application.");
  201. if(ap_handshake_socks_reply(conn, SOCKS4_REQUEST_GRANTED) < 0) {
  202. conn->marked_for_close = 1;
  203. }
  204. break;
  205. case RELAY_COMMAND_SENDME:
  206. if(!conn) {
  207. if(edge_type == EDGE_AP) {
  208. assert(layer_hint);
  209. layer_hint->package_window += CIRCWINDOW_INCREMENT;
  210. log_fn(LOG_DEBUG,"circ-level sendme at AP, packagewindow %d.", layer_hint->package_window);
  211. circuit_resume_edge_reading(circ, EDGE_AP, layer_hint);
  212. } else {
  213. assert(!layer_hint);
  214. circ->package_window += CIRCWINDOW_INCREMENT;
  215. log_fn(LOG_DEBUG,"circ-level sendme at exit, packagewindow %d.", circ->package_window);
  216. circuit_resume_edge_reading(circ, EDGE_EXIT, layer_hint);
  217. }
  218. return 0;
  219. }
  220. conn->package_window += STREAMWINDOW_INCREMENT;
  221. log_fn(LOG_DEBUG,"stream-level sendme, packagewindow now %d.", conn->package_window);
  222. connection_start_reading(conn);
  223. connection_package_raw_inbuf(conn); /* handle whatever might still be on the inbuf */
  224. break;
  225. default:
  226. log_fn(LOG_DEBUG,"unknown relay command %d.",relay_command);
  227. }
  228. return 0;
  229. }
  230. int connection_edge_finished_flushing(connection_t *conn) {
  231. int e, len=sizeof(e);
  232. assert(conn);
  233. assert(conn->type == CONN_TYPE_AP || conn->type == CONN_TYPE_EXIT);
  234. switch(conn->state) {
  235. case EXIT_CONN_STATE_CONNECTING:
  236. if (getsockopt(conn->s, SOL_SOCKET, SO_ERROR, (void*)&e, &len) < 0) { /* not yet */
  237. if(errno != EINPROGRESS){
  238. /* yuck. kill it. */
  239. log_fn(LOG_DEBUG,"in-progress exit connect failed. Removing.");
  240. return -1;
  241. } else {
  242. log_fn(LOG_DEBUG,"in-progress exit connect still waiting.");
  243. return 0; /* no change, see if next time is better */
  244. }
  245. }
  246. /* the connect has finished. */
  247. log_fn(LOG_DEBUG,"Exit connection to %s:%u established.",
  248. conn->address,conn->port);
  249. conn->state = EXIT_CONN_STATE_OPEN;
  250. connection_watch_events(conn, POLLIN); /* stop writing, continue reading */
  251. if(connection_wants_to_flush(conn)) /* in case there are any queued relay cells */
  252. connection_start_writing(conn);
  253. return
  254. connection_edge_send_command(conn, circuit_get_by_conn(conn), RELAY_COMMAND_CONNECTED) || /* deliver a 'connected' relay cell back through the circuit. */
  255. connection_process_inbuf(conn); /* in case the server has written anything */
  256. case AP_CONN_STATE_OPEN:
  257. case EXIT_CONN_STATE_OPEN:
  258. connection_stop_writing(conn);
  259. return connection_consider_sending_sendme(conn, conn->type);
  260. default:
  261. log_fn(LOG_DEBUG,"BUG: called in unexpected state.");
  262. return 0;
  263. }
  264. return 0;
  265. }
  266. /*
  267. Local Variables:
  268. mode:c
  269. indent-tabs-mode:nil
  270. c-basic-offset:2
  271. End:
  272. */