#include <sys/types.h>
#include <sys/socket.h>
#include <stdarg.h>
#include <stdatomic.h>
#include <netgraph/ng_message.h>
#include <netgraph/ng_socket.h>
#include "netgraph.h"
#include "internal.h"
static _Atomic(unsigned int) gMsgId;
static int NgDeliverMsg(int cs, const char *path,
const struct ng_mesg *hdr, const void *args, size_t arglen);
int
NgSendMsg(int cs, const char *path,
int cookie, int cmd, const void *args, size_t arglen)
{
struct ng_mesg msg;
memset(&msg, 0, sizeof(msg));
msg.header.version = NG_VERSION;
msg.header.typecookie = cookie;
msg.header.token = atomic_fetch_add(&gMsgId, 1) & INT_MAX;
msg.header.flags = NGF_ORIG;
msg.header.cmd = cmd;
snprintf((char *)msg.header.cmdstr, NG_CMDSTRSIZ, "cmd%d", cmd);
if (NgDeliverMsg(cs, path, &msg, args, arglen) < 0)
return (-1);
return (msg.header.token);
}
int
NgSendAsciiMsg(int cs, const char *path, const char *fmt, ...)
{
struct ng_mesg *reply, *binary, *ascii;
char *buf, *cmd, *args;
va_list fmtargs;
int token;
va_start(fmtargs, fmt);
vasprintf(&buf, fmt, fmtargs);
va_end(fmtargs);
if (buf == NULL)
return (-1);
for (cmd = buf; isspace(*cmd); cmd++)
;
for (args = cmd; *args != '\0' && !isspace(*args); args++)
;
if (*args != '\0') {
while (isspace(*args))
*args++ = '\0';
}
if ((ascii = malloc(sizeof(struct ng_mesg)
+ strlen(args) + 1)) == NULL) {
free(buf);
return (-1);
}
memset(ascii, 0, sizeof(*ascii));
strncpy((char *)ascii->header.cmdstr, cmd,
sizeof(ascii->header.cmdstr) - 1);
strcpy(ascii->data, args);
ascii->header.arglen = strlen(ascii->data) + 1;
free(buf);
if (NgSendMsg(cs, path, NGM_GENERIC_COOKIE, NGM_ASCII2BINARY,
(u_char *)ascii, sizeof(*ascii) + ascii->header.arglen) < 0) {
free(ascii);
return (-1);
}
free(ascii);
if (NgAllocRecvMsg(cs, &reply, NULL) < 0)
return (-1);
binary = (struct ng_mesg *)reply->data;
binary->header.token = atomic_fetch_add(&gMsgId, 1) & INT_MAX;
binary->header.version = NG_VERSION;
if (NgDeliverMsg(cs,
path, binary, binary->data, binary->header.arglen) < 0) {
free(reply);
return (-1);
}
token = binary->header.token;
free(reply);
return (token);
}
int
NgSendReplyMsg(int cs, const char *path,
const struct ng_mesg *msg, const void *args, size_t arglen)
{
struct ng_mesg rep;
rep = *msg;
rep.header.flags = NGF_RESP;
return (NgDeliverMsg(cs, path, &rep, args, arglen));
}
static int
NgDeliverMsg(int cs, const char *path,
const struct ng_mesg *hdr, const void *args, size_t arglen)
{
u_char sgbuf[NG_PATHSIZ + NGSA_OVERHEAD];
struct sockaddr_ng *const sg = (struct sockaddr_ng *) sgbuf;
u_char *buf = NULL;
struct ng_mesg *msg;
int errnosv = 0;
int rtn = 0;
if (args == NULL)
arglen = 0;
if ((buf = malloc(sizeof(*msg) + arglen)) == NULL) {
errnosv = errno;
if (_gNgDebugLevel >= 1)
NGLOG("malloc");
rtn = -1;
goto done;
}
msg = (struct ng_mesg *) buf;
*msg = *hdr;
msg->header.arglen = arglen;
memcpy(msg->data, args, arglen);
sg->sg_family = AF_NETGRAPH;
strlcpy(sg->sg_data, path, NG_PATHSIZ);
sg->sg_len = strlen(sg->sg_data) + 1 + NGSA_OVERHEAD;
if (_gNgDebugLevel >= 2) {
NGLOGX("SENDING %s:",
(msg->header.flags & NGF_RESP) ? "RESPONSE" : "MESSAGE");
_NgDebugSockaddr(sg);
_NgDebugMsg(msg, sg->sg_data);
}
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;
}
if (msg->header.cmd & NGM_HASREPLY && !(msg->header.flags & NGF_RESP)) {
struct pollfd rfds;
int n;
rfds.fd = cs;
rfds.events = POLLIN;
rfds.revents = 0;
n = poll(&rfds, 1, INFTIM);
if (n == -1) {
errnosv = errno;
if (_gNgDebugLevel >= 1)
NGLOG("poll");
rtn = -1;
}
}
done:
free(buf);
errno = errnosv;
return (rtn);
}
int
NgRecvMsg(int cs, struct ng_mesg *rep, size_t replen, char *path)
{
u_char sgbuf[NG_PATHSIZ + NGSA_OVERHEAD];
struct sockaddr_ng *const sg = (struct sockaddr_ng *) sgbuf;
socklen_t sglen = sizeof(sgbuf);
int len, errnosv;
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)
strlcpy(path, sg->sg_data, NG_PATHSIZ);
if (_gNgDebugLevel >= 2) {
NGLOGX("RECEIVED %s:",
(rep->header.flags & NGF_RESP) ? "RESPONSE" : "MESSAGE");
_NgDebugSockaddr(sg);
_NgDebugMsg(rep, sg->sg_data);
}
return (len);
errout:
errno = errnosv;
return (-1);
}
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);
}
int
NgRecvAsciiMsg(int cs, struct ng_mesg *reply, size_t replen, char *path)
{
struct ng_mesg *msg, *ascii;
int bufSize, errnosv;
u_char *buf;
bufSize = 2 * sizeof(*reply) + replen;
if ((buf = malloc(bufSize)) == NULL)
return (-1);
msg = (struct ng_mesg *)buf;
ascii = (struct ng_mesg *)msg->data;
if (NgRecvMsg(cs, msg, bufSize, path) < 0)
goto fail;
memcpy(reply, msg, sizeof(*msg));
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;
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);
free(buf);
return (0);
}
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);
}