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 15static void obj_input(struct nl_object *obj, void *arg) 16{ 17 struct nl_dump_params dp = { 18 .dp_type = NL_DUMP_STATS, 19 .dp_fd = stdout, 20 .dp_dump_msgtype = 1, 21 }; 22 23 nl_object_dump(obj, &dp); 24} 25 26static 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 35int main(int argc, char *argv[]) 36{ 37 struct nl_sock *sock; 38 struct nl_cache *link_cache; 39 int err = 1; 40 int i, idx; 41 42 static const struct { 43 enum rtnetlink_groups gr_id; 44 const char* gr_name; 45 } known_groups[] = { 46 { RTNLGRP_LINK, "link" }, 47 { RTNLGRP_NOTIFY, "notify" }, 48 { RTNLGRP_NEIGH, "neigh" }, 49 { RTNLGRP_TC, "tc" }, 50 { RTNLGRP_IPV4_IFADDR, "ipv4-ifaddr" }, 51 { RTNLGRP_IPV4_MROUTE, "ipv4-mroute" }, 52 { RTNLGRP_IPV4_ROUTE, "ipv4-route" }, 53 { RTNLGRP_IPV6_IFADDR, "ipv6-ifaddr" }, 54 { RTNLGRP_IPV6_MROUTE, "ipv6-mroute" }, 55 { RTNLGRP_IPV6_ROUTE, "ipv6-route" }, 56 { RTNLGRP_IPV6_IFINFO, "ipv6-ifinfo" }, 57 { RTNLGRP_DECnet_IFADDR, "decnet-ifaddr" }, 58 { RTNLGRP_DECnet_ROUTE, "decnet-route" }, 59 { RTNLGRP_IPV6_PREFIX, "ipv6-prefix" }, 60 { RTNLGRP_NONE, NULL } 61 }; 62 63 sock = nl_cli_alloc_socket(); 64 nl_socket_disable_seq_check(sock); 65 nl_socket_modify_cb(sock, NL_CB_VALID, NL_CB_CUSTOM, event_input, NULL); 66 67 if (argc > 1 && !strcasecmp(argv[1], "-h")) { 68 printf("Usage: nl-monitor [<groups>]\n"); 69 70 printf("Known groups:"); 71 for (i = 0; known_groups[i].gr_id != RTNLGRP_NONE; i++) 72 printf(" %s", known_groups[i].gr_name); 73 printf("\n"); 74 return 2; 75 } 76 77 nl_cli_connect(sock, NETLINK_ROUTE); 78 79 for (idx = 1; argc > idx; idx++) { 80 for (i = 0; known_groups[i].gr_id != RTNLGRP_NONE; i++) { 81 if (!strcmp(argv[idx], known_groups[i].gr_name)) { 82 83 if ((err = nl_socket_add_membership(sock, known_groups[i].gr_id)) < 0) { 84 nl_cli_fatal(err, "%s: %s\n", argv[idx], 85 nl_geterror(err)); 86 } 87 88 break; 89 } 90 } 91 if (known_groups[i].gr_id == RTNLGRP_NONE) 92 fprintf(stderr, "Warning: Unknown group: %s\n", argv[idx]); 93 } 94 95 link_cache = nl_cli_link_alloc_cache(sock); 96 97 while (1) { 98 fd_set rfds; 99 int fd, retval; 100 101 fd = nl_socket_get_fd(sock); 102 103 FD_ZERO(&rfds); 104 FD_SET(fd, &rfds); 105 /* wait for an incoming message on the netlink socket */ 106 retval = select(fd+1, &rfds, NULL, NULL, NULL); 107 108 if (retval) { 109 /* FD_ISSET(fd, &rfds) will be true */ 110 nl_recvmsgs_default(sock); 111 } 112 } 113 114 return 0; 115} 116