netgraph.c revision 52445
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 * $FreeBSD: head/usr.bin/netstat/netgraph.c 52445 1999-10-23 17:45:11Z dillon $ 35 */ 36 37#ifndef lint 38static const char rcsid[] = 39 "$Id: atalk.c,v 1.11 1998/07/06 21:01:22 bde Exp $"; 40#endif /* not lint */ 41 42#include <sys/param.h> 43#include <sys/queue.h> 44#include <sys/socket.h> 45#include <sys/socketvar.h> 46#include <sys/protosw.h> 47#include <sys/linker.h> 48 49#include <net/route.h> 50 51#include <netgraph/ng_message.h> 52#include <netgraph/ng_socket.h> 53#include <netgraph/ng_socketvar.h> 54 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" 62 63static int first = 1; 64static int csock = -1; 65 66void 67netgraphprotopr(u_long off, char *name) 68{ 69 struct ngpcb *this, *next; 70 struct ngpcb ngpcb; 71 struct ngsock info; 72 struct socket sockb; 73 int debug = 1; 74 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/", "/modules/", NULL }; 80 struct nlist sym[] = { { "_ngsocklist" }, { NULL } }; 81 const char **pre; 82 struct kld_file_stat ks; 83 int fileid; 84 85 /* See if module is loaded */ 86 if ((fileid = kldfind(modname)) < 0) { 87 if (debug) 88 warn("kldfind(%s)", modname); 89 return; 90 } 91 92 /* Get module info */ 93 memset(&ks, 0, sizeof(ks)); 94 ks.version = sizeof(struct kld_file_stat); 95 if (kldstat(fileid, &ks) < 0) { 96 if (debug) 97 warn("kldstat(%d)", fileid); 98 return; 99 } 100 101 /* Get symbol table from module file */ 102 for (pre = mpath; *pre; pre++) { 103 char path[MAXPATHLEN]; 104 105 snprintf(path, sizeof(path), "%s%s", *pre, modname); 106 if (nlist(path, sym) == 0) 107 break; 108 } 109 110 /* Did we find it? */ 111 if (sym[0].n_value == 0) { 112 if (debug) 113 warnx("%s not found", modname); 114 return; 115 } 116 117 /* Symbol found at load address plus symbol offset */ 118 off = (u_long) ks.address + sym[0].n_value; 119 } 120 121 /* Get pointer to first socket */ 122 kread(off, (char *)&this, sizeof(this)); 123 124 /* Get my own socket node */ 125 if (csock == -1) 126 NgMkSockNode(NULL, &csock, NULL); 127 128 for (; this != NULL; this = next) { 129 u_char rbuf[sizeof(struct ng_mesg) + sizeof(struct nodeinfo)]; 130 struct ng_mesg *resp = (struct ng_mesg *) rbuf; 131 struct nodeinfo *ni = (struct nodeinfo *) resp->data; 132 char path[64]; 133 134 /* Read in ngpcb structure */ 135 kread((u_long)this, (char *)&ngpcb, sizeof(ngpcb)); 136 next = ngpcb.socks.le_next; 137 138 /* Read in socket structure */ 139 kread((u_long)ngpcb.ng_socket, (char *)&sockb, sizeof(sockb)); 140 141 /* Check type of socket */ 142 if (strcmp(name, "ctrl") == 0 && ngpcb.type != NG_CONTROL) 143 continue; 144 if (strcmp(name, "data") == 0 && ngpcb.type != NG_DATA) 145 continue; 146 147 /* Do headline */ 148 if (first) { 149 printf("Netgraph sockets\n"); 150 if (Aflag) 151 printf("%-8.8s ", "PCB"); 152 printf("%-5.5s %-6.6s %-6.6s %-14.14s %s\n", 153 "Type", "Recv-Q", "Send-Q", 154 "Node Address", "#Hooks"); 155 first = 0; 156 } 157 158 /* Show socket */ 159 if (Aflag) 160 printf("%8lx ", (u_long) this); 161 printf("%-5.5s %6lu %6lu ", 162 name, sockb.so_rcv.sb_cc, sockb.so_snd.sb_cc); 163 164 /* Get ngsock structure */ 165 if (ngpcb.sockdata == 0) /* unconnected data socket */ 166 goto finish; 167 kread((u_long)ngpcb.sockdata, (char *)&info, sizeof(info)); 168 169 /* Get info on associated node */ 170 if (info.node == 0 || csock == -1) 171 goto finish; 172 snprintf(path, sizeof(path), "[%lx]:", (u_long) info.node); 173 if (NgSendMsg(csock, path, 174 NGM_GENERIC_COOKIE, NGM_NODEINFO, NULL, 0) < 0) 175 goto finish; 176 if (NgRecvMsg(csock, resp, sizeof(rbuf), NULL) < 0) 177 goto finish; 178 179 /* Display associated node info */ 180 if (*ni->name != '\0') 181 snprintf(path, sizeof(path), "%s:", ni->name); 182 printf("%-14.14s %4d", path, ni->hooks); 183finish: 184 putchar('\n'); 185 } 186} 187 188