list.c revision 125011
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: head/usr.sbin/ngctl/list.c 125011 2004-01-26 10:27:18Z ru $
3852419Sjulian */
3952419Sjulian
4052419Sjulian#include "ngctl.h"
4152419Sjulian
4252419Sjulianstatic int ListCmd(int ac, char **av);
4352419Sjulian
4452419Sjulianconst struct ngcmd list_cmd = {
4552419Sjulian	ListCmd,
4652419Sjulian	"list [-n]",
4752419Sjulian	"Show information about all nodes",
4852419Sjulian	"The list command shows information every node that currently"
4952419Sjulian	" exists in the netgraph system. The optional -n argument limits"
5053913Sarchie	" this list to only those nodes with a global name assignment.",
5153913Sarchie	{ "ls" }
5252419Sjulian};
5352419Sjulian
5452419Sjulianstatic int
5552419SjulianListCmd(int ac, char **av)
5652419Sjulian{
5752419Sjulian	u_char rbuf[16 * 1024];
5852419Sjulian	struct ng_mesg *const resp = (struct ng_mesg *) rbuf;
5952419Sjulian	struct namelist *const nlist = (struct namelist *) resp->data;
6052419Sjulian	int named_only = 0;
61125011Sru	int ch, rtn = CMDRTN_OK;
62125011Sru	u_int k;
6352419Sjulian
6452419Sjulian	/* Get options */
6552419Sjulian	optind = 1;
6652419Sjulian	while ((ch = getopt(ac, av, "n")) != EOF) {
6752419Sjulian		switch (ch) {
6852419Sjulian		case 'n':
6952419Sjulian			named_only = 1;
7052419Sjulian			break;
7152419Sjulian		case '?':
7252419Sjulian		default:
7352419Sjulian			return(CMDRTN_USAGE);
7452419Sjulian			break;
7552419Sjulian		}
7652419Sjulian	}
7752419Sjulian	ac -= optind;
7852419Sjulian	av += optind;
7952419Sjulian
8052419Sjulian	/* Get arguments */
8152419Sjulian	switch (ac) {
8252419Sjulian	case 0:
8352419Sjulian		break;
8452419Sjulian	default:
8552419Sjulian		return(CMDRTN_USAGE);
8652419Sjulian	}
8752419Sjulian
8852419Sjulian	/* Get list of nodes */
8952419Sjulian	if (NgSendMsg(csock, ".", NGM_GENERIC_COOKIE,
9052419Sjulian	    named_only ? NGM_LISTNAMES : NGM_LISTNODES, NULL, 0) < 0) {
9152419Sjulian		warn("send msg");
9252419Sjulian		return(CMDRTN_ERROR);
9352419Sjulian	}
9452419Sjulian	if (NgRecvMsg(csock, resp, sizeof(rbuf), NULL) < 0) {
9552419Sjulian		warn("recv msg");
9652419Sjulian		return(CMDRTN_ERROR);
9752419Sjulian	}
9852419Sjulian
9952419Sjulian	/* Show each node */
10052419Sjulian	printf("There are %d total %snodes:\n",
10152419Sjulian	    nlist->numnames, named_only ? "named " : "");
10252419Sjulian	for (k = 0; k < nlist->numnames; k++) {
103122556Sharti		char	path[NG_PATHSIZ];
104125011Sru		char	*argv[3] = { "list", "-n", path };
10552419Sjulian
10652419Sjulian		snprintf(path, sizeof(path),
10752419Sjulian		    "[%lx]:", (u_long) nlist->nodeinfo[k].id);
108125011Sru		if ((rtn = (*show_cmd.func)(3, argv)) != CMDRTN_OK)
10952419Sjulian			break;
11052419Sjulian	}
11152419Sjulian
11252419Sjulian	/* Done */
11352419Sjulian	return (rtn);
11452419Sjulian}
11552419Sjulian
116