Re: [Openvpn-devel] [PATCH 1/3] netsh: Specify interfaces by index rather than name

2020-09-24 Thread Lev Stipakov
Good old "name vs index" thing.

According to "man" of netsh, index indeed could be used for "set
address" and "add route":

> interface - Interface name or index.

> +DWORD adapter_index;
>  if (r6->adapter_index)  /* vpn server special route */
>  {
> -buf_printf(&out, "interface=%lu", r6->adapter_index );
> +adapter_index = r6->adapter_index;
>  gateway_needed = true;
>  }
>  else
>  {
> -buf_printf(&out, "interface=%lu", tt->adapter_index );
> +adapter_index = tt->adapter_index;
>  }

Here I would do something like

  DWORD adapter_index = tt->adapter_index;
  if (r6->adapter_index)
  {
  adapter_index = r6->adapter_index;
  gateway_needed = true;
  }

to make code more concise, but I am fine with current implementation too.

Stared at the code, built and tested on MSVC/Win10. Works as expected.

Acked-by: Lev Stipakov 


___
Openvpn-devel mailing list
Openvpn-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/openvpn-devel


[Openvpn-devel] [PATCH 1/3] netsh: Specify interfaces by index rather than name

2020-09-23 Thread Simon Rozman via Openvpn-devel
This is more efficient and less error prone.

Signed-off-by: Simon Rozman 
---
 src/openvpn/route.c | 26 +++---
 src/openvpn/tun.c   | 88 +
 2 files changed, 53 insertions(+), 61 deletions(-)

diff --git a/src/openvpn/route.c b/src/openvpn/route.c
index bd6b968b..d75aa5f4 100644
--- a/src/openvpn/route.c
+++ b/src/openvpn/route.c
@@ -1987,25 +1987,24 @@ add_route_ipv6(struct route_ipv6 *r6, const struct 
tuntap *tt,
 }
 else
 {
-struct buffer out = alloc_buf_gc(64, &gc);
+DWORD adapter_index;
 if (r6->adapter_index)  /* vpn server special route */
 {
-buf_printf(&out, "interface=%lu", r6->adapter_index );
+adapter_index = r6->adapter_index;
 gateway_needed = true;
 }
 else
 {
-buf_printf(&out, "interface=%lu", tt->adapter_index );
+adapter_index = tt->adapter_index;
 }
-device = buf_bptr(&out);
 
-/* netsh interface ipv6 add route 2001:db8::/32 MyTunDevice */
-argv_printf(&argv, "%s%s interface ipv6 add route %s/%d %s",
+/* netsh interface ipv6 add route 2001:db8::/32 42 */
+argv_printf(&argv, "%s%s interface ipv6 add route %s/%d %lu",
 get_win_sys_path(),
 NETSH_PATH_SUFFIX,
 network,
 r6->netbits,
-device);
+adapter_index);
 
 /* next-hop depends on TUN or TAP mode:
  * - in TAP mode, we use the "real" next-hop
@@ -2431,25 +2430,24 @@ delete_route_ipv6(const struct route_ipv6 *r6, const 
struct tuntap *tt,
 }
 else
 {
-struct buffer out = alloc_buf_gc(64, &gc);
+DWORD adapter_index;
 if (r6->adapter_index)  /* vpn server special route */
 {
-buf_printf(&out, "interface=%lu", r6->adapter_index );
+adapter_index = r6->adapter_index;
 gateway_needed = true;
 }
 else
 {
-buf_printf(&out, "interface=%lu", tt->adapter_index );
+adapter_index = tt->adapter_index;
 }
-device = buf_bptr(&out);
 
-/* netsh interface ipv6 delete route 2001:db8::/32 MyTunDevice */
-argv_printf(&argv, "%s%s interface ipv6 delete route %s/%d %s",
+/* netsh interface ipv6 delete route 2001:db8::/32 42 */
+argv_printf(&argv, "%s%s interface ipv6 delete route %s/%d %lu",
 get_win_sys_path(),
 NETSH_PATH_SUFFIX,
 network,
 r6->netbits,
-device);
+adapter_index);
 
 /* next-hop depends on TUN or TAP mode:
  * - in TAP mode, we use the "real" next-hop
diff --git a/src/openvpn/tun.c b/src/openvpn/tun.c
index faa02504..8fd3229f 100644
--- a/src/openvpn/tun.c
+++ b/src/openvpn/tun.c
@@ -68,7 +68,7 @@ const static GUID GUID_DEVINTERFACE_NET = { 0xcac88484, 
0x7515, 0x4c03, { 0x82,
 #define NI_OPTIONS (1<<2)
 
 static void netsh_ifconfig(const struct tuntap_options *to,
-   const char *flex_name,
+   DWORD adapter_index,
const in_addr_t ip,
const in_addr_t netmask,
const unsigned int flags);
@@ -79,7 +79,7 @@ static void windows_set_mtu(const int iface_index,
 
 static void netsh_set_dns6_servers(const struct in6_addr *addr_list,
const int addr_len,
-   const char *flex_name);
+   DWORD adapter_index);
 
 static void netsh_command(const struct argv *a, int n, int msglevel);
 
@@ -1103,10 +1103,9 @@ do_ifconfig_ipv6(struct tuntap *tt, const char *ifname, 
int tun_mtu,
 }
 else
 {
-/* example: netsh interface ipv6 set address interface=42
+/* example: netsh interface ipv6 set address 42
  *  2001:608:8003::d/bits store=active
  */
-char iface[64];
 
 /* in TUN mode, we only simulate a subnet, so the interface
  * is configured with /128 + a route to fe80::8.  In TAP mode,
@@ -1114,10 +1113,8 @@ do_ifconfig_ipv6(struct tuntap *tt, const char *ifname, 
int tun_mtu,
  */
 int netbits = (tt->type == DEV_TYPE_TUN) ? 128 : tt->netbits_ipv6;
 
-openvpn_snprintf(iface, sizeof(iface), "interface=%lu",
- tt->adapter_index);
-argv_printf(&argv, "%s%s interface ipv6 set address %s %s/%d 
store=active",
-get_win_sys_path(), NETSH_PATH_SUFFIX, iface,
+argv_printf(&argv, "%s%s interface ipv6 set address %lu %s/%d 
store=active",
+get_win_sys_path(), NETSH_PATH_SUFFIX, tt->adapter_index,
 ifconfig_ipv6_local, netbits);
 netsh_command(&argv,