show.c revision 158882
193139Sru 2146515Sru/* 321495Sjmacd * show.c 4146515Sru * 521495Sjmacd * Copyright (c) 1996-1999 Whistle Communications, Inc. 621495Sjmacd * All rights reserved. 721495Sjmacd * 821495Sjmacd * Subject to the following obligations and disclaimer of warranty, use and 921495Sjmacd * redistribution of this software, in source or object code forms, with or 1021495Sjmacd * without modifications are expressly permitted by Whistle Communications; 1121495Sjmacd * provided, however, that: 1221495Sjmacd * 1. Any and all reproductions of the source or object code must include the 1321495Sjmacd * copyright notice above and the following disclaimer of warranties; and 1421495Sjmacd * 2. No rights are granted, in any manner or form, to use Whistle 1521495Sjmacd * Communications, Inc. trademarks, including the mark "WHISTLE 1621495Sjmacd * COMMUNICATIONS" on advertising, endorsements, or otherwise except as 1721495Sjmacd * such appears in the above copyright notice or in the software. 1821495Sjmacd * 1921495Sjmacd * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND 2021495Sjmacd * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO 2121495Sjmacd * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, 2242660Smarkm * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF 2342660Smarkm * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. 2421495Sjmacd * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY 2542660Smarkm * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS 2621495Sjmacd * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE. 2721495Sjmacd * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES 2821495Sjmacd * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING 2942660Smarkm * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 3021495Sjmacd * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR 3121495Sjmacd * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY 3221495Sjmacd * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 3321495Sjmacd * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 3421495Sjmacd * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY 3521495Sjmacd * OF SUCH DAMAGE. 3621495Sjmacd * 3721495Sjmacd * $FreeBSD: head/usr.sbin/ngctl/show.c 158882 2006-05-24 14:46:55Z glebius $ 3821495Sjmacd */ 3921495Sjmacd 4021495Sjmacd#include <err.h> 4121495Sjmacd#include <netgraph.h> 4221495Sjmacd#include <stdio.h> 4321495Sjmacd#include <stdlib.h> 4421495Sjmacd#include <unistd.h> 4521495Sjmacd 4621495Sjmacd#include "ngctl.h" 4721495Sjmacd 4821495Sjmacd#define FMT " %-15s %-15s %-12s %-15s %-15s\n" 4921495Sjmacd#define UNNAMED "<unnamed>" 5021495Sjmacd#define NOSTATUS "<no status>" 5121495Sjmacd 5221495Sjmacdstatic int ShowCmd(int ac, char **av); 5393139Sru 5493139Sruconst struct ngcmd show_cmd = { 5521495Sjmacd ShowCmd, 5693139Sru "show [-n] <path>", 5721495Sjmacd "Show information about the node at <path>", 5821495Sjmacd "If the -n flag is given, hooks are not listed.", 5921495Sjmacd { "inquire", "info" } 6021495Sjmacd}; 6121495Sjmacd 6221495Sjmacdstatic int 6321495SjmacdShowCmd(int ac, char **av) 6421495Sjmacd{ 6521495Sjmacd char *path; 6621495Sjmacd struct ng_mesg *resp; 6721495Sjmacd struct hooklist *hlist; 6821495Sjmacd struct nodeinfo *ninfo; 6921495Sjmacd int ch, no_hooks = 0; 70146515Sru 7121495Sjmacd /* Get options */ 7221495Sjmacd optind = 1; 73146515Sru while ((ch = getopt(ac, av, "n")) != EOF) { 74146515Sru switch (ch) { 7521495Sjmacd case 'n': 7621495Sjmacd no_hooks = 1; 77146515Sru break; 7821495Sjmacd case '?': 7921495Sjmacd default: 80146515Sru return(CMDRTN_USAGE); 8121495Sjmacd break; 8242660Smarkm } 83 } 84 ac -= optind; 85 av += optind; 86 87 /* Get arguments */ 88 switch (ac) { 89 case 1: 90 path = av[0]; 91 break; 92 default: 93 return(CMDRTN_USAGE); 94 } 95 96 /* Get node info and hook list */ 97 if (NgSendMsg(csock, path, NGM_GENERIC_COOKIE, 98 NGM_LISTHOOKS, NULL, 0) < 0) { 99 warn("send msg"); 100 return(CMDRTN_ERROR); 101 } 102 if (NgAllocRecvMsg(csock, &resp, NULL) < 0) { 103 warn("recv msg"); 104 return(CMDRTN_ERROR); 105 } 106 107 /* Show node information */ 108 hlist = (struct hooklist *) resp->data; 109 ninfo = &hlist->nodeinfo; 110 if (!*ninfo->name) 111 snprintf(ninfo->name, sizeof(ninfo->name), "%s", UNNAMED); 112 printf(" Name: %-15s Type: %-15s ID: %08x Num hooks: %d\n", 113 ninfo->name, ninfo->type, ninfo->id, ninfo->hooks); 114 if (!no_hooks && ninfo->hooks > 0) { 115 u_int k; 116 117 printf(FMT, "Local hook", "Peer name", 118 "Peer type", "Peer ID", "Peer hook"); 119 printf(FMT, "----------", "---------", 120 "---------", "-------", "---------"); 121 for (k = 0; k < ninfo->hooks; k++) { 122 struct linkinfo *const link = &hlist->link[k]; 123 struct nodeinfo *const peer = &hlist->link[k].nodeinfo; 124 char idbuf[20]; 125 126 if (!*peer->name) { 127 snprintf(peer->name, sizeof(peer->name), 128 "%s", UNNAMED); 129 } 130 snprintf(idbuf, sizeof(idbuf), "%08x", peer->id); 131 printf(FMT, link->ourhook, peer->name, 132 peer->type, idbuf, link->peerhook); 133 } 134 } 135 free(resp); 136 return(CMDRTN_OK); 137} 138 139 140