chiark / gitweb /
Actual forwarder program compiles apparently ok.
authorian <ian>
Tue, 30 May 2000 18:48:16 +0000 (18:48 +0000)
committerian <ian>
Tue, 30 May 2000 18:48:16 +0000 (18:48 +0000)
ipif/.cvsignore
ipif/Makefile
ipif/automechgen.sh
ipif/forwarder.c
ipif/forwarder.h
ipif/mech-blowfish.c
ipif/mech-null.c
ipif/mech-pkcs5.c
ipif/mech-sequence.c
ipif/mech-timestamp.c
ipif/utils.c [new file with mode: 0644]

index 03796d07192d0879c2b92d9e1720d5f797358e67..db26654958207ea507e5525e3b2a3d2b6a3b4285 100644 (file)
@@ -1,2 +1,3 @@
 service
 automech.[ch]
+udptunnel-forwarder
index 4637bbc2646514f0f5a6428b6df1f3ea6752e43d..122a72e4b7d296ed6320ef1c815d25cc4a899066 100644 (file)
@@ -35,7 +35,7 @@ TARGETS=      service udptunnel-forwarder
 MECHFILES=     null pkcs5 timestamp sequence blowfish
 MECHOBJS=      $(foreach m, $(MECHFILES), mech-$m.o)
 
-OBJS_FORWARD=  forwarder.o $(MECHOBJS) blowfish.o automech.c
+OBJS_FORWARD=  forwarder.o $(MECHOBJS) blowfish.o automech.c utils.c
 
 all:           $(TARGETS)
 
@@ -52,5 +52,5 @@ udptunnel-forwarder:  $(OBJS_FORWARD)
 automech.c automech.h:         automechgen.sh Makefile
                ./$< $(MECHFILES)
 
-forwarder.o $(MECHOBJS) automech.o:    mech.h automech.h
-blowfish.o mech-blowfish.o:            blowfish.h
+forwarder.o $(MECHOBJS) automech.o utils.o:    forwarder.h automech.h
+blowfish.o mech-blowfish.o:                    blowfish.h
index bfa1c87c832b5e242c95fc04e643259dca4ca411..a712a7345d34b6e5513a8004bc695a0e4c614fba 100755 (executable)
@@ -3,7 +3,7 @@ exec >automech.c.new
 exec 3>automech.h.new
 
 cat <<END
