]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/netstat/netgraph.c
This commit was generated by cvs2svn to compensate for changes in r156251,
[FreeBSD/FreeBSD.git] / usr.bin / netstat / netgraph.c
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.
15  * 
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.
33  */
34
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37
38 #include <sys/param.h>
39 #include <sys/queue.h>
40 #include <sys/socket.h>
41 #include <sys/socketvar.h>
42 #include <sys/protosw.h>
43 #include <sys/linker.h>
44
45 #include <net/route.h>
46
47 #include <netgraph.h>
48 #include <netgraph/ng_message.h>
49 #include <netgraph/ng_socket.h>
50 #include <netgraph/ng_socketvar.h>
51
52 #include <nlist.h>
53 #include <errno.h>
54 #include <stdio.h>
55 #include <string.h>
56 #include <unistd.h>
57 #include <err.h>
58 #include "netstat.h"
59
60 static  int first = 1;
61 static  int csock = -1;
62
63 void
64 netgraphprotopr(u_long off, const char *name, int af1 __unused)
65 {
66         struct ngpcb *this, *next;
67         struct ngpcb ngpcb;
68         struct ngsock info;
69         struct socket sockb;
70         int debug = 1;
71
72         /* If symbol not found, try looking in the KLD module */
73         if (off == 0) {
74                 const char *const modname = "ng_socket.ko";
75 /* XXX We should get "mpath" from "sysctl kern.module_path" */
76                 const char *mpath[] = { "/", "/boot/", "/modules/", NULL };
77                 struct nlist sym[] = { { "_ngsocklist" }, { NULL } };
78                 const char **pre;
79                 struct kld_file_stat ks;
80                 int fileid;
81
82                 /* See if module is loaded */
83                 if ((fileid = kldfind(modname)) < 0) {
84                         if (debug)
85                                 warn("kldfind(%s)", modname);
86                         return;
87                 }
88
89                 /* Get module info */
90                 memset(&ks, 0, sizeof(ks));
91                 ks.version = sizeof(struct kld_file_stat);
92                 if (kldstat(fileid, &ks) < 0) {
93                         if (debug)
94                                 warn("kldstat(%d)", fileid);
95                         return;
96                 }
97
98                 /* Get symbol table from module file */
99                 for (pre = mpath; *pre; pre++) {
100                         char path[MAXPATHLEN];
101
102                         snprintf(path, sizeof(path), "%s%s", *pre, modname);
103                         if (nlist(path, sym) == 0)
104                                 break;
105                 }
106
107                 /* Did we find it? */
108                 if (sym[0].n_value == 0) {
109                         if (debug)
110                                 warnx("%s not found", modname);
111                         return;
112                 }
113
114                 /* Symbol found at load address plus symbol offset */
115                 off = (u_long) ks.address + sym[0].n_value;
116         }
117
118         /* Get pointer to first socket */
119         kread(off, (char *)&this, sizeof(this));
120
121         /* Get my own socket node */
122         if (csock == -1)
123                 NgMkSockNode(NULL, &csock, NULL);
124
125         for (; this != NULL; this = next) {
126                 u_char rbuf[sizeof(struct ng_mesg) + sizeof(struct nodeinfo)];
127                 struct ng_mesg *resp = (struct ng_mesg *) rbuf;
128                 struct nodeinfo *ni = (struct nodeinfo *) resp->data;
129                 char path[64];
130
131                 /* Read in ngpcb structure */
132                 kread((u_long)this, (char *)&ngpcb, sizeof(ngpcb));
133                 next = LIST_NEXT(&ngpcb, socks);
134
135                 /* Read in socket structure */
136                 kread((u_long)ngpcb.ng_socket, (char *)&sockb, sizeof(sockb));
137
138                 /* Check type of socket */
139                 if (strcmp(name, "ctrl") == 0 && ngpcb.type != NG_CONTROL)
140                         continue;
141                 if (strcmp(name, "data") == 0 && ngpcb.type != NG_DATA)
142                         continue;
143
144                 /* Do headline */
145                 if (first) {
146                         printf("Netgraph sockets\n");
147                         if (Aflag)
148                                 printf("%-8.8s ", "PCB");
149                         printf("%-5.5s %-6.6s %-6.6s %-14.14s %s\n",
150                             "Type", "Recv-Q", "Send-Q",
151                             "Node Address", "#Hooks");
152                         first = 0;
153                 }
154
155                 /* Show socket */
156                 if (Aflag)
157                         printf("%8lx ", (u_long) this);
158                 printf("%-5.5s %6u %6u ",
159                     name, sockb.so_rcv.sb_cc, sockb.so_snd.sb_cc);
160
161                 /* Get ngsock structure */
162                 if (ngpcb.sockdata == 0)        /* unconnected data socket */
163                         goto finish;
164                 kread((u_long)ngpcb.sockdata, (char *)&info, sizeof(info));
165
166                 /* Get info on associated node */
167                 if (info.node == 0 || csock == -1)
168                         goto finish;
169                 snprintf(path, sizeof(path), "[%lx]:", (u_long) info.node);
170                 if (NgSendMsg(csock, path,
171                     NGM_GENERIC_COOKIE, NGM_NODEINFO, NULL, 0) < 0)
172                         goto finish;
173                 if (NgRecvMsg(csock, resp, sizeof(rbuf), NULL) < 0)
174                         goto finish;
175
176                 /* Display associated node info */
177                 if (*ni->name != '\0')
178                         snprintf(path, sizeof(path), "%s:", ni->name);
179                 printf("%-14.14s %4d", path, ni->hooks);
180 finish:
181                 putchar('\n');
182         }
183 }
184