libnl  3.4.0
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-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 #include <linux/netlink.h>
18 #include <linux/netfilter/nfnetlink.h>
19 
20 static void obj_input(struct nl_object *obj, void *arg)
21 {
22  struct nl_dump_params dp = {
24  .dp_fd = stdout,
25  .dp_dump_msgtype = 1,
26  };
27 
28  nl_object_dump(obj, &dp);
29 }
30 
31 static int event_input(struct nl_msg *msg, void *arg)
32 {
33  if (nl_msg_parse(msg, &obj_input, NULL) < 0)
34  fprintf(stderr, "<<EVENT>> Unknown message type\n");
35 
36  /* Exit nl_recvmsgs_def() and return to the main select() */
37  return NL_STOP;
38 }
39 
40 int main(int argc, char *argv[])
41 {
42  struct nl_sock *sock;
43  int err;
44  int i, idx;
45 
46  static const struct {
47  enum nfnetlink_groups gr_id;
48  const char* gr_name;
49  } groups[] = {
50  { NFNLGRP_CONNTRACK_NEW, "ct-new" },
51  { NFNLGRP_CONNTRACK_UPDATE, "ct-update" },
52  { NFNLGRP_CONNTRACK_DESTROY, "ct-destroy" },
53  { NFNLGRP_NONE, NULL }
54  };
55 
56  sock = nl_cli_alloc_socket();
58  nl_socket_modify_cb(sock, NL_CB_VALID, NL_CB_CUSTOM, event_input, NULL);
59 
60  if (argc > 1 && !strcasecmp(argv[1], "-h")) {
61  printf("Usage: nf-monitor [<groups>]\n");
62 
63  printf("Known groups:");
64  for (i = 0; groups[i].gr_id != NFNLGRP_NONE; i++)
65  printf(" %s", groups[i].gr_name);
66  printf("\n");
67  return 2;
68  }
69 
70  nl_cli_connect(sock, NETLINK_NETFILTER);
71 
72  for (idx = 1; argc > idx; idx++) {
73  for (i = 0; groups[i].gr_id != NFNLGRP_NONE; i++) {
74  if (strcmp(argv[idx], groups[i].gr_name))
75  continue;
76 
77  err = nl_socket_add_membership(sock, groups[i].gr_id);
78  if (err < 0)
79  nl_cli_fatal(err,
80  "Unable to add membership: %s",
81  nl_geterror(err));
82  break;
83  }
84 
85  if (groups[i].gr_id == NFNLGRP_NONE)
86  nl_cli_fatal(NLE_OBJ_NOTFOUND, "Unknown group: \"%s\"",
87  argv[idx]);
88  }
89 
90  while (1) {
91  fd_set rfds;
92  int fd, retval;
93 
94  fd = nl_socket_get_fd(sock);
95 
96  FD_ZERO(&rfds);
97  FD_SET(fd, &rfds);
98  /* wait for an incoming message on the netlink socket */
99  retval = select(fd+1, &rfds, NULL, NULL, NULL);
100 
101  if (retval) {
102  /* FD_ISSET(fd, &rfds) will be true */
103  nl_recvmsgs_default(sock);
104  }
105  }
106 
107  return 0;
108 }
Customized handler specified by the user.
Definition: handlers.h:83
int nl_socket_get_fd(const struct nl_sock *sk)
Return the file descriptor of the backing socket.
Definition: socket.c:583
enum nl_dump_type dp_type
Specifies the type of dump that is requested.
Definition: types.h:38
Stop parsing altogether and discard remaining messages.
Definition: handlers.h:68
int nl_socket_modify_cb(struct nl_sock *sk, enum nl_cb_type type, enum nl_cb_kind kind, nl_recvmsg_msg_cb_t func, void *arg)
Modify the callback handler associated with the socket.
Definition: socket.c:770
void nl_object_dump(struct nl_object *obj, struct nl_dump_params *params)
Dump this object according to the specified parameters.
Definition: object.c:288
void nl_socket_disable_seq_check(struct nl_sock *sk)
Disable sequence number checking.
Definition: socket.c:282
int nl_recvmsgs_default(struct nl_sock *sk)
Receive a set of message from a netlink socket using handlers in nl_sock.
Definition: nl.c:1093
Message is valid.
Definition: handlers.h:95
void nl_cli_fatal(int err, const char *fmt,...)
Print error message and quit application.
Definition: utils.c:77
Dumping parameters.
Definition: types.h:33
Dump all attributes including statistics.
Definition: types.h:24