xref: /freebsd/usr.sbin/ngctl/msg.c (revision 9d901d3b5f40634b402e76a13e7448b6ada50efe)
1f8307e12SArchie Cobbs 
2f8307e12SArchie Cobbs /*
3f8307e12SArchie Cobbs  * msg.c
4f8307e12SArchie Cobbs  *
5f8307e12SArchie Cobbs  * Copyright (c) 1999 Whistle Communications, Inc.
6f8307e12SArchie Cobbs  * All rights reserved.
7f8307e12SArchie Cobbs  *
8f8307e12SArchie Cobbs  * Subject to the following obligations and disclaimer of warranty, use and
9f8307e12SArchie Cobbs  * redistribution of this software, in source or object code forms, with or
10f8307e12SArchie Cobbs  * without modifications are expressly permitted by Whistle Communications;
11f8307e12SArchie Cobbs  * provided, however, that:
12f8307e12SArchie Cobbs  * 1. Any and all reproductions of the source or object code must include the
13f8307e12SArchie Cobbs  *    copyright notice above and the following disclaimer of warranties; and
14f8307e12SArchie Cobbs  * 2. No rights are granted, in any manner or form, to use Whistle
15f8307e12SArchie Cobbs  *    Communications, Inc. trademarks, including the mark "WHISTLE
16f8307e12SArchie Cobbs  *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
17f8307e12SArchie Cobbs  *    such appears in the above copyright notice or in the software.
18f8307e12SArchie Cobbs  *
19f8307e12SArchie Cobbs  * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
20f8307e12SArchie Cobbs  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
21f8307e12SArchie Cobbs  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
22f8307e12SArchie Cobbs  * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
23f8307e12SArchie Cobbs  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
24f8307e12SArchie Cobbs  * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
25f8307e12SArchie Cobbs  * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
26f8307e12SArchie Cobbs  * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
27f8307e12SArchie Cobbs  * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
28f8307e12SArchie Cobbs  * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
29f8307e12SArchie Cobbs  * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
30f8307e12SArchie Cobbs  * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
31f8307e12SArchie Cobbs  * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
32f8307e12SArchie Cobbs  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33f8307e12SArchie Cobbs  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34f8307e12SArchie Cobbs  * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
35f8307e12SArchie Cobbs  * OF SUCH DAMAGE.
36f8307e12SArchie Cobbs  *
37f8307e12SArchie Cobbs  * $Whistle: msg.c,v 1.2 1999/11/29 23:38:35 archie Exp $
38f8307e12SArchie Cobbs  * $FreeBSD$
39f8307e12SArchie Cobbs  */
40f8307e12SArchie Cobbs 
41f8307e12SArchie Cobbs #include "ngctl.h"
42f8307e12SArchie Cobbs 
436ebb8ebbSArchie Cobbs #define BUF_SIZE	4096
44f8307e12SArchie Cobbs 
45f8307e12SArchie Cobbs static int MsgCmd(int ac, char **av);
46f8307e12SArchie Cobbs 
47f8307e12SArchie Cobbs const struct ngcmd msg_cmd = {
48f8307e12SArchie Cobbs 	MsgCmd,
49f8307e12SArchie Cobbs 	"msg path command [args ... ]",
50f8307e12SArchie Cobbs 	"Send a netgraph control message to the node at \"path\"",
51f8307e12SArchie Cobbs 	"The msg command constructs a netgraph control message from the"
52f8307e12SArchie Cobbs 	" command name and ASCII arguments (if any) and sends that message"
53f8307e12SArchie Cobbs 	" to the node.  It does this by first asking the node to convert"
54b1ea6388SArchie Cobbs 	" the ASCII message into binary format, and resending the result.",
55f8307e12SArchie Cobbs 	{ "cmd" }
56f8307e12SArchie Cobbs };
57f8307e12SArchie Cobbs 
58f8307e12SArchie Cobbs static int
59f8307e12SArchie Cobbs MsgCmd(int ac, char **av)
60f8307e12SArchie Cobbs {
61f8307e12SArchie Cobbs 	char buf[BUF_SIZE];
62f8307e12SArchie Cobbs 	char *path, *cmdstr;
63f8307e12SArchie Cobbs 	int i;
64f8307e12SArchie Cobbs 
65f8307e12SArchie Cobbs 	/* Get arguments */
66f8307e12SArchie Cobbs 	if (ac < 3)
67f8307e12SArchie Cobbs 		return(CMDRTN_USAGE);
68f8307e12SArchie Cobbs 	path = av[1];
69f8307e12SArchie Cobbs 	cmdstr = av[2];
70f8307e12SArchie Cobbs 
71f8307e12SArchie Cobbs 	/* Put command and arguments back together as one string */
72f8307e12SArchie Cobbs 	for (*buf = '\0', i = 3; i < ac; i++) {
73f8307e12SArchie Cobbs 		snprintf(buf + strlen(buf),
74f8307e12SArchie Cobbs 		    sizeof(buf) - strlen(buf), " %s", av[i]);
75f8307e12SArchie Cobbs 	}
76f8307e12SArchie Cobbs 
77f8307e12SArchie Cobbs 	/* Send it */
78f8307e12SArchie Cobbs 	if (NgSendAsciiMsg(csock, path, "%s%s", cmdstr, buf) < 0) {
79f8307e12SArchie Cobbs 		warn("send msg");
80f8307e12SArchie Cobbs 		return(CMDRTN_ERROR);
81f8307e12SArchie Cobbs 	}
82f8307e12SArchie Cobbs 
836ebb8ebbSArchie Cobbs 	/* See if a synchronous reply awaits */
846ebb8ebbSArchie Cobbs 	{
856ebb8ebbSArchie Cobbs 		struct timeval tv;
866ebb8ebbSArchie Cobbs 		fd_set rfds;
876ebb8ebbSArchie Cobbs 
886ebb8ebbSArchie Cobbs 		FD_ZERO(&rfds);
896ebb8ebbSArchie Cobbs 		FD_SET(csock, &rfds);
906ebb8ebbSArchie Cobbs 		memset(&tv, 0, sizeof(tv));
916ebb8ebbSArchie Cobbs 		switch (select(csock + 1, &rfds, NULL, NULL, &tv)) {
926ebb8ebbSArchie Cobbs 		case -1:
936ebb8ebbSArchie Cobbs 			err(EX_OSERR, "select");
946ebb8ebbSArchie Cobbs 		case 0:
956ebb8ebbSArchie Cobbs 			break;
966ebb8ebbSArchie Cobbs 		default:
976ebb8ebbSArchie Cobbs 			MsgRead();
986ebb8ebbSArchie Cobbs 			break;
996ebb8ebbSArchie Cobbs 		}
1006ebb8ebbSArchie Cobbs 	}
1016ebb8ebbSArchie Cobbs 
102f8307e12SArchie Cobbs 	/* Done */
103f8307e12SArchie Cobbs 	return(CMDRTN_OK);
104f8307e12SArchie Cobbs }
105f8307e12SArchie Cobbs 
1066ebb8ebbSArchie Cobbs /*
1076ebb8ebbSArchie Cobbs  * Read and display the next incoming control message
1086ebb8ebbSArchie Cobbs  */
1096ebb8ebbSArchie Cobbs void
1106ebb8ebbSArchie Cobbs MsgRead()
1116ebb8ebbSArchie Cobbs {
1126ebb8ebbSArchie Cobbs 	u_char buf[2 * sizeof(struct ng_mesg) + BUF_SIZE];
1136ebb8ebbSArchie Cobbs 	struct ng_mesg *const m = (struct ng_mesg *)buf;
1146ebb8ebbSArchie Cobbs 	struct ng_mesg *const ascii = (struct ng_mesg *)m->data;
1159d901d3bSHartmut Brandt 	char path[NG_PATHSIZ];
1166ebb8ebbSArchie Cobbs 
1176ebb8ebbSArchie Cobbs 	/* Get incoming message (in binary form) */
1186ebb8ebbSArchie Cobbs 	if (NgRecvMsg(csock, m, sizeof(buf), path) < 0) {
1196ebb8ebbSArchie Cobbs 		warn("recv incoming message");
1206ebb8ebbSArchie Cobbs 		return;
1216ebb8ebbSArchie Cobbs 	}
1226ebb8ebbSArchie Cobbs 
1236ebb8ebbSArchie Cobbs 	/* Ask originating node to convert message to ASCII */
1246ebb8ebbSArchie Cobbs 	if (NgSendMsg(csock, path, NGM_GENERIC_COOKIE,
1256ebb8ebbSArchie Cobbs 	      NGM_BINARY2ASCII, m, sizeof(*m) + m->header.arglen) < 0
1266ebb8ebbSArchie Cobbs 	    || NgRecvMsg(csock, m, sizeof(buf), NULL) < 0) {
1276ebb8ebbSArchie Cobbs 		printf("Rec'd %s %d from \"%s\":\n",
1286ebb8ebbSArchie Cobbs 		    (m->header.flags & NGF_RESP) != 0 ? "response" : "command",
1296ebb8ebbSArchie Cobbs 		    m->header.cmd, path);
1306ebb8ebbSArchie Cobbs 		if (m->header.arglen == 0)
1316ebb8ebbSArchie Cobbs 			printf("No arguments\n");
1326ebb8ebbSArchie Cobbs 		else
1336ebb8ebbSArchie Cobbs 			DumpAscii(m->data, m->header.arglen);
1346ebb8ebbSArchie Cobbs 		return;
1356ebb8ebbSArchie Cobbs 	}
1366ebb8ebbSArchie Cobbs 
1376ebb8ebbSArchie Cobbs 	/* Display message in ASCII form */
1386ebb8ebbSArchie Cobbs 	printf("Rec'd %s \"%s\" (%d) from \"%s\":\n",
1396ebb8ebbSArchie Cobbs 	    (ascii->header.flags & NGF_RESP) != 0 ? "response" : "command",
1406ebb8ebbSArchie Cobbs 	    ascii->header.cmdstr, ascii->header.cmd, path);
1416ebb8ebbSArchie Cobbs 	if (*ascii->data != '\0')
1426ebb8ebbSArchie Cobbs 		printf("Args:\t%s\n", ascii->data);
1436ebb8ebbSArchie Cobbs 	else
1446ebb8ebbSArchie Cobbs 		printf("No arguments\n");
1456ebb8ebbSArchie Cobbs }
1466ebb8ebbSArchie Cobbs 
147