summarylogtreecommitdiffstats
path: root/0004-interfaces-Fix-fetching-of-interfaces-on-Win32.patch
blob: 6d8c08d0db04e2bd33d3eebfec3c6f70a0e70c40 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
From 0754179c2d0db97d06a3f0f9e500bfc54fc48108 Mon Sep 17 00:00:00 2001
From: Nirbheek Chauhan <nirbheek@centricular.com>
Date: Thu, 9 Jul 2020 17:55:53 +0530
Subject: [PATCH 4/4] interfaces: Fix fetching of interfaces on Win32

nice_interfaces_get_local_interfaces() was returning the hardware
device names of interfaces that have been seen in the past or could be
connected. It had no relation to the actual interfaces present on the
device. Perhaps this worked back in the XP days, but it is returning
garbage today.

Use GetAdapterAddresses(), and return the adapter friendly names as
the interface names. At the same time, update
nice_interfaces_get_ip_for_interface() to match.

Coincidentally, this also fixes UWP compatibility since GetIfTable and
GetIpAddrTable are not available when targeting UWP. They are only
available for desktop apps:

https://docs.microsoft.com/en-us/windows/win32/api/iphlpapi/nf-iphlpapi-getiftable
https://docs.microsoft.com/en-us/windows/win32/api/iphlpapi/nf-iphlpapi-getipaddrtable
---
 agent/interfaces.c | 174 ++++++++++++++++++++++-----------------------
 1 file changed, 85 insertions(+), 89 deletions(-)

diff --git a/agent/interfaces.c b/agent/interfaces.c
index 9a5300e..b99450d 100644
--- a/agent/interfaces.c
+++ b/agent/interfaces.c
@@ -497,39 +497,13 @@ nice_interfaces_get_ip_for_interface (gchar *interface_name)
 #define MIB_IPADDR_DELETED 0x0040
 #endif
 
