Code cleanup
This commit is contained in:
14
src/main.c
14
src/main.c
@@ -25,7 +25,7 @@
|
|||||||
#define GT_BUFFER_SIZE (4*1024*1024)
|
#define GT_BUFFER_SIZE (4*1024*1024)
|
||||||
#define GT_TIMEOUT (1000)
|
#define GT_TIMEOUT (1000)
|
||||||
|
|
||||||
struct netio {
|
struct fdbuf {
|
||||||
int fd;
|
int fd;
|
||||||
buffer_t read;
|
buffer_t read;
|
||||||
buffer_t write;
|
buffer_t write;
|
||||||
@@ -680,8 +680,8 @@ int main (int argc, char **argv)
|
|||||||
if (!ai)
|
if (!ai)
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
struct netio tun = { .fd = -1 };
|
struct fdbuf tun = { .fd = -1 };
|
||||||
struct netio sock = { .fd = -1 };
|
struct fdbuf sock = { .fd = -1 };
|
||||||
|
|
||||||
tun.fd = tun_create(dev, option_is_set(opts, "multiqueue"));
|
tun.fd = tun_create(dev, option_is_set(opts, "multiqueue"));
|
||||||
|
|
||||||
@@ -846,8 +846,10 @@ int main (int argc, char **argv)
|
|||||||
if (r>0)
|
if (r>0)
|
||||||
sock.write.read += r;
|
sock.write.read += r;
|
||||||
} else {
|
} else {
|
||||||
if (stop_loop)
|
if (stop_loop) {
|
||||||
|
gt_log("%s: shutdown\n", sockname);
|
||||||
shutdown(sock.fd, SHUT_WR);
|
shutdown(sock.fd, SHUT_WR);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
buffer_shift(&sock.write);
|
buffer_shift(&sock.write);
|
||||||
@@ -876,8 +878,10 @@ int main (int argc, char **argv)
|
|||||||
size_t size = buffer_read_size(&tun.write);
|
size_t size = buffer_read_size(&tun.write);
|
||||||
ssize_t ip_size = ip_get_size(tun.write.read, size);
|
ssize_t ip_size = ip_get_size(tun.write.read, size);
|
||||||
|
|
||||||
if (!ip_size)
|
if (!ip_size) {
|
||||||
|
gt_log("%s: bad packet!\n", sockname);
|
||||||
goto restart;
|
goto restart;
|
||||||
|
}
|
||||||
|
|
||||||
if (ip_size<0 || (size_t)ip_size>size)
|
if (ip_size<0 || (size_t)ip_size>size)
|
||||||
break;
|
break;
|
||||||
|
|||||||
28
src/tun.c
28
src/tun.c
@@ -56,15 +56,16 @@ int tun_create (char *name, int multiqueue)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
gt_print("tun name: %s\n", ifr.ifr_name);
|
gt_log("tun name: %s\n", ifr.ifr_name);
|
||||||
|
|
||||||
return fd;
|
return fd;
|
||||||
}
|
}
|
||||||
#elif defined(__APPLE__)
|
#elif defined(__APPLE__)
|
||||||
int tun_create (_unused_ char *name, _unused_ int mq)
|
int tun_create (_unused_ char *name, _unused_ int mq)
|
||||||
{
|
{
|
||||||
for (unsigned dev_id = 0U; dev_id<32U; dev_id++) {
|
for (unsigned dev_id = 0; dev_id < 32; dev_id++) {
|
||||||
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);
|
||||||
|
|
||||||
@@ -91,7 +92,7 @@ int tun_create (_unused_ char *name, _unused_ int mq)
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
gt_print("tun name: /dev/utun%u\n", dev_id);
|
gt_log("tun name: /dev/utun%u\n", dev_id);
|
||||||
|
|
||||||
return fd;
|
return fd;
|
||||||
}
|
}
|
||||||
@@ -101,17 +102,19 @@ int tun_create (_unused_ char *name, _unused_ int mq)
|
|||||||
#else
|
#else
|
||||||
int tun_create (_unused_ char *name, _unused_ int mq)
|
int tun_create (_unused_ char *name, _unused_ int mq)
|
||||||
{
|
{
|
||||||
for (unsigned dev_id = 0U; dev_id<32U; dev_id++) {
|
for (unsigned dev_id = 0; dev_id < 32; dev_id++) {
|
||||||
char dev_path[11U];
|
char dev_path[11];
|
||||||
|
|
||||||
sngt_print(dev_path, sizeof(dev_path), "/dev/tun%u", dev_id);
|
snprintf(dev_path, sizeof(dev_path), "/dev/tun%u", dev_id);
|
||||||
|
|
||||||
int fd = open(dev_path, O_RDWR);
|
int fd = open(dev_path, O_RDWR);
|
||||||
|
|
||||||
if (fd!=-1) {
|
if (fd==-1)
|
||||||
gt_print("tun name: /dev/tun%u\n", dev_id);
|
continue;
|
||||||
return fd;
|
|
||||||
}
|
gt_log("tun name: %s\n", dev_path);
|
||||||
|
|
||||||
|
return fd;
|
||||||
}
|
}
|
||||||
|
|
||||||
return -1;
|
return -1;
|
||||||
@@ -125,6 +128,7 @@ ssize_t tun_read (int fd, void *data, size_t size)
|
|||||||
|
|
||||||
#ifdef GT_BSD_TUN
|
#ifdef GT_BSD_TUN
|
||||||
uint32_t family;
|
uint32_t family;
|
||||||
|
|
||||||
struct iovec iov[2] = {
|
struct iovec iov[2] = {
|
||||||
{ .iov_base = &family, .iov_len = sizeof(family) },
|
{ .iov_base = &family, .iov_len = sizeof(family) },
|
||||||
{ .iov_base = data, .iov_len = size }
|
{ .iov_base = data, .iov_len = size }
|
||||||
@@ -140,7 +144,7 @@ ssize_t tun_read (int fd, void *data, size_t size)
|
|||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
if (errno)
|
if (errno)
|
||||||
perror("readv");
|
perror("tun read");
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -189,7 +193,7 @@ ssize_t tun_write (int fd, const void *data, size_t size)
|
|||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
if (errno)
|
if (errno)
|
||||||
perror("write");
|
perror("tun write");
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user