diff --git a/src/bind.c b/src/bind.c index e0cf1a3..2da5646 100644 --- a/src/bind.c +++ b/src/bind.c @@ -165,7 +165,7 @@ gt_bind(int argc, char **argv) } char tun_name[64]; - const int tun_fd = tun_create(tun_name, sizeof(tun_name) - 1, dev); + const int tun_fd = tun_create(tun_name, sizeof(tun_name), dev); if (tun_fd == -1) { gt_log("couldn't create tun device\n"); diff --git a/src/ctl.c b/src/ctl.c index f28529f..cbc708f 100644 --- a/src/ctl.c +++ b/src/ctl.c @@ -2,6 +2,7 @@ #include "ctl.h" #include "str.h" +#include #include #include #include @@ -41,17 +42,15 @@ ctl_setsun(struct sockaddr_un *dst, const char *dir, const char *file) .sun_family = AF_UNIX, }; - const char *path[] = {dir, "/", file}; - const size_t len = sizeof(sun.sun_path) - 1; + int ret = snprintf(sun.sun_path, sizeof(sun.sun_path), "%s/%s", dir, file); - if (str_cat(sun.sun_path, len, path, COUNT(path)) == len) { - if (str_cat(NULL, len + 1, path, COUNT(path)) > len) { - errno = EINVAL; - return -1; - } + if (ret <= 0 || (size_t)ret >= sizeof(sun.sun_path)) { + errno = EINVAL; + return -1; } - *dst = sun; + if (dst) + *dst = sun; return 0; } diff --git a/src/iface.c b/src/iface.c index 1728155..c39af08 100644 --- a/src/iface.c +++ b/src/iface.c @@ -1,7 +1,7 @@ #include "common.h" #include "iface.h" -#include "str.h" +#include #include #include @@ -17,13 +17,11 @@ iface_set_mtu(const char *dev_name, size_t mtu) .ifr_mtu = (int)mtu, }; - const size_t len = sizeof(ifr.ifr_name) - 1; + int ret = snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s", dev_name); - if (str_cpy(ifr.ifr_name, len, dev_name) == len) { - if (str_len(dev_name, len + 1) > len) { - errno = EINTR; - return -1; - } + if (ret <= 0 || (size_t)ret >= sizeof(ifr.ifr_name)) { + errno = EINTR; + return -1; } int fd = socket(AF_INET, SOCK_DGRAM, 0); @@ -31,7 +29,7 @@ iface_set_mtu(const char *dev_name, size_t mtu) if (fd == -1) return -1; - int ret = ioctl(fd, SIOCSIFMTU, &ifr); + ret = ioctl(fd, SIOCSIFMTU, &ifr); int err = errno; close(fd); diff --git a/src/str.h b/src/str.h index b547c6a..b012277 100644 --- a/src/str.h +++ b/src/str.h @@ -31,32 +31,3 @@ str_len(const char *restrict str, size_t len) return strnlen(str, len); } - -static inline size_t -str_cat(char *dst, size_t dst_len, const char **src, size_t count) -{ - if (count && !src) - return 0; - - size_t len = 0; - - for (size_t i = 0; i < count && dst_len > len; i++) { - size_t n = str_len(src[i], dst_len - len); - - if (dst && n) - memmove(&dst[len], src[i], n); - - len += n; - } - - if (dst) - dst[len] = 0; - - return len; -} - -static inline size_t -str_cpy(char *dst, size_t dst_len, const char *src) -{ - return str_cat(dst, dst_len, &src, 1); -} diff --git a/src/tun.c b/src/tun.c index d519542..05c0a1e 100644 --- a/src/tun.c +++ b/src/tun.c @@ -32,9 +32,9 @@ static int tun_create_by_id(char *name, size_t len, unsigned id) { - int ret = snprintf(name, len + 1, "utun%u", id); + int ret = snprintf(name, len, "utun%u", id); - if (ret <= 0 || ret > len) { + if (ret <= 0 || (size_t)ret >= len) { errno = EINVAL; return -1; } @@ -44,8 +44,9 @@ tun_create_by_id(char *name, size_t len, unsigned id) if (fd == -1) return -1; - struct ctl_info ci = {0}; - str_cpy(ci.ctl_name, sizeof(ci.ctl_name) - 1, UTUN_CONTROL_NAME); + struct ctl_info ci = { + .ctl_name = UTUN_CONTROL_NAME, + }; if (ioctl(fd, CTLIOCGINFO, &ci)) { int err = errno; @@ -96,10 +97,9 @@ tun_create_by_name(char *name, size_t len, const char *dev_name) .ifr_flags = IFF_TUN | IFF_NO_PI, }; - const size_t ifr_len = sizeof(ifr.ifr_name) - 1; + int ret = snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s", dev_name); - if ((len < ifr_len) || - (str_len(dev_name, ifr_len + 1) > ifr_len)) { + if (ret <= 0 || (size_t)ret >= sizeof(ifr.ifr_name)) { errno = EINVAL; return -1; } @@ -109,8 +109,6 @@ tun_create_by_name(char *name, size_t len, const char *dev_name) if (fd == -1) return -1; - str_cpy(ifr.ifr_name, ifr_len, dev_name); - if (ioctl(fd, TUNSETIFF, &ifr)) { int err = errno; close(fd); @@ -118,8 +116,6 @@ tun_create_by_name(char *name, size_t len, const char *dev_name) return -1; } - str_cpy(name, len, ifr.ifr_name); - return fd; } @@ -128,21 +124,13 @@ tun_create_by_name(char *name, size_t len, const char *dev_name) static int tun_create_by_name(char *name, size_t len, const char *dev_name) { - char tmp[128]; - int ret = snprintf(tmp, sizeof(tmp), "/dev/%s", dev_name); + int ret = snprintf(name, len, "/dev/%s", dev_name); - if (ret <= 0 || (size_t)ret >= sizeof(tmp)) { + if (ret <= 0 || (size_t)ret >= len) { errno = EINVAL; return -1; } - if (str_cpy(name, len, dev_name) == len) { - if (str_len(dev_name, len + 1) > len) { - errno = EINVAL; - return -1; - } - } - return open(tmp, O_RDWR); }