4 * Copyright (c) 1996-1999 Whistle Communications, Inc.
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.
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
36 * Author: Archie Cobbs <archie@whistle.com>
38 * $Whistle: msg.c,v 1.9 1999/01/20 00:57:23 archie Exp $
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
44 #include <sys/types.h>
45 #include <sys/socket.h>
47 #include <netgraph/ng_message.h>
48 #include <netgraph/ng_socket.h>
53 /* Next message token value */
56 /* For delivering both messages and replies */
57 static int NgDeliverMsg(int cs, const char *path,
58 const struct ng_mesg *hdr, const void *args, size_t arglen);
61 * Send a message to a node using control socket node "cs".
62 * Returns -1 if error and sets errno appropriately.
63 * If successful, returns the message ID (token) used.
66 NgSendMsg(int cs, const char *path,
67 int cookie, int cmd, const void *args, size_t arglen)
71 /* Prepare message header */
72 memset(&msg, 0, sizeof(msg));
73 msg.header.version = NG_VERSION;
74 msg.header.typecookie = cookie;
77 msg.header.token = gMsgId;
78 msg.header.flags = NGF_ORIG;
80 snprintf((char *)msg.header.cmdstr, NG_CMDSTRSIZ, "cmd%d", cmd);
83 if (NgDeliverMsg(cs, path, &msg, args, arglen) < 0)
85 return (msg.header.token);
89 * Send a message given in ASCII format. We first ask the node to translate
90 * the command into binary, and then we send the binary.
93 NgSendAsciiMsg(int cs, const char *path, const char *fmt, ...)
95 struct ng_mesg *reply, *binary, *ascii;
96 char *buf, *cmd, *args;
100 /* Parse out command and arguments */
101 va_start(fmtargs, fmt);
102 vasprintf(&buf, fmt, fmtargs);
107 /* Parse out command, arguments */
108 for (cmd = buf; isspace(*cmd); cmd++)
110 for (args = cmd; *args != '\0' && !isspace(*args); args++)
113 while (isspace(*args))
117 /* Get a bigger buffer to hold inner message header plus arg string */
118 if ((ascii = malloc(sizeof(struct ng_mesg)
119 + strlen(args) + 1)) == NULL) {
123 memset(ascii, 0, sizeof(*ascii));
125 /* Build inner header (only need cmdstr, arglen, and data fields) */
126 strncpy((char *)ascii->header.cmdstr, cmd,
127 sizeof(ascii->header.cmdstr) - 1);
128 strcpy(ascii->data, args);
129 ascii->header.arglen = strlen(ascii->data) + 1;
132 /* Send node a request to convert ASCII to binary */
133 if (NgSendMsg(cs, path, NGM_GENERIC_COOKIE, NGM_ASCII2BINARY,
134 (u_char *)ascii, sizeof(*ascii) + ascii->header.arglen) < 0) {
141 if (NgAllocRecvMsg(cs, &reply, NULL) < 0)
144 /* Now send binary version */
145 binary = (struct ng_mesg *)reply->data;
148 binary->header.token = gMsgId;
149 binary->header.version = NG_VERSION;
151 path, binary, binary->data, binary->header.arglen) < 0) {
155 token = binary->header.token;
161 * Send a message that is a reply to a previously received message.
162 * Returns -1 and sets errno on error, otherwise returns zero.
165 NgSendReplyMsg(int cs, const char *path,
166 const struct ng_mesg *msg, const void *args, size_t arglen)
170 /* Prepare message header */
172 rep.header.flags = NGF_RESP;
174 /* Deliver message */
175 return (NgDeliverMsg(cs, path, &rep, args, arglen));
179 * Send a message to a node using control socket node "cs".
180 * Returns -1 if error and sets errno appropriately, otherwise zero.
183 NgDeliverMsg(int cs, const char *path,
184 const struct ng_mesg *hdr, const void *args, size_t arglen)
186 u_char sgbuf[NG_PATHSIZ + NGSA_OVERHEAD];
187 struct sockaddr_ng *const sg = (struct sockaddr_ng *) sgbuf;
198 if ((buf = malloc(sizeof(*msg) + arglen)) == NULL) {
200 if (_gNgDebugLevel >= 1)
205 msg = (struct ng_mesg *) buf;
207 /* Finalize message */
209 msg->header.arglen = arglen;
210 memcpy(msg->data, args, arglen);
212 /* Prepare socket address */
213 sg->sg_family = AF_NETGRAPH;
214 /* XXX handle overflow */
215 strlcpy(sg->sg_data, path, NG_PATHSIZ);
216 sg->sg_len = strlen(sg->sg_data) + 1 + NGSA_OVERHEAD;
219 if (_gNgDebugLevel >= 2) {
220 NGLOGX("SENDING %s:",
221 (msg->header.flags & NGF_RESP) ? "RESPONSE" : "MESSAGE");
222 _NgDebugSockaddr(sg);
223 _NgDebugMsg(msg, sg->sg_data);
227 if (sendto(cs, msg, sizeof(*msg) + arglen,
228 0, (struct sockaddr *) sg, sg->sg_len) < 0) {
230 if (_gNgDebugLevel >= 1)
231 NGLOG("sendto(%s)", sg->sg_data);
236 /* Wait for reply if there should be one. */
237 if (msg->header.cmd & NGM_HASREPLY && !(msg->header.flags & NGF_RESP)) {
242 rfds.events = POLLIN;
244 n = poll(&rfds, 1, INFTIM);
247 if (_gNgDebugLevel >= 1)
255 free(buf); /* OK if buf is NULL */
261 * Receive a control message.
263 * On error, this returns -1 and sets errno.
264 * Otherwise, it returns the length of the received reply.
267 NgRecvMsg(int cs, struct ng_mesg *rep, size_t replen, char *path)
269 u_char sgbuf[NG_PATHSIZ + NGSA_OVERHEAD];
270 struct sockaddr_ng *const sg = (struct sockaddr_ng *) sgbuf;
271 socklen_t sglen = sizeof(sgbuf);
275 len = recvfrom(cs, rep, replen, 0, (struct sockaddr *) sg, &sglen);
278 if (_gNgDebugLevel >= 1)
283 strlcpy(path, sg->sg_data, NG_PATHSIZ);
286 if (_gNgDebugLevel >= 2) {
287 NGLOGX("RECEIVED %s:",
288 (rep->header.flags & NGF_RESP) ? "RESPONSE" : "MESSAGE");
289 _NgDebugSockaddr(sg);
290 _NgDebugMsg(rep, sg->sg_data);
302 * Identical to NgRecvMsg() except buffer is dynamically allocated.
305 NgAllocRecvMsg(int cs, struct ng_mesg **rep, char *path)
310 optlen = sizeof(len);
311 if (getsockopt(cs, SOL_SOCKET, SO_RCVBUF, &len, &optlen) == -1 ||
312 (*rep = malloc(len)) == NULL)
314 if ((len = NgRecvMsg(cs, *rep, len, path)) < 0)
320 * Receive a control message and convert the arguments to ASCII
323 NgRecvAsciiMsg(int cs, struct ng_mesg *reply, size_t replen, char *path)
325 struct ng_mesg *msg, *ascii;
326 int bufSize, errnosv;
329 /* Allocate buffer */
330 bufSize = 2 * sizeof(*reply) + replen;
331 if ((buf = malloc(bufSize)) == NULL)
333 msg = (struct ng_mesg *)buf;
334 ascii = (struct ng_mesg *)msg->data;
336 /* Get binary message */
337 if (NgRecvMsg(cs, msg, bufSize, path) < 0)
339 memcpy(reply, msg, sizeof(*msg));
341 /* Ask originating node to convert the arguments to ASCII */
342 if (NgSendMsg(cs, path, NGM_GENERIC_COOKIE,
343 NGM_BINARY2ASCII, msg, sizeof(*msg) + msg->header.arglen) < 0)
345 if (NgRecvMsg(cs, msg, bufSize, NULL) < 0)
348 /* Copy result to client buffer */
349 if (sizeof(*ascii) + ascii->header.arglen > replen) {
357 strncpy(reply->data, ascii->data, ascii->header.arglen);
365 * Identical to NgRecvAsciiMsg() except buffer is dynamically allocated.
368 NgAllocRecvAsciiMsg(int cs, struct ng_mesg **reply, char *path)
373 optlen = sizeof(len);
374 if (getsockopt(cs, SOL_SOCKET, SO_RCVBUF, &len, &optlen) == -1 ||
375 (*reply = malloc(len)) == NULL)
377 if ((len = NgRecvAsciiMsg(cs, *reply, len, path)) < 0)