[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[or-cvs] made "app" connection be "exit" connection



Update of /home/or/cvsroot/src/or
In directory moria.seul.org:/home/arma/work/onion/cvs/src/or

Modified Files:
	Makefile.am buffers.c circuit.c command.c connection.c 
	connection_op.c connection_or.c main.c or.h 
Added Files:
	connection_exit.c 
Removed Files:
	connection_app.c 
Log Message:
made 'app' connection be 'exit' connection
general cleanup, particularly in buffers.c



--- NEW FILE: connection_exit.c ---

#include "or.h"

int connection_exit_process_inbuf(connection_t *conn) {

  assert(conn && conn->type == CONN_TYPE_EXIT);

  if(conn->inbuf_reached_eof) {
    /* eof reached, kill it. */
    log(LOG_DEBUG,"connection_exit_process_inbuf(): conn reached eof. Closing.");
    return -1;
  }

  log(LOG_DEBUG,"connection_exit_process_inbuf(): state %d.",conn->state);

  switch(conn->state) {
    case EXIT_CONN_STATE_CONNECTING:
      log(LOG_DEBUG,"connection_exit_process_inbuf(): text from server while in 'connecting' state. Leaving it on buffer.");
      return 0;
    case EXIT_CONN_STATE_OPEN:
      return connection_exit_package_inbuf(conn);
  }
  return 0;

}

int connection_exit_package_inbuf(connection_t *conn) {
  int amount_to_process;
  cell_t cell;
  circuit_t *circ;

  assert(conn && conn->type == CONN_TYPE_EXIT);

  amount_to_process = conn->inbuf_datalen;

  if(!amount_to_process)
    return 0;

  if(amount_to_process > CELL_PAYLOAD_SIZE) {
    cell.length = CELL_PAYLOAD_SIZE;
  } else {
    cell.length = amount_to_process;
  }

  if(connection_fetch_from_buf(cell.payload, cell.length, conn) < 0) {
    return -1;
  }

  circ = circuit_get_by_conn(conn);
  if(!circ) {
    log(LOG_DEBUG,"connection_exit_package_inbuf(): conn has no circuits!");
    return -1;
  }

  log(LOG_DEBUG,"connection_exit_package_inbuf(): Packaging %d bytes.",cell.length);
  cell.aci = circ->p_aci;
  cell.command = CELL_DATA;
  if(circuit_deliver_data_cell(&cell, circ, circ->p_conn, 'e') < 0) {
    log(LOG_DEBUG,"connection_exit_package_inbuf(): circuit_deliver_data_cell (backward) failed. Closing.");
    circuit_close(circ);
    return 0;
  }
  if(amount_to_process > CELL_PAYLOAD_SIZE)
    return(connection_exit_package_inbuf(conn));
  return 0;
}

int connection_exit_finished_flushing(connection_t *conn) {
  int e, len=sizeof(e);

  assert(conn && conn->type == CONN_TYPE_EXIT);

  switch(conn->state) {
    case EXIT_CONN_STATE_CONNECTING:
      if (getsockopt(conn->s, SOL_SOCKET, SO_ERROR, &e, &len) < 0)  { /* not yet */
        if(errno != EINPROGRESS){
          /* yuck. kill it. */
          log(LOG_DEBUG,"connection_exit_finished_flushing(): in-progress connect failed. Removing.");
          return -1;
        } else {
          return 0; /* no change, see if next time is better */
        }
      }
      /* the connect has finished. */

      log(LOG_DEBUG,"connection_exit_finished_flushing() : Connection to %s:%u established.",
          conn->address,ntohs(conn->port));
      
      conn->state = EXIT_CONN_STATE_OPEN;
      connection_watch_events(conn, POLLIN);
      return 0;
    case EXIT_CONN_STATE_OPEN:
      /* FIXME down the road, we'll clear out circuits that are pending to close */
      connection_watch_events(conn, POLLIN);
      return 0;
    default:
      log(LOG_DEBUG,"Bug: connection_exit_finished_flushing() called in unexpected state.");
      return 0;
  }

  return 0;
}

int connection_exit_process_data_cell(cell_t *cell, connection_t *conn) {
  struct hostent *rent;
  struct sockaddr_in dest_addr;
  int s;

  /* an outgoing data cell has arrived */

  assert(conn && conn->type == CONN_TYPE_EXIT);

  switch(conn->state) {
    case EXIT_CONN_STATE_CONNECTING_WAIT:
      log(LOG_DEBUG,"connection_exit_process_cell(): state is connecting_wait. cell length %d.", cell->length);
      if(!conn->ss_received) { /* this cell contains the ss */
        if(cell->length != sizeof(ss_t)) {
          log(LOG_DEBUG,"connection_exit_process_cell(): Supposed to contain SS but wrong size. Closing.");
          return -1;
        }
        memcpy(&conn->ss, cell->payload, cell->length);
        if(conn->ss.addr_fmt != SS_ADDR_FMT_ASCII_HOST_PORT) { /* unrecognized address format */
          log(LOG_DEBUG,"connection_exit_process_cell(): SS has unrecognized address format. Closing.");
          return -1;
        }
        conn->ss_received = 1;
	log(LOG_DEBUG,"connection_exit_process_cell(): SS received.");
      } else if (!conn->addr) { /* this cell contains the dest addr */
        if(!memchr(cell->payload,0,cell->length)) {
          log(LOG_DEBUG,"connection_exit_process_cell(): dest_addr cell has no \\0. Closing.");
          return -1;
        }
        conn->address = strdup(cell->payload);
        rent = gethostbyname(cell->payload);
        if (!rent) { 
          log(LOG_ERR,"connection_exit_process_cell(): Could not resolve dest addr %s.",cell->payload);
          return -1;
        }
        memcpy(&conn->addr, rent->h_addr,rent->h_length); 
	log(LOG_DEBUG,"connection_exit_process_cell(): addr %s resolves to %d.",cell->payload,conn->addr);
      } else if (!conn->port) { /* this cell contains the dest port */
        if(!memchr(cell->payload,'\0',cell->length)) {
          log(LOG_DEBUG,"connection_exit_process_cell(): dest_port cell has no \\0. Closing.");
          return -1;
        }
        conn->port = atoi(cell->payload);
        if(!conn->port) { /* bad port */
          log(LOG_DEBUG,"connection_exit_process_cell(): dest_port cell isn't a valid number. Closing.");
          return -1;
        }
        /* all the necessary info is here. Start the connect() */
        s=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
        if (s < 0)
        {
          log(LOG_ERR,"connection_exit_process_cell(): Error creating network socket.");
          return -1;
        }
        fcntl(s, F_SETFL, O_NONBLOCK); /* set s to non-blocking */
      
        memset((void *)&dest_addr,0,sizeof(dest_addr));
        dest_addr.sin_family = AF_INET;
        dest_addr.sin_port = conn->port;
        memcpy((void *)&dest_addr.sin_addr, &conn->addr, sizeof(uint32_t));
      
        log(LOG_DEBUG,"connection_exit_process_cell(): Connecting to %s:%u.",conn->address,ntohs(conn->port)); 

        if(connect(s,(struct sockaddr *)&dest_addr,sizeof(dest_addr)) < 0){
          if(errno != EINPROGRESS){
            /* yuck. kill it. */
            log(LOG_DEBUG,"connection_exit_process_cell(): Connect failed.");
            return -1;
          } else {
            /* it's in progress. set state appropriately and return. */
            conn->s = s;
            connection_set_poll_socket(conn);
            conn->state = EXIT_CONN_STATE_CONNECTING;
      
            /* i think only pollout is needed, but i'm curious if pollin ever gets caught -RD */
            log(LOG_DEBUG,"connection_exit_process_cell(): connect in progress, socket %d.",s);
            connection_watch_events(conn, POLLOUT | POLLIN);
            return 0;
          }
        }

        /* it succeeded. we're connected. */
        log(LOG_DEBUG,"connection_exit_process_cell(): Connection to %s:%u established.",conn->address,ntohs(conn->port));

        conn->s = s;
        connection_set_poll_socket(conn);
        conn->state = EXIT_CONN_STATE_OPEN;
        connection_watch_events(conn, POLLIN);
      } else { /* i'm not sure what this would be */
        log(LOG_DEBUG,"connection_exit_process_cell(): in connecting_wait, not sure why.");
      }
      return 0;
    case EXIT_CONN_STATE_CONNECTING:
      log(LOG_DEBUG,"connection_exit_process_cell(): Data receiving while connecting. Queueing.");
      /* FIXME kludge. shouldn't call write_to_buf directly. */
      return write_to_buf(cell->payload, cell->length, &conn->outbuf, &conn->outbuflen, &conn->outbuf_datalen);
    case EXIT_CONN_STATE_OPEN:
      return connection_write_to_buf(cell->payload, cell->length, conn);
  }

  return 0;
}


Index: Makefile.am
===================================================================
RCS file: /home/or/cvsroot/src/or/Makefile.am,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- Makefile.am	28 Jun 2002 23:26:42 -0000	1.1
+++ Makefile.am	30 Jun 2002 07:37:48 -0000	1.2
@@ -4,7 +4,7 @@
 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

Index: buffers.c
===================================================================
RCS file: /home/or/cvsroot/src/or/buffers.c,v
retrieving revision 1.1.1.1
retrieving revision 1.2
diff -u -d -r1.1.1.1 -r1.2
--- buffers.c	26 Jun 2002 22:45:50 -0000	1.1.1.1
+++ buffers.c	30 Jun 2002 07:37:48 -0000	1.2
@@ -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 @@
     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 @@
     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 */
-    return -1;
 
-  if(string_len > buf_datalen) /* we want too much. sorry. */
+  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;
 }
 

