Skip to content

Commit d4f4969

Browse files
authored
Merge pull request #240 from CESNET/prefixtags_add_dst
prefix_tags: Added tagging capability for both SRC and DST IP addresses.
2 parents 58eb6c5 + 5cea045 commit d4f4969

File tree

3 files changed

+32
-13
lines changed

3 files changed

+32
-13
lines changed

prefix_tags/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ Interfaces
1717
- Input: One UniRec interface
1818
- Template *MUST* contain fields `SRC_IP` or `DST_IP` (see cli options).
1919
- Output: One UniRec interface
20-
- Output template is copied from input template with `PREFIX_TAG` field added.
21-
- The module sends only records with matched `PREFIX_TAG`.
20+
- Output template is copied from input template with `PREFIX_TAG` and `PREFIX_TAG_DST` fields added.
21+
- The module sends only records with matched prefix.
2222

2323

2424
Usage
@@ -40,7 +40,7 @@ Parameters of module [OPTIONS]:
4040
-c --config <string> Configuration file.
4141
-d --dst Use only DST_IP field for prefix matching (default is both SRC_IP and DST_IP).
4242
-s --src Use only SRC_IP field for prefix matching (default is both SRC_IP and DST_IP).
43-
43+
-b --both Both SRC_IP and DST_IP is tagged. When -b is not enabled, the `PREFIX_TAG_DST` is always `0` and only PREFIX_TAG field is used
4444
Common TRAP parameters [COMMON]:
4545
--------------------------------
4646
-h [trap,1] If no argument, print this message. If "trap" or 1 is given, print TRAP help.

prefix_tags/prefix_tags.c

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
UR_FIELDS (
3232
ipaddr SRC_IP,
3333
ipaddr DST_IP,
34-
uint32 PREFIX_TAG
34+
uint32 PREFIX_TAG,
35+
uint32 PREFIX_TAG_DST
3536
)
3637

3738
trap_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

4749
static int stop = 0;
4850

4951
TRAP_DEFAULT_SIGNAL_HANDLER(stop = 1)
5052

5153
int CHECK_SRC_IP = 1;
5254
int CHECK_DST_IP = 1;
55+
int CHECK_BOTH = 0;
5356

5457

5558
int 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

prefix_tags/prefix_tags_functions.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ int update_output_format(ur_template_t *template_in, const void *data_in, ur_tem
2626
}
2727

2828
// Add PREFIX_TAG field
29-
*template_out = ur_expand_template("uint32 PREFIX_TAG", *template_out);
29+
*template_out = ur_expand_template("uint32 PREFIX_TAG,uint32 PREFIX_TAG_DST", *template_out);
3030
if (*template_out == NULL) {
3131
return -1;
3232
}

0 commit comments

Comments
 (0)