developer | 0f54b52 | 2023-03-08 10:04:11 +0800 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <string.h> |
| 4 | #include <errno.h> |
| 5 | #include <unistd.h> |
| 6 | #include <getopt.h> |
| 7 | |
| 8 | #include "netfilter_flowtable.h" |
| 9 | |
| 10 | void usage(void) |
| 11 | { |
| 12 | printf("#########flush flow table\n"); |
| 13 | printf("ftnl -F\n"); |
| 14 | printf("#########del flow from offload table\n"); |
| 15 | printf("ftnl -D [sip] [dip] [proto] [sport] [dport]\n"); |
| 16 | } |
| 17 | |
| 18 | int main(int argc, char *argv[]) |
| 19 | { |
| 20 | struct ftnl_handle *h; |
| 21 | struct flow_tuple tuple = {0}; |
| 22 | int msg = -1; |
| 23 | int c; |
| 24 | int ret = -1; |
| 25 | const char *optstring = "FD"; |
| 26 | struct option opts[] = { |
| 27 | {"sip", required_argument, NULL, 's'}, |
| 28 | {"dip", required_argument, NULL, 'd'}, |
| 29 | {"proto", required_argument, NULL, 'p'}, |
| 30 | {"sport", required_argument, NULL, 'm'}, |
| 31 | {"dport", required_argument, NULL, 'n'} |
| 32 | }; |
| 33 | |
| 34 | /* open netlink socket */ |
| 35 | h = ftnl_open(); |
| 36 | if (!h) |
| 37 | return ret; |
| 38 | |
| 39 | /* parse arg */ |
| 40 | while ((c = getopt_long(argc, argv, optstring, opts, NULL)) != -1) { |
| 41 | switch (c) { |
| 42 | case 'F': |
| 43 | msg = FT_MSG_FLUSH; |
| 44 | break; |
| 45 | case 'D': |
| 46 | msg = FT_MSG_DEL; |
| 47 | break; |
| 48 | case 's': |
| 49 | inet_aton(optarg, &tuple.sip4); |
| 50 | break; |
| 51 | case 'd': |
| 52 | inet_aton(optarg, &tuple.dip4); |
| 53 | break; |
| 54 | case 'p': |
| 55 | if (!strcmp(optarg, "tcp")) |
| 56 | tuple.proto = IPPROTO_TCP; |
| 57 | else if (!strcmp(optarg, "udp")) |
| 58 | tuple.proto = IPPROTO_UDP; |
| 59 | else { |
| 60 | printf("proto bad value...\n"); |
| 61 | printf("pls set proto to udp or tcp arg : %s\n", |
| 62 | optarg); |
| 63 | goto out; |
| 64 | } |
| 65 | break; |
| 66 | case 'm': |
| 67 | tuple.sport = htons(atoi(optarg)); |
| 68 | break; |
| 69 | case 'n': |
| 70 | tuple.dport = htons(atoi(optarg)); |
| 71 | break; |
| 72 | default: |
| 73 | usage(); |
| 74 | goto out; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | switch (msg) { |
| 79 | case FT_MSG_FLUSH: |
| 80 | ftnl_flush_table(h); |
| 81 | break; |
| 82 | case FT_MSG_DEL: |
| 83 | ftnl_del_flow(h, &tuple); |
| 84 | break; |
| 85 | default: |
| 86 | break; |
| 87 | } |
| 88 | |
| 89 | out: |
| 90 | ftnl_close(h); |
| 91 | return ret; |
| 92 | } |