warp-vpn

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

commit 113583661d4602da401c981ef2cdd2d4173140d5
parent a530a7d26665b113eb2d8fbdc887321dab1a2f75
Author: sin <sin@2f30.org>
Date:   Tue, 12 Apr 2016 15:17:58 +0100

some comments for netpkt.c

Diffstat:
Mnetpkt.c | 32++++++++++++++++++++++++++++----
1 file changed, 28 insertions(+), 4 deletions(-)

diff --git a/netpkt.c b/netpkt.c @@ -9,24 +9,39 @@ #include "stun.h" +/* the various state the input handler can be in */ enum { STATEINITIAL, STATENONCE, STATEHDR, STATEPAYLOAD, STATETAG, - STATEOPEN, + STATEDECRYPT, STATEDISCARD }; +/* output buffer for writing data to the network */ static unsigned char *wbuf; +/* input buffer for reading data from the network */ static unsigned char *rbuf; -static size_t rbuftotal, rbufrem; + +/* total number of bytes read into input buffer */ +static size_t rbuftotal +/* number of bytes remaining to read into buffer for current state */ +static size_t rbufrem; + +/* maximum input/output buffer size */ static size_t maxbuflen; + +/* nonce size in bytes */ static size_t noncelen; +/* tag size in bytes */ static size_t taglen; + +/* state tracking for input handling */ static int state; +/* create a packet and write it out to the network in one go */ int netwrite(int fd, unsigned char *pt, size_t ptlen, size_t *outlen) { @@ -44,6 +59,7 @@ netwrite(int fd, unsigned char *pt, size_t ptlen, size_t *outlen) } *outlen = ptlen; + /* spin until all data is written */ while (buflen > 0) { n = write(fd, p + total, buflen); if (n == 0) { @@ -60,6 +76,10 @@ netwrite(int fd, unsigned char *pt, size_t ptlen, size_t *outlen) return PKTCOMPLETE; } +/* + * Since stun is using non-blocking sockets it needs to handle + * partial reads. A state machine is devised for this purpose. + */ int netread(int fd, unsigned char *pt, size_t ptlen, size_t *outlen) { @@ -121,9 +141,9 @@ netread(int fd, unsigned char *pt, size_t ptlen, size_t *outlen) rbufrem -= n; } if (rbufrem == 0) - state = STATEOPEN; + state = STATEDECRYPT; break; - case STATEOPEN: + case STATEDECRYPT: state = STATEINITIAL; if (!cryptoopen(pt, outlen, ptlen, rbuf, noncelen, &rbuf[noncelen + HDRLEN], @@ -151,6 +171,10 @@ out: return PKTFAILED; } +/* + * reset state machine, required when a fatal error occurs or when + * a client disconnects + */ void netreset(void) {