working version with crypto
[anytun.git] / encryptedPacket.cpp
blobc0221e9fef134a3a29f0d14b780cba19758176d0
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 "log.h"
40 EncryptedPacket::EncryptedPacket(u_int32_t payload_length, bool allow_realloc)
41 : Buffer(payload_length + sizeof(struct HeaderStruct), allow_realloc)
43 header_ = reinterpret_cast<struct HeaderStruct*>(buf_);
44 payload_ = buf_ + sizeof(struct HeaderStruct);
45 auth_tag_ = NULL;
46 if(header_)
48 header_->seq_nr = 0;
49 header_->sender_id = 0;
50 header_->mux = 0;
54 seq_nr_t EncryptedPacket::getSeqNr() const
56 if(header_)
57 return SEQ_NR_T_NTOH(header_->seq_nr);
59 return 0;
62 sender_id_t EncryptedPacket::getSenderId() const
64 if(header_)
65 return SENDER_ID_T_NTOH(header_->sender_id);
67 return 0;
70 mux_t EncryptedPacket::getMux() const
72 if(header_)
73 return MUX_T_NTOH(header_->mux);
75 return 0;
78 void EncryptedPacket::setSeqNr(seq_nr_t seq_nr)
80 if(header_)
81 header_->seq_nr = SEQ_NR_T_HTON(seq_nr);
84 void EncryptedPacket::setSenderId(sender_id_t sender_id)
86 if(header_)
87 header_->sender_id = SENDER_ID_T_HTON(sender_id);
90 void EncryptedPacket::setMux(mux_t mux)
92 if(header_)
93 header_->mux = MUX_T_HTON(mux);
96 void EncryptedPacket::setHeader(seq_nr_t seq_nr, sender_id_t sender_id, mux_t mux)
98 if(!header_)
99 return;
101 header_->seq_nr = SEQ_NR_T_HTON(seq_nr);
102 header_->sender_id = SENDER_ID_T_HTON(sender_id);
103 header_->mux = MUX_T_HTON(mux);
106 u_int32_t EncryptedPacket::getPayloadLength() const
108 if(!payload_)
109 return 0;
111 if(!auth_tag_)
112 return (length_ > sizeof(struct HeaderStruct)) ? (length_ - sizeof(struct HeaderStruct)) : 0;
114 return (length_ > (sizeof(struct HeaderStruct) + AUTHTAG_SIZE)) ? (length_ - sizeof(struct HeaderStruct) - AUTHTAG_SIZE) : 0;
117 void EncryptedPacket::setPayloadLength(u_int32_t payload_length)
119 Buffer::setLength(payload_length + sizeof(struct HeaderStruct));
120 // depending on allow_realloc buf_ may point to another address
121 // therefore in this case reinit() gets called by Buffer::setLength()
124 void EncryptedPacket::reinit()
126 header_ = reinterpret_cast<struct HeaderStruct*>(buf_);
127 payload_ = buf_ + sizeof(struct HeaderStruct);
129 if(length_ <= (sizeof(struct HeaderStruct)))
130 payload_ = NULL;
132 if(length_ < (sizeof(struct HeaderStruct))) {
133 header_ = NULL;
134 throw std::runtime_error("packet can't be initialized, buffer is too small");
137 if(auth_tag_)
139 if(length_ < (sizeof(struct HeaderStruct) + AUTHTAG_SIZE)) {
140 auth_tag_ = NULL;
141 throw std::runtime_error("auth-tag can't be enabled, buffer is too small");
143 auth_tag_ = buf_ + length_ - AUTHTAG_SIZE;
147 u_int8_t* EncryptedPacket::getPayload()
149 return payload_;
152 u_int8_t* EncryptedPacket::getAuthenticatedPortion()
154 return buf_;
157 u_int32_t EncryptedPacket::getAuthenticatedPortionLength()
159 if(!buf_)
160 return 0;
162 if(!auth_tag_)
163 return length_;
165 return (length_ > AUTHTAG_SIZE) ? (length_ - AUTHTAG_SIZE) : 0;
168 void EncryptedPacket::withAuthTag(bool b)
170 if((b && auth_tag_) || (!b && !auth_tag_))
171 return;
173 if(b)
175 if(length_ < (sizeof(struct HeaderStruct) + AUTHTAG_SIZE))
176 throw std::runtime_error("auth-tag can't be enabled, buffer is too small");
178 auth_tag_ = buf_ + length_ - AUTHTAG_SIZE;
180 else
181 auth_tag_ = NULL;
184 void EncryptedPacket::addAuthTag()
186 if(auth_tag_)
187 return;
189 auth_tag_ = buf_; // will be set to the correct value @ reinit
190 setLength(length_ + AUTHTAG_SIZE);
191 if(auth_tag_ == buf_) // reinit was not called by setLength
192 reinit();
195 void EncryptedPacket::removeAuthTag()
197 if(!auth_tag_)
198 return;
200 auth_tag_ = NULL;
201 setLength(length_ - AUTHTAG_SIZE);
204 u_int8_t* EncryptedPacket::getAuthTag()
206 return auth_tag_;
209 u_int32_t EncryptedPacket::getAuthTagLength()
211 if(auth_tag_)
212 return AUTHTAG_SIZE;
214 return 0;