nexmon – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /* |
2 | * src/nf-monitor.c Monitor netfilter events |
||
3 | * |
||
4 | * This library is free software; you can redistribute it and/or |
||
5 | * modify it under the terms of the GNU Lesser General Public |
||
6 | * License as published by the Free Software Foundation version 2.1 |
||
7 | * of the License. |
||
8 | * |
||
9 | * Copyright (c) 2003-2008 Thomas Graf <tgraf@suug.ch> |
||
10 | * Copyright (c) 2007 Philip Craig <philipc@snapgear.com> |
||
11 | * Copyright (c) 2007 Secure Computing Corporation |
||
12 | */ |
||
13 | |||
14 | #include <netlink/cli/utils.h> |
||
15 | #include <netlink/netfilter/nfnl.h> |
||
16 | |||
17 | static void obj_input(struct nl_object *obj, void *arg) |
||
18 | { |
||
19 | struct nl_dump_params dp = { |
||
20 | .dp_type = NL_DUMP_STATS, |
||
21 | .dp_fd = stdout, |
||
22 | .dp_dump_msgtype = 1, |
||
23 | }; |
||
24 | |||
25 | nl_object_dump(obj, &dp); |
||
26 | } |
||
27 | |||
28 | static int event_input(struct nl_msg *msg, void *arg) |
||
29 | { |
||
30 | if (nl_msg_parse(msg, &obj_input, NULL) < 0) |
||
31 | fprintf(stderr, "<<EVENT>> Unknown message type\n"); |
||
32 | |||
33 | /* Exit nl_recvmsgs_def() and return to the main select() */ |
||
34 | return NL_STOP; |
||
35 | } |
||
36 | |||
37 | int main(int argc, char *argv[]) |
||
38 | { |
||
39 | struct nl_sock *sock; |
||
40 | int err; |
||
41 | int i, idx; |
||
42 | |||
43 | static const struct { |
||
44 | enum nfnetlink_groups gr_id; |
||
45 | const char* gr_name; |
||
46 | } groups[] = { |
||
47 | { NFNLGRP_CONNTRACK_NEW, "ct-new" }, |
||
48 | { NFNLGRP_CONNTRACK_UPDATE, "ct-update" }, |
||
49 | { NFNLGRP_CONNTRACK_DESTROY, "ct-destroy" }, |
||
50 | { NFNLGRP_NONE, NULL } |
||
51 | }; |
||
52 | |||
53 | sock = nl_cli_alloc_socket(); |
||
54 | nl_socket_disable_seq_check(sock); |
||
55 | nl_socket_modify_cb(sock, NL_CB_VALID, NL_CB_CUSTOM, event_input, NULL); |
||
56 | |||
57 | if (argc > 1 && !strcasecmp(argv[1], "-h")) { |
||
58 | printf("Usage: nf-monitor [<groups>]\n"); |
||
59 | |||
60 | printf("Known groups:"); |
||
61 | for (i = 0; groups[i].gr_id != NFNLGRP_NONE; i++) |
||
62 | printf(" %s", groups[i].gr_name); |
||
63 | printf("\n"); |
||
64 | return 2; |
||
65 | } |
||
66 | |||
67 | nl_cli_connect(sock, NETLINK_NETFILTER); |
||
68 | |||
69 | for (idx = 1; argc > idx; idx++) { |
||
70 | for (i = 0; groups[i].gr_id != NFNLGRP_NONE; i++) { |
||
71 | if (strcmp(argv[idx], groups[i].gr_name)) |
||
72 | continue; |
||
73 | |||
74 | err = nl_socket_add_membership(sock, groups[i].gr_id); |
||
75 | if (err < 0) |
||
76 | nl_cli_fatal(err, |
||
77 | "Unable to add membership: %s", |
||
78 | nl_geterror(err)); |
||
79 | break; |
||
80 | } |
||
81 | |||
82 | if (groups[i].gr_id == NFNLGRP_NONE) |
||
83 | nl_cli_fatal(NLE_OBJ_NOTFOUND, "Unknown group: \"%s\"", |
||
84 | argv[idx]); |
||
85 | } |
||
86 | |||
87 | while (1) { |
||
88 | fd_set rfds; |
||
89 | int fd, retval; |
||
90 | |||
91 | fd = nl_socket_get_fd(sock); |
||
92 | |||
93 | FD_ZERO(&rfds); |
||
94 | FD_SET(fd, &rfds); |
||
95 | /* wait for an incoming message on the netlink socket */ |
||
96 | retval = select(fd+1, &rfds, NULL, NULL, NULL); |
||
97 | |||
98 | if (retval) { |
||
99 | /* FD_ISSET(fd, &rfds) will be true */ |
||
100 | nl_recvmsgs_default(sock); |
||
101 | } |
||
102 | } |
||
103 | |||
104 | return 0; |
||
105 | } |