From: "cron2 (Code Review)" Date: Fri, 31 Jan 2025 09:41:07 +0000 In-Reply-To: References: Reply-To: gert@...1296..., arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net, frank@...2641..., a@...2181... Message-ID: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="ezjmG+vDCKs="; charset=UTF-8 Subject: [Openvpn-devel] [M] Change in openvpn[master]: get_default_gateway(): implement platform support for Linux/IPROUTE2 List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: plaisthos , flichtenheld , openvpn-devel --ezjmG+vDCKs= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable cron2 has submitted this change. ( http://gerrit.openvpn.net/c/openvpn/+/88= 4?usp=3Demail ) Change subject: get_default_gateway(): implement platform support for Linux= /IPROUTE2 ...................................................................... get_default_gateway(): implement platform support for Linux/IPROUTE2 Remove the old "read /proc/net/route and try to parse it" implementation and always use the sitnl/netlink implementation of net_route_v4_best_gw(). This was kept "because we had it and it was working" but does not really provide any benefit - netlink for route queries is there for v6 anyway, and the main argument for keeping --enable-iproute2 is "some users want to run non-standard 'ip' binaries to do things" - which is not affected by this change. Change-Id: I6f17140109106b37e6b0e690df1d87720ccf6f91 Signed-off-by: Gert Doering Acked-by: Antonio Quartulli Message-Id: <20250131084707.24905-1-gert@...1296...> URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg30= 748.html Signed-off-by: Gert Doering --- M src/openvpn/networking_iproute2.c M src/openvpn/networking_sitnl.c 2 files changed, 8 insertions(+), 55 deletions(-) diff --git a/src/openvpn/networking_iproute2.c b/src/openvpn/networking_ipr= oute2.c index 975282c..6f13ef5 100644 --- a/src/openvpn/networking_iproute2.c +++ b/src/openvpn/networking_iproute2.c @@ -394,63 +394,15 @@ return ret; } -int -net_route_v4_best_gw(openvpn_net_ctx_t *ctx, const in_addr_t *dst, - in_addr_t *best_gw, char *best_iface) -{ - best_iface[0] =3D '\0'; - - FILE *fp =3D fopen("/proc/net/route", "r"); - if (!fp) - { - return -1; - } - - char line[256]; - int count =3D 0; - unsigned int lowest_metric =3D UINT_MAX; - while (fgets(line, sizeof(line), fp) !=3D NULL) - { - if (count) - { - unsigned int net_x =3D 0; - unsigned int mask_x =3D 0; - unsigned int gw_x =3D 0; - unsigned int metric =3D 0; - unsigned int flags =3D 0; - char name[16]; - name[0] =3D '\0'; - - const int np =3D sscanf(line, "%15s\t%x\t%x\t%x\t%*s\t%*s\t%d\= t%x", - name, &net_x, &gw_x, &flags, &metric, - &mask_x); - - if (np =3D=3D 6 && (flags & IFF_UP)) - { - const in_addr_t net =3D ntohl(net_x); - const in_addr_t mask =3D ntohl(mask_x); - const in_addr_t gw =3D ntohl(gw_x); - - if (!net && !mask && metric < lowest_metric) - { - *best_gw =3D gw; - strcpy(best_iface, name); - lowest_metric =3D metric; - } - } - } - ++count; - } - fclose(fp); - - return 0; -} - /* - * The following function is not implemented in the iproute backend as it + * The following functions are not implemented in the iproute backend as it * uses the sitnl implementation from networking_sitnl.c. * * int + * net_route_v4_best_gw(openvpn_net_ctx_t *ctx, const in_addr_t *dst, + * in_addr_t *best_gw, char *best_iface) + * + * int * net_route_v6_best_gw(const struct in6_addr *dst, * struct in6_addr *best_gw, char *best_iface) */ diff --git a/src/openvpn/networking_sitnl.c b/src/openvpn/networking_sitnl.c index 6b750e8..9de8912 100644 --- a/src/openvpn/networking_sitnl.c +++ b/src/openvpn/networking_sitnl.c @@ -619,8 +619,7 @@ } -#ifdef ENABLE_SITNL - +/* used by iproute2 implementation too */ int net_route_v4_best_gw(openvpn_net_ctx_t *ctx, const in_addr_t *dst, in_addr_t *best_gw, char *best_iface) @@ -652,6 +651,8 @@ return ret; } +#ifdef ENABLE_SITNL + int net_iface_up(openvpn_net_ctx_t *ctx, const char *iface, bool up) { -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/884?usp=3Demail To unsubscribe, or for help writing mail filters, visit http://gerrit.openv= pn.net/settings Gerrit-Project: openvpn Gerrit-Branch: master Gerrit-Change-Id: I6f17140109106b37e6b0e690df1d87720ccf6f91 Gerrit-Change-Number: 884 Gerrit-PatchSet: 3 Gerrit-Owner: cron2 Gerrit-Reviewer: flichtenheld Gerrit-Reviewer: ordex Gerrit-Reviewer: plaisthos Gerrit-CC: openvpn-devel Gerrit-MessageType: merged --ezjmG+vDCKs= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

cron2 submitted this change.

View Change

get_default_gateway(): implement platform supp=
ort for Linux/IPROUTE2

Remove the old "read /proc/net/route and= try to parse it" implementation
and always use the sitnl/netlink i= mplementation of net_route_v4_best_gw().

This was kept "because= we had it and it was working" but does not really
provide any bene= fit - netlink for route queries is there for v6 anyway,
and the main arg= ument for keeping --enable-iproute2 is "some users want
to run non-= standard 'ip' binaries to do things" - which is not affectedby this change.

Change-Id: I6f17140109106b37e6b0e690df1d87720ccf6f= 91
Signed-off-by: Gert Doering <gert@...1296...>
Acked-by: Anto= nio Quartulli <a@...2181...>
Message-Id: <20250131084707.24905-= 1-gert@...1296...>
URL: https://www.mail-archive.com/openvpn-devel@...1224...= sts.sourceforge.net/msg30748.html
Signed-off-by: Gert Doering <gert@...220...= ..1296...>
---
M src/openvpn/networking_iproute2.c
M src/openvp= n/networking_sitnl.c
2 files changed, 8 insertions(+), 55 deletions(-)
diff --git a/src/openvpn/networking_iproute2.c b/src/openvpn/networking_=
iproute2.c
index 975282c..6f13ef5 100644
--= - a/src/openvpn/networking_iproute2.c
+++ b/src/openvpn/net= working_iproute2.c
@@ -394,63 +394,15 @@
= return ret;
}

