Skip to content

Commit 52597c3

Browse files
committed
ipc: windows: use devpkey instead of nci for name
Signed-off-by: Jason A. Donenfeld <[email protected]>
1 parent fabe24d commit 52597c3

File tree

4 files changed

+41
-120
lines changed

4 files changed

+41
-120
lines changed

src/Makefile

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,13 @@ ifeq ($(PLATFORM),windows)
6262
CC := x86_64-w64-mingw32-clang
6363
WINDRES := $(shell $(CC) $(CFLAGS) -print-prog-name=windres 2>/dev/null)
6464
CFLAGS += -Iwincompat/include -include wincompat/compat.h -DWINVER=0x0601 -D_WIN32_WINNT=0x0601 -flto
65-
LDLIBS += -lws2_32 -lsetupapi -lole32 -ladvapi32 -lnci -Lwincompat
65+
LDLIBS += -lws2_32 -lsetupapi -lole32 -ladvapi32 -Lwincompat
6666
LDFLAGS += -flto -Wl,--dynamicbase -Wl,--nxcompat -Wl,--tsaware -mconsole
6767
LDFLAGS += -Wl,--major-os-version=6 -Wl,--minor-os-version=1 -Wl,--major-subsystem-version=6 -Wl,--minor-subsystem-version=1
6868
# The use of -Wl,/delayload: here implies we're using llvm-mingw
69-
LDFLAGS += -Wl,/delayload:ws2_32.dll -Wl,/delayload:setupapi.dll -Wl,/delayload:ole32.dll -Wl,/delayload:advapi32.dll -Wl,/delayload:nci.dll
69+
LDFLAGS += -Wl,/delayload:ws2_32.dll -Wl,/delayload:setupapi.dll -Wl,/delayload:ole32.dll -Wl,/delayload:advapi32.dll
7070
VERSION := $(patsubst "%",%,$(filter "%",$(file < version.h)))
71-
wg: wincompat/libc.o wincompat/init.o wincompat/loader.o wincompat/resources.o wincompat/nci.lib
72-
wincompat/nci.lib: wincompat/include/nci.h
73-
-@$(if $(BUILT_IN_RM),$(BUILT_IN_RM),$(RM)) $@
74-
$(LINK.o) -DGENERATE_LIB -shared -xc $< -o wincompat/nci.dll -Wl,--out-implib,$@
71+
wg: wincompat/libc.o wincompat/init.o wincompat/loader.o wincompat/resources.o
7572
wincompat/resources.o: wincompat/resources.rc wincompat/manifest.xml
7673
$(WINDRES) -DVERSION_STR=$(VERSION) -O coff -c 65001 -i $< -o $@
7774
endif

src/ipc-windows.h

Lines changed: 32 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111
#include <initguid.h>
1212
#include <devguid.h>
1313
#include <ddk/ndisguid.h>
14-
#include <nci.h>
1514
#include <wireguard.h>
1615
#include <hashtable.h>
1716

1817
#define IPC_SUPPORTS_KERNEL_INTERFACE
1918

2019
static bool have_cached_kernel_interfaces;
2120
static struct hashtable cached_kernel_interfaces;
21+
static const DEVPROPKEY devpkey_name = DEVPKEY_WG_NAME;
2222

