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