xref: /freebsd/lib/libnetgraph/msg.c (revision 6af83ee0d2941d18880b6aaa2b4facd1d30c6106)
1 /*
2  * msg.c
3  *
4  * Copyright (c) 1996-1999 Whistle Communications, Inc.
5  * All rights reserved.
6  *
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.
17  *
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
34  * OF SUCH DAMAGE.
35  *
36  * Author: Archie Cobbs <archie@whistle.com>
37  *
38  * $Whistle: msg.c,v 1.9 1999/01/20 00:57:23 archie Exp $
39  */
40 
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
43 
44 #include <sys/types.h>
45 #include <stdarg.h>
46 #include <netgraph/ng_message.h>
47 #include <netgraph/ng_socket.h>
48 
49 #include "netgraph.h"
50 #include "internal.h"
51 
52 /* Next message token value */
53 static int	gMsgId;
54 
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);
58 
59 /*
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.
63  */
64 int
65 NgSendMsg(int cs, const char *path,
66 	  int cookie, int cmd, const void *args, size_t arglen)
67 {
68 	struct ng_mesg msg;
69 
70 	/* Prepare message header */
71 	memset(&msg, 0, sizeof(msg));
72 	msg.header.version = NG_VERSION;
73 	msg.header.typecookie = cookie;
74 	if (++gMsgId < 0)
75 		gMsgId = 1;
76 	msg.header.token = gMsgId;
77 	msg.header.flags = NGF_ORIG;
78 	msg.header.cmd = cmd;
79 	snprintf(msg.header.cmdstr, NG_CMDSTRSIZ, "cmd%d", cmd);
80 
81 	/* Deliver message */
82 	if (NgDeliverMsg(cs, path, &msg, args, arglen) < 0)
83 		return (-1);
84 	return (msg.header.token);
85 }
86 
87 /*
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.
90  */
91 int
92 NgSendAsciiMsg(int cs, const char *path, const char *fmt, ...)
93 {
94 	struct ng_mesg *reply, *binary, *ascii;
95 	char *buf, *cmd, *args;
96 	va_list fmtargs;
97 	int token;
98 
99 	/* Parse out command and arguments */
100 	va_start(fmtargs, fmt);
101 	vasprintf(&buf, fmt, fmtargs);
102 	va_end(fmtargs);
103 	if (buf == NULL)
104 		return (-1);
105 
106 	/* Parse out command, arguments */
107 	for (cmd = buf; isspace(*cmd); cmd++)
108 		;
109 	for (args = cmd; *args != '\0' && !isspace(*args); args++)
110 		;
111 	if (*args != '\0') {
112 		while (isspace(*args))
113 			*args++ = '\0';
114 	}
115 
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) {
119 		free(buf);
120 		return (-1);
121 	}
122 	memset(ascii, 0, sizeof(*ascii));
123 
124 	/* Build inner header (only need cmdstr, arglen, and data fields) */
125 	strncpy(ascii->header.cmdstr, cmd, sizeof(ascii->header.cmdstr) - 1);
126 	strcpy(ascii->data, args);
127 	ascii->header.arglen = strlen(ascii->data) + 1;
128 	free(buf);
129 
130 	/* Send node a request to convert ASCII to binary */
131 	if (NgSendMsg(cs, path, NGM_GENERIC_COOKIE, NGM_ASCII2BINARY,
132 	    (u_char *)ascii, sizeof(*ascii) + ascii->header.arglen) < 0) {
133 		free(ascii);
134 		return (-1);
135 	}
136 	free(ascii);
137 
138 	/* Get reply */
139 	if (NgAllocRecvMsg(cs, &reply, NULL) < 0)
140 		return (-1);
141 
142 	/* Now send binary version */
143 	binary = (struct ng_mesg *)reply->data;
144 	if (++gMsgId < 0)
145 		gMsgId = 1;
146 	binary->header.token = gMsgId;
147 	binary->header.version = NG_VERSION;
148 	if (NgDeliverMsg(cs,
149 	    path, binary, binary->data, binary->header.arglen) < 0) {
150 		free(reply);
151 		return (-1);
152 	}
153 	token = binary->header.token;
154 	free(reply);
155 	return (token);
156 }
157 
158 /*
159  * Send a message that is a reply to a previously received message.
160  * Returns -1 and sets errno on error, otherwise returns zero.
161  */
162 int
163 NgSendReplyMsg(int cs, const char *path,
164 	const struct ng_mesg *msg, const void *args, size_t arglen)
165 {
166 	struct ng_mesg rep;
167 
168 	/* Prepare message header */
169 	rep = *msg;
170 	rep.header.flags = NGF_RESP;
171 
172 	/* Deliver message */
173 	return (NgDeliverMsg(cs, path, &rep, args, arglen));
174 }
175 
176 /*
177  * Send a message to a node using control socket node "cs".
178  * Returns -1 if error and sets errno appropriately, otherwise zero.
179  */
180 static int
181 NgDeliverMsg(int cs, const char *path,
182 	const struct ng_mesg *hdr, const void *args, size_t arglen)
183 {
184 	u_char sgbuf[NG_PATHSIZ + NGSA_OVERHEAD];
185 	struct sockaddr_ng *const sg = (struct sockaddr_ng *) sgbuf;
186 	u_char *buf = NULL;
187 	struct ng_mesg *msg;
188 	int errnosv = 0;
189 	int rtn = 0;
190 
191 	/* Sanity check */
192 	if (args == NULL)
193 		arglen = 0;
194 
195 	/* Get buffer */
196 	if ((buf = malloc(sizeof(*msg) + arglen)) == NULL) {
197 		errnosv = errno;
198 		if (_gNgDebugLevel >= 1)
199 			NGLOG("malloc");
200 		rtn = -1;
201 		goto done;
202 	}
203 	msg = (struct ng_mesg *) buf;
204 
205 	/* Finalize message */
206 	*msg = *hdr;
207 	msg->header.arglen = arglen;
208 	memcpy(msg->data, args, arglen);
209 
210 	/* Prepare socket address */
211 	sg->sg_family = AF_NETGRAPH;
212 	/* XXX handle overflow */
213 	strlcpy(sg->sg_data, path, NG_PATHSIZ);
214 	sg->sg_len = strlen(sg->sg_data) + 1 + NGSA_OVERHEAD;
215 
216 	/* Debugging */
217 	if (_gNgDebugLevel >= 2) {
218 		NGLOGX("SENDING %s:",
219 		    (msg->header.flags & NGF_RESP) ? "RESPONSE" : "MESSAGE");
220 		_NgDebugSockaddr(sg);
221 		_NgDebugMsg(msg, sg->sg_data);
222 	}
223 
224 	/* Send it */
225 	if (sendto(cs, msg, sizeof(*msg) + arglen,
226 		   0, (struct sockaddr *) sg, sg->sg_len) < 0) {
227 		errnosv = errno;
228 		if (_gNgDebugLevel >= 1)
229 			NGLOG("sendto(%s)", sg->sg_data);
230 		rtn = -1;
231 		goto done;
232 	}
233 
234 done:
235 	/* Done */
236 	free(buf);		/* OK if buf is NULL */
237 	errno = errnosv;
238 	return (rtn);
239 }
240 
241 /*
242  * Receive a control message.
243  *
244  * On error, this returns -1 and sets errno.
245  * Otherwise, it returns the length of the received reply.
246  */
247 int
248 NgRecvMsg(int cs, struct ng_mesg *rep, size_t replen, char *path)
249 {
250 	u_char sgbuf[NG_PATHSIZ + NGSA_OVERHEAD];
251 	struct sockaddr_ng *const sg = (struct sockaddr_ng *) sgbuf;
252 	int len, sglen = sizeof(sgbuf);
253 	int errnosv;
254 
255 	/* Read reply */
256 	len = recvfrom(cs, rep, replen, 0, (struct sockaddr *) sg, &sglen);
257 	if (len < 0) {
258 		errnosv = errno;
259 		if (_gNgDebugLevel >= 1)
260 			NGLOG("recvfrom");
261 		goto errout;
262 	}
263 	if (path != NULL)
264 		strlcpy(path, sg->sg_data, NG_PATHSIZ);
265 
266 	/* Debugging */
267 	if (_gNgDebugLevel >= 2) {
268 		NGLOGX("RECEIVED %s:",
269 		    (rep->header.flags & NGF_RESP) ? "RESPONSE" : "MESSAGE");
270 		_NgDebugSockaddr(sg);
271 		_NgDebugMsg(rep, sg->sg_data);
272 	}
273 
274 	/* Done */
275 	return (len);
276 
277 errout:
278 	errno = errnosv;
279 	return (-1);
280 }
281 
282 /*
283  * Identical to NgRecvMsg() except buffer is dynamically allocated.
284  */
285 int
286 NgAllocRecvMsg(int cs, struct ng_mesg **rep, char *path)
287 {
288 	int len;
289 	socklen_t optlen;
290 
291 	optlen = sizeof(len);
292 	if (getsockopt(cs, SOL_SOCKET, SO_RCVBUF, &len, &optlen) == -1 ||
293 	    (*rep = malloc(len)) == NULL)
294 		return (-1);
295 	if ((len = NgRecvMsg(cs, *rep, len, path)) < 0)
296 		free(*rep);
297 	return (len);
298 }
299 
300 /*
301  * Receive a control message and convert the arguments to ASCII
302  */
303 int
304 NgRecvAsciiMsg(int cs, struct ng_mesg *reply, size_t replen, char *path)
305 {
306 	struct ng_mesg *msg, *ascii;
307 	int bufSize, errnosv;
308 	u_char *buf;
309 
310 	/* Allocate buffer */
311 	bufSize = 2 * sizeof(*reply) + replen;
312 	if ((buf = malloc(bufSize)) == NULL)
313 		return (-1);
314 	msg = (struct ng_mesg *)buf;
315 	ascii = (struct ng_mesg *)msg->data;
316 
317 	/* Get binary message */
318 	if (NgRecvMsg(cs, msg, bufSize, path) < 0)
319 		goto fail;
320 	memcpy(reply, msg, sizeof(*msg));
321 
322 	/* Ask originating node to convert the arguments to ASCII */
323 	if (NgSendMsg(cs, path, NGM_GENERIC_COOKIE,
324 	    NGM_BINARY2ASCII, msg, sizeof(*msg) + msg->header.arglen) < 0)
325 		goto fail;
326 	if (NgRecvMsg(cs, msg, bufSize, NULL) < 0)
327 		goto fail;
328 
329 	/* Copy result to client buffer */
330 	if (sizeof(*ascii) + ascii->header.arglen > replen) {
331 		errno = ERANGE;
332 fail:
333 		errnosv = errno;
334 		free(buf);
335 		errno = errnosv;
336 		return (-1);
337 	}
338 	strncpy(reply->data, ascii->data, ascii->header.arglen);
339 
340 	/* Done */
341 	free(buf);
342 	return (0);
343 }
344 
345 /*
346  * Identical to NgRecvAsciiMsg() except buffer is dynamically allocated.
347  */
348 int
349 NgAllocRecvAsciiMsg(int cs, struct ng_mesg **reply, char *path)
350 {
351 	int len;
352 	socklen_t optlen;
353 
354 	optlen = sizeof(len);
355 	if (getsockopt(cs, SOL_SOCKET, SO_RCVBUF, &len, &optlen) == -1 ||
356 	    (*reply = malloc(len)) == NULL)
357 		return (-1);
358 	if ((len = NgRecvAsciiMsg(cs, *reply, len, path)) < 0)
359 		free(*reply);
360 	return (len);
361 }
362