From 5f8123d7e45d925739a84eb208da2eb8edcd4179 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Wed, 27 Feb 2008 22:50:01 +0000 Subject: added get Src/Dst Address @ plainPacket --- anytun.cpp | 3 --- plainPacket.cpp | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++- plainPacket.h | 8 ++++++++ 3 files changed, 67 insertions(+), 4 deletions(-) diff --git a/anytun.cpp b/anytun.cpp index c1181f1..acf86c9 100644 --- a/anytun.cpp +++ b/anytun.cpp @@ -66,9 +66,6 @@ #include "threadParam.h" -#define PAYLOAD_TYPE_TAP 0x6558 -#define PAYLOAD_TYPE_TUN 0x0800 - #define MAX_PACKET_LENGTH 1600 #define SESSION_KEYLEN_AUTH 20 // TODO: hardcoded size diff --git a/plainPacket.cpp b/plainPacket.cpp index 37c4338..1789854 100644 --- a/plainPacket.cpp +++ b/plainPacket.cpp @@ -31,11 +31,13 @@ #include #include #include +#include +#include +#include #include "datatypes.h" #include "plainPacket.h" - PlainPacket::PlainPacket(u_int32_t payload_length, bool allow_realloc) : Buffer(payload_length + sizeof(payload_type_t), allow_realloc) { payload_type_ = reinterpret_cast(buf_); @@ -80,3 +82,59 @@ u_int8_t* PlainPacket::getPayload() { return payload_; } + + +NetworkAddress PlainPacket::getSrcAddr() const +{ + if(!payload_type_ || !payload_) + return NetworkAddress(); + + if(PAYLOAD_TYPE_TAP) // Ehternet + { + // TODO + return NetworkAddress(); + } + else if(PAYLOAD_TYPE_TUN) // IPv4 + { + if(length_ < (sizeof(payload_type_t)+sizeof(struct ip))) + return NetworkAddress(); + struct ip* hdr = reinterpret_cast(payload_); + return NetworkAddress(hdr->ip_src); + } + else if(PAYLOAD_TYPE_TUN6) // IPv6 + { + if(length_ < (sizeof(payload_type_t)+sizeof(struct ip6_hdr))) + return NetworkAddress(); + struct ip6_hdr* hdr = reinterpret_cast(payload_); + return NetworkAddress(hdr->ip6_dst); + } + return NetworkAddress(); +} + + +NetworkAddress PlainPacket::getDstAddr() const +{ + if(!payload_type_ || !payload_) + return NetworkAddress(); + + if(PAYLOAD_TYPE_TAP) // Ehternet + { + // TODO + return NetworkAddress(); + } + else if(PAYLOAD_TYPE_TUN) // IPv4 + { + if(length_ < (sizeof(payload_type_t)+sizeof(struct ip))) + return NetworkAddress(); + struct ip* hdr = reinterpret_cast(payload_); + return NetworkAddress(hdr->ip_dst); + } + else if(PAYLOAD_TYPE_TUN6) // IPv6 + { + if(length_ < (sizeof(payload_type_t)+sizeof(struct ip6_hdr))) + return NetworkAddress(); + struct ip6_hdr* hdr = reinterpret_cast(payload_); + return NetworkAddress(hdr->ip6_dst); + } + return NetworkAddress(); +} diff --git a/plainPacket.h b/plainPacket.h index e44df32..03ae507 100644 --- a/plainPacket.h +++ b/plainPacket.h @@ -34,12 +34,18 @@ #include "datatypes.h" #include "buffer.h" +#include "networkAddress.h" + class Cipher; /** * plain SATP packet class
* includes payload_type and payload */ +#define PAYLOAD_TYPE_TAP 0x6558 +#define PAYLOAD_TYPE_TUN 0x0800 +#define PAYLOAD_TYPE_TUN6 0x86DD + class PlainPacket : public Buffer { public: @@ -85,6 +91,8 @@ public: */ u_int8_t* getPayload(); + NetworkAddress getSrcAddr() const; + NetworkAddress getDstAddr() const; private: PlainPacket(); -- cgit v1.2.3