5 * Copyright (c) 1996-1999 Whistle Communications, Inc.
8 * Subject to the following obligations and disclaimer of warranty, use and
9 * redistribution of this software, in source or object code forms, with or
10 * without modifications are expressly permitted by Whistle Communications;
11 * provided, however, that:
12 * 1. Any and all reproductions of the source or object code must include the
13 * copyright notice above and the following disclaimer of warranties; and
14 * 2. No rights are granted, in any manner or form, to use Whistle
15 * Communications, Inc. trademarks, including the mark "WHISTLE
16 * COMMUNICATIONS" on advertising, endorsements, or otherwise except as
17 * such appears in the above copyright notice or in the software.
19 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
20 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
21 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
22 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
23 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
24 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
25 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
26 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
27 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
28 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
29 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
30 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
31 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
37 * Author: Archie Cobbs <archie@whistle.com>
39 * $FreeBSD: src/lib/libnetgraph/msg.c,v 1.2.2.3 2001/10/29 18:36:30 archie Exp $
40 * $Whistle: msg.c,v 1.9 1999/01/20 00:57:23 archie Exp $
43 #include <sys/types.h>
45 #include <netgraph/ng_message.h>
46 #include <netgraph/ng_socket.h>
51 /* Next message token value */
54 /* For delivering both messages and replies */
55 static int NgDeliverMsg(int cs, const char *path,
56 const struct ng_mesg *hdr, const void *args, size_t arglen);
59 * Send a message to a node using control socket node "cs".
60 * Returns -1 if error and sets errno appropriately.
61 * If successful, returns the message ID (token) used.
64 NgSendMsg(int cs, const char *path,
65 int cookie, int cmd, const void *args, size_t arglen)
69 /* Prepare message header */
70 memset(&msg, 0, sizeof(msg));
71 msg.header.version = NG_VERSION;
72 msg.header.typecookie = cookie;
75 msg.header.token = gMsgId;
76 msg.header.flags = NGF_ORIG;
78 snprintf(msg.header.cmdstr, NG_CMDSTRLEN + 1, "cmd%d", cmd);
81 if (NgDeliverMsg(cs, path, &msg, args, arglen) < 0)
83 return (msg.header.token);
87 * Send a message given in ASCII format. We first ask the node to translate
88 * the command into binary, and then we send the binary.
91 NgSendAsciiMsg(int cs, const char *path, const char *fmt, ...)
93 const int bufSize = 1024;
94 char replybuf[2 * sizeof(struct ng_mesg) + bufSize];
95 struct ng_mesg *const reply = (struct ng_mesg *)replybuf;
96 struct ng_mesg *const binary = (struct ng_mesg *)reply->data;
97 struct ng_mesg *ascii;
98 char *buf, *cmd, *args;
101 /* Parse out command and arguments */
102 va_start(fmtargs, fmt);
103 vasprintf(&buf, fmt, fmtargs);
108 /* Parse out command, arguments */
109 for (cmd = buf; isspace(*cmd); cmd++)
111 for (args = cmd; *args != '\0' && !isspace(*args); args++)
114 while (isspace(*args))
118 /* Get a bigger buffer to hold inner message header plus arg string */
119 if ((ascii = malloc(sizeof(struct ng_mesg)
120 + strlen(args) + 1)) == NULL) {
124 memset(ascii, 0, sizeof(*ascii));
126 /* Build inner header (only need cmdstr, arglen, and data fields) */
127 strncpy(ascii->header.cmdstr, cmd, 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)
138 if (NgRecvMsg(cs, reply, sizeof(replybuf), NULL) < 0)
141 /* Now send binary version */
144 binary->header.token = gMsgId;
146 path, binary, binary->data, binary->header.arglen) < 0)
148 return (binary->header.token);
152 * Send a message that is a reply to a previously received message.
153 * Returns -1 and sets errno on error, otherwise returns zero.
156 NgSendReplyMsg(int cs, const char *path,
157 const struct ng_mesg *msg, const void *args, size_t arglen)
161 /* Prepare message header */
163 rep.header.flags = NGF_RESP;
165 /* Deliver message */
166 return (NgDeliverMsg(cs, path, &rep, args, arglen));
170 * Send a message to a node using control socket node "cs".
171 * Returns -1 if error and sets errno appropriately, otherwise zero.
174 NgDeliverMsg(int cs, const char *path,
175 const struct ng_mesg *hdr, const void *args, size_t arglen)
177 u_char sgbuf[NG_PATHLEN + 3];
178 struct sockaddr_ng *const sg = (struct sockaddr_ng *) sgbuf;
189 if ((buf = malloc(sizeof(*msg) + arglen)) == NULL) {
191 if (_gNgDebugLevel >= 1)
196 msg = (struct ng_mesg *) buf;
198 /* Finalize message */
200 msg->header.arglen = arglen;
201 memcpy(msg->data, args, arglen);
203 /* Prepare socket address */
204 sg->sg_family = AF_NETGRAPH;
205 snprintf(sg->sg_data, NG_PATHLEN + 1, "%s", path);
206 sg->sg_len = strlen(sg->sg_data) + 3;
209 if (_gNgDebugLevel >= 2) {
210 NGLOGX("SENDING %s:",
211 (msg->header.flags & NGF_RESP) ? "RESPONSE" : "MESSAGE");
212 _NgDebugSockaddr(sg);
213 _NgDebugMsg(msg, sg->sg_data);
217 if (sendto(cs, msg, sizeof(*msg) + arglen,
218 0, (struct sockaddr *) sg, sg->sg_len) < 0) {
220 if (_gNgDebugLevel >= 1)
221 NGLOG("sendto(%s)", sg->sg_data);
228 free(buf); /* OK if buf is NULL */
234 * Receive a control message.
236 * On error, this returns -1 and sets errno.
237 * Otherwise, it returns the length of the received reply.
240 NgRecvMsg(int cs, struct ng_mesg *rep, size_t replen, char *path)
242 u_char sgbuf[NG_PATHLEN + sizeof(struct sockaddr_ng)];
243 struct sockaddr_ng *const sg = (struct sockaddr_ng *) sgbuf;
244 int len, sglen = sizeof(sgbuf);
248 len = recvfrom(cs, rep, replen, 0, (struct sockaddr *) sg, &sglen);
251 if (_gNgDebugLevel >= 1)
256 snprintf(path, NG_PATHLEN + 1, "%s", sg->sg_data);
259 if (_gNgDebugLevel >= 2) {
260 NGLOGX("RECEIVED %s:",
261 (rep->header.flags & NGF_RESP) ? "RESPONSE" : "MESSAGE");
262 _NgDebugSockaddr(sg);
263 _NgDebugMsg(rep, sg->sg_data);
275 * Receive a control message and convert the arguments to ASCII
278 NgRecvAsciiMsg(int cs, struct ng_mesg *reply, size_t replen, char *path)
280 struct ng_mesg *msg, *ascii;
281 int bufSize, errnosv;
284 /* Allocate buffer */
285 bufSize = 2 * sizeof(*reply) + replen;
286 if ((buf = malloc(bufSize)) == NULL)
288 msg = (struct ng_mesg *)buf;
289 ascii = (struct ng_mesg *)msg->data;
291 /* Get binary message */
292 if (NgRecvMsg(cs, msg, bufSize, path) < 0)
294 memcpy(reply, msg, sizeof(*msg));
296 /* Ask originating node to convert the arguments to ASCII */
297 if (NgSendMsg(cs, path, NGM_GENERIC_COOKIE,
298 NGM_BINARY2ASCII, msg, sizeof(*msg) + msg->header.arglen) < 0)
300 if (NgRecvMsg(cs, msg, bufSize, NULL) < 0)
303 /* Copy result to client buffer */
304 if (sizeof(*ascii) + ascii->header.arglen > replen) {
312 strncpy(reply->data, ascii->data, ascii->header.arglen);