5 * Copyright (c) 1996-1999 Whistle Communications, Inc.
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.
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
37 * Author: Archie Cobbs <archie@whistle.com>
39 * $FreeBSD: src/lib/libnetgraph/sock.c,v 1.2 2000/01/28 00:48:27 archie Exp $
40 * $DragonFly: src/lib/libnetgraph/sock.c,v 1.4 2005/02/17 13:59:35 joerg Exp $
41 * $Whistle: sock.c,v 1.12 1999/01/20 00:57:23 archie Exp $
44 #include <sys/types.h>
46 #include <netgraph/ng_message.h>
47 #include <netgraph/socket/ng_socket.h>
52 /* The socket node type KLD */
53 #define NG_SOCKET_KLD "ng_socket.ko"
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.
61 NgMkSockNode(const char *name, int *csp, int *dsp)
63 char namebuf[NG_NODELEN + 1];
64 int cs = -1; /* control socket */
65 int ds = -1; /* data socket */
68 /* Empty name means no name */
69 if (name && *name == 0)
72 /* Create control socket; this also creates the netgraph node.
73 If we get a 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) {
79 if (_gNgDebugLevel >= 1)
80 NGLOG("can't load %s", NG_SOCKET_KLD);
83 cs = socket(AF_NETGRAPH, SOCK_DGRAM, NG_CONTROL);
88 if (_gNgDebugLevel >= 1)
94 /* Assign the node the desired name, if any */
96 u_char sbuf[NG_NODELEN + 3];
97 struct sockaddr_ng *const sg = (struct sockaddr_ng *) sbuf;
100 snprintf(sg->sg_data, NG_NODELEN + 1, "%s", name);
101 sg->sg_family = AF_NETGRAPH;
102 sg->sg_len = strlen(sg->sg_data) + 3;
103 if (bind(cs, (struct sockaddr *) sg, sg->sg_len) < 0) {
105 if (_gNgDebugLevel >= 1)
106 NGLOG("bind(%s)", sg->sg_data);
111 snprintf(namebuf, sizeof(namebuf), "%s", name);
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;
117 /* Find out the node ID */
118 if (NgSendMsg(cs, ".", NGM_GENERIC_COOKIE,
119 NGM_NODEINFO, NULL, 0) < 0) {
121 if (_gNgDebugLevel >= 1)
122 NGLOG("send nodeinfo");
125 if (NgRecvMsg(cs, resp, sizeof(rbuf), NULL) < 0) {
127 if (_gNgDebugLevel >= 1)
128 NGLOG("recv nodeinfo");
132 /* Save node "name" */
133 snprintf(namebuf, sizeof(namebuf), "[%lx]", (u_long) ni->id);
136 /* Create data socket if desired */
138 u_char sbuf[NG_NODELEN + 4];
139 struct sockaddr_ng *const sg = (struct sockaddr_ng *) sbuf;
141 /* Create data socket, initially just "floating" */
142 if ((ds = socket(AF_NETGRAPH, SOCK_DGRAM, NG_DATA)) < 0) {
144 if (_gNgDebugLevel >= 1)
149 /* Associate the data socket with the node */
150 snprintf(sg->sg_data, NG_NODELEN + 2, "%s:", namebuf);
151 sg->sg_family = AF_NETGRAPH;
152 sg->sg_len = strlen(sg->sg_data) + 3;
153 if (connect(ds, (struct sockaddr *) sg, sg->sg_len) < 0) {
155 if (_gNgDebugLevel >= 1)
156 NGLOG("connect(%s)", sg->sg_data);
161 /* Return the socket(s) */
181 * Assign a globally unique name to a node
182 * Returns -1 if error and sets errno.
185 NgNameNode(int cs, const char *path, const char *fmt, ...)
190 /* Build message arg */
192 vsnprintf(ngn.name, sizeof(ngn.name), fmt, args);
196 if (NgSendMsg(cs, path,
197 NGM_GENERIC_COOKIE, NGM_NAME, &ngn, sizeof(ngn)) < 0) {
198 if (_gNgDebugLevel >= 1)
199 NGLOGX("%s: failed", __func__);
208 * Read a packet from a data socket
209 * Returns -1 if error and sets errno.
212 NgRecvData(int ds, u_char * buf, size_t len, char *hook)
214 u_char frombuf[NG_HOOKLEN + sizeof(struct sockaddr_ng)];
215 struct sockaddr_ng *const from = (struct sockaddr_ng *) frombuf;
216 int fromlen = sizeof(frombuf);
220 rtn = recvfrom(ds, buf, len, 0, (struct sockaddr *) from, &fromlen);
223 if (_gNgDebugLevel >= 1)
231 snprintf(hook, NG_HOOKLEN + 1, "%s", from->sg_data);
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);
246 * Write a packet to a data socket. The packet will be sent
247 * out the corresponding node on the specified hook.
248 * Returns -1 if error and sets errno.
251 NgSendData(int ds, const char *hook, const u_char * buf, size_t len)
253 u_char sgbuf[NG_HOOKLEN + sizeof(struct sockaddr_ng)];
254 struct sockaddr_ng *const sg = (struct sockaddr_ng *) sgbuf;
257 /* Set up destination hook */
258 sg->sg_family = AF_NETGRAPH;
259 snprintf(sg->sg_data, NG_HOOKLEN + 1, "%s", hook);
260 sg->sg_len = strlen(sg->sg_data) + 3;
263 if (_gNgDebugLevel >= 2) {
264 NGLOGX("WRITE PACKET to hook \"%s\" (%d bytes)", hook, len);
265 _NgDebugSockaddr(sg);
266 if (_gNgDebugLevel >= 3)
267 _NgDebugBytes(buf, len);
271 if (sendto(ds, buf, len, 0, (struct sockaddr *) sg, sg->sg_len) < 0) {
273 if (_gNgDebugLevel >= 1)
274 NGLOG("sendto(%s)", sg->sg_data);