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 methods 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-2014 Markus Grüneis, 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 as published by
21 * the Free Software Foundation, either version 3 of the License, or
24 * Anytun is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with Anytun. If not, see <http://www.gnu.org/licenses/>.
32 * In addition, as a special exception, the copyright holders give
33 * permission to link the code of portions of this program with the
34 * OpenSSL library under certain conditions as described in each
35 * individual source file, and distribute linked combinations
37 * You must obey the GNU General Public License in all respects
38 * for all of the code used other than OpenSSL. If you modify
39 * file(s) with this exception, you may extend this exception to your
40 * version of the file(s), but you are not obligated to do so. If you
41 * do not wish to do so, delete this exception statement from your
42 * version. If you delete this exception statement from all source
43 * files in the program, then also delete it here.
51 #include <boost/archive/text_oarchive.hpp>
52 #include <boost/archive/text_iarchive.hpp>
53 #include "datatypes.h"
56 Buffer::Buffer(bool allow_realloc) : buf_(0), length_(0), real_length_(0), allow_realloc_(allow_realloc)
60 Buffer::Buffer(uint32_t length, bool allow_realloc) : length_(length), real_length_(length_ + Buffer::OVER_SIZE_),
61 allow_realloc_(allow_realloc)
63 buf_ = new uint8_t[real_length_];
67 throw std::bad_alloc();
69 std::memset(buf_, 0, real_length_);
72 Buffer::Buffer(uint8_t* data, uint32_t length, bool allow_realloc) : length_(length), real_length_(length + Buffer::OVER_SIZE_),
73 allow_realloc_(allow_realloc)
81 buf_ = new uint8_t[real_length_];
85 throw std::bad_alloc();
87 std::memcpy(buf_, data, length_);
90 Buffer::Buffer(std::string hex_data, bool allow_realloc) : length_(static_cast<uint32_t>(hex_data.size())/2),
91 real_length_(length_ + Buffer::OVER_SIZE_),
92 allow_realloc_(allow_realloc)
94 buf_ = new uint8_t[real_length_];
98 throw std::bad_alloc();
101 for(uint32_t i=0; i<length_; ++i) {
103 std::istringstream ss(std::string(hex_data.c_str(), i*2, 2));
104 if(!(ss >> std::hex >> tmp)) { tmp = 0; }
105 buf_[i] = static_cast<uint8_t>(tmp);
116 Buffer::Buffer(const Buffer& src) : length_(src.length_), real_length_(src.real_length_), allow_realloc_(src.allow_realloc_)
118 buf_ = new uint8_t[real_length_];
122 throw std::bad_alloc();
124 std::memcpy(buf_, src.buf_, length_);
127 void Buffer::operator=(const Buffer& src)
133 length_ = src.length_;
134 real_length_ = src.real_length_;
135 allow_realloc_ = src.allow_realloc_;
137 buf_ = new uint8_t[real_length_];
141 throw std::bad_alloc();
143 std::memcpy(buf_, src.buf_, length_);
146 bool Buffer::operator==(const Buffer& cmp) const
148 if(length_ != cmp.length_) {
152 if(!std::memcmp(buf_, cmp.buf_, length_)) {
159 Buffer Buffer::operator^(const Buffer& xor_by) const
161 uint32_t res_length = (xor_by.length_ > length_) ? xor_by.length_ : length_;
162 uint32_t min_length = (xor_by.length_ < length_) ? xor_by.length_ : length_;
163 Buffer res(res_length);
165 for(uint32_t index = 0; index < min_length; index++) {
166 res[index] = buf_[index] ^ xor_by[index];
172 uint32_t Buffer::getLength() const
177 void Buffer::setLength(uint32_t new_length)
179 if(new_length == length_) {
183 if(new_length > real_length_) {
184 if(!allow_realloc_) {
185 throw std::out_of_range("buffer::setLength() - reallocation not allowed for this Buffer");
188 uint8_t* old_buf = buf_;
189 uint32_t old_length = length_;
191 length_ = new_length;
192 real_length_ = length_ + Buffer::OVER_SIZE_;
194 buf_ = new uint8_t[real_length_];
202 throw std::bad_alloc();
204 std::memcpy(buf_, old_buf, old_length);
210 old_buf = &buf_[old_length];
211 std::memset(old_buf, 0, real_length_ - old_length);
213 length_ = new_length;
220 uint8_t* Buffer::getBuf()
225 uint8_t& Buffer::operator[](uint32_t index)
227 if(index >= length_) {
228 throw std::out_of_range("buffer::operator[]");
234 uint8_t Buffer::operator[](uint32_t index) const
236 if(index >= length_) {
237 throw std::out_of_range("buffer::operator[] const");
243 Buffer::operator uint8_t*()
248 std::string Buffer::getHexDump() const
250 std::stringstream ss;
251 ss << "Length=" << length_ << std::endl << std::hex << std::uppercase;
252 for(uint32_t index = 0; index < length_; index++) {
253 ss << std::setw(2) << std::setfill('0') << uint32_t(buf_[index]) << " ";
254 if(!((index+1) % 16)) {
258 if(!((index+1) % 8)) {
265 std::string Buffer::getHexDumpOneLine() const
267 std::stringstream ss;
268 ss << length_ << " Bytes,'" << std::hex << std::uppercase;
269 for(uint32_t index = 0; index < length_; index++) {
270 ss << std::setw(2) << std::setfill('0') << uint32_t(buf_[index]);
276 bool Buffer::isReallocAllowed() const
278 return allow_realloc_;