11541Srgrimes/*-
21541Srgrimes * Copyright (c) 1996-1999 Whistle Communications, Inc.
31541Srgrimes * All rights reserved.
41541Srgrimes *
51541Srgrimes * Subject to the following obligations and disclaimer of warranty, use and
61541Srgrimes * redistribution of this software, in source or object code forms, with or
71541Srgrimes * without modifications are expressly permitted by Whistle Communications;
81541Srgrimes * provided, however, that:
91541Srgrimes * 1. Any and all reproductions of the source or object code must include the
101541Srgrimes *    copyright notice above and the following disclaimer of warranties; and
111541Srgrimes * 2. No rights are granted, in any manner or form, to use Whistle
121541Srgrimes *    Communications, Inc. trademarks, including the mark "WHISTLE
131541Srgrimes *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
141541Srgrimes *    such appears in the above copyright notice or in the software.
151541Srgrimes *
161541Srgrimes * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
171541Srgrimes * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
181541Srgrimes * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
191541Srgrimes * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
201541Srgrimes * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
211541Srgrimes * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
221541Srgrimes * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
231541Srgrimes * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
241541Srgrimes * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
251541Srgrimes * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
261541Srgrimes * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
271541Srgrimes * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
281541Srgrimes * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
291541Srgrimes * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
301541Srgrimes * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
311541Srgrimes * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
321541Srgrimes * OF SUCH DAMAGE.
331541Srgrimes */
3437689Sdfr
351541Srgrimes#include <sys/cdefs.h>
361541Srgrimes__FBSDID("$FreeBSD$");
372165Spaul
382165Spaul#include <sys/param.h>
392165Spaul#include <sys/queue.h>
401541Srgrimes#include <sys/socket.h>
411541Srgrimes#include <sys/socketvar.h>
421541Srgrimes#include <sys/protosw.h>
4318207Sbde#include <sys/linker.h>
441541Srgrimes
451541Srgrimes#include <net/route.h>
461541Srgrimes
471541Srgrimes#include <netgraph.h>
4811913Sphk#include <netgraph/ng_message.h>
4912685Speter#include <netgraph/ng_socket.h>
5012685Speter#include <netgraph/ng_socketvar.h>
5112685Speter
5222521Sdyson#include <nlist.h>
5312685Speter#include <errno.h>
5411913Sphk#include <stdint.h>
5534924Sbde#include <stdio.h>
561541Srgrimes#include <string.h>
5734924Sbde#include <unistd.h>
5834924Sbde#include <err.h>
591541Srgrimes#include "netstat.h"
6034924Sbde
611541Srgrimesstatic	int first = 1;
621541Srgrimesstatic	int csock = -1;
6311865Sphk
6411865Sphkvoid
6511865Sphknetgraphprotopr(u_long off, const char *name, int af1 __unused,
6611865Sphk    int proto __unused)
6711865Sphk{
6811865Sphk	struct ngpcb *this, *next;
6911865Sphk	struct ngpcb ngpcb;
7011865Sphk	struct socket sockb;
7111865Sphk	int debug = 1;
7211865Sphk
7311865Sphk	/* If symbol not found, try looking in the KLD module */
7411865Sphk	if (off == 0) {
7511865Sphk		if (debug)
7611865Sphk			fprintf(stderr,
7711865Sphk			    "Error reading symbols from ng_socket.ko");
7811865Sphk		return;
7911865Sphk	}
8011865Sphk
8111865Sphk	/* Get pointer to first socket */
8211865Sphk	kread(off, (char *)&this, sizeof(this));
8311865Sphk
8411865Sphk	/* Get my own socket node */
8511865Sphk	if (csock == -1)
8611865Sphk		NgMkSockNode(NULL, &csock, NULL);
8711865Sphk
8823328Sache	for (; this != NULL; this = next) {
8923322Sache		u_char rbuf[sizeof(struct ng_mesg) + sizeof(struct nodeinfo)];
9011865Sphk		struct ng_mesg *resp = (struct ng_mesg *) rbuf;
9111865Sphk		struct nodeinfo *ni = (struct nodeinfo *) resp->data;
9211865Sphk		char path[64];
9311865Sphk
9411865Sphk		/* Read in ngpcb structure */
9511865Sphk		kread((u_long)this, (char *)&ngpcb, sizeof(ngpcb));
961541Srgrimes		next = LIST_NEXT(&ngpcb, socks);
971541Srgrimes
981541Srgrimes		/* Read in socket structure */
991541Srgrimes		kread((u_long)ngpcb.ng_socket, (char *)&sockb, sizeof(sockb));
1001541Srgrimes
1018876Srgrimes		/* Check type of socket */
1021541Srgrimes		if (strcmp(name, "ctrl") == 0 && ngpcb.type != NG_CONTROL)
1031541Srgrimes			continue;
1041541Srgrimes		if (strcmp(name, "data") == 0 && ngpcb.type != NG_DATA)
1051541Srgrimes			continue;
1061541Srgrimes
1071541Srgrimes		/* Do headline */
1081541Srgrimes		if (first) {
1091541Srgrimes			printf("Netgraph sockets\n");
1101541Srgrimes			if (Aflag)
1111541Srgrimes				printf("%-8.8s ", "PCB");
1121541Srgrimes			printf("%-5.5s %-6.6s %-6.6s %-14.14s %s\n",
1131541Srgrimes			    "Type", "Recv-Q", "Send-Q",
1141541Srgrimes			    "Node Address", "#Hooks");
1151541Srgrimes			first = 0;
1161541Srgrimes		}
1171541Srgrimes
1181541Srgrimes		/* Show socket */
1191541Srgrimes		if (Aflag)
1201541Srgrimes			printf("%8lx ", (u_long) this);
12137689Sdfr		printf("%-5.5s %6u %6u ",
1221541Srgrimes		    name, sockb.so_rcv.sb_cc, sockb.so_snd.sb_cc);
12337689Sdfr
12437689Sdfr		/* Get info on associated node */
12537689Sdfr		if (ngpcb.node_id == 0 || csock == -1)
12637689Sdfr			goto finish;
1271541Srgrimes		snprintf(path, sizeof(path), "[%x]:", ngpcb.node_id);
1281541Srgrimes		if (NgSendMsg(csock, path,
1291541Srgrimes		    NGM_GENERIC_COOKIE, NGM_NODEINFO, NULL, 0) < 0)
1301541Srgrimes			goto finish;
1311541Srgrimes		if (NgRecvMsg(csock, resp, sizeof(rbuf), NULL) < 0)
1321541Srgrimes			goto finish;
1331541Srgrimes
1341541Srgrimes		/* Display associated node info */
1351541Srgrimes		if (*ni->name != '\0')
1361541Srgrimes			snprintf(path, sizeof(path), "%s:", ni->name);
1371541Srgrimes		printf("%-14.14s %4d", path, ni->hooks);
1381541Srgrimesfinish:
1391541Srgrimes		putchar('\n');
1401541Srgrimes	}
1412165Spaul}
1422165Spaul
143