service
automech.[ch]
+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)
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
exec 3>automech.h.new
cat <<END
-#include "mech.h"
+#include "forwarder.h"
const struct mechanism *const mechanismlists[]= {
END
#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;
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;
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,
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) {
}
}
+ alarm(timeout);
+
different= !public_remote_specd ||
memcmp(&this_saddr,&public_remote,sizeof(this_saddr));
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) {
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) {
#include <stdio.h>
#include <time.h>
+#include <sys/utsname.h>
#include "automech.h"
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);
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))
* encoding for MAC: do CBC and prepend last ciphertext block
*/
-#include "mech.h"
+#include "forwarder.h"
#include "blowfish.h"
struct mechdata {
/*
*/
-#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) { }
* the number of bytes appended
*/
-#include "mech.h"
+#include "forwarder.h"
struct mechdata {
unsigned mask;
#include <netinet/in.h>
-#include "mech.h"
+#include "forwarder.h"
struct mechdata {
uint32_t number;
}
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 }
};
#include <stdint.h>
#include <netinet/in.h>
-#include "mech.h"
+#include "forwarder.h"
struct mechdata {
uint32_t max_skew, max_age;
--- /dev/null
+/*
+ */
+
+#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;
+}