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,23 @@ 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+
97+ int src_res = is_from_configured_prefix (config , & src_ip , & prefix_tag );
98+ int dst_res = is_from_configured_prefix (config , & dst_ip , & prefix_tag_dst );
9199
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 ))) {
100+ if ((CHECK_SRC_IP && src_res ) || (CHECK_DST_IP && dst_res )) {
94101 debug_print ("tagging %d\n" , prefix_tag );
95102 // data_out should have the right size since TRAP_E_FORMAT_CHANGED _had_ to be returned before getting here
96103 ur_copy_fields (template_out , data_out , template_in , data_in );
97104 // Set PREFIX_TAG field
98- ur_set (template_out , data_out , F_PREFIX_TAG , prefix_tag );
99-
105+ if (CHECK_BOTH == 0 ) {
106+ ur_set (template_out , data_out , F_PREFIX_TAG , (prefix_tag != 0 )?prefix_tag :prefix_tag_dst );
107+ } else {
108+ ur_set (template_out , data_out , F_PREFIX_TAG , prefix_tag );
109+ ur_set (template_out , data_out , F_PREFIX_TAG_DST , prefix_tag_dst );
110+ }
100111 uint16_t data_out_size = ur_rec_size (template_out , data_out );
101112 debug_print ("data_out_size %d\n" , data_out_size );
102113 int send_error = trap_send (INTERFACE_OUT , data_out , data_out_size );
@@ -146,7 +157,9 @@ int main(int argc, char **argv)
146157 case 's' :
147158 CHECK_DST_IP = 0 ;
148159 break ;
149-
160+ case 'b' :
161+ CHECK_BOTH = 1 ;
162+ break ;
150163 }
151164 }
152165
0 commit comments