-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathbgpstream_elem.c
More file actions
427 lines (356 loc) · 11.6 KB
/
Copy pathbgpstream_elem.c
File metadata and controls
427 lines (356 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/*
* Copyright (C) 2014 The Regents of the University of California.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "bgpstream_elem_int.h"
#include "bgpstream_log.h"
#include "bgpstream_record.h"
#include "bgpstream_utils.h"
#include "bgpstream_int.h" // for bgpstream_char_snprintf()
#include "config.h"
#ifdef WITH_RPKI
#include "bgpstream_utils_rpki.h"
#endif
#include "utils.h"
#include <assert.h>
#include <inttypes.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
/* ==================== PROTECTED FUNCTIONS ==================== */
/* ==================== PUBLIC FUNCTIONS ==================== */
bgpstream_elem_t *bgpstream_elem_create()
{
// allocate memory for new element
bgpstream_elem_t *elem = NULL;
if ((elem = (bgpstream_elem_t *)malloc_zero(sizeof(bgpstream_elem_t))) ==
NULL) {
goto err;
}
// all fields are initialized to zero
// need to create as path
if ((elem->as_path = bgpstream_as_path_create()) == NULL) {
goto err;
}
// and a community set
if ((elem->communities = bgpstream_community_set_create()) == NULL) {
goto err;
}
return elem;
err:
bgpstream_elem_destroy(elem);
return NULL;
}
void bgpstream_elem_destroy(bgpstream_elem_t *elem)
{
if (elem == NULL) {
return;
}
bgpstream_as_path_destroy(elem->as_path);
elem->as_path = NULL;
bgpstream_community_set_destroy(elem->communities);
elem->communities = NULL;
free(elem);
}
void bgpstream_elem_clear(bgpstream_elem_t *elem)
{
bgpstream_as_path_clear(elem->as_path);
bgpstream_community_set_clear(elem->communities);
/* reset the remaining type-dependent fields so that a re-used elem does not
* leak stale values (e.g. path attributes from a previous elem) */
elem->nexthop.version = BGPSTREAM_ADDR_VERSION_UNKNOWN;
elem->has_origin = 0;
elem->has_med = 0;
elem->has_local_pref = 0;
elem->atomic_aggregate = 0;
elem->aggregator.has_aggregator = 0;
}
bgpstream_elem_t *bgpstream_elem_copy(bgpstream_elem_t *dst,
const bgpstream_elem_t *src)
{
/* save all ptrs before memcpy */
bgpstream_as_path_t *dst_aspath = dst->as_path;
bgpstream_community_set_t *dst_comms = dst->communities;
/* do a memcpy and then manually copy the as path and communities */
memcpy(dst, src, sizeof(bgpstream_elem_t));
/* restore all ptrs */
dst->as_path = dst_aspath;
dst->communities = dst_comms;
if (bgpstream_as_path_copy(dst->as_path, src->as_path) != 0) {
return NULL;
}
if (bgpstream_community_set_copy(dst->communities, src->communities) != 0) {
return NULL;
}
return dst;
}
int bgpstream_elem_type_snprintf(char *buf, size_t len,
bgpstream_elem_type_t type)
{
char ch;
switch (type) {
case BGPSTREAM_ELEM_TYPE_RIB: ch = 'R'; break;
case BGPSTREAM_ELEM_TYPE_ANNOUNCEMENT: ch = 'A'; break;
case BGPSTREAM_ELEM_TYPE_WITHDRAWAL: ch = 'W'; break;
case BGPSTREAM_ELEM_TYPE_PEERSTATE: ch = 'S'; break;
case BGPSTREAM_ELEM_TYPE_END_OF_RIB: ch = 'E'; break;
default:
if (len > 0)
buf[0] = '\0';
return 0;
}
return bgpstream_char_snprintf(buf, len, ch);
}
int bgpstream_elem_peerstate_snprintf(char *buf, size_t len,
bgpstream_elem_peerstate_t state)
{
size_t written = 0;
switch (state) {
case BGPSTREAM_ELEM_PEERSTATE_IDLE:
strncpy(buf, "IDLE", len);
written = strlen("IDLE");
break;
case BGPSTREAM_ELEM_PEERSTATE_CONNECT:
strncpy(buf, "CONNECT", len);
written = strlen("CONNECT");
break;
case BGPSTREAM_ELEM_PEERSTATE_ACTIVE:
strncpy(buf, "ACTIVE", len);
written = strlen("ACTIVE");
break;
case BGPSTREAM_ELEM_PEERSTATE_OPENSENT:
strncpy(buf, "OPENSENT", len);
written = strlen("OPENSENT");
break;
case BGPSTREAM_ELEM_PEERSTATE_OPENCONFIRM:
strncpy(buf, "OPENCONFIRM", len);
written = strlen("OPENCONFIRM");
break;
case BGPSTREAM_ELEM_PEERSTATE_ESTABLISHED:
strncpy(buf, "ESTABLISHED", len);
written = strlen("ESTABLISHED");
break;
case BGPSTREAM_ELEM_PEERSTATE_CLEARING:
strncpy(buf, "CLEARING", len);
written = strlen("CLEARING");
break;
case BGPSTREAM_ELEM_PEERSTATE_DELETED:
strncpy(buf, "DELETED", len);
written = strlen("DELETED");
break;
default:
if (len > 0) {
buf[0] = '\0';
}
break;
}
/* we promise to always nul-terminate */
if (written > len) {
buf[len - 1] = '\0';
}
return written;
}
#define B_REMAIN (len > written ? len - written : 0) /* unsigned */
#define B_FULL (written >= len)
#define ADD_PIPE \
do { \
if (len > written + 1) { \
buf_p[0] = '|'; \
buf_p[1] = '\0'; \
} \
buf_p++; \
written++; \
} while (0)
#define SEEK_STR_END \
do { \
while (*buf_p != '\0') { \
written++; \
buf_p++; \
} \
} while (0)
char *bgpstream_elem_custom_snprintf(char *buf, size_t len,
bgpstream_elem_t const *elem,
int print_type)
{
size_t written = 0; /* < how many bytes we wanted to write */
size_t c = 0; /* < how many chars were written */
char *buf_p = buf;
bgpstream_as_path_seg_t *seg;
/* common fields */
/* [message_type|]peer_asn|peer_ip| */
if (print_type) {
/* MESSAGE TYPE */
c = bgpstream_elem_type_snprintf(buf_p, B_REMAIN, elem->type);
written += c;
buf_p += c;
ADD_PIPE;
}
/* PEER ASN */
c = snprintf(buf_p, B_REMAIN, "%" PRIu32, elem->peer_asn);
written += c;
buf_p += c;
ADD_PIPE;
/* PEER IP */
/* Note: this can fail in rare cases where the peer address is not
present in the elem (old quagga collectors sometimes didn't dump
this information for state change and open messages). This will
result in an empty peer IP field. But if it fails due to lack of
space, we should fail too. */
if (bgpstream_addr_ntop(buf_p, B_REMAIN, &elem->peer_ip) == NULL &&
errno == ENOSPC) {
return NULL;
}
SEEK_STR_END;
ADD_PIPE;
/* conditional fields */
switch (elem->type) {
case BGPSTREAM_ELEM_TYPE_RIB:
case BGPSTREAM_ELEM_TYPE_ANNOUNCEMENT:
/* PREFIX */
if (bgpstream_pfx_snprintf(buf_p, B_REMAIN, &(elem->prefix)) == NULL) {
if (errno != ENOSPC)
bgpstream_log(BGPSTREAM_LOG_ERR, "Malformed prefix (R/A)");
return NULL;
}
SEEK_STR_END;
ADD_PIPE;
/* NEXT HOP */
if (bgpstream_addr_ntop(buf_p, B_REMAIN, &elem->nexthop) == NULL &&
errno == ENOSPC) {
return NULL;
}
SEEK_STR_END;
ADD_PIPE;
/* AS PATH */
c = bgpstream_as_path_snprintf(buf_p, B_REMAIN, elem->as_path);
written += c;
buf_p += c;
ADD_PIPE;
/* ORIGIN AS */
if ((seg = bgpstream_as_path_get_origin_seg(elem->as_path)) != NULL) {
c = bgpstream_as_path_seg_snprintf(buf_p, B_REMAIN, seg);
written += c;
buf_p += c;
}
ADD_PIPE;
/* COMMUNITIES */
c = bgpstream_community_set_snprintf(buf_p, B_REMAIN, elem->communities);
written += c;
buf_p += c;
ADD_PIPE;
/* OLD STATE (empty) */
ADD_PIPE;
/* NEW STATE (empty) */
#ifdef WITH_RPKI
/* RPKI validation */
/* If the RPKI parameter was set, the corresponding input was valid and the
configure was completely set up -> the elem can be validated by RPKI */
if (elem->annotations.rpki_active) {
char result[B_REMAIN];
if (bgpstream_rpki_validate(elem, result, sizeof(result))) {
c = snprintf(buf_p, B_REMAIN, "%s", result);
written += c;
buf_p += c;
}
}
#endif
/* END OF LINE */
break;
case BGPSTREAM_ELEM_TYPE_WITHDRAWAL:
/* PREFIX */
if (bgpstream_pfx_snprintf(buf_p, B_REMAIN, &(elem->prefix)) == NULL) {
if (errno != ENOSPC)
bgpstream_log(BGPSTREAM_LOG_ERR, "Malformed prefix (W)");
return NULL;
}
SEEK_STR_END;
ADD_PIPE;
/* NEXT HOP (empty) */
ADD_PIPE;
/* AS PATH (empty) */
ADD_PIPE;
/* ORIGIN AS (empty) */
ADD_PIPE;
/* COMMUNITIES (empty) */
ADD_PIPE;
/* OLD STATE (empty) */
ADD_PIPE;
/* NEW STATE (empty) */
/* END OF LINE */
break;
case BGPSTREAM_ELEM_TYPE_PEERSTATE:
/* PREFIX (empty) */
ADD_PIPE;
/* NEXT HOP (empty) */
ADD_PIPE;
/* AS PATH (empty) */
ADD_PIPE;
/* ORIGIN AS (empty) */
ADD_PIPE;
/* COMMUNITIES (empty) */
ADD_PIPE;
/* OLD STATE */
c = bgpstream_elem_peerstate_snprintf(buf_p, B_REMAIN, elem->old_state);
written += c;
buf_p += c;
ADD_PIPE;
/* NEW STATE (empty) */
c = bgpstream_elem_peerstate_snprintf(buf_p, B_REMAIN, elem->new_state);
written += c;
buf_p += c;
/* END OF LINE */
break;
case BGPSTREAM_ELEM_TYPE_END_OF_RIB:
/* PREFIX (AFI indicator) */
if (bgpstream_pfx_snprintf(buf_p, B_REMAIN, &(elem->prefix)) == NULL) {
if (errno != ENOSPC)
bgpstream_log(BGPSTREAM_LOG_ERR, "Malformed prefix (E)");
return NULL;
}
SEEK_STR_END;
ADD_PIPE;
/* NEXT HOP (empty) */
ADD_PIPE;
/* AS PATH (empty) */
ADD_PIPE;
/* ORIGIN AS (empty) */
ADD_PIPE;
/* COMMUNITIES (empty) */
ADD_PIPE;
/* OLD STATE (empty) */
ADD_PIPE;
/* NEW STATE (empty) */
/* END OF LINE */
break;
default:
bgpstream_log(BGPSTREAM_LOG_ERR, "Error during elem processing");
return NULL;
}
return B_FULL ? NULL : buf;
}
char *bgpstream_elem_snprintf(char *buf, size_t len,
bgpstream_elem_t const *elem)
{
return bgpstream_elem_custom_snprintf(buf, len, elem, 1);
}