Add an indexing feature to the -t option.
[dragonfly.git] / lib / libnetgraph / sock.c
blob573aa1dbf5486f0fec855094635847594dda3f65
2 /*
3 * sock.c
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.
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.
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.5 2007/06/03 23:41:25 swildner Exp $
41 * $Whistle: sock.c,v 1.12 1999/01/20 00:57:23 archie Exp $
44 #include <sys/types.h>
45 #include <stdarg.h>
46 #include <netgraph/ng_message.h>
47 #include <netgraph/socket/ng_socket.h>
49 #include "netgraph.h"
50 #include "internal.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.
60 int
61 NgMkSockNode(const char *name, int *csp, int *dsp)
63 char namebuf[NG_NODESIZ];
64 int cs = -1; /* control socket */
65 int ds = -1; /* data socket */
66 int errnosv;
68 /* Empty name means no name */
69 if (name && *name == 0)
70 name = NULL;
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) {
78 errnosv = errno;
79 if (_gNgDebugLevel >= 1)
80 NGLOG("can't load %s", NG_SOCKET_KLD);
81 goto errout;
83 cs = socket(AF_NETGRAPH, SOCK_DGRAM, NG_CONTROL);
84 if (cs >= 0)
85 goto gotNode;
87 errnosv = errno;
88 if (_gNgDebugLevel >= 1)
89 NGLOG("socket");
90 goto errout;
93 gotNode:
94 /* Assign the node the desired name, if any */
95 if (name != NULL) {
96 u_char sbuf[NG_NODESIZ + 2];
97 struct sockaddr_ng *const sg = (struct sockaddr_ng *) sbuf;
99 /* Assign name */
100 snprintf(sg->sg_data, NG_NODESIZ, "%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) {
104 errnosv = errno;
105 if (_gNgDebugLevel >= 1)
106 NGLOG("bind(%s)", sg->sg_data);
107 goto errout;
110 /* Save node name */
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) {
120 errnosv = errno;
121 if (_gNgDebugLevel >= 1)
122 NGLOG("send nodeinfo");
123 goto errout;
125 if (NgRecvMsg(cs, resp, sizeof(rbuf), NULL) < 0) {
126 errnosv = errno;
127 if (_gNgDebugLevel >= 1)
128 NGLOG("recv nodeinfo");
129 goto errout;
132 /* Save node "name" */
133 snprintf(namebuf, sizeof(namebuf), "[%lx]", (u_long) ni->id);
136 /* Create data socket if desired */
137 if (dsp != NULL) {
138 u_char sbuf[NG_NODESIZ + 3];
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) {
143 errnosv = errno;
144 if (_gNgDebugLevel >= 1)
145 NGLOG("socket");
146 goto errout;
149 /* Associate the data socket with the node */
150 snprintf(sg->sg_data, NG_NODESIZ + 1, "%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) {
154 errnosv = errno;
155 if (_gNgDebugLevel >= 1)
156 NGLOG("connect(%s)", sg->sg_data);
157 goto errout;
161 /* Return the socket(s) */
162 if (csp)
163 *csp = cs;
164 else
165 close(cs);
166 if (dsp)
167 *dsp = ds;
168 return (0);
170 errout:
171 /* Failed */
172 if (cs >= 0)
173 close(cs);
174 if (ds >= 0)
175 close(ds);
176 errno = errnosv;
177 return (-1);
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, ...)
187 struct ngm_name ngn;
188 va_list args;
190 /* Build message arg */
191 va_start(args, fmt);
192 vsnprintf(ngn.name, sizeof(ngn.name), fmt, args);
193 va_end(args);
195 /* Send message */
196 if (NgSendMsg(cs, path,
197 NGM_GENERIC_COOKIE, NGM_NAME, &ngn, sizeof(ngn)) < 0) {
198 if (_gNgDebugLevel >= 1)
199 NGLOGX("%s: failed", __func__);
200 return (-1);
203 /* Done */
204 return (0);
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_HOOKSIZ - 1 + sizeof(struct sockaddr_ng)];
215 struct sockaddr_ng *const from = (struct sockaddr_ng *) frombuf;
216 int fromlen = sizeof(frombuf);
217 int rtn, errnosv;
219 /* Read packet */
220 rtn = recvfrom(ds, buf, len, 0, (struct sockaddr *) from, &fromlen);
221 if (rtn < 0) {
222 errnosv = errno;
223 if (_gNgDebugLevel >= 1)
224 NGLOG("recvfrom");
225 errno = errnosv;
226 return (-1);
229 /* Copy hook name */
230 if (hook != NULL)
231 snprintf(hook, NG_HOOKSIZ, "%s", from->sg_data);
233 /* Debugging */
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);
241 /* Done */
242 return (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_HOOKSIZ - 1 + sizeof(struct sockaddr_ng)];
254 struct sockaddr_ng *const sg = (struct sockaddr_ng *) sgbuf;
255 int errnosv;
257 /* Set up destination hook */
258 sg->sg_family = AF_NETGRAPH;
259 snprintf(sg->sg_data, NG_HOOKSIZ, "%s", hook);
260 sg->sg_len = strlen(sg->sg_data) + 3;
262 /* Debugging */
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);
270 /* Send packet */
271 if (sendto(ds, buf, len, 0, (struct sockaddr *) sg, sg->sg_len) < 0) {
272 errnosv = errno;
273 if (_gNgDebugLevel >= 1)
274 NGLOG("sendto(%s)", sg->sg_data);
275 errno = errnosv;
276 return (-1);
279 /* Done */
280 return (0);