refactor methods
This commit is contained in:
@@ -4,11 +4,13 @@
|
||||
<tins/tins.h>
|
||||
<thread>
|
||||
|
||||
1439330649 source:/home/dev/projects/1T1NAT/src/map/natmap.cpp
|
||||
1439937901 source:/home/dev/projects/1T1NAT/src/map/natmap.cpp
|
||||
"natmap.h"
|
||||
|
||||
1439327491 /home/dev/projects/1T1NAT/src/map/natmap.h
|
||||
1439938543 /home/dev/projects/1T1NAT/src/map/natmap.h
|
||||
<vector>
|
||||
<map>
|
||||
<queue>
|
||||
<tins/tins.h>
|
||||
|
||||
1439326884 source:/home/dev/projects/1T1NAT/test/unittest_main.cpp
|
||||
@@ -17,7 +19,7 @@
|
||||
<tins/tins.h>
|
||||
<vector>
|
||||
|
||||
1439329061 source:/home/dev/projects/1T1NAT/test/case/testcaseipcalc.cpp
|
||||
1439500558 source:/home/dev/projects/1T1NAT/test/case/testcaseipcalc.cpp
|
||||
"UnitTest++/UnitTest++.h"
|
||||
"../../src/map/natmap.h"
|
||||
<tins/tins.h>
|
||||
|
||||
@@ -1,29 +1,24 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
|
||||
<CodeBlocks_layout_file>
|
||||
<ActiveTarget name="UnitTest" />
|
||||
<File name="test/unittest_main.cpp" open="1" top="0" tabpos="3" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
|
||||
<Cursor>
|
||||
<Cursor1 position="107" topLine="0" />
|
||||
</Cursor>
|
||||
</File>
|
||||
<File name="test/case/testcaseipcalc.cpp" open="1" top="1" tabpos="4" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
|
||||
<Cursor>
|
||||
<Cursor1 position="3105" topLine="0" />
|
||||
</Cursor>
|
||||
</File>
|
||||
<File name="main.cpp" open="0" top="0" tabpos="1" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
|
||||
<Cursor>
|
||||
<Cursor1 position="1492" topLine="54" />
|
||||
</Cursor>
|
||||
</File>
|
||||
<File name="src/map/natmap.h" open="1" top="0" tabpos="2" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
|
||||
<Cursor>
|
||||
<Cursor1 position="16" topLine="4" />
|
||||
<Cursor1 position="1544" topLine="21" />
|
||||
</Cursor>
|
||||
</File>
|
||||
<File name="src/map/natmap.cpp" open="1" top="0" tabpos="1" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
|
||||
<File name="main.cpp" open="1" top="0" tabpos="4" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
|
||||
<Cursor>
|
||||
<Cursor1 position="863" topLine="18" />
|
||||
<Cursor1 position="1963" topLine="51" />
|
||||
</Cursor>
|
||||
</File>
|
||||
<File name="src/map/natmap.cpp" open="1" top="1" tabpos="1" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
|
||||
<Cursor>
|
||||
<Cursor1 position="258" topLine="138" />
|
||||
</Cursor>
|
||||
</File>
|
||||
<File name="test/unittest_main.cpp" open="0" top="0" tabpos="3" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
|
||||
<Cursor>
|
||||
<Cursor1 position="107" topLine="0" />
|
||||
</Cursor>
|
||||
</File>
|
||||
<File name="test/suite/testsuitenat.cpp" open="0" top="0" tabpos="4" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
|
||||
@@ -31,4 +26,9 @@
|
||||
<Cursor1 position="87" topLine="0" />
|
||||
</Cursor>
|
||||
</File>
|
||||
<File name="test/case/testcaseipcalc.cpp" open="1" top="0" tabpos="3" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
|
||||
<Cursor>
|
||||
<Cursor1 position="3433" topLine="62" />
|
||||
</Cursor>
|
||||
</File>
|
||||
</CodeBlocks_layout_file>
|
||||
|
||||
1
main.cpp
1
main.cpp
@@ -39,6 +39,7 @@ bool arpm2(const PDU &pdu)
|
||||
{
|
||||
// Retrieve the ARP layer
|
||||
const ARP &arp = pdu.rfind_pdu<ARP>();
|
||||
|
||||
std::cout << arp.opcode()<< std::endl;
|
||||
if (arp.opcode() == ARP::REPLY)
|
||||
{
|
||||
|
||||
@@ -8,7 +8,7 @@ NatMap::NatMap(): NatMap(Tins::NetworkInterface::all())
|
||||
//ctor
|
||||
}
|
||||
|
||||
NatMap::NatMap(std::vector<Tins::NetworkInterface> interfaceList) : interfaces(interfaceList)
|
||||
NatMap::NatMap(NetworkInterfaceList interfaceList) : interfaces(interfaceList), arpMap(), transMap(), pduQueue(), zeroIp()
|
||||
{
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ NatMap::~NatMap()
|
||||
//dtor
|
||||
}
|
||||
|
||||
NatMap::NatMap(const NatMap& other) : interfaces(other.interfaces)
|
||||
NatMap::NatMap(const NatMap& other) : interfaces(other.interfaces), arpMap(), transMap(), pduQueue(), zeroIp()
|
||||
{
|
||||
//copy ctor
|
||||
}
|
||||
@@ -27,6 +27,9 @@ NatMap& NatMap::operator=(const NatMap& rhs)
|
||||
if (this == &rhs) return *this; // handle self assignment
|
||||
|
||||
interfaces = rhs.interfaces;
|
||||
arpMap = rhs.arpMap;
|
||||
transMap = rhs.transMap;
|
||||
pduQueue = rhs.pduQueue;
|
||||
return *this;
|
||||
}
|
||||
|
||||
@@ -39,4 +42,121 @@ const Tins::IPv4Address NatMap::mapIPv4Address(const Tins::IPv4Address & ip, con
|
||||
return Tins::IPv4Address(resultIp);
|
||||
}
|
||||
|
||||
const Tins::IPv4Range NatMap::calcIpRange(const Tins::NetworkInterface::Info & interfaceInfo)
|
||||
{
|
||||
return Tins::IPv4Range::from_mask(interfaceInfo.ip_addr, interfaceInfo.netmask);
|
||||
}
|
||||
|
||||
void NatMap::handlePdu(const Tins::PDU * pdu)
|
||||
{
|
||||
if (pdu == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
switch(pdu->pdu_type())
|
||||
{
|
||||
case Tins::PDU::ARP:
|
||||
handleArp(static_cast<const Tins::ARP *>(pdu));
|
||||
break;
|
||||
case Tins::PDU::IP:
|
||||
handleIp(static_cast<const Tins::IP *>(pdu));
|
||||
return;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
handlePdu(const_cast<Tins::PDU *>(pdu->inner_pdu()));
|
||||
}
|
||||
|
||||
void NatMap::handleIp(const Tins::IP * ip)
|
||||
{
|
||||
IpAdressMap::iterator transIpIter = transMap.find(ip->dst_addr());
|
||||
if (transIpIter != transMap.end())
|
||||
{
|
||||
// handle know traslation ip
|
||||
TranslateIpPacket(ip, transIpIter->second);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Determine Traslation Ip
|
||||
}
|
||||
}
|
||||
|
||||
void NatMap::TranslateIpPacket(const Tins::IP * ip, const Tins::IPv4Address & transIp)
|
||||
{
|
||||
// copy ip packet, for modifaktion
|
||||
Tins::IP * modifyIp = ip->clone();
|
||||
|
||||
// set translated dst address
|
||||
modifyIp->dst_addr(transIp);
|
||||
|
||||
// translate src adress
|
||||
const Tins::IPv4Address & originSrc = ip->src_addr();
|
||||
IpAdressMap::iterator transIpIter = transMap.find(originSrc);
|
||||
Tins::IPv4Address transSrcAttr;
|
||||
if (transIpIter != transMap.end())
|
||||
{
|
||||
// set translated src address
|
||||
transSrcAttr = transIpIter->second;
|
||||
}
|
||||
else
|
||||
{
|
||||
transSrcAttr = InsertOrUdpateTranslateIpAddress(originSrc, interfaces);
|
||||
}
|
||||
|
||||
modifyIp->src_addr(transSrcAttr);
|
||||
}
|
||||
|
||||
Tins::IPv4Address NatMap::InsertOrUdpateTranslateIpAddress(const Tins::IPv4Address & originIp, NetworkInterfaceList & interfaceList)
|
||||
{
|
||||
// calc translated ip address for first up and not same interfaces
|
||||
for (NetworkInterfaceList::iterator interfaceIter = interfaceList.begin() ; interfaceIter != interfaceList.end(); ++interfaceIter)
|
||||
{
|
||||
// insert or update translated ip address
|
||||
const Tins::NetworkInterface::Info & interfaceInfo = interfaceIter->info();
|
||||
if (!interfaceInfo.is_up)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
Tins::IPv4Range range = calcIpRange(interfaceInfo);//networkInterfaceIpRangeMap[interfaceInfo.ip_addr];
|
||||
if (range.contains(originIp))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
return InsertOrUdpateTranslateIpAddress(originIp, interfaceInfo);
|
||||
}
|
||||
|
||||
return zeroIp;
|
||||
}
|
||||
|
||||
Tins::IPv4Address NatMap::InsertOrUdpateTranslateIpAddress(const Tins::IPv4Address & originIp, const Tins::NetworkInterface::Info & interfaceInfo)
|
||||
{
|
||||
// translated ip address
|
||||
Tins::IPv4Address transAddr = mapIPv4Address(originIp, interfaceInfo);
|
||||
|
||||
// insert forward translation
|
||||
transMap.insert(IPv4AddressEntry(originIp, transAddr));
|
||||
|
||||
// insert or update backward translation
|
||||
IpAdressMap::iterator transIpIter = transMap.find(transAddr);
|
||||
if (transIpIter != transMap.end())
|
||||
{
|
||||
transMap[transAddr] = originIp;
|
||||
}
|
||||
else
|
||||
{
|
||||
transMap.insert(IPv4AddressEntry(transAddr,originIp));
|
||||
}
|
||||
|
||||
return transAddr;
|
||||
}
|
||||
|
||||
void NatMap::handleArp(const Tins::ARP * /*arp*/)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
#define NATMAP_H
|
||||
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <queue>
|
||||
#include <tins/tins.h>
|
||||
|
||||
namespace otonat
|
||||
@@ -9,18 +11,37 @@ namespace otonat
|
||||
class NatMap
|
||||
{
|
||||
public:
|
||||
typedef std::vector<Tins::NetworkInterface> NetworkInterfaceList;
|
||||
typedef std::queue<const Tins::PDU *> PduQueue;
|
||||
typedef std::pair<Tins::IPv4Address,Tins::IPv4Address> IPv4AddressEntry;
|
||||
typedef std::map<Tins::IPv4Address, Tins::HWAddress<6>> IpAdressMacMap;
|
||||
typedef std::map<Tins::IPv4Address, Tins::IPv4Address> IpAdressMap;
|
||||
|
||||
|
||||
NatMap();
|
||||
NatMap(std::vector<Tins::NetworkInterface> interfaceList);
|
||||
NatMap(NetworkInterfaceList interfaceList);
|
||||
virtual ~NatMap();
|
||||
NatMap(const NatMap& other);
|
||||
NatMap& operator=(const NatMap& other);
|
||||
std::vector<Tins::NetworkInterface> interfaces;
|
||||
NetworkInterfaceList interfaces;
|
||||
IpAdressMacMap arpMap;
|
||||
IpAdressMap transMap;
|
||||
PduQueue pduQueue;
|
||||
void handlePdu(const Tins::PDU * pdu);
|
||||
|
||||
static const Tins::IPv4Address mapIPv4Address(const Tins::IPv4Address & ip, const Tins::NetworkInterface::Info & interfaceInfo);
|
||||
|
||||
static const Tins::IPv4Range calcIpRange(const Tins::NetworkInterface::Info & interfaceInfo);
|
||||
|
||||
protected:
|
||||
|
||||
private:
|
||||
|
||||
void handleIp(const Tins::IP * ip);
|
||||
void handleArp(const Tins::ARP * arp);
|
||||
Tins::IPv4Address InsertOrUdpateTranslateIpAddress(const Tins::IPv4Address & originIp, const Tins::NetworkInterface::Info & interfaceInfo);
|
||||
Tins::IPv4Address InsertOrUdpateTranslateIpAddress(const Tins::IPv4Address & originIp, NetworkInterfaceList & interfaceList);
|
||||
void TranslateIpPacket(const Tins::IP * ip, const Tins::IPv4Address & transIp);
|
||||
Tins::IPv4Address zeroIp;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -94,4 +94,11 @@ SUITE(NatTests)
|
||||
CHECK_EQUAL(expetedIp,resultIp);
|
||||
}
|
||||
|
||||
TEST(TestHandlePDU)
|
||||
{
|
||||
Tins::EthernetII eth = Tins::EthernetII() / Tins::IP() / Tins::TCP();
|
||||
otonat::NatMap natMap = otonat::NatMap();
|
||||
natMap.handlePdu(ð);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user