Commit 310a8da0 authored by Roger Dingledine's avatar Roger Dingledine
Browse files

break reached_eof() out of process_inbuf()


svn:r2930
parent 671d84dc
Loading
Loading
Loading
Loading
+29 −3
Changes for src/or/connection.c: 29 added lines, 3 removed lines.
Original line number Original line Diff line number Diff line
@@ -87,6 +87,7 @@ static int connection_handle_listener_read(connection_t *conn, int new_type);
static int connection_receiver_bucket_should_increase(connection_t *conn);
static int connection_receiver_bucket_should_increase(connection_t *conn);
static int connection_finished_flushing(connection_t *conn);
static int connection_finished_flushing(connection_t *conn);
static int connection_finished_connecting(connection_t *conn);
static int connection_finished_connecting(connection_t *conn);
static int connection_reached_eof(connection_t *conn);
static int connection_read_to_buf(connection_t *conn, int *max_to_read);
static int connection_read_to_buf(connection_t *conn, int *max_to_read);
static int connection_process_inbuf(connection_t *conn, int package_partial);
static int connection_process_inbuf(connection_t *conn, int package_partial);
static int connection_bucket_read_limit(connection_t *conn);
static int connection_bucket_read_limit(connection_t *conn);
@@ -856,6 +857,9 @@ loop_again:
  if (connection_process_inbuf(conn, 1) < 0) {
  if (connection_process_inbuf(conn, 1) < 0) {
    return -1;
    return -1;
  }
  }
  if (conn->inbuf_reached_eof && connection_reached_eof(conn) < 0) {
    return -1;
  }
  return 0;
  return 0;
}
}


@@ -1294,7 +1298,7 @@ static int connection_process_inbuf(connection_t *conn, int package_partial) {
    case CONN_TYPE_CONTROL:
    case CONN_TYPE_CONTROL:
      return connection_control_process_inbuf(conn);
      return connection_control_process_inbuf(conn);
    default:
    default:
      log_fn(LOG_WARN,"got unexpected conn->type %d.", conn->type);
      log_fn(LOG_WARN,"got unexpected conn type %d.", conn->type);
      return -1;
      return -1;
  }
  }
}
}
@@ -1326,7 +1330,7 @@ static int connection_finished_flushing(connection_t *conn) {
    case CONN_TYPE_CONTROL:
    case CONN_TYPE_CONTROL:
      return connection_control_finished_flushing(conn);
      return connection_control_finished_flushing(conn);
    default:
    default:
      log_fn(LOG_WARN,"got unexpected conn->type %d.", conn->type);
      log_fn(LOG_WARN,"got unexpected conn type %d.", conn->type);
      return -1;
      return -1;
  }
  }
}
}
@@ -1349,7 +1353,29 @@ static int connection_finished_connecting(connection_t *conn)
    case CONN_TYPE_DIR:
    case CONN_TYPE_DIR:
      return connection_dir_finished_connecting(conn);
      return connection_dir_finished_connecting(conn);
    default:
    default:
      tor_assert(0);
      log_fn(LOG_WARN,"got unexpected conn type %d.", conn->type);
      return -1;
  }
}

static int connection_reached_eof(connection_t *conn)
{
  switch (conn->type) {
    case CONN_TYPE_OR:
      return connection_or_reached_eof(conn);
    case CONN_TYPE_AP:
    case CONN_TYPE_EXIT:
      return connection_edge_reached_eof(conn);
    case CONN_TYPE_DIR:
      return connection_dir_reached_eof(conn);
    case CONN_TYPE_DNSWORKER:
      return connection_dns_reached_eof(conn);
    case CONN_TYPE_CPUWORKER:
      return connection_cpu_reached_eof(conn);
    case CONN_TYPE_CONTROL:
      return connection_control_reached_eof(conn);
    default:
      log_fn(LOG_WARN,"got unexpected conn type %d.", conn->type);
      return -1;
      return -1;
  }
  }
}
}
+16 −19
Changes for src/or/connection_edge.c: 16 added lines, 19 removed lines.
Original line number Original line Diff line number Diff line
@@ -18,26 +18,9 @@ static smartlist_t *redirect_exit_list = NULL;


static int connection_ap_handshake_process_socks(connection_t *conn);
static int connection_ap_handshake_process_socks(connection_t *conn);


