[commit] r2249 - Add primitive for obtaining Win32 DNS info.

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

[commit] r2249 - Add primitive for obtaining Win32 DNS info.

commits-3
 
Author: andreas
Date: 2010-07-27 20:33:19 -0700 (Tue, 27 Jul 2010)
New Revision: 2249

Added:
   trunk/platforms/win32/vm/sqWin32DnsInfo.c
Modified:
   trunk/platforms/win32/vm/sqWin32Exports.c
Log:
Add primitive for obtaining Win32 DNS info.

Added: trunk/platforms/win32/vm/sqWin32DnsInfo.c
===================================================================
--- trunk/platforms/win32/vm/sqWin32DnsInfo.c                        (rev 0)
+++ trunk/platforms/win32/vm/sqWin32DnsInfo.c 2010-07-28 03:33:19 UTC (rev 2249)
@@ -0,0 +1,180 @@
+/* sqWin32DnsInfo.c: Gathers DNS information on Windows */
+#include <windows.h>
+#include "sq.h"
+
+extern struct VirtualMachine *interpreterProxy;
+#define vm interpreterProxy
+
+/* Large enough since we only use 3 entries with 1k bytes max */
+static char buf[20000];
+
+static char* lookup(HKEY hk, char *key) {
+  static char buf[1000];
+  DWORD dwLength, ret;
+  dwLength = sizeof(buf)-1;
+  *buf = 0;
+  ret = RegQueryValueEx(hk, key, NULL, NULL, buf, &dwLength);
+  if(buf[dwLength]) buf[dwLength] = 0;
+  return buf;
+}
+
+/* Print base registry info. Return true if DNS info was provided. */
+static int printBaseInfo(char *hkeyName) {
+  HKEY hkey;
+  DWORD ret;
+  char *value;
+  int result = 0;
+
+  ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, hkeyName, 0, KEY_READ, &hkey);
+  if(ret != ERROR_SUCCESS) {
+    printf("RegOpenKeyEx failed\n");
+    return vm->primitiveFail();
+  }
+
+  /* Provide Hostname info if present */
+  value = lookup(hkey, "Hostname");
+  if(*value) {
+    strcat(buf, "\nhostname ");
+    strcat(buf, value);
+  }
+
+  /* The list of suffixes to search */
+  value = lookup(hkey, "SearchList");
+  if(*value) {
+    strcat(buf, "\nsearch ");
+    strcat(buf, value);
+  }
+
+  /* The (static or dhcp) domain */
+  value = lookup(hkey, "Domain");
+  if(*value) {
+    strcat(buf, "\ndomain ");
+    strcat(buf, value);
+    strcat(buf, " # static");
+  } else {
+    value = lookup(hkey, "DhcpDomain");
+    if(*value) {
+      strcat(buf, "\ndomain ");
+      strcat(buf, value);
+      strcat(buf, " # dhcp");
+    }
+  }
+
+  /* And the (static or dhcp) configured name servers */
+  value = lookup(hkey, "NameServer");
+  if(*value) {
+    strcat(buf, "\nnameserver ");
+    strcat(buf, value);
+    strcat(buf, " # static");
+    result = 1;
+  } else {
+    value = lookup(hkey, "DhcpNameServer");
+    if(*value) {
+      strcat(buf, "\nnameserver ");
+      strcat(buf, value);
+      strcat(buf, " # dhcp");
+      result = 1;
+    }
+  }
+  RegCloseKey(hkey);
+  return result;
+}
+
+static int printAdapterInfo(char *hkeyName) {
+  HKEY hkey;
+  DWORD ret;
+  char *value;
+  int result = 0;
+
+  ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, hkeyName, 0, KEY_READ, &hkey);
+  if(ret != ERROR_SUCCESS) {
+    printf("RegOpenKeyEx failed\n");
+    return vm->primitiveFail();
+  }
+
+  /* Provide interface address if present */
+  value = lookup(hkey, "IPAddress");
+  if(*value && strcmp(value, "0.0.0.0")) {
+    /* Skip interface info since we can't currently determine
+       if the information is correct or a dead adapter */
+#if 0
+    strcat(buf, "\ninterface ");
+    strcat(buf, value);
+    strcat(buf, " # static");
+#endif
+  } else {
+    value = lookup(hkey, "DhcpIPAddress");
+    if(*value && strcmp(value, "0.0.0.0")) {
+#if 0
+      strcat(buf, "\ninterface ");
+      strcat(buf, value);
+      strcat(buf, " # dhcp ");
+#endif
+    } else {
+      /* Don't bother printing DNS for an adapter w/o IP */
+      goto done;
+    }
+  }
+
+  /* Print the (static or dhcp) configured name servers */
+  value = lookup(hkey, "NameServer");
+  if(*value) {
+    strcat(buf, "\nnameserver ");
+    strcat(buf, value);
+    strcat(buf, " # static");
+    result = 1;
+  } else {
+    value = lookup(hkey, "DhcpNameServer");
+    if(*value) {
+      strcat(buf, "\nnameserver ");
+      strcat(buf, value);
+      strcat(buf, " # dhcp");
+      result = 1;
+    }
+  }
+
+ done:
+  RegCloseKey(hkey);
+  return result;
+}
+
+#define BASE_KEY \
+  "SYSTEM\\CurrentControlSet\\services\\Tcpip\\Parameters\\Interfaces"
+
+EXPORT(int) primitiveDnsInfo(void) {
+  HKEY hkey;
+  sqInt sz, stroop;
+  DWORD dwLength, ret, index;
+  char adapter[1024];
+
+  *buf = 0;
+  strcat(buf, "# Global config settings");
+
+  /* Print the global options */
+  if(printBaseInfo("SYSTEM\\CurrentControlSet\\services\\Tcpip\\Parameters")) {
+    /* If we have DNS entries globally, skip enumerating all interfaces */
+    goto done;
+  }
+
+  /* Enumerate the available interfaces */
+  ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, BASE_KEY, 0, KEY_READ, &hkey);
+  for(index=0;;index++) {
+    char keyName[1024];
+    dwLength = sizeof(adapter);
+    ret = RegEnumKeyEx(hkey, index, adapter, &dwLength, NULL, NULL, NULL, NULL);
+    if(ret != ERROR_SUCCESS) break;
+    strcpy(keyName, BASE_KEY);
+    strcat(keyName, "\\");
+    strcat(keyName, adapter);
+    /* Should look up and print the adapter name */
+    printAdapterInfo(keyName);
+  }
+
+ done:
+  sz = strlen(buf);
+  stroop = vm->instantiateClassindexableSize(vm->classString(), sz);
+  if(!vm->failed()) {
+    memcpy(vm->firstIndexableField(stroop), buf, sz);
+    vm->popthenPush(vm->methodArgumentCount()+1, stroop);
+  }
+}

