1
0
mirror of https://github.com/mfontanini/libtins synced 2026-01-26 03:51:35 +01:00

Added support for IPv6 extension headers. Specific headers getters and setters are not yet implemented.

This commit is contained in:
Matias Fontanini
2012-11-22 17:18:59 -03:00
parent 8276e7d086
commit a938d2ecfd
6 changed files with 245 additions and 27 deletions

View File

@@ -6,6 +6,7 @@
#include "tcp.h"
#include "udp.h"
#include "icmp.h"
#include "rawpdu.h"
#include "ipv6_address.h"
#include "utils.h"
@@ -14,12 +15,12 @@ using namespace Tins;
class IPv6Test : public testing::Test {
public:
static const uint8_t expected_packet[];
static const uint8_t expected_packet1[], expected_packet2[];
void test_equals(const IPv6 &ip1, const IPv6 &ip2);
void test_equals(IPv6 &ip1, IPv6 &ip2);
};
const uint8_t IPv6Test::expected_packet[] = {
const uint8_t IPv6Test::expected_packet1[] = {
'i', '\xa8', '\'', '4', '\x00', '(', '\x06', '@', '\x00', '\x00',
'\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00',
'\x00', '\x00', '\x00', '\x00', '\x01', '\x00', '\x00', '\x00', '\x00',
@@ -31,6 +32,45 @@ const uint8_t IPv6Test::expected_packet[] = {
'\x00', '\x00', '\x01', '\x03', '\x03', '\x07'
};
const uint8_t IPv6Test::expected_packet2[] = {
'`', '\x00', '\x00', '\x00', '\x00', '$', '\x00', '\x01', '\xfe',
'\x80', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x02',
'\xd0', '\t', '\xff', '\xfe', '\xe3', '\xe8', '\xde', '\xff', '\x02',
'\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00',
'\x00', '\x00', '\x00', '\x00', '\x16', ':', '\x00', '\x05', '\x02',
'\x00', '\x00', '\x01', '\x00', '\x8f', '\x00', 't', '\xfe', '\x00',
'\x00', '\x00', '\x01', '\x04', '\x00', '\x00', '\x00', '\xff', '\x02',
'\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00',
'\x01', '\xff', '\x98', '\x06', '\xe1'
};
void IPv6Test::test_equals(IPv6 &ip1, IPv6 &ip2) {
EXPECT_EQ(ip1.version(), ip2.version());
EXPECT_EQ(ip1.traffic_class(), ip2.traffic_class());
EXPECT_EQ(ip1.flow_label(), ip2.flow_label());
EXPECT_EQ(ip1.payload_length(), ip2.payload_length());
EXPECT_EQ(ip1.next_header(), ip2.next_header());
EXPECT_EQ(ip1.hop_limit(), ip2.hop_limit());
EXPECT_EQ(ip1.dst_addr(), ip2.dst_addr());
EXPECT_EQ(ip1.src_addr(), ip2.src_addr());
EXPECT_EQ(bool(ip1.search_option(IPv6::HOP_BY_HOP)), bool(ip2.search_option(IPv6::HOP_BY_HOP)));
const IPv6::ipv6_ext_header *header1 = ip1.search_option(IPv6::HOP_BY_HOP),
*header2 = ip2.search_option(IPv6::HOP_BY_HOP);
if(header1 && header2) {
EXPECT_EQ(header1->data_size(), header2->data_size());
}
EXPECT_EQ(bool(ip1.inner_pdu()), bool(ip2.inner_pdu()));
const RawPDU *raw1 = ip1.find_pdu<RawPDU>(), *raw2 = ip2.find_pdu<RawPDU>();
ASSERT_EQ(bool(raw1), bool(raw2));
if(raw1) {
EXPECT_EQ(raw1->payload(), raw2->payload());
}
}
TEST_F(IPv6Test, Constructor) {
IPv6 ipv6("::1:2:3", "f0aa:beef::1");
EXPECT_EQ(ipv6.version(), 6);
@@ -44,7 +84,7 @@ TEST_F(IPv6Test, Constructor) {
}
TEST_F(IPv6Test, ConstructorFromBuffer) {
IPv6 ipv6(expected_packet, sizeof(expected_packet));
IPv6 ipv6(expected_packet1, sizeof(expected_packet1));
EXPECT_EQ(ipv6.version(), 6);
EXPECT_EQ(ipv6.traffic_class(), 0x9a);
EXPECT_EQ(ipv6.flow_label(), 0x82734);
@@ -60,6 +100,36 @@ TEST_F(IPv6Test, ConstructorFromBuffer) {
EXPECT_EQ(tcp->dport(), 80);
}
// This one has a hop-by-hop ext header
TEST_F(IPv6Test, ConstructorFromBuffer2) {
IPv6 ipv6(expected_packet2, sizeof(expected_packet2));
EXPECT_EQ(ipv6.version(), 6);
EXPECT_EQ(ipv6.traffic_class(), 0);
EXPECT_EQ(ipv6.flow_label(), 0);
EXPECT_EQ(ipv6.payload_length(), 36);
EXPECT_EQ(ipv6.next_header(), IPv6::HOP_BY_HOP);
EXPECT_EQ(ipv6.hop_limit(), 1);
EXPECT_EQ(ipv6.dst_addr(), "ff02::16");
EXPECT_EQ(ipv6.src_addr(), "fe80::2d0:9ff:fee3:e8de");
// This will have to be changed when ICMPv6 is implemented
RawPDU *pdu = ipv6.find_pdu<RawPDU>();
ASSERT_TRUE(pdu);
EXPECT_EQ(pdu->payload_size(), 28);
const IPv6::ipv6_ext_header *header = ipv6.search_option(IPv6::HOP_BY_HOP);
ASSERT_TRUE(header);
EXPECT_EQ(header->data_size(), 6);
}
TEST_F(IPv6Test, Serialize) {
IPv6 ip1(expected_packet2, sizeof(expected_packet2));
IPv6::serialization_type buffer = ip1.serialize();
ASSERT_EQ(buffer.size(), sizeof(expected_packet2));
EXPECT_TRUE(std::equal(buffer.begin(), buffer.end(), expected_packet2));
IPv6 ip2(&buffer[0], buffer.size());
test_equals(ip1, ip2);
}
TEST_F(IPv6Test, Version) {
IPv6 ipv6;
ipv6.version(3);