tun_write/read were made macros in linux for write and read

This avoids an unnecessary function call.

Signed-off-by: Nikos Mavrogiannopoulos <n.mavrogiannopoulos@gmail.com>
This commit is contained in:
Nikos Mavrogiannopoulos
2026-05-30 22:49:07 +02:00
parent 2be9af605b
commit 30f33ac9d1
5 changed files with 64 additions and 47 deletions
-4
View File
@@ -32,8 +32,4 @@ struct tun_lease_st {
int fd; int fd;
}; };
ssize_t tun_write(int sockfd, const void *buf, size_t len);
ssize_t tun_read(int sockfd, void *buf, size_t len);
int tun_claim(int sockfd);
#endif #endif
+1
View File
@@ -39,6 +39,7 @@
#include <vpn.h> #include <vpn.h>
#include "html.h" #include "html.h"
#include <worker.h> #include <worker.h>
#include <worker-tun.h>
#include <common.h> #include <common.h>
#include <tlslib.h> #include <tlslib.h>
+3 -43
View File
@@ -49,23 +49,11 @@
#include <ccan/list/list.h> #include <ccan/list/list.h>
#include "vhost.h" #include "vhost.h"
#include "log.h" #include "log.h"
#include "worker-tun.h"
#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
#include <net/if_var.h>
#include <netinet/in_var.h>
#endif
#if defined(__OpenBSD__)
#include <netinet6/in6_var.h>
#endif
#if defined(__DragonFly__)
#include <net/tun/if_tun.h>
#endif
#if defined(__OpenBSD__) || defined(TUNSIFHEAD)
#define TUN_AF_PREFIX 1
#endif
#ifdef TUN_AF_PREFIX #ifdef TUN_AF_PREFIX
/* BSD-specific code, in linux tun_write and tun_read are
* just write and read. */
ssize_t tun_write(int sockfd, const void *buf, size_t len) ssize_t tun_write(int sockfd, const void *buf, size_t len)
{ {
uint32_t head; uint32_t head;
@@ -117,32 +105,4 @@ ssize_t tun_read(int sockfd, void *buf, size_t len)
ret -= sizeof(uint32_t); ret -= sizeof(uint32_t);
return ret; return ret;
} }
#else
ssize_t tun_write(int sockfd, const void *buf, size_t len)
{
return write(sockfd, buf, len);
}
ssize_t tun_read(int sockfd, void *buf, size_t len)
{
return read(sockfd, buf, len);
}
#endif #endif
#ifndef __FreeBSD__
int tun_claim(int sockfd)
{
return 0;
}
#else
/*
* FreeBSD has a mechanism by which a tunnel has a single controlling process,
* and only that one process may close it. When the controlling process closes
* the tunnel, the state is torn down.
*/
int tun_claim(int sockfd)
{
return ioctl(sockfd, TUNSIFPID, 0);
}
#endif /* !__FreeBSD__ */
+59
View File
@@ -0,0 +1,59 @@
/*
* Copyright (C) 2026 Nikos Mavrogiannopoulos
*
* This file is part of ocserv.
*
* ocserv is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*/
#ifndef OC_WORKER_TUN_H
#define OC_WORKER_TUN_H
#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
#include <net/if_var.h>
#include <netinet/in_var.h>
#endif
#if defined(__OpenBSD__)
#include <netinet6/in6_var.h>
#endif
#if defined(__DragonFly__)
#include <net/tun/if_tun.h>
#endif
#if defined(__OpenBSD__) || defined(TUNSIFHEAD)
#define TUN_AF_PREFIX 1
#endif
#ifdef TUN_AF_PREFIX
ssize_t tun_write(int sockfd, const void *buf, size_t len);
ssize_t tun_read(int sockfd, void *buf, size_t len);
#else
#define tun_write write
#define tun_read read
#endif
#ifndef __FreeBSD__
#define tun_claim(sockfd) 0
#else
/*
* FreeBSD has a mechanism by which a tunnel has a single controlling process,
* and only that one process may close it. When the controlling process closes
* the tunnel, the state is torn down.
*/
static int tun_claim(int sockfd)
{
return ioctl(sockfd, TUNSIFPID, 0);
}
#endif /* !__FreeBSD__ */
#endif
+1
View File
@@ -63,6 +63,7 @@ static inline void worker_exit(int status)
#include <poll.h> #include <poll.h>
#include <math.h> #include <math.h>
#include <ev.h> #include <ev.h>
#include "worker-tun.h"
#if defined(__linux__) && !defined(IPV6_PATHMTU) #if defined(__linux__) && !defined(IPV6_PATHMTU)
#define IPV6_PATHMTU 61 #define IPV6_PATHMTU 61