warp-vpn

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

commit ac13c3d064f18ac41b1d8fd0a3f330811d5eba97
parent 6e63a724cbce44a1a3f98fa4ba278ac4c833661b
Author: sin <sin@2f30.org>
Date:   Tue, 12 Apr 2016 12:07:11 +0100

rename net.c to netpkt.c

Diffstat:
Makefile | 4++--
net.c | 87-------------------------------------------------------------------------------
netpkt.c | 87+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 89 insertions(+), 89 deletions(-)

diff --git a/Makefile b/Makefile @@ -2,8 +2,8 @@ include config.mk DISTFILES = Makefile README WHATSNEW UNLICENSE arg.h auth.c \ config.mk crypto.c dev_bsd.c dev_linux.c log.c \ - net.c stun.8 stun.c stun.h util.c -OBJ = $(EXTRAOBJ) auth.o crypto.o log.o net.o stun.o util.o + netpkt.c stun.8 stun.c stun.h util.c +OBJ = $(EXTRAOBJ) auth.o crypto.o log.o netpkt.o stun.o util.o BIN = stun all: $(BIN) diff --git a/net.c b/net.c @@ -1,87 +0,0 @@ -#include <sys/time.h> - -#include <errno.h> -#include <stdint.h> -#include <stdlib.h> - -#if defined(__linux__) -#include <bsd/stdlib.h> -#endif - -#include "stun.h" - -int -writenet(int fd, unsigned char *pt, int ptlen) -{ - unsigned char *pkt; - size_t noncelen = cryptononcelen(); - size_t taglen = cryptotaglen(); - size_t pktlen = noncelen + HDRLEN + ptlen + taglen; - size_t outlen; - int n; - - if (!(pkt = malloc(pktlen))) - return -1; - - arc4random_buf(pkt, noncelen); - pack16(&pkt[noncelen], ptlen); - if (!cryptoseal(&pkt[noncelen + HDRLEN], &outlen, - ptlen + taglen, pkt, noncelen, - pt, ptlen, &pkt[noncelen], HDRLEN)) { - free(pkt); - logwarn("cryptoseal failed"); - return -1; - } - - n = writeall(fd, pkt, pktlen); - free(pkt); - return n; -} - -/* - * Read one complete packet off the network. If the payload - * length has been tampered with the tag will either not match - * or the read will timeout after RCVTIMEO ms. Timing out is - * necessary to make sure the two ends synchronize again. - */ -int -readnet(int fd, unsigned char *pt, int ptlen) -{ - unsigned char *pkt; - size_t noncelen = cryptononcelen(); - size_t taglen = cryptotaglen(); - size_t pktlen = noncelen + HDRLEN + ptlen + taglen; - size_t outlen; - int n, ctlen; - - if (!(pkt = malloc(pktlen))) - return -1; - - if ((n = readall(fd, pkt, noncelen)) <= 0) - goto err; - if ((n = readall(fd, &pkt[noncelen], HDRLEN)) <= 0) - goto err; - /* if payload len is bogus cap it */ - if ((ctlen = unpack16(&pkt[noncelen])) > ptlen) - ctlen = ptlen; - if ((n = readall(fd, &pkt[noncelen + HDRLEN], ctlen + taglen)) <= 0) - goto err; - - if (!cryptoopen(pt, &outlen, ptlen, pkt, noncelen, - &pkt[noncelen + HDRLEN], ctlen + taglen, - &pkt[noncelen], HDRLEN)) { - free(pkt); - logwarn("cryptoopen failed"); - return BADPKT; - } - - free(pkt); - return outlen; -err: - free(pkt); - if (n == 0) - return 0; - if (errno != EWOULDBLOCK) - return -1; - return BADPKT; -} diff --git a/netpkt.c b/netpkt.c @@ -0,0 +1,87 @@ +#include <sys/time.h> + +#include <errno.h> +#include <stdint.h> +#include <stdlib.h> + +#if defined(__linux__) +#include <bsd/stdlib.h> +#endif + +#include "stun.h" + +int +writenet(int fd, unsigned char *pt, int ptlen) +{ + unsigned char *pkt; + size_t noncelen = cryptononcelen(); + size_t taglen = cryptotaglen(); + size_t pktlen = noncelen + HDRLEN + ptlen + taglen; + size_t outlen; + int n; + + if (!(pkt = malloc(pktlen))) + return -1; + + arc4random_buf(pkt, noncelen); + pack16(&pkt[noncelen], ptlen); + if (!cryptoseal(&pkt[noncelen + HDRLEN], &outlen, + ptlen + taglen, pkt, noncelen, + pt, ptlen, &pkt[noncelen], HDRLEN)) { + free(pkt); + logwarn("cryptoseal failed"); + return -1; + } + + n = writeall(fd, pkt, pktlen); + free(pkt); + return n; +} + +/* + * Read one complete packet off the network. If the payload + * length has been tampered with the tag will either not match + * or the read will timeout after RCVTIMEO ms. Timing out is + * necessary to make sure the two ends synchronize again. + */ +int +readnet(int fd, unsigned char *pt, int ptlen) +{ + unsigned char *pkt; + size_t noncelen = cryptononcelen(); + size_t taglen = cryptotaglen(); + size_t pktlen = noncelen + HDRLEN + ptlen + taglen; + size_t outlen; + int n, ctlen; + + if (!(pkt = malloc(pktlen))) + return -1; + + if ((n = readall(fd, pkt, noncelen)) <= 0) + goto err; + if ((n = readall(fd, &pkt[noncelen], HDRLEN)) <= 0) + goto err; + /* if payload len is bogus cap it */ + if ((ctlen = unpack16(&pkt[noncelen])) > ptlen) + ctlen = ptlen; + if ((n = readall(fd, &pkt[noncelen + HDRLEN], ctlen + taglen)) <= 0) + goto err; + + if (!cryptoopen(pt, &outlen, ptlen, pkt, noncelen, + &pkt[noncelen + HDRLEN], ctlen + taglen, + &pkt[noncelen], HDRLEN)) { + free(pkt); + logwarn("cryptoopen failed"); + return BADPKT; + } + + free(pkt); + return outlen; +err: + free(pkt); + if (n == 0) + return 0; + if (errno != EWOULDBLOCK) + return -1; + return BADPKT; +}