52 lines
1.7 KiB
C++
52 lines
1.7 KiB
C++
#include "IpAddressTranslator.h"
|
|
#include <tins/ip_address.h>
|
|
#include <tins/ipv6_address.h>
|
|
|
|
#ifndef NdpPrefixValueString
|
|
#define NdpPrefixValueString "FF02:0:0:0:0:1:FF00:0"
|
|
#endif
|
|
|
|
#ifndef NdpMaskValueString
|
|
#define NdpMaskValueString "::FF:FFFF"
|
|
#endif
|
|
|
|
const size_t IpAddressTranslator::lastAddressByteIndex = Tins::IPv4Address::address_size - 1;
|
|
const UPtrIPv6Address IpAddressTranslator::ndpPrefix = std::make_unique<Tins::IPv6Address>(NdpPrefixValueString);
|
|
const UPtrIPv6Address IpAddressTranslator::ndpMask = std::make_unique<Tins::IPv6Address>(NdpMaskValueString);
|
|
|
|
void IpAddressTranslator::toSolicitedNodeAddress(const Tins::IPv4Address &ipv4Address, Tins::IPv6Address &resultIpv6Address)
|
|
{
|
|
toIpv6Address(ipv4Address, resultIpv6Address);
|
|
toSolicitedNodeAddress(resultIpv6Address, resultIpv6Address);
|
|
}
|
|
|
|
void IpAddressTranslator::toSolicitedNodeAddress(const Tins::IPv6Address &ipv6Address, Tins::IPv6Address &resultIpv6Address)
|
|
{
|
|
resultIpv6Address = (*ndpMask & ipv6Address)| *ndpPrefix;
|
|
}
|
|
|
|
uint32_t IpAddressTranslator::toIpv4AddressBytes(IN const Tins::IPv6Address & ipv6Address)
|
|
{
|
|
uint32_t ipBytesNetworkOrder = 0;
|
|
uint8_t * ptrIpByte = reinterpret_cast<uint8_t *>( &ipBytesNetworkOrder );
|
|
Tins::IPv6Address::const_iterator it = ipv6Address.end();
|
|
for (int i = lastAddressByteIndex; i > -1; i--)
|
|
{
|
|
it--;
|
|
ptrIpByte[i] = *it;
|
|
}
|
|
|
|
return ipBytesNetworkOrder;
|
|
}
|
|
void IpAddressTranslator::toIpv6Address(IN const Tins::IPv4Address & ipv4Address, INOUT Tins::IPv6Address & resultIpv6Address)
|
|
{
|
|
Tins::IPv6Address::iterator it = resultIpv6Address.end();
|
|
uint32_t ipBytesNetworkOrder = ipv4Address;
|
|
uint8_t * ptrIpByte = reinterpret_cast<uint8_t *>( &ipBytesNetworkOrder );
|
|
for (int i = lastAddressByteIndex; i > -1; i--)
|
|
{
|
|
it--;
|
|
*it = ptrIpByte[i];
|
|
}
|
|
}
|