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