libnl  3.2.27
nl-monitor.c
1 /*
2  * src/nl-monitor.c Monitor 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-2009 Thomas Graf <tgraf@suug.ch>
10  */
11 
12 #include <netlink/cli/utils.h>
13 #include <netlink/cli/link.h>
14 
15 static void obj_input(struct nl_object *obj, void *arg)
16 {
17  struct nl_dump_params dp = {
19  .dp_fd = stdout,
20  .dp_dump_msgtype = 1,
21  };
22 
23  nl_object_dump(obj, &dp);
24 }
25 
26 static int event_input(struct nl_msg *msg, void *arg)
27 {
28  if (nl_msg_parse(msg, &obj_input, NULL) < 0)
29  fprintf(stderr, "<<EVENT>> Unknown message type\n");
30 
31  /* Exit nl_recvmsgs_def() and return to the main select() */
32  return NL_STOP;
33 }
34 
35 int main(int argc, char *argv[])
36 {
37  struct nl_sock *sock;
38  int err = 1;
39  int i, idx;
40 
41  static const struct {
42  enum rtnetlink_groups gr_id;
43  const char* gr_name;
44  } known_groups[] = {
45  { RTNLGRP_LINK, "link" },
46  { RTNLGRP_NOTIFY, "notify" },
47  { RTNLGRP_NEIGH, "neigh" },
48  { RTNLGRP_TC, "tc" },
49  { RTNLGRP_IPV4_IFADDR, "ipv4-ifaddr" },
50  { RTNLGRP_IPV4_MROUTE, "ipv4-mroute" },
51  { RTNLGRP_IPV4_ROUTE, "ipv4-route" },
52  { RTNLGRP_IPV6_IFADDR, "ipv6-ifaddr" },
53  { RTNLGRP_IPV6_MROUTE, "ipv6-mroute" },
54  { RTNLGRP_IPV6_ROUTE, "ipv6-route" },
55  { RTNLGRP_IPV6_IFINFO, "ipv6-ifinfo" },
56  { RTNLGRP_DECnet_IFADDR, "decnet-ifaddr" },
57  { RTNLGRP_DECnet_ROUTE, "decnet-route" },
58  { RTNLGRP_IPV6_PREFIX, "ipv6-prefix" },
59  { RTNLGRP_NONE, NULL }
60  };
61 
62  sock = nl_cli_alloc_socket();
64  nl_socket_modify_cb(sock, NL_CB_VALID, NL_CB_CUSTOM, event_input, NULL);
65 
66  if (argc > 1 && !strcasecmp(argv[1], "-h")) {
67  printf("Usage: nl-monitor [<groups>]\n");
68 
69  printf("Known groups:");
70  for (i = 0; known_groups[i].gr_id != RTNLGRP_NONE; i++)
71  printf(" %s", known_groups[i].gr_name);
72  printf("\n");
73  return 2;
74  }
75 
76  nl_cli_connect(sock, NETLINK_ROUTE);
77 
78  for (idx = 1; argc > idx; idx++) {
79  for (i = 0; known_groups[i].gr_id != RTNLGRP_NONE; i++) {
80  if (!strcmp(argv[idx], known_groups[i].gr_name)) {
81 
82  if ((err = nl_socket_add_membership(sock, known_groups[i].gr_id)) < 0) {
83  nl_cli_fatal(err, "%s: %s\n", argv[idx],
84  nl_geterror(err));
85  }
86 
87  break;
88  }
89  }
90  if (known_groups[i].gr_id == RTNLGRP_NONE)
91  fprintf(stderr, "Warning: Unknown group: %s\n", argv[idx]);
92  }
93 
94  nl_cli_link_alloc_cache(sock);
95 
96  while (1) {
97  fd_set rfds;
98  int fd, retval;
99 
100  fd = nl_socket_get_fd(sock);
101 
102  FD_ZERO(&rfds);
103  FD_SET(fd, &rfds);
104  /* wait for an incoming message on the netlink socket */
105  retval = select(fd+1, &rfds, NULL, NULL, NULL);
106 
107  if (retval) {
108  /* FD_ISSET(fd, &rfds) will be true */
109  nl_recvmsgs_default(sock);
110  }
111  }
112 
113  return 0;
114 }
Customized handler specified by the user.
Definition: handlers.h:80
int nl_socket_get_fd(const struct nl_sock *sk)
Return the file descriptor of the backing socket.
Definition: socket.c:586
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:65
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:771
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:281
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:1098
Message is valid.
Definition: handlers.h:92
void nl_cli_fatal(int err, const char *fmt,...)
Print error message and quit application.
Definition: utils.c:70
Dumping parameters.
Definition: types.h:33
Dump all attributes including statistics.
Definition: types.h:24