Code cleanup

This commit is contained in:
angt
2015-12-17 13:59:30 +01:00
parent db01c8b33f
commit 75c12b36d6
3 changed files with 100 additions and 65 deletions

View File

@@ -18,7 +18,7 @@ static inline void byte_set (void *dst, const char value, size_t size)
static inline void byte_cpy (void *dst, const void *src, size_t size) static inline void byte_cpy (void *dst, const void *src, size_t size)
{ {
if (!dst || !src) if (!dst)
return; return;
char *restrict d = dst; char *restrict d = dst;
@@ -43,6 +43,12 @@ static inline size_t str_cpy (char *restrict dst, const char *restrict src, size
return i; return i;
} }
_pure_
static inline int str_empty (const char *restrict str)
{
return !str || !str[0];
}
_pure_ _pure_
static inline int str_cmp (const char *restrict sa, const char *restrict sb) static inline int str_cmp (const char *restrict sa, const char *restrict sb)
{ {

View File

@@ -687,10 +687,15 @@ int main (int argc, char **argv)
char *host = NULL; char *host = NULL;
char *port = "5000"; char *port = "5000";
char *dev = PACKAGE_NAME;
char *keyfile = NULL; char *keyfile = NULL;
char *congestion = NULL; char *congestion = NULL;
#ifdef __linux__
char *dev = PACKAGE_NAME;
#else
char *dev = NULL;
#endif
long buffer_size = GT_BUFFER_SIZE; long buffer_size = GT_BUFFER_SIZE;
long ka_count = -1; long ka_count = -1;

View File

@@ -22,15 +22,20 @@
#endif #endif
#if defined(__APPLE__) || defined(__OpenBSD__) #if defined(__APPLE__) || defined(__OpenBSD__)
# define GT_BSD_TUN 1 #define GT_BSD_TUN
#endif #endif
#ifdef __linux__ #ifdef __linux__
int tun_create (char *name, int multiqueue) static int tun_create_by_id (_unused_ char *name, _unused_ size_t size, _unused_ unsigned id, _unused_ int mq)
{
return -1;
}
static int tun_create_by_name (char *name, size_t size, char *dev_name, int mq)
{ {
int fd = open("/dev/net/tun", O_RDWR); int fd = open("/dev/net/tun", O_RDWR);
if (fd<0) { if (fd==-1) {
perror("open /dev/net/tun"); perror("open /dev/net/tun");
return -1; return -1;
} }
@@ -39,7 +44,7 @@ int tun_create (char *name, int multiqueue)
.ifr_flags = IFF_TUN|IFF_NO_PI, .ifr_flags = IFF_TUN|IFF_NO_PI,
}; };
if (multiqueue) { if (mq) {
#ifdef IFF_MULTI_QUEUE #ifdef IFF_MULTI_QUEUE
ifr.ifr_flags |= IFF_MULTI_QUEUE; ifr.ifr_flags |= IFF_MULTI_QUEUE;
#else #else
@@ -47,36 +52,37 @@ int tun_create (char *name, int multiqueue)
#endif #endif
} }
str_cpy(ifr.ifr_name, name, IFNAMSIZ-1); str_cpy(ifr.ifr_name, dev_name, IFNAMSIZ-1);
int ret = ioctl(fd, TUNSETIFF, &ifr); if (ioctl(fd, TUNSETIFF, &ifr)) {
if (ret<0) {
perror("ioctl TUNSETIFF"); perror("ioctl TUNSETIFF");
close(fd);
return -1; return -1;
} }
gt_log("tun name: %s\n", ifr.ifr_name); str_cpy(name, ifr.ifr_name, size-1);
return fd; return fd;
} }
#elif defined(__APPLE__) #elif defined(__APPLE__)
int tun_create (_unused_ char *name, _unused_ int mq) static int tun_create_by_id (char *name, size_t size, unsigned id, _unused_ int mq)
{ {
for (unsigned dev_id = 0; dev_id < 32; dev_id++) { int fd = socket(PF_SYSTEM, SOCK_DGRAM, SYSPROTO_CONTROL);
if (fd==-1) {
perror("socket");
return -1;
}
struct ctl_info ci; struct ctl_info ci;
byte_set(&ci, 0, sizeof(ci)); byte_set(&ci, 0, sizeof(ci));
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);
int fd = socket(PF_SYSTEM, SOCK_DGRAM, SYSPROTO_CONTROL); if (ioctl(fd, CTLIOCGINFO, &ci)) {
perror("ioctl CTLIOCGINFO");
if (fd==-1)
return -1;
if (ioctl(fd, CTLIOCGINFO, &ci)==-1) {
close(fd); close(fd);
continue; return -1;
} }
struct sockaddr_ctl sc = { struct sockaddr_ctl sc = {
@@ -84,43 +90,61 @@ int tun_create (_unused_ char *name, _unused_ int mq)
.sc_len = sizeof(sc), .sc_len = sizeof(sc),
.sc_family = AF_SYSTEM, .sc_family = AF_SYSTEM,
.ss_sysaddr = AF_SYS_CONTROL, .ss_sysaddr = AF_SYS_CONTROL,
.sc_unit = dev_id+1, .sc_unit = id+1,
}; };
if (connect(fd, (struct sockaddr *)&sc, sizeof(sc))==-1) { if (connect(fd, (struct sockaddr *)&sc, sizeof(sc))) {
perror("connect");
close(fd); close(fd);
continue; return -1;
} }
gt_log("tun name: /dev/utun%u\n", dev_id); snprintf(name, size, "/dev/utun%u", id);
return fd; return fd;
} }
static int tun_create_by_name (char *name, size_t size, char *dev_name, _unused_ int mq)
{
unsigned id = 0;
if (sscanf(dev_name, "/dev/utun%u", &id)!=1)
return -1; return -1;
return tun_create_by_id(name, size, id, mq);
} }
#else #else
int tun_create (_unused_ char *name, _unused_ int mq) static int tun_create_by_id (char *name, size_t size, unsigned id, _unused_ int mq)
{ {
for (unsigned dev_id = 0; dev_id < 32; dev_id++) { snprintf(name, size, "/dev/tun%u", id);
char dev_path[11]; return open(name, O_RDWR);
}
snprintf(dev_path, sizeof(dev_path), "/dev/tun%u", dev_id); static int tun_create_by_name (char *name, size_t size, char *dev_name, _unused_ int mq)
{
str_cpy(name, dev_name, size-1);
return open(name, O_RDWR);
}
#endif
int fd = open(dev_path, O_RDWR); int tun_create (char *dev_name, int mq)
{
char name[64];
int fd = -1;
if (fd==-1) if (str_empty(dev_name)) {
continue; for (unsigned id=0; id<32 && fd==-1; id++)
fd = tun_create_by_id(name, sizeof(name), id, mq);
} else {
fd = tun_create_by_name(name, sizeof(name), dev_name, mq);
}
gt_log("tun name: %s\n", dev_path); if (fd!=-1)
gt_print("tun name: %s\n", name);
return fd; return fd;
} }
return -1;
}
#endif
ssize_t tun_read (int fd, void *data, size_t size) ssize_t tun_read (int fd, void *data, size_t size)
{ {
if (!size) if (!size)