init
This commit is contained in:
31
.gitignore
vendored
Normal file
31
.gitignore
vendored
Normal file
@@ -0,0 +1,31 @@
|
||||
# Created by https://www.gitignore.io
|
||||
|
||||
### C++ ###
|
||||
# Compiled Object files
|
||||
*.slo
|
||||
*.lo
|
||||
*.o
|
||||
*.obj
|
||||
|
||||
# Precompiled Headers
|
||||
*.gch
|
||||
*.pch
|
||||
|
||||
# Compiled Dynamic libraries
|
||||
*.so
|
||||
*.dylib
|
||||
*.dll
|
||||
|
||||
# Fortran module files
|
||||
*.mod
|
||||
|
||||
# Compiled Static libraries
|
||||
*.lai
|
||||
*.la
|
||||
*.a
|
||||
*.lib
|
||||
|
||||
# Executables
|
||||
*.exe
|
||||
*.out
|
||||
*.app
|
||||
95
1T1NAT.cbp
Normal file
95
1T1NAT.cbp
Normal file
@@ -0,0 +1,95 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
|
||||
<CodeBlocks_project_file>
|
||||
<FileVersion major="1" minor="6" />
|
||||
<Project>
|
||||
<Option title="1T1NAT" />
|
||||
<Option pch_mode="2" />
|
||||
<Option compiler="gcc" />
|
||||
<Option extended_obj_names="1" />
|
||||
<Build>
|
||||
<Target title="UnitTest">
|
||||
<Option output="bin/UnitTest/1T1NAT" prefix_auto="1" extension_auto="1" />
|
||||
<Option object_output="obj/UnitTest/" />
|
||||
<Option type="1" />
|
||||
<Option compiler="gcc" />
|
||||
<Option host_application="sudo" />
|
||||
<Option run_host_application_in_terminal="1" />
|
||||
<Compiler>
|
||||
<Add option="-std=c++11" />
|
||||
<Add option="-Wfatal-errors" />
|
||||
<Add option="-Wextra" />
|
||||
<Add option="-Wall" />
|
||||
<Add option="-g" />
|
||||
<Add option="-pthread" />
|
||||
</Compiler>
|
||||
<Linker>
|
||||
<Add library="/usr/local/lib/libtins.so.3.3" />
|
||||
<Add library="/lib/libpthread.so" />
|
||||
<Add library="/usr/local/lib/libUnitTest++.a" />
|
||||
</Linker>
|
||||
</Target>
|
||||
<Target title="Debug">
|
||||
<Option output="bin/Debug/1T1NAT" prefix_auto="1" extension_auto="1" />
|
||||
<Option object_output="obj/Debug/" />
|
||||
<Option type="1" />
|
||||
<Option compiler="gcc" />
|
||||
<Option host_application="sudo" />
|
||||
<Option run_host_application_in_terminal="1" />
|
||||
<Compiler>
|
||||
<Add option="-std=c++11" />
|
||||
<Add option="-g" />
|
||||
<Add option="-pthread" />
|
||||
</Compiler>
|
||||
<Linker>
|
||||
<Add library="/usr/local/lib/libtins.so.3.3" />
|
||||
<Add library="/lib64/libpthread.so" />
|
||||
</Linker>
|
||||
</Target>
|
||||
<Target title="Release">
|
||||
<Option output="bin/Release/1T1NAT" prefix_auto="1" extension_auto="1" />
|
||||
<Option object_output="obj/Release/" />
|
||||
<Option type="1" />
|
||||
<Option compiler="gcc" />
|
||||
<Compiler>
|
||||
<Add option="-O2" />
|
||||
</Compiler>
|
||||
<Linker>
|
||||
<Add option="-s" />
|
||||
</Linker>
|
||||
</Target>
|
||||
</Build>
|
||||
<Compiler>
|
||||
<Add option="-Wall" />
|
||||
<Add option="-fexceptions" />
|
||||
</Compiler>
|
||||
<Unit filename="main.cpp">
|
||||
<Option target="Debug" />
|
||||
<Option target="Release" />
|
||||
</Unit>
|
||||
<Unit filename="src/map/natmap.cpp" />
|
||||
<Unit filename="src/map/natmap.h" />
|
||||
<Unit filename="test/case/testcaseipcalc.cpp">
|
||||
<Option target="UnitTest" />
|
||||
</Unit>
|
||||
<Unit filename="test/suite/testsuitearp.cpp">
|
||||
<Option target="UnitTest" />
|
||||
</Unit>
|
||||
<Unit filename="test/suite/testsuiteip.cpp">
|
||||
<Option target="UnitTest" />
|
||||
</Unit>
|
||||
<Unit filename="test/suite/testsuitenat.cpp">
|
||||
<Option target="UnitTest" />
|
||||
</Unit>
|
||||
<Unit filename="test/unittest_main.cpp">
|
||||
<Option target="UnitTest" />
|
||||
</Unit>
|
||||
<Extensions>
|
||||
<envvars />
|
||||
<code_completion />
|
||||
<lib_finder disable_auto="1">
|
||||
<lib name="libtins" />
|
||||
</lib_finder>
|
||||
<debugger />
|
||||
</Extensions>
|
||||
</Project>
|
||||
</CodeBlocks_project_file>
|
||||
33
1T1NAT.depend
Normal file
33
1T1NAT.depend
Normal file
@@ -0,0 +1,33 @@
|
||||
# depslib dependency file v1.0
|
||||
1439147884 source:/home/dev/projects/1T1NAT/main.cpp
|
||||
<iostream>
|
||||
<tins/tins.h>
|
||||
<thread>
|
||||
|
||||
1439330649 source:/home/dev/projects/1T1NAT/src/map/natmap.cpp
|
||||
"natmap.h"
|
||||
|
||||
1439327491 /home/dev/projects/1T1NAT/src/map/natmap.h
|
||||
<vector>
|
||||
<tins/tins.h>
|
||||
|
||||
1439326884 source:/home/dev/projects/1T1NAT/test/unittest_main.cpp
|
||||
"UnitTest++/UnitTest++.h"
|
||||
"../src/map/natmap.h"
|
||||
<tins/tins.h>
|
||||
<vector>
|
||||
|
||||
1439329061 source:/home/dev/projects/1T1NAT/test/case/testcaseipcalc.cpp
|
||||
"UnitTest++/UnitTest++.h"
|
||||
"../../src/map/natmap.h"
|
||||
<tins/tins.h>
|
||||
<vector>
|
||||
|
||||
1439325103 source:/home/dev/projects/1T1NAT/test/suite/testsuitearp.cpp
|
||||
|
||||
1439325103 source:/home/dev/projects/1T1NAT/test/suite/testsuiteip.cpp
|
||||
|
||||
1439327293 source:/home/dev/projects/1T1NAT/test/suite/testsuitenat.cpp
|
||||
"UnitTest++/UnitTest++.h"
|
||||
"../../src/map/natmap.h"
|
||||
|
||||
34
1T1NAT.layout
Normal file
34
1T1NAT.layout
Normal file
@@ -0,0 +1,34 @@
|
||||
<?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" />
|
||||
</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">
|
||||
<Cursor>
|
||||
<Cursor1 position="863" topLine="18" />
|
||||
</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">
|
||||
<Cursor>
|
||||
<Cursor1 position="87" topLine="0" />
|
||||
</Cursor>
|
||||
</File>
|
||||
</CodeBlocks_layout_file>
|
||||
117
main.cpp
Normal file
117
main.cpp
Normal file
@@ -0,0 +1,117 @@
|
||||
#include <iostream>
|
||||
#include <tins/tins.h>
|
||||
#include <thread>
|
||||
|
||||
using namespace std;
|
||||
using namespace Tins;
|
||||
|
||||
const IPv4Address calcAdress(const IPv4Address & ip)
|
||||
{
|
||||
IPv4Address incommingIp = IPv4Address(ip);
|
||||
IPv4Address networkStartIp = IPv4Address("10.0.0.0");
|
||||
IPv4Address subnetp = IPv4Address("255.255.240.0");
|
||||
|
||||
uint32_t ipInt = (incommingIp & ~subnetp) | networkStartIp;
|
||||
IPv4Address result = IPv4Address(ipInt);
|
||||
std::cout << "Destination address: " << incommingIp << std::endl;
|
||||
std::cout << "new Destination address: " << result<< std::endl;
|
||||
std::cout << "####"<< std::endl;
|
||||
return result;
|
||||
}
|
||||
|
||||
bool arpm(const PDU &pdu)
|
||||
{
|
||||
// Retrieve the ARP layer
|
||||
const ARP &arp = pdu.rfind_pdu<ARP>();
|
||||
std::cout << arp.opcode()<< std::endl;
|
||||
if (arp.opcode() == ARP::REQUEST)
|
||||
{
|
||||
PacketSender sender;
|
||||
const NetworkInterface iface("vboxnet0");
|
||||
EthernetII req = ARP::make_arp_request(calcAdress(arp.target_ip_addr()),"10.0.3.42","08:00:27:d3:ef:1e");
|
||||
sender.send(req, iface);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
PacketSender sender;
|
||||
const NetworkInterface iface("vboxnet1");
|
||||
EthernetII rep = ARP::make_arp_reply("172.16.3.42","172.17.0.20","08:00:27:d3:ef:1e","08:00:27:29:f2:55");
|
||||
sender.send(rep, iface);
|
||||
} else if (arp.target_ip_addr().to_string() == "10.0.3.42")
|
||||
{
|
||||
PacketSender sender;
|
||||
const NetworkInterface iface("vboxnet0");
|
||||
EthernetII rep = ARP::make_arp_reply("10.0.0.20","10.0.3.42","08:00:27:29:f2:55","08:00:27:d3:ef:1e");
|
||||
sender.send(rep, iface);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool doo(PDU &some_pdu)
|
||||
{
|
||||
//PacketWriter writer("before.pcap", PacketWriter::ETH2);
|
||||
//writer.write(some_pdu);
|
||||
|
||||
IP &ip = some_pdu.rfind_pdu<IP>();
|
||||
ip.dst_addr(calcAdress(ip.dst_addr()));
|
||||
ip.src_addr("10.0.3.42");
|
||||
//writer = PacketWriter("after.pcap", PacketWriter::ETH2);
|
||||
//writer.write(some_pdu);
|
||||
PacketSender sender;
|
||||
some_pdu.send(sender,"vboxnet0");
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void test1()
|
||||
{
|
||||
SnifferConfiguration config;
|
||||
config.set_promisc_mode(true);
|
||||
config.set_immediate_mode(true);
|
||||
//config.set_filter("ip src 192.168.0.100");
|
||||
Sniffer sniffer("vboxnet1", config);
|
||||
sniffer.sniff_loop(arpm);
|
||||
}
|
||||
|
||||
void test2()
|
||||
{
|
||||
SnifferConfiguration config;
|
||||
config.set_promisc_mode(true);
|
||||
config.set_immediate_mode(true);
|
||||
//config.set_filter("ip src 192.168.0.100");
|
||||
Sniffer sniffer("vboxnet0", config);
|
||||
sniffer.sniff_loop(arpm2);
|
||||
}
|
||||
|
||||
void test3()
|
||||
{
|
||||
SnifferConfiguration config;
|
||||
config.set_promisc_mode(true);
|
||||
config.set_immediate_mode(true);
|
||||
//config.set_filter("ip src 192.168.0.100");
|
||||
Sniffer sniffer("vboxnet1", config);
|
||||
sniffer.sniff_loop(doo);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
thread t1(test1);
|
||||
thread t2(test2);
|
||||
thread t3(test3);
|
||||
t1.join();
|
||||
t2.join();
|
||||
t3.join();
|
||||
return 0;
|
||||
}
|
||||
|
||||
42
src/map/natmap.cpp
Normal file
42
src/map/natmap.cpp
Normal file
@@ -0,0 +1,42 @@
|
||||
#include "natmap.h"
|
||||
|
||||
namespace otonat
|
||||
{
|
||||
|
||||
NatMap::NatMap(): NatMap(Tins::NetworkInterface::all())
|
||||
{
|
||||
//ctor
|
||||
}
|
||||
|
||||
NatMap::NatMap(std::vector<Tins::NetworkInterface> interfaceList) : interfaces(interfaceList)
|
||||
{
|
||||
}
|
||||
|
||||
NatMap::~NatMap()
|
||||
{
|
||||
//dtor
|
||||
}
|
||||
|
||||
NatMap::NatMap(const NatMap& other) : interfaces(other.interfaces)
|
||||
{
|
||||
//copy ctor
|
||||
}
|
||||
|
||||
NatMap& NatMap::operator=(const NatMap& rhs)
|
||||
{
|
||||
if (this == &rhs) return *this; // handle self assignment
|
||||
|
||||
interfaces = rhs.interfaces;
|
||||
return *this;
|
||||
}
|
||||
|
||||
const Tins::IPv4Address NatMap::mapIPv4Address(const Tins::IPv4Address & ip, const Tins::NetworkInterface::Info & interfaceInfo)
|
||||
{
|
||||
const uint32_t & netmask = interfaceInfo.netmask;
|
||||
const uint32_t & interfaceIp = interfaceInfo.ip_addr;
|
||||
const uint32_t networkStartIp = interfaceIp & netmask;
|
||||
const uint32_t resultIp = (ip & ~netmask) | networkStartIp;
|
||||
return Tins::IPv4Address(resultIp);
|
||||
}
|
||||
|
||||
}
|
||||
28
src/map/natmap.h
Normal file
28
src/map/natmap.h
Normal file
@@ -0,0 +1,28 @@
|
||||
#ifndef NATMAP_H
|
||||
#define NATMAP_H
|
||||
|
||||
#include <vector>
|
||||
#include <tins/tins.h>
|
||||
|
||||
namespace otonat
|
||||
{
|
||||
class NatMap
|
||||
{
|
||||
public:
|
||||
NatMap();
|
||||
NatMap(std::vector<Tins::NetworkInterface> interfaceList);
|
||||
virtual ~NatMap();
|
||||
NatMap(const NatMap& other);
|
||||
NatMap& operator=(const NatMap& other);
|
||||
std::vector<Tins::NetworkInterface> interfaces;
|
||||
static const Tins::IPv4Address mapIPv4Address(const Tins::IPv4Address & ip, const Tins::NetworkInterface::Info & interfaceInfo);
|
||||
|
||||
protected:
|
||||
|
||||
private:
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
#endif // NATMAP_H
|
||||
97
test/case/testcaseipcalc.cpp
Normal file
97
test/case/testcaseipcalc.cpp
Normal file
@@ -0,0 +1,97 @@
|
||||
#include "UnitTest++/UnitTest++.h"
|
||||
#include "../../src/map/natmap.h"
|
||||
#include <tins/tins.h>
|
||||
#include <vector>
|
||||
|
||||
SUITE(NatTests)
|
||||
{
|
||||
class NatTestFixure
|
||||
{
|
||||
public:
|
||||
otonat::NatMap natMap = otonat::NatMap();
|
||||
|
||||
Tins::IPv4Address deviceIpEth0 = Tins::IPv4Address("10.0.3.40");
|
||||
|
||||
Tins::IPv4Address deviceIpEth1 = Tins::IPv4Address("192.168.23.42");
|
||||
|
||||
Tins::IPv4Address deviceIpEth2 = Tins::IPv4Address("172.27.123.4");
|
||||
|
||||
Tins::NetworkInterface::Info eth0Info =
|
||||
{
|
||||
.ip_addr = Tins::IPv4Address("10.0.0.2"),
|
||||
.netmask = Tins::IPv4Address("255.0.0.0"),
|
||||
.bcast_addr = Tins::IPv4Address("10.255.255.255"),
|
||||
.hw_addr = Tins::HWAddress<6>("00:00:00:00:00:01"),
|
||||
.is_up = true
|
||||
};
|
||||
|
||||
Tins::NetworkInterface::Info eth1Info =
|
||||
{
|
||||
.ip_addr = Tins::IPv4Address("192.168.23.42"),
|
||||
.netmask = Tins::IPv4Address("255.255.255.0"),
|
||||
.bcast_addr = Tins::IPv4Address("192.168.23.255"),
|
||||
.hw_addr = Tins::HWAddress<6>("00:00:00:00:00:02"),
|
||||
.is_up = true
|
||||
};
|
||||
|
||||
Tins::NetworkInterface::Info eth2Info =
|
||||
{
|
||||
.ip_addr = Tins::IPv4Address("172.16.47.11"),
|
||||
.netmask = Tins::IPv4Address("255.240.0.0"),
|
||||
.bcast_addr = Tins::IPv4Address("172.31.255.255"),
|
||||
.hw_addr = Tins::HWAddress<6>("00:00:00:00:00:03"),
|
||||
.is_up = true
|
||||
};
|
||||
};
|
||||
|
||||
TEST_FIXTURE(NatTestFixure, NetworkInterfaces)
|
||||
{
|
||||
CHECK(!natMap.interfaces.empty());
|
||||
}
|
||||
|
||||
TEST_FIXTURE(NatTestFixure, IpCalcEth0)
|
||||
{
|
||||
Tins::IPv4Address expetedIp = deviceIpEth0;
|
||||
Tins::IPv4Address resultIp = natMap.mapIPv4Address(deviceIpEth0, eth0Info);
|
||||
CHECK_EQUAL(expetedIp,resultIp);
|
||||
|
||||
expetedIp = Tins::IPv4Address("10.168.23.42");
|
||||
resultIp = natMap.mapIPv4Address(deviceIpEth1, eth0Info);
|
||||
CHECK_EQUAL(expetedIp,resultIp);
|
||||
|
||||
expetedIp = Tins::IPv4Address("10.27.123.4");
|
||||
resultIp = natMap.mapIPv4Address(deviceIpEth2, eth0Info);
|
||||
CHECK_EQUAL(expetedIp,resultIp);
|
||||
}
|
||||
|
||||
TEST_FIXTURE(NatTestFixure, IpCalcEth1)
|
||||
{
|
||||
Tins::IPv4Address expetedIp = Tins::IPv4Address("192.168.23.40");
|
||||
Tins::IPv4Address resultIp = natMap.mapIPv4Address(deviceIpEth0, eth1Info);
|
||||
CHECK_EQUAL(expetedIp,resultIp);
|
||||
|
||||
expetedIp = deviceIpEth1;
|
||||
resultIp = natMap.mapIPv4Address(deviceIpEth1, eth1Info);
|
||||
CHECK_EQUAL(expetedIp,resultIp);
|
||||
|
||||
expetedIp = Tins::IPv4Address("192.168.23.4");
|
||||
resultIp = natMap.mapIPv4Address(deviceIpEth2, eth1Info);
|
||||
CHECK_EQUAL(expetedIp,resultIp);
|
||||
}
|
||||
|
||||
TEST_FIXTURE(NatTestFixure, IpCalcEth2)
|
||||
{
|
||||
Tins::IPv4Address expetedIp = Tins::IPv4Address("172.16.3.40");
|
||||
Tins::IPv4Address resultIp = natMap.mapIPv4Address(deviceIpEth0, eth2Info);
|
||||
CHECK_EQUAL(expetedIp,resultIp);
|
||||
|
||||
expetedIp = Tins::IPv4Address("172.24.23.42");
|
||||
resultIp = natMap.mapIPv4Address(deviceIpEth1, eth2Info);
|
||||
CHECK_EQUAL(expetedIp,resultIp);
|
||||
|
||||
expetedIp = deviceIpEth2;
|
||||
resultIp = natMap.mapIPv4Address(deviceIpEth2, eth2Info);
|
||||
CHECK_EQUAL(expetedIp,resultIp);
|
||||
}
|
||||
|
||||
}
|
||||
9
test/unittest_main.cpp
Normal file
9
test/unittest_main.cpp
Normal file
@@ -0,0 +1,9 @@
|
||||
#include "UnitTest++/UnitTest++.h"
|
||||
#include "../src/map/natmap.h"
|
||||
#include <tins/tins.h>
|
||||
#include <vector>
|
||||
|
||||
int main(int, const char *[])
|
||||
{
|
||||
return UnitTest::RunAllTests();
|
||||
}
|
||||
Reference in New Issue
Block a user