File Coverage

lib/Rex/Hardware/Network/FreeBSD.pm
Criterion Covered Total %
statement 14 92 15.2
branch 0 42 0.0
condition 0 3 0.0
subroutine 5 10 50.0
pod 0 5 0.0
total 19 152 12.5


line stmt bran cond sub pod time code
1             #
2             # (c) Jan Gehring
3             #
4              
5             package Rex::Hardware::Network::FreeBSD;
6              
7 1     1   13 use v5.12.5;
  1         4  
8 1     1   5 use warnings;
  1         11  
  1         38  
9              
10             our $VERSION = '1.14.3'; # VERSION
11              
12 1     1   6 use Rex::Logger;
  1         24  
  1         24  
13 1     1   48 use Rex::Helper::Run;
  1         8  
  1         53  
14 1     1   5 use Rex::Helper::Array;
  1         2  
  1         1247  
15              
16             sub get_network_devices {
17              
18 0     0 0   my @device_list = map { /^([a-z0-9]+)\:/i } i_run "ifconfig -a";
  0            
19              
20 0           @device_list = array_uniq(@device_list);
21 0           return \@device_list;
22              
23             }
24              
25             sub get_network_configuration {
26              
27 0     0 0   my $devices = get_network_devices();
28              
29 0           my $device_info = {};
30              
31 0           for my $dev ( @{$devices} ) {
  0            
32              
33 0           my $ifconfig = i_run("ifconfig $dev");
34              
35 0 0         $device_info->{$dev} = {
36             ip => [ ( $ifconfig =~ m/inet (\d+\.\d+\.\d+\.\d+)/ ) ]->[0],
37             netmask => $ifconfig =~ m/(?:netmask 0x|netmask )([a-f0-9]+)/
38             ? sprintf( "%d.%d.%d.%d", unpack "C4", pack "H*", $1 )
39             : undef,
40             broadcast => [ ( $ifconfig =~ m/broadcast (\d+\.\d+\.\d+\.\d+)/ ) ]->[0],
41             mac => [
42             ( $ifconfig =~ m/(ether|address:|lladdr) (..?:..?:..?:..?:..?:..?)/ )
43             ]->[1],
44             is_bridge => 0,
45             };
46              
47             }
48              
49 0           return $device_info;
50              
51             }
52              
53             sub route {
54              
55 0     0 0   my @route = i_run "netstat -nr", fail_ok => 1;
56 0           my @ret;
57 0 0         if ( $? != 0 ) {
58 0           die("Error running netstat");
59             }
60              
61 0           my ( $in_v6, $in_v4 );
62 0           for my $route_entry (@route) {
63 0 0         if ( $route_entry eq "Internet:" ) {
64 0           $in_v4 = 1;
65 0           next;
66             }
67              
68 0 0         if ( $route_entry eq "Internet6:" ) {
69 0           $in_v6 = 1;
70 0           $in_v4 = 0;
71 0           next;
72             }
73              
74 0 0         if ( $route_entry =~ m/^$/ ) {
75 0           $in_v6 = 0;
76 0           $in_v4 = 0;
77 0           next;
78             }
79              
80 0 0         if ( $route_entry =~ m/^Destination/ ) {
81 0           next;
82             }
83              
84 0 0         if ($in_v4) {
85 0           my ( $dest, $gw, $flags, $refs, $use, $netif, $expire ) =
86             split( /\s+/, $route_entry, 7 );
87 0           push(
88             @ret,
89             {
90             destination => $dest,
91             gateway => $gw,
92             flags => $flags,
93             iface => $netif,
94             refs => $refs,
95             use => $use,
96             expire => $expire,
97             }
98             );
99              
100 0           next;
101             }
102              
103 0 0         if ($in_v6) {
104 0           my ( $dest, $gw, $flags, $netif, $expire ) =
105             split( /\s+/, $route_entry, 5 );
106 0           push(
107             @ret,
108             {
109             destination => $dest,
110             gateway => $gw,
111             flags => $flags,
112             iface => $netif,
113             expire => $expire,
114             }
115             );
116             }
117              
118             }
119              
120 0           return @ret;
121              
122             }
123              
124             sub default_gateway {
125              
126 0     0 0   my ( $class, $new_default_gw ) = @_;
127              
128 0 0         if ($new_default_gw) {
129 0 0         if ( default_gateway() ) {
130 0           i_run "route del default", fail_ok => 1;
131 0 0         if ( $? != 0 ) {
132 0           die("Error running route del default");
133             }
134             }
135              
136 0           i_run "route add default $new_default_gw", fail_ok => 1;
137 0 0         if ( $? != 0 ) {
138 0           die("Error route add default");
139             }
140              
141             }
142             else {
143 0           my @route = route();
144              
145             my ($default_route) = grep {
146 0           $_->{"flags"} =~ m/UG/
147             && ( $_->{"destination"} eq "0.0.0.0"
148 0 0 0       || $_->{"destination"} eq "default" )
149             } @route;
150 0 0         return $default_route->{"gateway"} if $default_route;
151             }
152             }
153              
154             sub netstat {
155              
156 0     0 0   my @ret;
157 0           my @netstat = i_run "netstat -na", fail_ok => 1;
158              
159 0 0         if ( $? != 0 ) {
160 0           die("Error running netstat");
161             }
162              
163 0           shift @netstat;
164              
165 0           my ( $in_inet, $in_unix ) = ( 0, 0 );
166              
167 0           for my $line (@netstat) {
168 0 0         if ( $line =~ m/^Proto\s*Recv/ ) {
169 0           $in_inet = 1;
170 0           next;
171             }
172              
173 0 0         if ( $line =~ m/^Active UNIX/ ) {
174 0           $in_inet = 0;
175 0           $in_unix = 1;
176 0           next;
177             }
178              
179 0 0         if ( $line =~ m/^Address\s*Type/ ) {
180 0           next;
181             }
182              
183 0 0         if ($in_inet) {
184 0           my ( $proto, $recvq, $sendq, $local_addr, $foreign_addr, $state ) =
185             split( /\s+/, $line, 6 );
186 0 0         if ( $proto eq "tcp4" ) { $proto = "tcp"; }
  0            
187             push(
188 0           @ret,
189             {
190             proto => $proto,
191             recvq => $recvq,
192             sendq => $sendq,
193             local_addr => $local_addr,
194             foreign_addr => $foreign_addr,
195             state => $state,
196             }
197             );
198 0           next;
199             }
200              
201 0 0         if ($in_unix) {
202             my (
203 0           $address, $type, $recvq, $sendq, $inode,
204             $conn, $refs, $nextref, $addr
205             ) = split( /\s+/, $line, 9 );
206 0           push(
207             @ret,
208             {
209             proto => "unix",
210             address => $address,
211             refcnt => $refs,
212             type => $type,
213             inode => $inode,
214             path => $addr,
215             recvq => $recvq,
216             sendq => $sendq,
217             conn => $conn,
218             nextref => $nextref,
219             }
220             );
221              
222 0           next;
223             }
224             }
225              
226 0           return @ret;
227              
228             }
229              
230             1;