Just enjoy snprintf()
Signed-off-by: Adrien Gallouët <adrien@gallouet.fr>
This commit is contained in:
@@ -165,7 +165,7 @@ gt_bind(int argc, char **argv)
|
|||||||
}
|
}
|
||||||
|
|
||||||
char tun_name[64];
|
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) {
|
if (tun_fd == -1) {
|
||||||
gt_log("couldn't create tun device\n");
|
gt_log("couldn't create tun device\n");
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
#include "ctl.h"
|
#include "ctl.h"
|
||||||
#include "str.h"
|
#include "str.h"
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
@@ -41,16 +42,14 @@ ctl_setsun(struct sockaddr_un *dst, const char *dir, const char *file)
|
|||||||
.sun_family = AF_UNIX,
|
.sun_family = AF_UNIX,
|
||||||
};
|
};
|
||||||
|
|
||||||
const char *path[] = {dir, "/", file};
|
int ret = snprintf(sun.sun_path, sizeof(sun.sun_path), "%s/%s", dir, file);
|
||||||
const size_t len = sizeof(sun.sun_path) - 1;
|
|
||||||
|
|
||||||
if (str_cat(sun.sun_path, len, path, COUNT(path)) == len) {
|
if (ret <= 0 || (size_t)ret >= sizeof(sun.sun_path)) {
|
||||||
if (str_cat(NULL, len + 1, path, COUNT(path)) > len) {
|
|
||||||
errno = EINVAL;
|
errno = EINVAL;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
if (dst)
|
||||||
*dst = sun;
|
*dst = sun;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
10
src/iface.c
10
src/iface.c
@@ -1,7 +1,7 @@
|
|||||||
#include "common.h"
|
#include "common.h"
|
||||||
#include "iface.h"
|
#include "iface.h"
|
||||||
#include "str.h"
|
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
#include <net/if.h>
|
#include <net/if.h>
|
||||||
#include <sys/ioctl.h>
|
#include <sys/ioctl.h>
|
||||||
|
|
||||||
@@ -17,21 +17,19 @@ iface_set_mtu(const char *dev_name, size_t mtu)
|
|||||||
.ifr_mtu = (int)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 (ret <= 0 || (size_t)ret >= sizeof(ifr.ifr_name)) {
|
||||||
if (str_len(dev_name, len + 1) > len) {
|
|
||||||
errno = EINTR;
|
errno = EINTR;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
int fd = socket(AF_INET, SOCK_DGRAM, 0);
|
int fd = socket(AF_INET, SOCK_DGRAM, 0);
|
||||||
|
|
||||||
if (fd == -1)
|
if (fd == -1)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
int ret = ioctl(fd, SIOCSIFMTU, &ifr);
|
ret = ioctl(fd, SIOCSIFMTU, &ifr);
|
||||||
|
|
||||||
int err = errno;
|
int err = errno;
|
||||||
close(fd);
|
close(fd);
|
||||||
|
|||||||
29
src/str.h
29
src/str.h
@@ -31,32 +31,3 @@ str_len(const char *restrict str, size_t len)
|
|||||||
|
|
||||||
return strnlen(str, 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);
|
|
||||||
}
|
|
||||||
|
|||||||
30
src/tun.c
30
src/tun.c
@@ -32,9 +32,9 @@
|
|||||||
static int
|
static int
|
||||||
tun_create_by_id(char *name, size_t len, unsigned id)
|
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;
|
errno = EINVAL;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@@ -44,8 +44,9 @@ tun_create_by_id(char *name, size_t len, unsigned id)
|
|||||||
if (fd == -1)
|
if (fd == -1)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
struct ctl_info ci = {0};
|
struct ctl_info ci = {
|
||||||
str_cpy(ci.ctl_name, sizeof(ci.ctl_name) - 1, UTUN_CONTROL_NAME);
|
.ctl_name = UTUN_CONTROL_NAME,
|
||||||
|
};
|
||||||
|
|
||||||
if (ioctl(fd, CTLIOCGINFO, &ci)) {
|
if (ioctl(fd, CTLIOCGINFO, &ci)) {
|
||||||
int err = errno;
|
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,
|
.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) ||
|
if (ret <= 0 || (size_t)ret >= sizeof(ifr.ifr_name)) {
|
||||||
(str_len(dev_name, ifr_len + 1) > ifr_len)) {
|
|
||||||
errno = EINVAL;
|
errno = EINVAL;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@@ -109,8 +109,6 @@ tun_create_by_name(char *name, size_t len, const char *dev_name)
|
|||||||
if (fd == -1)
|
if (fd == -1)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
str_cpy(ifr.ifr_name, ifr_len, dev_name);
|
|
||||||
|
|
||||||
if (ioctl(fd, TUNSETIFF, &ifr)) {
|
if (ioctl(fd, TUNSETIFF, &ifr)) {
|
||||||
int err = errno;
|
int err = errno;
|
||||||
close(fd);
|
close(fd);
|
||||||
@@ -118,8 +116,6 @@ tun_create_by_name(char *name, size_t len, const char *dev_name)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
str_cpy(name, len, ifr.ifr_name);
|
|
||||||
|
|
||||||
return fd;
|
return fd;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -128,21 +124,13 @@ tun_create_by_name(char *name, size_t len, const char *dev_name)
|
|||||||
static int
|
static int
|
||||||
tun_create_by_name(char *name, size_t len, const char *dev_name)
|
tun_create_by_name(char *name, size_t len, const char *dev_name)
|
||||||
{
|
{
|
||||||
char tmp[128];
|
int ret = snprintf(name, len, "/dev/%s", dev_name);
|
||||||
int ret = snprintf(tmp, sizeof(tmp), "/dev/%s", dev_name);
|
|
||||||
|
|
||||||
if (ret <= 0 || (size_t)ret >= sizeof(tmp)) {
|
if (ret <= 0 || (size_t)ret >= len) {
|
||||||
errno = EINVAL;
|
errno = EINVAL;
|
||||||
return -1;
|
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);
|
return open(tmp, O_RDWR);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user