From f71abadb890e3aa10c9fb75d44e7f5909414b505 Mon Sep 17 00:00:00 2001 From: Alan Somers Date: Mon, 31 Mar 2014 12:27:55 -0600 Subject: [PATCH 01/19] Fix the "rake test" target. It was failing because the different os-specific files redefine the same symbols. Therefore, they can't all be required in the same process. I split them up into three processes. Also, :test was redundant with :test_units, so I removed the latter. --- Rakefile | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/Rakefile b/Rakefile index b7aa106..7f0cf70 100644 --- a/Rakefile +++ b/Rakefile @@ -3,18 +3,33 @@ require 'rake/testtask' $VERBOSE = true -desc "Run all unit tests" +desc "Default Task" task :default => [ :test ] -desc "Run all unit tests together" +desc "Run all unit tests." +task :test => ["test:bsd", "test:linux", "test:sunos"] + +# The different os-specific files cannot be required in the same process, since +# the redefine the same symbols. So we can't test them in a single test/unit +# process. Instead, we'll split them up into the minimum possible number of +# processes, each with its own rake task. Rake::TestTask.new do |t| - t.test_files = FileList['test/unit/tc_*.rb'] + t.name = "test:bsd" + t.test_files = %w(darwin dragonflybsd freebsd netbsd openbsd osx).collect do |os| + "test/unit/tc_#{os}.rb" + end end -desc "Run the unit tests in test/" -task :test_units do - Dir.glob('test/unit/*').each do |t| - puts `/usr/bin/env ruby #{t}` +Rake::TestTask.new do |t| + t.name = "test:linux" + t.test_files = %w(linux).collect do |os| + "test/unit/tc_#{os}.rb" end end +Rake::TestTask.new do |t| + t.name = "test:sunos" + t.test_files = %w(sunos).collect do |os| + "test/unit/tc_#{os}.rb" + end +end From 3ba309e53e780edad63a9be4150aaf63a0f2b117 Mon Sep 17 00:00:00 2001 From: Alan Somers Date: Mon, 31 Mar 2014 12:31:51 -0600 Subject: [PATCH 02/19] s/Fauled/Failed --- test/unit/tc_dragonflybsd.rb | 2 +- test/unit/tc_sunos.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/unit/tc_dragonflybsd.rb b/test/unit/tc_dragonflybsd.rb index 5bc859f..3c63732 100644 --- a/test/unit/tc_dragonflybsd.rb +++ b/test/unit/tc_dragonflybsd.rb @@ -9,7 +9,7 @@ def setup def test_interface_list assert(@cfg.interfaces == ["rl0", "lo0"], - "Fauled to parse all interfaces") + "Failed to parse all interfaces") end def test_mac_parse diff --git a/test/unit/tc_sunos.rb b/test/unit/tc_sunos.rb index 711da48..e1b3666 100644 --- a/test/unit/tc_sunos.rb +++ b/test/unit/tc_sunos.rb @@ -9,7 +9,7 @@ def setup def test_interface_list assert(@@cfg.interfaces.sort == ["le1", "lo0", "bge0"].sort, - "Fauled to parse all interfaces") + "Failed to parse all interfaces") end def test_mac_parse From e1947eb2af8ab8fb8b8a92332b786547d3e638de Mon Sep 17 00:00:00 2001 From: Alan Somers Date: Mon, 31 Mar 2014 12:34:19 -0600 Subject: [PATCH 03/19] Fix dragonflybsd's test_interface_list. The list wasn't sorted. This should've been applied in change 6b2b2e8b7554b14221388971516095d0933acc2a, but wasn't due to an oversight. --- test/unit/tc_dragonflybsd.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/tc_dragonflybsd.rb b/test/unit/tc_dragonflybsd.rb index 3c63732..95d3ae9 100644 --- a/test/unit/tc_dragonflybsd.rb +++ b/test/unit/tc_dragonflybsd.rb @@ -8,7 +8,7 @@ def setup end def test_interface_list - assert(@cfg.interfaces == ["rl0", "lo0"], + assert(@cfg.interfaces.sort == ["rl0", "lo0"].sort, "Failed to parse all interfaces") end From 9d7ed132f5eb61258d38fc2b8b68457f63dd460c Mon Sep 17 00:00:00 2001 From: Alan Somers Date: Mon, 31 Mar 2014 13:32:00 -0600 Subject: [PATCH 04/19] Added a test_mtu test for FreeBSD. No existing test checked the parsing of freebsd_netstat.txt. I had to add it to the test file. I also had to make up example data for rl0. It seems that the author created freebsd.txt and freebsd_netstat.txt at different times or with different configurations. --- ifconfig_examples/freebsd_netstat.txt | 6 ++++++ test/unit/tc_freebsd.rb | 14 +++++++++++--- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/ifconfig_examples/freebsd_netstat.txt b/ifconfig_examples/freebsd_netstat.txt index 1f1add2..4171b9d 100644 --- a/ifconfig_examples/freebsd_netstat.txt +++ b/ifconfig_examples/freebsd_netstat.txt @@ -10,6 +10,12 @@ xl0 1500 fe80:1::201 fe80:1::201:2ff:f 0 - 0 0 ff02:1::2:465d:245a(refs: 1) ff02:1::1 (refs: 1) ff02:1::1:ffc6:4bea(refs: 1) +rl0 1500 00:00:21:03:08:e1 1 0 16 1 0 32 0 0 0 +rl0 1500 10.0.0 some_address 2 - 17 2 - 33 - - - +rl0 1500 fe80::200 fe80:1::200:2ff:f 0 - 0 0 - 0 - - - +rl0 1500 ff02:1::2:465d:245a(refs: 1) +rl0 1500 ff02:1::1 (refs: 1) +rl0 1500 ff02:1::1:ffc6:4bea(refs: 1) lo0 16384 574 0 118916 574 0 118916 0 0 0 lo0 16384 localhost ::1 0 - 0 0 - 0 - - - ff01::1 (refs: 1) diff --git a/test/unit/tc_freebsd.rb b/test/unit/tc_freebsd.rb index 138dbd0..4d5d96e 100644 --- a/test/unit/tc_freebsd.rb +++ b/test/unit/tc_freebsd.rb @@ -2,9 +2,11 @@ class TC_FreeBSDTest < Test::Unit::TestCase def setup - sample = IO.readlines("#{File.dirname(__FILE__)}"+ - "/../../ifconfig_examples/freebsd.txt").join - @cfg = IfconfigWrapper.new('BSD',sample).parse + ifconfig_sample = IO.readlines("#{File.dirname(__FILE__)}"+ + "/../../ifconfig_examples/freebsd.txt").join + netstat_sample = IO.readlines("#{File.dirname(__FILE__)}"+ + "/../../ifconfig_examples/freebsd_netstat.txt").join + @cfg = IfconfigWrapper.new('BSD',ifconfig_sample, netstat_sample).parse end def test_interface_list @@ -37,4 +39,10 @@ def test_attribs end + def test_mtu + assert_equal(1500, @cfg['xl0'].mtu) + assert_equal(1500, @cfg['rl0'].mtu) + assert_equal(16384, @cfg['lo0'].mtu) + end + end From 512ab926d1f071c168be02f69c8ac9882ddf3dcd Mon Sep 17 00:00:00 2001 From: Alan Somers Date: Mon, 31 Mar 2014 13:40:17 -0600 Subject: [PATCH 05/19] Fix formatting error from change 9d7ed132f5eb61258d38fc2b8b68457f63dd460c --- ifconfig_examples/freebsd_netstat.txt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ifconfig_examples/freebsd_netstat.txt b/ifconfig_examples/freebsd_netstat.txt index 4171b9d..c9cf346 100644 --- a/ifconfig_examples/freebsd_netstat.txt +++ b/ifconfig_examples/freebsd_netstat.txt @@ -13,9 +13,9 @@ xl0 1500 fe80:1::201 fe80:1::201:2ff:f 0 - 0 0 rl0 1500 00:00:21:03:08:e1 1 0 16 1 0 32 0 0 0 rl0 1500 10.0.0 some_address 2 - 17 2 - 33 - - - rl0 1500 fe80::200 fe80:1::200:2ff:f 0 - 0 0 - 0 - - - -rl0 1500 ff02:1::2:465d:245a(refs: 1) -rl0 1500 ff02:1::1 (refs: 1) -rl0 1500 ff02:1::1:ffc6:4bea(refs: 1) + ff02:1::2:465d:245a(refs: 1) + ff02:1::1 (refs: 1) + ff02:1::1:ffc6:4bea(refs: 1) lo0 16384 574 0 118916 574 0 118916 0 0 0 lo0 16384 localhost ::1 0 - 0 0 - 0 - - - ff01::1 (refs: 1) From aeeefaec44b96f59233a61f8ce96adeb363671b4 Mon Sep 17 00:00:00 2001 From: Alan Somers Date: Mon, 31 Mar 2014 14:29:38 -0600 Subject: [PATCH 06/19] Fix Rx/Tx statistics on FreeBSD In r199803 on 2009-11-25, FreeBSD added an "Idrop" column to netstat(1). All currently supported releases of FreeBSD include that column. ifconfig_examples/freebsd_netstat.txt Add an Idrop column. Remove the "Drop" column, which would only be present with "-d". Remove the "Ibytes" and "Obytes" columns, which would only be present with "-b". lib/ifconfig/bsd/ifconfig.rb Parse netstat(1)'s output's header instead of assuming fixed positions for fields. test/unit/tc_freebsd.rb Add asserts for the rx and tx packets and errors. --- ifconfig_examples/freebsd_netstat.txt | 22 +++++++++++----------- lib/ifconfig/bsd/ifconfig.rb | 18 +++++++++++++----- test/unit/tc_freebsd.rb | 5 ++++- 3 files changed, 28 insertions(+), 17 deletions(-) diff --git a/ifconfig_examples/freebsd_netstat.txt b/ifconfig_examples/freebsd_netstat.txt index c9cf346..4e444e1 100644 --- a/ifconfig_examples/freebsd_netstat.txt +++ b/ifconfig_examples/freebsd_netstat.txt @@ -1,30 +1,30 @@ -Name Mtu Network Address Ipkts Ierrs Ibytes Opkts Oerrs Obytes Coll Time Drop -xl0 1500 00:01:02:c6:4b:ea 7878752 2 404325088 5502452 0 1276760877 0 0 0 +Name Mtu Network Address Ipkts Ierrs Idrop Opkts Oerrs Coll Time +xl0 1500 00:01:02:c6:4b:ea 7878752 2 0 5502452 0 0 0 33:33:46:5d:24:5a 33:33:00:00:00:01 33:33:ff:c6:4b:ea 01:00:5e:00:00:01 -xl0 1500 192.168.0 beleriand 7865045 - 287935758 5496244 - 1199565347 - - - +xl0 1500 192.168.0 beleriand 7865045 - - 5496244 - - - ALL-SYSTEMS.MCAST.NET -xl0 1500 fe80:1::201 fe80:1::201:2ff:f 0 - 0 0 - 0 - - - +xl0 1500 fe80:1::201 fe80:1::201:2ff:f 0 - - 0 - - - ff02:1::2:465d:245a(refs: 1) ff02:1::1 (refs: 1) ff02:1::1:ffc6:4bea(refs: 1) -rl0 1500 00:00:21:03:08:e1 1 0 16 1 0 32 0 0 0 -rl0 1500 10.0.0 some_address 2 - 17 2 - 33 - - - -rl0 1500 fe80::200 fe80:1::200:2ff:f 0 - 0 0 - 0 - - - +rl0 1500 00:00:21:03:08:e1 1 0 0 1 0 0 0 +rl0 1500 10.0.0 some_address 2 - - 2 - - - +rl0 1500 fe80::200 fe80:1::200:2ff:f 0 - - 0 - - - ff02:1::2:465d:245a(refs: 1) ff02:1::1 (refs: 1) ff02:1::1:ffc6:4bea(refs: 1) -lo0 16384 574 0 118916 574 0 118916 0 0 0 -lo0 16384 localhost ::1 0 - 0 0 - 0 - - - +lo0 16384 574 0 0 574 0 0 0 +lo0 16384 localhost ::1 0 - - 0 - - - ff01::1 (refs: 1) ff02:2::1 (refs: 1) ff02:2::1:ff00:1 (refs: 2) -lo0 16384 fe80:2::1 fe80:2::1 0 - 0 0 - 0 - - - +lo0 16384 fe80:2::1 fe80:2::1 0 - - 0 - - - ff01::1 (refs: 1) ff02:2::1 (refs: 1) ff02:2::1:ff00:1 (refs: 2) -lo0 16384 your-net localhost 574 - 118916 574 - 118916 - - - +lo0 16384 your-net localhost 574 - - 574 - - - ALL-SYSTEMS.MCAST.NET diff --git a/lib/ifconfig/bsd/ifconfig.rb b/lib/ifconfig/bsd/ifconfig.rb index 11c1f74..1c0f42f 100644 --- a/lib/ifconfig/bsd/ifconfig.rb +++ b/lib/ifconfig/bsd/ifconfig.rb @@ -9,6 +9,10 @@ class Ifconfig # ifconfig = user provided ifconifg output # netstat = same, but for netstat -in # + # XXX On FreeBSD, "netstat -inb" will show byte _and_ packet statistics, but + # on NetBSD and OpenBSD, it will show byte statistics only. We use "-in" + # instead of "-inb" for cross-platform compatibility. + # @@ifcfg_cmd = "/usr/bin/env ifconfig -a" @@netstat_cmd = "/usr/bin/env netstat -in" def initialize(ifconfig=nil,netstat=nil,verbose=nil) @@ -49,17 +53,21 @@ def initialize(ifconfig=nil,netstat=nil,verbose=nil) # def parse_activity(iface) mtu = rxpackets = rxerrors = txpackets = txerrors = 0 + fields = {} @netstat.split("\n").each { |line| line.strip! - if line =~ /^#{iface}/ + if line =~/^Name/ + headers = line.split(" ") + headers.each_index {|i| fields[headers[i]] = i} + elsif line =~ /^#{iface}/ next if line.split[2] =~ /\/ puts "matched line for "+iface if @verbose toks = line.split mtu = toks[1] - rxpackets += toks[4].to_i - rxerrors += toks[5].to_i - txpackets += toks[6].to_i - txerrors += toks[7].to_i + rxpackets += toks[fields["Ipkts"]].to_i + rxerrors += toks[fields["Ierrs"]].to_i + txpackets += toks[fields["Opkts"]].to_i + txerrors += toks[fields["Oerrs"]].to_i @ifaces[iface].mtu = mtu.to_i @ifaces[iface].rx = { 'packets' => rxpackets, 'errors' => rxerrors } diff --git a/test/unit/tc_freebsd.rb b/test/unit/tc_freebsd.rb index 4d5d96e..3a9213d 100644 --- a/test/unit/tc_freebsd.rb +++ b/test/unit/tc_freebsd.rb @@ -36,7 +36,10 @@ def test_addr_types def test_attribs assert(@cfg['rl0'].rx['bytes'].class == Fixnum || NilClass && @cfg['rl0'].tx['bytes'].class == Fixnum || NilClass, "Wrong class") - + assert_equal(7865045, @cfg['xl0'].rx['packets']) + assert_equal(0, @cfg['xl0'].rx['errors']) + assert_equal(5496244, @cfg['xl0'].tx['packets']) + assert_equal(0, @cfg['xl0'].tx['errors']) end def test_mtu From 61bd9070d080a4b2045807a013dc6c5d3f242e5b Mon Sep 17 00:00:00 2001 From: Alan Somers Date: Mon, 31 Mar 2014 14:48:54 -0600 Subject: [PATCH 07/19] Update the FreeBSD example files with output from FreeBSD 9.2. The new files don't include aliases or any IPv6 address other than link-local, but they do include fibs and laggs. Also, the old file appears to have been created without the "-n" option. --- ifconfig_examples/freebsd.txt | 62 +++++++++++++++++++-------- ifconfig_examples/freebsd_netstat.txt | 43 ++++++------------- test/unit/tc_freebsd.rb | 37 ++++++++-------- 3 files changed, 77 insertions(+), 65 deletions(-) diff --git a/ifconfig_examples/freebsd.txt b/ifconfig_examples/freebsd.txt index b3fd865..4d3b833 100644 --- a/ifconfig_examples/freebsd.txt +++ b/ifconfig_examples/freebsd.txt @@ -1,17 +1,45 @@ -xl0: flags=8843 mtu 1500 - options=3 - inet 192.168.0.2 netmask 0xffffff00 broadcast 192.168.0.255 - inet6 fe80::201:2ff:fec6:4bea%xl0 prefixlen 64 scopeid 0x1 - ether 00:01:02:c6:4b:ea - media: Ethernet autoselect (100baseTX ) - status: active -rl0: flags=8943 mtu 1500 - inet 10.0.0.10 netmask 0xffffff00 broadcast 10.0.0.255 - inet6 fe80::200:21ff:fe03:8e1%rl0 prefixlen 64 scopeid 0x1 - ether 00:00:21:03:08:e1 - media: Ethernet autoselect (100baseTX ) - status: active -lo0: flags=8049 mtu 16384 - inet6 ::1 prefixlen 128 - inet6 fe80::1%lo0 prefixlen 64 scopeid 0x2 - inet 127.0.0.1 netmask 0xff000000 +igb0: flags=8843 metric 0 mtu 1500 + options=401bb + ether 00:11:22:33:44:55 + inet6 fe80::225:90ff:fe95:c98c%igb0 prefixlen 64 scopeid 0x1 + inet 10.1.3.176 netmask 0xfffff000 broadcast 10.1.15.255 + nd6 options=29 + media: Ethernet autoselect (1000baseT ) + status: active +igb1: flags=8843 metric 0 mtu 1500 + options=401bb + ether 00:11:22:33:44:66 + nd6 options=29 + media: Ethernet autoselect (1000baseT ) + status: active + fib: 1 +igb2: flags=8843 metric 0 mtu 1500 + options=401bb + ether 00:11:22:33:44:66 + nd6 options=29 + media: Ethernet autoselect (1000baseT ) + status: active +igb3: flags=8843 metric 0 mtu 1500 + options=401bb + ether 00:11:22:33:44:66 + nd6 options=29 + media: Ethernet autoselect (1000baseT ) + status: active +lo0: flags=8049 metric 0 mtu 16384 + options=600003 + inet6 ::1 prefixlen 128 + inet6 fe80::1%lo0 prefixlen 64 scopeid 0x7 + inet 127.0.0.1 netmask 0xff000000 + nd6 options=21 +lagg0: flags=8843 metric 0 mtu 1500 + options=401bb + ether 00:11:22:33:44:66 + inet 10.1.19.176 netmask 0xfffff000 broadcast 10.1.31.255 + nd6 options=29 + media: Ethernet autoselect + status: active + fib: 1 + laggproto lacp lagghash l2,l3,l4 + laggport: igb3 flags=1c + laggport: igb2 flags=1c + laggport: igb1 flags=1c diff --git a/ifconfig_examples/freebsd_netstat.txt b/ifconfig_examples/freebsd_netstat.txt index 4e444e1..576e058 100644 --- a/ifconfig_examples/freebsd_netstat.txt +++ b/ifconfig_examples/freebsd_netstat.txt @@ -1,30 +1,13 @@ -Name Mtu Network Address Ipkts Ierrs Idrop Opkts Oerrs Coll Time -xl0 1500 00:01:02:c6:4b:ea 7878752 2 0 5502452 0 0 0 - 33:33:46:5d:24:5a - 33:33:00:00:00:01 - 33:33:ff:c6:4b:ea - 01:00:5e:00:00:01 -xl0 1500 192.168.0 beleriand 7865045 - - 5496244 - - - - ALL-SYSTEMS.MCAST.NET -xl0 1500 fe80:1::201 fe80:1::201:2ff:f 0 - - 0 - - - - ff02:1::2:465d:245a(refs: 1) - ff02:1::1 (refs: 1) - ff02:1::1:ffc6:4bea(refs: 1) -rl0 1500 00:00:21:03:08:e1 1 0 0 1 0 0 0 -rl0 1500 10.0.0 some_address 2 - - 2 - - - -rl0 1500 fe80::200 fe80:1::200:2ff:f 0 - - 0 - - - - ff02:1::2:465d:245a(refs: 1) - ff02:1::1 (refs: 1) - ff02:1::1:ffc6:4bea(refs: 1) -lo0 16384 574 0 0 574 0 0 0 -lo0 16384 localhost ::1 0 - - 0 - - - - ff01::1 (refs: 1) - ff02:2::1 (refs: 1) - ff02:2::1:ff00:1 (refs: 2) -lo0 16384 fe80:2::1 fe80:2::1 0 - - 0 - - - - ff01::1 (refs: 1) - ff02:2::1 (refs: 1) - ff02:2::1:ff00:1 (refs: 2) -lo0 16384 your-net localhost 574 - - 574 - - - - ALL-SYSTEMS.MCAST.NET - +Name Mtu Network Address Ipkts Ierrs Idrop Opkts Oerrs Coll +igb0 1500 00:11:22:33:44:55 5748766 0 0 1591506 0 0 +igb0 1500 fe80::225:90f fe80::225:90ff:fe 0 - - 2 - - +igb0 1500 10.1.0.0/20 10.1.3.176 1683211 - - 1480094 - - +igb1 1500 00:11:22:33:44:66 6369236 0 0 36130 0 0 +igb2 1500 00:11:22:33:44:66 57591 0 0 35355 0 0 +igb3 1500 00:11:22:33:44:66 53758 0 0 37262 0 0 +lo0 16384 7486204 0 0 7486201 0 0 +lo0 16384 ::1/128 ::1 6628 - - 6628 - - +lo0 16384 fe80::1%lo0/6 fe80::1 0 - - 0 - - +lo0 16384 127.0.0.0/8 127.0.0.1 7479584 - - 7479582 - - +lagg0 1500 00:11:22:33:44:66 6294267 0 0 11768 2 0 +lagg0 1500 10.1.16.0/20 10.1.19.176 334619 - - 9436 - - diff --git a/test/unit/tc_freebsd.rb b/test/unit/tc_freebsd.rb index 3a9213d..74c1e99 100644 --- a/test/unit/tc_freebsd.rb +++ b/test/unit/tc_freebsd.rb @@ -10,41 +10,42 @@ def setup end def test_interface_list - assert(@cfg.interfaces.sort == ["rl0", "lo0", "xl0"].sort, + assert(@cfg.interfaces.sort == ["igb0", "igb1", "igb2", "igb3", + "lo0", "lagg0"].sort, "Failed to parse all interfaces") end def test_mac_parse - assert(@cfg['rl0'].mac == "00:00:21:03:08:e1", - "Failed to parse MAC address: "+@cfg['rl0'].mac) + assert(@cfg['igb0'].mac == "00:11:22:33:44:55", + "Failed to parse MAC address: "+@cfg['igb0'].mac) end def test_flags - assert(@cfg['rl0'].flags.include?('BROADCAST') && - @cfg['rl0'].flags.include?('RUNNING') && - @cfg['rl0'].flags.include?('MULTICAST') && - @cfg['rl0'].up?, - "FLAG Parsing failed: #{@cfg['rl0'].flags}") + assert(@cfg['igb0'].flags.include?('BROADCAST') && + @cfg['igb0'].flags.include?('RUNNING') && + @cfg['igb0'].flags.include?('MULTICAST') && + @cfg['igb0'].up?, + "FLAG Parsing failed: #{@cfg['igb0'].flags}") end def test_addr_types - assert(@cfg['rl0'].addr_types.include?('inet') && - @cfg['rl0'].addr_types.include?('inet6'), + assert(@cfg['igb0'].addr_types.include?('inet') && + @cfg['igb0'].addr_types.include?('inet6'), "Failed to parse all address types") end def test_attribs - assert(@cfg['rl0'].rx['bytes'].class == Fixnum || NilClass && - @cfg['rl0'].tx['bytes'].class == Fixnum || NilClass, "Wrong class") - assert_equal(7865045, @cfg['xl0'].rx['packets']) - assert_equal(0, @cfg['xl0'].rx['errors']) - assert_equal(5496244, @cfg['xl0'].tx['packets']) - assert_equal(0, @cfg['xl0'].tx['errors']) + assert(@cfg['igb0'].rx['bytes'].class == Fixnum || NilClass && + @cfg['igb0'].tx['bytes'].class == Fixnum || NilClass, "Wrong class") + assert_equal(1683211, @cfg['igb0'].rx['packets']) + assert_equal(0, @cfg['igb0'].rx['errors']) + assert_equal(1480096, @cfg['igb0'].tx['packets']) + assert_equal(0, @cfg['igb0'].tx['errors']) end def test_mtu - assert_equal(1500, @cfg['xl0'].mtu) - assert_equal(1500, @cfg['rl0'].mtu) + assert_equal(1500, @cfg['igb0'].mtu) + assert_equal(1500, @cfg['lagg0'].mtu) assert_equal(16384, @cfg['lo0'].mtu) end From ea8bc59bb8b1580b32ef0a4a24765bec1419bec3 Mon Sep 17 00:00:00 2001 From: Alan Somers Date: Mon, 31 Mar 2014 15:00:08 -0600 Subject: [PATCH 08/19] Add support for FreeBSD fibs. --- lib/ifconfig/bsd/interface_types.rb | 7 +++++++ lib/ifconfig/common/interface_types.rb | 4 +++- test/unit/tc_freebsd.rb | 6 ++++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/ifconfig/bsd/interface_types.rb b/lib/ifconfig/bsd/interface_types.rb index b21b1bb..e5f7a6c 100644 --- a/lib/ifconfig/bsd/interface_types.rb +++ b/lib/ifconfig/bsd/interface_types.rb @@ -17,10 +17,17 @@ def parse_ifconfig add_network(line) when /flags\=/i parse_flags(line) + when /\s*fib:/ + parse_fib(line) end } end + # parses the "fib: 1" line + def parse_fib(line) + @fib = line.split()[1].to_i + end + # parses the "UP LOOPBACK RUNNING MTU:3924 Metric:1" line # def parse_flags(line) diff --git a/lib/ifconfig/common/interface_types.rb b/lib/ifconfig/common/interface_types.rb index 7da3ede..e789414 100644 --- a/lib/ifconfig/common/interface_types.rb +++ b/lib/ifconfig/common/interface_types.rb @@ -15,9 +15,10 @@ def initialize(name, ifacetxt) @mtu = nil @metric = nil @rx = @tx = {} + @fib = 0 parse_ifconfig end - attr_reader :status, :name, :flags, :mtu + attr_reader :status, :name, :fib, :flags, :mtu attr_accessor :tx, :rx # take array and turn each two entries into @@ -105,6 +106,7 @@ def to_s s += " MTU: #{@mtu}\n" s += " Metric: #{@metric}\n" s += " Flags: #{@flags.join(',')}\n" + s += " Fib: #{@fib}\n" if @fib != 0 s += " Status: UP" if self.status return s end diff --git a/test/unit/tc_freebsd.rb b/test/unit/tc_freebsd.rb index 74c1e99..79b5f68 100644 --- a/test/unit/tc_freebsd.rb +++ b/test/unit/tc_freebsd.rb @@ -43,6 +43,12 @@ def test_attribs assert_equal(0, @cfg['igb0'].tx['errors']) end + def test_fib + assert_equal(0, @cfg['lo0'].fib) + assert_equal(0, @cfg['igb0'].fib) + assert_equal(1, @cfg['lagg0'].fib) + end + def test_mtu assert_equal(1500, @cfg['igb0'].mtu) assert_equal(1500, @cfg['lagg0'].mtu) From cd09cb00ebd954859e0aa3bd335d998f0e18f4ab Mon Sep 17 00:00:00 2001 From: Alan Somers Date: Mon, 31 Mar 2014 16:21:23 -0600 Subject: [PATCH 09/19] Never invoke IO::popen during unit tests. Previously, the unit tests were all invoking "netstat -in" because its output wasn't injected. I fixed this by injecting it in all tests. I also had to change some function signatures. test/unit/tc_netbsd.rb ifconfig_examples/netbsd_netstat.txt Create netbsd netstat output based on the behavior of NetBSD 6.0 amd64 ifconfig_examples/sunos_netstat.txt test/unit/tc_sunos.rb Create sunos netstat output based on the behavior of Illumian 1.0 (a5) i386. test/unit/tc_dragonflybsd.rb Use the existing netstat output for dragonfly bsd test/unit/tc_darwin.rb test/unit/tc_openbsd.rb test/unit/tc_osx.rb Use the empty string as netstat output because I don't have access to these platforms. The existing testcases don't actually check the results of parsing netstat anyway. lib/ifconfig/bsd/interface_types.rb lib/ifconfig/common/interface_types.rb lib/ifconfig/linux/interface_types.rb lib/ifconfig/sunos/ifconfig.rb lib/ifconfig/sunos/interface_types.rb Add a "netstattxt" parameter to parse_ifconfig to allow netstat's output to be injected on sunos. --- ifconfig_examples/netbsd_netstat.txt | 8 ++++++ ifconfig_examples/sunos_netstat.txt | 8 ++++++ lib/ifconfig/bsd/interface_types.rb | 2 +- lib/ifconfig/common/interface_types.rb | 8 +++--- lib/ifconfig/linux/interface_types.rb | 2 +- lib/ifconfig/sunos/ifconfig.rb | 6 ++--- lib/ifconfig/sunos/interface_types.rb | 21 +++++++++------- test/unit/tc_darwin.rb | 3 ++- test/unit/tc_dragonflybsd.rb | 4 ++- test/unit/tc_netbsd.rb | 4 ++- test/unit/tc_openbsd.rb | 3 ++- test/unit/tc_osx.rb | 3 ++- test/unit/tc_sunos.rb | 34 ++++++++++++++------------ 13 files changed, 67 insertions(+), 39 deletions(-) create mode 100644 ifconfig_examples/netbsd_netstat.txt create mode 100644 ifconfig_examples/sunos_netstat.txt diff --git a/ifconfig_examples/netbsd_netstat.txt b/ifconfig_examples/netbsd_netstat.txt new file mode 100644 index 0000000..995eb7e --- /dev/null +++ b/ifconfig_examples/netbsd_netstat.txt @@ -0,0 +1,8 @@ +Name Mtu Network Address Ipkts Ierrs Opkts Oerrs Colls +cs0 1500 08:00:2b:81:62:ca 42320 0 131 0 0 +cs0 1500 fe80::/64 fe80::a00:2bff:fe 42320 0 131 0 0 +cs0 1500 192.168.0/24 192.168.6.156 42320 0 131 0 0 +lo0 33648 0 0 0 0 0 +lo0 33648 127/8 127.0.0.1 0 0 0 0 0 +lo0 33648 ::1/128 ::1 0 0 0 0 0 +lo0 33648 fe80::/64 fe80::1 0 0 0 0 0 diff --git a/ifconfig_examples/sunos_netstat.txt b/ifconfig_examples/sunos_netstat.txt new file mode 100644 index 0000000..9a16d45 --- /dev/null +++ b/ifconfig_examples/sunos_netstat.txt @@ -0,0 +1,8 @@ +Name Mtu Net/Dest Address Ipkts Ierrs Opkts Oerrs Collis Queue +lo0 8232 127.0.0.0 127.0.0.1 162 0 162 0 0 0 +bge0 1500 10.32.4.0 10.32.4.137 3460 0 114 0 0 0 +le1 1500 172.16.0.0 172.16.254.99 1000 0 500 0 0 0 + +Name Mtu Net/Dest Address Ipkts Ierrs Opkts Oerrs Collis +lo0 8252 ::1 ::1 162 0 162 0 0 +bge0 1500 fe80:: fe80::a00:20ff:fe72:9724 2000 0 1100 0 0 diff --git a/lib/ifconfig/bsd/interface_types.rb b/lib/ifconfig/bsd/interface_types.rb index e5f7a6c..f28e5ff 100644 --- a/lib/ifconfig/bsd/interface_types.rb +++ b/lib/ifconfig/bsd/interface_types.rb @@ -10,7 +10,7 @@ class NetworkAdapter # iterate line by line and dispatch to helper functions # for lines that match a pattern # - def parse_ifconfig + def parse_ifconfig(netstattxt=nil) @ifconfig.split("\n").each { |line| case line when /^\s+#{@protos}/ diff --git a/lib/ifconfig/common/interface_types.rb b/lib/ifconfig/common/interface_types.rb index e789414..0716e9a 100644 --- a/lib/ifconfig/common/interface_types.rb +++ b/lib/ifconfig/common/interface_types.rb @@ -1,7 +1,7 @@ # $Id: interface_types.rb,v 1.1.1.1 2005/07/02 19:10:58 hobe Exp $ # class NetworkAdapter - def initialize(name, ifacetxt) + def initialize(name, ifacetxt, netstattxt=nil) @name = name @ifconfig = ifacetxt @status = false @@ -16,7 +16,7 @@ def initialize(name, ifacetxt) @metric = nil @rx = @tx = {} @fib = 0 - parse_ifconfig + parse_ifconfig(netstattxt) end attr_reader :status, :name, :fib, :flags, :mtu attr_accessor :tx, :rx @@ -116,8 +116,8 @@ def to_s # function to get the mac address # class EthernetAdapter < NetworkAdapter - def initialize(name,ifconfigtxt) - super(name,ifconfigtxt) + def initialize(name,ifconfigtxt,netstattxt=nil) + super(name,ifconfigtxt,netstattxt) @mac = set_mac end diff --git a/lib/ifconfig/linux/interface_types.rb b/lib/ifconfig/linux/interface_types.rb index ac8e6e7..cbd51e2 100644 --- a/lib/ifconfig/linux/interface_types.rb +++ b/lib/ifconfig/linux/interface_types.rb @@ -32,7 +32,7 @@ def parse_activity(line) # iterate line by line and dispatch to helper functions # for lines that match a pattern # - def parse_ifconfig + def parse_ifconfig(netstattxt=nil) @ifconfig.split("\n").each { |line| case line when /^\s+#{@protos}/ diff --git a/lib/ifconfig/sunos/ifconfig.rb b/lib/ifconfig/sunos/ifconfig.rb index b205f52..36f59bb 100644 --- a/lib/ifconfig/sunos/ifconfig.rb +++ b/lib/ifconfig/sunos/ifconfig.rb @@ -8,7 +8,7 @@ class Ifconfig # Can manually specify the platform (should be output of the 'uname' command) # and the ifconfig input # - def initialize(input=nil,verbose=nil) + def initialize(input=nil,netstat=nil,verbose=nil) if input.nil? cmd = IO.popen('which ifconfig'){ |f| f.readlines[0] } exit unless !cmd.nil? @@ -27,9 +27,9 @@ def initialize(input=nil,verbose=nil) iface_name = get_iface_name(iface) case iface when /^lo\d\:/im - @ifaces[iface_name] = LoopbackInterface.new(iface_name,iface) + @ifaces[iface_name] = LoopbackInterface.new(iface_name,iface,netstat) when /\s+ether\s+/im - @ifaces[iface_name] = EthernetAdapter.new(iface_name,iface) + @ifaces[iface_name] = EthernetAdapter.new(iface_name,iface,netstat) else puts "Unknown Adapter Type: #{iface}" if @verbose end diff --git a/lib/ifconfig/sunos/interface_types.rb b/lib/ifconfig/sunos/interface_types.rb index 7644e3d..19f1245 100644 --- a/lib/ifconfig/sunos/interface_types.rb +++ b/lib/ifconfig/sunos/interface_types.rb @@ -7,23 +7,26 @@ class NetworkAdapter # Parse activity on interface # - def parse_activity + def parse_activity(netstattxt) #imaptest1# netstat -in #Name Mtu Net/Dest Address Ipkts Ierrs Opkts Oerrs Collis Queue #lo0 8232 0.0.0.0 0.0.0.4 267824 0 267824 0 0 0 #bge0 1500 10.0.0.0 10.32.4.138 10935939 0 7741167 0 0 0 - cmd = "netstat -in | grep #{@name}" - line = IO.popen(cmd).gets - return if line.nil? - name,@mtu,dfgw,addr,@rx['packets'],@rx['errors'], - @tx['packets'],@tx['errors'],collisions,queue = line.split - @mtu = @mtu.to_i + cmd = "netstat -in" + txt = netstattxt || IO.popen(cmd) + txt.each_line do |line| + next if line.nil? + next unless line =~ /#{@name}/ + name,@mtu,dfgw,addr,@rx['packets'],@rx['errors'], + @tx['packets'],@tx['errors'],collisions,queue = line.split + @mtu = @mtu.to_i + end end # iterate line by line and dispatch to helper functions # for lines that match a pattern # - def parse_ifconfig + def parse_ifconfig(netstattxt=nil) @ifconfig.split("\n").each { |line| case line when /^\s+#{@protos}/ @@ -32,7 +35,7 @@ def parse_ifconfig parse_flags(line) end } - parse_activity + parse_activity(netstattxt) end # parses the "UP LOOPBACK RUNNING MTU:3924 Metric:1" line diff --git a/test/unit/tc_darwin.rb b/test/unit/tc_darwin.rb index 46c9ba8..bc449ec 100644 --- a/test/unit/tc_darwin.rb +++ b/test/unit/tc_darwin.rb @@ -4,7 +4,8 @@ class TC_DarwinTest < Test::Unit::TestCase def setup sample = IO.readlines("#{File.dirname(__FILE__)}"+ "/../../ifconfig_examples/darwin.txt").join - @cfg = IfconfigWrapper.new('BSD',sample).parse + netstat_sample = "" + @cfg = IfconfigWrapper.new('BSD',sample, netstat_sample).parse end def test_interface_list diff --git a/test/unit/tc_dragonflybsd.rb b/test/unit/tc_dragonflybsd.rb index 95d3ae9..9a685cf 100644 --- a/test/unit/tc_dragonflybsd.rb +++ b/test/unit/tc_dragonflybsd.rb @@ -4,7 +4,9 @@ class TC_DragonFlyBSDTest < Test::Unit::TestCase def setup sample = IO.readlines("#{File.dirname(__FILE__)}"+ "/../../ifconfig_examples/dragonflybsd.txt").join - @cfg = IfconfigWrapper.new('BSD',sample).parse + netstat_sample = IO.readlines("#{File.dirname(__FILE__)}"+ + "/../../ifconfig_examples/dragonflybsd_netstat.txt").join + @cfg = IfconfigWrapper.new('BSD',sample, netstat_sample).parse end def test_interface_list diff --git a/test/unit/tc_netbsd.rb b/test/unit/tc_netbsd.rb index 4e9c56a..19e461e 100644 --- a/test/unit/tc_netbsd.rb +++ b/test/unit/tc_netbsd.rb @@ -4,7 +4,9 @@ class TC_NetBSDTest < Test::Unit::TestCase def setup sample = IO.readlines("#{File.dirname(__FILE__)}"+ '/../../ifconfig_examples/netbsd.txt').join - @cfg = IfconfigWrapper.new('BSD',sample).parse + netstat_sample = IO.readlines("#{File.dirname(__FILE__)}"+ + "/../../ifconfig_examples/netbsd_netstat.txt").join + @cfg = IfconfigWrapper.new('BSD',sample, netstat_sample).parse end def test_interface_list diff --git a/test/unit/tc_openbsd.rb b/test/unit/tc_openbsd.rb index c474755..ec91805 100644 --- a/test/unit/tc_openbsd.rb +++ b/test/unit/tc_openbsd.rb @@ -4,7 +4,8 @@ class TC_OpenBSDTest < Test::Unit::TestCase def setup sample = IO.readlines("#{File.dirname(__FILE__)}"+ '/../../ifconfig_examples/openbsd.txt').join - @cfg = IfconfigWrapper.new('BSD',sample).parse + netstat_sample = "" + @cfg = IfconfigWrapper.new('BSD',sample,netstat_sample).parse end def test_interface_list diff --git a/test/unit/tc_osx.rb b/test/unit/tc_osx.rb index 5c352db..93a72ee 100644 --- a/test/unit/tc_osx.rb +++ b/test/unit/tc_osx.rb @@ -4,7 +4,8 @@ class TC_OsxTest < Test::Unit::TestCase def setup sample = IO.readlines("#{File.dirname(__FILE__)}"+ "/../../ifconfig_examples/osx.txt").join - @cfg = IfconfigWrapper.new('BSD',sample).parse + netstat_sample = "" + @cfg = IfconfigWrapper.new('BSD',sample,netstat_sample).parse end def test_interface_list diff --git a/test/unit/tc_sunos.rb b/test/unit/tc_sunos.rb index e1b3666..cc85ca0 100644 --- a/test/unit/tc_sunos.rb +++ b/test/unit/tc_sunos.rb @@ -4,42 +4,44 @@ class TC_SunOSTest < Test::Unit::TestCase def setup sample = IO.readlines("#{File.dirname(__FILE__)}"+ '/../../ifconfig_examples/sunos.txt').join - @@cfg = IfconfigWrapper.new('SunOS',sample).parse + netstat_sample = IO.readlines("#{File.dirname(__FILE__)}"+ + "/../../ifconfig_examples/sunos_netstat.txt").join + @cfg = IfconfigWrapper.new('SunOS',sample,netstat_sample).parse end def test_interface_list - assert(@@cfg.interfaces.sort == ["le1", "lo0", "bge0"].sort, + assert(@cfg.interfaces.sort == ["le1", "lo0", "bge0"].sort, "Failed to parse all interfaces") end def test_mac_parse - assert(@@cfg['bge0'].mac == "0:3:ba:42:9d:ef", - "Failed to parse MAC address: "+@@cfg['bge0'].mac) + assert(@cfg['bge0'].mac == "0:3:ba:42:9d:ef", + "Failed to parse MAC address: "+@cfg['bge0'].mac) end def test_flags - assert(@@cfg['bge0'].flags.include?('BROADCAST') && - @@cfg['bge0'].flags.include?('RUNNING') && - @@cfg['bge0'].flags.include?('MULTICAST') && - @@cfg['bge0'].up?, - "FLAG Parsing failed: #{@@cfg['bge0'].flags}") + assert(@cfg['bge0'].flags.include?('BROADCAST') && + @cfg['bge0'].flags.include?('RUNNING') && + @cfg['bge0'].flags.include?('MULTICAST') && + @cfg['bge0'].up?, + "FLAG Parsing failed: #{@cfg['bge0'].flags}") end def test_addr_types - assert(@@cfg['bge0'].addr_types.include?('inet') && - @@cfg['bge0'].addr_types.include?('inet6'), + assert(@cfg['bge0'].addr_types.include?('inet') && + @cfg['bge0'].addr_types.include?('inet6'), "Failed to parse all address types") end def test_networks - assert(@@cfg['bge0'].addr_types.include?('inet') && - @@cfg['bge0'].addr_types.include?('inet6'), - "Missing Address Types: #{@@cfg['bge0'].addr_types}") + assert(@cfg['bge0'].addr_types.include?('inet') && + @cfg['bge0'].addr_types.include?('inet6'), + "Missing Address Types: #{@cfg['bge0'].addr_types}") end def test_attribs - assert(@@cfg['bge0'].rx['bytes'].class == Fixnum || NilClass&& - @@cfg['bge0'].tx['bytes'].class == Fixnum || NilClass, "Wrong class") + assert(@cfg['bge0'].rx['bytes'].class == Fixnum || NilClass&& + @cfg['bge0'].tx['bytes'].class == Fixnum || NilClass, "Wrong class") end end From 9efc3c2edbfd2d3397e4cae1997529b24efb9277 Mon Sep 17 00:00:00 2001 From: Alan Somers Date: Mon, 31 Mar 2014 16:56:25 -0600 Subject: [PATCH 10/19] Added media type detection for BSD. ifconfig_examples/freebsd.txt ifconfig_examples/freebsd_netstat.txt test/unit/tc_freebsd.rb Added a 10G interface to the examples for FreeBSD lib/ifconfig/bsd/interface_types.rb lib/ifconfig/common/interface_types.rb Added parse_media. It's a noop for non-BSD. --- ifconfig_examples/freebsd.txt | 7 +++++++ ifconfig_examples/freebsd_netstat.txt | 2 ++ lib/ifconfig/bsd/interface_types.rb | 10 ++++++++++ lib/ifconfig/common/interface_types.rb | 5 ++++- test/unit/tc_freebsd.rb | 12 +++++++++++- 5 files changed, 34 insertions(+), 2 deletions(-) diff --git a/ifconfig_examples/freebsd.txt b/ifconfig_examples/freebsd.txt index 4d3b833..96bf0e0 100644 --- a/ifconfig_examples/freebsd.txt +++ b/ifconfig_examples/freebsd.txt @@ -25,6 +25,13 @@ igb3: flags=8843 metric 0 mtu 1500 nd6 options=29 media: Ethernet autoselect (1000baseT ) status: active +cxgbe0: flags=8843 metric 0 mtu 1500 + options=6c07bb + ether 22:33:44:55:66:77 + inet 10.1.0.11 netmask 0xfffff000 broadcast 10.1.15.255 + nd6 options=29 + media: Ethernet 10Gbase-Twinax + status: active lo0: flags=8049 metric 0 mtu 16384 options=600003 inet6 ::1 prefixlen 128 diff --git a/ifconfig_examples/freebsd_netstat.txt b/ifconfig_examples/freebsd_netstat.txt index 576e058..c76f52a 100644 --- a/ifconfig_examples/freebsd_netstat.txt +++ b/ifconfig_examples/freebsd_netstat.txt @@ -5,6 +5,8 @@ igb0 1500 10.1.0.0/20 10.1.3.176 1683211 - - 1480094 - igb1 1500 00:11:22:33:44:66 6369236 0 0 36130 0 0 igb2 1500 00:11:22:33:44:66 57591 0 0 35355 0 0 igb3 1500 00:11:22:33:44:66 53758 0 0 37262 0 0 +cxgbe0 1500 22:33:44:55:66:77 1234567 0 0 2345678 0 0 +cxgbe0 1500 10.1.0.0/20 10.1.0.11 1234568 - - 2345679 - - lo0 16384 7486204 0 0 7486201 0 0 lo0 16384 ::1/128 ::1 6628 - - 6628 - - lo0 16384 fe80::1%lo0/6 fe80::1 0 - - 0 - - diff --git a/lib/ifconfig/bsd/interface_types.rb b/lib/ifconfig/bsd/interface_types.rb index f28e5ff..bd24e35 100644 --- a/lib/ifconfig/bsd/interface_types.rb +++ b/lib/ifconfig/bsd/interface_types.rb @@ -19,6 +19,8 @@ def parse_ifconfig(netstattxt=nil) parse_flags(line) when /\s*fib:/ parse_fib(line) + when /\s*media:/ + parse_media(line) end } end @@ -62,6 +64,14 @@ def add_network(line) class EthernetAdapter + # Parses the "media: Ethernet autoselect (1000baseT )" line + def parse_media(line) + match = line.match /Ethernet (autoselect )?(\()?(\S+?)[ )]/ + if match + @media = match[3] + end + end + def set_mac begin match=@ifconfig.match(/\s+ether\s+([a-f\d]{1,2}(?:\:[a-f\d]{1,2}){5})/im) diff --git a/lib/ifconfig/common/interface_types.rb b/lib/ifconfig/common/interface_types.rb index 0716e9a..5290c6f 100644 --- a/lib/ifconfig/common/interface_types.rb +++ b/lib/ifconfig/common/interface_types.rb @@ -92,6 +92,9 @@ def has_addr?(addr) return self.addresses.include?(addr) end + def parse_media + end + def to_s s = @name+":"+self.ifacetype.to_s+"\n" @networks.keys.sort.each { |network| @@ -121,7 +124,7 @@ def initialize(name,ifconfigtxt,netstattxt=nil) @mac = set_mac end - attr_reader :mac, :interrupt, :rxbytes, :txbytes, :rxpackets, + attr_reader :mac, :media, :interrupt, :rxbytes, :txbytes, :rxpackets, :txpackets def to_s diff --git a/test/unit/tc_freebsd.rb b/test/unit/tc_freebsd.rb index 79b5f68..e4e169c 100644 --- a/test/unit/tc_freebsd.rb +++ b/test/unit/tc_freebsd.rb @@ -11,7 +11,7 @@ def setup def test_interface_list assert(@cfg.interfaces.sort == ["igb0", "igb1", "igb2", "igb3", - "lo0", "lagg0"].sort, + "cxgbe0", "lo0", "lagg0"].sort, "Failed to parse all interfaces") end @@ -47,6 +47,16 @@ def test_fib assert_equal(0, @cfg['lo0'].fib) assert_equal(0, @cfg['igb0'].fib) assert_equal(1, @cfg['lagg0'].fib) + assert_equal(0, @cfg['cxgbe0'].fib) + end + + def test_media + assert_equal("1000baseT", @cfg['igb0'].media) + assert_equal("1000baseT", @cfg['igb0'].media) + assert_equal("1000baseT", @cfg['igb0'].media) + assert_equal("1000baseT", @cfg['igb0'].media) + assert_equal("10Gbase-Twinax", @cfg['cxgbe0'].media) + assert_equal(nil, @cfg['lagg0'].media) end def test_mtu From 09d2e22e72168b1849054509067b6aadfbb177ee Mon Sep 17 00:00:00 2001 From: Alan Somers Date: Mon, 31 Mar 2014 17:13:58 -0600 Subject: [PATCH 11/19] Added specific support for link aggregation lib/ifconfig/bsd/ifconfig.rb lib/ifconfig/bsd/interface_types.rb lib/ifconfig/common/interface_types.rb Added a LinkAggregation interface type. It only works on FreeBSD ATM. test/unit/tc_freebsd.rb Added tests for the existing example output files. --- lib/ifconfig/bsd/ifconfig.rb | 6 ++++++ lib/ifconfig/bsd/interface_types.rb | 19 +++++++++++++++++++ lib/ifconfig/common/interface_types.rb | 14 ++++++++++++++ test/unit/tc_freebsd.rb | 9 +++++++++ 4 files changed, 48 insertions(+) diff --git a/lib/ifconfig/bsd/ifconfig.rb b/lib/ifconfig/bsd/ifconfig.rb index 1c0f42f..ed22b02 100644 --- a/lib/ifconfig/bsd/ifconfig.rb +++ b/lib/ifconfig/bsd/ifconfig.rb @@ -31,6 +31,12 @@ def initialize(ifconfig=nil,netstat=nil,verbose=nil) when /^lo\d\:/im @ifaces[iface_name] = LoopbackInterface.new(iface_name,iface) parse_activity(iface_name) + when /^lagg[0-9]:/ + # This clause is only matched on FreeBSD. Other OSes have similar + # drivers, for example agr(4) on NetBSD, but ruby-ifconfig does not + # yet support them. + @ifaces[iface_name] = LinkAggregation.new(iface_name,iface) + parse_activity(iface_name) when /\s+media\:\s+Ethernet\s+/im @ifaces[iface_name] = EthernetAdapter.new(iface_name,iface) parse_activity(iface_name) diff --git a/lib/ifconfig/bsd/interface_types.rb b/lib/ifconfig/bsd/interface_types.rb index bd24e35..9c76e5b 100644 --- a/lib/ifconfig/bsd/interface_types.rb +++ b/lib/ifconfig/bsd/interface_types.rb @@ -84,3 +84,22 @@ def set_mac end end end + + +class LinkAggregation + def parse_ifconfig(netstattxt=nil) + super(netstattxt) + @ifconfig.split("\n").each { |line| + proto_match = line.match( /^\s*laggproto\s*(\S+)\s*/ ) + if proto_match + @laggproto = proto_match[1] + next + end + port_match = line.match( /^\s*laggport:\s*(\S+)\s*/ ) + if port_match + @lagg_children << port_match[1] + next + end + } + end +end diff --git a/lib/ifconfig/common/interface_types.rb b/lib/ifconfig/common/interface_types.rb index 5290c6f..bcb0e76 100644 --- a/lib/ifconfig/common/interface_types.rb +++ b/lib/ifconfig/common/interface_types.rb @@ -110,6 +110,8 @@ def to_s s += " Metric: #{@metric}\n" s += " Flags: #{@flags.join(',')}\n" s += " Fib: #{@fib}\n" if @fib != 0 + s += " Lagg Proto: #{@laggproto}\n" if @laggproto + s += " Lagg Children: #{@lagg_children}\n" if @lagg_children s += " Status: UP" if self.status return s end @@ -144,3 +146,15 @@ class IPv6_in_IPv4 < NetworkAdapter class SerialLineIP < NetworkAdapter end + +# Represents combinations of network interfaces. Variously known as lagg, +# bond, teaming, trunking, etc. +class LinkAggregation < EthernetAdapter + attr_reader :lagg_children, :laggproto + + def initialize(name,ifconfigtxt,netstattxt=nil) + @lagg_children = [] + @laggproto = nil + super(name,ifconfigtxt,netstattxt) + end +end diff --git a/test/unit/tc_freebsd.rb b/test/unit/tc_freebsd.rb index e4e169c..d1a7003 100644 --- a/test/unit/tc_freebsd.rb +++ b/test/unit/tc_freebsd.rb @@ -65,4 +65,13 @@ def test_mtu assert_equal(16384, @cfg['lo0'].mtu) end + def test_laggproto + assert_equal("lacp", @cfg['lagg0'].laggproto) + end + + def test_lagg_children + assert_equal(['igb1', 'igb2', 'igb3'].sort, + @cfg['lagg0'].lagg_children.sort) + end + end From 09f4f7a4bc9a1a80c62362fa475b7e2b51b1bf7f Mon Sep 17 00:00:00 2001 From: Alan Somers Date: Tue, 1 Apr 2014 10:08:22 -0600 Subject: [PATCH 12/19] Update documentation and gemspec. Increase version to 1.3.0 because of feature additions. Remove the rubyforge project. It refers to an old version (aaalex's) and Rubyforge will soon disappear anyway. --- Changelog | 9 +++++++++ README | 11 ++++++----- ruby-ifconfig.gemspec | 13 +++++++------ 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/Changelog b/Changelog index 0d7d2c9..b5abeac 100644 --- a/Changelog +++ b/Changelog @@ -1,3 +1,12 @@ +Version 1.3.0 +- import into asomers/ruby-ifconfig on Github +- bug fixes, mostly in the tests +- added support for fibs, media type, and lagg devices on FreeBSD + +Version 1.2.4 +- import into langhorst/ruby-ifconfig on Github +- bug fixes, mostly in the tests + Version 1.2.3 - added addrs_with_mask method in NetworkAdapter (Alex Peuchert) diff --git a/README b/README index 2cc7f07..b8264b5 100644 --- a/README +++ b/README @@ -2,10 +2,11 @@ This is a Ruby wrapper around the ifconfig command. The goal is to make getting any information that ifconfig provides easy to access. -It was developed on Linux 2.6 with ifconfig from net-tools 1.60. It also has -support for SunOS, Darwin and Open, Free, Net, and DragonflyBSD. These have -not been thoroughly tested. Non Linux platforms call netstat for interface -packet information +It was developed on Linux 2.6 with ifconfig from net-tools 1.60. The FreeBSD +support has been tested on FreBSD 9.2 amd64. The package also has support for +SunOS, Darwin, OSX and Open, Net, and DragonflyBSD. These have not been +thoroughly tested. Non Linux platforms call netstat for interface packet +information. Please send me ifconfig / netstat output for platforms that don't work. @@ -29,7 +30,7 @@ Prerequisites You must have a working ifconfig binary in your path. The output format of you particular version of ifconfig must be supported. -Currently this has only been tested with net-tool 1.6 on Linux +Currently this has only been tested with FreeBSD 9.2 and net-tool 1.6 on Linux. Example ------ diff --git a/ruby-ifconfig.gemspec b/ruby-ifconfig.gemspec index 1d0fe99..8884090 100644 --- a/ruby-ifconfig.gemspec +++ b/ruby-ifconfig.gemspec @@ -2,12 +2,11 @@ Gem::Specification.new do |s| s.name = %q{ruby-ifconfig} - s.version = "1.2.4" - s.date = %q{2011-09-19} - s.authors = ["Daniel Hobe", "Alex Peuchert", "Ali Jelveh"] - s.email = %q{daniel@nightrunner.com} - s.homepage = %q{http://github.com/dudemeister/ruby-ifconfig} - s.rubyforge_project = %q{ruby-ifconfig} + s.version = "1.3.0" + s.date = %q{2014-01-01} + s.authors = ["Daniel Hobe", "Alex Peuchert", "Ali Jelveh", "Alan Somers"] + s.email = %q{asomers@freebsd.org} + s.homepage = %q{http://github.com/asomers/ruby-ifconfig} s.description = %q{Ruby wrapper around the ifconfig command.} s.summary = %q{This is a Ruby wrapper around the ifconfig command. The goal is to make getting any information that ifconfig provides easy to access.} @@ -58,12 +57,14 @@ Gem::Specification.new do |s| ifconfig_examples/dragonflybsd.txt ifconfig_examples/dragonflybsd_netstat.txt ifconfig_examples/netbsd.txt + ifconfig_examples/netbsd_netstat.txt ifconfig_examples/freebsd_netstat.txt ifconfig_examples/linux.txt ifconfig_examples/linux_ethernet.txt ifconfig_examples/freebsd.txt ifconfig_examples/openbsd.txt ifconfig_examples/sunos.txt + ifconfig_examples/sunos_netstat.txt ifconfig_examples/osx.txt ] From 977266f2094d355d2536dc43b2beab91f00cd296 Mon Sep 17 00:00:00 2001 From: Alan Somers Date: Wed, 2 Apr 2014 12:44:51 -0600 Subject: [PATCH 13/19] Two small conviences 1) Don't raise an exception when looking up laggproto on a non-lagg interface. Just return nil. 2) Print out the media type in NetworkAdapter.to_s --- lib/ifconfig/common/interface_types.rb | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/ifconfig/common/interface_types.rb b/lib/ifconfig/common/interface_types.rb index bcb0e76..e5e193e 100644 --- a/lib/ifconfig/common/interface_types.rb +++ b/lib/ifconfig/common/interface_types.rb @@ -18,7 +18,7 @@ def initialize(name, ifacetxt, netstattxt=nil) @fib = 0 parse_ifconfig(netstattxt) end - attr_reader :status, :name, :fib, :flags, :mtu + attr_reader :status, :name, :fib, :flags, :mtu, :lagg_children, :laggproto attr_accessor :tx, :rx # take array and turn each two entries into @@ -107,6 +107,7 @@ def to_s end s += " MTU: #{@mtu}\n" + s += " Media: #{@media}\n" s += " Metric: #{@metric}\n" s += " Flags: #{@flags.join(',')}\n" s += " Fib: #{@fib}\n" if @fib != 0 @@ -150,8 +151,6 @@ class SerialLineIP < NetworkAdapter # Represents combinations of network interfaces. Variously known as lagg, # bond, teaming, trunking, etc. class LinkAggregation < EthernetAdapter - attr_reader :lagg_children, :laggproto - def initialize(name,ifconfigtxt,netstattxt=nil) @lagg_children = [] @laggproto = nil From 1033d009f0fc92069692a04fa752d8e8d3368f37 Mon Sep 17 00:00:00 2001 From: Alan Somers Date: Wed, 2 Apr 2014 14:16:32 -0600 Subject: [PATCH 14/19] Remove the test_.rb files. Add a selftest.rb test/test_.rb are redundant with the unit tests. Added selftest.rb, which prints the ifconfig data available on the running system; not from the test fixtures. --- test/selftest.rb | 9 +++++++++ test/test_bsd.rb | 35 ----------------------------------- test/test_darwin.rb | 33 --------------------------------- test/test_dragonflybsd.rb | 35 ----------------------------------- test/test_linux.rb | 30 ------------------------------ test/test_netbsd.rb | 33 --------------------------------- test/test_openbsd.rb | 33 --------------------------------- test/test_osx.rb | 33 --------------------------------- test/test_sunos.rb | 35 ----------------------------------- 9 files changed, 9 insertions(+), 267 deletions(-) create mode 100644 test/selftest.rb delete mode 100755 test/test_bsd.rb delete mode 100755 test/test_darwin.rb delete mode 100755 test/test_dragonflybsd.rb delete mode 100755 test/test_linux.rb delete mode 100755 test/test_netbsd.rb delete mode 100755 test/test_openbsd.rb delete mode 100755 test/test_osx.rb delete mode 100755 test/test_sunos.rb diff --git a/test/selftest.rb b/test/selftest.rb new file mode 100644 index 0000000..49d3a8a --- /dev/null +++ b/test/selftest.rb @@ -0,0 +1,9 @@ +#!/usr/bin/ruby -w + +$:.unshift File.expand_path('../lib', File.dirname(__FILE__)) + +require 'ifconfig' + +cfg = IfconfigWrapper.new().parse + +puts cfg diff --git a/test/test_bsd.rb b/test/test_bsd.rb deleted file mode 100755 index 08e5457..0000000 --- a/test/test_bsd.rb +++ /dev/null @@ -1,35 +0,0 @@ -#!/usr/bin/ruby -w - -$:.unshift File.expand_path('../lib', File.dirname(__FILE__)) - -require 'ifconfig' -require 'pp' - -ifcfg = IO.readlines(File.expand_path('../../ifconfig_examples/freebsd.txt', __FILE__)).join -netstat = IO.readlines(File.expand_path('../../ifconfig_examples/freebsd_netstat.txt', __FILE__)).join - -ifconfig = IfconfigWrapper.new('BSD',ifcfg,netstat).parse - -puts "Interfaces: (ifconfig.interfaces)" -pp ifconfig.interfaces - -puts "\nrl0 mac address: (ifconfig['rl0'].mac)" -pp ifconfig['rl0'].mac - -puts "\nIpV4 addresses on rl0: (ifconfig['rl0'].addresses('inet'))" -pp ifconfig['rl0'].addresses('inet') - -puts "\nAll addresses reported by ifconfig: (ifconfig.addresses)" -pp ifconfig.addrs_with_type - -puts "\nList of address types for rl0: (ifconfig['rl0'].addr_types)" -pp ifconfig['rl0'].addr_types - -puts "\niconfig.each { block }" -ifconfig.each do |iface| - pp iface.name if iface.up? -end - -puts -s = ifconfig.to_s -puts s diff --git a/test/test_darwin.rb b/test/test_darwin.rb deleted file mode 100755 index 72e6435..0000000 --- a/test/test_darwin.rb +++ /dev/null @@ -1,33 +0,0 @@ -#!/usr/bin/ruby -w - -$:.unshift File.expand_path('../lib', File.dirname(__FILE__)) - -require 'ifconfig' -require 'pp' - -sample = IO.readlines(File.expand_path('../../ifconfig_examples/darwin.txt', __FILE__)).join -ifconfig = IfconfigWrapper.new('BSD',sample).parse - -puts "Interfaces: (ifconfig.interfaces)" -pp ifconfig.interfaces - -puts "\nen0 mac address: (ifconfig['en0'].mac)" -pp ifconfig['en0'].mac - -puts "\nIpV4 addresses on en0: (ifconfig['en0'].addresses('inet'))" -pp ifconfig['en0'].addresses('inet') - -puts "\nAll addresses reported by ifconfig: (ifconfig.addresses)" -pp ifconfig.addrs_with_type - -puts "\nList of address types for en0: (ifconfig['en0'].addr_types)" -pp ifconfig['en0'].addr_types - -puts "\niconfig.each { block }" -ifconfig.each do |iface| - pp iface.name if iface.up? -end - -puts -s = ifconfig.to_s -puts s diff --git a/test/test_dragonflybsd.rb b/test/test_dragonflybsd.rb deleted file mode 100755 index 66e0ece..0000000 --- a/test/test_dragonflybsd.rb +++ /dev/null @@ -1,35 +0,0 @@ -#!/usr/bin/ruby -w - -$:.unshift File.expand_path('../lib', File.dirname(__FILE__)) - -require 'ifconfig' -require 'pp' - -ifcfg = IO.readlines(File.expand_path('../../ifconfig_examples/dragonflybsd.txt', __FILE__)).join -netstat = IO.readlines(File.expand_path('../../ifconfig_examples/dragonflybsd_netstat.txt', __FILE__)).join - -ifconfig = IfconfigWrapper.new('BSD',ifcfg,netstat).parse - -puts "Interfaces: (ifconfig.interfaces)" -pp ifconfig.interfaces - -puts "\nrl0 mac address: (ifconfig['rl0'].mac)" -pp ifconfig['rl0'].mac - -puts "\nIpV4 addresses on rl0: (ifconfig['rl0'].addresses('inet'))" -pp ifconfig['rl0'].addresses('inet') - -puts "\nAll addresses reported by ifconfig: (ifconfig.addresses)" -pp ifconfig.addrs_with_type - -puts "\nList of address types for rl0: (ifconfig['rl0'].addr_types)" -pp ifconfig['rl0'].addr_types - -puts "\niconfig.each { block }" -ifconfig.each do |iface| - pp iface.name if iface.up? -end - -puts -s = ifconfig.to_s -puts s diff --git a/test/test_linux.rb b/test/test_linux.rb deleted file mode 100755 index 02833b7..0000000 --- a/test/test_linux.rb +++ /dev/null @@ -1,30 +0,0 @@ -#!/usr/bin/ruby -w - -$:.unshift File.expand_path('../lib', File.dirname(__FILE__)) - -require 'ifconfig' -require 'pp' - -sample = IO.readlines(File.expand_path('../../ifconfig_examples/linux.txt', __FILE__)).join -ifconfig = IfconfigWrapper.new('Linux',sample).parse - -puts "Interfaces: (ifconfig.interfaces)" -pp ifconfig.interfaces - -puts "\neth0 mac address: (ifconfig['eth0'].mac)" -pp ifconfig['eth0'].mac - -puts "\nIpV4 addresses on eth0: (ifconfig['eth0'].addresses('inet'))" -pp ifconfig['eth0'].addresses('inet') - -puts "\nAll addresses reported by ifconfig: (ifconfig.addresses)" -pp ifconfig.addrs_with_type - -puts "\niconfig.each { block }" -ifconfig.each do |iface| - pp iface.name if iface.up? -end - -puts -s = ifconfig.to_s -puts s diff --git a/test/test_netbsd.rb b/test/test_netbsd.rb deleted file mode 100755 index 1429178..0000000 --- a/test/test_netbsd.rb +++ /dev/null @@ -1,33 +0,0 @@ -#!/usr/bin/ruby -w - -$:.unshift File.expand_path('../lib', File.dirname(__FILE__)) - -require 'ifconfig' -require 'pp' - -sample = IO.readlines(File.expand_path('../../ifconfig_examples/netbsd.txt', __FILE__)).join -ifconfig = IfconfigWrapper.new('BSD',sample).parse - -puts "Interfaces: (ifconfig.interfaces)" -pp ifconfig.interfaces - -puts "\ncs0 mac address: (ifconfig['cs0'].mac)" -pp ifconfig['cs0'].mac - -puts "\nIpV4 addresses on cs0: (ifconfig['cs0'].addresses('inet'))" -pp ifconfig['cs0'].addresses('inet') - -puts "\nAll addresses reported by ifconfig: (ifconfig.addresses)" -pp ifconfig.addrs_with_type - -puts "\nList of address types for cs0: (ifconfig['cs0'].addr_types)" -pp ifconfig['cs0'].addr_types - -puts "\niconfig.each { block }" -ifconfig.each do |iface| - pp iface.name if iface.up? -end - -puts -s = ifconfig.to_s -puts s diff --git a/test/test_openbsd.rb b/test/test_openbsd.rb deleted file mode 100755 index 10eb5e2..0000000 --- a/test/test_openbsd.rb +++ /dev/null @@ -1,33 +0,0 @@ -#!/usr/bin/ruby -w - -$:.unshift File.expand_path('../lib', File.dirname(__FILE__)) - -require 'ifconfig' -require 'pp' - -sample = IO.readlines(File.expand_path('../../ifconfig_examples/openbsd.txt', __FILE__)).join -ifconfig = IfconfigWrapper.new('BSD',sample).parse - -puts "Interfaces: (ifconfig.interfaces)" -pp ifconfig.interfaces - -puts "\nxl0 mac address: (ifconfig['xl0'].mac)" -pp ifconfig['xl0'].mac - -puts "\nIpV4 addresses on xl0: (ifconfig['xl0'].addresses('inet'))" -pp ifconfig['xl0'].addresses('inet') - -puts "\nAll addresses reported by ifconfig: (ifconfig.addresses)" -pp ifconfig.addrs_with_type - -puts "\nList of address types for xl0: (ifconfig['xl0'].addr_types)" -pp ifconfig['xl0'].addr_types - -puts "\niconfig.each { block }" -ifconfig.each do |iface| - pp iface.name if iface.up? -end - -puts -s = ifconfig.to_s -puts s diff --git a/test/test_osx.rb b/test/test_osx.rb deleted file mode 100755 index 4b9b265..0000000 --- a/test/test_osx.rb +++ /dev/null @@ -1,33 +0,0 @@ -#!/usr/bin/ruby -w - -$:.unshift File.expand_path('../lib', File.dirname(__FILE__)) - -require 'ifconfig' -require 'pp' - -sample = IO.readlines(File.expand_path('../../ifconfig_examples/osx.txt', __FILE__)).join -ifconfig = IfconfigWrapper.new('BSD',sample).parse - -puts "Interfaces: (ifconfig.interfaces)" -pp ifconfig.interfaces - -puts "\nen0 mac address: (ifconfig['en0'].mac)" -pp ifconfig['en0'].mac - -puts "\nIpV4 addresses on en0: (ifconfig['en0'].addresses('inet'))" -pp ifconfig['en0'].addresses('inet') - -puts "\nAll addresses reported by ifconfig: (ifconfig.addresses)" -pp ifconfig.addrs_with_type - -puts "\nList of address types for en0: (ifconfig['en0'].addr_types)" -pp ifconfig['en0'].addr_types - -puts "\niconfig.each { block }" -ifconfig.each do |iface| - pp iface.name if iface.up? -end - -puts -s = ifconfig.to_s -puts s diff --git a/test/test_sunos.rb b/test/test_sunos.rb deleted file mode 100755 index dc3c2e7..0000000 --- a/test/test_sunos.rb +++ /dev/null @@ -1,35 +0,0 @@ -#!/usr/bin/ruby -w - -$:.unshift File.expand_path('../lib', File.dirname(__FILE__)) - -# -#imaptest1# netstat -in -#Name Mtu Net/Dest Address Ipkts Ierrs Opkts Oerrs Collis Queue -#lo0 8232 0.0.0.0 0.0.0.4 267824 0 267824 0 0 0 -#bge0 1500 10.0.0.0 10.32.4.138 10935939 0 7741167 0 0 0 -require 'ifconfig' -require 'pp' - -sample = IO.readlines(File.expand_path('../../ifconfig_examples/sunos.txt', __FILE__)).join -ifconfig = IfconfigWrapper.new('SunOS',sample).parse - -puts "Interfaces: (ifconfig.interfaces)" -pp ifconfig.interfaces - -puts "\nbge0 mac address: (ifconfig['bge0'].mac)" -pp ifconfig['bge0'].mac - -puts "\nIpV4 addresses on bge0: (ifconfig['bge0'].addresses('inet'))" -pp ifconfig['bge0'].addresses('inet') - -puts "\nAll addresses reported by ifconfig: (ifconfig.addresses)" -pp ifconfig.addrs_with_type - -puts "\niconfig.each { block }" -ifconfig.each do |iface| - pp iface.name if iface.up? -end - -puts -s = ifconfig.to_s -puts s From 2841dd5b2c4804d144bae1d2f901b5dd0e91e703 Mon Sep 17 00:00:00 2001 From: Alan Somers Date: Wed, 2 Apr 2014 14:24:34 -0600 Subject: [PATCH 15/19] Added parsing for interface capabilities lib/ifconfig/bsd/interface_types.rb Parse the "options=" or "enabled=" line. lib/ifconfig/common/interface_types.rb Display capabilities in NetworkAdapter.to_s ifconfig_examples/dragonflybsd.txt test/unit/tc_dragonflybsd.rb Add test case using output format of DragonFlyBSD 3.6.0. ifconfig_examples/netbsd.txt test/unit/tc_netbsd.rb Add test case using output format of NetBSD 6.0. test/unit/tc_freebsd.rb Add test case using data already present in the fixture. --- ifconfig_examples/dragonflybsd.txt | 1 + ifconfig_examples/netbsd.txt | 2 ++ lib/ifconfig/bsd/interface_types.rb | 21 +++++++++++++++++---- lib/ifconfig/common/interface_types.rb | 3 +++ test/unit/tc_dragonflybsd.rb | 5 +++++ test/unit/tc_freebsd.rb | 6 ++++++ test/unit/tc_netbsd.rb | 5 +++++ 7 files changed, 39 insertions(+), 4 deletions(-) diff --git a/ifconfig_examples/dragonflybsd.txt b/ifconfig_examples/dragonflybsd.txt index 23f887a..a0ac4ba 100644 --- a/ifconfig_examples/dragonflybsd.txt +++ b/ifconfig_examples/dragonflybsd.txt @@ -1,4 +1,5 @@ rl0: flags=8843 mtu 1500 + options=1b inet6 fe80::202:44ff:fe8f:bb15%rl0 prefixlen 64 scopeid 0x2 inet 192.168.1.24 netmask 0xffffff00 broadcast 192.168.1.255 ether 00:02:44:8f:bb:15 diff --git a/ifconfig_examples/netbsd.txt b/ifconfig_examples/netbsd.txt index 544ef44..bccc3c5 100644 --- a/ifconfig_examples/netbsd.txt +++ b/ifconfig_examples/netbsd.txt @@ -1,4 +1,6 @@ cs0: flags=8863 mtu 1500 + capabilities=2bf80 + enabled=3000 address: 08:00:2b:81:62:ca media: Ethernet 10baseT status: active diff --git a/lib/ifconfig/bsd/interface_types.rb b/lib/ifconfig/bsd/interface_types.rb index 9c76e5b..391ac59 100644 --- a/lib/ifconfig/bsd/interface_types.rb +++ b/lib/ifconfig/bsd/interface_types.rb @@ -15,12 +15,18 @@ def parse_ifconfig(netstattxt=nil) case line when /^\s+#{@protos}/ add_network(line) - when /flags\=/i + when /\s*flags\=/i parse_flags(line) - when /\s*fib:/ + when /^\s*fib:/ parse_fib(line) - when /\s*media:/ + when /^\s*media:/ parse_media(line) + when /^\s*options=/ + # FreeBSD and DragonFlyBSD use "options=" + parse_capabilities(line) + when /^\s*enabled=/ + # NetBSD uses "enabled=" + parse_capabilities(line) end } end @@ -30,6 +36,13 @@ def parse_fib(line) @fib = line.split()[1].to_i end + # parses the "options=1b" line + # + def parse_capabilities(line) + caps = line.match(/\<(\S+)\>/i)[1] + @capabilities = caps.strip.split(',') + end + # parses the "UP LOOPBACK RUNNING MTU:3924 Metric:1" line # def parse_flags(line) @@ -76,7 +89,7 @@ def set_mac begin match=@ifconfig.match(/\s+ether\s+([a-f\d]{1,2}(?:\:[a-f\d]{1,2}){5})/im) return match[1] unless match.nil? - # Openbsd + # OpenBSD and NetBSD match = @ifconfig.match(/\s+address\:\s+([a-f\d]{1,2}(?:\:[a-f\d]{1,2}){5})/im) return match[1] unless match.nil? rescue NoMethodError diff --git a/lib/ifconfig/common/interface_types.rb b/lib/ifconfig/common/interface_types.rb index e5e193e..a603b12 100644 --- a/lib/ifconfig/common/interface_types.rb +++ b/lib/ifconfig/common/interface_types.rb @@ -12,6 +12,7 @@ def initialize(name, ifacetxt, netstattxt=nil) 'EtherTalk Phase 2'].join("|") @networks = {} @flags = [] + @capabilities = [] @mtu = nil @metric = nil @rx = @tx = {} @@ -19,6 +20,7 @@ def initialize(name, ifacetxt, netstattxt=nil) parse_ifconfig(netstattxt) end attr_reader :status, :name, :fib, :flags, :mtu, :lagg_children, :laggproto + attr_reader :capabilities attr_accessor :tx, :rx # take array and turn each two entries into @@ -106,6 +108,7 @@ def to_s s += " RX packets: #{self.rx['packets']}, TX packets: #{self.tx['packets']}\n" end + s += " Capabilities: #{@capabilities.join(',')}\n" s += " MTU: #{@mtu}\n" s += " Media: #{@media}\n" s += " Metric: #{@metric}\n" diff --git a/test/unit/tc_dragonflybsd.rb b/test/unit/tc_dragonflybsd.rb index 9a685cf..3ad3492 100644 --- a/test/unit/tc_dragonflybsd.rb +++ b/test/unit/tc_dragonflybsd.rb @@ -39,4 +39,9 @@ def test_attribs end + def test_capabilities + expected = %w(RXCSUM TXCSUM VLAN_MTU VLAN_HWTAGGING) + assert_equal(expected.sort, @cfg['rl0'].capabilities.sort) + end + end diff --git a/test/unit/tc_freebsd.rb b/test/unit/tc_freebsd.rb index d1a7003..3da25c0 100644 --- a/test/unit/tc_freebsd.rb +++ b/test/unit/tc_freebsd.rb @@ -74,4 +74,10 @@ def test_lagg_children @cfg['lagg0'].lagg_children.sort) end + def test_capabilities + expected = %w(RXCSUM TXCSUM VLAN_MTU VLAN_HWTAGGING JUMBO_MTU VLAN_HWCSUM + TSO4 VLAN_HWTSO) + assert_equal(expected.sort, @cfg['igb0'].capabilities.sort) + end + end diff --git a/test/unit/tc_netbsd.rb b/test/unit/tc_netbsd.rb index 19e461e..e759bdd 100644 --- a/test/unit/tc_netbsd.rb +++ b/test/unit/tc_netbsd.rb @@ -39,4 +39,9 @@ def test_attribs end + def test_capabilities + expected = %w(UDP4CSUM_Rx UDP4CSUM_Tx) + assert_equal(expected.sort, @cfg['cs0'].capabilities.sort) + end + end From 215b481fc3827a72646ba49f663bdd13117b77d9 Mon Sep 17 00:00:00 2001 From: Alan Somers Date: Wed, 2 Apr 2014 14:32:02 -0600 Subject: [PATCH 16/19] Fix Ruby warnings. --- lib/ifconfig/bsd/interface_types.rb | 9 ++++++--- lib/ifconfig/common/interface_types.rb | 3 +++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/ifconfig/bsd/interface_types.rb b/lib/ifconfig/bsd/interface_types.rb index 391ac59..9589943 100644 --- a/lib/ifconfig/bsd/interface_types.rb +++ b/lib/ifconfig/bsd/interface_types.rb @@ -57,8 +57,8 @@ def parse_flags(line) def add_network(line) case line when /^\s+inet\s/ - addr,mask = line.match(/\s+([\d\d?\d?\.]{4,})\s+netmask\s+(\S+)/i)[1..2] - bcast = line.match(/\s+broadcast\s([\d\d?\d?\.]{4,})/i) + addr,mask = line.match(/\s+((\d{1,3}\.){3,}\d{1,3})\s+netmask\s+(\S+)/i)[1..2] + bcast = line.match(/\s+broadcast\s([{1,3}d?\.]{4,})/i) bcast = bcast[1] unless bcast.nil? @networks['inet'] = Ipv4Network.new(addr, mask, bcast) when /^\s+inet6\s/ @@ -79,7 +79,7 @@ def add_network(line) class EthernetAdapter # Parses the "media: Ethernet autoselect (1000baseT )" line def parse_media(line) - match = line.match /Ethernet (autoselect )?(\()?(\S+?)[ )]/ + match = line.match(/Ethernet (autoselect )?(\()?(\S+?)[ )]/) if match @media = match[3] end @@ -101,6 +101,9 @@ def set_mac class LinkAggregation def parse_ifconfig(netstattxt=nil) + if @lagg_children.nil? + @lagg_children = [] + end super(netstattxt) @ifconfig.split("\n").each { |line| proto_match = line.match( /^\s*laggproto\s*(\S+)\s*/ ) diff --git a/lib/ifconfig/common/interface_types.rb b/lib/ifconfig/common/interface_types.rb index a603b12..dcb9e44 100644 --- a/lib/ifconfig/common/interface_types.rb +++ b/lib/ifconfig/common/interface_types.rb @@ -12,6 +12,9 @@ def initialize(name, ifacetxt, netstattxt=nil) 'EtherTalk Phase 2'].join("|") @networks = {} @flags = [] + @media = nil + @laggproto = nil + @lagg_children = nil @capabilities = [] @mtu = nil @metric = nil From 5a36930a842120b796f309f94400aab137dcfb08 Mon Sep 17 00:00:00 2001 From: jfqd Date: Tue, 5 Apr 2016 16:03:38 +0200 Subject: [PATCH 17/19] fix test files list to get gem build passing --- ruby-ifconfig.gemspec | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) mode change 100644 => 100755 ruby-ifconfig.gemspec diff --git a/ruby-ifconfig.gemspec b/ruby-ifconfig.gemspec old mode 100644 new mode 100755 index 8884090..83613a7 --- a/ruby-ifconfig.gemspec +++ b/ruby-ifconfig.gemspec @@ -36,11 +36,8 @@ Gem::Specification.new do |s| ] s.test_files = %w[ - test/test_dragonflybsd.rb - test/test_openbsd.rb - test/test_darwin.rb - test/test_bsd.rb - test/test_sunos.rb + test/selftest.rb + test/test_helper.rb test/unit/tc_openbsd.rb test/unit/tc_dragonflybsd.rb test/unit/tc_linux.rb @@ -49,10 +46,6 @@ Gem::Specification.new do |s| test/unit/tc_darwin.rb test/unit/tc_sunos.rb test/unit/tc_osx.rb - test/test_helper.rb - test/test_netbsd.rb - test/test_linux.rb - test/test_osx.rb ifconfig_examples/darwin.txt ifconfig_examples/dragonflybsd.txt ifconfig_examples/dragonflybsd_netstat.txt From 81651c4699e0bbfba81d7626b29c5109caebd062 Mon Sep 17 00:00:00 2001 From: jfqd Date: Tue, 5 Apr 2016 16:25:10 +0200 Subject: [PATCH 18/19] virtual ip addresses on solarish systems might miss ether part --- ifconfig_examples/sunos.txt | 6 ++++-- lib/ifconfig/sunos/ifconfig.rb | 3 ++- test/unit/tc_sunos.rb | 7 ++++--- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/ifconfig_examples/sunos.txt b/ifconfig_examples/sunos.txt index 39321e4..2f105ed 100644 --- a/ifconfig_examples/sunos.txt +++ b/ifconfig_examples/sunos.txt @@ -1,10 +1,12 @@ lo0: flags=1000849 mtu 8232 index 1 inet 127.0.0.1 netmask ff000000 inet6 ::1/128 -bge0: flags=1000843 mtu 1500 index 2 +bge0: flags=1000843 mtu 1500 index 2 inet 10.32.4.138 netmask ffffff00 broadcast 10.32.4.255 ether 0:3:ba:42:9d:ef inet6 fe80::a00:20ff:fe72:9724/10 -le1: flags=1000843 mtu 1500 index 3 +bge0:1: flags=1000843 mtu 1500 index 2 + inet 10.32.4.139 netmask ffffff00 broadcast 10.32.4.255 +le1: flags=1000843 mtu 1500 index 3 inet 172.16.254.99 netmask ffff0000 broadcast 172.16.255.255 ether 8:0:20:1d:71:eb diff --git a/lib/ifconfig/sunos/ifconfig.rb b/lib/ifconfig/sunos/ifconfig.rb index 36f59bb..76a665f 100644 --- a/lib/ifconfig/sunos/ifconfig.rb +++ b/lib/ifconfig/sunos/ifconfig.rb @@ -31,7 +31,8 @@ def initialize(input=nil,netstat=nil,verbose=nil) when /\s+ether\s+/im @ifaces[iface_name] = EthernetAdapter.new(iface_name,iface,netstat) else - puts "Unknown Adapter Type: #{iface}" if @verbose + # fallback for virtual nics like bge:0 + @ifaces[iface_name] = EthernetAdapter.new(iface_name,iface,netstat) end end end diff --git a/test/unit/tc_sunos.rb b/test/unit/tc_sunos.rb index cc85ca0..eaec40f 100644 --- a/test/unit/tc_sunos.rb +++ b/test/unit/tc_sunos.rb @@ -10,12 +10,12 @@ def setup end def test_interface_list - assert(@cfg.interfaces.sort == ["le1", "lo0", "bge0"].sort, + assert(@cfg.interfaces.sort == ["le1", "lo0", "bge0", "bge0:1"].sort, "Failed to parse all interfaces") end def test_mac_parse - assert(@cfg['bge0'].mac == "0:3:ba:42:9d:ef", + assert(@cfg['bge0'].mac == "0:3:ba:42:9d:ef", "Failed to parse MAC address: "+@cfg['bge0'].mac) end @@ -23,6 +23,7 @@ def test_flags assert(@cfg['bge0'].flags.include?('BROADCAST') && @cfg['bge0'].flags.include?('RUNNING') && @cfg['bge0'].flags.include?('MULTICAST') && + @cfg['bge0'].flags.include?('ROUTER') && @cfg['bge0'].up?, "FLAG Parsing failed: #{@cfg['bge0'].flags}") end @@ -40,7 +41,7 @@ def test_networks end def test_attribs - assert(@cfg['bge0'].rx['bytes'].class == Fixnum || NilClass&& + assert(@cfg['bge0'].rx['bytes'].class == Fixnum || NilClass && @cfg['bge0'].tx['bytes'].class == Fixnum || NilClass, "Wrong class") end From 4808b13d93cdb0a2e44898e9b2c1b100b18bc4fa Mon Sep 17 00:00:00 2001 From: jfqd Date: Tue, 5 Apr 2016 22:24:37 +0200 Subject: [PATCH 19/19] virtual nics do not have an ether part --- lib/ifconfig/sunos/ifconfig.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ifconfig/sunos/ifconfig.rb b/lib/ifconfig/sunos/ifconfig.rb index 76a665f..4bee92d 100644 --- a/lib/ifconfig/sunos/ifconfig.rb +++ b/lib/ifconfig/sunos/ifconfig.rb @@ -32,7 +32,7 @@ def initialize(input=nil,netstat=nil,verbose=nil) @ifaces[iface_name] = EthernetAdapter.new(iface_name,iface,netstat) else # fallback for virtual nics like bge:0 - @ifaces[iface_name] = EthernetAdapter.new(iface_name,iface,netstat) + @ifaces[iface_name] = NetworkAdapter.new(iface_name,iface,netstat) end end end