15 #include <arpa/inet.h> 28 is_message_owner =
true;
43 is_message_owner =
false;
46 BgpPacket::~BgpPacket() {
47 if (m_msg != NULL && is_message_owner)
delete m_msg;
53 if (is_message_owner) written = m_msg->
print(indent, *to, *buf_sz);
54 else written = msg->
print(indent, *to, *buf_sz);
55 if (written < 0)
return 0;
76 if (buf_sz < 19 && buf_sz > 4096) {
77 logger->
log(ERROR,
"BgpPacket::parse: got a packet with invalid size.\n");
81 if (!is_message_owner) {
82 logger->
log(FATAL,
"BgpPacket::parse: can't parse: read-only packet.\n");
86 if (is_message_owner && m_msg != NULL) {
87 logger->
log(FATAL,
"BgpPacket::parse: can't parse: message pointer not NULL.\n");
91 const uint8_t *buffer = from + 18;
92 uint8_t msg_type = getValue<uint8_t>(&buffer);
102 size_t msg_sz = buf_sz - 19;
103 ssize_t parsed_len = m_msg->
parse(buffer, msg_sz);
105 if (parsed_len < 0) {
110 if (msg_sz != (
size_t) parsed_len) {
111 logger->
log(FATAL,
"BgpPacket::parse: parsed message length invalid but no error reported.\n");
115 return parsed_len + 19;
129 if (is_message_owner && m_msg == NULL) {
130 logger->
log(FATAL,
"BgpPacket::write: can't write: message pointer NULL.\n");
134 if (!is_message_owner && msg == NULL) {
135 logger->
log(FATAL,
"BgpPacket::write: can't write: message pointer NULL.\n");
140 logger->
log(ERROR,
"BgpPacket::write: dst buffer too small.\n");
144 uint8_t *buffer = to;
145 memset(buffer,
'\xff', 16);
149 uint8_t *pkt_len_ptr = buffer;
153 putValue<uint8_t> (&buffer, is_message_owner ? m_msg->type : msg->type);
154 ssize_t msg_len = -1;
156 if (is_message_owner) msg_len = m_msg->
write(buffer, buf_sz - 19);
157 else msg_len = msg->
write(buffer, buf_sz - 19);
159 if (msg_len < 0)
return msg_len;
161 size_t pkt_len = msg_len + 19;
162 putValue<uint16_t>(&pkt_len_ptr, htons(pkt_len));
173 return is_message_owner ? m_msg : msg;
The BgpOpenMessage class.
BgpPacket(BgpLogHandler *logger, bool is_4b, const BgpMessage *msg)
Construct a new BgpPacket object for serializing BGP message.
ssize_t doPrint(size_t indent, uint8_t **to, size_t *buf_sz) const
Print implementation.
The BgpNotificationMessage object.
The serializable base class.
ssize_t print(uint8_t *to, size_t buf_sz) const
Print the Serializable object as human readable string.
const BgpMessage * getMessage() const
Get pointer to the contained message.
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...
ssize_t parse(const uint8_t *from, size_t buf_sz)
Deserialize a BGP message.
The BgpMessage base class.
Buffer operation helpers.
Top level deserialization/serialization entry point for BGP messages.
The BgpKeepaliveMessage class.
virtual ssize_t write(uint8_t *to, size_t buf_sz) const =0
Serialize a BGP message body.
virtual ssize_t parse(const uint8_t *from, size_t msg_sz)=0
Deserialize a BGP message body.
void forwardParseError(const Serializable &other)
Forward error information from other Serializable object.
ssize_t write(uint8_t *to, size_t buf_sz) const
Serialize a BGP message.
BGP protocol serializers/deserializers.
The BgpUpdateMessage class.