Compare commits

...

14 Commits

Author SHA1 Message Date
Adrien Gallouët
0664fc3b21 Update mud 2017-01-18 15:27:18 +00:00
Adrien Gallouët
2cb24c0523 Update mud 2017-01-16 16:11:18 +00:00
Adrien Gallouët
65be22202c Update mud 2017-01-12 13:26:35 +00:00
Adrien Gallouët
6cc32bafd9 Code cleanup 2017-01-12 13:26:23 +00:00
Adrien Gallouët
6c268e658f Reset default MTU to 1500 2017-01-06 13:17:46 +00:00
Adrien Gallouët
33e24632d0 Update mud 2017-01-06 11:50:31 +00:00
Adrien Gallouët
e1b4c6aafc Add debug.bin 2017-01-06 11:02:09 +00:00
Adrien Gallouët
09d1932588 Code cleanup 2017-01-06 10:16:13 +00:00
Adrien Gallouët
4988479df4 Drop packets with bad length (too small) 2017-01-05 16:45:14 +00:00
Adrien Gallouët
7779e61c15 Update mud 2017-01-04 14:37:51 +00:00
Adrien Gallouët
2cc8caec35 Don't try to send empty packet 2017-01-04 14:35:26 +00:00
Adrien Gallouët
8c8715187b Code cleanup 2017-01-04 14:27:55 +00:00
Adrien Gallouët
c591a4d3cc Drop too large packets 2017-01-04 14:15:18 +00:00
Adrien Gallouët
76cd7ed4b8 Don't handle errors in tun.c 2017-01-04 14:07:30 +00:00
8 changed files with 68 additions and 76 deletions

View File

@@ -11,5 +11,8 @@ cd ..
[ -x glorytun ] || exit 1 [ -x glorytun ] || exit 1
mkdir -p deploy mkdir -p deploy
cp glorytun deploy/glorytun-$(cat VERSION)-$(uname -m).debug.bin
strip -s glorytun strip -s glorytun
mv glorytun deploy/glorytun-$(cat VERSION)-$(uname -m).bin cp glorytun deploy/glorytun-$(cat VERSION)-$(uname -m).bin

View File

@@ -17,7 +17,6 @@ glorytun_SOURCES = \
src/db.c \ src/db.c \
src/db.h src/db.h
glorytun_CFLAGS += -I$(srcdir)/mud
glorytun_SOURCES += \ glorytun_SOURCES += \
mud/mud.h \ mud/mud.h \
mud/mud.c mud/mud.c

2
mud

Submodule mud updated: fd1a1285ac...f6b6610980

View File

