Commit f563bbd2 authored by Roger Dingledine's avatar Roger Dingledine
Browse files

refactor so connection_write_to_buf() never fails


svn:r537
parent a6bab569
Loading
Loading
Loading
Loading
+4 −8
Original line number Diff line number Diff line
@@ -267,7 +267,8 @@ int circuit_deliver_relay_cell(cell_t *cell, circuit_t *circ,
  }

  log_fn(LOG_DEBUG,"Passing on unrecognized cell.");
  return connection_write_cell_to_buf(cell, conn);
  connection_write_cell_to_buf(cell, conn);
  return 0;
}

int relay_crypt(circuit_t *circ, char *in, int inlen, char cell_direction,
@@ -737,9 +738,7 @@ int circuit_send_next_onion_skin(circuit_t *circ) {
      return -1;
    }

    if(connection_write_cell_to_buf(&cell, circ->n_conn) < 0) {
      return -1;
    }
    connection_write_cell_to_buf(&cell, circ->n_conn);

    circ->cpath->state = CPATH_STATE_AWAITING_KEYS;
    circ->state = CIRCUIT_STATE_BUILDING;
@@ -843,10 +842,7 @@ int circuit_extend(cell_t *cell, circuit_t *circ) {

  memcpy(newcell.payload, cell->payload+RELAY_HEADER_SIZE+6, DH_ONIONSKIN_LEN);

  if(connection_write_cell_to_buf(&newcell, circ->n_conn) < 0) {
    return -1;
  }

  connection_write_cell_to_buf(&newcell, circ->n_conn);
  return 0;
}

+9 −8
Original line number Diff line number Diff line
@@ -489,13 +489,10 @@ int connection_handle_write(connection_t *conn) {
  return 0;
}

int connection_write_to_buf(const char *string, int len, connection_t *conn) {
void connection_write_to_buf(const char *string, int len, connection_t *conn) {

  if(!len)
    return 0;

  if(conn->marked_for_close)
    return 0;
  if(!len || conn->marked_for_close)
    return;

  if( (!connection_speaks_cells(conn)) ||
      (!connection_state_is_open(conn)) ||
@@ -506,7 +503,10 @@ int connection_write_to_buf(const char *string, int len, connection_t *conn) {
    conn->outbuf_flushlen += len;
  }

  return write_to_buf(string, len, conn->outbuf);
  if(write_to_buf(string, len, conn->outbuf) < 0) {
    log_fn(LOG_WARNING,"write_to_buf failed. Closing connection (fd %d).", conn->s);
    conn->marked_for_close = 1;
  }
}

connection_t *connection_exact_get_by_addr_port(uint32_t addr, uint16_t port) {
@@ -653,7 +653,8 @@ int connection_send_destroy(aci_t aci, connection_t *conn) {
  cell.aci = aci;
  cell.command = CELL_DESTROY;
  log_fn(LOG_INFO,"Sending destroy (aci %d).",aci);
  return connection_write_cell_to_buf(&cell, conn);
  connection_write_cell_to_buf(&cell, conn);
  return 0;
}

int connection_process_inbuf(connection_t *conn) {
+9 −14
Original line number Diff line number Diff line
@@ -171,13 +171,9 @@ int connection_edge_process_relay_cell(cell_t *cell, circuit_t *circ, connection

//      printf("New text for buf (%d bytes): '%s'", cell->length - RELAY_HEADER_SIZE, cell->payload + RELAY_HEADER_SIZE);
      stats_n_data_bytes_received += (cell->length - RELAY_HEADER_SIZE);
      if(connection_write_to_buf(cell->payload + RELAY_HEADER_SIZE,
                                 cell->length - RELAY_HEADER_SIZE, conn) < 0) {
/*ENDCLOSE*/    conn->marked_for_close = 1;
        return 0;
      }
      if(connection_consider_sending_sendme(conn, edge_type) < 0)
/*ENDCLOSE*/    conn->marked_for_close = 1;
      connection_write_to_buf(cell->payload + RELAY_HEADER_SIZE,
                             cell->length - RELAY_HEADER_SIZE, conn);
      connection_consider_sending_sendme(conn, edge_type);
      return 0;
    case RELAY_COMMAND_END:
      if(!conn) {
@@ -302,7 +298,8 @@ int connection_edge_finished_flushing(connection_t *conn) {
    case AP_CONN_STATE_OPEN:
    case EXIT_CONN_STATE_OPEN:
      connection_stop_writing(conn);
      return connection_consider_sending_sendme(conn, conn->type);
      connection_consider_sending_sendme(conn, conn->type);
      return 0;
    case AP_CONN_STATE_SOCKS_WAIT:
      connection_stop_writing(conn);
      return 0;
@@ -403,18 +400,18 @@ repeat_connection_package_raw_inbuf:
  goto repeat_connection_package_raw_inbuf;
}

int connection_consider_sending_sendme(connection_t *conn, int edge_type) {
void connection_consider_sending_sendme(connection_t *conn, int edge_type) {
  circuit_t *circ;
  cell_t cell;
 
  if(connection_outbuf_too_full(conn))
    return 0;
    return;
 
  circ = circuit_get_by_conn(conn);
  if(!circ) {
    /* this can legitimately happen if the destroy has already arrived and torn down the circuit */
    log_fn(LOG_INFO,"No circuit associated with conn. Skipping.");
    return 0;
    return;
  }
 
  memset(&cell, 0, sizeof(cell_t));
@@ -434,11 +431,9 @@ int connection_consider_sending_sendme(connection_t *conn, int edge_type) {
    if(circuit_deliver_relay_cell(&cell, circ, CELL_DIRECTION(edge_type), conn->cpath_layer) < 0) {
      log_fn(LOG_WARNING,"circuit_deliver_relay_cell failed. Closing.");
      circuit_close(circ);
      return 0;
      return;
    }
  }
 
  return 0;
}

static int connection_ap_handshake_process_socks(connection_t *conn) {
+2 −2
Original line number Diff line number Diff line
@@ -248,13 +248,13 @@ static int connection_tls_finish_handshake(connection_t *conn) {

/* ********************************** */

int connection_write_cell_to_buf(const cell_t *cellp, connection_t *conn) {
void connection_write_cell_to_buf(const cell_t *cellp, connection_t *conn) {
  char networkcell[CELL_NETWORK_SIZE];
  char *n = networkcell;

  cell_pack(n, cellp);
 
  return connection_write_to_buf(n, CELL_NETWORK_SIZE, conn);
  connection_write_to_buf(n, CELL_NETWORK_SIZE, conn);
}

/* if there's a whole cell there, pull it off and process it. */
+3 −7
Original line number Diff line number Diff line
@@ -267,13 +267,9 @@ int assign_to_cpuworker(connection_t *cpuworker, unsigned char question_type,
    cpuworker->state = CPUWORKER_STATE_BUSY_ONION;
    num_cpuworkers_busy++;

    if(connection_write_to_buf(&question_type, 1, cpuworker) < 0 ||
       connection_write_to_buf(tag, sizeof(tag), cpuworker) < 0 ||
       connection_write_to_buf(circ->onionskin, DH_ONIONSKIN_LEN, cpuworker) < 0) {
      log_fn(LOG_WARNING,"Write failed. Closing worker and failing circ.");
      cpuworker->marked_for_close = 1;
      return -1;
    }
    connection_write_to_buf(&question_type, 1, cpuworker);
    connection_write_to_buf(tag, sizeof(tag), cpuworker);
    connection_write_to_buf(circ->onionskin, DH_ONIONSKIN_LEN, cpuworker);
  }
  return 0;    
}
Loading