1 /* capture_win_ifnames.c
2 * Routines supporting the use of Windows friendly interface names within Wireshark
3 * Copyright 2011-2012, Mike Garratt <wireshark@evn.co.nz>
5 * Wireshark - Network traffic analyzer
6 * By Gerald Combs <gerald@wireshark.org>
7 * Copyright 1998 Gerald Combs
9 * SPDX-License-Identifier: GPL-2.0-or-later
16 #include <wireshark.h>
26 #ifndef NDIS_IF_MAX_STRING_SIZE
27 #define NDIS_IF_MAX_STRING_SIZE IF_MAX_STRING_SIZE /* =256 in <ifdef.h> */
31 #define NETIO_STATUS DWORD
34 #include "capture/capture_ifinfo.h"
35 #include "capture/capture_win_ifnames.h"
37 #include <wsutil/file_util.h>
39 static int gethexdigit(const char *p
)
41 if(*p
>= '0' && *p
<= '9'){
43 }else if(*p
>= 'A' && *p
<= 'F'){
44 return *p
- 'A' + 0xA;
45 }else if(*p
>= 'a' && *p
<= 'f'){
46 return *p
- 'a' + 0xa;
48 return -1; /* Not a hex digit */
52 static bool get8hexdigits(const char *p
, DWORD
*d
)
59 for(i
= 0; i
< 8; i
++){
60 digit
= gethexdigit(p
++);
62 return false; /* Not a hex digit */
64 val
= (val
<< 4) | digit
;
70 static bool get4hexdigits(const char *p
, WORD
*w
)
77 for(i
= 0; i
< 4; i
++){
78 digit
= gethexdigit(p
++);
80 return false; /* Not a hex digit */
82 val
= (val
<< 4) | digit
;
89 * If a string is a GUID in {}, fill in a GUID structure with the GUID
90 * value and return true; otherwise, if the string is not a valid GUID
91 * in {}, return false.
94 parse_as_guid(const char *guid_text
, GUID
*guid
)
99 if(*guid_text
!= '{'){
100 return false; /* Nope, not enclosed in {} */
103 /* There must be 8 hex digits; if so, they go into guid->Data1 */
104 if(!get8hexdigits(guid_text
, &guid
->Data1
)){
105 return false; /* nope, not 8 hex digits */
108 /* Now there must be a hyphen */
109 if(*guid_text
!= '-'){
110 return false; /* Nope */
113 /* There must be 4 hex digits; if so, they go into guid->Data2 */
114 if(!get4hexdigits(guid_text
, &guid
->Data2
)){
115 return false; /* nope, not 4 hex digits */
118 /* Now there must be a hyphen */
119 if(*guid_text
!= '-'){
120 return false; /* Nope */
123 /* There must be 4 hex digits; if so, they go into guid->Data3 */
124 if(!get4hexdigits(guid_text
, &guid
->Data3
)){
125 return false; /* nope, not 4 hex digits */
128 /* Now there must be a hyphen */
129 if(*guid_text
!= '-'){
130 return false; /* Nope */
134 * There must be 4 hex digits; if so, they go into the first 2 bytes
137 for(i
= 0; i
< 2; i
++){
138 digit1
= gethexdigit(guid_text
);
140 return false; /* Not a hex digit */
143 digit2
= gethexdigit(guid_text
);
145 return false; /* Not a hex digit */
148 guid
->Data4
[i
] = (digit1
<< 4)|(digit2
);
150 /* Now there must be a hyphen */
151 if(*guid_text
!= '-'){
152 return false; /* Nope */
156 * There must be 12 hex digits; if so,t hey go into the next 6 bytes
159 for(i
= 0; i
< 6; i
++){
160 digit1
= gethexdigit(guid_text
);
162 return false; /* Not a hex digit */
165 digit2
= gethexdigit(guid_text
);
167 return false; /* Not a hex digit */
170 guid
->Data4
[i
+2] = (digit1
<< 4)|(digit2
);
172 /* Now there must be a closing } */
173 if(*guid_text
!= '}'){
174 return false; /* Nope */
177 /* And that must be the end of the string */
178 if(*guid_text
!= '\0'){
179 return false; /* Nope */
184 /**********************************************************************************/
185 /* Get the friendly name for the given GUID */
187 get_interface_friendly_name_from_device_guid(__in GUID
*guid
)
191 /* Need to convert an Interface GUID to the interface friendly name (e.g. "Local Area Connection")
192 * The functions required to do this all reside within iphlpapi.dll
195 NET_LUID InterfaceLuid
;
196 hr
= ConvertInterfaceGuidToLuid(guid
, &InterfaceLuid
);
198 /* guid->luid success */
199 WCHAR wName
[NDIS_IF_MAX_STRING_SIZE
+ 1];
200 hr
= ConvertInterfaceLuidToAlias(&InterfaceLuid
, wName
, NDIS_IF_MAX_STRING_SIZE
+1);
202 /* luid->friendly name success */
204 /* Get the required buffer size, and then convert the string
205 * from UTF-16 to UTF-8. */
208 size
= WideCharToMultiByte(CP_UTF8
, 0, wName
, -1, NULL
, 0, NULL
, NULL
);
210 name
= (char *) g_malloc(size
);
212 size
= WideCharToMultiByte(CP_UTF8
, 0, wName
, -1, name
, size
, NULL
, NULL
);
216 /* Failed, clean up the allocation */
223 /* Failed to get a name */
228 * Given an interface name, try to extract the GUID from it and parse it.
229 * If that fails, return NULL; if that succeeds, attempt to get the
230 * friendly name for the interface in question. If that fails, return
231 * NULL, otherwise return the friendly name, allocated with g_malloc()
232 * (so that it must be freed with g_free()).
235 get_windows_interface_friendly_name(const char *interface_devicename
)
237 const char* guid_text
;
240 /* Extract the guid text from the interface device name */
241 if(strncmp("\\Device\\NPF_", interface_devicename
, 12)==0){
242 guid_text
=interface_devicename
+12; /* skip over the '\Device\NPF_' prefix, assume the rest is the guid text */
244 guid_text
=interface_devicename
;
247 if (!parse_as_guid(guid_text
, &guid
)){
248 return NULL
; /* not a GUID, so no friendly name */
251 /* guid okay, get the interface friendly name associated with the guid */
252 return get_interface_friendly_name_from_device_guid(&guid
);
255 /**************************************************************************************/
259 * Editor modelines - https://www.wireshark.org/tools/modelines.html
264 * indent-tabs-mode: nil
267 * vi: set shiftwidth=4 tabstop=8 expandtab:
268 * :indentSize=4:tabSize=8:noTabs=true: