1 /* 2 * msg.c 3 * 4 * Copyright (c) 1999 Whistle Communications, Inc. 5 * All rights reserved. 6 * 7 * Subject to the following obligations and disclaimer of warranty, use and 8 * redistribution of this software, in source or object code forms, with or 9 * without modifications are expressly permitted by Whistle Communications; 10 * provided, however, that: 11 * 1. Any and all reproductions of the source or object code must include the 12 * copyright notice above and the following disclaimer of warranties; and 13 * 2. No rights are granted, in any manner or form, to use Whistle 14 * Communications, Inc. trademarks, including the mark "WHISTLE 15 * COMMUNICATIONS" on advertising, endorsements, or otherwise except as 16 * such appears in the above copyright notice or in the software. 17 * 18 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND 19 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO 20 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, 21 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF 22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. 23 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY 24 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS 25 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE. 26 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES 27 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING 28 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 29 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR 30 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY 31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 33 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY 34 * OF SUCH DAMAGE. 35 * 36 * $Whistle: msg.c,v 1.2 1999/11/29 23:38:35 archie Exp $ 37 */ 38 39 #include <err.h> 40 #include <netgraph.h> 41 #include <poll.h> 42 #include <stdio.h> 43 #include <stdlib.h> 44 #include <string.h> 45 #include <sysexits.h> 46 #include <unistd.h> 47 48 #include "ngctl.h" 49 50 static int MsgCmd(int ac, char **av); 51 52 const struct ngcmd msg_cmd = { 53 MsgCmd, 54 "msg path command [args ... ]", 55 "Send a netgraph control message to the node at \"path\"", 56 "The msg command constructs a netgraph control message from the" 57 " command name and ASCII arguments (if any) and sends that message" 58 " to the node. It does this by first asking the node to convert" 59 " the ASCII message into binary format, and resending the result.", 60 { "cmd" } 61 }; 62 63 static int 64 MsgCmd(int ac, char **av) 65 { 66 struct pollfd pfds[1] = { 67 { .fd = csock, .events = POLLIN }, 68 }; 69 char *buf; 70 char *path, *cmdstr; 71 int i, len; 72 73 /* Get arguments */ 74 if (ac < 3) 75 return (CMDRTN_USAGE); 76 path = av[1]; 77 cmdstr = av[2]; 78 79 /* Put command and arguments back together as one string */ 80 for (len = 1, i = 3; i < ac; i++) 81 len += strlen(av[i]) + 1; 82 if ((buf = malloc(len)) == NULL) { 83 warn("malloc"); 84 return (CMDRTN_ERROR); 85 } 86 for (*buf = '\0', i = 3; i < ac; i++) { 87 snprintf(buf + strlen(buf), 88 len - strlen(buf), " %s", av[i]); 89 } 90 91 /* Send it */ 92 if (NgSendAsciiMsg(csock, path, "%s%s", cmdstr, buf) < 0) { 93 free(buf); 94 warn("send msg"); 95 return (CMDRTN_ERROR); 96 } 97 free(buf); 98 99 /* See if a synchronous reply awaits */ 100 switch (poll(pfds, 1, 0)) { 101 case -1: 102 err(EX_OSERR, "poll"); 103 case 0: 104 break; 105 default: 106 MsgRead(); 107 break; 108 } 109 110 /* Done */ 111 return (CMDRTN_OK); 112 } 113 114 /* 115 * Read and display the next incoming control message 116 */ 117 void 118 MsgRead(void) 119 { 120 struct ng_mesg *m, *m2; 121 struct ng_mesg *ascii; 122 char path[NG_PATHSIZ]; 123 124 /* Get incoming message (in binary form) */ 125 if (NgAllocRecvMsg(csock, &m, path) < 0) { 126 warn("recv incoming message"); 127 return; 128 } 129 130 /* Ask originating node to convert message to ASCII */ 131 if (NgSendMsg(csock, path, NGM_GENERIC_COOKIE, 132 NGM_BINARY2ASCII, m, sizeof(*m) + m->header.arglen) < 0 || 133 NgAllocRecvMsg(csock, &m2, NULL) < 0) { 134 printf("Rec'd %s %d from \"%s\":\n", 135 (m->header.flags & NGF_RESP) != 0 ? "response" : "command", 136 m->header.cmd, path); 137 if (m->header.arglen == 0) 138 printf("No arguments\n"); 139 else 140 DumpAscii((const u_char *)m->data, m->header.arglen); 141 free(m); 142 return; 143 } 144 145 /* Display message in ASCII form */ 146 free(m); 147 ascii = (struct ng_mesg *)m2->data; 148 printf("Rec'd %s \"%s\" (%d) from \"%s\":\n", 149 (ascii->header.flags & NGF_RESP) != 0 ? "response" : "command", 150 ascii->header.cmdstr, ascii->header.cmd, path); 151 if (*ascii->data != '\0') 152 printf("Args:\t%s\n", ascii->data); 153 else 154 printf("No arguments\n"); 155 free(m2); 156 } 157