1175061Sobrien/*-
252419Sjulian * Copyright (c) 1996-1999 Whistle Communications, Inc.
352419Sjulian * All rights reserved.
4175061Sobrien *
552419Sjulian * Subject to the following obligations and disclaimer of warranty, use and
652419Sjulian * redistribution of this software, in source or object code forms, with or
752419Sjulian * without modifications are expressly permitted by Whistle Communications;
852419Sjulian * provided, however, that:
952419Sjulian * 1. Any and all reproductions of the source or object code must include the
1052419Sjulian *    copyright notice above and the following disclaimer of warranties; and
1152419Sjulian * 2. No rights are granted, in any manner or form, to use Whistle
1252419Sjulian *    Communications, Inc. trademarks, including the mark "WHISTLE
1352419Sjulian *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
1452419Sjulian *    such appears in the above copyright notice or in the software.
15175061Sobrien *
1652419Sjulian * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
1752419Sjulian * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
1852419Sjulian * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
1952419Sjulian * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
2052419Sjulian * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
2152419Sjulian * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
2252419Sjulian * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
2352419Sjulian * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
2452419Sjulian * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
2552419Sjulian * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
2652419Sjulian * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
2752419Sjulian * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
2852419Sjulian * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
2952419Sjulian * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3052419Sjulian * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
3152419Sjulian * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
3252419Sjulian * OF SUCH DAMAGE.
3352419Sjulian */
3452419Sjulian
35132671Scharnier#include <sys/cdefs.h>
36132671Scharnier__FBSDID("$FreeBSD$");
3752419Sjulian
3852419Sjulian#include <sys/param.h>
3952419Sjulian#include <sys/queue.h>
4052419Sjulian#include <sys/socket.h>
4152419Sjulian#include <sys/socketvar.h>
4252419Sjulian#include <sys/protosw.h>
4352419Sjulian#include <sys/linker.h>
4452419Sjulian
4552419Sjulian#include <net/route.h>
4652419Sjulian
4752450Sdillon#include <netgraph.h>
4852419Sjulian#include <netgraph/ng_message.h>
4952419Sjulian#include <netgraph/ng_socket.h>
5052419Sjulian#include <netgraph/ng_socketvar.h>
5152419Sjulian
52200462Sdelphij#include <errno.h>
53160787Syar#include <stdint.h>
5452419Sjulian#include <stdio.h>
55279122Smarcel#include <stdbool.h>
5652419Sjulian#include <string.h>
57200462Sdelphij#include <unistd.h>
5852419Sjulian#include <err.h>
59279122Smarcel#include <libxo/xo.h>
6052419Sjulian#include "netstat.h"
6152419Sjulian
6252419Sjulianstatic	int first = 1;
6352419Sjulianstatic	int csock = -1;
6452419Sjulian
6552419Sjulianvoid
66171465Sjhbnetgraphprotopr(u_long off, const char *name, int af1 __unused,
67171465Sjhb    int proto __unused)
6852419Sjulian{
6952419Sjulian	struct ngpcb *this, *next;
7052419Sjulian	struct ngpcb ngpcb;
7152419Sjulian	struct socket sockb;
7252419Sjulian	int debug = 1;
7352419Sjulian
7452419Sjulian	/* If symbol not found, try looking in the KLD module */
7552419Sjulian	if (off == 0) {
76259638Smelifaro		if (debug)
77279122Smarcel			xo_warnx("Error reading symbols from ng_socket.ko");
78259638Smelifaro		return;
7952419Sjulian	}
8052419Sjulian
8152419Sjulian	/* Get pointer to first socket */
8252419Sjulian	kread(off, (char *)&this, sizeof(this));
8352419Sjulian
8452419Sjulian	/* Get my own socket node */
8552419Sjulian	if (csock == -1)
8652419Sjulian		NgMkSockNode(NULL, &csock, NULL);
8752419Sjulian
8852419Sjulian	for (; this != NULL; this = next) {
8952419Sjulian		u_char rbuf[sizeof(struct ng_mesg) + sizeof(struct nodeinfo)];
9052419Sjulian		struct ng_mesg *resp = (struct ng_mesg *) rbuf;
9152419Sjulian		struct nodeinfo *ni = (struct nodeinfo *) resp->data;
9252419Sjulian		char path[64];
9352419Sjulian
9452419Sjulian		/* Read in ngpcb structure */
9552419Sjulian		kread((u_long)this, (char *)&ngpcb, sizeof(ngpcb));
9670524Sphk		next = LIST_NEXT(&ngpcb, socks);
9752419Sjulian
9852419Sjulian		/* Read in socket structure */
9952419Sjulian		kread((u_long)ngpcb.ng_socket, (char *)&sockb, sizeof(sockb));
10052419Sjulian
10152419Sjulian		/* Check type of socket */
10252419Sjulian		if (strcmp(name, "ctrl") == 0 && ngpcb.type != NG_CONTROL)
10352419Sjulian			continue;
10452419Sjulian		if (strcmp(name, "data") == 0 && ngpcb.type != NG_DATA)
10552419Sjulian			continue;
10652419Sjulian
10752419Sjulian		/* Do headline */
10852419Sjulian		if (first) {
109279122Smarcel			xo_emit("{T:Netgraph sockets}\n");
11052419Sjulian			if (Aflag)
111279122Smarcel				xo_emit("{T:/%-8.8s} ", "PCB");
112279122Smarcel			xo_emit("{T:/%-5.5s} {T:/%-6.6s} {T:/%-6.6s} "
113279122Smarcel			    "{T:/%-14.14s} {T:/%s}\n",
114279122Smarcel			    "Type", "Recv-Q", "Send-Q", "Node Address",
115279122Smarcel			    "#Hooks");
11652419Sjulian			first = 0;
11752419Sjulian		}
11852419Sjulian
11952419Sjulian		/* Show socket */
12052419Sjulian		if (Aflag)
121279122Smarcel			xo_emit("{:address/%8lx} ", (u_long) this);
122279122Smarcel		xo_emit("{t:name/%-5.5s} {:receive-bytes-waiting/%6u} "
123279122Smarcel		    "{:send-byte-waiting/%6u} ",
124275326Sglebius		    name, sockb.so_rcv.sb_ccc, sockb.so_snd.sb_ccc);
12552419Sjulian
12652419Sjulian		/* Get info on associated node */
127230481Sglebius		if (ngpcb.node_id == 0 || csock == -1)
12852419Sjulian			goto finish;
129230481Sglebius		snprintf(path, sizeof(path), "[%x]:", ngpcb.node_id);
13052419Sjulian		if (NgSendMsg(csock, path,
13152419Sjulian		    NGM_GENERIC_COOKIE, NGM_NODEINFO, NULL, 0) < 0)
13252419Sjulian			goto finish;
13352419Sjulian		if (NgRecvMsg(csock, resp, sizeof(rbuf), NULL) < 0)
13452419Sjulian			goto finish;
13552419Sjulian
13652419Sjulian		/* Display associated node info */
13752419Sjulian		if (*ni->name != '\0')
13852419Sjulian			snprintf(path, sizeof(path), "%s:", ni->name);
139279122Smarcel		xo_emit("{t:path/%-14.14s} {:hooks/%4d}", path, ni->hooks);
14052419Sjulianfinish:
141279122Smarcel		xo_emit("\n");
14252419Sjulian	}
14352419Sjulian}
14452419Sjulian
145