chiark / gitweb /
Works at least without crypto.
[userv-utils.git] / ipif / forwarder.c
index ae327868a67f17e067f8db41c8f9111d61b4d571..a9e43e096e16bd8c62b38a92c7241fa70b8dcddd 100644 (file)
 #include <unistd.h>
 #include <fcntl.h>
 
-#include "mech.h"
+#include "forwarder.h"
 
 #define MAXMECHS 10
-#define PROGRAM "udptunnel-forwarder"
-char programid[SYS_NMLN+sizeof(PROGRAM)+3];
 
-static const char *const *argv;
 static size_t buffer_size;
 
 static int public_local_fd, private_in_fd, private_out_fd;
@@ -68,74 +65,15 @@ static size_t accum_used, accum_avail;
 static time_t nextsendka;
 
 
-void arg_assert_fail(const char *msg) {
-  fprintf(stderr, PROGRAM ": argument error: %s\n",msg);
-  exit(12);
-}
-
-void sysfail(const char *msg) {
-  fprintf(stderr, "%s: fatal system error: %s: %s\n", programid, msg, strerror(errno));
-  exit(8);
-}
-
-void fail(const char *msg) {
-  fprintf(stderr, "%s: fatal error: %s\n", programid, msg);
-  exit(4);
-}
-
-void sysdiag(const char *msg) {
-  fprintf(stderr, "%s: system/network error: %s: %s\n", programid, msg, strerror(errno));
-}
-
-void diag(const char *msg) {
-  fprintf(stderr, "%s: %s\n", programid, msg);
-}
-
-time_t now(void) {
-  time_t r;
-  if (time(&r) == (time_t)-1) sysfail("get time of day");
-  return r;
-}
-
-void *xmalloc(size_t sz) {
-  void *r;
-  r= malloc(sz);
-  if (!r) sysfail("allocate memory");
-  return r;
-}
-
-void get_random(void *ptr, size_t sz) {
-  static FILE *randfile;
-
-  size_t r;
-
-  if (!randfile) {
-    randfile= fopen("/dev/urandom","rb");
-    if (!randfile && errno==ENOENT) randfile= fopen("/dev/random","rb");
-    if (!randfile) sysfail("open random number generator");
+void random_key(void *ptr, size_t sz) {
+  if (encdec_keys_write) {
+    get_random(ptr,sz);
+    write_must(encdec_keys_fd,ptr,sz,"write keys datastream");
+  } else {
+    read_must(encdec_keys_fd,ptr,sz,"read keys datastream");
   }
-
-  r= fread(ptr,1,sz,randfile);
-  if (r == sz) return;
-  (ferror(randfile) ? sysfail : fail)("cannot read random number generator");
 }
 
-const char *getarg_string(void) {
-  const char *arg;
-  
-  arg= *++argv;
-  arg_assert(arg);
-  return arg;
-}
-
-unsigned long getarg_ulong(void) {
-  char *ep;
-  unsigned long ul;
-  
-  ul= strtoul(getarg_string(),&ep,0);
-  arg_assert(!*ep);
-  return ul;
-}
 
 static void setnonblock(int fd, int nonblock) {
   int r;
@@ -169,27 +107,28 @@ static void inbound(void) {
   int r, i, different, this_saddrlen;
   const char *emsg;
 
-  alarm(timeout);
-
+  buf_in.start= buf_in.base+1;
+  buf_in.size= buffer_size-2;
+  
   setnonblock(public_local_fd,1);
   this_saddrlen= sizeof(this_saddr);
-  r= recvfrom(public_local_fd, buf_in.base, buffer_size-1, 0,
+  r= recvfrom(public_local_fd, buf_in.start, buf_in.size, 0,
              &this_saddr, &this_saddrlen);
   if (!r) { diag("empty ciphertext"); return; }
 
   if (r<0) {
-    if (errno != EAGAIN && errno != EINTR) sysdiag("receive");
+    if (errno != EAGAIN && errno != EINTR) { sysdiag("receive"); sleep(1); }
     return;
   }
   if (this_saddr.sin_family != AF_INET) {
-    fprintf(stderr,"%s: received unknown AF %lu",
+    fprintf(stderr,"%s: received unknown AF %lu\n",
            programid, (unsigned long)this_saddr.sin_family);
     return;
   }
   assert(this_saddrlen == sizeof(this_saddr));
 
-  buf_in.size= buffer_size;
-  buf_in.start= buf_in.base;
+  assert(r <= buf_in.size);
+  buf_in.size= r;
   for (i=n_mechs-1; i>=0; i--) {
     emsg= mechs[i]->decode(md_in[i],&buf_in);
     if (emsg) {
@@ -198,21 +137,24 @@ static void inbound(void) {
     }
   }
 
-  different= !public_remote_specd ||
-    memcmp(&this_saddr,&public_remote,sizeof(this_saddr));
+  alarm(timeout);
+
+  different= (!public_remote_specd ||
+             public_remote.sin_addr.s_addr != this_saddr.sin_addr.s_addr ||
+             public_remote.sin_port != this_saddr.sin_port);
 
   if (different) {
 
     if (public_remote_specd==2) {
-      fprintf(stderr, "%s: packet from unexpected sender %s:%lu",
+      fprintf(stderr, "%s: packet from unexpected sender %s:%lu\n",
              programid, inet_ntoa(this_saddr.sin_addr),
-             (unsigned long)this_saddr.sin_port);
+             (unsigned long)ntohs(this_saddr.sin_port));
       return;
     }
 
-    fprintf(stderr, "%s: tunnel open with peer %s:%lu",
+    fprintf(stderr, "%s: tunnel open with peer %s:%lu\n",
            programid, inet_ntoa(this_saddr.sin_addr),
-           (unsigned long)this_saddr.sin_port);
+           (unsigned long)ntohs(this_saddr.sin_port));
     nextsendka= now();
     public_remote_specd= 1;
     memcpy(&public_remote,&this_saddr,sizeof(public_remote));
@@ -224,22 +166,17 @@ static void inbound(void) {
   }
 
   any_recvd= 1;
-  
-  buf_in.start[buf_in.size]= 0300;
-  *--buf_in.start= 0300;
-  buf_in.size+= 2;
 
-  setnonblock(private_in_fd,0);
-  while (buf_in.size) {
-    r= write(private_in_fd, buf_in.start, buf_in.size);
-    assert(r && r <= buf_in.size);
-    if (r<0) {
-      if (errno == EINTR) continue;
-      sysfail("write down");
-    }
-    buf_in.start += r;
-    buf_in.size -= r;
+  if (!buf_in.size || *buf_in.start != 0300) {
+    *--buf_in.start= 0300;
+    buf_in.size++;
   }
+  if (buf_in.start[buf_in.size-1] != 0300) {
+    buf_in.start[buf_in.size++]= 0300;
+  }
+
+  setnonblock(private_in_fd,0);
+  write_must(private_in_fd, buf_in.start, buf_in.size, "write down");
 }
 
 static void sendpacket(const unsigned char *message, size_t size) {
@@ -284,7 +221,7 @@ static void outbound(void) {
     after_eaten= accum_buf;
     while ((delim= memchr(after_eaten, 0300, accum_used))) {
       this_packet= delim - after_eaten;
-      sendpacket(after_eaten, this_packet);
+      if (this_packet) sendpacket(after_eaten, this_packet);
       accum_used -= this_packet+1;
       after_eaten = delim+1;
     }
@@ -310,17 +247,18 @@ int main(int argc, const char *const *const argv_in) {
   sprintf(programid, PROGRAM ": %.*s", SYS_NMLN, uname_result.nodename);
   
   public_local_fd= getarg_ulong();
+  private_in_fd= getarg_ulong();
+  private_out_fd= getarg_ulong();
   mtu2= getarg_ulong() * 2;
   keepalive= getarg_ulong();
   timeout= getarg_ulong();
-  private_in_fd= getarg_ulong();
-  private_out_fd= getarg_ulong();
   
   arg= getarg_string();
   if (*arg) {
     public_remote_specd= 1;
+    public_remote.sin_family= AF_INET;
     arg_assert(inet_aton(arg,&public_remote.sin_addr));
-    public_remote.sin_port= getarg_ulong();
+    public_remote.sin_port= htons(getarg_ulong());
   }
 
   encdec_keys_fd= getarg_ulong();
@@ -351,7 +289,7 @@ int main(int argc, const char *const *const argv_in) {
 
     if (keepalive) {
       tnow= now();
-      if (tnow >= nextsendka) sendpacket("\300",1);
+      if (tnow >= nextsendka && public_remote_specd) sendpacket("\300",1);
       polltimeout= (nextsendka - tnow)*1000;
     } else {
       polltimeout= -1;
@@ -362,7 +300,7 @@ int main(int argc, const char *const *const argv_in) {
     if (r==-1 && errno==EINTR) continue;
     if (r==-1) sysfail("poll");
 
-    if (pollfds[0].revents & POLLIN) inbound();
-    if (pollfds[1].revents & POLLOUT) outbound();
+    if (pollfds[0].revents & (POLLIN|POLLERR)) inbound();
+    if (pollfds[1].revents & (POLLIN|POLLERR)) outbound();
   }
 }