1
0
mirror of https://github.com/mfontanini/libtins synced 2026-01-23 02:35:57 +01:00

Added DHCPv6 initial support.

This commit is contained in:
Matias Fontanini
2013-01-16 20:15:04 -03:00
parent 9d0e84f1fa
commit 044d0a5a31
6 changed files with 2652 additions and 1 deletions

View File

@@ -108,6 +108,14 @@ include/pdu_option.h:
include/ethernetII.h:
include/network_interface.h:
src/dhcpv6.o: src/dhcpv6.cpp include/dhcpv6.h include/pdu.h \
include/small_uint.h
include/dhcpv6.h:
include/pdu.h:
include/small_uint.h:
src/dns.o: src/dns.cpp include/dns.h include/macros.h include/pdu.h \
include/endianness.h include/dns_record.h include/cxxstd.h \
include/ip_address.h include/ipv6_address.h

286
include/dhcpv6.h Normal file
View File

@@ -0,0 +1,286 @@
/*
* Copyright (c) 2012, Nasel
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#ifndef TINS_DHCPV6_H
#define TINS_DHCPV6_H
#include <list>
#include "pdu.h"
#include "endianness.h"
#include "small_uint.h"
#include "ipv6_address.h"
#include "pdu_option.h"
namespace Tins {
/**
* Represents a DHCPv6 PDU.
*/
class DHCPv6 : public PDU {
public:
/**
* Represents a DHCPv6 option.
*/
class dhcpv6_option {
public:
typedef std::vector<uint8_t> container_type;
typedef container_type::value_type data_type;
typedef uint16_t option_type;
/**
* \brief Constructs a PDUOption.
* \param opt The option type.
* \param length The option's data length.
* \param data The option's data(if any).
*/
dhcpv6_option(option_type opt = 0, size_t length = 0, const data_type *data = 0)
: option_(opt), option_size_(length) {
if(data)
value_.insert(value_.end(), data, data + length);
}
/**
* \brief Constructs a PDUOption from iterators, which
* indicate the data to be stored in it.
*
* \param opt The option type.
* \param start The beginning of the option data.
* \param end The end of the option data.
*/
template<typename ForwardIterator>
dhcpv6_option(option_type opt, ForwardIterator start, ForwardIterator end)
: option_(opt), option_size_(std::distance(start, end))
{
value_.insert(value_.end(), start, end);
}
/**
* Retrieves this option's type.
* \return uint8_t containing this option's size.
*/
uint16_t option() const {
return option_;
}
/**
* Sets this option's type
* \param opt The option type to be set.
*/
void option(uint16_t opt) {
option_ = opt;
}
/**
* Retrieves this option's data.
*
* If this method is called when data_size() == 0,
* dereferencing the returned pointer will result in undefined
* behaviour.
*
* \return const data_type& containing this option's value.
*/
const data_type *data_ptr() const {
return &*value_.begin();
}
/**
* Retrieves the length of this option's data.
*/
uint16_t data_size() const {
return option_size_;
}
private:
option_type option_;
uint16_t option_size_;
container_type value_;
};
/**
* The type used to store the DHCPv6 options.
*/
typedef std::list<dhcpv6_option> options_type;
/**
* The type used to store IP addresses.
*/
typedef IPv6Address ipaddress_type;
/**
* This PDU's flag.
*/
static const PDU::PDUType pdu_flag = PDU::DHCPv6;
/**
* Default constructor.
*/
DHCPv6();
/**
* \brief Constructor which constructs a DHCPv6 object from a buffer
* and adds all identifiable PDUs found in the buffer as children
* of this one.
* \param buffer The buffer from which this PDU will be constructed.
* \param total_sz The total size of the buffer.
*/
DHCPv6(const uint8_t *buffer, uint32_t total_sz);
// Getters
/**
* \brief Getter for the message type field.
*
* \return The stored message type field.
*/
uint8_t msg_type() const { return header_data[0]; }
/**
* \brief Getter for the hop count field.
*
* \return The stored hop count field.
*/
uint8_t hop_count() const { return header_data[1]; }
/**
* \brief Getter for the transaction id field.
*
* \return The stored transaction id field.
*/
small_uint<24> transaction_id() const {
return (header_data[1] << 16) | (header_data[2] << 8) | header_data[3];
}
/**
* \brief Getter for the peer address field.
*
* \return The stored peer address field.
*/
const ipaddress_type &peer_address() const { return peer_addr; }
/**
* \brief Getter for the link address field.
*
* \return The stored link address field.
*/
const ipaddress_type &link_address() const { return link_addr; }
// Setters
/**
* \brief Setter for the message type field.
*
* \param type The new message type.
*/
void msg_type(uint8_t type);
/**
* \brief Setter for the hop count field.
*
* \param count The new hop count.
*/
void hop_count(uint8_t count);
/**
* \brief Setter for the transaction id field.
*
* \param id The new transaction id.
*/
void transaction_id(small_uint<24> id);
/**
* \brief Setter for the peer address field.
*
* \param count The new peer address.
*/
void peer_address(const ipaddress_type &addr);
/**
* \brief Setter for the link address field.
*
* \param count The new link address.
*/
void link_address(const ipaddress_type &addr);
// Other stuff
/**
* Indicates whether this is a relay agent/server message
*/
bool is_relay_message() const;
/**
* \brief Adds a DHCPv6 option.
*
* The option is added after the last option in the option
* fields.
*
* \param option The option to be added
*/
void add_option(const dhcpv6_option &option);
/**
* \brief Searchs for an option that matchs the given flag.
*
* If the option is not found, a null pointer is returned.
* Deleting the returned pointer will result in <b>undefined
* behaviour</b>.
*
* \param id The option identifier to be searched.
*/
const dhcpv6_option *search_option(uint16_t id) const;
// PDU stuff
/**
* \brief Returns the header size.
*
* This metod overrides PDU::header_size. \sa PDU::header_size
*/
uint32_t header_size() const;
/**
* \brief Getter for the PDU's type.
* \sa PDU::pdu_type
*/
PDUType pdu_type() const { return pdu_flag; }
/**
* \sa PDU::clone
*/
DHCPv6 *clone() const {
return new DHCPv6(*this);
}
private:
void write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *);
uint8_t* write_option(const dhcpv6_option &option, uint8_t* buffer) const;
uint8_t header_data[4];
uint32_t options_size;
ipaddress_type link_addr, peer_addr;
options_type options_;
};
}
#endif // TINS_DHCPV6_H

