]> git.ipfire.org Git - thirdparty/dnspython.git/commitdiff
find enabled NICs on Windows Vista
authorBob Halley <halley@dnspython.org>
Thu, 8 Feb 2007 02:28:16 +0000 (02:28 +0000)
committerBob Halley <halley@dnspython.org>
Thu, 8 Feb 2007 02:28:16 +0000 (02:28 +0000)
ChangeLog
dns/resolver.py

index eb21452e479803c7aa5776056ca8f52a1cd7df6e..7b000097e59cd69bfa1fff2d7983e9f166220456 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2007-02-07  Bob Halley  <halley@dnspython.org>
+
+       * dns/resolver.py: Integrate code submitted by Paul Marks to
+         determine whether a Windows NIC is enabled.  The way dnspython
+         used to do this does not work on Windows Vista.
+
 2006-11-03  Bob Halley  <halley@dnspython.org>
 
        * dns/rdtypes/IN/DHCID.py: Added support for the DHCID RR type.
index c7faf3652bb40d8aea5209a14aa1b0b5bfcb3051..15eee73f2447523bae298e3cee54aaffe7aa4737 100644 (file)
@@ -453,16 +453,10 @@ class Resolver(object):
                             guid = _winreg.EnumKey(interfaces, i)
                             i += 1
                             key = _winreg.OpenKey(interfaces, guid)
+                            if not self._win32_is_nic_enabled(lm, guid, key):
+                                continue
                             try:
-                                # enabled interfaces seem to have a non-empty
-                                # NTEContextList
-                                try:
-                                    (nte, ttype) = _winreg.QueryValueEx(key,
-                                                             'NTEContextList')
-                                except WindowsError:
-                                    nte = None
-                                if nte:
-                                    self._config_win32_fromkey(key)
+                                self._config_win32_fromkey(key)
                             finally:
                                 key.Close()
                         except EnvironmentError:
@@ -472,6 +466,61 @@ class Resolver(object):
         finally:
             lm.Close()
 
+ def _win32_is_nic_enabled(self, lm, guid, interface_key):
+     # Look in the Windows Registry to determine whether the network
+     # interface corresponding to the given guid is enabled.
+     #
+     # (Code contributed by Paul Marks, thanks!)
+     #
+     try:
+         # This hard-coded location seems to be consistent, at least
+         # from Windows 2000 through Vista.
+         connection_key = _winreg.OpenKey(
+             lm,
+             r'SYSTEM\CurrentControlSet\Control\Network'
+             r'\{4D36E972-E325-11CE-BFC1-08002BE10318}'
+             r'\%s\Connection' % guid)
+
+         try:
+             # The PnpInstanceID points to a key inside Enum
+             (pnp_id, ttype) = _winreg.QueryValueEx(
+                 connection_key, 'PnpInstanceID')
+
+             if ttype != _winreg.REG_SZ:
+                 raise ValueError
+
+             device_key = _winreg.OpenKey(
+                 lm, r'SYSTEM\CurrentControlSet\Enum\%s' % pnp_id)
+
+             try:
+                 # Get ConfigFlags for this device
+                 (flags, ttype) = _winreg.QueryValueEx(
+                     device_key, 'ConfigFlags')
+
+                 if ttype != _winreg.REG_DWORD:
+                     raise ValueError
+
+                 # Based on experimentation, bit 0x1 indicates that the
+                 # device is disabled.
+                 return not (flags & 0x1)
+
+             finally:
+                 device_key.Close()
+         finally:
+             connection_key.Close()
+     except (EnvironmentError, ValueError):
+         # Pre-vista, enabled interfaces seem to have a non-empty
+         # NTEContextList; this was how dnspython detected enabled
+         # nics before the code above was contributed.  We've retained
+         # the old method since we don't know if the code above works
+         # on Windows 95/98/ME.
+         try:
+             (nte, ttype) = _winreg.QueryValueEx(interface_key,
+                                                 'NTEContextList')
+             return nte is not None
+         except WindowsError:
+             return False
+
     def _compute_timeout(self, start):
         now = time.time()
         if now < start: