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