]>
git.proxmox.com Git - mirror_iproute2.git/blob - ip/rtmon.c
2 * rtmon.c RTnetlink listener.
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>
17 #include <sys/socket.h>
19 #include <netinet/in.h>
25 #include "libnetlink.h"
27 static int init_phase
= 1;
29 static void write_stamp(FILE *fp
)
32 struct nlmsghdr
*n1
= (void *)buf
;
35 n1
->nlmsg_type
= NLMSG_TSTAMP
;
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
);
46 static int dump_msg(struct rtnl_ctrl_data
*ctrl
,
47 struct nlmsghdr
*n
, void *arg
)
49 FILE *fp
= (FILE *)arg
;
53 fwrite((void *)n
, 1, NLMSG_ALIGN(n
->nlmsg_len
), fp
);
58 static int dump_msg2(struct nlmsghdr
*n
, void *arg
)
60 return dump_msg(NULL
, n
, arg
);
63 static void usage(void)
66 "Usage: rtmon [ OPTIONS ] file FILE [ all | LISTofOBJECTS ]\n"
67 "OPTIONS := { -f[amily] { inet | inet6 | link | help } |\n"
68 " -4 | -6 | -0 | -V[ersion] }\n"
69 "LISTofOBJECTS := [ link ] [ address ] [ route ]\n");
74 main(int argc
, char **argv
)
77 struct rtnl_handle rth
;
78 int family
= AF_UNSPEC
;
79 unsigned int groups
= ~0U;
86 if (matches(argv
[1], "-family") == 0) {
91 if (strcmp(argv
[1], "inet") == 0)
93 else if (strcmp(argv
[1], "inet6") == 0)
95 else if (strcmp(argv
[1], "link") == 0)
97 else if (strcmp(argv
[1], "help") == 0)
100 fprintf(stderr
, "Protocol ID \"%s\" is unknown, try \"rtmon help\".\n", argv
[1]);
103 } else if (strcmp(argv
[1], "-4") == 0) {
105 } else if (strcmp(argv
[1], "-6") == 0) {
107 } else if (strcmp(argv
[1], "-0") == 0) {
109 } else if (matches(argv
[1], "-Version") == 0) {
110 printf("rtmon utility, iproute2-ss%s\n", SNAPSHOT
);
112 } else if (matches(argv
[1], "file") == 0) {
118 } else if (matches(argv
[1], "link") == 0) {
121 } else if (matches(argv
[1], "address") == 0) {
124 } else if (matches(argv
[1], "route") == 0) {
127 } else if (strcmp(argv
[1], "all") == 0) {
129 } else if (matches(argv
[1], "help") == 0) {
132 fprintf(stderr
, "Argument \"%s\" is unknown, try \"rtmon help\".\n", argv
[1]);
139 fprintf(stderr
, "Not enough information: argument \"file\" is required\n");
143 groups
|= nl_mgrp(RTNLGRP_LINK
);
145 if (!family
|| family
== AF_INET
)
146 groups
|= nl_mgrp(RTNLGRP_IPV4_IFADDR
);
147 if (!family
|| family
== AF_INET6
)
148 groups
|= nl_mgrp(RTNLGRP_IPV6_IFADDR
);
151 if (!family
|| family
== AF_INET
)
152 groups
|= nl_mgrp(RTNLGRP_IPV4_ROUTE
);
153 if (!family
|| family
== AF_INET6
)
154 groups
|= nl_mgrp(RTNLGRP_IPV6_ROUTE
);
157 fp
= fopen(file
, "w");
159 perror("Cannot fopen");
163 if (rtnl_open(&rth
, groups
) < 0)
166 if (rtnl_linkdump_req(&rth
, AF_UNSPEC
) < 0) {
167 perror("Cannot send dump request");
173 if (rtnl_dump_filter(&rth
, dump_msg2
, fp
) < 0) {
174 fprintf(stderr
, "Dump terminated\n");
180 if (rtnl_listen(&rth
, dump_msg
, (void *)fp
) < 0)