4 * Copyright (c) 1996-1999 Whistle Communications, Inc.
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.
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
36 * Author: Archie Cobbs <archie@whistle.com>
38 * $Whistle: sock.c,v 1.12 1999/01/20 00:57:23 archie Exp $
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
44 #include <sys/types.h>
45 #include <sys/socket.h>
47 #include <netgraph/ng_message.h>
48 #include <netgraph/ng_socket.h>
53 /* The socket node type KLD */
54 #define NG_SOCKET_KLD "ng_socket.ko"
57 * Create a socket type node and give it the supplied name.
58 * Return data and control sockets corresponding to the node.
59 * Returns -1 if error and sets errno.
62 NgMkSockNode(const char *name, int *csp, int *dsp)
64 char namebuf[NG_NODESIZ];
65 int cs = -1; /* control socket */
66 int ds = -1; /* data socket */
69 /* Empty name means no name */
70 if (name && *name == 0)
73 /* Create control socket; this also creates the netgraph node.
74 If we get an EAFNOSUPPORT then the socket node type is
75 not loaded, so load it and try again. */
76 if ((cs = socket(AF_NETGRAPH, SOCK_DGRAM, NG_CONTROL)) < 0) {
77 if (errno == EAFNOSUPPORT) {
78 if (kldload(NG_SOCKET_KLD) < 0) {
80 if (_gNgDebugLevel >= 1)
81 NGLOG("can't load %s", NG_SOCKET_KLD);
84 cs = socket(AF_NETGRAPH, SOCK_DGRAM, NG_CONTROL);
89 if (_gNgDebugLevel >= 1)
95 /* Assign the node the desired name, if any */
97 u_char sbuf[NG_NODESIZ + NGSA_OVERHEAD];
98 struct sockaddr_ng *const sg = (struct sockaddr_ng *) sbuf;
101 strlcpy(sg->sg_data, name, NG_NODESIZ);
102 sg->sg_family = AF_NETGRAPH;
103 sg->sg_len = strlen(sg->sg_data) + 1 + NGSA_OVERHEAD;
104 if (bind(cs, (struct sockaddr *) sg, sg->sg_len) < 0) {
106 if (_gNgDebugLevel >= 1)
107 NGLOG("bind(%s)", sg->sg_data);
112 strlcpy(namebuf, name, sizeof(namebuf));
113 } else if (dsp != NULL) {
114 u_char rbuf[sizeof(struct ng_mesg) + sizeof(struct nodeinfo)];
115 struct ng_mesg *const resp = (struct ng_mesg *) rbuf;
116 struct nodeinfo *const ni = (struct nodeinfo *) resp->data;
118 /* Find out the node ID */
119 if (NgSendMsg(cs, ".", NGM_GENERIC_COOKIE,
120 NGM_NODEINFO, NULL, 0) < 0) {
122 if (_gNgDebugLevel >= 1)
123 NGLOG("send nodeinfo");
126 if (NgRecvMsg(cs, resp, sizeof(rbuf), NULL) < 0) {
128 if (_gNgDebugLevel >= 1)
129 NGLOG("recv nodeinfo");
133 /* Save node "name" */
134 snprintf(namebuf, sizeof(namebuf), "[%lx]", (u_long) ni->id);
137 /* Create data socket if desired */
139 u_char sbuf[NG_NODESIZ + 1 + NGSA_OVERHEAD];
140 struct sockaddr_ng *const sg = (struct sockaddr_ng *) sbuf;
142 /* Create data socket, initially just "floating" */
143 if ((ds = socket(AF_NETGRAPH, SOCK_DGRAM, NG_DATA)) < 0) {
145 if (_gNgDebugLevel >= 1)
150 /* Associate the data socket with the node */
151 snprintf(sg->sg_data, NG_NODESIZ + 1, "%s:", namebuf);
152 sg->sg_family = AF_NETGRAPH;
153 sg->sg_len = strlen(sg->sg_data) + 1 + NGSA_OVERHEAD;
154 if (connect(ds, (struct sockaddr *) sg, sg->sg_len) < 0) {
156 if (_gNgDebugLevel >= 1)
157 NGLOG("connect(%s)", sg->sg_data);
162 /* Return the socket(s) */
182 * Assign a globally unique name to a node
183 * Returns -1 if error and sets errno.
186 NgNameNode(int cs, const char *path, const char *fmt, ...)
191 /* Build message arg */
193 vsnprintf(ngn.name, sizeof(ngn.name), fmt, args);
197 if (NgSendMsg(cs, path,
198 NGM_GENERIC_COOKIE, NGM_NAME, &ngn, sizeof(ngn)) < 0) {
199 if (_gNgDebugLevel >= 1)
200 NGLOGX("%s: failed", __func__);
209 * Read a packet from a data socket
210 * Returns -1 if error and sets errno.
213 NgRecvData(int ds, u_char * buf, size_t len, char *hook)
215 u_char frombuf[NG_HOOKSIZ + NGSA_OVERHEAD];
216 struct sockaddr_ng *const from = (struct sockaddr_ng *) frombuf;
217 socklen_t fromlen = sizeof(frombuf);
221 rtn = recvfrom(ds, buf, len, 0, (struct sockaddr *) from, &fromlen);
224 if (_gNgDebugLevel >= 1)
232 strlcpy(hook, from->sg_data, NG_HOOKSIZ);
235 if (_gNgDebugLevel >= 2) {
236 NGLOGX("READ %s from hook \"%s\" (%d bytes)",
237 rtn ? "PACKET" : "EOF", from->sg_data, rtn);
238 if (_gNgDebugLevel >= 3)
239 _NgDebugBytes(buf, rtn);
247 * Identical to NgRecvData() except buffer is dynamically allocated.
250 NgAllocRecvData(int ds, u_char **buf, char *hook)
255 optlen = sizeof(len);
256 if (getsockopt(ds, SOL_SOCKET, SO_RCVBUF, &len, &optlen) == -1 ||
257 (*buf = malloc(len)) == NULL)
259 if ((len = NgRecvData(ds, *buf, len, hook)) < 0)
265 * Write a packet to a data socket. The packet will be sent
266 * out the corresponding node on the specified hook.
267 * Returns -1 if error and sets errno.
270 NgSendData(int ds, const char *hook, const u_char * buf, size_t len)
272 u_char sgbuf[NG_HOOKSIZ + NGSA_OVERHEAD];
273 struct sockaddr_ng *const sg = (struct sockaddr_ng *) sgbuf;
276 /* Set up destination hook */
277 sg->sg_family = AF_NETGRAPH;
278 strlcpy(sg->sg_data, hook, NG_HOOKSIZ);
279 sg->sg_len = strlen(sg->sg_data) + 1 + NGSA_OVERHEAD;
282 if (_gNgDebugLevel >= 2) {
283 NGLOGX("WRITE PACKET to hook \"%s\" (%d bytes)", hook, len);
284 _NgDebugSockaddr(sg);
285 if (_gNgDebugLevel >= 3)
286 _NgDebugBytes(buf, len);
290 if (sendto(ds, buf, len, 0, (struct sockaddr *) sg, sg->sg_len) < 0) {
292 if (_gNgDebugLevel >= 1)
293 NGLOG("sendto(%s)", sg->sg_data);