diff options
Diffstat (limited to 'usr.sbin/ngctl')
-rw-r--r-- | usr.sbin/ngctl/Makefile | 27 | ||||
-rw-r--r-- | usr.sbin/ngctl/Makefile.depend | 22 | ||||
-rw-r--r-- | usr.sbin/ngctl/config.c | 111 | ||||
-rw-r--r-- | usr.sbin/ngctl/connect.c | 90 | ||||
-rw-r--r-- | usr.sbin/ngctl/debug.c | 84 | ||||
-rw-r--r-- | usr.sbin/ngctl/dot.c | 200 | ||||
-rw-r--r-- | usr.sbin/ngctl/list.c | 146 | ||||
-rw-r--r-- | usr.sbin/ngctl/main.c | 665 | ||||
-rw-r--r-- | usr.sbin/ngctl/mkpeer.c | 90 | ||||
-rw-r--r-- | usr.sbin/ngctl/msg.c | 165 | ||||
-rw-r--r-- | usr.sbin/ngctl/name.c | 81 | ||||
-rw-r--r-- | usr.sbin/ngctl/ngctl.8 | 141 | ||||
-rw-r--r-- | usr.sbin/ngctl/ngctl.h | 82 | ||||
-rw-r--r-- | usr.sbin/ngctl/rmhook.c | 86 | ||||
-rw-r--r-- | usr.sbin/ngctl/show.c | 139 | ||||
-rw-r--r-- | usr.sbin/ngctl/shutdown.c | 79 | ||||
-rw-r--r-- | usr.sbin/ngctl/status.c | 101 | ||||
-rw-r--r-- | usr.sbin/ngctl/types.c | 101 | ||||
-rw-r--r-- | usr.sbin/ngctl/write.c | 121 |
19 files changed, 2531 insertions, 0 deletions
diff --git a/usr.sbin/ngctl/Makefile b/usr.sbin/ngctl/Makefile new file mode 100644 index 0000000..71b5d2b --- /dev/null +++ b/usr.sbin/ngctl/Makefile @@ -0,0 +1,27 @@ +# $FreeBSD$ +# $Whistle: Makefile,v 1.3 1999/01/16 00:10:11 archie Exp $ + +.include <src.opts.mk> + +PROG= ngctl +MAN= ngctl.8 +SRCS= main.c mkpeer.c config.c connect.c dot.c name.c show.c list.c \ + msg.c debug.c shutdown.c rmhook.c status.c types.c write.c +WARNS?= 3 + +.if defined(RELEASE_CRUNCH) +NGCTL_NO_LIBEDIT= +.endif + +.if ${MK_LIBTHR} == "no" +NGCTL_NO_LIBEDIT= +.endif + +LIBADD= netgraph + +.if !defined(NGCTL_NO_LIBEDIT) +CFLAGS+= -DEDITLINE +LIBADD+= edit pthread +.endif + +.include <bsd.prog.mk> diff --git a/usr.sbin/ngctl/Makefile.depend b/usr.sbin/ngctl/Makefile.depend new file mode 100644 index 0000000..a0a9832 --- /dev/null +++ b/usr.sbin/ngctl/Makefile.depend @@ -0,0 +1,22 @@ +# $FreeBSD$ +# Autogenerated - do NOT edit! + +DIRDEPS = \ + gnu/lib/csu \ + gnu/lib/libgcc \ + include \ + include/xlocale \ + lib/${CSU_DIR} \ + lib/libc \ + lib/libcompiler_rt \ + lib/libedit \ + lib/libnetgraph \ + lib/libthr \ + lib/ncurses/ncursesw \ + + +.include <dirdeps.mk> + +.if ${DEP_RELDIR} == ${_DEP_RELDIR} +# local dependencies - needed for -jN in clean tree +.endif diff --git a/usr.sbin/ngctl/config.c b/usr.sbin/ngctl/config.c new file mode 100644 index 0000000..9b1a275 --- /dev/null +++ b/usr.sbin/ngctl/config.c @@ -0,0 +1,111 @@ +/* + * config.c + * + * Copyright (c) 1996-1999 Whistle Communications, Inc. + * All rights reserved. + * + * Subject to the following obligations and disclaimer of warranty, use and + * redistribution of this software, in source or object code forms, with or + * without modifications are expressly permitted by Whistle Communications; + * provided, however, that: + * 1. Any and all reproductions of the source or object code must include the + * copyright notice above and the following disclaimer of warranties; and + * 2. No rights are granted, in any manner or form, to use Whistle + * Communications, Inc. trademarks, including the mark "WHISTLE + * COMMUNICATIONS" on advertising, endorsements, or otherwise except as + * such appears in the above copyright notice or in the software. + * + * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND + * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO + * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, + * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. + * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY + * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS + * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE. + * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES + * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING + * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, + * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY + * OF SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#include <err.h> +#include <errno.h> +#include <netgraph.h> +#include <stdio.h> +#include <string.h> +#include <unistd.h> + +#include "ngctl.h" + +#define NOCONFIG "<no config>" + +static int ConfigCmd(int ac, char **av); + +const struct ngcmd config_cmd = { + ConfigCmd, + "config <path> [arguments]", + "get or set configuration of node at <path>", + NULL, + { NULL } +}; + +static int +ConfigCmd(int ac, char **av) +{ + u_char sbuf[sizeof(struct ng_mesg) + NG_TEXTRESPONSE]; + struct ng_mesg *const resp = (struct ng_mesg *) sbuf; + char *const status = (char *) resp->data; + char *path; + char buf[NG_TEXTRESPONSE]; + int nostat = 0, i; + + /* Get arguments */ + if (ac < 2) + return (CMDRTN_USAGE); + path = av[1]; + + *buf = '\0'; + for (i = 2; i < ac; i++) { + if (i != 2) + strcat(buf, " "); + strcat(buf, av[i]); + } + + /* Get node config summary */ + if (*buf != '\0') + i = NgSendMsg(csock, path, NGM_GENERIC_COOKIE, + NGM_TEXT_CONFIG, buf, strlen(buf) + 1); + else + i = NgSendMsg(csock, path, NGM_GENERIC_COOKIE, + NGM_TEXT_CONFIG, NULL, 0); + if (i < 0) { + switch (errno) { + case EINVAL: + nostat = 1; + break; + default: + warn("send msg"); + return (CMDRTN_ERROR); + } + } else { + if (NgRecvMsg(csock, resp, sizeof(sbuf), NULL) < 0 + || (resp->header.flags & NGF_RESP) == 0) + nostat = 1; + } + + /* Show it */ + if (nostat) + printf("No config available for \"%s\"\n", path); + else + printf("Config for \"%s\":\n%s\n", path, status); + return (CMDRTN_OK); +} + diff --git a/usr.sbin/ngctl/connect.c b/usr.sbin/ngctl/connect.c new file mode 100644 index 0000000..cd5307e --- /dev/null +++ b/usr.sbin/ngctl/connect.c @@ -0,0 +1,90 @@ + +/* + * connect.c + * + * Copyright (c) 1996-1999 Whistle Communications, Inc. + * All rights reserved. + * + * Subject to the following obligations and disclaimer of warranty, use and + * redistribution of this software, in source or object code forms, with or + * without modifications are expressly permitted by Whistle Communications; + * provided, however, that: + * 1. Any and all reproductions of the source or object code must include the + * copyright notice above and the following disclaimer of warranties; and + * 2. No rights are granted, in any manner or form, to use Whistle + * Communications, Inc. trademarks, including the mark "WHISTLE + * COMMUNICATIONS" on advertising, endorsements, or otherwise except as + * such appears in the above copyright notice or in the software. + * + * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND + * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO + * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, + * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. + * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY + * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS + * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE. + * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES + * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING + * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, + * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY + * OF SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#include <err.h> +#include <netgraph.h> +#include <stdio.h> + +#include "ngctl.h" + +static int ConnectCmd(int ac, char **av); + +const struct ngcmd connect_cmd = { + ConnectCmd, + "connect [path] <relpath> <hook> <peerhook>", + "Connects hook <peerhook> of the node at <relpath> to <hook>", + "The connect command creates a link between the two nodes at" + " \"path\" and \"relpath\" using hooks \"hook\" and \"peerhook\"," + " respectively. The \"relpath\", if not absolute, is specified" + " relative to the node at \"path\"." + " If \"path\" is omitted then \".\" is assumed.", + { "join" } +}; + +static int +ConnectCmd(int ac, char **av) +{ + struct ngm_connect con; + const char *path = "."; + + /* Get arguments */ + switch (ac) { + case 5: + path = av[1]; + ac--; + av++; + /* FALLTHROUGH */ + case 4: + snprintf(con.path, sizeof(con.path), "%s", av[1]); + snprintf(con.ourhook, sizeof(con.ourhook), "%s", av[2]); + snprintf(con.peerhook, sizeof(con.peerhook), "%s", av[3]); + break; + default: + return (CMDRTN_USAGE); + } + + /* Send message */ + if (NgSendMsg(csock, path, NGM_GENERIC_COOKIE, + NGM_CONNECT, &con, sizeof(con)) < 0) { + warn("send msg"); + return (CMDRTN_ERROR); + } + return (CMDRTN_OK); +} + diff --git a/usr.sbin/ngctl/debug.c b/usr.sbin/ngctl/debug.c new file mode 100644 index 0000000..d64e32d --- /dev/null +++ b/usr.sbin/ngctl/debug.c @@ -0,0 +1,84 @@ + +/* + * debug.c + * + * Copyright (c) 1996-1999 Whistle Communications, Inc. + * All rights reserved. + * + * Subject to the following obligations and disclaimer of warranty, use and + * redistribution of this software, in source or object code forms, with or + * without modifications are expressly permitted by Whistle Communications; + * provided, however, that: + * 1. Any and all reproductions of the source or object code must include the + * copyright notice above and the following disclaimer of warranties; and + * 2. No rights are granted, in any manner or form, to use Whistle + * Communications, Inc. trademarks, including the mark "WHISTLE + * COMMUNICATIONS" on advertising, endorsements, or otherwise except as + * such appears in the above copyright notice or in the software. + * + * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND + * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO + * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, + * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. + * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY + * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS + * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE. + * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES + * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING + * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, + * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY + * OF SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#include <netgraph.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "ngctl.h" + +static int DebugCmd(int ac, char **av); + +const struct ngcmd debug_cmd = { + DebugCmd, + "debug [level]", + "Get/set debugging verbosity level", + "Without any argument, this command displays the current" + " debugging verbosity level. If the argument is ``+'' or ``-''" + " the debug level is incremented or decremented; otherwise," + " it must be an absolute numerical level.", + { NULL } +}; + +static int +DebugCmd(int ac, char **av) +{ + int level; + + /* Get arguments */ + switch (ac) { + case 2: + if (!strcmp(av[1], "+")) + level = NgSetDebug(-1) + 1; + else if (!strcmp(av[1], "-")) + level = NgSetDebug(-1) - 1; + else if ((level = atoi(av[1])) < 0) + return (CMDRTN_USAGE); + NgSetDebug(level); + break; + case 1: + printf("Current debug level is %d\n", NgSetDebug(-1)); + break; + default: + return (CMDRTN_USAGE); + } + return (CMDRTN_OK); +} + diff --git a/usr.sbin/ngctl/dot.c b/usr.sbin/ngctl/dot.c new file mode 100644 index 0000000..045101a --- /dev/null +++ b/usr.sbin/ngctl/dot.c @@ -0,0 +1,200 @@ + +/* + * dot.c + * + * Copyright (c) 2004 Brian Fundakowski Feldman + * Copyright (c) 1996-1999 Whistle Communications, Inc. + * All rights reserved. + * + * Subject to the following obligations and disclaimer of warranty, use and + * redistribution of this software, in source or object code forms, with or + * without modifications are expressly permitted by Whistle Communications; + * provided, however, that: + * 1. Any and all reproductions of the source or object code must include the + * copyright notice above and the following disclaimer of warranties; and + * 2. No rights are granted, in any manner or form, to use Whistle + * Communications, Inc. trademarks, including the mark "WHISTLE + * COMMUNICATIONS" on advertising, endorsements, or otherwise except as + * such appears in the above copyright notice or in the software. + * + * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND + * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO + * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, + * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. + * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY + * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS + * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE. + * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES + * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING + * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, + * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY + * OF SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#include <err.h> +#include <inttypes.h> +#include <netgraph.h> +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> + +#include "ngctl.h" + +#define UNNAMED "\\<unnamed\\>" + +static int DotCmd(int ac, char **av); + +const struct ngcmd dot_cmd = { + DotCmd, + "dot [outputfile]", + "Produce a GraphViz (.dot) of the entire netgraph.", + "If no outputfile is specified, stdout will be assumed.", + { "graphviz", "confdot" } +}; + +static int +DotCmd(int ac, char **av) +{ + struct ng_mesg *nlresp; + struct namelist *nlist; + FILE *f = stdout; + int ch; + u_int i; + + /* Get options */ + optind = 1; + while ((ch = getopt(ac, av, "")) != -1) { + switch (ch) { + case '?': + default: + return (CMDRTN_USAGE); + break; + } + } + ac -= optind; + av += optind; + + /* Get arguments */ + switch (ac) { + case 1: + f = fopen(av[0], "w"); + if (f == NULL) { + warn("Could not open %s for writing", av[0]); + return (CMDRTN_ERROR); + } + case 0: + break; + default: + if (f != stdout) + (void)fclose(f); + return (CMDRTN_USAGE); + } + + /* Get list of nodes */ + if (NgSendMsg(csock, ".", NGM_GENERIC_COOKIE, NGM_LISTNODES, NULL, + 0) < 0) { + warn("send listnodes msg"); + goto error; + } + if (NgAllocRecvMsg(csock, &nlresp, NULL) < 0) { + warn("recv listnodes msg"); + goto error; + } + + nlist = (struct namelist *)nlresp->data; + fprintf(f, "graph netgraph {\n"); + /* TODO: implement rank = same or subgraphs at some point */ + fprintf(f, "\tedge [ weight = 1.0 ];\n"); + fprintf(f, "\tnode [ shape = record, fontsize = 12 ] {\n"); + for (i = 0; i < nlist->numnames; i++) + fprintf(f, "\t\t\"%jx\" [ label = \"{%s:|{%s|[%jx]:}}\" ];\n", + (uintmax_t)nlist->nodeinfo[i].id, + nlist->nodeinfo[i].name[0] != '\0' ? + nlist->nodeinfo[i].name : UNNAMED, + nlist->nodeinfo[i].type, (uintmax_t)nlist->nodeinfo[i].id); + fprintf(f, "\t};\n"); + + fprintf(f, "\tsubgraph cluster_disconnected {\n"); + fprintf(f, "\t\tbgcolor = pink;\n"); + for (i = 0; i < nlist->numnames; i++) + if (nlist->nodeinfo[i].hooks == 0) + fprintf(f, "\t\t\"%jx\";\n", + (uintmax_t)nlist->nodeinfo[i].id); + fprintf(f, "\t};\n"); + + for (i = 0; i < nlist->numnames; i++) { + struct ng_mesg *hlresp; + struct hooklist *hlist; + struct nodeinfo *ninfo; + char path[NG_PATHSIZ]; + u_int j; + + (void)snprintf(path, sizeof(path), "[%jx]:", + (uintmax_t)nlist->nodeinfo[i].id); + + /* Get node info and hook list */ + if (NgSendMsg(csock, path, NGM_GENERIC_COOKIE, NGM_LISTHOOKS, + NULL, 0) < 0) { + free(nlresp); + warn("send listhooks msg"); + goto error; + } + if (NgAllocRecvMsg(csock, &hlresp, NULL) < 0) { + free(nlresp); + warn("recv listhooks msg"); + goto error; + } + + hlist = (struct hooklist *)hlresp->data; + ninfo = &hlist->nodeinfo; + if (ninfo->hooks == 0) { + free(hlresp); + continue; + } + + fprintf(f, "\tnode [ shape = octagon, fontsize = 10 ] {\n"); + for (j = 0; j < ninfo->hooks; j++) + fprintf(f, "\t\t\"%jx.%s\" [ label = \"%s\" ];\n", + (uintmax_t)nlist->nodeinfo[i].id, + hlist->link[j].ourhook, hlist->link[j].ourhook); + fprintf(f, "\t};\n"); + + fprintf(f, "\t{\n\t\tedge [ weight = 2.0, style = bold ];\n"); + for (j = 0; j < ninfo->hooks; j++) + fprintf(f, "\t\t\"%jx\" -- \"%jx.%s\";\n", + (uintmax_t)nlist->nodeinfo[i].id, + (uintmax_t)nlist->nodeinfo[i].id, + hlist->link[j].ourhook); + fprintf(f, "\t};\n"); + + for (j = 0; j < ninfo->hooks; j++) { + /* Only print the edges going in one direction. */ + if (hlist->link[j].nodeinfo.id > nlist->nodeinfo[i].id) + continue; + fprintf(f, "\t\"%jx.%s\" -- \"%jx.%s\";\n", + (uintmax_t)nlist->nodeinfo[i].id, + hlist->link[j].ourhook, + (uintmax_t)hlist->link[j].nodeinfo.id, + hlist->link[j].peerhook); + } + free(hlresp); + } + + fprintf(f, "}\n"); + + free(nlresp); + if (f != stdout) + (void)fclose(f); + return (CMDRTN_OK); +error: + if (f != stdout) + (void)fclose(f); + return (CMDRTN_ERROR); +} diff --git a/usr.sbin/ngctl/list.c b/usr.sbin/ngctl/list.c new file mode 100644 index 0000000..da4174c --- /dev/null +++ b/usr.sbin/ngctl/list.c @@ -0,0 +1,146 @@ + +/* + * list.c + * + * Copyright (c) 1996-1999 Whistle Communications, Inc. + * All rights reserved. + * + * Subject to the following obligations and disclaimer of warranty, use and + * redistribution of this software, in source or object code forms, with or + * without modifications are expressly permitted by Whistle Communications; + * provided, however, that: + * 1. Any and all reproductions of the source or object code must include the + * copyright notice above and the following disclaimer of warranties; and + * 2. No rights are granted, in any manner or form, to use Whistle + * Communications, Inc. trademarks, including the mark "WHISTLE + * COMMUNICATIONS" on advertising, endorsements, or otherwise except as + * such appears in the above copyright notice or in the software. + * + * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND + * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO + * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, + * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. + * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY + * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS + * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE. + * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES + * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING + * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, + * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY + * OF SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#include <err.h> +#include <netgraph.h> +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> + +#include "ngctl.h" + +#define UNNAMED "<unnamed>" + +static int ListCmd(int ac, char **av); + +const struct ngcmd list_cmd = { + ListCmd, + "list [-ln]", + "Show information about all nodes", + "The list command shows information about every node that currently" + " exists in the netgraph system. The optional -n argument limits" + " this list to only those nodes with a global name assignment." + " The optional -l argument provides verbose output that includes" + " hook information as well.", + { "ls" } +}; + +static int +ListCmd(int ac, char **av) +{ + struct ng_mesg *resp; + struct namelist *nlist; + struct nodeinfo *ninfo; + int list_hooks = 0; + int named_only = 0; + int ch, rtn = CMDRTN_OK; + + /* Get options */ + optind = 1; + while ((ch = getopt(ac, av, "ln")) != -1) { + switch (ch) { + case 'l': + list_hooks = 1; + break; + case 'n': + named_only = 1; + break; + case '?': + default: + return (CMDRTN_USAGE); + break; + } + } + ac -= optind; + av += optind; + + /* Get arguments */ + switch (ac) { + case 0: + break; + default: + return (CMDRTN_USAGE); + } + + /* Get list of nodes */ + if (NgSendMsg(csock, ".", NGM_GENERIC_COOKIE, + named_only ? NGM_LISTNAMES : NGM_LISTNODES, NULL, 0) < 0) { + warn("send msg"); + return (CMDRTN_ERROR); + } + if (NgAllocRecvMsg(csock, &resp, NULL) < 0) { + warn("recv msg"); + return (CMDRTN_ERROR); + } + + /* Show each node */ + nlist = (struct namelist *) resp->data; + printf("There are %d total %snodes:\n", + nlist->numnames, named_only ? "named " : ""); + ninfo = nlist->nodeinfo; + if (list_hooks) { + char path[NG_PATHSIZ]; + char *argv[2] = { "show", path }; + + while (nlist->numnames > 0) { + snprintf(path, sizeof(path), + "[%lx]:", (u_long)ninfo->id); + if ((rtn = (*show_cmd.func)(2, argv)) != CMDRTN_OK) + break; + ninfo++; + nlist->numnames--; + } + } else { + while (nlist->numnames > 0) { + if (!*ninfo->name) + snprintf(ninfo->name, sizeof(ninfo->name), + "%s", UNNAMED); + printf(" Name: %-15s Type: %-15s ID: %08x " + "Num hooks: %d\n", + ninfo->name, ninfo->type, ninfo->id, ninfo->hooks); + ninfo++; + nlist->numnames--; + } + } + + /* Done */ + free(resp); + return (rtn); +} + diff --git a/usr.sbin/ngctl/main.c b/usr.sbin/ngctl/main.c new file mode 100644 index 0000000..4b1cdab --- /dev/null +++ b/usr.sbin/ngctl/main.c @@ -0,0 +1,665 @@ + +/* + * main.c + * + * Copyright (c) 1996-1999 Whistle Communications, Inc. + * All rights reserved. + * + * Subject to the following obligations and disclaimer of warranty, use and + * redistribution of this software, in source or object code forms, with or + * without modifications are expressly permitted by Whistle Communications; + * provided, however, that: + * 1. Any and all reproductions of the source or object code must include the + * copyright notice above and the following disclaimer of warranties; and + * 2. No rights are granted, in any manner or form, to use Whistle + * Communications, Inc. trademarks, including the mark "WHISTLE + * COMMUNICATIONS" on advertising, endorsements, or otherwise except as + * such appears in the above copyright notice or in the software. + * + * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND + * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO + * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, + * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. + * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY + * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS + * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE. + * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES + * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING + * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, + * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY + * OF SUCH DAMAGE. + * + * $Whistle: main.c,v 1.12 1999/11/29 19:17:46 archie Exp $ + */ + +#include <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +#include <sys/param.h> +#include <sys/socket.h> +#include <sys/select.h> + +#include <ctype.h> +#include <err.h> +#include <errno.h> +#include <limits.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sysexits.h> +#include <unistd.h> +#ifdef EDITLINE +#include <signal.h> +#include <histedit.h> +#include <pthread.h> +#endif + +#include <netgraph.h> + +#include "ngctl.h" + +#define PROMPT "+ " +#define MAX_ARGS 512 +#define WHITESPACE " \t\r\n\v\f" +#define DUMP_BYTES_PER_LINE 16 + +/* Internal functions */ +static int ReadFile(FILE *fp); +static void ReadSockets(fd_set *); +static int DoParseCommand(const char *line); +static int DoCommand(int ac, char **av); +static int DoInteractive(void); +static const struct ngcmd *FindCommand(const char *string); +static int MatchCommand(const struct ngcmd *cmd, const char *s); +static void Usage(const char *msg); +static int ReadCmd(int ac, char **av); +static int HelpCmd(int ac, char **av); +static int QuitCmd(int ac, char **av); +#ifdef EDITLINE +static volatile sig_atomic_t unblock; +static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; +static pthread_cond_t cond = PTHREAD_COND_INITIALIZER; +#endif + +/* List of commands */ +static const struct ngcmd *const cmds[] = { + &config_cmd, + &connect_cmd, + &debug_cmd, + &dot_cmd, + &help_cmd, + &list_cmd, + &mkpeer_cmd, + &msg_cmd, + &name_cmd, + &read_cmd, + &rmhook_cmd, + &show_cmd, + &shutdown_cmd, + &status_cmd, + &types_cmd, + &write_cmd, + &quit_cmd, + NULL +}; + +/* Commands defined in this file */ +const struct ngcmd read_cmd = { + ReadCmd, + "read <filename>", + "Read and execute commands from a file", + NULL, + { "source", "." } +}; +const struct ngcmd help_cmd = { + HelpCmd, + "help [command]", + "Show command summary or get more help on a specific command", + NULL, + { "?" } +}; +const struct ngcmd quit_cmd = { + QuitCmd, + "quit", + "Exit program", + NULL, + { "exit" } +}; + +/* Our control and data sockets */ +int csock, dsock; + +/* + * main() + */ +int +main(int ac, char *av[]) +{ + char name[NG_NODESIZ]; + int interactive = isatty(0) && isatty(1); + FILE *fp = NULL; + int ch, rtn = 0; + + /* Set default node name */ + snprintf(name, sizeof(name), "ngctl%d", getpid()); + + /* Parse command line */ + while ((ch = getopt(ac, av, "df:n:")) != -1) { + switch (ch) { + case 'd': + NgSetDebug(NgSetDebug(-1) + 1); + break; + case 'f': + if (strcmp(optarg, "-") == 0) + fp = stdin; + else if ((fp = fopen(optarg, "r")) == NULL) + err(EX_NOINPUT, "%s", optarg); + break; + case 'n': + snprintf(name, sizeof(name), "%s", optarg); + break; + case '?': + default: + Usage((char *)NULL); + break; + } + } + ac -= optind; + av += optind; + + /* Create a new socket node */ + if (NgMkSockNode(name, &csock, &dsock) < 0) + err(EX_OSERR, "can't create node"); + + /* Do commands as requested */ + if (ac == 0) { + if (fp != NULL) { + rtn = ReadFile(fp); + } else if (interactive) { + rtn = DoInteractive(); + } else + Usage("no command specified"); + } else { + rtn = DoCommand(ac, av); + } + + /* Convert command return code into system exit code */ + switch (rtn) { + case CMDRTN_OK: + case CMDRTN_QUIT: + rtn = 0; + break; + case CMDRTN_USAGE: + rtn = EX_USAGE; + break; + case CMDRTN_ERROR: + rtn = EX_OSERR; + break; + } + return (rtn); +} + +/* + * Process commands from a file + */ +static int +ReadFile(FILE *fp) +{ + char line[LINE_MAX]; + int num, rtn; + + for (num = 1; fgets(line, sizeof(line), fp) != NULL; num++) { + if (*line == '#') + continue; + if ((rtn = DoParseCommand(line)) != 0) { + warnx("line %d: error in file", num); + return (rtn); + } + } + return (CMDRTN_OK); +} + +#ifdef EDITLINE +/* Signal handler for Monitor() thread. */ +static void +Unblock(int signal __unused) +{ + + unblock = 1; +} + +/* + * Thread that monitors csock and dsock while main thread + * can be blocked in el_gets(). + */ +static void * +Monitor(void *v __unused) +{ + struct sigaction act; + const int maxfd = MAX(csock, dsock) + 1; + + act.sa_handler = Unblock; + sigemptyset(&act.sa_mask); + act.sa_flags = 0; + sigaction(SIGUSR1, &act, NULL); + + pthread_mutex_lock(&mutex); + for (;;) { + fd_set rfds; + + /* See if any data or control messages are arriving. */ + FD_ZERO(&rfds); + FD_SET(csock, &rfds); + FD_SET(dsock, &rfds); + unblock = 0; + if (select(maxfd, &rfds, NULL, NULL, NULL) <= 0) { + if (errno == EINTR) { + if (unblock == 1) + pthread_cond_wait(&cond, &mutex); + continue; + } + err(EX_OSERR, "select"); + } + ReadSockets(&rfds); + } + + return (NULL); +} + +static char * +Prompt(EditLine *el __unused) +{ + + return (PROMPT); +} + +/* + * Here we start a thread, that will monitor the netgraph + * sockets and catch any unexpected messages or data on them, + * that can arrive while user edits his/her commands. + * + * Whenever we expect data on netgraph sockets, we send signal + * to monitoring thread. The signal forces it to exit select() + * system call and sleep on condvar until we wake it. While + * monitoring thread sleeps, we can do our work with netgraph + * sockets. + */ +static int +DoInteractive(void) +{ + pthread_t monitor; + EditLine *el; + History *hist; + HistEvent hev = { 0, "" }; + + (*help_cmd.func)(0, NULL); + pthread_create(&monitor, NULL, Monitor, NULL); + el = el_init(getprogname(), stdin, stdout, stderr); + if (el == NULL) + return (CMDRTN_ERROR); + el_set(el, EL_PROMPT, Prompt); + el_set(el, EL_SIGNAL, 1); + el_set(el, EL_EDITOR, "emacs"); + hist = history_init(); + if (hist == NULL) + return (CMDRTN_ERROR); + history(hist, &hev, H_SETSIZE, 100); + history(hist, &hev, H_SETUNIQUE, 1); + el_set(el, EL_HIST, history, (const char *)hist); + el_source(el, NULL); + + for (;;) { + const char *buf; + int count; + + if ((buf = el_gets(el, &count)) == NULL) { + printf("\n"); + break; + } + history(hist, &hev, H_ENTER, buf); + pthread_kill(monitor, SIGUSR1); + pthread_mutex_lock(&mutex); + if (DoParseCommand(buf) == CMDRTN_QUIT) { + pthread_mutex_unlock(&mutex); + break; + } + pthread_cond_signal(&cond); + pthread_mutex_unlock(&mutex); + } + + history_end(hist); + el_end(el); + pthread_cancel(monitor); + + return (CMDRTN_QUIT); +} + +#else /* !EDITLINE */ + +/* + * Interactive mode w/o libedit functionality. + */ +static int +DoInteractive(void) +{ + const int maxfd = MAX(csock, dsock) + 1; + + (*help_cmd.func)(0, NULL); + while (1) { + struct timeval tv; + fd_set rfds; + + /* See if any data or control messages are arriving */ + FD_ZERO(&rfds); + FD_SET(csock, &rfds); + FD_SET(dsock, &rfds); + memset(&tv, 0, sizeof(tv)); + if (select(maxfd, &rfds, NULL, NULL, &tv) <= 0) { + + /* Issue prompt and wait for anything to happen */ + printf("%s", PROMPT); + fflush(stdout); + FD_ZERO(&rfds); + FD_SET(0, &rfds); + FD_SET(csock, &rfds); + FD_SET(dsock, &rfds); + if (select(maxfd, &rfds, NULL, NULL, NULL) < 0) + err(EX_OSERR, "select"); + + /* If not user input, print a newline first */ + if (!FD_ISSET(0, &rfds)) + printf("\n"); + } + + ReadSockets(&rfds); + + /* Get any user input */ + if (FD_ISSET(0, &rfds)) { + char buf[LINE_MAX]; + + if (fgets(buf, sizeof(buf), stdin) == NULL) { + printf("\n"); + break; + } + if (DoParseCommand(buf) == CMDRTN_QUIT) + break; + } + } + return (CMDRTN_QUIT); +} +#endif /* !EDITLINE */ + +/* + * Read and process data on netgraph control and data sockets. + */ +static void +ReadSockets(fd_set *rfds) +{ + /* Display any incoming control message. */ + if (FD_ISSET(csock, rfds)) + MsgRead(); + + /* Display any incoming data packet. */ + if (FD_ISSET(dsock, rfds)) { + char hook[NG_HOOKSIZ]; + u_char *buf; + int rl; + + /* Read packet from socket. */ + if ((rl = NgAllocRecvData(dsock, &buf, hook)) < 0) + err(EX_OSERR, "reading hook \"%s\"", hook); + if (rl == 0) + errx(EX_OSERR, "EOF from hook \"%s\"?", hook); + + /* Write packet to stdout. */ + printf("Rec'd data packet on hook \"%s\":\n", hook); + DumpAscii(buf, rl); + free(buf); + } +} + +/* + * Parse a command line and execute the command + */ +static int +DoParseCommand(const char *line) +{ + char *av[MAX_ARGS]; + int ac; + + /* Parse line */ + for (ac = 0, av[0] = strtok((char *)line, WHITESPACE); + ac < MAX_ARGS - 1 && av[ac]; + av[++ac] = strtok(NULL, WHITESPACE)); + + /* Do command */ + return (DoCommand(ac, av)); +} + +/* + * Execute the command + */ +static int +DoCommand(int ac, char **av) +{ + const struct ngcmd *cmd; + int rtn; + + if (ac == 0 || *av[0] == 0) + return (CMDRTN_OK); + if ((cmd = FindCommand(av[0])) == NULL) + return (CMDRTN_ERROR); + if ((rtn = (*cmd->func)(ac, av)) == CMDRTN_USAGE) + warnx("usage: %s", cmd->cmd); + return (rtn); +} + +/* + * Find a command + */ +static const struct ngcmd * +FindCommand(const char *string) +{ + int k, found = -1; + + for (k = 0; cmds[k] != NULL; k++) { + if (MatchCommand(cmds[k], string)) { + if (found != -1) { + warnx("\"%s\": ambiguous command", string); + return (NULL); + } + found = k; + } + } + if (found == -1) { + warnx("\"%s\": unknown command", string); + return (NULL); + } + return (cmds[found]); +} + +/* + * See if string matches a prefix of "cmd" (or an alias) case insensitively + */ +static int +MatchCommand(const struct ngcmd *cmd, const char *s) +{ + int a; + + /* Try to match command, ignoring the usage stuff */ + if (strlen(s) <= strcspn(cmd->cmd, WHITESPACE)) { + if (strncasecmp(s, cmd->cmd, strlen(s)) == 0) + return (1); + } + + /* Try to match aliases */ + for (a = 0; a < MAX_CMD_ALIAS && cmd->aliases[a] != NULL; a++) { + if (strlen(cmd->aliases[a]) >= strlen(s)) { + if (strncasecmp(s, cmd->aliases[a], strlen(s)) == 0) + return (1); + } + } + + /* No match */ + return (0); +} + +/* + * ReadCmd() + */ +static int +ReadCmd(int ac, char **av) +{ + FILE *fp; + int rtn; + + /* Open file */ + switch (ac) { + case 2: + if ((fp = fopen(av[1], "r")) == NULL) { + warn("%s", av[1]); + return (CMDRTN_ERROR); + } + break; + default: + return (CMDRTN_USAGE); + } + + /* Process it */ + rtn = ReadFile(fp); + fclose(fp); + return (rtn); +} + +/* + * HelpCmd() + */ +static int +HelpCmd(int ac, char **av) +{ + const struct ngcmd *cmd; + int k; + + switch (ac) { + case 0: + case 1: + /* Show all commands */ + printf("Available commands:\n"); + for (k = 0; cmds[k] != NULL; k++) { + char *s, buf[100]; + + cmd = cmds[k]; + snprintf(buf, sizeof(buf), "%s", cmd->cmd); + for (s = buf; *s != '\0' && !isspace(*s); s++); + *s = '\0'; + printf(" %-10s %s\n", buf, cmd->desc); + } + return (CMDRTN_OK); + default: + /* Show help on a specific command */ + if ((cmd = FindCommand(av[1])) != NULL) { + printf("usage: %s\n", cmd->cmd); + if (cmd->aliases[0] != NULL) { + int a = 0; + + printf("Aliases: "); + while (1) { + printf("%s", cmd->aliases[a++]); + if (a == MAX_CMD_ALIAS + || cmd->aliases[a] == NULL) { + printf("\n"); + break; + } + printf(", "); + } + } + printf("Summary: %s\n", cmd->desc); + if (cmd->help != NULL) { + const char *s; + char buf[65]; + int tot, len, done; + + printf("Description:\n"); + for (s = cmd->help; *s != '\0'; s += len) { + while (isspace(*s)) + s++; + tot = snprintf(buf, + sizeof(buf), "%s", s); + len = strlen(buf); + done = len == tot; + if (!done) { + while (len > 0 + && !isspace(buf[len-1])) + buf[--len] = '\0'; + } + printf(" %s\n", buf); + } + } + } + } + return (CMDRTN_OK); +} + +/* + * QuitCmd() + */ +static int +QuitCmd(int ac __unused, char **av __unused) +{ + return (CMDRTN_QUIT); +} + +/* + * Dump data in hex and ASCII form + */ +void +DumpAscii(const u_char *buf, int len) +{ + char ch, sbuf[100]; + int k, count; + + for (count = 0; count < len; count += DUMP_BYTES_PER_LINE) { + snprintf(sbuf, sizeof(sbuf), "%04x: ", count); + for (k = 0; k < DUMP_BYTES_PER_LINE; k++) { + if (count + k < len) { + snprintf(sbuf + strlen(sbuf), + sizeof(sbuf) - strlen(sbuf), + "%02x ", buf[count + k]); + } else { + snprintf(sbuf + strlen(sbuf), + sizeof(sbuf) - strlen(sbuf), " "); + } + } + snprintf(sbuf + strlen(sbuf), sizeof(sbuf) - strlen(sbuf), " "); + for (k = 0; k < DUMP_BYTES_PER_LINE; k++) { + if (count + k < len) { + ch = isprint(buf[count + k]) ? + buf[count + k] : '.'; + snprintf(sbuf + strlen(sbuf), + sizeof(sbuf) - strlen(sbuf), "%c", ch); + } else { + snprintf(sbuf + strlen(sbuf), + sizeof(sbuf) - strlen(sbuf), " "); + } + } + printf("%s\n", sbuf); + } +} + +/* + * Usage() + */ +static void +Usage(const char *msg) +{ + if (msg) + warnx("%s", msg); + fprintf(stderr, + "usage: ngctl [-d] [-f file] [-n name] [command ...]\n"); + exit(EX_USAGE); +} diff --git a/usr.sbin/ngctl/mkpeer.c b/usr.sbin/ngctl/mkpeer.c new file mode 100644 index 0000000..1c6896b --- /dev/null +++ b/usr.sbin/ngctl/mkpeer.c @@ -0,0 +1,90 @@ + +/* + * mkpeer.c + * + * Copyright (c) 1996-1999 Whistle Communications, Inc. + * All rights reserved. + * + * Subject to the following obligations and disclaimer of warranty, use and + * redistribution of this software, in source or object code forms, with or + * without modifications are expressly permitted by Whistle Communications; + * provided, however, that: + * 1. Any and all reproductions of the source or object code must include the + * copyright notice above and the following disclaimer of warranties; and + * 2. No rights are granted, in any manner or form, to use Whistle + * Communications, Inc. trademarks, including the mark "WHISTLE + * COMMUNICATIONS" on advertising, endorsements, or otherwise except as + * such appears in the above copyright notice or in the software. + * + * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND + * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO + * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, + * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. + * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY + * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS + * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE. + * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES + * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING + * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, + * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY + * OF SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#include <err.h> +#include <netgraph.h> +#include <stdio.h> + +#include "ngctl.h" + +static int MkPeerCmd(int ac, char **av); + +const struct ngcmd mkpeer_cmd = { + MkPeerCmd, + "mkpeer [path] <type> <hook> <peerhook>", + "Create and connect a new node to the node at \"path\"", + "The mkpeer command atomically creates a new node of type \"type\"" + " and connects it to the node at \"path\". The hooks used for the" + " connection are \"hook\" on the original node and \"peerhook\"" + " on the new node." + " If \"path\" is omitted then \".\" is assumed.", + { NULL } +}; + +static int +MkPeerCmd(int ac, char **av) +{ + struct ngm_mkpeer mkp; + const char *path = "."; + + /* Get arguments */ + switch (ac) { + case 5: + path = av[1]; + ac--; + av++; + /* FALLTHROUGH */ + case 4: + snprintf(mkp.type, sizeof(mkp.type), "%s", av[1]); + snprintf(mkp.ourhook, sizeof(mkp.ourhook), "%s", av[2]); + snprintf(mkp.peerhook, sizeof(mkp.peerhook), "%s", av[3]); + break; + default: + return (CMDRTN_USAGE); + } + + /* Send message */ + if (NgSendMsg(csock, path, NGM_GENERIC_COOKIE, + NGM_MKPEER, &mkp, sizeof(mkp)) < 0) { + warn("send msg"); + return (CMDRTN_ERROR); + } + return (CMDRTN_OK); +} + diff --git a/usr.sbin/ngctl/msg.c b/usr.sbin/ngctl/msg.c new file mode 100644 index 0000000..38f0db7 --- /dev/null +++ b/usr.sbin/ngctl/msg.c @@ -0,0 +1,165 @@ + +/* + * msg.c + * + * Copyright (c) 1999 Whistle Communications, Inc. + * All rights reserved. + * + * Subject to the following obligations and disclaimer of warranty, use and + * redistribution of this software, in source or object code forms, with or + * without modifications are expressly permitted by Whistle Communications; + * provided, however, that: + * 1. Any and all reproductions of the source or object code must include the + * copyright notice above and the following disclaimer of warranties; and + * 2. No rights are granted, in any manner or form, to use Whistle + * Communications, Inc. trademarks, including the mark "WHISTLE + * COMMUNICATIONS" on advertising, endorsements, or otherwise except as + * such appears in the above copyright notice or in the software. + * + * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND + * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO + * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, + * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. + * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY + * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS + * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE. + * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES + * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING + * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, + * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY + * OF SUCH DAMAGE. + * + * $Whistle: msg.c,v 1.2 1999/11/29 23:38:35 archie Exp $ + */ + +#include <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +#include <err.h> +#include <netgraph.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sysexits.h> +#include <unistd.h> + +#include "ngctl.h" + +static int MsgCmd(int ac, char **av); + +const struct ngcmd msg_cmd = { + MsgCmd, + "msg path command [args ... ]", + "Send a netgraph control message to the node at \"path\"", + "The msg command constructs a netgraph control message from the" + " command name and ASCII arguments (if any) and sends that message" + " to the node. It does this by first asking the node to convert" + " the ASCII message into binary format, and resending the result.", + { "cmd" } +}; + +static int +MsgCmd(int ac, char **av) +{ + char *buf; + char *path, *cmdstr; + int i, len; + + /* Get arguments */ + if (ac < 3) + return (CMDRTN_USAGE); + path = av[1]; + cmdstr = av[2]; + + /* Put command and arguments back together as one string */ + for (len = 1, i = 3; i < ac; i++) + len += strlen(av[i]) + 1; + if ((buf = malloc(len)) == NULL) { + warn("malloc"); + return (CMDRTN_ERROR); + } + for (*buf = '\0', i = 3; i < ac; i++) { + snprintf(buf + strlen(buf), + len - strlen(buf), " %s", av[i]); + } + + /* Send it */ + if (NgSendAsciiMsg(csock, path, "%s%s", cmdstr, buf) < 0) { + free(buf); + warn("send msg"); + return (CMDRTN_ERROR); + } + free(buf); + + /* See if a synchronous reply awaits */ + { + struct timeval tv; + fd_set rfds; + + FD_ZERO(&rfds); + FD_SET(csock, &rfds); + memset(&tv, 0, sizeof(tv)); + switch (select(csock + 1, &rfds, NULL, NULL, &tv)) { + case -1: + err(EX_OSERR, "select"); + case 0: + break; + default: + MsgRead(); + break; + } + } + + /* Done */ + return (CMDRTN_OK); +} + +/* + * Read and display the next incoming control message + */ +void +MsgRead(void) +{ + struct ng_mesg *m, *m2; + struct ng_mesg *ascii; + char path[NG_PATHSIZ]; + + /* Get incoming message (in binary form) */ + if (NgAllocRecvMsg(csock, &m, path) < 0) { + warn("recv incoming message"); + return; + } + + /* Ask originating node to convert message to ASCII */ + if (NgSendMsg(csock, path, NGM_GENERIC_COOKIE, + NGM_BINARY2ASCII, m, sizeof(*m) + m->header.arglen) < 0 + || NgAllocRecvMsg(csock, &m2, NULL) < 0) { + printf("Rec'd %s %d from \"%s\":\n", + (m->header.flags & NGF_RESP) != 0 ? "response" : "command", + m->header.cmd, path); + if (m->header.arglen == 0) + printf("No arguments\n"); + else + DumpAscii((const u_char *)m->data, m->header.arglen); + free(m); + return; + } + + /* Display message in ASCII form */ + free(m); + ascii = (struct ng_mesg *)m2->data; + printf("Rec'd %s \"%s\" (%d) from \"%s\":\n", + (ascii->header.flags & NGF_RESP) != 0 ? "response" : "command", + ascii->header.cmdstr, ascii->header.cmd, path); + if (*ascii->data != '\0') + printf("Args:\t%s\n", ascii->data); + else + printf("No arguments\n"); + free(m2); +} + diff --git a/usr.sbin/ngctl/name.c b/usr.sbin/ngctl/name.c new file mode 100644 index 0000000..7149d35 --- /dev/null +++ b/usr.sbin/ngctl/name.c @@ -0,0 +1,81 @@ + +/* + * name.c + * + * Copyright (c) 1996-1999 Whistle Communications, Inc. + * All rights reserved. + * + * Subject to the following obligations and disclaimer of warranty, use and + * redistribution of this software, in source or object code forms, with or + * without modifications are expressly permitted by Whistle Communications; + * provided, however, that: + * 1. Any and all reproductions of the source or object code must include the + * copyright notice above and the following disclaimer of warranties; and + * 2. No rights are granted, in any manner or form, to use Whistle + * Communications, Inc. trademarks, including the mark "WHISTLE + * COMMUNICATIONS" on advertising, endorsements, or otherwise except as + * such appears in the above copyright notice or in the software. + * + * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND + * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO + * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, + * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. + * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY + * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS + * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE. + * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES + * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING + * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, + * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY + * OF SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#include <err.h> +#include <stdio.h> +#include <unistd.h> +#include <netgraph.h> + +#include "ngctl.h" + +static int NameCmd(int ac, char **av); + +const struct ngcmd name_cmd = { + NameCmd, + "name <path> <name>", + "Assign name <name> to the node at <path>", + NULL, + { NULL } +}; + +static int +NameCmd(int ac, char **av) +{ + struct ngm_name name; + char *path; + + /* Get arguments */ + switch (ac) { + case 3: + path = av[1]; + snprintf(name.name, sizeof(name.name), "%s", av[2]); + break; + default: + return (CMDRTN_USAGE); + } + + /* Send message */ + if (NgSendMsg(csock, path, NGM_GENERIC_COOKIE, + NGM_NAME, &name, sizeof(name)) < 0) { + warn("send msg"); + return (CMDRTN_ERROR); + } + return (CMDRTN_OK); +} + diff --git a/usr.sbin/ngctl/ngctl.8 b/usr.sbin/ngctl/ngctl.8 new file mode 100644 index 0000000..8ba9f5d --- /dev/null +++ b/usr.sbin/ngctl/ngctl.8 @@ -0,0 +1,141 @@ +.\" Copyright (c) 1996-1999 Whistle Communications, Inc. +.\" All rights reserved. +.\" +.\" Subject to the following obligations and disclaimer of warranty, use and +.\" redistribution of this software, in source or object code forms, with or +.\" without modifications are expressly permitted by Whistle Communications; +.\" provided, however, that: +.\" 1. Any and all reproductions of the source or object code must include the +.\" copyright notice above and the following disclaimer of warranties; and +.\" 2. No rights are granted, in any manner or form, to use Whistle +.\" Communications, Inc. trademarks, including the mark "WHISTLE +.\" COMMUNICATIONS" on advertising, endorsements, or otherwise except as +.\" such appears in the above copyright notice or in the software. +.\" +.\" THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND +.\" TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO +.\" REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, +.\" INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF +.\" MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. +.\" WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY +.\" REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS +.\" SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE. +.\" IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES +.\" RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING +.\" WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, +.\" PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR +.\" SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY +.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +.\" THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY +.\" OF SUCH DAMAGE. +.\" +.\" $FreeBSD$ +.\" $Whistle: ngctl.8,v 1.6 1999/01/20 03:19:44 archie Exp $ +.\" +.Dd January 19, 1999 +.Dt NGCTL 8 +.Os +.Sh NAME +.Nm ngctl +.Nd netgraph control utility +.Sh SYNOPSIS +.Nm +.Op Fl d +.Op Fl f Ar filename +.Op Fl n Ar nodename +.Op Ar command ... +.Sh DESCRIPTION +The +.Nm +utility creates a new netgraph node of type +.Em socket +which can be used to issue netgraph commands. +If no +.Fl f +flag is given, no +command is supplied on the command line, and standard input is a tty, +.Nm +will enter interactive mode. +Otherwise +.Nm +will execute the supplied command(s) and exit immediately. +.Pp +Nodes can be created, removed, joined together, etc. +.Tn ASCII +formatted control messages can be sent to any node if that node +supports binary/ASCII control message conversion. +.Pp +In interactive mode, +.Nm +will display any control messages and data packets received by the socket node. +In the case of control messages, the message arguments are displayed in +.Tn ASCII +form if the originating node supports conversion. +.Pp +The options are as follows: +.Bl -tag -width indent +.It Fl f Ar nodeinfo +Read commands from the named file. +A single dash represents the standard input. +Blank lines and lines starting with a +.Dq # +are ignored. +.It Fl n Ar nodename +Assign +.Em nodename +to the newly created netgraph node. +The default name is +.Em ngctlXXX +where XXX is the process ID number. +.It Fl d +Increase the debugging verbosity level. +.El +.Sh COMMANDS +The currently supported commands in +.Nm +are: +.Pp +.Bd -literal -offset indent -compact +config get or set configuration of node at <path> +connect Connects hook <peerhook> of the node at <relpath> to <hook> +debug Get/set debugging verbosity level +dot Produce a GraphViz (.dot) of the entire netgraph. +help Show command summary or get more help on a specific command +list Show information about all nodes +mkpeer Create and connect a new node to the node at "path" +msg Send a netgraph control message to the node at "path" +name Assign name <name> to the node at <path> +read Read and execute commands from a file +rmhook Disconnect hook "hook" of the node at "path" +show Show information about the node at <path> +shutdown Shutdown the node at <path> +status Get human readable status information from the node at <path> +types Show information about all installed node types +write Send a data packet down the hook named by "hook". +quit Exit program +.Ed +.Pp +Some commands have aliases, e.g., +.Dq ls +is the same as +.Dq list . +The +.Dq help +command displays the available +commands, their usage and aliases, and a brief description. +.Sh EXIT STATUS +.Ex -std +.Sh SEE ALSO +.Xr netgraph 3 , +.Xr netgraph 4 , +.Xr nghook 8 +.Sh HISTORY +The +.Nm netgraph +system was designed and first implemented at Whistle Communications, Inc.\& in +a version of +.Fx 2.2 +customized for the Whistle InterJet. +.Sh AUTHORS +.An Archie Cobbs Aq Mt archie@whistle.com diff --git a/usr.sbin/ngctl/ngctl.h b/usr.sbin/ngctl/ngctl.h new file mode 100644 index 0000000..4d8c2ad --- /dev/null +++ b/usr.sbin/ngctl/ngctl.h @@ -0,0 +1,82 @@ + +/* + * ngctl.h + * + * Copyright (c) 1996-1999 Whistle Communications, Inc. + * All rights reserved. + * + * Subject to the following obligations and disclaimer of warranty, use and + * redistribution of this software, in source or object code forms, with or + * without modifications are expressly permitted by Whistle Communications; + * provided, however, that: + * 1. Any and all reproductions of the source or object code must include the + * copyright notice above and the following disclaimer of warranties; and + * 2. No rights are granted, in any manner or form, to use Whistle + * Communications, Inc. trademarks, including the mark "WHISTLE + * COMMUNICATIONS" on advertising, endorsements, or otherwise except as + * such appears in the above copyright notice or in the software. + * + * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND + * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO + * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, + * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. + * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY + * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS + * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE. + * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES + * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING + * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, + * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY + * OF SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#define MAX_CMD_ALIAS 8 + +/* Command descriptors */ +struct ngcmd { + int (*func)(int ac, char **av); /* command function */ + const char *cmd; /* command usage */ + const char *desc; /* description */ + const char *help; /* help text */ + const char *aliases[MAX_CMD_ALIAS]; /* command aliases */ +}; + +/* Command return values */ +#define CMDRTN_OK 0 +#define CMDRTN_USAGE 1 +#define CMDRTN_ERROR 2 +#define CMDRTN_QUIT 3 + +/* Available commands */ +extern const struct ngcmd config_cmd; +extern const struct ngcmd connect_cmd; +extern const struct ngcmd debug_cmd; +extern const struct ngcmd dot_cmd; +extern const struct ngcmd help_cmd; +extern const struct ngcmd list_cmd; +extern const struct ngcmd mkpeer_cmd; +extern const struct ngcmd msg_cmd; +extern const struct ngcmd name_cmd; +extern const struct ngcmd read_cmd; +extern const struct ngcmd rmhook_cmd; +extern const struct ngcmd show_cmd; +extern const struct ngcmd shutdown_cmd; +extern const struct ngcmd status_cmd; +extern const struct ngcmd types_cmd; +extern const struct ngcmd write_cmd; +extern const struct ngcmd quit_cmd; + +/* Data and control sockets */ +extern int csock, dsock; + +/* Misc functions */ +extern void MsgRead(void); +extern void DumpAscii(const u_char *buf, int len); + diff --git a/usr.sbin/ngctl/rmhook.c b/usr.sbin/ngctl/rmhook.c new file mode 100644 index 0000000..9a47a9d --- /dev/null +++ b/usr.sbin/ngctl/rmhook.c @@ -0,0 +1,86 @@ + +/* + * rmhook.c + * + * Copyright (c) 1996-1999 Whistle Communications, Inc. + * All rights reserved. + * + * Subject to the following obligations and disclaimer of warranty, use and + * redistribution of this software, in source or object code forms, with or + * without modifications are expressly permitted by Whistle Communications; + * provided, however, that: + * 1. Any and all reproductions of the source or object code must include the + * copyright notice above and the following disclaimer of warranties; and + * 2. No rights are granted, in any manner or form, to use Whistle + * Communications, Inc. trademarks, including the mark "WHISTLE + * COMMUNICATIONS" on advertising, endorsements, or otherwise except as + * such appears in the above copyright notice or in the software. + * + * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND + * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO + * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, + * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. + * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY + * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS + * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE. + * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES + * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING + * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, + * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY + * OF SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#include <err.h> +#include <netgraph.h> +#include <stdio.h> + +#include "ngctl.h" + +static int RmHookCmd(int ac, char **av); + +const struct ngcmd rmhook_cmd = { + RmHookCmd, + "rmhook [path] <hook>", + "Disconnect hook \"hook\" of the node at \"path\"", + "The rmhook command forces the node at \"path\" to break the link" + " formed by its hook \"hook\", if connected." + " If \"path\" is omitted then \".\" is assumed.", + { "disconnect" } +}; + +static int +RmHookCmd(int ac, char **av) +{ + struct ngm_rmhook rmh; + const char *path = "."; + + /* Get arguments */ + switch (ac) { + case 3: + path = av[1]; + ac--; + av++; + /* FALLTHROUGH */ + case 2: + snprintf(rmh.ourhook, sizeof(rmh.ourhook), "%s", av[1]); + break; + default: + return (CMDRTN_USAGE); + } + + /* Send message */ + if (NgSendMsg(csock, path, NGM_GENERIC_COOKIE, + NGM_RMHOOK, &rmh, sizeof(rmh)) < 0) { + warn("send msg"); + return (CMDRTN_ERROR); + } + return (CMDRTN_OK); +} + diff --git a/usr.sbin/ngctl/show.c b/usr.sbin/ngctl/show.c new file mode 100644 index 0000000..26eea04 --- /dev/null +++ b/usr.sbin/ngctl/show.c @@ -0,0 +1,139 @@ + +/* + * show.c + * + * Copyright (c) 1996-1999 Whistle Communications, Inc. + * All rights reserved. + * + * Subject to the following obligations and disclaimer of warranty, use and + * redistribution of this software, in source or object code forms, with or + * without modifications are expressly permitted by Whistle Communications; + * provided, however, that: + * 1. Any and all reproductions of the source or object code must include the + * copyright notice above and the following disclaimer of warranties; and + * 2. No rights are granted, in any manner or form, to use Whistle + * Communications, Inc. trademarks, including the mark "WHISTLE + * COMMUNICATIONS" on advertising, endorsements, or otherwise except as + * such appears in the above copyright notice or in the software. + * + * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND + * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO + * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, + * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. + * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY + * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS + * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE. + * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES + * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING + * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, + * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY + * OF SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#include <err.h> +#include <netgraph.h> +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> + +#include "ngctl.h" + +#define FMT " %-15s %-15s %-12s %-15s %-15s\n" +#define UNNAMED "<unnamed>" +#define NOSTATUS "<no status>" + +static int ShowCmd(int ac, char **av); + +const struct ngcmd show_cmd = { + ShowCmd, + "show [-n] <path>", + "Show information about the node at <path>", + "If the -n flag is given, hooks are not listed.", + { "inquire", "info" } +}; + +static int +ShowCmd(int ac, char **av) +{ + char *path; + struct ng_mesg *resp; + struct hooklist *hlist; + struct nodeinfo *ninfo; + int ch, no_hooks = 0; + + /* Get options */ + optind = 1; + while ((ch = getopt(ac, av, "n")) != -1) { + switch (ch) { + case 'n': + no_hooks = 1; + break; + case '?': + default: + return (CMDRTN_USAGE); + break; + } + } + ac -= optind; + av += optind; + + /* Get arguments */ + switch (ac) { + case 1: + path = av[0]; + break; + default: + return (CMDRTN_USAGE); + } + + /* Get node info and hook list */ + if (NgSendMsg(csock, path, NGM_GENERIC_COOKIE, + NGM_LISTHOOKS, NULL, 0) < 0) { + warn("send msg"); + return (CMDRTN_ERROR); + } + if (NgAllocRecvMsg(csock, &resp, NULL) < 0) { + warn("recv msg"); + return (CMDRTN_ERROR); + } + + /* Show node information */ + hlist = (struct hooklist *) resp->data; + ninfo = &hlist->nodeinfo; + if (!*ninfo->name) + snprintf(ninfo->name, sizeof(ninfo->name), "%s", UNNAMED); + printf(" Name: %-15s Type: %-15s ID: %08x Num hooks: %d\n", + ninfo->name, ninfo->type, ninfo->id, ninfo->hooks); + if (!no_hooks && ninfo->hooks > 0) { + u_int k; + + printf(FMT, "Local hook", "Peer name", + "Peer type", "Peer ID", "Peer hook"); + printf(FMT, "----------", "---------", + "---------", "-------", "---------"); + for (k = 0; k < ninfo->hooks; k++) { + struct linkinfo *const link = &hlist->link[k]; + struct nodeinfo *const peer = &hlist->link[k].nodeinfo; + char idbuf[20]; + + if (!*peer->name) { + snprintf(peer->name, sizeof(peer->name), + "%s", UNNAMED); + } + snprintf(idbuf, sizeof(idbuf), "%08x", peer->id); + printf(FMT, link->ourhook, peer->name, + peer->type, idbuf, link->peerhook); + } + } + free(resp); + return (CMDRTN_OK); +} + + diff --git a/usr.sbin/ngctl/shutdown.c b/usr.sbin/ngctl/shutdown.c new file mode 100644 index 0000000..b06beb5 --- /dev/null +++ b/usr.sbin/ngctl/shutdown.c @@ -0,0 +1,79 @@ + +/* + * shutdown.c + * + * Copyright (c) 1996-1999 Whistle Communications, Inc. + * All rights reserved. + * + * Subject to the following obligations and disclaimer of warranty, use and + * redistribution of this software, in source or object code forms, with or + * without modifications are expressly permitted by Whistle Communications; + * provided, however, that: + * 1. Any and all reproductions of the source or object code must include the + * copyright notice above and the following disclaimer of warranties; and + * 2. No rights are granted, in any manner or form, to use Whistle + * Communications, Inc. trademarks, including the mark "WHISTLE + * COMMUNICATIONS" on advertising, endorsements, or otherwise except as + * such appears in the above copyright notice or in the software. + * + * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND + * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO + * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, + * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. + * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY + * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS + * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE. + * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES + * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING + * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, + * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY + * OF SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#include <err.h> +#include <netgraph.h> +#include <unistd.h> + +#include "ngctl.h" + +static int ShutdownCmd(int ac, char **av); + +const struct ngcmd shutdown_cmd = { + ShutdownCmd, + "shutdown <path>", + "Shutdown the node at <path>", + NULL, + { "kill", "rmnode" } +}; + +static int +ShutdownCmd(int ac, char **av) +{ + char *path; + + /* Get arguments */ + switch (ac) { + case 2: + path = av[1]; + break; + default: + return (CMDRTN_USAGE); + } + + /* Shutdown node */ + if (NgSendMsg(csock, path, NGM_GENERIC_COOKIE, + NGM_SHUTDOWN, NULL, 0) < 0) { + warn("shutdown"); + return (CMDRTN_ERROR); + } + return (CMDRTN_OK); +} + + diff --git a/usr.sbin/ngctl/status.c b/usr.sbin/ngctl/status.c new file mode 100644 index 0000000..7827c63 --- /dev/null +++ b/usr.sbin/ngctl/status.c @@ -0,0 +1,101 @@ + +/* + * status.c + * + * Copyright (c) 1996-1999 Whistle Communications, Inc. + * All rights reserved. + * + * Subject to the following obligations and disclaimer of warranty, use and + * redistribution of this software, in source or object code forms, with or + * without modifications are expressly permitted by Whistle Communications; + * provided, however, that: + * 1. Any and all reproductions of the source or object code must include the + * copyright notice above and the following disclaimer of warranties; and + * 2. No rights are granted, in any manner or form, to use Whistle + * Communications, Inc. trademarks, including the mark "WHISTLE + * COMMUNICATIONS" on advertising, endorsements, or otherwise except as + * such appears in the above copyright notice or in the software. + * + * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND + * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO + * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, + * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. + * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY + * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS + * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE. + * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES + * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING + * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, + * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY + * OF SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#include <err.h> +#include <errno.h> +#include <netgraph.h> +#include <stdio.h> + +#include "ngctl.h" + +#define NOSTATUS "<no status>" + +static int StatusCmd(int ac, char **av); + +const struct ngcmd status_cmd = { + StatusCmd, + "status <path>", + "Get human readable status information from the node at <path>", + NULL, + { NULL } +}; + +static int +StatusCmd(int ac, char **av) +{ + u_char sbuf[sizeof(struct ng_mesg) + NG_TEXTRESPONSE]; + struct ng_mesg *const resp = (struct ng_mesg *) sbuf; + char *const status = (char *) resp->data; + char *path; + int nostat = 0; + + /* Get arguments */ + switch (ac) { + case 2: + path = av[1]; + break; + default: + return (CMDRTN_USAGE); + } + + /* Get node status summary */ + if (NgSendMsg(csock, path, NGM_GENERIC_COOKIE, + NGM_TEXT_STATUS, NULL, 0) < 0) { + switch (errno) { + case EINVAL: + nostat = 1; + break; + default: + warn("send msg"); + return (CMDRTN_ERROR); + } + } else { + if (NgRecvMsg(csock, resp, sizeof(sbuf), NULL) < 0 + || (resp->header.flags & NGF_RESP) == 0) + nostat = 1; + } + + /* Show it */ + if (nostat) + printf("No status available for \"%s\"\n", path); + else + printf("Status for \"%s\":\n%s\n", path, status); + return (CMDRTN_OK); +} + diff --git a/usr.sbin/ngctl/types.c b/usr.sbin/ngctl/types.c new file mode 100644 index 0000000..5ed4b53 --- /dev/null +++ b/usr.sbin/ngctl/types.c @@ -0,0 +1,101 @@ + +/* + * types.c + * + * Copyright (c) 1996-1999 Whistle Communications, Inc. + * All rights reserved. + * + * Subject to the following obligations and disclaimer of warranty, use and + * redistribution of this software, in source or object code forms, with or + * without modifications are expressly permitted by Whistle Communications; + * provided, however, that: + * 1. Any and all reproductions of the source or object code must include the + * copyright notice above and the following disclaimer of warranties; and + * 2. No rights are granted, in any manner or form, to use Whistle + * Communications, Inc. trademarks, including the mark "WHISTLE + * COMMUNICATIONS" on advertising, endorsements, or otherwise except as + * such appears in the above copyright notice or in the software. + * + * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND + * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO + * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, + * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. + * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY + * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS + * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE. + * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES + * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING + * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, + * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY + * OF SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#include <err.h> +#include <netgraph.h> +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> + +#include "ngctl.h" + +static int TypesCmd(int ac, char **av); + +const struct ngcmd types_cmd = { + TypesCmd, + "types", + "Show information about all installed node types", + NULL, + { NULL } +}; + +static int +TypesCmd(int ac, char **av __unused) +{ + struct ng_mesg *resp; + struct typelist *tlist; + int rtn = CMDRTN_OK; + u_int k; + + /* Get arguments */ + switch (ac) { + case 1: + break; + default: + return (CMDRTN_USAGE); + } + + /* Get list of types */ + if (NgSendMsg(csock, ".", NGM_GENERIC_COOKIE, + NGM_LISTTYPES, NULL, 0) < 0) { + warn("send msg"); + return (CMDRTN_ERROR); + } + if (NgAllocRecvMsg(csock, &resp, NULL) < 0) { + warn("recv msg"); + return (CMDRTN_ERROR); + } + + /* Show each type */ + tlist = (struct typelist *) resp->data; + printf("There are %d total types:\n", tlist->numtypes); + if (tlist->numtypes > 0) { + printf("%15s Number of living nodes\n", "Type name"); + printf("%15s ----------------------\n", "---------"); + } + for (k = 0; k < tlist->numtypes; k++) { + struct typeinfo *const ti = &tlist->typeinfo[k]; + printf("%15s %5d\n", ti->type_name, ti->numnodes); + } + + /* Done */ + free(resp); + return (rtn); +} + diff --git a/usr.sbin/ngctl/write.c b/usr.sbin/ngctl/write.c new file mode 100644 index 0000000..4114211 --- /dev/null +++ b/usr.sbin/ngctl/write.c @@ -0,0 +1,121 @@ + +/* + * write.c + * + * Copyright (c) 2002 Archie L. Cobbs + * All rights reserved. + * + * Subject to the following obligations and disclaimer of warranty, use and + * redistribution of this software, in source or object code forms, with or + * without modifications are expressly permitted by Archie L. Cobbs; + * provided, however, that: + * 1. Any and all reproductions of the source or object code must include the + * copyright notice above and the following disclaimer of warranties + * + * THIS SOFTWARE IS BEING PROVIDED BY ARCHIE L. COBBS AS IS", AND TO + * THE MAXIMUM EXTENT PERMITTED BY LAW, ARCHIE L. COBBS MAKES NO + * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, + * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. + * ARCHIE L. COBBS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY + * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS + * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE. + * IN NO EVENT SHALL ARCHIE L. COBBS BE LIABLE FOR ANY DAMAGES + * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING + * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, + * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ARCHIE L. COBBS IS ADVISED OF THE POSSIBILITY + * OF SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#include <sys/types.h> +#include <sys/socket.h> + +#include <err.h> +#include <stdio.h> +#include <string.h> +#include <unistd.h> + +#include <netgraph/ng_socket.h> + +#include "ngctl.h" + +#define BUF_SIZE 8192 + +static int WriteCmd(int ac, char **av); + +const struct ngcmd write_cmd = { + WriteCmd, + "write hook < -f file | byte ... >", + "Send a data packet down the hook named by \"hook\".", + "The data may be contained in a file, or may be described directly" + " on the command line by supplying a sequence of bytes.", + { "w" } +}; + +static int +WriteCmd(int ac, char **av) +{ + u_int32_t sagbuf[64]; + struct sockaddr_ng *sag = (struct sockaddr_ng *)sagbuf; + u_char buf[BUF_SIZE]; + const char *hook; + FILE *fp; + u_int len; + int byte; + int i; + + /* Get arguments */ + if (ac < 3) + return (CMDRTN_USAGE); + hook = av[1]; + + /* Get data */ + if (strcmp(av[2], "-f") == 0) { + if (ac != 4) + return (CMDRTN_USAGE); + if ((fp = fopen(av[3], "r")) == NULL) { + warn("can't read file \"%s\"", av[3]); + return (CMDRTN_ERROR); + } + if ((len = fread(buf, 1, sizeof(buf), fp)) == 0) { + if (ferror(fp)) + warn("can't read file \"%s\"", av[3]); + else + warnx("file \"%s\" is empty", av[3]); + fclose(fp); + return (CMDRTN_ERROR); + } + fclose(fp); + } else { + for (i = 2, len = 0; i < ac && len < sizeof(buf); i++, len++) { + if (sscanf(av[i], "%i", &byte) != 1 + || (byte < -128 || byte > 255)) { + warnx("invalid byte \"%s\"", av[i]); + return (CMDRTN_ERROR); + } + buf[len] = (u_char)byte; + } + if (len == 0) + return (CMDRTN_USAGE); + } + + /* Send data */ + sag->sg_len = 3 + strlen(hook); + sag->sg_family = AF_NETGRAPH; + strlcpy(sag->sg_data, hook, sizeof(sagbuf) - 2); + if (sendto(dsock, buf, len, + 0, (struct sockaddr *)sag, sag->sg_len) == -1) { + warn("writing to hook \"%s\"", hook); + return (CMDRTN_ERROR); + } + + /* Done */ + return (CMDRTN_OK); +} + |