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-2009 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/>.
38 #include <boost/archive/text_oarchive.hpp>
39 #include <boost/archive/text_iarchive.hpp>
40 #include "datatypes.h"
43 Buffer::Buffer(bool allow_realloc) : buf_(0), length_(0), real_length_(0), allow_realloc_(allow_realloc)
47 Buffer::Buffer(uint32_t length, bool allow_realloc) : length_(length), real_length_(length_ + Buffer::OVER_SIZE_),
48 allow_realloc_(allow_realloc)
50 buf_ = new uint8_t[real_length_];
54 throw std::bad_alloc();
56 std::memset(buf_, 0, real_length_);
59 Buffer::Buffer(uint8_t* data, uint32_t length, bool allow_realloc) : length_(length), real_length_(length + Buffer::OVER_SIZE_),
60 allow_realloc_(allow_realloc)
68 buf_ = new uint8_t[real_length_];
72 throw std::bad_alloc();
74 std::memcpy(buf_, data, length_);
77 Buffer::Buffer(std::string hex_data, bool allow_realloc) : length_(static_cast<uint32_t>(hex_data.size())/2),
78 real_length_(length_ + Buffer::OVER_SIZE_),
79 allow_realloc_(allow_realloc)
81 buf_ = new uint8_t[real_length_];
85 throw std::bad_alloc();
88 for(uint32_t i=0; i<length_; ++i) {
90 std::istringstream ss(std::string(hex_data.c_str(), i*2, 2));
91 if(!(ss >> std::hex >> tmp)) { tmp = 0; }
92 buf_[i] = static_cast<uint8_t>(tmp);
103 Buffer::Buffer(const Buffer& src) : length_(src.length_), real_length_(src.real_length_), allow_realloc_(src.allow_realloc_)
105 buf_ = new uint8_t[real_length_];
109 throw std::bad_alloc();
111 std::memcpy(buf_, src.buf_, length_);
114 void Buffer::operator=(const Buffer& src)
120 length_ = src.length_;
121 real_length_ = src.real_length_;
122 allow_realloc_ = src.allow_realloc_;
124 buf_ = new uint8_t[real_length_];
128 throw std::bad_alloc();
130 std::memcpy(buf_, src.buf_, length_);
133 bool Buffer::operator==(const Buffer& cmp) const
135 if(length_ != cmp.length_) {
139 if(!std::memcmp(buf_, cmp.buf_, length_)) {
146 Buffer Buffer::operator^(const Buffer& xor_by) const
148 uint32_t res_length = (xor_by.length_ > length_) ? xor_by.length_ : length_;
149 uint32_t min_length = (xor_by.length_ < length_) ? xor_by.length_ : length_;
150 Buffer res(res_length);
152 for(uint32_t index = 0; index < min_length; index++) {
153 res[index] = buf_[index] ^ xor_by[index];
159 uint32_t Buffer::getLength() const
164 void Buffer::setLength(uint32_t new_length)
166 if(new_length == length_) {
170 if(new_length > real_length_) {
171 if(!allow_realloc_) {
172 throw std::out_of_range("buffer::setLength() - reallocation not allowed for this Buffer");
175 uint8_t* old_buf = buf_;
176 uint32_t old_length = length_;
178 length_ = new_length;
179 real_length_ = length_ + Buffer::OVER_SIZE_;
181 buf_ = new uint8_t[real_length_];
189 throw std::bad_alloc();
191 std::memcpy(buf_, old_buf, old_length);
197 old_buf = &buf_[old_length];
198 std::memset(old_buf, 0, real_length_ - old_length);
200 length_ = new_length;
207 uint8_t* Buffer::getBuf()
212 uint8_t& Buffer::operator[](uint32_t index)
214 if(index >= length_) {
215 throw std::out_of_range("buffer::operator[]");
221 uint8_t Buffer::operator[](uint32_t index) const
223 if(index >= length_) {
224 throw std::out_of_range("buffer::operator[] const");
230 Buffer::operator uint8_t*()
235 std::string Buffer::getHexDump() const
237 std::stringstream ss;
238 ss << "Length=" << length_ << std::endl << std::hex << std::uppercase;
239 for(uint32_t index = 0; index < length_; index++) {
240 ss << std::setw(2) << std::setfill('0') << uint32_t(buf_[index]) << " ";
241 if(!((index+1) % 16)) {
245 if(!((index+1) % 8)) {
252 std::string Buffer::getHexDumpOneLine() const
254 std::stringstream ss;
255 ss << length_ << " Bytes,'" << std::hex << std::uppercase;
256 for(uint32_t index = 0; index < length_; index++) {
257 ss << std::setw(2) << std::setfill('0') << uint32_t(buf_[index]);
263 bool Buffer::isReallocAllowed() const
265 return allow_realloc_;