add pdu sender

This commit is contained in:
stubbfel
2015-09-16 21:48:48 +02:00
parent 52a1aa9367
commit edc11a5352
8 changed files with 179 additions and 13 deletions

59
src/PduSender.cpp Normal file
View File

@@ -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<Tins::ARP>();
if (arp != nullptr) {
dstIp = arp->target_ip_addr();
}
if (dstIp != zeroIp) {
const Tins::IP * ip = pdu->find_pdu<Tins::IP>();
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;
}
}
}
}
}

31
src/PduSender.h Normal file
View File

@@ -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 <tins/tins.h>
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 */

View File

@@ -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;

View File

@@ -9,7 +9,8 @@
#include <mutex>
namespace otonat {
static const Tins::IPv4Address zeroIp;
class NatMap {
public:
typedef std::vector<NatRange> 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);