]> git.proxmox.com Git - mirror_iproute2.git/blob - ip/rtmon.c
Merge branch 'master' into net-next
[mirror_iproute2.git] / ip / rtmon.c
1 /*
2 * rtmon.c RTnetlink listener.
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 <fcntl.h>
17 #include <sys/socket.h>
18 #include <sys/time.h>
19 #include <netinet/in.h>
20 #include <string.h>
21
22 #include "SNAPSHOT.h"
23
24 #include "utils.h"
25 #include "libnetlink.h"
26
27 static int init_phase = 1;
28
29 static void write_stamp(FILE *fp)
30 {
31 char buf[128];
32 struct nlmsghdr *n1 = (void *)buf;
33 struct timeval tv;
34
35 n1->nlmsg_type = NLMSG_TSTAMP;
36 n1->nlmsg_flags = 0;
37 n1->nlmsg_seq = 0;
38 n1->nlmsg_pid = 0;
39 n1->nlmsg_len = NLMSG_LENGTH(4*2);
40 gettimeofday(&tv, NULL);
41 ((__u32 *)NLMSG_DATA(n1))[0] = tv.tv_sec;
42 ((__u32 *)NLMSG_DATA(n1))[1] = tv.tv_usec;
43 fwrite((void *)n1, 1, NLMSG_ALIGN(n1->nlmsg_len), fp);
44 }
45
46 static int dump_msg(const struct sockaddr_nl *who, struct rtnl_ctrl_data *ctrl,
47 struct nlmsghdr *n, void *arg)
48 {
49 FILE *fp = (FILE *)arg;
50
51 if (!init_phase)
52 write_stamp(fp);
53 fwrite((void *)n, 1, NLMSG_ALIGN(n->nlmsg_len), fp);
54 fflush(fp);
55 return 0;
56 }
57
58 static int dump_msg2(const struct sockaddr_nl *who,
59 struct nlmsghdr *n, void *arg)
60 {
61 return dump_msg(who, NULL, n, arg);
62 }
63
64 static void usage(void)
65 {
66 fprintf(stderr, "Usage: rtmon file FILE [ all | LISTofOBJECTS]\n");
67 fprintf(stderr, "LISTofOBJECTS := [ link ] [ address ] [ route ]\n");
68 exit(-1);
69 }
70
71 int
72 main(int argc, char **argv)
73 {
74 FILE *fp;
75 struct rtnl_handle rth;
76 int family = AF_UNSPEC;
77 unsigned int groups = ~0U;
78 int llink = 0;
79 int laddr = 0;
80 int lroute = 0;
81 char *file = NULL;
82
83 while (argc > 1) {
84 if (matches(argv[1], "-family") == 0) {
85 argc--;
86 argv++;
87 if (argc <= 1)
88 usage();
89 if (strcmp(argv[1], "inet") == 0)
90 family = AF_INET;
91 else if (strcmp(argv[1], "inet6") == 0)
92 family = AF_INET6;
93 else if (strcmp(argv[1], "link") == 0)
94 family = AF_INET6;
95 else if (strcmp(argv[1], "help") == 0)
96 usage();
97 else {
98 fprintf(stderr, "Protocol ID \"%s\" is unknown, try \"rtmon help\".\n", argv[1]);
99 exit(-1);
100 }
101 } else if (strcmp(argv[1], "-4") == 0) {
102 family = AF_INET;
103 } else if (strcmp(argv[1], "-6") == 0) {
104 family = AF_INET6;
105 } else if (strcmp(argv[1], "-0") == 0) {
106 family = AF_PACKET;
107 } else if (matches(argv[1], "-Version") == 0) {
108 printf("rtmon utility, iproute2-ss%s\n", SNAPSHOT);
109 exit(0);
110 } else if (matches(argv[1], "file") == 0) {
111 argc--;
112 argv++;
113 if (argc <= 1)
114 usage();
115 file = argv[1];
116 } else if (matches(argv[1], "link") == 0) {
117 llink = 1;
118 groups = 0;
119 } else if (matches(argv[1], "address") == 0) {
120 laddr = 1;
121 groups = 0;
122 } else if (matches(argv[1], "route") == 0) {
123 lroute = 1;
124 groups = 0;
125 } else if (strcmp(argv[1], "all") == 0) {
126 groups = ~0U;
127 } else if (matches(argv[1], "help") == 0) {
128 usage();
129 } else {
130 fprintf(stderr, "Argument \"%s\" is unknown, try \"rtmon help\".\n", argv[1]);
131 exit(-1);
132 }
133 argc--; argv++;
134 }
135
136 if (file == NULL) {
137 fprintf(stderr, "Not enough information: argument \"file\" is required\n");
138 exit(-1);
139 }
140 if (llink)
141 groups |= nl_mgrp(RTNLGRP_LINK);
142 if (laddr) {
143 if (!family || family == AF_INET)
144 groups |= nl_mgrp(RTNLGRP_IPV4_IFADDR);
145 if (!family || family == AF_INET6)
146 groups |= nl_mgrp(RTNLGRP_IPV6_IFADDR);
147 }
148 if (lroute) {
149 if (!family || family == AF_INET)
150 groups |= nl_mgrp(RTNLGRP_IPV4_ROUTE);
151 if (!family || family == AF_INET6)
152 groups |= nl_mgrp(RTNLGRP_IPV6_ROUTE);
153 }
154
155 fp = fopen(file, "w");
156 if (fp == NULL) {
157 perror("Cannot fopen");
158 exit(-1);
159 }
160
161 if (rtnl_open(&rth, groups) < 0)
162 exit(1);
163
164 if (rtnl_wilddump_request(&rth, AF_UNSPEC, RTM_GETLINK) < 0) {
165 perror("Cannot send dump request");
166 exit(1);
167 }
168
169 write_stamp(fp);
170
171 if (rtnl_dump_filter(&rth, dump_msg2, fp) < 0) {
172 fprintf(stderr, "Dump terminated\n");
173 return 1;
174 }
175
176 init_phase = 0;
177
178 if (rtnl_listen(&rth, dump_msg, (void *)fp) < 0)
179 exit(2);
180
181 exit(0);
182 }