152419Sjulian
252419Sjulian/*
352419Sjulian * list.c
452419Sjulian *
552419Sjulian * Copyright (c) 1996-1999 Whistle Communications, Inc.
652419Sjulian * All rights reserved.
752419Sjulian *
852419Sjulian * Subject to the following obligations and disclaimer of warranty, use and
952419Sjulian * redistribution of this software, in source or object code forms, with or
1052419Sjulian * without modifications are expressly permitted by Whistle Communications;
1152419Sjulian * provided, however, that:
1252419Sjulian * 1. Any and all reproductions of the source or object code must include the
1352419Sjulian *    copyright notice above and the following disclaimer of warranties; and
1452419Sjulian * 2. No rights are granted, in any manner or form, to use Whistle
1552419Sjulian *    Communications, Inc. trademarks, including the mark "WHISTLE
1652419Sjulian *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
1752419Sjulian *    such appears in the above copyright notice or in the software.
1852419Sjulian *
1952419Sjulian * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
2052419Sjulian * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
2152419Sjulian * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
2252419Sjulian * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
2352419Sjulian * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
2452419Sjulian * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
2552419Sjulian * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
2652419Sjulian * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
2752419Sjulian * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
2852419Sjulian * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
2952419Sjulian * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
3052419Sjulian * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
3152419Sjulian * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
3252419Sjulian * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3352419Sjulian * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
3452419Sjulian * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
3552419Sjulian * OF SUCH DAMAGE.
3652419Sjulian *
3752419Sjulian * $FreeBSD$
3852419Sjulian */
3952419Sjulian
40158882Sglebius#include <err.h>
41158882Sglebius#include <netgraph.h>
42158882Sglebius#include <stdio.h>
43158882Sglebius#include <stdlib.h>
44158882Sglebius#include <unistd.h>
45158882Sglebius
4652419Sjulian#include "ngctl.h"
4752419Sjulian
48141575Sru#define UNNAMED		"<unnamed>"
49141575Sru
5052419Sjulianstatic int ListCmd(int ac, char **av);
5152419Sjulian
5252419Sjulianconst struct ngcmd list_cmd = {
5352419Sjulian	ListCmd,
54141575Sru	"list [-ln]",
5552419Sjulian	"Show information about all nodes",
56141575Sru	"The list command shows information about every node that currently"
5752419Sjulian	" exists in the netgraph system. The optional -n argument limits"
58141575Sru	" this list to only those nodes with a global name assignment."
59141575Sru	" The optional -l argument provides verbose output that includes"
60141575Sru	" hook information as well.",
6153913Sarchie	{ "ls" }
6252419Sjulian};
6352419Sjulian
6452419Sjulianstatic int
6552419SjulianListCmd(int ac, char **av)
6652419Sjulian{
67125115Sru	struct ng_mesg *resp;
68125115Sru	struct namelist *nlist;
69141575Sru	struct nodeinfo *ninfo;
70141575Sru	int list_hooks = 0;
7152419Sjulian	int named_only = 0;
72125011Sru	int ch, rtn = CMDRTN_OK;
7352419Sjulian
7452419Sjulian	/* Get options */
7552419Sjulian	optind = 1;
76166529Skevlo	while ((ch = getopt(ac, av, "ln")) != -1) {
7752419Sjulian		switch (ch) {
78141575Sru		case 'l':
79141575Sru			list_hooks = 1;
80141575Sru			break;
8152419Sjulian		case 'n':
8252419Sjulian			named_only = 1;
8352419Sjulian			break;
8452419Sjulian		case '?':
8552419Sjulian		default:
86160002Sglebius			return (CMDRTN_USAGE);
8752419Sjulian			break;
8852419Sjulian		}
8952419Sjulian	}
9052419Sjulian	ac -= optind;
9152419Sjulian	av += optind;
9252419Sjulian
9352419Sjulian	/* Get arguments */
9452419Sjulian	switch (ac) {
9552419Sjulian	case 0:
9652419Sjulian		break;
9752419Sjulian	default:
98160002Sglebius		return (CMDRTN_USAGE);
9952419Sjulian	}
10052419Sjulian
10152419Sjulian	/* Get list of nodes */
10252419Sjulian	if (NgSendMsg(csock, ".", NGM_GENERIC_COOKIE,
10352419Sjulian	    named_only ? NGM_LISTNAMES : NGM_LISTNODES, NULL, 0) < 0) {
10452419Sjulian		warn("send msg");
105160002Sglebius		return (CMDRTN_ERROR);
10652419Sjulian	}
107125115Sru	if (NgAllocRecvMsg(csock, &resp, NULL) < 0) {
10852419Sjulian		warn("recv msg");
109160002Sglebius		return (CMDRTN_ERROR);
11052419Sjulian	}
11152419Sjulian
11252419Sjulian	/* Show each node */
113125115Sru	nlist = (struct namelist *) resp->data;
11452419Sjulian	printf("There are %d total %snodes:\n",
11552419Sjulian	    nlist->numnames, named_only ? "named " : "");
116141575Sru	ninfo = nlist->nodeinfo;
117141575Sru	if (list_hooks) {
118122556Sharti		char	path[NG_PATHSIZ];
119141575Sru		char	*argv[2] = { "show", path };
12052419Sjulian
121141575Sru		while (nlist->numnames > 0) {
122141575Sru			snprintf(path, sizeof(path),
123141575Sru			    "[%lx]:", (u_long)ninfo->id);
124141575Sru			if ((rtn = (*show_cmd.func)(2, argv)) != CMDRTN_OK)
125141575Sru				break;
126141575Sru			ninfo++;
127141575Sru			nlist->numnames--;
128141575Sru		}
129141575Sru	} else {
130141575Sru		while (nlist->numnames > 0) {
131141575Sru			if (!*ninfo->name)
132141575Sru				snprintf(ninfo->name, sizeof(ninfo->name),
133141575Sru				    "%s", UNNAMED);
134141575Sru			printf("  Name: %-15s Type: %-15s ID: %08x   "
135141575Sru			    "Num hooks: %d\n",
136141575Sru			    ninfo->name, ninfo->type, ninfo->id, ninfo->hooks);
137141575Sru			ninfo++;
138141575Sru			nlist->numnames--;
139141575Sru		}
14052419Sjulian	}
14152419Sjulian
14252419Sjulian	/* Done */
143125115Sru	free(resp);
14452419Sjulian	return (rtn);
14552419Sjulian}
14652419Sjulian
147