mirror of
https://github.com/mfontanini/libtins
synced 2026-01-23 02:35:57 +01:00
Added IPv6 class. It's working, but there's no support for extension headers yet.
This commit is contained in:
12
depends.d
12
depends.d
@@ -274,6 +274,18 @@ include/packet_sender.h:
|
||||
include/constants.h:
|
||||
src/ipv6_address.o: src/ipv6_address.cpp include/ipv6_address.h
|
||||
|
||||
include/ipv6_address.h:
|
||||
src/ipv6.o: src/ipv6.cpp include/ipv6.h include/pdu.h \
|
||||
include/endianness.h include/small_uint.h include/ipv6_address.h
|
||||
|
||||
include/ipv6.h:
|
||||
|
||||
include/pdu.h:
|
||||
|
||||
include/endianness.h:
|
||||
|
||||
include/small_uint.h:
|
||||
|
||||
include/ipv6_address.h:
|
||||
src/llc.o: src/llc.cpp include/pdu.h include/llc.h include/pdu.h \
|
||||
include/endianness.h include/rawpdu.h
|
||||
|
||||
@@ -73,7 +73,7 @@ namespace Tins {
|
||||
|
||||
struct Ethernet {
|
||||
enum e {
|
||||
PUP = 0x0200, /* Xerox PUP */
|
||||
//~ PUP = 0x0200, /* Xerox PUP */
|
||||
SPRITE = 0x0500, /* Sprite */
|
||||
IP = 0x0800, /* IP */
|
||||
ARP = 0x0806, /* Address resolution */
|
||||
|
||||
@@ -49,7 +49,7 @@ namespace Tins {
|
||||
class IP : public PDU {
|
||||
public:
|
||||
/**
|
||||
* his PDU's flag.
|
||||
* This PDU's flag.
|
||||
*/
|
||||
static const PDU::PDUType pdu_flag = PDU::IP;
|
||||
|
||||
|
||||
252
include/ipv6.h
Normal file
252
include/ipv6.h
Normal file
@@ -0,0 +1,252 @@
|
||||
/*
|
||||
* 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_IPV6_h
|
||||
#define TINS_IPV6_h
|
||||
|
||||
#include "pdu.h"
|
||||
#include "endianness.h"
|
||||
#include "small_uint.h"
|
||||
#include "ipv6_address.h"
|
||||
|
||||
namespace Tins {
|
||||
class PacketSender;
|
||||
|
||||
class IPv6 : public PDU {
|
||||
public:
|
||||
/**
|
||||
* This PDU's flag.
|
||||
*/
|
||||
static const PDU::PDUType pdu_flag = PDU::IP;
|
||||
|
||||
/**
|
||||
* The type used to store addresses.
|
||||
*/
|
||||
typedef IPv6Address address_type;
|
||||
|
||||
/**
|
||||
* \brief Constructs an IPv6 object.
|
||||
*
|
||||
* \param ip_dst The destination ip address(optional).
|
||||
* \param ip_src The source ip address(optional).
|
||||
* \param child pointer to a PDU which will be set as the inner_pdu
|
||||
* for the packet being constructed(optional).
|
||||
*/
|
||||
IPv6(address_type ip_dst = address_type(),
|
||||
address_type ip_src = address_type(),
|
||||
PDU *child = 0);
|
||||
|
||||
/**
|
||||
* \brief Constructor which creates an IPv6 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.
|
||||
*/
|
||||
IPv6(const uint8_t *buffer, uint32_t total_sz);
|
||||
|
||||
// Getters
|
||||
|
||||
/**
|
||||
* \brief Getter for the version field.
|
||||
* \return The stored version field value.
|
||||
*/
|
||||
small_uint<4> version() const {
|
||||
return _header.version;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Getter for the traffic_class field.
|
||||
* \return The stored traffic_class field value.
|
||||
*/
|
||||
uint8_t traffic_class() const {
|
||||
#if TINS_IS_LITTLE_ENDIAN
|
||||
return ((_header.traffic_class << 4) & 0xf0) |
|
||||
((_header.flow_label[0] >> 4) & 0x0f);
|
||||
#else
|
||||
return _header.traffic_class;
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Getter for the flow_label field.
|
||||
* \return The stored flow_label field value.
|
||||
*/
|
||||
small_uint<20> flow_label() const {
|
||||
#if TINS_IS_LITTLE_ENDIAN
|
||||
return ((_header.flow_label[0] & 0x0f) << 16)
|
||||
| (_header.flow_label[1] << 8)
|
||||
| (_header.flow_label[2]);
|
||||
#else
|
||||
return _header.flow_label;
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Getter for the payload_length field.
|
||||
* \return The stored payload_length field value.
|
||||
*/
|
||||
uint16_t payload_length() const {
|
||||
return Endian::be_to_host(_header.payload_length);
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Getter for the next_header field.
|
||||
* \return The stored next_header field value.
|
||||
*/
|
||||
uint8_t next_header() const {
|
||||
return _header.next_header;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Getter for the hop_limit field.
|
||||
* \return The stored hop_limit field value.
|
||||
*/
|
||||
uint8_t hop_limit() const {
|
||||
return _header.hop_limit;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Getter for the src_addr field.
|
||||
* \return The stored src_addr field value.
|
||||
*/
|
||||
address_type src_addr() const {
|
||||
return _header.src_addr;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Getter for the dst_addr field.
|
||||
* \return The stored dst_addr field value.
|
||||
*/
|
||||
address_type dst_addr() const {
|
||||
return _header.dst_addr;
|
||||
}
|
||||
|
||||
// Setters
|
||||
|
||||
/**
|
||||
* \brief Setter for the version field.
|
||||
* \param new_version The new version field value.
|
||||
*/
|
||||
void version(small_uint<4> new_version);
|
||||
|
||||
/**
|
||||
* \brief Setter for the traffic_class field.
|
||||
* \param new_traffic_class The new traffic_class field value.
|
||||
*/
|
||||
void traffic_class(uint8_t new_traffic_class);
|
||||
|
||||
/**
|
||||
* \brief Setter for the flow_label field.
|
||||
* \param new_flow_label The new flow_label field value.
|
||||
*/
|
||||
void flow_label(small_uint<20> new_flow_label);
|
||||
|
||||
/**
|
||||
* \brief Setter for the payload_length field.
|
||||
* \param new_payload_length The new payload_length field value.
|
||||
*/
|
||||
void payload_length(uint16_t new_payload_length);
|
||||
|
||||
/**
|
||||
* \brief Setter for the next_header field.
|
||||
* \param new_next_header The new next_header field value.
|
||||
*/
|
||||
void next_header(uint8_t new_next_header);
|
||||
|
||||
/**
|
||||
* \brief Setter for the hop_limit field.
|
||||
* \param new_hop_limit The new hop_limit field value.
|
||||
*/
|
||||
void hop_limit(uint8_t new_hop_limit);
|
||||
|
||||
/**
|
||||
* \brief Setter for the src_addr field.
|
||||
* \param new_src_addr The new src_addr field value.
|
||||
*/
|
||||
void src_addr(const address_type &new_src_addr);
|
||||
|
||||
/**
|
||||
* \brief Setter for the dst_addr field.
|
||||
* \param new_dst_addr The new dst_addr field value.
|
||||
*/
|
||||
void dst_addr(const address_type &new_dst_addr);
|
||||
|
||||
/**
|
||||
* \brief Returns the header size.
|
||||
*
|
||||
* This metod overrides PDU::header_size. \sa PDU::header_size
|
||||
*/
|
||||
uint32_t header_size() const;
|
||||
|
||||
/**
|
||||
* \sa PDU::clone
|
||||
*/
|
||||
IPv6 *clone() const {
|
||||
return new IPv6(*this);
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Getter for the PDU's type.
|
||||
* \sa PDU::pdu_type
|
||||
*/
|
||||
PDUType pdu_type() const { return pdu_flag; }
|
||||
|
||||
/**
|
||||
* \sa PDU::send()
|
||||
*/
|
||||
void send(PacketSender &sender);
|
||||
private:
|
||||
void write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *parent);
|
||||
|
||||
struct ipv6_header {
|
||||
#if TINS_IS_BIG_ENDIAN
|
||||
uint32_t version:4,
|
||||
traffic_class:8,
|
||||
flow_label:20;
|
||||
uint32_t payload_length:16,
|
||||
next_header:8,
|
||||
hop_limit:8;
|
||||
#else
|
||||
uint32_t traffic_class:4,
|
||||
version:4;
|
||||
uint8_t flow_label[3];
|
||||
uint16_t payload_length;
|
||||
uint8_t next_header;
|
||||
uint8_t hop_limit;
|
||||
#endif
|
||||
uint8_t src_addr[16], dst_addr[16];
|
||||
} __attribute__((packed));
|
||||
|
||||
ipv6_header _header;
|
||||
};
|
||||
}
|
||||
|
||||
#endif // TINS_IPV6_h
|
||||
@@ -66,7 +66,14 @@ public:
|
||||
IPv6Address();
|
||||
|
||||
/**
|
||||
* \brief Constructor from a text representation.
|
||||
* \brief Constructor from a text representation char*.
|
||||
* \param addr The text representation from which to construct this
|
||||
* object.
|
||||
*/
|
||||
IPv6Address(const char *addr);
|
||||
|
||||
/**
|
||||
* \brief Constructor from a text representation std::string.
|
||||
* \param addr The text representation from which to construct this
|
||||
* object.
|
||||
*/
|
||||
@@ -181,6 +188,8 @@ public:
|
||||
return os << addr.to_string();
|
||||
}
|
||||
private:
|
||||
void init(const char *addr);
|
||||
|
||||
uint8_t address[address_size];
|
||||
};
|
||||
}
|
||||
|
||||
@@ -63,6 +63,7 @@ namespace Tins {
|
||||
IP_SOCKET,
|
||||
ARP_SOCKET,
|
||||
ICMP_SOCKET,
|
||||
IPV6_SOCKET,
|
||||
SOCKETS_END
|
||||
};
|
||||
|
||||
|
||||
@@ -101,7 +101,8 @@ namespace Tins {
|
||||
RC4EAPOL,
|
||||
RSNEAPOL,
|
||||
DNS,
|
||||
LOOPBACK
|
||||
LOOPBACK,
|
||||
IPv6
|
||||
};
|
||||
|
||||
/** \brief PDU constructor
|
||||
|
||||
@@ -40,6 +40,7 @@
|
||||
#include "icmp.h"
|
||||
#include "dot11.h"
|
||||
#include "ip.h"
|
||||
#include "ipv6.h"
|
||||
#include "packet_sender.h"
|
||||
#include "packet_writer.h"
|
||||
#include "pdu.h"
|
||||
|
||||
@@ -42,6 +42,7 @@
|
||||
#include <fstream>
|
||||
#include <stdint.h>
|
||||
#include "ip_address.h"
|
||||
#include "ipv6_address.h"
|
||||
#include "hw_address.h"
|
||||
|
||||
namespace Tins {
|
||||
@@ -190,6 +191,16 @@ namespace Tins {
|
||||
* \return The pseudo header checksum.
|
||||
*/
|
||||
uint32_t pseudoheader_checksum(IPv4Address source_ip, IPv4Address dest_ip, uint32_t len, uint32_t flag);
|
||||
|
||||
/** \brief Performs the pseudo header checksum used in TCP and UDP PDUs.
|
||||
*
|
||||
* \param source_ip The source ip address.
|
||||
* \param dest_ip The destination ip address.
|
||||
* \param len The length to be included in the pseudo header.
|
||||
* \param flag The flag to use in the protocol field of the pseudo header.
|
||||
* \return The pseudo header checksum.
|
||||
*/
|
||||
uint32_t pseudoheader_checksum(IPv6Address source_ip, IPv6Address dest_ip, uint32_t len, uint32_t flag);
|
||||
|
||||
/** \brief Generic function to iterate through interface and collect
|
||||
* data.
|
||||
|
||||
@@ -40,6 +40,7 @@
|
||||
#include "packet_sender.h"
|
||||
#include "rawpdu.h"
|
||||
#include "ip.h"
|
||||
#include "ipv6.h"
|
||||
#include "arp.h"
|
||||
#include "constants.h"
|
||||
|
||||
@@ -72,6 +73,9 @@ EthernetII::EthernetII(const uint8_t *buffer, uint32_t total_sz)
|
||||
case Constants::Ethernet::IP:
|
||||
next = new Tins::IP(buffer, total_sz);
|
||||
break;
|
||||
case Constants::Ethernet::IPV6:
|
||||
next = new Tins::IPv6(buffer, total_sz);
|
||||
break;
|
||||
case Constants::Ethernet::ARP:
|
||||
next = new Tins::ARP(buffer, total_sz);
|
||||
break;
|
||||
@@ -143,6 +147,9 @@ void EthernetII::write_serialization(uint8_t *buffer, uint32_t total_sz, const P
|
||||
case PDU::IP:
|
||||
type = Constants::Ethernet::IP;
|
||||
break;
|
||||
case PDU::IPv6:
|
||||
type = Constants::Ethernet::IPV6;
|
||||
break;
|
||||
case PDU::ARP:
|
||||
type = Constants::Ethernet::ARP;
|
||||
break;
|
||||
|
||||
136
src/ipv6.cpp
Normal file
136
src/ipv6.cpp
Normal file
@@ -0,0 +1,136 @@
|
||||
#include <cstring>
|
||||
#include <cassert>
|
||||
#ifndef WIN32
|
||||
#include <netinet/in.h>
|
||||
#include <sys/socket.h>
|
||||
#endif
|
||||
#include "ipv6.h"
|
||||
#include "constants.h"
|
||||
#include "packet_sender.h"
|
||||
#include "ip.h"
|
||||
#include "tcp.h"
|
||||
#include "udp.h"
|
||||
#include "icmp.h"
|
||||
#include "rawpdu.h"
|
||||
|
||||
namespace Tins {
|
||||
|
||||
IPv6::IPv6(address_type ip_dst, address_type ip_src, PDU *child) {
|
||||
std::memset(&_header, 0, sizeof(_header));
|
||||
version(6);
|
||||
dst_addr(ip_dst);
|
||||
src_addr(ip_src);
|
||||
}
|
||||
|
||||
IPv6::IPv6(const uint8_t *buffer, uint32_t total_sz) {
|
||||
if(total_sz < sizeof(_header))
|
||||
throw std::runtime_error("Not enough size for an IPv6 PDU");
|
||||
std::memcpy(&_header, buffer, sizeof(_header));
|
||||
buffer += sizeof(_header);
|
||||
total_sz -= sizeof(_header);
|
||||
if (total_sz) {
|
||||
switch(_header.next_header) {
|
||||
case Constants::IP::PROTO_TCP:
|
||||
inner_pdu(new Tins::TCP(buffer, total_sz));
|
||||
break;
|
||||
case Constants::IP::PROTO_UDP:
|
||||
inner_pdu(new Tins::UDP(buffer, total_sz));
|
||||
break;
|
||||
case Constants::IP::PROTO_ICMP:
|
||||
inner_pdu(new Tins::ICMP(buffer, total_sz));
|
||||
break;
|
||||
default:
|
||||
inner_pdu(new Tins::RawPDU(buffer, total_sz));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void IPv6::version(small_uint<4> new_version) {
|
||||
_header.version = new_version;
|
||||
}
|
||||
|
||||
void IPv6::traffic_class(uint8_t new_traffic_class) {
|
||||
#if TINS_IS_LITTLE_ENDIAN
|
||||
_header.traffic_class = (new_traffic_class >> 4) & 0xf;
|
||||
_header.flow_label[0] = (_header.flow_label[0] & 0x0f) | ((new_traffic_class << 4) & 0xf0);
|
||||
#else
|
||||
_header.traffic_class = new_traffic_class;
|
||||
#endif
|
||||
}
|
||||
|
||||
void IPv6::flow_label(small_uint<20> new_flow_label) {
|
||||
#if TINS_IS_LITTLE_ENDIAN
|
||||
uint32_t value = Endian::host_to_be<uint32_t>(new_flow_label);
|
||||
_header.flow_label[2] = (value >> 24) & 0xff;
|
||||
_header.flow_label[1] = (value >> 16) & 0xff;
|
||||
_header.flow_label[0] = ((value >> 8) & 0x0f) | (_header.flow_label[0] & 0xf0);
|
||||
#else
|
||||
_header.flow_label = new_flow_label;
|
||||
#endif
|
||||
}
|
||||
|
||||
void IPv6::payload_length(uint16_t new_payload_length) {
|
||||
_header.payload_length = Endian::host_to_be(new_payload_length);
|
||||
}
|
||||
|
||||
void IPv6::next_header(uint8_t new_next_header) {
|
||||
_header.next_header = new_next_header;
|
||||
}
|
||||
|
||||
void IPv6::hop_limit(uint8_t new_hop_limit) {
|
||||
_header.hop_limit = new_hop_limit;
|
||||
}
|
||||
|
||||
void IPv6::src_addr(const address_type &new_src_addr) {
|
||||
new_src_addr.copy(_header.src_addr);
|
||||
}
|
||||
|
||||
void IPv6::dst_addr(const address_type &new_dst_addr) {
|
||||
new_dst_addr.copy(_header.dst_addr);
|
||||
}
|
||||
|
||||
uint32_t IPv6::header_size() const {
|
||||
return sizeof(_header);
|
||||
}
|
||||
|
||||
void IPv6::write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *parent) {
|
||||
assert(total_sz >= sizeof(_header));
|
||||
if(inner_pdu()) {
|
||||
uint8_t new_flag;
|
||||
switch(inner_pdu()->pdu_type()) {
|
||||
case PDU::IP:
|
||||
new_flag = Constants::IP::PROTO_IPIP;
|
||||
break;
|
||||
case PDU::TCP:
|
||||
new_flag = Constants::IP::PROTO_TCP;
|
||||
break;
|
||||
case PDU::UDP:
|
||||
new_flag = Constants::IP::PROTO_UDP;
|
||||
break;
|
||||
case PDU::ICMP:
|
||||
new_flag = Constants::IP::PROTO_ICMP;
|
||||
break;
|
||||
default:
|
||||
// check for other protos
|
||||
new_flag = 0xff;
|
||||
};
|
||||
next_header(new_flag);
|
||||
}
|
||||
payload_length(total_sz - sizeof(_header));
|
||||
std::memcpy(buffer, &_header, sizeof(_header));
|
||||
}
|
||||
|
||||
void IPv6::send(PacketSender &sender) {
|
||||
struct sockaddr_in6 link_addr;
|
||||
PacketSender::SocketType type = PacketSender::IPV6_SOCKET;
|
||||
link_addr.sin6_family = AF_INET6;
|
||||
link_addr.sin6_port = 0;
|
||||
std::copy(_header.dst_addr, _header.dst_addr + address_type::address_size, (uint8_t*)&link_addr.sin6_addr);
|
||||
if(inner_pdu() && inner_pdu()->pdu_type() == PDU::ICMP)
|
||||
type = PacketSender::ICMP_SOCKET;
|
||||
|
||||
sender.send_l3(*this, (struct sockaddr*)&link_addr, sizeof(link_addr), type);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -41,12 +41,20 @@ namespace Tins {
|
||||
std::fill(address, address + address_size, 0);
|
||||
}
|
||||
|
||||
IPv6Address::IPv6Address(const char *addr) {
|
||||
init(addr);
|
||||
}
|
||||
|
||||
IPv6Address::IPv6Address(const_iterator ptr) {
|
||||
std::copy(ptr, ptr + address_size, address);
|
||||
}
|
||||
|
||||
IPv6Address::IPv6Address(const std::string &addr) {
|
||||
if(inet_pton(AF_INET6, addr.c_str(), address) == 0)
|
||||
init(addr.c_str());
|
||||
}
|
||||
|
||||
void IPv6Address::init(const char *addr) {
|
||||
if(inet_pton(AF_INET6, addr, address) == 0)
|
||||
throw malformed_address();
|
||||
}
|
||||
|
||||
|
||||
38
src/tcp.cpp
38
src/tcp.cpp
@@ -31,6 +31,7 @@
|
||||
#include <cassert>
|
||||
#include "tcp.h"
|
||||
#include "ip.h"
|
||||
#include "ipv6.h"
|
||||
#include "constants.h"
|
||||
#include "rawpdu.h"
|
||||
#include "utils.h"
|
||||
@@ -297,18 +298,35 @@ void TCP::write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *par
|
||||
}
|
||||
}
|
||||
|
||||
const Tins::IP *ip_packet = dynamic_cast<const Tins::IP*>(parent);
|
||||
memcpy(tcp_start, &_tcp, sizeof(tcphdr));
|
||||
if(!_tcp.check && ip_packet) {
|
||||
uint32_t checksum = Utils::pseudoheader_checksum(ip_packet->src_addr(),
|
||||
ip_packet->dst_addr(),
|
||||
size(), Constants::IP::PROTO_TCP) +
|
||||
Utils::do_checksum(tcp_start, tcp_start + total_sz);
|
||||
while (checksum >> 16)
|
||||
checksum = (checksum & 0xffff) + (checksum >> 16);
|
||||
|
||||
((tcphdr*)tcp_start)->check = Endian::host_to_be<uint16_t>(~checksum);
|
||||
|
||||
if(!_tcp.check) {
|
||||
const Tins::IP *ip_packet = dynamic_cast<const Tins::IP*>(parent);
|
||||
if(ip_packet) {
|
||||
uint32_t checksum = Utils::pseudoheader_checksum(ip_packet->src_addr(),
|
||||
ip_packet->dst_addr(),
|
||||
size(), Constants::IP::PROTO_TCP) +
|
||||
Utils::do_checksum(tcp_start, tcp_start + total_sz);
|
||||
while (checksum >> 16)
|
||||
checksum = (checksum & 0xffff) + (checksum >> 16);
|
||||
|
||||
((tcphdr*)tcp_start)->check = Endian::host_to_be<uint16_t>(~checksum);
|
||||
}
|
||||
else {
|
||||
const Tins::IPv6 *ipv6_packet = dynamic_cast<const Tins::IPv6*>(parent);
|
||||
if(ipv6_packet) {
|
||||
uint32_t checksum = Utils::pseudoheader_checksum(ipv6_packet->src_addr(),
|
||||
ipv6_packet->dst_addr(),
|
||||
size(), Constants::IP::PROTO_TCP) +
|
||||
Utils::do_checksum(tcp_start, tcp_start + total_sz);
|
||||
while (checksum >> 16)
|
||||
checksum = (checksum & 0xffff) + (checksum >> 16);
|
||||
|
||||
((tcphdr*)tcp_start)->check = Endian::host_to_be<uint16_t>(~checksum);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_tcp.check = 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -193,6 +193,17 @@ uint32_t Utils::pseudoheader_checksum(IPv4Address source_ip, IPv4Address dest_ip
|
||||
return checksum;
|
||||
}
|
||||
|
||||
uint32_t Utils::pseudoheader_checksum(IPv6Address source_ip, IPv6Address dest_ip, uint32_t len, uint32_t flag) {
|
||||
uint32_t checksum = 0;
|
||||
IPv6Address::const_iterator it;
|
||||
for(it = source_ip.begin(); it != source_ip.end(); ++it)
|
||||
checksum += *it;
|
||||
for(it = dest_ip.begin(); it != dest_ip.end(); ++it)
|
||||
checksum += *it;
|
||||
checksum += flag + len;
|
||||
return checksum;
|
||||
}
|
||||
|
||||
uint32_t Utils::crc32(const uint8_t* data, uint32_t data_size) {
|
||||
uint32_t i, crc = 0;
|
||||
static uint32_t crc_table[] = {
|
||||
|
||||
@@ -150,6 +150,37 @@ src/ipv6address.o: src/ipv6address.cpp ../include/ipv6_address.h \
|
||||
|
||||
../include/ip_address.h:
|
||||
|
||||
../include/hw_address.h:
|
||||
src/ipv6.o: src/ipv6.cpp ../include/ipv6.h ../include/pdu.h \
|
||||
../include/endianness.h ../include/small_uint.h \
|
||||
../include/ipv6_address.h ../include/tcp.h ../include/pdu_option.h \
|
||||
../include/udp.h ../include/icmp.h ../include/ipv6_address.h \
|
||||
../include/utils.h ../include/ip_address.h ../include/hw_address.h
|
||||
|
||||
../include/ipv6.h:
|
||||
|
||||
../include/pdu.h:
|
||||
|
||||
../include/endianness.h:
|
||||
|
||||
../include/small_uint.h:
|
||||
|
||||
../include/ipv6_address.h:
|
||||
|
||||
../include/tcp.h:
|
||||
|
||||
../include/pdu_option.h:
|
||||
|
||||
../include/udp.h:
|
||||
|
||||
../include/icmp.h:
|
||||
|
||||
../include/ipv6_address.h:
|
||||
|
||||
../include/utils.h:
|
||||
|
||||
../include/ip_address.h:
|
||||
|
||||
../include/hw_address.h:
|
||||
src/llc.o: src/llc.cpp ../include/llc.h ../include/pdu.h \
|
||||
../include/endianness.h
|
||||
@@ -1115,6 +1146,19 @@ include/tests/dot11.h:
|
||||
../include/constants.h:
|
||||
../src/ipv6_address.o: ../src/ipv6_address.cpp ../include/ipv6_address.h
|
||||
|
||||
../include/ipv6_address.h:
|
||||
../src/ipv6.o: ../src/ipv6.cpp ../include/ipv6.h ../include/pdu.h \
|
||||
../include/endianness.h ../include/small_uint.h \
|
||||
../include/ipv6_address.h
|
||||
|
||||
../include/ipv6.h:
|
||||
|
||||
../include/pdu.h:
|
||||
|
||||
../include/endianness.h:
|
||||
|
||||
../include/small_uint.h:
|
||||
|
||||
../include/ipv6_address.h:
|
||||
../src/llc.o: ../src/llc.cpp ../include/pdu.h ../include/llc.h \
|
||||
../include/pdu.h ../include/endianness.h ../include/rawpdu.h
|
||||
|
||||
110
tests/src/ipv6.cpp
Normal file
110
tests/src/ipv6.cpp
Normal file
@@ -0,0 +1,110 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#include <stdint.h>
|
||||
#include "ipv6.h"
|
||||
#include "tcp.h"
|
||||
#include "udp.h"
|
||||
#include "icmp.h"
|
||||
#include "ipv6_address.h"
|
||||
#include "utils.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace Tins;
|
||||
|
||||
class IPv6Test : public testing::Test {
|
||||
public:
|
||||
static const uint8_t expected_packet[];
|
||||
|
||||
void test_equals(const IPv6 &ip1, const IPv6 &ip2);
|
||||
};
|
||||
|
||||
const uint8_t IPv6Test::expected_packet[] = {
|
||||
'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',
|
||||
'\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00',
|
||||
'\x00', '\x00', '\x01', '\xc6', '\x8c', '\x00', 'P', 'h', 'H', '\x03',
|
||||
'\x0c', '\x00', '\x00', '\x00', '\x00', '\xa0', '\x02', '\x7f', '\xf0',
|
||||
'\x00', '0', '\x00', '\x00', '\x02', '\x04', '?', '\xf8', '\x04',
|
||||
'\x02', '\x08', '\n', '\x00', '\x84', '\xa3', '\x9c', '\x00', '\x00',
|
||||
'\x00', '\x00', '\x01', '\x03', '\x03', '\x07'
|
||||
};
|
||||
|
||||
TEST_F(IPv6Test, Constructor) {
|
||||
IPv6 ipv6("::1:2:3", "f0aa:beef::1");
|
||||
EXPECT_EQ(ipv6.version(), 6);
|
||||
EXPECT_EQ(ipv6.traffic_class(), 0);
|
||||
EXPECT_EQ(ipv6.flow_label(), 0);
|
||||
EXPECT_EQ(ipv6.payload_length(), 0);
|
||||
EXPECT_EQ(ipv6.next_header(), 0);
|
||||
EXPECT_EQ(ipv6.hop_limit(), 0);
|
||||
EXPECT_EQ(ipv6.dst_addr(), "::1:2:3");
|
||||
EXPECT_EQ(ipv6.src_addr(), "f0aa:beef::1");
|
||||
}
|
||||
|
||||
TEST_F(IPv6Test, ConstructorFromBuffer) {
|
||||
IPv6 ipv6(expected_packet, sizeof(expected_packet));
|
||||
EXPECT_EQ(ipv6.version(), 6);
|
||||
EXPECT_EQ(ipv6.traffic_class(), 0x9a);
|
||||
EXPECT_EQ(ipv6.flow_label(), 0x82734);
|
||||
EXPECT_EQ(ipv6.payload_length(), 40);
|
||||
EXPECT_EQ(ipv6.next_header(), 6);
|
||||
EXPECT_EQ(ipv6.hop_limit(), 64);
|
||||
EXPECT_EQ(ipv6.dst_addr(), "::1");
|
||||
EXPECT_EQ(ipv6.src_addr(), "::1");
|
||||
ASSERT_TRUE(ipv6.inner_pdu());
|
||||
TCP *tcp = ipv6.find_pdu<TCP>();
|
||||
ASSERT_TRUE(tcp);
|
||||
EXPECT_EQ(tcp->sport(), 50828);
|
||||
EXPECT_EQ(tcp->dport(), 80);
|
||||
}
|
||||
|
||||
TEST_F(IPv6Test, Version) {
|
||||
IPv6 ipv6;
|
||||
ipv6.version(3);
|
||||
EXPECT_EQ(ipv6.version(), 3);
|
||||
}
|
||||
|
||||
TEST_F(IPv6Test, TrafficClass) {
|
||||
IPv6 ipv6;
|
||||
ipv6.traffic_class(0x7a);
|
||||
EXPECT_EQ(ipv6.traffic_class(), 0x7a);
|
||||
}
|
||||
|
||||
TEST_F(IPv6Test, FlowLabel) {
|
||||
IPv6 ipv6;
|
||||
ipv6.flow_label(0x918d7);
|
||||
EXPECT_EQ(ipv6.flow_label(), 0x918d7);
|
||||
}
|
||||
|
||||
TEST_F(IPv6Test, PayloadLength) {
|
||||
IPv6 ipv6;
|
||||
ipv6.payload_length(0xaf71);
|
||||
EXPECT_EQ(ipv6.payload_length(), 0xaf71);
|
||||
}
|
||||
|
||||
TEST_F(IPv6Test, NextHeader) {
|
||||
IPv6 ipv6;
|
||||
ipv6.next_header(0x7a);
|
||||
EXPECT_EQ(ipv6.next_header(), 0x7a);
|
||||
}
|
||||
|
||||
TEST_F(IPv6Test, HopLimit) {
|
||||
IPv6 ipv6;
|
||||
ipv6.hop_limit(0x7a);
|
||||
EXPECT_EQ(ipv6.hop_limit(), 0x7a);
|
||||
}
|
||||
|
||||
TEST_F(IPv6Test, SourceAddress) {
|
||||
IPv6 ipv6;
|
||||
ipv6.src_addr("99af:1293::1");
|
||||
EXPECT_EQ(ipv6.src_addr(), "99af:1293::1");
|
||||
}
|
||||
|
||||
TEST_F(IPv6Test, DestinationAddress) {
|
||||
IPv6 ipv6;
|
||||
ipv6.dst_addr("99af:1293::1");
|
||||
EXPECT_EQ(ipv6.dst_addr(), "99af:1293::1");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user