1999-10-21 09:06:11 +00:00
|
|
|
/*
|
|
|
|
* msg.c
|
|
|
|
*
|
|
|
|
* Copyright (c) 1996-1999 Whistle Communications, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Subject to the following obligations and disclaimer of warranty, use and
|
|
|
|
* redistribution of this software, in source or object code forms, with or
|
|
|
|
* without modifications are expressly permitted by Whistle Communications;
|
|
|
|
* provided, however, that:
|
|
|
|
* 1. Any and all reproductions of the source or object code must include the
|
|
|
|
* copyright notice above and the following disclaimer of warranties; and
|
|
|
|
* 2. No rights are granted, in any manner or form, to use Whistle
|
|
|
|
* Communications, Inc. trademarks, including the mark "WHISTLE
|
|
|
|
* COMMUNICATIONS" on advertising, endorsements, or otherwise except as
|
|
|
|
* such appears in the above copyright notice or in the software.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
|
|
|
|
* TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
|
|
|
|
* REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
|
|
|
|
* INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
|
|
|
|
* WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
|
|
|
|
* REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
|
|
|
|
* SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
|
|
|
|
* IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
|
|
|
|
* RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
|
|
|
|
* WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
|
|
|
* PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
|
|
|
|
* OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* Author: Archie Cobbs <archie@whistle.com>
|
|
|
|
*
|
|
|
|
* $Whistle: msg.c,v 1.9 1999/01/20 00:57:23 archie Exp $
|
|
|
|
*/
|
|
|
|
|
2001-09-30 22:03:54 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
1999-10-21 09:06:11 +00:00
|
|
|
#include <sys/types.h>
|
2006-10-17 16:56:29 +00:00
|
|
|
#include <sys/socket.h>
|
1999-10-21 09:06:11 +00:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <netgraph/ng_message.h>
|
|
|
|
#include <netgraph/ng_socket.h>
|
|
|
|
|
|
|
|
#include "netgraph.h"
|
|
|
|
#include "internal.h"
|
|
|
|
|
|
|
|
/* Next message token value */
|
|
|
|
static int gMsgId;
|
|
|
|
|
|
|
|
/* For delivering both messages and replies */
|
|
|
|
static int NgDeliverMsg(int cs, const char *path,
|
|
|
|
const struct ng_mesg *hdr, const void *args, size_t arglen);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Send a message to a node using control socket node "cs".
|
|
|
|
* Returns -1 if error and sets errno appropriately.
|
|
|
|
* If successful, returns the message ID (token) used.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
NgSendMsg(int cs, const char *path,
|
|
|
|
int cookie, int cmd, const void *args, size_t arglen)
|
|
|
|
{
|
|
|
|
struct ng_mesg msg;
|
|
|
|
|
|
|
|
/* Prepare message header */
|
|
|
|
memset(&msg, 0, sizeof(msg));
|
|
|
|
msg.header.version = NG_VERSION;
|
|
|
|
msg.header.typecookie = cookie;
|
2000-06-21 23:01:07 +00:00
|
|
|
if (++gMsgId < 0)
|
|
|
|
gMsgId = 1;
|
|
|
|
msg.header.token = gMsgId;
|
1999-10-21 09:06:11 +00:00
|
|
|
msg.header.flags = NGF_ORIG;
|
|
|
|
msg.header.cmd = cmd;
|
2005-04-26 14:25:11 +00:00
|
|
|
snprintf((char *)msg.header.cmdstr, NG_CMDSTRSIZ, "cmd%d", cmd);
|
1999-10-21 09:06:11 +00:00
|
|
|
|
|
|
|
/* Deliver message */
|
|
|
|
if (NgDeliverMsg(cs, path, &msg, args, arglen) < 0)
|
|
|
|
return (-1);
|
2001-10-25 04:02:25 +00:00
|
|
|
return (msg.header.token);
|
1999-10-21 09:06:11 +00:00
|
|
|
}
|
|
|
|
|
1999-11-30 02:45:32 +00:00
|
|
|
/*
|
|
|
|
* Send a message given in ASCII format. We first ask the node to translate
|
|
|
|
* the command into binary, and then we send the binary.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
NgSendAsciiMsg(int cs, const char *path, const char *fmt, ...)
|
|
|
|
{
|
2004-01-27 20:25:14 +00:00
|
|
|
struct ng_mesg *reply, *binary, *ascii;
|
1999-11-30 02:45:32 +00:00
|
|
|
char *buf, *cmd, *args;
|
|
|
|
va_list fmtargs;
|
2004-01-27 20:25:14 +00:00
|
|
|
int token;
|
1999-11-30 02:45:32 +00:00
|
|
|
|
|
|
|
/* Parse out command and arguments */
|
|
|
|
va_start(fmtargs, fmt);
|
|
|
|
vasprintf(&buf, fmt, fmtargs);
|
|
|
|
va_end(fmtargs);
|
|
|
|
if (buf == NULL)
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
/* Parse out command, arguments */
|
|
|
|
for (cmd = buf; isspace(*cmd); cmd++)
|
|
|
|
;
|
|
|
|
for (args = cmd; *args != '\0' && !isspace(*args); args++)
|
|
|
|
;
|
|
|
|
if (*args != '\0') {
|
|
|
|
while (isspace(*args))
|
|
|
|
*args++ = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get a bigger buffer to hold inner message header plus arg string */
|
|
|
|
if ((ascii = malloc(sizeof(struct ng_mesg)
|
2000-06-02 21:51:36 +00:00
|
|
|
+ strlen(args) + 1)) == NULL) {
|
1999-11-30 02:45:32 +00:00
|
|
|
free(buf);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
memset(ascii, 0, sizeof(*ascii));
|
|
|
|
|
|
|
|
/* Build inner header (only need cmdstr, arglen, and data fields) */
|
2005-04-26 14:25:11 +00:00
|
|
|
strncpy((char *)ascii->header.cmdstr, cmd,
|
|
|
|
sizeof(ascii->header.cmdstr) - 1);
|
1999-11-30 02:45:32 +00:00
|
|
|
strcpy(ascii->data, args);
|
|
|
|
ascii->header.arglen = strlen(ascii->data) + 1;
|
|
|
|
free(buf);
|
|
|
|
|
|
|
|
/* Send node a request to convert ASCII to binary */
|
|
|
|
if (NgSendMsg(cs, path, NGM_GENERIC_COOKIE, NGM_ASCII2BINARY,
|
2004-01-27 18:38:22 +00:00
|
|
|
(u_char *)ascii, sizeof(*ascii) + ascii->header.arglen) < 0) {
|
|
|
|
free(ascii);
|
1999-11-30 02:45:32 +00:00
|
|
|
return (-1);
|
2004-01-27 18:38:22 +00:00
|
|
|
}
|
|
|
|
free(ascii);
|
1999-11-30 02:45:32 +00:00
|
|
|
|
|
|
|
/* Get reply */
|
2004-01-27 20:25:14 +00:00
|
|
|
if (NgAllocRecvMsg(cs, &reply, NULL) < 0)
|
1999-11-30 02:45:32 +00:00
|
|
|
return (-1);
|
|
|
|
|
|
|
|
/* Now send binary version */
|
2004-01-27 20:25:14 +00:00
|
|
|
binary = (struct ng_mesg *)reply->data;
|
2000-06-21 23:01:07 +00:00
|
|
|
if (++gMsgId < 0)
|
|
|
|
gMsgId = 1;
|
|
|
|
binary->header.token = gMsgId;
|
2005-02-04 21:36:10 +00:00
|
|
|
binary->header.version = NG_VERSION;
|
2000-06-21 23:01:07 +00:00
|
|
|
if (NgDeliverMsg(cs,
|
2004-01-27 20:25:14 +00:00
|
|
|
path, binary, binary->data, binary->header.arglen) < 0) {
|
|
|
|
free(reply);
|
2000-06-21 23:01:07 +00:00
|
|
|
return (-1);
|
2004-01-27 20:25:14 +00:00
|
|
|
}
|
|
|
|
token = binary->header.token;
|
|
|
|
free(reply);
|
|
|
|
return (token);
|
1999-11-30 02:45:32 +00:00
|
|
|
}
|
|
|
|
|
1999-10-21 09:06:11 +00:00
|
|
|
/*
|
|
|
|
* Send a message that is a reply to a previously received message.
|
|
|
|
* Returns -1 and sets errno on error, otherwise returns zero.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
NgSendReplyMsg(int cs, const char *path,
|
|
|
|
const struct ng_mesg *msg, const void *args, size_t arglen)
|
|
|
|
{
|
|
|
|
struct ng_mesg rep;
|
|
|
|
|
|
|
|
/* Prepare message header */
|
|
|
|
rep = *msg;
|
|
|
|
rep.header.flags = NGF_RESP;
|
|
|
|
|
|
|
|
/* Deliver message */
|
|
|
|
return (NgDeliverMsg(cs, path, &rep, args, arglen));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Send a message to a node using control socket node "cs".
|
|
|
|
* Returns -1 if error and sets errno appropriately, otherwise zero.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
NgDeliverMsg(int cs, const char *path,
|
|
|
|
const struct ng_mesg *hdr, const void *args, size_t arglen)
|
|
|
|
{
|
2003-11-14 08:09:01 +00:00
|
|
|
u_char sgbuf[NG_PATHSIZ + NGSA_OVERHEAD];
|
1999-10-21 09:06:11 +00:00
|
|
|
struct sockaddr_ng *const sg = (struct sockaddr_ng *) sgbuf;
|
|
|
|
u_char *buf = NULL;
|
|
|
|
struct ng_mesg *msg;
|
|
|
|
int errnosv = 0;
|
|
|
|
int rtn = 0;
|
|
|
|
|
|
|
|
/* Sanity check */
|
|
|
|
if (args == NULL)
|
|
|
|
arglen = 0;
|
|
|
|
|
|
|
|
/* Get buffer */
|
|
|
|
if ((buf = malloc(sizeof(*msg) + arglen)) == NULL) {
|
|
|
|
errnosv = errno;
|
|
|
|
if (_gNgDebugLevel >= 1)
|
|
|
|
NGLOG("malloc");
|
|
|
|
rtn = -1;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
msg = (struct ng_mesg *) buf;
|
|
|
|
|
|
|
|
/* Finalize message */
|
|
|
|
*msg = *hdr;
|
|
|
|
msg->header.arglen = arglen;
|
|
|
|
memcpy(msg->data, args, arglen);
|
|
|
|
|
|
|
|
/* Prepare socket address */
|
|
|
|
sg->sg_family = AF_NETGRAPH;
|
2003-11-14 08:09:01 +00:00
|
|
|
/* XXX handle overflow */
|
|
|
|
strlcpy(sg->sg_data, path, NG_PATHSIZ);
|
|
|
|
sg->sg_len = strlen(sg->sg_data) + 1 + NGSA_OVERHEAD;
|
1999-10-21 09:06:11 +00:00
|
|
|
|
|
|
|
/* Debugging */
|
|
|
|
if (_gNgDebugLevel >= 2) {
|
|
|
|
NGLOGX("SENDING %s:",
|
|
|
|
(msg->header.flags & NGF_RESP) ? "RESPONSE" : "MESSAGE");
|
|
|
|
_NgDebugSockaddr(sg);
|
1999-11-30 02:45:32 +00:00
|
|
|
_NgDebugMsg(msg, sg->sg_data);
|
1999-10-21 09:06:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Send it */
|
|
|
|
if (sendto(cs, msg, sizeof(*msg) + arglen,
|
|
|
|
0, (struct sockaddr *) sg, sg->sg_len) < 0) {
|
|
|
|
errnosv = errno;
|
|
|
|
if (_gNgDebugLevel >= 1)
|
|
|
|
NGLOG("sendto(%s)", sg->sg_data);
|
|
|
|
rtn = -1;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2006-01-12 19:14:40 +00:00
|
|
|
/* Wait for reply if there should be one. */
|
2013-03-11 13:05:11 +00:00
|
|
|
if (msg->header.cmd & NGM_HASREPLY && !(msg->header.flags & NGF_RESP)) {
|
2007-05-14 14:18:41 +00:00
|
|
|
struct pollfd rfds;
|
2006-01-12 19:14:40 +00:00
|
|
|
int n;
|
|
|
|
|
2007-05-14 14:18:41 +00:00
|
|
|
rfds.fd = cs;
|
|
|
|
rfds.events = POLLIN;
|
|
|
|
rfds.revents = 0;
|
|
|
|
n = poll(&rfds, 1, INFTIM);
|
2006-01-12 19:14:40 +00:00
|
|
|
if (n == -1) {
|
|
|
|
errnosv = errno;
|
|
|
|
if (_gNgDebugLevel >= 1)
|
2007-05-14 14:18:41 +00:00
|
|
|
NGLOG("poll");
|
2006-01-12 19:14:40 +00:00
|
|
|
rtn = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-10-21 09:06:11 +00:00
|
|
|
done:
|
|
|
|
/* Done */
|
|
|
|
free(buf); /* OK if buf is NULL */
|
|
|
|
errno = errnosv;
|
|
|
|
return (rtn);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Receive a control message.
|
|
|
|
*
|
|
|
|
* On error, this returns -1 and sets errno.
|
|
|
|
* Otherwise, it returns the length of the received reply.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
NgRecvMsg(int cs, struct ng_mesg *rep, size_t replen, char *path)
|
|
|
|
{
|
2003-11-14 08:09:01 +00:00
|
|
|
u_char sgbuf[NG_PATHSIZ + NGSA_OVERHEAD];
|
1999-10-21 09:06:11 +00:00
|
|
|
struct sockaddr_ng *const sg = (struct sockaddr_ng *) sgbuf;
|
2005-04-26 14:25:11 +00:00
|
|
|
socklen_t sglen = sizeof(sgbuf);
|
|
|
|
int len, errnosv;
|
1999-10-21 09:06:11 +00:00
|
|
|
|
|
|
|
/* Read reply */
|
|
|
|
len = recvfrom(cs, rep, replen, 0, (struct sockaddr *) sg, &sglen);
|
|
|
|
if (len < 0) {
|
|
|
|
errnosv = errno;
|
|
|
|
if (_gNgDebugLevel >= 1)
|
|
|
|
NGLOG("recvfrom");
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
if (path != NULL)
|
2003-11-14 08:09:01 +00:00
|
|
|
strlcpy(path, sg->sg_data, NG_PATHSIZ);
|
1999-10-21 09:06:11 +00:00
|
|
|
|
|
|
|
/* Debugging */
|
|
|
|
if (_gNgDebugLevel >= 2) {
|
|
|
|
NGLOGX("RECEIVED %s:",
|
|
|
|
(rep->header.flags & NGF_RESP) ? "RESPONSE" : "MESSAGE");
|
|
|
|
_NgDebugSockaddr(sg);
|
1999-11-30 02:45:32 +00:00
|
|
|
_NgDebugMsg(rep, sg->sg_data);
|
1999-10-21 09:06:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Done */
|
|
|
|
return (len);
|
|
|
|
|
|
|
|
errout:
|
|
|
|
errno = errnosv;
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
2004-01-27 20:25:14 +00:00
|
|
|
/*
|
|
|
|
* Identical to NgRecvMsg() except buffer is dynamically allocated.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
NgAllocRecvMsg(int cs, struct ng_mesg **rep, char *path)
|
|
|
|
{
|
|
|
|
int len;
|
|
|
|
socklen_t optlen;
|
|
|
|
|
|
|
|
optlen = sizeof(len);
|
|
|
|
if (getsockopt(cs, SOL_SOCKET, SO_RCVBUF, &len, &optlen) == -1 ||
|
|
|
|
(*rep = malloc(len)) == NULL)
|
|
|
|
return (-1);
|
|
|
|
if ((len = NgRecvMsg(cs, *rep, len, path)) < 0)
|
|
|
|
free(*rep);
|
|
|
|
return (len);
|
|
|
|
}
|
|
|
|
|
1999-11-30 02:45:32 +00:00
|
|
|
/*
|
|
|
|
* Receive a control message and convert the arguments to ASCII
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
NgRecvAsciiMsg(int cs, struct ng_mesg *reply, size_t replen, char *path)
|
|
|
|
{
|
|
|
|
struct ng_mesg *msg, *ascii;
|
|
|
|
int bufSize, errnosv;
|
|
|
|
u_char *buf;
|
|
|
|
|
|
|
|
/* Allocate buffer */
|
|
|
|
bufSize = 2 * sizeof(*reply) + replen;
|
|
|
|
if ((buf = malloc(bufSize)) == NULL)
|
|
|
|
return (-1);
|
|
|
|
msg = (struct ng_mesg *)buf;
|
|
|
|
ascii = (struct ng_mesg *)msg->data;
|
|
|
|
|
|
|
|
/* Get binary message */
|
|
|
|
if (NgRecvMsg(cs, msg, bufSize, path) < 0)
|
|
|
|
goto fail;
|
|
|
|
memcpy(reply, msg, sizeof(*msg));
|
|
|
|
|
|
|
|
/* Ask originating node to convert the arguments to ASCII */
|
|
|
|
if (NgSendMsg(cs, path, NGM_GENERIC_COOKIE,
|
|
|
|
NGM_BINARY2ASCII, msg, sizeof(*msg) + msg->header.arglen) < 0)
|
|
|
|
goto fail;
|
|
|
|
if (NgRecvMsg(cs, msg, bufSize, NULL) < 0)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
/* Copy result to client buffer */
|
|
|
|
if (sizeof(*ascii) + ascii->header.arglen > replen) {
|
|
|
|
errno = ERANGE;
|
|
|
|
fail:
|
|
|
|
errnosv = errno;
|
|
|
|
free(buf);
|
|
|
|
errno = errnosv;
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
strncpy(reply->data, ascii->data, ascii->header.arglen);
|
|
|
|
|
|
|
|
/* Done */
|
|
|
|
free(buf);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2004-01-27 20:25:14 +00:00
|
|
|
/*
|
|
|
|
* Identical to NgRecvAsciiMsg() except buffer is dynamically allocated.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
NgAllocRecvAsciiMsg(int cs, struct ng_mesg **reply, char *path)
|
|
|
|
{
|
|
|
|
int len;
|
|
|
|
socklen_t optlen;
|
|
|
|
|
|
|
|
optlen = sizeof(len);
|
|
|
|
if (getsockopt(cs, SOL_SOCKET, SO_RCVBUF, &len, &optlen) == -1 ||
|
|
|
|
(*reply = malloc(len)) == NULL)
|
|
|
|
return (-1);
|
|
|
|
if ((len = NgRecvAsciiMsg(cs, *reply, len, path)) < 0)
|
|
|
|
free(*reply);
|
|
|
|
return (len);
|
|
|
|
}
|