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 * $Whistle: sock.c,v 1.12 1999/01/20 00:57:23 archie Exp $
43 #include <sys/types.h>
45 #include <netgraph/ng_message.h>
46 #include <netgraph/ng_socket.h>
51 /* The socket node type KLD */
52 #define NG_SOCKET_KLD "ng_socket.ko"
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.
60 NgMkSockNode(const char *name, int *csp, int *dsp)
62 char namebuf[NG_NODELEN + 1];
63 int cs = -1; /* control socket */
64 int ds = -1; /* data socket */
67 /* Empty name means no name */
68 if (name && *name == 0)
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) {
78 if (_gNgDebugLevel >= 1)
79 NGLOG("can't load %s", NG_SOCKET_KLD);
82 cs = socket(AF_NETGRAPH, SOCK_DGRAM, NG_CONTROL);
87 if (_gNgDebugLevel >= 1)
93 /* Assign the node the desired name, if any */
95 u_char sbuf[NG_NODELEN + 3];
96 struct sockaddr_ng *const sg = (struct sockaddr_ng *) sbuf;
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) {
104 if (_gNgDebugLevel >= 1)
105 NGLOG("bind(%s)", sg->sg_data);
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;
116 /* Find out the node ID */
117 if (NgSendMsg(cs, ".", NGM_GENERIC_COOKIE,
118 NGM_NODEINFO, NULL, 0) < 0) {
120 if (_gNgDebugLevel >= 1)
121 NGLOG("send nodeinfo");
124 if (NgRecvMsg(cs, resp, sizeof(rbuf), NULL) < 0) {
126 if (_gNgDebugLevel >= 1)
127 NGLOG("recv nodeinfo");
131 /* Save node "name" */
132 snprintf(namebuf, sizeof(namebuf), "[%lx]", (u_long) ni->id);
135 /* Create data socket if desired */
137 u_char sbuf[NG_NODELEN + 4];
138 struct sockaddr_ng *const sg = (struct sockaddr_ng *) sbuf;
140 /* Create data socket, initially just "floating" */
141 if ((ds = socket(AF_NETGRAPH, SOCK_DGRAM, NG_DATA)) < 0) {
143 if (_gNgDebugLevel >= 1)
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) {
154 if (_gNgDebugLevel >= 1)
155 NGLOG("connect(%s)", sg->sg_data);
160 /* Return the socket(s) */
180 * Assign a globally unique name to a node
181 * Returns -1 if error and sets errno.
184 NgNameNode(int cs, const char *path, const char *fmt, ...)
189 /* Build message arg */
191 vsnprintf(ngn.name, sizeof(ngn.name), fmt, args);
195 if (NgSendMsg(cs, path,
196 NGM_GENERIC_COOKIE, NGM_NAME, &ngn, sizeof(ngn)) < 0) {
197 if (_gNgDebugLevel >= 1)
198 NGLOGX("%s: failed", __FUNCTION__);
207 * Read a packet from a data socket
208 * Returns -1 if error and sets errno.
211 NgRecvData(int ds, u_char * buf, size_t len, char *hook)
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);
219 rtn = recvfrom(ds, buf, len, 0, (struct sockaddr *) from, &fromlen);
222 if (_gNgDebugLevel >= 1)
230 snprintf(hook, NG_HOOKLEN + 1, "%s", from->sg_data);
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);
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.
250 NgSendData(int ds, const char *hook, const u_char * buf, size_t len)
252 u_char sgbuf[NG_HOOKLEN + sizeof(struct sockaddr_ng)];
253 struct sockaddr_ng *const sg = (struct sockaddr_ng *) sgbuf;
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;
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);
270 if (sendto(ds, buf, len, 0, (struct sockaddr *) sg, sg->sg_len) < 0) {
272 if (_gNgDebugLevel >= 1)
273 NGLOG("sendto(%s)", sg->sg_data);