14cf49a43SJulian Elischer
24cf49a43SJulian Elischer /*
34cf49a43SJulian Elischer * list.c
44cf49a43SJulian Elischer *
54cf49a43SJulian Elischer * Copyright (c) 1996-1999 Whistle Communications, Inc.
64cf49a43SJulian Elischer * All rights reserved.
74cf49a43SJulian Elischer *
84cf49a43SJulian Elischer * Subject to the following obligations and disclaimer of warranty, use and
94cf49a43SJulian Elischer * redistribution of this software, in source or object code forms, with or
104cf49a43SJulian Elischer * without modifications are expressly permitted by Whistle Communications;
114cf49a43SJulian Elischer * provided, however, that:
124cf49a43SJulian Elischer * 1. Any and all reproductions of the source or object code must include the
134cf49a43SJulian Elischer * copyright notice above and the following disclaimer of warranties; and
144cf49a43SJulian Elischer * 2. No rights are granted, in any manner or form, to use Whistle
154cf49a43SJulian Elischer * Communications, Inc. trademarks, including the mark "WHISTLE
164cf49a43SJulian Elischer * COMMUNICATIONS" on advertising, endorsements, or otherwise except as
174cf49a43SJulian Elischer * such appears in the above copyright notice or in the software.
184cf49a43SJulian Elischer *
194cf49a43SJulian Elischer * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
204cf49a43SJulian Elischer * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
214cf49a43SJulian Elischer * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
224cf49a43SJulian Elischer * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
234cf49a43SJulian Elischer * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
244cf49a43SJulian Elischer * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
254cf49a43SJulian Elischer * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
264cf49a43SJulian Elischer * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
274cf49a43SJulian Elischer * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
284cf49a43SJulian Elischer * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
294cf49a43SJulian Elischer * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
304cf49a43SJulian Elischer * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
314cf49a43SJulian Elischer * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
324cf49a43SJulian Elischer * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
334cf49a43SJulian Elischer * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
344cf49a43SJulian Elischer * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
354cf49a43SJulian Elischer * OF SUCH DAMAGE.
364cf49a43SJulian Elischer */
374cf49a43SJulian Elischer
3878cdd8edSGleb Smirnoff #include <err.h>
3978cdd8edSGleb Smirnoff #include <netgraph.h>
4078cdd8edSGleb Smirnoff #include <stdio.h>
4178cdd8edSGleb Smirnoff #include <stdlib.h>
4278cdd8edSGleb Smirnoff #include <unistd.h>
4378cdd8edSGleb Smirnoff
444cf49a43SJulian Elischer #include "ngctl.h"
454cf49a43SJulian Elischer
4662f8299bSRuslan Ermilov #define UNNAMED "<unnamed>"
4762f8299bSRuslan Ermilov
484cf49a43SJulian Elischer static int ListCmd(int ac, char **av);
494cf49a43SJulian Elischer
504cf49a43SJulian Elischer const struct ngcmd list_cmd = {
514cf49a43SJulian Elischer ListCmd,
5262f8299bSRuslan Ermilov "list [-ln]",
534cf49a43SJulian Elischer "Show information about all nodes",
5462f8299bSRuslan Ermilov "The list command shows information about every node that currently"
554cf49a43SJulian Elischer " exists in the netgraph system. The optional -n argument limits"
5662f8299bSRuslan Ermilov " this list to only those nodes with a global name assignment."
5762f8299bSRuslan Ermilov " The optional -l argument provides verbose output that includes"
5862f8299bSRuslan Ermilov " hook information as well.",
59f8307e12SArchie Cobbs { "ls" }
604cf49a43SJulian Elischer };
614cf49a43SJulian Elischer
624cf49a43SJulian Elischer static int
ListCmd(int ac,char ** av)634cf49a43SJulian Elischer ListCmd(int ac, char **av)
644cf49a43SJulian Elischer {
659fe5fadaSRuslan Ermilov struct ng_mesg *resp;
669fe5fadaSRuslan Ermilov struct namelist *nlist;
6762f8299bSRuslan Ermilov struct nodeinfo *ninfo;
6862f8299bSRuslan Ermilov int list_hooks = 0;
694cf49a43SJulian Elischer int named_only = 0;
70723c1c2eSRuslan Ermilov int ch, rtn = CMDRTN_OK;
714cf49a43SJulian Elischer
724cf49a43SJulian Elischer /* Get options */
734cf49a43SJulian Elischer optind = 1;
749c20ad30SKevin Lo while ((ch = getopt(ac, av, "ln")) != -1) {
754cf49a43SJulian Elischer switch (ch) {
7662f8299bSRuslan Ermilov case 'l':
7762f8299bSRuslan Ermilov list_hooks = 1;
7862f8299bSRuslan Ermilov break;
794cf49a43SJulian Elischer case 'n':
804cf49a43SJulian Elischer named_only = 1;
814cf49a43SJulian Elischer break;
824cf49a43SJulian Elischer case '?':
834cf49a43SJulian Elischer default:
844cf49a43SJulian Elischer return (CMDRTN_USAGE);
854cf49a43SJulian Elischer break;
864cf49a43SJulian Elischer }
874cf49a43SJulian Elischer }
884cf49a43SJulian Elischer ac -= optind;
894cf49a43SJulian Elischer av += optind;
904cf49a43SJulian Elischer
914cf49a43SJulian Elischer /* Get arguments */
924cf49a43SJulian Elischer switch (ac) {
934cf49a43SJulian Elischer case 0:
944cf49a43SJulian Elischer break;
954cf49a43SJulian Elischer default:
964cf49a43SJulian Elischer return (CMDRTN_USAGE);
974cf49a43SJulian Elischer }
984cf49a43SJulian Elischer
994cf49a43SJulian Elischer /* Get list of nodes */
1004cf49a43SJulian Elischer if (NgSendMsg(csock, ".", NGM_GENERIC_COOKIE,
1014cf49a43SJulian Elischer named_only ? NGM_LISTNAMES : NGM_LISTNODES, NULL, 0) < 0) {
1024cf49a43SJulian Elischer warn("send msg");
1034cf49a43SJulian Elischer return (CMDRTN_ERROR);
1044cf49a43SJulian Elischer }
1059fe5fadaSRuslan Ermilov if (NgAllocRecvMsg(csock, &resp, NULL) < 0) {
1064cf49a43SJulian Elischer warn("recv msg");
1074cf49a43SJulian Elischer return (CMDRTN_ERROR);
1084cf49a43SJulian Elischer }
1094cf49a43SJulian Elischer
1104cf49a43SJulian Elischer /* Show each node */
1119fe5fadaSRuslan Ermilov nlist = (struct namelist *) resp->data;
1124cf49a43SJulian Elischer printf("There are %d total %snodes:\n",
1134cf49a43SJulian Elischer nlist->numnames, named_only ? "named " : "");
11462f8299bSRuslan Ermilov ninfo = nlist->nodeinfo;
11562f8299bSRuslan Ermilov if (list_hooks) {
1169d901d3bSHartmut Brandt char path[NG_PATHSIZ];
11762f8299bSRuslan Ermilov char *argv[2] = { "show", path };
1184cf49a43SJulian Elischer
11962f8299bSRuslan Ermilov while (nlist->numnames > 0) {
1204cf49a43SJulian Elischer snprintf(path, sizeof(path),
12162f8299bSRuslan Ermilov "[%lx]:", (u_long)ninfo->id);
12262f8299bSRuslan Ermilov if ((rtn = (*show_cmd.func)(2, argv)) != CMDRTN_OK)
1234cf49a43SJulian Elischer break;
12462f8299bSRuslan Ermilov ninfo++;
12562f8299bSRuslan Ermilov nlist->numnames--;
126*bb224798SDevin Teske if (nlist->numnames > 0)
127*bb224798SDevin Teske printf("\n");
12862f8299bSRuslan Ermilov }
12962f8299bSRuslan Ermilov } else {
13062f8299bSRuslan Ermilov while (nlist->numnames > 0) {
13162f8299bSRuslan Ermilov if (!*ninfo->name)
13262f8299bSRuslan Ermilov snprintf(ninfo->name, sizeof(ninfo->name),
13362f8299bSRuslan Ermilov "%s", UNNAMED);
13462f8299bSRuslan Ermilov printf(" Name: %-15s Type: %-15s ID: %08x "
13562f8299bSRuslan Ermilov "Num hooks: %d\n",
13662f8299bSRuslan Ermilov ninfo->name, ninfo->type, ninfo->id, ninfo->hooks);
13762f8299bSRuslan Ermilov ninfo++;
13862f8299bSRuslan Ermilov nlist->numnames--;
13962f8299bSRuslan Ermilov }
1404cf49a43SJulian Elischer }
1414cf49a43SJulian Elischer
1424cf49a43SJulian Elischer /* Done */
1439fe5fadaSRuslan Ermilov free(resp);
1444cf49a43SJulian Elischer return (rtn);
1454cf49a43SJulian Elischer }
1464cf49a43SJulian Elischer
147