commit 48991fcdf77b9d7f863df794d2cd742d3a9ef5b1 Author: stubbfel Date: Wed Aug 12 00:29:54 2015 +0200 init diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8f3f28f --- /dev/null +++ b/.gitignore @@ -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 diff --git a/1T1NAT.cbp b/1T1NAT.cbp new file mode 100644 index 0000000..593597a --- /dev/null +++ b/1T1NAT.cbp @@ -0,0 +1,95 @@ + + + + + + diff --git a/1T1NAT.depend b/1T1NAT.depend new file mode 100644 index 0000000..578641b --- /dev/null +++ b/1T1NAT.depend @@ -0,0 +1,33 @@ +# depslib dependency file v1.0 +1439147884 source:/home/dev/projects/1T1NAT/main.cpp + + + + +1439330649 source:/home/dev/projects/1T1NAT/src/map/natmap.cpp + "natmap.h" + +1439327491 /home/dev/projects/1T1NAT/src/map/natmap.h + + + +1439326884 source:/home/dev/projects/1T1NAT/test/unittest_main.cpp + "UnitTest++/UnitTest++.h" + "../src/map/natmap.h" + + + +1439329061 source:/home/dev/projects/1T1NAT/test/case/testcaseipcalc.cpp + "UnitTest++/UnitTest++.h" + "../../src/map/natmap.h" + + + +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" + diff --git a/1T1NAT.layout b/1T1NAT.layout new file mode 100644 index 0000000..b31bd4d --- /dev/null +++ b/1T1NAT.layout @@ -0,0 +1,34 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..4c6f6cd --- /dev/null +++ b/main.cpp @@ -0,0 +1,117 @@ +#include +#include +#include + +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(); + 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(); + 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.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; +} + diff --git a/src/map/natmap.cpp b/src/map/natmap.cpp new file mode 100644 index 0000000..8ccd49c --- /dev/null +++ b/src/map/natmap.cpp @@ -0,0 +1,42 @@ +#include "natmap.h" + +namespace otonat +{ + +NatMap::NatMap(): NatMap(Tins::NetworkInterface::all()) +{ + //ctor +} + +NatMap::NatMap(std::vector 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); +} + +} diff --git a/src/map/natmap.h b/src/map/natmap.h new file mode 100644 index 0000000..f8ad57e --- /dev/null +++ b/src/map/natmap.h @@ -0,0 +1,28 @@ +#ifndef NATMAP_H +#define NATMAP_H + +#include +#include + +namespace otonat +{ +class NatMap +{ +public: + NatMap(); + NatMap(std::vector interfaceList); + virtual ~NatMap(); + NatMap(const NatMap& other); + NatMap& operator=(const NatMap& other); + std::vector interfaces; + static const Tins::IPv4Address mapIPv4Address(const Tins::IPv4Address & ip, const Tins::NetworkInterface::Info & interfaceInfo); + +protected: + +private: + +}; +} + + +#endif // NATMAP_H diff --git a/test/case/testcaseipcalc.cpp b/test/case/testcaseipcalc.cpp new file mode 100644 index 0000000..5930d92 --- /dev/null +++ b/test/case/testcaseipcalc.cpp @@ -0,0 +1,97 @@ +#include "UnitTest++/UnitTest++.h" +#include "../../src/map/natmap.h" +#include +#include + +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); + } + +} diff --git a/test/unittest_main.cpp b/test/unittest_main.cpp new file mode 100644 index 0000000..e8a5055 --- /dev/null +++ b/test/unittest_main.cpp @@ -0,0 +1,9 @@ +#include "UnitTest++/UnitTest++.h" +#include "../src/map/natmap.h" +#include +#include + +int main(int, const char *[]) +{ + return UnitTest::RunAllTests(); +}