turns printfs back on
[freebsd-src/fkvm-freebsd.git] / usr.sbin / flowctl / flowctl.c
blob856fe583a3bb869ffcfe6931d31e4ab8e5016122
1 /*-
2 * Copyright (c) 2004-2005 Gleb Smirnoff <glebius@FreeBSD.org>
3 * Copyright (c) 2001-2003 Roman V. Palagin <romanp@unshadow.net>
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
27 * $SourceForge: flowctl.c,v 1.15 2004/08/31 20:24:58 glebius Exp $
30 #ifndef lint
31 static const char rcs_id[] =
32 "@(#) $FreeBSD$";
33 #endif
35 #include <sys/types.h>
36 #include <sys/time.h>
37 #include <sys/socket.h>
38 #include <sys/queue.h>
40 #include <net/if.h>
41 #include <netinet/in.h>
43 #include <arpa/inet.h>
45 #include <err.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <unistd.h>
51 #include <netgraph.h>
52 #include <netgraph/netflow/ng_netflow.h>
54 #define CISCO_SH_FLOW_HEADER "SrcIf SrcIPaddress DstIf DstIPaddress Pr SrcP DstP Pkts\n"
55 #define CISCO_SH_FLOW "%-13s %-15s %-13s %-15s %2u %4.4x %4.4x %6lu\n"
57 #define CISCO_SH_VERB_FLOW_HEADER "SrcIf SrcIPaddress DstIf DstIPaddress Pr TOS Flgs Pkts\n" \
58 "Port Msk AS Port Msk AS NextHop B/Pk Active\n"
60 #define CISCO_SH_VERB_FLOW "%-14s %-15s %-14s %-15s %2u %3x %4x %6lu\n" \
61 "%4.4x /%-2u %-5u %4.4x /%-2u %-5u %-15s %9u %8u\n\n"
63 static int flow_cache_print(struct ngnf_flows *recs);
64 static int flow_cache_print_verbose(struct ngnf_flows *recs);
65 static int ctl_show(int, char **);
66 static void help(void);
67 static void execute_command(int, char **);
69 struct ip_ctl_cmd {
70 char *cmd_name;
71 int (*cmd_func)(int argc, char **argv);
74 struct ip_ctl_cmd cmds[] = {
75 {"show", ctl_show},
76 {NULL, NULL},
79 int cs;
80 char ng_nodename[NG_PATHLEN + 1];
82 int
83 main(int argc, char **argv)
85 int c;
86 char sname[NG_NODESIZ];
87 int rcvbuf = SORCVBUF_SIZE;
88 char *ng_name;
90 /* parse options */
91 while ((c = getopt(argc, argv, "d:")) != -1) {
92 switch (c) {
93 case 'd': /* set libnetgraph debug level. */
94 NgSetDebug(atoi(optarg));
95 break;
99 argc -= optind;
100 argv += optind;
101 ng_name = argv[0];
102 if (ng_name == NULL)
103 help();
104 argc--;
105 argv++;
107 snprintf(ng_nodename, sizeof(ng_nodename), "%s:", ng_name);
109 /* create control socket. */
110 snprintf(sname, sizeof(sname), "flowctl%i", getpid());
112 if (NgMkSockNode(sname, &cs, NULL) == -1)
113 err(1, "NgMkSockNode");
115 /* set receive buffer size */
116 if (setsockopt(cs, SOL_SOCKET, SO_RCVBUF, &rcvbuf, sizeof(int)) == -1)
117 err(1, "setsockopt(SOL_SOCKET, SO_RCVBUF)");
119 /* parse and execute command */
120 execute_command(argc, argv);
122 close(cs);
124 exit(0);
127 static void
128 execute_command(int argc, char **argv)
130 int cindex = -1;
131 int i;
133 if (!argc)
134 help();
135 for (i = 0; cmds[i].cmd_name != NULL; i++)
136 if (!strncmp(argv[0], cmds[i].cmd_name, strlen(argv[0]))) {
137 if (cindex != -1)
138 errx(1, "ambiguous command: %s", argv[0]);
139 cindex = i;
141 if (cindex == -1)
142 errx(1, "bad command: %s", argv[0]);
143 argc--;
144 argv++;
145 (*cmds[cindex].cmd_func)(argc, argv);
148 static int
149 ctl_show(int argc, char **argv)
151 struct ng_mesg *ng_mesg;
152 struct ngnf_flows *data;
153 char path[NG_PATHLEN + 1];
154 int token, nread, last = 0;
155 int verbose = 0;
157 if (argc > 0 && !strncmp(argv[0], "verbose", strlen(argv[0])))
158 verbose = 1;
160 ng_mesg = alloca(SORCVBUF_SIZE);
162 if (verbose)
163 printf(CISCO_SH_VERB_FLOW_HEADER);
164 else
165 printf(CISCO_SH_FLOW_HEADER);
167 for (;;) {
168 /* request set of accounting records */
169 token = NgSendMsg(cs, ng_nodename, NGM_NETFLOW_COOKIE,
170 NGM_NETFLOW_SHOW, (void *)&last, sizeof(last));
171 if (token == -1)
172 err(1, "NgSendMsg(NGM_NETFLOW_SHOW)");
174 /* read reply */
175 nread = NgRecvMsg(cs, ng_mesg, SORCVBUF_SIZE, path);
176 if (nread == -1)
177 err(1, "NgRecvMsg() failed");
179 if (ng_mesg->header.token != token)
180 err(1, "NgRecvMsg(NGM_NETFLOW_SHOW): token mismatch");
182 data = (struct ngnf_flows*)ng_mesg->data;
183 if ((ng_mesg->header.arglen < (sizeof(*data))) ||
184 (ng_mesg->header.arglen < (sizeof(*data) +
185 (data->nentries * sizeof(struct flow_entry_data)))))
186 err(1, "NgRecvMsg(NGM_NETFLOW_SHOW): arglen too small");
188 if (verbose)
189 (void )flow_cache_print_verbose(data);
190 else
191 (void )flow_cache_print(data);
193 if (data->last != 0)
194 last = data->last;
195 else
196 break;
199 return (0);
202 static int
203 flow_cache_print(struct ngnf_flows *recs)
205 struct flow_entry_data *fle;
206 char src[INET_ADDRSTRLEN], dst[INET_ADDRSTRLEN];
207 char src_if[IFNAMSIZ], dst_if[IFNAMSIZ];
208 int i;
210 /* quick check */
211 if (recs->nentries == 0)
212 return (0);
214 fle = recs->entries;
215 for (i = 0; i < recs->nentries; i++, fle++) {
216 inet_ntop(AF_INET, &fle->r.r_src, src, sizeof(src));
217 inet_ntop(AF_INET, &fle->r.r_dst, dst, sizeof(dst));
218 printf(CISCO_SH_FLOW,
219 if_indextoname(fle->fle_i_ifx, src_if),
220 src,
221 if_indextoname(fle->fle_o_ifx, dst_if),
222 dst,
223 fle->r.r_ip_p,
224 ntohs(fle->r.r_sport),
225 ntohs(fle->r.r_dport),
226 fle->packets);
230 return (i);
233 static int
234 flow_cache_print_verbose(struct ngnf_flows *recs)
236 struct flow_entry_data *fle;
237 char src[INET_ADDRSTRLEN], dst[INET_ADDRSTRLEN], next[INET_ADDRSTRLEN];
238 char src_if[IFNAMSIZ], dst_if[IFNAMSIZ];
239 int i;
241 /* quick check */
242 if (recs->nentries == 0)
243 return (0);
245 fle = recs->entries;
246 for (i = 0; i < recs->nentries; i++, fle++) {
247 inet_ntop(AF_INET, &fle->r.r_src, src, sizeof(src));
248 inet_ntop(AF_INET, &fle->r.r_dst, dst, sizeof(dst));
249 inet_ntop(AF_INET, &fle->next_hop, next, sizeof(next));
250 printf(CISCO_SH_VERB_FLOW,
251 if_indextoname(fle->fle_i_ifx, src_if),
252 src,
253 if_indextoname(fle->fle_o_ifx, dst_if),
254 dst,
255 fle->r.r_ip_p,
256 fle->r.r_tos,
257 fle->tcp_flags,
258 fle->packets,
259 ntohs(fle->r.r_sport),
260 fle->src_mask,
262 ntohs(fle->r.r_dport),
263 fle->dst_mask,
265 next,
266 (u_int)(fle->bytes / fle->packets),
271 return (i);
274 static void
275 help(void)
277 extern char *__progname;
279 fprintf(stderr, "usage: %s [-d level] nodename command\n", __progname);
280 exit (0);