[Openvpn-devel,M] Change in openvpn[master]: get_default_gateway(): implement platform support for Linux/IPROUTE2

Message ID eed758ca844518546c11c7e3e2e3c92ee80beb1e-HTML@gerrit.openvpn.net
State New
Headers show
Series [Openvpn-devel,M] Change in openvpn[master]: get_default_gateway(): implement platform support for Linux/IPROUTE2 | expand

Commit Message

cron2 (Code Review) Jan. 30, 2025, 6:13 p.m. UTC
Attention is currently required from: flichtenheld, plaisthos.

Hello plaisthos, flichtenheld,

I'd like you to do a code review.
Please visit

    http://gerrit.openvpn.net/c/openvpn/+/884?usp=email

to review the following change.


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 <gert@greenie.muc.de>
---
M src/openvpn/networking_iproute2.c
M src/openvpn/networking_sitnl.c
2 files changed, 8 insertions(+), 55 deletions(-)



  git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/84/884/1

Patch

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/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] = '\0';
-
-    FILE *fp = fopen("/proc/net/route", "r");
-    if (!fp)
-    {
-        return -1;
-    }
-
-    char line[256];
-    int count = 0;
-    unsigned int lowest_metric = UINT_MAX;
-    while (fgets(line, sizeof(line), fp) != NULL)
-    {
-        if (count)
-        {
-            unsigned int net_x = 0;
-            unsigned int mask_x = 0;
-            unsigned int gw_x = 0;
-            unsigned int metric = 0;
-            unsigned int flags = 0;
-            char name[16];
-            name[0] = '\0';
-
-            const int np = 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 == 6 && (flags & IFF_UP))
-            {
-                const in_addr_t net = ntohl(net_x);
-                const in_addr_t mask = ntohl(mask_x);
-                const in_addr_t gw = ntohl(gw_x);
-
-                if (!net && !mask && metric < lowest_metric)
-                {
-                    *best_gw = gw;
-                    strcpy(best_iface, name);
-                    lowest_metric = 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)
 {