4040 */
4141
4242#include <ipfixcol2.h>
43- #include <stdlib.h>
44- #include <unistd.h>
45- #include <inttypes.h>
43+ #include <maxminddb.h>
4644
4745#include "config.h"
4846
@@ -64,10 +62,24 @@ IPX_API struct ipx_plugin_info ipx_plugin_info = {
6462
6563/** Instance */
6664struct instance_data {
67- /** Parsed configuration of the instance */
65+ /** Parsed configuration of the instance */
6866 struct asn_config * config ;
67+ /** Modifier instance */
68+ ipx_modifier_t * modifier ;
69+ /** Message builder instance */
70+ ipx_msg_builder_t * builder ;
71+ /** MaxMindDB database instance */
72+ MMDB_s database ;
6973};
7074
75+ /** Type of AS number */
76+ enum asn_type {ASN_SRC , ASN_DST };
77+
78+ /** Fields defining new possible elements in enriched messages */
79+ struct ipx_modifier_field asn_fields [] = {
80+ [ASN_SRC ] = {.id = 16 , .length = 4 , .en = 0 }, // bgpSourceASNumber
81+ [ASN_DST ] = {.id = 17 , .length = 4 , .en = 0 } // bgpDestinationASNumber
82+ };
7183
7284// -------------------------------------------------------------------------------------------------
7385
@@ -80,12 +92,51 @@ ipx_plugin_init(ipx_ctx_t *ctx, const char *params)
8092 return IPX_ERR_DENIED ;
8193 }
8294
95+ // Parse XML configuration
8396 if ((data -> config = config_parse (ctx , params )) == NULL ) {
8497 free (data );
8598 return IPX_ERR_DENIED ;
8699 }
87100
101+ // Setup ASN database
102+ if (MMDB_open (data -> config -> db_path , MMDB_MODE_MMAP , & (data -> database )) != MMDB_SUCCESS ) {
103+ config_destroy (data -> config );
104+ free (data );
105+ return IPX_ERR_DENIED ;
106+ }
107+
108+ // Create modifier
109+ enum ipx_verb_level verb = ipx_ctx_verb_get (ctx );
110+ const fds_iemgr_t * iemgr = ipx_ctx_iemgr_get (ctx );
111+ const char * ident = ipx_ctx_name_get (ctx );
112+ size_t fsize = sizeof (asn_fields ) / sizeof (* asn_fields );
113+
114+ data -> modifier = ipx_modifier_create (asn_fields , fsize , NULL , iemgr , & verb , ident );
115+ if (!data -> modifier ) {
116+ config_destroy (data -> config );
117+ MMDB_close (& (data -> database ));
118+ free (data );
119+ return IPX_ERR_DENIED ;
120+ }
121+
122+ // Create message builder
123+ data -> builder = ipx_msg_builder_create ();
124+ if (!data -> builder ) {
125+ config_destroy (data -> config );
126+ MMDB_close (& (data -> database ));
127+ ipx_modifier_destroy (data -> modifier );
128+ free (data );
129+ return IPX_ERR_DENIED ;
130+ }
131+
88132 ipx_ctx_private_set (ctx , data );
133+
134+ // Subscribe to session messages
135+ const ipx_msg_mask_t new_mask = IPX_MSG_SESSION | IPX_MSG_IPFIX ;
136+ if (ipx_ctx_subscribe (ctx , & new_mask , NULL ) != IPX_OK ) {
137+ return IPX_ERR_DENIED ;
138+ }
139+
89140 return IPX_OK ;
90141}
91142
@@ -95,6 +146,22 @@ ipx_plugin_destroy(ipx_ctx_t *ctx, void *cfg)
95146 (void ) ctx ; // Suppress warnings
96147 struct instance_data * data = (struct instance_data * ) cfg ;
97148
149+ /* Create garbage message when destroying modifier because orher plugins might be
150+ referencing templates in modifier */
151+
152+ ipx_msg_garbage_cb cb = (ipx_msg_garbage_cb ) & ipx_modifier_destroy ;
153+ ipx_msg_garbage_t * gb_msg = ipx_msg_garbage_create (data -> modifier , cb );
154+ if (gb_msg ) {
155+ ipx_ctx_msg_pass (ctx , ipx_msg_garbage2base (gb_msg ));
156+ } else {
157+ /* If creating garbage message fails, don't destroy modifier (memory leak) because
158+ of possible segfault */
159+ IPX_CTX_WARNING (ctx , "Could not destroy modifier (%s)" , ipx_ctx_name_get (ctx ));
160+ }
161+
162+ ipx_msg_builder_destroy (data -> builder );
163+ config_destroy (data -> config );
164+ MMDB_close (& (data -> database ));
98165 free (data );
99166}
100167
0 commit comments