diff --git a/Makefile.am b/Makefile.am index 4511207..db2279f 100644 --- a/Makefile.am +++ b/Makefile.am @@ -14,6 +14,8 @@ glorytun_SOURCES = \ src/option.h \ src/tun.c \ src/tun.h \ + src/iface.c \ + src/iface.h \ src/db.c \ src/db.h diff --git a/src/iface.c b/src/iface.c new file mode 100644 index 0000000..b047889 --- /dev/null +++ b/src/iface.c @@ -0,0 +1,29 @@ +#include "common.h" + +#include "str.h" + +#include +#include + +int +iface_set_mtu(char *dev_name, int mtu) +{ + struct ifreq ifr = { + .ifr_mtu = mtu, + }; + + str_cpy(ifr.ifr_name, dev_name, IFNAMSIZ - 1); + + int fd = socket(AF_INET, SOCK_DGRAM, 0); + + if (fd == -1) + return -1; + + int ret = ioctl(fd, SIOCSIFMTU, &ifr); + + int err = errno; + close(fd); + errno = err; + + return ret; +} diff --git a/src/iface.h b/src/iface.h new file mode 100644 index 0000000..01baf8d --- /dev/null +++ b/src/iface.h @@ -0,0 +1,3 @@ +#pragma once + +int iface_set_mtu (char *, int); diff --git a/src/main.c b/src/main.c index 45c1e03..4da1fe7 100644 --- a/src/main.c +++ b/src/main.c @@ -5,6 +5,7 @@ #include "option.h" #include "str.h" #include "tun.h" +#include "iface.h" #include #include @@ -263,7 +264,7 @@ gt_setup_mtu(struct mud *mud, char *tun_name) gt_log("setup MTU to %i on interface %s\n", mtu, tun_name); - if (tun_set_mtu(tun_name, mtu) == -1) + if (iface_set_mtu(tun_name, mtu) == -1) perror("tun_set_mtu"); } diff --git a/src/tun.c b/src/tun.c index cd88ab1..f2b8532 100644 --- a/src/tun.c +++ b/src/tun.c @@ -239,29 +239,6 @@ tun_write(int fd, const void *data, size_t size) #endif } -int -tun_set_mtu(char *dev_name, int mtu) -{ - struct ifreq ifr = { - .ifr_mtu = mtu, - }; - - str_cpy(ifr.ifr_name, dev_name, IFNAMSIZ - 1); - - int fd = socket(AF_INET, SOCK_DGRAM, 0); - - if (fd == -1) - return -1; - - int ret = ioctl(fd, SIOCSIFMTU, &ifr); - - int err = errno; - close(fd); - errno = err; - - return ret; -} - int tun_set_persist(int fd, int on) { diff --git a/src/tun.h b/src/tun.h index df99d74..86bb252 100644 --- a/src/tun.h +++ b/src/tun.h @@ -3,5 +3,4 @@ int tun_create (char *, char **); int tun_read (int, void *, size_t); int tun_write (int, const void *, size_t); -int tun_set_mtu (char *, int); int tun_set_persist (int, int);