msg.c revision 160002
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 * $FreeBSD: head/usr.sbin/ngctl/msg.c 160002 2006-06-28 10:38:38Z glebius $ 3953913Sarchie */ 4053913Sarchie 41158882Sglebius#include <err.h> 42158882Sglebius#include <netgraph.h> 43158882Sglebius#include <stdio.h> 44158882Sglebius#include <stdlib.h> 45158882Sglebius#include <string.h> 46158882Sglebius#include <sysexits.h> 47158882Sglebius#include <unistd.h> 48158882Sglebius 4953913Sarchie#include "ngctl.h" 5053913Sarchie 5153913Sarchiestatic int MsgCmd(int ac, char **av); 5253913Sarchie 5353913Sarchieconst struct ngcmd msg_cmd = { 5453913Sarchie MsgCmd, 5553913Sarchie "msg path command [args ... ]", 5653913Sarchie "Send a netgraph control message to the node at \"path\"", 5753913Sarchie "The msg command constructs a netgraph control message from the" 5853913Sarchie " command name and ASCII arguments (if any) and sends that message" 5953913Sarchie " to the node. It does this by first asking the node to convert" 6058018Sarchie " the ASCII message into binary format, and resending the result.", 6153913Sarchie { "cmd" } 6253913Sarchie}; 6353913Sarchie 6453913Sarchiestatic int 6553913SarchieMsgCmd(int ac, char **av) 6653913Sarchie{ 67125115Sru char *buf; 6853913Sarchie char *path, *cmdstr; 69125115Sru int i, len; 7053913Sarchie 7153913Sarchie /* Get arguments */ 7253913Sarchie if (ac < 3) 73160002Sglebius return (CMDRTN_USAGE); 7453913Sarchie path = av[1]; 7553913Sarchie cmdstr = av[2]; 7653913Sarchie 7753913Sarchie /* Put command and arguments back together as one string */ 78125115Sru for (len = 1, i = 3; i < ac; i++) 79125115Sru len += strlen(av[i]) + 1; 80125115Sru if ((buf = malloc(len)) == NULL) { 81125115Sru warn("malloc"); 82160002Sglebius return (CMDRTN_ERROR); 83125115Sru } 8453913Sarchie for (*buf = '\0', i = 3; i < ac; i++) { 8553913Sarchie snprintf(buf + strlen(buf), 86125115Sru len - strlen(buf), " %s", av[i]); 8753913Sarchie } 8853913Sarchie 8953913Sarchie /* Send it */ 9053913Sarchie if (NgSendAsciiMsg(csock, path, "%s%s", cmdstr, buf) < 0) { 91125115Sru free(buf); 9253913Sarchie warn("send msg"); 93160002Sglebius return (CMDRTN_ERROR); 9453913Sarchie } 95125115Sru free(buf); 9653913Sarchie 9761880Sarchie /* See if a synchronous reply awaits */ 9861880Sarchie { 9961880Sarchie struct timeval tv; 10061880Sarchie fd_set rfds; 10161880Sarchie 10261880Sarchie FD_ZERO(&rfds); 10361880Sarchie FD_SET(csock, &rfds); 10461880Sarchie memset(&tv, 0, sizeof(tv)); 10561880Sarchie switch (select(csock + 1, &rfds, NULL, NULL, &tv)) { 10661880Sarchie case -1: 10761880Sarchie err(EX_OSERR, "select"); 10861880Sarchie case 0: 10961880Sarchie break; 11061880Sarchie default: 11161880Sarchie MsgRead(); 11261880Sarchie break; 11361880Sarchie } 11461880Sarchie } 11561880Sarchie 11653913Sarchie /* Done */ 117160002Sglebius return (CMDRTN_OK); 11853913Sarchie} 11953913Sarchie 12061880Sarchie/* 12161880Sarchie * Read and display the next incoming control message 12261880Sarchie */ 12361880Sarchievoid 12461880SarchieMsgRead() 12561880Sarchie{ 126125115Sru struct ng_mesg *m, *m2; 127125115Sru struct ng_mesg *ascii; 128122556Sharti char path[NG_PATHSIZ]; 12961880Sarchie 13061880Sarchie /* Get incoming message (in binary form) */ 131125115Sru if (NgAllocRecvMsg(csock, &m, path) < 0) { 13261880Sarchie warn("recv incoming message"); 13361880Sarchie return; 13461880Sarchie } 13561880Sarchie 13661880Sarchie /* Ask originating node to convert message to ASCII */ 13761880Sarchie if (NgSendMsg(csock, path, NGM_GENERIC_COOKIE, 13861880Sarchie NGM_BINARY2ASCII, m, sizeof(*m) + m->header.arglen) < 0 139125115Sru || NgAllocRecvMsg(csock, &m2, NULL) < 0) { 14061880Sarchie printf("Rec'd %s %d from \"%s\":\n", 14161880Sarchie (m->header.flags & NGF_RESP) != 0 ? "response" : "command", 14261880Sarchie m->header.cmd, path); 14361880Sarchie if (m->header.arglen == 0) 14461880Sarchie printf("No arguments\n"); 14561880Sarchie else 14661880Sarchie DumpAscii(m->data, m->header.arglen); 147125115Sru free(m); 14861880Sarchie return; 14961880Sarchie } 15061880Sarchie 15161880Sarchie /* Display message in ASCII form */ 152125115Sru free(m); 153125115Sru ascii = (struct ng_mesg *)m2->data; 15461880Sarchie printf("Rec'd %s \"%s\" (%d) from \"%s\":\n", 15561880Sarchie (ascii->header.flags & NGF_RESP) != 0 ? "response" : "command", 15661880Sarchie ascii->header.cmdstr, ascii->header.cmd, path); 15761880Sarchie if (*ascii->data != '\0') 15861880Sarchie printf("Args:\t%s\n", ascii->data); 15961880Sarchie else 16061880Sarchie printf("No arguments\n"); 161125115Sru free(m2); 16261880Sarchie} 16361880Sarchie 164