Try to fix Windows compilation
[metze/wireshark/wip.git] / capture_win_ifnames.c
index bbd44eb1180ac85321345337008e9b86cb717580..7c9a7ebcb44631dd92503918f33f4a3fe8a309d5 100644 (file)
 
 #ifdef _WIN32
 
-#include <windows.h>
-#include <objbase.h> /* for CLSIDFromString() to convert guid text to a GUID */
-#include <tchar.h>
 #include <winsock2.h>
+#include <windows.h>
 #include <iphlpapi.h>
 #include <stdio.h>
 #include <stdlib.h>
 
 #include <ntddndis.h>
 
+#ifndef NDIS_IF_MAX_STRING_SIZE
+#define NDIS_IF_MAX_STRING_SIZE IF_MAX_STRING_SIZE   /* =256 in <ifdef.h> */
+#endif
+
+#ifndef NETIO_STATUS
+#define NETIO_STATUS DWORD
+#endif
+
 #include "log.h"
 
 #include "capture_ifinfo.h"
 #include "capture_win_ifnames.h"
 #include "wsutil/file_util.h"
 
-/* Link with ole32.lib - provides CLSIDFromString() to convert guid text to a GUID */
-#pragma comment(lib, "ole32.lib")
-
 /**********************************************************************************/
 gboolean IsWindowsVistaOrLater()
 {
@@ -236,22 +239,69 @@ static int GetInterfaceFriendlyNameFromDeviceGuid(__in GUID *guid, __out char **
     return 0;
 }
 
+static int gethexdigit(const char *p)
+{
+    if(*p >= '0' && *p <= '9'){
+        return *p - '0';
+    }else if(*p >= 'A' && *p <= 'F'){
+        return *p - 'A' + 0xA;
+    }else if(*p >= 'a' && *p <= 'f'){
+        return *p - 'a' + 0xa;
+    }else{
+        return -1; /* Not a hex digit */
+    }
+}
+
+static gboolean get8hexdigits(const char *p, DWORD *d)
+{
+    int digit;
+    DWORD val;
+    int i;
+
+    val = 0;
+    for(i = 0; i < 8; i++){
+        digit = gethexdigit(p++);
+        if(digit == -1){
+            return FALSE; /* Not a hex digit */
+        }
+        val = (val << 4) | digit;
+    }
+    *d = val;
+    return TRUE;
+}
+
+static gboolean get4hexdigits(const char *p, WORD *w)
+{
+    int digit;
+    WORD val;
+    int i;
+
+    val = 0;
+    for(i = 0; i < 4; i++){
+        digit = gethexdigit(p++);
+        if(digit == -1){
+            return FALSE; /* Not a hex digit */
+        }
+        val = (val << 4) | digit;
+    }
+    *w = val;
+    return TRUE;
+}
 
 /**********************************************************************************/
 /* returns the interface friendly name for a device name, if it is unable to
 * resolve the name, "" is returned */
-void get_windows_interface_friendlyname(/* IN */ char *interface_devicename, /* OUT */char **interface_friendlyname)
+void get_windows_interface_friendlyname(/* IN */ const char *interface_devicename, /* OUT */char **interface_friendlyname)
 {
     const char* guid_text;
     GUID guid;
+    int i;
+    int digit1, digit2;
 
     g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "test, 1,2,3");
 
     /* ensure we can return a result */
     if(interface_friendlyname==NULL){
-        g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "open_raw_pipe sdfsd");
-        fflush(stderr);
-        fflush(stdout);
         g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_ERROR,
             "invalid interface_friendlyname parameter to get_windows_interface_friendlyname() function.");
         return;
