1f8307e12SArchie Cobbs 2f8307e12SArchie Cobbs /* 3f8307e12SArchie Cobbs * msg.c 4f8307e12SArchie Cobbs * 5f8307e12SArchie Cobbs * Copyright (c) 1999 Whistle Communications, Inc. 6f8307e12SArchie Cobbs * All rights reserved. 7f8307e12SArchie Cobbs * 8f8307e12SArchie Cobbs * Subject to the following obligations and disclaimer of warranty, use and 9f8307e12SArchie Cobbs * redistribution of this software, in source or object code forms, with or 10f8307e12SArchie Cobbs * without modifications are expressly permitted by Whistle Communications; 11f8307e12SArchie Cobbs * provided, however, that: 12f8307e12SArchie Cobbs * 1. Any and all reproductions of the source or object code must include the 13f8307e12SArchie Cobbs * copyright notice above and the following disclaimer of warranties; and 14f8307e12SArchie Cobbs * 2. No rights are granted, in any manner or form, to use Whistle 15f8307e12SArchie Cobbs * Communications, Inc. trademarks, including the mark "WHISTLE 16f8307e12SArchie Cobbs * COMMUNICATIONS" on advertising, endorsements, or otherwise except as 17f8307e12SArchie Cobbs * such appears in the above copyright notice or in the software. 18f8307e12SArchie Cobbs * 19f8307e12SArchie Cobbs * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND 20f8307e12SArchie Cobbs * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO 21f8307e12SArchie Cobbs * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, 22f8307e12SArchie Cobbs * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF 23f8307e12SArchie Cobbs * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. 24f8307e12SArchie Cobbs * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY 25f8307e12SArchie Cobbs * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS 26f8307e12SArchie Cobbs * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE. 27f8307e12SArchie Cobbs * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES 28f8307e12SArchie Cobbs * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING 29f8307e12SArchie Cobbs * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 30f8307e12SArchie Cobbs * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR 31f8307e12SArchie Cobbs * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY 32f8307e12SArchie Cobbs * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 33f8307e12SArchie Cobbs * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 34f8307e12SArchie Cobbs * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY 35f8307e12SArchie Cobbs * OF SUCH DAMAGE. 36f8307e12SArchie Cobbs * 37f8307e12SArchie Cobbs * $Whistle: msg.c,v 1.2 1999/11/29 23:38:35 archie Exp $ 38f8307e12SArchie Cobbs */ 39f8307e12SArchie Cobbs 40*d31ff7a8SPhilippe Charnier #include <sys/cdefs.h> 41*d31ff7a8SPhilippe Charnier __FBSDID("$FreeBSD$"); 42*d31ff7a8SPhilippe Charnier 4378cdd8edSGleb Smirnoff #include <err.h> 4478cdd8edSGleb Smirnoff #include <netgraph.h> 4578cdd8edSGleb Smirnoff #include <stdio.h> 4678cdd8edSGleb Smirnoff #include <stdlib.h> 4778cdd8edSGleb Smirnoff #include <string.h> 4878cdd8edSGleb Smirnoff #include <sysexits.h> 4978cdd8edSGleb Smirnoff #include <unistd.h> 5078cdd8edSGleb Smirnoff 51f8307e12SArchie Cobbs #include "ngctl.h" 52f8307e12SArchie Cobbs 53f8307e12SArchie Cobbs static int MsgCmd(int ac, char **av); 54f8307e12SArchie Cobbs 55f8307e12SArchie Cobbs const struct ngcmd msg_cmd = { 56f8307e12SArchie Cobbs MsgCmd, 57f8307e12SArchie Cobbs "msg path command [args ... ]", 58f8307e12SArchie Cobbs "Send a netgraph control message to the node at \"path\"", 59f8307e12SArchie Cobbs "The msg command constructs a netgraph control message from the" 60f8307e12SArchie Cobbs " command name and ASCII arguments (if any) and sends that message" 61f8307e12SArchie Cobbs " to the node. It does this by first asking the node to convert" 62b1ea6388SArchie Cobbs " the ASCII message into binary format, and resending the result.", 63f8307e12SArchie Cobbs { "cmd" } 64f8307e12SArchie Cobbs }; 65f8307e12SArchie Cobbs 66f8307e12SArchie Cobbs static int 67f8307e12SArchie Cobbs MsgCmd(int ac, char **av) 68f8307e12SArchie Cobbs { 699fe5fadaSRuslan Ermilov char *buf; 70f8307e12SArchie Cobbs char *path, *cmdstr; 719fe5fadaSRuslan Ermilov int i, len; 72f8307e12SArchie Cobbs 73f8307e12SArchie Cobbs /* Get arguments */ 74f8307e12SArchie Cobbs if (ac < 3) 75f8307e12SArchie Cobbs return (CMDRTN_USAGE); 76f8307e12SArchie Cobbs path = av[1]; 77f8307e12SArchie Cobbs cmdstr = av[2]; 78f8307e12SArchie Cobbs 79f8307e12SArchie Cobbs /* Put command and arguments back together as one string */ 809fe5fadaSRuslan Ermilov for (len = 1, i = 3; i < ac; i++) 819fe5fadaSRuslan Ermilov len += strlen(av[i]) + 1; 829fe5fadaSRuslan Ermilov if ((buf = malloc(len)) == NULL) { 839fe5fadaSRuslan Ermilov warn("malloc"); 849fe5fadaSRuslan Ermilov return (CMDRTN_ERROR); 859fe5fadaSRuslan Ermilov } 86f8307e12SArchie Cobbs for (*buf = '\0', i = 3; i < ac; i++) { 87f8307e12SArchie Cobbs snprintf(buf + strlen(buf), 889fe5fadaSRuslan Ermilov len - strlen(buf), " %s", av[i]); 89f8307e12SArchie Cobbs } 90f8307e12SArchie Cobbs 91f8307e12SArchie Cobbs /* Send it */ 92f8307e12SArchie Cobbs if (NgSendAsciiMsg(csock, path, "%s%s", cmdstr, buf) < 0) { 939fe5fadaSRuslan Ermilov free(buf); 94f8307e12SArchie Cobbs warn("send msg"); 95f8307e12SArchie Cobbs return (CMDRTN_ERROR); 96f8307e12SArchie Cobbs } 979fe5fadaSRuslan Ermilov free(buf); 98f8307e12SArchie Cobbs 996ebb8ebbSArchie Cobbs /* See if a synchronous reply awaits */ 1006ebb8ebbSArchie Cobbs { 1016ebb8ebbSArchie Cobbs struct timeval tv; 1026ebb8ebbSArchie Cobbs fd_set rfds; 1036ebb8ebbSArchie Cobbs 1046ebb8ebbSArchie Cobbs FD_ZERO(&rfds); 1056ebb8ebbSArchie Cobbs FD_SET(csock, &rfds); 1066ebb8ebbSArchie Cobbs memset(&tv, 0, sizeof(tv)); 1076ebb8ebbSArchie Cobbs switch (select(csock + 1, &rfds, NULL, NULL, &tv)) { 1086ebb8ebbSArchie Cobbs case -1: 1096ebb8ebbSArchie Cobbs err(EX_OSERR, "select"); 1106ebb8ebbSArchie Cobbs case 0: 1116ebb8ebbSArchie Cobbs break; 1126ebb8ebbSArchie Cobbs default: 1136ebb8ebbSArchie Cobbs MsgRead(); 1146ebb8ebbSArchie Cobbs break; 1156ebb8ebbSArchie Cobbs } 1166ebb8ebbSArchie Cobbs } 1176ebb8ebbSArchie Cobbs 118f8307e12SArchie Cobbs /* Done */ 119f8307e12SArchie Cobbs return (CMDRTN_OK); 120f8307e12SArchie Cobbs } 121f8307e12SArchie Cobbs 1226ebb8ebbSArchie Cobbs /* 1236ebb8ebbSArchie Cobbs * Read and display the next incoming control message 1246ebb8ebbSArchie Cobbs */ 1256ebb8ebbSArchie Cobbs void 126*d31ff7a8SPhilippe Charnier MsgRead(void) 1276ebb8ebbSArchie Cobbs { 1289fe5fadaSRuslan Ermilov struct ng_mesg *m, *m2; 1299fe5fadaSRuslan Ermilov struct ng_mesg *ascii; 1309d901d3bSHartmut Brandt char path[NG_PATHSIZ]; 1316ebb8ebbSArchie Cobbs 1326ebb8ebbSArchie Cobbs /* Get incoming message (in binary form) */ 1339fe5fadaSRuslan Ermilov if (NgAllocRecvMsg(csock, &m, path) < 0) { 1346ebb8ebbSArchie Cobbs warn("recv incoming message"); 1356ebb8ebbSArchie Cobbs return; 1366ebb8ebbSArchie Cobbs } 1376ebb8ebbSArchie Cobbs 1386ebb8ebbSArchie Cobbs /* Ask originating node to convert message to ASCII */ 1396ebb8ebbSArchie Cobbs if (NgSendMsg(csock, path, NGM_GENERIC_COOKIE, 1406ebb8ebbSArchie Cobbs NGM_BINARY2ASCII, m, sizeof(*m) + m->header.arglen) < 0 1419fe5fadaSRuslan Ermilov || NgAllocRecvMsg(csock, &m2, NULL) < 0) { 1426ebb8ebbSArchie Cobbs printf("Rec'd %s %d from \"%s\":\n", 1436ebb8ebbSArchie Cobbs (m->header.flags & NGF_RESP) != 0 ? "response" : "command", 1446ebb8ebbSArchie Cobbs m->header.cmd, path); 1456ebb8ebbSArchie Cobbs if (m->header.arglen == 0) 1466ebb8ebbSArchie Cobbs printf("No arguments\n"); 1476ebb8ebbSArchie Cobbs else 1489a419481SAlexander Kabaev DumpAscii((const u_char *)m->data, m->header.arglen); 1499fe5fadaSRuslan Ermilov free(m); 1506ebb8ebbSArchie Cobbs return; 1516ebb8ebbSArchie Cobbs } 1526ebb8ebbSArchie Cobbs 1536ebb8ebbSArchie Cobbs /* Display message in ASCII form */ 1549fe5fadaSRuslan Ermilov free(m); 1559fe5fadaSRuslan Ermilov ascii = (struct ng_mesg *)m2->data; 1566ebb8ebbSArchie Cobbs printf("Rec'd %s \"%s\" (%d) from \"%s\":\n", 1576ebb8ebbSArchie Cobbs (ascii->header.flags & NGF_RESP) != 0 ? "response" : "command", 1586ebb8ebbSArchie Cobbs ascii->header.cmdstr, ascii->header.cmd, path); 1596ebb8ebbSArchie Cobbs if (*ascii->data != '\0') 1606ebb8ebbSArchie Cobbs printf("Args:\t%s\n", ascii->data); 1616ebb8ebbSArchie Cobbs else 1626ebb8ebbSArchie Cobbs printf("No arguments\n"); 1639fe5fadaSRuslan Ermilov free(m2); 1646ebb8ebbSArchie Cobbs } 1656ebb8ebbSArchie Cobbs 166