#define _KERNEL_STRUCTURES
#include <sys/param.h>
#include <sys/queue.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/protosw.h>
#include <sys/linker.h>
#include <net/route.h>
#include <netgraph.h>
#include <netgraph/ng_message.h>
#include <netgraph/socket/ng_socket.h>
#include <netgraph/socket/ng_socketvar.h>
#include <nlist.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <err.h>
#include "netstat.h"
static int first = 1;
static int csock = -1;
void
netgraphprotopr(u_long off, const char *name, int af1 __unused)
{
struct ngpcb *this, *next;
struct ngpcb ngpcb;
struct ngsock info;
struct socket sockb;
int debug = 1;
if (off == 0) {
const char *const modname = "ng_socket.ko";
const char *mpath[] = { "/", "/boot/", "/boot/modules/", NULL };
struct nlist sym[] = { { .n_name = "_ngsocklist" },
{ .n_name = NULL } };
const char **pre;
struct kld_file_stat ks;
int fileid;
if ((fileid = kldfind(modname)) < 0) {
if (debug)
warn("kldfind(%s)", modname);
return;
}
memset(&ks, 0, sizeof(ks));
ks.version = sizeof(struct kld_file_stat);
if (kldstat(fileid, &ks) < 0) {
if (debug)
warn("kldstat(%d)", fileid);
return;
}
for (pre = mpath; *pre; pre++) {
char path[MAXPATHLEN];
snprintf(path, sizeof(path), "%s%s", *pre, modname);
if (nlist(path, sym) == 0)
break;
}
if (sym[0].n_value == 0) {
if (debug)
warnx("%s not found", modname);
return;
}
off = (u_long) ks.address + sym[0].n_value;
}
kread(off, (char *)&this, sizeof(this));
if (csock == -1)
NgMkSockNode(NULL, &csock, NULL);
for (; this != NULL; this = next) {
u_char rbuf[sizeof(struct ng_mesg) + sizeof(struct nodeinfo)];
struct ng_mesg *resp = (struct ng_mesg *) rbuf;
struct nodeinfo *ni = (struct nodeinfo *) resp->data;
char path[64];
kread((u_long)this, (char *)&ngpcb, sizeof(ngpcb));
next = LIST_NEXT(&ngpcb, socks);
kread((u_long)ngpcb.ng_socket, (char *)&sockb, sizeof(sockb));
if (strcmp(name, "ctrl") == 0 && ngpcb.type != NG_CONTROL)
continue;
if (strcmp(name, "data") == 0 && ngpcb.type != NG_DATA)
continue;
if (first) {
printf("Netgraph sockets\n");
if (Aflag)
printf("%-8.8s ", "PCB");
printf("%-5.5s %-6.6s %-6.6s %-14.14s %s\n",
"Type", "Recv-Q", "Send-Q",
"Node Address", "#Hooks");
first = 0;
}
if (Aflag)
printf("%8lx ", (u_long) this);
printf("%-5.5s %6lu %6lu ",
name, sockb.so_rcv.ssb_cc, sockb.so_snd.ssb_cc);
if (ngpcb.sockdata == 0)
goto finish;
kread((u_long)ngpcb.sockdata, (char *)&info, sizeof(info));
if (info.node == 0 || csock == -1)
goto finish;
snprintf(path, sizeof(path), "[%lx]:", (u_long) info.node);
if (NgSendMsg(csock, path,
NGM_GENERIC_COOKIE, NGM_NODEINFO, NULL, 0) < 0)
goto finish;
if (NgRecvMsg(csock, resp, sizeof(rbuf), NULL) < 0)
goto finish;
if (*ni->name != '\0')
snprintf(path, sizeof(path), "%s:", ni->name);
printf("%-14.14s %4d", path, ni->hooks);
finish:
putchar('\n');
}
}