add packaging
[platform/upstream/libnl1.git] / src / nf-monitor.c
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-2006 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 "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_handle *nlh;
40         int err = 1;
41         int i, idx;
42
43         static const struct {
44                 enum nfnetlink_groups gr_id;
45                 const char* gr_name;
46         } known_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         if (nltool_init(argc, argv) < 0)
54                 return -1;
55
56         nlh = nltool_alloc_handle();
57         if (nlh == NULL)
58                 return -1;
59
60         nl_disable_sequence_check(nlh);
61
62         nl_socket_modify_cb(nlh, NL_CB_VALID, NL_CB_CUSTOM, event_input, NULL);
63
64         if (argc > 1 && !strcasecmp(argv[1], "-h")) {
65                 printf("Usage: nf-monitor [<groups>]\n");
66
67                 printf("Known groups:");
68                 for (i = 0; known_groups[i].gr_id != NFNLGRP_NONE; i++)
69                         printf(" %s", known_groups[i].gr_name);
70                 printf("\n");
71                 return 2;
72         }
73
74         if (nfnl_connect(nlh) < 0) {
75                 fprintf(stderr, "%s\n", nl_geterror());
76                 goto errout;
77         }
78
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)) {
82
83                                 if (nl_socket_add_membership(nlh, known_groups[i].gr_id) < 0) {
84                                         fprintf(stderr, "%s: %s\n", argv[idx], nl_geterror());
85                                         goto errout;
86                                 }
87
88                                 break;
89                         }
90                 }
91                 if (known_groups[i].gr_id == NFNLGRP_NONE)
92                         fprintf(stderr, "Warning: Unknown group: %s\n", argv[idx]);
93         }
94
95         while (1) {
96                 fd_set rfds;
97                 int fd, retval;
98
99                 fd = nl_socket_get_fd(nlh);
100
101                 FD_ZERO(&rfds);
102                 FD_SET(fd, &rfds);
103                 /* wait for an incoming message on the netlink socket */
104                 retval = select(fd+1, &rfds, NULL, NULL, NULL);
105
106                 if (retval) {
107                         /* FD_ISSET(fd, &rfds) will be true */
108                         nl_recvmsgs_default(nlh);
109                 }
110         }
111
112         nl_close(nlh);
113 errout:
114         return err;
115 }