[or-cvs] onion proxy now speaks socks4a

Roger Dingledine arma at seul.org
Sun Sep 22 11:09:10 UTC 2002


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

Modified Files:
	connection.c connection_ap.c connection_exit.c main.c or.h 
Log Message:
onion proxy now speaks socks4a

httpap is obsolete; we support privoxy directly now!

smtpap is obsolete; need to find a good socks4a-enabled smtp proxy/client

I dub thee 0.0.1.



Index: connection.c
===================================================================
RCS file: /home/or/cvsroot/src/or/connection.c,v
retrieving revision 1.22
retrieving revision 1.23
diff -u -d -r1.22 -r1.23
--- connection.c	21 Sep 2002 22:41:48 -0000	1.22
+++ connection.c	22 Sep 2002 11:09:07 -0000	1.23
@@ -117,8 +117,6 @@
     free(conn->address);
   if(conn->dest_addr)
     free(conn->dest_addr);
-  if(conn->dest_port)
-    free(conn->dest_port);
 
   if(connection_speaks_cells(conn)) {
     if (conn->f_crypto)

Index: connection_ap.c
===================================================================
RCS file: /home/or/cvsroot/src/or/connection_ap.c,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -d -r1.12 -r1.13
--- connection_ap.c	19 Sep 2002 20:13:27 -0000	1.12
+++ connection_ap.c	22 Sep 2002 11:09:07 -0000	1.13
@@ -5,6 +5,7 @@
 #include "or.h"
 
 extern int global_role; /* from main.c */
+static const char socks_userid[] = "anonymous";
 
 int connection_ap_process_inbuf(connection_t *conn) {
 
@@ -19,8 +20,8 @@
 //  log(LOG_DEBUG,"connection_ap_process_inbuf(): state %d.",conn->state);
 
   switch(conn->state) {
-    case AP_CONN_STATE_SS_WAIT:
-      return ap_handshake_process_ss(conn);
+    case AP_CONN_STATE_SOCKS_WAIT:
+      return ap_handshake_process_socks(conn);
     case AP_CONN_STATE_OPEN:
       return connection_package_raw_inbuf(conn);
     default:
@@ -30,107 +31,91 @@
   return 0;
 }
 
-int ap_handshake_process_ss(connection_t *conn) {
-  uint16_t len;
+int ap_handshake_process_socks(connection_t *conn) {
+  char c;
+  socks4_t socks4_info; 
+  static char destaddr[512]; /* XXX there's a race condition waiting to happen here */
+  static int destlen=0;
 
   assert(conn);
 
-  log(LOG_DEBUG,"ap_handshake_process_ss() entered.");
+  log(LOG_DEBUG,"ap_handshake_process_socks() entered.");
 
-  if(!conn->ss_received) { /* try to pull it in */
+  if(!conn->socks_version) { /* try to pull it in */
 
-    if(conn->inbuf_datalen < sizeof(ss_t)) /* entire ss available? */
+    if(conn->inbuf_datalen < sizeof(socks4_t)) /* basic info available? */
       return 0; /* not yet */
 
-    if(connection_fetch_from_buf((char *)&conn->ss,sizeof(ss_t),conn) < 0)
+    if(connection_fetch_from_buf((char *)&socks4_info,sizeof(socks4_t),conn) < 0)
       return -1;
 
-    conn->ss_received = sizeof(ss_t);
-    log(LOG_DEBUG,"ap_handshake_process_ss(): Successfully read ss.");
+    log(LOG_DEBUG,"ap_handshake_process_socks(): Successfully read socks info.");
 
-    if ((conn->ss.version == 0) || (conn->ss.version != OR_VERSION)) { /* unsupported version */
-      log(LOG_NOTICE,"ap_handshake_process_ss(): ss: Unsupported version '%c'.",conn->ss.version);
-      if(tolower(conn->ss.version) == 'g') {
-        log(LOG_NOTICE,"ap_handshake_process_ss(): are you using the onion proxy as a web proxy?");
-      }
-      return -1;
-    }
-    if (conn->ss.addr_fmt != SS_ADDR_FMT_ASCII_HOST_PORT) { /* unrecognized address format */
-      log(LOG_DEBUG,"ap_handshake_process_ss(): ss: Unrecognized address format.");
+    if(socks4_info.version != 4) {
+      log(LOG_NOTICE,"ap_handshake_process_socks(): Unrecognized version %d.",socks4_info.version);
+      ap_handshake_socks_reply(conn, SOCKS4_REQUEST_REJECT);
       return -1;
     }
-  }
-
-  if(!conn->dest_addr) { /* no dest_addr found yet */
-
-    if(conn->inbuf_datalen < sizeof(uint16_t))
-      return 0; /* not yet */
-
-    if(connection_fetch_from_buf((char *)&len,sizeof(uint16_t),conn) < 0)
-      return -1;
+    conn->socks_version = socks4_info.version;
 
-    len = ntohs(len);
-    if(len > 512) {
-      log(LOG_DEBUG,"ap_handshake_process_ss(): Addr length %d too high.",len);
+    if(socks4_info.command != 1) { /* not a connect? we don't support it. */
+      log(LOG_NOTICE,"ap_handshake_process_socks(): command %d not '1'.",socks4_info.command);
+      ap_handshake_socks_reply(conn, SOCKS4_REQUEST_REJECT);
       return -1;
     }
 
-    conn->dest_addr = malloc(len+1);
-    if(!conn->dest_addr) {
-      log(LOG_DEBUG,"ap_handshake_process_ss(): Addr malloc failed");
+    conn->dest_port = ntohs(*(uint16_t*)&socks4_info.destport);
+    if(!conn->dest_port) {
+      log(LOG_NOTICE,"ap_handshake_process_socks(): Port is zero.");
+      ap_handshake_socks_reply(conn, SOCKS4_REQUEST_REJECT);
       return -1;
     }
+    log(LOG_NOTICE,"ap_handshake_process_socks(): Dest port is %d.",conn->dest_port);
 
-    conn->dest_addr[len] = 0; /* null terminate it */
-    conn->dest_addr_len = len;
-    log(LOG_DEBUG,"Preparing a dest_addr of %d+1 bytes.",len);
-  }
-  if(conn->dest_addr_len != conn->dest_addr_received) { /* try to fetch it all in */
-
-    if(conn->inbuf_datalen < conn->dest_addr_len)
-      return 0; /* not yet */
-
-    if(connection_fetch_from_buf(conn->dest_addr,conn->dest_addr_len,conn) < 0)
+    if(socks4_info.destip[0] || 
+       socks4_info.destip[1] ||
+       socks4_info.destip[2] ||
+       !socks4_info.destip[3]) { /* must be in form 0.0.0.x, at least for now */
+      log(LOG_NOTICE,"ap_handshake_process_socks(): destip not in form 0.0.0.x.");
+      ap_handshake_socks_reply(conn, SOCKS4_REQUEST_REJECT);
       return -1;
-    log(LOG_DEBUG,"ap_handshake_process_ss(): Read dest_addr '%s'.",conn->dest_addr);
+    }
+    log(LOG_DEBUG,"ap_handshake_process_socks(): Successfully read destip (0.0.0.x.)");
 
-    conn->dest_addr_received = conn->dest_addr_len;
   }
-  /* now do the same thing for port */
-  if(!conn->dest_port) { /* no dest_port found yet */
-
-    if(conn->inbuf_datalen < sizeof(uint16_t))
-      return 0; /* not yet */
-
-    if(connection_fetch_from_buf((char *)&len,sizeof(uint16_t),conn) < 0)
-      return -1;
 
-    len = ntohs(len);
-    if(len > 10) {
-      log(LOG_DEBUG,"ap_handshake_process_ss(): Port length %d too high.",len);
-      return -1;
-    }
-
-    conn->dest_port = malloc(len+1);
-    if(!conn->dest_port) {
-      log(LOG_DEBUG,"ap_handshake_process_ss(): Port malloc failed");
-      return -1;
+  if(!conn->read_username) { /* the socks spec says we've got to read stuff until we get a null */
+    while(conn->inbuf_datalen) {
+      if(connection_fetch_from_buf((char *)&c,1,conn) < 0)
+        return -1;
+      if(!c) {
+        conn->read_username = 1;
+        log(LOG_DEBUG,"ap_handshake_process_socks(): Successfully read username.");
+        break;
+      }
     }
-
-    conn->dest_port[len] = 0; /* null terminate it */
-    conn->dest_port_len = len;
-    log(LOG_DEBUG,"Preparing a dest_port of %d+1 bytes.",len);
   }
-  if(conn->dest_port_len != conn->dest_port_received) { /* try to fetch it all in */
 
-    if(conn->inbuf_datalen < conn->dest_port_len)
-      return 0; /* not yet */
-
-    if(connection_fetch_from_buf(conn->dest_port,conn->dest_port_len,conn) < 0)
-      return -1;
-    log(LOG_DEBUG,"ap_handshake_process_ss(): Read dest_port '%s'.",conn->dest_port);
+  if(!conn->dest_addr) { /* no dest_addr found yet */
 
-    conn->dest_port_received = conn->dest_port_len;
+    while(conn->inbuf_datalen) {
+      if(connection_fetch_from_buf((char *)&c,1,conn) < 0)
+        return -1;
+      destaddr[destlen++] = c;
+      if(destlen > 500) {
+        log(LOG_NOTICE,"ap_handshake_process_socks(): dest_addr too long!");
+        ap_handshake_socks_reply(conn, SOCKS4_REQUEST_REJECT);
+        destlen = 0;
+        return -1;
+      }
+      if(!c) { /* we found the null; we're done */
+        conn->dest_addr = strdup(destaddr);
+        destlen = 0;
+        log(LOG_NOTICE,"ap_handshake_process_socks(): successfully read dest addr '%s'",
+          conn->dest_addr);
+        break;
+      }
+    }
   }
 
   /* now we're all ready to make an onion, etc */
@@ -291,6 +276,7 @@
   }
   free(tmpbuf);
 
+#if 0
   /* deliver the ss in a data cell */
   cell.command = CELL_DATA;
   cell.aci = circ->n_aci;
@@ -302,12 +288,13 @@
     circuit_close(circ);
     return -1;
   }
+#endif
 
   /* deliver the dest_addr in a data cell */
   cell.command = CELL_DATA;
   cell.aci = circ->n_aci;
-  cell.length = ap_conn->dest_addr_len+1;
-  strncpy(cell.payload, ap_conn->dest_addr, ap_conn->dest_addr_len+1);
+  strncpy(cell.payload, ap_conn->dest_addr, CELL_PAYLOAD_SIZE);
+  cell.length = strlen(cell.payload)+1;
   log(LOG_DEBUG,"ap_handshake_send_onion(): Sending a data cell for addr...");
   if(circuit_deliver_data_cell(&cell, circ, circ->n_conn, 'e') < 0) {
     log(LOG_DEBUG,"ap_handshake_send_onion(): failed to deliver addr cell. Closing.");
@@ -318,8 +305,8 @@
   /* deliver the dest_port in a data cell */
   cell.command = CELL_DATA;
   cell.aci = circ->n_aci;
-  cell.length = ap_conn->dest_port_len+1;
-  strncpy(cell.payload, ap_conn->dest_port, ap_conn->dest_port_len+1);
+  snprintf(cell.payload, CELL_PAYLOAD_SIZE, "%d", ap_conn->dest_port);
+  cell.length = strlen(cell.payload)+1;
   log(LOG_DEBUG,"ap_handshake_send_onion(): Sending a data cell for port...");
   if(circuit_deliver_data_cell(&cell, circ, circ->n_conn, 'e') < 0) {
     log(LOG_DEBUG,"ap_handshake_send_onion(): failed to deliver port cell. Closing.");
@@ -336,13 +323,24 @@
   return 0;
 }
 
-int connection_ap_send_connected(connection_t *conn) {
-  char zero=0;
+int ap_handshake_socks_reply(connection_t *conn, char result) {
+  socks4_t socks4_info; 
 
   assert(conn);
 
-  /* give the AP a "0" byte, because it wants to hear that we've connected */
-  return connection_write_to_buf(&zero, 1, conn);
+  socks4_info.version = 4;
+  socks4_info.command = result;
+  socks4_info.destport[0] = socks4_info.destport[1] = 0;
+  socks4_info.destip[0] = socks4_info.destip[1] = socks4_info.destip[2] = socks4_info.destip[3] = 0;
+
+  connection_write_to_buf((char *)&socks4_info, sizeof(socks4_t), conn); 
+  return connection_flush_buf(conn); /* try to flush it, in case we're about to close the conn */
+}
+
+int connection_ap_send_connected(connection_t *conn) {
+  assert(conn);
+
+  return ap_handshake_socks_reply(conn, SOCKS4_REQUEST_GRANTED);
 }
 
 int connection_ap_process_data_cell(cell_t *cell, connection_t *conn) {
@@ -388,7 +386,7 @@
 }
 
 int connection_ap_handle_listener_read(connection_t *conn) {
-  log(LOG_NOTICE,"AP: Received a connection request. Waiting for SS.");
-  return connection_handle_listener_read(conn, CONN_TYPE_AP, AP_CONN_STATE_SS_WAIT);
+  log(LOG_NOTICE,"AP: Received a connection request. Waiting for socksinfo.");
+  return connection_handle_listener_read(conn, CONN_TYPE_AP, AP_CONN_STATE_SOCKS_WAIT);
 } 
 

Index: connection_exit.c
===================================================================
RCS file: /home/or/cvsroot/src/or/connection_exit.c,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -d -r1.10 -r1.11
--- connection_exit.c	17 Sep 2002 08:14:37 -0000	1.10
+++ connection_exit.c	22 Sep 2002 11:09:07 -0000	1.11
@@ -93,6 +93,7 @@
   switch(conn->state) {
     case EXIT_CONN_STATE_CONNECTING_WAIT:
       log(LOG_DEBUG,"connection_exit_process_data_cell(): state is connecting_wait. cell length %d.", cell->length);
+#if 0
       if(!conn->ss_received) { /* this cell contains the ss */
         if(cell->length != sizeof(ss_t)) {
           log(LOG_DEBUG,"connection_exit_process_data_cell(): Supposed to contain SS but wrong size. Closing.");
@@ -104,8 +105,10 @@
           return -1;
         }
         conn->ss_received = 1;
-	log(LOG_DEBUG,"connection_exit_process_data_cell(): SS received.");
-      } else if (!conn->addr) { /* this cell contains the dest addr */
+        log(LOG_DEBUG,"connection_exit_process_data_cell(): SS received.");
+      } else 
+#endif
+      if (!conn->addr) { /* this cell contains the dest addr */
         if(!memchr(cell->payload,0,cell->length)) {
           log(LOG_DEBUG,"connection_exit_process_data_cell(): dest_addr cell has no \\0. Closing.");
           return -1;
@@ -117,7 +120,7 @@
           return -1;
         }
         memcpy(&conn->addr, rent->h_addr,rent->h_length); 
-	log(LOG_DEBUG,"connection_exit_process_data_cell(): addr is %s.",cell->payload);
+        log(LOG_DEBUG,"connection_exit_process_data_cell(): addr is %s.",cell->payload);
       } else if (!conn->port) { /* this cell contains the dest port */
         if(!memchr(cell->payload,'\0',cell->length)) {
           log(LOG_DEBUG,"connection_exit_process_data_cell(): dest_port cell has no \\0. Closing.");
@@ -167,6 +170,10 @@
         conn->s = s;
         connection_set_poll_socket(conn);
         conn->state = EXIT_CONN_STATE_OPEN;
+        if(connection_wants_to_flush(conn)) { /* in case there are any queued data cells */
+          log(LOG_NOTICE,"connection_exit_process_data_cell(): tell roger: newly connected conn had data waiting!");
+//          connection_start_writing(conn);
+        }
         connection_watch_events(conn, POLLIN);
 
         /* also, deliver a 'connected' cell back through the circuit. */

Index: main.c
===================================================================
RCS file: /home/or/cvsroot/src/or/main.c,v
retrieving revision 1.22
retrieving revision 1.23
diff -u -d -r1.22 -r1.23
--- main.c	21 Sep 2002 22:41:48 -0000	1.22
+++ main.c	22 Sep 2002 11:09:07 -0000	1.23
@@ -13,8 +13,6 @@
         { NULL };
 
 static struct pollfd poll_array[MAXCONNECTIONS];
-/*  =       { [0 ... MAXCONNECTIONS-1] = { -1, 0, 0 } };
- */
 
 static int nfds=0; /* number of connections currently active */
 

Index: or.h
===================================================================
RCS file: /home/or/cvsroot/src/or/or.h,v
retrieving revision 1.23
retrieving revision 1.24
diff -u -d -r1.23 -r1.24
--- or.h	21 Sep 2002 22:41:48 -0000	1.23
+++ or.h	22 Sep 2002 11:09:07 -0000	1.24
@@ -46,7 +46,7 @@
 			      can be overridden by config file */
 
 #define MAX_BUF_SIZE (640*1024)
-#define DEFAULT_BANDWIDTH_OP 1024
+#define DEFAULT_BANDWIDTH_OP 102400
 
 #define ACI_TYPE_LOWER 0
 #define ACI_TYPE_HIGHER 1
@@ -100,7 +100,7 @@
 #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_SOCKS_WAIT 0
 #define AP_CONN_STATE_OR_WAIT 1
 #define AP_CONN_STATE_OPEN 2
 
@@ -147,6 +147,21 @@
   unsigned char payload[120];
 } cell_t;
 
+#define SOCKS4_REQUEST_GRANTED          90
+#define SOCKS4_REQUEST_REJECT           91
+#define SOCKS4_REQUEST_IDENT_FAILED     92
+#define SOCKS4_REQUEST_IDENT_CONFLICT   93
+
+/* structure of a socks client operation */
+typedef struct {
+   unsigned char version;     /* socks version number */
+   unsigned char command;     /* command code */
+   unsigned char destport[2]; /* destination port, network order */
+   unsigned char destip[4];   /* destination address */
+   /* userid follows, terminated by a NULL */
+   /* dest host follows, terminated by a NULL */
+} socks4_t;
+
 typedef struct
 { 
 
@@ -190,12 +205,14 @@
 
 /* used by exit and ap: */
 
-  ss_t ss; /* standard structure */
-  int ss_received; /* size of ss, received so far */
+//  ss_t ss; /* standard structure */
+//  int ss_received; /* size of ss, received so far */
 
-  char *dest_addr, *dest_port;
-  uint16_t dest_addr_len, dest_port_len;
-  uint16_t dest_addr_received, dest_port_received;
+  char socks_version; 
+  char read_username;
+
+  char *dest_addr;
+  uint16_t dest_port;
   
 /* used by OR, to keep state while connect()ing: Kludge. */
 
@@ -460,7 +477,7 @@
 
 int connection_ap_process_inbuf(connection_t *conn);
 
-int ap_handshake_process_ss(connection_t *conn);
+int ap_handshake_process_socks(connection_t *conn);
 
 int ap_handshake_create_onion(connection_t *conn);
 
@@ -472,6 +489,7 @@
 
 int ap_handshake_send_onion(connection_t *ap_conn, connection_t *or_conn, circuit_t *circ);
 
+int ap_handshake_socks_reply(connection_t *conn, char result);
 int connection_ap_send_connected(connection_t *conn);
 int connection_ap_process_data_cell(cell_t *cell, connection_t *conn);
 



More information about the tor-commits mailing list