-int
-net_route_v4_best_gw(openvpn_net_ctx_t *ctx, const in_addr_t = *dst,
- = in_addr_t *best_gw, char *best_iface)
-{
= - best_iface[0] =3D '\0';
-
- FI= LE *fp =3D fopen("/proc/net/route", "r");
- if (!fp)
- {
- return -1;
- }
-
- char line[256];
- int count =3D 0;
- unsigned int lowest_met= ric =3D UINT_MAX;
- = while (fgets(line, sizeof(line), fp) !=3D NULL)
- {
- if (count)
- {
- = unsigned int net_x =3D 0;
- unsigned int mask_x =3D 0;
- unsigned int gw_x =3D 0;
= - unsigned int metric = =3D 0;
- uns= igned int flags =3D 0;
= - char name[16];
- name[0] =3D '\0';
-
= - const int np =3D sscanf(line, "%15s\t%x\t%x\t%x\t%*s\t%*s= \t%d\t%x",
- = name, &net_x, &gw_x, &flags, &m= etric,
- = &mask_x);
-
- = if (np =3D=3D 6 && (flags & IFF_UP))
- {
- const in_addr_t net =3D ntohl(net_x);<= /span>
- const = in_addr_t mask =3D ntohl(mask_x);
- const in_addr_t gw =3D ntohl(gw_x);
<= span style=3D"color: hsl(0, 100%, 40%);">-

- if (!net && !mask && m= etric < lowest_metric)
- {
-= *best_gw =3D gw;
- strcpy(best_iface, name);
- lowest_metric = =3D metric;

- = }
- }<= /span>
- }
- ++count;

- }
- fclose(fp);
-
- ret= urn 0;
-}
-

/*
- * The following function is not imp= lemented in the iproute backend as it

+ * The following functions are not implemented in the ipr= oute backend as it
* uses the sitnl implementation from n= etworking_sitnl.c.
*
* int
+ * net_route_v4_best_gw(openvpn_= net_ctx_t *ctx, const in_addr_t *dst,
+ * in_addr_t *best_gw, char *best_ifa= ce)
+ *
+ * int
* net_ro= ute_v6_best_gw(const struct in6_addr *dst,
* = struct in6_addr *best_gw, char *best_iface)
*/
diff --git a/src/openvpn/networking_sitnl.c b/src/openvpn/ne= tworking_sitnl.c
index 6b750e8..9de8912 100644
--- a/src/openvpn/networking_sitnl.c

+++ b/src/openvpn/= networking_sitnl.c
@@ -619,8 +619,7 @@

}

-#ifdef ENABLE_SITNL
-
+/* used = by iproute2 implementation too */
int
net= _route_v4_best_gw(openvpn_net_ctx_t *ctx, const in_addr_t *dst,
<= span> in_addr_t *best_gw, char *best_iface)
= @@ -652,6 +651,8 @@
return ret;
= }

= +#ifdef ENABLE_SITNL
= +
int
net_iface_up(openvpn_net_ctx_t *ctx= , const char *iface, bool up)
{

To view, visit change 884. To unsubscribe, or for help writing mail fi= lters, visit settings.

=
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I6f17140109106b37e6b0e690df1= d87720ccf6f91
Gerrit-Change-Number: 884
Gerrit-PatchSet: 3
Gerrit-Owner: cron2 <gert@...1296...>
Gerrit-Reviewer: flichtenheld <frank@...2716...= 1...>
Gerrit-Reviewer: ordex <a@...2181...>
Gerrit-Reviewer: plaisthos <arne-openvpn@...1186...= .1227...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
Gerrit-MessageType: merged
--ezjmG+vDCKs=--