]>
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>
18 #include <sys/socket.h>
20 #include <netinet/in.h>
26 #include "libnetlink.h"
28 int resolve_hosts
= 0;
29 static int init_phase
= 1;
31 static void write_stamp(FILE *fp
)
34 struct nlmsghdr
*n1
= (void*)buf
;
41 n1
->nlmsg_len
= NLMSG_LENGTH(4*2);
42 gettimeofday(&tv
, NULL
);
43 ((__u32
*)NLMSG_DATA(n1
))[0] = tv
.tv_sec
;
44 ((__u32
*)NLMSG_DATA(n1
))[1] = tv
.tv_usec
;
45 fwrite((void*)n1
, 1, NLMSG_ALIGN(n1
->nlmsg_len
), fp
);
48 static int dump_msg(const struct sockaddr_nl
*who
, struct nlmsghdr
*n
,
51 FILE *fp
= (FILE*)arg
;
54 fwrite((void*)n
, 1, NLMSG_ALIGN(n
->nlmsg_len
), fp
);
61 fprintf(stderr
, "Usage: rtmon file FILE [ all | LISTofOBJECTS]\n");
62 fprintf(stderr
, "LISTofOBJECTS := [ link ] [ address ] [ route ]\n");
67 main(int argc
, char **argv
)
70 struct rtnl_handle rth
;
71 int family
= AF_UNSPEC
;
72 unsigned groups
= ~0U;
79 if (matches(argv
[1], "-family") == 0) {
84 if (strcmp(argv
[1], "inet") == 0)
86 else if (strcmp(argv
[1], "inet6") == 0)
88 else if (strcmp(argv
[1], "link") == 0)
90 else if (strcmp(argv
[1], "help") == 0)
93 fprintf(stderr
, "Protocol ID \"%s\" is unknown, try \"rtmon help\".\n", argv
[1]);
96 } else if (strcmp(argv
[1], "-4") == 0) {
98 } else if (strcmp(argv
[1], "-6") == 0) {
100 } else if (strcmp(argv
[1], "-0") == 0) {
102 } else if (matches(argv
[1], "-Version") == 0) {
103 printf("rtmon utility, iproute2-ss%s\n", SNAPSHOT
);
105 } else if (matches(argv
[1], "file") == 0) {
111 } else if (matches(argv
[1], "link") == 0) {
114 } else if (matches(argv
[1], "address") == 0) {
117 } else if (matches(argv
[1], "route") == 0) {
120 } else if (strcmp(argv
[1], "all") == 0) {
122 } else if (matches(argv
[1], "help") == 0) {
125 fprintf(stderr
, "Argument \"%s\" is unknown, try \"rtmon help\".\n", argv
[1]);
132 fprintf(stderr
, "Not enough information: argument \"file\" is required\n");
136 groups
|= nl_mgrp(RTNLGRP_LINK
);
138 if (!family
|| family
== AF_INET
)
139 groups
|= nl_mgrp(RTNLGRP_IPV4_IFADDR
);
140 if (!family
|| family
== AF_INET6
)
141 groups
|= nl_mgrp(RTNLGRP_IPV6_IFADDR
);
144 if (!family
|| family
== AF_INET
)
145 groups
|= nl_mgrp(RTNLGRP_IPV4_ROUTE
);
146 if (!family
|| family
== AF_INET6
)
147 groups
|= nl_mgrp(RTNLGRP_IPV6_ROUTE
);
150 fp
= fopen(file
, "w");
152 perror("Cannot fopen");
156 if (rtnl_open(&rth
, groups
) < 0)
159 if (rtnl_wilddump_request(&rth
, AF_UNSPEC
, RTM_GETLINK
) < 0) {
160 perror("Cannot send dump request");
166 if (rtnl_dump_filter(&rth
, dump_msg
, fp
, NULL
, NULL
) < 0) {
167 fprintf(stderr
, "Dump terminated\n");
173 if (rtnl_listen(&rth
, dump_msg
, (void*)fp
) < 0)