Index: circuit.c
===================================================================
RCS file: /home/or/cvsroot/src/or/circuit.c,v
retrieving revision 1.1.1.1
retrieving revision 1.2
diff -u -d -r1.1.1.1 -r1.2
--- circuit.c	26 Jun 2002 22:45:50 -0000	1.1.1.1
+++ circuit.c	30 Jun 2002 07:37:48 -0000	1.2
@@ -242,9 +242,9 @@
     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 */

Index: command.c
===================================================================
RCS file: /home/or/cvsroot/src/or/command.c,v
retrieving revision 1.1.1.1
retrieving revision 1.2
diff -u -d -r1.1.1.1 -r1.2
--- command.c	26 Jun 2002 22:45:50 -0000	1.1.1.1
+++ command.c	30 Jun 2002 07:37:48 -0000	1.2
@@ -96,6 +96,9 @@
       /* 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 @@
     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 @@
     circ->n_conn = n_conn;
     return;
   }
-
 }
 
 void command_process_data_cell(cell_t *cell, connection_t *conn) {
@@ -207,6 +209,5 @@
   if(cell->aci == circ->n_aci) /* the destroy came from ahead */
     connection_send_destroy(circ->p_aci, circ->p_conn);
   circuit_free(circ);
-
 }
 

Index: connection.c
===================================================================
RCS file: /home/or/cvsroot/src/or/connection.c,v
retrieving revision 1.1.1.1
retrieving revision 1.2
diff -u -d -r1.1.1.1 -r1.2
--- connection.c	26 Jun 2002 22:45:50 -0000	1.1.1.1
+++ connection.c	30 Jun 2002 07:37:48 -0000	1.2
@@ -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 @@
     "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 @@
   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 @@
  /* 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 @@
   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 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 @@
   }
  
   return 0;
-
 }
 
 int connection_read_to_buf(connection_t *conn) {
@@ -226,11 +231,10 @@
 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 @@
       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 @@
       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;

Index: connection_op.c
===================================================================
RCS file: /home/or/cvsroot/src/or/connection_op.c,v
retrieving revision 1.1.1.1
retrieving revision 1.2
diff -u -d -r1.1.1.1 -r1.2
--- connection_op.c	26 Jun 2002 22:45:50 -0000	1.1.1.1
+++ connection_op.c	30 Jun 2002 07:37:49 -0000	1.2
@@ -1,14 +1,6 @@
 
 #include "or.h"
 
-connection_t *connection_op_new(void) {
-  return connection_new(CONN_TYPE_OP);
-}
-
-connection_t *connection_op_listener_new(void) {
-  return connection_new(CONN_TYPE_OP_LISTENER);
-}
-
 int connection_op_process_inbuf(connection_t *conn) {
 
   assert(conn && conn->type == CONN_TYPE_OP);

Index: connection_or.c
===================================================================
RCS file: /home/or/cvsroot/src/or/connection_or.c,v
retrieving revision 1.1.1.1
retrieving revision 1.2
diff -u -d -r1.1.1.1 -r1.2
--- connection_or.c	26 Jun 2002 22:45:50 -0000	1.1.1.1
+++ connection_or.c	30 Jun 2002 07:37:49 -0000	1.2
@@ -33,7 +33,6 @@
   }
 
   return 0;
-
 }
 
 int connection_or_finished_flushing(connection_t *conn) {
@@ -89,14 +88,6 @@
 
 /*********************/
 
