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