Modified: trunk/platforms/win32/vm/sqWin32Exports.c
===================================================================
--- trunk/platforms/win32/vm/sqWin32Exports.c 2010-07-23 01:09:10 UTC (rev 2248)
+++ trunk/platforms/win32/vm/sqWin32Exports.c 2010-07-28 03:33:19 UTC (rev 2249)
@@ -11,6 +11,7 @@
 int primitivePluginRequestFileHandle(void);
 int primitivePluginDestroyRequest(void);
 int primitivePluginRequestState(void);
+int primitiveDnsInfo(void);
 
 extern void* stWindow;
 extern void* firstMessageHook;
@@ -29,6 +30,7 @@
   {"","primitivePluginRequestFileHandle", primitivePluginRequestFileHandle},
   {"","primitivePluginDestroyRequest", primitivePluginDestroyRequest},
   {"","primitivePluginRequestState", primitivePluginRequestState},
+  {"","primitiveDnsInfo", primitiveDnsInfo},
   {"", "printf", printf},
   {"","stWindow", &stWindow},
   {"","firstMessageHook", &firstMessageHook},

Reply | Threaded
Open this post in threaded view
|

Re: [commit] r2249 - Add primitive for obtaining Win32 DNS info.

Frank Shearar
 
On 2010/07/28 05:33, [hidden email] wrote:

>
> Author: andreas
> Date: 2010-07-27 20:33:19 -0700 (Tue, 27 Jul 2010)
> New Revision: 2249
>
> Added:
>     trunk/platforms/win32/vm/sqWin32DnsInfo.c
> Modified:
>     trunk/platforms/win32/vm/sqWin32Exports.c
> Log:
> Add primitive for obtaining Win32 DNS info.

>
> Added: trunk/platforms/win32/vm/sqWin32DnsInfo.c
> ===================================================================
> --- trunk/platforms/win32/vm/sqWin32DnsInfo.c                        (rev 0)
> +++ trunk/platforms/win32/vm/sqWin32DnsInfo.c 2010-07-28 03:33:19 UTC (rev 2249)

I'd expected to see a bunch of calls to the IP Helper API - calls to
GetAdaptersAddresses and friends - for enumerating the adapters.

I don't think that IP Helper allows for asking about name resolution, so
even modulo my ignorance of talking to the registry for adapter info, I
like the below for its uniformity.

frank