-connection_t *connection_or_new(void) {
-  return connection_new(CONN_TYPE_OR);
-}
-
-connection_t *connection_or_new_listener(void) {
-  return connection_new(CONN_TYPE_OR_LISTENER);
-}
-
 void conn_or_init_crypto(connection_t *conn) {
   int x;
 
@@ -132,15 +123,14 @@
   struct sockaddr_in router_addr;
   int s;
 
-  if ((!router) || (!prkey) || (!local))
-    return -1;
+  assert(router && prkey && local);
 
   if(router->addr == local->sin_addr.s_addr && router->port == local->sin_port) {
     /* this is me! don't connect to me. */
     return 0;
   }
 
-  conn = connection_or_new();
+  conn = connection_new(CONN_TYPE_OR);
 
   /* set up conn so it's got all the data we need to remember */
   conn->addr = router->addr, conn->port = router->port;

Index: main.c
===================================================================
RCS file: /home/or/cvsroot/src/or/main.c,v
retrieving revision 1.1.1.1
retrieving revision 1.2
diff -u -d -r1.1.1.1 -r1.2
--- main.c	26 Jun 2002 22:45:50 -0000	1.1.1.1
+++ main.c	30 Jun 2002 07:37:49 -0000	1.2
@@ -73,7 +73,6 @@
 }
 
 int connection_remove(connection_t *conn) {
-
   int current_index;
 
   assert(conn);
@@ -103,7 +102,6 @@
 }
 
 connection_t *connection_get_by_addr_port(uint32_t addr, uint16_t port) {
-
   int i;
   connection_t *conn;
 
@@ -115,11 +113,9 @@
   }
 
   return NULL;
