Skip to contentSkip to conversation

Fix for "Cannot read current default gateway" problem on Linux

PATCH David Balazic 3 participants 5 messages Feb 2, 2009 – Feb 3, 2009

lore
Share & tools
lore
#0 lore raw
route.c +32 -15
diff -u openvpn_trunk/route.c openvpn_patched/route.c
@@ -45,7 +45,7 @@
 
 static void add_route (struct route *r, const struct tuntap *tt, unsigned int flags, const struct env_set *es);
 static void delete_route (const struct route *r, const struct tuntap *tt, unsigned int flags, const struct env_set *es);
-static bool get_default_gateway (in_addr_t *ret);
+static bool get_default_gateway (in_addr_t *ret, char *gw_if_name);
 
 struct route_option_list *
 new_route_option_list (struct gc_arena *a)
@@ -298,7 +298,7 @@
       rl->spec.remote_host_defined = true;
     }
 
-  rl->spec.net_gateway_defined = get_default_gateway (&rl->spec.net_gateway);
+  rl->spec.net_gateway_defined = get_default_gateway (&rl->spec.net_gateway, rl->spec.net_gateway_ifname);
   if (rl->spec.net_gateway_defined)
     {
       setenv_route_addr (es, "net_gateway", rl->spec.net_gateway, -1);
@@ -362,6 +362,7 @@
 add_route3 (in_addr_t network,
 	    in_addr_t netmask,
 	    in_addr_t gateway,
+	    char * gateway_ifname,
 	    const struct tuntap *tt,
 	    unsigned int flags,
 	    const struct env_set *es)
@@ -372,6 +373,7 @@
   r.network = network;
   r.netmask = netmask;
   r.gateway = gateway;
+  r.gateway_ifname = gateway_ifname;
   add_route (&r, tt, flags, es);
 }
 
@@ -418,6 +420,7 @@
 	    add_route3 (rl->spec.remote_host,
 			~0,
 			rl->spec.net_gateway,
+			rl->spec.net_gateway_ifname,
 			tt,
 			flags,
 			es);
@@ -428,6 +431,7 @@
 	      add_route3 (0x00000000,
 			  0x80000000,
 			  rl->spec.remote_endpoint,
+			  NULL,
 			  tt,
 			  flags,
 			  es);
@@ -436,6 +440,7 @@
 	      add_route3 (0x80000000,
 			  0x80000000,
 			  rl->spec.remote_endpoint,
+			  NULL,
 			  tt,
 			  flags,
 			  es);
@@ -454,6 +459,7 @@
 	      add_route3 (0,
 			  0,
 			  rl->spec.remote_endpoint,
+			  NULL,
 			  tt,
 			  flags,
 			  es);
@@ -511,6 +517,7 @@
 	  add_route3 (0,
 		      0,
 		      rl->spec.net_gateway,
+		      rl->spec.net_gateway_ifname,
 		      tt,
 		      flags,
 		      es);
@@ -678,18 +685,24 @@
 
 #if defined(TARGET_LINUX)
 #ifdef CONFIG_FEATURE_IPROUTE
-  buf_printf (&buf, IPROUTE_PATH " route add %s/%d via %s",
+  buf_printf (&buf, IPROUTE_PATH " route add %s/%d",
 	      network,
-	      count_netmask_bits(netmask),
-	      gateway);
+	      count_netmask_bits(netmask));
+  if (r->gateway)
+    argv_printf_cat (&argv, "via %s", gateway);
+  else
+    argv_printf_cat (&argv, "dev %s", r->gateway_ifname);
   if (r->metric_defined)
     buf_printf (&buf, " metric %d", r->metric);
 
 #else
-  buf_printf (&buf, ROUTE_PATH " add -net %s netmask %s gw %s",
+  buf_printf (&buf, ROUTE_PATH " add -net %s netmask %s",
 	      network,
-	      netmask,
-	      gateway);
+	      netmask);
+  if (r->gateway)
+    argv_printf_cat (&argv, "gw %s", gateway);
+  else
+    argv_printf_cat (&argv, "dev %s", r->gateway_ifname);
   if (r->metric_defined)
     buf_printf (&buf, " metric %d", r->metric);
 #endif  /*CONFIG_FEATURE_IPROUTE*/
@@ -1014,7 +1027,7 @@
 }
 
 static bool
