-
Notifications
You must be signed in to change notification settings - Fork 596
/
Copy pathiptlite_main.c
94 lines (77 loc) · 2.36 KB
/
iptlite_main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/****************************************************************************
* apps/netutils/iptlite/iptlite_main.c
* iptlite networking application
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include "../../../nuttx/net/devif/devif.h"
#include <nuttx/config.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
/****************************************************************************
* Private Functions
****************************************************************************/
void listall_rules(void)
{
int rules_counter = nflite_get_rules_counter();
char** table = nflite_listall();
printf("%3s %10s %16s %16s %9s %9s\n", \
"ID", "RULE", "SRC IPADDR", "DEST IPADDR", "SRC PORT", "DEST PORT");
for (int i = 0; i < rules_counter; i++)
{
for (int j = 0; j < RULE_INFO_MAX_SIZE; j++)
{
printf("%c", table[i][j]);
}
printf("\n");
}
}
void add_rule(int rule, char * srcip, char * destip, char * srcprt, \
char * destprt)
{
in_addr_t srcipaddr, destipaddr;
in_port_t srcport, destport;
bool rule_added;
inet_pton(AF_INET, srcip, &srcipaddr);
inet_pton(AF_INET, destip, &destipaddr);
srcport = htons(strtoul(srcprt, NULL, 10));
destport = htons(strtoul(destprt, NULL, 10));
rule_added = nflite_addrule(
rule, srcipaddr, destipaddr, srcport, destport);
printf("rule_added? %s\n", rule_added ? "true" : "false");
}
/****************************************************************************
* iptlite_main
****************************************************************************/
int main(int argc, FAR char *argv[])
{
int rule;
if (argc < 2)
{
printf("Not enough arguments!\n");
return -1;
}
if (strcmp(argv[1], "DROP") == 0 && argc == 6)
{
rule = 0;
add_rule(rule, argv[2], argv[3], argv[4], argv[5]);
}
else if (strcmp(argv[1], "FLUSHALL") == 0 && argc == 2)
{
rule = 1;
nflite_flushall();
}
else if (strcmp(argv[1], "LISTALL") == 0 && argc == 2)
{
rule = 2;
listall_rules();
}
else
{
printf("Invalid command! Verify command pattern.\n");
return -1;
}
return 0;
}