2 * ipmonitor.c "ip monitor".
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
18 #include <sys/socket.h>
19 #include <netinet/in.h>
20 #include <arpa/inet.h>
25 #include "ip_common.h"
27 static void usage(void) __attribute__((noreturn));
29 static void usage(void)
31 fprintf(stderr, "Usage: ip monitor [ all | LISTofOBJECTS ]\n");
36 int accept_msg(const struct sockaddr_nl *who,
37 struct nlmsghdr *n, void *arg)
39 FILE *fp = (FILE*)arg;
41 if (n->nlmsg_type == RTM_NEWROUTE || n->nlmsg_type == RTM_DELROUTE) {
42 print_route(who, n, arg);
45 if (n->nlmsg_type == RTM_NEWLINK || n->nlmsg_type == RTM_DELLINK) {
46 ll_remember_index(who, n, NULL);
47 print_linkinfo(who, n, arg);
50 if (n->nlmsg_type == RTM_NEWADDR || n->nlmsg_type == RTM_DELADDR) {
51 print_addrinfo(who, n, arg);
54 if (n->nlmsg_type == RTM_NEWNEIGH || n->nlmsg_type == RTM_DELNEIGH) {
55 print_neigh(who, n, arg);
58 if (n->nlmsg_type == RTM_NEWPREFIX) {
59 print_prefix(who, n, arg);
62 if (n->nlmsg_type == 15) {
64 time_t secs = ((__u32*)NLMSG_DATA(n))[0];
65 long usecs = ((__u32*)NLMSG_DATA(n))[1];
66 tstr = asctime(localtime(&secs));
67 tstr[strlen(tstr)-1] = 0;
68 fprintf(fp, "Timestamp: %s %lu us\n", tstr, usecs);
71 if (n->nlmsg_type == RTM_NEWQDISC ||
72 n->nlmsg_type == RTM_DELQDISC ||
73 n->nlmsg_type == RTM_NEWTCLASS ||
74 n->nlmsg_type == RTM_DELTCLASS ||
75 n->nlmsg_type == RTM_NEWTFILTER ||
76 n->nlmsg_type == RTM_DELTFILTER)
78 if (n->nlmsg_type != NLMSG_ERROR && n->nlmsg_type != NLMSG_NOOP &&
79 n->nlmsg_type != NLMSG_DONE) {
80 fprintf(fp, "Unknown message: %08x %08x %08x\n",
81 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
86 int do_ipmonitor(int argc, char **argv)
88 struct rtnl_handle rth;
90 unsigned groups = ~RTMGRP_TC;
96 ipaddr_reset_filter(1);
97 iproute_reset_filter();
98 ipneigh_reset_filter();
101 if (matches(*argv, "file") == 0) {
104 } else if (matches(*argv, "link") == 0) {
107 } else if (matches(*argv, "address") == 0) {
110 } else if (matches(*argv, "route") == 0) {
113 } else if (matches(*argv, "prefix") == 0) {
116 } else if (strcmp(*argv, "all") == 0) {
118 } else if (matches(*argv, "help") == 0) {
121 fprintf(stderr, "Argument \"%s\" is unknown, try \"ip monitor help\".\n", *argv);
128 groups |= RTMGRP_LINK;
130 if (!preferred_family || preferred_family == AF_INET)
131 groups |= RTMGRP_IPV4_IFADDR;
132 if (!preferred_family || preferred_family == AF_INET6)
133 groups |= RTMGRP_IPV6_IFADDR;
136 if (!preferred_family || preferred_family == AF_INET)
137 groups |= RTMGRP_IPV4_ROUTE;
138 if (!preferred_family || preferred_family == AF_INET6)
139 groups |= RTMGRP_IPV6_ROUTE;
142 if (!preferred_family || preferred_family == AF_INET6)
143 groups |= RTMGRP_IPV6_PREFIX;
148 fp = fopen(file, "r");
150 perror("Cannot fopen");
153 return rtnl_from_file(fp, accept_msg, (void*)stdout);
156 if (rtnl_open(&rth, groups) < 0)
161 if (rtnl_listen(&rth, accept_msg, (void*)stdout) < 0)