From de5c4f910203dd976ad8c17eff90874f548d6bc3 Mon Sep 17 00:00:00 2001 From: Othmar Gsenger Date: Thu, 31 Jan 2008 11:39:39 +0000 Subject: packet and cypher fixes --- Makefile | 4 - anytun.cpp | 10 +- cypher.cpp | 25 +++-- cypher.h | 11 +- encryptedPacket.cpp | 172 +++++-------------------------- encryptedPacket.h | 12 ++- packet.cpp | 283 ---------------------------------------------------- packet.h | 88 ---------------- plainPacket.cpp | 29 ++++-- plainPacket.h | 17 +++- 10 files changed, 96 insertions(+), 555 deletions(-) delete mode 100644 packet.cpp delete mode 100644 packet.h diff --git a/Makefile b/Makefile index 3ba33b4..38de924 100644 --- a/Makefile +++ b/Makefile @@ -50,7 +50,6 @@ OBJS = anytun.o \ buffer.o \ syncBuffer.o \ syncCommand.o \ - packet.o \ plainPacket.o \ encryptedPacket.o \ cypher.o \ @@ -95,9 +94,6 @@ buffer.o: buffer.cpp buffer.h syncBuffer.o: syncBuffer.cpp syncBuffer.h $(C++) $(CCFLAGS) $< -c -packet.o: packet.cpp packet.h buffer.h - $(C++) $(CCFLAGS) $< -c - plainPacket.o: plainPacket.cpp plainPacket.h buffer.h $(C++) $(CCFLAGS) $< -c diff --git a/anytun.cpp b/anytun.cpp index 9cf7280..ad2a433 100644 --- a/anytun.cpp +++ b/anytun.cpp @@ -141,7 +141,7 @@ void* sender(void* p) u_int16_t mux = 0; while(1) { - plain_packet.setLength( 1600); + plain_packet.setLength( plain_packet.getMaxLength()); // read packet from device u_int32_t len = param->dev.read(plain_packet); @@ -171,7 +171,7 @@ void* sender(void* p) c->setKey(session_key); c->setSalt(session_salt); - c->cypher(packet, plain_packet, plain_packet.getLength(), conn.seq_nr_, param->opt.getSenderId()); + c->encrypt(plain_packet, packet, conn.seq_nr_, param->opt.getSenderId()); packet.setHeader(conn.seq_nr_, param->opt.getSenderId(), mux); conn.seq_nr_++; @@ -234,8 +234,8 @@ void* receiver(void* p) { string remote_host; u_int16_t remote_port; - packet.setLength( packet.getSize() ); - plain_packet.setLength( plain_packet.getSize() ); + packet.setLength( packet.getMaxLength() ); + plain_packet.setLength( plain_packet.getMaxLength() ); // u_int16_t sid = 0, seq = 0; // read packet from socket @@ -279,7 +279,7 @@ void* receiver(void* p) conn.kd_.generate(LABEL_SATP_SALT, packet.getSeqNr(), session_salt, session_salt.getLength()); c->setKey(session_key); c->setSalt(session_salt); - c->cypher(plain_packet, packet, packet.getLength(), packet.getSeqNr(), packet.getSenderId()); + c->decrypt(packet, plain_packet); // check payload_type and remove it if((param->dev.getType() == TunDevice::TYPE_TUN && plain_packet.getPayloadType() != PAYLOAD_TYPE_TUN) || diff --git a/cypher.cpp b/cypher.cpp index d5ca35b..aa305ca 100644 --- a/cypher.cpp +++ b/cypher.cpp @@ -38,16 +38,23 @@ #include "mpi.h" #include "log.h" +void Cypher::encrypt(const PlainPacket & in,EncryptedPacket & out, seq_nr_t seq_nr, sender_id_t sender_id) +{ + cypher(out.payload_, in.complete_payload_ , in.complete_payload_length_, seq_nr, sender_id); + out.setSenderId(sender_id); + out.setSeqNr(seq_nr); + out.setPayloadLength(in.complete_payload_length_); +} +void Cypher::decrypt(const EncryptedPacket & in,PlainPacket & out) +{ + cypher(out.complete_payload_, in.payload_ , in.payload_length_, in.getSeqNr(), in.getSenderId()); + out.setCompletePayloadLength(in.payload_length_); +} -void NullCypher::cypher(Buffer& out, Buffer& in, u_int32_t length, seq_nr_t seq_nr, sender_id_t sender_id) +void NullCypher::cypher(u_int8_t * out, u_int8_t * in, u_int32_t length, seq_nr_t seq_nr, sender_id_t sender_id) { - try - { - for(u_int32_t i=0; i @@ -45,7 +47,10 @@ public: void setKey(Buffer key) {}; void setSalt(Buffer salt) {}; - virtual void cypher(Buffer& in, Buffer& out, u_int32_t length, seq_nr_t seq_nr, sender_id_t sender_id) {}; + void encrypt(const PlainPacket & in,EncryptedPacket & out, seq_nr_t seq_nr, sender_id_t sender_id); + void decrypt(const EncryptedPacket & in,PlainPacket & out); +private: + virtual void cypher(u_int8_t * in, u_int8_t * out, u_int32_t length, seq_nr_t seq_nr, sender_id_t sender_id) {}; }; class NullCypher : public Cypher @@ -54,7 +59,7 @@ public: NullCypher() {}; ~NullCypher() {}; protected: - void cypher(Buffer& in, Buffer& out, u_int32_t length, seq_nr_t seq_nr, sender_id_t sender_id); + void cypher(u_int8_t * in, u_int8_t * out, u_int32_t length, seq_nr_t seq_nr, sender_id_t sender_id); }; class AesIcmCypher : public Cypher @@ -64,12 +69,12 @@ public: ~AesIcmCypher(); void setKey(Buffer key); void setSalt(Buffer salt); - void cypher(Buffer& in, Buffer& out, u_int32_t length, seq_nr_t seq_nr, sender_id_t sender_id); static const char* MIN_GCRYPT_VERSION; static const u_int32_t GCRYPT_SEC_MEM = 16384; // 16k secure memory protected: + void cypher(u_int8_t * in, u_int8_t * out, u_int32_t length, seq_nr_t seq_nr, sender_id_t sender_id); gcry_cipher_hd_t cipher_; Buffer salt_; }; diff --git a/encryptedPacket.cpp b/encryptedPacket.cpp index 35ccda0..9c091b5 100644 --- a/encryptedPacket.cpp +++ b/encryptedPacket.cpp @@ -44,9 +44,9 @@ EncryptedPacket::EncryptedPacket(u_int32_t max_payload_length) { header_ = reinterpret_cast(buf_); auth_tag_ = NULL; - buf_ = buf_ + sizeof(struct HeaderStruct); // no authtag yet - length_ = length_ - sizeof(struct HeaderStruct); - size_ = max_payload_length + AUTHTAG_SIZE; + payload_ = buf_ + sizeof(struct HeaderStruct); // no authtag yet + length_ = sizeof(struct HeaderStruct); + max_length_ = max_payload_length + AUTHTAG_SIZE; } @@ -54,9 +54,17 @@ EncryptedPacket::~EncryptedPacket() { buf_ = reinterpret_cast(header_); if( auth_tag_ == NULL ) - length_ = size_ + sizeof(struct HeaderStruct) + AUTHTAG_SIZE; + length_ = max_length_ + sizeof(struct HeaderStruct) + AUTHTAG_SIZE; else - length_ = size_ + sizeof(struct HeaderStruct); + length_ = max_length_ + sizeof(struct HeaderStruct); +} + +void EncryptedPacket::setPayloadLength(u_int8_t payload_length) +{ + if( auth_tag_) + length_= payload_length + sizeof(struct HeaderStruct)+AUTHTAG_SIZE; + else + length_= payload_length + sizeof(struct HeaderStruct); } seq_nr_t EncryptedPacket::getSeqNr() const @@ -74,14 +82,14 @@ mux_t EncryptedPacket::getMux() const return MUX_T_NTOH(header_->mux); } -u_int32_t EncryptedPacket::getSize() const +u_int32_t EncryptedPacket::getMaxLength() const { - return size_; + return max_length_; } void EncryptedPacket::setLength(u_int32_t length) { - if(length > size_) + if(length > max_length_) throw std::out_of_range("can't set length greater then size ofsize of allocated memory"); length_ = length; @@ -120,16 +128,20 @@ void EncryptedPacket::withAuthTag(bool b) { if( b && (auth_tag_ != NULL) ) throw std::runtime_error("packet already has auth tag function enabled"); + //TODO: return instead? + if( ! b && (auth_tag_ == NULL) ) + throw std::runtime_error("packet already has auth tag function disabled"); + //TODO: return instead? if( b ) { auth_tag_ = reinterpret_cast( buf_ + sizeof(struct HeaderStruct) ); - buf_ = buf_ + AUTHTAG_SIZE; + payload_ = payload_ + AUTHTAG_SIZE; length_ -= AUTHTAG_SIZE; - size_ -= AUTHTAG_SIZE; + max_length_ -= AUTHTAG_SIZE; } else { - buf_ = reinterpret_cast( auth_tag_ ); + payload_ = reinterpret_cast( auth_tag_ ); length_ += AUTHTAG_SIZE; - size_ += AUTHTAG_SIZE; + max_length_ += AUTHTAG_SIZE; auth_tag_ = NULL; } } @@ -158,139 +170,3 @@ AuthTag EncryptedPacket::getAuthTag() const return at; } - -//Packet& Packet::addHeader(seq_nr_t seq_nr, sender_id_t sender_id) -//{ -// if(!has_header_) -// { -// if(sizeof(struct HeaderStruct) > resizeFront(length_ + sizeof(struct HeaderStruct))) -// return *this; -// -// has_header_ = true; -// } -// struct HeaderStruct* header; -// header = reinterpret_cast(buf_); -// header->seq_nr = SEQ_NR_T_HTON(seq_nr); -// header->sender_id = SENDER_ID_T_HTON(sender_id); -// return *this; -//} -// -// -//bool Packet::hasPayloadType() const -//{ -// return has_payload_type_; -//} -// -//Packet& Packet::withPayloadType(bool b) -//{ -// if(b && length_ >= sizeof(payload_type_t)) -// has_payload_type_ = true; -// else -// has_payload_type_ = false; -// -// return *this; -//} -// -//payload_type_t Packet::getPayloadType() const -//{ -// if(!has_payload_type_) -// return 0; -// -// if((!has_auth_tag_ && length_ < sizeof(payload_type_t)) || -// (has_auth_tag_ && length_ < (sizeof(payload_type_t) + AUTHTAG_SIZE))) -// return 0; -// -// payload_type_t* payload_type; -// -// if(!has_auth_tag_) -// payload_type = reinterpret_cast(buf_ + length_ - sizeof(payload_type_t)); -// else -// payload_type = reinterpret_cast(buf_ + length_ - sizeof(payload_type_t) - AUTHTAG_SIZE); -// return PAYLOAD_TYPE_T_NTOH(*payload_type); -//} -// -//Packet& Packet::addPayloadType(payload_type_t payload_type) -//{ -// if(has_auth_tag_) -// throw std::runtime_error("can't add payload_type with existing auth_tag"); -// -// if(!has_payload_type_) -// { -// u_int32_t new_length = length_ + sizeof(payload_type_t); -// if(new_length > resizeBack(new_length)) -// return *this; -// -// has_payload_type_ = true; -// } -// payload_type_t* payload_type_ptr; -// payload_type_ptr = reinterpret_cast(buf_ + length_ - sizeof(payload_type_t)); -// *payload_type_ptr = PAYLOAD_TYPE_T_HTON(payload_type); -// return *this; -//} -// -//Packet& Packet::removePayloadType() -//{ -// if(has_auth_tag_) -// throw std::runtime_error("can't remove payload_type with existing auth_tag"); -// -// if(!has_payload_type_) -// return *this; -// -// if(length_ >= sizeof(payload_type_t)) -// resizeBack(length_ - sizeof(payload_type_t)); -// -// has_payload_type_ = false; -// -// return *this; -//} -// -// -// -// -//AuthTag Packet::getAuthTag() const -//{ -// if(!has_auth_tag_) -// return AuthTag(0); -// -// if(length_ < AUTHTAG_SIZE) -// return AuthTag(0); -// -// //AuthTag* auth_tag; -// //auth_tag = reinterpret_cast(buf_ + length_ - AUTHTAG_SIZE); -// //return AUTH_TAG_T_NTOH(*auth_tag); -// AuthTag auth_tag; -// auth_tag = AuthTag(buf_ + length_ - AUTHTAG_SIZE, AUTHTAG_SIZE); -// return auth_tag; -//} -// -//Packet& Packet::addAuthTag(AuthTag auth_tag) -//{ -// if(!has_auth_tag_) -// { -// u_int32_t new_length = length_ + auth_tag.getLength(); -// if(new_length > resizeBack(new_length)) -// return *this; -// -// has_auth_tag_ = true; -// } -// -// AuthTag* auth_tag_ptr; -// auth_tag_ptr = reinterpret_cast(buf_ + length_ - auth_tag.getLength()); -// std::memcpy(auth_tag_ptr, auth_tag.getBuf(), auth_tag.getLength()); -// -// return *this; -//} -// -//Packet& Packet::removeAuthTag() -//{ -// if(!has_auth_tag_) -// return *this; -// -// if(length_ >= AUTHTAG_SIZE) -// resizeBack(length_ - AUTHTAG_SIZE); -// -// has_auth_tag_ = false; -// -// return *this; -//} -// diff --git a/encryptedPacket.h b/encryptedPacket.h index 7b70e17..83b831e 100644 --- a/encryptedPacket.h +++ b/encryptedPacket.h @@ -34,7 +34,7 @@ #include "datatypes.h" #include "buffer.h" #include "authTag.h" - +class Cypher; class EncryptedPacket : public Buffer { public: @@ -98,7 +98,7 @@ public: * Get the maximum payload size * @return maximum payload size */ - u_int32_t getSize() const; + u_int32_t getMaxLength() const; /** * Set the real length of the payload @@ -111,6 +111,8 @@ public: AuthTag getAuthTag() const; void setAuthTag(AuthTag& tag); + void setPayloadLength(u_int8_t payload_length); + // bool hasHeader() const; // Packet& withHeader(bool b); @@ -131,9 +133,13 @@ private: struct HeaderStruct* header_; AuthTag* auth_tag_; - u_int32_t size_; + u_int32_t max_length_; static const u_int32_t AUTHTAG_SIZE = 10; // 10byte +protected: + friend class Cypher; + u_int8_t * payload_; + u_int32_t payload_length_; }; #endif diff --git a/packet.cpp b/packet.cpp deleted file mode 100644 index 3a80682..0000000 --- a/packet.cpp +++ /dev/null @@ -1,283 +0,0 @@ -/* - * anytun - * - * The secure anycast tunneling protocol (satp) defines a protocol used - * for communication between any combination of unicast and anycast - * tunnel endpoints. It has less protocol overhead than IPSec in Tunnel - * mode and allows tunneling of every ETHER TYPE protocol (e.g. - * ethernet, ip, arp ...). satp directly includes cryptography and - * message authentication based on the methodes used by SRTP. It is - * intended to deliver a generic, scaleable and secure solution for - * tunneling and relaying of packets of any protocol. - * - * - * Copyright (C) 2007 anytun.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License version 2 - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program (see the file COPYING included with this - * distribution); if not, write to the Free Software Foundation, Inc., - * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -#include -#include -#include -#include // for std::memcpy - -#include "datatypes.h" -#include "authTag.h" - -#include "packet.h" - - -Packet::Packet() -{ - has_header_ = false; - has_payload_type_ = false; - has_auth_tag_ = false; -} - -Packet::Packet(u_int32_t payload_length) - : Buffer(payload_length + sizeof(struct HeaderStruct) + sizeof(payload_type_t) + AUTHTAG_SIZE) -{ - has_header_ = false; - has_payload_type_ = false; - has_auth_tag_ = false; -} - -Packet::Packet(const Buffer &src) : Buffer(src) -{ - has_header_ = false; - has_payload_type_ = false; - has_auth_tag_ = false; -} - -bool Packet::hasHeader() const -{ - return has_header_; -} - -Packet& Packet::withHeader(bool b) -{ - if(b && length_ >= sizeof(struct HeaderStruct)) - has_header_ = true; - else - has_header_ = false; - - return *this; -} - -seq_nr_t Packet::getSeqNr() const -{ - if(!has_header_) - return 0; - - struct HeaderStruct* header; - header = reinterpret_cast(buf_); - return SEQ_NR_T_NTOH(header->seq_nr); -} - -sender_id_t Packet::getSenderId() const -{ - if(!has_header_) - return 0; - - struct HeaderStruct* header; - header = reinterpret_cast(buf_); - return SENDER_ID_T_NTOH(header->sender_id); -} - -Packet& Packet::addHeader(seq_nr_t seq_nr, sender_id_t sender_id) -{ - if(!has_header_) - { - if(sizeof(struct HeaderStruct) > resizeFront(length_ + sizeof(struct HeaderStruct))) - return *this; - - has_header_ = true; - } - struct HeaderStruct* header; - header = reinterpret_cast(buf_); - header->seq_nr = SEQ_NR_T_HTON(seq_nr); - header->sender_id = SENDER_ID_T_HTON(sender_id); - return *this; -} - -Packet& Packet::removeHeader() -{ - if(!has_header_) - return *this; - - if(length_ >= sizeof(struct HeaderStruct)) - resizeFront(length_ - sizeof(struct HeaderStruct)); - - has_header_ = false; - - return *this; -} - -Packet& Packet::setSeqNr(seq_nr_t seq_nr) -{ - if(has_header_) - { - struct HeaderStruct* header; - header = reinterpret_cast(buf_); - header->seq_nr = SEQ_NR_T_HTON(seq_nr); - } - return *this; -} - -Packet& Packet::setSenderId(sender_id_t sender_id) -{ - if(has_header_) - { - struct HeaderStruct* header; - header = reinterpret_cast(buf_); - header->sender_id = SENDER_ID_T_HTON(sender_id); - } - return *this; -} - - - -bool Packet::hasPayloadType() const -{ - return has_payload_type_; -} - -Packet& Packet::withPayloadType(bool b) -{ - if(b && length_ >= sizeof(payload_type_t)) - has_payload_type_ = true; - else - has_payload_type_ = false; - - return *this; -} - -payload_type_t Packet::getPayloadType() const -{ - if(!has_payload_type_) - return 0; - - if((!has_auth_tag_ && length_ < sizeof(payload_type_t)) || - (has_auth_tag_ && length_ < (sizeof(payload_type_t) + AUTHTAG_SIZE))) - return 0; - - payload_type_t* payload_type; - - if(!has_auth_tag_) - payload_type = reinterpret_cast(buf_ + length_ - sizeof(payload_type_t)); - else - payload_type = reinterpret_cast(buf_ + length_ - sizeof(payload_type_t) - AUTHTAG_SIZE); - return PAYLOAD_TYPE_T_NTOH(*payload_type); -} - -Packet& Packet::addPayloadType(payload_type_t payload_type) -{ - if(has_auth_tag_) - throw std::runtime_error("can't add payload_type with existing auth_tag"); - - if(!has_payload_type_) - { - u_int32_t new_length = length_ + sizeof(payload_type_t); - if(new_length > resizeBack(new_length)) - return *this; - - has_payload_type_ = true; - } - payload_type_t* payload_type_ptr; - payload_type_ptr = reinterpret_cast(buf_ + length_ - sizeof(payload_type_t)); - *payload_type_ptr = PAYLOAD_TYPE_T_HTON(payload_type); - return *this; -} - -Packet& Packet::removePayloadType() -{ - if(has_auth_tag_) - throw std::runtime_error("can't remove payload_type with existing auth_tag"); - - if(!has_payload_type_) - return *this; - - if(length_ >= sizeof(payload_type_t)) - resizeBack(length_ - sizeof(payload_type_t)); - - has_payload_type_ = false; - - return *this; -} - - - -bool Packet::hasAuthTag() const -{ - return has_auth_tag_; -} - -Packet& Packet::withAuthTag(bool b) -{ - if(b && length_ >= AUTHTAG_SIZE) - has_auth_tag_ = true; - else - has_auth_tag_ = false; - - return *this; -} - -AuthTag Packet::getAuthTag() const -{ - if(!has_auth_tag_) - return AuthTag(0); - - if(length_ < AUTHTAG_SIZE) - return AuthTag(0); - - //AuthTag* auth_tag; - //auth_tag = reinterpret_cast(buf_ + length_ - AUTHTAG_SIZE); - //return AUTH_TAG_T_NTOH(*auth_tag); - AuthTag auth_tag; - auth_tag = AuthTag(buf_ + length_ - AUTHTAG_SIZE, AUTHTAG_SIZE); - return auth_tag; -} - -Packet& Packet::addAuthTag(AuthTag auth_tag) -{ - if(!has_auth_tag_) - { - u_int32_t new_length = length_ + auth_tag.getLength(); - if(new_length > resizeBack(new_length)) - return *this; - - has_auth_tag_ = true; - } - - AuthTag* auth_tag_ptr; - auth_tag_ptr = reinterpret_cast(buf_ + length_ - auth_tag.getLength()); - std::memcpy(auth_tag_ptr, auth_tag.getBuf(), auth_tag.getLength()); - - return *this; -} - -Packet& Packet::removeAuthTag() -{ - if(!has_auth_tag_) - return *this; - - if(length_ >= AUTHTAG_SIZE) - resizeBack(length_ - AUTHTAG_SIZE); - - has_auth_tag_ = false; - - return *this; -} - diff --git a/packet.h b/packet.h deleted file mode 100644 index 72c7b6b..0000000 --- a/packet.h +++ /dev/null @@ -1,88 +0,0 @@ -/* - * anytun - * - * The secure anycast tunneling protocol (satp) defines a protocol used - * for communication between any combination of unicast and anycast - * tunnel endpoints. It has less protocol overhead than IPSec in Tunnel - * mode and allows tunneling of every ETHER TYPE protocol (e.g. - * ethernet, ip, arp ...). satp directly includes cryptography and - * message authentication based on the methodes used by SRTP. It is - * intended to deliver a generic, scaleable and secure solution for - * tunneling and relaying of packets of any protocol. - * - * - * Copyright (C) 2007 anytun.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License version 2 - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program (see the file COPYING included with this - * distribution); if not, write to the Free Software Foundation, Inc., - * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -#ifndef _PACKET_H_ -#define _PACKET_H_ - -#include "datatypes.h" -#include "buffer.h" -#include "authTag.h" - -class Packet : public Buffer -{ -public: - Packet(); - - /** - * Packet Constructor - * @param payload_length Payload Length - */ - Packet(u_int32_t payload_length); - Packet(const Buffer &src); - - bool hasHeader() const; - Packet& withHeader(bool b); - seq_nr_t getSeqNr() const; - sender_id_t getSenderId() const; - Packet& addHeader(seq_nr_t seq_nr, sender_id_t sender_id); - Packet& removeHeader(); - Packet& setSeqNr(seq_nr_t seq_nr); - Packet& setSenderId(sender_id_t sender_id); - - bool hasPayloadType() const; - Packet& withPayloadType(bool b); - payload_type_t getPayloadType() const; - Packet& addPayloadType(payload_type_t payload_type); - Packet& removePayloadType(); - - bool hasAuthTag() const; - Packet& withAuthTag(bool b); - AuthTag getAuthTag() const; - Packet& addAuthTag(AuthTag auth_tag); - Packet& removeAuthTag(); - -private: - struct HeaderStruct - { - seq_nr_t seq_nr; - sender_id_t sender_id; - }__attribute__((__packed__)); - bool has_header_; - bool has_payload_type_; - bool has_auth_tag_; - - struct HeaderStruct* header_; - payload_type_t* payload_type_; - AuthTag* auth_tag_; - - static const u_int32_t AUTHTAG_SIZE = 10; -}; - -#endif diff --git a/plainPacket.cpp b/plainPacket.cpp index 1df611d..d915a30 100644 --- a/plainPacket.cpp +++ b/plainPacket.cpp @@ -40,16 +40,30 @@ PlainPacket::~PlainPacket() { - buf_ = reinterpret_cast(payload_type_); - length_ = size_; + buf_=complete_payload_; + length_=max_length_; } PlainPacket::PlainPacket(u_int32_t max_payload_length) : Buffer(max_payload_length + sizeof(payload_type_t)) { + splitPayload(); +} + +void PlainPacket::splitPayload() +{ + complete_payload_length_ = length_; + complete_payload_ = buf_; + payload_type_ = reinterpret_cast(buf_); buf_ += sizeof(payload_type_t); - length_ = max_payload_length; - size_ = length_; + length_ -= sizeof(payload_type_t); + max_length_ = length_; +} + +void PlainPacket::setCompletePayloadLength(u_int32_t payload_length) +{ + complete_payload_length_ = payload_length; + length_=complete_payload_length_-sizeof(payload_type_t); } payload_type_t PlainPacket::getPayloadType() const @@ -64,13 +78,14 @@ void PlainPacket::setPayloadType(payload_type_t payload_type) void PlainPacket::setLength(u_int32_t length) { - if(length > size_) + if(length > max_length_) throw std::out_of_range("can't set length greater then size ofsize of allocated memory"); length_ = length; + complete_payload_length_ = length_ + sizeof(payload_type_t); } -u_int32_t PlainPacket::getSize() const +u_int32_t PlainPacket::getMaxLength() const { - return size_; + return max_length_; } diff --git a/plainPacket.h b/plainPacket.h index 8234f41..22664f4 100644 --- a/plainPacket.h +++ b/plainPacket.h @@ -33,8 +33,8 @@ #include "datatypes.h" #include "buffer.h" -#include "authTag.h" +class Cypher; /** * plain SATP packet class
* includes payload_type and payload @@ -63,17 +63,19 @@ public: */ void setPayloadType(payload_type_t payload_type); + void setCompletePayloadLength(u_int32_t payload_length); + /** * Set the real payload length * @param length the real payload length */ - void setRealPayloadLengt(u_int32_t length); + //void setRealPayloadLengt(u_int32_t length); /** * Get the real payload length * @return the real length of the payload */ - u_int32_t getRealPayloadLength(); + //u_int32_t getRealPayloadLength(); /** * Set the length of the payload @@ -85,13 +87,18 @@ public: * Get the size of the allocated memory for the payload * @return maximum size of payload */ - u_int32_t getSize() const; + u_int32_t getMaxLength() const; private: PlainPacket(); PlainPacket(const PlainPacket &src); + void splitPayload(); + u_int32_t max_length_; payload_type_t* payload_type_; - u_int32_t size_; +protected: + friend class Cypher; + u_int8_t * complete_payload_; + u_int32_t complete_payload_length_; }; #endif -- cgit v1.2.3