From: ian Date: Tue, 30 May 2000 18:48:16 +0000 (+0000) Subject: Actual forwarder program compiles apparently ok. X-Git-Tag: userv-utils-0-1-finger-mergeup-1~12 X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?p=userv-utils.git;a=commitdiff_plain;h=f9e59051127e72847e1bee84ad52670e875f7d2f;ds=sidebyside Actual forwarder program compiles apparently ok. --- diff --git a/ipif/.cvsignore b/ipif/.cvsignore index 03796d0..db26654 100644 --- a/ipif/.cvsignore +++ b/ipif/.cvsignore @@ -1,2 +1,3 @@ service automech.[ch] +udptunnel-forwarder diff --git a/ipif/Makefile b/ipif/Makefile index 4637bbc..122a72e 100644 --- a/ipif/Makefile +++ b/ipif/Makefile @@ -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 diff --git a/ipif/automechgen.sh b/ipif/automechgen.sh index bfa1c87..a712a73 100755 --- a/ipif/automechgen.sh +++ b/ipif/automechgen.sh @@ -3,7 +3,7 @@ exec >automech.c.new exec 3>automech.h.new cat < #include -#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) { diff --git a/ipif/forwarder.h b/ipif/forwarder.h index fbaf2e2..08eee56 100644 --- a/ipif/forwarder.h +++ b/ipif/forwarder.h @@ -6,6 +6,7 @@ #include #include +#include #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)) diff --git a/ipif/mech-blowfish.c b/ipif/mech-blowfish.c index 898abe6..07ec8c1 100644 --- a/ipif/mech-blowfish.c +++ b/ipif/mech-blowfish.c @@ -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 { diff --git a/ipif/mech-null.c b/ipif/mech-null.c index 0e9f8e9..8ff117c 100644 --- a/ipif/mech-null.c +++ b/ipif/mech-null.c @@ -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) { } diff --git a/ipif/mech-pkcs5.c b/ipif/mech-pkcs5.c index 3603276..85b5863 100644 --- a/ipif/mech-pkcs5.c +++ b/ipif/mech-pkcs5.c @@ -7,7 +7,7 @@ * the number of bytes appended */ -#include "mech.h" +#include "forwarder.h" struct mechdata { unsigned mask; diff --git a/ipif/mech-sequence.c b/ipif/mech-sequence.c index 2b6bcb3..2090b37 100644 --- a/ipif/mech-sequence.c +++ b/ipif/mech-sequence.c @@ -9,7 +9,7 @@ #include -#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 } }; diff --git a/ipif/mech-timestamp.c b/ipif/mech-timestamp.c index f679b10..e250c08 100644 --- a/ipif/mech-timestamp.c +++ b/ipif/mech-timestamp.c @@ -14,7 +14,7 @@ #include #include -#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 index 0000000..c215390 --- /dev/null +++ b/ipif/utils.c @@ -0,0 +1,142 @@ +/* + */ + +#include +#include +#include +#include +#include + +#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; +}