/usr/share/fusioninventory/lib/FusionInventory/Agent/Tools/Network.pm is in fusioninventory-agent 1:2.3.10.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 | package FusionInventory::Agent::Tools::Network;
use strict;
use warnings;
use base 'Exporter';
use UNIVERSAL::require;
use Net::IP qw(:PROC);
use FusionInventory::Agent::Tools;
our @EXPORT = qw(
$mac_address_pattern
$ib_mac_address_pattern
$any_mac_address_pattern
$ip_address_pattern
$alt_mac_address_pattern
$hex_ip_address_pattern
$network_pattern
getSubnetAddress
getSubnetAddressIPv6
getNetworkMask
getNetworkMaskIPv6
hex2canonical
alt2canonical
resolve
compile
isPartOf
);
my $dec_byte = qr/[0-9]{1,3}/;
my $hex_byte = qr/[0-9A-F]{1,2}/i;
my $padded_hex_byte = qr/[0-9A-F]{2}/i;
our $mac_address_pattern = qr/
$hex_byte : $hex_byte : $hex_byte : $hex_byte : $hex_byte : $hex_byte
/x;
our $ib_mac_address_pattern = qr/
$hex_byte : $hex_byte : $hex_byte : $hex_byte : $hex_byte : $hex_byte :
$hex_byte : $hex_byte : $hex_byte : $hex_byte : $hex_byte : $hex_byte :
$hex_byte : $hex_byte : $hex_byte : $hex_byte : $hex_byte : $hex_byte :
$hex_byte : $hex_byte
/x;
our $any_mac_address_pattern = qr/
(?:$ib_mac_address_pattern|$mac_address_pattern)
/x;
our $ip_address_pattern = qr/
$dec_byte \. $dec_byte \. $dec_byte \. $dec_byte
/x;
our $alt_mac_address_pattern = qr/
$padded_hex_byte
$padded_hex_byte
$padded_hex_byte
$padded_hex_byte
$padded_hex_byte
$padded_hex_byte
/x;
our $hex_ip_address_pattern = qr/
$padded_hex_byte
$padded_hex_byte
$padded_hex_byte
$padded_hex_byte
/x;
our $network_pattern = qr/
$dec_byte (?:\. $dec_byte (?:\. $dec_byte (?:\. $dec_byte)?)?)? \/ \d{1,2}
/x;
sub getSubnetAddress {
my ($address, $mask) = @_;
return undef unless $address && $mask; ## no critic (ExplicitReturnUndef)
my $binaddress = ip_iptobin($address, 4);
my $binmask = ip_iptobin($mask, 4);
my $binsubnet = $binaddress & $binmask; ## no critic (ProhibitBitwise)
return ip_bintoip($binsubnet, 4);
}
sub getSubnetAddressIPv6 {
my ($address, $mask) = @_;
return undef unless $address && $mask; ## no critic (ExplicitReturnUndef)
my $binaddress = ip_iptobin(ip_expand_address($address, 6), 6);
my $binmask = ip_iptobin(ip_expand_address($mask, 6), 6);
my $binsubnet = $binaddress & $binmask; ## no critic (ProhibitBitwise)
return ip_compress_address(ip_bintoip($binsubnet, 6), 6);
}
sub hex2canonical {
my ($address) = @_;
return unless $address;
my @bytes = $address =~ /^(?:0x)?(..)(..)(..)(..)$/;
return join('.', map { hex($_) } @bytes);
}
sub alt2canonical {
my ($address) = @_;
return unless $address;
my @bytes = $address =~ /^(?:0x)?(..)(..)(..)(..)(..)(..)$/;
return join(':', @bytes);
}
sub getNetworkMask {
my ($prefix) = @_;
return undef unless $prefix; ## no critic (ExplicitReturnUndef)
return ip_bintoip(ip_get_mask($prefix, 4), 4);
}
sub getNetworkMaskIPv6 {
my ($prefix) = @_;
return undef unless $prefix; ## no critic (ExplicitReturnUndef)
return ip_compress_address(ip_bintoip(ip_get_mask($prefix, 6), 6), 6);
}
sub resolve {
my ($string, $logger) = @_;
my @ret;
Socket::GetAddrInfo->require();
Socket->require();
my ($error, @results) = Socket::GetAddrInfo::getaddrinfo(
$string, "", { socktype => Socket::SOCK_RAW() }
);
if ($error) {
$logger->error(
"unable to get address for '$string': $error"
) if $logger;
return;
}
# and push all of their addresses in the list
foreach my $result (@results) {
my ($error, $host) = Socket::GetAddrInfo::getnameinfo(
$result->{addr},
Socket::GetAddrInfo::NI_NUMERICHOST(),
);
if ($error) {
$logger->error(
"unable to translate binary address for '$string': $error"
) if $logger;
next;
}
# Drop the zone index, as Net::IP does not support it
$host =~ s/%.*$//;
push @ret, Net::IP->new($host);
}
return @ret;
}
sub compile {
my ($string, $logger) = @_;
return unless $string;
# that's already an IP address, just convert it
return Net::IP->new($string)
if $string =~ /^$ip_address_pattern/;
# otherwise resolve the name
return resolve($string, $logger);
}
sub isPartOf {
my ($string, $ranges, $logger) = @_;
return unless $string;
return unless $ranges;
my $address = Net::IP->new($string);
if (!$address) {
$logger->error("Not well formatted source IP: $string");
return;
}
foreach my $range (@{$ranges}) {
my $result = $address->overlaps($range);
if (!$result && Net::IP::Error()) {
$logger->debug("Server: ".Net::IP::Error());
next;
}
# included in trusted range
return 1 if $result == $IP_A_IN_B_OVERLAP;
# equals trusted address
return 1 if $result == $IP_IDENTICAL;
}
return 0;
}
1;
__END__
=head1 NAME
FusionInventory::Agent::Tools::Network - Network-related patterns and functions
=head1 DESCRIPTION
This module provides some network-related patterns and functions.
=head1 PATTERNS
=head2 mac_address_pattern
This pattern matches a MAC address in canonical form (aa:bb:cc:dd:ee:ff).
=head2 ip_address_pattern
This pattern matches an IP address in canonical form (xyz.xyz.xyz.xyz).
=head2 alt_mac_address_pattern
This pattern matches a MAC address in alternative form (aabbccddeeff).
=head2 hex_ip_address_pattern
This pattern matches an IP address in hexadecimal form (aabbccdd).
=head1 FUNCTIONS
=head2 hex2canonical($address)
Convert an ip address from hexadecimal to canonical form.
=head2 alt2canonical($address)
Convert a mac address from alternative to canonical form.
=head2 getSubnetAddress($address, $mask)
Returns the subnet address for IPv4.
=head2 getSubnetAddressIPv6($address, $mask)
Returns the subnet address for IPv6.
=head2 getNetworkMask($prefix)
Returns the network mask for IPv4.
=head2 getNetworkMaskIPv6($prefix)
Returns the network mask for IPv6.
=head2 resolve($host, $logger)
Returns a list of addresses, as Net::IP objects, for the given host name, as a
string.
=head2 compile($spec, $logger)
Returns a list of addresses, as Net::IP objects, for the given IP address or
host name, as a string.
=head2 isPartOf($address, $addresses, $logger)
Returns true if the given address, as a string, is part of any address from the
given list, as Net::IP objects.
|