13b2bd0f6Slogwang
23b2bd0f6Slogwang /*
33b2bd0f6Slogwang * msg.c
43b2bd0f6Slogwang *
53b2bd0f6Slogwang * Copyright (c) 1999 Whistle Communications, Inc.
63b2bd0f6Slogwang * All rights reserved.
73b2bd0f6Slogwang *
83b2bd0f6Slogwang * Subject to the following obligations and disclaimer of warranty, use and
93b2bd0f6Slogwang * redistribution of this software, in source or object code forms, with or
103b2bd0f6Slogwang * without modifications are expressly permitted by Whistle Communications;
113b2bd0f6Slogwang * provided, however, that:
123b2bd0f6Slogwang * 1. Any and all reproductions of the source or object code must include the
133b2bd0f6Slogwang * copyright notice above and the following disclaimer of warranties; and
143b2bd0f6Slogwang * 2. No rights are granted, in any manner or form, to use Whistle
153b2bd0f6Slogwang * Communications, Inc. trademarks, including the mark "WHISTLE
163b2bd0f6Slogwang * COMMUNICATIONS" on advertising, endorsements, or otherwise except as
173b2bd0f6Slogwang * such appears in the above copyright notice or in the software.
183b2bd0f6Slogwang *
193b2bd0f6Slogwang * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
203b2bd0f6Slogwang * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
213b2bd0f6Slogwang * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
223b2bd0f6Slogwang * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
233b2bd0f6Slogwang * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
243b2bd0f6Slogwang * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
253b2bd0f6Slogwang * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
263b2bd0f6Slogwang * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
273b2bd0f6Slogwang * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
283b2bd0f6Slogwang * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
293b2bd0f6Slogwang * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
303b2bd0f6Slogwang * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
313b2bd0f6Slogwang * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
323b2bd0f6Slogwang * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
333b2bd0f6Slogwang * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
343b2bd0f6Slogwang * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
353b2bd0f6Slogwang * OF SUCH DAMAGE.
363b2bd0f6Slogwang *
373b2bd0f6Slogwang * $Whistle: msg.c,v 1.2 1999/11/29 23:38:35 archie Exp $
383b2bd0f6Slogwang */
393b2bd0f6Slogwang
403b2bd0f6Slogwang #include <sys/cdefs.h>
413b2bd0f6Slogwang __FBSDID("$FreeBSD$");
423b2bd0f6Slogwang
433b2bd0f6Slogwang #include <err.h>
443b2bd0f6Slogwang #include <netgraph.h>
453b2bd0f6Slogwang #include <stdio.h>
463b2bd0f6Slogwang #include <stdlib.h>
473b2bd0f6Slogwang #include <string.h>
483b2bd0f6Slogwang #include <sysexits.h>
493b2bd0f6Slogwang #include <unistd.h>
503b2bd0f6Slogwang
513b2bd0f6Slogwang #include "ngctl.h"
523b2bd0f6Slogwang
533b2bd0f6Slogwang static int MsgCmd(int ac, char **av);
543b2bd0f6Slogwang
553b2bd0f6Slogwang const struct ngcmd msg_cmd = {
563b2bd0f6Slogwang MsgCmd,
573b2bd0f6Slogwang "msg path command [args ... ]",
583b2bd0f6Slogwang "Send a netgraph control message to the node at \"path\"",
593b2bd0f6Slogwang "The msg command constructs a netgraph control message from the"
603b2bd0f6Slogwang " command name and ASCII arguments (if any) and sends that message"
613b2bd0f6Slogwang " to the node. It does this by first asking the node to convert"
623b2bd0f6Slogwang " the ASCII message into binary format, and resending the result.",
633b2bd0f6Slogwang { "cmd" }
643b2bd0f6Slogwang };
653b2bd0f6Slogwang
663b2bd0f6Slogwang static int
MsgCmd(int ac,char ** av)673b2bd0f6Slogwang MsgCmd(int ac, char **av)
683b2bd0f6Slogwang {
693b2bd0f6Slogwang char *buf;
703b2bd0f6Slogwang char *path, *cmdstr;
713b2bd0f6Slogwang int i, len;
723b2bd0f6Slogwang
733b2bd0f6Slogwang /* Get arguments */
743b2bd0f6Slogwang if (ac < 3)
753b2bd0f6Slogwang return (CMDRTN_USAGE);
763b2bd0f6Slogwang path = av[1];
773b2bd0f6Slogwang cmdstr = av[2];
783b2bd0f6Slogwang
793b2bd0f6Slogwang /* Put command and arguments back together as one string */
803b2bd0f6Slogwang for (len = 1, i = 3; i < ac; i++)
813b2bd0f6Slogwang len += strlen(av[i]) + 1;
823b2bd0f6Slogwang if ((buf = malloc(len)) == NULL) {
833b2bd0f6Slogwang warn("malloc");
843b2bd0f6Slogwang return (CMDRTN_ERROR);
853b2bd0f6Slogwang }
863b2bd0f6Slogwang for (*buf = '\0', i = 3; i < ac; i++) {
873b2bd0f6Slogwang snprintf(buf + strlen(buf),
883b2bd0f6Slogwang len - strlen(buf), " %s", av[i]);
893b2bd0f6Slogwang }
903b2bd0f6Slogwang
913b2bd0f6Slogwang /* Send it */
923b2bd0f6Slogwang if (NgSendAsciiMsg(csock, path, "%s%s", cmdstr, buf) < 0) {
933b2bd0f6Slogwang free(buf);
943b2bd0f6Slogwang warn("send msg");
953b2bd0f6Slogwang return (CMDRTN_ERROR);
963b2bd0f6Slogwang }
973b2bd0f6Slogwang free(buf);
983b2bd0f6Slogwang
99*d4a07e70Sfengbojiang #ifndef FSTACK
1003b2bd0f6Slogwang /* See if a synchronous reply awaits */
1013b2bd0f6Slogwang {
1023b2bd0f6Slogwang struct timeval tv;
1033b2bd0f6Slogwang fd_set rfds;
1043b2bd0f6Slogwang
1053b2bd0f6Slogwang FD_ZERO(&rfds);
1063b2bd0f6Slogwang FD_SET(csock, &rfds);
1073b2bd0f6Slogwang memset(&tv, 0, sizeof(tv));
1083b2bd0f6Slogwang switch (select(csock + 1, &rfds, NULL, NULL, &tv)) {
1093b2bd0f6Slogwang case -1:
1103b2bd0f6Slogwang err(EX_OSERR, "select");
1113b2bd0f6Slogwang case 0:
1123b2bd0f6Slogwang break;
1133b2bd0f6Slogwang default:
1143b2bd0f6Slogwang MsgRead();
1153b2bd0f6Slogwang break;
1163b2bd0f6Slogwang }
1173b2bd0f6Slogwang }
118*d4a07e70Sfengbojiang #else
119*d4a07e70Sfengbojiang MsgRead();
120*d4a07e70Sfengbojiang #endif
1213b2bd0f6Slogwang
1223b2bd0f6Slogwang /* Done */
1233b2bd0f6Slogwang return (CMDRTN_OK);
1243b2bd0f6Slogwang }
1253b2bd0f6Slogwang
1263b2bd0f6Slogwang /*
1273b2bd0f6Slogwang * Read and display the next incoming control message
1283b2bd0f6Slogwang */
1293b2bd0f6Slogwang void
MsgRead(void)1303b2bd0f6Slogwang MsgRead(void)
1313b2bd0f6Slogwang {
1323b2bd0f6Slogwang struct ng_mesg *m, *m2;
1333b2bd0f6Slogwang struct ng_mesg *ascii;
1343b2bd0f6Slogwang char path[NG_PATHSIZ];
1353b2bd0f6Slogwang
1363b2bd0f6Slogwang /* Get incoming message (in binary form) */
1373b2bd0f6Slogwang if (NgAllocRecvMsg(csock, &m, path) < 0) {
1383b2bd0f6Slogwang warn("recv incoming message");
1393b2bd0f6Slogwang return;
1403b2bd0f6Slogwang }
1413b2bd0f6Slogwang
1423b2bd0f6Slogwang /* Ask originating node to convert message to ASCII */
1433b2bd0f6Slogwang if (NgSendMsg(csock, path, NGM_GENERIC_COOKIE,
1443b2bd0f6Slogwang NGM_BINARY2ASCII, m, sizeof(*m) + m->header.arglen) < 0
1453b2bd0f6Slogwang || NgAllocRecvMsg(csock, &m2, NULL) < 0) {
1463b2bd0f6Slogwang printf("Rec'd %s %d from \"%s\":\n",
1473b2bd0f6Slogwang (m->header.flags & NGF_RESP) != 0 ? "response" : "command",
1483b2bd0f6Slogwang m->header.cmd, path);
1493b2bd0f6Slogwang if (m->header.arglen == 0)
1503b2bd0f6Slogwang printf("No arguments\n");
1513b2bd0f6Slogwang else
1523b2bd0f6Slogwang DumpAscii((const u_char *)m->data, m->header.arglen);
1533b2bd0f6Slogwang free(m);
1543b2bd0f6Slogwang return;
1553b2bd0f6Slogwang }
1563b2bd0f6Slogwang
1573b2bd0f6Slogwang /* Display message in ASCII form */
1583b2bd0f6Slogwang free(m);
1593b2bd0f6Slogwang ascii = (struct ng_mesg *)m2->data;
1603b2bd0f6Slogwang printf("Rec'd %s \"%s\" (%d) from \"%s\":\n",
1613b2bd0f6Slogwang (ascii->header.flags & NGF_RESP) != 0 ? "response" : "command",
1623b2bd0f6Slogwang ascii->header.cmdstr, ascii->header.cmd, path);
1633b2bd0f6Slogwang if (*ascii->data != '\0')
1643b2bd0f6Slogwang printf("Args:\t%s\n", ascii->data);
1653b2bd0f6Slogwang else
1663b2bd0f6Slogwang printf("No arguments\n");
1673b2bd0f6Slogwang free(m2);
1683b2bd0f6Slogwang }
1693b2bd0f6Slogwang
170