MCPcopy Create free account
hub / github.com/SNAS/openbmp / parseBMPv2

Method parseBMPv2

Server/src/bmp/parseBMP.cpp:123–277  ·  view source on GitHub ↗

* Parse v1 and v2 BMP header * * \details * v2 uses the same common header, but adds the Peer Up message type. * * \param [in] sock Socket to read the message from */

Source from the content-addressed store, hash-verified

121* \param [in] sock Socket to read the message from
122*/
123void parseBMP::parseBMPv2(int sock) {
124 struct common_hdr_old c_hdr = { 0 };
125 ssize_t i = 0;
126 char buf[256] = {0};
127
128 SELF_DEBUG("parseBMP: sock=%d: Reading %d bytes", sock, BMP_HDRv1v2_LEN);
129
130 bmp_len = 0;
131
132 if ((i = Recv(sock, &c_hdr, BMP_HDRv1v2_LEN, MSG_WAITALL))
133 != BMP_HDRv1v2_LEN) {
134 SELF_DEBUG("sock=%d: Couldn't read all bytes, read %zd bytes",
135 sock, i);
136 throw "ERROR: Cannot read v1/v2 BMP common header.";
137 }
138 // Process the message based on type
139 bmp_type = c_hdr.type;
140 switch (c_hdr.type) {
141 case 0: // Route monitoring
142 SELF_DEBUG("sock=%d : BMP MSG : route monitor", sock);
143
144 // Get the length of the remaining message by reading the BGP length
145 if ((i=Recv(sock, buf, 18, MSG_PEEK | MSG_WAITALL)) == 18) {
146 uint16_t len;
147 memcpy(&len, (buf+16), 2);
148 bgp::SWAP_BYTES(&len);
149 bmp_len = len;
150
151 } else {
152 LOG_ERR("sock=%d: Failed to read BGP message to get length of BMP message", sock);
153 throw "Failed to read BGP message for BMP length";
154 }
155 break;
156
157 case 1: // Statistics Report
158 SELF_DEBUG("sock=%d : BMP MSG : stats report", sock);
159 LOG_INFO("sock=%d : BMP MSG : stats report", sock);
160 break;
161
162 case 2: // Peer down notification
163 LOG_INFO("sock=%d: BMP MSG: Peer down", sock);
164
165 // Get the length of the remaining message by reading the BGP length
166 if ((i=Recv(sock, buf, 1, MSG_PEEK)) != 1) {
167
168 // Is there a BGP message
169 if (buf[0] == 1 or buf[0] == 3) {
170 if ((i = Recv(sock, buf, 18, MSG_PEEK | MSG_WAITALL)) == 18) {
171 memcpy(&bmp_len, buf + 16, 2);
172 bgp::SWAP_BYTES(&bmp_len);
173
174 } else {
175 LOG_ERR("sock=%d: Failed to read peer down BGP message to get length of BMP message", sock);
176 throw "Failed to read BGP message for BMP length";
177 }
178 }
179 } else {
180 LOG_ERR("sock=%d: Failed to read peer down reason", sock);

Callers

nothing calls this directly

Calls 1

SWAP_BYTESFunction · 0.85

Tested by

no test coverage detected