-get_default_gateway (in_addr_t *gateway)
+get_default_gateway (in_addr_t *gateway, char *gw_ifname)
 {
   struct gc_arena gc = gc_new ();
   int i;
@@ -1242,7 +1255,7 @@
 #elif defined(TARGET_LINUX)
 
 static bool
-get_default_gateway (in_addr_t *gateway)
+get_default_gateway (in_addr_t *gateway, char *gw_if_name)
 {
   struct gc_arena gc = gc_new ();
   bool ret = false;
@@ -1283,6 +1296,10 @@
 		  if (!net && !mask && metric < lowest_metric)
 		    {
 		      best_gw = gw;
+		      if (gw_if_name)
+				{
+		    	  sscanf (line, "%" IFNAME_MAXLEN_STR "s\t", gw_if_name);
+				}
 		      lowest_metric = metric;
 		      best_count = count;
 		    }
@@ -1292,7 +1309,7 @@
 	}
       fclose (fp);
 
-      if (best_gw)
+      if (best_count)
 	{
 	  *gateway = best_gw;
 	  ret = true;
@@ -1370,7 +1387,7 @@
         ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
 
 static bool
-get_default_gateway (in_addr_t *ret)
+get_default_gateway (in_addr_t *ret, char *gw_if_name)
 {
   struct gc_arena gc = gc_new ();
   int s, seq, l, pid, rtm_addrs, i;
@@ -1527,7 +1544,7 @@
         ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
 
 static bool
-get_default_gateway (in_addr_t *ret)
+get_default_gateway (in_addr_t *ret, char *gw_if_name)
 {
   struct gc_arena gc = gc_new ();
   int s, seq, l, pid, rtm_addrs, i;
@@ -1683,7 +1700,7 @@
         ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
 
 static bool
-get_default_gateway (in_addr_t *ret)
+get_default_gateway (in_addr_t *ret, char *gw_if_name)
 {
   struct gc_arena gc = gc_new ();
   int s, seq, l, rtm_addrs, i;
@@ -1782,7 +1799,7 @@
 #else
 
 static bool
-get_default_gateway (in_addr_t *ret)
+get_default_gateway (in_addr_t *ret, char *gw_if_name)
 {
   return false;
 }
route.h +5 -0
diff -u openvpn_trunk/route.h openvpn_patched/route.h
@@ -48,11 +48,15 @@
  */
 #define ROUTE_DELETE_FIRST  2
 
+#define IFNAME_MAXLEN 32
+#define IFNAME_MAXLEN_STR "32"
+
 struct route_special_addr
 {
   in_addr_t remote_endpoint;
   bool remote_endpoint_defined;
   in_addr_t net_gateway;
+  char net_gateway_ifname[IFNAME_MAXLEN];
   bool net_gateway_defined;
   in_addr_t remote_host;
   bool remote_host_defined;
@@ -79,6 +83,7 @@
   in_addr_t network;
   in_addr_t netmask;
   in_addr_t gateway;
+  char * gateway_ifname;
   bool metric_defined;
   int metric;
 };

---------------------------------------------------------------------
http://www.nosoftwarepatents.com Innovation, not litigation !
---
David Balažic mailto:[email protected]...
HERMES Softlab http://www.hermes-softlab.com
Zagrebska cesta 104 Phone: +386 2 450 8947
SI-2000 Maribor
Slovenija
---------------------------------------------------------------------
"Be excellent to each other." -
Bill S. Preston, Esq. & "Ted" Theodore Logan
---------------------------------------------------------------------

#1 lore raw

route.c line 51

Quoted 12 lines · On Monday 02 February 2009 11:49:50 David Balazic wrote:

diff -u openvpn_trunk/route.c openvpn_patched/route.c

route.c +1 -1
@@ -45,7 +45,7 @@
 
 static void add_route (struct route *r, const struct tuntap *tt, unsigned int flags, const struct env_set *es);
 static void delete_route (const struct route *r, const struct tuntap *tt, unsigned int flags, const struct env_set *es);
-static bool get_default_gateway (in_addr_t *ret);
+static bool get_default_gateway (in_addr_t *ret, char *gw_if_name);
 
 struct route_option_list *
 new_route_option_list (struct gc_arena *a)

Which version of diffutils do you use?
How come there is a tab after file name in the header? How come the above hank has 8 lines but reported as 7 lines?

Alon.

#2 lore raw

[Openvpn-devel] Submitting patches to openvpn-devel

I know that many open source projects ask for patches to be submitted inline, rather than via email attachment. Quite honestly, I don't think this approach works in practice. Many email clients in popular use will munge the patch making it unusable. This creates a hassle for the maintainers as they must request the patch to be resubmitted.

I know that in the past, the OpenVPN Project has echoed the common practice of asking for patches to be submitted inline. I would like to change that practice now. So please, in the future, submit patches as attachments. For trivial patches, it's okay paste them inline for discussion purposes as long as they are also included as an attachment.

Thanks,
James

Quoted 29 lines · Alon Bar-Lev

route.c line 51

Quoted 12 lines · On Monday 02 February 2009 11:49:50 David Balazic wrote:

diff -u openvpn_trunk/route.c openvpn_patched/route.c

route.c +1 -1
@@ -45,7 +45,7 @@
 
 static void add_route (struct route *r, const struct tuntap *tt, unsigned int flags, const struct env_set *es);
 static void delete_route (const struct route *r, const struct tuntap *tt, unsigned int flags, const struct env_set *es);
-static bool get_default_gateway (in_addr_t *ret);
+static bool get_default_gateway (in_addr_t *ret, char *gw_if_name);
 
 struct route_option_list *
 new_route_option_list (struct gc_arena *a)

Which version of diffutils do you use?
How come there is a tab after file name in the header? How come the above hank has 8 lines but reported as 7 lines?

Alon.

------------------------------------------------------------------------------
This SF.net email is sponsored by:
SourcForge Community
SourceForge wants to tell your story.
http://p.sf.net/sfu/sf-spreadtheword
_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel

#3 lore raw

[Openvpn-devel] Diff format

In reply to Alon Bar-Lev

Alon Bar-Lev wrote:

Subject: Fix for "Cannot read current default gateway" problem on Linux

Quoted 10 lines · On Monday 02 February 2009 11:49:50 David Balazic wrote:

route.c line 46

Quoted 6 lines

diff -u openvpn_trunk/route.c openvpn_patched/route.c

route.c +0 -0
@@ -45,7 +45,7 @@
 
 static void add_route (struct route *r, const struct 

tuntap *tt, unsigned int flags, const struct env_set *es);

route.c line 47

Quoted 1 line
route.c +0 -0
 static void delete_route (const struct route *r, const 

struct tuntap *tt, unsigned int flags, const struct env_set *es);

Quoted 8 lines

route.c line 51

Quoted 5 lines
route.c +1 -1
-static bool get_default_gateway (in_addr_t *ret);
+static bool get_default_gateway (in_addr_t *ret, char *gw_if_name);
 
 struct route_option_list *
 new_route_option_list (struct gc_arena *a)

Which version of diffutils do you use?
How come there is a tab after file name in the header?

Well, I asked and it seems to be the normal state of things:

* From: Dave Korn <dave dot korn dot cygwin at googlemail dot com>
* To: cygwin at cygwin dot com
* Date: Mon, 2 Feb 2009 17:56:10 +0000
* Subject: Re: diff inserts tab into header ?
* References: <[email protected]...>

Quoted 3 lines · xerces8 wrote:
Quoted 3 lines
Quoted 2 lines

I discovered this when trying to send a patch to a project and they rejected it because of this tab chars.

Are you sure this is the case? Can you send the exact message they

sent

Quoted 6 lines
Quoted 1 line

you when they rejected the patch?

Sure, they said this:

Quoted 2 lines
Quoted 2 lines

Which version of diffutils do you use?
How come there is a tab after file name in the header?

They must be using a custom version or something strange like that.
The
TAB is placed there by the routine 'print_context_label()' in context.c, you
can view the CVS history at

http://cvs.savannah.gnu.org/viewvc/diffutils/src/context.c?root=diffutil
s&view=log

and it's been right there since version 1.1, seventeen years ago (see line 46).

So AFAICT GNU diff has /always/ done this.

Regards,
David

#4 lore raw

Re: [Openvpn-devel] Diff format

There had been multiple issues.
You can investigate last posting and find what exactly was the problem. I truly don't care.
It did not apply.
Alon.

Quoted 19 lines · David Balazic

They must be using a custom version or something strange like that.
The
TAB is placed there by the routine 'print_context_label()' in context.c, you
can view the CVS history at

http://cvs.savannah.gnu.org/viewvc/diffutils/src/context.c?root=diffutil
s&view=log

and it's been right there since version 1.1, seventeen years ago (see line 46).

So AFAICT GNU diff has /always/ done this.

Regards,

David