xref: /freebsd/lib/libnetgraph/sock.c (revision 22cf89c938886d14f5796fc49f9f020c23ea8eaf)
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 #include <sys/types.h>
43 #include <sys/socket.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  */
59 int
60 NgMkSockNode(const char *name, int *csp, int *dsp)
61 {
62 	char namebuf[NG_NODESIZ];
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 an EAFNOSUPPORT 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 == EAFNOSUPPORT) {
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 
92 gotNode:
93 	/* Assign the node the desired name, if any */
94 	if (name != NULL) {
95 		u_char sbuf[NG_NODESIZ + NGSA_OVERHEAD];
96 		struct sockaddr_ng *const sg = (struct sockaddr_ng *) sbuf;
97 
98 		/* Assign name */
99 		strlcpy(sg->sg_data, name, NG_NODESIZ);
100 		sg->sg_family = AF_NETGRAPH;
101 		sg->sg_len = strlen(sg->sg_data) + 1 + NGSA_OVERHEAD;
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 		strlcpy(namebuf, name, sizeof(namebuf));
111 	} else if (dsp != NULL) {
112 		union {
113 			u_char rbuf[sizeof(struct ng_mesg) +
114 			    sizeof(struct nodeinfo)];
115 			struct ng_mesg res;
116 		} res;
117 		struct nodeinfo *const ni = (struct nodeinfo *) res.res.data;
118 
119 		/* Find out the node ID */
120 		if (NgSendMsg(cs, ".", NGM_GENERIC_COOKIE,
121 		    NGM_NODEINFO, NULL, 0) < 0) {
122 			errnosv = errno;
123 			if (_gNgDebugLevel >= 1)
124 				NGLOG("send nodeinfo");
125 			goto errout;
126 		}
127 		if (NgRecvMsg(cs, &res.res, sizeof(res.rbuf), NULL) < 0) {
128 			errnosv = errno;
129 			if (_gNgDebugLevel >= 1)
130 				NGLOG("recv nodeinfo");
131 			goto errout;
132 		}
133 
134 		/* Save node "name" */
135 		snprintf(namebuf, sizeof(namebuf), "[%lx]", (u_long) ni->id);
136 	}
137 
138 	/* Create data socket if desired */
139 	if (dsp != NULL) {
140 		u_char sbuf[NG_NODESIZ + 1 + NGSA_OVERHEAD];
141 		struct sockaddr_ng *const sg = (struct sockaddr_ng *) sbuf;
142 
143 		/* Create data socket, initially just "floating" */
144 		if ((ds = socket(AF_NETGRAPH, SOCK_DGRAM, NG_DATA)) < 0) {
145 			errnosv = errno;
146 			if (_gNgDebugLevel >= 1)
147 				NGLOG("socket");
148 			goto errout;
149 		}
150 
151 		/* Associate the data socket with the node */
152 		snprintf(sg->sg_data, NG_NODESIZ + 1, "%s:", namebuf);
153 		sg->sg_family = AF_NETGRAPH;
154 		sg->sg_len = strlen(sg->sg_data) + 1 + NGSA_OVERHEAD;
155 		if (connect(ds, (struct sockaddr *) sg, sg->sg_len) < 0) {
156 			errnosv = errno;
157 			if (_gNgDebugLevel >= 1)
158 				NGLOG("connect(%s)", sg->sg_data);
159 			goto errout;
160 		}
161 	}
162 
163 	/* Return the socket(s) */
164 	if (csp)
165 		*csp = cs;
166 	else
167 		close(cs);
168 	if (dsp)
169 		*dsp = ds;
170 	return (0);
171 
172 errout:
173 	/* Failed */
174 	if (cs >= 0)
175 		close(cs);
176 	if (ds >= 0)
177 		close(ds);
178 	errno = errnosv;
179 	return (-1);
180 }
181 
182 /*
183  * Assign a globally unique name to a node
184  * Returns -1 if error and sets errno.
185  */
186 int
187 NgNameNode(int cs, const char *path, const char *fmt, ...)
188 {
189 	struct ngm_name ngn;
190 	va_list args;
191 
192 	/* Build message arg */
193 	va_start(args, fmt);
194 	vsnprintf(ngn.name, sizeof(ngn.name), fmt, args);
195 	va_end(args);
196 
197 	/* Send message */
198 	if (NgSendMsg(cs, path,
199 	    NGM_GENERIC_COOKIE, NGM_NAME, &ngn, sizeof(ngn)) < 0) {
200 		if (_gNgDebugLevel >= 1)
201 			NGLOGX("%s: failed", __func__);
202 		return (-1);
203 	}
204 
205 	/* Done */
206 	return (0);
207 }
208 
209 /*
210  * Read a packet from a data socket
211  * Returns -1 if error and sets errno.
212  */
213 int
214 NgRecvData(int ds, u_char * buf, size_t len, char *hook)
215 {
216 	u_char frombuf[NG_HOOKSIZ + NGSA_OVERHEAD];
217 	struct sockaddr_ng *const from = (struct sockaddr_ng *) frombuf;
218 	socklen_t fromlen = sizeof(frombuf);
219 	int rtn, errnosv;
220 
221 	/* Read packet */
222 	rtn = recvfrom(ds, buf, len, 0, (struct sockaddr *) from, &fromlen);
223 	if (rtn < 0) {
224 		errnosv = errno;
225 		if (_gNgDebugLevel >= 1)
226 			NGLOG("recvfrom");
227 		errno = errnosv;
228 		return (-1);
229 	}
230 
231 	/* Copy hook name */
232 	if (hook != NULL)
233 		strlcpy(hook, from->sg_data, NG_HOOKSIZ);
234 
235 	/* Debugging */
236 	if (_gNgDebugLevel >= 2) {
237 		NGLOGX("READ %s from hook \"%s\" (%d bytes)",
238 		       rtn ? "PACKET" : "EOF", from->sg_data, rtn);
239 		if (_gNgDebugLevel >= 3)
240 			_NgDebugBytes(buf, rtn);
241 	}
242 
243 	/* Done */
244 	return (rtn);
245 }
246 
247 /*
248  * Identical to NgRecvData() except buffer is dynamically allocated.
249  */
250 int
251 NgAllocRecvData(int ds, u_char **buf, char *hook)
252 {
253 	int len;
254 	socklen_t optlen;
255 
256 	optlen = sizeof(len);
257 	if (getsockopt(ds, SOL_SOCKET, SO_RCVBUF, &len, &optlen) == -1 ||
258 	    (*buf = malloc(len)) == NULL)
259 		return (-1);
260 	if ((len = NgRecvData(ds, *buf, len, hook)) < 0)
261 		free(*buf);
262 	return (len);
263 }
264 
265 /*
266  * Write a packet to a data socket. The packet will be sent
267  * out the corresponding node on the specified hook.
268  * Returns -1 if error and sets errno.
269  */
270 int
271 NgSendData(int ds, const char *hook, const u_char * buf, size_t len)
272 {
273 	u_char sgbuf[NG_HOOKSIZ + NGSA_OVERHEAD];
274 	struct sockaddr_ng *const sg = (struct sockaddr_ng *) sgbuf;
275 	int errnosv;
276 
277 	/* Set up destination hook */
278 	sg->sg_family = AF_NETGRAPH;
279 	strlcpy(sg->sg_data, hook, NG_HOOKSIZ);
280 	sg->sg_len = strlen(sg->sg_data) + 1 + NGSA_OVERHEAD;
281 
282 	/* Debugging */
283 	if (_gNgDebugLevel >= 2) {
284 		NGLOGX("WRITE PACKET to hook \"%s\" (%d bytes)", hook, len);
285 		_NgDebugSockaddr(sg);
286 		if (_gNgDebugLevel >= 3)
287 			_NgDebugBytes(buf, len);
288 	}
289 
290 	/* Send packet */
291 	if (sendto(ds, buf, len, 0, (struct sockaddr *) sg, sg->sg_len) < 0) {
292 		errnosv = errno;
293 		if (_gNgDebugLevel >= 1)
294 			NGLOG("sendto(%s)", sg->sg_data);
295 		errno = errnosv;
296 		return (-1);
297 	}
298 
299 	/* Done */
300 	return (0);
301 }
302 
303