From 9454b5c9e176e620134c9315ae1e1108407c2416 Mon Sep 17 00:00:00 2001 From: angt Date: Thu, 19 Nov 2015 23:00:57 +0100 Subject: [PATCH] Code cleanup --- src/main.c | 14 +++++++++----- src/tun.c | 28 ++++++++++++++++------------ 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/src/main.c b/src/main.c index 2eca0f2..a258203 100644 --- a/src/main.c +++ b/src/main.c @@ -25,7 +25,7 @@ #define GT_BUFFER_SIZE (4*1024*1024) #define GT_TIMEOUT (1000) -struct netio { +struct fdbuf { int fd; buffer_t read; buffer_t write; @@ -680,8 +680,8 @@ int main (int argc, char **argv) if (!ai) return 1; - struct netio tun = { .fd = -1 }; - struct netio sock = { .fd = -1 }; + struct fdbuf tun = { .fd = -1 }; + struct fdbuf sock = { .fd = -1 }; tun.fd = tun_create(dev, option_is_set(opts, "multiqueue")); @@ -846,8 +846,10 @@ int main (int argc, char **argv) if (r>0) sock.write.read += r; } else { - if (stop_loop) + if (stop_loop) { + gt_log("%s: shutdown\n", sockname); shutdown(sock.fd, SHUT_WR); + } } buffer_shift(&sock.write); @@ -876,8 +878,10 @@ int main (int argc, char **argv) size_t size = buffer_read_size(&tun.write); 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; + } if (ip_size<0 || (size_t)ip_size>size) break; diff --git a/src/tun.c b/src/tun.c index 84864e4..74dc905 100644 --- a/src/tun.c +++ b/src/tun.c @@ -56,15 +56,16 @@ int tun_create (char *name, int multiqueue) return -1; } - gt_print("tun name: %s\n", ifr.ifr_name); + gt_log("tun name: %s\n", ifr.ifr_name); return fd; } #elif defined(__APPLE__) 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; + byte_set(&ci, 0, sizeof(ci)); 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; } - gt_print("tun name: /dev/utun%u\n", dev_id); + gt_log("tun name: /dev/utun%u\n", dev_id); return fd; } @@ -101,17 +102,19 @@ int tun_create (_unused_ char *name, _unused_ int mq) #else int tun_create (_unused_ char *name, _unused_ int mq) { - for (unsigned dev_id = 0U; dev_id<32U; dev_id++) { - char dev_path[11U]; + for (unsigned dev_id = 0; dev_id < 32; dev_id++) { + 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); - if (fd!=-1) { - gt_print("tun name: /dev/tun%u\n", dev_id); - return fd; - } + if (fd==-1) + continue; + + gt_log("tun name: %s\n", dev_path); + + return fd; } return -1; @@ -125,6 +128,7 @@ ssize_t tun_read (int fd, void *data, size_t size) #ifdef GT_BSD_TUN uint32_t family; + struct iovec iov[2] = { { .iov_base = &family, .iov_len = sizeof(family) }, { .iov_base = data, .iov_len = size } @@ -140,7 +144,7 @@ ssize_t tun_read (int fd, void *data, size_t size) return -1; if (errno) - perror("readv"); + perror("tun read"); return 0; } @@ -189,7 +193,7 @@ ssize_t tun_write (int fd, const void *data, size_t size) return -1; if (errno) - perror("write"); + perror("tun write"); return 0; }