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(u_int32_t length, bool allow_realloc) : length_(length), real_length_(length_ + Buffer::OVER_SIZE_),
48 allow_realloc_(allow_realloc)
50 buf_ = new u_int8_t[real_length_];
54 throw std::bad_alloc();
56 std::memset(buf_, 0, real_length_);
59 Buffer::Buffer(u_int8_t* data, u_int32_t length, bool allow_realloc) : length_(length), real_length_(length + Buffer::OVER_SIZE_),
60 allow_realloc_(allow_realloc)
68 buf_ = new u_int8_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<u_int32_t>(hex_data.size())/2),
78 real_length_(length_ + Buffer::OVER_SIZE_),
79 allow_realloc_(allow_realloc)
81 buf_ = new u_int8_t[real_length_];
85 throw std::bad_alloc();
88 for(u_int32_t i=0; i<length_; ++i)
91 std::istringstream ss(std::string(hex_data.c_str(), i*2, 2));
92 if(!(ss >> std::hex >> tmp)) tmp = 0;
93 buf_[i] = static_cast<u_int8_t>(tmp);
103 Buffer::Buffer(const Buffer &src) : length_(src.length_), real_length_(src.real_length_), allow_realloc_(src.allow_realloc_)
105 buf_ = new u_int8_t[real_length_];
109 throw std::bad_alloc();
111 std::memcpy(buf_, src.buf_, length_);
114 void Buffer::operator=(const Buffer &src)
119 length_ = src.length_;
120 real_length_ = src.real_length_;
121 allow_realloc_ = src.allow_realloc_;
123 buf_ = new u_int8_t[real_length_];
127 throw std::bad_alloc();
129 std::memcpy(buf_, src.buf_, length_);
132 bool Buffer::operator==(const Buffer &cmp) const
134 if(length_ != cmp.length_)
137 if(!std::memcmp(buf_, cmp.buf_, length_))
143 Buffer Buffer::operator^(const Buffer &xor_by) const
145 u_int32_t res_length = (xor_by.length_ > length_) ? xor_by.length_ : length_;
146 u_int32_t min_length = (xor_by.length_ < length_) ? xor_by.length_ : length_;
147 Buffer res(res_length);
149 for( u_int32_t index = 0; index < min_length; index++ )
150 res[index] = buf_[index] ^ xor_by[index];
155 u_int32_t Buffer::getLength() const
160 void Buffer::setLength(u_int32_t new_length)
162 if(new_length == length_)
165 if(new_length > real_length_)
168 throw std::out_of_range("buffer::setLength() - reallocation not allowed for this Buffer");
170 u_int8_t* old_buf = buf_;
171 u_int32_t old_length = length_;
173 length_ = new_length;
174 real_length_ = length_ + Buffer::OVER_SIZE_;
176 buf_ = new u_int8_t[real_length_];
183 throw std::bad_alloc();
185 std::memcpy(buf_, old_buf, old_length);
190 old_buf = &buf_[old_length];
191 std::memset(old_buf, 0, real_length_ - old_length);
194 length_ = new_length;
200 u_int8_t* Buffer::getBuf()
205 u_int8_t& Buffer::operator[](u_int32_t index)
208 throw std::out_of_range("buffer::operator[]");
213 u_int8_t Buffer::operator[](u_int32_t index) const
216 throw std::out_of_range("buffer::operator[] const");
221 Buffer::operator u_int8_t*()
226 std::string Buffer::getHexDump() const
228 std::stringstream ss;
229 ss << "Length=" << length_ << std::endl << std::hex << std::uppercase;
230 for( u_int32_t index = 0; index < length_; index++ )
232 ss << std::setw(2) << std::setfill('0') << u_int32_t(buf_[index]) << " ";
233 if(!((index+1) % 16)) {
243 std::string Buffer::getHexDumpOneLine() const
245 std::stringstream ss;
246 ss << length_ << " Bytes,'" << std::hex << std::uppercase;
247 for( u_int32_t index = 0; index < length_; index++ )
249 ss << std::setw(2) << std::setfill('0') << u_int32_t(buf_[index]);
255 bool Buffer::isReallocAllowed() const
257 return allow_realloc_;