View File

@@ -104,7 +104,8 @@ namespace Tins {
LOOPBACK,
IPv6,
ICMPv6,
SLL
SLL,
DHCPv6
};
/** \brief PDU constructor

147
src/dhcpv6.cpp Normal file
View File

@@ -0,0 +1,147 @@
/*
* Copyright (c) 2012, Nasel
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <iostream> //borrame
#include <vector>
#include <algorithm>
#include "dhcpv6.h"
namespace Tins {
DHCPv6::DHCPv6() : options_size() {
std::fill(header_data, header_data + sizeof(header_data), 0);
}
DHCPv6::DHCPv6(const uint8_t *buffer, uint32_t total_sz)
: options_size()
{
const char *err_msg = "Not enough size for a DHCPv6 header",
*opt_err_msg = "Not enough size for a DHCPv6 option";
if(total_sz == 0)
throw std::runtime_error(err_msg);
// Relay Agent/Server Messages
bool is_relay_msg = (buffer[0] == 12 || buffer[0] == 13);
uint32_t required_size = is_relay_msg ? 2 : 4;
if(total_sz < required_size)
throw std::runtime_error(err_msg);
std::copy(buffer, buffer + required_size, header_data);
buffer += required_size;
total_sz -= required_size;
if(is_relay_message()) {
if(total_sz < ipaddress_type::address_size * 2)
throw std::runtime_error(err_msg);
link_addr = buffer;
peer_addr = buffer + ipaddress_type::address_size;
buffer += ipaddress_type::address_size * 2;
total_sz -= ipaddress_type::address_size * 2;
}
options_size = total_sz;
while(total_sz) {
if(total_sz < sizeof(uint16_t) * 2)
throw std::runtime_error(opt_err_msg);
const uint16_t option = Endian::be_to_host(*(const uint16_t*)buffer);
const uint16_t data_size = Endian::be_to_host(
*(const uint16_t*)(buffer + sizeof(uint16_t))
);
if(total_sz - sizeof(uint16_t) * 2 < data_size)
throw std::runtime_error(opt_err_msg);
buffer += sizeof(uint16_t) * 2;
add_option(
dhcpv6_option(option, buffer, buffer + data_size)
);
buffer += data_size;
total_sz -= sizeof(uint16_t) * 2 + data_size;
}
}
void DHCPv6::add_option(const dhcpv6_option &option) {
options_.push_back(option);
}
const DHCPv6::dhcpv6_option *DHCPv6::search_option(uint16_t id) const {
for(options_type::const_iterator it = options_.begin(); it != options_.end(); ++it) {
if(it->option() == id)
return &*it;
}
return 0;
}
uint8_t* DHCPv6::write_option(const dhcpv6_option &option, uint8_t* buffer) const {
*(uint16_t*)buffer = Endian::host_to_be(option.option());
*(uint16_t*)&buffer[sizeof(uint16_t)] = Endian::host_to_be(option.data_size());
std::cout << "Size: " << option.data_size() << std::endl;
return std::copy(
option.data_ptr(),
option.data_ptr() + option.data_size(),
buffer + sizeof(uint16_t) * 2
);
}
void DHCPv6::msg_type(uint8_t type) {
header_data[0] = type;
}
void DHCPv6::hop_count(uint8_t count) {
header_data[1] = count;
}
void DHCPv6::transaction_id(small_uint<24> id) {
uint32_t id_32 = id;
header_data[1] = id_32 >> 16;
header_data[2] = id_32 >> 8;
header_data[3] = id_32 & 0xff;
}
void DHCPv6::peer_address(const ipaddress_type &addr) {
peer_addr = addr;
}
void DHCPv6::link_address(const ipaddress_type &addr) {
link_addr = addr;
}
bool DHCPv6::is_relay_message() const {
return msg_type() == 12 || msg_type() == 13;
}
uint32_t DHCPv6::header_size() const {
return (is_relay_message() ? (2 + ipaddress_type::address_size * 2) : 4) + options_size;
}
void DHCPv6::write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *) {
const uint32_t required_size = is_relay_message() ? 2 : 4;
buffer = std::copy(header_data, header_data + required_size, buffer);
if(is_relay_message()) {
buffer = link_addr.copy(buffer);
buffer = peer_addr.copy(buffer);
}
for(options_type::const_iterator it = options_.begin(); it != options_.end(); ++it)
buffer = write_option(*it, buffer);
}
}

2140
tests/depends.d Normal file

File diff suppressed because it is too large Load Diff

69
tests/src/dhcpv6.cpp Normal file
View File

@@ -0,0 +1,69 @@
#include <gtest/gtest.h>
#include <cstring>
#include <string>
#include <stdint.h>
#include "dhcpv6.h"
using namespace Tins;
class DHCPv6Test : public testing::Test {
public:
static const uint8_t expected_packet[];
void test_equals(const DHCPv6 &dhcp1, const DHCPv6 &dhcp2);
};
const uint8_t DHCPv6Test::expected_packet[] = {
'\x01', '\xe8', '(', '\xb9', '\x00', '\x01', '\x00', '\n', '\x00',
'\x03', '\x00', '\x01', '\x00', '\x01', '\x02', '\x03', '\x04',
'\x05', '\x00', '\x03', '\x00', '\x0c', '\x00', '\x00', '\x00',
'\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00',
'\x00', '\x00', '\x08', '\x00', '\x02', '\x00', '\x00', '\x00',
'\x06', '\x00', '\x02', '\x00', '\x03'
};
TEST_F(DHCPv6Test, DefaultConstructor) {
DHCPv6 dhcp;
EXPECT_EQ(0, dhcp.msg_type());
EXPECT_EQ(0, dhcp.hop_count());
EXPECT_EQ(0, dhcp.transaction_id());
}
TEST_F(DHCPv6Test, ConstructorFromBuffer) {
DHCPv6 dhcp(expected_packet, sizeof(expected_packet));
EXPECT_EQ(1, dhcp.msg_type());
EXPECT_EQ(0xe828b9, dhcp.transaction_id());
EXPECT_TRUE(dhcp.search_option(1));
EXPECT_TRUE(dhcp.search_option(3));
EXPECT_TRUE(dhcp.search_option(6));
EXPECT_TRUE(dhcp.search_option(8));
EXPECT_FALSE(dhcp.search_option(2));
}
TEST_F(DHCPv6Test, Serialize) {
DHCPv6 dhcp(expected_packet, sizeof(expected_packet));
DHCPv6::serialization_type buffer = dhcp.serialize();
ASSERT_EQ(sizeof(expected_packet), buffer.size());
EXPECT_EQ(
DHCPv6::serialization_type(expected_packet, expected_packet + sizeof(expected_packet)),
buffer
);
}
TEST_F(DHCPv6Test, MessageType) {
DHCPv6 dhcp;
dhcp.msg_type(0x8a);
EXPECT_EQ(0x8a, dhcp.msg_type());
}
TEST_F(DHCPv6Test, HopCount) {
DHCPv6 dhcp;
dhcp.hop_count(0x8a);
EXPECT_EQ(0x8a, dhcp.hop_count());
}
TEST_F(DHCPv6Test, TransactionId) {
DHCPv6 dhcp;
dhcp.transaction_id(0x8af2ad);
EXPECT_EQ(0x8af2ad, dhcp.transaction_id());
}