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 * $FreeBSD: src/lib/libnetgraph/msg.c,v 1.14 2007/05/14 14:18:41 mav Exp $
39 * $DragonFly: src/lib/libnetgraph/msg.c,v 1.4 2007/06/03 23:41:25 swildner Exp $
40 * $Whistle: msg.c,v 1.9 1999/01/20 00:57:23 archie Exp $
43 #include <sys/types.h>
44 #include <sys/socket.h>
46 #include <netgraph7/ng_message.h>
47 #include <netgraph7/socket/ng_socket.h>
52 /* Next message token value */
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);
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.
65 NgSendMsg(int cs, const char *path,
66 int cookie, int cmd, const void *args, size_t arglen)
70 /* Prepare message header */
71 memset(&msg, 0, sizeof(msg));
72 msg.header.version = NG_VERSION;
73 msg.header.typecookie = cookie;
76 msg.header.token = gMsgId;
77 msg.header.flags = NGF_ORIG;
79 snprintf((char *)msg.header.cmdstr, NG_CMDSTRSIZ, "cmd%d", cmd);
82 if (NgDeliverMsg(cs, path, &msg, args, arglen) < 0)
84 return (msg.header.token);
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.
92 NgSendAsciiMsg(int cs, const char *path, const char *fmt, ...)
94 struct ng_mesg *reply, *binary, *ascii;
95 char *buf, *cmd, *args;
99 /* Parse out command and arguments */
100 va_start(fmtargs, fmt);
101 vasprintf(&buf, fmt, fmtargs);
106 /* Parse out command, arguments */
107 for (cmd = buf; isspace(*cmd); cmd++)
109 for (args = cmd; *args != '\0' && !isspace(*args); args++)
112 while (isspace(*args))
116 /* Get a bigger buffer to hold inner message header plus arg string */
117 if ((ascii = malloc(sizeof(struct ng_mesg)
118 + strlen(args) + 1)) == NULL) {
122 memset(ascii, 0, sizeof(*ascii));
124 /* Build inner header (only need cmdstr, arglen, and data fields) */
125 strncpy((char *)ascii->header.cmdstr, cmd,
126 sizeof(ascii->header.cmdstr) - 1);
127 strcpy(ascii->data, args);
128 ascii->header.arglen = strlen(ascii->data) + 1;
131 /* Send node a request to convert ASCII to binary */
132 if (NgSendMsg(cs, path, NGM_GENERIC_COOKIE, NGM_ASCII2BINARY,
133 (u_char *)ascii, sizeof(*ascii) + ascii->header.arglen) < 0) {
140 if (NgAllocRecvMsg(cs, &reply, NULL) < 0)
143 /* Now send binary version */
144 binary = (struct ng_mesg *)reply->data;
147 binary->header.token = gMsgId;
148 binary->header.version = NG_VERSION;
150 path, binary, binary->data, binary->header.arglen) < 0) {
154 token = binary->header.token;
160 * Send a message that is a reply to a previously received message.
161 * Returns -1 and sets errno on error, otherwise returns zero.
164 NgSendReplyMsg(int cs, const char *path,
165 const struct ng_mesg *msg, const void *args, size_t arglen)
169 /* Prepare message header */
171 rep.header.flags = NGF_RESP;
173 /* Deliver message */
174 return (NgDeliverMsg(cs, path, &rep, args, arglen));
178 * Send a message to a node using control socket node "cs".
179 * Returns -1 if error and sets errno appropriately, otherwise zero.
182 NgDeliverMsg(int cs, const char *path,
183 const struct ng_mesg *hdr, const void *args, size_t arglen)
185 u_char sgbuf[NG_PATHSIZ + NGSA_OVERHEAD];
186 struct sockaddr_ng *const sg = (struct sockaddr_ng *) sgbuf;
197 if ((buf = malloc(sizeof(*msg) + arglen)) == NULL) {
199 if (_gNgDebugLevel >= 1)
204 msg = (struct ng_mesg *) buf;
206 /* Finalize message */
208 msg->header.arglen = arglen;
209 memcpy(msg->data, args, arglen);
211 /* Prepare socket address */
212 sg->sg_family = AF_NETGRAPH;
213 /* XXX handle overflow */
214 strlcpy(sg->sg_data, path, NG_PATHSIZ);
215 sg->sg_len = strlen(sg->sg_data) + 1 + NGSA_OVERHEAD;
218 if (_gNgDebugLevel >= 2) {
219 NGLOGX("SENDING %s:",
220 (msg->header.flags & NGF_RESP) ? "RESPONSE" : "MESSAGE");
221 _NgDebugSockaddr(sg);
222 _NgDebugMsg(msg, sg->sg_data);
226 if (sendto(cs, msg, sizeof(*msg) + arglen,
227 0, (struct sockaddr *) sg, sg->sg_len) < 0) {
229 if (_gNgDebugLevel >= 1)
230 NGLOG("sendto(%s)", sg->sg_data);
235 /* Wait for reply if there should be one. */
236 if (msg->header.cmd & NGM_HASREPLY) {
241 rfds.events = POLLIN;
243 n = poll(&rfds, 1, INFTIM);
246 if (_gNgDebugLevel >= 1)
254 free(buf); /* OK if buf is NULL */
260 * Receive a control message.
262 * On error, this returns -1 and sets errno.
263 * Otherwise, it returns the length of the received reply.
266 NgRecvMsg(int cs, struct ng_mesg *rep, size_t replen, char *path)
268 u_char sgbuf[NG_PATHSIZ + NGSA_OVERHEAD];
269 struct sockaddr_ng *const sg = (struct sockaddr_ng *) sgbuf;
270 socklen_t sglen = sizeof(sgbuf);
274 len = recvfrom(cs, rep, replen, 0, (struct sockaddr *) sg, &sglen);
277 if (_gNgDebugLevel >= 1)
282 strlcpy(path, sg->sg_data, NG_PATHSIZ);
285 if (_gNgDebugLevel >= 2) {
286 NGLOGX("RECEIVED %s:",
287 (rep->header.flags & NGF_RESP) ? "RESPONSE" : "MESSAGE");
288 _NgDebugSockaddr(sg);
289 _NgDebugMsg(rep, sg->sg_data);
301 * Identical to NgRecvMsg() except buffer is dynamically allocated.
304 NgAllocRecvMsg(int cs, struct ng_mesg **rep, char *path)
309 optlen = sizeof(len);
310 if (getsockopt(cs, SOL_SOCKET, SO_RCVBUF, &len, &optlen) == -1 ||
311 (*rep = malloc(len)) == NULL)
313 if ((len = NgRecvMsg(cs, *rep, len, path)) < 0)
319 * Receive a control message and convert the arguments to ASCII
322 NgRecvAsciiMsg(int cs, struct ng_mesg *reply, size_t replen, char *path)
324 struct ng_mesg *msg, *ascii;
325 int bufSize, errnosv;
328 /* Allocate buffer */
329 bufSize = 2 * sizeof(*reply) + replen;
330 if ((buf = malloc(bufSize)) == NULL)
332 msg = (struct ng_mesg *)buf;
333 ascii = (struct ng_mesg *)msg->data;
335 /* Get binary message */
336 if (NgRecvMsg(cs, msg, bufSize, path) < 0)
338 memcpy(reply, msg, sizeof(*msg));
340 /* Ask originating node to convert the arguments to ASCII */
341 if (NgSendMsg(cs, path, NGM_GENERIC_COOKIE,
342 NGM_BINARY2ASCII, msg, sizeof(*msg) + msg->header.arglen) < 0)
344 if (NgRecvMsg(cs, msg, bufSize, NULL) < 0)
347 /* Copy result to client buffer */
348 if (sizeof(*ascii) + ascii->header.arglen > replen) {
356 strncpy(reply->data, ascii->data, ascii->header.arglen);
364 * Identical to NgRecvAsciiMsg() except buffer is dynamically allocated.
367 NgAllocRecvAsciiMsg(int cs, struct ng_mesg **reply, char *path)
372 optlen = sizeof(len);
373 if (getsockopt(cs, SOL_SOCKET, SO_RCVBUF, &len, &optlen) == -1 ||
374 (*reply = malloc(len)) == NULL)
376 if ((len = NgRecvAsciiMsg(cs, *reply, len, path)) < 0)