xref: /src/usr.sbin/ngctl/show.c (revision 3cbdcabf714d5187e22d8ff1cbfbc261dc8622eb)
1 /*
2  * show.c
3  *
4  * Copyright (c) 1996-1999 Whistle Communications, Inc.
5  * All rights reserved.
6  *
7  * Subject to the following obligations and disclaimer of warranty, use and
8  * redistribution of this software, in source or object code forms, with or
9  * without modifications are expressly permitted by Whistle Communications;
10  * provided, however, that:
11  * 1. Any and all reproductions of the source or object code must include the
12  *    copyright notice above and the following disclaimer of warranties; and
13  * 2. No rights are granted, in any manner or form, to use Whistle
14  *    Communications, Inc. trademarks, including the mark "WHISTLE
15  *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
16  *    such appears in the above copyright notice or in the software.
17  *
18  * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
19  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
20  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
21  * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
22  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
23  * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
24  * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
25  * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
26  * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
27  * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
28  * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
29  * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
30  * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
31  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33  * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
34  * OF SUCH DAMAGE.
35  */
36 
37 #include <err.h>
38 #include <netgraph.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <unistd.h>
42 
43 #include "ngctl.h"
44 
45 #define FMT		"  %-15s %-15s %-12s %-15s %-15s\n"
46 #define UNNAMED		"<unnamed>"
47 #define NOSTATUS	"<no status>"
48 
49 static int ShowCmd(int ac, char **av);
50 
51 const struct ngcmd show_cmd = {
52 	ShowCmd,
53 	"show [-n] <path>",
54 	"Show information about the node at <path>",
55 	"If the -n flag is given, hooks are not listed.",
56 	{ "inquire", "info" }
57 };
58 
59 static int
ShowCmd(int ac,char ** av)60 ShowCmd(int ac, char **av)
61 {
62 	char *path;
63 	struct ng_mesg *resp;
64 	struct hooklist *hlist;
65 	struct nodeinfo *ninfo;
66 	int ch, no_hooks = 0;
67 
68 	/* Get options */
69 	optreset = 1;
70 	optind = 1;
71 	while ((ch = getopt(ac, av, "n")) != -1) {
72 		switch (ch) {
73 		case 'n':
74 			no_hooks = 1;
75 			break;
76 		default:
77 			return (CMDRTN_USAGE);
78 			break;
79 		}
80 	}
81 	ac -= optind;
82 	av += optind;
83 
84 	/* Get arguments */
85 	switch (ac) {
86 	case 1:
87 		path = av[0];
88 		break;
89 	default:
90 		return (CMDRTN_USAGE);
91 	}
92 
93 	/* Get node info and hook list */
94 	if (NgSendMsg(csock, path, NGM_GENERIC_COOKIE,
95 	    NGM_LISTHOOKS, NULL, 0) < 0) {
96 		warn("send msg");
97 		return (CMDRTN_ERROR);
98 	}
99 	if (NgAllocRecvMsg(csock, &resp, NULL) < 0) {
100 		warn("recv msg");
101 		return (CMDRTN_ERROR);
102 	}
103 
104 	/* Show node information */
105 	hlist = (struct hooklist *) resp->data;
106 	ninfo = &hlist->nodeinfo;
107 	if (!*ninfo->name)
108 		snprintf(ninfo->name, sizeof(ninfo->name), "%s", UNNAMED);
109 	printf("  Name: %-15s Type: %-15s ID: %08x   Num hooks: %d\n",
110 	    ninfo->name, ninfo->type, ninfo->id, ninfo->hooks);
111 	if (!no_hooks && ninfo->hooks > 0) {
112 		u_int k;
113 
114 		printf(FMT, "Local hook", "Peer name",
115 		    "Peer type", "Peer ID", "Peer hook");
116 		printf(FMT, "----------", "---------",
117 		    "---------", "-------", "---------");
118 		for (k = 0; k < ninfo->hooks; k++) {
119 			struct linkinfo *const link = &hlist->link[k];
120 			struct nodeinfo *const peer = &hlist->link[k].nodeinfo;
121 			char idbuf[20];
122 
123 			if (!*peer->name) {
124 				snprintf(peer->name, sizeof(peer->name),
125 				  "%s", UNNAMED);
126 			}
127 			snprintf(idbuf, sizeof(idbuf), "%08x", peer->id);
128 			printf(FMT, link->ourhook, peer->name,
129 			    peer->type, idbuf, link->peerhook);
130 		}
131 	}
132 	free(resp);
133 	return (CMDRTN_OK);
134 }
135