165475bc8SDavid E. O'Brien /*-
24cf49a43SJulian Elischer * Copyright (c) 1996-1999 Whistle Communications, Inc.
34cf49a43SJulian Elischer * All rights reserved.
44cf49a43SJulian Elischer *
54cf49a43SJulian Elischer * Subject to the following obligations and disclaimer of warranty, use and
64cf49a43SJulian Elischer * redistribution of this software, in source or object code forms, with or
74cf49a43SJulian Elischer * without modifications are expressly permitted by Whistle Communications;
84cf49a43SJulian Elischer * provided, however, that:
94cf49a43SJulian Elischer * 1. Any and all reproductions of the source or object code must include the
104cf49a43SJulian Elischer * copyright notice above and the following disclaimer of warranties; and
114cf49a43SJulian Elischer * 2. No rights are granted, in any manner or form, to use Whistle
124cf49a43SJulian Elischer * Communications, Inc. trademarks, including the mark "WHISTLE
134cf49a43SJulian Elischer * COMMUNICATIONS" on advertising, endorsements, or otherwise except as
144cf49a43SJulian Elischer * such appears in the above copyright notice or in the software.
154cf49a43SJulian Elischer *
164cf49a43SJulian Elischer * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
174cf49a43SJulian Elischer * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
184cf49a43SJulian Elischer * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
194cf49a43SJulian Elischer * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
204cf49a43SJulian Elischer * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
214cf49a43SJulian Elischer * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
224cf49a43SJulian Elischer * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
234cf49a43SJulian Elischer * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
244cf49a43SJulian Elischer * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
254cf49a43SJulian Elischer * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
264cf49a43SJulian Elischer * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
274cf49a43SJulian Elischer * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
284cf49a43SJulian Elischer * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
294cf49a43SJulian Elischer * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
304cf49a43SJulian Elischer * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
314cf49a43SJulian Elischer * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
324cf49a43SJulian Elischer * OF SUCH DAMAGE.
334cf49a43SJulian Elischer */
344cf49a43SJulian Elischer
354cf49a43SJulian Elischer #include <sys/param.h>
364cf49a43SJulian Elischer #include <sys/queue.h>
374cf49a43SJulian Elischer #include <sys/socket.h>
38*0e229f34SGleb Smirnoff #define _WANT_SOCKET
394cf49a43SJulian Elischer #include <sys/socketvar.h>
404cf49a43SJulian Elischer #include <sys/protosw.h>
414cf49a43SJulian Elischer #include <sys/linker.h>
424cf49a43SJulian Elischer
434cf49a43SJulian Elischer #include <net/route.h>
444cf49a43SJulian Elischer
45ebdb21f4SMatthew Dillon #include <netgraph.h>
464cf49a43SJulian Elischer #include <netgraph/ng_message.h>
474cf49a43SJulian Elischer #include <netgraph/ng_socket.h>
484cf49a43SJulian Elischer #include <netgraph/ng_socketvar.h>
494cf49a43SJulian Elischer
50821df508SXin LI #include <errno.h>
517b95a1ebSYaroslav Tykhiy #include <stdint.h>
524cf49a43SJulian Elischer #include <stdio.h>
53ade9ccfeSMarcel Moolenaar #include <stdbool.h>
544cf49a43SJulian Elischer #include <string.h>
55821df508SXin LI #include <unistd.h>
56ade9ccfeSMarcel Moolenaar #include <libxo/xo.h>
574cf49a43SJulian Elischer #include "netstat.h"
584cf49a43SJulian Elischer
594cf49a43SJulian Elischer static int first = 1;
604cf49a43SJulian Elischer static int csock = -1;
614cf49a43SJulian Elischer
624cf49a43SJulian Elischer void
netgraphprotopr(u_long off,const char * name,int af1 __unused,int proto __unused)63feda1a43SJohn Baldwin netgraphprotopr(u_long off, const char *name, int af1 __unused,
64feda1a43SJohn Baldwin int proto __unused)
654cf49a43SJulian Elischer {
664cf49a43SJulian Elischer struct ngpcb *this, *next;
674cf49a43SJulian Elischer struct ngpcb ngpcb;
684cf49a43SJulian Elischer struct socket sockb;
694cf49a43SJulian Elischer int debug = 1;
704cf49a43SJulian Elischer
714cf49a43SJulian Elischer /* If symbol not found, try looking in the KLD module */
724cf49a43SJulian Elischer if (off == 0) {
734cf49a43SJulian Elischer if (debug)
74ade9ccfeSMarcel Moolenaar xo_warnx("Error reading symbols from ng_socket.ko");
754cf49a43SJulian Elischer return;
764cf49a43SJulian Elischer }
774cf49a43SJulian Elischer
784cf49a43SJulian Elischer /* Get pointer to first socket */
794cf49a43SJulian Elischer kread(off, (char *)&this, sizeof(this));
804cf49a43SJulian Elischer
814cf49a43SJulian Elischer /* Get my own socket node */
824cf49a43SJulian Elischer if (csock == -1)
834cf49a43SJulian Elischer NgMkSockNode(NULL, &csock, NULL);
844cf49a43SJulian Elischer
854cf49a43SJulian Elischer for (; this != NULL; this = next) {
864cf49a43SJulian Elischer u_char rbuf[sizeof(struct ng_mesg) + sizeof(struct nodeinfo)];
874cf49a43SJulian Elischer struct ng_mesg *resp = (struct ng_mesg *) rbuf;
884cf49a43SJulian Elischer struct nodeinfo *ni = (struct nodeinfo *) resp->data;
894cf49a43SJulian Elischer char path[64];
904cf49a43SJulian Elischer
914cf49a43SJulian Elischer /* Read in ngpcb structure */
924cf49a43SJulian Elischer kread((u_long)this, (char *)&ngpcb, sizeof(ngpcb));
934d51ef63SPoul-Henning Kamp next = LIST_NEXT(&ngpcb, socks);
944cf49a43SJulian Elischer
954cf49a43SJulian Elischer /* Read in socket structure */
964cf49a43SJulian Elischer kread((u_long)ngpcb.ng_socket, (char *)&sockb, sizeof(sockb));
974cf49a43SJulian Elischer
984cf49a43SJulian Elischer /* Check type of socket */
994cf49a43SJulian Elischer if (strcmp(name, "ctrl") == 0 && ngpcb.type != NG_CONTROL)
1004cf49a43SJulian Elischer continue;
1014cf49a43SJulian Elischer if (strcmp(name, "data") == 0 && ngpcb.type != NG_DATA)
1024cf49a43SJulian Elischer continue;
1034cf49a43SJulian Elischer
1044cf49a43SJulian Elischer /* Do headline */
1054cf49a43SJulian Elischer if (first) {
106ade9ccfeSMarcel Moolenaar xo_emit("{T:Netgraph sockets}\n");
1074cf49a43SJulian Elischer if (Aflag)
108ade9ccfeSMarcel Moolenaar xo_emit("{T:/%-8.8s} ", "PCB");
109ade9ccfeSMarcel Moolenaar xo_emit("{T:/%-5.5s} {T:/%-6.6s} {T:/%-6.6s} "
110ade9ccfeSMarcel Moolenaar "{T:/%-14.14s} {T:/%s}\n",
111ade9ccfeSMarcel Moolenaar "Type", "Recv-Q", "Send-Q", "Node Address",
112ade9ccfeSMarcel Moolenaar "#Hooks");
1134cf49a43SJulian Elischer first = 0;
1144cf49a43SJulian Elischer }
1154cf49a43SJulian Elischer
1164cf49a43SJulian Elischer /* Show socket */
1174cf49a43SJulian Elischer if (Aflag)
118ade9ccfeSMarcel Moolenaar xo_emit("{:address/%8lx} ", (u_long) this);
119ade9ccfeSMarcel Moolenaar xo_emit("{t:name/%-5.5s} {:receive-bytes-waiting/%6u} "
120ade9ccfeSMarcel Moolenaar "{:send-byte-waiting/%6u} ",
1210f9d0a73SGleb Smirnoff name, sockb.so_rcv.sb_ccc, sockb.so_snd.sb_ccc);
1224cf49a43SJulian Elischer
1234cf49a43SJulian Elischer /* Get info on associated node */
1244b2b8a37SGleb Smirnoff if (ngpcb.node_id == 0 || csock == -1)
1254cf49a43SJulian Elischer goto finish;
1264b2b8a37SGleb Smirnoff snprintf(path, sizeof(path), "[%x]:", ngpcb.node_id);
1274cf49a43SJulian Elischer if (NgSendMsg(csock, path,
1284cf49a43SJulian Elischer NGM_GENERIC_COOKIE, NGM_NODEINFO, NULL, 0) < 0)
1294cf49a43SJulian Elischer goto finish;
1304cf49a43SJulian Elischer if (NgRecvMsg(csock, resp, sizeof(rbuf), NULL) < 0)
1314cf49a43SJulian Elischer goto finish;
1324cf49a43SJulian Elischer
1334cf49a43SJulian Elischer /* Display associated node info */
1344cf49a43SJulian Elischer if (*ni->name != '\0')
1354cf49a43SJulian Elischer snprintf(path, sizeof(path), "%s:", ni->name);
136ade9ccfeSMarcel Moolenaar xo_emit("{t:path/%-14.14s} {:hooks/%4d}", path, ni->hooks);
1374cf49a43SJulian Elischer finish:
138ade9ccfeSMarcel Moolenaar xo_emit("\n");
1394cf49a43SJulian Elischer }
1404cf49a43SJulian Elischer }
1414cf49a43SJulian Elischer
142