summaryrefslogtreecommitdiff
path: root/lib/Vyatta/Misc.pm
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Vyatta/Misc.pm')
-rwxr-xr-xlib/Vyatta/Misc.pm66
1 files changed, 36 insertions, 30 deletions
diff --git a/lib/Vyatta/Misc.pm b/lib/Vyatta/Misc.pm
index 04a958c..60c50b4 100755
--- a/lib/Vyatta/Misc.pm
+++ b/lib/Vyatta/Misc.pm
@@ -24,8 +24,9 @@
package Vyatta::Misc;
require Exporter;
@ISA = qw(Exporter);
-@EXPORT = qw(get_sysfs_value getNetAddIP isIpAddress is_ip_v4_or_v6 is_dhcp_enabled is_address_enabled);
-@EXPORT_OK = qw(get_sysfs_value getNetAddIP isIpAddress is_ip_v4_or_v6 getPortRuleString);
+@EXPORT = qw(get_sysfs_value getInterfaces getNetAddIP isIpAddress is_ip_v4_or_v6 is_dhcp_enabled is_address_enabled);
+@EXPORT_OK = qw(get_sysfs_value getNetAddIP isIpAddress is_ip_v4_or_v6
+ getInterfacesIPadresses getPortRuleString);
use strict;
@@ -89,41 +90,46 @@ sub generate_dhclient_intf_files {
}
+sub getInterfaces {
+ opendir (my $sys_class, '/sys/class/net')
+ or die "can't open /sys/class/net: $!";
+ my @interfaces = grep !/^\./, readdir $sys_class;
+ closedir $sys_class;
+ return @interfaces;
+}
+
+my %type_hash = (
+ 'broadcast' => IFF_BROADCAST,
+ 'multicast' => IFF_MULTICAST,
+ 'pointtopoint' => IFF_POINTOPOINT,
+);
+
# getInterfacesIPadresses() returns IP addresses for the interface type passed to it
# possible type of interfaces : 'broadcast', 'pointopoint', 'multicast', 'all'
# the loopback IP address is never returned with any of the above parameters
sub getInterfacesIPadresses {
+ my $type = shift;
+ my $mask;
+ my @ips;
- my $interface_type = shift;
- if (!($interface_type =~ m/broadcast|pointopoint|multicast|all/)) {
- print STDERR "Invalid interface type specified to retrive IP addresses for\n";
- return; # undef
- }
- my @interfaces_on_system = `ifconfig -a | awk '\$2 ~ /Link/ {print \$1}'`;
- chomp @interfaces_on_system;
- my @intf_ips = ();
- my $intf_ips_index = 0;
- foreach my $intf_system (@interfaces_on_system) {
- if (!($intf_system eq 'lo')) {
- my $is_intf_interface_type = 0;
- if (!($interface_type eq 'all')) {
- $is_intf_interface_type =
- `ip link show $intf_system 2>/dev/null | grep -i $interface_type | wc -l`;
- } else {
- $is_intf_interface_type = 1;
- }
- if ($is_intf_interface_type > 0) {
- $intf_ips[$intf_ips_index] =
- `ip addr show $intf_system 2>/dev/null | grep inet | grep -v inet6 | awk '{print \$2}'`;
- if (!($intf_ips[$intf_ips_index] eq '')){
- $intf_ips_index++;
- }
- }
- }
+ $type or die "Interface type not defined";
+
+ if ($type ne 'all') {
+ $mask = $type_hash{$type};
+ die "Invalid type specified to retreive IP addresses for: $type";
}
- chomp @intf_ips;
- return (@intf_ips);
+ foreach my $name (getInterfaces()) {
+ my $intf = new Vyatta::Interface($name);
+ next unless $intf;
+
+ my $flags = $intf->flags();
+ next if ($flags & IFF_LOOPBACK);
+
+ my @addresses = $intf->address();
+ push @ips, @addresses;
+ }
+ return @ips;
}
sub getNetAddrIP {