[or-cvs] add an ap_conn connecting state; report connecting delay

Roger Dingledine arma at seul.org
Tue Jan 20 09:21:48 UTC 2004


Update of /home/or/cvsroot/src/or
In directory moria.mit.edu:/home2/arma/work/onion/cvs/src/or

Modified Files:
	circuit.c connection.c connection_edge.c main.c or.h 
Log Message:
add an ap_conn connecting state; report connecting delay


Index: circuit.c
===================================================================
RCS file: /home/or/cvsroot/src/or/circuit.c,v
retrieving revision 1.131
retrieving revision 1.132
diff -u -d -r1.131 -r1.132
--- circuit.c	2 Jan 2004 09:03:38 -0000	1.131
+++ circuit.c	20 Jan 2004 09:21:45 -0000	1.132
@@ -736,8 +736,28 @@
   } /* end switch */
 }
 
-void circuit_dump_details(int severity, circuit_t *circ, int poll_index,
-                          char *type, int this_circid, int other_circid) {
+void circuit_log_path(int severity, circuit_t *circ) {
+  static char b[1024];
+  struct crypt_path_t *hop;
+  routerinfo_t *router;
+  assert(circ->cpath);
+  strcpy(b,"Stream is on circ: ");
+  for(hop=circ->cpath;hop->next != circ->cpath; hop=hop->next) {
+    router = router_get_by_addr_port(hop->addr,hop->port);
+    if(router) {
+      /* XXX strcat causes buffer overflow */
+      strcat(b,router->nickname);
+      strcat(b,",");
+    } else {
+      strcat(b,"UNKNOWN,");
+    }
+  }
+  log_fn(severity,"%s",b);
+}
+
+static void
+circuit_dump_details(int severity, circuit_t *circ, int poll_index,
+                     char *type, int this_circid, int other_circid) {
   struct crypt_path_t *hop;
   log(severity,"Conn %d has %s circuit: circID %d (other side %d), state %d (%s), born %d",
       poll_index, type, this_circid, other_circid, circ->state,

Index: connection.c
===================================================================
RCS file: /home/or/cvsroot/src/or/connection.c,v
retrieving revision 1.147
retrieving revision 1.148
diff -u -d -r1.147 -r1.148
--- connection.c	20 Jan 2004 02:14:51 -0000	1.147
+++ connection.c	20 Jan 2004 09:21:46 -0000	1.148
@@ -45,7 +45,8 @@
     "", /* 3 */
     "awaiting dest info",         /* app, 4 */
     "waiting for safe circuit",        /* 5 */
-    "open" },                          /* 6 */
+    "waiting for connected",           /* 6 */
+    "open" },                          /* 7 */
   { "ready" }, /* dir listener, 0 */
   { "",                           /* dir, 0 */
     "connecting (fetch)",              /* 1 */

Index: connection_edge.c
===================================================================
RCS file: /home/or/cvsroot/src/or/connection_edge.c,v
retrieving revision 1.87
retrieving revision 1.88
diff -u -d -r1.87 -r1.88
--- connection_edge.c	19 Jan 2004 04:58:39 -0000	1.87
+++ connection_edge.c	20 Jan 2004 09:21:46 -0000	1.88
@@ -87,8 +87,8 @@
       log_fn(LOG_INFO,"text from server while in 'connecting' state at exit. Leaving it on buffer.");
       return 0;
   }
-
-  return 0;
+  log_fn(LOG_WARN,"Got unexpected state %d. Closing.",conn->state);
+  return -1;
 }
 
 static char *connection_edge_end_reason(char *payload, uint16_t length) {
@@ -213,6 +213,27 @@
       conn->marked_for_close = 1;
       conn->has_sent_end = 1;
       return 0;
+    }
+    if(conn->type == CONN_TYPE_AP && rh.command == RELAY_COMMAND_CONNECTED) {
+      if(conn->state != AP_CONN_STATE_CONNECTING) {
+        log_fn(LOG_WARN,"Got 'connected' while not in state connecting. Dropping.");
+        return 0;
+      }
+      log_fn(LOG_INFO,"Connected! Notifying application.");
+      conn->state = AP_CONN_STATE_OPEN;
+      if (rh.length >= 4) {
+        addr = ntohl(*(uint32_t*)(cell->payload + RELAY_HEADER_SIZE));
+        client_dns_set_entry(conn->socks_request->address, addr);
+      }
+      log_fn(LOG_WARN,"'connected' received after %d seconds.",
+             (int)(time(NULL) - conn->timestamp_lastread));
+      circuit_log_path(LOG_WARN,circ);
+      if(connection_ap_handshake_socks_reply(conn, NULL, 0, 1) < 0) {
+        log_fn(LOG_INFO,"Writing to socks-speaking application failed. Closing.");
+        if(connection_edge_end(conn, END_STREAM_REASON_MISC, conn->cpath_layer) < 0)
+          log_fn(LOG_WARN,"3: I called connection_edge_end redundantly.");
+      }
+      return 0;
     } else {
       log_fn(LOG_WARN,"Got an unexpected relay command %d, in state %d (%s). Closing.",
              rh.command, conn->state, conn_state_to_string[conn->type][conn->state]);
@@ -351,25 +372,8 @@
       circuit_truncated(circ, layer_hint);
       return 0;
     case RELAY_COMMAND_CONNECTED:
-      if(edge_type == EDGE_EXIT) {
-        log_fn(LOG_WARN,"'connected' unsupported at exit. Dropping.");
-        return 0;
-      }
-      if(!conn) {
-        log_fn(LOG_INFO,"connected cell dropped, unknown stream.");
-        return 0;
-      }
-      log_fn(LOG_INFO,"Connected! Notifying application.");
-      if (rh.length >= 4) {
-        addr = ntohl(*(uint32_t*)(cell->payload + RELAY_HEADER_SIZE));
-        client_dns_set_entry(conn->socks_request->address, addr);
-      }
-      if(connection_ap_handshake_socks_reply(conn, NULL, 0, 1) < 0) {
-        log_fn(LOG_INFO,"Writing to socks-speaking application failed. Closing.");
-        if(connection_edge_end(conn, END_STREAM_REASON_MISC, conn->cpath_layer) < 0)
-          log_fn(LOG_WARN,"3: I called connection_edge_end redundantly.");
-      }
-      return 0;
+      log_fn(LOG_WARN,"'connected' unsupported while open. Closing.");
+      return -1;
     case RELAY_COMMAND_SENDME:
       if(!conn) {
         if(edge_type == EDGE_AP) {
@@ -521,6 +525,28 @@
   goto repeat_connection_edge_package_raw_inbuf;
 }
 
+void connection_ap_expire_beginning(void) {
+  connection_t **carray;
+  connection_t *conn;
+  int n, i;
+  time_t now = time(NULL);
+
+  get_connection_array(&carray, &n);
+
+  for (i = 0; i < n; ++i) {
+    conn = carray[i];
+    if (conn->type != CONN_TYPE_AP ||
+        conn->state != AP_CONN_STATE_CONNECTING)
+      continue;
+    if (now - conn->timestamp_lastread > 30) {
+      log_fn(LOG_WARN,"Stream is %d seconds late. Closing.",
+             (int)(now - conn->timestamp_lastread));
+      /* XXX here is where it should move back into 'pending' state */
+      conn->marked_for_close = 1;
+    }
+  }
+}
+
 /* Tell any APs that are waiting for a new circuit that one is available */
 void connection_ap_attach_pending(void)
 {
@@ -649,6 +675,9 @@
 
   connection_start_reading(conn);
 
+  /* here, print the circ's path. so people can figure out which circs are sucking. */
+  circuit_log_path(LOG_WARN,circ);
+
   if(!circ->timestamp_dirty)
     circ->timestamp_dirty = time(NULL);
 
@@ -725,12 +754,11 @@
 
   ap_conn->package_window = STREAMWINDOW_START;
   ap_conn->deliver_window = STREAMWINDOW_START;
-  ap_conn->state = AP_CONN_STATE_OPEN;
+  ap_conn->state = AP_CONN_STATE_CONNECTING;
   /* XXX Right now, we rely on the socks client not to send us any data
    * XXX until we've sent back a socks reply.  (If it does, we could wind
    * XXX up packaging that data and sending it to the exit, then later having
    * XXX the exit refuse us.)
-   * XXX Perhaps we should grow an AP_CONN_STATE_CONNECTING state.
    */
   log_fn(LOG_INFO,"Address/port sent, ap socket %d, n_circ_id %d",ap_conn->s,circ->n_circ_id);
   return;

Index: main.c
===================================================================
RCS file: /home/or/cvsroot/src/or/main.c,v
retrieving revision 1.165
retrieving revision 1.166
diff -u -d -r1.165 -r1.166
--- main.c	19 Jan 2004 06:12:32 -0000	1.165
+++ main.c	20 Jan 2004 09:21:46 -0000	1.166
@@ -313,6 +313,13 @@
    */
   circuit_expire_building();
 
+  /* 2b. Also look at pending streams and prune the ones that 'began'
+   *     a long time ago but haven't gotten a 'connected' yet.
+   *     Do this before step 3, so we can put them back into pending
+   *     state to be picked up by the new circuit.
+   */
+  connection_ap_expire_beginning();
+
   /* 3. Every second, we try a new circuit if there are no valid
    *    circuits. Every NewCircuitPeriod seconds, we expire circuits
    *    that became dirty more than NewCircuitPeriod seconds ago,

Index: or.h
===================================================================
RCS file: /home/or/cvsroot/src/or/or.h,v
retrieving revision 1.220
retrieving revision 1.221
diff -u -d -r1.220 -r1.221
--- or.h	20 Jan 2004 02:14:30 -0000	1.220
+++ or.h	20 Jan 2004 09:21:46 -0000	1.221
@@ -164,8 +164,9 @@
 #define _AP_CONN_STATE_MIN 4
 #define AP_CONN_STATE_SOCKS_WAIT 4
 #define AP_CONN_STATE_CIRCUIT_WAIT 5
-#define AP_CONN_STATE_OPEN 6
-#define _AP_CONN_STATE_MAX 6
+#define AP_CONN_STATE_CONNECTING 6
+#define AP_CONN_STATE_OPEN 7
+#define _AP_CONN_STATE_MAX 7
 
 #define _DIR_CONN_STATE_MIN 1
 #define DIR_CONN_STATE_CONNECTING_FETCH 1
@@ -589,6 +590,7 @@
 void circuit_close(circuit_t *circ);
 void circuit_about_to_close_connection(connection_t *conn);
 
+void circuit_log_path(int severity, circuit_t *circ);
 void circuit_dump_by_conn(connection_t *conn, int severity);
 
 void circuit_expire_unused_circuits(void);
@@ -687,6 +689,7 @@
 
 void connection_exit_connect(connection_t *conn);
 int connection_ap_can_use_exit(connection_t *conn, routerinfo_t *exit);
+void connection_ap_expire_beginning(void);
 void connection_ap_attach_pending(void);
 
 extern uint64_t stats_n_data_cells_packaged;



More information about the tor-commits mailing list