sock.c revision 135576
1/*
2 * sock.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: sock.c,v 1.12 1999/01/20 00:57:23 archie Exp $
39 */
40
41#include <sys/cdefs.h>
42__FBSDID("$FreeBSD: head/lib/libnetgraph/sock.c 135576 2004-09-22 16:56:49Z stefanf $");
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/* The socket node type KLD */
53#define NG_SOCKET_KLD	"ng_socket.ko"
54
55/*
56 * Create a socket type node and give it the supplied name.
57 * Return data and control sockets corresponding to the node.
58 * Returns -1 if error and sets errno.
59 */
60int
61NgMkSockNode(const char *name, int *csp, int *dsp)
62{
63	char namebuf[NG_NODESIZ];
64	int cs = -1;		/* control socket */
65	int ds = -1;		/* data socket */
66	int errnosv;
67
68	/* Empty name means no name */
69	if (name && *name == 0)
70		name = NULL;
71
72	/* Create control socket; this also creates the netgraph node.
73	   If we get an EPROTONOSUPPORT then the socket node type is
74	   not loaded, so load it and try again. */
75	if ((cs = socket(AF_NETGRAPH, SOCK_DGRAM, NG_CONTROL)) < 0) {
76		if (errno == EPROTONOSUPPORT) {
77			if (kldload(NG_SOCKET_KLD) < 0) {
78				errnosv = errno;
79				if (_gNgDebugLevel >= 1)
80					NGLOG("can't load %s", NG_SOCKET_KLD);
81				goto errout;
82			}
83			cs = socket(AF_NETGRAPH, SOCK_DGRAM, NG_CONTROL);
84			if (cs >= 0)
85				goto gotNode;
86		}
87		errnosv = errno;
88		if (_gNgDebugLevel >= 1)
89			NGLOG("socket");
90		goto errout;
91	}
92
93gotNode:
94	/* Assign the node the desired name, if any */
95	if (name != NULL) {
96		u_char sbuf[NG_NODESIZ + NGSA_OVERHEAD];
97		struct sockaddr_ng *const sg = (struct sockaddr_ng *) sbuf;
98
99		/* Assign name */
100		strlcpy(sg->sg_data, name, NG_NODESIZ);
101		sg->sg_family = AF_NETGRAPH;
102		sg->sg_len = strlen(sg->sg_data) + 1 + NGSA_OVERHEAD;
103		if (bind(cs, (struct sockaddr *) sg, sg->sg_len) < 0) {
104			errnosv = errno;
105			if (_gNgDebugLevel >= 1)
106				NGLOG("bind(%s)", sg->sg_data);
107			goto errout;
108		}
109
110		/* Save node name */
111		strlcpy(namebuf, name, sizeof(namebuf));
112	} else if (dsp != NULL) {
113		u_char rbuf[sizeof(struct ng_mesg) + sizeof(struct nodeinfo)];
114		struct ng_mesg *const resp = (struct ng_mesg *) rbuf;
115		struct nodeinfo *const ni = (struct nodeinfo *) resp->data;
116
117		/* Find out the node ID */
118		if (NgSendMsg(cs, ".", NGM_GENERIC_COOKIE,
119		    NGM_NODEINFO, NULL, 0) < 0) {
120			errnosv = errno;
121			if (_gNgDebugLevel >= 1)
122				NGLOG("send nodeinfo");
123			goto errout;
124		}
125		if (NgRecvMsg(cs, resp, sizeof(rbuf), NULL) < 0) {
126			errnosv = errno;
127			if (_gNgDebugLevel >= 1)
128				NGLOG("recv nodeinfo");
129			goto errout;
130		}
131
132		/* Save node "name" */
133		snprintf(namebuf, sizeof(namebuf), "[%lx]", (u_long) ni->id);
134	}
135
136	/* Create data socket if desired */
137	if (dsp != NULL) {
138		u_char sbuf[NG_NODESIZ + 1 + NGSA_OVERHEAD];
139		struct sockaddr_ng *const sg = (struct sockaddr_ng *) sbuf;
140
141		/* Create data socket, initially just "floating" */
142		if ((ds = socket(AF_NETGRAPH, SOCK_DGRAM, NG_DATA)) < 0) {
143			errnosv = errno;
144			if (_gNgDebugLevel >= 1)
145				NGLOG("socket");
146			goto errout;
147		}
148
149		/* Associate the data socket with the node */
150		snprintf(sg->sg_data, NG_NODESIZ + 1, "%s:", namebuf);
151		sg->sg_family = AF_NETGRAPH;
152		sg->sg_len = strlen(sg->sg_data) + 1 + NGSA_OVERHEAD;
153		if (connect(ds, (struct sockaddr *) sg, sg->sg_len) < 0) {
154			errnosv = errno;
155			if (_gNgDebugLevel >= 1)
156				NGLOG("connect(%s)", sg->sg_data);
157			goto errout;
158		}
159	}
160
161	/* Return the socket(s) */
162	if (csp)
163		*csp = cs;
164	else
165		close(cs);
166	if (dsp)
167		*dsp = ds;
168	return (0);
169
170errout:
171	/* Failed */
172	if (cs >= 0)
173		close(cs);
174	if (ds >= 0)
175		close(ds);
176	errno = errnosv;
177	return (-1);
178}
179
180/*
181 * Assign a globally unique name to a node
182 * Returns -1 if error and sets errno.
183 */
184int
185NgNameNode(int cs, const char *path, const char *fmt, ...)
186{
187	struct ngm_name ngn;
188	va_list args;
189
190	/* Build message arg */
191	va_start(args, fmt);
192	vsnprintf(ngn.name, sizeof(ngn.name), fmt, args);
193	va_end(args);
194
195	/* Send message */
196	if (NgSendMsg(cs, path,
197	    NGM_GENERIC_COOKIE, NGM_NAME, &ngn, sizeof(ngn)) < 0) {
198		if (_gNgDebugLevel >= 1)
199			NGLOGX("%s: failed", __func__);
200		return (-1);
201	}
202
203	/* Done */
204	return (0);
205}
206
207/*
208 * Read a packet from a data socket
209 * Returns -1 if error and sets errno.
210 */
211int
212NgRecvData(int ds, u_char * buf, size_t len, char *hook)
213{
214	u_char frombuf[NG_HOOKSIZ + NGSA_OVERHEAD];
215	struct sockaddr_ng *const from = (struct sockaddr_ng *) frombuf;
216	int fromlen = sizeof(frombuf);
217	int rtn, errnosv;
218
219	/* Read packet */
220	rtn = recvfrom(ds, buf, len, 0, (struct sockaddr *) from, &fromlen);
221	if (rtn < 0) {
222		errnosv = errno;
223		if (_gNgDebugLevel >= 1)
224			NGLOG("recvfrom");
225		errno = errnosv;
226		return (-1);
227	}
228
229	/* Copy hook name */
230	if (hook != NULL)
231		strlcpy(hook, from->sg_data, NG_HOOKSIZ);
232
233	/* Debugging */
234	if (_gNgDebugLevel >= 2) {
235		NGLOGX("READ %s from hook \"%s\" (%d bytes)",
236		       rtn ? "PACKET" : "EOF", from->sg_data, rtn);
237		if (_gNgDebugLevel >= 3)
238			_NgDebugBytes(buf, rtn);
239	}
240
241	/* Done */
242	return (rtn);
243}
244
245/*
246 * Identical to NgRecvData() except buffer is dynamically allocated.
247 */
248int
249NgAllocRecvData(int ds, u_char **buf, char *hook)
250{
251	int len;
252	socklen_t optlen;
253
254	optlen = sizeof(len);
255	if (getsockopt(ds, SOL_SOCKET, SO_RCVBUF, &len, &optlen) == -1 ||
256	    (*buf = malloc(len)) == NULL)
257		return (-1);
258	if ((len = NgRecvData(ds, *buf, len, hook)) < 0)
259		free(*buf);
260	return (len);
261}
262
263/*
264 * Write a packet to a data socket. The packet will be sent
265 * out the corresponding node on the specified hook.
266 * Returns -1 if error and sets errno.
267 */
268int
269NgSendData(int ds, const char *hook, const u_char * buf, size_t len)
270{
271	u_char sgbuf[NG_HOOKSIZ + NGSA_OVERHEAD];
272	struct sockaddr_ng *const sg = (struct sockaddr_ng *) sgbuf;
273	int errnosv;
274
275	/* Set up destination hook */
276	sg->sg_family = AF_NETGRAPH;
277	strlcpy(sg->sg_data, hook, NG_HOOKSIZ);
278	sg->sg_len = strlen(sg->sg_data) + 1 + NGSA_OVERHEAD;
279
280	/* Debugging */
281	if (_gNgDebugLevel >= 2) {
282		NGLOGX("WRITE PACKET to hook \"%s\" (%d bytes)", hook, len);
283		_NgDebugSockaddr(sg);
284		if (_gNgDebugLevel >= 3)
285			_NgDebugBytes(buf, len);
286	}
287
288	/* Send packet */
289	if (sendto(ds, buf, len, 0, (struct sockaddr *) sg, sg->sg_len) < 0) {
290		errnosv = errno;
291		if (_gNgDebugLevel >= 1)
292			NGLOG("sendto(%s)", sg->sg_data);
293		errno = errnosv;
294		return (-1);
295	}
296
297	/* Done */
298	return (0);
299}
300
301