libbgp  0.6
A C++ BGP Library.
serializable.cc
Go to the documentation of this file.
1 
11 #include "serializable.h"
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 
16 namespace libbgp {
17 
24  err_code = 0;
25  err_subcode = 0;
26  err_data = NULL;
27  err_len = 0;
28  this->logger = logger;
29 }
30 
36  if (err_len > 0 && err_data != NULL) free(err_data);
37 }
38 
45 bool Serializable::hasError() const {
46  return err_len != 0;
47 }
48 
61 void Serializable::setError(uint8_t err, uint8_t suberr, const uint8_t *data, size_t data_len) {
62  err_code = err;
63  err_subcode = suberr;
64 
65  if (err_len != 0) {
66  logger->log(FATAL, "Serializable::setError: error already exists.\n");
67  throw "err_exist";
68  }
69 
70  if (data_len == 0) return;
71  err_len = data_len;
72  err_data = (uint8_t *) malloc(err_len);
73  memcpy(err_data, data, data_len);
74 }
75 
76 uint8_t Serializable::getErrorCode() const {
77  return err_code;
78 }
79 
80 uint8_t Serializable::getErrorSubCode() const {
81  return err_subcode;
82 }
83 
84 const uint8_t* Serializable::getError() const {
85  return err_data;
86 }
87 
88 size_t Serializable::getErrorLength() const {
89  return err_len;
90 }
91 
98  setError(other.getErrorCode(), other.getErrorSubCode(), other.getError(), other.getErrorLength());
99 }
100 
110 ssize_t Serializable::print(uint8_t *to, size_t buf_sz) const {
111  return doPrint(0, &to, &buf_sz);
112 }
113 
125 ssize_t Serializable::print(size_t indent, uint8_t *to, size_t buf_sz) const {
126  return doPrint(indent, &to, &buf_sz);
127 }
128 
144 ssize_t Serializable::_print(size_t indent, uint8_t **to, size_t *buf_left, const char* format, ...) {
145  if (*buf_left <= indent * 4) return 0;
146  for (size_t i = 0; i < indent; i++) {
147  sprintf((char *) *to, " ");
148  *to += 4;
149  *buf_left -= 4;
150  }
151  va_list args;
152  va_start(args, format);
153  ssize_t sz = vsnprintf((char *) *to, *buf_left, format, args);
154  va_end(args);
155 
156  if (sz < 0) return sz;
157  if ((size_t) sz > *buf_left) {
158  size_t written = *buf_left;
159  *buf_left = 0;
160  return written + indent * 4;
161  }
162 
163  *buf_left -= sz;
164  *to += sz;
165 
166  return sz + indent * 4;
167 }
168 
176 ssize_t Serializable::length() const {
177  logger->log(WARN, "Serializable::length: using default implementation of Serializable::length() (write to dummy).\n");
178  uint8_t buffer[4096];
179  return write(buffer, 4096);
180 }
181 
188  this->logger = logger;
189 }
190 
191 }
virtual ssize_t doPrint(size_t indent, uint8_t **to, size_t *buf_sz) const =0
Print implementation.
virtual ssize_t length() const
Get size in bytes required to serialize the object.
static ssize_t _print(size_t indent, uint8_t **to, size_t *buf_left, const char *format,...)
Print helper.
bool hasError() const
Check if error information available.
Definition: serializable.cc:45
~Serializable()
Destroy the Serializable:: Serializable object.
Definition: serializable.cc:35
The serializable base class.
Definition: serializable.h:26
ssize_t print(uint8_t *to, size_t buf_sz) const
Print the Serializable object as human readable string.
void log(LogLevel level, const char *format_str,...)
Log a message. Consider using LIBBGP_LOG if logging the message needs a lot of computing power...
virtual ssize_t write(uint8_t *to, size_t buf_sz) const =0
Serialize the object and write to buffer.
void setLogger(BgpLogHandler *logger)
Replace logger for this object.
void setError(uint8_t err, uint8_t suberr, const uint8_t *data, size_t data_len)
Set the error information.
Definition: serializable.cc:61
Definition: bgp-afi.h:14
The BgpLogHandler class.
The serializable base.
Serializable(BgpLogHandler *logger)
Construct a new Serializable:: Serializable object.
Definition: serializable.cc:23
void forwardParseError(const Serializable &other)
Forward error information from other Serializable object.
Definition: serializable.cc:97