3131UR_FIELDS (
3232 ipaddr SRC_IP ,
3333 ipaddr DST_IP ,
34- uint32 PREFIX_TAG
34+ uint32 PREFIX_TAG ,
35+ uint32 PREFIX_TAG_DST
3536)
3637
3738trap_module_info_t * module_info = NULL ;
@@ -42,19 +43,22 @@ trap_module_info_t *module_info = NULL;
4243#define MODULE_PARAMS (PARAM ) \
4344 PARAM('c', "config", "Configuration file.", required_argument, "string") \
4445 PARAM('d', "dst", "Use only DST_IP field for prefix matching (default is both SRC_IP and DST_IP).", no_argument, "none") \
45- PARAM('s', "src", "Use only SRC_IP field for prefix matching (default is both SRC_IP and DST_IP).", no_argument, "none")
46+ PARAM('s', "src", "Use only SRC_IP field for prefix matching (default is both SRC_IP and DST_IP).", no_argument, "none") \
47+ PARAM('b', "both", "Tag both SRC_IP and DST_IP", no_argument,"none")
4648
4749static int stop = 0 ;
4850
4951TRAP_DEFAULT_SIGNAL_HANDLER (stop = 1 )
5052
5153int CHECK_SRC_IP = 1 ;
5254int CHECK_DST_IP = 1 ;
55+ int CHECK_BOTH = 0 ;
5356
5457
5558int prefix_tags (ipps_context_t * config ) {
5659 int error = 0 ;
5760 uint32_t prefix_tag ;
61+ uint32_t prefix_tag_dst ;
5862 const void * data_in = NULL ;
5963 uint16_t data_in_size ;
6064 void * data_out = NULL ;
@@ -69,10 +73,9 @@ int prefix_tags(ipps_context_t *config) {
6973 while (stop == 0 ) {
7074 int recv_error = TRAP_RECEIVE (INTERFACE_IN , data_in , data_in_size , template_in );
7175 TRAP_DEFAULT_RECV_ERROR_HANDLING (recv_error , continue , error = -2 ; goto cleanup )
72-
7376 if (recv_error == TRAP_E_FORMAT_CHANGED ) {
7477 // Copy format to output interface and add PREFIX_TAG
75- error = update_output_format (template_in , data_in , & template_out , & data_out );
78+ error = update_output_format (template_in , data_in , & template_out , & data_out );
7679 if (error ) {
7780 goto cleanup ;
7881 }
@@ -88,15 +91,29 @@ int prefix_tags(ipps_context_t *config) {
8891
8992 ip_addr_t src_ip = ur_get (template_in , data_in , F_SRC_IP );
9093 ip_addr_t dst_ip = ur_get (template_in , data_in , F_DST_IP );
94+ prefix_tag = 0 ;
95+ prefix_tag_dst = 0 ;
96+ int src_res = 0 ;
97+ int dst_res = 0 ;
98+
99+ if (CHECK_SRC_IP || CHECK_BOTH ){
100+ src_res = is_from_configured_prefix (config , & src_ip , & prefix_tag );
101+ }
102+ if ((!src_res && CHECK_DST_IP ) || CHECK_BOTH ){
103+ dst_res = is_from_configured_prefix (config , & dst_ip , & prefix_tag_dst );
104+ }
91105
92- if ((CHECK_SRC_IP && is_from_configured_prefix (config , & src_ip , & prefix_tag )) // Misusing short-circuit evaluation
93- || (CHECK_DST_IP && is_from_configured_prefix (config , & dst_ip , & prefix_tag ))) {
106+ if (src_res || dst_res ) {
94107 debug_print ("tagging %d\n" , prefix_tag );
95108 // data_out should have the right size since TRAP_E_FORMAT_CHANGED _had_ to be returned before getting here
96109 ur_copy_fields (template_out , data_out , template_in , data_in );
97110 // Set PREFIX_TAG field
98- ur_set (template_out , data_out , F_PREFIX_TAG , prefix_tag );
99-
111+ if (CHECK_BOTH == 0 ) {
112+ ur_set (template_out , data_out , F_PREFIX_TAG , (prefix_tag != 0 )?prefix_tag :prefix_tag_dst );
113+ } else {
114+ ur_set (template_out , data_out , F_PREFIX_TAG , prefix_tag );
115+ ur_set (template_out , data_out , F_PREFIX_TAG_DST , prefix_tag_dst );
116+ }
100117 uint16_t data_out_size = ur_rec_size (template_out , data_out );
101118 debug_print ("data_out_size %d\n" , data_out_size );
102119 int send_error = trap_send (INTERFACE_OUT , data_out , data_out_size );
@@ -146,7 +163,9 @@ int main(int argc, char **argv)
146163 case 's' :
147164 CHECK_DST_IP = 0 ;
148165 break ;
149-
166+ case 'b' :
167+ CHECK_BOTH = 1 ;
168+ break ;
150169 }
151170 }
152171
0 commit comments