-GList * nice_interfaces_get_local_interfaces (void)
+static IP_ADAPTER_ADDRESSES *
+_nice_get_adapters_addresses (void)
 {
-  ULONG size = 0;
-  PMIB_IFTABLE if_table;
-  GList * ret = NULL;
-
-  GetIfTable(NULL, &size, TRUE);
-
-  if (!size)
-    return NULL;
-
-  if_table = (PMIB_IFTABLE)g_malloc0(size);
-
-  if (GetIfTable(if_table, &size, TRUE) == ERROR_SUCCESS) {
-    DWORD i;
-    for (i = 0; i < if_table->dwNumEntries; i++) {
-      ret = g_list_prepend (ret, g_strdup ((gchar*)if_table->table[i].bDescr));
-    }
-  }
-
-  g_free(if_table);
-
-  return ret;
-}
-
-GList * nice_interfaces_get_local_ips (gboolean include_loopback)
-{
-  IP_ADAPTER_ADDRESSES *addresses = NULL, *a;
+  IP_ADAPTER_ADDRESSES *addresses = NULL;
   ULONG status;
   guint iterations;
   ULONG addresses_size;
-  DWORD pref = 0;
-  GList *ret = NULL;
 
   /* As suggested on
    * http://msdn.microsoft.com/en-gb/library/windows/desktop/aa365915%28v=vs.85%29.aspx */
@@ -555,11 +529,45 @@ GList * nice_interfaces_get_local_ips (gboolean include_loopback)
 
   /* Error? */
   if (status != NO_ERROR) {
-    nice_debug ("Error retrieving local addresses (error code %lu).", status);
+    gchar *msg = g_win32_error_message (status);
+    nice_debug ("Error retrieving local addresses: %s", msg);
+    g_free (msg);
     g_free (addresses);
     return NULL;
   }
 
+  return addresses;
+}
+
+GList * nice_interfaces_get_local_interfaces (void)
+{
+  IP_ADAPTER_ADDRESSES *addresses, *a;
+  GList *ret = NULL;
+
+  addresses = _nice_get_adapters_addresses ();
+  if (!addresses)
+    return NULL;
+
+  for (a = addresses; a != NULL; a = a->Next) {
+    gchar *name = g_utf16_to_utf8 (a->FriendlyName, -1, NULL, NULL, NULL);
+    ret = g_list_append (ret, name);
+  }
+
+  g_free(addresses);
+
+  return ret;
+}
+
+GList * nice_interfaces_get_local_ips (gboolean include_loopback)
+{
+  IP_ADAPTER_ADDRESSES *addresses, *a;
+  DWORD pref = 0;
+  GList *ret = NULL;
+
+  addresses = _nice_get_adapters_addresses ();
+  if (!addresses)
+    return NULL;
+
   /*
    * Get the best interface for transport to 0.0.0.0.
    * This interface should be first in list!
@@ -613,7 +621,7 @@ GList * nice_interfaces_get_local_ips (gboolean include_loopback)
         continue;
       }
 
-      nice_debug ("IP address: %s", addr_string);
+      nice_debug ("Adapter %S IP address: %s", a->FriendlyName, addr_string);
 
       if (a->IfIndex == pref || a->Ipv6IfIndex == pref)
         ret = g_list_prepend (ret, addr_string);
@@ -627,79 +635,67 @@ GList * nice_interfaces_get_local_ips (gboolean include_loopback)
   return ret;
 }
 
-/*
- * returns ip address as an utf8 string
- */
-// Source for idx's type (Was IF_INDEX):
-// http://msdn.microsoft.com/en-us/library/aa366836(v=VS.85).aspx
-// (Title: MIB_IFROW structure)
-static gchar *
-win32_get_ip_for_interface (DWORD idx)
+gchar * nice_interfaces_get_ip_for_interface (gchar *interface_name)
 {
-  ULONG size = 0;
-  PMIB_IPADDRTABLE ip_table;
+  IP_ADAPTER_ADDRESSES *addresses, *a;
+  IP_ADAPTER_UNICAST_ADDRESS *unicast;
+  DWORD status;
   gchar * ret = NULL;
 
-  GetIpAddrTable (NULL, &size, TRUE);
-
-  if (!size)
+  addresses = _nice_get_adapters_addresses ();
+  if (!addresses)
     return NULL;
 
-  ip_table = (PMIB_IPADDRTABLE)g_malloc0 (size);
-
-  if (GetIpAddrTable (ip_table, &size, TRUE) == ERROR_SUCCESS) {
-    DWORD i;
-    for (i = 0; i < ip_table->dwNumEntries; i++) {
-      PMIB_IPADDRROW ipaddr = &ip_table->table[i];
-      if (ipaddr->dwIndex == idx &&
-          !(ipaddr->wType & (MIB_IPADDR_DISCONNECTED | MIB_IPADDR_DELETED))) {
-        ret = g_strdup_printf ("%lu.%lu.%lu.%lu",
-            (ipaddr->dwAddr      ) & 0xFF,
-            (ipaddr->dwAddr >>  8) & 0xFF,
-            (ipaddr->dwAddr >> 16) & 0xFF,
-            (ipaddr->dwAddr >> 24) & 0xFF);
-        break;
-      }
-    }
-  }
-
-  g_free (ip_table);
-  return ret;
-}
+  for (a = addresses; a != NULL; a = a->Next) {
+    IP_ADAPTER_UNICAST_ADDRESS *unicast;
+    gchar *name;
 
-gchar * nice_interfaces_get_ip_for_interface (gchar *interface_name)
-{
-  ULONG size = 0;
-  PMIB_IFTABLE if_table;
-  gchar * ret = NULL;
+    /* Various conditions for ignoring the interface. */
+    if (a->OperStatus == IfOperStatusDown ||
+        a->OperStatus == IfOperStatusNotPresent ||
+        a->OperStatus == IfOperStatusLowerLayerDown) {
+      nice_debug ("Rejecting interface '%S' because it is down or not present",
+          a->FriendlyName);
+      continue;
+    }
 
-  GetIfTable (NULL, &size, TRUE);
+    name = g_utf16_to_utf8 (a->FriendlyName, -1, NULL, NULL, NULL);
+    status = g_ascii_strcasecmp (interface_name, name);
+    g_free (name);
 
-  if (!size)
-    return NULL;
+    /* Found the adapter */
+    if (status == 0)
+      break;
 
-  if_table = (PMIB_IFTABLE)g_malloc0 (size);
+    nice_debug ("Rejecting interface '%s' != '%s'", name, interface_name);
+  }
 
-  if (GetIfTable (if_table, &size, TRUE) == ERROR_SUCCESS) {
-    DWORD i;
-    gchar * tmp_str;
-    for (i = 0; i < if_table->dwNumEntries; i++) {
-      tmp_str = g_utf16_to_utf8 (
-          if_table->table[i].wszName, MAX_INTERFACE_NAME_LEN,
-          NULL, NULL, NULL);
+  if (!a) {
+    nice_debug ("No matches found for interface %s", interface_name);
+    goto out;
+  }
 
-      if (strlen (interface_name) == strlen (tmp_str) &&
-          g_ascii_strncasecmp (interface_name, tmp_str, strlen (interface_name)) == 0) {
-        ret = win32_get_ip_for_interface (if_table->table[i].dwIndex);
-        g_free (tmp_str);
-        break;
-      }
+  /* Grab the interface’s ipv4 unicast addresses. */
+  for (unicast = a->FirstUnicastAddress;
+       unicast != NULL; unicast = unicast->Next) {
+    if (unicast->Address.lpSockaddr->sa_family != AF_INET) {
+      nice_debug ("Rejecting ipv6 address on interface %S", a->FriendlyName);
+      continue;
+    }
 
-      g_free (tmp_str);
+    ret = sockaddr_to_string (unicast->Address.lpSockaddr);
+    if (ret == NULL) {
+      nice_debug ("Failed to convert address to string for interface: %S",
+          a->FriendlyName);
+      continue;
     }
+
+    nice_debug ("Adapter %S IP address: %s", a->FriendlyName, ret);
+    break;
   }
 
-  g_free (if_table);
+out:
+  g_free (addresses);
 
   return ret;
 }
-- 
2.27.0.windows.1