add ip4 to ip6 handler

This commit is contained in:
stubbfel
2016-12-06 22:15:17 +01:00
parent 79d7336689
commit 6979ae8bb2
5 changed files with 111 additions and 3 deletions

View File

@@ -0,0 +1,74 @@
#include "Ip4ToIp6PacketHandler.h"
#include <tins/ethernetII.h>
#include <tins/ip.h>
#include <tins/ipv6.h>
#include <tins/ipv6_address.h>
#include "IpAddressTranslator.h"
Ip4ToIp6PacketHandler::Ip4ToIp6PacketHandler(const Tins::IPv6Address & newPrefix)
{
prefix = std::make_unique<Tins::IPv6Address>(newPrefix);
}
Ip4ToIp6PacketHandler::~Ip4ToIp6PacketHandler()
{
}
bool Ip4ToIp6PacketHandler::handle(IN const Tins::PDU & pdu, IN IPacketHandler * callBackHandler)
{
// callback handvet requeried
if (callBackHandler == nullptr)
{
return false;
}
// get ipv4 packet
const Tins::IP * ipv4Pdu = pdu.find_pdu<Tins::IP>();
if(ipv4Pdu == nullptr)
{
return false;
}
// convert ipv4 addresses to ipv6 addresses
UPtrIPv6Address srcIp6Address = std::make_unique<Tins::IPv6Address>(*prefix);
UPtrIPv6Address dstIp6Address = std::make_unique<Tins::IPv6Address>(*prefix);
IpAddressTranslator::toIpv6Address(ipv4Pdu->src_addr(), *srcIp6Address);
IpAddressTranslator::toIpv6Address(ipv4Pdu->dst_addr(), *dstIp6Address);
// create ip4 pdu
std::unique_ptr<const Tins::IPv6> ipv6Pdu = std::make_unique<const Tins::IPv6>(*dstIp6Address,*srcIp6Address);
// create forwarding frame
std::unique_ptr<Tins::EthernetII> ptrForwardEthPdu = std::make_unique<Tins::EthernetII>();
Tins::EthernetII & forwardEthPdu = *ptrForwardEthPdu;
// copy src and dst mac address
const Tins::EthernetII * ethPdu = pdu.find_pdu<Tins::EthernetII>();
if(ethPdu != nullptr)
{
forwardEthPdu.src_addr(ethPdu->src_addr());
forwardEthPdu.dst_addr(ethPdu->dst_addr());
}
forwardEthPdu /= *ipv6Pdu;
//copy ipv4 payload to ip6 payload
const Tins::PDU * ipPayloadPdu = ipv4Pdu->inner_pdu();
if(ipPayloadPdu != nullptr)
{
forwardEthPdu /= *ipPayloadPdu;
}
// forward frame
return callBackHandler->handle(forwardEthPdu, this);
}
const Tins::IPv6Address & Ip4ToIp6PacketHandler::getPrefix() const
{
return *prefix;
}
void Ip4ToIp6PacketHandler::setPrefix(const Tins::IPv6Address & newPrefix)
{
*prefix = newPrefix;
}

View File

@@ -0,0 +1,22 @@
#ifndef IP4TO6PACKETHANDLER_H
#define IP4TO6PACKETHANDLER_H
#include "AbstractPacketHandler.h"
#include "Ip6Packet_t.h"
class Ip4ToIp6PacketHandler : public AbstractPacketHandler
{
public:
Ip4ToIp6PacketHandler(const Tins::IPv6Address & prefix);
virtual ~Ip4ToIp6PacketHandler();
virtual bool handle(IN const Tins::PDU & pdu, IN IPacketHandler * callBackHandler = nullptr) override;
const Tins::IPv6Address & getPrefix() const;
void setPrefix(const Tins::IPv6Address & newPrefix);
private:
UPtrIPv6Address prefix;
};
#endif

12
src/Ip6Packet_t.h Normal file
View File

@@ -0,0 +1,12 @@
#ifndef IP6PACKET_T_H
#define IP6PACKET_T_H
#include <memory>
namespace Tins
{
class IPv6Address;
}
typedef std::unique_ptr<Tins::IPv6Address> UPtrIPv6Address;
#endif

View File

@@ -1,5 +1,5 @@
#include "Ip6ToIp4PacketHandler.h" #include "Ip6ToIp4PacketHandler.h"
#include "tins/ethernetII.h" #include <tins/ethernetII.h>
#include <tins/ip.h> #include <tins/ip.h>
#include <tins/ipv6.h> #include <tins/ipv6.h>
#include "IpAddressTranslator.h" #include "IpAddressTranslator.h"

View File

@@ -1,5 +1,5 @@
#ifndef IP4TO6PACKETHANDLER_H #ifndef IP6TO4PACKETHANDLER_H
#define IP4TO6PACKETHANDLER_H #define IP6TO4PACKETHANDLER_H
#include "AbstractPacketHandler.h" #include "AbstractPacketHandler.h"