summaryrefslogtreecommitdiffstats
path: root/lib/libnetgraph
diff options
context:
space:
mode:
Diffstat (limited to 'lib/libnetgraph')
-rw-r--r--lib/libnetgraph/Makefile28
-rw-r--r--lib/libnetgraph/debug.c346
-rw-r--r--lib/libnetgraph/internal.h74
-rw-r--r--lib/libnetgraph/msg.c378
-rw-r--r--lib/libnetgraph/netgraph.3398
-rw-r--r--lib/libnetgraph/netgraph.h69
-rw-r--r--lib/libnetgraph/sock.c300
7 files changed, 1593 insertions, 0 deletions
diff --git a/lib/libnetgraph/Makefile b/lib/libnetgraph/Makefile
new file mode 100644
index 0000000..42596e2
--- /dev/null
+++ b/lib/libnetgraph/Makefile
@@ -0,0 +1,28 @@
+# $FreeBSD$
+# $Whistle: Makefile,v 1.4 1999/01/17 03:41:02 julian Exp $
+
+LIB= netgraph
+WARNS?= 3
+MAN= netgraph.3
+
+SHLIB_MAJOR= 2
+
+SRCS= sock.c msg.c debug.c
+INCS= netgraph.h
+
+MLINKS+= netgraph.3 NgMkSockNode.3
+MLINKS+= netgraph.3 NgNameNode.3
+MLINKS+= netgraph.3 NgSendMsg.3
+MLINKS+= netgraph.3 NgSendAsciiMsg.3
+MLINKS+= netgraph.3 NgSendMsgReply.3
+MLINKS+= netgraph.3 NgRecvMsg.3
+MLINKS+= netgraph.3 NgAllocRecvMsg.3
+MLINKS+= netgraph.3 NgRecvAsciiMsg.3
+MLINKS+= netgraph.3 NgAllocRecvAsciiMsg.3
+MLINKS+= netgraph.3 NgSendData.3
+MLINKS+= netgraph.3 NgRecvData.3
+MLINKS+= netgraph.3 NgAllocRecvData.3
+MLINKS+= netgraph.3 NgSetDebug.3
+MLINKS+= netgraph.3 NgSetErrLog.3
+
+.include <bsd.lib.mk>
diff --git a/lib/libnetgraph/debug.c b/lib/libnetgraph/debug.c
new file mode 100644
index 0000000..dfc75f5
--- /dev/null
+++ b/lib/libnetgraph/debug.c
@@ -0,0 +1,346 @@
+/*
+ * debug.c
+ *
+ * Copyright (c) 1996-1999 Whistle Communications, Inc.
+ * All rights reserved.
+ *
+ * Subject to the following obligations and disclaimer of warranty, use and
+ * redistribution of this software, in source or object code forms, with or
+ * without modifications are expressly permitted by Whistle Communications;
+ * provided, however, that:
+ * 1. Any and all reproductions of the source or object code must include the
+ * copyright notice above and the following disclaimer of warranties; and
+ * 2. No rights are granted, in any manner or form, to use Whistle
+ * Communications, Inc. trademarks, including the mark "WHISTLE
+ * COMMUNICATIONS" on advertising, endorsements, or otherwise except as
+ * such appears in the above copyright notice or in the software.
+ *
+ * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
+ * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
+ * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
+ * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
+ * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
+ * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
+ * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
+ * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
+ * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
+ * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
+ * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
+ * OF SUCH DAMAGE.
+ *
+ * Author: Archie Cobbs <archie@whistle.com>
+ *
+ * $Whistle: debug.c,v 1.24 1999/01/24 01:15:33 archie Exp $
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/types.h>
+#include <sys/time.h>
+#include <sys/ioctl.h>
+
+#include <stdarg.h>
+
+#include <netinet/in.h>
+#include <net/ethernet.h>
+#include <net/bpf.h>
+
+#include <netgraph/ng_message.h>
+#include <netgraph/ng_socket.h>
+
+#include "netgraph.h"
+#include "internal.h"
+
+#include <netgraph/ng_UI.h>
+#include <netgraph/ng_async.h>
+#include <netgraph/ng_atmllc.h>
+#include <netgraph/ng_bpf.h>
+#include <netgraph/ng_bridge.h>
+#include <netgraph/ng_cisco.h>
+#include <netgraph/ng_device.h>
+#include <netgraph/ng_echo.h>
+#include <netgraph/ng_eiface.h>
+#include <netgraph/ng_etf.h>
+#include <netgraph/ng_ether.h>
+#include <netgraph/ng_fec.h>
+#include <netgraph/ng_frame_relay.h>
+#include <netgraph/ng_gif.h>
+#include <netgraph/ng_gif_demux.h>
+#include <netgraph/ng_hole.h>
+#include <netgraph/ng_hub.h>
+#include <netgraph/ng_iface.h>
+#include <netgraph/ng_ip_input.h>
+#include <netgraph/ng_ipfw.h>
+#include <netgraph/ng_ksocket.h>
+#include <netgraph/ng_l2tp.h>
+#include <netgraph/ng_lmi.h>
+#include <netgraph/ng_mppc.h>
+#include <netgraph/ng_nat.h>
+#include <netgraph/ng_one2many.h>
+#include <netgraph/ng_ppp.h>
+#include <netgraph/ng_pppoe.h>
+#include <netgraph/ng_pptpgre.h>
+#include <netgraph/ng_rfc1490.h>
+#include <netgraph/ng_socket.h>
+#include <netgraph/ng_source.h>
+#include <netgraph/ng_split.h>
+#include <netgraph/ng_sppp.h>
+#include <netgraph/ng_tcpmss.h>
+#include <netgraph/ng_tee.h>
+#include <netgraph/ng_tty.h>
+#include <netgraph/ng_vjc.h>
+#include <netgraph/ng_vlan.h>
+#ifdef WHISTLE
+#include <machine/../isa/df_def.h>
+#include <machine/../isa/if_wfra.h>
+#include <machine/../isa/ipac.h>
+#include <netgraph/ng_df.h>
+#include <netgraph/ng_ipac.h>
+#include <netgraph/ng_tn.h>
+#endif
+
+/* Global debug level */
+int _gNgDebugLevel = 0;
+
+/* Debug printing functions */
+void (*_NgLog) (const char *fmt,...) = warn;
+void (*_NgLogx) (const char *fmt,...) = warnx;
+
+/* Internal functions */
+static const char *NgCookie(int cookie);
+
+/* Known typecookie list */
+struct ng_cookie {
+ int cookie;
+ const char *type;
+};
+
+#define COOKIE(c) { NGM_ ## c ## _COOKIE, #c }
+
+/* List of known cookies */
+static const struct ng_cookie cookies[] = {
+ COOKIE(UI),
+ COOKIE(ASYNC),
+ COOKIE(ATMLLC),
+ COOKIE(BPF),
+ COOKIE(BRIDGE),
+ COOKIE(CISCO),
+ COOKIE(DEVICE),
+ COOKIE(ECHO),
+ COOKIE(EIFACE),
+ COOKIE(ETF),
+ COOKIE(ETHER),
+ COOKIE(FEC),
+ COOKIE(FRAMERELAY),
+ COOKIE(GIF),
+ COOKIE(GIF_DEMUX),
+ COOKIE(GENERIC),
+ COOKIE(HOLE),
+ COOKIE(HUB),
+ COOKIE(IFACE),
+ COOKIE(IP_INPUT),
+ COOKIE(IPFW),
+ COOKIE(KSOCKET),
+ COOKIE(L2TP),
+ COOKIE(LMI),
+ COOKIE(MPPC),
+ COOKIE(NAT),
+ COOKIE(ONE2MANY),
+ COOKIE(PPP),
+ COOKIE(PPPOE),
+ COOKIE(PPTPGRE),
+ COOKIE(RFC1490),
+ COOKIE(SOCKET),
+ COOKIE(SOURCE),
+ COOKIE(SPLIT),
+ COOKIE(SPPP),
+ COOKIE(TCPMSS),
+ COOKIE(TEE),
+ COOKIE(TTY),
+ COOKIE(VJC),
+ COOKIE(VLAN),
+#ifdef WHISTLE
+ COOKIE(DF),
+ COOKIE(IPAC),
+ COOKIE(TN),
+ COOKIE(WFRA),
+#endif
+ { 0, NULL }
+};
+
+/*
+ * Set debug level, ie, verbosity, if "level" is non-negative.
+ * Returns old debug level.
+ */
+int
+NgSetDebug(int level)
+{
+ int old = _gNgDebugLevel;
+
+ if (level < 0)
+ level = old;
+ _gNgDebugLevel = level;
+ return (old);
+}
+
+/*
+ * Set debug logging functions.
+ */
+void
+NgSetErrLog(void (*log) (const char *fmt,...),
+ void (*logx) (const char *fmt,...))
+{
+ _NgLog = log;
+ _NgLogx = logx;
+}
+
+/*
+ * Display a netgraph sockaddr
+ */
+void
+_NgDebugSockaddr(const struct sockaddr_ng *sg)
+{
+ NGLOGX("SOCKADDR: { fam=%d len=%d addr=\"%s\" }",
+ sg->sg_family, sg->sg_len, sg->sg_data);
+}
+
+#define ARGS_BUFSIZE 2048
+#define RECURSIVE_DEBUG_ADJUST 4
+
+/*
+ * Display a negraph message
+ */
+void
+_NgDebugMsg(const struct ng_mesg *msg, const char *path)
+{
+ u_char buf[2 * sizeof(struct ng_mesg) + ARGS_BUFSIZE];
+ struct ng_mesg *const req = (struct ng_mesg *)buf;
+ struct ng_mesg *const bin = (struct ng_mesg *)req->data;
+ int arglen, csock = -1;
+
+ /* Display header stuff */
+ NGLOGX("NG_MESG :");
+ NGLOGX(" vers %d", msg->header.version);
+ NGLOGX(" arglen %d", msg->header.arglen);
+ NGLOGX(" flags %ld", msg->header.flags);
+ NGLOGX(" token %lu", (u_long)msg->header.token);
+ NGLOGX(" cookie %s (%d)",
+ NgCookie(msg->header.typecookie), msg->header.typecookie);
+
+ /* At lower debugging levels, skip ASCII translation */
+ if (_gNgDebugLevel <= 2)
+ goto fail2;
+
+ /* If path is not absolute, don't bother trying to use relative
+ address on a different socket for the ASCII translation */
+ if (strchr(path, ':') == NULL)
+ goto fail2;
+
+ /* Get a temporary socket */
+ if (NgMkSockNode(NULL, &csock, NULL) < 0)
+ goto fail;
+
+ /* Copy binary message into request message payload */
+ arglen = msg->header.arglen;
+ if (arglen > ARGS_BUFSIZE)
+ arglen = ARGS_BUFSIZE;
+ memcpy(bin, msg, sizeof(*msg) + arglen);
+ bin->header.arglen = arglen;
+
+ /* Lower debugging to avoid infinite recursion */
+ _gNgDebugLevel -= RECURSIVE_DEBUG_ADJUST;
+
+ /* Ask the node to translate the binary message to ASCII for us */
+ if (NgSendMsg(csock, path, NGM_GENERIC_COOKIE,
+ NGM_BINARY2ASCII, bin, sizeof(*bin) + bin->header.arglen) < 0) {
+ _gNgDebugLevel += RECURSIVE_DEBUG_ADJUST;
+ goto fail;
+ }
+ if (NgRecvMsg(csock, req, sizeof(buf), NULL) < 0) {
+ _gNgDebugLevel += RECURSIVE_DEBUG_ADJUST;
+ goto fail;
+ }
+
+ /* Restore debugging level */
+ _gNgDebugLevel += RECURSIVE_DEBUG_ADJUST;
+
+ /* Display command string and arguments */
+ NGLOGX(" cmd %s (%d)", bin->header.cmdstr, bin->header.cmd);
+ NGLOGX(" args %s", bin->data);
+ goto done;
+
+fail:
+ /* Just display binary version */
+ NGLOGX(" [error decoding message: %s]", strerror(errno));
+fail2:
+ NGLOGX(" cmd %d", msg->header.cmd);
+ NGLOGX(" args (%d bytes)", msg->header.arglen);
+ _NgDebugBytes((u_char *)msg->data, msg->header.arglen);
+
+done:
+ if (csock != -1)
+ (void)close(csock);
+}
+
+/*
+ * Return the name of the node type corresponding to the cookie
+ */
+static const char *
+NgCookie(int cookie)
+{
+ int k;
+
+ for (k = 0; cookies[k].cookie != 0; k++) {
+ if (cookies[k].cookie == cookie)
+ return cookies[k].type;
+ }
+ return "??";
+}
+
+/*
+ * Dump bytes in hex
+ */
+void
+_NgDebugBytes(const u_char *ptr, int len)
+{
+ char buf[100];
+ int k, count;
+
+#define BYPERLINE 16
+
+ for (count = 0; count < len; ptr += BYPERLINE, count += BYPERLINE) {
+
+ /* Do hex */
+ snprintf(buf, sizeof(buf), "%04x: ", count);
+ for (k = 0; k < BYPERLINE; k++, count++)
+ if (count < len)
+ snprintf(buf + strlen(buf),
+ sizeof(buf) - strlen(buf), "%02x ", ptr[k]);
+ else
+ snprintf(buf + strlen(buf),
+ sizeof(buf) - strlen(buf), " ");
+ snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " ");
+ count -= BYPERLINE;
+
+ /* Do ASCII */
+ for (k = 0; k < BYPERLINE; k++, count++)
+ if (count < len)
+ snprintf(buf + strlen(buf),
+ sizeof(buf) - strlen(buf),
+ "%c", isprint(ptr[k]) ? ptr[k] : '.');
+ else
+ snprintf(buf + strlen(buf),
+ sizeof(buf) - strlen(buf), " ");
+ count -= BYPERLINE;
+
+ /* Print it */
+ NGLOGX("%s", buf);
+ }
+}
+
diff --git a/lib/libnetgraph/internal.h b/lib/libnetgraph/internal.h
new file mode 100644
index 0000000..0c947e5
--- /dev/null
+++ b/lib/libnetgraph/internal.h
@@ -0,0 +1,74 @@
+
+/*
+ * internal.h
+ *
+ * Copyright (c) 1996-1999 Whistle Communications, Inc.
+ * All rights reserved.
+ *
+ * Subject to the following obligations and disclaimer of warranty, use and
+ * redistribution of this software, in source or object code forms, with or
+ * without modifications are expressly permitted by Whistle Communications;
+ * provided, however, that:
+ * 1. Any and all reproductions of the source or object code must include the
+ * copyright notice above and the following disclaimer of warranties; and
+ * 2. No rights are granted, in any manner or form, to use Whistle
+ * Communications, Inc. trademarks, including the mark "WHISTLE
+ * COMMUNICATIONS" on advertising, endorsements, or otherwise except as
+ * such appears in the above copyright notice or in the software.
+ *
+ * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
+ * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
+ * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
+ * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
+ * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
+ * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
+ * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
+ * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
+ * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
+ * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
+ * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
+ * OF SUCH DAMAGE.
+ *
+ * Author: Archie Cobbs <archie@whistle.com>
+ *
+ * $FreeBSD$
+ * $Whistle: internal.h,v 1.5 1999/01/20 00:57:22 archie Exp $
+ */
+
+#include <sys/types.h>
+#include <sys/param.h>
+#include <sys/time.h>
+#include <sys/socket.h>
+#include <sys/select.h>
+#include <sys/linker.h>
+#include <stddef.h>
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include <errno.h>
+#include <ctype.h>
+#include <err.h>
+
+/* the 'sockaddr overhead' for a netgraph address. This is everything before
+ * the string that constitutes the address. */
+#define NGSA_OVERHEAD (offsetof(struct sockaddr_ng, sg_data))
+
+extern int _gNgDebugLevel;
+
+extern void (*_NgLog)(const char *fmt, ...);
+extern void (*_NgLogx)(const char *fmt, ...);
+
+#define NGLOG (*_NgLog)
+#define NGLOGX (*_NgLogx)
+
+extern void _NgDebugSockaddr(const struct sockaddr_ng *sg);
+extern void _NgDebugMsg(const struct ng_mesg *msg, const char *path);
+extern void _NgDebugBytes(const u_char *ptr, int size);
+
diff --git a/lib/libnetgraph/msg.c b/lib/libnetgraph/msg.c
new file mode 100644
index 0000000..8b4ca06
--- /dev/null
+++ b/lib/libnetgraph/msg.c
@@ -0,0 +1,378 @@
+/*
+ * msg.c
+ *
+ * Copyright (c) 1996-1999 Whistle Communications, Inc.
+ * All rights reserved.
+ *
+ * Subject to the following obligations and disclaimer of warranty, use and
+ * redistribution of this software, in source or object code forms, with or
+ * without modifications are expressly permitted by Whistle Communications;
+ * provided, however, that:
+ * 1. Any and all reproductions of the source or object code must include the
+ * copyright notice above and the following disclaimer of warranties; and
+ * 2. No rights are granted, in any manner or form, to use Whistle
+ * Communications, Inc. trademarks, including the mark "WHISTLE
+ * COMMUNICATIONS" on advertising, endorsements, or otherwise except as
+ * such appears in the above copyright notice or in the software.
+ *
+ * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
+ * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
+ * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
+ * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
+ * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
+ * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
+ * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
+ * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
+ * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
+ * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
+ * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
+ * OF SUCH DAMAGE.
+ *
+ * Author: Archie Cobbs <archie@whistle.com>
+ *
+ * $Whistle: msg.c,v 1.9 1999/01/20 00:57:23 archie Exp $
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/types.h>
+#include <stdarg.h>
+#include <netgraph/ng_message.h>
+#include <netgraph/ng_socket.h>
+
+#include "netgraph.h"
+#include "internal.h"
+
+/* Next message token value */
+static int gMsgId;
+
+/* For delivering both messages and replies */
+static int NgDeliverMsg(int cs, const char *path,
+ const struct ng_mesg *hdr, const void *args, size_t arglen);
+
+/*
+ * Send a message to a node using control socket node "cs".
+ * Returns -1 if error and sets errno appropriately.
+ * If successful, returns the message ID (token) used.
+ */
+int
+NgSendMsg(int cs, const char *path,
+ int cookie, int cmd, const void *args, size_t arglen)
+{
+ struct ng_mesg msg;
+
+ /* Prepare message header */
+ memset(&msg, 0, sizeof(msg));
+ msg.header.version = NG_VERSION;
+ msg.header.typecookie = cookie;
+ if (++gMsgId < 0)
+ gMsgId = 1;
+ msg.header.token = gMsgId;
+ msg.header.flags = NGF_ORIG;
+ msg.header.cmd = cmd;
+ snprintf((char *)msg.header.cmdstr, NG_CMDSTRSIZ, "cmd%d", cmd);
+
+ /* Deliver message */
+ if (NgDeliverMsg(cs, path, &msg, args, arglen) < 0)
+ return (-1);
+ return (msg.header.token);
+}
+
+/*
+ * Send a message given in ASCII format. We first ask the node to translate
+ * the command into binary, and then we send the binary.
+ */
+int
+NgSendAsciiMsg(int cs, const char *path, const char *fmt, ...)
+{
+ struct ng_mesg *reply, *binary, *ascii;
+ char *buf, *cmd, *args;
+ va_list fmtargs;
+ int token;
+
+ /* Parse out command and arguments */
+ va_start(fmtargs, fmt);
+ vasprintf(&buf, fmt, fmtargs);
+ va_end(fmtargs);
+ if (buf == NULL)
+ return (-1);
+
+ /* Parse out command, arguments */
+ for (cmd = buf; isspace(*cmd); cmd++)
+ ;
+ for (args = cmd; *args != '\0' && !isspace(*args); args++)
+ ;
+ if (*args != '\0') {
+ while (isspace(*args))
+ *args++ = '\0';
+ }
+
+ /* Get a bigger buffer to hold inner message header plus arg string */
+ if ((ascii = malloc(sizeof(struct ng_mesg)
+ + strlen(args) + 1)) == NULL) {
+ free(buf);
+ return (-1);
+ }
+ memset(ascii, 0, sizeof(*ascii));
+
+ /* Build inner header (only need cmdstr, arglen, and data fields) */
+ strncpy((char *)ascii->header.cmdstr, cmd,
+ sizeof(ascii->header.cmdstr) - 1);
+ strcpy(ascii->data, args);
+ ascii->header.arglen = strlen(ascii->data) + 1;
+ free(buf);
+
+ /* Send node a request to convert ASCII to binary */
+ if (NgSendMsg(cs, path, NGM_GENERIC_COOKIE, NGM_ASCII2BINARY,
+ (u_char *)ascii, sizeof(*ascii) + ascii->header.arglen) < 0) {
+ free(ascii);
+ return (-1);
+ }
+ free(ascii);
+
+ /* Get reply */
+ if (NgAllocRecvMsg(cs, &reply, NULL) < 0)
+ return (-1);
+
+ /* Now send binary version */
+ binary = (struct ng_mesg *)reply->data;
+ if (++gMsgId < 0)
+ gMsgId = 1;
+ binary->header.token = gMsgId;
+ binary->header.version = NG_VERSION;
+ if (NgDeliverMsg(cs,
+ path, binary, binary->data, binary->header.arglen) < 0) {
+ free(reply);
+ return (-1);
+ }
+ token = binary->header.token;
+ free(reply);
+ return (token);
+}
+
+/*
+ * Send a message that is a reply to a previously received message.
+ * Returns -1 and sets errno on error, otherwise returns zero.
+ */
+int
+NgSendReplyMsg(int cs, const char *path,
+ const struct ng_mesg *msg, const void *args, size_t arglen)
+{
+ struct ng_mesg rep;
+
+ /* Prepare message header */
+ rep = *msg;
+ rep.header.flags = NGF_RESP;
+
+ /* Deliver message */
+ return (NgDeliverMsg(cs, path, &rep, args, arglen));
+}
+
+/*
+ * Send a message to a node using control socket node "cs".
+ * Returns -1 if error and sets errno appropriately, otherwise zero.
+ */
+static int
+NgDeliverMsg(int cs, const char *path,
+ const struct ng_mesg *hdr, const void *args, size_t arglen)
+{
+ u_char sgbuf[NG_PATHSIZ + NGSA_OVERHEAD];
+ struct sockaddr_ng *const sg = (struct sockaddr_ng *) sgbuf;
+ u_char *buf = NULL;
+ struct ng_mesg *msg;
+ int errnosv = 0;
+ int rtn = 0;
+
+ /* Sanity check */
+ if (args == NULL)
+ arglen = 0;
+
+ /* Get buffer */
+ if ((buf = malloc(sizeof(*msg) + arglen)) == NULL) {
+ errnosv = errno;
+ if (_gNgDebugLevel >= 1)
+ NGLOG("malloc");
+ rtn = -1;
+ goto done;
+ }
+ msg = (struct ng_mesg *) buf;
+
+ /* Finalize message */
+ *msg = *hdr;
+ msg->header.arglen = arglen;
+ memcpy(msg->data, args, arglen);
+
+ /* Prepare socket address */
+ sg->sg_family = AF_NETGRAPH;
+ /* XXX handle overflow */
+ strlcpy(sg->sg_data, path, NG_PATHSIZ);
+ sg->sg_len = strlen(sg->sg_data) + 1 + NGSA_OVERHEAD;
+
+ /* Debugging */
+ if (_gNgDebugLevel >= 2) {
+ NGLOGX("SENDING %s:",
+ (msg->header.flags & NGF_RESP) ? "RESPONSE" : "MESSAGE");
+ _NgDebugSockaddr(sg);
+ _NgDebugMsg(msg, sg->sg_data);
+ }
+
+ /* Send it */
+ if (sendto(cs, msg, sizeof(*msg) + arglen,
+ 0, (struct sockaddr *) sg, sg->sg_len) < 0) {
+ errnosv = errno;
+ if (_gNgDebugLevel >= 1)
+ NGLOG("sendto(%s)", sg->sg_data);
+ rtn = -1;
+ goto done;
+ }
+
+ /* Wait for reply if there should be one. */
+ if (msg->header.cmd & NGM_HASREPLY) {
+ fd_set rfds;
+ int n;
+
+ FD_ZERO(&rfds);
+ FD_SET(cs, &rfds);
+ n = select(cs + 1, &rfds, NULL, NULL, NULL);
+ if (n == -1) {
+ errnosv = errno;
+ if (_gNgDebugLevel >= 1)
+ NGLOG("select");
+ rtn = -1;
+ }
+ }
+
+done:
+ /* Done */
+ free(buf); /* OK if buf is NULL */
+ errno = errnosv;
+ return (rtn);
+}
+
+/*
+ * Receive a control message.
+ *
+ * On error, this returns -1 and sets errno.
+ * Otherwise, it returns the length of the received reply.
+ */
+int
+NgRecvMsg(int cs, struct ng_mesg *rep, size_t replen, char *path)
+{
+ u_char sgbuf[NG_PATHSIZ + NGSA_OVERHEAD];
+ struct sockaddr_ng *const sg = (struct sockaddr_ng *) sgbuf;
+ socklen_t sglen = sizeof(sgbuf);
+ int len, errnosv;
+
+ /* Read reply */
+ len = recvfrom(cs, rep, replen, 0, (struct sockaddr *) sg, &sglen);
+ if (len < 0) {
+ errnosv = errno;
+ if (_gNgDebugLevel >= 1)
+ NGLOG("recvfrom");
+ goto errout;
+ }
+ if (path != NULL)
+ strlcpy(path, sg->sg_data, NG_PATHSIZ);
+
+ /* Debugging */
+ if (_gNgDebugLevel >= 2) {
+ NGLOGX("RECEIVED %s:",
+ (rep->header.flags & NGF_RESP) ? "RESPONSE" : "MESSAGE");
+ _NgDebugSockaddr(sg);
+ _NgDebugMsg(rep, sg->sg_data);
+ }
+
+ /* Done */
+ return (len);
+
+errout:
+ errno = errnosv;
+ return (-1);
+}
+
+/*
+ * Identical to NgRecvMsg() except buffer is dynamically allocated.
+ */
+int
+NgAllocRecvMsg(int cs, struct ng_mesg **rep, char *path)
+{
+ int len;
+ socklen_t optlen;
+
+ optlen = sizeof(len);
+ if (getsockopt(cs, SOL_SOCKET, SO_RCVBUF, &len, &optlen) == -1 ||
+ (*rep = malloc(len)) == NULL)
+ return (-1);
+ if ((len = NgRecvMsg(cs, *rep, len, path)) < 0)
+ free(*rep);
+ return (len);
+}
+
+/*
+ * Receive a control message and convert the arguments to ASCII
+ */
+int
+NgRecvAsciiMsg(int cs, struct ng_mesg *reply, size_t replen, char *path)
+{
+ struct ng_mesg *msg, *ascii;
+ int bufSize, errnosv;
+ u_char *buf;
+
+ /* Allocate buffer */
+ bufSize = 2 * sizeof(*reply) + replen;
+ if ((buf = malloc(bufSize)) == NULL)
+ return (-1);
+ msg = (struct ng_mesg *)buf;
+ ascii = (struct ng_mesg *)msg->data;
+
+ /* Get binary message */
+ if (NgRecvMsg(cs, msg, bufSize, path) < 0)
+ goto fail;
+ memcpy(reply, msg, sizeof(*msg));
+
+ /* Ask originating node to convert the arguments to ASCII */
+ if (NgSendMsg(cs, path, NGM_GENERIC_COOKIE,
+ NGM_BINARY2ASCII, msg, sizeof(*msg) + msg->header.arglen) < 0)
+ goto fail;
+ if (NgRecvMsg(cs, msg, bufSize, NULL) < 0)
+ goto fail;
+
+ /* Copy result to client buffer */
+ if (sizeof(*ascii) + ascii->header.arglen > replen) {
+ errno = ERANGE;
+fail:
+ errnosv = errno;
+ free(buf);
+ errno = errnosv;
+ return (-1);
+ }
+ strncpy(reply->data, ascii->data, ascii->header.arglen);
+
+ /* Done */
+ free(buf);
+ return (0);
+}
+
+/*
+ * Identical to NgRecvAsciiMsg() except buffer is dynamically allocated.
+ */
+int
+NgAllocRecvAsciiMsg(int cs, struct ng_mesg **reply, char *path)
+{
+ int len;
+ socklen_t optlen;
+
+ optlen = sizeof(len);
+ if (getsockopt(cs, SOL_SOCKET, SO_RCVBUF, &len, &optlen) == -1 ||
+ (*reply = malloc(len)) == NULL)
+ return (-1);
+ if ((len = NgRecvAsciiMsg(cs, *reply, len, path)) < 0)
+ free(*reply);
+ return (len);
+}
diff --git a/lib/libnetgraph/netgraph.3 b/lib/libnetgraph/netgraph.3
new file mode 100644
index 0000000..d2ef8bd
--- /dev/null
+++ b/lib/libnetgraph/netgraph.3
@@ -0,0 +1,398 @@
+.\" Copyright (c) 1996-1999 Whistle Communications, Inc.
+.\" All rights reserved.
+.\"
+.\" Subject to the following obligations and disclaimer of warranty, use and
+.\" redistribution of this software, in source or object code forms, with or
+.\" without modifications are expressly permitted by Whistle Communications;
+.\" provided, however, that:
+.\" 1. Any and all reproductions of the source or object code must include the
+.\" copyright notice above and the following disclaimer of warranties; and
+.\" 2. No rights are granted, in any manner or form, to use Whistle
+.\" Communications, Inc. trademarks, including the mark "WHISTLE
+.\" COMMUNICATIONS" on advertising, endorsements, or otherwise except as
+.\" such appears in the above copyright notice or in the software.
+.\"
+.\" THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
+.\" TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
+.\" REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
+.\" INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
+.\" MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
+.\" WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
+.\" REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
+.\" SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
+.\" IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
+.\" RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
+.\" WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
+.\" PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
+.\" SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
+.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+.\" THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
+.\" OF SUCH DAMAGE.
+.\"
+.\" Author: Archie Cobbs <archie@whistle.com>
+.\"
+.\" $FreeBSD$
+.\" $Whistle: netgraph.3,v 1.7 1999/01/25 07:14:06 archie Exp $
+.\"
+.Dd January 27, 2004
+.Dt NETGRAPH 3
+.Os
+.Sh NAME
+.Nm NgMkSockNode ,
+.Nm NgNameNode ,
+.Nm NgSendMsg ,
+.Nm NgSendAsciiMsg ,
+.Nm NgSendMsgReply ,
+.Nm NgRecvMsg ,
+.Nm NgAllocRecvMsg ,
+.Nm NgRecvAsciiMsg ,
+.Nm NgAllocRecvAsciiMsg ,
+.Nm NgSendData ,
+.Nm NgRecvData ,
+.Nm NgAllocRecvData ,
+.Nm NgSetDebug ,
+.Nm NgSetErrLog
+.Nd netgraph user library
+.Sh LIBRARY
+.Lb libnetgraph
+.Sh SYNOPSIS
+.In netgraph.h
+.Ft int
+.Fn NgMkSockNode "const char *name" "int *csp" "int *dsp"
+.Ft int
+.Fn NgNameNode "int cs" "const char *path" "const char *fmt" ...
+.Ft int
+.Fo NgSendMsg
+.Fa "int cs" "const char *path" "int cookie" "int cmd" "const void *arg"
+.Fa "size_t arglen"
+.Fc
+.Ft int
+.Fn NgSendAsciiMsg "int cs" "const char *path" "const char *fmt" ...
+.Ft int
+.Fo NgSendMsgReply
+.Fa "int cs" "const char *path" "struct ng_mesg *msg" "const void *arg"
+.Fa "size_t arglen"
+.Fc
+.Ft int
+.Fn NgRecvMsg "int cs" "struct ng_mesg *rep" "size_t replen" "char *path"
+.Ft int
+.Fn NgAllocRecvMsg "int cs" "struct ng_mesg **rep" "char *path"
+.Ft int
+.Fn NgRecvAsciiMsg "int cs" "struct ng_mesg *rep" "size_t replen" "char *path"
+.Ft int
+.Fn NgAllocRecvAsciiMsg "int cs" "struct ng_mesg **rep" "char *path"
+.Ft int
+.Fn NgSendData "int ds" "const char *hook" "const u_char *buf" "size_t len"
+.Ft int
+.Fn NgRecvData "int ds" "u_char *buf" "size_t len" "char *hook"
+.Ft int
+.Fn NgAllocRecvData "int ds" "u_char **buf" "char *hook"
+.Ft int
+.Fn NgSetDebug "int level"
+.Ft void
+.Fo NgSetErrLog
+.Fa "void \*[lp]*log\*[rp]\*[lp]const char *fmt, ...\*[rp]"
+.Fa "void \*[lp]*logx\*[rp]\*[lp]const char *fmt, ...\*[rp]"
+.Fc
+.Sh DESCRIPTION
+These functions facilitate user-mode program participation in the kernel
+.Xr netgraph 4
+graph-based networking system, by utilizing the netgraph
+.Vt socket
+node type (see
+.Xr ng_socket 4 ) .
+.Pp
+The
+.Fn NgMkSockNode
+function should be called first, to create a new
+.Vt socket
+type netgraph node with associated control and data sockets.
+If
+.Fa name
+is
+.No non- Ns Dv NULL ,
+the node will have that global name assigned to it.
+The
+.Fa csp
+and
+.Fa dsp
+arguments will be set to the newly opened control and data sockets
+associated with the node; either
+.Fa csp
+or
+.Fa dsp
+may be
+.Dv NULL
+if only one socket is desired.
+The
+.Fn NgMkSockNode
+function loads the
+.Vt socket
+node type KLD if it is not already loaded.
+.Pp
+The
+.Fn NgNameNode
+function assigns a global name to the node addressed by
+.Fa path .
+.Pp
+The
+.Fn NgSendMsg
+function sends a binary control message from the
+.Vt socket
+node associated with control socket
+.Fa cs
+to the node addressed by
+.Fa path .
+The
+.Fa cookie
+indicates how to interpret
+.Fa cmd ,
+which indicates a specific command.
+Extra argument data (if any) is specified by
+.Fa arg
+and
+.Fa arglen .
+The
+.Fa cookie , cmd ,
+and argument data are defined by the header file corresponding
+to the type of the node being addressed.
+The unique, non-negative token value chosen for use in the message
+header is returned.
+This value is typically used to associate replies.
+.Pp
+Use
+.Fn NgSendMsgReply
+to send reply to a previously received control message.
+The original message header should be pointed to by
+.Fa msg .
+.Pp
+The
+.Fn NgSendAsciiMsg
+function performs the same function as
+.Fn NgSendMsg ,
+but adds support for
+.Tn ASCII
+encoding of control messages.
+The
+.Fn NgSendAsciiMsg
+function formats its input a la
+.Xr printf 3
+and then sends the resulting
+.Tn ASCII
+string to the node in a
+.Dv NGM_ASCII2BINARY
+control message.
+The node returns a binary version of the
+message, which is then sent back to the node just as with
+.Fn NgSendMsg .
+As with
+.Fn NgSendMsg ,
+the message token value is returned.
+Note that
+.Tn ASCII
+conversion may not be supported by all node types.
+.Pp
+The
+.Fn NgRecvMsg
+function reads the next control message received by the node associated with
+control socket
+.Fa cs .
+The message and any extra argument data must fit in
+.Fa replen
+bytes.
+If
+.Fa path
+is
+.No non- Ns Dv NULL ,
+it must point to a buffer of at least
+.Dv NG_PATHSIZ
+bytes, which will be filled in (and
+.Dv NUL
+terminated) with the path to
+the node from which the message was received.
+.Pp
+The length of the control message is returned.
+A return value of zero indicates that the socket was closed.
+.Pp
+The
+.Fn NgAllocRecvMsg
+function works exactly like
+.Fn NgRecvMsg ,
+except that the buffer for a message is dynamically allocated
+to guarantee that a message is not truncated.
+The size of the buffer is equal to the socket's receive buffer size.
+The caller is responsible for freeing the buffer when it is no longer required.
+.Pp
+The
+.Fn NgRecvAsciiMsg
+function works exactly like
+.Fn NgRecvMsg ,
+except that after the message is received, any binary arguments
+are converted to
+.Tn ASCII
+by sending a
+.Dv NGM_BINARY2ASCII
+request back to the originating node.
+The result is the same as
+.Fn NgRecvMsg ,
+with the exception that the reply arguments field will contain a
+.Dv NUL Ns -terminated
+.Tn ASCII
+version of the arguments (and the reply
+header argument length field will be adjusted).
+.Pp
+The
+.Fn NgAllocRecvAsciiMsg
+function works exactly like
+.Fn NgRecvAsciiMsg ,
+except that the buffer for a message is dynamically allocated
+to guarantee that a message is not truncated.
+The size of the buffer is equal to the socket's receive buffer size.
+The caller is responsible for freeing the buffer when it is no longer required.
+.Pp
+The
+.Fn NgSendData
+function writes a data packet out on the specified hook of the node
+corresponding to data socket
+.Fa ds .
+The node must already be connected to some other node via that hook.
+.Pp
+The
+.Fn NgRecvData
+function reads the next data packet (of up to
+.Fa len
+bytes) received by the node corresponding to data socket
+.Fa ds
+and stores it in
+.Fa buf ,
+which must be large enough to hold the entire packet.
+If
+.Fa hook
+is
+.No non- Ns Dv NULL ,
+it must point to a buffer of at least
+.Dv NG_HOOKSIZ
+bytes, which will be filled in (and
+.Dv NUL
+terminated) with the name of
+the hook on which the data was received.
+.Pp
+The length of the packet is returned.
+A return value of zero indicates that the socket was closed.
+.Pp
+The
+.Fn NgAllocRecvData
+function works exactly like
+.Fn NgRecvData ,
+except that the buffer for a data packet is dynamically allocated
+to guarantee that a data packet is not truncated.
+The size of the buffer is equal to the socket's receive buffer size.
+The caller is responsible for freeing the buffer when it is no longer required.
+.Pp
+The
+.Fn NgSetDebug
+and
+.Fn NgSetErrLog
+functions are used for debugging.
+The
+.Fn NgSetDebug
+function sets the debug level (if non-negative), and returns the old setting.
+Higher debug levels result in more verbosity.
+The default is zero.
+All debug and error messages are logged via the functions
+specified in the most recent call to
+.Fn NgSetErrLog .
+The default logging functions are
+.Xr vwarn 3
+and
+.Xr vwarnx 3 .
+.Pp
+At debug level 3, the library attempts to display control message arguments
+in
+.Tn ASCII
+format; however, this results in additional messages being
+sent which may interfere with debugging.
+At even higher levels,
+even these additional messages will be displayed, etc.
+.Pp
+Note that
+.Xr select 2
+can be used on the data and the control sockets to detect the presence of
+incoming data and control messages, respectively.
+Data and control packets are always written and read atomically, i.e.,
+in one whole piece.
+.Pp
+User mode programs must be linked with the
+.Fl l Ns Li netgraph
+flag to link in this library.
+.Sh INITIALIZATION
+To enable netgraph in your kernel, either your kernel must be
+compiled with
+.Cd "options NETGRAPH"
+in the kernel configuration
+file, or else the
+.Xr netgraph 4
+and
+.Xr ng_socket 4
+KLD modules must have been loaded via
+.Xr kldload 8 .
+.Sh RETURN VALUES
+The
+.Fn NgSetDebug
+function returns the previous debug setting.
+.Pp
+The
+.Fn NgSetErrLog
+function has no return value.
+.Pp
+All other functions return \-1 if there was an error and set
+.Va errno
+accordingly.
+.Pp
+A return value of zero from
+.Fn NgRecvMsg
+or
+.Fn NgRecvData
+indicates that the netgraph socket has been closed.
+.Pp
+For
+.Fn NgSendAsciiMsg
+and
+.Fn NgRecvAsciiMsg ,
+the following additional errors are possible:
+.Bl -tag -width Er
+.It Bq Er ENOSYS
+The node type does not know how to encode or decode the control message.
+.It Bq Er ERANGE
+The encoded or decoded arguments were too long for the supplied buffer.
+.It Bq Er ENOENT
+An unknown structure field was seen in an
+.Tn ASCII
+control message.
+.It Bq Er EALREADY
+The same structure field was specified twice in an
+.Tn ASCII
+control message.
+.It Bq Er EINVAL
+.Tn ASCII
+control message parse error or illegal value.
+.It Bq Er E2BIG
+ASCII control message array or fixed width string buffer overflow.
+.El
+.Sh SEE ALSO
+.Xr select 2 ,
+.Xr socket 2 ,
+.Xr warnx 3 ,
+.Xr kld 4 ,
+.Xr netgraph 4 ,
+.Xr ng_socket 4
+.Sh HISTORY
+The
+.Nm netgraph
+system was designed and first implemented at Whistle Communications, Inc.\& in
+a version of
+.Fx 2.2
+customized for the Whistle InterJet.
+.Sh AUTHORS
+.An "Archie Cobbs" Aq archie@FreeBSD.org
diff --git a/lib/libnetgraph/netgraph.h b/lib/libnetgraph/netgraph.h
new file mode 100644
index 0000000..4eb15bd
--- /dev/null
+++ b/lib/libnetgraph/netgraph.h
@@ -0,0 +1,69 @@
+
+/*
+ * netgraph.h
+ *
+ * Copyright (c) 1996-1999 Whistle Communications, Inc.
+ * All rights reserved.
+ *
+ * Subject to the following obligations and disclaimer of warranty, use and
+ * redistribution of this software, in source or object code forms, with or
+ * without modifications are expressly permitted by Whistle Communications;
+ * provided, however, that:
+ * 1. Any and all reproductions of the source or object code must include the
+ * copyright notice above and the following disclaimer of warranties; and
+ * 2. No rights are granted, in any manner or form, to use Whistle
+ * Communications, Inc. trademarks, including the mark "WHISTLE
+ * COMMUNICATIONS" on advertising, endorsements, or otherwise except as
+ * such appears in the above copyright notice or in the software.
+ *
+ * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
+ * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
+ * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
+ * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
+ * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
+ * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
+ * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
+ * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
+ * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
+ * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
+ * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
+ * OF SUCH DAMAGE.
+ *
+ * Author: Archie Cobbs <archie@whistle.com>
+ *
+ * $FreeBSD$
+ * $Whistle: netgraph.h,v 1.7 1999/01/20 00:57:23 archie Exp $
+ */
+
+#ifndef _NETGRAPH_H_
+#define _NETGRAPH_H_
+
+#include <sys/types.h>
+#include <netgraph/ng_message.h>
+
+__BEGIN_DECLS
+int NgMkSockNode(const char *, int *, int *);
+int NgNameNode(int, const char *, const char *, ...) __printflike(3, 4);
+int NgSendMsg(int, const char *, int, int, const void *, size_t);
+int NgSendAsciiMsg(int, const char *, const char *, ...) __printflike(3, 4);
+int NgSendReplyMsg(int, const char *,
+ const struct ng_mesg *, const void *, size_t);
+int NgRecvMsg(int, struct ng_mesg *, size_t, char *);
+int NgAllocRecvMsg(int, struct ng_mesg **, char *);
+int NgRecvAsciiMsg(int, struct ng_mesg *, size_t, char *);
+int NgAllocRecvAsciiMsg(int, struct ng_mesg **, char *);
+int NgSendData(int, const char *, const u_char *, size_t);
+int NgRecvData(int, u_char *, size_t, char *);
+int NgAllocRecvData(int, u_char **, char *);
+int NgSetDebug(int);
+void NgSetErrLog(void (*)(const char *fmt, ...),
+ void (*)(const char *fmt, ...));
+__END_DECLS
+
+#endif
+
diff --git a/lib/libnetgraph/sock.c b/lib/libnetgraph/sock.c
new file mode 100644
index 0000000..395f28b
--- /dev/null
+++ b/lib/libnetgraph/sock.c
@@ -0,0 +1,300 @@
+/*
+ * sock.c
+ *
+ * Copyright (c) 1996-1999 Whistle Communications, Inc.
+ * All rights reserved.
+ *
+ * Subject to the following obligations and disclaimer of warranty, use and
+ * redistribution of this software, in source or object code forms, with or
+ * without modifications are expressly permitted by Whistle Communications;
+ * provided, however, that:
+ * 1. Any and all reproductions of the source or object code must include the
+ * copyright notice above and the following disclaimer of warranties; and
+ * 2. No rights are granted, in any manner or form, to use Whistle
+ * Communications, Inc. trademarks, including the mark "WHISTLE
+ * COMMUNICATIONS" on advertising, endorsements, or otherwise except as
+ * such appears in the above copyright notice or in the software.
+ *
+ * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
+ * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
+ * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
+ * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
+ * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
+ * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
+ * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
+ * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
+ * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
+ * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
+ * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
+ * OF SUCH DAMAGE.
+ *
+ * Author: Archie Cobbs <archie@whistle.com>
+ *
+ * $Whistle: sock.c,v 1.12 1999/01/20 00:57:23 archie Exp $
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/types.h>
+#include <stdarg.h>
+#include <netgraph/ng_message.h>
+#include <netgraph/ng_socket.h>
+
+#include "netgraph.h"
+#include "internal.h"
+
+/* The socket node type KLD */
+#define NG_SOCKET_KLD "ng_socket.ko"
+
+/*
+ * Create a socket type node and give it the supplied name.
+ * Return data and control sockets corresponding to the node.
+ * Returns -1 if error and sets errno.
+ */
+int
+NgMkSockNode(const char *name, int *csp, int *dsp)
+{
+ char namebuf[NG_NODESIZ];
+ int cs = -1; /* control socket */
+ int ds = -1; /* data socket */
+ int errnosv;
+
+ /* Empty name means no name */
+ if (name && *name == 0)
+ name = NULL;
+
+ /* Create control socket; this also creates the netgraph node.
+ If we get an EPROTONOSUPPORT then the socket node type is
+ not loaded, so load it and try again. */
+ if ((cs = socket(AF_NETGRAPH, SOCK_DGRAM, NG_CONTROL)) < 0) {
+ if (errno == EPROTONOSUPPORT) {
+ if (kldload(NG_SOCKET_KLD) < 0) {
+ errnosv = errno;
+ if (_gNgDebugLevel >= 1)
+ NGLOG("can't load %s", NG_SOCKET_KLD);
+ goto errout;
+ }
+ cs = socket(AF_NETGRAPH, SOCK_DGRAM, NG_CONTROL);
+ if (cs >= 0)
+ goto gotNode;
+ }
+ errnosv = errno;
+ if (_gNgDebugLevel >= 1)
+ NGLOG("socket");
+ goto errout;
+ }
+
+gotNode:
+ /* Assign the node the desired name, if any */
+ if (name != NULL) {
+ u_char sbuf[NG_NODESIZ + NGSA_OVERHEAD];
+ struct sockaddr_ng *const sg = (struct sockaddr_ng *) sbuf;
+
+ /* Assign name */
+ strlcpy(sg->sg_data, name, NG_NODESIZ);
+ sg->sg_family = AF_NETGRAPH;
+ sg->sg_len = strlen(sg->sg_data) + 1 + NGSA_OVERHEAD;
+ if (bind(cs, (struct sockaddr *) sg, sg->sg_len) < 0) {
+ errnosv = errno;
+ if (_gNgDebugLevel >= 1)
+ NGLOG("bind(%s)", sg->sg_data);
+ goto errout;
+ }
+
+ /* Save node name */
+ strlcpy(namebuf, name, sizeof(namebuf));
+ } else if (dsp != NULL) {
+ u_char rbuf[sizeof(struct ng_mesg) + sizeof(struct nodeinfo)];
+ struct ng_mesg *const resp = (struct ng_mesg *) rbuf;
+ struct nodeinfo *const ni = (struct nodeinfo *) resp->data;
+
+ /* Find out the node ID */
+ if (NgSendMsg(cs, ".", NGM_GENERIC_COOKIE,
+ NGM_NODEINFO, NULL, 0) < 0) {
+ errnosv = errno;
+ if (_gNgDebugLevel >= 1)
+ NGLOG("send nodeinfo");
+ goto errout;
+ }
+ if (NgRecvMsg(cs, resp, sizeof(rbuf), NULL) < 0) {
+ errnosv = errno;
+ if (_gNgDebugLevel >= 1)
+ NGLOG("recv nodeinfo");
+ goto errout;
+ }
+
+ /* Save node "name" */
+ snprintf(namebuf, sizeof(namebuf), "[%lx]", (u_long) ni->id);
+ }
+
+ /* Create data socket if desired */
+ if (dsp != NULL) {
+ u_char sbuf[NG_NODESIZ + 1 + NGSA_OVERHEAD];
+ struct sockaddr_ng *const sg = (struct sockaddr_ng *) sbuf;
+
+ /* Create data socket, initially just "floating" */
+ if ((ds = socket(AF_NETGRAPH, SOCK_DGRAM, NG_DATA)) < 0) {
+ errnosv = errno;
+ if (_gNgDebugLevel >= 1)
+ NGLOG("socket");
+ goto errout;
+ }
+
+ /* Associate the data socket with the node */
+ snprintf(sg->sg_data, NG_NODESIZ + 1, "%s:", namebuf);
+ sg->sg_family = AF_NETGRAPH;
+ sg->sg_len = strlen(sg->sg_data) + 1 + NGSA_OVERHEAD;
+ if (connect(ds, (struct sockaddr *) sg, sg->sg_len) < 0) {
+ errnosv = errno;
+ if (_gNgDebugLevel >= 1)
+ NGLOG("connect(%s)", sg->sg_data);
+ goto errout;
+ }
+ }
+
+ /* Return the socket(s) */
+ if (csp)
+ *csp = cs;
+ else
+ close(cs);
+ if (dsp)
+ *dsp = ds;
+ return (0);
+
+errout:
+ /* Failed */
+ if (cs >= 0)
+ close(cs);
+ if (ds >= 0)
+ close(ds);
+ errno = errnosv;
+ return (-1);
+}
+
+/*
+ * Assign a globally unique name to a node
+ * Returns -1 if error and sets errno.
+ */
+int
+NgNameNode(int cs, const char *path, const char *fmt, ...)
+{
+ struct ngm_name ngn;
+ va_list args;
+
+ /* Build message arg */
+ va_start(args, fmt);
+ vsnprintf(ngn.name, sizeof(ngn.name), fmt, args);
+ va_end(args);
+
+ /* Send message */
+ if (NgSendMsg(cs, path,
+ NGM_GENERIC_COOKIE, NGM_NAME, &ngn, sizeof(ngn)) < 0) {
+ if (_gNgDebugLevel >= 1)
+ NGLOGX("%s: failed", __func__);
+ return (-1);
+ }
+
+ /* Done */
+ return (0);
+}
+
+/*
+ * Read a packet from a data socket
+ * Returns -1 if error and sets errno.
+ */
+int
+NgRecvData(int ds, u_char * buf, size_t len, char *hook)
+{
+ u_char frombuf[NG_HOOKSIZ + NGSA_OVERHEAD];
+ struct sockaddr_ng *const from = (struct sockaddr_ng *) frombuf;
+ socklen_t fromlen = sizeof(frombuf);
+ int rtn, errnosv;
+
+ /* Read packet */
+ rtn = recvfrom(ds, buf, len, 0, (struct sockaddr *) from, &fromlen);
+ if (rtn < 0) {
+ errnosv = errno;
+ if (_gNgDebugLevel >= 1)
+ NGLOG("recvfrom");
+ errno = errnosv;
+ return (-1);
+ }
+
+ /* Copy hook name */
+ if (hook != NULL)
+ strlcpy(hook, from->sg_data, NG_HOOKSIZ);
+
+ /* Debugging */
+ if (_gNgDebugLevel >= 2) {
+ NGLOGX("READ %s from hook \"%s\" (%d bytes)",
+ rtn ? "PACKET" : "EOF", from->sg_data, rtn);
+ if (_gNgDebugLevel >= 3)
+ _NgDebugBytes(buf, rtn);
+ }
+
+ /* Done */
+ return (rtn);
+}
+
+/*
+ * Identical to NgRecvData() except buffer is dynamically allocated.
+ */
+int
+NgAllocRecvData(int ds, u_char **buf, char *hook)
+{
+ int len;
+ socklen_t optlen;
+
+ optlen = sizeof(len);
+ if (getsockopt(ds, SOL_SOCKET, SO_RCVBUF, &len, &optlen) == -1 ||
+ (*buf = malloc(len)) == NULL)
+ return (-1);
+ if ((len = NgRecvData(ds, *buf, len, hook)) < 0)
+ free(*buf);
+ return (len);
+}
+
+/*
+ * Write a packet to a data socket. The packet will be sent
+ * out the corresponding node on the specified hook.
+ * Returns -1 if error and sets errno.
+ */
+int
+NgSendData(int ds, const char *hook, const u_char * buf, size_t len)
+{
+ u_char sgbuf[NG_HOOKSIZ + NGSA_OVERHEAD];
+ struct sockaddr_ng *const sg = (struct sockaddr_ng *) sgbuf;
+ int errnosv;
+
+ /* Set up destination hook */
+ sg->sg_family = AF_NETGRAPH;
+ strlcpy(sg->sg_data, hook, NG_HOOKSIZ);
+ sg->sg_len = strlen(sg->sg_data) + 1 + NGSA_OVERHEAD;
+
+ /* Debugging */
+ if (_gNgDebugLevel >= 2) {
+ NGLOGX("WRITE PACKET to hook \"%s\" (%d bytes)", hook, len);
+ _NgDebugSockaddr(sg);
+ if (_gNgDebugLevel >= 3)
+ _NgDebugBytes(buf, len);
+ }
+
+ /* Send packet */
+ if (sendto(ds, buf, len, 0, (struct sockaddr *) sg, sg->sg_len) < 0) {
+ errnosv = errno;
+ if (_gNgDebugLevel >= 1)
+ NGLOG("sendto(%s)", sg->sg_data);
+ errno = errnosv;
+ return (-1);
+ }
+
+ /* Done */
+ return (0);
+}
+
OpenPOWER on IntegriCloud