1 /* 2 * msg.c 3 * 4 * Copyright (c) 1996-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 * Author: Archie Cobbs <archie@whistle.com> 37 * 38 * $Whistle: msg.c,v 1.9 1999/01/20 00:57:23 archie Exp $ 39 */ 40 41 #include <sys/cdefs.h> 42 __FBSDID("$FreeBSD$"); 43 44 #include <sys/types.h> 45 #include <stdarg.h> 46 #include <netgraph/ng_message.h> 47 #include <netgraph/ng_socket.h> 48 49 #include "netgraph.h" 50 #include "internal.h" 51 52 /* Next message token value */ 53 static int gMsgId; 54 55 /* For delivering both messages and replies */ 56 static int NgDeliverMsg(int cs, const char *path, 57 const struct ng_mesg *hdr, const void *args, size_t arglen); 58 59 /* 60 * Send a message to a node using control socket node "cs". 61 * Returns -1 if error and sets errno appropriately. 62 * If successful, returns the message ID (token) used. 63 */ 64 int 65 NgSendMsg(int cs, const char *path, 66 int cookie, int cmd, const void *args, size_t arglen) 67 { 68 struct ng_mesg msg; 69 70 /* Prepare message header */ 71 memset(&msg, 0, sizeof(msg)); 72 msg.header.version = NG_VERSION; 73 msg.header.typecookie = cookie; 74 if (++gMsgId < 0) 75 gMsgId = 1; 76 msg.header.token = gMsgId; 77 msg.header.flags = NGF_ORIG; 78 msg.header.cmd = cmd; 79 snprintf(msg.header.cmdstr, NG_CMDSTRSIZ, "cmd%d", cmd); 80 81 /* Deliver message */ 82 if (NgDeliverMsg(cs, path, &msg, args, arglen) < 0) 83 return (-1); 84 return (msg.header.token); 85 } 86 87 /* 88 * Send a message given in ASCII format. We first ask the node to translate 89 * the command into binary, and then we send the binary. 90 */ 91 int 92 NgSendAsciiMsg(int cs, const char *path, const char *fmt, ...) 93 { 94 const int bufSize = 1024; 95 char replybuf[2 * sizeof(struct ng_mesg) + bufSize]; 96 struct ng_mesg *const reply = (struct ng_mesg *)replybuf; 97 struct ng_mesg *const binary = (struct ng_mesg *)reply->data; 98 struct ng_mesg *ascii; 99 char *buf, *cmd, *args; 100 va_list fmtargs; 101 102 /* Parse out command and arguments */ 103 va_start(fmtargs, fmt); 104 vasprintf(&buf, fmt, fmtargs); 105 va_end(fmtargs); 106 if (buf == NULL) 107 return (-1); 108 109 /* Parse out command, arguments */ 110 for (cmd = buf; isspace(*cmd); cmd++) 111 ; 112 for (args = cmd; *args != '\0' && !isspace(*args); args++) 113 ; 114 if (*args != '\0') { 115 while (isspace(*args)) 116 *args++ = '\0'; 117 } 118 119 /* Get a bigger buffer to hold inner message header plus arg string */ 120 if ((ascii = malloc(sizeof(struct ng_mesg) 121 + strlen(args) + 1)) == NULL) { 122 free(buf); 123 return (-1); 124 } 125 memset(ascii, 0, sizeof(*ascii)); 126 127 /* Build inner header (only need cmdstr, arglen, and data fields) */ 128 strncpy(ascii->header.cmdstr, cmd, sizeof(ascii->header.cmdstr) - 1); 129 strcpy(ascii->data, args); 130 ascii->header.arglen = strlen(ascii->data) + 1; 131 free(buf); 132 133 /* Send node a request to convert ASCII to binary */ 134 if (NgSendMsg(cs, path, NGM_GENERIC_COOKIE, NGM_ASCII2BINARY, 135 (u_char *)ascii, sizeof(*ascii) + ascii->header.arglen) < 0) 136 return (-1); 137 138 /* Get reply */ 139 if (NgRecvMsg(cs, reply, sizeof(replybuf), NULL) < 0) 140 return (-1); 141 142 /* Now send binary version */ 143 if (++gMsgId < 0) 144 gMsgId = 1; 145 binary->header.token = gMsgId; 146 if (NgDeliverMsg(cs, 147 path, binary, binary->data, binary->header.arglen) < 0) 148 return (-1); 149 return (binary->header.token); 150 } 151 152 /* 153 * Send a message that is a reply to a previously received message. 154 * Returns -1 and sets errno on error, otherwise returns zero. 155 */ 156 int 157 NgSendReplyMsg(int cs, const char *path, 158 const struct ng_mesg *msg, const void *args, size_t arglen) 159 { 160 struct ng_mesg rep; 161 162 /* Prepare message header */ 163 rep = *msg; 164 rep.header.flags = NGF_RESP; 165 166 /* Deliver message */ 167 return (NgDeliverMsg(cs, path, &rep, args, arglen)); 168 } 169 170 /* 171 * Send a message to a node using control socket node "cs". 172 * Returns -1 if error and sets errno appropriately, otherwise zero. 173 */ 174 static int 175 NgDeliverMsg(int cs, const char *path, 176 const struct ng_mesg *hdr, const void *args, size_t arglen) 177 { 178 u_char sgbuf[NG_PATHSIZ + NGSA_OVERHEAD]; 179 struct sockaddr_ng *const sg = (struct sockaddr_ng *) sgbuf; 180 u_char *buf = NULL; 181 struct ng_mesg *msg; 182 int errnosv = 0; 183 int rtn = 0; 184 185 /* Sanity check */ 186 if (args == NULL) 187 arglen = 0; 188 189 /* Get buffer */ 190 if ((buf = malloc(sizeof(*msg) + arglen)) == NULL) { 191 errnosv = errno; 192 if (_gNgDebugLevel >= 1) 193 NGLOG("malloc"); 194 rtn = -1; 195 goto done; 196 } 197 msg = (struct ng_mesg *) buf; 198 199 /* Finalize message */ 200 *msg = *hdr; 201 msg->header.arglen = arglen; 202 memcpy(msg->data, args, arglen); 203 204 /* Prepare socket address */ 205 sg->sg_family = AF_NETGRAPH; 206 /* XXX handle overflow */ 207 strlcpy(sg->sg_data, path, NG_PATHSIZ); 208 sg->sg_len = strlen(sg->sg_data) + 1 + NGSA_OVERHEAD; 209 210 /* Debugging */ 211 if (_gNgDebugLevel >= 2) { 212 NGLOGX("SENDING %s:", 213 (msg->header.flags & NGF_RESP) ? "RESPONSE" : "MESSAGE"); 214 _NgDebugSockaddr(sg); 215 _NgDebugMsg(msg, sg->sg_data); 216 } 217 218 /* Send it */ 219 if (sendto(cs, msg, sizeof(*msg) + arglen, 220 0, (struct sockaddr *) sg, sg->sg_len) < 0) { 221 errnosv = errno; 222 if (_gNgDebugLevel >= 1) 223 NGLOG("sendto(%s)", sg->sg_data); 224 rtn = -1; 225 goto done; 226 } 227 228 done: 229 /* Done */ 230 free(buf); /* OK if buf is NULL */ 231 errno = errnosv; 232 return (rtn); 233 } 234 235 /* 236 * Receive a control message. 237 * 238 * On error, this returns -1 and sets errno. 239 * Otherwise, it returns the length of the received reply. 240 */ 241 int 242 NgRecvMsg(int cs, struct ng_mesg *rep, size_t replen, char *path) 243 { 244 u_char sgbuf[NG_PATHSIZ + NGSA_OVERHEAD]; 245 struct sockaddr_ng *const sg = (struct sockaddr_ng *) sgbuf; 246 int len, sglen = sizeof(sgbuf); 247 int errnosv; 248 249 /* Read reply */ 250 len = recvfrom(cs, rep, replen, 0, (struct sockaddr *) sg, &sglen); 251 if (len < 0) { 252 errnosv = errno; 253 if (_gNgDebugLevel >= 1) 254 NGLOG("recvfrom"); 255 goto errout; 256 } 257 if (path != NULL) 258 strlcpy(path, sg->sg_data, NG_PATHSIZ); 259 260 /* Debugging */ 261 if (_gNgDebugLevel >= 2) { 262 NGLOGX("RECEIVED %s:", 263 (rep->header.flags & NGF_RESP) ? "RESPONSE" : "MESSAGE"); 264 _NgDebugSockaddr(sg); 265 _NgDebugMsg(rep, sg->sg_data); 266 } 267 268 /* Done */ 269 return (len); 270 271 errout: 272 errno = errnosv; 273 return (-1); 274 } 275 276 /* 277 * Receive a control message and convert the arguments to ASCII 278 */ 279 int 280 NgRecvAsciiMsg(int cs, struct ng_mesg *reply, size_t replen, char *path) 281 { 282 struct ng_mesg *msg, *ascii; 283 int bufSize, errnosv; 284 u_char *buf; 285 286 /* Allocate buffer */ 287 bufSize = 2 * sizeof(*reply) + replen; 288 if ((buf = malloc(bufSize)) == NULL) 289 return (-1); 290 msg = (struct ng_mesg *)buf; 291 ascii = (struct ng_mesg *)msg->data; 292 293 /* Get binary message */ 294 if (NgRecvMsg(cs, msg, bufSize, path) < 0) 295 goto fail; 296 memcpy(reply, msg, sizeof(*msg)); 297 298 /* Ask originating node to convert the arguments to ASCII */ 299 if (NgSendMsg(cs, path, NGM_GENERIC_COOKIE, 300 NGM_BINARY2ASCII, msg, sizeof(*msg) + msg->header.arglen) < 0) 301 goto fail; 302 if (NgRecvMsg(cs, msg, bufSize, NULL) < 0) 303 goto fail; 304 305 /* Copy result to client buffer */ 306 if (sizeof(*ascii) + ascii->header.arglen > replen) { 307 errno = ERANGE; 308 fail: 309 errnosv = errno; 310 free(buf); 311 errno = errnosv; 312 return (-1); 313 } 314 strncpy(reply->data, ascii->data, ascii->header.arglen); 315 316 /* Done */ 317 free(buf); 318 return (0); 319 } 320 321