libbgp  0.6
A C++ BGP Library.
bgp-notification-message.cc
Go to the documentation of this file.
1 
12 #include "bgp-errcode.h"
13 #include "value-op.h"
14 #include <stdlib.h>
15 #include <string.h>
16 
17 namespace libbgp {
18 
24 BgpNotificationMessage::BgpNotificationMessage(__attribute__((unused)) BgpLogHandler *loggger) : BgpMessage(logger) {
25  type = NOTIFICATION;
26  err_data = 0;
27  data_len = 0;
28 }
29 
30 BgpNotificationMessage::~BgpNotificationMessage() {
31  if (data_len > 0) free(data);
32 }
33 
42 BgpNotificationMessage::BgpNotificationMessage(__attribute__((unused)) BgpLogHandler *loggger, uint8_t errcode, uint8_t subcode, const uint8_t *data, uint16_t data_len) : BgpMessage(logger) {
43  type = NOTIFICATION;
44  this->errcode = errcode;
45  this->subcode = subcode;
46  this->data_len = data_len;
47  if (data_len > 0) {
48  this->data = (uint8_t *) malloc(data_len);
49  memcpy(this->data, data, data_len);
50  }
51 }
52 
53 ssize_t BgpNotificationMessage::parse(const uint8_t *from, size_t msg_sz) {
54  if (msg_sz < 2) {
55  setError(E_HEADER, E_LENGTH, NULL, 0);
56  return -1;
57  }
58 
59  const uint8_t *buffer = from;
60 
61  errcode = getValue<uint8_t>(&buffer);
62  subcode = getValue<uint8_t>(&buffer);
63 
64  if (msg_sz > 2) {
65  setError(E_HEADER, E_LENGTH, NULL, 0);
66  return -1;
67  }
68 
69  return msg_sz;
70 }
71 
72 ssize_t BgpNotificationMessage::write(uint8_t *to, size_t buf_sz) const {
73  if (buf_sz < (size_t) (data_len + 2)) return -1;
74  uint8_t *buffer = to;
75 
76  putValue<uint8_t>(&buffer, errcode);
77  putValue<uint8_t>(&buffer, subcode);
78  memcpy(buffer, data, data_len);
79 
80  return data_len + 2;
81 }
82 
83 ssize_t BgpNotificationMessage::doPrint(size_t indent, uint8_t **to, size_t *buf_sz) const {
84  size_t written = 0;
85 
86  written += _print(indent, to, buf_sz, "NotificationMessage {\n");
87  const char *err_msg = bgp_error_code_str[errcode];
88  const char *err_sub_msg = bgp_error_code_str[0];
89  switch (errcode) {
90  case E_HEADER: err_sub_msg = bgp_header_error_subcode_str[subcode]; break;
91  case E_OPEN: err_sub_msg = bgp_open_error_subcode_str[subcode]; break;
92  case E_UPDATE: err_sub_msg = bgp_update_error_str[subcode]; break;
93  case E_FSM: err_sub_msg = bgp_fsm_error_str[subcode]; break;
94  case E_CEASE: err_sub_msg = bgp_cease_error_str[subcode]; break;
95  }
96 
97  indent++; {
98  written += _print(indent, to, buf_sz, "Error { %s }\n", err_msg);
99  written += _print(indent, to, buf_sz, "SubError { %s }\n", err_sub_msg);
100  }; indent--;
101 
102  written += _print(indent, to, buf_sz, "}\n");
103 
104  return written;
105 }
106 
107 }
ssize_t doPrint(size_t indent, uint8_t **to, size_t *buf_sz) const
Print implementation.
const char * bgp_error_code_str[7]
Error strings for BGP error codes.
Definition: bgp-errcode.cc:19
BgpMessage(BgpLogHandler *logger)
Construct a new Bgp Message object.
Definition: bgp-message.h:42
static ssize_t _print(size_t indent, uint8_t **to, size_t *buf_left, const char *format,...)
Print helper.
ssize_t write(uint8_t *to, size_t buf_sz) const
Serialize a BGP message body.
ssize_t parse(const uint8_t *from, size_t msg_sz)
Deserialize a BGP message body.
const char * bgp_fsm_error_str[4]
Error strings for BGP FSM error subcodes.
Definition: bgp-errcode.cc:78
const char * bgp_cease_error_str[9]
Error strings for BGP cease error subcodes.
Definition: bgp-errcode.cc:89
void setError(uint8_t err, uint8_t suberr, const uint8_t *data, size_t data_len)
Set the error information.
Definition: serializable.cc:61
Buffer operation helpers.
const char * bgp_open_error_subcode_str[8]
Error strings for BGP open message error subcodes.
Definition: bgp-errcode.cc:44
Definition: bgp-afi.h:14
const char * bgp_update_error_str[12]
Error strings for BGP update message error subcodes.
Definition: bgp-errcode.cc:59
BGP error codes.
The BGP notification message.
const char * bgp_header_error_subcode_str[4]
Error strings for BGP header error subcodes.
Definition: bgp-errcode.cc:33