netstat(1): Raise WARNS to 6 and fix warnings.
[dragonfly.git] / usr.bin / netstat / netgraph.c
blob50f8fee464f82c6c8adcc6ab85aa950eb43bb74f
1 /*
2 * Copyright (c) 1996-1999 Whistle Communications, Inc.
3 * All rights reserved.
4 *
5 * Subject to the following obligations and disclaimer of warranty, use and
6 * redistribution of this software, in source or object code forms, with or
7 * without modifications are expressly permitted by Whistle Communications;
8 * provided, however, that:
9 * 1. Any and all reproductions of the source or object code must include the
10 * copyright notice above and the following disclaimer of warranties; and
11 * 2. No rights are granted, in any manner or form, to use Whistle
12 * Communications, Inc. trademarks, including the mark "WHISTLE
13 * COMMUNICATIONS" on advertising, endorsements, or otherwise except as
14 * such appears in the above copyright notice or in the software.
16 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
17 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
18 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
19 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
21 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
22 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
23 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
24 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
25 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
26 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
27 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
28 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
32 * OF SUCH DAMAGE.
34 * $FreeBSD: src/usr.bin/netstat/netgraph.c,v 1.3.2.2 2001/08/10 09:07:09 ru Exp $
35 * $DragonFly: src/usr.bin/netstat/netgraph.c,v 1.6 2008/09/02 11:50:46 matthias Exp $
37 * $Id: atalk.c,v 1.11 1998/07/06 21:01:22 bde Exp $
40 #define _KERNEL_STRUCTURES
41 #include <sys/param.h>
42 #include <sys/queue.h>
43 #include <sys/socket.h>
44 #include <sys/socketvar.h>
45 #include <sys/protosw.h>
46 #include <sys/linker.h>
48 #include <net/route.h>
50 #include <netgraph.h>
51 #include <netgraph/ng_message.h>
52 #include <netgraph/socket/ng_socket.h>
53 #include <netgraph/socket/ng_socketvar.h>
55 #include <nlist.h>
56 #include <errno.h>
57 #include <stdio.h>
58 #include <string.h>
59 #include <unistd.h>
60 #include <err.h>
61 #include "netstat.h"
63 static int first = 1;
64 static int csock = -1;
66 void
67 netgraphprotopr(u_long off, const char *name, int af1 __unused)
69 struct ngpcb *this, *next;
70 struct ngpcb ngpcb;
71 struct ngsock info;
72 struct socket sockb;
73 int debug = 1;
75 /* If symbol not found, try looking in the KLD module */
76 if (off == 0) {
77 const char *const modname = "ng_socket.ko";
78 /* XXX We should get "mpath" from "sysctl kern.module_path" */
79 const char *mpath[] = { "/", "/boot/", "/boot/modules/", NULL };
80 struct nlist sym[] = { { .n_name = "_ngsocklist" },
81 { .n_name = NULL } };
82 const char **pre;
83 struct kld_file_stat ks;
84 int fileid;
86 /* See if module is loaded */
87 if ((fileid = kldfind(modname)) < 0) {
88 if (debug)
89 warn("kldfind(%s)", modname);
90 return;
93 /* Get module info */
94 memset(&ks, 0, sizeof(ks));
95 ks.version = sizeof(struct kld_file_stat);
96 if (kldstat(fileid, &ks) < 0) {
97 if (debug)
98 warn("kldstat(%d)", fileid);
99 return;
102 /* Get symbol table from module file */
103 for (pre = mpath; *pre; pre++) {
104 char path[MAXPATHLEN];
106 snprintf(path, sizeof(path), "%s%s", *pre, modname);
107 if (nlist(path, sym) == 0)
108 break;
111 /* Did we find it? */
112 if (sym[0].n_value == 0) {
113 if (debug)
114 warnx("%s not found", modname);
115 return;
118 /* Symbol found at load address plus symbol offset */
119 off = (u_long) ks.address + sym[0].n_value;
122 /* Get pointer to first socket */
123 kread(off, (char *)&this, sizeof(this));
125 /* Get my own socket node */
126 if (csock == -1)
127 NgMkSockNode(NULL, &csock, NULL);
129 for (; this != NULL; this = next) {
130 u_char rbuf[sizeof(struct ng_mesg) + sizeof(struct nodeinfo)];
131 struct ng_mesg *resp = (struct ng_mesg *) rbuf;
132 struct nodeinfo *ni = (struct nodeinfo *) resp->data;
133 char path[64];
135 /* Read in ngpcb structure */
136 kread((u_long)this, (char *)&ngpcb, sizeof(ngpcb));
137 next = LIST_NEXT(&ngpcb, socks);
139 /* Read in socket structure */
140 kread((u_long)ngpcb.ng_socket, (char *)&sockb, sizeof(sockb));
142 /* Check type of socket */
143 if (strcmp(name, "ctrl") == 0 && ngpcb.type != NG_CONTROL)
144 continue;
145 if (strcmp(name, "data") == 0 && ngpcb.type != NG_DATA)
146 continue;
148 /* Do headline */
149 if (first) {
150 printf("Netgraph sockets\n");
151 if (Aflag)
152 printf("%-8.8s ", "PCB");
153 printf("%-5.5s %-6.6s %-6.6s %-14.14s %s\n",
154 "Type", "Recv-Q", "Send-Q",
155 "Node Address", "#Hooks");
156 first = 0;
159 /* Show socket */
160 if (Aflag)
161 printf("%8lx ", (u_long) this);
162 printf("%-5.5s %6lu %6lu ",
163 name, sockb.so_rcv.ssb_cc, sockb.so_snd.ssb_cc);
165 /* Get ngsock structure */
166 if (ngpcb.sockdata == 0) /* unconnected data socket */
167 goto finish;
168 kread((u_long)ngpcb.sockdata, (char *)&info, sizeof(info));
170 /* Get info on associated node */
171 if (info.node == 0 || csock == -1)
172 goto finish;
173 snprintf(path, sizeof(path), "[%lx]:", (u_long) info.node);
174 if (NgSendMsg(csock, path,
175 NGM_GENERIC_COOKIE, NGM_NODEINFO, NULL, 0) < 0)
176 goto finish;
177 if (NgRecvMsg(csock, resp, sizeof(rbuf), NULL) < 0)
178 goto finish;
180 /* Display associated node info */
181 if (*ni->name != '\0')
182 snprintf(path, sizeof(path), "%s:", ni->name);
183 printf("%-14.14s %4d", path, ni->hooks);
184 finish:
185 putchar('\n');