Skip to content

Commit

Permalink
move getInterfaces() in Tools::Win32
Browse files Browse the repository at this point in the history
This function is heavily used outside of inventory task.
  • Loading branch information
guillomovitch committed Feb 27, 2013
1 parent 8d8817c commit 389c6af
Show file tree
Hide file tree
Showing 6 changed files with 329 additions and 342 deletions.
4 changes: 2 additions & 2 deletions lib/FusionInventory/Agent/Task/Deploy/P2P.pm
Expand Up @@ -101,8 +101,8 @@ sub findPeer {
@interfaces = FusionInventory::Agent::Tools::Linux::getInterfacesFromIfconfig();

} elsif ($OSNAME eq 'MSWin32') {
FusionInventory::Agent::Task::Inventory::Input::Win32::Networks->require();
@interfaces = FusionInventory::Agent::Task::Inventory::Input::Win32::Networks::_getInterfaces();
FusionInventory::Agent::Tools::Win32->require();
@interfaces = FusionInventory::Agent::Tools::Win32::getInterfaces();
}


Expand Down
149 changes: 1 addition & 148 deletions lib/FusionInventory/Agent/Task/Inventory/Input/Win32/Networks.pm
Expand Up @@ -17,7 +17,7 @@ sub doInventory {
my $inventory = $params{inventory};
my (@gateways, @dns, @ips);

foreach my $interface (_getInterfaces()) {
foreach my $interface (getInterfaces()) {
push @gateways, $interface->{IPGATEWAY}
if $interface->{IPGATEWAY};
push @dns, $interface->{dns}
Expand All @@ -42,151 +42,4 @@ sub doInventory {

}

sub _getInterfaces {

my @configurations;

foreach my $object (getWMIObjects(
class => 'Win32_NetworkAdapterConfiguration',
properties => [ qw/Index Description IPEnabled DHCPServer MACAddress
MTU DefaultIPGateway DNSServerSearchOrder IPAddress
IPSubnet/ ]
)) {

my $configuration = {
DESCRIPTION => $object->{Description},
STATUS => $object->{IPEnabled} ? "Up" : "Down",
IPDHCP => $object->{DHCPServer},
MACADDR => $object->{MACAddress},
MTU => $object->{MTU}
};

if ($object->{DefaultIPGateway}) {
$configuration->{IPGATEWAY} = $object->{DefaultIPGateway}->[0];
}

if ($object->{DNSServerSearchOrder}) {
$configuration->{dns} = $object->{DNSServerSearchOrder}->[0];
}

if ($object->{IPAddress}) {
foreach my $address (@{$object->{IPAddress}}) {
my $prefix = shift @{$object->{IPSubnet}};
push @{$configuration->{addresses}}, [ $address, $prefix ];
}
}

$configurations[$object->{Index}] = $configuration;
}

my @interfaces;

foreach my $object (getWMIObjects(
class => 'Win32_NetworkAdapter',
properties => [ qw/Index PNPDeviceID Speed PhysicalAdapter
AdapterTypeId/ ]
)) {
# http://comments.gmane.org/gmane.comp.monitoring.fusion-inventory.devel/34
next unless $object->{PNPDeviceID};

my $configuration = $configurations[$object->{Index}];

if ($configuration->{addresses}) {
foreach my $address (@{$configuration->{addresses}}) {

my $interface = {
SPEED => $object->{Speed},
PNPDEVICEID => $object->{PNPDeviceID},
MACADDR => $configuration->{MACADDR},
DESCRIPTION => $configuration->{DESCRIPTION},
STATUS => $configuration->{STATUS},
IPDHCP => $configuration->{IPDHCP},
MTU => $configuration->{MTU},
IPGATEWAY => $configuration->{IPGATEWAY},
dns => $configuration->{dns},
};

if ($address->[0] =~ /$ip_address_pattern/) {
$interface->{IPADDRESS} = $address->[0];
$interface->{IPMASK} = $address->[1];
$interface->{IPSUBNET} = getSubnetAddress(
$interface->{IPADDRESS},
$interface->{IPMASK}
);
} else {
$interface->{IPADDRESS6} = $address->[0];
$interface->{IPMASK6} = getNetworkMaskIPv6($address->[1]);
$interface->{IPSUBNET6} = getSubnetAddressIPv6(
$interface->{IPADDRESS6},
$interface->{IPMASK6}
);
}

$interface->{VIRTUALDEV} = _isVirtual($object, $configuration);
$interface->{TYPE} = _getType($object);

push @interfaces, $interface;
}
} else {
next unless $configuration->{MACADDR};

my $interface = {
SPEED => $object->{Speed},
PNPDEVICEID => $object->{PNPDeviceID},
MACADDR => $configuration->{MACADDR},
DESCRIPTION => $configuration->{DESCRIPTION},
STATUS => $configuration->{STATUS},
IPDHCP => $configuration->{IPDHCP},
MTU => $configuration->{MTU},
IPGATEWAY => $configuration->{IPGATEWAY},
dns => $configuration->{dns},
};

$interface->{VIRTUALDEV} = _isVirtual($object, $configuration);
$interface->{TYPE} = _getType($object);

push @interfaces, $interface;
}

}

return
@interfaces;

}

sub _isVirtual {
my ($object, $configuration) = @_;

# PhysicalAdapter only work on OS > XP
if (defined $object->{PhysicalAdapter}) {
return $object->{PhysicalAdapter} ? 0 : 1;
}

# http://forge.fusioninventory.org/issues/1166
if ($configuration->{DESCRIPTION} &&
$configuration->{DESCRIPTION} =~ /RAS/ &&
$configuration->{DESCRIPTION} =~ /Adapter/i
) {
return 1;
}

return $object->{PNPDeviceID} =~ /^ROOT/ ? 1 : 0;
}

sub _getType {
my ($object) = @_;

return unless defined $object->{AdapterTypeId};

# available adapter types:
# http://msdn.microsoft.com/en-us/library/windows/desktop/aa394216%28v=vs.85%29.aspx
# don't bother discriminating between wired and wireless ethernet adapters
# for sake of simplicity
return
$object->{AdapterTypeId} == 0 ? 'ethernet' :
$object->{AdapterTypeId} == 9 ? 'wifi' :
undef ;
}

1;
4 changes: 2 additions & 2 deletions lib/FusionInventory/Agent/Task/WakeOnLan.pm
Expand Up @@ -128,8 +128,8 @@ sub _getInterface {
last;
}
if ($OSNAME eq 'MSWin32') {
FusionInventory::Agent::Task::Inventory::Input::Win32::Networks->require();
$function = \&FusionInventory::Agent::Task::Inventory::Input::Win32::Networks::_getInterfaces;
FusionInventory::Agent::Tools::Win32->require();
$function = \&FusionInventory::Agent::Tools::Win32::getInterfaces;
last;
}
}
Expand Down
149 changes: 149 additions & 0 deletions lib/FusionInventory/Agent/Tools/Win32.pm
Expand Up @@ -22,6 +22,7 @@ use Win32::TieRegistry (
);

use FusionInventory::Agent::Tools;
use FusionInventory::Agent::Tools::Network;

Win32::OLE->Option(CP => Win32::OLE::CP_UTF8);

Expand All @@ -32,6 +33,7 @@ our @EXPORT = qw(
encodeFromRegistry
KEY_WOW64_64
KEY_WOW64_32
getInterfaces
getRegistryValue
getRegistryKey
getWMIObjects
Expand Down Expand Up @@ -270,6 +272,153 @@ sub parseProductKey {
return $cd_key;
}

sub getInterfaces {

my @configurations;

foreach my $object (getWMIObjects(
class => 'Win32_NetworkAdapterConfiguration',
properties => [ qw/Index Description IPEnabled DHCPServer MACAddress
MTU DefaultIPGateway DNSServerSearchOrder IPAddress
IPSubnet/ ]
)) {

my $configuration = {
DESCRIPTION => $object->{Description},
STATUS => $object->{IPEnabled} ? "Up" : "Down",
IPDHCP => $object->{DHCPServer},
MACADDR => $object->{MACAddress},
MTU => $object->{MTU}
};

if ($object->{DefaultIPGateway}) {
$configuration->{IPGATEWAY} = $object->{DefaultIPGateway}->[0];
}

if ($object->{DNSServerSearchOrder}) {
$configuration->{dns} = $object->{DNSServerSearchOrder}->[0];
}

if ($object->{IPAddress}) {
foreach my $address (@{$object->{IPAddress}}) {
my $prefix = shift @{$object->{IPSubnet}};
push @{$configuration->{addresses}}, [ $address, $prefix ];
}
}

$configurations[$object->{Index}] = $configuration;
}

my @interfaces;

foreach my $object (getWMIObjects(
class => 'Win32_NetworkAdapter',
properties => [ qw/Index PNPDeviceID Speed PhysicalAdapter
AdapterTypeId/ ]
)) {
# http://comments.gmane.org/gmane.comp.monitoring.fusion-inventory.devel/34
next unless $object->{PNPDeviceID};

my $configuration = $configurations[$object->{Index}];

if ($configuration->{addresses}) {
foreach my $address (@{$configuration->{addresses}}) {

my $interface = {
SPEED => $object->{Speed},
PNPDEVICEID => $object->{PNPDeviceID},
MACADDR => $configuration->{MACADDR},
DESCRIPTION => $configuration->{DESCRIPTION},
STATUS => $configuration->{STATUS},
IPDHCP => $configuration->{IPDHCP},
MTU => $configuration->{MTU},
IPGATEWAY => $configuration->{IPGATEWAY},
dns => $configuration->{dns},
};

if ($address->[0] =~ /$ip_address_pattern/) {
$interface->{IPADDRESS} = $address->[0];
$interface->{IPMASK} = $address->[1];
$interface->{IPSUBNET} = getSubnetAddress(
$interface->{IPADDRESS},
$interface->{IPMASK}
);
} else {
$interface->{IPADDRESS6} = $address->[0];
$interface->{IPMASK6} = getNetworkMaskIPv6($address->[1]);
$interface->{IPSUBNET6} = getSubnetAddressIPv6(
$interface->{IPADDRESS6},
$interface->{IPMASK6}
);
}

$interface->{VIRTUALDEV} = _isVirtual($object, $configuration);
$interface->{TYPE} = _getType($object);

push @interfaces, $interface;
}
} else {
next unless $configuration->{MACADDR};

my $interface = {
SPEED => $object->{Speed},
PNPDEVICEID => $object->{PNPDeviceID},
MACADDR => $configuration->{MACADDR},
DESCRIPTION => $configuration->{DESCRIPTION},
STATUS => $configuration->{STATUS},
IPDHCP => $configuration->{IPDHCP},
MTU => $configuration->{MTU},
IPGATEWAY => $configuration->{IPGATEWAY},
dns => $configuration->{dns},
};

$interface->{VIRTUALDEV} = _isVirtual($object, $configuration);
$interface->{TYPE} = _getType($object);

push @interfaces, $interface;
}

}

return
@interfaces;

}

sub _isVirtual {
my ($object, $configuration) = @_;

# PhysicalAdapter only work on OS > XP
if (defined $object->{PhysicalAdapter}) {
return $object->{PhysicalAdapter} ? 0 : 1;
}

# http://forge.fusioninventory.org/issues/1166
if ($configuration->{DESCRIPTION} &&
$configuration->{DESCRIPTION} =~ /RAS/ &&
$configuration->{DESCRIPTION} =~ /Adapter/i
) {
return 1;
}

return $object->{PNPDeviceID} =~ /^ROOT/ ? 1 : 0;
}

sub _getType {
my ($object) = @_;

return unless defined $object->{AdapterTypeId};

# available adapter types:
# http://msdn.microsoft.com/en-us/library/windows/desktop/aa394216%28v=vs.85%29.aspx
# don't bother discriminating between wired and wireless ethernet adapters
# for sake of simplicity
return
$object->{AdapterTypeId} == 0 ? 'ethernet' :
$object->{AdapterTypeId} == 9 ? 'wifi' :
undef ;
}

1;
__END__
Expand Down

0 comments on commit 389c6af

Please sign in to comment.