/** Handle new bytes on conn->inbuf, or notification of eof.
/** There was an EOF. Send an end and mark the connection for close.
 *
 * If there was an EOF, then send an end and mark the connection
 * for close.
 *
 * Otherwise handle it based on state:
 *   - If it's waiting for socks info, try to read another step of the
 *     socks handshake out of conn->inbuf.
 *   - If it's open, then package more relay cells from the stream.
 *   - Else, leave the bytes on inbuf alone for now.
 *
 * Mark and return -1 if there was an unexpected error with the conn,
 * else return 0.
 */
 */
int connection_edge_process_inbuf(connection_t *conn, int package_partial) {
int connection_edge_reached_eof(connection_t *conn) {

  tor_assert(conn);
  tor_assert(conn->type == CONN_TYPE_AP || conn->type == CONN_TYPE_EXIT);

  if(conn->inbuf_reached_eof) {
#ifdef HALF_OPEN
#ifdef HALF_OPEN
  /* eof reached; we're done reading, but we might want to write more. */
  /* eof reached; we're done reading, but we might want to write more. */
  conn->done_receiving = 1;
  conn->done_receiving = 1;
@@ -65,6 +48,20 @@ int connection_edge_process_inbuf(connection_t *conn, int package_partial) {
#endif
#endif
}
}


/** Handle new bytes on conn->inbuf based on state:
 *   - If it's waiting for socks info, try to read another step of the
 *     socks handshake out of conn->inbuf.
 *   - If it's open, then package more relay cells from the stream.
 *   - Else, leave the bytes on inbuf alone for now.
 *
 * Mark and return -1 if there was an unexpected error with the conn,
 * else return 0.
 */
int connection_edge_process_inbuf(connection_t *conn, int package_partial) {

  tor_assert(conn);
  tor_assert(conn->type == CONN_TYPE_AP || conn->type == CONN_TYPE_EXIT);

  switch(conn->state) {
  switch(conn->state) {
    case AP_CONN_STATE_SOCKS_WAIT:
    case AP_CONN_STATE_SOCKS_WAIT:
      if(connection_ap_handshake_process_socks(conn) < 0) {
      if(connection_ap_handshake_process_socks(conn) < 0) {
+6 −6
Changes for src/or/connection_or.c: 6 added lines, 6 removed lines.
Original line number Original line Diff line number Diff line
@@ -40,6 +40,12 @@ static void cell_unpack(cell_t *dest, const char *src) {
  memcpy(dest->payload, src+3, CELL_PAYLOAD_SIZE);
  memcpy(dest->payload, src+3, CELL_PAYLOAD_SIZE);
}
}


int connection_or_reached_eof(connection_t *conn) {
  log_fn(LOG_INFO,"OR connection reached EOF. Closing.");
  connection_mark_for_close(conn);
  return 0;
}

/** Handle any new bytes that have come in on connection <b>conn</b>.
/** Handle any new bytes that have come in on connection <b>conn</b>.
 * If conn is in 'open' state, hand it to
 * If conn is in 'open' state, hand it to
 * connection_or_process_cells_from_inbuf()
 * connection_or_process_cells_from_inbuf()
@@ -50,12 +56,6 @@ int connection_or_process_inbuf(connection_t *conn) {
  tor_assert(conn);
  tor_assert(conn);
  tor_assert(conn->type == CONN_TYPE_OR);
  tor_assert(conn->type == CONN_TYPE_OR);


  if(conn->inbuf_reached_eof) {
    log_fn(LOG_INFO,"OR connection reached EOF. Closing.");
    connection_mark_for_close(conn);
    return 0;
  }

  if(conn->state != OR_CONN_STATE_OPEN)
  if(conn->state != OR_CONN_STATE_OPEN)
    return 0; /* don't do anything */
    return 0; /* don't do anything */
  return connection_or_process_cells_from_inbuf(conn);
  return connection_or_process_cells_from_inbuf(conn);
+9 −8
Changes for src/or/control.c: 9 added lines, 8 removed lines.
Original line number Original line Diff line number Diff line
@@ -391,8 +391,15 @@ connection_control_finished_flushing(connection_t *conn) {
  return 0;
  return 0;
}
}


/** Called when <b>conn</b> has received more bytes on its inbuf, or has
/** Called when <b>conn</b> has gotten its socket closed. */
 * gotten its socket closed. */
int connection_control_reached_eof(connection_t *conn) {
  log_fn(LOG_INFO,"Control connection reached EOF. Closing.");
  connection_mark_for_close(conn);
  return 0;
}

/** Called when <b>conn</b> has received more bytes on its inbuf.
 */
int
int
connection_control_process_inbuf(connection_t *conn) {
connection_control_process_inbuf(connection_t *conn) {
  uint16_t body_len, command_type;
  uint16_t body_len, command_type;
@@ -401,12 +408,6 @@ connection_control_process_inbuf(connection_t *conn) {
  tor_assert(conn);
  tor_assert(conn);
  tor_assert(conn->type == CONN_TYPE_CONTROL);
  tor_assert(conn->type == CONN_TYPE_CONTROL);


  if(conn->inbuf_reached_eof) {
    log_fn(LOG_INFO,"Control connection reached EOF. Closing.");
    connection_mark_for_close(conn);
    return 0;
  }

 again:
 again:
  /* Try to suck a control message from the buffer. */
  /* Try to suck a control message from the buffer. */
  switch(fetch_from_buf_control(conn->inbuf, &body_len, &command_type, &body))
  switch(fetch_from_buf_control(conn->inbuf, &body_len, &command_type, &body))
+18 −17
Changes for src/or/cpuworker.c: 18 added lines, 17 removed lines.
Original line number Original line Diff line number Diff line
@@ -91,9 +91,25 @@ void cpuworkers_rotate(void)
  spawn_enough_cpuworkers();
  spawn_enough_cpuworkers();
}
}


/** If the cpuworker closes the connection,
 * mark it as closed and spawn a new one as needed. */
int connection_cpu_reached_eof(connection_t *conn) {
  log_fn(LOG_WARN,"Read eof. Worker died unexpectedly.");
  if(conn->state != CPUWORKER_STATE_IDLE) {
    /* the circ associated with this cpuworker will have to wait until
     * it gets culled in run_connection_housekeeping(), since we have
     * no way to find out which circ it was. */
    log_fn(LOG_WARN,"...and it left a circuit queued; abandoning circ.");
    num_cpuworkers_busy--;
  }
  num_cpuworkers--;
  spawn_enough_cpuworkers(); /* try to regrow. hope we don't end up spinning. */
  connection_mark_for_close(conn);
  return 0;
}

/** Called when we get data from a cpuworker.  If the answer is not complete,
/** Called when we get data from a cpuworker.  If the answer is not complete,
 * wait for a complete answer.  If the cpuworker closes the connection,
 * wait for a complete answer. If the answer is complete,
 * mark it as closed and spawn a new one as needed.  If the answer is complete,
 * process it as appropriate.
 * process it as appropriate.
 */
 */
int connection_cpu_process_inbuf(connection_t *conn) {
int connection_cpu_process_inbuf(connection_t *conn) {
@@ -108,21 +124,6 @@ int connection_cpu_process_inbuf(connection_t *conn) {
  tor_assert(conn);
  tor_assert(conn);
  tor_assert(conn->type == CONN_TYPE_CPUWORKER);
  tor_assert(conn->type == CONN_TYPE_CPUWORKER);


  if(conn->inbuf_reached_eof) {
    log_fn(LOG_WARN,"Read eof. Worker died unexpectedly.");
    if(conn->state != CPUWORKER_STATE_IDLE) {
      /* the circ associated with this cpuworker will have to wait until
       * it gets culled in run_connection_housekeeping(), since we have
       * no way to find out which circ it was. */
      log_fn(LOG_WARN,"...and it left a circuit queued; abandoning circ.");
      num_cpuworkers_busy--;
    }
    num_cpuworkers--;
    spawn_enough_cpuworkers(); /* try to regrow. hope we don't end up spinning. */
    connection_mark_for_close(conn);
    return 0;
  }

  if(conn->state == CPUWORKER_STATE_BUSY_ONION) {
  if(conn->state == CPUWORKER_STATE_BUSY_ONION) {
    if(buf_datalen(conn->inbuf) < LEN_ONION_RESPONSE) /* entire answer available? */
    if(buf_datalen(conn->inbuf) < LEN_ONION_RESPONSE) /* entire answer available? */
      return 0; /* not yet */
      return 0; /* not yet */
Loading