|
From: flichtenheld (C. Review) <ge...@op...> - 2025-11-14 12:37:38
|
Attention is currently required from: plaisthos.
Hello plaisthos,
I'd like you to do a code review.
Please visit
http://gerrit.openvpn.net/c/openvpn/+/1378?usp=email
to review the following change.
Change subject: tun: Refactor BSD write_tun/read_tun
......................................................................
tun: Refactor BSD write_tun/read_tun
There was a lot of duplicated code, merge it together.
Change-Id: Ifd9384287648d1f37a625d9ed6a09733208fa56c
Signed-off-by: Frank Lichtenheld <fr...@li...>
---
M src/openvpn/tun.c
1 file changed, 27 insertions(+), 228 deletions(-)
git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/78/1378/1
diff --git a/src/openvpn/tun.c b/src/openvpn/tun.c
index 06b7ae5..22f0c17 100644
--- a/src/openvpn/tun.c
+++ b/src/openvpn/tun.c
@@ -1685,10 +1685,10 @@
#endif
}
-#if defined(TARGET_OPENBSD) || defined(TARGET_DARWIN)
+#if defined(TARGET_FREEBSD) || defined(TARGET_DRAGONFLY) || defined(TARGET_NETBSD) || defined(TARGET_OPENBSD) || defined(TARGET_DARWIN)
/*
- * OpenBSD and Mac OS X when using utun
+ * BSDs and Mac OS X when using utun
* have a slightly incompatible TUN device from
* the rest of the world, in that it prepends a
* uint32 to the beginning of the IP header
@@ -1733,11 +1733,17 @@
{
u_int32_t type;
struct iovec iv[2];
- struct openvpn_iphdr *iph;
+#if defined(TARGET_FREEBSD) || defined(TARGET_DRAGONFLY)
+ struct ip *iph = (struct ip *)buf;
- iph = (struct openvpn_iphdr *)buf;
+ bool is_ipv6 = iph->ip_v == 6;
+#else
+ struct openvpn_iphdr *iph = (struct openvpn_iphdr *)buf;
- if (OPENVPN_IPH_GET_VER(iph->version_len) == 6)
+ bool is_ipv6 = OPENVPN_IPH_GET_VER(iph->version_len) == 6;
+#endif
+
+ if (is_ipv6)
{
type = htonl(AF_INET6);
}
@@ -1784,7 +1790,22 @@
#pragma GCC diagnostic pop
#endif
-#endif /* if defined (TARGET_OPENBSD) || defined(TARGET_DARWIN) */
+#if !defined(TARGET_DARWIN)
+int
+write_tun(struct tuntap *tt, uint8_t *buf, int len)
+{
+ write_tun_header(tt, buf, len);
+}
+
+int
+read_tun(struct tuntap *tt, uint8_t *buf, int len)
+{
+ read_tun_header(tt, buf, len);
+}
+#endif
+
+
+#endif /* defined(TARGET_FREEBSD) || defined(TARGET_DRAGONFLY) || defined(TARGET_NETBSD) || if defined (TARGET_OPENBSD) || defined(TARGET_DARWIN) */
bool
tun_name_is_fixed(const char *dev)
@@ -2688,18 +2709,6 @@
argv_free(&argv);
}
-int
-write_tun(struct tuntap *tt, uint8_t *buf, int len)
-{
- return write_tun_header(tt, buf, len);
-}
-
-int
-read_tun(struct tuntap *tt, uint8_t *buf, int len)
-{
- return read_tun_header(tt, buf, len);
-}
-
#elif defined(TARGET_NETBSD)
/*
@@ -2801,88 +2810,8 @@
argv_free(&argv);
}
-static inline int
-netbsd_modify_read_write_return(int len)
-{
- if (len > 0)
- {
- return len > sizeof(u_int32_t) ? len - sizeof(u_int32_t) : 0;
- }
- else
- {
- return len;
- }
-}
-
-int
-write_tun(struct tuntap *tt, uint8_t *buf, int len)
-{
- if (tt->type == DEV_TYPE_TUN)
- {
- u_int32_t type;
- struct iovec iv[2];
- struct openvpn_iphdr *iph;
-
- iph = (struct openvpn_iphdr *)buf;
-
- if (OPENVPN_IPH_GET_VER(iph->version_len) == 6)
- {
- type = htonl(AF_INET6);
- }
- else
- {
- type = htonl(AF_INET);
- }
-
- iv[0].iov_base = (char *)&type;
- iv[0].iov_len = sizeof(type);
- iv[1].iov_base = buf;
- iv[1].iov_len = len;
-
- return netbsd_modify_read_write_return(writev(tt->fd, iv, 2));
- }
- else
- {
- return write(tt->fd, buf, len);
- }
-}
-
-int
-read_tun(struct tuntap *tt, uint8_t *buf, int len)
-{
- if (tt->type == DEV_TYPE_TUN)
- {
- u_int32_t type;
- struct iovec iv[2];
-
- iv[0].iov_base = (char *)&type;
- iv[0].iov_len = sizeof(type);
- iv[1].iov_base = buf;
- iv[1].iov_len = len;
-
- return netbsd_modify_read_write_return(readv(tt->fd, iv, 2));
- }
- else
- {
- return read(tt->fd, buf, len);
- }
-}
-
#elif defined(TARGET_FREEBSD)
-static inline int
-freebsd_modify_read_write_return(int len)
-{
- if (len > 0)
- {
- return len > sizeof(u_int32_t) ? len - sizeof(u_int32_t) : 0;
- }
- else
- {
- return len;
- }
-}
-
void
open_tun(const char *dev, const char *dev_type, const char *dev_node, struct tuntap *tt,
openvpn_net_ctx_t *ctx)
@@ -2955,84 +2884,8 @@
argv_free(&argv);
}
-#if defined(__GNUC__) || defined(__clang__)
-#pragma GCC diagnostic push
-#pragma GCC diagnostic ignored "-Wconversion"
-#endif
-
-int
-write_tun(struct tuntap *tt, uint8_t *buf, int len)
-{
- if (tt->type == DEV_TYPE_TUN)
- {
- u_int32_t type;
- struct iovec iv[2];
- struct ip *iph;
-
- iph = (struct ip *)buf;
-
- if (iph->ip_v == 6)
- {
- type = htonl(AF_INET6);
- }
- else
- {
- type = htonl(AF_INET);
- }
-
- iv[0].iov_base = (char *)&type;
- iv[0].iov_len = sizeof(type);
- iv[1].iov_base = buf;
- iv[1].iov_len = len;
-
- return freebsd_modify_read_write_return(writev(tt->fd, iv, 2));
- }
- else
- {
- return write(tt->fd, buf, len);
- }
-}
-
-int
-read_tun(struct tuntap *tt, uint8_t *buf, int len)
-{
- if (tt->type == DEV_TYPE_TUN)
- {
- u_int32_t type;
- struct iovec iv[2];
-
- iv[0].iov_base = (char *)&type;
- iv[0].iov_len = sizeof(type);
- iv[1].iov_base = buf;
- iv[1].iov_len = len;
-
- return freebsd_modify_read_write_return(readv(tt->fd, iv, 2));
- }
- else
- {
- return read(tt->fd, buf, len);
- }
-}
-
-#if defined(__GNUC__) || defined(__clang__)
-#pragma GCC diagnostic pop
-#endif
-
#elif defined(TARGET_DRAGONFLY)
-static inline int
-dragonfly_modify_read_write_return(int len)
-{
- if (len > 0)
- {
- return len > sizeof(u_int32_t) ? len - sizeof(u_int32_t) : 0;
- }
- else
- {
- return len;
- }
-}
-
void
open_tun(const char *dev, const char *dev_type, const char *dev_node, struct tuntap *tt,
openvpn_net_ctx_t *ctx)
@@ -3059,60 +2912,6 @@
free(tt);
}
-int
-write_tun(struct tuntap *tt, uint8_t *buf, int len)
-{
- if (tt->type == DEV_TYPE_TUN)
- {
- u_int32_t type;
- struct iovec iv[2];
- struct ip *iph;
-
- iph = (struct ip *)buf;
-
- if (iph->ip_v == 6)
- {
- type = htonl(AF_INET6);
- }
- else
- {
- type = htonl(AF_INET);
- }
-
- iv[0].iov_base = (char *)&type;
- iv[0].iov_len = sizeof(type);
- iv[1].iov_base = buf;
- iv[1].iov_len = len;
-
- return dragonfly_modify_read_write_return(writev(tt->fd, iv, 2));
- }
- else
- {
- return write(tt->fd, buf, len);
- }
-}
-
-int
-read_tun(struct tuntap *tt, uint8_t *buf, int len)
-{
- if (tt->type == DEV_TYPE_TUN)
- {
- u_int32_t type;
- struct iovec iv[2];
-
- iv[0].iov_base = (char *)&type;
- iv[0].iov_len = sizeof(type);
- iv[1].iov_base = buf;
- iv[1].iov_len = len;
-
- return dragonfly_modify_read_write_return(readv(tt->fd, iv, 2));
- }
- else
- {
- return read(tt->fd, buf, len);
- }
-}
-
#elif defined(TARGET_DARWIN)
/* Darwin (MacOS X) is mostly "just use the generic stuff", but there
--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/1378?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings?usp=email
Gerrit-MessageType: newchange
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Ifd9384287648d1f37a625d9ed6a09733208fa56c
Gerrit-Change-Number: 1378
Gerrit-PatchSet: 1
Gerrit-Owner: flichtenheld <fr...@li...>
Gerrit-Reviewer: cron2 <ge...@gr...>
Gerrit-Reviewer: plaisthos <arn...@rf...>
Gerrit-CC: openvpn-devel <ope...@li...>
Gerrit-Attention: plaisthos <arn...@rf...>
|
|
From: cron2 (C. Review) <ge...@op...> - 2025-11-14 12:41:10
|
Attention is currently required from: flichtenheld, plaisthos. cron2 has posted comments on this change by flichtenheld. ( http://gerrit.openvpn.net/c/openvpn/+/1378?usp=email ) Change subject: tun: Refactor BSD write_tun/read_tun ...................................................................... Patch Set 1: -Code-Review (1 comment) This change is ready for review. File src/openvpn/tun.c: http://gerrit.openvpn.net/c/openvpn/+/1378/comment/ec691247_5975103b?usp=email : PS1, Line 1798: } ah, and this lost the `return` part when being moved... `return write_tun_header...` -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/1378?usp=email To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings?usp=email Gerrit-MessageType: comment Gerrit-Project: openvpn Gerrit-Branch: master Gerrit-Change-Id: Ifd9384287648d1f37a625d9ed6a09733208fa56c Gerrit-Change-Number: 1378 Gerrit-PatchSet: 1 Gerrit-Owner: flichtenheld <fr...@li...> Gerrit-Reviewer: cron2 <ge...@gr...> Gerrit-Reviewer: plaisthos <arn...@rf...> Gerrit-CC: openvpn-devel <ope...@li...> Gerrit-Attention: plaisthos <arn...@rf...> Gerrit-Attention: flichtenheld <fr...@li...> Gerrit-Comment-Date: Fri, 14 Nov 2025 12:41:01 +0000 Gerrit-HasComments: Yes Gerrit-Has-Labels: Yes |
|
From: cron2 (C. Review) <ge...@op...> - 2025-11-14 12:41:26
|
Attention is currently required from: flichtenheld, plaisthos. cron2 has posted comments on this change by flichtenheld. ( http://gerrit.openvpn.net/c/openvpn/+/1378?usp=email ) Change subject: tun: Refactor BSD write_tun/read_tun ...................................................................... Patch Set 1: Code-Review-2 -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/1378?usp=email To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings?usp=email Gerrit-MessageType: comment Gerrit-Project: openvpn Gerrit-Branch: master Gerrit-Change-Id: Ifd9384287648d1f37a625d9ed6a09733208fa56c Gerrit-Change-Number: 1378 Gerrit-PatchSet: 1 Gerrit-Owner: flichtenheld <fr...@li...> Gerrit-Reviewer: cron2 <ge...@gr...> Gerrit-Reviewer: plaisthos <arn...@rf...> Gerrit-CC: openvpn-devel <ope...@li...> Gerrit-Attention: plaisthos <arn...@rf...> Gerrit-Attention: flichtenheld <fr...@li...> Gerrit-Comment-Date: Fri, 14 Nov 2025 12:41:12 +0000 Gerrit-HasComments: No Gerrit-Has-Labels: Yes |
|
From: flichtenheld (C. Review) <ge...@op...> - 2025-11-14 13:28:39
|
Attention is currently required from: flichtenheld, plaisthos.
Hello cron2, plaisthos,
I'd like you to reexamine a change. Please visit
http://gerrit.openvpn.net/c/openvpn/+/1378?usp=email
to look at the new patch set (#2).
Change subject: tun: Refactor BSD write_tun/read_tun
......................................................................
tun: Refactor BSD write_tun/read_tun
There was a lot of duplicated code, merge it together.
Change-Id: Ifd9384287648d1f37a625d9ed6a09733208fa56c
Signed-off-by: Frank Lichtenheld <fr...@li...>
---
M src/openvpn/tun.c
1 file changed, 20 insertions(+), 229 deletions(-)
git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/78/1378/2
diff --git a/src/openvpn/tun.c b/src/openvpn/tun.c
index 06b7ae5..90505a0 100644
--- a/src/openvpn/tun.c
+++ b/src/openvpn/tun.c
@@ -1685,10 +1685,10 @@
#endif
}
-#if defined(TARGET_OPENBSD) || defined(TARGET_DARWIN)
+#if defined(TARGET_FREEBSD) || defined(TARGET_DRAGONFLY) || defined(TARGET_NETBSD) || defined(TARGET_OPENBSD) || defined(TARGET_DARWIN)
/*
- * OpenBSD and Mac OS X when using utun
+ * BSDs and Mac OS X when using utun
* have a slightly incompatible TUN device from
* the rest of the world, in that it prepends a
* uint32 to the beginning of the IP header
@@ -1733,11 +1733,9 @@
{
u_int32_t type;
struct iovec iv[2];
- struct openvpn_iphdr *iph;
+ struct ip *iph = (struct ip *)buf;
- iph = (struct openvpn_iphdr *)buf;
-
- if (OPENVPN_IPH_GET_VER(iph->version_len) == 6)
+ if (iph->ip_v == 6)
{
type = htonl(AF_INET6);
}
@@ -1784,7 +1782,22 @@
#pragma GCC diagnostic pop
#endif
-#endif /* if defined (TARGET_OPENBSD) || defined(TARGET_DARWIN) */
+#if !defined(TARGET_DARWIN)
+int
+write_tun(struct tuntap *tt, uint8_t *buf, int len)
+{
+ return write_tun_header(tt, buf, len);
+}
+
+int
+read_tun(struct tuntap *tt, uint8_t *buf, int len)
+{
+ return read_tun_header(tt, buf, len);
+}
+#endif
+
+
+#endif /* defined(TARGET_FREEBSD) || defined(TARGET_DRAGONFLY) || defined(TARGET_NETBSD) || if defined (TARGET_OPENBSD) || defined(TARGET_DARWIN) */
bool
tun_name_is_fixed(const char *dev)
@@ -2688,18 +2701,6 @@
argv_free(&argv);
}
-int
-write_tun(struct tuntap *tt, uint8_t *buf, int len)
-{
- return write_tun_header(tt, buf, len);
-}
-
-int
-read_tun(struct tuntap *tt, uint8_t *buf, int len)
-{
- return read_tun_header(tt, buf, len);
-}
-
#elif defined(TARGET_NETBSD)
/*
@@ -2801,88 +2802,8 @@
argv_free(&argv);
}
-static inline int
-netbsd_modify_read_write_return(int len)
-{
- if (len > 0)
- {
- return len > sizeof(u_int32_t) ? len - sizeof(u_int32_t) : 0;
- }
- else
- {
- return len;
- }
-}
-
-int
-write_tun(struct tuntap *tt, uint8_t *buf, int len)
-{
- if (tt->type == DEV_TYPE_TUN)
- {
- u_int32_t type;
- struct iovec iv[2];
- struct openvpn_iphdr *iph;
-
- iph = (struct openvpn_iphdr *)buf;
-
- if (OPENVPN_IPH_GET_VER(iph->version_len) == 6)
- {
- type = htonl(AF_INET6);
- }
- else
- {
- type = htonl(AF_INET);
- }
-
- iv[0].iov_base = (char *)&type;
- iv[0].iov_len = sizeof(type);
- iv[1].iov_base = buf;
- iv[1].iov_len = len;
-
- return netbsd_modify_read_write_return(writev(tt->fd, iv, 2));
- }
- else
- {
- return write(tt->fd, buf, len);
- }
-}
-
-int
-read_tun(struct tuntap *tt, uint8_t *buf, int len)
-{
- if (tt->type == DEV_TYPE_TUN)
- {
- u_int32_t type;
- struct iovec iv[2];
-
- iv[0].iov_base = (char *)&type;
- iv[0].iov_len = sizeof(type);
- iv[1].iov_base = buf;
- iv[1].iov_len = len;
-
- return netbsd_modify_read_write_return(readv(tt->fd, iv, 2));
- }
- else
- {
- return read(tt->fd, buf, len);
- }
-}
-
#elif defined(TARGET_FREEBSD)
-static inline int
-freebsd_modify_read_write_return(int len)
-{
- if (len > 0)
- {
- return len > sizeof(u_int32_t) ? len - sizeof(u_int32_t) : 0;
- }
- else
- {
- return len;
- }
-}
-
void
open_tun(const char *dev, const char *dev_type, const char *dev_node, struct tuntap *tt,
openvpn_net_ctx_t *ctx)
@@ -2955,84 +2876,8 @@
argv_free(&argv);
}
-#if defined(__GNUC__) || defined(__clang__)
-#pragma GCC diagnostic push
-#pragma GCC diagnostic ignored "-Wconversion"
-#endif
-
-int
-write_tun(struct tuntap *tt, uint8_t *buf, int len)
-{
- if (tt->type == DEV_TYPE_TUN)
- {
- u_int32_t type;
- struct iovec iv[2];
- struct ip *iph;
-
- iph = (struct ip *)buf;
-
- if (iph->ip_v == 6)
- {
- type = htonl(AF_INET6);
- }
- else
- {
- type = htonl(AF_INET);
- }
-
- iv[0].iov_base = (char *)&type;
- iv[0].iov_len = sizeof(type);
- iv[1].iov_base = buf;
- iv[1].iov_len = len;
-
- return freebsd_modify_read_write_return(writev(tt->fd, iv, 2));
- }
- else
- {
- return write(tt->fd, buf, len);
- }
-}
-
-int
-read_tun(struct tuntap *tt, uint8_t *buf, int len)
-{
- if (tt->type == DEV_TYPE_TUN)
- {
- u_int32_t type;
- struct iovec iv[2];
-
- iv[0].iov_base = (char *)&type;
- iv[0].iov_len = sizeof(type);
- iv[1].iov_base = buf;
- iv[1].iov_len = len;
-
- return freebsd_modify_read_write_return(readv(tt->fd, iv, 2));
- }
- else
- {
- return read(tt->fd, buf, len);
- }
-}
-
-#if defined(__GNUC__) || defined(__clang__)
-#pragma GCC diagnostic pop
-#endif
-
#elif defined(TARGET_DRAGONFLY)
-static inline int
-dragonfly_modify_read_write_return(int len)
-{
- if (len > 0)
- {
- return len > sizeof(u_int32_t) ? len - sizeof(u_int32_t) : 0;
- }
- else
- {
- return len;
- }
-}
-
void
open_tun(const char *dev, const char *dev_type, const char *dev_node, struct tuntap *tt,
openvpn_net_ctx_t *ctx)
@@ -3059,60 +2904,6 @@
free(tt);
}
-int
-write_tun(struct tuntap *tt, uint8_t *buf, int len)
-{
- if (tt->type == DEV_TYPE_TUN)
- {
- u_int32_t type;
- struct iovec iv[2];
- struct ip *iph;
-
- iph = (struct ip *)buf;
-
- if (iph->ip_v == 6)
- {
- type = htonl(AF_INET6);
- }
- else
- {
- type = htonl(AF_INET);
- }
-
- iv[0].iov_base = (char *)&type;
- iv[0].iov_len = sizeof(type);
- iv[1].iov_base = buf;
- iv[1].iov_len = len;
-
- return dragonfly_modify_read_write_return(writev(tt->fd, iv, 2));
- }
- else
- {
- return write(tt->fd, buf, len);
- }
-}
-
-int
-read_tun(struct tuntap *tt, uint8_t *buf, int len)
-{
- if (tt->type == DEV_TYPE_TUN)
- {
- u_int32_t type;
- struct iovec iv[2];
-
- iv[0].iov_base = (char *)&type;
- iv[0].iov_len = sizeof(type);
- iv[1].iov_base = buf;
- iv[1].iov_len = len;
-
- return dragonfly_modify_read_write_return(readv(tt->fd, iv, 2));
- }
- else
- {
- return read(tt->fd, buf, len);
- }
-}
-
#elif defined(TARGET_DARWIN)
/* Darwin (MacOS X) is mostly "just use the generic stuff", but there
--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/1378?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings?usp=email
Gerrit-MessageType: newpatchset
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Ifd9384287648d1f37a625d9ed6a09733208fa56c
Gerrit-Change-Number: 1378
Gerrit-PatchSet: 2
Gerrit-Owner: flichtenheld <fr...@li...>
Gerrit-Reviewer: cron2 <ge...@gr...>
Gerrit-Reviewer: plaisthos <arn...@rf...>
Gerrit-CC: openvpn-devel <ope...@li...>
Gerrit-Attention: plaisthos <arn...@rf...>
Gerrit-Attention: flichtenheld <fr...@li...>
|
|
From: flichtenheld (C. Review) <ge...@op...> - 2025-11-14 13:41:48
|
Attention is currently required from: cron2, plaisthos. flichtenheld has posted comments on this change by flichtenheld. ( http://gerrit.openvpn.net/c/openvpn/+/1378?usp=email ) Change subject: tun: Refactor BSD write_tun/read_tun ...................................................................... Patch Set 2: (2 comments) File src/openvpn/tun.c: http://gerrit.openvpn.net/c/openvpn/+/1378/comment/4ca9d7f6_c2a30b9c?usp=email : PS1, Line 1744: #endif > it makes little sense to keep the historic code difference - this basically does the same thing, jus […] Done http://gerrit.openvpn.net/c/openvpn/+/1378/comment/25c2eb38_1e1267b2?usp=email : PS1, Line 1798: } > ah, and this lost the `return` part when being moved... `return write_tun_header... […] Done -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/1378?usp=email To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings?usp=email Gerrit-MessageType: comment Gerrit-Project: openvpn Gerrit-Branch: master Gerrit-Change-Id: Ifd9384287648d1f37a625d9ed6a09733208fa56c Gerrit-Change-Number: 1378 Gerrit-PatchSet: 2 Gerrit-Owner: flichtenheld <fr...@li...> Gerrit-Reviewer: cron2 <ge...@gr...> Gerrit-Reviewer: plaisthos <arn...@rf...> Gerrit-CC: openvpn-devel <ope...@li...> Gerrit-Attention: plaisthos <arn...@rf...> Gerrit-Attention: cron2 <ge...@gr...> Gerrit-Comment-Date: Fri, 14 Nov 2025 13:41:34 +0000 Gerrit-HasComments: Yes Gerrit-Has-Labels: No Comment-In-Reply-To: cron2 <ge...@gr...> |