commit acbdc8a1c66824541ddd39a07b8b8ca1c97ee828
parent 41a5831f0f97a4498607533845954b7a0f9565ce
Author: sin <sin@2f30.org>
Date: Thu, 14 Apr 2016 11:42:06 +0100
set address family directly in devwrite()
Diffstat:
4 files changed, 12 insertions(+), 15 deletions(-)
diff --git a/dev_bsd.c b/dev_bsd.c
@@ -47,16 +47,19 @@ devopen(char *ifname)
}
int
-devwrite(int fd, unsigned char *buf, int len, int af)
+devwrite(int fd, unsigned char *buf, int len)
{
struct iovec iov[2];
- uint32_t type = htonl(af);
- int n;
+ uint32_t type;
+ int n, af;
switch (devtype) {
case TAPDEV:
return write(fd, buf, len);
case TUNDEV:
+ if ((af = ipversion(buf)) < 0)
+ return -1;
+ type = htonl(af);
iov[0].iov_base = &type;
iov[0].iov_len = sizeof(type);
iov[1].iov_base = buf;
diff --git a/dev_linux.c b/dev_linux.c
@@ -42,7 +42,7 @@ devopen(char *ifname)
}
int
-devwrite(int fd, unsigned char *buf, int len, int af)
+devwrite(int fd, unsigned char *buf, int len)
{
return write(fd, buf, len);
}
diff --git a/stun.h b/stun.h
@@ -51,7 +51,7 @@ int cryptoopen(unsigned char *, size_t *, size_t, const unsigned char *,
/* dev_*.c */
int devopen(char *);
-int devwrite(int, unsigned char *, int, int);
+int devwrite(int, unsigned char *, int);
int devread(int, unsigned char *, int);
/* log.c */
diff --git a/tunnel.c b/tunnel.c
@@ -8,7 +8,7 @@ tunnel(int netfd, int devfd)
unsigned char buf[MAXPAYLOADLEN];
size_t outlen;
struct pollfd pfd[2];
- int n, af;
+ int n;
pfd[0].fd = netfd;
pfd[0].events = POLLIN;
@@ -22,16 +22,10 @@ tunnel(int netfd, int devfd)
if (pfd[0].revents & (POLLIN | POLLHUP)) {
n = netread(netfd, buf, sizeof(buf), &outlen);
- if (n == PKTFAILED) {
+ if (n == PKTFAILED)
return -1;
- } else if (n == PKTCOMPLETE) {
- if (devtype == TUNDEV) {
- if ((af = ipversion(buf)) > 0)
- devwrite(devfd, buf, outlen, af);
- } else {
- devwrite(devfd, buf, outlen, -1);
- }
- }
+ else if (n == PKTCOMPLETE)
+ devwrite(devfd, buf, outlen);
}
if (pfd[1].revents & (POLLIN | POLLHUP)) {