181fceb3archie
281fceb3archie/*
381fceb3archie * msg.c
481fceb3archie *
581fceb3archie * Copyright (c) 1999 Whistle Communications, Inc.
681fceb3archie * All rights reserved.
781fceb3archie *
881fceb3archie * Subject to the following obligations and disclaimer of warranty, use and
981fceb3archie * redistribution of this software, in source or object code forms, with or
1081fceb3archie * without modifications are expressly permitted by Whistle Communications;
1181fceb3archie * provided, however, that:
1281fceb3archie * 1. Any and all reproductions of the source or object code must include the
1381fceb3archie *    copyright notice above and the following disclaimer of warranties; and
1481fceb3archie * 2. No rights are granted, in any manner or form, to use Whistle
1581fceb3archie *    Communications, Inc. trademarks, including the mark "WHISTLE
1681fceb3archie *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
1781fceb3archie *    such appears in the above copyright notice or in the software.
1881fceb3archie *
1981fceb3archie * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
2081fceb3archie * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
2181fceb3archie * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
2281fceb3archie * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
2381fceb3archie * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
2481fceb3archie * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
2581fceb3archie * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
2681fceb3archie * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
2781fceb3archie * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
2881fceb3archie * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
2981fceb3archie * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
3081fceb3archie * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
3181fceb3archie * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
3281fceb3archie * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3381fceb3archie * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
3481fceb3archie * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
3581fceb3archie * OF SUCH DAMAGE.
3681fceb3archie *
3781fceb3archie * $Whistle: msg.c,v 1.2 1999/11/29 23:38:35 archie Exp $
3881fceb3archie */
3981fceb3archie
402c12157charnier#include <sys/cdefs.h>
412c12157charnier__FBSDID("$FreeBSD$");
422c12157charnier
43ae95ee0glebius#include <err.h>
44ae95ee0glebius#include <netgraph.h>
45ae95ee0glebius#include <stdio.h>
46ae95ee0glebius#include <stdlib.h>
47ae95ee0glebius#include <string.h>
48ae95ee0glebius#include <sysexits.h>
49ae95ee0glebius#include <unistd.h>
50ae95ee0glebius
5181fceb3archie#include "ngctl.h"
5281fceb3archie
5381fceb3archiestatic int MsgCmd(int ac, char **av);
5481fceb3archie
5581fceb3archieconst struct ngcmd msg_cmd = {
5681fceb3archie	MsgCmd,
5781fceb3archie	"msg path command [args ... ]",
5881fceb3archie	"Send a netgraph control message to the node at \"path\"",
5981fceb3archie	"The msg command constructs a netgraph control message from the"
6081fceb3archie	" command name and ASCII arguments (if any) and sends that message"
6181fceb3archie	" to the node.  It does this by first asking the node to convert"
627ee0a0earchie	" the ASCII message into binary format, and resending the result.",
6381fceb3archie	{ "cmd" }
6481fceb3archie};
6581fceb3archie
6681fceb3archiestatic int
6781fceb3archieMsgCmd(int ac, char **av)
6881fceb3archie{
6963673a7ru	char *buf;
7081fceb3archie	char *path, *cmdstr;
7163673a7ru	int i, len;
7281fceb3archie
7381fceb3archie	/* Get arguments */
7481fceb3archie	if (ac < 3)
750552c3fglebius		return (CMDRTN_USAGE);
7681fceb3archie	path = av[1];
7781fceb3archie	cmdstr = av[2];
7881fceb3archie
7981fceb3archie	/* Put command and arguments back together as one string */
8063673a7ru	for (len = 1, i = 3; i < ac; i++)
8163673a7ru		len += strlen(av[i]) + 1;
8263673a7ru	if ((buf = malloc(len)) == NULL) {
8363673a7ru		warn("malloc");
840552c3fglebius		return (CMDRTN_ERROR);
8563673a7ru	}
8681fceb3archie	for (*buf = '\0', i = 3; i < ac; i++) {
8781fceb3archie		snprintf(buf + strlen(buf),
8863673a7ru		    len - strlen(buf), " %s", av[i]);
8981fceb3archie	}
9081fceb3archie
9181fceb3archie	/* Send it */
9281fceb3archie	if (NgSendAsciiMsg(csock, path, "%s%s", cmdstr, buf) < 0) {
9363673a7ru		free(buf);
9481fceb3archie		warn("send msg");
950552c3fglebius		return (CMDRTN_ERROR);
9681fceb3archie	}
9763673a7ru	free(buf);
9881fceb3archie
99f1f4c48archie	/* See if a synchronous reply awaits */
100f1f4c48archie	{
101f1f4c48archie		struct timeval tv;
102f1f4c48archie		fd_set rfds;
103f1f4c48archie
104f1f4c48archie		FD_ZERO(&rfds);
105f1f4c48archie		FD_SET(csock, &rfds);
106f1f4c48archie		memset(&tv, 0, sizeof(tv));
107f1f4c48archie		switch (select(csock + 1, &rfds, NULL, NULL, &tv)) {
108f1f4c48archie		case -1:
109f1f4c48archie			err(EX_OSERR, "select");
110f1f4c48archie		case 0:
111f1f4c48archie			break;
112f1f4c48archie		default:
113f1f4c48archie			MsgRead();
114f1f4c48archie			break;
115f1f4c48archie		}
116f1f4c48archie	}
117f1f4c48archie
11881fceb3archie	/* Done */
1190552c3fglebius	return (CMDRTN_OK);
12081fceb3archie}
12181fceb3archie
122f1f4c48archie/*
123f1f4c48archie * Read and display the next incoming control message
124f1f4c48archie */
125f1f4c48archievoid
1262c12157charnierMsgRead(void)
127f1f4c48archie{
12863673a7ru	struct ng_mesg *m, *m2;
12963673a7ru	struct ng_mesg *ascii;
130173237aharti	char path[NG_PATHSIZ];
131f1f4c48archie
132f1f4c48archie	/* Get incoming message (in binary form) */
13363673a7ru	if (NgAllocRecvMsg(csock, &m, path) < 0) {
134f1f4c48archie		warn("recv incoming message");
135f1f4c48archie		return;
136f1f4c48archie	}
137f1f4c48archie
138f1f4c48archie	/* Ask originating node to convert message to ASCII */
139f1f4c48archie	if (NgSendMsg(csock, path, NGM_GENERIC_COOKIE,
140f1f4c48archie	      NGM_BINARY2ASCII, m, sizeof(*m) + m->header.arglen) < 0
14163673a7ru	    || NgAllocRecvMsg(csock, &m2, NULL) < 0) {
142f1f4c48archie		printf("Rec'd %s %d from \"%s\":\n",
143f1f4c48archie		    (m->header.flags & NGF_RESP) != 0 ? "response" : "command",
144f1f4c48archie		    m->header.cmd, path);
145f1f4c48archie		if (m->header.arglen == 0)
146f1f4c48archie			printf("No arguments\n");
147f1f4c48archie		else
1488c74632kan			DumpAscii((const u_char *)m->data, m->header.arglen);
14963673a7ru		free(m);
150f1f4c48archie		return;
151f1f4c48archie	}
152f1f4c48archie
153f1f4c48archie	/* Display message in ASCII form */
15463673a7ru	free(m);
15563673a7ru	ascii = (struct ng_mesg *)m2->data;
156f1f4c48archie	printf("Rec'd %s \"%s\" (%d) from \"%s\":\n",
157f1f4c48archie	    (ascii->header.flags & NGF_RESP) != 0 ? "response" : "command",
158f1f4c48archie	    ascii->header.cmdstr, ascii->header.cmd, path);
159f1f4c48archie	if (*ascii->data != '\0')
160f1f4c48archie		printf("Args:\t%s\n", ascii->data);
161f1f4c48archie	else
162f1f4c48archie		printf("No arguments\n");
16363673a7ru	free(m2);
164f1f4c48archie}
165f1f4c48archie
166