17 #include <sys/types.h>
25 #include "rofl/common/croflexception.h"
26 #include "rofl/common/cclock.h"
27 #include "rofl/common/fframe.h"
28 #include "rofl/common/cmemory.h"
29 #include "rofl/common/thread_helper.h"
31 #include "rofl/common/openflow/openflow.h"
63 rofl::cmemory(0), head(0), tail(0), initial_head(0), initial_tail(0) {};
75 size_t size,
size_t head = DEFAULT_HSPACE,
size_t tail = DEFAULT_TSPACE) :
76 rofl::cmemory(head + size + tail), head(head), tail(tail), initial_head(head), initial_tail(tail) {};
82 uint8_t *buf,
size_t buflen,
size_t head = DEFAULT_HSPACE,
size_t tail = DEFAULT_TSPACE) :
83 rofl::cmemory(head + buflen + tail), head(head), tail(tail), initial_head(head), initial_tail(tail)
84 { memcpy(
somem()+head, buf, buflen); };
96 operator= (
const cpacket& pack) {
102 initial_head = pack.initial_head;
103 initial_tail = pack.initial_tail;
125 operator[] (
size_t index) {
126 if (index >= length()) {
129 return (rofl::cmemory::operator[] (head + index));
136 operator== (
const cpacket& p) {
137 if (length() != p.length())
139 return (not (memcmp(soframe(), p.soframe(), length())));
146 operator== (
const cmemory& m) {
147 if (length() != m.
memlen())
149 return (not (memcmp(soframe(), m.
somem(), length())));
158 if (length() != p.length())
160 return (memcmp(soframe(), p.soframe(), length()));
169 if (length() != m.
memlen())
171 return (memcmp(soframe(), m.
somem(), length()));
180 cpacket p(this->length() + pack.length());
181 memcpy(p.soframe(), this->soframe(), this->length());
182 memcpy(p.soframe() + this->length(), pack.soframe(), pack.length());
192 size_t offset = this->length();
194 memcpy(this->soframe() + offset, pack.soframe(), pack.length());
209 memcpy(soframe(), buf, buflen);
233 {
return (0 == length()); };
246 pack(uint8_t* buf,
size_t buflen) {
247 if (buflen < length()) {
248 throw ePacketInval(
"cpacket::pack() insufficient space");
250 memcpy(buf, soframe(), buflen);
259 memcpy(soframe(), buf, buflen);
276 uint8_t* ptr,
size_t len) {
throw eNotImplemented(); };
287 throw ePacketInval(
"cpacket::tag_insert() insufficient head space");
299 throw ePacketInval(
"cpacket::tag_insert() invalid tag size");
308 push(
unsigned int offset,
unsigned int nbytes) {
310 resize(length() + nbytes);
312 memmove(
somem() + head - nbytes,
somem() + head, offset);
314 memset(
somem() + head + offset, 0, nbytes);
315 return (soframe() + offset);
322 pop(
unsigned int offset,
unsigned int nbytes) {
323 if ((head + offset + nbytes) >
memlen()) {
324 throw ePacketOutOfRange(
"rofl::cpacket::pop()");
326 memmove(
somem() + head + nbytes,
somem() + head, offset);
327 memset(
somem() + head, 0, nbytes);
336 friend std::ostream &
337 operator<<(std::ostream& os,
const cpacket& pack) {
339 os <<
"data:" << (
void*)pack.soframe() <<
" ";
340 os <<
"datalen:" << (int)pack.length() <<
" ";
341 os <<
">" << std::endl;
344 unsigned int nbytes = 32;
345 if (pack.length() > 0) {
346 for (
unsigned int i=0; i < pack.length(); i++) {
347 if (0 == (i % nbytes)) {
351 << std::dec << (i/nbytes) <<
": " << std::hex
353 << std::setfill(
' ');
356 os << std::setfill(
'0')
358 << std::hex << (int)(*(pack.soframe() + i)) << std::dec
363 if (0 == ((i+1) % 8))
365 if (0 == ((i+1) % nbytes))
375 static const int DEFAULT_SIZE = 0;
376 static const int DEFAULT_HSPACE = 64;
377 static const int DEFAULT_TSPACE = 256;
Definition: croflexception.h:57
virtual void assign(uint8_t *buf, size_t buflen)
Overwrites memory area with plain buffer specified. Resizes internal memory as necessary.
Definition: cpacket.h:204
size_t memlen() const
Returns length of allocated memory area.
Definition: cmemory.cc:109
uint8_t * somem() const
Returns pointer to start of allocated memory area.
Definition: cmemory.cc:101
virtual void pack(uint8_t *buf, size_t buflen)
Copies content of this cmemory instance to specified buffer.
Definition: cpacket.h:246
virtual void unpack(uint8_t *buf, size_t buflen)
Copies content of specified buffer into this cmemory instance .
Definition: cpacket.h:257
virtual uint8_t * resize(size_t len)
Resizes allocated memory area by calling C-function realloc().
Definition: cmemory.cc:253
C++ abstraction for malloc'ed memory areas.
Definition: cmemory.h:44
virtual uint8_t * resize(size_t len)
Resizes allocated memory area by calling C-function realloc().
Definition: cpacket.h:216
cmemory & operator=(cmemory const &m)
Assignment operator.
Definition: cmemory.cc:85
bool empty() const
Returns boolean value empty/non empty.
Definition: cpacket.h:232
Definition: croflexception.h:27
void clear()
Clears the allocated memory area by setting all bytes to 0.
Definition: cmemory.cc:281