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-2008 Thomas Graf <tgraf@suug.ch>
10 * Copyright (c) 2007 Philip Craig <philipc@snapgear.com>
11 * Copyright (c) 2007 Secure Computing Corporation
14 #include <netlink/cli/utils.h>
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[])
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 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);
57 if (argc > 1 && !strcasecmp(argv[1], "-h")) {
58 printf("Usage: nf-monitor [<groups>]\n");
60 printf("Known groups:");
61 for (i = 0; groups[i].gr_id != NFNLGRP_NONE; i++)
62 printf(" %s", groups[i].gr_name);
67 nl_cli_connect(sock, NETLINK_NETFILTER);
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))
74 err = nl_socket_add_membership(sock, groups[i].gr_id);
77 "Unable to add membership: %s",
82 if (groups[i].gr_id == NFNLGRP_NONE)
83 nl_cli_fatal(NLE_OBJ_NOTFOUND, "Unknown group: \"%s\"",
91 fd = nl_socket_get_fd(sock);
95 /* wait for an incoming message on the netlink socket */
96 retval = select(fd+1, &rfds, NULL, NULL, NULL);
99 /* FD_ISSET(fd, &rfds) will be true */
100 nl_recvmsgs_default(sock);