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

made 'app' connection be 'exit' connection

general cleanup, particularly in buffers.c


svn:r17
parent e6f67fb1
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -4,7 +4,7 @@ bin_PROGRAMS = or
or_LDADD = -L../common -lor

or_SOURCES = args.c buffers.c cell.c circuit.c command.c connection.c \
             connection_app.c connection_op.c connection_or.c config.c \
             connection_exit.c connection_op.c connection_or.c config.c \
             main.c onion.c routers.c

noinst_HEADERS = or.h
+36 −86
Original line number Diff line number Diff line
@@ -3,52 +3,35 @@

#include "or.h"

int buf_new(char **pbuf, size_t *pbuflen, size_t *pbuf_datalen) {
int buf_new(char **buf, size_t *buflen, size_t *buf_datalen) {

  if (!pbuf || !pbuflen || !pbuf_datalen) /* invalid parameters */
    return -1;
  assert(buf && buflen && buf_datalen);

  *pbuf = (char *)malloc(MAX_BUF_SIZE);
  if(!*pbuf)
  *buf = (char *)malloc(MAX_BUF_SIZE);
  if(!*buf)
    return -1;
  memset(*pbuf,0,MAX_BUF_SIZE);
  *pbuflen = MAX_BUF_SIZE;
  *pbuf_datalen = 0;
  memset(*buf,0,MAX_BUF_SIZE);
  *buflen = MAX_BUF_SIZE;
  *buf_datalen = 0;

  return 0;
}

int buf_free(char *buf) {

void buf_free(char *buf) {
  free(buf);

  return 0;
}

int read_to_buf(int s, char **pbuf, size_t *pbuflen, size_t *pbuf_datalen, int *preached_eof) {
int read_to_buf(int s, char **buf, size_t *buflen, size_t *buf_datalen, int *reached_eof) {

  /* grab from s, put onto buf, return how many bytes read */

  int read_result;
  char *buf;
  size_t buflen;
  size_t buf_datalen;

  if (!pbuf || !pbuflen || !pbuf_datalen || !preached_eof) /* invalid parameters */
    return -1 ;

  if(s<0) {
    log(LOG_DEBUG,"read_to_buf() received negative socket %d.",s);
    return -1;
  }
  assert(buf && *buf && buflen && buf_datalen && reached_eof && (s>=0));

  /* this is the point where you would grow the buffer, if you want to */
  buf = *pbuf, buflen = *pbuflen, buf_datalen = *pbuf_datalen;

  if (!buf) /* invalid parameter */
    return -1;

  read_result = read(s, buf+buf_datalen, buflen - buf_datalen);
  read_result = read(s, *buf+*buf_datalen, *buflen - *buf_datalen);
  if (read_result < 0) {
    if(errno!=EAGAIN) { /* it's a real error */
      return -1;
@@ -56,46 +39,32 @@ int read_to_buf(int s, char **pbuf, size_t *pbuflen, size_t *pbuf_datalen, int *
    return 0;
  } else if (read_result == 0) {
    log(LOG_DEBUG,"read_to_buf(): Encountered eof");
    *preached_eof = 1;
    *reached_eof = 1;
    return 0;
  } else { /* we read some bytes */
    *pbuf_datalen = buf_datalen + read_result;
    log(LOG_DEBUG,"read_to_buf(): Read %d bytes. %d on inbuf.",read_result, *pbuf_datalen);
    *buf_datalen += read_result;
    log(LOG_DEBUG,"read_to_buf(): Read %d bytes. %d on inbuf.",read_result, *buf_datalen);
    return read_result;
  }

}

int flush_buf(int s, char **pbuf, size_t *pbuflen, size_t *pbuf_datalen) {
int flush_buf(int s, char **buf, size_t *buflen, size_t *buf_datalen) {

  /* push from buf onto s
   * then memmove to front of buf
   * return -1 or how many bytes remain on the buf */

  int write_result;
  char *buf;
  size_t buflen;
  size_t buf_datalen;

  if (!pbuf || !pbuflen || !pbuf_datalen) /* invalid parameters */
    return -1;

  if(s<0) {
    log(LOG_DEBUG,"flush_buf() received negative socket %d.",s);
    return -1;
  }

  assert(buf && *buf && buflen && buf_datalen && (s>=0));

  if(*pbuf_datalen == 0) /* nothing to flush */
  if(*buf_datalen == 0) /* nothing to flush */
    return 0;

  /* this is the point where you would grow the buffer, if you want to */
  buf = *pbuf, buflen = *pbuflen, buf_datalen = *pbuf_datalen;

  if (!buf) /* invalid parameter */
    return -1;

  write_result = write(s, buf, buf_datalen);
  write_result = write(s, buf, *buf_datalen);
  if (write_result < 0) {
    if(errno!=EAGAIN) { /* it's a real error */
      return -1;
@@ -103,72 +72,53 @@ int flush_buf(int s, char **pbuf, size_t *pbuflen, size_t *pbuf_datalen) {
    log(LOG_DEBUG,"flush_buf(): write() would block, returning.");
    return 0;
  } else {
    *pbuf_datalen -= write_result;
    memmove(buf, buf+write_result, *pbuf_datalen);
    log(LOG_DEBUG,"flush_buf(): flushed %d bytes, %d remain.",write_result,*pbuf_datalen);
    return *pbuf_datalen;
    *buf_datalen -= write_result;
    memmove(*buf, *buf+write_result, *buf_datalen);
    log(LOG_DEBUG,"flush_buf(): flushed %d bytes, %d remain.",write_result,*buf_datalen);
    return *buf_datalen;
  }

}

int write_to_buf(char *string, size_t string_len,
                 char **pbuf, size_t *pbuflen, size_t *pbuf_datalen) {
                 char **buf, size_t *buflen, size_t *buf_datalen) {

  /* append string to buf (growing as needed, return -1 if "too big")
   * return total number of bytes on the buf
   */

  char *buf;
  size_t buflen;
  size_t buf_datalen;

  if (!string || !pbuf || !pbuflen || !pbuf_datalen) /* invalid parameters */
    return -1;
  assert(string && buf && *buf && buflen && buf_datalen);

  /* this is the point where you would grow the buffer, if you want to */
  buf = *pbuf, buflen = *pbuflen, buf_datalen = *pbuf_datalen;

  if (!buf) /* invalid parameter */
    return -1;

  if (string_len + buf_datalen > buflen) { /* we're out of luck */
  if (string_len + *buf_datalen > *buflen) { /* we're out of luck */
    log(LOG_DEBUG, "write_to_buf(): buflen too small. Time to implement growing dynamic bufs.");
    return -1;
  }

  memcpy(buf+buf_datalen, string, string_len);
  *pbuf_datalen += string_len;
  log(LOG_DEBUG,"write_to_buf(): added %d bytes to buf (now %d total).",string_len, *pbuf_datalen);
  return *pbuf_datalen;
  memcpy(*buf+*buf_datalen, string, string_len);
  *buf_datalen += string_len;
  log(LOG_DEBUG,"write_to_buf(): added %d bytes to buf (now %d total).",string_len, *buf_datalen);
  return *buf_datalen;

}

int fetch_from_buf(char *string, size_t string_len,
                 char **pbuf, size_t *pbuflen, size_t *pbuf_datalen) {
                 char **buf, size_t *buflen, size_t *buf_datalen) {

  /* if there is string_len bytes in buf, write them onto string,
   * then memmove buf back (that is, remove them from buf) */

  char *buf;
  size_t buflen;
  size_t buf_datalen;

  if (!string || !pbuf || !pbuflen || !pbuf_datalen) /* invalid parameters */
    return -1;
  assert(string && buf && *buf && buflen && buf_datalen);

  /* this is the point where you would grow the buffer, if you want to */
  buf = *pbuf, buflen = *pbuflen, buf_datalen = *pbuf_datalen;

  if (!buf) /* invalid parameter */
  if(string_len > *buf_datalen) /* we want too much. sorry. */
    return -1;
 
  if(string_len > buf_datalen) /* we want too much. sorry. */
    return -1;
 
  memcpy(string,buf,string_len);
  *pbuf_datalen -= string_len;
  memmove(buf, buf+string_len, *pbuf_datalen);
  return *pbuf_datalen;

  memcpy(string,*buf,string_len);
  *buf_datalen -= string_len;
  memmove(*buf, *buf+string_len, *buf_datalen);
  return *buf_datalen;
}
+3 −3
Original line number Diff line number Diff line
@@ -242,9 +242,9 @@ int circuit_deliver_data_cell(cell_t *cell, circuit_t *circ, connection_t *conn,
    return -1;
  }

  if(conn->type == CONN_TYPE_APP) { /* send payload directly */
    log(LOG_DEBUG,"circuit_deliver_data_cell(): Sending to application.");
    if(connection_app_process_data_cell(cell, conn) < 0) {
  if(conn->type == CONN_TYPE_EXIT) { /* send payload directly */
    log(LOG_DEBUG,"circuit_deliver_data_cell(): Sending to exit.");
    if(connection_exit_process_data_cell(cell, conn) < 0) {
      return -1;
    }
  } else { /* send it as a cell */
+7 −6
Original line number Diff line number Diff line
@@ -96,6 +96,9 @@ void command_process_create_cell(cell_t *cell, connection_t *conn) {
      /* i've disabled making connections through OPs, but it's definitely
       * possible here. I'm not sure if it would be a bug or a feature. -RD
       */
      /* note also that this will close circuits where the onion has the same
       * router twice in a row in the path. i think that's ok. -RD
       */
      log(LOG_DEBUG,"command_process_create_cell(): Next router not connected. Closing.");
      circuit_close(circ);
    }
@@ -132,15 +135,15 @@ void command_process_create_cell(cell_t *cell, connection_t *conn) {
    free((void *)cellbuf);
    return;

  } else { /* this is destined for an app */
    log(LOG_DEBUG,"command_process_create_cell(): Creating new application connection.");
    n_conn = connection_new(CONN_TYPE_APP);
  } else { /* this is destined for an exit */
    log(LOG_DEBUG,"command_process_create_cell(): Creating new exit connection.");
    n_conn = connection_new(CONN_TYPE_EXIT);
    if(!n_conn) {
      log(LOG_DEBUG,"command_process_create_cell(): connection_new failed. Closing.");
      circuit_close(circ);
      return;
    }
    n_conn->state = APP_CONN_STATE_CONNECTING_WAIT;
    n_conn->state = EXIT_CONN_STATE_CONNECTING_WAIT;
    n_conn->s = -1; /* not yet valid */
    if(connection_add(n_conn) < 0) { /* no space, forget it */
      log(LOG_DEBUG,"command_process_create_cell(): connection_add failed. Closing.");
@@ -151,7 +154,6 @@ void command_process_create_cell(cell_t *cell, connection_t *conn) {
    circ->n_conn = n_conn;
    return;
  }

}

void command_process_data_cell(cell_t *cell, connection_t *conn) {
@@ -207,6 +209,5 @@ void command_process_destroy_cell(cell_t *cell, connection_t *conn) {
  if(cell->aci == circ->n_aci) /* the destroy came from ahead */
    connection_send_destroy(circ->p_aci, circ->p_conn);
  circuit_free(circ);

}
+18 −14
Original line number Diff line number Diff line
@@ -3,6 +3,8 @@

/********* START VARIABLES **********/

#if 0
/* these are now out of date :( -RD */
char *conn_type_to_string[] = {
  "OP listener", /* 0 */
  "OP",          /* 1 */
@@ -26,12 +28,13 @@ char *conn_state_to_string[][10] = {
    "sending auth (as server)",     /* 5 */
    "waiting for nonce (as server)",/* 6 */
    "open" },                       /* 7 */
  { "connecting",                 /* app, 0 */
  { "connecting",                /* exit, 0 */
    "open",                            /* 1 */
    "waiting for dest info",           /* 2 */
    "flushing buffer, then will close",/* 3 */
    "close_wait" }                     /* 4 */
};
#endif

/********* END VARIABLES ************/

@@ -44,8 +47,9 @@ connection_t *connection_new(int type) {
  memset(conn,0,sizeof(connection_t)); /* zero it out to start */

  conn->type = type;
  buf_new(&conn->inbuf, &conn->inbuflen, &conn->inbuf_datalen);
  buf_new(&conn->outbuf, &conn->outbuflen, &conn->outbuf_datalen);
  if(buf_new(&conn->inbuf, &conn->inbuflen, &conn->inbuf_datalen) < 0 ||
     buf_new(&conn->outbuf, &conn->outbuflen, &conn->outbuf_datalen) < 0)
    return NULL;

  return conn;
}
@@ -61,8 +65,8 @@ void connection_free(connection_t *conn) {
 /* FIXME should we do these for all connections, or just ORs, or what */
  if(conn->type == CONN_TYPE_OR ||
     conn->type == CONN_TYPE_OP) {
//    EVP_CIPHER_CTX_cleanup(&conn->f_ctx);
//    EVP_CIPHER_CTX_cleanup(&conn->b_ctx);
    EVP_CIPHER_CTX_cleanup(&conn->f_ctx);
    EVP_CIPHER_CTX_cleanup(&conn->b_ctx);
  }

  if(conn->s > 0)
@@ -99,6 +103,8 @@ int connection_create_listener(RSA *prkey, struct sockaddr_in *local, int type)
  fcntl(s, F_SETFL, O_NONBLOCK); /* set s to non-blocking */

  conn = connection_new(type);
  if(!conn)
    return -1;
  conn->s = s;

  if(connection_add(conn) < 0) { /* no space, forget it */
@@ -156,7 +162,7 @@ int connection_handle_listener_read(connection_t *conn, int new_type, int new_st
}

int retry_all_connections(routerinfo_t **router_array, int rarray_len,
  RSA *prkey, uint16_t or_port, uint16_t op_port) {
  RSA *prkey, uint16_t or_port, uint16_t op_port, uint16_t ap_port) {

  /* start all connections that should be up but aren't */

@@ -201,7 +207,6 @@ int retry_all_connections(routerinfo_t **router_array, int rarray_len,
  }
 
  return 0;

}

int connection_read_to_buf(connection_t *conn) {
@@ -226,11 +231,10 @@ int connection_write_to_buf(char *string, int len, connection_t *conn) {
int connection_send_destroy(aci_t aci, connection_t *conn) {
  cell_t cell;

  if(!conn)
    return -1;
  assert(conn);

  if(conn->type == CONN_TYPE_OP ||
     conn->type == CONN_TYPE_APP) {
     conn->type == CONN_TYPE_EXIT) {
     log(LOG_DEBUG,"connection_send_destroy(): At an edge. Marking connection for close.");
     conn->marked_for_close = 1;
     return 0;
@@ -290,8 +294,8 @@ int connection_process_inbuf(connection_t *conn) {
      return connection_op_process_inbuf(conn);
    case CONN_TYPE_OR:
      return connection_or_process_inbuf(conn);
    case CONN_TYPE_APP:
      return connection_app_process_inbuf(conn);
    case CONN_TYPE_EXIT:
      return connection_exit_process_inbuf(conn);
    default:
      log(LOG_DEBUG,"connection_process_inbuf() got unexpected conn->type.");
      return -1;
@@ -309,8 +313,8 @@ int connection_finished_flushing(connection_t *conn) {
      return connection_op_finished_flushing(conn);
    case CONN_TYPE_OR:
      return connection_or_finished_flushing(conn);
    case CONN_TYPE_APP:
      return connection_app_finished_flushing(conn);
    case CONN_TYPE_EXIT:
      return connection_exit_finished_flushing(conn);
    default:
      log(LOG_DEBUG,"connection_finished_flushing() got unexpected conn->type.");
      return -1;
Loading