1176771Sraj
2192532Sraj/*
3176771Sraj * show.c
4176771Sraj *
5176771Sraj * Copyright (c) 1996-1999 Whistle Communications, Inc.
6176771Sraj * All rights reserved.
7176771Sraj *
8176771Sraj * Subject to the following obligations and disclaimer of warranty, use and
9176771Sraj * redistribution of this software, in source or object code forms, with or
10176771Sraj * without modifications are expressly permitted by Whistle Communications;
11176771Sraj * provided, however, that:
12176771Sraj * 1. Any and all reproductions of the source or object code must include the
13176771Sraj *    copyright notice above and the following disclaimer of warranties; and
14176771Sraj * 2. No rights are granted, in any manner or form, to use Whistle
15176771Sraj *    Communications, Inc. trademarks, including the mark "WHISTLE
16176771Sraj *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
17176771Sraj *    such appears in the above copyright notice or in the software.
18176771Sraj *
19176771Sraj * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
20176771Sraj * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
21176771Sraj * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
22176771Sraj * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
23176771Sraj * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
24176771Sraj * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
25176771Sraj * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
26176771Sraj * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
27176771Sraj * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
28176771Sraj * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
29176771Sraj * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
30176771Sraj * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
31176771Sraj * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
32176771Sraj * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33176771Sraj * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34176771Sraj * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
35176771Sraj * OF SUCH DAMAGE.
36176771Sraj *
37176771Sraj * $FreeBSD$
38176771Sraj */
39187151Sraj
40187151Sraj#include <err.h>
41187151Sraj#include <netgraph.h>
42190701Smarcel#include <stdio.h>
43187151Sraj#include <stdlib.h>
44187151Sraj#include <unistd.h>
45187151Sraj
46187151Sraj#include "ngctl.h"
47187151Sraj
48187151Sraj#define FMT		"  %-15s %-15s %-12s %-15s %-15s\n"
49176771Sraj#define UNNAMED		"<unnamed>"
50176771Sraj#define NOSTATUS	"<no status>"
51176771Sraj
52176771Srajstatic int ShowCmd(int ac, char **av);
53176771Sraj
54176771Srajconst struct ngcmd show_cmd = {
55176771Sraj	ShowCmd,
56187149Sraj	"show [-n] <path>",
57176771Sraj	"Show information about the node at <path>",
58176771Sraj	"If the -n flag is given, hooks are not listed.",
59176771Sraj	{ "inquire", "info" }
60176771Sraj};
61176771Sraj
62224611Smarcelstatic int
63176771SrajShowCmd(int ac, char **av)
64176771Sraj{
65176771Sraj	char *path;
66242535Salc	struct ng_mesg *resp;
67222813Sattilio	struct hooklist *hlist;
68192532Sraj	struct nodeinfo *ninfo;
69176771Sraj	int ch, no_hooks = 0;
70176771Sraj
71176771Sraj	/* Get options */
72176771Sraj	optind = 1;
73176771Sraj	while ((ch = getopt(ac, av, "n")) != -1) {
74176771Sraj		switch (ch) {
75176771Sraj		case 'n':
76176771Sraj			no_hooks = 1;
77176771Sraj			break;
78176771Sraj		case '?':
79176771Sraj		default:
80176771Sraj			return (CMDRTN_USAGE);
81176771Sraj			break;
82176771Sraj		}
83176771Sraj	}
84192067Snwhitehorn	ac -= optind;
85176771Sraj	av += optind;
86176771Sraj
87176771Sraj	/* Get arguments */
88176771Sraj	switch (ac) {
89176771Sraj	case 1:
90176771Sraj		path = av[0];
91176771Sraj		break;
92176771Sraj	default:
93176771Sraj		return (CMDRTN_USAGE);
94176771Sraj	}
95176771Sraj
96176771Sraj	/* Get node info and hook list */
97176771Sraj	if (NgSendMsg(csock, path, NGM_GENERIC_COOKIE,
98176771Sraj	    NGM_LISTHOOKS, NULL, 0) < 0) {
99176771Sraj		warn("send msg");
100176771Sraj		return (CMDRTN_ERROR);
101176771Sraj	}
102176771Sraj	if (NgAllocRecvMsg(csock, &resp, NULL) < 0) {
103190701Smarcel		warn("recv msg");
104190701Smarcel		return (CMDRTN_ERROR);
105190701Smarcel	}
106190701Smarcel
107190701Smarcel	/* Show node information */
108224611Smarcel	hlist = (struct hooklist *) resp->data;
109224611Smarcel	ninfo = &hlist->nodeinfo;
110224611Smarcel	if (!*ninfo->name)
111242526Smarcel		snprintf(ninfo->name, sizeof(ninfo->name), "%s", UNNAMED);
112224611Smarcel	printf("  Name: %-15s Type: %-15s ID: %08x   Num hooks: %d\n",
113224611Smarcel	    ninfo->name, ninfo->type, ninfo->id, ninfo->hooks);
114224611Smarcel	if (!no_hooks && ninfo->hooks > 0) {
115190701Smarcel		u_int k;
116190701Smarcel
117176771Sraj		printf(FMT, "Local hook", "Peer name",
118190701Smarcel		    "Peer type", "Peer ID", "Peer hook");
119190701Smarcel		printf(FMT, "----------", "---------",
120190701Smarcel		    "---------", "-------", "---------");
121190701Smarcel		for (k = 0; k < ninfo->hooks; k++) {
122192067Snwhitehorn			struct linkinfo *const link = &hlist->link[k];
123192067Snwhitehorn			struct nodeinfo *const peer = &hlist->link[k].nodeinfo;
124192067Snwhitehorn			char idbuf[20];
125192067Snwhitehorn
126192067Snwhitehorn			if (!*peer->name) {
127176771Sraj				snprintf(peer->name, sizeof(peer->name),
128176771Sraj				  "%s", UNNAMED);
129176771Sraj			}
130176771Sraj			snprintf(idbuf, sizeof(idbuf), "%08x", peer->id);
131176771Sraj			printf(FMT, link->ourhook, peer->name,
132187149Sraj			    peer->type, idbuf, link->peerhook);
133187149Sraj		}
134176771Sraj	}
135176771Sraj	free(resp);
136176771Sraj	return (CMDRTN_OK);
137176771Sraj}
138176771Sraj
139176771Sraj
140176771Sraj