summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--anytun.cpp3
-rw-r--r--plainPacket.cpp60
-rw-r--r--plainPacket.h8
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 <stdexcept>
#include <iostream>
#include <arpa/inet.h>
+#include <net/ethernet.h>
+#include <netinet/ip.h>
+#include <netinet/ip6.h>
#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<payload_type_t*>(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<struct ip*>(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<struct ip6_hdr*>(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<struct ip*>(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<struct ip6_hdr*>(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<br>
* 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();