Add helper function tun_uses_header()

Remove code duplication and hide some ifdefs
This commit is contained in:
Erik Ekman 2020-07-03 17:06:14 +02:00
parent 07b2978326
commit d8f3ac8971

View File

@ -507,20 +507,24 @@ read_tun(int tun_fd, char *buf, size_t len)
} }
} }
#else #else
int static int
write_tun(int tun_fd, char *data, size_t len) tun_uses_header(void)
{ {
#if defined (FREEBSD) || defined (NETBSD) #if defined (FREEBSD) || defined (NETBSD)
/* FreeBSD/NetBSD has no header */ /* FreeBSD/NetBSD has no header */
int header = 0; return 0;
#elif defined (DARWIN) #elif defined (DARWIN)
/* Darwin tun has no header, Darwin utun does */ /* Darwin tun has no header, Darwin utun does */
int header = !strncmp(if_name, "utun", 4); return !strncmp(if_name, "utun", 4);
#else /* LINUX/OPENBSD */ #else /* LINUX/OPENBSD */
int header = 1; return 1;
#endif #endif
}
if (!header) { int
write_tun(int tun_fd, char *data, size_t len)
{
if (!tun_uses_header()) {
data += 4; data += 4;
len -= 4; len -= 4;
} else { } else {
@ -551,17 +555,7 @@ write_tun(int tun_fd, char *data, size_t len)
ssize_t ssize_t
read_tun(int tun_fd, char *buf, size_t len) read_tun(int tun_fd, char *buf, size_t len)
{ {
#if defined (FREEBSD) || defined (NETBSD) if (!tun_uses_header()) {
/* FreeBSD/NetBSD has no header */
int header = 0;
#elif defined (DARWIN)
/* Darwin tun has no header, Darwin utun does */
int header = !strncmp(if_name, "utun", 4);
#else /* LINUX/OPENBSD */
int header = 1;
#endif
if (!header) {
int bytes; int bytes;
memset(buf, 0, 4); memset(buf, 0, 4);