Checkign in new iproute2
[iproute2.git] / ip / ipmonitor.c
1 /*
2  * ipmonitor.c          "ip monitor".
3  *
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.
8  *
9  * Authors:     Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10  *
11  */
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include <syslog.h>
17 #include <fcntl.h>
18 #include <sys/socket.h>
19 #include <netinet/in.h>
20 #include <arpa/inet.h>
21 #include <string.h>
22 #include <time.h>
23
24 #include "utils.h"
25 #include "ip_common.h"
26
27 static void usage(void) __attribute__((noreturn));
28
29 static void usage(void)
30 {
31         fprintf(stderr, "Usage: ip monitor [ all | LISTofOBJECTS ]\n");
32         exit(-1);
33 }
34
35
36 int accept_msg(const struct sockaddr_nl *who,
37                struct nlmsghdr *n, void *arg)
38 {
39         FILE *fp = (FILE*)arg;
40
41         if (timestamp)
42                 print_timestamp(fp);
43
44         if (n->nlmsg_type == RTM_NEWROUTE || n->nlmsg_type == RTM_DELROUTE) {
45                 print_route(who, n, arg);
46                 return 0;
47         }
48         if (n->nlmsg_type == RTM_NEWLINK || n->nlmsg_type == RTM_DELLINK) {
49                 ll_remember_index(who, n, NULL);
50                 print_linkinfo(who, n, arg);
51                 return 0;
52         }
53         if (n->nlmsg_type == RTM_NEWADDR || n->nlmsg_type == RTM_DELADDR) {
54                 print_addrinfo(who, n, arg);
55                 return 0;
56         }
57         if (n->nlmsg_type == RTM_NEWADDRLABEL || n->nlmsg_type == RTM_DELADDRLABEL) {
58                 print_addrlabel(who, n, arg);
59                 return 0;
60         }
61         if (n->nlmsg_type == RTM_NEWNEIGH || n->nlmsg_type == RTM_DELNEIGH) {
62                 print_neigh(who, n, arg);
63                 return 0;
64         }
65         if (n->nlmsg_type == RTM_NEWPREFIX) {
66                 print_prefix(who, n, arg);
67                 return 0;
68         }
69         if (n->nlmsg_type == RTM_NEWRULE || n->nlmsg_type == RTM_DELRULE) {
70                 print_rule(who, n, arg);
71                 return 0;
72         }
73         if (n->nlmsg_type == 15) {
74                 char *tstr;
75                 time_t secs = ((__u32*)NLMSG_DATA(n))[0];
76                 long usecs = ((__u32*)NLMSG_DATA(n))[1];
77                 tstr = asctime(localtime(&secs));
78                 tstr[strlen(tstr)-1] = 0;
79                 fprintf(fp, "Timestamp: %s %lu us\n", tstr, usecs);
80                 return 0;
81         }
82         if (n->nlmsg_type == RTM_NEWQDISC ||
83             n->nlmsg_type == RTM_DELQDISC ||
84             n->nlmsg_type == RTM_NEWTCLASS ||
85             n->nlmsg_type == RTM_DELTCLASS ||
86             n->nlmsg_type == RTM_NEWTFILTER ||
87             n->nlmsg_type == RTM_DELTFILTER)
88                 return 0;
89         if (n->nlmsg_type != NLMSG_ERROR && n->nlmsg_type != NLMSG_NOOP &&
90             n->nlmsg_type != NLMSG_DONE) {
91                 fprintf(fp, "Unknown message: %08x %08x %08x\n",
92                         n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
93         }
94         return 0;
95 }
96
97 int do_ipmonitor(int argc, char **argv)
98 {
99         char *file = NULL;
100         unsigned groups = ~RTMGRP_TC;
101         int llink=0;
102         int laddr=0;
103         int lroute=0;
104         int lprefix=0;
105
106         rtnl_close(&rth);
107         ipaddr_reset_filter(1);
108         iproute_reset_filter();
109         ipneigh_reset_filter();
110
111         while (argc > 0) {
112                 if (matches(*argv, "file") == 0) {
113                         NEXT_ARG();
114                         file = *argv;
115                 } else if (matches(*argv, "link") == 0) {
116                         llink=1;
117                         groups = 0;
118                 } else if (matches(*argv, "address") == 0) {
119                         laddr=1;
120                         groups = 0;
121                 } else if (matches(*argv, "route") == 0) {
122                         lroute=1;
123                         groups = 0;
124                 } else if (matches(*argv, "prefix") == 0) {
125                         lprefix=1;
126                         groups = 0;
127                 } else if (strcmp(*argv, "all") == 0) {
128                         groups = ~RTMGRP_TC;
129                 } else if (matches(*argv, "help") == 0) {
130                         usage();
131                 } else {
132                         fprintf(stderr, "Argument \"%s\" is unknown, try \"ip monitor help\".\n", *argv);
133                         exit(-1);
134                 }
135                 argc--; argv++;
136         }
137
138         if (llink)
139                 groups |= nl_mgrp(RTNLGRP_LINK);
140         if (laddr) {
141                 if (!preferred_family || preferred_family == AF_INET)
142                         groups |= nl_mgrp(RTNLGRP_IPV4_IFADDR);
143                 if (!preferred_family || preferred_family == AF_INET6)
144                         groups |= nl_mgrp(RTNLGRP_IPV6_IFADDR);
145         }
146         if (lroute) {
147                 if (!preferred_family || preferred_family == AF_INET)
148                         groups |= nl_mgrp(RTNLGRP_IPV4_ROUTE);
149                 if (!preferred_family || preferred_family == AF_INET6)
150                         groups |= nl_mgrp(RTNLGRP_IPV6_ROUTE);
151         }
152         if (lprefix) {
153                 if (!preferred_family || preferred_family == AF_INET6)
154                         groups |= nl_mgrp(RTNLGRP_IPV6_PREFIX);
155         }
156
157         if (file) {
158                 FILE *fp;
159                 fp = fopen(file, "r");
160                 if (fp == NULL) {
161                         perror("Cannot fopen");
162                         exit(-1);
163                 }
164                 return rtnl_from_file(fp, accept_msg, stdout);
165         }
166
167         if (rtnl_open(&rth, groups) < 0)
168                 exit(1);
169         ll_init_map(&rth);
170
171         if (rtnl_listen(&rth, accept_msg, stdout) < 0)
172                 exit(2);
173
174         return 0;
175 }