[Pkg-puppet-devel] [facter] 36/61: (#16668) Support IPv6 adapter binding order

Stig Sandbeck Mathisen ssm at debian.org
Mon Nov 4 15:02:00 UTC 2013


This is an automated email from the git hooks/post-receive script.

ssm pushed a commit to branch master
in repository facter.

commit 9f18629dd74a9fd6323e454a73a93837f384df5b
Author: Josh Cooper <josh at puppetlabs.com>
Date:   Mon Jul 29 17:02:01 2013 -0700

    (#16668) Support IPv6 adapter binding order
    
    Previously, if multiple interfaces had the same IP connection metric,
    then we would always use the IPv4 specific adapter binding order, which
    could be incorrect when reporting IPv6 IP related facts.
    
    This commit adds a method for returning the ordered list of interfaces
    supporting IPv6, and updates the various facts to use the appropriate
    IPv4 or IPv6 method.
---
 lib/facter/ipaddress.rb       |    3 +-
 lib/facter/ipaddress6.rb      |    2 +-
 lib/facter/netmask.rb         |    2 +-
 lib/facter/util/ip/windows.rb |   73 ++++++++++++++++++++++++++++++++---------
 lib/facter/util/macaddress.rb |    2 +-
 5 files changed, 61 insertions(+), 21 deletions(-)

diff --git a/lib/facter/ipaddress.rb b/lib/facter/ipaddress.rb
index 97ed464..f4b46b1 100644
--- a/lib/facter/ipaddress.rb
+++ b/lib/facter/ipaddress.rb
@@ -105,12 +105,11 @@ end
 
 Facter.add(:ipaddress) do
   confine :kernel => %w{windows}
-
   setcode do
     require 'facter/util/ip/windows'
     ipaddr = nil
 
-    adapters = Facter::Util::IP::Windows.get_preferred_network_adapters
+    adapters = Facter::Util::IP::Windows.get_preferred_ipv4_adapters
     adapters.find do |nic|
       nic.IPAddress.any? do |addr|
         ipaddr = addr if Facter::Util::IP::Windows.valid_ipv4_address?(addr)
diff --git a/lib/facter/ipaddress6.rb b/lib/facter/ipaddress6.rb
index 559088b..c60cf3f 100644
--- a/lib/facter/ipaddress6.rb
+++ b/lib/facter/ipaddress6.rb
@@ -69,7 +69,7 @@ Facter.add(:ipaddress6) do
     require 'facter/util/ip/windows'
     ipaddr = nil
 
-    adapters = Facter::Util::IP::Windows.get_preferred_network_adapters
+    adapters = Facter::Util::IP::Windows.get_preferred_ipv6_adapters
     adapters.find do |nic|
       nic.IPAddress.any? do |addr|
         ipaddr = addr if Facter::Util::IP::Windows.valid_ipv6_address?(addr)
diff --git a/lib/facter/netmask.rb b/lib/facter/netmask.rb
index 9f749b3..4730e5f 100644
--- a/lib/facter/netmask.rb
+++ b/lib/facter/netmask.rb
@@ -29,7 +29,7 @@ Facter.add(:netmask) do
 
     mask = nil
 
-    adapters = Facter::Util::IP::Windows.get_preferred_network_adapters
+    adapters = Facter::Util::IP::Windows.get_preferred_ipv4_adapters
     adapters.find do |nic|
       nic.IPSubnet.any? do |subnet|
         mask = subnet if Facter::Util::IP::Windows.valid_ipv4_address?(subnet)
diff --git a/lib/facter/util/ip/windows.rb b/lib/facter/util/ip/windows.rb
index aec988e..7c5aba3 100644
--- a/lib/facter/util/ip/windows.rb
+++ b/lib/facter/util/ip/windows.rb
@@ -51,7 +51,6 @@ class Facter::Util::IP::Windows
   # the MTU for eth0.
   #
   # @param interface [String] label [String]
-  #
   # @return [String] or [NilClass]
   #
   # @api private
@@ -99,36 +98,79 @@ class Facter::Util::IP::Windows
     nics
   end
 
-  # Gets a list of active adapters and sorts by the lowest connection metric (aka best weight) and MACAddress to ensure order
+  # Gets a list of active IPv4 network adapter configurations sorted by the
+  # lowest IP connection metric. If two configurations have the same metric,
+  # then the IPv4 specific binding order as specified in the registry will
+  # be used.
   #
-  # @return [Win32OLE]
+  # return [Array<Win32OLE>]
   #
   # @api private
-  def self.get_preferred_network_adapters
-    require 'facter/util/registry'
-    bindings = {}
-
-    Facter::Util::Registry.hklm_read('SYSTEM\CurrentControlSet\Services\Tcpip\Linkage','Bind').each_with_index do |entry, index|
-      match_data = entry.match(/\\Device\\(\{.*\})/)
-      unless match_data.nil?
-        bindings[match_data[1]] = index
-      end
-    end
+  def self.get_preferred_ipv4_adapters
+    get_preferred_network_adapters(Bindings4.new)
+  end
 
+  # Gets a list of active IPv6 network adapter configurations sorted by the
+  # lowest IP connection metric. If two configurations have the same metric,
+  # then the IPv6 specific binding order as specified in the registry will
+  # be used.
+  #
+  # return [Array<Win32OLE>]
+  #
+  # @api private
+  def self.get_preferred_ipv6_adapters
+    get_preferred_network_adapters(Bindings6.new)
+  end
+
+  # Gets a list of active network adapter configurations sorted by the lowest
+  # IP connection metric. If two configurations have the same metric, then
+  # the adapter binding order as specified in the registry will be used.
+  # Note the order may different for IPv4 vs IPv6 addresses.
+  #
+  # @see http://support.microsoft.com/kb/894564
+  # @return [Array<Win32OLE>]
+  #
+  # @api private
+  def self.get_preferred_network_adapters(bindings)
     network_adapter_configurations.sort do |nic_left,nic_right|
       cmp = nic_left.IPConnectionMetric <=> nic_right.IPConnectionMetric
       if cmp == 0
-        bindings[nic_left.SettingID] <=> bindings[nic_right.SettingID]
+        bindings.bindings[nic_left.SettingID] <=> bindings.bindings[nic_right.SettingID]
       else
         cmp
       end
     end
   end
 
+  class Bindings4
+    def initialize
+      @key = 'SYSTEM\CurrentControlSet\Services\Tcpip\Linkage'
+    end
+
+    def bindings
+      require 'facter/util/registry'
+      bindings = {}
+
+      Facter::Util::Registry.hklm_read(@key, 'Bind').each_with_index do |entry, index|
+        match_data = entry.match(/\\Device\\(\{.*\})/)
+        unless match_data.nil?
+          bindings[match_data[1]] = index
+        end
+      end
+
+      bindings
+    end
+  end
+
+  class Bindings6 < Bindings4
+    def initialize
+      @key = 'SYSTEM\CurrentControlSet\Services\Tcpip6\Linkage'
+    end
+  end
+
   # Determines if the value passed in is a valid ipv4 address.
   #
   # @param ip_address [String]
-  #
   # @return [String] or [NilClass]
   #
   # @api private
@@ -148,7 +190,6 @@ class Facter::Util::IP::Windows
   # Determines if the value passed in is a valid ipv6 address.
   #
   # @param ip_address [String]
-  #
   # @return [String] or [NilClass]
   #
   # @api private
diff --git a/lib/facter/util/macaddress.rb b/lib/facter/util/macaddress.rb
index 0309971..2848672 100644
--- a/lib/facter/util/macaddress.rb
+++ b/lib/facter/util/macaddress.rb
@@ -35,7 +35,7 @@ module Facter::Util::Macaddress
     def macaddress
       require 'facter/util/ip/windows'
 
-      adapter = Facter::Util::IP::Windows.get_preferred_network_adapters.first
+      adapter = Facter::Util::IP::Windows.get_preferred_ipv4_adapters.first
       adapter ? adapter.MACAddress : nil
     end
     module_function :macaddress

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-puppet/facter.git



More information about the Pkg-puppet-devel mailing list