3030
3131#include "bgpdump_lib.h"
3232#include "utils.h"
33+ #include "khash.h"
3334
3435#include "bgpstream.h"
3536#include "bgpstream_utils.h"
3637
3738#include "bgpstream_debug.h"
3839#include "bgpstream_record.h"
3940
41+ #include "bgpstream_constants.h"
4042#include "bgpstream_elem_int.h"
41- #include "utils/ bgpstream_utils_rtr.h"
43+ #include "bgpstream_utils_rtr.h"
4244
4345/* ==================== PROTECTED FUNCTIONS ==================== */
4446
@@ -83,12 +85,25 @@ void bgpstream_elem_destroy(bgpstream_elem_t *elem) {
8385 bgpstream_community_set_destroy (elem -> communities );
8486 elem -> communities = NULL ;
8587
88+ #ifdef WITH_RTR
89+ if (elem -> annotations .active ){
90+ kh_destroy (rpki_result , elem -> annotations .rpki_kh );
91+ elem -> annotations .rpki_kh = NULL ;
92+ }
93+ #endif
94+
8695 free (elem );
8796}
8897
8998void bgpstream_elem_clear (bgpstream_elem_t * elem ) {
9099 bgpstream_as_path_clear (elem -> aspath );
91100 bgpstream_community_set_clear (elem -> communities );
101+ #ifdef WITH_RTR
102+ if (elem -> annotations .active &&
103+ (elem -> annotations .rpki_validation_status != BGPSTREAM_ELEM_RPKI_VALIDATION_STATUS_NOTVALIDATED )){
104+ kh_clear (rpki_result , elem -> annotations .rpki_kh );
105+ }
106+ #endif
92107}
93108
94109bgpstream_elem_t * bgpstream_elem_copy (bgpstream_elem_t * dst ,
@@ -348,14 +363,16 @@ char *bgpstream_elem_custom_snprintf(char *buf, size_t len,
348363 if (B_FULL )
349364 return NULL ;
350365
351- #if defined( FOUND_RTR )
366+ #ifdef WITH_RTR
352367 /* RPKI Validation */
353- char buf_rpki [1024 ];
354- c = bgpstream_elem_get_rpki_validation_result_snprintf (
355- buf_rpki , sizeof (buf_rpki ), elem );
356- strcat (buf , buf_rpki );
357- written += c ;
358- buf_p += c ;
368+ if (elem -> annotations .active ) {
369+ char buf_rpki [BGPSTREAM_RPKI_RST_MAX_LEN ];
370+ c = bgpstream_elem_get_rpki_validation_result_snprintf (
371+ buf_rpki , sizeof (buf_rpki ), elem );
372+ strcat (buf , buf_rpki );
373+ written += c ;
374+ buf_p += c ;
375+ }
359376#endif
360377 /* END OF LINE */
361378 break ;
@@ -434,46 +451,26 @@ char *bgpstream_elem_snprintf(char *buf, size_t len,
434451 return bgpstream_elem_custom_snprintf (buf , len , elem , 1 );
435452}
436453
437- #if defined( FOUND_RTR )
454+ #ifdef WITH_RTR
438455int bgpstream_elem_get_rpki_validation_result_snprintf (
439456 char * buf , size_t len , bgpstream_elem_t const * elem )
440457{
441- char result_output [1024 ] = "" ;
458+ int key ;
459+ char * val ;
460+ char result_output [BGPSTREAM_RPKI_RST_MAX_LEN ];
461+ char valid_prefixes [BGPSTREAM_RPKI_RST_MAX_LEN ];
442462 if (elem -> annotations .rpki_validation_status !=
443463 BGPSTREAM_ELEM_RPKI_VALIDATION_STATUS_NOTFOUND ) {
444464 snprintf (result_output , sizeof (result_output ), "%s%s" , result_output ,
445465 elem -> annotations .rpki_validation_status ==
446466 BGPSTREAM_ELEM_RPKI_VALIDATION_STATUS_INVALID
447- ? "invalid;"
448- : "valid;" );
449- for (int i = 0 ; i < elem -> annotations .rpki_validation_result .asn_used ;
450- i ++ ) {
451- char asn [1024 ];
452- snprintf (asn , sizeof (asn ), "%" PRIu32 "," ,
453- elem -> annotations .rpki_validation_result .asn_pfx [i ].asn );
454- strcat (result_output , asn );
455- for (int j = 0 ;
456- j < elem -> annotations .rpki_validation_result .asn_pfx [i ].pfx_used ;
457- j ++ ) {
458- char valid_prefix [INET6_ADDRSTRLEN ];
459- bgpstream_pfx_snprintf (valid_prefix , INET6_ADDRSTRLEN ,
460- (bgpstream_pfx_t * )& elem -> annotations .rpki_validation_result
461- .asn_pfx [i ].pfxs [j ].pfx );
462- strcat (result_output , valid_prefix );
463- snprintf (asn , sizeof (asn ), "-%" PRIu8 ,
464- elem -> annotations .rpki_validation_result .asn_pfx [i ]
465- .pfxs [j ].max_pfx_len );
466- strcat (result_output , asn );
467- strcat (result_output ,
468- j != elem -> annotations .rpki_validation_result .asn_pfx [i ]
469- .pfx_used - 1
470- ? " "
471- : "" );
472- }
473- strcat (result_output ,
474- i != elem -> annotations .rpki_validation_result .asn_used - 1 ? ";"
475- : "" );
476- }
467+ ? "invalid;" : "valid;" );
468+
469+ kh_foreach (elem -> annotations .rpki_kh , key , val ,
470+ snprintf (valid_prefixes , sizeof (valid_prefixes ), "%i,%s;" , key , val );
471+ strcat (result_output , valid_prefixes );
472+ );
473+ result_output [strlen (result_output ) - 1 ] = 0 ;
477474 } else {
478475 snprintf (result_output , sizeof (result_output ), "%s%s" , result_output ,
479476 "notfound" );
@@ -482,17 +479,16 @@ int bgpstream_elem_get_rpki_validation_result_snprintf(
482479 return snprintf (buf , len , "%s" , result_output );
483480}
484481
485- void bgpstream_elem_get_rpki_validation_result (bgpstream_elem_t * elem ,
482+ void bgpstream_elem_get_rpki_validation_result (struct rtr_mgr_config * cfg , bgpstream_elem_t * elem ,
486483 char * prefix ,
487484 uint32_t origin_asn ,
488485 uint8_t mask_len )
489486{
490487 if (elem -> annotations .rpki_validation_status ==
491488 BGPSTREAM_ELEM_RPKI_VALIDATION_STATUS_NOTVALIDATED ) {
492- cfg_tr = bgpstream_get_rtr_config ();
493489
494490 struct reasoned_result res_reasoned =
495- bgpstream_rtr_validate_reason (cfg_tr , origin_asn , prefix , mask_len );
491+ bgpstream_rtr_validate_reason (cfg , origin_asn , prefix , mask_len );
496492
497493 if (res_reasoned .result == BGP_PFXV_STATE_VALID ) {
498494 elem -> annotations .rpki_validation_status =
@@ -509,27 +505,43 @@ void bgpstream_elem_get_rpki_validation_result(bgpstream_elem_t *elem,
509505
510506 if (elem -> annotations .rpki_validation_status !=
511507 BGPSTREAM_ELEM_RPKI_VALIDATION_STATUS_NOTFOUND ) {
512- bgpstream_rpki_validation_result_init (
513- & elem -> annotations .rpki_validation_result , 2 );
514- char valid_prefix [INET6_ADDRSTRLEN ];
508+
515509 char reason_prefix [INET6_ADDRSTRLEN ];
510+ char buf_p [BGPSTREAM_RPKI_RST_MAX_LEN ];
511+
512+ int ret ;
513+ khiter_t k ;
516514
515+ if (elem -> annotations .khash_init != 1 ) {
516+ elem -> annotations .rpki_kh = kh_init (rpki_result );
517+ elem -> annotations .khash_init = 1 ;
518+ }
519+
517520 for (int i = 0 ; i < res_reasoned .reason_len ; i ++ ) {
518- bgpstream_rpki_validation_result_insert_asn (
519- & elem -> annotations .rpki_validation_result ,
520- res_reasoned .reason [i ].asn );
521- lrtr_ip_addr_to_str (& (res_reasoned .reason [i ].prefix ), reason_prefix ,
522- sizeof (reason_prefix ));
523- snprintf (valid_prefix , sizeof (valid_prefix ), "%s/%" PRIu8 , reason_prefix ,
524- res_reasoned .reason [i ].min_len );
525-
526- bgpstream_pfx_t pfx ;
527- bgpstream_str2pfx (valid_prefix , (bgpstream_pfx_storage_t * )& pfx );
528- bgpstream_rpki_validation_result_insert_pfx (
529- & elem -> annotations .rpki_validation_result ,
530- res_reasoned .reason [i ].asn , & pfx , res_reasoned .reason [i ].max_len );
521+ if (kh_get (rpki_result , elem -> annotations .rpki_kh , res_reasoned .reason [i ].asn ) ==
522+ kh_end (elem -> annotations .rpki_kh )){
523+ k = kh_put (rpki_result , elem -> annotations .rpki_kh , (int ) res_reasoned .reason [i ].asn , & ret );
524+ kh_val (elem -> annotations .rpki_kh , k ) = '\0' ;
525+ }
526+ else {
527+ k = kh_get (rpki_result , elem -> annotations .rpki_kh , (int ) res_reasoned .reason [i ].asn );
528+ }
529+
530+ lrtr_ip_addr_to_str (& (res_reasoned .reason [i ].prefix ), reason_prefix , sizeof (reason_prefix ));
531+ snprintf (elem -> annotations .valid_prefix [k ], BGPSTREAM_RPKI_RST_MAX_LEN , "%s/%" PRIu8 "-%" PRIu8 ,
532+ reason_prefix , res_reasoned .reason [i ].min_len , res_reasoned .reason [i ].max_len );
533+
534+ if (!kh_val (elem -> annotations .rpki_kh , k )){
535+ kh_val (elem -> annotations .rpki_kh , k ) = elem -> annotations .valid_prefix [k ];
536+ }
537+ else if (!strstr (kh_val (elem -> annotations .rpki_kh , k ), elem -> annotations .valid_prefix [k ])) {
538+ snprintf (buf_p , sizeof (buf_p ), "%s %s" , kh_val (elem -> annotations .rpki_kh , k ),
539+ elem -> annotations .valid_prefix [k ]);
540+ kh_val (elem -> annotations .rpki_kh , k ) = buf_p ;
541+ }
531542 }
532543 }
544+
533545 free (res_reasoned .reason );
534546 }
535547}
0 commit comments