stun

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

commit a530a7d26665b113eb2d8fbdc887321dab1a2f75
parent 7b677c29851dc9db9f4f4f18ecc44b64fbedaba4
Author: sin <sin@2f30.org>
Date:   Tue, 12 Apr 2016 15:00:07 +0100

cleanup error handling in netpkt.c

Diffstat:
Mnetpkt.c | 56++++++++++++++++----------------------------------------
1 file changed, 16 insertions(+), 40 deletions(-)

diff --git a/netpkt.c b/netpkt.c @@ -74,14 +74,8 @@ netread(int fd, unsigned char *pt, size_t ptlen, size_t *outlen) break; case STATENONCE: while (rbufrem > 0) { - n = read(fd, rbuf + rbuftotal, rbufrem); - if (n == 0) { - return PKTFAILED; - } else if (n < 0) { - if (errno == EWOULDBLOCK) - return PKTPARTIAL; - return PKTFAILED; - } + if ((n = read(fd, rbuf + rbuftotal, rbufrem)) <= 0) + goto out; rbuftotal += n; rbufrem -= n; } @@ -92,14 +86,8 @@ netread(int fd, unsigned char *pt, size_t ptlen, size_t *outlen) break; case STATEHDR: while (rbufrem > 0) { - n = read(fd, rbuf + rbuftotal, rbufrem); - if (n == 0) { - return PKTFAILED; - } else if (n < 0) { - if (errno == EWOULDBLOCK); - return PKTPARTIAL; - return PKTFAILED; - } + if ((n = read(fd, rbuf + rbuftotal, rbufrem)) <= 0) + goto out; rbuftotal += n; rbufrem -= n; } @@ -115,14 +103,8 @@ netread(int fd, unsigned char *pt, size_t ptlen, size_t *outlen) break; case STATEPAYLOAD: while (rbufrem > 0) { - n = read(fd, rbuf + rbuftotal, rbufrem); - if (n == 0) { - return PKTFAILED; - } else if (n < 0) { - if (errno == EWOULDBLOCK); - return PKTPARTIAL; - return PKTFAILED; - } + if ((n = read(fd, rbuf + rbuftotal, rbufrem)) <= 0) + goto out; rbuftotal += n; rbufrem -= n; } @@ -133,14 +115,8 @@ netread(int fd, unsigned char *pt, size_t ptlen, size_t *outlen) break; case STATETAG: while (rbufrem > 0) { - n = read(fd, rbuf + rbuftotal, rbufrem); - if (n == 0) { - return PKTFAILED; - } else if (n < 0) { - if (errno == EWOULDBLOCK); - return PKTPARTIAL; - return PKTFAILED; - } + if ((n = read(fd, rbuf + rbuftotal, rbufrem)) <= 0) + goto out; rbuftotal += n; rbufrem -= n; } @@ -160,19 +136,19 @@ netread(int fd, unsigned char *pt, size_t ptlen, size_t *outlen) case STATEDISCARD: state = STATEINITIAL; for (;;) { - n = read(fd, rbuf, maxbuflen); - if (n == 0) { - return PKTFAILED; - } else if (n < 0) { - if (errno == EWOULDBLOCK) - return PKTPARTIAL; - return PKTFAILED; - } + if ((n = read(fd, rbuf, maxbuflen)) <= 0) + goto out; } break; } } return PKTFAILED; +out: + if (n == 0) + return PKTFAILED; + if (errno == EWOULDBLOCK) + return PKTPARTIAL; + return PKTFAILED; } void