msg.c revision 125115
1
2/*
3 * msg.c
4 *
5 * Copyright (c) 1999 Whistle Communications, Inc.
6 * All rights reserved.
7 *
8 * Subject to the following obligations and disclaimer of warranty, use and
9 * redistribution of this software, in source or object code forms, with or
10 * without modifications are expressly permitted by Whistle Communications;
11 * provided, however, that:
12 * 1. Any and all reproductions of the source or object code must include the
13 *    copyright notice above and the following disclaimer of warranties; and
14 * 2. No rights are granted, in any manner or form, to use Whistle
15 *    Communications, Inc. trademarks, including the mark "WHISTLE
16 *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
17 *    such appears in the above copyright notice or in the software.
18 *
19 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
20 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
21 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
22 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
23 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
24 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
25 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
26 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
27 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
28 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
29 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
30 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
31 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
35 * OF SUCH DAMAGE.
36 *
37 * $Whistle: msg.c,v 1.2 1999/11/29 23:38:35 archie Exp $
38 * $FreeBSD: head/usr.sbin/ngctl/msg.c 125115 2004-01-27 21:52:52Z ru $
39 */
40
41#include "ngctl.h"
42
43static int MsgCmd(int ac, char **av);
44
45const struct ngcmd msg_cmd = {
46	MsgCmd,
47	"msg path command [args ... ]",
48	"Send a netgraph control message to the node at \"path\"",
49	"The msg command constructs a netgraph control message from the"
50	" command name and ASCII arguments (if any) and sends that message"
51	" to the node.  It does this by first asking the node to convert"
52	" the ASCII message into binary format, and resending the result.",
53	{ "cmd" }
54};
55
56static int
57MsgCmd(int ac, char **av)
58{
59	char *buf;
60	char *path, *cmdstr;
61	int i, len;
62
63	/* Get arguments */
64	if (ac < 3)
65		return(CMDRTN_USAGE);
66	path = av[1];
67	cmdstr = av[2];
68
69	/* Put command and arguments back together as one string */
70	for (len = 1, i = 3; i < ac; i++)
71		len += strlen(av[i]) + 1;
72	if ((buf = malloc(len)) == NULL) {
73		warn("malloc");
74		return(CMDRTN_ERROR);
75	}
76	for (*buf = '\0', i = 3; i < ac; i++) {
77		snprintf(buf + strlen(buf),
78		    len - strlen(buf), " %s", av[i]);
79	}
80
81	/* Send it */
82	if (NgSendAsciiMsg(csock, path, "%s%s", cmdstr, buf) < 0) {
83		free(buf);
84		warn("send msg");
85		return(CMDRTN_ERROR);
86	}
87	free(buf);
88
89	/* See if a synchronous reply awaits */
90	{
91		struct timeval tv;
92		fd_set rfds;
93
94		FD_ZERO(&rfds);
95		FD_SET(csock, &rfds);
96		memset(&tv, 0, sizeof(tv));
97		switch (select(csock + 1, &rfds, NULL, NULL, &tv)) {
98		case -1:
99			err(EX_OSERR, "select");
100		case 0:
101			break;
102		default:
103			MsgRead();
104			break;
105		}
106	}
107
108	/* Done */
109	return(CMDRTN_OK);
110}
111
112/*
113 * Read and display the next incoming control message
114 */
115void
116MsgRead()
117{
118	struct ng_mesg *m, *m2;
119	struct ng_mesg *ascii;
120	char path[NG_PATHSIZ];
121
122	/* Get incoming message (in binary form) */
123	if (NgAllocRecvMsg(csock, &m, path) < 0) {
124		warn("recv incoming message");
125		return;
126	}
127
128	/* Ask originating node to convert message to ASCII */
129	if (NgSendMsg(csock, path, NGM_GENERIC_COOKIE,
130	      NGM_BINARY2ASCII, m, sizeof(*m) + m->header.arglen) < 0
131	    || NgAllocRecvMsg(csock, &m2, NULL) < 0) {
132		printf("Rec'd %s %d from \"%s\":\n",
133		    (m->header.flags & NGF_RESP) != 0 ? "response" : "command",
134		    m->header.cmd, path);
135		if (m->header.arglen == 0)
136			printf("No arguments\n");
137		else
138			DumpAscii(m->data, m->header.arglen);
139		free(m);
140		return;
141	}
142
143	/* Display message in ASCII form */
144	free(m);
145	ascii = (struct ng_mesg *)m2->data;
146	printf("Rec'd %s \"%s\" (%d) from \"%s\":\n",
147	    (ascii->header.flags & NGF_RESP) != 0 ? "response" : "command",
148	    ascii->header.cmdstr, ascii->header.cmd, path);
149	if (*ascii->data != '\0')
150		printf("Args:\t%s\n", ascii->data);
151	else
152		printf("No arguments\n");
153	free(m2);
154}
155
156