TEST_F(EthernetIITest, SerializeSmallEthernetWithPadding) { EthernetII eth(smallip_packet, sizeof(smallip_packet)); ASSERT_TRUE(eth.inner_pdu() != NULL); PDU::serialization_type serialized = eth.serialize(); EXPECT_EQ(serialized.size(), sizeof(smallip_packet)); EXPECT_TRUE(std::equal(serialized.begin(), serialized.end(), smallip_packet)); }
void PacketSender::send_l3(PDU &pdu, struct sockaddr* link_addr, uint32_t len_addr, SocketType type) { open_l3_socket(type); int sock = _sockets[type]; PDU::serialization_type buffer = pdu.serialize(); if(sendto(sock, (const char*)&buffer[0], buffer.size(), 0, link_addr, len_addr) == -1) throw socket_write_error(make_error_string()); }
TEST_F(TCPTest, ConstructorFromBuffer) { TCP tcp1(expected_packet, sizeof(expected_packet)); EXPECT_EQ(tcp1.dport(), 0x4f1d); EXPECT_EQ(tcp1.sport(), 0x7f4d); EXPECT_EQ(tcp1.seq(), 0xf1dae546); EXPECT_EQ(tcp1.ack_seq(), 0x5faed123U); EXPECT_EQ(tcp1.window(), 0x71da); EXPECT_EQ(tcp1.urg_ptr(), 0x1fae); EXPECT_EQ(tcp1.data_offset(), 0xd); EXPECT_EQ(tcp1.timestamp(), (std::pair<uint32_t, uint32_t>(0x4fd23acb, 0x89fe1234))); EXPECT_TRUE(tcp1.has_sack_permitted()); EXPECT_EQ(tcp1.winscale(), 0x7a); EXPECT_EQ(tcp1.mss(), 0x98fa); TCP::sack_type edges = tcp1.sack(); TCP::sack_type::const_iterator iter = edges.begin(); ASSERT_EQ(edges.size(), 2U); EXPECT_EQ(*iter++, 0x00010203U); EXPECT_EQ(*iter++, 0x04050607U); PDU::serialization_type buffer = tcp1.serialize(); TCP tcp2(&buffer[0], buffer.size()); test_equals(tcp1, tcp2); }
TEST_F(EthernetIITest, Serialize) { EthernetII eth(dst_addr, src_addr); eth.payload_type(p_type); PDU::serialization_type serialized = eth.serialize(); ASSERT_EQ(serialized.size(), sizeof(expected_packet)); EXPECT_TRUE(std::equal(serialized.begin(), serialized.end(), expected_packet)); }
TEST_F(LoopbackTest, MatchesResponse) { Loopback loop1 = Loopback() / IP("192.168.0.1", "192.168.0.2") / TCP(22, 21); loop1.family(PF_INET); Loopback loop2 = Loopback() / IP("192.168.0.2", "192.168.0.1") / TCP(21, 22); loop2.family(PF_INET); PDU::serialization_type buffer = loop2.serialize(); EXPECT_TRUE(loop1.matches_response(&buffer[0], buffer.size())); }
TEST_F(Dot1QTest, SerializeAfterInnerPduRemoved) { EthernetII eth1 = EthernetII() / Dot1Q() / IP(); eth1.serialize(); eth1.rfind_pdu<Dot1Q>().inner_pdu(0); PDU::serialization_type buffer = eth1.serialize(); EthernetII eth2(&buffer[0], buffer.size()); EXPECT_EQ(eth1.size(), eth2.size()); }
// Make sure that a big payload is not considered ICMP extensions TEST_F(IPv6Test, BigEncapsulatedPacketIsNotConsideredToHaveExtensions) { IPv6 encapsulated = IPv6(TINS_DEFAULT_TEST_IP) / UDP(99, 12) / RawPDU(std::string(250, 'A')); EthernetII pkt = EthernetII() / IPv6() / ICMPv6(ICMPv6::TIME_EXCEEDED) / encapsulated; PDU::serialization_type buffer = pkt.serialize(); EthernetII serialized(&buffer[0], buffer.size()); ASSERT_EQ(encapsulated.size(), serialized.rfind_pdu<RawPDU>().payload().size()); ASSERT_TRUE(serialized.rfind_pdu<ICMPv6>().extensions().extensions().empty()); }
TEST_F(Dot1QTest, SerializeCraftedPacket) { EthernetII pkt = EthernetII() / Dot1Q(10) / IP("192.168.1.2") / TCP(23, 45) / RawPDU("asdasdasd"); PDU::serialization_type buffer = pkt.serialize(); EXPECT_EQ(buffer[12], 0x81); EXPECT_EQ(buffer[13], 0x00); EthernetII pkt2(&buffer[0], buffer.size()); const Dot1Q &q1 = pkt2.rfind_pdu<Dot1Q>(); EXPECT_EQ(10, q1.id()); }
TEST_F(DHCPTest, Serialize) { DHCP dhcp1(expected_packet, sizeof(expected_packet)); PDU::serialization_type buffer = dhcp1.serialize(); ASSERT_EQ(buffer.size(), sizeof(expected_packet)); EXPECT_TRUE(std::equal(buffer.begin(), buffer.end(), expected_packet)); DHCP dhcp2(&buffer[0], (uint32_t)buffer.size()); test_equals(dhcp1, dhcp2); }
TEST_F(UDPTest, ChecksumCheck) { EthernetII pkt1(checksum_packet, sizeof(checksum_packet)); const UDP &udp1 = pkt1.rfind_pdu<UDP>(); uint16_t checksum = udp1.checksum(); PDU::serialization_type buffer = pkt1.serialize(); EthernetII pkt2(&buffer[0], (uint32_t)buffer.size()); const UDP &udp2 = pkt2.rfind_pdu<UDP>(); EXPECT_EQ(checksum, udp2.checksum()); EXPECT_EQ(udp1.checksum(), udp2.checksum()); }
TEST_F(SNAPTest, ConstructorFromBuffer) { SNAP snap1(expected_packet, sizeof(expected_packet)); PDU::serialization_type buffer = snap1.serialize(); EXPECT_EQ(3, snap1.control()); EXPECT_EQ(0xaa, snap1.dsap()); EXPECT_EQ(0xaa, snap1.ssap()); EXPECT_EQ(0x0800, snap1.eth_type()); EXPECT_EQ(1U, snap1.org_code()); SNAP snap2(&buffer[0], (uint32_t)buffer.size()); test_equals(snap1, snap2); }
TEST_F(PPPoETest, StackedOnEthernetSerializationWithTags) { PPPoE pdu(expected_packet, sizeof(expected_packet)); EthernetII eth = EthernetII() / pdu; PDU::serialization_type buffer = eth.serialize(); EthernetII eth2(&buffer[0], (uint32_t)buffer.size()); PPPoE* unserialized = eth2.find_pdu<PPPoE>(); ASSERT_TRUE(unserialized != NULL); EXPECT_EQ( PPPoE::serialization_type(expected_packet, expected_packet + sizeof(expected_packet)), unserialized->serialize() ); }
void PacketSender::send_l2(PDU &pdu, struct sockaddr* link_addr, uint32_t len_addr, const NetworkInterface &iface) { int sock = get_ether_socket(iface); PDU::serialization_type buffer = pdu.serialize(); if(!buffer.empty()) { #if defined(BSD) || defined(__FreeBSD_kernel__) if(::write(sock, &buffer[0], buffer.size()) == -1) #else if(::sendto(sock, &buffer[0], buffer.size(), 0, link_addr, len_addr) == -1) #endif throw socket_write_error(make_error_string()); } }
// Use a large buffer. This wil set the length field TEST_F(IPv6Test, SerializePacketHavingICMPExtensionsWithLengthAndLotsOfPayload) { IPv6 encapsulated = IPv6(TINS_DEFAULT_TEST_IP) / UDP(99, 12) / RawPDU(std::string(250, 'A')); EthernetII pkt = EthernetII() / IPv6() / ICMPv6(ICMPv6::TIME_EXCEEDED) / encapsulated; const uint8_t payload[] = { 24, 150, 1, 1 }; ICMPExtension extension(1, 1); ICMPExtension::payload_type ext_payload(payload, payload + sizeof(payload)); extension.payload(ext_payload); pkt.rfind_pdu<ICMPv6>().extensions().add_extension(extension); PDU::serialization_type buffer = pkt.serialize(); EthernetII serialized(&buffer[0], buffer.size()); ASSERT_EQ(1, serialized.rfind_pdu<ICMPv6>().extensions().extensions().size()); EXPECT_EQ(ext_payload, serialized.rfind_pdu<ICMPv6>().extensions().extensions().begin()->payload()); }
TEST_F(ARPTest, ConstructorFromBuffer) { ARP arp1(expected_packet, sizeof(expected_packet)); PDU::serialization_type buffer = arp1.serialize(); ARP arp2(&buffer[0], (uint32_t)buffer.size()); EXPECT_EQ(arp1.opcode(), arp2.opcode()); ASSERT_EQ(arp1.hw_addr_length(), arp2.hw_addr_length()); EXPECT_EQ(arp1.hw_addr_format(), arp2.hw_addr_format()); ASSERT_EQ(arp1.prot_addr_length(), arp2.prot_addr_length()); EXPECT_EQ(arp1.prot_addr_format(), arp2.prot_addr_format()); EXPECT_EQ(arp1.sender_ip_addr(), arp2.sender_ip_addr()); EXPECT_EQ(arp1.target_ip_addr(), arp2.target_ip_addr()); EXPECT_EQ(arp1.sender_hw_addr(), arp2.sender_hw_addr()); EXPECT_EQ(arp1.target_hw_addr(), arp2.target_hw_addr()); }
TEST_F(UDPTest, ConstructorFromBuffer) { UDP udp1(expected_packet, sizeof(expected_packet)); PDU::serialization_type buffer = udp1.serialize(); EXPECT_EQ(buffer.size(), sizeof(expected_packet)); EXPECT_EQ(udp1.dport(), 0x47f1); EXPECT_EQ(udp1.sport(), 0xf51a); EXPECT_EQ(udp1.length(), 8); UDP udp2(&buffer[0], buffer.size()); EXPECT_EQ(udp1.dport(), udp2.dport()); EXPECT_EQ(udp1.sport(), udp2.sport()); EXPECT_EQ(udp1.length(), udp2.length()); EXPECT_EQ(udp1.size(), udp2.size()); EXPECT_EQ(udp1.header_size(), udp2.header_size()); }
void PacketWriter::write(PDU &pdu) { PDU::serialization_type buffer = pdu.serialize(); timeval tm; #ifndef _WIN32 gettimeofday(&tm, 0); #else // fixme tm = timeval(); #endif struct pcap_pkthdr header = { tm, static_cast<bpf_u_int32>(buffer.size()), static_cast<bpf_u_int32>(buffer.size()) }; pcap_dump((u_char*)dumper, &header, &buffer[0]); }
TEST_F(Dot1QTest, QinQ) { EthernetII pkt = EthernetII() / Dot1Q(10) / Dot1Q(42) / IP("192.168.1.2") / TCP(23, 45) / RawPDU("asdasdasd"); PDU::serialization_type buffer = pkt.serialize(); // First PID of the serialized packet should be Dot1AD = 0x88a8 EXPECT_EQ(buffer[12], 0x88); EXPECT_EQ(buffer[13], 0xa8); // Second PID of the serialized packet chouls be Dot1Q = 0x8100 EXPECT_EQ(buffer[16], 0x81); EXPECT_EQ(buffer[17], 0x00); EthernetII pkt2(&buffer[0], buffer.size()); const Dot1Q& q1 = pkt2.rfind_pdu<Dot1Q>(); ASSERT_TRUE(q1.inner_pdu() != NULL); const Dot1Q& q2 = q1.inner_pdu()->rfind_pdu<Dot1Q>(); EXPECT_EQ(10, q1.id()); EXPECT_EQ(42, q2.id()); }
TEST_F(Dot11BeaconTest, PCAPLoad1) { const uint8_t buffer[] = { 128, 0, 0, 0, 255, 255, 255, 255, 255, 255, 244, 236, 56, 254, 77, 146, 244, 236, 56, 254, 77, 146, 224, 234, 128, 209, 212, 206, 44, 0, 0, 0, 100, 0, 49, 4, 0, 7, 83, 101, 103, 117, 110, 100, 111, 1, 8, 130, 132, 139, 150, 12, 18, 24, 36, 3, 1, 1, 5, 4, 0, 1, 0, 0, 7, 6, 85, 83, 32, 1, 13, 20, 42, 1, 0, 48, 20, 1, 0, 0, 15, 172, 4, 1, 0, 0, 15, 172, 4, 1, 0, 0, 15, 172, 2, 0, 0, 50, 4, 48, 72, 96, 108, 221, 24, 0, 80, 242, 2, 1, 1, 3, 0, 3, 164, 0, 0, 39, 164, 0, 0, 66, 67, 94, 0, 98, 50, 47, 0, 221, 9, 0, 3, 127, 1, 1, 0, 0, 255, 127 }; typedef byte_array country_container; Dot11Beacon dot11(buffer, sizeof(buffer)); float rates[] = { 1.0f, 2.0f, 5.5f, 11.0f, 6.0f, 9.0f, 12.0f, 18.0f}, ext_rates[] = { 24.0f, 36.0f, 48.0f, 54.0f }; Dot11Beacon::rates_type rates_parsed = dot11.supported_rates(); Dot11Beacon::rates_type ext_rates_parsed = dot11.extended_supported_rates(); Dot11Beacon::tim_type tim(0, 1, 0, byte_array(1)), tim_parsed = dot11.tim(); Dot11Beacon::country_params country("US ", country_container(1, 1), country_container(1, 13), country_container(1, 20)), country_parsed = dot11.country(); EXPECT_EQ(dot11.ssid(), "Segundo"); ASSERT_EQ(rates_parsed.size(), sizeof(rates) / sizeof(float)); EXPECT_TRUE(std::equal(rates_parsed.begin(), rates_parsed.end(), rates)); ASSERT_EQ(ext_rates_parsed.size(), sizeof(ext_rates) / sizeof(float)); EXPECT_TRUE(std::equal(ext_rates_parsed.begin(), ext_rates_parsed.end(), ext_rates)); EXPECT_EQ(1, dot11.ds_parameter_set()); EXPECT_EQ(tim.dtim_count, tim_parsed.dtim_count); EXPECT_EQ(tim.dtim_period, tim_parsed.dtim_period); EXPECT_EQ(tim.bitmap_control, tim_parsed.bitmap_control); EXPECT_EQ(tim.partial_virtual_bitmap, tim_parsed.partial_virtual_bitmap); EXPECT_EQ(country.country, country_parsed.country); EXPECT_EQ(country.first_channel, country_parsed.first_channel); EXPECT_EQ(country.number_channels, country_parsed.number_channels); EXPECT_EQ(country.max_transmit_power, country_parsed.max_transmit_power); EXPECT_EQ(dot11.erp_information(), 0); PDU::serialization_type serialized = dot11.serialize(); ASSERT_EQ(sizeof(buffer), serialized.size()); EXPECT_TRUE(std::equal(serialized.begin(), serialized.end(), buffer)); }
PDU *IPv4Stream::allocate_pdu() const { PDU::serialization_type buffer; buffer.reserve(total_size); // Check if we actually have all the data we need. Otherwise return nullptr; uint16_t expected = 0; for(fragments_type::const_iterator it = fragments.begin(); it != fragments.end(); ++it) { if(expected != it->offset()) return 0; expected = static_cast<uint16_t>(it->offset() + it->payload().size()); buffer.insert(buffer.end(), it->payload().begin(), it->payload().end()); } return Internals::pdu_from_flag( static_cast<Constants::IP::e>(transport_proto), buffer.empty() ? 0 : &buffer[0], static_cast<uint32_t>(buffer.size()) ); }
TEST_F(Dot11PSPollTest, Serialize) { Dot11PSPoll pdu(expected_packet, sizeof(expected_packet)); PDU::serialization_type buffer = pdu.serialize(); ASSERT_EQ(sizeof(expected_packet), buffer.size()); EXPECT_TRUE(std::equal(buffer.begin(), buffer.end(), expected_packet)); }
bool OfflinePacketFilter::matches_filter(PDU& pdu) const { PDU::serialization_type buffer = pdu.serialize(); return matches_filter(&buffer[0], static_cast<uint32_t>(buffer.size())); }
TEST_F(TCPTest, Serialize) { TCP tcp1(expected_packet, sizeof(expected_packet)); PDU::serialization_type buffer = tcp1.serialize(); ASSERT_EQ(buffer.size(), sizeof(expected_packet)); EXPECT_TRUE(std::equal(buffer.begin(), buffer.end(), expected_packet)); }
TEST_F(PPPoETest, StackedOnEthernet) { EthernetII eth = EthernetII() / PPPoE(); PDU::serialization_type buffer = eth.serialize(); EthernetII eth2(&buffer[0], (uint32_t)buffer.size()); ASSERT_TRUE(eth2.find_pdu<PPPoE>() != NULL); }