From: Wolfgang Bumiller Date: Fri, 3 Jun 2016 09:09:23 +0000 (+0200) Subject: move Network::get_active_interfaces to ProcFSTools X-Git-Url: https://git.proxmox.com/?p=pve-common.git;a=commitdiff_plain;h=f0d1b04fdad02d1a7348f1396d20003224690055 move Network::get_active_interfaces to ProcFSTools This avoids a circular dependency between PVE::INotify and PVE::Network. Also renamed to get_active_network_interfaces since the package name now doesn't hint at this anymore. --- diff --git a/src/PVE/INotify.pm b/src/PVE/INotify.pm index 8cc00f8..7fb3490 100644 --- a/src/PVE/INotify.pm +++ b/src/PVE/INotify.pm @@ -14,6 +14,7 @@ use Fcntl qw(:DEFAULT :flock); use PVE::SafeSyslog; use PVE::Exception qw(raise_param_exc); use PVE::Tools; +use PVE::ProcFSTools; use Storable qw(dclone); use Linux::Inotify2; use base 'Exporter'; @@ -785,7 +786,7 @@ my $extract_ovs_option = sub { sub read_etc_network_interfaces { my ($filename, $fh) = @_; my $proc_net_dev = IO::File->new('/proc/net/dev', 'r'); - my $active = PVE::Network::get_active_interfaces(); + my $active = PVE::ProcFSTools::get_active_network_interfaces(); return __read_etc_network_interfaces($fh, $proc_net_dev, $active); } diff --git a/src/PVE/Network.pm b/src/PVE/Network.pm index ce4305a..63476d3 100644 --- a/src/PVE/Network.pm +++ b/src/PVE/Network.pm @@ -11,12 +11,6 @@ use POSIX qw(ECONNREFUSED); use Net::IP; -use Socket qw(IPPROTO_IP); - -use constant IFF_UP => 1; -use constant IFNAMSIZ => 16; -use constant SIOCGIFFLAGS => 0x8913; - # host network related utility functions our $ipv4_reverse_mask = [ @@ -537,36 +531,4 @@ sub is_ip_in_cidr { return $cidr_obj->overlaps($ip_obj) == $Net::IP::IP_B_IN_A_OVERLAP; } -# struct ifreq { // FOR SIOCGIFFLAGS: -# char ifrn_name[IFNAMSIZ] -# short ifru_flags -# }; -my $STRUCT_IFREQ_SIOCGIFFLAGS = 'Z' . IFNAMSIZ . 's1'; -sub get_active_interfaces { - # Use the interface name list from /proc/net/dev - open my $fh, '<', '/proc/net/dev' - or die "failed to open /proc/net/dev: $!\n"; - # And filter by IFF_UP flag fetched via a PF_INET6 socket ioctl: - my $sock; - socket($sock, PF_INET6, SOCK_DGRAM, &IPPROTO_IP) - or socket($sock, PF_INET, SOCK_DGRAM, &IPPROTO_IP) - or return []; - - my $ifaces = []; - while(defined(my $line = <$fh>)) { - next if $line !~ /^\s*([^:\s]+):/; - my $ifname = $1; - my $ifreq = pack($STRUCT_IFREQ_SIOCGIFFLAGS, $ifname, 0); - if (!defined(ioctl($sock, SIOCGIFFLAGS, $ifreq))) { - warn "failed to get interface flags for: $ifname\n"; - next; - } - my ($name, $flags) = unpack($STRUCT_IFREQ_SIOCGIFFLAGS, $ifreq); - push @$ifaces, $ifname if ($flags & IFF_UP); - } - close $fh; - close $sock; - return $ifaces; -} - 1; diff --git a/src/PVE/ProcFSTools.pm b/src/PVE/ProcFSTools.pm index eadb4e6..05fd744 100644 --- a/src/PVE/ProcFSTools.pm +++ b/src/PVE/ProcFSTools.pm @@ -8,6 +8,12 @@ use IO::File; use PVE::Tools; use Cwd qw(); +use Socket qw(PF_INET PF_INET6 SOCK_DGRAM IPPROTO_IP); + +use constant IFF_UP => 1; +use constant IFNAMSIZ => 16; +use constant SIOCGIFFLAGS => 0x8913; + my $clock_ticks = POSIX::sysconf(&POSIX::_SC_CLK_TCK); my $cpuinfo; @@ -374,4 +380,36 @@ sub upid_wait { } } +# struct ifreq { // FOR SIOCGIFFLAGS: +# char ifrn_name[IFNAMSIZ] +# short ifru_flags +# }; +my $STRUCT_IFREQ_SIOCGIFFLAGS = 'Z' . IFNAMSIZ . 's1'; +sub get_active_network_interfaces { + # Use the interface name list from /proc/net/dev + open my $fh, '<', '/proc/net/dev' + or die "failed to open /proc/net/dev: $!\n"; + # And filter by IFF_UP flag fetched via a PF_INET6 socket ioctl: + my $sock; + socket($sock, PF_INET6, SOCK_DGRAM, &IPPROTO_IP) + or socket($sock, PF_INET, SOCK_DGRAM, &IPPROTO_IP) + or return []; + + my $ifaces = []; + while(defined(my $line = <$fh>)) { + next if $line !~ /^\s*([^:\s]+):/; + my $ifname = $1; + my $ifreq = pack($STRUCT_IFREQ_SIOCGIFFLAGS, $ifname, 0); + if (!defined(ioctl($sock, SIOCGIFFLAGS, $ifreq))) { + warn "failed to get interface flags for: $ifname\n"; + next; + } + my ($name, $flags) = unpack($STRUCT_IFREQ_SIOCGIFFLAGS, $ifreq); + push @$ifaces, $ifname if ($flags & IFF_UP); + } + close $fh; + close $sock; + return $ifaces; +} + 1;