$:.unshift File.join(File.dirname(__FILE__), "..", "lib") require 'test/unit' require 'libnet4r' class TC_Libnet_IPv4 < Test::Unit::TestCase def test_unpack ip = [ 0x45, 0x12, 0x00, 0x40, 0x43, 0x21, 0x00, 0x00, 0x48, 0x11, 0xac, 0x24, 0xc0, 0xa8, 0x01, 0x02, 0xc0, 0xa8, 0x01, 0x03, 0x74, 0x68, 0x65, 0x20, 0x71, 0x75, 0x69, 0x63, 0x6b, 0x20, 0x62, 0x72, 0x6f, 0x77, 0x6e, 0x20, 0x66, 0x6f, 0x78, 0x20, 0x6a, 0x75, 0x6d, 0x70, 0x65, 0x64, 0x20, 0x6f, 0x76, 0x65, 0x72, 0x20, 0x74, 0x68, 0x65, 0x20, 0x6c, 0x61, 0x7a, 0x79, 0x20, 0x64, 0x6f, 0x67 ].pack("C*") i = Libnet::IPv4.decode(ip) assert_equal(4, i.version) assert_equal(5, i.ihl) assert_equal(0x12, i.tos) assert_equal(0x40, i.length) assert_equal(0x4321, i.id) assert_equal(0x0, i.frag_off) assert_equal(72, i.ttl) assert_equal(Libnet::IPPROTO_UDP, i.protocol) assert_equal(0x24, i.checksum) assert_equal(0xc0a80102, i.src_ip) assert_equal(0xc0a80103, i.dst_ip) assert_equal("the quick brown fox jumped over the lazy dog", i.payload) assert_raise ArgumentError do Libnet::IPv4.decode("foobar") end end def test_pack ip = [ 0x45, 0x12, 0x00, 0x40, 0x43, 0x21, 0x00, 0x00, 0x48, 0x11, 0xac, 0x24, 0xc0, 0xa8, 0x01, 0x02, 0xc0, 0xa8, 0x01, 0x03, 0x74, 0x68, 0x65, 0x20, 0x71, 0x75, 0x69, 0x63, 0x6b, 0x20, 0x62, 0x72, 0x6f, 0x77, 0x6e, 0x20, 0x66, 0x6f, 0x78, 0x20, 0x6a, 0x75, 0x6d, 0x70, 0x65, 0x64, 0x20, 0x6f, 0x76, 0x65, 0x72, 0x20, 0x74, 0x68, 0x65, 0x20, 0x6c, 0x61, 0x7a, 0x79, 0x20, 0x64, 0x6f, 0x67 ].pack("C*") payload = "the quick brown fox jumped over the lazy dog" i = Libnet::IPv4.new i.tos = 0x12 i.length = Libnet::HL_IPV4 + payload.length i.id = 0x4321 i.frag_off = 0 i.ttl = 72 i.protocol = Libnet::IPPROTO_UDP i.checksum = 0 i.src_ip = '192.168.1.2' i.dst_ip = 0xc0a80103 i.payload = payload l = Libnet.new assert_nil(i.ptag) l.build_ipv4(i) assert(i.ptag > 0) assert_equal(ip, l.pack) l = Libnet.new i = nil l.build_ipv4 do |i| i.tos = 0x12 i.length = Libnet::HL_IPV4 + payload.length i.id = 0x4321 i.frag_off = 0 i.ttl = 72 i.protocol = Libnet::IPPROTO_UDP i.checksum = 0 i.src_ip = '192.168.1.2' i.dst_ip = 0xc0a80103 i.payload = payload end assert(i.ptag > 0) assert_equal(ip, l.pack) end def test_modify_ptag ip1 = [ 0x45, 0x12, 0x00, 0x40, 0x43, 0x21, 0x00, 0x00, 0x48, 0x11, 0xac, 0x24, 0xc0, 0xa8, 0x01, 0x02, 0xc0, 0xa8, 0x01, 0x03, 0x74, 0x68, 0x65, 0x20, 0x71, 0x75, 0x69, 0x63, 0x6b, 0x20, 0x62, 0x72, 0x6f, 0x77, 0x6e, 0x20, 0x66, 0x6f, 0x78, 0x20, 0x6a, 0x75, 0x6d, 0x70, 0x65, 0x64, 0x20, 0x6f, 0x76, 0x65, 0x72, 0x20, 0x74, 0x68, 0x65, 0x20, 0x6c, 0x61, 0x7a, 0x79, 0x20, 0x64, 0x6f, 0x67 ].pack("C*") ip2 = [ 0x45, 0x12, 0x00, 0x40, 0x43, 0x21, 0x00, 0x00, 0x48, 0x11, 0xac, 0x23, 0xc0, 0xa8, 0x01, 0x02, 0xc0, 0xa8, 0x01, 0x04, 0x74, 0x68, 0x65, 0x20, 0x71, 0x75, 0x69, 0x63, 0x6b, 0x20, 0x62, 0x72, 0x6f, 0x77, 0x6e, 0x20, 0x66, 0x6f, 0x78, 0x20, 0x6a, 0x75, 0x6d, 0x70, 0x65, 0x64, 0x20, 0x6f, 0x76, 0x65, 0x72, 0x20, 0x74, 0x68, 0x65, 0x20, 0x6c, 0x61, 0x7a, 0x79, 0x20, 0x64, 0x6f, 0x67 ].pack("C*") payload = "the quick brown fox jumped over the lazy dog" l = Libnet.new i = nil l.build_ipv4 do |i| i.tos = 0x12 i.length = Libnet::HL_IPV4 + payload.length i.id = 0x4321 i.frag_off = 0 i.ttl = 72 i.protocol = Libnet::IPPROTO_UDP i.checksum = 0 i.src_ip = '192.168.1.2' i.dst_ip = 0xc0a80103 i.payload = payload end assert(i.ptag > 0) assert_equal(ip1, l.pack) ptag = i.ptag i.dst_ip = '192.168.1.4' l.build_ipv4(i) assert_equal(ptag, i.ptag) assert_equal(ip2, l.pack) end def test_ethernet bytes = [ # ethernet 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x08, 0x00, #ipv4 0x45, 0x12, 0x00, 0x40, 0x43, 0x21, 0x00, 0x00, 0x48, 0x11, 0xac, 0x24, 0xc0, 0xa8, 0x01, 0x02, 0xc0, 0xa8, 0x01, 0x03, 0x74, 0x68, 0x65, 0x20, 0x71, 0x75, 0x69, 0x63, 0x6b, 0x20, 0x62, 0x72, 0x6f, 0x77, 0x6e, 0x20, 0x66, 0x6f, 0x78, 0x20, 0x6a, 0x75, 0x6d, 0x70, 0x65, 0x64, 0x20, 0x6f, 0x76, 0x65, 0x72, 0x20, 0x74, 0x68, 0x65, 0x20, 0x6c, 0x61, 0x7a, 0x79, 0x20, 0x64, 0x6f, 0x67 ].pack("C*") payload = "the quick brown fox jumped over the lazy dog" l = Libnet.new l.build_ipv4 do |i| i.tos = 0x12 i.length = Libnet::HL_IPV4 + payload.length i.id = 0x4321 i.frag_off = 0 i.ttl = 72 i.protocol = Libnet::IPPROTO_UDP i.checksum = 0 i.src_ip = '192.168.1.2' i.dst_ip = 0xc0a80103 i.payload = payload end l.build_ethernet do |e| e.dst = 'aa:bb:cc:dd:ee:ff' e.src = '11:22:33:44:55:66' e.type = 0x0800 end assert_equal(bytes, l.pack) end def test_vlan bytes = [ # vlan 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0xbe, 0xef, 0xca, 0xaa, 0x08, 0x00, #ipv4 0x45, 0x12, 0x00, 0x40, 0x43, 0x21, 0x00, 0x00, 0x48, 0x11, 0xac, 0x24, 0xc0, 0xa8, 0x01, 0x02, 0xc0, 0xa8, 0x01, 0x03, 0x74, 0x68, 0x65, 0x20, 0x71, 0x75, 0x69, 0x63, 0x6b, 0x20, 0x62, 0x72, 0x6f, 0x77, 0x6e, 0x20, 0x66, 0x6f, 0x78, 0x20, 0x6a, 0x75, 0x6d, 0x70, 0x65, 0x64, 0x20, 0x6f, 0x76, 0x65, 0x72, 0x20, 0x74, 0x68, 0x65, 0x20, 0x6c, 0x61, 0x7a, 0x79, 0x20, 0x64, 0x6f, 0x67 ].pack("C*") payload = "the quick brown fox jumped over the lazy dog" l = Libnet.new l.build_ipv4 do |i| i.tos = 0x12 i.length = Libnet::HL_IPV4 + payload.length i.id = 0x4321 i.frag_off = 0 i.ttl = 72 i.protocol = Libnet::IPPROTO_UDP i.checksum = 0 i.src_ip = '192.168.1.2' i.dst_ip = 0xc0a80103 i.payload = payload end l.build_vlan do |v| v.dst = 'aa:bb:cc:dd:ee:ff' v.src = '11:22:33:44:55:66' v.tpi = 0xbeef v.priority = 6 v.cfi = 0 v.id = 2730 v.type = 0x0800 end assert_equal(bytes, l.pack) end end