summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Pointner <equinox@anytun.org>2007-06-18 18:50:41 +0000
committerChristian Pointner <equinox@anytun.org>2007-06-18 18:50:41 +0000
commit2a49fd31380e3b4fb041ee919eb429f074008924 (patch)
treef477e2c102d597967d5ea0be4960f6d4f532758d
parentbugfix @buffer resize (diff)
added header functions to package
-rw-r--r--anytun.cpp49
-rw-r--r--datatypes.h11
-rw-r--r--package.cpp96
-rw-r--r--package.h30
4 files changed, 164 insertions, 22 deletions
diff --git a/anytun.cpp b/anytun.cpp
index 72040c4..4b11231 100644
--- a/anytun.cpp
+++ b/anytun.cpp
@@ -35,6 +35,7 @@
#include "tunDevice.h"
#include "buffer.h"
+#include "package.h"
#include "cypher.h"
#include "authAlgo.h"
@@ -42,28 +43,50 @@ int main(int argc, char* argv[])
{
std::cout << "anytun - secure anycast tunneling protocol" << std::endl;
-// Buffer test(25);
-// for(unsigned int i=0; i<test.getLength(); ++i)
-// test[i] = i+1;
+ Buffer test(25);
+ for(unsigned int i=0; i<test.getLength(); ++i)
+ test[i] = i+1;
+ Package pack(test);
+
+ std::cout << std::hex;
+ std::cout << "pack[0-" << pack.getLength() << "]: '";
+ for(unsigned int i=0; i<pack.getLength(); ++i)
+ std::cout << (int)pack[i] << ",";
+ std::cout << std::endl;
+ std::cout << "pack: hdr=" << pack.hasHeader() << " seq_nr=" << pack.getSeqNr() << " sender_id=" << pack.getSenderId() << std::endl;
+
+ pack.setSeqNr(0x55AA55AA).setSenderId(0xBB11);
+
+ std::cout << "pack[0-" << pack.getLength() << "]: '";
+ for(unsigned int i=0; i<pack.getLength(); ++i)
+ std::cout << (int)pack[i] << ",";
+ std::cout << std::endl;
+ std::cout << "pack: hdr=" << pack.hasHeader() << " seq_nr=" << pack.getSeqNr() << " sender_id=" << pack.getSenderId() << std::endl;
-// std::cout << "test[0-" << test.getLength() << "]: '" << std::hex;
-// for(unsigned int i=0; i<test.getLength(); ++i)
-// std::cout << (int)test[i] << ",";
-// std::cout << std::dec << std::endl;
+ pack.addHeader(0x12345678, 0x9ABC);
+
+ std::cout << "pack[0-" << pack.getLength() << "]: '";
+ for(unsigned int i=0; i<pack.getLength(); ++i)
+ std::cout << (int)pack[i] << ",";
+ std::cout << std::endl;
+ std::cout << "pack: hdr=" << pack.hasHeader() << " seq_nr=" << pack.getSeqNr() << " sender_id=" << pack.getSenderId() << std::endl;
-// test.resizeFront(1);
+ pack.removeHeader();
+
+ std::cout << "pack[0-" << pack.getLength() << "]: '";
+ for(unsigned int i=0; i<pack.getLength(); ++i)
+ std::cout << (int)pack[i] << ",";
+ std::cout << std::endl;
+ std::cout << "pack: hdr=" << pack.hasHeader() << " seq_nr=" << pack.getSeqNr() << " sender_id=" << pack.getSenderId() << std::endl;
-// std::cout << "test[0-" << test.getLength() << "]: '" << std::hex;
-// for(unsigned int i=0; i<test.getLength(); ++i)
-// std::cout << (int)test[i] << ",";
-// std::cout << std::dec << std::endl;
+
+ std::cout << std::dec;
// TunDevice* dev;
// dev = new TunDevice("tun", "192.168.200.1", "192.168.201.1");
// std::cout << "dev created (opened)" << std::endl;
// std::cout << "dev opened - actual name is '" << dev->getActualName() << "'" << std::endl;
// std::cout << "dev type is '" << dev->getType() << "'" << std::endl;
-
// sleep(10);
diff --git a/datatypes.h b/datatypes.h
index af08549..4083e7a 100644
--- a/datatypes.h
+++ b/datatypes.h
@@ -44,8 +44,19 @@ typedef signed long long int64_t;
typedef unsigned long long u_int64_t;
typedef u_int32_t seq_nr_t;
+#define SEQ_NR_T_NTOH(a) ntohl(a)
+#define SEQ_NR_T_HTON(a) htonl(a)
+
typedef u_int16_t sender_id_t;
+#define SENDER_ID_T_NTOH(a) ntohs(a)
+#define SENDER_ID_T_HTON(a) htons(a)
+
typedef u_int16_t payload_type_t;
+#define PAYLOAD_TYPE_T_NTOH(a) ntohs(a)
+#define PAYLOAD_TYPE_T_HTON(a) htons(a)
+
typedef u_int32_t auth_tag_t;
+#define AUTH_TAG_T_NTOH(a) ntohl(a)
+#define AUTH_TAG_T_HTON(a) htonl(a)
#endif
diff --git a/package.cpp b/package.cpp
index b008a67..e41c7a0 100644
--- a/package.cpp
+++ b/package.cpp
@@ -28,7 +28,103 @@
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
+#include <arpa/inet.h>
+
#include "datatypes.h"
#include "package.h"
+Package::Package()
+{
+ header_ = 0;
+ payload_type_ = 0;
+ auth_tag_ = 0;
+}
+
+Package::Package(u_int32_t length) : Buffer(length)
+{
+ header_ = 0;
+ payload_type_ = 0;
+ auth_tag_ = 0;
+}
+
+Package::Package(const Buffer &src) : Buffer(src)
+{
+ header_ = 0;
+ payload_type_ = 0;
+ auth_tag_ = 0;
+}
+
+bool Package::hasHeader() const
+{
+ return header_;
+}
+
+Package& Package::withHeader(bool b)
+{
+ if(b && length_ >= sizeof(struct HeaderStruct))
+ header_ = reinterpret_cast<struct HeaderStruct*>(buf_);
+ else
+ header_ = 0;
+
+ return *this;
+}
+
+seq_nr_t Package::getSeqNr() const
+{
+ if(!header_)
+ return 0;
+
+ return SEQ_NR_T_NTOH(header_->seq_nr);
+}
+
+sender_id_t Package::getSenderId() const
+{
+ if(!header_)
+ return 0;
+
+ return SENDER_ID_T_NTOH(header_->sender_id);
+}
+
+Package& Package::addHeader(seq_nr_t seq_nr, sender_id_t sender_id)
+{
+ if(!header_)
+ {
+ if(sizeof(struct HeaderStruct) > resizeFront(length_ + sizeof(struct HeaderStruct)))
+ return *this;
+
+ header_ = reinterpret_cast<struct HeaderStruct*>(buf_);
+ }
+ header_->seq_nr = SEQ_NR_T_HTON(seq_nr);
+ header_->sender_id = SENDER_ID_T_HTON(sender_id);
+ return *this;
+}
+
+Package& Package::removeHeader()
+{
+ if(!header_)
+ return *this;
+
+ if(length_ >= sizeof(struct HeaderStruct))
+ resizeFront(length_ - sizeof(struct HeaderStruct));
+
+ header_ = 0;
+
+ return *this;
+}
+
+Package& Package::setSeqNr(seq_nr_t seq_nr)
+{
+ if(header_)
+ header_->seq_nr = SEQ_NR_T_HTON(seq_nr);
+
+ return *this;
+}
+
+Package& Package::setSenderId(sender_id_t sender_id)
+{
+ if(header_)
+ header_->sender_id = SENDER_ID_T_HTON(sender_id);
+
+ return *this;
+}
diff --git a/package.h b/package.h
index 53f71f5..2a28f2c 100644
--- a/package.h
+++ b/package.h
@@ -37,29 +37,41 @@
class Package : public Buffer
{
public:
- Package() {}
- Package(const Buffer &src) {}
+ Package();
+ Package(u_int32_t length);
+ Package(const Buffer &src);
bool hasHeader() const;
Package& withHeader(bool b);
seq_nr_t getSeqNr() const;
sender_id_t getSenderId() const;
- Package& setHeader(seq_nr_t seq_nr, sender_id_t sender_id);
+ Package& addHeader(seq_nr_t seq_nr, sender_id_t sender_id);
+ Package& removeHeader();
+ Package& setSeqNr(seq_nr_t seq_nr);
+ Package& setSenderId(sender_id_t sender_id);
+
bool hasPayloadType() const;
Package& withPayloadType(bool b);
payload_type_t getPayloadType() const;
- Package& setPayloadType(payload_type_t payload_type);
-
+ Package& addPayloadType(payload_type_t payload_type);
+ Package& removePayloadType();
+
bool hasAuthTag() const;
Package& withAuthTag(bool b);
auth_tag_t getAuthTag() const;
- Package& setAuthTag(auth_tag_t auth_tag);
+ Package& addAuthTag(auth_tag_t auth_tag);
+ Package& removeAuthTag();
private:
- bool has_header_;
- bool has_payload_type_;
- bool has_auth_tag_;
+ struct HeaderStruct
+ {
+ seq_nr_t seq_nr;
+ sender_id_t sender_id;
+ }__attribute__((__packed__));
+ struct HeaderStruct* header_;
+ payload_type_t* payload_type_;
+ auth_tag_t* auth_tag_;
};
#endif