Skip to content

Commit 7f29e8b

Browse files
JackyYinanakryiko
authored andcommitted
feat: Support -i option in sockfilter
Add `-i <interface>` option for user to specify which network interface they want to watch for, if not specified, `lo` is used. Signed-off-by: Jacky Yin <jjyyg1123@gmail.com>
1 parent 9850024 commit 7f29e8b

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ Currently, most of the IPv4 protocols defined in `uapi/linux/in.h` are included,
298298
please check `ipproto_mapping` of `examples/c/sockfilter.c` for the supported protocols.
299299

300300
```shell
301-
$ sudo ./sockfilter
301+
$ sudo ./sockfilter -i <interface>
302302
interface:lo protocol: UDP 127.0.0.1:51845(src) -> 127.0.0.1:53(dst)
303303
interface:lo protocol: UDP 127.0.0.1:41552(src) -> 127.0.0.1:53(dst)
304304
```

examples/c/sockfilter.c

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
22
/* Copyright (c) 2022 Jacky Yin */
3+
#include <argp.h>
34
#include <arpa/inet.h>
45
#include <assert.h>
56
#include <bpf/libbpf.h>
@@ -15,6 +16,46 @@
1516
#include "sockfilter.h"
1617
#include "sockfilter.skel.h"
1718

19+
static struct env {
20+
const char *interface;
21+
} env;
22+
23+
const char argp_program_doc[] =
24+
"BPF socket filter demo application.\n"
25+
"\n"
26+
"This program watch network packet of specified interface and print out src/dst\n"
27+
"information.\n"
28+
"\n"
29+
"Currently only IPv4 is supported.\n"
30+
"\n"
31+
"USAGE: ./sockfilter [-i <interface>]\n";
32+
33+
static const struct argp_option opts[] = {
34+
{ "interface", 'i', "INTERFACE", 0, "Network interface to attach" },
35+
{},
36+
};
37+
38+
static error_t parse_arg(int key, char *arg, struct argp_state *state)
39+
{
40+
switch (key) {
41+
case 'i':
42+
env.interface = arg;
43+
break;
44+
case ARGP_KEY_ARG:
45+
argp_usage(state);
46+
break;
47+
default:
48+
return ARGP_ERR_UNKNOWN;
49+
}
50+
return 0;
51+
}
52+
53+
static const struct argp argp = {
54+
.options = opts,
55+
.parser = parse_arg,
56+
.doc = argp_program_doc,
57+
};
58+
1859
static const char *ipproto_mapping[IPPROTO_MAX] = {
1960
[IPPROTO_IP] = "IP", [IPPROTO_ICMP] = "ICMP", [IPPROTO_IGMP] = "IGMP",
2061
[IPPROTO_IPIP] = "IPIP", [IPPROTO_TCP] = "TCP", [IPPROTO_EGP] = "EGP",
@@ -99,6 +140,12 @@ int main(int argc, char **argv)
99140
struct sockfilter_bpf *skel;
100141
int err, prog_fd, sock;
101142

143+
env.interface = "lo";
144+
/* Parse command line arguments */
145+
err = argp_parse(&argp, argc, argv, 0, NULL, NULL);
146+
if (err)
147+
return -err;
148+
102149
/* Set up libbpf errors and debug info callback */
103150
libbpf_set_print(libbpf_print_fn);
104151

@@ -122,7 +169,7 @@ int main(int argc, char **argv)
122169
}
123170

124171
/* Create raw socket for localhost interface */
125-
sock = open_raw_sock("lo");
172+
sock = open_raw_sock(env.interface);
126173
if (sock < 0) {
127174
err = -2;
128175
fprintf(stderr, "Failed to open raw socket\n");

0 commit comments

Comments
 (0)