switched to endpoint @ anytun-config as well
[anytun.git] / src / encryptedPacket.cpp
blob1562f71e35f8edb271139883b13130a91d97f1d8
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-2008 Othmar Gsenger, Erwin Nindl,
15 * Christian Pointner <satp@wirdorange.org>
17 * This file is part of Anytun.
19 * Anytun is free software: you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License version 3 as
21 * published by the Free Software Foundation.
23 * Anytun is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with anytun. If not, see <http://www.gnu.org/licenses/>.
32 #include <stdexcept>
33 #include <iostream>
34 #include <arpa/inet.h>
35 #include <cstdio> // for std::memcpy
37 #include "encryptedPacket.h"
38 #include "datatypes.h"
39 #include "log.h"
41 EncryptedPacket::EncryptedPacket(u_int32_t payload_length, bool allow_realloc)
42 : Buffer(payload_length + sizeof(struct HeaderStruct), allow_realloc)
44 header_ = reinterpret_cast<struct HeaderStruct*>(buf_);
45 payload_ = buf_ + sizeof(struct HeaderStruct);
46 auth_tag_ = NULL;
47 if(header_)
49 header_->seq_nr = 0;
50 header_->sender_id = 0;
51 header_->mux = 0;
55 seq_nr_t EncryptedPacket::getSeqNr() const
57 if(header_)
58 return SEQ_NR_T_NTOH(header_->seq_nr);
60 return 0;
63 sender_id_t EncryptedPacket::getSenderId() const
65 if(header_)
66 return SENDER_ID_T_NTOH(header_->sender_id);
68 return 0;
71 mux_t EncryptedPacket::getMux() const
73 if(header_)
74 return MUX_T_NTOH(header_->mux);
76 return 0;
79 void EncryptedPacket::setSeqNr(seq_nr_t seq_nr)
81 if(header_)
82 header_->seq_nr = SEQ_NR_T_HTON(seq_nr);
85 void EncryptedPacket::setSenderId(sender_id_t sender_id)
87 if(header_)
88 header_->sender_id = SENDER_ID_T_HTON(sender_id);
91 void EncryptedPacket::setMux(mux_t mux)
93 if(header_)
94 header_->mux = MUX_T_HTON(mux);
97 void EncryptedPacket::setHeader(seq_nr_t seq_nr, sender_id_t sender_id, mux_t mux)
99 if(!header_)
100 return;
102 header_->seq_nr = SEQ_NR_T_HTON(seq_nr);
103 header_->sender_id = SENDER_ID_T_HTON(sender_id);
104 header_->mux = MUX_T_HTON(mux);
107 u_int32_t EncryptedPacket::getPayloadLength() const
109 if(!payload_)
110 return 0;
112 if(!auth_tag_)
113 return (length_ > sizeof(struct HeaderStruct)) ? (length_ - sizeof(struct HeaderStruct)) : 0;
115 return (length_ > (sizeof(struct HeaderStruct) + AUTHTAG_SIZE)) ? (length_ - sizeof(struct HeaderStruct) - AUTHTAG_SIZE) : 0;
118 void EncryptedPacket::setPayloadLength(u_int32_t payload_length)
120 Buffer::setLength(payload_length + sizeof(struct HeaderStruct));
121 // depending on allow_realloc buf_ may point to another address
122 // therefore in this case reinit() gets called by Buffer::setLength()
125 void EncryptedPacket::reinit()
127 header_ = reinterpret_cast<struct HeaderStruct*>(buf_);
128 payload_ = buf_ + sizeof(struct HeaderStruct);
130 if(length_ <= (sizeof(struct HeaderStruct)))
131 payload_ = NULL;
133 if(length_ < (sizeof(struct HeaderStruct))) {
134 header_ = NULL;
135 throw std::runtime_error("packet can't be initialized, buffer is too small");
138 if(auth_tag_)
140 if(length_ < (sizeof(struct HeaderStruct) + AUTHTAG_SIZE)) {
141 auth_tag_ = NULL;
142 throw std::runtime_error("auth-tag can't be enabled, buffer is too small");
144 auth_tag_ = buf_ + length_ - AUTHTAG_SIZE;
148 u_int8_t* EncryptedPacket::getPayload()
150 return payload_;
153 u_int8_t* EncryptedPacket::getAuthenticatedPortion()
155 return buf_;
158 u_int32_t EncryptedPacket::getAuthenticatedPortionLength()
160 if(!buf_)
161 return 0;
163 if(!auth_tag_)
164 return length_;
166 return (length_ > AUTHTAG_SIZE) ? (length_ - AUTHTAG_SIZE) : 0;
169 void EncryptedPacket::withAuthTag(bool b)
171 if((b && auth_tag_) || (!b && !auth_tag_))
172 return;
174 if(b)
176 if(length_ < (sizeof(struct HeaderStruct) + AUTHTAG_SIZE))
177 throw std::runtime_error("auth-tag can't be enabled, buffer is too small");
179 auth_tag_ = buf_ + length_ - AUTHTAG_SIZE;
181 else
182 auth_tag_ = NULL;
185 void EncryptedPacket::addAuthTag()
187 if(auth_tag_)
188 return;
190 auth_tag_ = buf_; // will be set to the correct value @ reinit
191 setLength(length_ + AUTHTAG_SIZE);
192 if(auth_tag_ == buf_) // reinit was not called by setLength
193 reinit();
196 void EncryptedPacket::removeAuthTag()
198 if(!auth_tag_)
199 return;
201 auth_tag_ = NULL;
202 setLength(length_ - AUTHTAG_SIZE);
205 u_int8_t* EncryptedPacket::getAuthTag()
207 return auth_tag_;
210 u_int32_t EncryptedPacket::getAuthTagLength()
212 if(auth_tag_)
213 return AUTHTAG_SIZE;
215 return 0;