2 * src/nf-monitor.c Monitor netfilter events
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
9 * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
10 * Copyright (c) 2007 Philip Craig <philipc@snapgear.com>
11 * Copyright (c) 2007 Secure Computing Corporation
15 #include <netlink/netfilter/nfnl.h>
17 static void obj_input(struct nl_object *obj, void *arg)
19 struct nl_dump_params dp = {
20 .dp_type = NL_DUMP_STATS,
25 nl_object_dump(obj, &dp);
28 static int event_input(struct nl_msg *msg, void *arg)
30 if (nl_msg_parse(msg, &obj_input, NULL) < 0)
31 fprintf(stderr, "<<EVENT>> Unknown message type\n");
33 /* Exit nl_recvmsgs_def() and return to the main select() */
37 int main(int argc, char *argv[])
39 struct nl_handle *nlh;
44 enum nfnetlink_groups gr_id;
47 { NFNLGRP_CONNTRACK_NEW, "ct-new" },
48 { NFNLGRP_CONNTRACK_UPDATE, "ct-update" },
49 { NFNLGRP_CONNTRACK_DESTROY, "ct-destroy" },
50 { NFNLGRP_NONE, NULL }
53 if (nltool_init(argc, argv) < 0)
56 nlh = nltool_alloc_handle();
60 nl_disable_sequence_check(nlh);
62 nl_socket_modify_cb(nlh, NL_CB_VALID, NL_CB_CUSTOM, event_input, NULL);
64 if (argc > 1 && !strcasecmp(argv[1], "-h")) {
65 printf("Usage: nf-monitor [<groups>]\n");
67 printf("Known groups:");
68 for (i = 0; known_groups[i].gr_id != NFNLGRP_NONE; i++)
69 printf(" %s", known_groups[i].gr_name);
74 if (nfnl_connect(nlh) < 0) {
75 fprintf(stderr, "%s\n", nl_geterror());
79 for (idx = 1; argc > idx; idx++) {
80 for (i = 0; known_groups[i].gr_id != NFNLGRP_NONE; i++) {
81 if (!strcmp(argv[idx], known_groups[i].gr_name)) {
83 if (nl_socket_add_membership(nlh, known_groups[i].gr_id) < 0) {
84 fprintf(stderr, "%s: %s\n", argv[idx], nl_geterror());
91 if (known_groups[i].gr_id == NFNLGRP_NONE)
92 fprintf(stderr, "Warning: Unknown group: %s\n", argv[idx]);
99 fd = nl_socket_get_fd(nlh);
103 /* wait for an incoming message on the netlink socket */
104 retval = select(fd+1, &rfds, NULL, NULL, NULL);
107 /* FD_ISSET(fd, &rfds) will be true */
108 nl_recvmsgs_default(nlh);