xref: /f-stack/tools/ngctl/mkpeer.c (revision 3b2bd0f6)
1*3b2bd0f6Slogwang 
2*3b2bd0f6Slogwang /*
3*3b2bd0f6Slogwang  * mkpeer.c
4*3b2bd0f6Slogwang  *
5*3b2bd0f6Slogwang  * Copyright (c) 1996-1999 Whistle Communications, Inc.
6*3b2bd0f6Slogwang  * All rights reserved.
7*3b2bd0f6Slogwang  *
8*3b2bd0f6Slogwang  * Subject to the following obligations and disclaimer of warranty, use and
9*3b2bd0f6Slogwang  * redistribution of this software, in source or object code forms, with or
10*3b2bd0f6Slogwang  * without modifications are expressly permitted by Whistle Communications;
11*3b2bd0f6Slogwang  * provided, however, that:
12*3b2bd0f6Slogwang  * 1. Any and all reproductions of the source or object code must include the
13*3b2bd0f6Slogwang  *    copyright notice above and the following disclaimer of warranties; and
14*3b2bd0f6Slogwang  * 2. No rights are granted, in any manner or form, to use Whistle
15*3b2bd0f6Slogwang  *    Communications, Inc. trademarks, including the mark "WHISTLE
16*3b2bd0f6Slogwang  *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
17*3b2bd0f6Slogwang  *    such appears in the above copyright notice or in the software.
18*3b2bd0f6Slogwang  *
19*3b2bd0f6Slogwang  * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
20*3b2bd0f6Slogwang  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
21*3b2bd0f6Slogwang  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
22*3b2bd0f6Slogwang  * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
23*3b2bd0f6Slogwang  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
24*3b2bd0f6Slogwang  * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
25*3b2bd0f6Slogwang  * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
26*3b2bd0f6Slogwang  * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
27*3b2bd0f6Slogwang  * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
28*3b2bd0f6Slogwang  * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
29*3b2bd0f6Slogwang  * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
30*3b2bd0f6Slogwang  * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
31*3b2bd0f6Slogwang  * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
32*3b2bd0f6Slogwang  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33*3b2bd0f6Slogwang  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34*3b2bd0f6Slogwang  * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
35*3b2bd0f6Slogwang  * OF SUCH DAMAGE.
36*3b2bd0f6Slogwang  *
37*3b2bd0f6Slogwang  * $FreeBSD$
38*3b2bd0f6Slogwang  */
39*3b2bd0f6Slogwang 
40*3b2bd0f6Slogwang #include <err.h>
41*3b2bd0f6Slogwang #include <netgraph.h>
42*3b2bd0f6Slogwang #include <stdio.h>
43*3b2bd0f6Slogwang 
44*3b2bd0f6Slogwang #include "ngctl.h"
45*3b2bd0f6Slogwang 
46*3b2bd0f6Slogwang static int MkPeerCmd(int ac, char **av);
47*3b2bd0f6Slogwang 
48*3b2bd0f6Slogwang const struct ngcmd mkpeer_cmd = {
49*3b2bd0f6Slogwang 	MkPeerCmd,
50*3b2bd0f6Slogwang 	"mkpeer [path] <type> <hook> <peerhook>",
51*3b2bd0f6Slogwang 	"Create and connect a new node to the node at \"path\"",
52*3b2bd0f6Slogwang 	"The mkpeer command atomically creates a new node of type \"type\""
53*3b2bd0f6Slogwang 	" and connects it to the node at \"path\". The hooks used for the"
54*3b2bd0f6Slogwang 	" connection are \"hook\" on the original node and \"peerhook\""
55*3b2bd0f6Slogwang 	" on the new node."
56*3b2bd0f6Slogwang 	" If \"path\" is omitted then \".\" is assumed.",
57*3b2bd0f6Slogwang 	{ NULL }
58*3b2bd0f6Slogwang };
59*3b2bd0f6Slogwang 
60*3b2bd0f6Slogwang static int
MkPeerCmd(int ac,char ** av)61*3b2bd0f6Slogwang MkPeerCmd(int ac, char **av)
62*3b2bd0f6Slogwang {
63*3b2bd0f6Slogwang 	struct ngm_mkpeer mkp;
64*3b2bd0f6Slogwang 	const char *path = ".";
65*3b2bd0f6Slogwang 
66*3b2bd0f6Slogwang 	/* Get arguments */
67*3b2bd0f6Slogwang 	switch (ac) {
68*3b2bd0f6Slogwang 	case 5:
69*3b2bd0f6Slogwang 		path = av[1];
70*3b2bd0f6Slogwang 		ac--;
71*3b2bd0f6Slogwang 		av++;
72*3b2bd0f6Slogwang 		/* FALLTHROUGH */
73*3b2bd0f6Slogwang 	case 4:
74*3b2bd0f6Slogwang 		snprintf(mkp.type, sizeof(mkp.type), "%s", av[1]);
75*3b2bd0f6Slogwang 		snprintf(mkp.ourhook, sizeof(mkp.ourhook), "%s", av[2]);
76*3b2bd0f6Slogwang 		snprintf(mkp.peerhook, sizeof(mkp.peerhook), "%s", av[3]);
77*3b2bd0f6Slogwang 		break;
78*3b2bd0f6Slogwang 	default:
79*3b2bd0f6Slogwang 		return (CMDRTN_USAGE);
80*3b2bd0f6Slogwang 	}
81*3b2bd0f6Slogwang 
82*3b2bd0f6Slogwang 	/* Send message */
83*3b2bd0f6Slogwang 	if (NgSendMsg(csock, path, NGM_GENERIC_COOKIE,
84*3b2bd0f6Slogwang 	    NGM_MKPEER, &mkp, sizeof(mkp)) < 0) {
85*3b2bd0f6Slogwang 		warn("send msg");
86*3b2bd0f6Slogwang 		return (CMDRTN_ERROR);
87*3b2bd0f6Slogwang 	}
88*3b2bd0f6Slogwang 	return (CMDRTN_OK);
89*3b2bd0f6Slogwang }
90*3b2bd0f6Slogwang 
91