-
 }
 
 connection_t *connection_get_by_type(int type) {
-
   int i;
   connection_t *conn;
 
@@ -130,7 +126,6 @@
   }
 
   return NULL;
-
 }
 
 routerinfo_t *router_get_by_addr_port(uint32_t addr, uint16_t port) {
@@ -159,7 +154,6 @@
 }
 
 void check_conn_read(int i) {
-
   int retval;
   connection_t *conn;
 
@@ -174,7 +168,7 @@
     } else if (conn->type == CONN_TYPE_OR_LISTENER) {
       retval = connection_or_handle_listener_read(conn);
     } else {
-      /* else it's an OP, OR, or app */
+      /* else it's an OP, OR, or exit */
       retval = connection_read_to_buf(conn);
       if (retval >= 0) { /* all still well */
         retval = connection_process_inbuf(conn);
@@ -195,7 +189,6 @@
 }
 
 void check_conn_write(int i) {
-
   int retval;
   connection_t *conn;
 
@@ -209,7 +202,7 @@
       log(LOG_DEBUG,"check_conn_write(): Got a listener socket. Can't happen!");
       retval = -1;
     } else {
-      /* else it's an OP, OR, or app */
+      /* else it's an OP, OR, or exit */
       retval = connection_flush_buf(conn); /* conns in CONNECTING state will fall through... */
       if(retval == 0) { /* it's done flushing */
         retval = connection_finished_flushing(conn); /* ...and get handled here. */
@@ -245,26 +238,7 @@
   }
 }
 
-#if 0
-void check_conn_hup(int i) {
-  connection_t *conn;
-
-  if(poll_array[i].revents & POLLHUP) { /* they've hung up */
-    conn = connection_array[i];
-    log(LOG_DEBUG,"check_conn_hup(): socket %d has hung up.",conn->s);
-    connection_remove(conn);
-    connection_free(conn);
-    
-    if(i<nfds) { /* we just replaced the one at i with a new one.
-                    process it too. */
-      check_conn_hup(i);
-    }
-  }
-}
-#endif
-
 int do_main_loop(void) {
-
   int i;
 
   /* load the routers file */
@@ -288,14 +262,14 @@
 
   /* try to connect to all the other ORs, and start the listeners */
   retry_all_connections(router_array, rarray_len, prkey, 
-		        options[NetworkPort].r.i,options[EntryPort].r.i);
+		        options[NetworkPort].r.i,options[EntryPort].r.i, 0);
 
   for(;;) {
     poll(poll_array, nfds, -1); /* poll until we have an event */
 
     /* do all the reads first, so we can detect closed sockets */
     for(i=0;i<nfds;i++)
-      check_conn_read(i);
+      check_conn_read(i); /* this also blows away broken connections */
 
     /* then do the writes */
     for(i=0;i<nfds;i++)
@@ -304,28 +278,18 @@
     /* any of the conns need to be closed now? */
     for(i=0;i<nfds;i++)
       check_conn_marked(i); 
-
-#if 0 /* no, check_conn_read() takes care of hups. */
-    /* remove the ones that have disconnected */
-    for(i=0;i<nfds;i++)
-      check_conn_hup(i);
-#endif
   }
-
 }
 
