removed debug statements
[anytun.git] / encryptedPacket.cpp
blobb618f99e7f08bb044d1bb355864f9a7dee94cd63
1 /*
2 * anytun
4 * The secure anycast tunneling protocol (satp) defines a protocol used
5 * for communication between any combination of unicast and anycast
6 * tunnel endpoints. It has less protocol overhead than IPSec in Tunnel
7 * mode and allows tunneling of every ETHER TYPE protocol (e.g.
8 * ethernet, ip, arp ...). satp directly includes cryptography and
9 * message authentication based on the methodes used by SRTP. It is
10 * intended to deliver a generic, scaleable and secure solution for
11 * tunneling and relaying of packets of any protocol.
14 * Copyright (C) 2007 anytun.org <satp@wirdorange.org>
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License version 2
18 * as published by the Free Software Foundation.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program (see the file COPYING included with this
27 * distribution); if not, write to the Free Software Foundation, Inc.,
28 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 #include <stdexcept>
32 #include <iostream>
33 #include <arpa/inet.h>
34 #include <cstdio> // for std::memcpy
36 #include "encryptedPacket.h"
37 #include "datatypes.h"
38 #include "authTag.h"
39 #include "log.h"
41 // TODO: fix auth_tag stuff
42 EncryptedPacket::EncryptedPacket(u_int32_t payload_length, bool allow_realloc)
43 : Buffer(payload_length + sizeof(struct HeaderStruct), allow_realloc)
45 header_ = reinterpret_cast<struct HeaderStruct*>(buf_);
46 payload_ = buf_ + sizeof(struct HeaderStruct); // TODO: fix auth_tag stuff
47 auth_tag_ = NULL; // TODO: fix auth_tag stuff
48 if(header_)
50 header_->seq_nr = 0;
51 header_->sender_id = 0;
52 header_->mux = 0;
56 seq_nr_t EncryptedPacket::getSeqNr() const
58 if(header_)
59 return SEQ_NR_T_NTOH(header_->seq_nr);
61 return 0;
64 sender_id_t EncryptedPacket::getSenderId() const
66 if(header_)
67 return SENDER_ID_T_NTOH(header_->sender_id);
69 return 0;
72 mux_t EncryptedPacket::getMux() const
74 if(header_)
75 return MUX_T_NTOH(header_->mux);
77 return 0;
80 void EncryptedPacket::setSeqNr(seq_nr_t seq_nr)
82 if(header_)
83 header_->seq_nr = SEQ_NR_T_HTON(seq_nr);
86 void EncryptedPacket::setSenderId(sender_id_t sender_id)
88 if(header_)
89 header_->sender_id = SENDER_ID_T_HTON(sender_id);
92 void EncryptedPacket::setMux(mux_t mux)
94 if(header_)
95 header_->mux = MUX_T_HTON(mux);
98 void EncryptedPacket::setHeader(seq_nr_t seq_nr, sender_id_t sender_id, mux_t mux)
100 if(!header_)
101 return;
103 header_->seq_nr = SEQ_NR_T_HTON(seq_nr);
104 header_->sender_id = SENDER_ID_T_HTON(sender_id);
105 header_->mux = MUX_T_HTON(mux);
108 u_int32_t EncryptedPacket::getPayloadLength() const
110 return (length_ > sizeof(struct HeaderStruct)) ? (length_ - sizeof(struct HeaderStruct)) : 0; // TODO: fix auth_tag stuff
113 void EncryptedPacket::setPayloadLength(u_int32_t payload_length)
115 Buffer::setLength(payload_length + sizeof(struct HeaderStruct));
116 // depending on allow_realloc buf_ may point to another address
117 // therefore in this case reinit() gets called by Buffer::setLength()
120 void EncryptedPacket::reinit()
122 Buffer::reinit();
123 header_ = reinterpret_cast<struct HeaderStruct*>(buf_);
124 payload_ = buf_ + sizeof(struct HeaderStruct); // TODO: fix auth_tag stuff
125 auth_tag_ = NULL; // TODO: fix auth_tag stuff
128 u_int8_t* EncryptedPacket::getPayload()
130 return payload_;
138 // TODO: fix auth_tag stuff
140 bool EncryptedPacket::hasAuthTag() const
142 // if( auth_tag_ == NULL )
143 return false;
144 // return true;
147 void EncryptedPacket::withAuthTag(bool b)
149 // if( b && (auth_tag_ != NULL) )
150 // throw std::runtime_error("packet already has auth tag function enabled");
151 // //TODO: return instead?
152 // if( ! b && (auth_tag_ == NULL) )
153 // throw std::runtime_error("packet already has auth tag function disabled");
154 // //TODO: return instead?
156 // if( b ) {
157 // auth_tag_ = reinterpret_cast<AuthTag*>( buf_ + sizeof(struct HeaderStruct) );
158 // payload_ = payload_ + AUTHTAG_SIZE;
159 // length_ -= AUTHTAG_SIZE;
160 // max_length_ -= AUTHTAG_SIZE;
161 // } else {
162 // payload_ = reinterpret_cast<u_int8_t*>( auth_tag_ );
163 // length_ += AUTHTAG_SIZE;
164 // max_length_ += AUTHTAG_SIZE;
165 // auth_tag_ = NULL;
166 // }
169 void EncryptedPacket::setAuthTag(AuthTag& tag)
171 // if( auth_tag_ == NULL )
172 // throw std::runtime_error("auth tag not enabled");
174 // if( tag == AuthTag(0) )
175 // return;
177 // if( tag.getLength() != AUTHTAG_SIZE )
178 // throw std::length_error("authtag length mismatch with AUTHTAG_SIZE");
180 // std::memcpy( auth_tag_, tag.getBuf(), AUTHTAG_SIZE );
183 AuthTag EncryptedPacket::getAuthTag() const
185 // if( auth_tag_ == NULL )
186 // throw std::runtime_error("auth tag not enabled");
188 AuthTag at(AUTHTAG_SIZE);
189 // std::memcpy(at, auth_tag_, AUTHTAG_SIZE );
190 return at;