153913Sarchie 253913Sarchie/* 353913Sarchie * msg.c 453913Sarchie * 553913Sarchie * Copyright (c) 1999 Whistle Communications, Inc. 653913Sarchie * All rights reserved. 753913Sarchie * 853913Sarchie * Subject to the following obligations and disclaimer of warranty, use and 953913Sarchie * redistribution of this software, in source or object code forms, with or 1053913Sarchie * without modifications are expressly permitted by Whistle Communications; 1153913Sarchie * provided, however, that: 1253913Sarchie * 1. Any and all reproductions of the source or object code must include the 1353913Sarchie * copyright notice above and the following disclaimer of warranties; and 1453913Sarchie * 2. No rights are granted, in any manner or form, to use Whistle 1553913Sarchie * Communications, Inc. trademarks, including the mark "WHISTLE 1653913Sarchie * COMMUNICATIONS" on advertising, endorsements, or otherwise except as 1753913Sarchie * such appears in the above copyright notice or in the software. 1853913Sarchie * 1953913Sarchie * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND 2053913Sarchie * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO 2153913Sarchie * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, 2253913Sarchie * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF 2353913Sarchie * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. 2453913Sarchie * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY 2553913Sarchie * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS 2653913Sarchie * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE. 2753913Sarchie * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES 2853913Sarchie * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING 2953913Sarchie * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 3053913Sarchie * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR 3153913Sarchie * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY 3253913Sarchie * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 3353913Sarchie * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 3453913Sarchie * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY 3553913Sarchie * OF SUCH DAMAGE. 3653913Sarchie * 3753913Sarchie * $Whistle: msg.c,v 1.2 1999/11/29 23:38:35 archie Exp $ 3853913Sarchie */ 3953913Sarchie 40216588Scharnier#include <sys/cdefs.h> 41216588Scharnier__FBSDID("$FreeBSD$"); 42216588Scharnier 43158882Sglebius#include <err.h> 44158882Sglebius#include <netgraph.h> 45158882Sglebius#include <stdio.h> 46158882Sglebius#include <stdlib.h> 47158882Sglebius#include <string.h> 48158882Sglebius#include <sysexits.h> 49158882Sglebius#include <unistd.h> 50158882Sglebius 5153913Sarchie#include "ngctl.h" 5253913Sarchie 5353913Sarchiestatic int MsgCmd(int ac, char **av); 5453913Sarchie 5553913Sarchieconst struct ngcmd msg_cmd = { 5653913Sarchie MsgCmd, 5753913Sarchie "msg path command [args ... ]", 5853913Sarchie "Send a netgraph control message to the node at \"path\"", 5953913Sarchie "The msg command constructs a netgraph control message from the" 6053913Sarchie " command name and ASCII arguments (if any) and sends that message" 6153913Sarchie " to the node. It does this by first asking the node to convert" 6258018Sarchie " the ASCII message into binary format, and resending the result.", 6353913Sarchie { "cmd" } 6453913Sarchie}; 6553913Sarchie 6653913Sarchiestatic int 6753913SarchieMsgCmd(int ac, char **av) 6853913Sarchie{ 69125115Sru char *buf; 7053913Sarchie char *path, *cmdstr; 71125115Sru int i, len; 7253913Sarchie 7353913Sarchie /* Get arguments */ 7453913Sarchie if (ac < 3) 75160002Sglebius return (CMDRTN_USAGE); 7653913Sarchie path = av[1]; 7753913Sarchie cmdstr = av[2]; 7853913Sarchie 7953913Sarchie /* Put command and arguments back together as one string */ 80125115Sru for (len = 1, i = 3; i < ac; i++) 81125115Sru len += strlen(av[i]) + 1; 82125115Sru if ((buf = malloc(len)) == NULL) { 83125115Sru warn("malloc"); 84160002Sglebius return (CMDRTN_ERROR); 85125115Sru } 8653913Sarchie for (*buf = '\0', i = 3; i < ac; i++) { 8753913Sarchie snprintf(buf + strlen(buf), 88125115Sru len - strlen(buf), " %s", av[i]); 8953913Sarchie } 9053913Sarchie 9153913Sarchie /* Send it */ 9253913Sarchie if (NgSendAsciiMsg(csock, path, "%s%s", cmdstr, buf) < 0) { 93125115Sru free(buf); 9453913Sarchie warn("send msg"); 95160002Sglebius return (CMDRTN_ERROR); 9653913Sarchie } 97125115Sru free(buf); 9853913Sarchie 9961880Sarchie /* See if a synchronous reply awaits */ 10061880Sarchie { 10161880Sarchie struct timeval tv; 10261880Sarchie fd_set rfds; 10361880Sarchie 10461880Sarchie FD_ZERO(&rfds); 10561880Sarchie FD_SET(csock, &rfds); 10661880Sarchie memset(&tv, 0, sizeof(tv)); 10761880Sarchie switch (select(csock + 1, &rfds, NULL, NULL, &tv)) { 10861880Sarchie case -1: 10961880Sarchie err(EX_OSERR, "select"); 11061880Sarchie case 0: 11161880Sarchie break; 11261880Sarchie default: 11361880Sarchie MsgRead(); 11461880Sarchie break; 11561880Sarchie } 11661880Sarchie } 11761880Sarchie 11853913Sarchie /* Done */ 119160002Sglebius return (CMDRTN_OK); 12053913Sarchie} 12153913Sarchie 12261880Sarchie/* 12361880Sarchie * Read and display the next incoming control message 12461880Sarchie */ 12561880Sarchievoid 126216588ScharnierMsgRead(void) 12761880Sarchie{ 128125115Sru struct ng_mesg *m, *m2; 129125115Sru struct ng_mesg *ascii; 130122556Sharti char path[NG_PATHSIZ]; 13161880Sarchie 13261880Sarchie /* Get incoming message (in binary form) */ 133125115Sru if (NgAllocRecvMsg(csock, &m, path) < 0) { 13461880Sarchie warn("recv incoming message"); 13561880Sarchie return; 13661880Sarchie } 13761880Sarchie 13861880Sarchie /* Ask originating node to convert message to ASCII */ 13961880Sarchie if (NgSendMsg(csock, path, NGM_GENERIC_COOKIE, 14061880Sarchie NGM_BINARY2ASCII, m, sizeof(*m) + m->header.arglen) < 0 141125115Sru || NgAllocRecvMsg(csock, &m2, NULL) < 0) { 14261880Sarchie printf("Rec'd %s %d from \"%s\":\n", 14361880Sarchie (m->header.flags & NGF_RESP) != 0 ? "response" : "command", 14461880Sarchie m->header.cmd, path); 14561880Sarchie if (m->header.arglen == 0) 14661880Sarchie printf("No arguments\n"); 14761880Sarchie else 148162491Skan DumpAscii((const u_char *)m->data, m->header.arglen); 149125115Sru free(m); 15061880Sarchie return; 15161880Sarchie } 15261880Sarchie 15361880Sarchie /* Display message in ASCII form */ 154125115Sru free(m); 155125115Sru ascii = (struct ng_mesg *)m2->data; 15661880Sarchie printf("Rec'd %s \"%s\" (%d) from \"%s\":\n", 15761880Sarchie (ascii->header.flags & NGF_RESP) != 0 ? "response" : "command", 15861880Sarchie ascii->header.cmdstr, ascii->header.cmd, path); 15961880Sarchie if (*ascii->data != '\0') 16061880Sarchie printf("Args:\t%s\n", ascii->data); 16161880Sarchie else 16261880Sarchie printf("No arguments\n"); 163125115Sru free(m2); 16461880Sarchie} 16561880Sarchie 166