stun

simple point to point tunnel
git clone git://git.2f30.org/stun
Log | Files | Refs | README

commit e16058b3c99e12748825795ad42f5af9802acdec
parent f194aa62a707a0b5c8a30ae72a96aca00aebf60b
Author: sin <sin@2f30.org>
Date:   Tue, 12 Apr 2016 14:34:13 +0100

correctly reset state machine

Diffstat:
Mnetpkt.c | 17++++++++---------
Mstun.c | 2++
Mstun.h | 1+
3 files changed, 11 insertions(+), 9 deletions(-)

diff --git a/netpkt.c b/netpkt.c @@ -25,7 +25,7 @@ static size_t rbuftotal, rbufrem; static size_t maxbuflen; static size_t noncelen; static size_t taglen; -static int state = STATEINITIAL; +static int state; int netwrite(int fd, unsigned char *pt, size_t ptlen, size_t *outlen) @@ -76,12 +76,10 @@ netread(int fd, unsigned char *pt, size_t ptlen, size_t *outlen) while (rbufrem > 0) { n = read(fd, rbuf + rbuftotal, rbufrem); if (n == 0) { - state = STATEINITIAL; return PKTFAILED; } else if (n < 0) { if (errno == EWOULDBLOCK) return PKTPARTIAL; - state = STATEINITIAL; return PKTFAILED; } rbuftotal += n; @@ -96,12 +94,10 @@ netread(int fd, unsigned char *pt, size_t ptlen, size_t *outlen) while (rbufrem > 0) { n = read(fd, rbuf + rbuftotal, rbufrem); if (n == 0) { - state = STATEINITIAL; return PKTFAILED; } else if (n < 0) { if (errno == EWOULDBLOCK); return PKTPARTIAL; - state = STATEINITIAL; return PKTFAILED; } rbuftotal += n; @@ -122,12 +118,10 @@ netread(int fd, unsigned char *pt, size_t ptlen, size_t *outlen) while (rbufrem > 0) { n = read(fd, rbuf + rbuftotal, rbufrem); if (n == 0) { - state = STATEINITIAL; return PKTFAILED; } else if (n < 0) { if (errno == EWOULDBLOCK); return PKTPARTIAL; - state = STATEINITIAL; return PKTFAILED; } rbuftotal += n; @@ -142,12 +136,10 @@ netread(int fd, unsigned char *pt, size_t ptlen, size_t *outlen) while (rbufrem > 0) { n = read(fd, rbuf + rbuftotal, rbufrem); if (n == 0) { - state = STATEINITIAL; return PKTFAILED; } else if (n < 0) { if (errno == EWOULDBLOCK); return PKTPARTIAL; - state = STATEINITIAL; return PKTFAILED; } rbuftotal += n; @@ -187,6 +179,12 @@ netread(int fd, unsigned char *pt, size_t ptlen, size_t *outlen) } void +netreset(void) +{ + state = STATEINITIAL; +} + +void netinit(void) { noncelen = cryptononcelen(); @@ -196,4 +194,5 @@ netinit(void) logerr("oom"); if (!(rbuf = malloc(maxbuflen))) logerr("oom"); + netreset(); } diff --git a/stun.c b/stun.c @@ -168,6 +168,7 @@ serversetup(int devfd) tunnel(netfd, devfd); err: + netreset(); close(netfd); if (debug) logdbg("remote peer disconnected: %s", @@ -216,6 +217,7 @@ clientsetup(int devfd) tunnel(netfd, devfd); err: + netreset(); close(netfd); logwarn("connection to %s:%s dropped", host, port); return -1; diff --git a/stun.h b/stun.h @@ -54,6 +54,7 @@ void logerr(char *, ...); /* netpkt.c */ int netwrite(int, unsigned char *, size_t, size_t *); int netread(int, unsigned char *, size_t, size_t *); +void netreset(void); void netinit(void); /* util.c */