@@ -266,38 +316,98 @@ void get_windows_interface_friendlyname(/* IN */ char *interface_devicename, /*
         guid_text=interface_devicename;
     }
 
-    /*** Convert the guid text the GUID structure */
-    {
-        /* Part 1: guid_text to unicode, dynamically allocating sufficent memory for conversion*/
-        WCHAR wGuidText[39];
-        HRESULT hr;
-        int size=39; /* a guid should always been 38 unicode characters in length (+1 for null termination) */
-        size=MultiByteToWideChar(CP_ACP, 0, guid_text, -1, wGuidText, size);
-        if(size!=39){
-            /* guid text to unicode conversion failed */
-            g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_ERROR,
-                "Failed the extract guid from interface devicename, unicode convert result=%d, guid input ='%s', LastErrorCode=0x%08x.",
-                size, guid_text, GetLastError());
-            return;
+    /*
+     * If what follows is a GUID in {}, then convert it to a GUID structure
+     * and use that to look up the interface to get its friendly name.
+     */
+    if(*guid_text != '{'){
+        return; /* Nope, not enclosed in {} */
+    }
+    guid_text++;
+    /* There must be 8 hex digits; if so, they go into guid.Data1 */
+    if(!get8hexdigits(guid_text, &guid.Data1)){
+        return; /* nope, not 8 hex digits */
+    }
+    guid_text += 8;
+    /* Now there must be a hyphen */
+    if(*guid_text != '-'){
+        return; /* Nope */
+    }
+    guid_text++;
+    /* There must be 4 hex digits; if so, they go into guid.Data2 */
+    if(!get4hexdigits(guid_text, &guid.Data2)){
+        return; /* nope, not 4 hex digits */
+    }
+    guid_text += 4;
+    /* Now there must be a hyphen */
+    if(*guid_text != '-'){
+        return; /* Nope */
+    }
+    guid_text++;
+    /* There must be 4 hex digits; if so, they go into guid.Data3 */
+    if(!get4hexdigits(guid_text, &guid.Data3)){
+        return; /* nope, not 4 hex digits */
+    }
+    guid_text += 4;
+    /* Now there must be a hyphen */
+    if(*guid_text != '-'){
+        return; /* Nope */
+    }
+    guid_text++;
+    /*
+     * There must be 4 hex digits; if so, they go into the first 2 bytes
+     * of guid.Data4.
+     */
+    for(i = 0; i < 2; i++){
+        digit1 = gethexdigit(guid_text);
+        if(digit1 == -1){
+            return; /* Not a hex digit */
         }
-        /* Part 2: unicode guid text to GUID structure */
-        hr = CLSIDFromString(wGuidText, (LPCLSID)&guid);
-        if (hr != S_OK){
-            /* guid text to unicode conversion failed */
-            g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_ERROR,
-                "Failed to convert interface devicename guid to GUID structure, convert result=0x%08x, guid input ='%s', LastErrorCode=0x%08x.",
-                hr, guid_text, GetLastError());
-            return;
+        guid_text++;
+        digit2 = gethexdigit(guid_text);
+        if(digit2 == -1){
+            return; /* Not a hex digit */
+        }
+        guid_text++;
+        guid.Data4[i] = (digit1 << 4)|(digit2);
+    }
+    /* Now there must be a hyphen */
+    if(*guid_text != '-'){
+        return; /* Nope */
+    }
+    guid_text++;
+    /*
+     * There must be 12 hex digits; if so,t hey go into the next 6 bytes
+     * of guid.Data4.
+     */
+    for(i = 0; i < 6; i++){
+        digit1 = gethexdigit(guid_text);
+        if(digit1 == -1){
+            return; /* Not a hex digit */
+        }
+        guid_text++;
+        digit2 = gethexdigit(guid_text);
+        if(digit2 == -1){
+            return; /* Not a hex digit */
         }
+        guid_text++;
+        guid.Data4[i+2] = (digit1 << 4)|(digit2);
+    }
+    /* Now there must be a closing } */
+    if(*guid_text != '}'){
+        return; /* Nope */
+    }
+    guid_text++;
+    /* And that must be the end of the string */
+    if(*guid_text != '\0'){
+        return; /* Nope */
     }
 
     /* guid okay, get the interface friendly name associated with the guid */
     {
         int r=GetInterfaceFriendlyNameFromDeviceGuid(&guid, interface_friendlyname);
         if(r!=NO_ERROR){
-            g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_ERROR,
-                "Failed to retrieve interface friendly name associated with interface '%s', LastErrorCode=0x%08x.",
-                interface_devicename, GetLastError());
+            /* A message has been logged by GetInterfaceFriendlyNameFromDeviceGuid() */
             *interface_friendlyname=NULL; /* failed to get friendly name, ensure the ultimate result is NULL */
             return;
         }