Initial import from FreeBSD RELENG_4:
[games.git] / lib / libnetgraph / msg.c
1
2 /*
3  * msg.c
4  *
5  * Copyright (c) 1996-1999 Whistle Communications, Inc.
6  * All rights reserved.
7  * 
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.
18  * 
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
35  * OF SUCH DAMAGE.
36  *
37  * Author: Archie Cobbs <archie@whistle.com>
38  *
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 $
41  */
42
43 #include <sys/types.h>
44 #include <stdarg.h>
45 #include <netgraph/ng_message.h>
46 #include <netgraph/ng_socket.h>
47
48 #include "netgraph.h"
49 #include "internal.h"
50
51 /* Next message token value */
52 static int      gMsgId;
53
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);
57
58 /*
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.
62  */
63 int
64 NgSendMsg(int cs, const char *path,
65           int cookie, int cmd, const void *args, size_t arglen)
66 {
67         struct ng_mesg msg;
68
69         /* Prepare message header */
70         memset(&msg, 0, sizeof(msg));
71         msg.header.version = NG_VERSION;
72         msg.header.typecookie = cookie;
73         if (++gMsgId < 0)
74                 gMsgId = 1;
75         msg.header.token = gMsgId;
76         msg.header.flags = NGF_ORIG;
77         msg.header.cmd = cmd;
78         snprintf(msg.header.cmdstr, NG_CMDSTRLEN + 1, "cmd%d", cmd);
79
80         /* Deliver message */
81         if (NgDeliverMsg(cs, path, &msg, args, arglen) < 0)
82                 return (-1);
83         return (msg.header.token);
84 }
85
86 /*
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.
89  */
90 int
91 NgSendAsciiMsg(int cs, const char *path, const char *fmt, ...)
92 {
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;
99         va_list fmtargs;
100
101         /* Parse out command and arguments */
102         va_start(fmtargs, fmt);
103         vasprintf(&buf, fmt, fmtargs);
104         va_end(fmtargs);
105         if (buf == NULL)
106                 return (-1);
107
108         /* Parse out command, arguments */
109         for (cmd = buf; isspace(*cmd); cmd++)
110                 ;
111         for (args = cmd; *args != '\0' && !isspace(*args); args++)
112                 ;
113         if (*args != '\0') {
114                 while (isspace(*args))
115                         *args++ = '\0';
116         }
117
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) {
121                 free(buf);
122                 return (-1);
123         }
124         memset(ascii, 0, sizeof(*ascii));
125
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;
130         free(buf);
131
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)
135                 return (-1);
136
137         /* Get reply */
138         if (NgRecvMsg(cs, reply, sizeof(replybuf), NULL) < 0)
139                 return (-1);
140
141         /* Now send binary version */
142         if (++gMsgId < 0)
143                 gMsgId = 1;
144         binary->header.token = gMsgId;
145         if (NgDeliverMsg(cs,
146             path, binary, binary->data, binary->header.arglen) < 0)
147                 return (-1);
148         return (binary->header.token);
149 }
150
151 /*
152  * Send a message that is a reply to a previously received message.
153  * Returns -1 and sets errno on error, otherwise returns zero.
154  */
155 int
156 NgSendReplyMsg(int cs, const char *path,
157         const struct ng_mesg *msg, const void *args, size_t arglen)
158 {
159         struct ng_mesg rep;
160
161         /* Prepare message header */
162         rep = *msg;
163         rep.header.flags = NGF_RESP;
164
165         /* Deliver message */
166         return (NgDeliverMsg(cs, path, &rep, args, arglen));
167 }
168
169 /*
170  * Send a message to a node using control socket node "cs".
171  * Returns -1 if error and sets errno appropriately, otherwise zero.
172  */
173 static int
174 NgDeliverMsg(int cs, const char *path,
175         const struct ng_mesg *hdr, const void *args, size_t arglen)
176 {
177         u_char sgbuf[NG_PATHLEN + 3];
178         struct sockaddr_ng *const sg = (struct sockaddr_ng *) sgbuf;
179         u_char *buf = NULL;
180         struct ng_mesg *msg;
181         int errnosv = 0;
182         int rtn = 0;
183
184         /* Sanity check */
185         if (args == NULL)
186                 arglen = 0;
187
188         /* Get buffer */
189         if ((buf = malloc(sizeof(*msg) + arglen)) == NULL) {
190                 errnosv = errno;
191                 if (_gNgDebugLevel >= 1)
192                         NGLOG("malloc");
193                 rtn = -1;
194                 goto done;
195         }
196         msg = (struct ng_mesg *) buf;
197
198         /* Finalize message */
199         *msg = *hdr;
200         msg->header.arglen = arglen;
201         memcpy(msg->data, args, arglen);
202
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;
207
208         /* Debugging */
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);
214         }
215
216         /* Send it */
217         if (sendto(cs, msg, sizeof(*msg) + arglen,
218                    0, (struct sockaddr *) sg, sg->sg_len) < 0) {
219                 errnosv = errno;
220                 if (_gNgDebugLevel >= 1)
221                         NGLOG("sendto(%s)", sg->sg_data);
222                 rtn = -1;
223                 goto done;
224         }
225
226 done:
227         /* Done */
228         free(buf);              /* OK if buf is NULL */
229         errno = errnosv;
230         return (rtn);
231 }
232
233 /*
234  * Receive a control message.
235  *
236  * On error, this returns -1 and sets errno.
237  * Otherwise, it returns the length of the received reply.
238  */
239 int
240 NgRecvMsg(int cs, struct ng_mesg *rep, size_t replen, char *path)
241 {
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);
245         int errnosv;
246
247         /* Read reply */
248         len = recvfrom(cs, rep, replen, 0, (struct sockaddr *) sg, &sglen);
249         if (len < 0) {
250                 errnosv = errno;
251                 if (_gNgDebugLevel >= 1)
252                         NGLOG("recvfrom");
253                 goto errout;
254         }
255         if (path != NULL)
256                 snprintf(path, NG_PATHLEN + 1, "%s", sg->sg_data);
257
258         /* Debugging */
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);
264         }
265
266         /* Done */
267         return (len);
268
269 errout:
270         errno = errnosv;
271         return (-1);
272 }
273
274 /*
275  * Receive a control message and convert the arguments to ASCII
276  */
277 int
278 NgRecvAsciiMsg(int cs, struct ng_mesg *reply, size_t replen, char *path)
279 {
280         struct ng_mesg *msg, *ascii;
281         int bufSize, errnosv;
282         u_char *buf;
283
284         /* Allocate buffer */
285         bufSize = 2 * sizeof(*reply) + replen;
286         if ((buf = malloc(bufSize)) == NULL)
287                 return (-1);
288         msg = (struct ng_mesg *)buf;
289         ascii = (struct ng_mesg *)msg->data;
290
291         /* Get binary message */
292         if (NgRecvMsg(cs, msg, bufSize, path) < 0)
293                 goto fail;
294         memcpy(reply, msg, sizeof(*msg));
295
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)
299                 goto fail;
300         if (NgRecvMsg(cs, msg, bufSize, NULL) < 0)
301                 goto fail;
302
303         /* Copy result to client buffer */
304         if (sizeof(*ascii) + ascii->header.arglen > replen) {
305                 errno = ERANGE;
306 fail:
307                 errnosv = errno;
308                 free(buf);
309                 errno = errnosv;
310                 return (-1);
311         }
312         strncpy(reply->data, ascii->data, ascii->header.arglen);
313
314         /* Done */
315         free(buf);
316         return (0);
317 }
318