diff --git a/nbproject/Makefile-Debug.mk b/nbproject/Makefile-Debug.mk
index 7a45012..aa76813 100644
--- a/nbproject/Makefile-Debug.mk
+++ b/nbproject/Makefile-Debug.mk
@@ -35,6 +35,7 @@ OBJECTDIR=${CND_BUILDDIR}/${CND_CONF}/${CND_PLATFORM}
# Object Files
OBJECTFILES= \
+ ${OBJECTDIR}/src/PduSender.o \
${OBJECTDIR}/src/main.o \
${OBJECTDIR}/src/map/NatRange.o \
${OBJECTDIR}/src/map/natmap.o
@@ -70,6 +71,11 @@ ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/1t1nat: ${OBJECTFILES}
${MKDIR} -p ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}
${LINK.cc} -o ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/1t1nat ${OBJECTFILES} ${LDLIBSOPTIONS}
+${OBJECTDIR}/src/PduSender.o: src/PduSender.cpp
+ ${MKDIR} -p ${OBJECTDIR}/src
+ ${RM} "$@.d"
+ $(COMPILE.cc) -g -Wall -std=c++11 -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/src/PduSender.o src/PduSender.cpp
+
${OBJECTDIR}/src/main.o: src/main.cpp
${MKDIR} -p ${OBJECTDIR}/src
${RM} "$@.d"
@@ -107,6 +113,19 @@ ${TESTDIR}/tests/nattestrunner.o: tests/nattestrunner.cpp
$(COMPILE.cc) -g -Wall -I. -std=c++11 `cppunit-config --cflags` -MMD -MP -MF "$@.d" -o ${TESTDIR}/tests/nattestrunner.o tests/nattestrunner.cpp
+${OBJECTDIR}/src/PduSender_nomain.o: ${OBJECTDIR}/src/PduSender.o src/PduSender.cpp
+ ${MKDIR} -p ${OBJECTDIR}/src
+ @NMOUTPUT=`${NM} ${OBJECTDIR}/src/PduSender.o`; \
+ if (echo "$$NMOUTPUT" | ${GREP} '|main$$') || \
+ (echo "$$NMOUTPUT" | ${GREP} 'T main$$') || \
+ (echo "$$NMOUTPUT" | ${GREP} 'T _main$$'); \
+ then \
+ ${RM} "$@.d";\
+ $(COMPILE.cc) -g -Wall -std=c++11 -Dmain=__nomain -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/src/PduSender_nomain.o src/PduSender.cpp;\
+ else \
+ ${CP} ${OBJECTDIR}/src/PduSender.o ${OBJECTDIR}/src/PduSender_nomain.o;\
+ fi
+
${OBJECTDIR}/src/main_nomain.o: ${OBJECTDIR}/src/main.o src/main.cpp
${MKDIR} -p ${OBJECTDIR}/src
@NMOUTPUT=`${NM} ${OBJECTDIR}/src/main.o`; \
diff --git a/nbproject/Makefile-Release.mk b/nbproject/Makefile-Release.mk
index e9f07ec..84063e1 100644
--- a/nbproject/Makefile-Release.mk
+++ b/nbproject/Makefile-Release.mk
@@ -35,6 +35,7 @@ OBJECTDIR=${CND_BUILDDIR}/${CND_CONF}/${CND_PLATFORM}
# Object Files
OBJECTFILES= \
+ ${OBJECTDIR}/src/PduSender.o \
${OBJECTDIR}/src/main.o \
${OBJECTDIR}/src/map/NatRange.o \
${OBJECTDIR}/src/map/natmap.o
@@ -70,6 +71,11 @@ ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/1t1nat: ${OBJECTFILES}
${MKDIR} -p ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}
${LINK.cc} -o ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/1t1nat ${OBJECTFILES} ${LDLIBSOPTIONS}
+${OBJECTDIR}/src/PduSender.o: src/PduSender.cpp
+ ${MKDIR} -p ${OBJECTDIR}/src
+ ${RM} "$@.d"
+ $(COMPILE.cc) -O2 -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/src/PduSender.o src/PduSender.cpp
+
${OBJECTDIR}/src/main.o: src/main.cpp
${MKDIR} -p ${OBJECTDIR}/src
${RM} "$@.d"
@@ -107,6 +113,19 @@ ${TESTDIR}/tests/nattestrunner.o: tests/nattestrunner.cpp
$(COMPILE.cc) -O2 -I. `cppunit-config --cflags` -MMD -MP -MF "$@.d" -o ${TESTDIR}/tests/nattestrunner.o tests/nattestrunner.cpp
+${OBJECTDIR}/src/PduSender_nomain.o: ${OBJECTDIR}/src/PduSender.o src/PduSender.cpp
+ ${MKDIR} -p ${OBJECTDIR}/src
+ @NMOUTPUT=`${NM} ${OBJECTDIR}/src/PduSender.o`; \
+ if (echo "$$NMOUTPUT" | ${GREP} '|main$$') || \
+ (echo "$$NMOUTPUT" | ${GREP} 'T main$$') || \
+ (echo "$$NMOUTPUT" | ${GREP} 'T _main$$'); \
+ then \
+ ${RM} "$@.d";\
+ $(COMPILE.cc) -O2 -Dmain=__nomain -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/src/PduSender_nomain.o src/PduSender.cpp;\
+ else \
+ ${CP} ${OBJECTDIR}/src/PduSender.o ${OBJECTDIR}/src/PduSender_nomain.o;\
+ fi
+
${OBJECTDIR}/src/main_nomain.o: ${OBJECTDIR}/src/main.o src/main.cpp
${MKDIR} -p ${OBJECTDIR}/src
@NMOUTPUT=`${NM} ${OBJECTDIR}/src/main.o`; \
diff --git a/nbproject/Makefile-Unitest.mk b/nbproject/Makefile-Unitest.mk
index 54d0b26..20ed031 100644
--- a/nbproject/Makefile-Unitest.mk
+++ b/nbproject/Makefile-Unitest.mk
@@ -35,6 +35,7 @@ OBJECTDIR=${CND_BUILDDIR}/${CND_CONF}/${CND_PLATFORM}
# Object Files
OBJECTFILES= \
+ ${OBJECTDIR}/src/PduSender.o \
${OBJECTDIR}/src/main.o \
${OBJECTDIR}/src/map/NatRange.o \
${OBJECTDIR}/src/map/natmap.o
@@ -70,6 +71,11 @@ ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/1t1nat: ${OBJECTFILES}
${MKDIR} -p ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}
${LINK.cc} -o ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/1t1nat ${OBJECTFILES} ${LDLIBSOPTIONS}
+${OBJECTDIR}/src/PduSender.o: src/PduSender.cpp
+ ${MKDIR} -p ${OBJECTDIR}/src
+ ${RM} "$@.d"
+ $(COMPILE.cc) -g -Itest -std=c++11 -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/src/PduSender.o src/PduSender.cpp
+
${OBJECTDIR}/src/main.o: src/main.cpp
${MKDIR} -p ${OBJECTDIR}/src
${RM} "$@.d"
@@ -107,6 +113,19 @@ ${TESTDIR}/tests/nattestrunner.o: tests/nattestrunner.cpp
$(COMPILE.cc) -g -Itest -I. -std=c++11 `cppunit-config --cflags` -MMD -MP -MF "$@.d" -o ${TESTDIR}/tests/nattestrunner.o tests/nattestrunner.cpp
+${OBJECTDIR}/src/PduSender_nomain.o: ${OBJECTDIR}/src/PduSender.o src/PduSender.cpp
+ ${MKDIR} -p ${OBJECTDIR}/src
+ @NMOUTPUT=`${NM} ${OBJECTDIR}/src/PduSender.o`; \
+ if (echo "$$NMOUTPUT" | ${GREP} '|main$$') || \
+ (echo "$$NMOUTPUT" | ${GREP} 'T main$$') || \
+ (echo "$$NMOUTPUT" | ${GREP} 'T _main$$'); \
+ then \
+ ${RM} "$@.d";\
+ $(COMPILE.cc) -g -Itest -std=c++11 -Dmain=__nomain -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/src/PduSender_nomain.o src/PduSender.cpp;\
+ else \
+ ${CP} ${OBJECTDIR}/src/PduSender.o ${OBJECTDIR}/src/PduSender_nomain.o;\
+ fi
+
${OBJECTDIR}/src/main_nomain.o: ${OBJECTDIR}/src/main.o src/main.cpp
${MKDIR} -p ${OBJECTDIR}/src
@NMOUTPUT=`${NM} ${OBJECTDIR}/src/main.o`; \
diff --git a/nbproject/configurations.xml b/nbproject/configurations.xml
index 01c9221..a82e7fb 100644
--- a/nbproject/configurations.xml
+++ b/nbproject/configurations.xml
@@ -8,6 +8,7 @@
src/map/natmap.h
src/map/NatRange.h
+ src/PduSender.h
src/map/natmap.cpp
src/map/NatRange.cpp
+ src/PduSender.cpp
src/main.cpp
+ -
+
+ -
+
-
-
@@ -136,6 +142,10 @@
+
-
+
+ -
+
-
-
@@ -187,6 +197,10 @@
+
-
+
+ -
+
-
-
diff --git a/src/PduSender.cpp b/src/PduSender.cpp
new file mode 100644
index 0000000..d935da6
--- /dev/null
+++ b/src/PduSender.cpp
@@ -0,0 +1,59 @@
+/*
+ * File: PduSender.cpp
+ * Author: dev
+ *
+ * Created on 16. September 2015, 20:31
+ */
+
+#include "PduSender.h"
+namespace otonat {
+
+ PduSender::PduSender(NatMap * map) {
+ this->map = map;
+ }
+
+ PduSender::PduSender(const PduSender& orig) {
+ this->map = orig.map;
+ }
+
+ PduSender& PduSender::operator=(const PduSender& rhs) {
+ if (this == &rhs) return *this; // handle self assignment
+
+ this->map = rhs.map;
+
+ return *this;
+ }
+
+ PduSender::~PduSender() {
+ }
+
+ void PduSender::SendPdusFromQueue() {
+ while (true) {
+ Tins::PDU * pdu = this->map->popPduOutgoingPduQueue();
+ if (pdu == nullptr) {
+ continue;
+ }
+
+ Tins::IPv4Address dstIp = zeroIp;
+ const Tins::ARP * arp = pdu->find_pdu();
+ if (arp != nullptr) {
+ dstIp = arp->target_ip_addr();
+ }
+
+ if (dstIp != zeroIp) {
+ const Tins::IP * ip = pdu->find_pdu();
+ if (ip != nullptr) {
+ dstIp = ip->dst_addr();
+ }
+ }
+
+ for (NatRange & range : this->map->ranges) {
+ if(range.calcIpRange(true).contains(dstIp)){
+ sender.send(*pdu, range.interface);
+ delete pdu;
+ return;
+ }
+ }
+ }
+ }
+}
diff --git a/src/PduSender.h b/src/PduSender.h
new file mode 100644
index 0000000..776b877
--- /dev/null
+++ b/src/PduSender.h
@@ -0,0 +1,31 @@
+/*
+ * File: PduSender.h
+ * Author: dev
+ *
+ * Created on 16. September 2015, 20:31
+ */
+
+#ifndef PDUSENDER_H
+#define PDUSENDER_H
+
+#include "map/natmap.h"
+#include
+
+namespace otonat {
+
+ class PduSender {
+ public:
+ PduSender(NatMap * map);
+ PduSender(const PduSender& orig);
+ PduSender& operator=(const PduSender& rhs);
+ virtual ~PduSender();
+ NatMap * map;
+
+ void SendPdusFromQueue();
+ private:
+ Tins::PacketSender sender;
+ };
+}
+
+#endif /* PDUSENDER_H */
+
diff --git a/src/map/natmap.cpp b/src/map/natmap.cpp
index d9a8a84..6b12c08 100644
--- a/src/map/natmap.cpp
+++ b/src/map/natmap.cpp
@@ -24,7 +24,7 @@ namespace otonat {
this->outgoingQueueMutex.unlock();
}
- NatMap::NatMap(const NatMap& other) : ranges(other.ranges), transMap(other.transMap), reqIpMap(other.reqIpMap), incommingPduQueue(other.incommingPduQueue), outgoingPduQueue(other.outgoingPduQueue), zeroIp(other.zeroIp) {
+ NatMap::NatMap(const NatMap& other) : ranges(other.ranges), transMap(other.transMap), reqIpMap(other.reqIpMap), incommingPduQueue(other.incommingPduQueue), outgoingPduQueue(other.outgoingPduQueue) {
}
NatMap& NatMap::operator=(const NatMap& rhs) {
@@ -239,13 +239,13 @@ namespace otonat {
bool NatMap::handleArpReply(Tins::ARP* arp) {
const Tins::IPv4Address targetIp = arp->target_ip_addr();
const Tins::IPv4Address transTargetIp = TranslateArpIp(targetIp);
- if (transTargetIp == this->zeroIp) {
+ if (transTargetIp == zeroIp) {
return false;
}
const Tins::IPv4Address senderIp = arp->sender_ip_addr();
const Tins::IPv4Address transSenderIp = TranslateArpIp(senderIp);
- if (transSenderIp == this->zeroIp) {
+ if (transSenderIp == zeroIp) {
return false;
}
@@ -260,7 +260,7 @@ namespace otonat {
if (transArpIpIter == transMap.end()) {
IpAdressMap::const_iterator transReqArpIpIter = this->reqIpMap.find(arpIp);
if (transReqArpIpIter == reqIpMap.end()) {
- return this->zeroIp;
+ return zeroIp;
}
transArpIp = transReqArpIpIter->second;
@@ -335,7 +335,7 @@ namespace otonat {
pushPduToPduQueue(pdu, this->incommingPduQueue, this ->incommingQueueMutex);
}
- const Tins::PDU * NatMap::popPduIncommingPduQueue() {
+ Tins::PDU * NatMap::popPduIncommingPduQueue() {
return popPduPduQueue(this->incommingPduQueue, this->incommingQueueMutex);
}
@@ -343,14 +343,19 @@ namespace otonat {
pushPduToPduQueue(pdu, this->outgoingPduQueue, this->outgoingQueueMutex);
}
- const Tins::PDU * NatMap::popPduOutgoingPduQueue() {
+ Tins::PDU * NatMap::popPduOutgoingPduQueue() {
return popPduPduQueue(this->outgoingPduQueue, this->outgoingQueueMutex);
}
- const Tins::PDU * NatMap::popPduPduQueue(PduQueue & queue, std::mutex & mtx) {
+ Tins::PDU * NatMap::popPduPduQueue(PduQueue & queue, std::mutex & mtx) {
mtx.lock();
+ if(queue.empty()){
+ mtx.unlock();
+ return nullptr;
+ }
+
const Tins::PDU * result = queue.front();
- const Tins::PDU * outPut = result->clone();
+ Tins::PDU * outPut = result->clone();
queue.pop();
mtx.unlock();
delete result;
diff --git a/src/map/natmap.h b/src/map/natmap.h
index e011ff9..bd9388f 100644
--- a/src/map/natmap.h
+++ b/src/map/natmap.h
@@ -9,7 +9,8 @@
#include
namespace otonat {
-
+ static const Tins::IPv4Address zeroIp;
+
class NatMap {
public:
typedef std::vector NatRangeList;
@@ -31,11 +32,11 @@ namespace otonat {
PduQueue outgoingPduQueue;
void handlePdu(const Tins::PDU * pdu);
void pushPduToIncommingPduQueue(const Tins::PDU * pdu);
- const Tins::PDU * popPduIncommingPduQueue();
+ Tins::PDU * popPduIncommingPduQueue();
void pushPduToOutgoingPduQueue(const Tins::PDU * pdu);
- const Tins::PDU * popPduOutgoingPduQueue();
+ Tins::PDU * popPduOutgoingPduQueue();
- static const Tins::PDU * popPduPduQueue(PduQueue & queue, std::mutex & mtx);
+ static Tins::PDU * popPduPduQueue(PduQueue & queue, std::mutex & mtx);
static void pushPduToPduQueue(const Tins::PDU * pdu, PduQueue & queue, std::mutex & mtx);
protected:
@@ -51,7 +52,6 @@ namespace otonat {
Tins::IPv4Address InsertOrUdpateTranslateIpAddress(const Tins::IPv4Address & originIp, const Tins::IPv4Address & otherTransSameRangeIp, NatRangeList & rangeList);
void TranslateIpPacket(Tins::IP * ip, const Tins::IPv4Address & transDstIp);
const Tins::IPv4Address TranslateArpIp(const Tins::IPv4Address & arpIp);
- const Tins::IPv4Address zeroIp;
bool isForMeOrFromMeIp(const Tins::IP * ip) const;
bool isForMeOrFromMeArp(const Tins::ARP * arp) const;
static bool isForMeOrFromMeIp(const Tins::IP * ip, const NatRangeList & rangeList);