bugfix with packet length
[anytun.git] / buffer.cpp
blobcea8d2c198332bf54d96ea3fd117f6d391a6fb14
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 <string>
33 #include <sstream>
34 #include <boost/archive/text_oarchive.hpp>
35 #include <boost/archive/text_iarchive.hpp>
36 #include "datatypes.h"
37 #include "buffer.h"
39 Buffer::Buffer(bool allow_realloc) : buf_(0), length_(0), real_length_(0), allow_realloc_(allow_realloc)
43 Buffer::Buffer(u_int32_t length, bool allow_realloc) : length_(length), real_length_(length_ + Buffer::OVER_SIZE_),
44 allow_realloc_(allow_realloc)
46 buf_ = new u_int8_t[real_length_];
47 if(!buf_) {
48 length_ = 0;
49 real_length_ = 0;
50 throw std::bad_alloc();
52 std::memset(buf_, 0, real_length_);
55 Buffer::Buffer(u_int8_t* data, u_int32_t length, bool allow_realloc) : length_(length), real_length_(length + Buffer::OVER_SIZE_),
56 allow_realloc_(allow_realloc)
58 buf_ = new u_int8_t[real_length_];
59 if(!buf_) {
60 length_ = 0;
61 real_length_ = 0;
62 throw std::bad_alloc();
64 std::memcpy(buf_, data, length_);
67 Buffer::~Buffer()
69 if(buf_)
70 delete[] buf_;
73 Buffer::Buffer(const Buffer &src) : length_(src.length_), real_length_(src.real_length_), allow_realloc_(src.allow_realloc_)
75 buf_ = new u_int8_t[real_length_];
76 if(!buf_) {
77 length_ = 0;
78 real_length_ = 0;
79 throw std::bad_alloc();
81 std::memcpy(buf_, src.buf_, length_);
84 void Buffer::operator=(const Buffer &src)
86 if(buf_)
87 delete[] buf_;
89 length_ = src.length_;
90 real_length_ = src.real_length_;
91 allow_realloc_ = src.allow_realloc_;
93 buf_ = new u_int8_t[real_length_];
94 if(!buf_) {
95 length_ = 0;
96 real_length_ = 0;
97 throw std::bad_alloc();
99 std::memcpy(buf_, src.buf_, length_);
104 bool Buffer::operator==(const Buffer &cmp) const
106 if(length_ != cmp.length_)
107 return false;
109 if(!std::memcmp(buf_, cmp.buf_, length_))
110 return true;
112 return false;
115 Buffer Buffer::operator^(const Buffer &xor_by) const
117 u_int32_t res_length = (xor_by.length_ > length_) ? xor_by.length_ : length_;
118 u_int32_t min_length = (xor_by.length_ < length_) ? xor_by.length_ : length_;
119 Buffer res(res_length);
121 for( u_int32_t index = 0; index < min_length; index++ )
122 res[index] = buf_[index] ^ xor_by[index];
124 return res;
127 u_int32_t Buffer::getLength() const
129 return length_;
132 void Buffer::setLength(u_int32_t new_length)
134 if(new_length == length_)
135 return;
137 if(new_length > real_length_)
139 if(!allow_realloc_)
140 throw std::out_of_range("buffer::setLength() - reallocation not allowed for this Buffer");
142 u_int8_t* old_buf = buf_;
143 u_int32_t old_length = length_;
145 length_ = new_length;
146 real_length_ = length_ + Buffer::OVER_SIZE_;
148 buf_ = new u_int8_t[real_length_];
149 if(!buf_) {
150 length_ = 0;
151 real_length_ = 0;
152 if(old_buf)
153 delete[] old_buf;
155 throw std::bad_alloc();
157 std::memcpy(buf_, old_buf, old_length);
159 if(old_buf)
160 delete[] old_buf;
162 old_buf = &buf_[old_length];
163 std::memset(old_buf, 0, real_length_ - old_length);
165 else
166 length_ = new_length;
170 u_int8_t* Buffer::getBuf()
172 return buf_;
175 u_int8_t& Buffer::operator[](u_int32_t index)
177 if(index >= length_)
178 throw std::out_of_range("buffer::operator[]");
180 return buf_[index];
183 u_int8_t Buffer::operator[](u_int32_t index) const
185 if(index >= length_)
186 throw std::out_of_range("buffer::operator[] const");
188 return buf_[index];
191 Buffer::operator u_int8_t*()
193 return buf_;
196 std::string Buffer::getHexDump() const
198 std::stringstream ss;
199 ss << "Length=" << length_ << std::endl << std::hex << std::uppercase;
200 for( u_int32_t index = 0; index < length_; index++ )
202 ss << std::setw(2) << std::setfill('0') << u_int32_t(buf_[index]) << " ";
203 if(!((index+1) % 16)) {
204 ss << std::endl;
205 continue;
207 if(!((index+1) % 8))
208 ss << " ";
210 return ss.str();
213 bool Buffer::isReallocAllowed() const
215 return allow_realloc_;