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 * $FreeBSD$ 39f8307e12SArchie Cobbs */ 40f8307e12SArchie Cobbs 41f8307e12SArchie Cobbs #include "ngctl.h" 42f8307e12SArchie Cobbs 43f8307e12SArchie Cobbs static int MsgCmd(int ac, char **av); 44f8307e12SArchie Cobbs 45f8307e12SArchie Cobbs const struct ngcmd msg_cmd = { 46f8307e12SArchie Cobbs MsgCmd, 47f8307e12SArchie Cobbs "msg path command [args ... ]", 48f8307e12SArchie Cobbs "Send a netgraph control message to the node at \"path\"", 49f8307e12SArchie Cobbs "The msg command constructs a netgraph control message from the" 50f8307e12SArchie Cobbs " command name and ASCII arguments (if any) and sends that message" 51f8307e12SArchie Cobbs " to the node. It does this by first asking the node to convert" 52b1ea6388SArchie Cobbs " the ASCII message into binary format, and resending the result.", 53f8307e12SArchie Cobbs { "cmd" } 54f8307e12SArchie Cobbs }; 55f8307e12SArchie Cobbs 56f8307e12SArchie Cobbs static int 57f8307e12SArchie Cobbs MsgCmd(int ac, char **av) 58f8307e12SArchie Cobbs { 599fe5fadaSRuslan Ermilov char *buf; 60f8307e12SArchie Cobbs char *path, *cmdstr; 619fe5fadaSRuslan Ermilov int i, len; 62f8307e12SArchie Cobbs 63f8307e12SArchie Cobbs /* Get arguments */ 64f8307e12SArchie Cobbs if (ac < 3) 65f8307e12SArchie Cobbs return(CMDRTN_USAGE); 66f8307e12SArchie Cobbs path = av[1]; 67f8307e12SArchie Cobbs cmdstr = av[2]; 68f8307e12SArchie Cobbs 69f8307e12SArchie Cobbs /* Put command and arguments back together as one string */ 709fe5fadaSRuslan Ermilov for (len = 1, i = 3; i < ac; i++) 719fe5fadaSRuslan Ermilov len += strlen(av[i]) + 1; 729fe5fadaSRuslan Ermilov if ((buf = malloc(len)) == NULL) { 739fe5fadaSRuslan Ermilov warn("malloc"); 749fe5fadaSRuslan Ermilov return(CMDRTN_ERROR); 759fe5fadaSRuslan Ermilov } 76f8307e12SArchie Cobbs for (*buf = '\0', i = 3; i < ac; i++) { 77f8307e12SArchie Cobbs snprintf(buf + strlen(buf), 789fe5fadaSRuslan Ermilov len - strlen(buf), " %s", av[i]); 79f8307e12SArchie Cobbs } 80f8307e12SArchie Cobbs 81f8307e12SArchie Cobbs /* Send it */ 82f8307e12SArchie Cobbs if (NgSendAsciiMsg(csock, path, "%s%s", cmdstr, buf) < 0) { 839fe5fadaSRuslan Ermilov free(buf); 84f8307e12SArchie Cobbs warn("send msg"); 85f8307e12SArchie Cobbs return(CMDRTN_ERROR); 86f8307e12SArchie Cobbs } 879fe5fadaSRuslan Ermilov free(buf); 88f8307e12SArchie Cobbs 896ebb8ebbSArchie Cobbs /* See if a synchronous reply awaits */ 906ebb8ebbSArchie Cobbs { 916ebb8ebbSArchie Cobbs struct timeval tv; 926ebb8ebbSArchie Cobbs fd_set rfds; 936ebb8ebbSArchie Cobbs 946ebb8ebbSArchie Cobbs FD_ZERO(&rfds); 956ebb8ebbSArchie Cobbs FD_SET(csock, &rfds); 966ebb8ebbSArchie Cobbs memset(&tv, 0, sizeof(tv)); 976ebb8ebbSArchie Cobbs switch (select(csock + 1, &rfds, NULL, NULL, &tv)) { 986ebb8ebbSArchie Cobbs case -1: 996ebb8ebbSArchie Cobbs err(EX_OSERR, "select"); 1006ebb8ebbSArchie Cobbs case 0: 1016ebb8ebbSArchie Cobbs break; 1026ebb8ebbSArchie Cobbs default: 1036ebb8ebbSArchie Cobbs MsgRead(); 1046ebb8ebbSArchie Cobbs break; 1056ebb8ebbSArchie Cobbs } 1066ebb8ebbSArchie Cobbs } 1076ebb8ebbSArchie Cobbs 108f8307e12SArchie Cobbs /* Done */ 109f8307e12SArchie Cobbs return(CMDRTN_OK); 110f8307e12SArchie Cobbs } 111f8307e12SArchie Cobbs 1126ebb8ebbSArchie Cobbs /* 1136ebb8ebbSArchie Cobbs * Read and display the next incoming control message 1146ebb8ebbSArchie Cobbs */ 1156ebb8ebbSArchie Cobbs void 1166ebb8ebbSArchie Cobbs MsgRead() 1176ebb8ebbSArchie Cobbs { 1189fe5fadaSRuslan Ermilov struct ng_mesg *m, *m2; 1199fe5fadaSRuslan Ermilov struct ng_mesg *ascii; 1209d901d3bSHartmut Brandt char path[NG_PATHSIZ]; 1216ebb8ebbSArchie Cobbs 1226ebb8ebbSArchie Cobbs /* Get incoming message (in binary form) */ 1239fe5fadaSRuslan Ermilov if (NgAllocRecvMsg(csock, &m, path) < 0) { 1246ebb8ebbSArchie Cobbs warn("recv incoming message"); 1256ebb8ebbSArchie Cobbs return; 1266ebb8ebbSArchie Cobbs } 1276ebb8ebbSArchie Cobbs 1286ebb8ebbSArchie Cobbs /* Ask originating node to convert message to ASCII */ 1296ebb8ebbSArchie Cobbs if (NgSendMsg(csock, path, NGM_GENERIC_COOKIE, 1306ebb8ebbSArchie Cobbs NGM_BINARY2ASCII, m, sizeof(*m) + m->header.arglen) < 0 1319fe5fadaSRuslan Ermilov || NgAllocRecvMsg(csock, &m2, NULL) < 0) { 1326ebb8ebbSArchie Cobbs printf("Rec'd %s %d from \"%s\":\n", 1336ebb8ebbSArchie Cobbs (m->header.flags & NGF_RESP) != 0 ? "response" : "command", 1346ebb8ebbSArchie Cobbs m->header.cmd, path); 1356ebb8ebbSArchie Cobbs if (m->header.arglen == 0) 1366ebb8ebbSArchie Cobbs printf("No arguments\n"); 1376ebb8ebbSArchie Cobbs else 1386ebb8ebbSArchie Cobbs DumpAscii(m->data, m->header.arglen); 1399fe5fadaSRuslan Ermilov free(m); 1406ebb8ebbSArchie Cobbs return; 1416ebb8ebbSArchie Cobbs } 1426ebb8ebbSArchie Cobbs 1436ebb8ebbSArchie Cobbs /* Display message in ASCII form */ 1449fe5fadaSRuslan Ermilov free(m); 1459fe5fadaSRuslan Ermilov ascii = (struct ng_mesg *)m2->data; 1466ebb8ebbSArchie Cobbs printf("Rec'd %s \"%s\" (%d) from \"%s\":\n", 1476ebb8ebbSArchie Cobbs (ascii->header.flags & NGF_RESP) != 0 ? "response" : "command", 1486ebb8ebbSArchie Cobbs ascii->header.cmdstr, ascii->header.cmd, path); 1496ebb8ebbSArchie Cobbs if (*ascii->data != '\0') 1506ebb8ebbSArchie Cobbs printf("Args:\t%s\n", ascii->data); 1516ebb8ebbSArchie Cobbs else 1526ebb8ebbSArchie Cobbs printf("No arguments\n"); 1539fe5fadaSRuslan Ermilov free(m2); 1546ebb8ebbSArchie Cobbs } 1556ebb8ebbSArchie Cobbs 156