warp-vpn

point to point VPN implementation
git clone git://git.2f30.org/warp-vpn
Log | Files | Refs | README

commit c44fd2fe0c0c255909b3078f57a5057db8befc83
parent 3cc01593ecb9dfe2c2ff5f6d3053cdbf18b67aab
Author: sin <sin@2f30.org>
Date:   Wed,  6 Apr 2016 15:34:13 +0100

simplify readnet()

Diffstat:
Mstun.c | 47++++++++++++++---------------------------------
1 file changed, 14 insertions(+), 33 deletions(-)

diff --git a/stun.c b/stun.c @@ -405,41 +405,14 @@ readnet(int fd, unsigned char *pt, int ptlen) if (!(pkt = malloc(pktlen))) return -1; - n = readall(fd, pkt, noncelen); - if (n == 0) { - free(pkt); - return 0; - } else if (n < 0) { - free(pkt); - if (errno != EWOULDBLOCK) - return -1; - return BADPKT; - } - - n = readall(fd, &pkt[noncelen], HDRLEN); - if (n == 0) { - free(pkt); - return 0; - } else if (n < 0) { - free(pkt); - if (errno != EWOULDBLOCK) - return -1; - return BADPKT; - } - + if ((n = readall(fd, pkt, noncelen)) <= 0) + goto err; + if ((n = readall(fd, &pkt[noncelen], HDRLEN)) <= 0) + goto err; if ((ctlen = unpack16(&pkt[noncelen])) > ptlen) ctlen = ptlen; - - n = readall(fd, &pkt[noncelen + HDRLEN], ctlen + taglen); - if (n == 0) { - free(pkt); - return 0; - } else if (n < 0) { - free(pkt); - if (errno != EWOULDBLOCK) - return -1; - return BADPKT; - } + if ((n = readall(fd, &pkt[noncelen + HDRLEN], ctlen + taglen)) <= 0) + goto err; if (!EVP_AEAD_CTX_open(&dctx, pt, &outlen, ptlen, pkt, noncelen, &pkt[noncelen + HDRLEN], ctlen + taglen, @@ -447,8 +420,16 @@ readnet(int fd, unsigned char *pt, int ptlen) free(pkt); return BADPKT; } + free(pkt); return outlen; +err: + free(pkt); + if (n == 0) + return 0; + if (errno != EWOULDBLOCK) + return -1; + return BADPKT; } int