diff options
-rw-r--r-- | lib/Vyatta/Interface.pm | 32 | ||||
-rwxr-xr-x | lib/Vyatta/Misc.pm | 66 | ||||
-rw-r--r-- | tests/interface.pl | 6 |
3 files changed, 68 insertions, 36 deletions
diff --git a/lib/Vyatta/Interface.pm b/lib/Vyatta/Interface.pm index cce703a..ead45fd 100644 --- a/lib/Vyatta/Interface.pm +++ b/lib/Vyatta/Interface.pm @@ -22,10 +22,32 @@ package Vyatta::Interface; use strict; use warnings; use Vyatta::Config; +use base 'Exporter'; +our @EXPORT = qw(IFF_UP IFF_BROADCAST IFF_DEBUG IFF_LOOPBACK + IFF_POINTOPOINT IFF_RUNNING IFF_NOARP + IFF_PROMISC IFF_MULTICAST); + use constant { - IFF_UP => 0x1, - IFF_RUNNING => 0x40, + IFF_UP => 0x1, # interface is up + IFF_BROADCAST => 0x2, # broadcast address valid + IFF_DEBUG => 0x4, # turn on debugging + IFF_LOOPBACK => 0x8, # is a loopback net + IFF_POINTOPOINT => 0x10, # interface is has p-p link + IFF_NOTRAILERS => 0x20, # avoid use of trailers + IFF_RUNNING => 0x40, # interface RFC2863 OPER_UP + IFF_NOARP => 0x80, # no ARP protocol + IFF_PROMISC => 0x100, # receive all packets + IFF_ALLMULTI => 0x200, # receive all multicast packets + IFF_MASTER => 0x400, # master of a load balancer + IFF_SLAVE => 0x800, # slave of a load balancer + IFF_MULTICAST => 0x1000, # Supports multicast + IFF_PORTSEL => 0x2000, # can set media type + IFF_AUTOMEDIA => 0x4000, # auto media select active + IFF_DYNAMIC => 0x8000, # dialup device with changing addresses + IFF_LOWER_UP => 0x10000, # driver signals L1 up + IFF_DORMANT => 0x20000, # driver signals dormant + IFF_ECHO => 0x40000, # echo sent packets }; my %net_prefix = ( @@ -178,7 +200,7 @@ sub exists { return ( -d "/sys/class/net/$self->{name}" ); } -sub _flags { +sub flags { my $self = shift; open my $flags, '<', "/sys/class/net/$self->{name}/flags" @@ -193,7 +215,7 @@ sub _flags { # device exists and is online sub up { my $self = shift; - my $flags = $self->_flags(); + my $flags = $self->flags(); return $flags && ( $flags & IFF_UP ); } @@ -201,7 +223,7 @@ sub up { # device exists and is running (ie carrier present) sub running { my $self = shift; - my $flags = $self->_flags(); + my $flags = $self->flags(); return $flags && ( $flags & IFF_RUNNING ); } 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 { diff --git a/tests/interface.pl b/tests/interface.pl index 989d6c5..eaa295f 100644 --- a/tests/interface.pl +++ b/tests/interface.pl @@ -7,8 +7,12 @@ use strict; use warnings; use Vyatta::Interface; +use Vyatta::Misc qw(getInterfaces getInterfacesIPadresses); -foreach my $arg (@ARGV) { +my @ips = getInterfacesIPadresses('all'); +print "IP addresses = ",join(' ',@ips), "\n"; + +foreach my $arg (getInterfaces()) { print "$arg : "; my $intf = new Vyatta::Interface($arg); |