stun

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

commit f05be7cab79ef6d8852f900826347bf34030ba22
parent 06f762a2c6bed9bd942a92daa2cad841c0f5f93f
Author: sin <sin@2f30.org>
Date:   Mon, 21 Mar 2016 11:15:40 +0000

Add timeout for challenge-response and close the socket on timeout

Diffstat:
Mstun.c | 27+++++++++++++++++++++++----
1 file changed, 23 insertions(+), 4 deletions(-)

diff --git a/stun.c b/stun.c @@ -25,6 +25,7 @@ #include "arg.h" +#define CHALLENGETIMEO 1000 /* in ms*/ #define HDRLEN 2 #define MTU 1440 @@ -248,14 +249,32 @@ int challenge(int netfd) { unsigned char buf[4]; + struct pollfd pfd[1]; uint32_t n, reply; + int ret; pack32(buf, n = arc4random()); - if (writenet(netfd, buf, sizeof(buf)) <= 0 || - readnet(netfd, buf, sizeof(buf)) <= 0) + if (writenet(netfd, buf, sizeof(buf)) <= 0) return -1; - reply = unpack32(buf); - return n + 1 == reply; + pfd[0].fd = netfd; + pfd[0].events = POLLIN; + ret = poll(pfd, 1, CHALLENGETIMEO); + switch (ret) { + case -1: + warn("poll"); + return -1; + case 0: + warnx("challenge-response timed out"); + return -1; + default: + if (pfd[0].revents & (POLLIN | POLLHUP)) { + if (readnet(netfd, buf, sizeof(buf)) <= 0) + return -1; + reply = unpack32(buf); + return n + 1 == reply; + } + } + return -1; } int