2 * src/nl-monitor.c Monitor 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>
13 #include <netlink/route/link.h>
14 #include <netlink/route/addr.h>
16 static void obj_input(struct nl_object *obj, void *arg)
18 struct nl_dump_params dp = {
19 .dp_type = NL_DUMP_STATS,
24 nl_object_dump(obj, &dp);
27 static int event_input(struct nl_msg *msg, void *arg)
29 if (nl_msg_parse(msg, &obj_input, NULL) < 0)
30 fprintf(stderr, "<<EVENT>> Unknown message type\n");
32 /* Exit nl_recvmsgs_def() and return to the main select() */
36 int main(int argc, char *argv[])
38 struct nl_handle *nlh;
39 struct nl_cache *link_cache;
44 enum rtnetlink_groups gr_id;
47 { RTNLGRP_LINK, "link" },
48 { RTNLGRP_NOTIFY, "notify" },
49 { RTNLGRP_NEIGH, "neigh" },
51 { RTNLGRP_IPV4_IFADDR, "ipv4-ifaddr" },
52 { RTNLGRP_IPV4_MROUTE, "ipv4-mroute" },
53 { RTNLGRP_IPV4_ROUTE, "ipv4-route" },
54 { RTNLGRP_IPV6_IFADDR, "ipv6-ifaddr" },
55 { RTNLGRP_IPV6_MROUTE, "ipv6-mroute" },
56 { RTNLGRP_IPV6_ROUTE, "ipv6-route" },
57 { RTNLGRP_IPV6_IFINFO, "ipv6-ifinfo" },
58 { RTNLGRP_DECnet_IFADDR, "decnet-ifaddr" },
59 { RTNLGRP_DECnet_ROUTE, "decnet-route" },
60 { RTNLGRP_IPV6_PREFIX, "ipv6-prefix" },
61 { RTNLGRP_NONE, NULL }
64 if (nltool_init(argc, argv) < 0)
67 nlh = nltool_alloc_handle();
71 nl_disable_sequence_check(nlh);
73 nl_socket_modify_cb(nlh, NL_CB_VALID, NL_CB_CUSTOM, event_input, NULL);
75 if (argc > 1 && !strcasecmp(argv[1], "-h")) {
76 printf("Usage: nl-monitor [<groups>]\n");
78 printf("Known groups:");
79 for (i = 0; known_groups[i].gr_id != RTNLGRP_NONE; i++)
80 printf(" %s", known_groups[i].gr_name);
85 if (nl_connect(nlh, NETLINK_ROUTE) < 0) {
86 fprintf(stderr, "%s\n", nl_geterror());
90 for (idx = 1; argc > idx; idx++) {
91 for (i = 0; known_groups[i].gr_id != RTNLGRP_NONE; i++) {
92 if (!strcmp(argv[idx], known_groups[i].gr_name)) {
94 if (nl_socket_add_membership(nlh, known_groups[i].gr_id) < 0) {
95 fprintf(stderr, "%s: %s\n", argv[idx], nl_geterror());
102 if (known_groups[i].gr_id == RTNLGRP_NONE)
103 fprintf(stderr, "Warning: Unknown group: %s\n", argv[idx]);
106 if ((link_cache = rtnl_link_alloc_cache(nlh)) == NULL) {
107 fprintf(stderr, "%s\n", nl_geterror());
111 nl_cache_mngt_provide(link_cache);
117 fd = nl_socket_get_fd(nlh);
121 /* wait for an incoming message on the netlink socket */
122 retval = select(fd+1, &rfds, NULL, NULL, NULL);
125 /* FD_ISSET(fd, &rfds) will be true */
126 nl_recvmsgs_default(nlh);
130 nl_cache_free(link_cache);