-#include "mech.h"
+#include "forwarder.h"
 const struct mechanism *const mechanismlists[]= {
 END
 
index ae327868a67f17e067f8db41c8f9111d61b4d571..9b7e51ee554181a270b77254d8428ce342bd1ef3 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,8 +107,6 @@ static void inbound(void) {
   int r, i, different, this_saddrlen;
   const char *emsg;
 
-  alarm(timeout);
-
   setnonblock(public_local_fd,1);
   this_saddrlen= sizeof(this_saddr);
   r= recvfrom(public_local_fd, buf_in.base, buffer_size-1, 0,
@@ -178,7 +114,7 @@ static void inbound(void) {
   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) {
@@ -198,6 +134,8 @@ static void inbound(void) {
     }
   }
 
+  alarm(timeout);
+
   different= !public_remote_specd ||
     memcmp(&this_saddr,&public_remote,sizeof(this_saddr));
 
@@ -230,16 +168,7 @@ static void inbound(void) {
   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;
-  }
+  write_must(private_in_fd, buf_in.start, buf_in.size, "write down");
 }
 
 static void sendpacket(const unsigned char *message, size_t size) {
@@ -310,11 +239,11 @@ 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) {
index fbaf2e2433dfb102972c102ec589a8370589182e..08eee56ed4c96276942bd9b0a557172760cd71a2 100644 (file)
@@ -6,6 +6,7 @@
 
 #include <stdio.h>
 #include <time.h>
+#include <sys/utsname.h>
 
 #include "automech.h"
 
@@ -44,6 +45,7 @@ extern const struct mechanism *const mechanismlists[];
 const char *getarg_string(void);
 unsigned long getarg_ulong(void);
 
+#define PROGRAM "udptunnel-forwarder"
 extern char programid[];
 
 void *buf_append(struct buffer *buf, size_t amount);
@@ -56,10 +58,14 @@ void fail(const char *msg);
 void sysdiag(const char *msg);
 void diag(const char *msg);
 
+extern const char *const *argv;
+
 time_t now(void);
 void *xmalloc(size_t sz);
 void get_random(void *ptr, size_t sz);
 void random_key(void *ptr, size_t sz);
+void write_must(int fd, const void *p_in, int sz, const char *what);
+void read_must(int fd, void *p_in, int sz, const char *what);
 
 void arg_assert_fail(const char *msg);
 #define arg_assert(v)  (v ? (void)0 : arg_assert_fail(#v))
index 898abe67386e331ec020020cc187cf4ae2bc45bb..07ec8c13cd89f463ede1271c0b7258aa80d4ffed 100644 (file)
@@ -9,7 +9,7 @@
  * encoding for MAC: do CBC and prepend last ciphertext block
  */
 
-#include "mech.h"
+#include "forwarder.h"
 #include "blowfish.h"
 
 struct mechdata {
index 0e9f8e91866a06bec77277c382c3e446c4eca831..8ff117ccb02253f518abcb2522c02023764e68c4 100644 (file)
@@ -1,7 +1,7 @@
 /*
  */
 
-#include "mech.h"
+#include "forwarder.h"
 
 static void mes_null(struct mechdata **md_r, int *maxprefix_io, int *maxsuffix_io) { }
 static void mds_null(struct mechdata **md_r) { }
index 36032767b18500c1e4cf3d03652071becf9542ba..85b586379e70e5f21c63ded960d19f312a4b8a3b 100644 (file)
@@ -7,7 +7,7 @@
  *           the number of bytes appended
  */
 
-#include "mech.h"
+#include "forwarder.h"
 
 struct mechdata {
   unsigned mask;
index 2b6bcb33c880a3aba9696bb878c08d454013b7d3..2090b37dd9b264ebb570e3868bb83aafb48e6c3e 100644 (file)
@@ -9,7 +9,7 @@
 
 #include <netinet/in.h>
 
-#include "mech.h"
+#include "forwarder.h"
 
 struct mechdata {
   uint32_t number;
@@ -59,7 +59,7 @@ static const char *mdec_skip(struct mechdata *md, struct buffer *buf) {
 }
 
 const struct mechanism mechlist_sequence[]= {
- { "nonce32",         mes_sequence, mds_sequence,  menc_sequence, mdec_skip    },
- { "sequence32",      mes_sequence, mds_sequence,  menc_sequence, mdec_check   },
+ { "nonce",         mes_sequence, mds_sequence,  menc_sequence, mdec_skip    },
+ { "sequence",      mes_sequence, mds_sequence,  menc_sequence, mdec_check   },
  { 0 }
 };
index f679b104e625c889308a1274f07b3cb7466fdf4a..e250c084d89f87fe26472618c7eae06afd2feffd 100644 (file)
@@ -14,7 +14,7 @@
 #include <stdint.h>
 #include <netinet/in.h>
 
-#include "mech.h"
+#include "forwarder.h"
 
 struct mechdata {
   uint32_t max_skew, max_age;
diff --git a/ipif/utils.c b/ipif/utils.c
new file mode 100644 (file)
index 0000000..c215390
--- /dev/null
@@ -0,0 +1,142 @@
+/*
+ */
+
+#include <string.h>
+#include <errno.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <assert.h>
+
+#include "forwarder.h"
+
+const char *const *argv;
+char programid[SYS_NMLN+sizeof(PROGRAM)+3];
+
+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 write_must(int fd, const void *p_in, int sz, const char *what) {
+  const unsigned char *p= p_in;
+  int r;
+  
+  while (sz) {
+    r= write(fd,p,sz);
+    if (r<0) {
+      if (errno == EINTR) continue;
+      else sysfail(what);
+    }
+    assert(r && r <= sz);
+    p += r;
+    sz -= r;
+  }
+}
+
+void read_must(int fd, void *p_in, int sz, const char *what) {
+  unsigned char *p= p_in;
+  int r;
+  
+  while (sz) {
+    r= read(fd,p,sz);
+    if (r<0) {
+      if (errno == EINTR) continue;
+      else sysfail(what);
+    }
+    if (r==0) fail(what);
+    assert(r <= sz);
+    p += r;
+    sz -= 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");
+  }
+
+  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;
+}
+
+void *buf_append(struct buffer *buf, size_t amount) {
+  void *p;
+
+  p= buf->start + buf->size;
+  buf->size += amount;
+  return p;
+}
+  
+void *buf_prepend(struct buffer *buf, size_t amount) {
+  buf->size += amount;
+  return buf->start -= amount;
+}
+
+void *buf_unappend(struct buffer *buf, size_t amount) {
+  if (buf->size < amount) return 0;
+  return buf->start + (buf->size -= amount);
+}
+
+void *buf_unprepend(struct buffer *buf, size_t amount) {
+  void *p;
+
+  p= buf->start;
+  buf->start += amount;
+  buf->size -= amount;
+  return p;
+}