xref: /dragonfly/lib/libnetgraph7/msg.c (revision 0147868e)
1*0147868eSNuno Antunes /*
2*0147868eSNuno Antunes  * msg.c
3*0147868eSNuno Antunes  *
4*0147868eSNuno Antunes  * Copyright (c) 1996-1999 Whistle Communications, Inc.
5*0147868eSNuno Antunes  * All rights reserved.
6*0147868eSNuno Antunes  *
7*0147868eSNuno Antunes  * Subject to the following obligations and disclaimer of warranty, use and
8*0147868eSNuno Antunes  * redistribution of this software, in source or object code forms, with or
9*0147868eSNuno Antunes  * without modifications are expressly permitted by Whistle Communications;
10*0147868eSNuno Antunes  * provided, however, that:
11*0147868eSNuno Antunes  * 1. Any and all reproductions of the source or object code must include the
12*0147868eSNuno Antunes  *    copyright notice above and the following disclaimer of warranties; and
13*0147868eSNuno Antunes  * 2. No rights are granted, in any manner or form, to use Whistle
14*0147868eSNuno Antunes  *    Communications, Inc. trademarks, including the mark "WHISTLE
15*0147868eSNuno Antunes  *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
16*0147868eSNuno Antunes  *    such appears in the above copyright notice or in the software.
17*0147868eSNuno Antunes  *
18*0147868eSNuno Antunes  * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
19*0147868eSNuno Antunes  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
20*0147868eSNuno Antunes  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
21*0147868eSNuno Antunes  * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
22*0147868eSNuno Antunes  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
23*0147868eSNuno Antunes  * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
24*0147868eSNuno Antunes  * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
25*0147868eSNuno Antunes  * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
26*0147868eSNuno Antunes  * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
27*0147868eSNuno Antunes  * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
28*0147868eSNuno Antunes  * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
29*0147868eSNuno Antunes  * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
30*0147868eSNuno Antunes  * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
31*0147868eSNuno Antunes  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32*0147868eSNuno Antunes  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33*0147868eSNuno Antunes  * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
34*0147868eSNuno Antunes  * OF SUCH DAMAGE.
35*0147868eSNuno Antunes  *
36*0147868eSNuno Antunes  * Author: Archie Cobbs <archie@whistle.com>
37*0147868eSNuno Antunes  *
38*0147868eSNuno Antunes  * $FreeBSD: src/lib/libnetgraph/msg.c,v 1.14 2007/05/14 14:18:41 mav Exp $
39*0147868eSNuno Antunes  * $DragonFly: src/lib/libnetgraph/msg.c,v 1.4 2007/06/03 23:41:25 swildner Exp $
40*0147868eSNuno Antunes  * $Whistle: msg.c,v 1.9 1999/01/20 00:57:23 archie Exp $
41*0147868eSNuno Antunes  */
42*0147868eSNuno Antunes 
43*0147868eSNuno Antunes #include <sys/types.h>
44*0147868eSNuno Antunes #include <sys/socket.h>
45*0147868eSNuno Antunes #include <stdarg.h>
46*0147868eSNuno Antunes #include <netgraph7/ng_message.h>
47*0147868eSNuno Antunes #include <netgraph7/socket/ng_socket.h>
48*0147868eSNuno Antunes 
49*0147868eSNuno Antunes #include "netgraph.h"
50*0147868eSNuno Antunes #include "internal.h"
51*0147868eSNuno Antunes 
52*0147868eSNuno Antunes /* Next message token value */
53*0147868eSNuno Antunes static int	gMsgId;
54*0147868eSNuno Antunes 
55*0147868eSNuno Antunes /* For delivering both messages and replies */
56*0147868eSNuno Antunes static int	NgDeliverMsg(int cs, const char *path,
57*0147868eSNuno Antunes 		  const struct ng_mesg *hdr, const void *args, size_t arglen);
58*0147868eSNuno Antunes 
59*0147868eSNuno Antunes /*
60*0147868eSNuno Antunes  * Send a message to a node using control socket node "cs".
61*0147868eSNuno Antunes  * Returns -1 if error and sets errno appropriately.
62*0147868eSNuno Antunes  * If successful, returns the message ID (token) used.
63*0147868eSNuno Antunes  */
64*0147868eSNuno Antunes int
NgSendMsg(int cs,const char * path,int cookie,int cmd,const void * args,size_t arglen)65*0147868eSNuno Antunes NgSendMsg(int cs, const char *path,
66*0147868eSNuno Antunes 	  int cookie, int cmd, const void *args, size_t arglen)
67*0147868eSNuno Antunes {
68*0147868eSNuno Antunes 	struct ng_mesg msg;
69*0147868eSNuno Antunes 
70*0147868eSNuno Antunes 	/* Prepare message header */
71*0147868eSNuno Antunes 	memset(&msg, 0, sizeof(msg));
72*0147868eSNuno Antunes 	msg.header.version = NG_VERSION;
73*0147868eSNuno Antunes 	msg.header.typecookie = cookie;
74*0147868eSNuno Antunes 	if (++gMsgId < 0)
75*0147868eSNuno Antunes 		gMsgId = 1;
76*0147868eSNuno Antunes 	msg.header.token = gMsgId;
77*0147868eSNuno Antunes 	msg.header.flags = NGF_ORIG;
78*0147868eSNuno Antunes 	msg.header.cmd = cmd;
79*0147868eSNuno Antunes 	snprintf((char *)msg.header.cmdstr, NG_CMDSTRSIZ, "cmd%d", cmd);
80*0147868eSNuno Antunes 
81*0147868eSNuno Antunes 	/* Deliver message */
82*0147868eSNuno Antunes 	if (NgDeliverMsg(cs, path, &msg, args, arglen) < 0)
83*0147868eSNuno Antunes 		return (-1);
84*0147868eSNuno Antunes 	return (msg.header.token);
85*0147868eSNuno Antunes }
86*0147868eSNuno Antunes 
87*0147868eSNuno Antunes /*
88*0147868eSNuno Antunes  * Send a message given in ASCII format. We first ask the node to translate
89*0147868eSNuno Antunes  * the command into binary, and then we send the binary.
90*0147868eSNuno Antunes  */
91*0147868eSNuno Antunes int
NgSendAsciiMsg(int cs,const char * path,const char * fmt,...)92*0147868eSNuno Antunes NgSendAsciiMsg(int cs, const char *path, const char *fmt, ...)
93*0147868eSNuno Antunes {
94*0147868eSNuno Antunes 	struct ng_mesg *reply, *binary, *ascii;
95*0147868eSNuno Antunes 	char *buf, *cmd, *args;
96*0147868eSNuno Antunes 	va_list fmtargs;
97*0147868eSNuno Antunes 	int token;
98*0147868eSNuno Antunes 
99*0147868eSNuno Antunes 	/* Parse out command and arguments */
100*0147868eSNuno Antunes 	va_start(fmtargs, fmt);
101*0147868eSNuno Antunes 	vasprintf(&buf, fmt, fmtargs);
102*0147868eSNuno Antunes 	va_end(fmtargs);
103*0147868eSNuno Antunes 	if (buf == NULL)
104*0147868eSNuno Antunes 		return (-1);
105*0147868eSNuno Antunes 
106*0147868eSNuno Antunes 	/* Parse out command, arguments */
107*0147868eSNuno Antunes 	for (cmd = buf; isspace(*cmd); cmd++)
108*0147868eSNuno Antunes 		;
109*0147868eSNuno Antunes 	for (args = cmd; *args != '\0' && !isspace(*args); args++)
110*0147868eSNuno Antunes 		;
111*0147868eSNuno Antunes 	if (*args != '\0') {
112*0147868eSNuno Antunes 		while (isspace(*args))
113*0147868eSNuno Antunes 			*args++ = '\0';
114*0147868eSNuno Antunes 	}
115*0147868eSNuno Antunes 
116*0147868eSNuno Antunes 	/* Get a bigger buffer to hold inner message header plus arg string */
117*0147868eSNuno Antunes 	if ((ascii = malloc(sizeof(struct ng_mesg)
118*0147868eSNuno Antunes 	    + strlen(args) + 1)) == NULL) {
119*0147868eSNuno Antunes 		free(buf);
120*0147868eSNuno Antunes 		return (-1);
121*0147868eSNuno Antunes 	}
122*0147868eSNuno Antunes 	memset(ascii, 0, sizeof(*ascii));
123*0147868eSNuno Antunes 
124*0147868eSNuno Antunes 	/* Build inner header (only need cmdstr, arglen, and data fields) */
125*0147868eSNuno Antunes 	strncpy((char *)ascii->header.cmdstr, cmd,
126*0147868eSNuno Antunes 	    sizeof(ascii->header.cmdstr) - 1);
127*0147868eSNuno Antunes 	strcpy(ascii->data, args);
128*0147868eSNuno Antunes 	ascii->header.arglen = strlen(ascii->data) + 1;
129*0147868eSNuno Antunes 	free(buf);
130*0147868eSNuno Antunes 
131*0147868eSNuno Antunes 	/* Send node a request to convert ASCII to binary */
132*0147868eSNuno Antunes 	if (NgSendMsg(cs, path, NGM_GENERIC_COOKIE, NGM_ASCII2BINARY,
133*0147868eSNuno Antunes 	    (u_char *)ascii, sizeof(*ascii) + ascii->header.arglen) < 0) {
134*0147868eSNuno Antunes 		free(ascii);
135*0147868eSNuno Antunes 		return (-1);
136*0147868eSNuno Antunes 	}
137*0147868eSNuno Antunes 	free(ascii);
138*0147868eSNuno Antunes 
139*0147868eSNuno Antunes 	/* Get reply */
140*0147868eSNuno Antunes 	if (NgAllocRecvMsg(cs, &reply, NULL) < 0)
141*0147868eSNuno Antunes 		return (-1);
142*0147868eSNuno Antunes 
143*0147868eSNuno Antunes 	/* Now send binary version */
144*0147868eSNuno Antunes 	binary = (struct ng_mesg *)reply->data;
145*0147868eSNuno Antunes 	if (++gMsgId < 0)
146*0147868eSNuno Antunes 		gMsgId = 1;
147*0147868eSNuno Antunes 	binary->header.token = gMsgId;
148*0147868eSNuno Antunes 	binary->header.version = NG_VERSION;
149*0147868eSNuno Antunes 	if (NgDeliverMsg(cs,
150*0147868eSNuno Antunes 	    path, binary, binary->data, binary->header.arglen) < 0) {
151*0147868eSNuno Antunes 		free(reply);
152*0147868eSNuno Antunes 		return (-1);
153*0147868eSNuno Antunes 	}
154*0147868eSNuno Antunes 	token = binary->header.token;
155*0147868eSNuno Antunes 	free(reply);
156*0147868eSNuno Antunes 	return (token);
157*0147868eSNuno Antunes }
158*0147868eSNuno Antunes 
159*0147868eSNuno Antunes /*
160*0147868eSNuno Antunes  * Send a message that is a reply to a previously received message.
161*0147868eSNuno Antunes  * Returns -1 and sets errno on error, otherwise returns zero.
162*0147868eSNuno Antunes  */
163*0147868eSNuno Antunes int
NgSendReplyMsg(int cs,const char * path,const struct ng_mesg * msg,const void * args,size_t arglen)164*0147868eSNuno Antunes NgSendReplyMsg(int cs, const char *path,
165*0147868eSNuno Antunes 	const struct ng_mesg *msg, const void *args, size_t arglen)
166*0147868eSNuno Antunes {
167*0147868eSNuno Antunes 	struct ng_mesg rep;
168*0147868eSNuno Antunes 
169*0147868eSNuno Antunes 	/* Prepare message header */
170*0147868eSNuno Antunes 	rep = *msg;
171*0147868eSNuno Antunes 	rep.header.flags = NGF_RESP;
172*0147868eSNuno Antunes 
173*0147868eSNuno Antunes 	/* Deliver message */
174*0147868eSNuno Antunes 	return (NgDeliverMsg(cs, path, &rep, args, arglen));
175*0147868eSNuno Antunes }
176*0147868eSNuno Antunes 
177*0147868eSNuno Antunes /*
178*0147868eSNuno Antunes  * Send a message to a node using control socket node "cs".
179*0147868eSNuno Antunes  * Returns -1 if error and sets errno appropriately, otherwise zero.
180*0147868eSNuno Antunes  */
181*0147868eSNuno Antunes static int
NgDeliverMsg(int cs,const char * path,const struct ng_mesg * hdr,const void * args,size_t arglen)182*0147868eSNuno Antunes NgDeliverMsg(int cs, const char *path,
183*0147868eSNuno Antunes 	const struct ng_mesg *hdr, const void *args, size_t arglen)
184*0147868eSNuno Antunes {
185*0147868eSNuno Antunes 	u_char sgbuf[NG_PATHSIZ + NGSA_OVERHEAD];
186*0147868eSNuno Antunes 	struct sockaddr_ng *const sg = (struct sockaddr_ng *) sgbuf;
187*0147868eSNuno Antunes 	u_char *buf = NULL;
188*0147868eSNuno Antunes 	struct ng_mesg *msg;
189*0147868eSNuno Antunes 	int errnosv = 0;
190*0147868eSNuno Antunes 	int rtn = 0;
191*0147868eSNuno Antunes 
192*0147868eSNuno Antunes 	/* Sanity check */
193*0147868eSNuno Antunes 	if (args == NULL)
194*0147868eSNuno Antunes 		arglen = 0;
195*0147868eSNuno Antunes 
196*0147868eSNuno Antunes 	/* Get buffer */
197*0147868eSNuno Antunes 	if ((buf = malloc(sizeof(*msg) + arglen)) == NULL) {
198*0147868eSNuno Antunes 		errnosv = errno;
199*0147868eSNuno Antunes 		if (_gNgDebugLevel >= 1)
200*0147868eSNuno Antunes 			NGLOG("malloc");
201*0147868eSNuno Antunes 		rtn = -1;
202*0147868eSNuno Antunes 		goto done;
203*0147868eSNuno Antunes 	}
204*0147868eSNuno Antunes 	msg = (struct ng_mesg *) buf;
205*0147868eSNuno Antunes 
206*0147868eSNuno Antunes 	/* Finalize message */
207*0147868eSNuno Antunes 	*msg = *hdr;
208*0147868eSNuno Antunes 	msg->header.arglen = arglen;
209*0147868eSNuno Antunes 	memcpy(msg->data, args, arglen);
210*0147868eSNuno Antunes 
211*0147868eSNuno Antunes 	/* Prepare socket address */
212*0147868eSNuno Antunes 	sg->sg_family = AF_NETGRAPH;
213*0147868eSNuno Antunes 	/* XXX handle overflow */
214*0147868eSNuno Antunes 	strlcpy(sg->sg_data, path, NG_PATHSIZ);
215*0147868eSNuno Antunes 	sg->sg_len = strlen(sg->sg_data) + 1 + NGSA_OVERHEAD;
216*0147868eSNuno Antunes 
217*0147868eSNuno Antunes 	/* Debugging */
218*0147868eSNuno Antunes 	if (_gNgDebugLevel >= 2) {
219*0147868eSNuno Antunes 		NGLOGX("SENDING %s:",
220*0147868eSNuno Antunes 		    (msg->header.flags & NGF_RESP) ? "RESPONSE" : "MESSAGE");
221*0147868eSNuno Antunes 		_NgDebugSockaddr(sg);
222*0147868eSNuno Antunes 		_NgDebugMsg(msg, sg->sg_data);
223*0147868eSNuno Antunes 	}
224*0147868eSNuno Antunes 
225*0147868eSNuno Antunes 	/* Send it */
226*0147868eSNuno Antunes 	if (sendto(cs, msg, sizeof(*msg) + arglen,
227*0147868eSNuno Antunes 		   0, (struct sockaddr *) sg, sg->sg_len) < 0) {
228*0147868eSNuno Antunes 		errnosv = errno;
229*0147868eSNuno Antunes 		if (_gNgDebugLevel >= 1)
230*0147868eSNuno Antunes 			NGLOG("sendto(%s)", sg->sg_data);
231*0147868eSNuno Antunes 		rtn = -1;
232*0147868eSNuno Antunes 		goto done;
233*0147868eSNuno Antunes 	}
234*0147868eSNuno Antunes 
235*0147868eSNuno Antunes 	/* Wait for reply if there should be one. */
236*0147868eSNuno Antunes 	if (msg->header.cmd & NGM_HASREPLY) {
237*0147868eSNuno Antunes 		struct pollfd rfds;
238*0147868eSNuno Antunes 		int n;
239*0147868eSNuno Antunes 
240*0147868eSNuno Antunes 		rfds.fd = cs;
241*0147868eSNuno Antunes 		rfds.events = POLLIN;
242*0147868eSNuno Antunes 		rfds.revents = 0;
243*0147868eSNuno Antunes 		n = poll(&rfds, 1, INFTIM);
244*0147868eSNuno Antunes 		if (n == -1) {
245*0147868eSNuno Antunes 			errnosv = errno;
246*0147868eSNuno Antunes 			if (_gNgDebugLevel >= 1)
247*0147868eSNuno Antunes 				NGLOG("poll");
248*0147868eSNuno Antunes 			rtn = -1;
249*0147868eSNuno Antunes 		}
250*0147868eSNuno Antunes 	}
251*0147868eSNuno Antunes 
252*0147868eSNuno Antunes done:
253*0147868eSNuno Antunes 	/* Done */
254*0147868eSNuno Antunes 	free(buf);		/* OK if buf is NULL */
255*0147868eSNuno Antunes 	errno = errnosv;
256*0147868eSNuno Antunes 	return (rtn);
257*0147868eSNuno Antunes }
258*0147868eSNuno Antunes 
259*0147868eSNuno Antunes /*
260*0147868eSNuno Antunes  * Receive a control message.
261*0147868eSNuno Antunes  *
262*0147868eSNuno Antunes  * On error, this returns -1 and sets errno.
263*0147868eSNuno Antunes  * Otherwise, it returns the length of the received reply.
264*0147868eSNuno Antunes  */
265*0147868eSNuno Antunes int
NgRecvMsg(int cs,struct ng_mesg * rep,size_t replen,char * path)266*0147868eSNuno Antunes NgRecvMsg(int cs, struct ng_mesg *rep, size_t replen, char *path)
267*0147868eSNuno Antunes {
268*0147868eSNuno Antunes 	u_char sgbuf[NG_PATHSIZ + NGSA_OVERHEAD];
269*0147868eSNuno Antunes 	struct sockaddr_ng *const sg = (struct sockaddr_ng *) sgbuf;
270*0147868eSNuno Antunes 	socklen_t sglen = sizeof(sgbuf);
271*0147868eSNuno Antunes 	int len, errnosv;
272*0147868eSNuno Antunes 
273*0147868eSNuno Antunes 	/* Read reply */
274*0147868eSNuno Antunes 	len = recvfrom(cs, rep, replen, 0, (struct sockaddr *) sg, &sglen);
275*0147868eSNuno Antunes 	if (len < 0) {
276*0147868eSNuno Antunes 		errnosv = errno;
277*0147868eSNuno Antunes 		if (_gNgDebugLevel >= 1)
278*0147868eSNuno Antunes 			NGLOG("recvfrom");
279*0147868eSNuno Antunes 		goto errout;
280*0147868eSNuno Antunes 	}
281*0147868eSNuno Antunes 	if (path != NULL)
282*0147868eSNuno Antunes 		strlcpy(path, sg->sg_data, NG_PATHSIZ);
283*0147868eSNuno Antunes 
284*0147868eSNuno Antunes 	/* Debugging */
285*0147868eSNuno Antunes 	if (_gNgDebugLevel >= 2) {
286*0147868eSNuno Antunes 		NGLOGX("RECEIVED %s:",
287*0147868eSNuno Antunes 		    (rep->header.flags & NGF_RESP) ? "RESPONSE" : "MESSAGE");
288*0147868eSNuno Antunes 		_NgDebugSockaddr(sg);
289*0147868eSNuno Antunes 		_NgDebugMsg(rep, sg->sg_data);
290*0147868eSNuno Antunes 	}
291*0147868eSNuno Antunes 
292*0147868eSNuno Antunes 	/* Done */
293*0147868eSNuno Antunes 	return (len);
294*0147868eSNuno Antunes 
295*0147868eSNuno Antunes errout:
296*0147868eSNuno Antunes 	errno = errnosv;
297*0147868eSNuno Antunes 	return (-1);
298*0147868eSNuno Antunes }
299*0147868eSNuno Antunes 
300*0147868eSNuno Antunes /*
301*0147868eSNuno Antunes  * Identical to NgRecvMsg() except buffer is dynamically allocated.
302*0147868eSNuno Antunes  */
303*0147868eSNuno Antunes int
NgAllocRecvMsg(int cs,struct ng_mesg ** rep,char * path)304*0147868eSNuno Antunes NgAllocRecvMsg(int cs, struct ng_mesg **rep, char *path)
305*0147868eSNuno Antunes {
306*0147868eSNuno Antunes 	int len;
307*0147868eSNuno Antunes 	socklen_t optlen;
308*0147868eSNuno Antunes 
309*0147868eSNuno Antunes 	optlen = sizeof(len);
310*0147868eSNuno Antunes 	if (getsockopt(cs, SOL_SOCKET, SO_RCVBUF, &len, &optlen) == -1 ||
311*0147868eSNuno Antunes 	    (*rep = malloc(len)) == NULL)
312*0147868eSNuno Antunes 		return (-1);
313*0147868eSNuno Antunes 	if ((len = NgRecvMsg(cs, *rep, len, path)) < 0)
314*0147868eSNuno Antunes 		free(*rep);
315*0147868eSNuno Antunes 	return (len);
316*0147868eSNuno Antunes }
317*0147868eSNuno Antunes 
318*0147868eSNuno Antunes /*
319*0147868eSNuno Antunes  * Receive a control message and convert the arguments to ASCII
320*0147868eSNuno Antunes  */
321*0147868eSNuno Antunes int
NgRecvAsciiMsg(int cs,struct ng_mesg * reply,size_t replen,char * path)322*0147868eSNuno Antunes NgRecvAsciiMsg(int cs, struct ng_mesg *reply, size_t replen, char *path)
323*0147868eSNuno Antunes {
324*0147868eSNuno Antunes 	struct ng_mesg *msg, *ascii;
325*0147868eSNuno Antunes 	int bufSize, errnosv;
326*0147868eSNuno Antunes 	u_char *buf;
327*0147868eSNuno Antunes 
328*0147868eSNuno Antunes 	/* Allocate buffer */
329*0147868eSNuno Antunes 	bufSize = 2 * sizeof(*reply) + replen;
330*0147868eSNuno Antunes 	if ((buf = malloc(bufSize)) == NULL)
331*0147868eSNuno Antunes 		return (-1);
332*0147868eSNuno Antunes 	msg = (struct ng_mesg *)buf;
333*0147868eSNuno Antunes 	ascii = (struct ng_mesg *)msg->data;
334*0147868eSNuno Antunes 
335*0147868eSNuno Antunes 	/* Get binary message */
336*0147868eSNuno Antunes 	if (NgRecvMsg(cs, msg, bufSize, path) < 0)
337*0147868eSNuno Antunes 		goto fail;
338*0147868eSNuno Antunes 	memcpy(reply, msg, sizeof(*msg));
339*0147868eSNuno Antunes 
340*0147868eSNuno Antunes 	/* Ask originating node to convert the arguments to ASCII */
341*0147868eSNuno Antunes 	if (NgSendMsg(cs, path, NGM_GENERIC_COOKIE,
342*0147868eSNuno Antunes 	    NGM_BINARY2ASCII, msg, sizeof(*msg) + msg->header.arglen) < 0)
343*0147868eSNuno Antunes 		goto fail;
344*0147868eSNuno Antunes 	if (NgRecvMsg(cs, msg, bufSize, NULL) < 0)
345*0147868eSNuno Antunes 		goto fail;
346*0147868eSNuno Antunes 
347*0147868eSNuno Antunes 	/* Copy result to client buffer */
348*0147868eSNuno Antunes 	if (sizeof(*ascii) + ascii->header.arglen > replen) {
349*0147868eSNuno Antunes 		errno = ERANGE;
350*0147868eSNuno Antunes fail:
351*0147868eSNuno Antunes 		errnosv = errno;
352*0147868eSNuno Antunes 		free(buf);
353*0147868eSNuno Antunes 		errno = errnosv;
354*0147868eSNuno Antunes 		return (-1);
355*0147868eSNuno Antunes 	}
356*0147868eSNuno Antunes 	strncpy(reply->data, ascii->data, ascii->header.arglen);
357*0147868eSNuno Antunes 
358*0147868eSNuno Antunes 	/* Done */
359*0147868eSNuno Antunes 	free(buf);
360*0147868eSNuno Antunes 	return (0);
361*0147868eSNuno Antunes }
362*0147868eSNuno Antunes 
363*0147868eSNuno Antunes /*
364*0147868eSNuno Antunes  * Identical to NgRecvAsciiMsg() except buffer is dynamically allocated.
365*0147868eSNuno Antunes  */
366*0147868eSNuno Antunes int
NgAllocRecvAsciiMsg(int cs,struct ng_mesg ** reply,char * path)367*0147868eSNuno Antunes NgAllocRecvAsciiMsg(int cs, struct ng_mesg **reply, char *path)
368*0147868eSNuno Antunes {
369*0147868eSNuno Antunes 	int len;
370*0147868eSNuno Antunes 	socklen_t optlen;
371*0147868eSNuno Antunes 
372*0147868eSNuno Antunes 	optlen = sizeof(len);
373*0147868eSNuno Antunes 	if (getsockopt(cs, SOL_SOCKET, SO_RCVBUF, &len, &optlen) == -1 ||
374*0147868eSNuno Antunes 	    (*reply = malloc(len)) == NULL)
375*0147868eSNuno Antunes 		return (-1);
376*0147868eSNuno Antunes 	if ((len = NgRecvAsciiMsg(cs, *reply, len, path)) < 0)
377*0147868eSNuno Antunes 		free(*reply);
378*0147868eSNuno Antunes 	return (len);
379*0147868eSNuno Antunes }
380