]>
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>
21 #include <netinet/in.h>
27 #include "libnetlink.h"
29 int resolve_hosts
= 0;
30 static int init_phase
= 1;
32 static void write_stamp(FILE *fp
)
35 struct nlmsghdr
*n1
= (void*)buf
;
42 n1
->nlmsg_len
= NLMSG_LENGTH(4*2);
43 gettimeofday(&tv
, NULL
);
44 ((__u32
*)NLMSG_DATA(n1
))[0] = tv
.tv_sec
;
45 ((__u32
*)NLMSG_DATA(n1
))[1] = tv
.tv_usec
;
46 fwrite((void*)n1
, 1, NLMSG_ALIGN(n1
->nlmsg_len
), fp
);
49 static int dump_msg(const struct sockaddr_nl
*who
, const struct nlmsghdr
*n
,
52 FILE *fp
= (FILE*)arg
;
55 fwrite((void*)n
, 1, NLMSG_ALIGN(n
->nlmsg_len
), fp
);
62 fprintf(stderr
, "Usage: rtmon file FILE [ all | LISTofOBJECTS]\n");
63 fprintf(stderr
, "LISTofOBJECTS := [ link ] [ address ] [ route ]\n");
68 main(int argc
, char **argv
)
71 struct rtnl_handle rth
;
72 int family
= AF_UNSPEC
;
73 unsigned groups
= ~0U;
80 if (matches(argv
[1], "-family") == 0) {
85 if (strcmp(argv
[1], "inet") == 0)
87 else if (strcmp(argv
[1], "inet6") == 0)
89 else if (strcmp(argv
[1], "link") == 0)
91 else if (strcmp(argv
[1], "help") == 0)
94 fprintf(stderr
, "Protocol ID \"%s\" is unknown, try \"rtmon help\".\n", argv
[1]);
97 } else if (strcmp(argv
[1], "-4") == 0) {
99 } else if (strcmp(argv
[1], "-6") == 0) {
101 } else if (strcmp(argv
[1], "-0") == 0) {
103 } else if (matches(argv
[1], "-Version") == 0) {
104 printf("rtmon utility, iproute2-ss%s\n", SNAPSHOT
);
106 } else if (matches(argv
[1], "file") == 0) {
112 } else if (matches(argv
[1], "link") == 0) {
115 } else if (matches(argv
[1], "address") == 0) {
118 } else if (matches(argv
[1], "route") == 0) {
121 } else if (strcmp(argv
[1], "all") == 0) {
123 } else if (matches(argv
[1], "help") == 0) {
126 fprintf(stderr
, "Argument \"%s\" is unknown, try \"rtmon help\".\n", argv
[1]);
133 fprintf(stderr
, "Not enough information: argument \"file\" is required\n");
137 groups
|= RTMGRP_LINK
;
139 if (!family
|| family
== AF_INET
)
140 groups
|= RTMGRP_IPV4_IFADDR
;
141 if (!family
|| family
== AF_INET6
)
142 groups
|= RTMGRP_IPV6_IFADDR
;
145 if (!family
|| family
== AF_INET
)
146 groups
|= RTMGRP_IPV4_ROUTE
;
147 if (!family
|| family
== AF_INET6
)
148 groups
|= RTMGRP_IPV6_ROUTE
;
151 fp
= fopen(file
, "w");
153 perror("Cannot fopen");
157 if (rtnl_open(&rth
, groups
) < 0)
160 if (rtnl_wilddump_request(&rth
, AF_UNSPEC
, RTM_GETLINK
) < 0) {
161 perror("Cannot send dump request");
167 if (rtnl_dump_filter(&rth
, dump_msg
, fp
, NULL
, NULL
) < 0) {
168 fprintf(stderr
, "Dump terminated\n");
174 if (rtnl_listen(&rth
, dump_msg
, (void*)fp
) < 0)