@@ -10,16 +10,17 @@ struct ip_common {
uint16_t size; uint16_t size;
}; };
_pure_ _pure_ static inline uint8_t
static inline uint8_t ip_get_version (const uint8_t *data, size_t size) ip_get_version(const uint8_t *data, size_t size)
{ {
if (size<20) if (size < 20)
return 0; return 0;
return data[0]>>4; return data[0] >> 4;
} }
static inline int ip_get_common (struct ip_common *ic, const uint8_t *data, size_t size) static inline int
ip_get_common(struct ip_common *ic, const uint8_t *data, size_t size)
{ {
ic->version = ip_get_version(data, size); ic->version = ip_get_version(data, size);
@@ -27,14 +28,16 @@ static inline int ip_get_common (struct ip_common *ic, const uint8_t *data, size
case 4: case 4:
ic->tc = data[1]; ic->tc = data[1];
ic->proto = data[9]; ic->proto = data[9];
ic->hdr_size = (data[0]&0xF)<<2; ic->hdr_size = (data[0] & 0xF) << 2;
ic->size = ((data[2]<<8)|data[3]); ic->size = ((data[2] << 8) | data[3]);
return 0; if (ic->size >= 20)
return 0;
break;
case 6: case 6:
ic->tc = ((data[0]&0xF)<<4)|(data[1]>>4); ic->tc = ((data[0] & 0xF) << 4) | (data[1] >> 4);
ic->proto = data[6]; ic->proto = data[6];
ic->hdr_size = 40; ic->hdr_size = 40;
ic->size = ((data[4]<<8)|data[5])+40; ic->size = ((data[4] << 8) | data[5]) + 40;
return 0; return 0;
} }

View File

@@ -18,13 +18,13 @@
#include <arpa/inet.h> #include <arpa/inet.h>
#include <netdb.h> #include <netdb.h>
#include "mud.h" #include "../mud/mud.h"
#ifndef O_CLOEXEC #ifndef O_CLOEXEC
#define O_CLOEXEC 0 #define O_CLOEXEC 0
#endif #endif
#define GT_MTU(X) ((X) - 28) #define GT_MTU(X) ((X)-28)
static struct { static struct {
volatile sig_atomic_t quit; volatile sig_atomic_t quit;
@@ -50,6 +50,7 @@ static struct {
.bind = { .bind = {
.port = 5000, .port = 5000,
}, },
.mtu = 1500,
.timeout = 5000, .timeout = 5000,
.ipv4 = 1, .ipv4 = 1,
#ifdef __linux__ #ifdef __linux__
@@ -385,10 +386,10 @@ main(int argc, char **argv)
if (FD_ISSET(tun_fd, &rfds)) { if (FD_ISSET(tun_fd, &rfds)) {
size_t size = 0; size_t size = 0;
while (sizeof(buf) - size > gt.mtu) { while (sizeof(buf) - size >= gt.mtu) {
const ssize_t r = tun_read(tun_fd, &buf[size], const int r = tun_read(tun_fd, &buf[size], sizeof(buf) - size);
sizeof(buf) - size);
if (r <= 0) if (r <= 0 || r > gt.mtu)
break; break;
struct ip_common ic; struct ip_common ic;
@@ -409,10 +410,8 @@ main(int argc, char **argv)
struct ip_common ic; struct ip_common ic;
if ((ip_get_common(&ic, &buf[q], size - q)) || if ((ip_get_common(&ic, &buf[q], size - q)) ||
(ic.size > size - q)) { (ic.size > size - q))
size = q;
break; break;
}
if (q + ic.size > p + gt.mtu) if (q + ic.size > p + gt.mtu)
break; break;
@@ -423,6 +422,9 @@ main(int argc, char **argv)
tc = ic.tc & 0xFC; tc = ic.tc & 0xFC;
} }
if (p >= q)
break;
int r = mud_send(mud, &buf[p], q - p, tc); int r = mud_send(mud, &buf[p], q - p, tc);
if (r == -1 && errno == EMSGSIZE) { if (r == -1 && errno == EMSGSIZE) {

View File

@@ -2,14 +2,15 @@
#include "common.h" #include "common.h"
static inline size_t str_cpy (char *restrict dst, const char *restrict src, size_t len) static inline size_t
str_cpy(char *restrict dst, const char *restrict src, size_t len)
{ {
if (!dst || !src) if (!dst || !src)
return 0; return 0;
size_t i; size_t i;
for (i=0; i<len && src[i]; i++) for (i = 0; i < len && src[i]; i++)
dst[i] = src[i]; dst[i] = src[i];
dst[i] = 0; dst[i] = 0;
@@ -17,29 +18,29 @@ static inline size_t str_cpy (char *restrict dst, const char *restrict src, size
return i; return i;
} }
_pure_ _pure_ static inline int
static inline int str_empty (const char *restrict str) str_empty(const char *restrict str)
{ {
return !str || !str[0]; return !str || !str[0];
} }
_pure_ _pure_ static inline size_t
static inline size_t str_cmp (const char *restrict sa, const char *restrict sb) str_cmp(const char *restrict sa, const char *restrict sb)
{ {
if (!sa || !sb) if (!sa || !sb)
return 1; return 1;
size_t i = 0; size_t i = 0;
while (sa[i]==sb[i]) while (sa[i] == sb[i])
if (!sa[i++]) if (!sa[i++])
return 0; return 0;
return i+1; return i + 1;
} }
_pure_ _pure_ static inline size_t
static inline size_t str_len (const char *restrict str) str_len(const char *restrict str)
{ {
if (!str) if (!str)
return 0; return 0;
@@ -47,11 +48,12 @@ static inline size_t str_len (const char *restrict str)
return strlen(str); return strlen(str);
} }
static inline char *str_cat (const char **strs, size_t count) static inline char *
str_cat(const char **strs, size_t count)
{ {
size_t size = 1; size_t size = 1;
for (size_t i=0; i<count; i++) for (size_t i = 0; i < count; i++)
size += str_len(strs[i]); size += str_len(strs[i]);
char *str = malloc(size); char *str = malloc(size);
@@ -61,7 +63,7 @@ static inline char *str_cat (const char **strs, size_t count)
char *p = str; char *p = str;
for (size_t i=0; i<count; i++) { for (size_t i = 0; i < count; i++) {
size_t len = str_len(strs[i]); size_t len = str_len(strs[i]);
memcpy(p, strs[i], len); memcpy(p, strs[i], len);
p += len; p += len;

View File

@@ -45,7 +45,9 @@ tun_create_by_id(char *name, size_t size, unsigned id)
str_cpy(ci.ctl_name, UTUN_CONTROL_NAME, sizeof(ci.ctl_name) - 1); str_cpy(ci.ctl_name, UTUN_CONTROL_NAME, sizeof(ci.ctl_name) - 1);
if (ioctl(fd, CTLIOCGINFO, &ci)) { if (ioctl(fd, CTLIOCGINFO, &ci)) {
int err = errno;
close(fd); close(fd);
errno = err;
return -1; return -1;
} }
@@ -58,7 +60,9 @@ tun_create_by_id(char *name, size_t size, unsigned id)
}; };
if (connect(fd, (struct sockaddr *)&sc, sizeof(sc))) { if (connect(fd, (struct sockaddr *)&sc, sizeof(sc))) {
int err = errno;
close(fd); close(fd);
errno = err;
return -1; return -1;
} }
@@ -72,8 +76,10 @@ tun_create_by_name(char *name, size_t size, char *dev_name)
{ {
unsigned id = 0; unsigned id = 0;
if (sscanf(dev_name, "utun%u", &id) != 1) if (sscanf(dev_name, "utun%u", &id) != 1) {
errno = EINVAL;
return -1; return -1;
}
return tun_create_by_id(name, size, id); return tun_create_by_id(name, size, id);
} }
@@ -152,11 +158,11 @@ tun_create(char *dev_name, char **ret_name)
return fd; return fd;
} }
ssize_t int
tun_read(int fd, void *data, size_t size) tun_read(int fd, void *data, size_t size)
{ {
if (!size) if (!size)
return -1; return 0;
#ifdef GT_BSD_TUN #ifdef GT_BSD_TUN
uint32_t family; uint32_t family;
@@ -173,35 +179,24 @@ tun_read(int fd, void *data, size_t size)
}; };
ssize_t ret = readv(fd, iov, 2); ssize_t ret = readv(fd, iov, 2);
#else
ssize_t ret = read(fd, data, size);
#endif
if (ret == -1) { if (ret <= (ssize_t)0)
if (errno == EAGAIN || errno == EINTR) return ret;
return -1;
if (errno) if (ret <= (ssize_t)sizeof(family))
perror("tun read");
return 0;
}
#ifdef GT_BSD_TUN
if (ret < (ssize_t)sizeof(family))
return 0; return 0;
return ret - sizeof(family); return ret - sizeof(family);
#else #else
return ret; return read(fd, data, size);
#endif #endif
} }
ssize_t int
tun_write(int fd, const void *data, size_t size) tun_write(int fd, const void *data, size_t size)
{ {
if (!size) if (!size)
return -1; return 0;
#ifdef GT_BSD_TUN #ifdef GT_BSD_TUN
uint32_t family; uint32_t family;
@@ -214,6 +209,7 @@ tun_write(int fd, const void *data, size_t size)
family = htonl(AF_INET6); family = htonl(AF_INET6);
break; break;
default: default:
errno = EINVAL;
return -1; return -1;
} }
@@ -229,27 +225,16 @@ tun_write(int fd, const void *data, size_t size)
}; };
ssize_t ret = writev(fd, iov, 2); ssize_t ret = writev(fd, iov, 2);
#else
ssize_t ret = write(fd, data, size);
#endif
if (ret == -1) { if (ret <= (ssize_t)0)
if (errno == EAGAIN || errno == EINTR) return ret;
return -1;
if (errno) if (ret <= (ssize_t)sizeof(family))
perror("tun write");
return 0;
}
#ifdef GT_BSD_TUN
if (ret < (ssize_t)sizeof(family))
return 0; return 0;
return ret - sizeof(family); return ret - sizeof(family);
#else #else
return ret; return write(fd, data, size);
#endif #endif
} }

View File

@@ -1,8 +1,6 @@
#pragma once #pragma once
#include <unistd.h> int tun_create (char *, char **);
int tun_read (int, void *, size_t);
int tun_create (char *, char **); int tun_write (int, const void *, size_t);
ssize_t tun_read (int, void *, size_t); int tun_set_mtu (char *, int);
ssize_t tun_write (int, const void *, size_t);
int tun_set_mtu (char *, int);