152419Sjulian
252419Sjulian/*
352419Sjulian * show.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
4852419Sjulian#define FMT		"  %-15s %-15s %-12s %-15s %-15s\n"
4952419Sjulian#define UNNAMED		"<unnamed>"
5052419Sjulian#define NOSTATUS	"<no status>"
5152419Sjulian
5252419Sjulianstatic int ShowCmd(int ac, char **av);
5352419Sjulian
5452419Sjulianconst struct ngcmd show_cmd = {
5552419Sjulian	ShowCmd,
5652419Sjulian	"show [-n] <path>",
5752419Sjulian	"Show information about the node at <path>",
5853913Sarchie	"If the -n flag is given, hooks are not listed.",
5953913Sarchie	{ "inquire", "info" }
6052419Sjulian};
6152419Sjulian
6252419Sjulianstatic int
6352419SjulianShowCmd(int ac, char **av)
6452419Sjulian{
6552419Sjulian	char *path;
66125115Sru	struct ng_mesg *resp;
67125115Sru	struct hooklist *hlist;
68125115Sru	struct nodeinfo *ninfo;
6952419Sjulian	int ch, no_hooks = 0;
7052419Sjulian
7152419Sjulian	/* Get options */
7252419Sjulian	optind = 1;
73166529Skevlo	while ((ch = getopt(ac, av, "n")) != -1) {
7452419Sjulian		switch (ch) {
7552419Sjulian		case 'n':
7652419Sjulian			no_hooks = 1;
7752419Sjulian			break;
7852419Sjulian		case '?':
7952419Sjulian		default:
80160002Sglebius			return (CMDRTN_USAGE);
8152419Sjulian			break;
8252419Sjulian		}
8352419Sjulian	}
8452419Sjulian	ac -= optind;
8552419Sjulian	av += optind;
8652419Sjulian
8752419Sjulian	/* Get arguments */
8852419Sjulian	switch (ac) {
8952419Sjulian	case 1:
9052419Sjulian		path = av[0];
9152419Sjulian		break;
9252419Sjulian	default:
93160002Sglebius		return (CMDRTN_USAGE);
9452419Sjulian	}
9552419Sjulian
9652419Sjulian	/* Get node info and hook list */
9752419Sjulian	if (NgSendMsg(csock, path, NGM_GENERIC_COOKIE,
9852419Sjulian	    NGM_LISTHOOKS, NULL, 0) < 0) {
9952419Sjulian		warn("send msg");
100160002Sglebius		return (CMDRTN_ERROR);
10152419Sjulian	}
102125115Sru	if (NgAllocRecvMsg(csock, &resp, NULL) < 0) {
10352419Sjulian		warn("recv msg");
104160002Sglebius		return (CMDRTN_ERROR);
10552419Sjulian	}
10652419Sjulian
10752419Sjulian	/* Show node information */
108125115Sru	hlist = (struct hooklist *) resp->data;
109125115Sru	ninfo = &hlist->nodeinfo;
11052419Sjulian	if (!*ninfo->name)
11152419Sjulian		snprintf(ninfo->name, sizeof(ninfo->name), "%s", UNNAMED);
11252419Sjulian	printf("  Name: %-15s Type: %-15s ID: %08x   Num hooks: %d\n",
11352419Sjulian	    ninfo->name, ninfo->type, ninfo->id, ninfo->hooks);
11452419Sjulian	if (!no_hooks && ninfo->hooks > 0) {
115125011Sru		u_int k;
11652419Sjulian
11752419Sjulian		printf(FMT, "Local hook", "Peer name",
11852419Sjulian		    "Peer type", "Peer ID", "Peer hook");
11952419Sjulian		printf(FMT, "----------", "---------",
12052419Sjulian		    "---------", "-------", "---------");
12152419Sjulian		for (k = 0; k < ninfo->hooks; k++) {
12252419Sjulian			struct linkinfo *const link = &hlist->link[k];
12352419Sjulian			struct nodeinfo *const peer = &hlist->link[k].nodeinfo;
12452419Sjulian			char idbuf[20];
12552419Sjulian
12652419Sjulian			if (!*peer->name) {
12752419Sjulian				snprintf(peer->name, sizeof(peer->name),
12852419Sjulian				  "%s", UNNAMED);
12952419Sjulian			}
13052419Sjulian			snprintf(idbuf, sizeof(idbuf), "%08x", peer->id);
13152419Sjulian			printf(FMT, link->ourhook, peer->name,
13252419Sjulian			    peer->type, idbuf, link->peerhook);
13352419Sjulian		}
13452419Sjulian	}
135125115Sru	free(resp);
136160002Sglebius	return (CMDRTN_OK);
13752419Sjulian}
13852419Sjulian
13952419Sjulian
140