2323
static int kernel_get_wireguard_interfaces(struct string_list *list)
2424
{
@@ -32,11 +32,10 @@ static int kernel_get_wireguard_interfaces(struct string_list *list)
3232

3333
for (DWORD i = 0;; ++i) {
3434
bool found = false;
35-
DWORD buf_len = 0, value_type, ret;
35+
DWORD buf_len = 0, value_type;
3636
WCHAR *buf = NULL, adapter_name[MAX_ADAPTER_NAME];
3737
SP_DEVINFO_DATA dev_info_data = { .cbSize = sizeof(SP_DEVINFO_DATA) };
38-
HKEY key;
39-
GUID instance_id;
38+
DEVPROPTYPE prop_type;
4039
ULONG status, problem_code;
4140
char *interface_name;
4241
struct hashtable_entry *entry;
@@ -49,11 +48,12 @@ static int kernel_get_wireguard_interfaces(struct string_list *list)
4948

5049
while (!SetupDiGetDeviceRegistryPropertyW(dev_info, &dev_info_data, SPDRP_HARDWAREID, &value_type, (BYTE *)buf, buf_len, &buf_len)) {
5150
free(buf);
51+
buf = NULL;
5252
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
53-
goto skip;
53+
break;
5454
buf = malloc(buf_len);
5555
if (!buf)
56-
goto skip;
56+
break;
5757
}
5858

5959
if (!buf || value_type != REG_MULTI_SZ || buf_len < sizeof(*buf) * 2 || buf[buf_len / sizeof(*buf) - 1] || buf[buf_len / sizeof(*buf) - 2]) {
@@ -70,45 +70,25 @@ static int kernel_get_wireguard_interfaces(struct string_list *list)
7070
free(buf);
7171
if (!found)
7272
continue;
73-
buf = NULL;
74-
buf_len = 0;
7573

76-
key = SetupDiOpenDevRegKey(dev_info, &dev_info_data, DICS_FLAG_GLOBAL, 0, DIREG_DRV, KEY_QUERY_VALUE);
77-
if (key == INVALID_HANDLE_VALUE)
78-
continue;
79-
buf_len = 39 * sizeof(*buf);
80-
buf = malloc(buf_len);
81-
if (!buf)
74+
if (!SetupDiGetDevicePropertyW(dev_info, &dev_info_data, &devpkey_name,
75+
&prop_type, (PBYTE)adapter_name,
76+
sizeof(adapter_name), NULL, 0) ||
77+
prop_type != DEVPROP_TYPE_STRING)
8278
continue;
83-
while ((ret = RegQueryValueExW(key, L"NetCfgInstanceId", NULL, &value_type, (BYTE *)buf, &buf_len)) != ERROR_SUCCESS) {
84-
free(buf);
85-
if (ret != ERROR_MORE_DATA)
86-
goto cleanup_key;
87-
buf = malloc(buf_len);
88-
if (!buf)
89-
goto cleanup_key;
90-
}
91-
if (!buf || value_type != REG_SZ || buf_len < sizeof(*buf) || buf[buf_len / sizeof(*buf) - 1])
92-
goto cleanup_buf;
93-
if (FAILED(CLSIDFromString(buf, &instance_id)))
94-
goto cleanup_buf;
95-
96-
if (NciGetConnectionName(&instance_id, adapter_name, sizeof(adapter_name), NULL) != ERROR_SUCCESS)
97-
goto cleanup_buf;
9879
adapter_name[_countof(adapter_name) - 1] = L'0';
9980
if (!adapter_name[0])
100-
goto cleanup_buf;
101-
81+
continue;
10282
buf_len = WideCharToMultiByte(CP_UTF8, 0, adapter_name, -1, NULL, 0, NULL, NULL);
10383
if (!buf_len)
104-
goto cleanup_buf;
84+
continue;
10585
interface_name = malloc(buf_len);
10686
if (!interface_name)
107-
goto cleanup_buf;
87+
continue;
10888
buf_len = WideCharToMultiByte(CP_UTF8, 0, adapter_name, -1, interface_name, buf_len, NULL, NULL);
10989
if (!buf_len) {
11090
free(interface_name);
111-
goto cleanup_buf;
91+
continue;
11292
}
11393

11494
if (CM_Get_DevNode_Status(&status, &problem_code, dev_info_data.DevInst, 0) == CR_SUCCESS &&
@@ -118,26 +98,20 @@ static int kernel_get_wireguard_interfaces(struct string_list *list)
11898
entry = hashtable_find_or_insert_entry(&cached_kernel_interfaces, interface_name);
11999
free(interface_name);
120100
if (!entry)
121-
goto cleanup_entry;
101+
continue;
122102

123103
if (SetupDiGetDeviceInstanceIdW(dev_info, &dev_info_data, NULL, 0, &buf_len) || GetLastError() != ERROR_INSUFFICIENT_BUFFER)
124-
goto cleanup_entry;
104+
continue;
125105
entry->value = calloc(sizeof(WCHAR), buf_len);
126106
if (!entry->value)
127-
goto cleanup_entry;
107+
continue;
128108
if (!SetupDiGetDeviceInstanceIdW(dev_info, &dev_info_data, entry->value, buf_len, &buf_len)) {
129109
free(entry->value);
130110
entry->value = NULL;
131-
goto cleanup_entry;
111+
continue;
132112
}
133113

134-
cleanup_entry:
135-
will_have_cached_kernel_interfaces |= entry != NULL && entry->value != NULL;
136-
cleanup_buf:
137-
free(buf);
138-
cleanup_key:
139-
RegCloseKey(key);
140-
skip:;
114+
will_have_cached_kernel_interfaces = true;
141115
}
142116
SetupDiDestroyDeviceInfoList(dev_info);
143117
have_cached_kernel_interfaces = will_have_cached_kernel_interfaces;
@@ -187,11 +161,10 @@ static HANDLE kernel_interface_handle(const char *iface)
187161

188162
for (DWORD i = 0; !interfaces; ++i) {
189163
bool found = false;
190-
DWORD buf_len = 0, value_type, ret;
164+
DWORD buf_len = 0, value_type;
191165
WCHAR *buf = NULL, adapter_name[MAX_ADAPTER_NAME];
192166
SP_DEVINFO_DATA dev_info_data = { .cbSize = sizeof(SP_DEVINFO_DATA) };
193-
HKEY key;
194-
GUID instance_id;
167+
DEVPROPTYPE prop_type;
195168
char *interface_name;
196169

197170
if (!SetupDiEnumDeviceInfo(dev_info, i, &dev_info_data)) {
@@ -202,11 +175,12 @@ static HANDLE kernel_interface_handle(const char *iface)
202175

203176
while (!SetupDiGetDeviceRegistryPropertyW(dev_info, &dev_info_data, SPDRP_HARDWAREID, &value_type, (BYTE *)buf, buf_len, &buf_len)) {
204177
free(buf);
178+
buf = NULL;
205179
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
206-
goto skip;
180+
break;
207181
buf = malloc(buf_len);
208182
if (!buf)
209-
goto skip;
183+
break;
210184
}
211185

212186
if (!buf || value_type != REG_MULTI_SZ || buf_len < sizeof(*buf) * 2 || buf[buf_len / sizeof(*buf) - 1] || buf[buf_len / sizeof(*buf) - 2]) {
@@ -223,53 +197,28 @@ static HANDLE kernel_interface_handle(const char *iface)
223197
free(buf);
224198
if (!found)
225199
continue;
226-
found = false;
227-
buf = NULL;
228-
buf_len = 0;
229200

230-
key = SetupDiOpenDevRegKey(dev_info, &dev_info_data, DICS_FLAG_GLOBAL, 0, DIREG_DRV, KEY_QUERY_VALUE);
231-
if (key == INVALID_HANDLE_VALUE)
201+
if (!SetupDiGetDevicePropertyW(dev_info, &dev_info_data, &devpkey_name,
202+
&prop_type, (PBYTE)adapter_name,
203+
sizeof(adapter_name), NULL, 0) ||
204+
prop_type != DEVPROP_TYPE_STRING)
232205
continue;
233-
buf_len = 39 * sizeof(*buf);
234-
buf = malloc(buf_len);
235-
if (!buf)
236-
continue;
237-
while ((ret = RegQueryValueExW(key, L"NetCfgInstanceId", NULL, &value_type, (BYTE *)buf, &buf_len)) != ERROR_SUCCESS) {
238-
free(buf);
239-
if (ret != ERROR_MORE_DATA)
240-
goto cleanup_key;
241-
buf = malloc(buf_len);
242-
if (!buf)
243-
goto cleanup_key;
244-
}
245-
if (!buf || value_type != REG_SZ || buf_len < sizeof(*buf) || buf[buf_len / sizeof(*buf) - 1])
246-
goto cleanup_buf;
247-
if (FAILED(CLSIDFromString(buf, &instance_id)))
248-
goto cleanup_buf;
249-
250-
if (NciGetConnectionName(&instance_id, adapter_name, sizeof(adapter_name), NULL) != ERROR_SUCCESS)
251-
goto cleanup_buf;
252206
adapter_name[_countof(adapter_name) - 1] = L'0';
253207
if (!adapter_name[0])
254-
goto cleanup_buf;
255-
208+
continue;
256209
buf_len = WideCharToMultiByte(CP_UTF8, 0, adapter_name, -1, NULL, 0, NULL, NULL);
257210
if (!buf_len)
258-
goto cleanup_buf;
211+
continue;
259212
interface_name = malloc(buf_len);
260213
if (!interface_name)
261-
goto cleanup_buf;
214+
continue;
262215
buf_len = WideCharToMultiByte(CP_UTF8, 0, adapter_name, -1, interface_name, buf_len, NULL, NULL);
263216
if (!buf_len) {
264217
free(interface_name);
265-
goto cleanup_buf;
218+
continue;
266219
}
267220
found = !strcmp(interface_name, iface);
268221
free(interface_name);
269-
cleanup_buf:
270-
free(buf);
271-
cleanup_key:
272-
RegCloseKey(key);
273222
if (!found)
274223
continue;
275224

@@ -296,7 +245,6 @@ static HANDLE kernel_interface_handle(const char *iface)
296245
}
297246
cleanup_instance_id:
298247
free(buf);
299-
skip:;
300248
}
301249
SetupDiDestroyDeviceInfoList(dev_info);
302250
if (!interfaces) {

src/uapi/windows/wireguard.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,10 @@ typedef struct _WG_IOCTL_INTERFACE
7171
#define WG_IOCTL_GET CTL_CODE(45208U, 321, METHOD_OUT_DIRECT, FILE_READ_DATA | FILE_WRITE_DATA)
7272
#define WG_IOCTL_SET CTL_CODE(45208U, 322, METHOD_IN_DIRECT, FILE_READ_DATA | FILE_WRITE_DATA)
7373

74+
#define DEVPKEY_WG_NAME (DEVPROPKEY) { \
75+
{ 0x65726957, 0x7547, 0x7261, { 0x64, 0x4e, 0x61, 0x6d, 0x65, 0x4b, 0x65, 0x79 } }, \
76+
DEVPROPID_FIRST_USABLE + 1 \
77+
}
78+
79+
7480
#endif

src/wincompat/include/nci.h

Lines changed: 0 additions & 30 deletions
This file was deleted.

0 commit comments

Comments
 (0)