-void catch ()
-{
+void catch () {
   errno = 0; /* netcat does this. it looks fun. */
 
   log(LOG_DEBUG,"Catching ^c, exiting cleanly.");
    
   exit(0);
-  
 }
 
-int main(int argc, char *argv[])
-{
+int main(int argc, char *argv[]) {
   int retval = 0;
 
   char *conf_filename = NULL; /* configuration file */

Index: or.h
===================================================================
RCS file: /home/or/cvsroot/src/or/or.h,v
retrieving revision 1.1.1.1
retrieving revision 1.2
diff -u -d -r1.1.1.1 -r1.2
--- or.h	26 Jun 2002 22:45:50 -0000	1.1.1.1
+++ or.h	30 Jun 2002 07:37:49 -0000	1.2
@@ -49,7 +49,9 @@
 #define CONN_TYPE_OP 2
 #define CONN_TYPE_OR_LISTENER 3
 #define CONN_TYPE_OR 4
-#define CONN_TYPE_APP 5
+#define CONN_TYPE_EXIT 5
+#define CONN_TYPE_AP_LISTENER 6
+#define CONN_TYPE_AP 7
 
 #define LISTENER_STATE_READY 0
 
@@ -60,23 +62,33 @@
 #define OP_CONN_STATE_CLOSE_WAIT 3 /* have sent a destroy, awaiting a confirmation */
 #endif
 
-#define OR_CONN_STATE_CLIENT_CONNECTING 0
-#define OR_CONN_STATE_CLIENT_SENDING_AUTH 1 /* sending address and info */
-#define OR_CONN_STATE_CLIENT_AUTH_WAIT 2 /* have sent address and info, waiting */
-#define OR_CONN_STATE_CLIENT_SENDING_NONCE 3 /* sending nonce, last piece of handshake */
-#define OR_CONN_STATE_SERVER_AUTH_WAIT 4 /* waiting for address and info */
-#define OR_CONN_STATE_SERVER_SENDING_AUTH 5 /* writing auth and nonce */
-#define OR_CONN_STATE_SERVER_NONCE_WAIT 6 /* waiting for confirmation of nonce */
-#define OR_CONN_STATE_OPEN 7 /* ready to send/receive cells. */
+/* how to read these states:
+ * foo_CONN_STATE_bar_baz:
+ * "I am acting as a bar, currently in stage baz of talking with a foo."
+ */
+#define OR_CONN_STATE_OP_CONNECTING 0
+#define OR_CONN_STATE_OP_SENDING_KEYS 1
+#define OR_CONN_STATE_CLIENT_CONNECTING 2
+#define OR_CONN_STATE_CLIENT_SENDING_AUTH 3 /* sending address and info */
+#define OR_CONN_STATE_CLIENT_AUTH_WAIT 4 /* have sent address and info, waiting */
+#define OR_CONN_STATE_CLIENT_SENDING_NONCE 5 /* sending nonce, last piece of handshake */
+#define OR_CONN_STATE_SERVER_AUTH_WAIT 6 /* waiting for address and info */
+#define OR_CONN_STATE_SERVER_SENDING_AUTH 7 /* writing auth and nonce */
+#define OR_CONN_STATE_SERVER_NONCE_WAIT 8 /* waiting for confirmation of nonce */
+#define OR_CONN_STATE_OPEN 9 /* ready to send/receive cells. */
 
-#define APP_CONN_STATE_CONNECTING_WAIT 0 /* waiting for standard structure or dest info */
-#define APP_CONN_STATE_CONNECTING 1
-#define APP_CONN_STATE_OPEN 2
+#define EXIT_CONN_STATE_CONNECTING_WAIT 0 /* waiting for standard structure or dest info */
+#define EXIT_CONN_STATE_CONNECTING 1
+#define EXIT_CONN_STATE_OPEN 2
 #if 0
-#define APP_CONN_STATE_CLOSE 3 /* flushing the buffer, then will close */
-#define APP_CONN_STATE_CLOSE_WAIT 4 /* have sent a destroy, awaiting a confirmation */
+#define EXIT_CONN_STATE_CLOSE 3 /* flushing the buffer, then will close */
+#define EXIT_CONN_STATE_CLOSE_WAIT 4 /* have sent a destroy, awaiting a confirmation */
 #endif
 
+#define AP_CONN_STATE_SS_WAIT 0
+#define AP_CONN_STATE_OR_WAIT 1
+#define AP_CONN_STATE_OPEN 2
+
 #define CIRCUIT_STATE_OPEN_WAIT 0 /* receiving/processing the onion */
 #define CIRCUIT_STATE_OPEN 1 /* onion processed, ready to send data along the connection */
 #define CIRCUIT_STATE_CLOSE_WAIT1 2 /* sent two "destroy" signals, waiting for acks */
@@ -130,7 +142,7 @@
   uint32_t addr; /* these two uniquely identify a router */
   uint16_t port;
 
-/* used by app: */
+/* used by exit: */
 
   ss_t ss; /* standard structure */
   int ss_received; /* size of ss, received so far */
@@ -229,26 +241,26 @@
 
 /********************************* buffers.c ***************************/
 
-int buf_new(char **pbuf, size_t *pbuflen, size_t *pbuf_datalen);
+int buf_new(char **buf, size_t *buflen, size_t *buf_datalen);
 
-int buf_free(char *buf);
+void buf_free(char *buf);
 
-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 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_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
    */
 
 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) */
 
@@ -305,7 +317,7 @@
 
 /* start all connections that should be up but aren't */
 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);
 
 int connection_read_to_buf(connection_t *conn);
 
@@ -328,9 +340,6 @@
 int connection_or_process_inbuf(connection_t *conn);
 int connection_or_finished_flushing(connection_t *conn);
 
-connection_t *connection_or_new(void);
-connection_t *connection_or_listener_new(void);
-
 void conn_or_init_crypto(connection_t *conn);
 
 int or_handshake_client_process_auth(connection_t *conn);
@@ -346,9 +355,6 @@
 
 /********************************* connection_op.c ***************************/
 
-connection_t *connection_op_new(void);
-connection_t *connection_op_listener_new(void);
-
 int op_handshake_process_keys(connection_t *conn);
 
 int connection_op_process_inbuf(connection_t *conn);
@@ -359,15 +365,13 @@
 
 int connection_op_handle_listener_read(connection_t *conn);
 
-/********************************* connection_app.c ***************************/
-
-connection_t *connection_app_new(void);
+/********************************* connection_exit.c ***************************/
 
-int connection_app_process_inbuf(connection_t *conn);
-int connection_app_package_inbuf(connection_t *conn);
-int connection_app_process_data_cell(cell_t *cell, connection_t *conn);
+int connection_exit_process_inbuf(connection_t *conn);
+int connection_exit_package_inbuf(connection_t *conn);
+int connection_exit_process_data_cell(cell_t *cell, connection_t *conn);
 
-int connection_app_finished_flushing(connection_t *conn);
+int connection_exit_finished_flushing(connection_t *conn);
 
 /********************************* main.c ***************************/
 

--- connection_app.c DELETED ---