[rtems-libbsd commit] FreeBSD user space support - First additions

Joel Sherrill joel at rtems.org
Wed Jul 11 18:22:04 UTC 2012


Module:    rtems-libbsd
Branch:    master
Commit:    1b13f6981a8c459d3f6b91cebf1f359cdabbb129
Changeset: http://git.rtems.org/rtems-libbsd/commit/?id=1b13f6981a8c459d3f6b91cebf1f359cdabbb129

Author:    Joel Sherrill <joel.sherrill at oarcorp.com>
Date:      Wed Jul 11 13:23:45 2012 -0500

FreeBSD user space support - First additions

This requires a full checkout of the FreeBSD 8.2 source code.
The checkout we have been using is missing at least the libc,
include, and sys directories.  The script to copy files in is
crude.

This eventually needs to separate the manually written .h files
out and add the RTEMS specific network initialization support
with functionality to program the IP addresses and gateways as
the old stack did.

---

 freebsd-userspace/Makefile                        |   81 ++
 freebsd-userspace/README                          |   11 +
 freebsd-userspace/from-freebsd.sh                 |  205 +++
 freebsd-userspace/include/arpa/inet.h             |  182 +++
 freebsd-userspace/include/arpa/nameser.h          |  584 +++++++++
 freebsd-userspace/include/arpa/nameser_compat.h   |  203 +++
 freebsd-userspace/include/db.h                    |  221 ++++
 freebsd-userspace/include/ifaddrs.h               |   65 +
 freebsd-userspace/include/include/arpa/inet.h     |    1 +
 freebsd-userspace/include/include/arpa/nameser.h  |    1 +
 freebsd-userspace/include/net/if.h                |    1 +
 freebsd-userspace/include/net/if_dl.h             |    1 +
 freebsd-userspace/include/net/if_types.h          |    1 +
 freebsd-userspace/include/net/in.h                |    1 +
 freebsd-userspace/include/net/route.h             |    1 +
 freebsd-userspace/include/netdb.h                 |  297 +++++
 freebsd-userspace/include/netinet/in.h            |    1 +
 freebsd-userspace/include/nsswitch.h              |  251 ++++
 freebsd-userspace/include/resolv.h                |  502 ++++++++
 freebsd-userspace/include/sys/socket.h            |    1 +
 freebsd-userspace/include/sys/sysctl.h            |    1 +
 freebsd-userspace/lib/libc/include/libc_private.h |  225 ++++
 freebsd-userspace/lib/libc/include/namespace.h    |  263 ++++
 freebsd-userspace/lib/libc/include/nss_tls.h      |   80 ++
 freebsd-userspace/lib/libc/include/reentrant.h    |  135 ++
 freebsd-userspace/lib/libc/include/resolv_mt.h    |   47 +
 freebsd-userspace/lib/libc/include/un-namespace.h |  253 ++++
 freebsd-userspace/lib/libc/inet/inet_addr.c       |  215 ++++
 freebsd-userspace/lib/libc/inet/inet_cidr_ntop.c  |  263 ++++
 freebsd-userspace/lib/libc/inet/inet_cidr_pton.c  |  279 +++++
 freebsd-userspace/lib/libc/inet/inet_lnaof.c      |   70 ++
 freebsd-userspace/lib/libc/inet/inet_makeaddr.c   |   73 ++
 freebsd-userspace/lib/libc/inet/inet_net_ntop.c   |  288 +++++
 freebsd-userspace/lib/libc/inet/inet_net_pton.c   |  416 +++++++
 freebsd-userspace/lib/libc/inet/inet_neta.c       |   98 ++
 freebsd-userspace/lib/libc/inet/inet_netof.c      |   69 +
 freebsd-userspace/lib/libc/inet/inet_network.c    |  111 ++
 freebsd-userspace/lib/libc/inet/inet_ntoa.c       |   78 ++
 freebsd-userspace/lib/libc/inet/inet_ntop.c       |  203 +++
 freebsd-userspace/lib/libc/inet/inet_pton.c       |  225 ++++
 freebsd-userspace/lib/libc/inet/nsap_addr.c       |  122 ++
 freebsd-userspace/lib/libc/net/base64.c           |  317 +++++
 freebsd-userspace/lib/libc/net/ether_addr.c       |  232 ++++
 freebsd-userspace/lib/libc/net/gethostbydns.c     |  781 ++++++++++++
 freebsd-userspace/lib/libc/net/gethostbyht.c      |  341 +++++
 freebsd-userspace/lib/libc/net/gethostbynis.c     |  354 ++++++
 freebsd-userspace/lib/libc/net/gethostnamadr.c    |  736 +++++++++++
 freebsd-userspace/lib/libc/net/getifaddrs.c       |  420 +++++++
 freebsd-userspace/lib/libc/net/getifmaddrs.c      |  205 +++
 freebsd-userspace/lib/libc/net/getnameinfo.c      |  455 +++++++
 freebsd-userspace/lib/libc/net/getnetbydns.c      |  467 +++++++
 freebsd-userspace/lib/libc/net/getnetbyht.c       |  290 +++++
 freebsd-userspace/lib/libc/net/getnetbynis.c      |  261 ++++
 freebsd-userspace/lib/libc/net/getnetnamadr.c     |  460 +++++++
 freebsd-userspace/lib/libc/net/getproto.c         |  145 +++
 freebsd-userspace/lib/libc/net/getprotoent.c      |  556 +++++++++
 freebsd-userspace/lib/libc/net/getprotoname.c     |  153 +++
 freebsd-userspace/lib/libc/net/getservent.c       | 1375 +++++++++++++++++++++
 freebsd-userspace/lib/libc/net/netdb_private.h    |  145 +++
 freebsd-userspace/lib/libc/net/res_config.h       |    6 +
 freebsd-userspace/local/port_before.h             |    1 +
 freebsd-userspace/local/pthread_np.h              |    1 +
 freebsd-userspace/local/sys/_pthreadtypes.h       |    1 +
 freebsd-userspace/local/syslog.h                  |    1 +
 freebsd-userspace/sys/net/ethernet.h              |  405 ++++++
 freebsd-userspace/sys/sys/syslog.h                |  203 +++
 66 files changed, 14436 insertions(+), 0 deletions(-)

diff --git a/freebsd-userspace/Makefile b/freebsd-userspace/Makefile
new file mode 100644
index 0000000..d288187
--- /dev/null
+++ b/freebsd-userspace/Makefile
@@ -0,0 +1,81 @@
+include ../config.inc
+
+include $(RTEMS_MAKEFILE_PATH)/Makefile.inc
+include $(RTEMS_CUSTOM)
+include $(PROJECT_ROOT)/make/leaf.cfg
+
+CFLAGS += -I $(INSTALL_BASE)/include
+#CFLAGS += -ffreestanding 
+CFLAGS += -I include 
+CFLAGS += -I lib/libc/include 
+CFLAGS += -I sys
+CFLAGS += -I local 
+#CFLAGS += -I . 
+#CFLAGS += -I rtemsbsd 
+##CFLAGS += -I rtemsbsd/$(RTEMS_CPU)/include 
+#CFLAGS += -I freebsd/$(RTEMS_CPU)/include 
+#CFLAGS += -I contrib/altq 
+#CFLAGS += -I contrib/pf 
+#CFLAGS += -B $(INSTALL_BASE) 
+CFLAGS += -w 
+CFLAGS += -std=gnu99
+CFLAGS += -MT $@ -MD -MP -MF $(basename $@).d
+
+C_FILES =
+C_FILES += lib/libc/inet/inet_addr.c
+C_FILES += lib/libc/inet/inet_cidr_ntop.c
+C_FILES += lib/libc/inet/inet_cidr_pton.c
+C_FILES += lib/libc/inet/inet_lnaof.c
+C_FILES += lib/libc/inet/inet_makeaddr.c
+C_FILES += lib/libc/inet/inet_net_pton.c
+C_FILES += lib/libc/inet/inet_ntop.c
+C_FILES += lib/libc/inet/inet_neta.c
+C_FILES += lib/libc/inet/inet_net_ntop.c
+C_FILES += lib/libc/inet/inet_netof.c
+C_FILES += lib/libc/inet/inet_network.c
+C_FILES += lib/libc/inet/inet_ntoa.c
+C_FILES += lib/libc/inet/inet_pton.c
+C_FILES += lib/libc/inet/nsap_addr.c
+
+C_FILES += lib/libc/net/base64.c
+C_FILES += lib/libc/net/ether_addr.c
+# C_FILES += lib/libc/net/gethostbydns.c
+# C_FILES += lib/libc/net/gethostbyht.c
+C_FILES += lib/libc/net/gethostbynis.c
+# C_FILES += lib/libc/net/gethostnamadr.c
+C_FILES += lib/libc/net/getifaddrs.c
+C_FILES += lib/libc/net/getifmaddrs.c
+C_FILES += lib/libc/net/getnameinfo.c
+# C_FILES += lib/libc/net/getnetbydns.c
+# C_FILES += lib/libc/net/getnetbyht.c
+C_FILES += lib/libc/net/getnetbynis.c
+# C_FILES += lib/libc/net/getnetnamadr.c
+# C_FILES += lib/libc/net/getproto.c
+# C_FILES += lib/libc/net/getprotoent.c
+# C_FILES += lib/libc/net/getprotoname.c
+# C_FILES += lib/libc/net/getservent.c
+
+C_O_FILES = $(C_FILES:%.c=%.o)
+C_D_FILES = $(C_FILES:%.c=%.d)
+
+LIB = libbsdc.a
+
+all: lib_bsdc
+
+$(LIB): $(C_O_FILES)
+	$(AR) rcu $@ $^
+
+lib_bsdc:
+	$(MAKE) $(LIB)
+
+install: $(LIB)
+	#install -d $(INSTALL_BASE)/include
+	#install -c -m 644 $(LIB) $(INSTALL_BASE)
+
+clean:
+	rm -f $(LIB) $(C_O_FILES) $(C_D_FILES)
+
+-include $(C_D_FILES)
+
+doc: 
+
diff --git a/freebsd-userspace/README b/freebsd-userspace/README
new file mode 100644
index 0000000..9aca018
--- /dev/null
+++ b/freebsd-userspace/README
@@ -0,0 +1,11 @@
+
+The following describes the contents of this directory:
+
+from-freebsd.sh - this script
+include         - From FreeBSD 8.2 include
+lib             - From FreeBSD 8.2 lib
+local           - Mix of hand and autogenerated
+Makefile        - Hand written Makefile
+sys             - From FreeBSD 8.2 sys
+
+
diff --git a/freebsd-userspace/from-freebsd.sh b/freebsd-userspace/from-freebsd.sh
new file mode 100755
index 0000000..c8373b4
--- /dev/null
+++ b/freebsd-userspace/from-freebsd.sh
@@ -0,0 +1,205 @@
+#! /bin/bash
+#
+#  Temporary hack of a script to get FreeBSD user space header
+#  files and network libc source code into this tree.
+#
+#  NOTE: libc and include are NOT in the FreeBSD checkout used for the
+#        USB and TCP/IP stacks. This has to be addressed.
+#
+#  This probably could be integrated into the main Python script.
+#  But so far, there are no real transformations needed and the
+#  script just performs two tasks:
+#
+#  + copies source files from FreeBSD libc and include files into this tree
+#  + generates "wrapper" header files for the public network .h files
+#
+src=/home/joel/newbsd/git/freebsd-8.2
+dest=/home/joel/newbsd/git/rtems-libbsd/freebsd-userspace
+
+progname=${0##*/}        # fast basename hack for ksh, bash
+
+USAGE=\
+"usage: $progname [ -opts ]
+	-v	   -- verbose
+	-f DIR	   -- FreeBSD directory
+	-r DIR	   -- rtems-libbsd top directory
+"
+
+check_status()
+{
+  if [ $1 -ne 0 ] ; then
+    shift 
+    echo "$USAGE" >&2
+    echo >&2
+    echo "FAILED: " "$*" >&2
+    exit 1
+  fi
+}
+
+usage()
+{
+  echo "$USAGE" >&2
+  exit 1
+}
+
+#
+# process the options
+#
+# defaults for getopt vars
+#
+
+src=NOTSET
+dest=NOTSET
+verbose="no"
+
+while getopts "vf:r:" OPT
+do
+ case "$OPT" in
+   v) verbose="yes";;
+   f) src="$OPTARG";;
+   r) dest="$OPTARG";;
+   *) usage;;
+  esac
+done
+
+# Check arguments specified
+test ${src} != "NOTSET" 
+check_status $? "FreeBSD source directory not specified"
+
+test ${dest} != "NOTSET" 
+check_status $? "rtems-libbsd source directory not specified"
+
+# Check that they are directories
+test -d ${src}
+check_status $? "FreeBSD source directory is not a directory"
+
+test -d ${dest}
+check_status $? "rtems-libbsd source directory is not a directory"
+
+# Check that they appear to be the right directories
+test -d ${src}/include
+check_status $? "FreeBSD source directory does not have include directory"
+
+test -d ${src}/lib/libc
+check_status $? "FreeBSD source directory does not have libc directory"
+
+test -d ${dest}/freebsd-userspace
+check_status $? "rtems-libbsd source directory does not have freebsd-userspace directory"
+
+dest=${dest}/freebsd-userspace
+
+# source files to simply copy
+while read f
+do
+  d=`dirname $f`
+  test -d ${dest}/${d} || mkdir -p ${dest}/${d}
+  test -r ${src}/${f}
+  check_status $? "${src}/${f} is not present"
+
+  test ${verbose} = "yes" && echo "Simple copy $f"
+  cp ${src}/${f} ${dest}/${d}
+done <<EOF
+include/db.h
+include/ifaddrs.h
+include/netdb.h
+include/nsswitch.h
+include/resolv.h
+include/arpa/nameser_compat.h
+include/arpa/inet.h
+sys/net/ethernet.h
+sys/sys/syslog.h
+lib/libc/include/libc_private.h
+lib/libc/include/namespace.h
+lib/libc/include/nss_tls.h
+lib/libc/include/reentrant.h
+lib/libc/include/resolv_mt.h
+lib/libc/include/un-namespace.h
+lib/libc/inet/inet_addr.c
+lib/libc/inet/inet_cidr_ntop.c
+lib/libc/inet/inet_cidr_pton.c
+lib/libc/inet/inet_lnaof.c
+lib/libc/inet/inet_makeaddr.c
+lib/libc/inet/inet_neta.c
+lib/libc/inet/inet_net_ntop.c
+lib/libc/inet/inet_netof.c
+lib/libc/inet/inet_net_pton.c
+lib/libc/inet/inet_network.c
+lib/libc/inet/inet_ntoa.c
+lib/libc/inet/inet_ntop.c
+lib/libc/inet/inet_pton.c
+lib/libc/inet/nsap_addr.c
+lib/libc/net/netdb_private.h
+lib/libc/net/res_config.h
+EOF
+
+# disable BIND_4_COMPAT since it trips a weird endian issue in nameser_compat.h
+sed -e 's/#define BIND_4_COMPAT/\/* #define BIND_4_COMPAT *\//' \
+  <${src}/include/arpa/nameser.h \
+  >${dest}/include/arpa/nameser.h
+
+# source files to prepend "include of local/port_before.h"
+while read f
+do
+  d=`dirname $f`
+  test -d ${dest}/${d} || mkdir -p ${dest}/${d}
+  test -r ${src}/${f}
+  check_status $? "${src}/${f} is not present"
+
+  test ${verbose} = "yes" && echo "Copy with prepend $f"
+   
+  ( echo "#include \"port_before.h\""; echo ; cat ${src}/${f} ) >${dest}/${f}
+done <<EOF
+lib/libc/net/base64.c
+lib/libc/net/ether_addr.c
+lib/libc/net/gethostbydns.c
+lib/libc/net/gethostbyht.c
+lib/libc/net/gethostbynis.c
+lib/libc/net/gethostnamadr.c
+lib/libc/net/getifaddrs.c
+lib/libc/net/getifmaddrs.c
+lib/libc/net/getnameinfo.c
+lib/libc/net/getnetbydns.c
+lib/libc/net/getnetbyht.c
+lib/libc/net/getnetbynis.c
+lib/libc/net/getnetnamadr.c
+lib/libc/net/getproto.c
+lib/libc/net/getprotoent.c
+lib/libc/net/getprotoname.c
+lib/libc/net/getservent.c
+EOF
+
+
+# files to "include with freebsd"
+while read f
+do
+  d=`dirname $f`
+  test -d include/${d}/${d} || mkdir -p include/${d}/${d}
+
+  test ${verbose} = "yes" && echo "Generate wrapper $f"
+  echo "#include <freebsd/${f}>" > include/${f}
+done <<EOF
+net/if.h
+net/if_dl.h
+net/if_types.h
+net/in.h
+net/route.h
+netinet/in.h
+sys/socket.h
+sys/sysctl.h
+EOF
+
+
+# empty files
+while read f
+do
+  test ${verbose} = "yes" && echo "Generate empty file $f"
+  echo "/* empty file */" > ${f}
+done <<EOF
+local/pthread_np.h
+local/sys/_pthreadtypes.h
+EOF
+
+
+# Fix syslog.h issues...
+echo "#include <sys/syslog.h>" > local/syslog.h
+
diff --git a/freebsd-userspace/include/arpa/inet.h b/freebsd-userspace/include/arpa/inet.h
new file mode 100644
index 0000000..91733a9
--- /dev/null
+++ b/freebsd-userspace/include/arpa/inet.h
@@ -0,0 +1,182 @@
+/*
+ * ++Copyright++ 1983, 1993
+ * -
+ * Copyright (c) 1983, 1993
+ *    The Regents of the University of California.  All rights reserved.
+ * 
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ * 	This product includes software developed by the University of
+ * 	California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ * 
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON 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 ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ * -
+ * Portions Copyright (c) 1993 by Digital Equipment Corporation.
+ * 
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies, and that
+ * the name of Digital Equipment Corporation not be used in advertising or
+ * publicity pertaining to distribution of the document or software without
+ * specific, written prior permission.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
+ * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
+ * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
+ * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
+ * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
+ * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
+ * SOFTWARE.
+ * -
+ * --Copyright--
+ */
+
+/*%
+ *	@(#)inet.h	8.1 (Berkeley) 6/2/93
+ *	$Id: inet.h,v 1.2.18.1 2005/04/27 05:00:50 sra Exp $
+ * $FreeBSD$
+ */
+
+#ifndef _ARPA_INET_H_
+#define	_ARPA_INET_H_
+
+/* External definitions for functions in inet(3). */
+
+#include <sys/cdefs.h>
+#include <sys/_types.h>
+
+/* Required for byteorder(3) functions. */
+#include <machine/endian.h>
+
+#define	INET_ADDRSTRLEN		16
+#define	INET6_ADDRSTRLEN	46
+
+#ifndef _UINT16_T_DECLARED
+typedef	__uint16_t	uint16_t;
+#define	_UINT16_T_DECLARED
+#endif
+
+#ifndef _UINT32_T_DECLARED
+typedef	__uint32_t	uint32_t;
+#define	_UINT32_T_DECLARED
+#endif
+
+#ifndef _IN_ADDR_T_DECLARED
+typedef	uint32_t	in_addr_t;
+#define	_IN_ADDR_T_DECLARED
+#endif
+
+#ifndef _IN_PORT_T_DECLARED
+typedef	uint16_t	in_port_t;
+#define	_IN_PORT_T_DECLARED
+#endif
+
+#if __BSD_VISIBLE
+#ifndef _SIZE_T_DECLARED
+typedef	__size_t	size_t;
+#define	_SIZE_T_DECLARED
+#endif
+#endif
+
+/*
+ * XXX socklen_t is used by a POSIX.1-2001 interface, but not required by
+ * POSIX.1-2001.
+ */
+#ifndef _SOCKLEN_T_DECLARED
+typedef	__socklen_t	socklen_t;
+#define	_SOCKLEN_T_DECLARED
+#endif
+
+#ifndef _STRUCT_IN_ADDR_DECLARED
+struct in_addr {
+	in_addr_t s_addr;
+};
+#define	_STRUCT_IN_ADDR_DECLARED
+#endif
+
+/* XXX all new diversions!! argh!! */
+#if __BSD_VISIBLE
+#define	inet_addr		__inet_addr
+#define	inet_aton		__inet_aton
+#define	inet_lnaof		__inet_lnaof
+#define	inet_makeaddr		__inet_makeaddr
+#define	inet_neta		__inet_neta
+#define	inet_netof		__inet_netof
+#define	inet_network		__inet_network
+#define	inet_net_ntop		__inet_net_ntop
+#define	inet_net_pton		__inet_net_pton
+#define	inet_cidr_ntop		__inet_cidr_ntop
+#define	inet_cidr_pton		__inet_cidr_pton
+#define	inet_ntoa		__inet_ntoa
+#define	inet_ntoa_r		__inet_ntoa_r
+#define	inet_pton		__inet_pton
+#define	inet_ntop		__inet_ntop
+#define	inet_nsap_addr		__inet_nsap_addr
+#define	inet_nsap_ntoa		__inet_nsap_ntoa
+#endif /* __BSD_VISIBLE */
+
+__BEGIN_DECLS
+#ifndef _BYTEORDER_PROTOTYPED
+#define	_BYTEORDER_PROTOTYPED
+uint32_t	 htonl(uint32_t);
+uint16_t	 htons(uint16_t);
+uint32_t	 ntohl(uint32_t);
+uint16_t	 ntohs(uint16_t);
+#endif
+
+in_addr_t	 inet_addr(const char *);
+/*const*/ char	*inet_ntoa(struct in_addr);
+const char	*inet_ntop(int, const void * __restrict, char * __restrict,
+		    socklen_t);
+int		 inet_pton(int, const char * __restrict, void * __restrict);
+
+#if __BSD_VISIBLE
+int		 inet_aton(const char *, struct in_addr *);
+in_addr_t	 inet_lnaof(struct in_addr);
+struct in_addr	 inet_makeaddr(in_addr_t, in_addr_t);
+char *		 inet_neta(in_addr_t, char *, size_t);
+in_addr_t	 inet_netof(struct in_addr);
+in_addr_t	 inet_network(const char *);
+char		*inet_net_ntop(int, const void *, int, char *, size_t);
+int		 inet_net_pton(int, const char *, void *, size_t);
+char		*inet_ntoa_r(struct in_addr, char *buf, socklen_t size);
+char		*inet_cidr_ntop(int, const void *, int, char *, size_t);
+int		 inet_cidr_pton(int, const char *, void *, int *);
+unsigned	 inet_nsap_addr(const char *, unsigned char *, int);
+char		*inet_nsap_ntoa(int, const unsigned char *, char *);
+#endif /* __BSD_VISIBLE */
+__END_DECLS
+
+#ifndef _BYTEORDER_FUNC_DEFINED
+#define	_BYTEORDER_FUNC_DEFINED
+#define	htonl(x)	__htonl(x)
+#define	htons(x)	__htons(x)
+#define	ntohl(x)	__ntohl(x)
+#define	ntohs(x)	__ntohs(x)
+#endif
+
+#endif /* !_ARPA_INET_H_ */
+
+/*! \file */
diff --git a/freebsd-userspace/include/arpa/nameser.h b/freebsd-userspace/include/arpa/nameser.h
new file mode 100644
index 0000000..30b538e
--- /dev/null
+++ b/freebsd-userspace/include/arpa/nameser.h
@@ -0,0 +1,584 @@
+/*
+ * Copyright (c) 1983, 1989, 1993
+ *    The Regents of the University of California.  All rights reserved.
+ * 
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ * 	This product includes software developed by the University of
+ * 	California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ * 
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON 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 ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+/*
+ * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
+ * Copyright (c) 1996-1999 by Internet Software Consortium.
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
+ * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+/*
+ *	$Id: nameser.h,v 1.7.18.2 2008/04/03 23:15:15 marka Exp $
+ * $FreeBSD$
+ */
+
+#ifndef _ARPA_NAMESER_H_
+#define _ARPA_NAMESER_H_
+
+/*! \file */
+
+/* #define BIND_4_COMPAT */
+
+#include <sys/param.h>
+#include <sys/types.h>
+#include <sys/cdefs.h>
+
+/*%
+ * Revision information.  This is the release date in YYYYMMDD format.
+ * It can change every day so the right thing to do with it is use it
+ * in preprocessor commands such as "#if (__NAMESER > 19931104)".  Do not
+ * compare for equality; rather, use it to determine whether your libbind.a
+ * contains a new enough lib/nameser/ to support the feature you need.
+ */
+
+#define __NAMESER	19991006	/*%< New interface version stamp. */
+/*
+ * Define constants based on RFC0883, RFC1034, RFC 1035
+ */
+#define NS_PACKETSZ	512	/*%< default UDP packet size */
+#define NS_MAXDNAME	1025	/*%< maximum domain name */
+#define NS_MAXMSG	65535	/*%< maximum message size */
+#define NS_MAXCDNAME	255	/*%< maximum compressed domain name */
+#define NS_MAXLABEL	63	/*%< maximum length of domain label */
+#define NS_HFIXEDSZ	12	/*%< #/bytes of fixed data in header */
+#define NS_QFIXEDSZ	4	/*%< #/bytes of fixed data in query */
+#define NS_RRFIXEDSZ	10	/*%< #/bytes of fixed data in r record */
+#define NS_INT32SZ	4	/*%< #/bytes of data in a u_int32_t */
+#define NS_INT16SZ	2	/*%< #/bytes of data in a u_int16_t */
+#define NS_INT8SZ	1	/*%< #/bytes of data in a u_int8_t */
+#define NS_INADDRSZ	4	/*%< IPv4 T_A */
+#define NS_IN6ADDRSZ	16	/*%< IPv6 T_AAAA */
+#define NS_CMPRSFLGS	0xc0	/*%< Flag bits indicating name compression. */
+#define NS_DEFAULTPORT	53	/*%< For both TCP and UDP. */
+/*
+ * These can be expanded with synonyms, just keep ns_parse.c:ns_parserecord()
+ * in synch with it.
+ */
+typedef enum __ns_sect {
+	ns_s_qd = 0,		/*%< Query: Question. */
+	ns_s_zn = 0,		/*%< Update: Zone. */
+	ns_s_an = 1,		/*%< Query: Answer. */
+	ns_s_pr = 1,		/*%< Update: Prerequisites. */
+	ns_s_ns = 2,		/*%< Query: Name servers. */
+	ns_s_ud = 2,		/*%< Update: Update. */
+	ns_s_ar = 3,		/*%< Query|Update: Additional records. */
+	ns_s_max = 4
+} ns_sect;
+
+/*%
+ * This is a message handle.  It is caller allocated and has no dynamic data.
+ * This structure is intended to be opaque to all but ns_parse.c, thus the
+ * leading _'s on the member names.  Use the accessor functions, not the _'s.
+ */
+typedef struct __ns_msg {
+	const u_char	*_msg, *_eom;
+	u_int16_t	_id, _flags, _counts[ns_s_max];
+	const u_char	*_sections[ns_s_max];
+	ns_sect		_sect;
+	int		_rrnum;
+	const u_char	*_msg_ptr;
+} ns_msg;
+
+/* Private data structure - do not use from outside library. */
+struct _ns_flagdata {  int mask, shift;  };
+extern struct _ns_flagdata _ns_flagdata[];
+
+/* Accessor macros - this is part of the public interface. */
+
+#define ns_msg_id(handle) ((handle)._id + 0)
+#define ns_msg_base(handle) ((handle)._msg + 0)
+#define ns_msg_end(handle) ((handle)._eom + 0)
+#define ns_msg_size(handle) ((handle)._eom - (handle)._msg)
+#define ns_msg_count(handle, section) ((handle)._counts[section] + 0)
+
+/*%
+ * This is a parsed record.  It is caller allocated and has no dynamic data.
+ */
+typedef	struct __ns_rr {
+	char		name[NS_MAXDNAME];
+	u_int16_t	type;
+	u_int16_t	rr_class;
+	u_int32_t	ttl;
+	u_int16_t	rdlength;
+	const u_char *	rdata;
+} ns_rr;
+
+/* Accessor macros - this is part of the public interface. */
+#define ns_rr_name(rr)	(((rr).name[0] != '\0') ? (rr).name : ".")
+#define ns_rr_type(rr)	((ns_type)((rr).type + 0))
+#define ns_rr_class(rr)	((ns_class)((rr).rr_class + 0))
+#define ns_rr_ttl(rr)	((rr).ttl + 0)
+#define ns_rr_rdlen(rr)	((rr).rdlength + 0)
+#define ns_rr_rdata(rr)	((rr).rdata + 0)
+
+/*%
+ * These don't have to be in the same order as in the packet flags word,
+ * and they can even overlap in some cases, but they will need to be kept
+ * in synch with ns_parse.c:ns_flagdata[].
+ */
+typedef enum __ns_flag {
+	ns_f_qr,		/*%< Question/Response. */
+	ns_f_opcode,		/*%< Operation code. */
+	ns_f_aa,		/*%< Authoritative Answer. */
+	ns_f_tc,		/*%< Truncation occurred. */
+	ns_f_rd,		/*%< Recursion Desired. */
+	ns_f_ra,		/*%< Recursion Available. */
+	ns_f_z,			/*%< MBZ. */
+	ns_f_ad,		/*%< Authentic Data (DNSSEC). */
+	ns_f_cd,		/*%< Checking Disabled (DNSSEC). */
+	ns_f_rcode,		/*%< Response code. */
+	ns_f_max
+} ns_flag;
+
+/*%
+ * Currently defined opcodes.
+ */
+typedef enum __ns_opcode {
+	ns_o_query = 0,		/*%< Standard query. */
+	ns_o_iquery = 1,	/*%< Inverse query (deprecated/unsupported). */
+	ns_o_status = 2,	/*%< Name server status query (unsupported). */
+				/* Opcode 3 is undefined/reserved. */
+	ns_o_notify = 4,	/*%< Zone change notification. */
+	ns_o_update = 5,	/*%< Zone update message. */
+	ns_o_max = 6
+} ns_opcode;
+
+/*%
+ * Currently defined response codes.
+ */
+typedef	enum __ns_rcode {
+	ns_r_noerror = 0,	/*%< No error occurred. */
+	ns_r_formerr = 1,	/*%< Format error. */
+	ns_r_servfail = 2,	/*%< Server failure. */
+	ns_r_nxdomain = 3,	/*%< Name error. */
+	ns_r_notimpl = 4,	/*%< Unimplemented. */
+	ns_r_refused = 5,	/*%< Operation refused. */
+	/* these are for BIND_UPDATE */
+	ns_r_yxdomain = 6,	/*%< Name exists */
+	ns_r_yxrrset = 7,	/*%< RRset exists */
+	ns_r_nxrrset = 8,	/*%< RRset does not exist */
+	ns_r_notauth = 9,	/*%< Not authoritative for zone */
+	ns_r_notzone = 10,	/*%< Zone of record different from zone section */
+	ns_r_max = 11,
+	/* The following are EDNS extended rcodes */
+	ns_r_badvers = 16,
+	/* The following are TSIG errors */
+	ns_r_badsig = 16,
+	ns_r_badkey = 17,
+	ns_r_badtime = 18
+} ns_rcode;
+
+/* BIND_UPDATE */
+typedef enum __ns_update_operation {
+	ns_uop_delete = 0,
+	ns_uop_add = 1,
+	ns_uop_max = 2
+} ns_update_operation;
+
+/*%
+ * This structure is used for TSIG authenticated messages
+ */
+struct ns_tsig_key {
+        char name[NS_MAXDNAME], alg[NS_MAXDNAME];
+        unsigned char *data;
+        int len;
+};
+typedef struct ns_tsig_key ns_tsig_key;
+
+/*%
+ * This structure is used for TSIG authenticated TCP messages
+ */
+struct ns_tcp_tsig_state {
+	int counter;
+	struct dst_key *key;
+	void *ctx;
+	unsigned char sig[NS_PACKETSZ];
+	int siglen;
+};
+typedef struct ns_tcp_tsig_state ns_tcp_tsig_state;
+
+#define NS_TSIG_FUDGE 300
+#define NS_TSIG_TCP_COUNT 100
+#define NS_TSIG_ALG_HMAC_MD5 "HMAC-MD5.SIG-ALG.REG.INT"
+
+#define NS_TSIG_ERROR_NO_TSIG -10
+#define NS_TSIG_ERROR_NO_SPACE -11
+#define NS_TSIG_ERROR_FORMERR -12
+
+/*%
+ * Currently defined type values for resources and queries.
+ */
+typedef enum __ns_type {
+	ns_t_invalid = 0,	/*%< Cookie. */
+	ns_t_a = 1,		/*%< Host address. */
+	ns_t_ns = 2,		/*%< Authoritative server. */
+	ns_t_md = 3,		/*%< Mail destination. */
+	ns_t_mf = 4,		/*%< Mail forwarder. */
+	ns_t_cname = 5,		/*%< Canonical name. */
+	ns_t_soa = 6,		/*%< Start of authority zone. */
+	ns_t_mb = 7,		/*%< Mailbox domain name. */
+	ns_t_mg = 8,		/*%< Mail group member. */
+	ns_t_mr = 9,		/*%< Mail rename name. */
+	ns_t_null = 10,		/*%< Null resource record. */
+	ns_t_wks = 11,		/*%< Well known service. */
+	ns_t_ptr = 12,		/*%< Domain name pointer. */
+	ns_t_hinfo = 13,	/*%< Host information. */
+	ns_t_minfo = 14,	/*%< Mailbox information. */
+	ns_t_mx = 15,		/*%< Mail routing information. */
+	ns_t_txt = 16,		/*%< Text strings. */
+	ns_t_rp = 17,		/*%< Responsible person. */
+	ns_t_afsdb = 18,	/*%< AFS cell database. */
+	ns_t_x25 = 19,		/*%< X_25 calling address. */
+	ns_t_isdn = 20,		/*%< ISDN calling address. */
+	ns_t_rt = 21,		/*%< Router. */
+	ns_t_nsap = 22,		/*%< NSAP address. */
+	ns_t_nsap_ptr = 23,	/*%< Reverse NSAP lookup (deprecated). */
+	ns_t_sig = 24,		/*%< Security signature. */
+	ns_t_key = 25,		/*%< Security key. */
+	ns_t_px = 26,		/*%< X.400 mail mapping. */
+	ns_t_gpos = 27,		/*%< Geographical position (withdrawn). */
+	ns_t_aaaa = 28,		/*%< Ip6 Address. */
+	ns_t_loc = 29,		/*%< Location Information. */
+	ns_t_nxt = 30,		/*%< Next domain (security). */
+	ns_t_eid = 31,		/*%< Endpoint identifier. */
+	ns_t_nimloc = 32,	/*%< Nimrod Locator. */
+	ns_t_srv = 33,		/*%< Server Selection. */
+	ns_t_atma = 34,		/*%< ATM Address */
+	ns_t_naptr = 35,	/*%< Naming Authority PoinTeR */
+	ns_t_kx = 36,		/*%< Key Exchange */
+	ns_t_cert = 37,		/*%< Certification record */
+	ns_t_a6 = 38,		/*%< IPv6 address (deprecates AAAA) */
+	ns_t_dname = 39,	/*%< Non-terminal DNAME (for IPv6) */
+	ns_t_sink = 40,		/*%< Kitchen sink (experimentatl) */
+	ns_t_opt = 41,		/*%< EDNS0 option (meta-RR) */
+	ns_t_apl = 42,		/*%< Address prefix list (RFC3123) */
+	ns_t_tkey = 249,	/*%< Transaction key */
+	ns_t_tsig = 250,	/*%< Transaction signature. */
+	ns_t_ixfr = 251,	/*%< Incremental zone transfer. */
+	ns_t_axfr = 252,	/*%< Transfer zone of authority. */
+	ns_t_mailb = 253,	/*%< Transfer mailbox records. */
+	ns_t_maila = 254,	/*%< Transfer mail agent records. */
+	ns_t_any = 255,		/*%< Wildcard match. */
+	ns_t_zxfr = 256,	/*%< BIND-specific, nonstandard. */
+	ns_t_max = 65536
+} ns_type;
+
+/* Exclusively a QTYPE? (not also an RTYPE) */
+#define	ns_t_qt_p(t) (ns_t_xfr_p(t) || (t) == ns_t_any || \
+		      (t) == ns_t_mailb || (t) == ns_t_maila)
+/* Some kind of meta-RR? (not a QTYPE, but also not an RTYPE) */
+#define	ns_t_mrr_p(t) ((t) == ns_t_tsig || (t) == ns_t_opt)
+/* Exclusively an RTYPE? (not also a QTYPE or a meta-RR) */
+#define ns_t_rr_p(t) (!ns_t_qt_p(t) && !ns_t_mrr_p(t))
+#define ns_t_udp_p(t) ((t) != ns_t_axfr && (t) != ns_t_zxfr)
+#define ns_t_xfr_p(t) ((t) == ns_t_axfr || (t) == ns_t_ixfr || \
+		       (t) == ns_t_zxfr)
+
+/*%
+ * Values for class field
+ */
+typedef enum __ns_class {
+	ns_c_invalid = 0,	/*%< Cookie. */
+	ns_c_in = 1,		/*%< Internet. */
+	ns_c_2 = 2,		/*%< unallocated/unsupported. */
+	ns_c_chaos = 3,		/*%< MIT Chaos-net. */
+	ns_c_hs = 4,		/*%< MIT Hesiod. */
+	/* Query class values which do not appear in resource records */
+	ns_c_none = 254,	/*%< for prereq. sections in update requests */
+	ns_c_any = 255,		/*%< Wildcard match. */
+	ns_c_max = 65536
+} ns_class;
+
+/* DNSSEC constants. */
+
+typedef enum __ns_key_types {
+	ns_kt_rsa = 1,		/*%< key type RSA/MD5 */
+	ns_kt_dh  = 2,		/*%< Diffie Hellman */
+	ns_kt_dsa = 3,		/*%< Digital Signature Standard (MANDATORY) */
+	ns_kt_private = 254	/*%< Private key type starts with OID */
+} ns_key_types;
+
+typedef enum __ns_cert_types {
+	cert_t_pkix = 1,	/*%< PKIX (X.509v3) */
+	cert_t_spki = 2,	/*%< SPKI */
+	cert_t_pgp  = 3,	/*%< PGP */
+	cert_t_url  = 253,	/*%< URL private type */
+	cert_t_oid  = 254	/*%< OID private type */
+} ns_cert_types;
+
+/* Flags field of the KEY RR rdata. */
+#define	NS_KEY_TYPEMASK		0xC000	/*%< Mask for "type" bits */
+#define	NS_KEY_TYPE_AUTH_CONF	0x0000	/*%< Key usable for both */
+#define	NS_KEY_TYPE_CONF_ONLY	0x8000	/*%< Key usable for confidentiality */
+#define	NS_KEY_TYPE_AUTH_ONLY	0x4000	/*%< Key usable for authentication */
+#define	NS_KEY_TYPE_NO_KEY	0xC000	/*%< No key usable for either; no key */
+/* The type bits can also be interpreted independently, as single bits: */
+#define	NS_KEY_NO_AUTH		0x8000	/*%< Key unusable for authentication */
+#define	NS_KEY_NO_CONF		0x4000	/*%< Key unusable for confidentiality */
+#define	NS_KEY_RESERVED2	0x2000	/* Security is *mandatory* if bit=0 */
+#define	NS_KEY_EXTENDED_FLAGS	0x1000	/*%< reserved - must be zero */
+#define	NS_KEY_RESERVED4	0x0800  /*%< reserved - must be zero */
+#define	NS_KEY_RESERVED5	0x0400  /*%< reserved - must be zero */
+#define	NS_KEY_NAME_TYPE	0x0300	/*%< these bits determine the type */
+#define	NS_KEY_NAME_USER	0x0000	/*%< key is assoc. with user */
+#define	NS_KEY_NAME_ENTITY	0x0200	/*%< key is assoc. with entity eg host */
+#define	NS_KEY_NAME_ZONE	0x0100	/*%< key is zone key */
+#define	NS_KEY_NAME_RESERVED	0x0300	/*%< reserved meaning */
+#define	NS_KEY_RESERVED8	0x0080  /*%< reserved - must be zero */
+#define	NS_KEY_RESERVED9	0x0040  /*%< reserved - must be zero */
+#define	NS_KEY_RESERVED10	0x0020  /*%< reserved - must be zero */
+#define	NS_KEY_RESERVED11	0x0010  /*%< reserved - must be zero */
+#define	NS_KEY_SIGNATORYMASK	0x000F	/*%< key can sign RR's of same name */
+#define	NS_KEY_RESERVED_BITMASK ( NS_KEY_RESERVED2 | \
+				  NS_KEY_RESERVED4 | \
+				  NS_KEY_RESERVED5 | \
+				  NS_KEY_RESERVED8 | \
+				  NS_KEY_RESERVED9 | \
+				  NS_KEY_RESERVED10 | \
+				  NS_KEY_RESERVED11 )
+#define NS_KEY_RESERVED_BITMASK2 0xFFFF /*%< no bits defined here */
+/* The Algorithm field of the KEY and SIG RR's is an integer, {1..254} */
+#define	NS_ALG_MD5RSA		1	/*%< MD5 with RSA */
+#define	NS_ALG_DH               2	/*%< Diffie Hellman KEY */
+#define	NS_ALG_DSA              3	/*%< DSA KEY */
+#define	NS_ALG_DSS              NS_ALG_DSA
+#define	NS_ALG_EXPIRE_ONLY	253	/*%< No alg, no security */
+#define	NS_ALG_PRIVATE_OID	254	/*%< Key begins with OID giving alg */
+/* Protocol values  */
+/* value 0 is reserved */
+#define NS_KEY_PROT_TLS         1
+#define NS_KEY_PROT_EMAIL       2
+#define NS_KEY_PROT_DNSSEC      3
+#define NS_KEY_PROT_IPSEC       4
+#define NS_KEY_PROT_ANY		255
+
+/* Signatures */
+#define	NS_MD5RSA_MIN_BITS	 512	/*%< Size of a mod or exp in bits */
+#define	NS_MD5RSA_MAX_BITS	4096
+	/* Total of binary mod and exp */
+#define	NS_MD5RSA_MAX_BYTES	((NS_MD5RSA_MAX_BITS+7/8)*2+3)
+	/* Max length of text sig block */
+#define	NS_MD5RSA_MAX_BASE64	(((NS_MD5RSA_MAX_BYTES+2)/3)*4)
+#define NS_MD5RSA_MIN_SIZE	((NS_MD5RSA_MIN_BITS+7)/8)
+#define NS_MD5RSA_MAX_SIZE	((NS_MD5RSA_MAX_BITS+7)/8)
+
+#define NS_DSA_SIG_SIZE         41
+#define NS_DSA_MIN_SIZE         213
+#define NS_DSA_MAX_BYTES        405
+
+/* Offsets into SIG record rdata to find various values */
+#define	NS_SIG_TYPE	0	/*%< Type flags */
+#define	NS_SIG_ALG	2	/*%< Algorithm */
+#define	NS_SIG_LABELS	3	/*%< How many labels in name */
+#define	NS_SIG_OTTL	4	/*%< Original TTL */
+#define	NS_SIG_EXPIR	8	/*%< Expiration time */
+#define	NS_SIG_SIGNED	12	/*%< Signature time */
+#define	NS_SIG_FOOT	16	/*%< Key footprint */
+#define	NS_SIG_SIGNER	18	/*%< Domain name of who signed it */
+/* How RR types are represented as bit-flags in NXT records */
+#define	NS_NXT_BITS 8
+#define	NS_NXT_BIT_SET(  n,p) (p[(n)/NS_NXT_BITS] |=  (0x80>>((n)%NS_NXT_BITS)))
+#define	NS_NXT_BIT_CLEAR(n,p) (p[(n)/NS_NXT_BITS] &= ~(0x80>>((n)%NS_NXT_BITS)))
+#define	NS_NXT_BIT_ISSET(n,p) (p[(n)/NS_NXT_BITS] &   (0x80>>((n)%NS_NXT_BITS)))
+#define NS_NXT_MAX 127
+
+/*%
+ * EDNS0 extended flags and option codes, host order.
+ */
+#define NS_OPT_DNSSEC_OK	0x8000U
+#define NS_OPT_NSID             3
+
+/*%
+ * Inline versions of get/put short/long.  Pointer is advanced.
+ */
+#define NS_GET16(s, cp) do { \
+	register const u_char *t_cp = (const u_char *)(cp); \
+	(s) = ((u_int16_t)t_cp[0] << 8) \
+	    | ((u_int16_t)t_cp[1]) \
+	    ; \
+	(cp) += NS_INT16SZ; \
+} while (0)
+
+#define NS_GET32(l, cp) do { \
+	register const u_char *t_cp = (const u_char *)(cp); \
+	(l) = ((u_int32_t)t_cp[0] << 24) \
+	    | ((u_int32_t)t_cp[1] << 16) \
+	    | ((u_int32_t)t_cp[2] << 8) \
+	    | ((u_int32_t)t_cp[3]) \
+	    ; \
+	(cp) += NS_INT32SZ; \
+} while (0)
+
+#define NS_PUT16(s, cp) do { \
+	register u_int16_t t_s = (u_int16_t)(s); \
+	register u_char *t_cp = (u_char *)(cp); \
+	*t_cp++ = t_s >> 8; \
+	*t_cp   = t_s; \
+	(cp) += NS_INT16SZ; \
+} while (0)
+
+#define NS_PUT32(l, cp) do { \
+	register u_int32_t t_l = (u_int32_t)(l); \
+	register u_char *t_cp = (u_char *)(cp); \
+	*t_cp++ = t_l >> 24; \
+	*t_cp++ = t_l >> 16; \
+	*t_cp++ = t_l >> 8; \
+	*t_cp   = t_l; \
+	(cp) += NS_INT32SZ; \
+} while (0)
+
+/*%
+ * ANSI C identifier hiding for bind's lib/nameser.
+ */
+#define	ns_msg_getflag		__ns_msg_getflag
+#define ns_get16		__ns_get16
+#define ns_get32		__ns_get32
+#define ns_put16		__ns_put16
+#define ns_put32		__ns_put32
+#define ns_initparse		__ns_initparse
+#define ns_skiprr		__ns_skiprr
+#define ns_parserr		__ns_parserr
+#define	ns_sprintrr		__ns_sprintrr
+#define	ns_sprintrrf		__ns_sprintrrf
+#define	ns_format_ttl		__ns_format_ttl
+#define	ns_parse_ttl		__ns_parse_ttl
+#if 0
+#define ns_datetosecs		__ns_datetosecs
+#endif
+#define	ns_name_ntol		__ns_name_ntol
+#define	ns_name_ntop		__ns_name_ntop
+#define	ns_name_pton		__ns_name_pton
+#define	ns_name_unpack		__ns_name_unpack
+#define	ns_name_pack		__ns_name_pack
+#define	ns_name_compress	__ns_name_compress
+#define	ns_name_uncompress	__ns_name_uncompress
+#define	ns_name_skip		__ns_name_skip
+#define	ns_name_rollback	__ns_name_rollback
+#if 0
+#define	ns_sign			__ns_sign
+#define	ns_sign2		__ns_sign2
+#define	ns_sign_tcp		__ns_sign_tcp
+#define	ns_sign_tcp2		__ns_sign_tcp2
+#define	ns_sign_tcp_init	__ns_sign_tcp_init
+#define ns_find_tsig		__ns_find_tsig
+#define	ns_verify		__ns_verify
+#define	ns_verify_tcp		__ns_verify_tcp
+#define	ns_verify_tcp_init	__ns_verify_tcp_init
+#endif
+#define	ns_samedomain		__ns_samedomain
+#if 0
+#define	ns_subdomain		__ns_subdomain
+#endif
+#define	ns_makecanon		__ns_makecanon
+#define	ns_samename		__ns_samename
+
+__BEGIN_DECLS
+int		ns_msg_getflag(ns_msg, int);
+u_int		ns_get16(const u_char *);
+u_long		ns_get32(const u_char *);
+void		ns_put16(u_int, u_char *);
+void		ns_put32(u_long, u_char *);
+int		ns_initparse(const u_char *, int, ns_msg *);
+int		ns_skiprr(const u_char *, const u_char *, ns_sect, int);
+int		ns_parserr(ns_msg *, ns_sect, int, ns_rr *);
+int		ns_sprintrr(const ns_msg *, const ns_rr *,
+			    const char *, const char *, char *, size_t);
+int		ns_sprintrrf(const u_char *, size_t, const char *,
+			     ns_class, ns_type, u_long, const u_char *,
+			     size_t, const char *, const char *,
+			     char *, size_t);
+int		ns_format_ttl(u_long, char *, size_t);
+int		ns_parse_ttl(const char *, u_long *);
+#if 0
+u_int32_t	ns_datetosecs(const char *cp, int *errp);
+#endif
+int		ns_name_ntol(const u_char *, u_char *, size_t);
+int		ns_name_ntop(const u_char *, char *, size_t);
+int		ns_name_pton(const char *, u_char *, size_t);
+int		ns_name_unpack(const u_char *, const u_char *,
+			       const u_char *, u_char *, size_t);
+int		ns_name_pack(const u_char *, u_char *, int,
+			     const u_char **, const u_char **);
+int		ns_name_uncompress(const u_char *, const u_char *,
+				   const u_char *, char *, size_t);
+int		ns_name_compress(const char *, u_char *, size_t,
+				 const u_char **, const u_char **);
+int		ns_name_skip(const u_char **, const u_char *);
+void		ns_name_rollback(const u_char *, const u_char **,
+				 const u_char **);
+#if 0
+int		ns_sign(u_char *, int *, int, int, void *,
+			const u_char *, int, u_char *, int *, time_t);
+int		ns_sign2(u_char *, int *, int, int, void *,
+			 const u_char *, int, u_char *, int *, time_t,
+			 u_char **, u_char **);
+int		ns_sign_tcp(u_char *, int *, int, int,
+			    ns_tcp_tsig_state *, int);
+int		ns_sign_tcp2(u_char *, int *, int, int,
+			     ns_tcp_tsig_state *, int,
+			     u_char **, u_char **);
+int		ns_sign_tcp_init(void *, const u_char *, int,
+				 ns_tcp_tsig_state *);
+u_char		*ns_find_tsig(u_char *, u_char *);
+int		ns_verify(u_char *, int *, void *,
+			  const u_char *, int, u_char *, int *,
+			  time_t *, int);
+int		ns_verify_tcp(u_char *, int *, ns_tcp_tsig_state *, int);
+int		ns_verify_tcp_init(void *, const u_char *, int,
+				   ns_tcp_tsig_state *);
+#endif
+int		ns_samedomain(const char *, const char *);
+#if 0
+int		ns_subdomain(const char *, const char *);
+#endif
+int		ns_makecanon(const char *, char *, size_t);
+int		ns_samename(const char *, const char *);
+__END_DECLS
+
+#ifdef BIND_4_COMPAT
+#include <arpa/nameser_compat.h>
+#endif
+
+#endif /* !_ARPA_NAMESER_H_ */
+/*! \file */
diff --git a/freebsd-userspace/include/arpa/nameser_compat.h b/freebsd-userspace/include/arpa/nameser_compat.h
new file mode 100644
index 0000000..64d05f8
--- /dev/null
+++ b/freebsd-userspace/include/arpa/nameser_compat.h
@@ -0,0 +1,203 @@
+/* Copyright (c) 1983, 1989
+ *    The Regents of the University of California.  All rights reserved.
+ * 
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ * 	This product includes software developed by the University of
+ * 	California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ * 
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON 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 ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+/*%
+ *      from nameser.h	8.1 (Berkeley) 6/2/93
+ *	$Id: nameser_compat.h,v 1.5.18.3 2006/05/19 02:36:00 marka Exp $
+ * $FreeBSD$
+ */
+
+#ifndef _ARPA_NAMESER_COMPAT_
+#define	_ARPA_NAMESER_COMPAT_
+
+#define	__BIND		19950621	/*%< (DEAD) interface version stamp. */
+
+#include <machine/endian.h>
+
+#if !defined(_BYTE_ORDER) || \
+    (_BYTE_ORDER != _BIG_ENDIAN && _BYTE_ORDER != _LITTLE_ENDIAN && \
+    _BYTE_ORDER != _PDP_ENDIAN)
+	/* you must determine what the correct bit order is for
+	 * your compiler - the next line is an intentional error
+	 * which will force your compiles to bomb until you fix
+	 * the above macros.
+	 */
+#error "Undefined or invalid _BYTE_ORDER";
+#endif
+
+/*%
+ * Structure for query header.  The order of the fields is machine- and
+ * compiler-dependent, depending on the byte/bit order and the layout
+ * of bit fields.  We use bit fields only in int variables, as this
+ * is all ANSI requires.  This requires a somewhat confusing rearrangement.
+ */
+
+typedef struct {
+	unsigned	id :16;		/*%< query identification number */
+#if _BYTE_ORDER == _BIG_ENDIAN
+			/* fields in third byte */
+	unsigned	qr: 1;		/*%< response flag */
+	unsigned	opcode: 4;	/*%< purpose of message */
+	unsigned	aa: 1;		/*%< authoritive answer */
+	unsigned	tc: 1;		/*%< truncated message */
+	unsigned	rd: 1;		/*%< recursion desired */
+			/* fields in fourth byte */
+	unsigned	ra: 1;		/*%< recursion available */
+	unsigned	unused :1;	/*%< unused bits (MBZ as of 4.9.3a3) */
+	unsigned	ad: 1;		/*%< authentic data from named */
+	unsigned	cd: 1;		/*%< checking disabled by resolver */
+	unsigned	rcode :4;	/*%< response code */
+#endif
+#if _BYTE_ORDER == _LITTLE_ENDIAN || _BYTE_ORDER == _PDP_ENDIAN
+			/* fields in third byte */
+	unsigned	rd :1;		/*%< recursion desired */
+	unsigned	tc :1;		/*%< truncated message */
+	unsigned	aa :1;		/*%< authoritive answer */
+	unsigned	opcode :4;	/*%< purpose of message */
+	unsigned	qr :1;		/*%< response flag */
+			/* fields in fourth byte */
+	unsigned	rcode :4;	/*%< response code */
+	unsigned	cd: 1;		/*%< checking disabled by resolver */
+	unsigned	ad: 1;		/*%< authentic data from named */
+	unsigned	unused :1;	/*%< unused bits (MBZ as of 4.9.3a3) */
+	unsigned	ra :1;		/*%< recursion available */
+#endif
+			/* remaining bytes */
+	unsigned	qdcount :16;	/*%< number of question entries */
+	unsigned	ancount :16;	/*%< number of answer entries */
+	unsigned	nscount :16;	/*%< number of authority entries */
+	unsigned	arcount :16;	/*%< number of resource entries */
+} HEADER;
+
+#define PACKETSZ	NS_PACKETSZ
+#define MAXDNAME	NS_MAXDNAME
+#define MAXCDNAME	NS_MAXCDNAME
+#define MAXLABEL	NS_MAXLABEL
+#define	HFIXEDSZ	NS_HFIXEDSZ
+#define QFIXEDSZ	NS_QFIXEDSZ
+#define RRFIXEDSZ	NS_RRFIXEDSZ
+#define	INT32SZ		NS_INT32SZ
+#define	INT16SZ		NS_INT16SZ
+#define	INT8SZ		NS_INT8SZ
+#define	INADDRSZ	NS_INADDRSZ
+#define	IN6ADDRSZ	NS_IN6ADDRSZ
+#define	INDIR_MASK	NS_CMPRSFLGS
+#define NAMESERVER_PORT	NS_DEFAULTPORT
+
+#define S_ZONE		ns_s_zn
+#define S_PREREQ	ns_s_pr
+#define S_UPDATE	ns_s_ud
+#define S_ADDT		ns_s_ar
+
+#define QUERY		ns_o_query
+#define IQUERY		ns_o_iquery
+#define STATUS		ns_o_status
+#define	NS_NOTIFY_OP	ns_o_notify
+#define	NS_UPDATE_OP	ns_o_update
+
+#define NOERROR		ns_r_noerror
+#define FORMERR		ns_r_formerr
+#define SERVFAIL	ns_r_servfail
+#define NXDOMAIN	ns_r_nxdomain
+#define NOTIMP		ns_r_notimpl
+#define REFUSED		ns_r_refused
+#define YXDOMAIN	ns_r_yxdomain
+#define YXRRSET		ns_r_yxrrset
+#define NXRRSET		ns_r_nxrrset
+#define NOTAUTH		ns_r_notauth
+#define NOTZONE		ns_r_notzone
+/*#define BADSIG		ns_r_badsig*/
+/*#define BADKEY		ns_r_badkey*/
+/*#define BADTIME		ns_r_badtime*/
+
+
+#define DELETE		ns_uop_delete
+#define ADD		ns_uop_add
+
+#define T_A		ns_t_a
+#define T_NS		ns_t_ns
+#define T_MD		ns_t_md
+#define T_MF		ns_t_mf
+#define T_CNAME		ns_t_cname
+#define T_SOA		ns_t_soa
+#define T_MB		ns_t_mb
+#define T_MG		ns_t_mg
+#define T_MR		ns_t_mr
+#define T_NULL		ns_t_null
+#define T_WKS		ns_t_wks
+#define T_PTR		ns_t_ptr
+#define T_HINFO		ns_t_hinfo
+#define T_MINFO		ns_t_minfo
+#define T_MX		ns_t_mx
+#define T_TXT		ns_t_txt
+#define	T_RP		ns_t_rp
+#define T_AFSDB		ns_t_afsdb
+#define T_X25		ns_t_x25
+#define T_ISDN		ns_t_isdn
+#define T_RT		ns_t_rt
+#define T_NSAP		ns_t_nsap
+#define T_NSAP_PTR	ns_t_nsap_ptr
+#define	T_SIG		ns_t_sig
+#define	T_KEY		ns_t_key
+#define	T_PX		ns_t_px
+#define	T_GPOS		ns_t_gpos
+#define	T_AAAA		ns_t_aaaa
+#define	T_LOC		ns_t_loc
+#define	T_NXT		ns_t_nxt
+#define	T_EID		ns_t_eid
+#define	T_NIMLOC	ns_t_nimloc
+#define	T_SRV		ns_t_srv
+#define T_ATMA		ns_t_atma
+#define T_NAPTR		ns_t_naptr
+#define T_A6		ns_t_a6
+#define T_OPT		ns_t_opt
+#define	T_TSIG		ns_t_tsig
+#define	T_IXFR		ns_t_ixfr
+#define T_AXFR		ns_t_axfr
+#define T_MAILB		ns_t_mailb
+#define T_MAILA		ns_t_maila
+#define T_ANY		ns_t_any
+
+#define C_IN		ns_c_in
+#define C_CHAOS		ns_c_chaos
+#define C_HS		ns_c_hs
+/* BIND_UPDATE */
+#define C_NONE		ns_c_none
+#define C_ANY		ns_c_any
+
+#define	GETSHORT		NS_GET16
+#define	GETLONG			NS_GET32
+#define	PUTSHORT		NS_PUT16
+#define	PUTLONG			NS_PUT32
+
+#endif /* _ARPA_NAMESER_COMPAT_ */
+/*! \file */
diff --git a/freebsd-userspace/include/db.h b/freebsd-userspace/include/db.h
new file mode 100644
index 0000000..e845fa8
--- /dev/null
+++ b/freebsd-userspace/include/db.h
@@ -0,0 +1,221 @@
+/*-
+ * Copyright (c) 1990, 1993, 1994
+ *	The Regents of the University of California.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *	This product includes software developed by the University of
+ *	California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON 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 ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ *	@(#)db.h	8.7 (Berkeley) 6/16/94
+ * $FreeBSD$
+ */
+
+#ifndef _DB_H_
+#define	_DB_H_
+
+#include <sys/types.h>
+#include <sys/cdefs.h>
+
+#include <limits.h>
+
+#define	RET_ERROR	-1		/* Return values. */
+#define	RET_SUCCESS	 0
+#define	RET_SPECIAL	 1
+
+#define	MAX_PAGE_NUMBER	0xffffffff	/* >= # of pages in a file */
+typedef uint32_t	pgno_t;
+#define	MAX_PAGE_OFFSET	65535		/* >= # of bytes in a page */
+typedef uint16_t	indx_t;
+#define	MAX_REC_NUMBER	0xffffffff	/* >= # of records in a tree */
+typedef uint32_t	recno_t;
+
+/* Key/data structure -- a Data-Base Thang. */
+typedef struct {
+	void	*data;			/* data */
+	size_t	 size;			/* data length */
+} DBT;
+
+/* Routine flags. */
+#define	R_CURSOR	1		/* del, put, seq */
+#define	__R_UNUSED	2		/* UNUSED */
+#define	R_FIRST		3		/* seq */
+#define	R_IAFTER	4		/* put (RECNO) */
+#define	R_IBEFORE	5		/* put (RECNO) */
+#define	R_LAST		6		/* seq (BTREE, RECNO) */
+#define	R_NEXT		7		/* seq */
+#define	R_NOOVERWRITE	8		/* put */
+#define	R_PREV		9		/* seq (BTREE, RECNO) */
+#define	R_SETCURSOR	10		/* put (RECNO) */
+#define	R_RECNOSYNC	11		/* sync (RECNO) */
+
+typedef enum { DB_BTREE, DB_HASH, DB_RECNO } DBTYPE;
+
+/*
+ * !!!
+ * The following flags are included in the dbopen(3) call as part of the
+ * open(2) flags.  In order to avoid conflicts with the open flags, start
+ * at the top of the 16 or 32-bit number space and work our way down.  If
+ * the open flags were significantly expanded in the future, it could be
+ * a problem.  Wish I'd left another flags word in the dbopen call.
+ *
+ * !!!
+ * None of this stuff is implemented yet.  The only reason that it's here
+ * is so that the access methods can skip copying the key/data pair when
+ * the DB_LOCK flag isn't set.
+ */
+#if UINT_MAX > 65535
+#define	DB_LOCK		0x20000000	/* Do locking. */
+#define	DB_SHMEM	0x40000000	/* Use shared memory. */
+#define	DB_TXN		0x80000000	/* Do transactions. */
+#else
+#define	DB_LOCK		    0x2000	/* Do locking. */
+#define	DB_SHMEM	    0x4000	/* Use shared memory. */
+#define	DB_TXN		    0x8000	/* Do transactions. */
+#endif
+
+/* Access method description structure. */
+typedef struct __db {
+	DBTYPE type;			/* Underlying db type. */
+	int (*close)(struct __db *);
+	int (*del)(const struct __db *, const DBT *, unsigned int);
+	int (*get)(const struct __db *, const DBT *, DBT *, unsigned int);
+	int (*put)(const struct __db *, DBT *, const DBT *, unsigned int);
+	int (*seq)(const struct __db *, DBT *, DBT *, unsigned int);
+	int (*sync)(const struct __db *, unsigned int);
+	void *internal;			/* Access method private. */
+	int (*fd)(const struct __db *);
+} DB;
+
+#define	BTREEMAGIC	0x053162
+#define	BTREEVERSION	3
+
+/* Structure used to pass parameters to the btree routines. */
+typedef struct {
+#define	R_DUP		0x01	/* duplicate keys */
+	unsigned long	flags;
+	unsigned int	cachesize;	/* bytes to cache */
+	int		maxkeypage;	/* maximum keys per page */
+	int		minkeypage;	/* minimum keys per page */
+	unsigned int	psize;		/* page size */
+	int		(*compare)	/* comparison function */
+			    (const DBT *, const DBT *);
+	size_t		(*prefix)	/* prefix function */
+			    (const DBT *, const DBT *);
+	int		lorder;		/* byte order */
+} BTREEINFO;
+
+#define	HASHMAGIC	0x061561
+#define	HASHVERSION	2
+
+/* Structure used to pass parameters to the hashing routines. */
+typedef struct {
+	unsigned int	bsize;		/* bucket size */
+	unsigned int	ffactor;	/* fill factor */
+	unsigned int	nelem;		/* number of elements */
+	unsigned int	cachesize;	/* bytes to cache */
+	uint32_t			/* hash function */
+		(*hash)(const void *, size_t);
+	int	lorder;		/* byte order */
+} HASHINFO;
+
+/* Structure used to pass parameters to the record routines. */
+typedef struct {
+#define	R_FIXEDLEN	0x01	/* fixed-length records */
+#define	R_NOKEY		0x02	/* key not required */
+#define	R_SNAPSHOT	0x04	/* snapshot the input */
+	unsigned long	flags;
+	unsigned int	cachesize; /* bytes to cache */
+	unsigned int	psize;	/* page size */
+	int		lorder;	/* byte order */
+	size_t		reclen;	/* record length (fixed-length records) */
+	unsigned char	bval;	/* delimiting byte (variable-length records */
+	char	*bfname;	/* btree file name */ 
+} RECNOINFO;
+
+#ifdef __DBINTERFACE_PRIVATE
+/*
+ * Little endian <==> big endian 32-bit swap macros.
+ *	M_32_SWAP	swap a memory location
+ *	P_32_SWAP	swap a referenced memory location
+ *	P_32_COPY	swap from one location to another
+ */
+#define	M_32_SWAP(a) {							\
+	uint32_t _tmp = a;						\
+	((char *)&a)[0] = ((char *)&_tmp)[3];				\
+	((char *)&a)[1] = ((char *)&_tmp)[2];				\
+	((char *)&a)[2] = ((char *)&_tmp)[1];				\
+	((char *)&a)[3] = ((char *)&_tmp)[0];				\
+}
+#define	P_32_SWAP(a) {							\
+	uint32_t _tmp = *(uint32_t *)a;					\
+	((char *)a)[0] = ((char *)&_tmp)[3];				\
+	((char *)a)[1] = ((char *)&_tmp)[2];				\
+	((char *)a)[2] = ((char *)&_tmp)[1];				\
+	((char *)a)[3] = ((char *)&_tmp)[0];				\
+}
+#define	P_32_COPY(a, b) {						\
+	((char *)&(b))[0] = ((char *)&(a))[3];				\
+	((char *)&(b))[1] = ((char *)&(a))[2];				\
+	((char *)&(b))[2] = ((char *)&(a))[1];				\
+	((char *)&(b))[3] = ((char *)&(a))[0];				\
+}
+
+/*
+ * Little endian <==> big endian 16-bit swap macros.
+ *	M_16_SWAP	swap a memory location
+ *	P_16_SWAP	swap a referenced memory location
+ *	P_16_COPY	swap from one location to another
+ */
+#define	M_16_SWAP(a) {							\
+	uint16_t _tmp = a;						\
+	((char *)&a)[0] = ((char *)&_tmp)[1];				\
+	((char *)&a)[1] = ((char *)&_tmp)[0];				\
+}
+#define	P_16_SWAP(a) {							\
+	uint16_t _tmp = *(uint16_t *)a;					\
+	((char *)a)[0] = ((char *)&_tmp)[1];				\
+	((char *)a)[1] = ((char *)&_tmp)[0];				\
+}
+#define	P_16_COPY(a, b) {						\
+	((char *)&(b))[0] = ((char *)&(a))[1];				\
+	((char *)&(b))[1] = ((char *)&(a))[0];				\
+}
+#endif
+
+__BEGIN_DECLS
+#if __BSD_VISIBLE
+DB *dbopen(const char *, int, int, DBTYPE, const void *);
+#endif
+
+#ifdef __DBINTERFACE_PRIVATE
+DB	*__bt_open(const char *, int, int, const BTREEINFO *, int);
+DB	*__hash_open(const char *, int, int, const HASHINFO *, int);
+DB	*__rec_open(const char *, int, int, const RECNOINFO *, int);
+void	 __dbpanic(DB *dbp);
+#endif
+__END_DECLS
+#endif /* !_DB_H_ */
diff --git a/freebsd-userspace/include/ifaddrs.h b/freebsd-userspace/include/ifaddrs.h
new file mode 100644
index 0000000..f0911a4
--- /dev/null
+++ b/freebsd-userspace/include/ifaddrs.h
@@ -0,0 +1,65 @@
+/*	$FreeBSD$	*/
+
+/*
+ * Copyright (c) 1995, 1999
+ *	Berkeley Software Design, Inc.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *
+ * THIS SOFTWARE IS PROVIDED BY Berkeley Software Design, Inc. ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL Berkeley Software Design, Inc. BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON 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 ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ *	BSDI ifaddrs.h,v 2.5 2000/02/23 14:51:59 dab Exp
+ */
+
+#ifndef	_IFADDRS_H_
+#define	_IFADDRS_H_
+
+struct ifaddrs {
+	struct ifaddrs  *ifa_next;
+	char		*ifa_name;
+	u_int		 ifa_flags;
+	struct sockaddr	*ifa_addr;
+	struct sockaddr	*ifa_netmask;
+	struct sockaddr	*ifa_dstaddr;
+	void		*ifa_data;
+};
+
+/*
+ * This may have been defined in <net/if.h>.  Note that if <net/if.h> is
+ * to be included it must be included before this header file.
+ */
+#ifndef	ifa_broadaddr
+#define	ifa_broadaddr	ifa_dstaddr	/* broadcast address interface */
+#endif
+
+struct ifmaddrs {
+	struct ifmaddrs	*ifma_next;
+	struct sockaddr	*ifma_name;
+	struct sockaddr	*ifma_addr;
+	struct sockaddr	*ifma_lladdr;
+};
+
+#include <sys/cdefs.h>
+
+__BEGIN_DECLS
+extern int getifaddrs(struct ifaddrs **);
+extern void freeifaddrs(struct ifaddrs *);
+extern int getifmaddrs(struct ifmaddrs **);
+extern void freeifmaddrs(struct ifmaddrs *);
+__END_DECLS
+
+#endif
diff --git a/freebsd-userspace/include/include/arpa/inet.h b/freebsd-userspace/include/include/arpa/inet.h
new file mode 100644
index 0000000..d39e161
--- /dev/null
+++ b/freebsd-userspace/include/include/arpa/inet.h
@@ -0,0 +1 @@
+#include <freebsd/include/arpa/inet.h>
diff --git a/freebsd-userspace/include/include/arpa/nameser.h b/freebsd-userspace/include/include/arpa/nameser.h
new file mode 100644
index 0000000..60a4cd9
--- /dev/null
+++ b/freebsd-userspace/include/include/arpa/nameser.h
@@ -0,0 +1 @@
+#include <freebsd/include/arpa/nameser.h>
diff --git a/freebsd-userspace/include/net/if.h b/freebsd-userspace/include/net/if.h
new file mode 100644
index 0000000..f8e296c
--- /dev/null
+++ b/freebsd-userspace/include/net/if.h
@@ -0,0 +1 @@
+#include <freebsd/net/if.h>
diff --git a/freebsd-userspace/include/net/if_dl.h b/freebsd-userspace/include/net/if_dl.h
new file mode 100644
index 0000000..1b8c752
--- /dev/null
+++ b/freebsd-userspace/include/net/if_dl.h
@@ -0,0 +1 @@
+#include <freebsd/net/if_dl.h>
diff --git a/freebsd-userspace/include/net/if_types.h b/freebsd-userspace/include/net/if_types.h
new file mode 100644
index 0000000..b0e6fb6
--- /dev/null
+++ b/freebsd-userspace/include/net/if_types.h
@@ -0,0 +1 @@
+#include <freebsd/net/if_types.h>
diff --git a/freebsd-userspace/include/net/in.h b/freebsd-userspace/include/net/in.h
new file mode 100644
index 0000000..224dbc6
--- /dev/null
+++ b/freebsd-userspace/include/net/in.h
@@ -0,0 +1 @@
+#include <freebsd/net/in.h>
diff --git a/freebsd-userspace/include/net/route.h b/freebsd-userspace/include/net/route.h
new file mode 100644
index 0000000..ac1e8aa
--- /dev/null
+++ b/freebsd-userspace/include/net/route.h
@@ -0,0 +1 @@
+#include <freebsd/net/route.h>
diff --git a/freebsd-userspace/include/netdb.h b/freebsd-userspace/include/netdb.h
new file mode 100644
index 0000000..c15b284
--- /dev/null
+++ b/freebsd-userspace/include/netdb.h
@@ -0,0 +1,297 @@
+/*-
+ * Copyright (c) 1980, 1983, 1988, 1993
+ *	The Regents of the University of California.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *	This product includes software developed by the University of
+ *	California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON 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 ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * -
+ * Portions Copyright (c) 1993 by Digital Equipment Corporation.
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies, and that
+ * the name of Digital Equipment Corporation not be used in advertising or
+ * publicity pertaining to distribution of the document or software without
+ * specific, written prior permission.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
+ * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
+ * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
+ * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
+ * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
+ * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
+ * SOFTWARE.
+ * -
+ * --Copyright--
+ */
+
+/*
+ *      @(#)netdb.h	8.1 (Berkeley) 6/2/93
+ *      From: Id: netdb.h,v 8.9 1996/11/19 08:39:29 vixie Exp $
+ * $FreeBSD$
+ */
+
+#ifndef _NETDB_H_
+#define _NETDB_H_
+
+#include <sys/cdefs.h>
+#include <sys/_types.h>
+
+#ifndef _SIZE_T_DECLARED
+typedef	__size_t	size_t;
+#define	_SIZE_T_DECLARED
+#endif
+
+#ifndef _SOCKLEN_T_DECLARED
+typedef	__socklen_t	socklen_t;
+#define	_SOCKLEN_T_DECLARED
+#endif
+
+#ifndef _UINT32_T_DECLARED
+typedef	__uint32_t	uint32_t;
+#define	_UINT32_T_DECLARED
+#endif
+
+#ifndef _PATH_HEQUIV
+# define	_PATH_HEQUIV	"/etc/hosts.equiv"
+#endif
+#define	_PATH_HOSTS	"/etc/hosts"
+#define	_PATH_NETWORKS	"/etc/networks"
+#define	_PATH_PROTOCOLS	"/etc/protocols"
+#define	_PATH_SERVICES	"/etc/services"
+#define	_PATH_SERVICES_DB "/var/db/services.db"
+
+#define	h_errno (*__h_errno())
+
+/*
+ * Structures returned by network data base library.  All addresses are
+ * supplied in host order, and returned in network order (suitable for
+ * use in system calls).
+ */
+struct hostent {
+	char	*h_name;	/* official name of host */
+	char	**h_aliases;	/* alias list */
+	int	h_addrtype;	/* host address type */
+	int	h_length;	/* length of address */
+	char	**h_addr_list;	/* list of addresses from name server */
+#define	h_addr	h_addr_list[0]	/* address, for backward compatibility */
+};
+
+struct netent {
+	char		*n_name;	/* official name of net */
+	char		**n_aliases;	/* alias list */
+	int		n_addrtype;	/* net address type */
+	uint32_t	n_net;		/* network # */
+};
+
+struct servent {
+	char	*s_name;	/* official service name */
+	char	**s_aliases;	/* alias list */
+	int	s_port;		/* port # */
+	char	*s_proto;	/* protocol to use */
+};
+
+struct protoent {
+	char	*p_name;	/* official protocol name */
+	char	**p_aliases;	/* alias list */
+	int	p_proto;	/* protocol # */
+};
+
+struct addrinfo {
+	int	ai_flags;	/* AI_PASSIVE, AI_CANONNAME, AI_NUMERICHOST */
+	int	ai_family;	/* PF_xxx */
+	int	ai_socktype;	/* SOCK_xxx */
+	int	ai_protocol;	/* 0 or IPPROTO_xxx for IPv4 and IPv6 */
+	socklen_t ai_addrlen;	/* length of ai_addr */
+	char	*ai_canonname;	/* canonical name for hostname */
+	struct	sockaddr *ai_addr;	/* binary address */
+	struct	addrinfo *ai_next;	/* next structure in linked list */
+};
+
+/*
+ * Error return codes from gethostbyname() and gethostbyaddr()
+ * (left in h_errno).
+ */
+
+#define	NETDB_INTERNAL	-1	/* see errno */
+#define	NETDB_SUCCESS	0	/* no problem */
+#define	HOST_NOT_FOUND	1 /* Authoritative Answer Host not found */
+#define	TRY_AGAIN	2 /* Non-Authoritative Host not found, or SERVERFAIL */
+#define	NO_RECOVERY	3 /* Non recoverable errors, FORMERR, REFUSED, NOTIMP */
+#define	NO_DATA		4 /* Valid name, no data record of requested type */
+#define	NO_ADDRESS	NO_DATA		/* no address, look for MX record */
+
+/*
+ * Error return codes from getaddrinfo()
+ */
+#if 0
+/* obsoleted */
+#define	EAI_ADDRFAMILY	 1	/* address family for hostname not supported */
+#endif
+#define	EAI_AGAIN	 2	/* temporary failure in name resolution */
+#define	EAI_BADFLAGS	 3	/* invalid value for ai_flags */
+#define	EAI_FAIL	 4	/* non-recoverable failure in name resolution */
+#define	EAI_FAMILY	 5	/* ai_family not supported */
+#define	EAI_MEMORY	 6	/* memory allocation failure */
+#if 0
+/* obsoleted */
+#define	EAI_NODATA	 7	/* no address associated with hostname */
+#endif
+#define	EAI_NONAME	 8	/* hostname nor servname provided, or not known */
+#define	EAI_SERVICE	 9	/* servname not supported for ai_socktype */
+#define	EAI_SOCKTYPE	10	/* ai_socktype not supported */
+#define	EAI_SYSTEM	11	/* system error returned in errno */
+#define	EAI_BADHINTS	12	/* invalid value for hints */
+#define	EAI_PROTOCOL	13	/* resolved protocol is unknown */
+#define	EAI_OVERFLOW	14	/* argument buffer overflow */
+#define	EAI_MAX		15
+
+/*
+ * Flag values for getaddrinfo()
+ */
+#define	AI_PASSIVE	0x00000001 /* get address to use bind() */
+#define	AI_CANONNAME	0x00000002 /* fill ai_canonname */
+#define	AI_NUMERICHOST	0x00000004 /* prevent host name resolution */
+#define	AI_NUMERICSERV	0x00000008 /* prevent service name resolution */
+/* valid flags for addrinfo (not a standard def, apps should not use it) */
+#define AI_MASK \
+    (AI_PASSIVE | AI_CANONNAME | AI_NUMERICHOST | AI_NUMERICSERV | \
+    AI_ADDRCONFIG)
+
+#define	AI_ALL		0x00000100 /* IPv6 and IPv4-mapped (with AI_V4MAPPED) */
+#define	AI_V4MAPPED_CFG	0x00000200 /* accept IPv4-mapped if kernel supports */
+#define	AI_ADDRCONFIG	0x00000400 /* only if any address is assigned */
+#define	AI_V4MAPPED	0x00000800 /* accept IPv4-mapped IPv6 address */
+/* special recommended flags for getipnodebyname */
+#define	AI_DEFAULT	(AI_V4MAPPED_CFG | AI_ADDRCONFIG)
+
+/*
+ * Constants for getnameinfo()
+ */
+#define	NI_MAXHOST	1025
+#define	NI_MAXSERV	32
+
+/*
+ * Flag values for getnameinfo()
+ */
+#define	NI_NOFQDN	0x00000001
+#define	NI_NUMERICHOST	0x00000002
+#define	NI_NAMEREQD	0x00000004
+#define	NI_NUMERICSERV	0x00000008
+#define	NI_DGRAM	0x00000010
+#if 0 /* obsolete */
+#define NI_WITHSCOPEID	0x00000020
+#endif
+
+/*
+ * Scope delimit character
+ */
+#define	SCOPE_DELIMITER	'%'
+
+__BEGIN_DECLS
+void		endhostent(void);
+void		endnetent(void);
+void		endprotoent(void);
+void		endservent(void);
+#if __BSD_VISIBLE || (__POSIX_VISIBLE && __POSIX_VISIBLE <= 200112)
+struct hostent	*gethostbyaddr(const void *, socklen_t, int);
+struct hostent	*gethostbyname(const char *);
+#endif
+struct hostent	*gethostent(void);
+struct netent	*getnetbyaddr(uint32_t, int);
+struct netent	*getnetbyname(const char *);
+struct netent	*getnetent(void);
+struct protoent	*getprotobyname(const char *);
+struct protoent	*getprotobynumber(int);
+struct protoent	*getprotoent(void);
+struct servent	*getservbyname(const char *, const char *);
+struct servent	*getservbyport(int, const char *);
+struct servent	*getservent(void);
+void		sethostent(int);
+/* void		sethostfile(const char *); */
+void		setnetent(int);
+void		setprotoent(int);
+int		getaddrinfo(const char *, const char *,
+			    const struct addrinfo *, struct addrinfo **);
+int		getnameinfo(const struct sockaddr *, socklen_t, char *,
+			    size_t, char *, size_t, int);
+void		freeaddrinfo(struct addrinfo *);
+const char	*gai_strerror(int);
+void		setservent(int);
+
+#if __BSD_VISIBLE
+void		endnetgrent(void);
+void		freehostent(struct hostent *);
+int		gethostbyaddr_r(const void *, socklen_t, int, struct hostent *,
+    char *, size_t, struct hostent **, int *);
+int		gethostbyname_r(const char *, struct hostent *, char *, size_t,
+    struct hostent **, int *);
+struct hostent	*gethostbyname2(const char *, int);
+int		gethostbyname2_r(const char *, int, struct hostent *, char *,
+    size_t, struct hostent **, int *);
+int		gethostent_r(struct hostent *, char *, size_t,
+    struct hostent **, int *);
+struct hostent	*getipnodebyaddr(const void *, size_t, int, int *);
+struct hostent	*getipnodebyname(const char *, int, int, int *);
+int		getnetbyaddr_r(uint32_t, int, struct netent *, char *, size_t,
+    struct netent**, int *);
+int		getnetbyname_r(const char *, struct netent *, char *, size_t,
+    struct netent **, int *);
+int		getnetent_r(struct netent *, char *, size_t, struct netent **,
+    int *);
+int		getnetgrent(char **, char **, char **);
+int		getprotobyname_r(const char *, struct protoent *, char *,
+    size_t, struct protoent **);
+int		getprotobynumber_r(int, struct protoent *, char *, size_t,
+    struct protoent **);
+int		getprotoent_r(struct protoent *, char *, size_t,
+    struct protoent **);
+int		getservbyname_r(const char *, const char *, struct servent *,
+    char *, size_t, struct servent **);
+int		getservbyport_r(int, const char *, struct servent *, char *,
+    size_t, struct servent **);
+int		getservent_r(struct servent *, char *, size_t,
+    struct servent **);
+void		herror(const char *);
+__const char	*hstrerror(int);
+int		innetgr(const char *, const char *, const char *, const char *);
+void		setnetgrent(const char *);
+#endif
+
+
+/*
+ * PRIVATE functions specific to the FreeBSD implementation
+ */
+
+/* DO NOT USE THESE, THEY ARE SUBJECT TO CHANGE AND ARE NOT PORTABLE!!! */
+int	* __h_errno(void);
+__END_DECLS
+
+#endif /* !_NETDB_H_ */
diff --git a/freebsd-userspace/include/netinet/in.h b/freebsd-userspace/include/netinet/in.h
new file mode 100644
index 0000000..14710ff
--- /dev/null
+++ b/freebsd-userspace/include/netinet/in.h
@@ -0,0 +1 @@
+#include <freebsd/netinet/in.h>
diff --git a/freebsd-userspace/include/nsswitch.h b/freebsd-userspace/include/nsswitch.h
new file mode 100644
index 0000000..8aea5ad
--- /dev/null
+++ b/freebsd-userspace/include/nsswitch.h
@@ -0,0 +1,251 @@
+/*	$NetBSD: nsswitch.h,v 1.6 1999/01/26 01:04:07 lukem Exp $	*/
+/*	$FreeBSD$ */
+
+/*-
+ * Copyright (c) 1997, 1998, 1999 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Luke Mewburn.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *        This product includes software developed by the NetBSD
+ *        Foundation, Inc. and its contributors.
+ * 4. Neither the name of The NetBSD Foundation nor the names of its
+ *    contributors may be used to endorse or promote products derived
+ *    from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON 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 ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef _NSSWITCH_H
+#define _NSSWITCH_H	1
+
+#include <sys/types.h>
+#include <stdarg.h>
+
+#define NSS_MODULE_INTERFACE_VERSION 1
+
+#ifndef _PATH_NS_CONF
+#define _PATH_NS_CONF	"/etc/nsswitch.conf"
+#endif
+
+/* NSS source actions */
+#define	NS_ACTION_CONTINUE	0	/* try the next source */
+#define	NS_ACTION_RETURN	1	/* look no further */
+
+#define	NS_SUCCESS	(1<<0)		/* entry was found */
+#define	NS_UNAVAIL	(1<<1)		/* source not responding, or corrupt */
+#define	NS_NOTFOUND	(1<<2)		/* source responded 'no such entry' */
+#define	NS_TRYAGAIN	(1<<3)		/* source busy, may respond to retry */
+#define NS_RETURN	(1<<4)		/* stop search, e.g. for ERANGE */
+#define NS_TERMINATE	(NS_SUCCESS|NS_RETURN) /* flags that end search */
+#define	NS_STATUSMASK	0x000000ff	/* bitmask to get the status flags */
+
+/*
+ * currently implemented sources
+ */
+#define NSSRC_FILES	"files"		/* local files */
+#define	NSSRC_DB	"db"		/* database */
+#define	NSSRC_DNS	"dns"		/* DNS; IN for hosts, HS for others */
+#define	NSSRC_NIS	"nis"		/* YP/NIS */
+#define	NSSRC_COMPAT	"compat"	/* passwd,group in YP compat mode */
+#define	NSSRC_CACHE	"cache"		/* nscd daemon */
+#define NSSRC_FALLBACK	"__fallback"	/* internal fallback source */
+
+/*
+ * currently implemented databases
+ */
+#define NSDB_HOSTS		"hosts"
+#define NSDB_GROUP		"group"
+#define NSDB_GROUP_COMPAT	"group_compat"
+#define NSDB_NETGROUP		"netgroup"
+#define NSDB_NETWORKS		"networks"
+#define NSDB_PASSWD		"passwd"
+#define NSDB_PASSWD_COMPAT	"passwd_compat"
+#define NSDB_SHELLS		"shells"
+#define NSDB_SERVICES		"services"
+#define NSDB_SERVICES_COMPAT	"services_compat"
+#define NSDB_SSH_HOSTKEYS	"ssh_hostkeys"
+#define NSDB_PROTOCOLS		"protocols"
+#define NSDB_RPC		"rpc"
+
+/*
+ * suggested databases to implement
+ */
+#define NSDB_ALIASES		"aliases"
+#define NSDB_AUTH		"auth"
+#define NSDB_AUTOMOUNT		"automount"
+#define NSDB_BOOTPARAMS		"bootparams"
+#define NSDB_ETHERS		"ethers"
+#define NSDB_EXPORTS		"exports"
+#define NSDB_NETMASKS		"netmasks"
+#define NSDB_PHONES		"phones"
+#define NSDB_PRINTCAP		"printcap"
+#define NSDB_REMOTE		"remote"
+#define NSDB_SENDMAILVARS	"sendmailvars"
+#define NSDB_TERMCAP		"termcap"
+#define NSDB_TTYS		"ttys"
+
+/*
+ * ns_dtab `method' function signature.
+ */ 
+typedef int (*nss_method)(void *_retval, void *_mdata, va_list _ap);
+
+/*
+ * Macro for generating method prototypes.
+ */
+#define NSS_METHOD_PROTOTYPE(method) \
+	int method(void *, void *, va_list)
+
+/*
+ * ns_dtab - `nsswitch dispatch table'
+ * Contains an entry for each source and the appropriate function to
+ * call.  ns_dtabs are used in the nsdispatch() API in order to allow
+ * the application to override built-in actions.
+ */
+typedef struct _ns_dtab {
+	const char	 *src;		/* Source this entry implements */
+	nss_method	  method;	/* Method to be called */
+	void		 *mdata;	/* Data passed to method */
+} ns_dtab;
+
+/*
+ * macros to help build an ns_dtab[]
+ */
+#define NS_FILES_CB(F,C)	{ NSSRC_FILES,	F,	C },
+#define NS_COMPAT_CB(F,C)	{ NSSRC_COMPAT,	F,	C },
+#define NS_FALLBACK_CB(F)	{ NSSRC_FALLBACK, F,	NULL },
+ 
+#ifdef HESIOD
+#   define NS_DNS_CB(F,C)	{ NSSRC_DNS,	F,	C },
+#else
+#   define NS_DNS_CB(F,C)
+#endif
+
+#ifdef YP
+#   define NS_NIS_CB(F,C)	{ NSSRC_NIS,	F,	C },
+#else
+#   define NS_NIS_CB(F,C)
+#endif
+
+/*
+ * ns_src - `nsswitch source'
+ * used by the nsparser routines to store a mapping between a source
+ * and its dispatch control flags for a given database.
+ */
+typedef struct _ns_src {
+	const char	*name;
+	u_int32_t	 flags;
+} ns_src;
+
+
+/*
+ * default sourcelist (if nsswitch.conf is missing, corrupt,
+ * or the requested database doesn't have an entry.
+ */
+extern const ns_src __nsdefaultsrc[];
+
+/*
+ * ns_mtab - NSS method table
+ * An NSS module provides a mapping from (database name, method name)
+ * tuples to the nss_method and associated data.
+ */
+typedef struct _ns_mtab {
+	const char	*database;
+	const char	*name;
+	nss_method	 method;
+	void		*mdata;
+} ns_mtab;
+
+/*
+ * NSS module de-registration, called at module unload.
+ */
+typedef void	 (*nss_module_unregister_fn)(ns_mtab *, unsigned int);
+
+/*
+ * NSS module registration, called at module load.
+ */
+typedef ns_mtab *(*nss_module_register_fn)(const char *, unsigned int *,
+		       nss_module_unregister_fn *);
+
+/* 
+ * Many NSS interfaces follow the getXXnam, getXXid, getXXent pattern.
+ * Developers are encouraged to use nss_lookup_type where approriate.
+ */
+enum nss_lookup_type {
+	nss_lt_name = 1,
+	nss_lt_id   = 2,
+	nss_lt_all  = 3
+};
+
+#ifdef _NS_PRIVATE
+/*
+ * private data structures for back-end nsswitch implementation
+ */
+
+/*
+ * ns_dbt - `nsswitch database thang'
+ * for each database in /etc/nsswitch.conf there is a ns_dbt, with its
+ * name and a list of ns_src's containing the source information.
+ */
+typedef struct _ns_dbt {
+	const char	*name;		/* name of database */
+	ns_src		*srclist;	/* list of sources */
+	int		 srclistsize;	/* size of srclist */
+} ns_dbt;
+
+/*
+ * ns_mod - NSS module
+ */
+typedef struct _ns_mod {
+	char		*name;		/* module name */
+	void		*handle;	/* handle from dlopen */
+	ns_mtab		*mtab;		/* method table */
+	unsigned int	 mtabsize;	/* count of entries in method table */
+	nss_module_unregister_fn unregister; /* called to unload module */
+} ns_mod;
+
+#endif /* _NS_PRIVATE */
+
+
+#include <sys/cdefs.h>
+
+__BEGIN_DECLS
+extern	int	nsdispatch(void *, const ns_dtab [], const char *,
+			   const char *, const ns_src [], ...);
+
+#ifdef _NS_PRIVATE
+extern	void		 _nsdbtaddsrc(ns_dbt *, const ns_src *);
+extern	void		 _nsdbtput(const ns_dbt *);
+extern	void		 _nsyyerror(const char *);
+extern	int		 _nsyylex(void);
+extern	int		 _nsyyparse(void);
+extern	int		 _nsyylineno;
+#ifdef _NSS_DEBUG
+extern	void		 _nsdbtdump(const ns_dbt *);
+#endif
+#endif /* _NS_PRIVATE */
+
+__END_DECLS
+
+#endif /* !_NSSWITCH_H */
diff --git a/freebsd-userspace/include/resolv.h b/freebsd-userspace/include/resolv.h
new file mode 100644
index 0000000..20f7954
--- /dev/null
+++ b/freebsd-userspace/include/resolv.h
@@ -0,0 +1,502 @@
+/*
+ * Copyright (c) 1983, 1987, 1989
+ *    The Regents of the University of California.  All rights reserved.
+ * 
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ * 	This product includes software developed by the University of
+ * 	California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ * 
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON 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 ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+/*
+ * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
+ * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
+ * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+/*%
+ *	@(#)resolv.h	8.1 (Berkeley) 6/2/93
+ *	$Id: resolv.h,v 1.19.18.4 2008/04/03 23:15:15 marka Exp $
+ * $FreeBSD$
+ */
+
+#ifndef _RESOLV_H_
+#define	_RESOLV_H_
+
+#include <sys/param.h>
+#include <sys/types.h>
+#include <sys/cdefs.h>
+#include <sys/socket.h>
+#include <stdio.h>
+#include <arpa/nameser.h>
+
+/*%
+ * Revision information.  This is the release date in YYYYMMDD format.
+ * It can change every day so the right thing to do with it is use it
+ * in preprocessor commands such as "#if (__RES > 19931104)".  Do not
+ * compare for equality; rather, use it to determine whether your resolver
+ * is new enough to contain a certain feature.
+ */
+
+#define	__RES	20030124
+
+/*%
+ * This used to be defined in res_query.c, now it's in herror.c.
+ * [XXX no it's not.  It's in irs/irs_data.c]
+ * It was
+ * never extern'd by any *.h file before it was placed here.  For thread
+ * aware programs, the last h_errno value set is stored in res->h_errno.
+ *
+ * XXX:	There doesn't seem to be a good reason for exposing RES_SET_H_ERRNO
+ *	(and __h_errno_set) to the public via <resolv.h>.
+ * XXX:	__h_errno_set is really part of IRS, not part of the resolver.
+ *	If somebody wants to build and use a resolver that doesn't use IRS,
+ *	what do they do?  Perhaps something like
+ *		#ifdef WANT_IRS
+ *		# define RES_SET_H_ERRNO(r,x) __h_errno_set(r,x)
+ *		#else
+ *		# define RES_SET_H_ERRNO(r,x) (h_errno = (r)->res_h_errno = (x))
+ *		#endif
+ */
+
+#define RES_SET_H_ERRNO(r,x) __h_errno_set(r,x)
+struct __res_state; /*%< forward */
+__BEGIN_DECLS
+void __h_errno_set(struct __res_state *, int);
+__END_DECLS
+
+/*%
+ * Resolver configuration file.
+ * Normally not present, but may contain the address of the
+ * initial name server(s) to query and the domain search list.
+ */
+
+#ifndef _PATH_RESCONF
+#define	_PATH_RESCONF        "/etc/resolv.conf"
+#endif
+
+typedef enum { res_goahead, res_nextns, res_modified, res_done, res_error }
+	res_sendhookact;
+
+typedef res_sendhookact (*res_send_qhook)(struct sockaddr * const *,
+					  const u_char **, int *,
+					  u_char *, int, int *);
+
+typedef res_sendhookact (*res_send_rhook)(const struct sockaddr *,
+					  const u_char *, int, u_char *,
+					  int, int *);
+
+struct res_sym {
+	int		number;	   /*%< Identifying number, like T_MX */
+	const char *	name;	   /*%< Its symbolic name, like "MX" */
+	const char *	humanname; /*%< Its fun name, like "mail exchanger" */
+};
+
+/*%
+ * Global defines and variables for resolver stub.
+ */
+#define	MAXNS			3	/*%< max # name servers we'll track */
+#define	MAXDFLSRCH		3	/*%< # default domain levels to try */
+#define	MAXDNSRCH		6	/*%< max # domains in search path */
+#define	LOCALDOMAINPARTS	2	/*%< min levels in name that is "local" */
+#define	RES_TIMEOUT		5	/*%< min. seconds between retries */
+#define	MAXRESOLVSORT		10	/*%< number of net to sort on */
+#define	RES_MAXNDOTS		15	/*%< should reflect bit field size */
+#define	RES_MAXRETRANS		30	/*%< only for resolv.conf/RES_OPTIONS */
+#define	RES_MAXRETRY		5	/*%< only for resolv.conf/RES_OPTIONS */
+#define	RES_DFLRETRY		2	/*%< Default #/tries. */
+#define	RES_MAXTIME		65535	/*%< Infinity, in milliseconds. */
+struct __res_state_ext;
+
+struct __res_state {
+	int	retrans;	 	/*%< retransmission time interval */
+	int	retry;			/*%< number of times to retransmit */
+	/*
+	 * XXX: If `sun' is defined, `options' and `pfcode' are
+	 * defined as u_int in original BIND9 distribution.  However,
+	 * it breaks binary backward compatibility against FreeBSD's
+	 * resolver.  So, we changed not to see `sun'.
+	 */
+#if defined(sun) && 0
+	u_int	options;		/*%< option flags - see below. */
+#else
+	u_long	options;		/*%< option flags - see below. */
+#endif
+	int	nscount;		/*%< number of name servers */
+	struct sockaddr_in
+		nsaddr_list[MAXNS];	/*%< address of name server */
+#define	nsaddr	nsaddr_list[0]		/*%< for backward compatibility */
+	u_short	id;			/*%< current message id */
+	char	*dnsrch[MAXDNSRCH+1];	/*%< components of domain to search */
+	char	defdname[256];		/*%< default domain (deprecated) */
+#if defined(sun) && 0
+	u_int	pfcode;			/*%< RES_PRF_ flags - see below. */
+#else
+	u_long	pfcode;			/*%< RES_PRF_ flags - see below. */
+#endif
+	unsigned ndots:4;		/*%< threshold for initial abs. query */
+	unsigned nsort:4;		/*%< number of elements in sort_list[] */
+	char	unused[3];
+	struct {
+		struct in_addr	addr;
+		u_int32_t	mask;
+	} sort_list[MAXRESOLVSORT];
+	res_send_qhook qhook;		/*%< query hook */
+	res_send_rhook rhook;		/*%< response hook */
+	int	res_h_errno;		/*%< last one set for this context */
+	int	_vcsock;		/*%< PRIVATE: for res_send VC i/o */
+	u_int	_flags;			/*%< PRIVATE: see below */
+	u_int	_pad;			/*%< make _u 64 bit aligned */
+	union {
+		/* On an 32-bit arch this means 512b total. */
+		char	pad[72 - 4*sizeof (int) - 2*sizeof (void *)];
+		struct {
+			u_int16_t		nscount;
+			u_int16_t		nstimes[MAXNS];	/*%< ms. */
+			int			nssocks[MAXNS];
+			struct __res_state_ext *ext;	/*%< extention for IPv6 */
+		} _ext;
+	} _u;
+};
+
+typedef struct __res_state *res_state;
+
+union res_sockaddr_union {
+	struct sockaddr_in	sin;
+#ifdef IN6ADDR_ANY_INIT
+	struct sockaddr_in6	sin6;
+#endif
+#ifdef ISC_ALIGN64
+	int64_t			__align64;	/*%< 64bit alignment */
+#else
+	int32_t			__align32;	/*%< 32bit alignment */
+#endif
+	char			__space[128];   /*%< max size */
+};
+
+/*%
+ * Resolver flags (used to be discrete per-module statics ints).
+ */
+#define	RES_F_VC	0x00000001	/*%< socket is TCP */
+#define	RES_F_CONN	0x00000002	/*%< socket is connected */
+#define	RES_F_EDNS0ERR	0x00000004	/*%< EDNS0 caused errors */
+#define	RES_F__UNUSED	0x00000008	/*%< (unused) */
+#define	RES_F_LASTMASK	0x000000F0	/*%< ordinal server of last res_nsend */
+#define	RES_F_LASTSHIFT	4		/*%< bit position of LASTMASK "flag" */
+#define	RES_GETLAST(res) (((res)._flags & RES_F_LASTMASK) >> RES_F_LASTSHIFT)
+
+/* res_findzonecut2() options */
+#define	RES_EXHAUSTIVE	0x00000001	/*%< always do all queries */
+#define	RES_IPV4ONLY	0x00000002	/*%< IPv4 only */
+#define	RES_IPV6ONLY	0x00000004	/*%< IPv6 only */
+
+/*%
+ * Resolver options (keep these in synch with res_debug.c, please)
+ */
+#define RES_INIT	0x00000001	/*%< address initialized */
+#define RES_DEBUG	0x00000002	/*%< print debug messages */
+#define RES_AAONLY	0x00000004	/*%< authoritative answers only (!IMPL)*/
+#define RES_USEVC	0x00000008	/*%< use virtual circuit */
+#define RES_PRIMARY	0x00000010	/*%< query primary server only (!IMPL) */
+#define RES_IGNTC	0x00000020	/*%< ignore truncation errors */
+#define RES_RECURSE	0x00000040	/*%< recursion desired */
+#define RES_DEFNAMES	0x00000080	/*%< use default domain name */
+#define RES_STAYOPEN	0x00000100	/*%< Keep TCP socket open */
+#define RES_DNSRCH	0x00000200	/*%< search up local domain tree */
+#define	RES_INSECURE1	0x00000400	/*%< type 1 security disabled */
+#define	RES_INSECURE2	0x00000800	/*%< type 2 security disabled */
+#define	RES_NOALIASES	0x00001000	/*%< shuts off HOSTALIASES feature */
+#define	RES_USE_INET6	0x00002000	/*%< use/map IPv6 in gethostbyname() */
+#define RES_ROTATE	0x00004000	/*%< rotate ns list after each query */
+#define	RES_NOCHECKNAME	0x00008000	/*%< do not check names for sanity. */
+#define	RES_KEEPTSIG	0x00010000	/*%< do not strip TSIG records */
+#define	RES_BLAST	0x00020000	/*%< blast all recursive servers */
+#define RES_NSID	0x00040000      /*%< request name server ID */
+#define RES_NOTLDQUERY	0x00100000	/*%< don't unqualified name as a tld */
+#define RES_USE_DNSSEC	0x00200000	/*%< use DNSSEC using OK bit in OPT */
+/* #define RES_DEBUG2	0x00400000 */	/* nslookup internal */
+/* KAME extensions: use higher bit to avoid conflict with ISC use */
+#define RES_USE_DNAME	0x10000000	/*%< use DNAME */
+#define RES_USE_EDNS0	0x40000000	/*%< use EDNS0 if configured */
+#define RES_NO_NIBBLE2	0x80000000	/*%< disable alternate nibble lookup */
+
+#define RES_DEFAULT	(RES_RECURSE | RES_DEFNAMES | \
+			 RES_DNSRCH | RES_NO_NIBBLE2)
+
+/*%
+ * Resolver "pfcode" values.  Used by dig.
+ */
+#define	RES_PRF_STATS	0x00000001
+#define	RES_PRF_UPDATE	0x00000002
+#define	RES_PRF_CLASS   0x00000004
+#define	RES_PRF_CMD	0x00000008
+#define	RES_PRF_QUES	0x00000010
+#define	RES_PRF_ANS	0x00000020
+#define	RES_PRF_AUTH	0x00000040
+#define	RES_PRF_ADD	0x00000080
+#define	RES_PRF_HEAD1	0x00000100
+#define	RES_PRF_HEAD2	0x00000200
+#define	RES_PRF_TTLID	0x00000400
+#define	RES_PRF_HEADX	0x00000800
+#define	RES_PRF_QUERY	0x00001000
+#define	RES_PRF_REPLY	0x00002000
+#define	RES_PRF_INIT	0x00004000
+#define	RES_PRF_TRUNC	0x00008000
+/*			0x00010000	*/
+
+/* Things involving an internal (static) resolver context. */
+__BEGIN_DECLS
+extern struct __res_state *__res_state(void);
+__END_DECLS
+#define _res (*__res_state())
+
+#ifndef __BIND_NOSTATIC
+#define fp_nquery		__fp_nquery
+#define fp_query		__fp_query
+#define hostalias		__hostalias
+#define p_query			__p_query
+#define res_close		__res_close
+#define res_init		__res_init
+#define res_isourserver		__res_isourserver
+#define res_mkquery		__res_mkquery
+#define res_opt			__res_opt
+#define res_query		__res_query
+#define res_querydomain		__res_querydomain
+#define res_search		__res_search
+#define res_send		__res_send
+#define res_sendsigned		__res_sendsigned
+
+__BEGIN_DECLS
+void		fp_nquery(const u_char *, int, FILE *);
+void		fp_query(const u_char *, FILE *);
+const char *	hostalias(const char *);
+void		p_query(const u_char *);
+void		res_close(void);
+int		res_init(void);
+int		res_isourserver(const struct sockaddr_in *);
+int		res_mkquery(int, const char *, int, int, const u_char *,
+				 int, const u_char *, u_char *, int);
+int		res_opt(int, u_char *, int, int);
+int		res_query(const char *, int, int, u_char *, int);
+int		res_querydomain(const char *, const char *, int, int,
+				     u_char *, int);
+int		res_search(const char *, int, int, u_char *, int);
+int		res_send(const u_char *, int, u_char *, int);
+int		res_sendsigned(const u_char *, int, ns_tsig_key *,
+				    u_char *, int);
+__END_DECLS
+#endif
+
+#if !defined(SHARED_LIBBIND) || defined(LIB)
+/*
+ * If libbind is a shared object (well, DLL anyway)
+ * these externs break the linker when resolv.h is 
+ * included by a lib client (like named)
+ * Make them go away if a client is including this
+ *
+ */
+extern const struct res_sym __p_key_syms[];
+extern const struct res_sym __p_cert_syms[];
+extern const struct res_sym __p_class_syms[];
+extern const struct res_sym __p_type_syms[];
+extern const struct res_sym __p_rcode_syms[];
+#endif /* SHARED_LIBBIND */
+
+#define b64_ntop		__b64_ntop
+#define b64_pton		__b64_pton
+#define dn_comp			__dn_comp
+#define dn_count_labels		__dn_count_labels
+#define dn_expand		__dn_expand
+#define dn_skipname		__dn_skipname
+#define fp_resstat		__fp_resstat
+#define loc_aton		__loc_aton
+#define loc_ntoa		__loc_ntoa
+#define p_cdname		__p_cdname
+#define p_cdnname		__p_cdnname
+#define p_class			__p_class
+#define p_fqname		__p_fqname
+#define p_fqnname		__p_fqnname
+#define p_option		__p_option
+#define p_secstodate		__p_secstodate
+#define p_section		__p_section
+#define p_time			__p_time
+#define p_type			__p_type
+#define p_rcode			__p_rcode
+#define p_sockun		__p_sockun
+#define putlong			__putlong
+#define putshort		__putshort
+#define res_dnok		__res_dnok
+#if 0
+#define res_findzonecut		__res_findzonecut
+#endif
+#define res_findzonecut2	__res_findzonecut2
+#define res_hnok		__res_hnok
+#define res_hostalias		__res_hostalias
+#define res_mailok		__res_mailok
+#define res_nameinquery		__res_nameinquery
+#define res_nclose		__res_nclose
+#define res_ninit		__res_ninit
+#define res_nmkquery		__res_nmkquery
+#define res_pquery		__res_pquery
+#define res_nquery		__res_nquery
+#define res_nquerydomain	__res_nquerydomain
+#define res_nsearch		__res_nsearch
+#define res_nsend		__res_nsend
+#if 0
+#define res_nsendsigned		__res_nsendsigned
+#endif
+#define res_nisourserver	__res_nisourserver
+#define res_ownok		__res_ownok
+#define res_queriesmatch	__res_queriesmatch
+#define res_randomid		__res_randomid
+#define sym_ntop		__sym_ntop
+#define sym_ntos		__sym_ntos
+#define sym_ston		__sym_ston
+#define res_nopt		__res_nopt
+#define res_nopt_rdata       	__res_nopt_rdata
+#define res_ndestroy		__res_ndestroy
+#define	res_nametoclass		__res_nametoclass
+#define	res_nametotype		__res_nametotype
+#define	res_setservers		__res_setservers
+#define	res_getservers		__res_getservers
+#if 0
+#define	res_buildprotolist	__res_buildprotolist
+#define	res_destroyprotolist	__res_destroyprotolist
+#define	res_destroyservicelist	__res_destroyservicelist
+#define	res_get_nibblesuffix	__res_get_nibblesuffix
+#define	res_get_nibblesuffix2	__res_get_nibblesuffix2
+#endif
+#define	res_ourserver_p		__res_ourserver_p
+#if 0
+#define	res_protocolname	__res_protocolname
+#define	res_protocolnumber	__res_protocolnumber
+#endif
+#define	res_send_setqhook	__res_send_setqhook
+#define	res_send_setrhook	__res_send_setrhook
+#if 0
+#define	res_servicename		__res_servicename
+#define	res_servicenumber	__res_servicenumber
+#endif
+__BEGIN_DECLS
+int		res_hnok(const char *);
+int		res_ownok(const char *);
+int		res_mailok(const char *);
+int		res_dnok(const char *);
+int		sym_ston(const struct res_sym *, const char *, int *);
+const char *	sym_ntos(const struct res_sym *, int, int *);
+const char *	sym_ntop(const struct res_sym *, int, int *);
+int		b64_ntop(u_char const *, size_t, char *, size_t);
+int		b64_pton(char const *, u_char *, size_t);
+int		loc_aton(const char *, u_char *);
+const char *	loc_ntoa(const u_char *, char *);
+int		dn_skipname(const u_char *, const u_char *);
+void		putlong(u_int32_t, u_char *);
+void		putshort(u_int16_t, u_char *);
+#ifndef __ultrix__
+u_int16_t	_getshort(const u_char *);
+u_int32_t	_getlong(const u_char *);
+#endif
+const char *	p_class(int);
+const char *	p_time(u_int32_t);
+const char *	p_type(int);
+const char *	p_rcode(int);
+const char *	p_sockun(union res_sockaddr_union, char *, size_t);
+const u_char *	p_cdnname(const u_char *, const u_char *, int, FILE *);
+const u_char *	p_cdname(const u_char *, const u_char *, FILE *);
+const u_char *	p_fqnname(const u_char *, const u_char *, int, char *, int);
+const u_char *	p_fqname(const u_char *, const u_char *, FILE *);
+const char *	p_option(u_long);
+char *		p_secstodate(u_long);
+int		dn_count_labels(const char *);
+int		dn_comp(const char *, u_char *, int, u_char **, u_char **);
+int		dn_expand(const u_char *, const u_char *, const u_char *,
+			  char *, int);
+u_int		res_randomid(void);
+int		res_nameinquery(const char *, int, int, const u_char *,
+				const u_char *);
+int		res_queriesmatch(const u_char *, const u_char *,
+				 const u_char *, const u_char *);
+const char *	p_section(int, int);
+/* Things involving a resolver context. */
+int		res_ninit(res_state);
+int		res_nisourserver(const res_state, const struct sockaddr_in *);
+void		fp_resstat(const res_state, FILE *);
+void		res_pquery(const res_state, const u_char *, int, FILE *);
+const char *	res_hostalias(const res_state, const char *, char *, size_t);
+int		res_nquery(res_state, const char *, int, int, u_char *, int);
+int		res_nsearch(res_state, const char *, int, int, u_char *, int);
+int		res_nquerydomain(res_state, const char *, const char *,
+				 int, int, u_char *, int);
+int		res_nmkquery(res_state, int, const char *, int, int,
+			     const u_char *, int, const u_char *,
+			     u_char *, int);
+int		res_nsend(res_state, const u_char *, int, u_char *, int);
+#if 0
+int		res_nsendsigned(res_state, const u_char *, int,
+				ns_tsig_key *, u_char *, int);
+int		res_findzonecut(res_state, const char *, ns_class, int,
+				char *, size_t, struct in_addr *, int);
+#endif
+int		res_findzonecut2(res_state, const char *, ns_class, int,
+				 char *, size_t,
+				 union res_sockaddr_union *, int);
+void		res_nclose(res_state);
+int		res_nopt(res_state, int, u_char *, int, int);
+int		res_nopt_rdata(res_state, int, u_char *, int, u_char *,
+			       u_short, u_short, u_char *);
+void		res_send_setqhook(res_send_qhook);
+void		res_send_setrhook(res_send_rhook);
+int		__res_vinit(res_state, int);
+#if 0
+void		res_destroyservicelist(void);
+const char *	res_servicename(u_int16_t, const char *);
+const char *	res_protocolname(int);
+void		res_destroyprotolist(void);
+void		res_buildprotolist(void);
+const char *	res_get_nibblesuffix(res_state);
+const char *	res_get_nibblesuffix2(res_state);
+#endif
+void		res_ndestroy(res_state);
+u_int16_t	res_nametoclass(const char *, int *);
+u_int16_t	res_nametotype(const char *, int *);
+void		res_setservers(res_state, const union res_sockaddr_union *,
+			       int);
+int		res_getservers(res_state, union res_sockaddr_union *, int);
+__END_DECLS
+
+#endif /* !_RESOLV_H_ */
+/*! \file */
diff --git a/freebsd-userspace/include/sys/socket.h b/freebsd-userspace/include/sys/socket.h
new file mode 100644
index 0000000..eaa7cc3
--- /dev/null
+++ b/freebsd-userspace/include/sys/socket.h
@@ -0,0 +1 @@
+#include <freebsd/sys/socket.h>
diff --git a/freebsd-userspace/include/sys/sysctl.h b/freebsd-userspace/include/sys/sysctl.h
new file mode 100644
index 0000000..c32590a
--- /dev/null
+++ b/freebsd-userspace/include/sys/sysctl.h
@@ -0,0 +1 @@
+#include <freebsd/sys/sysctl.h>
diff --git a/freebsd-userspace/lib/libc/include/libc_private.h b/freebsd-userspace/lib/libc/include/libc_private.h
new file mode 100644
index 0000000..d5f4212
--- /dev/null
+++ b/freebsd-userspace/lib/libc/include/libc_private.h
@@ -0,0 +1,225 @@
+/*
+ * Copyright (c) 1998 John Birrell <jb at cimlogic.com.au>.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the author nor the names of any co-contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON 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 ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ *
+ * Private definitions for libc, libc_r and libpthread.
+ *
+ */
+
+#ifndef _LIBC_PRIVATE_H_
+#define _LIBC_PRIVATE_H_
+#include <sys/_pthreadtypes.h>
+
+/*
+ * This global flag is non-zero when a process has created one
+ * or more threads. It is used to avoid calling locking functions
+ * when they are not required.
+ */
+extern int	__isthreaded;
+
+/*
+ * libc should use libc_dlopen internally, which respects a global
+ * flag where loading of new shared objects can be restricted.
+ */
+void *libc_dlopen(const char *, int);
+
+/*
+ * For dynamic linker.
+ */
+void _rtld_error(const char *fmt, ...);
+
+/*
+ * File lock contention is difficult to diagnose without knowing
+ * where locks were set. Allow a debug library to be built which
+ * records the source file and line number of each lock call.
+ */
+#ifdef	_FLOCK_DEBUG
+#define _FLOCKFILE(x)	_flockfile_debug(x, __FILE__, __LINE__)
+#else
+#define _FLOCKFILE(x)	_flockfile(x)
+#endif
+
+/*
+ * Macros for locking and unlocking FILEs. These test if the
+ * process is threaded to avoid locking when not required.
+ */
+#define	FLOCKFILE(fp)		if (__isthreaded) _FLOCKFILE(fp)
+#define	FUNLOCKFILE(fp)		if (__isthreaded) _funlockfile(fp)
+
+/*
+ * Indexes into the pthread jump table.
+ *
+ * Warning! If you change this type, you must also change the threads
+ * libraries that reference it (libc_r, libpthread).
+ */
+typedef enum {
+	PJT_ATFORK,
+	PJT_ATTR_DESTROY,
+	PJT_ATTR_GETDETACHSTATE,
+	PJT_ATTR_GETGUARDSIZE,
+	PJT_ATTR_GETINHERITSCHED,
+	PJT_ATTR_GETSCHEDPARAM,
+	PJT_ATTR_GETSCHEDPOLICY,
+	PJT_ATTR_GETSCOPE,
+	PJT_ATTR_GETSTACKADDR,
+	PJT_ATTR_GETSTACKSIZE,
+	PJT_ATTR_INIT,
+	PJT_ATTR_SETDETACHSTATE,
+	PJT_ATTR_SETGUARDSIZE,
+	PJT_ATTR_SETINHERITSCHED,
+	PJT_ATTR_SETSCHEDPARAM,
+	PJT_ATTR_SETSCHEDPOLICY,
+	PJT_ATTR_SETSCOPE,
+	PJT_ATTR_SETSTACKADDR,
+	PJT_ATTR_SETSTACKSIZE,
+	PJT_CANCEL,
+	PJT_CLEANUP_POP,
+	PJT_CLEANUP_PUSH,
+	PJT_COND_BROADCAST,
+	PJT_COND_DESTROY,
+	PJT_COND_INIT,
+	PJT_COND_SIGNAL,
+	PJT_COND_TIMEDWAIT,
+	PJT_COND_WAIT,
+	PJT_DETACH,
+	PJT_EQUAL,
+	PJT_EXIT,
+	PJT_GETSPECIFIC,
+	PJT_JOIN,
+	PJT_KEY_CREATE,
+	PJT_KEY_DELETE,
+	PJT_KILL,
+	PJT_MAIN_NP,
+	PJT_MUTEXATTR_DESTROY,
+	PJT_MUTEXATTR_INIT,
+	PJT_MUTEXATTR_SETTYPE,
+	PJT_MUTEX_DESTROY,
+	PJT_MUTEX_INIT,
+	PJT_MUTEX_LOCK,
+	PJT_MUTEX_TRYLOCK,
+	PJT_MUTEX_UNLOCK,
+	PJT_ONCE,
+	PJT_RWLOCK_DESTROY,
+	PJT_RWLOCK_INIT,
+	PJT_RWLOCK_RDLOCK,
+	PJT_RWLOCK_TRYRDLOCK,
+	PJT_RWLOCK_TRYWRLOCK,
+	PJT_RWLOCK_UNLOCK,
+	PJT_RWLOCK_WRLOCK,
+	PJT_SELF,
+	PJT_SETCANCELSTATE,
+	PJT_SETCANCELTYPE,
+	PJT_SETSPECIFIC,
+	PJT_SIGMASK,
+	PJT_TESTCANCEL,
+	PJT_MAX
+} pjt_index_t;
+
+typedef int (*pthread_func_t)(void);
+typedef pthread_func_t pthread_func_entry_t[2];
+
+extern pthread_func_entry_t __thr_jtable[];
+
+/*
+ * yplib internal interfaces
+ */
+#ifdef YP
+int _yp_check(char **);
+#endif
+
+/*
+ * Initialise TLS for static programs
+ */
+void _init_tls(void);
+
+/*
+ * Provides pthread_once()-like functionality for both single-threaded
+ * and multi-threaded applications.
+ */
+int _once(pthread_once_t *, void (*)(void));
+
+/*
+ * Set the TLS thread pointer
+ */
+void _set_tp(void *tp);
+
+/*
+ * This is a pointer in the C run-time startup code. It is used
+ * by getprogname() and setprogname().
+ */
+extern const char *__progname;
+
+/*
+ * This function is used by the threading libraries to notify malloc that a
+ * thread is exiting.
+ */
+void _malloc_thread_cleanup(void);
+
+/*
+ * These functions are used by the threading libraries in order to protect
+ * malloc across fork().
+ */
+void _malloc_prefork(void);
+void _malloc_postfork(void);
+
+/*
+ * Function to clean up streams, called from abort() and exit().
+ */
+extern void (*__cleanup)(void);
+
+/*
+ * Get kern.osreldate to detect ABI revisions.  Explicitly
+ * ignores value of $OSVERSION and caches result.  Prototypes
+ * for the wrapped "new" pad-less syscalls are here for now.
+ */
+extern int __getosreldate(void);
+#include <sys/_types.h>
+/* Without pad */
+extern __off_t	__sys_lseek(int, __off_t, int);
+extern int	__sys_ftruncate(int, __off_t);
+extern int	__sys_truncate(const char *, __off_t);
+extern __ssize_t __sys_pread(int, void *, __size_t, __off_t);
+extern __ssize_t __sys_pwrite(int, const void *, __size_t, __off_t);
+extern void *	__sys_mmap(void *, __size_t, int, int, int, __off_t);
+
+/* With pad */
+extern __off_t	__sys_freebsd6_lseek(int, int, __off_t, int);
+extern int	__sys_freebsd6_ftruncate(int, int, __off_t);
+extern int	__sys_freebsd6_truncate(const char *, int, __off_t);
+extern __ssize_t __sys_freebsd6_pread(int, void *, __size_t, int, __off_t);
+extern __ssize_t __sys_freebsd6_pwrite(int, const void *, __size_t, int, __off_t);
+extern void *	__sys_freebsd6_mmap(void *, __size_t, int, int, int, int, __off_t);
+
+/* Without back-compat translation */
+extern int	__sys_fcntl(int, int, ...);
+
+/* execve() with PATH processing to implement posix_spawnp() */
+int _execvpe(const char *, char * const *, char * const *);
+
+#endif /* _LIBC_PRIVATE_H_ */
diff --git a/freebsd-userspace/lib/libc/include/namespace.h b/freebsd-userspace/lib/libc/include/namespace.h
new file mode 100644
index 0000000..a65b929
--- /dev/null
+++ b/freebsd-userspace/lib/libc/include/namespace.h
@@ -0,0 +1,263 @@
+/*
+ * Copyright (c) 2001 Daniel Eischen <deischen at FreeBSD.org>.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON 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 ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+#ifndef _NAMESPACE_H_
+#define _NAMESPACE_H_
+
+/*
+ * Adjust names so that headers declare "hidden" names.
+ *
+ * README: When modifying this file don't forget to make the appropriate
+ *         changes in un-namespace.h!!!
+ */
+
+/*
+ * ISO C (C90) section.  Most names in libc aren't in ISO C, so they
+ * should be here.  Most aren't here...
+ */
+#define		err				_err
+#define		warn				_warn
+#define		nsdispatch			_nsdispatch
+
+/*
+ * Prototypes for syscalls/functions that need to be overridden
+ * in libc_r/libpthread.
+ */
+#define		accept				_accept
+#define		__acl_aclcheck_fd		___acl_aclcheck_fd
+#define		__acl_delete_fd			___acl_delete_fd
+#define		__acl_get_fd			___acl_get_fd
+#define		__acl_set_fd			___acl_set_fd
+#define		bind				_bind
+#define		__cap_get_fd			___cap_get_fd
+#define		__cap_set_fd			___cap_set_fd
+#define		close				_close
+#define		connect				_connect
+#define		dup				_dup
+#define		dup2				_dup2
+#define		execve				_execve
+#define		fcntl				_fcntl
+/*#define		flock				_flock */
+#define		flockfile			_flockfile
+#define		fpathconf			_fpathconf
+#define		fstat				_fstat
+#define		fstatfs				_fstatfs
+#define		fsync				_fsync
+#define		funlockfile			_funlockfile
+#define		getdirentries			_getdirentries
+#define		getlogin			_getlogin
+#define		getpeername			_getpeername
+#define		getprogname			_getprogname
+#define		getsockname			_getsockname
+#define		getsockopt			_getsockopt
+#define		ioctl				_ioctl
+/* #define		kevent				_kevent */
+#define		listen				_listen
+#define		nanosleep			_nanosleep
+#define		open				_open
+#define		poll				_poll
+#define		pthread_atfork			_pthread_atfork
+#define		pthread_attr_destroy		_pthread_attr_destroy
+#define		pthread_attr_get_np		_pthread_attr_get_np
+#define		pthread_attr_getaffinity_np	_pthread_attr_getaffinity_np
+#define		pthread_attr_getdetachstate	_pthread_attr_getdetachstate
+#define		pthread_attr_getguardsize	_pthread_attr_getguardsize
+#define		pthread_attr_getinheritsched	_pthread_attr_getinheritsched
+#define		pthread_attr_getschedparam	_pthread_attr_getschedparam
+#define		pthread_attr_getschedpolicy	_pthread_attr_getschedpolicy
+#define		pthread_attr_getscope		_pthread_attr_getscope
+#define		pthread_attr_getstack		_pthread_attr_getstack
+#define		pthread_attr_getstackaddr	_pthread_attr_getstackaddr
+#define		pthread_attr_getstacksize	_pthread_attr_getstacksize
+#define		pthread_attr_init		_pthread_attr_init
+#define		pthread_attr_setaffinity_np	_pthread_attr_setaffinity_np
+#define		pthread_attr_setcreatesuspend_np _pthread_attr_setcreatesuspend_np
+#define		pthread_attr_setdetachstate	_pthread_attr_setdetachstate
+#define		pthread_attr_setguardsize	_pthread_attr_setguardsize
+#define		pthread_attr_setinheritsched	_pthread_attr_setinheritsched
+#define		pthread_attr_setschedparam	_pthread_attr_setschedparam
+#define		pthread_attr_setschedpolicy	_pthread_attr_setschedpolicy
+#define		pthread_attr_setscope		_pthread_attr_setscope
+#define		pthread_attr_setstack		_pthread_attr_setstack
+#define		pthread_attr_setstackaddr	_pthread_attr_setstackaddr
+#define		pthread_attr_setstacksize	_pthread_attr_setstacksize
+#define		pthread_barrier_destroy		_pthread_barrier_destroy
+#define		pthread_barrier_init		_pthread_barrier_init
+#define		pthread_barrier_wait		_pthread_barrier_wait
+#define		pthread_barrierattr_destroy	_pthread_barrierattr_destroy
+#define		pthread_barrierattr_getpshared	_pthread_barrierattr_getpshared
+#define		pthread_barrierattr_init	_pthread_barrierattr_init
+#define		pthread_barrierattr_setpshared	_pthread_barrierattr_setpshared
+#define		pthread_cancel			_pthread_cancel
+#define		pthread_cond_broadcast		_pthread_cond_broadcast
+#define		pthread_cond_destroy		_pthread_cond_destroy
+#define		pthread_cond_init		_pthread_cond_init
+#define		pthread_cond_signal		_pthread_cond_signal
+#define		pthread_cond_timedwait		_pthread_cond_timedwait
+#define		pthread_cond_wait		_pthread_cond_wait
+#define		pthread_condattr_destroy	_pthread_condattr_destroy
+#define		pthread_condattr_getclock	_pthread_condattr_getclock
+#define		pthread_condattr_getpshared	_pthread_condattr_getpshared
+#define		pthread_condattr_init		_pthread_condattr_init
+#define		pthread_condattr_setclock	_pthread_condattr_setclock
+#define		pthread_condattr_setpshared	_pthread_condattr_setpshared
+#define		pthread_create			_pthread_create
+#define		pthread_detach			_pthread_detach
+#define		pthread_equal			_pthread_equal
+#define		pthread_exit			_pthread_exit
+#define		pthread_getaffinity_np		_pthread_getaffinity_np
+#define		pthread_getconcurrency		_pthread_getconcurrency
+#define		pthread_getcpuclockid		_pthread_getcpuclockid
+#define		pthread_getprio			_pthread_getprio
+#define		pthread_getschedparam		_pthread_getschedparam
+#define		pthread_getspecific		_pthread_getspecific
+#define		pthread_join			_pthread_join
+#define		pthread_key_create		_pthread_key_create
+#define		pthread_key_delete		_pthread_key_delete
+#define		pthread_kill			_pthread_kill
+#define		pthread_main_np			_pthread_main_np
+#define		pthread_multi_np		_pthread_multi_np
+#define		pthread_mutex_destroy		_pthread_mutex_destroy
+#define		pthread_mutex_getprioceiling	_pthread_mutex_getprioceiling
+#define		pthread_mutex_init		_pthread_mutex_init
+#define		pthread_mutex_isowned_np	_pthread_mutex_isowned_np
+#define		pthread_mutex_lock		_pthread_mutex_lock
+#define		pthread_mutex_setprioceiling	_pthread_mutex_setprioceiling
+#define		pthread_mutex_timedlock		_pthread_mutex_timedlock
+#define		pthread_mutex_trylock		_pthread_mutex_trylock
+#define		pthread_mutex_unlock		_pthread_mutex_unlock
+#define		pthread_mutexattr_destroy	_pthread_mutexattr_destroy
+#define		pthread_mutexattr_getkind_np	_pthread_mutexattr_getkind_np
+#define		pthread_mutexattr_getprioceiling _pthread_mutexattr_getprioceiling
+#define		pthread_mutexattr_getprotocol	_pthread_mutexattr_getprotocol
+#define		pthread_mutexattr_getpshared	_pthread_mutexattr_getpshared
+#define		pthread_mutexattr_gettype	_pthread_mutexattr_gettype
+#define		pthread_mutexattr_init		_pthread_mutexattr_init
+#define		pthread_mutexattr_setkind_np	_pthread_mutexattr_setkind_np
+#define		pthread_mutexattr_setprioceiling _pthread_mutexattr_setprioceiling
+#define		pthread_mutexattr_setprotocol	_pthread_mutexattr_setprotocol
+#define		pthread_mutexattr_setpshared	_pthread_mutexattr_setpshared
+#define		pthread_mutexattr_settype	_pthread_mutexattr_settype
+#define		pthread_once			_pthread_once
+#define		pthread_resume_all_np		_pthread_resume_all_np
+#define		pthread_resume_np		_pthread_resume_np
+#define		pthread_rwlock_destroy		_pthread_rwlock_destroy
+#define		pthread_rwlock_init		_pthread_rwlock_init
+#define		pthread_rwlock_rdlock		_pthread_rwlock_rdlock
+#define		pthread_rwlock_timedrdlock	_pthread_rwlock_timedrdlock
+#define		pthread_rwlock_timedwrlock	_pthread_rwlock_timedwrlock
+#define		pthread_rwlock_tryrdlock	_pthread_rwlock_tryrdlock
+#define		pthread_rwlock_trywrlock	_pthread_rwlock_trywrlock
+#define		pthread_rwlock_unlock		_pthread_rwlock_unlock
+#define		pthread_rwlock_wrlock		_pthread_rwlock_wrlock
+#define		pthread_rwlockattr_destroy	_pthread_rwlockattr_destroy
+#define		pthread_rwlockattr_getpshared	_pthread_rwlockattr_getpshared
+#define		pthread_rwlockattr_init		_pthread_rwlockattr_init
+#define		pthread_rwlockattr_setpshared	_pthread_rwlockattr_setpshared
+#define		pthread_self			_pthread_self
+#define		pthread_set_name_np		_pthread_set_name_np
+#define		pthread_setaffinity_np		_pthread_setaffinity_np
+#define		pthread_setcancelstate		_pthread_setcancelstate
+#define		pthread_setcanceltype		_pthread_setcanceltype
+#define		pthread_setconcurrency		_pthread_setconcurrency
+#define		pthread_setprio			_pthread_setprio
+#define		pthread_setschedparam		_pthread_setschedparam
+#define		pthread_setspecific		_pthread_setspecific
+#define		pthread_sigmask			_pthread_sigmask
+#define		pthread_single_np		_pthread_single_np
+#define		pthread_spin_destroy		_pthread_spin_destroy
+#define		pthread_spin_init		_pthread_spin_init
+#define		pthread_spin_lock		_pthread_spin_lock
+#define		pthread_spin_trylock		_pthread_spin_trylock
+#define		pthread_spin_unlock		_pthread_spin_unlock
+#define		pthread_suspend_all_np		_pthread_suspend_all_np
+#define		pthread_suspend_np		_pthread_suspend_np
+#define		pthread_switch_add_np		_pthread_switch_add_np
+#define		pthread_switch_delete_np	_pthread_switch_delete_np
+#define		pthread_testcancel		_pthread_testcancel
+#define		pthread_timedjoin_np		_pthread_timedjoin_np
+#define		pthread_yield			_pthread_yield
+#define		read				_read
+#define		readv				_readv
+#define		recvfrom			_recvfrom
+#define		recvmsg				_recvmsg
+#define		select				_select
+#define		sem_close			_sem_close
+#define		sem_destroy			_sem_destroy
+#define		sem_getvalue			_sem_getvalue
+#define		sem_init			_sem_init
+#define		sem_open			_sem_open
+#define		sem_post			_sem_post
+#define		sem_timedwait			_sem_timedwait
+#define		sem_trywait			_sem_trywait
+#define		sem_unlink			_sem_unlink
+#define		sem_wait			_sem_wait
+#define		sendmsg				_sendmsg
+#define		sendto				_sendto
+#define		setsockopt			_setsockopt
+/*#define		sigaction			_sigaction*/
+#define		sigprocmask			_sigprocmask
+#define		sigsuspend			_sigsuspend
+#define		socket				_socket
+#define		socketpair			_socketpair
+#define		usleep				_usleep
+#define		wait4				_wait4
+#define		waitpid				_waitpid
+#define		write				_write
+#define		writev				_writev
+
+
+/*
+ * Other hidden syscalls/functions that libc_r needs to override
+ * but are not used internally by libc.
+ *
+ * XXX - When modifying libc to use one of the following, remove
+ * the prototype from below and place it in the list above.
+ */
+#if 0
+#define		creat				_creat
+#define		fchflags			_fchflags
+#define		fchmod				_fchmod
+#define		ftrylockfile			_ftrylockfile
+#define		msync				_msync
+#define		nfssvc				_nfssvc
+#define		pause				_pause
+#define		sched_yield			_sched_yield
+#define		sendfile			_sendfile
+#define		shutdown			_shutdown
+#define		sigaltstack			_sigaltstack
+#define		sigpending			_sigpending
+#define		sigreturn			_sigreturn
+#define		sigsetmask			_sigsetmask
+#define		sleep				_sleep
+#define		system				_system
+#define		tcdrain				_tcdrain
+#define		wait				_wait
+#endif
+
+#endif /* _NAMESPACE_H_ */
diff --git a/freebsd-userspace/lib/libc/include/nss_tls.h b/freebsd-userspace/lib/libc/include/nss_tls.h
new file mode 100644
index 0000000..13ab367
--- /dev/null
+++ b/freebsd-userspace/lib/libc/include/nss_tls.h
@@ -0,0 +1,80 @@
+/*-
+ * Copyright (c) 2003 Networks Associates Technology, Inc.
+ * All rights reserved.
+ *
+ * This software was developed for the FreeBSD Project by
+ * Jacques A. Vidrine, Safeport Network Services, and Network
+ * Associates Laboratories, the Security Research Division of Network
+ * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035
+ * ("CBOSS"), as part of the DARPA CHATS research program.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON 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 ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ *
+ * Macros which generate thread local storage handling code in NSS modules.
+ */
+#ifndef _NSS_TLS_H_
+#define _NSS_TLS_H_
+
+#define NSS_TLS_HANDLING(name)					\
+static pthread_key_t name##_state_key;				\
+static	void	 name##_keyinit(void);				\
+static	int	 name##_getstate(struct name##_state **);	\
+\
+static void								\
+name##_keyinit(void)							\
+{									\
+	(void)_pthread_key_create(&name##_state_key, name##_endstate);	\
+}									\
+\
+static int							\
+name##_getstate(struct name##_state **p)			\
+{								\
+	static struct name##_state st;				\
+	static pthread_once_t	keyinit = PTHREAD_ONCE_INIT;	\
+	int			rv;				\
+								\
+	if (!__isthreaded || _pthread_main_np() != 0) {		\
+		*p = &st;					\
+		return (0);					\
+	}							\
+	rv = _pthread_once(&keyinit, name##_keyinit);		\
+	if (rv != 0)						\
+		return (rv);					\
+	*p = _pthread_getspecific(name##_state_key);		\
+	if (*p != NULL)						\
+		return (0);					\
+	*p = calloc(1, sizeof(**p));				\
+	if (*p == NULL)						\
+		return (ENOMEM);				\
+	rv = _pthread_setspecific(name##_state_key, *p);	\
+	if (rv != 0) {						\
+		free(*p);					\
+		*p = NULL;					\
+	}							\
+	return (rv);						\
+}								\
+/* allow the macro invocation to end with a semicolon */	\
+struct _clashproof_bmVjdGFy
+
+#endif /* _NSS_TLS_H_ */
diff --git a/freebsd-userspace/lib/libc/include/reentrant.h b/freebsd-userspace/lib/libc/include/reentrant.h
new file mode 100644
index 0000000..8ab328b
--- /dev/null
+++ b/freebsd-userspace/lib/libc/include/reentrant.h
@@ -0,0 +1,135 @@
+/*-
+ * Copyright (c) 1997,98 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by J.T. Conklin.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *        This product includes software developed by the NetBSD
+ *        Foundation, Inc. and its contributors.
+ * 4. Neither the name of The NetBSD Foundation nor the names of its
+ *    contributors may be used to endorse or promote products derived
+ *    from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON 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 ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+/*
+ * Requirements:
+ * 
+ * 1. The thread safe mechanism should be lightweight so the library can
+ *    be used by non-threaded applications without unreasonable overhead.
+ * 
+ * 2. There should be no dependency on a thread engine for non-threaded
+ *    applications.
+ * 
+ * 3. There should be no dependency on any particular thread engine.
+ * 
+ * 4. The library should be able to be compiled without support for thread
+ *    safety.
+ * 
+ * 
+ * Rationale:
+ * 
+ * One approach for thread safety is to provide discrete versions of the
+ * library: one thread safe, the other not.  The disadvantage of this is
+ * that libc is rather large, and two copies of a library which are 99%+
+ * identical is not an efficent use of resources.
+ * 
+ * Another approach is to provide a single thread safe library.  However,
+ * it should not add significant run time or code size overhead to non-
+ * threaded applications.
+ * 
+ * Since the NetBSD C library is used in other projects, it should be
+ * easy to replace the mutual exclusion primitives with ones provided by
+ * another system.  Similarly, it should also be easy to remove all
+ * support for thread safety completely if the target environment does
+ * not support threads.
+ * 
+ * 
+ * Implementation Details:
+ * 
+ * The mutex primitives used by the library (mutex_t, mutex_lock, etc.)
+ * are macros which expand to the cooresponding primitives provided by
+ * the thread engine or to nothing.  The latter is used so that code is
+ * not unreasonably cluttered with #ifdefs when all thread safe support
+ * is removed.
+ * 
+ * The mutex macros can be directly mapped to the mutex primitives from
+ * pthreads, however it should be reasonably easy to wrap another mutex
+ * implementation so it presents a similar interface.
+ * 
+ * Stub implementations of the mutex functions are provided with *weak*
+ * linkage.  These functions simply return success.  When linked with a
+ * thread library (i.e. -lpthread), the functions will override the
+ * stubs.
+ */
+
+#include <pthread.h>
+#include <pthread_np.h>
+#include "libc_private.h"
+
+#define mutex_t			pthread_mutex_t
+#define cond_t			pthread_cond_t
+#define rwlock_t		pthread_rwlock_t
+#define once_t			pthread_once_t
+
+#define thread_key_t		pthread_key_t
+#define MUTEX_INITIALIZER	PTHREAD_MUTEX_INITIALIZER
+#define RWLOCK_INITIALIZER	PTHREAD_RWLOCK_INITIALIZER
+#define ONCE_INITIALIZER	PTHREAD_ONCE_INIT
+
+#define mutex_init(m, a)	_pthread_mutex_init(m, a)
+#define mutex_lock(m)		if (__isthreaded) \
+				_pthread_mutex_lock(m)
+#define mutex_unlock(m)		if (__isthreaded) \
+				_pthread_mutex_unlock(m)
+#define mutex_trylock(m)	(__isthreaded ? 0 : _pthread_mutex_trylock(m))
+
+#define cond_init(c, a, p)	_pthread_cond_init(c, a)
+#define cond_signal(m)		if (__isthreaded) \
+				_pthread_cond_signal(m)
+#define cond_broadcast(m)	if (__isthreaded) \
+				_pthread_cond_broadcast(m)
+#define cond_wait(c, m)		if (__isthreaded) \
+				_pthread_cond_wait(c, m)
+
+#define rwlock_init(l, a)	_pthread_rwlock_init(l, a)
+#define rwlock_rdlock(l)	if (__isthreaded) \
+				_pthread_rwlock_rdlock(l)
+#define rwlock_wrlock(l)	if (__isthreaded) \
+				_pthread_rwlock_wrlock(l)
+#define rwlock_unlock(l)	if (__isthreaded) \
+				_pthread_rwlock_unlock(l)
+
+#define thr_keycreate(k, d)	_pthread_key_create(k, d)
+#define thr_setspecific(k, p)	_pthread_setspecific(k, p)
+#define thr_getspecific(k)	_pthread_getspecific(k)
+#define thr_sigsetmask(f, n, o)	_pthread_sigmask(f, n, o)
+
+#define thr_once(o, i)		_pthread_once(o, i)
+#define thr_self()		_pthread_self()
+#define thr_exit(x)		_pthread_exit(x)
+#define thr_main()		_pthread_main_np()
diff --git a/freebsd-userspace/lib/libc/include/resolv_mt.h b/freebsd-userspace/lib/libc/include/resolv_mt.h
new file mode 100644
index 0000000..27963a1
--- /dev/null
+++ b/freebsd-userspace/lib/libc/include/resolv_mt.h
@@ -0,0 +1,47 @@
+#ifndef _RESOLV_MT_H
+#define _RESOLV_MT_H
+
+#include <sys/types.h>
+#include <netinet/in.h>
+#include <arpa/nameser.h>
+#include <resolv.h>
+
+/* Access functions for the libresolv private interface */
+
+int	__res_enable_mt(void);
+int	__res_disable_mt(void);
+
+/* Per-thread context */
+
+typedef struct {
+int	no_hosts_fallback_private;
+int	retry_save;
+int	retry_private;
+char	inet_nsap_ntoa_tmpbuf[255*3];
+char	sym_ntos_unname[20];
+char	sym_ntop_unname[20];
+char	p_option_nbuf[40];
+char	p_time_nbuf[40];
+char	precsize_ntoa_retbuf[sizeof "90000000.00"];
+char	loc_ntoa_tmpbuf[sizeof
+"1000 60 60.000 N 1000 60 60.000 W -12345678.00m 90000000.00m 90000000.00m 90000000.00m"];
+char	p_secstodate_output[15];
+} mtctxres_t;
+
+/* Thread-specific data (TSD) */
+
+mtctxres_t	*___mtctxres(void);
+#define mtctxres	(___mtctxres())
+
+/* Various static data that should be TSD */
+
+#define sym_ntos_unname		(mtctxres->sym_ntos_unname)
+#define sym_ntop_unname		(mtctxres->sym_ntop_unname)
+#define inet_nsap_ntoa_tmpbuf	(mtctxres->inet_nsap_ntoa_tmpbuf)
+#define p_option_nbuf		(mtctxres->p_option_nbuf)
+#define p_time_nbuf		(mtctxres->p_time_nbuf)
+#define precsize_ntoa_retbuf	(mtctxres->precsize_ntoa_retbuf)
+#define loc_ntoa_tmpbuf		(mtctxres->loc_ntoa_tmpbuf)
+#define p_secstodate_output	(mtctxres->p_secstodate_output)
+
+#endif /* _RESOLV_MT_H */
diff --git a/freebsd-userspace/lib/libc/include/un-namespace.h b/freebsd-userspace/lib/libc/include/un-namespace.h
new file mode 100644
index 0000000..6b7f49a
--- /dev/null
+++ b/freebsd-userspace/lib/libc/include/un-namespace.h
@@ -0,0 +1,253 @@
+/*
+ * Copyright (c) 2001 Daniel Eischen <deischen at FreeBSD.org>.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON 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 ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+#ifndef _UN_NAMESPACE_H_
+#define _UN_NAMESPACE_H_
+
+#undef		accept
+#undef		__acl_aclcheck_fd
+#undef		__acl_delete_fd
+#undef		__acl_get_fd
+#undef		__acl_set_fd
+#undef		bind
+#undef		__cap_get_fd
+#undef		__cap_set_fd
+#undef		close
+#undef		connect
+#undef		dup
+#undef		dup2
+#undef		execve
+#undef		fcntl
+#undef		flock
+#undef		flockfile
+#undef		fpathconf
+#undef		fstat
+#undef		fstatfs
+#undef		fsync
+#undef		funlockfile
+#undef		getdirentries
+#undef		getlogin
+#undef		getpeername
+#undef		getprogname
+#undef		getsockname
+#undef		getsockopt
+#undef		ioctl
+#undef		kevent
+#undef		listen
+#undef		nanosleep
+#undef		open
+#undef		poll
+#undef		pthread_atfork
+#undef		pthread_attr_destroy
+#undef		pthread_attr_get_np
+#undef		pthread_attr_getaffinity_np
+#undef		pthread_attr_getdetachstate
+#undef		pthread_attr_getguardsize
+#undef		pthread_attr_getinheritsched
+#undef		pthread_attr_getschedparam
+#undef		pthread_attr_getschedpolicy
+#undef		pthread_attr_getscope
+#undef		pthread_attr_getstack
+#undef		pthread_attr_getstackaddr
+#undef		pthread_attr_getstacksize
+#undef		pthread_attr_init
+#undef		pthread_attr_setaffinity_np
+#undef		pthread_attr_setcreatesuspend_np
+#undef		pthread_attr_setdetachstate
+#undef		pthread_attr_setguardsize
+#undef		pthread_attr_setinheritsched
+#undef		pthread_attr_setschedparam
+#undef		pthread_attr_setschedpolicy
+#undef		pthread_attr_setscope
+#undef		pthread_attr_setstack
+#undef		pthread_attr_setstackaddr
+#undef		pthread_attr_setstacksize
+#undef		pthread_barrier_destroy
+#undef		pthread_barrier_init
+#undef		pthread_barrier_wait
+#undef		pthread_barrierattr_destroy
+#undef		pthread_barrierattr_getpshared
+#undef		pthread_barrierattr_init
+#undef		pthread_barrierattr_setpshared
+#undef		pthread_cancel
+#undef		pthread_cond_broadcast
+#undef		pthread_cond_destroy
+#undef		pthread_cond_init
+#undef		pthread_cond_signal
+#undef		pthread_cond_timedwait
+#undef		pthread_cond_wait
+#undef		pthread_condattr_destroy
+#undef		pthread_condattr_getclock
+#undef		pthread_condattr_getpshared
+#undef		pthread_condattr_init
+#undef		pthread_condattr_setclock
+#undef		pthread_condattr_setpshared
+#undef		pthread_create
+#undef		pthread_detach
+#undef		pthread_equal
+#undef		pthread_exit
+#undef		pthread_getaffinity_np
+#undef		pthread_getconcurrency
+#undef		pthread_getcpuclockid
+#undef		pthread_getprio
+#undef		pthread_getschedparam
+#undef		pthread_getspecific
+#undef		pthread_join
+#undef		pthread_key_create
+#undef		pthread_key_delete
+#undef		pthread_kill
+#undef		pthread_main_np
+#undef		pthread_multi_np
+#undef		pthread_mutex_destroy
+#undef		pthread_mutex_getprioceiling
+#undef		pthread_mutex_init
+#undef		pthread_mutex_isowned_np
+#undef		pthread_mutex_lock
+#undef		pthread_mutex_setprioceiling
+#undef		pthread_mutex_timedlock
+#undef		pthread_mutex_trylock
+#undef		pthread_mutex_unlock
+#undef		pthread_mutexattr_destroy
+#undef		pthread_mutexattr_getkind_np
+#undef		pthread_mutexattr_getprioceiling
+#undef		pthread_mutexattr_getprotocol
+#undef		pthread_mutexattr_getpshared
+#undef		pthread_mutexattr_gettype
+#undef		pthread_mutexattr_init
+#undef		pthread_mutexattr_setkind_np
+#undef		pthread_mutexattr_setprioceiling
+#undef		pthread_mutexattr_setprotocol
+#undef		pthread_mutexattr_setpshared
+#undef		pthread_mutexattr_settype
+#undef		pthread_once
+#undef		pthread_resume_all_np
+#undef		pthread_resume_np
+#undef		pthread_rwlock_destroy
+#undef		pthread_rwlock_init
+#undef		pthread_rwlock_rdlock
+#undef		pthread_rwlock_timedrdlock
+#undef		pthread_rwlock_timedwrlock
+#undef		pthread_rwlock_tryrdlock
+#undef		pthread_rwlock_trywrlock
+#undef		pthread_rwlock_unlock
+#undef		pthread_rwlock_wrlock
+#undef		pthread_rwlockattr_destroy
+#undef		pthread_rwlockattr_getpshared
+#undef		pthread_rwlockattr_init
+#undef		pthread_rwlockattr_setpshared
+#undef		pthread_self
+#undef		pthread_set_name_np
+#undef		pthread_setaffinity_np
+#undef		pthread_setcancelstate
+#undef		pthread_setcanceltype
+#undef		pthread_setconcurrency
+#undef		pthread_setprio
+#undef		pthread_setschedparam
+#undef		pthread_setspecific
+#undef		pthread_sigmask
+#undef		pthread_single_np
+#undef		pthread_spin_destroy
+#undef		pthread_spin_init
+#undef		pthread_spin_lock
+#undef		pthread_spin_trylock
+#undef		pthread_spin_unlock
+#undef		pthread_suspend_all_np
+#undef		pthread_suspend_np
+#undef		pthread_switch_add_np
+#undef		pthread_switch_delete_np
+#undef		pthread_testcancel
+#undef		pthread_timedjoin_np
+#undef		pthread_yield
+#undef		read
+#undef		readv
+#undef		recvfrom
+#undef		recvmsg
+#undef		select
+#undef		sem_close
+#undef		sem_destroy
+#undef		sem_getvalue
+#undef		sem_init
+#undef		sem_open
+#undef		sem_post
+#undef		sem_timedwait
+#undef		sem_trywait
+#undef		sem_unlink
+#undef		sem_wait
+#undef		sendmsg
+#undef		sendto
+#undef		setsockopt
+#undef		sigaction
+#undef		sigprocmask
+#undef		sigsuspend
+#undef		socket
+#undef		socketpair
+#undef		usleep
+#undef		wait4
+#undef		waitpid
+#undef		write
+#undef		writev
+
+#if 0
+#undef		creat
+#undef		fchflags
+#undef		fchmod
+#undef		ftrylockfile
+#undef		msync
+#undef		nfssvc
+#undef		pause
+#undef		sched_yield
+#undef		sendfile
+#undef		shutdown
+#undef		sigaltstack
+#undef		sigpending
+#undef		sigreturn
+#undef		sigsetmask
+#undef		sleep
+#undef		system
+#undef		tcdrain
+#undef		wait
+#endif	/* 0 */
+
+#ifdef _SIGNAL_H_
+int     	_sigaction(int, const struct sigaction *, struct sigaction *);
+#endif
+
+#ifdef _SYS_EVENT_H_
+int		_kevent(int, const struct kevent *, int, struct kevent *,
+		    int, const struct timespec *);
+#endif
+
+#ifdef _SYS_FCNTL_H_
+int		_flock(int, int);
+#endif
+
+#undef		err
+#undef		warn
+#undef		nsdispatch
+
+#endif	/* _UN_NAMESPACE_H_ */
diff --git a/freebsd-userspace/lib/libc/inet/inet_addr.c b/freebsd-userspace/lib/libc/inet/inet_addr.c
new file mode 100644
index 0000000..e606d34
--- /dev/null
+++ b/freebsd-userspace/lib/libc/inet/inet_addr.c
@@ -0,0 +1,215 @@
+/*
+ * Copyright (c) 1983, 1990, 1993
+ *    The Regents of the University of California.  All rights reserved.
+ * 
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ * 
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON 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 ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+/*
+ * Portions Copyright (c) 1993 by Digital Equipment Corporation.
+ * 
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies, and that
+ * the name of Digital Equipment Corporation not be used in advertising or
+ * publicity pertaining to distribution of the document or software without
+ * specific, written prior permission.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
+ * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
+ * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
+ * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
+ * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
+ * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
+ * SOFTWARE.
+ */
+
+/*
+ * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
+ * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
+ * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#if defined(LIBC_SCCS) && !defined(lint)
+static const char sccsid[] = "@(#)inet_addr.c	8.1 (Berkeley) 6/17/93";
+static const char rcsid[] = "$Id: inet_addr.c,v 1.4.18.1 2005/04/27 05:00:52 sra Exp $";
+#endif /* LIBC_SCCS and not lint */
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include "port_before.h"
+
+#include <sys/types.h>
+#include <sys/param.h>
+
+#include <netinet/in.h>
+#include <arpa/inet.h>
+
+#include <ctype.h>
+
+#include "port_after.h"
+
+/*%
+ * Ascii internet address interpretation routine.
+ * The value returned is in network order.
+ */
+in_addr_t		/* XXX should be struct in_addr :( */
+inet_addr(const char *cp) {
+	struct in_addr val;
+
+	if (inet_aton(cp, &val))
+		return (val.s_addr);
+	return (INADDR_NONE);
+}
+
+/*%
+ * Check whether "cp" is a valid ascii representation
+ * of an Internet address and convert to a binary address.
+ * Returns 1 if the address is valid, 0 if not.
+ * This replaces inet_addr, the return value from which
+ * cannot distinguish between failure and a local broadcast address.
+ */
+int
+inet_aton(const char *cp, struct in_addr *addr) {
+	u_long val;
+	int base, n;
+	char c;
+	u_int8_t parts[4];
+	u_int8_t *pp = parts;
+	int digit;
+
+	c = *cp;
+	for (;;) {
+		/*
+		 * Collect number up to ``.''.
+		 * Values are specified as for C:
+		 * 0x=hex, 0=octal, isdigit=decimal.
+		 */
+		if (!isdigit((unsigned char)c))
+			return (0);
+		val = 0; base = 10; digit = 0;
+		if (c == '0') {
+			c = *++cp;
+			if (c == 'x' || c == 'X')
+				base = 16, c = *++cp;
+			else {
+				base = 8;
+				digit = 1 ;
+			}
+		}
+		for (;;) {
+			if (isascii(c) && isdigit((unsigned char)c)) {
+				if (base == 8 && (c == '8' || c == '9'))
+					return (0);
+				val = (val * base) + (c - '0');
+				c = *++cp;
+				digit = 1;
+			} else if (base == 16 && isascii(c) && 
+				   isxdigit((unsigned char)c)) {
+				val = (val << 4) |
+					(c + 10 - (islower((unsigned char)c) ? 'a' : 'A'));
+				c = *++cp;
+				digit = 1;
+			} else
+				break;
+		}
+		if (c == '.') {
+			/*
+			 * Internet format:
+			 *	a.b.c.d
+			 *	a.b.c	(with c treated as 16 bits)
+			 *	a.b	(with b treated as 24 bits)
+			 */
+			if (pp >= parts + 3 || val > 0xffU)
+				return (0);
+			*pp++ = val;
+			c = *++cp;
+		} else
+			break;
+	}
+	/*
+	 * Check for trailing characters.
+	 */
+	if (c != '\0' && (!isascii(c) || !isspace((unsigned char)c)))
+		return (0);
+	/*
+	 * Did we get a valid digit?
+	 */
+	if (!digit)
+		return (0);
+	/*
+	 * Concoct the address according to
+	 * the number of parts specified.
+	 */
+	n = pp - parts + 1;
+	switch (n) {
+	case 1:				/*%< a -- 32 bits */
+		break;
+
+	case 2:				/*%< a.b -- 8.24 bits */
+		if (val > 0xffffffU)
+			return (0);
+		val |= parts[0] << 24;
+		break;
+
+	case 3:				/*%< a.b.c -- 8.8.16 bits */
+		if (val > 0xffffU)
+			return (0);
+		val |= (parts[0] << 24) | (parts[1] << 16);
+		break;
+
+	case 4:				/*%< a.b.c.d -- 8.8.8.8 bits */
+		if (val > 0xffU)
+			return (0);
+		val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
+		break;
+	}
+	if (addr != NULL)
+		addr->s_addr = htonl(val);
+	return (1);
+}
+
+/*
+ * Weak aliases for applications that use certain private entry points,
+ * and fail to include <arpa/inet.h>.
+ */
+#undef inet_addr
+__weak_reference(__inet_addr, inet_addr);
+#undef inet_aton
+__weak_reference(__inet_aton, inet_aton);
+
+/*! \file */
diff --git a/freebsd-userspace/lib/libc/inet/inet_cidr_ntop.c b/freebsd-userspace/lib/libc/inet/inet_cidr_ntop.c
new file mode 100644
index 0000000..645b3cd
--- /dev/null
+++ b/freebsd-userspace/lib/libc/inet/inet_cidr_ntop.c
@@ -0,0 +1,263 @@
+/*
+ * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
+ * Copyright (c) 1998,1999 by Internet Software Consortium.
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
+ * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#if defined(LIBC_SCCS) && !defined(lint)
+static const char rcsid[] = "$Id: inet_cidr_ntop.c,v 1.4.18.3 2006/10/11 02:32:47 marka Exp $";
+#endif
+
+#include "port_before.h"
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/nameser.h>
+#include <arpa/inet.h>
+
+#include <errno.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+
+#include "port_after.h"
+
+#ifdef SPRINTF_CHAR
+# define SPRINTF(x) strlen(sprintf/**/x)
+#else
+# define SPRINTF(x) ((size_t)sprintf x)
+#endif
+
+static char *
+inet_cidr_ntop_ipv4(const u_char *src, int bits, char *dst, size_t size);
+static char *
+inet_cidr_ntop_ipv6(const u_char *src, int bits, char *dst, size_t size);
+
+/*%
+ * char *
+ * inet_cidr_ntop(af, src, bits, dst, size)
+ *	convert network address from network to presentation format.
+ *	"src"'s size is determined from its "af".
+ * return:
+ *	pointer to dst, or NULL if an error occurred (check errno).
+ * note:
+ *	192.5.5.1/28 has a nonzero host part, which means it isn't a network
+ *	as called for by inet_net_ntop() but it can be a host address with
+ *	an included netmask.
+ * author:
+ *	Paul Vixie (ISC), October 1998
+ */
+char *
+inet_cidr_ntop(int af, const void *src, int bits, char *dst, size_t size) {
+	switch (af) {
+	case AF_INET:
+		return (inet_cidr_ntop_ipv4(src, bits, dst, size));
+	case AF_INET6:
+		return (inet_cidr_ntop_ipv6(src, bits, dst, size));
+	default:
+		errno = EAFNOSUPPORT;
+		return (NULL);
+	}
+}
+
+static int
+decoct(const u_char *src, int bytes, char *dst, size_t size) {
+	char *odst = dst;
+	char *t;
+	int b;
+
+	for (b = 1; b <= bytes; b++) {
+		if (size < sizeof "255.")
+			return (0);
+		t = dst;
+		dst += SPRINTF((dst, "%u", *src++));
+		if (b != bytes) {
+			*dst++ = '.';
+			*dst = '\0';
+		}
+		size -= (size_t)(dst - t);
+	}
+	return (dst - odst);
+}
+
+/*%
+ * static char *
+ * inet_cidr_ntop_ipv4(src, bits, dst, size)
+ *	convert IPv4 network address from network to presentation format.
+ *	"src"'s size is determined from its "af".
+ * return:
+ *	pointer to dst, or NULL if an error occurred (check errno).
+ * note:
+ *	network byte order assumed.  this means 192.5.5.240/28 has
+ *	0b11110000 in its fourth octet.
+ * author:
+ *	Paul Vixie (ISC), October 1998
+ */
+static char *
+inet_cidr_ntop_ipv4(const u_char *src, int bits, char *dst, size_t size) {
+	char *odst = dst;
+	size_t len = 4;
+	size_t b;
+	size_t bytes;
+
+	if ((bits < -1) || (bits > 32)) {
+		errno = EINVAL;
+		return (NULL);
+	}
+
+	/* Find number of significant bytes in address. */
+	if (bits == -1)
+		len = 4;
+	else
+		for (len = 1, b = 1 ; b < 4U; b++)
+			if (*(src + b))
+				len = b + 1;
+
+	/* Format whole octets plus nonzero trailing octets. */
+	bytes = (((bits <= 0) ? 1 : bits) + 7) / 8;
+	if (len > bytes)
+		bytes = len;
+	b = decoct(src, bytes, dst, size);
+	if (b == 0U)
+		goto emsgsize;
+	dst += b;
+	size -= b;
+
+	if (bits != -1) {
+		/* Format CIDR /width. */
+		if (size < sizeof "/32")
+			goto emsgsize;
+		dst += SPRINTF((dst, "/%u", bits));
+	}
+
+	return (odst);
+
+ emsgsize:
+	errno = EMSGSIZE;
+	return (NULL);
+}
+ 
+static char *
+inet_cidr_ntop_ipv6(const u_char *src, int bits, char *dst, size_t size) {
+	/*
+	 * Note that int32_t and int16_t need only be "at least" large enough
+	 * to contain a value of the specified size.  On some systems, like
+	 * Crays, there is no such thing as an integer variable with 16 bits.
+	 * Keep this in mind if you think this function should have been coded
+	 * to use pointer overlays.  All the world's not a VAX.
+	 */
+	char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255/128"];
+	char *tp;
+	struct { int base, len; } best, cur;
+	u_int words[NS_IN6ADDRSZ / NS_INT16SZ];
+	int i;
+
+	if ((bits < -1) || (bits > 128)) {
+		errno = EINVAL;
+		return (NULL);
+	}
+
+	/*
+	 * Preprocess:
+	 *	Copy the input (bytewise) array into a wordwise array.
+	 *	Find the longest run of 0x00's in src[] for :: shorthanding.
+	 */
+	memset(words, '\0', sizeof words);
+	for (i = 0; i < NS_IN6ADDRSZ; i++)
+		words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
+	best.base = -1;
+	best.len = 0;
+	cur.base = -1;
+	cur.len = 0;
+	for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
+		if (words[i] == 0) {
+			if (cur.base == -1)
+				cur.base = i, cur.len = 1;
+			else
+				cur.len++;
+		} else {
+			if (cur.base != -1) {
+				if (best.base == -1 || cur.len > best.len)
+					best = cur;
+				cur.base = -1;
+			}
+		}
+	}
+	if (cur.base != -1) {
+		if (best.base == -1 || cur.len > best.len)
+			best = cur;
+	}
+	if (best.base != -1 && best.len < 2)
+		best.base = -1;
+
+	/*
+	 * Format the result.
+	 */
+	tp = tmp;
+	for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
+		/* Are we inside the best run of 0x00's? */
+		if (best.base != -1 && i >= best.base &&
+		    i < (best.base + best.len)) {
+			if (i == best.base)
+				*tp++ = ':';
+			continue;
+		}
+		/* Are we following an initial run of 0x00s or any real hex? */
+		if (i != 0)
+			*tp++ = ':';
+		/* Is this address an encapsulated IPv4? */
+		if (i == 6 && best.base == 0 && (best.len == 6 ||
+		    (best.len == 7 && words[7] != 0x0001) ||
+		    (best.len == 5 && words[5] == 0xffff))) {
+			int n;
+
+			if (src[15] || bits == -1 || bits > 120)
+				n = 4;
+			else if (src[14] || bits > 112)
+				n = 3;
+			else
+				n = 2;
+			n = decoct(src+12, n, tp, sizeof tmp - (tp - tmp));
+			if (n == 0) {
+				errno = EMSGSIZE;
+				return (NULL);
+			}
+			tp += strlen(tp);
+			break;
+		}
+		tp += SPRINTF((tp, "%x", words[i]));
+	}
+
+	/* Was it a trailing run of 0x00's? */
+	if (best.base != -1 && (best.base + best.len) == 
+	    (NS_IN6ADDRSZ / NS_INT16SZ))
+		*tp++ = ':';
+	*tp = '\0';
+
+	if (bits != -1)
+		tp += SPRINTF((tp, "/%u", bits));
+
+	/*
+	 * Check for overflow, copy, and we're done.
+	 */
+	if ((size_t)(tp - tmp) > size) {
+		errno = EMSGSIZE;
+		return (NULL);
+	}
+	strcpy(dst, tmp);
+	return (dst);
+}
+
+/*! \file */
diff --git a/freebsd-userspace/lib/libc/inet/inet_cidr_pton.c b/freebsd-userspace/lib/libc/inet/inet_cidr_pton.c
new file mode 100644
index 0000000..b0586ff
--- /dev/null
+++ b/freebsd-userspace/lib/libc/inet/inet_cidr_pton.c
@@ -0,0 +1,279 @@
+/*
+ * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
+ * Copyright (c) 1998,1999 by Internet Software Consortium.
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
+ * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#if defined(LIBC_SCCS) && !defined(lint)
+static const char rcsid[] = "$Id: inet_cidr_pton.c,v 1.5.18.1 2005/04/27 05:00:53 sra Exp $";
+#endif
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include "port_before.h"
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/nameser.h>
+#include <arpa/inet.h>
+
+#include <assert.h>
+#include <ctype.h>
+#include <errno.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+
+#include "port_after.h"
+
+#ifdef SPRINTF_CHAR
+# define SPRINTF(x) strlen(sprintf/**/x)
+#else
+# define SPRINTF(x) ((size_t)sprintf x)
+#endif
+
+static int	inet_cidr_pton_ipv4 __P((const char *src, u_char *dst,
+					 int *bits, int ipv6));
+static int	inet_cidr_pton_ipv6 __P((const char *src, u_char *dst,
+					 int *bits));
+
+static int	getbits(const char *, int ipv6);
+
+/*%
+ * int
+ * inet_cidr_pton(af, src, dst, *bits)
+ *	convert network address from presentation to network format.
+ *	accepts inet_pton()'s input for this "af" plus trailing "/CIDR".
+ *	"dst" is assumed large enough for its "af".  "bits" is set to the
+ *	/CIDR prefix length, which can have defaults (like /32 for IPv4).
+ * return:
+ *	-1 if an error occurred (inspect errno; ENOENT means bad format).
+ *	0 if successful conversion occurred.
+ * note:
+ *	192.5.5.1/28 has a nonzero host part, which means it isn't a network
+ *	as called for by inet_net_pton() but it can be a host address with
+ *	an included netmask.
+ * author:
+ *	Paul Vixie (ISC), October 1998
+ */
+int
+inet_cidr_pton(int af, const char *src, void *dst, int *bits) {
+	switch (af) {
+	case AF_INET:
+		return (inet_cidr_pton_ipv4(src, dst, bits, 0));
+	case AF_INET6:
+		return (inet_cidr_pton_ipv6(src, dst, bits));
+	default:
+		errno = EAFNOSUPPORT;
+		return (-1);
+	}
+}
+
+static const char digits[] = "0123456789";
+
+static int
+inet_cidr_pton_ipv4(const char *src, u_char *dst, int *pbits, int ipv6) {
+	const u_char *odst = dst;
+	int n, ch, tmp, bits;
+	size_t size = 4;
+
+	/* Get the mantissa. */
+	while (ch = *src++, (isascii(ch) && isdigit(ch))) {
+		tmp = 0;
+		do {
+			n = strchr(digits, ch) - digits;
+			assert(n >= 0 && n <= 9);
+			tmp *= 10;
+			tmp += n;
+			if (tmp > 255)
+				goto enoent;
+		} while ((ch = *src++) != '\0' && isascii(ch) && isdigit(ch));
+		if (size-- == 0U)
+			goto emsgsize;
+		*dst++ = (u_char) tmp;
+		if (ch == '\0' || ch == '/')
+			break;
+		if (ch != '.')
+			goto enoent;
+	}
+
+	/* Get the prefix length if any. */
+	bits = -1;
+	if (ch == '/' && dst > odst) {
+		bits = getbits(src, ipv6);
+		if (bits == -2)
+			goto enoent;
+	} else if (ch != '\0')
+		goto enoent;
+
+	/* Prefix length can default to /32 only if all four octets spec'd. */
+	if (bits == -1) {
+		if (dst - odst == 4)
+			bits = ipv6 ? 128 : 32;
+		else
+			goto enoent;
+	}
+
+	/* If nothing was written to the destination, we found no address. */
+	if (dst == odst)
+		goto enoent;
+
+	/* If prefix length overspecifies mantissa, life is bad. */
+	if (((bits - (ipv6 ? 96 : 0)) / 8) > (dst - odst))
+		goto enoent;
+
+	/* Extend address to four octets. */
+	while (size-- > 0U)
+		*dst++ = 0;
+
+	*pbits = bits;
+	return (0);
+
+ enoent:
+	errno = ENOENT;
+	return (-1);
+
+ emsgsize:
+	errno = EMSGSIZE;
+	return (-1);
+}
+
+static int
+inet_cidr_pton_ipv6(const char *src, u_char *dst, int *pbits) {
+	static const char xdigits_l[] = "0123456789abcdef",
+			  xdigits_u[] = "0123456789ABCDEF";
+	u_char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp;
+	const char *xdigits, *curtok;
+	int ch, saw_xdigit;
+	u_int val;
+	int bits;
+
+	memset((tp = tmp), '\0', NS_IN6ADDRSZ);
+	endp = tp + NS_IN6ADDRSZ;
+	colonp = NULL;
+	/* Leading :: requires some special handling. */
+	if (*src == ':')
+		if (*++src != ':')
+			return (0);
+	curtok = src;
+	saw_xdigit = 0;
+	val = 0;
+	bits = -1;
+	while ((ch = *src++) != '\0') {
+		const char *pch;
+
+		if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
+			pch = strchr((xdigits = xdigits_u), ch);
+		if (pch != NULL) {
+			val <<= 4;
+			val |= (pch - xdigits);
+			if (val > 0xffff)
+				return (0);
+			saw_xdigit = 1;
+			continue;
+		}
+		if (ch == ':') {
+			curtok = src;
+			if (!saw_xdigit) {
+				if (colonp)
+					return (0);
+				colonp = tp;
+				continue;
+			} else if (*src == '\0') {
+				return (0);
+			}
+			if (tp + NS_INT16SZ > endp)
+				return (0);
+			*tp++ = (u_char) (val >> 8) & 0xff;
+			*tp++ = (u_char) val & 0xff;
+			saw_xdigit = 0;
+			val = 0;
+			continue;
+		}
+		if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) &&
+		    inet_cidr_pton_ipv4(curtok, tp, &bits, 1) == 0) {
+			tp += NS_INADDRSZ;
+			saw_xdigit = 0;
+			break;	/*%< '\\0' was seen by inet_pton4(). */
+		}
+		if (ch == '/') {
+			bits = getbits(src, 1);
+			if (bits == -2)
+				goto enoent;
+			break;
+		}
+		goto enoent;
+	}
+	if (saw_xdigit) {
+		if (tp + NS_INT16SZ > endp)
+			goto emsgsize;
+		*tp++ = (u_char) (val >> 8) & 0xff;
+		*tp++ = (u_char) val & 0xff;
+	}
+	if (colonp != NULL) {
+		/*
+		 * Since some memmove()'s erroneously fail to handle
+		 * overlapping regions, we'll do the shift by hand.
+		 */
+		const int n = tp - colonp;
+		int i;
+
+		if (tp == endp)
+			goto enoent;
+		for (i = 1; i <= n; i++) {
+			endp[- i] = colonp[n - i];
+			colonp[n - i] = 0;
+		}
+		tp = endp;
+	}
+
+	memcpy(dst, tmp, NS_IN6ADDRSZ);
+
+	*pbits = bits;
+	return (0);
+
+ enoent:
+	errno = ENOENT;
+	return (-1);
+
+ emsgsize:
+	errno = EMSGSIZE;
+	return (-1);
+}
+
+static int
+getbits(const char *src, int ipv6) {
+	int bits = 0;
+	char *cp, ch;
+	
+	if (*src == '\0')			/*%< syntax */
+		return (-2);
+	do {
+		ch = *src++;
+		cp = strchr(digits, ch);
+		if (cp == NULL)			/*%< syntax */
+			return (-2);
+		bits *= 10;
+		bits += cp - digits;
+		if (bits == 0 && *src != '\0')	/*%< no leading zeros */
+			return (-2);
+		if (bits > (ipv6 ? 128 : 32))	/*%< range error */
+			return (-2);
+	} while (*src != '\0');
+
+	return (bits);
+}
+
+/*! \file */
diff --git a/freebsd-userspace/lib/libc/inet/inet_lnaof.c b/freebsd-userspace/lib/libc/inet/inet_lnaof.c
new file mode 100644
index 0000000..7cab894
--- /dev/null
+++ b/freebsd-userspace/lib/libc/inet/inet_lnaof.c
@@ -0,0 +1,70 @@
+/*
+ * Copyright (c) 1983, 1993
+ *	The Regents of the University of California.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON 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 ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#if defined(LIBC_SCCS) && !defined(lint)
+static const char sccsid[] = "@(#)inet_lnaof.c	8.1 (Berkeley) 6/4/93";
+#endif /* LIBC_SCCS and not lint */
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include "port_before.h"
+
+#include <sys/param.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+
+#include "port_after.h"
+
+/*%
+ * Return the local network address portion of an
+ * internet address; handles class a/b/c network
+ * number formats.
+ */
+in_addr_t
+inet_lnaof(in)
+	struct in_addr in;
+{
+	in_addr_t i = ntohl(in.s_addr);
+
+	if (IN_CLASSA(i))
+		return ((i)&IN_CLASSA_HOST);
+	else if (IN_CLASSB(i))
+		return ((i)&IN_CLASSB_HOST);
+	else
+		return ((i)&IN_CLASSC_HOST);
+}
+
+/*
+ * Weak aliases for applications that use certain private entry points,
+ * and fail to include <arpa/inet.h>.
+ */
+#undef inet_lnaof
+__weak_reference(__inet_lnaof, inet_lnaof);
+
+/*! \file */
diff --git a/freebsd-userspace/lib/libc/inet/inet_makeaddr.c b/freebsd-userspace/lib/libc/inet/inet_makeaddr.c
new file mode 100644
index 0000000..04a37a0
--- /dev/null
+++ b/freebsd-userspace/lib/libc/inet/inet_makeaddr.c
@@ -0,0 +1,73 @@
+/*
+ * Copyright (c) 1983, 1993
+ *	The Regents of the University of California.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON 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 ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#if defined(LIBC_SCCS) && !defined(lint)
+static const char sccsid[] = "@(#)inet_makeaddr.c	8.1 (Berkeley) 6/4/93";
+#endif /* LIBC_SCCS and not lint */
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include "port_before.h"
+
+#include <sys/param.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+
+#include "port_after.h"
+
+/*%
+ * Formulate an Internet address from network + host.  Used in
+ * building addresses stored in the ifnet structure.
+ */
+struct in_addr
+inet_makeaddr(net, host)
+	in_addr_t net, host;
+{
+	struct in_addr a;
+
+	if (net < 128U)
+		a.s_addr = (net << IN_CLASSA_NSHIFT) | (host & IN_CLASSA_HOST);
+	else if (net < 65536U)
+		a.s_addr = (net << IN_CLASSB_NSHIFT) | (host & IN_CLASSB_HOST);
+	else if (net < 16777216L)
+		a.s_addr = (net << IN_CLASSC_NSHIFT) | (host & IN_CLASSC_HOST);
+	else
+		a.s_addr = net | host;
+	a.s_addr = htonl(a.s_addr);
+	return (a);
+}
+
+/*
+ * Weak aliases for applications that use certain private entry points,
+ * and fail to include <arpa/inet.h>.
+ */
+#undef inet_makeaddr
+__weak_reference(__inet_makeaddr, inet_makeaddr);
+
+/*! \file */
diff --git a/freebsd-userspace/lib/libc/inet/inet_net_ntop.c b/freebsd-userspace/lib/libc/inet/inet_net_ntop.c
new file mode 100644
index 0000000..867f441
--- /dev/null
+++ b/freebsd-userspace/lib/libc/inet/inet_net_ntop.c
@@ -0,0 +1,288 @@
+/*
+ * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
+ * Copyright (c) 1996,1999 by Internet Software Consortium.
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
+ * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#if defined(LIBC_SCCS) && !defined(lint)
+static const char rcsid[] = "$Id: inet_net_ntop.c,v 1.3.18.2 2006/06/20 02:51:32 marka Exp $";
+#endif
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include "port_before.h"
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+
+#include <errno.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+
+#include "port_after.h"
+
+#ifdef SPRINTF_CHAR
+# define SPRINTF(x) strlen(sprintf/**/x)
+#else
+# define SPRINTF(x) ((size_t)sprintf x)
+#endif
+
+static char *	inet_net_ntop_ipv4(const u_char *src, int bits, char *dst,
+		    size_t size);
+static char *	inet_net_ntop_ipv6(const u_char *src, int bits, char *dst,
+		    size_t size);
+
+/*%
+ * char *
+ * inet_net_ntop(af, src, bits, dst, size)
+ *	convert network number from network to presentation format.
+ *	generates CIDR style result always.
+ * return:
+ *	pointer to dst, or NULL if an error occurred (check errno).
+ * author:
+ *	Paul Vixie (ISC), July 1996
+ */
+char *
+inet_net_ntop(af, src, bits, dst, size)
+	int af;
+	const void *src;
+	int bits;
+	char *dst;
+	size_t size;
+{
+	switch (af) {
+	case AF_INET:
+		return (inet_net_ntop_ipv4(src, bits, dst, size));
+	case AF_INET6:
+		return (inet_net_ntop_ipv6(src, bits, dst, size));
+	default:
+		errno = EAFNOSUPPORT;
+		return (NULL);
+	}
+}
+
+/*%
+ * static char *
+ * inet_net_ntop_ipv4(src, bits, dst, size)
+ *	convert IPv4 network number from network to presentation format.
+ *	generates CIDR style result always.
+ * return:
+ *	pointer to dst, or NULL if an error occurred (check errno).
+ * note:
+ *	network byte order assumed.  this means 192.5.5.240/28 has
+ *	0b11110000 in its fourth octet.
+ * author:
+ *	Paul Vixie (ISC), July 1996
+ */
+static char *
+inet_net_ntop_ipv4(src, bits, dst, size)
+	const u_char *src;
+	int bits;
+	char *dst;
+	size_t size;
+{
+	char *odst = dst;
+	char *t;
+	u_int m;
+	int b;
+
+	if (bits < 0 || bits > 32) {
+		errno = EINVAL;
+		return (NULL);
+	}
+
+	if (bits == 0) {
+		if (size < sizeof "0")
+			goto emsgsize;
+		*dst++ = '0';
+		size--;
+		*dst = '\0';
+	}
+
+	/* Format whole octets. */
+	for (b = bits / 8; b > 0; b--) {
+		if (size <= sizeof "255.")
+			goto emsgsize;
+		t = dst;
+		dst += SPRINTF((dst, "%u", *src++));
+		if (b > 1) {
+			*dst++ = '.';
+			*dst = '\0';
+		}
+		size -= (size_t)(dst - t);
+	}
+
+	/* Format partial octet. */
+	b = bits % 8;
+	if (b > 0) {
+		if (size <= sizeof ".255")
+			goto emsgsize;
+		t = dst;
+		if (dst != odst)
+			*dst++ = '.';
+		m = ((1 << b) - 1) << (8 - b);
+		dst += SPRINTF((dst, "%u", *src & m));
+		size -= (size_t)(dst - t);
+	}
+
+	/* Format CIDR /width. */
+	if (size <= sizeof "/32")
+		goto emsgsize;
+	dst += SPRINTF((dst, "/%u", bits));
+	return (odst);
+
+ emsgsize:
+	errno = EMSGSIZE;
+	return (NULL);
+}
+
+/*%
+ * static char *
+ * inet_net_ntop_ipv6(src, bits, fakebits, dst, size)
+ *	convert IPv6 network number from network to presentation format.
+ *	generates CIDR style result always. Picks the shortest representation
+ *	unless the IP is really IPv4.
+ *	always prints specified number of bits (bits).
+ * return:
+ *	pointer to dst, or NULL if an error occurred (check errno).
+ * note:
+ *	network byte order assumed.  this means 192.5.5.240/28 has
+ *	0b11110000 in its fourth octet.
+ * author:
+ *	Vadim Kogan (UCB), June 2001
+ *  Original version (IPv4) by Paul Vixie (ISC), July 1996
+ */
+
+static char *
+inet_net_ntop_ipv6(const u_char *src, int bits, char *dst, size_t size) {
+	u_int	m;
+	int	b;
+	int	p;
+	int	zero_s, zero_l, tmp_zero_s, tmp_zero_l;
+	int	i;
+	int	is_ipv4 = 0;
+	unsigned char inbuf[16];
+	char outbuf[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255/128")];
+	char	*cp;
+	int	words;
+	u_char	*s;
+
+	if (bits < 0 || bits > 128) {
+		errno = EINVAL;
+		return (NULL);
+	}
+
+	cp = outbuf;
+
+	if (bits == 0) {
+		*cp++ = ':';
+		*cp++ = ':';
+		*cp = '\0';
+	} else {
+		/* Copy src to private buffer.  Zero host part. */
+		p = (bits + 7) / 8;
+		memcpy(inbuf, src, p);
+		memset(inbuf + p, 0, 16 - p);
+		b = bits % 8;
+		if (b != 0) {
+			m = ~0 << (8 - b);
+			inbuf[p-1] &= m;
+		}
+
+		s = inbuf;
+
+		/* how many words need to be displayed in output */
+		words = (bits + 15) / 16;
+		if (words == 1)
+			words = 2;
+
+		/* Find the longest substring of zero's */
+		zero_s = zero_l = tmp_zero_s = tmp_zero_l = 0;
+		for (i = 0; i < (words * 2); i += 2) {
+			if ((s[i] | s[i+1]) == 0) {
+				if (tmp_zero_l == 0)
+					tmp_zero_s = i / 2;
+				tmp_zero_l++;
+			} else {
+				if (tmp_zero_l && zero_l < tmp_zero_l) {
+					zero_s = tmp_zero_s;
+					zero_l = tmp_zero_l;
+					tmp_zero_l = 0;
+				}
+			}
+		}
+
+		if (tmp_zero_l && zero_l < tmp_zero_l) {
+			zero_s = tmp_zero_s;
+			zero_l = tmp_zero_l;
+		}
+
+		if (zero_l != words && zero_s == 0 && ((zero_l == 6) ||
+		    ((zero_l == 5 && s[10] == 0xff && s[11] == 0xff) ||
+		    ((zero_l == 7 && s[14] != 0 && s[15] != 1)))))
+			is_ipv4 = 1;
+
+		/* Format whole words. */
+		for (p = 0; p < words; p++) {
+			if (zero_l != 0 && p >= zero_s && p < zero_s + zero_l) {
+				/* Time to skip some zeros */
+				if (p == zero_s)
+					*cp++ = ':';
+				if (p == words - 1)
+					*cp++ = ':';
+				s++;
+				s++;
+				continue;
+			}
+
+			if (is_ipv4 && p > 5 ) {
+				*cp++ = (p == 6) ? ':' : '.';
+				cp += SPRINTF((cp, "%u", *s++));
+				/* we can potentially drop the last octet */
+				if (p != 7 || bits > 120) {
+					*cp++ = '.';
+					cp += SPRINTF((cp, "%u", *s++));
+				}
+			} else {
+				if (cp != outbuf)
+					*cp++ = ':';
+				cp += SPRINTF((cp, "%x", *s * 256 + s[1]));
+				s += 2;
+			}
+		}
+	}
+	/* Format CIDR /width. */
+	sprintf(cp, "/%u", bits);
+	if (strlen(outbuf) + 1 > size)
+		goto emsgsize;
+	strcpy(dst, outbuf);
+
+	return (dst);
+
+emsgsize:
+	errno = EMSGSIZE;
+	return (NULL);
+}
+
+/*
+ * Weak aliases for applications that use certain private entry points,
+ * and fail to include <arpa/inet.h>.
+ */
+#undef inet_net_ntop
+__weak_reference(__inet_net_ntop, inet_net_ntop);
+
+/*! \file */
diff --git a/freebsd-userspace/lib/libc/inet/inet_net_pton.c b/freebsd-userspace/lib/libc/inet/inet_net_pton.c
new file mode 100644
index 0000000..74df38b
--- /dev/null
+++ b/freebsd-userspace/lib/libc/inet/inet_net_pton.c
@@ -0,0 +1,416 @@
+/*
+ * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
+ * Copyright (c) 1996,1999 by Internet Software Consortium.
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
+ * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#if defined(LIBC_SCCS) && !defined(lint)
+static const char rcsid[] = "$Id: inet_net_pton.c,v 1.7.18.2 2008/08/26 04:42:43 marka Exp $";
+#endif
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include "port_before.h"
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/nameser.h>
+#include <arpa/inet.h>
+
+#include <assert.h>
+#include <ctype.h>
+#include <errno.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+
+#include "port_after.h"
+
+#ifdef SPRINTF_CHAR
+# define SPRINTF(x) strlen(sprintf/**/x)
+#else
+# define SPRINTF(x) ((size_t)sprintf x)
+#endif
+
+/*%
+ * static int
+ * inet_net_pton_ipv4(src, dst, size)
+ *	convert IPv4 network number from presentation to network format.
+ *	accepts hex octets, hex strings, decimal octets, and /CIDR.
+ *	"size" is in bytes and describes "dst".
+ * return:
+ *	number of bits, either imputed classfully or specified with /CIDR,
+ *	or -1 if some failure occurred (check errno).  ENOENT means it was
+ *	not an IPv4 network specification.
+ * note:
+ *	network byte order assumed.  this means 192.5.5.240/28 has
+ *	0b11110000 in its fourth octet.
+ * author:
+ *	Paul Vixie (ISC), June 1996
+ */
+static int
+inet_net_pton_ipv4(const char *src, u_char *dst, size_t size) {
+	static const char xdigits[] = "0123456789abcdef";
+	static const char digits[] = "0123456789";
+	int n, ch, tmp = 0, dirty, bits;
+	const u_char *odst = dst;
+
+	ch = *src++;
+	if (ch == '0' && (src[0] == 'x' || src[0] == 'X')
+	    && isascii((unsigned char)(src[1]))
+	    && isxdigit((unsigned char)(src[1]))) {
+		/* Hexadecimal: Eat nybble string. */
+		if (size <= 0U)
+			goto emsgsize;
+		dirty = 0;
+		src++;	/*%< skip x or X. */
+		while ((ch = *src++) != '\0' && isascii(ch) && isxdigit(ch)) {
+			if (isupper(ch))
+				ch = tolower(ch);
+			n = strchr(xdigits, ch) - xdigits;
+			assert(n >= 0 && n <= 15);
+			if (dirty == 0)
+				tmp = n;
+			else
+				tmp = (tmp << 4) | n;
+			if (++dirty == 2) {
+				if (size-- <= 0U)
+					goto emsgsize;
+				*dst++ = (u_char) tmp;
+				dirty = 0;
+			}
+		}
+		if (dirty) {  /*%< Odd trailing nybble? */
+			if (size-- <= 0U)
+				goto emsgsize;
+			*dst++ = (u_char) (tmp << 4);
+		}
+	} else if (isascii(ch) && isdigit(ch)) {
+		/* Decimal: eat dotted digit string. */
+		for (;;) {
+			tmp = 0;
+			do {
+				n = strchr(digits, ch) - digits;
+				assert(n >= 0 && n <= 9);
+				tmp *= 10;
+				tmp += n;
+				if (tmp > 255)
+					goto enoent;
+			} while ((ch = *src++) != '\0' &&
+				 isascii(ch) && isdigit(ch));
+			if (size-- <= 0U)
+				goto emsgsize;
+			*dst++ = (u_char) tmp;
+			if (ch == '\0' || ch == '/')
+				break;
+			if (ch != '.')
+				goto enoent;
+			ch = *src++;
+			if (!isascii(ch) || !isdigit(ch))
+				goto enoent;
+		}
+	} else
+		goto enoent;
+
+	bits = -1;
+	if (ch == '/' && isascii((unsigned char)(src[0])) &&
+	    isdigit((unsigned char)(src[0])) && dst > odst) {
+		/* CIDR width specifier.  Nothing can follow it. */
+		ch = *src++;	/*%< Skip over the /. */
+		bits = 0;
+		do {
+			n = strchr(digits, ch) - digits;
+			assert(n >= 0 && n <= 9);
+			bits *= 10;
+			bits += n;
+			if (bits > 32)
+				goto enoent;
+		} while ((ch = *src++) != '\0' && isascii(ch) && isdigit(ch));
+		if (ch != '\0')
+			goto enoent;
+	}
+
+	/* Firey death and destruction unless we prefetched EOS. */
+	if (ch != '\0')
+		goto enoent;
+
+	/* If nothing was written to the destination, we found no address. */
+	if (dst == odst)
+		goto enoent;
+	/* If no CIDR spec was given, infer width from net class. */
+	if (bits == -1) {
+		if (*odst >= 240)	/*%< Class E */
+			bits = 32;
+		else if (*odst >= 224)	/*%< Class D */
+			bits = 8;
+		else if (*odst >= 192)	/*%< Class C */
+			bits = 24;
+		else if (*odst >= 128)	/*%< Class B */
+			bits = 16;
+		else			/*%< Class A */
+			bits = 8;
+		/* If imputed mask is narrower than specified octets, widen. */
+		if (bits < ((dst - odst) * 8))
+			bits = (dst - odst) * 8;
+		/*
+		 * If there are no additional bits specified for a class D
+		 * address adjust bits to 4.
+		 */
+		if (bits == 8 && *odst == 224)
+			bits = 4;
+	}
+	/* Extend network to cover the actual mask. */
+	while (bits > ((dst - odst) * 8)) {
+		if (size-- <= 0U)
+			goto emsgsize;
+		*dst++ = '\0';
+	}
+	return (bits);
+
+ enoent:
+	errno = ENOENT;
+	return (-1);
+
+ emsgsize:
+	errno = EMSGSIZE;
+	return (-1);
+}
+
+static int
+getbits(const char *src, int *bitsp) {
+	static const char digits[] = "0123456789";
+	int n;
+	int val;
+	char ch;
+
+	val = 0;
+	n = 0;
+	while ((ch = *src++) != '\0') {
+		const char *pch;
+
+		pch = strchr(digits, ch);
+		if (pch != NULL) {
+			if (n++ != 0 && val == 0)	/*%< no leading zeros */
+				return (0);
+			val *= 10;
+			val += (pch - digits);
+			if (val > 128)			/*%< range */
+				return (0);
+			continue;
+		}
+		return (0);
+	}
+	if (n == 0)
+		return (0);
+	*bitsp = val;
+	return (1);
+}
+
+static int
+getv4(const char *src, u_char *dst, int *bitsp) {
+	static const char digits[] = "0123456789";
+	u_char *odst = dst;
+	int n;
+	u_int val;
+	char ch;
+
+	val = 0;
+	n = 0;
+	while ((ch = *src++) != '\0') {
+		const char *pch;
+
+		pch = strchr(digits, ch);
+		if (pch != NULL) {
+			if (n++ != 0 && val == 0)	/*%< no leading zeros */
+				return (0);
+			val *= 10;
+			val += (pch - digits);
+			if (val > 255)			/*%< range */
+				return (0);
+			continue;
+		}
+		if (ch == '.' || ch == '/') {
+			if (dst - odst > 3)		/*%< too many octets? */
+				return (0);
+			*dst++ = val;
+			if (ch == '/')
+				return (getbits(src, bitsp));
+			val = 0;
+			n = 0;
+			continue;
+		}
+		return (0);
+	}
+	if (n == 0)
+		return (0);
+	if (dst - odst > 3)		/*%< too many octets? */
+		return (0);
+	*dst++ = val;
+	return (1);
+}
+
+static int
+inet_net_pton_ipv6(const char *src, u_char *dst, size_t size) {
+	static const char xdigits_l[] = "0123456789abcdef",
+			  xdigits_u[] = "0123456789ABCDEF";
+	u_char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp;
+	const char *xdigits, *curtok;
+	int ch, saw_xdigit;
+	u_int val;
+	int digits;
+	int bits;
+	size_t bytes;
+	int words;
+	int ipv4;
+
+	memset((tp = tmp), '\0', NS_IN6ADDRSZ);
+	endp = tp + NS_IN6ADDRSZ;
+	colonp = NULL;
+	/* Leading :: requires some special handling. */
+	if (*src == ':')
+		if (*++src != ':')
+			goto enoent;
+	curtok = src;
+	saw_xdigit = 0;
+	val = 0;
+	digits = 0;
+	bits = -1;
+	ipv4 = 0;
+	while ((ch = *src++) != '\0') {
+		const char *pch;
+
+		if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
+			pch = strchr((xdigits = xdigits_u), ch);
+		if (pch != NULL) {
+			val <<= 4;
+			val |= (pch - xdigits);
+			if (++digits > 4)
+				goto enoent;
+			saw_xdigit = 1;
+			continue;
+		}
+		if (ch == ':') {
+			curtok = src;
+			if (!saw_xdigit) {
+				if (colonp)
+					goto enoent;
+				colonp = tp;
+				continue;
+			} else if (*src == '\0')
+				goto enoent;
+			if (tp + NS_INT16SZ > endp)
+				return (0);
+			*tp++ = (u_char) (val >> 8) & 0xff;
+			*tp++ = (u_char) val & 0xff;
+			saw_xdigit = 0;
+			digits = 0;
+			val = 0;
+			continue;
+		}
+		if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) &&
+		     getv4(curtok, tp, &bits) > 0) {
+			tp += NS_INADDRSZ;
+			saw_xdigit = 0;
+			ipv4 = 1;
+			break;	/*%< '\\0' was seen by inet_pton4(). */
+		}
+		if (ch == '/' && getbits(src, &bits) > 0)
+			break;
+		goto enoent;
+	}
+	if (saw_xdigit) {
+		if (tp + NS_INT16SZ > endp)
+			goto enoent;
+		*tp++ = (u_char) (val >> 8) & 0xff;
+		*tp++ = (u_char) val & 0xff;
+	}
+	if (bits == -1)
+		bits = 128;
+
+	words = (bits + 15) / 16;
+	if (words < 2)
+		words = 2;
+	if (ipv4)
+		words = 8;
+	endp =  tmp + 2 * words;
+
+	if (colonp != NULL) {
+		/*
+		 * Since some memmove()'s erroneously fail to handle
+		 * overlapping regions, we'll do the shift by hand.
+		 */
+		const int n = tp - colonp;
+		int i;
+
+		if (tp == endp)
+			goto enoent;
+		for (i = 1; i <= n; i++) {
+			endp[- i] = colonp[n - i];
+			colonp[n - i] = 0;
+		}
+		tp = endp;
+	}
+	if (tp != endp)
+		goto enoent;
+
+	bytes = (bits + 7) / 8;
+	if (bytes > size)
+		goto emsgsize;
+	memcpy(dst, tmp, bytes);
+	return (bits);
+
+ enoent:
+	errno = ENOENT;
+	return (-1);
+
+ emsgsize:
+	errno = EMSGSIZE;
+	return (-1);
+}
+
+/*%
+ * int
+ * inet_net_pton(af, src, dst, size)
+ *	convert network number from presentation to network format.
+ *	accepts hex octets, hex strings, decimal octets, and /CIDR.
+ *	"size" is in bytes and describes "dst".
+ * return:
+ *	number of bits, either imputed classfully or specified with /CIDR,
+ *	or -1 if some failure occurred (check errno).  ENOENT means it was
+ *	not a valid network specification.
+ * author:
+ *	Paul Vixie (ISC), June 1996
+ */
+int
+inet_net_pton(int af, const char *src, void *dst, size_t size) {
+	switch (af) {
+	case AF_INET:
+		return (inet_net_pton_ipv4(src, dst, size));
+	case AF_INET6:
+		return (inet_net_pton_ipv6(src, dst, size));
+	default:
+		errno = EAFNOSUPPORT;
+		return (-1);
+	}
+}
+
+/*
+ * Weak aliases for applications that use certain private entry points,
+ * and fail to include <arpa/inet.h>.
+ */
+#undef inet_net_pton
+__weak_reference(__inet_net_pton, inet_net_pton);
+
+/*! \file */
diff --git a/freebsd-userspace/lib/libc/inet/inet_neta.c b/freebsd-userspace/lib/libc/inet/inet_neta.c
new file mode 100644
index 0000000..72ac549
--- /dev/null
+++ b/freebsd-userspace/lib/libc/inet/inet_neta.c
@@ -0,0 +1,98 @@
+/*
+ * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
+ * Copyright (c) 1996,1999 by Internet Software Consortium.
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
+ * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#if defined(LIBC_SCCS) && !defined(lint)
+static const char rcsid[] = "$Id: inet_neta.c,v 1.2.18.1 2005/04/27 05:00:53 sra Exp $";
+#endif
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include "port_before.h"
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+
+#include <errno.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "port_after.h"
+
+#ifdef SPRINTF_CHAR
+# define SPRINTF(x) strlen(sprintf/**/x)
+#else
+# define SPRINTF(x) ((size_t)sprintf x)
+#endif
+
+/*%
+ * char *
+ * inet_neta(src, dst, size)
+ *	format an in_addr_t network number into presentation format.
+ * return:
+ *	pointer to dst, or NULL if an error occurred (check errno).
+ * note:
+ *	format of ``src'' is as for inet_network().
+ * author:
+ *	Paul Vixie (ISC), July 1996
+ */
+char *
+inet_neta(src, dst, size)
+	in_addr_t src;
+	char *dst;
+	size_t size;
+{
+	char *odst = dst;
+	char *tp;
+
+	while (src & 0xffffffff) {
+		u_char b = (src & 0xff000000) >> 24;
+
+		src <<= 8;
+		if (b) {
+			if (size < sizeof "255.")
+				goto emsgsize;
+			tp = dst;
+			dst += SPRINTF((dst, "%u", b));
+			if (src != 0L) {
+				*dst++ = '.';
+				*dst = '\0';
+			}
+			size -= (size_t)(dst - tp);
+		}
+	}
+	if (dst == odst) {
+		if (size < sizeof "0.0.0.0")
+			goto emsgsize;
+		strcpy(dst, "0.0.0.0");
+	}
+	return (odst);
+
+ emsgsize:
+	errno = EMSGSIZE;
+	return (NULL);
+}
+
+/*
+ * Weak aliases for applications that use certain private entry points,
+ * and fail to include <arpa/inet.h>.
+ */
+#undef inet_neta
+__weak_reference(__inet_neta, inet_neta);
+
+/*! \file */
diff --git a/freebsd-userspace/lib/libc/inet/inet_netof.c b/freebsd-userspace/lib/libc/inet/inet_netof.c
new file mode 100644
index 0000000..8931c30
--- /dev/null
+++ b/freebsd-userspace/lib/libc/inet/inet_netof.c
@@ -0,0 +1,69 @@
+/*
+ * Copyright (c) 1983, 1993
+ *	The Regents of the University of California.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON 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 ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#if defined(LIBC_SCCS) && !defined(lint)
+static const char sccsid[] = "@(#)inet_netof.c	8.1 (Berkeley) 6/4/93";
+#endif /* LIBC_SCCS and not lint */
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include "port_before.h"
+
+#include <sys/param.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+
+#include "port_after.h"
+
+/*%
+ * Return the network number from an internet
+ * address; handles class a/b/c network #'s.
+ */
+in_addr_t
+inet_netof(in)
+	struct in_addr in;
+{
+	in_addr_t i = ntohl(in.s_addr);
+
+	if (IN_CLASSA(i))
+		return (((i)&IN_CLASSA_NET) >> IN_CLASSA_NSHIFT);
+	else if (IN_CLASSB(i))
+		return (((i)&IN_CLASSB_NET) >> IN_CLASSB_NSHIFT);
+	else
+		return (((i)&IN_CLASSC_NET) >> IN_CLASSC_NSHIFT);
+}
+
+/*
+ * Weak aliases for applications that use certain private entry points,
+ * and fail to include <arpa/inet.h>.
+ */
+#undef inet_netof
+__weak_reference(__inet_netof, inet_netof);
+
+/*! \file */
diff --git a/freebsd-userspace/lib/libc/inet/inet_network.c b/freebsd-userspace/lib/libc/inet/inet_network.c
new file mode 100644
index 0000000..254db41
--- /dev/null
+++ b/freebsd-userspace/lib/libc/inet/inet_network.c
@@ -0,0 +1,111 @@
+/*
+ * Copyright (c) 1983, 1993
+ *	The Regents of the University of California.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON 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 ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#if defined(LIBC_SCCS) && !defined(lint)
+static const char sccsid[] = "@(#)inet_network.c	8.1 (Berkeley) 6/4/93";
+#endif /* LIBC_SCCS and not lint */
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include "port_before.h"
+
+#include <sys/types.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <ctype.h>
+
+#include "port_after.h"
+
+/*%
+ * Internet network address interpretation routine.
+ * The library routines call this routine to interpret
+ * network numbers.
+ */
+in_addr_t
+inet_network(cp)
+	const char *cp;
+{
+	in_addr_t val, base, n;
+	char c;
+	in_addr_t parts[4], *pp = parts;
+	int i, digit;
+
+again:
+	val = 0; base = 10; digit = 0;
+	if (*cp == '0')
+		digit = 1, base = 8, cp++;
+	if (*cp == 'x' || *cp == 'X')
+		base = 16, cp++;
+	while ((c = *cp) != 0) {
+		if (isdigit((unsigned char)c)) {
+			if (base == 8U && (c == '8' || c == '9'))
+				return (INADDR_NONE);
+			val = (val * base) + (c - '0');
+			cp++;
+			digit = 1;
+			continue;
+		}
+		if (base == 16U && isxdigit((unsigned char)c)) {
+			val = (val << 4) +
+			      (c + 10 - (islower((unsigned char)c) ? 'a' : 'A'));
+			cp++;
+			digit = 1;
+			continue;
+		}
+		break;
+	}
+	if (!digit)
+		return (INADDR_NONE);
+	if (pp >= parts + 4 || val > 0xffU)
+		return (INADDR_NONE);
+	if (*cp == '.') {
+		*pp++ = val, cp++;
+		goto again;
+	}
+	if (*cp && !isspace(*cp&0xff))
+		return (INADDR_NONE);
+	*pp++ = val;
+	n = pp - parts;
+	if (n > 4U)
+		return (INADDR_NONE);
+	for (val = 0, i = 0; i < n; i++) {
+		val <<= 8;
+		val |= parts[i] & 0xff;
+	}
+	return (val);
+}
+
+/*
+ * Weak aliases for applications that use certain private entry points,
+ * and fail to include <arpa/inet.h>.
+ */
+#undef inet_network
+__weak_reference(__inet_network, inet_network);
+
+/*! \file */
diff --git a/freebsd-userspace/lib/libc/inet/inet_ntoa.c b/freebsd-userspace/lib/libc/inet/inet_ntoa.c
new file mode 100644
index 0000000..f5d69fa
--- /dev/null
+++ b/freebsd-userspace/lib/libc/inet/inet_ntoa.c
@@ -0,0 +1,78 @@
+/*-
+ * Copyright (c) 1983, 1993
+ *	The Regents of the University of California.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON 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 ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#if defined(LIBC_SCCS) && !defined(lint)
+static const char sccsid[] = "@(#)inet_ntoa.c	8.1 (Berkeley) 6/4/93";
+static const char rcsid[] = "$Id: inet_ntoa.c,v 1.1.352.1 2005/04/27 05:00:54 sra Exp $";
+#endif /* LIBC_SCCS and not lint */
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include "port_before.h"
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+
+#include <stdio.h>
+#include <string.h>
+
+#include "port_after.h"
+
+/*%
+ * Convert network-format internet address
+ * to base 256 d.d.d.d representation.
+ */
+/*const*/ char *
+inet_ntoa(struct in_addr in) {
+	static char ret[18];
+
+	strcpy(ret, "[inet_ntoa error]");
+	(void) inet_ntop(AF_INET, &in, ret, sizeof ret);
+	return (ret);
+}
+
+char *
+inet_ntoa_r(struct in_addr in, char *buf, socklen_t size)
+{
+
+	(void) inet_ntop(AF_INET, &in, buf, size);
+	return (buf);
+}
+
+/*
+ * Weak aliases for applications that use certain private entry points,
+ * and fail to include <arpa/inet.h>.
+ */
+#undef inet_ntoa
+__weak_reference(__inet_ntoa, inet_ntoa);
+__weak_reference(__inet_ntoa_r, inet_ntoa_r);
+
+/*! \file */
diff --git a/freebsd-userspace/lib/libc/inet/inet_ntop.c b/freebsd-userspace/lib/libc/inet/inet_ntop.c
new file mode 100644
index 0000000..6d21027
--- /dev/null
+++ b/freebsd-userspace/lib/libc/inet/inet_ntop.c
@@ -0,0 +1,203 @@
+/*
+ * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
+ * Copyright (c) 1996-1999 by Internet Software Consortium.
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
+ * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#if defined(LIBC_SCCS) && !defined(lint)
+static const char rcsid[] = "$Id: inet_ntop.c,v 1.3.18.2 2005/11/03 23:02:22 marka Exp $";
+#endif /* LIBC_SCCS and not lint */
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include "port_before.h"
+
+#include <sys/param.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <arpa/nameser.h>
+
+#include <errno.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "port_after.h"
+
+/*%
+ * WARNING: Don't even consider trying to compile this on a system where
+ * sizeof(int) < 4.  sizeof(int) > 4 is fine; all the world's not a VAX.
+ */
+
+static const char *inet_ntop4(const u_char *src, char *dst, socklen_t size);
+static const char *inet_ntop6(const u_char *src, char *dst, socklen_t size);
+
+/* char *
+ * inet_ntop(af, src, dst, size)
+ *	convert a network format address to presentation format.
+ * return:
+ *	pointer to presentation format address (`dst'), or NULL (see errno).
+ * author:
+ *	Paul Vixie, 1996.
+ */
+const char *
+inet_ntop(int af, const void * __restrict src, char * __restrict dst,
+    socklen_t size)
+{
+	switch (af) {
+	case AF_INET:
+		return (inet_ntop4(src, dst, size));
+	case AF_INET6:
+		return (inet_ntop6(src, dst, size));
+	default:
+		errno = EAFNOSUPPORT;
+		return (NULL);
+	}
+	/* NOTREACHED */
+}
+
+/* const char *
+ * inet_ntop4(src, dst, size)
+ *	format an IPv4 address
+ * return:
+ *	`dst' (as a const)
+ * notes:
+ *	(1) uses no statics
+ *	(2) takes a u_char* not an in_addr as input
+ * author:
+ *	Paul Vixie, 1996.
+ */
+static const char *
+inet_ntop4(const u_char *src, char *dst, socklen_t size)
+{
+	static const char fmt[] = "%u.%u.%u.%u";
+	char tmp[sizeof "255.255.255.255"];
+	int l;
+
+	l = snprintf(tmp, sizeof(tmp), fmt, src[0], src[1], src[2], src[3]);
+	if (l <= 0 || (socklen_t) l >= size) {
+		errno = ENOSPC;
+		return (NULL);
+	}
+	strlcpy(dst, tmp, size);
+	return (dst);
+}
+
+/* const char *
+ * inet_ntop6(src, dst, size)
+ *	convert IPv6 binary address into presentation (printable) format
+ * author:
+ *	Paul Vixie, 1996.
+ */
+static const char *
+inet_ntop6(const u_char *src, char *dst, socklen_t size)
+{
+	/*
+	 * Note that int32_t and int16_t need only be "at least" large enough
+	 * to contain a value of the specified size.  On some systems, like
+	 * Crays, there is no such thing as an integer variable with 16 bits.
+	 * Keep this in mind if you think this function should have been coded
+	 * to use pointer overlays.  All the world's not a VAX.
+	 */
+	char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"], *tp;
+	struct { int base, len; } best, cur;
+	u_int words[NS_IN6ADDRSZ / NS_INT16SZ];
+	int i;
+
+	/*
+	 * Preprocess:
+	 *	Copy the input (bytewise) array into a wordwise array.
+	 *	Find the longest run of 0x00's in src[] for :: shorthanding.
+	 */
+	memset(words, '\0', sizeof words);
+	for (i = 0; i < NS_IN6ADDRSZ; i++)
+		words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
+	best.base = -1;
+	best.len = 0;
+	cur.base = -1;
+	cur.len = 0;
+	for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
+		if (words[i] == 0) {
+			if (cur.base == -1)
+				cur.base = i, cur.len = 1;
+			else
+				cur.len++;
+		} else {
+			if (cur.base != -1) {
+				if (best.base == -1 || cur.len > best.len)
+					best = cur;
+				cur.base = -1;
+			}
+		}
+	}
+	if (cur.base != -1) {
+		if (best.base == -1 || cur.len > best.len)
+			best = cur;
+	}
+	if (best.base != -1 && best.len < 2)
+		best.base = -1;
+
+	/*
+	 * Format the result.
+	 */
+	tp = tmp;
+	for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
+		/* Are we inside the best run of 0x00's? */
+		if (best.base != -1 && i >= best.base &&
+		    i < (best.base + best.len)) {
+			if (i == best.base)
+				*tp++ = ':';
+			continue;
+		}
+		/* Are we following an initial run of 0x00s or any real hex? */
+		if (i != 0)
+			*tp++ = ':';
+		/* Is this address an encapsulated IPv4? */
+		if (i == 6 && best.base == 0 && (best.len == 6 ||
+		    (best.len == 7 && words[7] != 0x0001) ||
+		    (best.len == 5 && words[5] == 0xffff))) {
+			if (!inet_ntop4(src+12, tp, sizeof tmp - (tp - tmp)))
+				return (NULL);
+			tp += strlen(tp);
+			break;
+		}
+		tp += sprintf(tp, "%x", words[i]);
+	}
+	/* Was it a trailing run of 0x00's? */
+	if (best.base != -1 && (best.base + best.len) == 
+	    (NS_IN6ADDRSZ / NS_INT16SZ))
+		*tp++ = ':';
+	*tp++ = '\0';
+
+	/*
+	 * Check for overflow, copy, and we're done.
+	 */
+	if ((socklen_t)(tp - tmp) > size) {
+		errno = ENOSPC;
+		return (NULL);
+	}
+	strcpy(dst, tmp);
+	return (dst);
+}
+
+/*
+ * Weak aliases for applications that use certain private entry points,
+ * and fail to include <arpa/inet.h>.
+ */
+#undef inet_ntop
+__weak_reference(__inet_ntop, inet_ntop);
+
+/*! \file */
diff --git a/freebsd-userspace/lib/libc/inet/inet_pton.c b/freebsd-userspace/lib/libc/inet/inet_pton.c
new file mode 100644
index 0000000..ae65099
--- /dev/null
+++ b/freebsd-userspace/lib/libc/inet/inet_pton.c
@@ -0,0 +1,225 @@
+/*
+ * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
+ * Copyright (c) 1996,1999 by Internet Software Consortium.
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
+ * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#if defined(LIBC_SCCS) && !defined(lint)
+static const char rcsid[] = "$Id: inet_pton.c,v 1.3.18.2 2005/07/28 07:38:07 marka Exp $";
+#endif /* LIBC_SCCS and not lint */
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include "port_before.h"
+#include <sys/param.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <arpa/nameser.h>
+#include <string.h>
+#include <errno.h>
+#include "port_after.h"
+
+/*%
+ * WARNING: Don't even consider trying to compile this on a system where
+ * sizeof(int) < 4.  sizeof(int) > 4 is fine; all the world's not a VAX.
+ */
+
+static int	inet_pton4(const char *src, u_char *dst);
+static int	inet_pton6(const char *src, u_char *dst);
+
+/* int
+ * inet_pton(af, src, dst)
+ *	convert from presentation format (which usually means ASCII printable)
+ *	to network format (which is usually some kind of binary format).
+ * return:
+ *	1 if the address was valid for the specified address family
+ *	0 if the address wasn't valid (`dst' is untouched in this case)
+ *	-1 if some other error occurred (`dst' is untouched in this case, too)
+ * author:
+ *	Paul Vixie, 1996.
+ */
+int
+inet_pton(int af, const char * __restrict src, void * __restrict dst)
+{
+	switch (af) {
+	case AF_INET:
+		return (inet_pton4(src, dst));
+	case AF_INET6:
+		return (inet_pton6(src, dst));
+	default:
+		errno = EAFNOSUPPORT;
+		return (-1);
+	}
+	/* NOTREACHED */
+}
+
+/* int
+ * inet_pton4(src, dst)
+ *	like inet_aton() but without all the hexadecimal and shorthand.
+ * return:
+ *	1 if `src' is a valid dotted quad, else 0.
+ * notice:
+ *	does not touch `dst' unless it's returning 1.
+ * author:
+ *	Paul Vixie, 1996.
+ */
+static int
+inet_pton4(const char *src, u_char *dst)
+{
+	static const char digits[] = "0123456789";
+	int saw_digit, octets, ch;
+	u_char tmp[NS_INADDRSZ], *tp;
+
+	saw_digit = 0;
+	octets = 0;
+	*(tp = tmp) = 0;
+	while ((ch = *src++) != '\0') {
+		const char *pch;
+
+		if ((pch = strchr(digits, ch)) != NULL) {
+			u_int new = *tp * 10 + (pch - digits);
+
+			if (saw_digit && *tp == 0)
+				return (0);
+			if (new > 255)
+				return (0);
+			*tp = new;
+			if (!saw_digit) {
+				if (++octets > 4)
+					return (0);
+				saw_digit = 1;
+			}
+		} else if (ch == '.' && saw_digit) {
+			if (octets == 4)
+				return (0);
+			*++tp = 0;
+			saw_digit = 0;
+		} else
+			return (0);
+	}
+	if (octets < 4)
+		return (0);
+	memcpy(dst, tmp, NS_INADDRSZ);
+	return (1);
+}
+
+/* int
+ * inet_pton6(src, dst)
+ *	convert presentation level address to network order binary form.
+ * return:
+ *	1 if `src' is a valid [RFC1884 2.2] address, else 0.
+ * notice:
+ *	(1) does not touch `dst' unless it's returning 1.
+ *	(2) :: in a full address is silently ignored.
+ * credit:
+ *	inspired by Mark Andrews.
+ * author:
+ *	Paul Vixie, 1996.
+ */
+static int
+inet_pton6(const char *src, u_char *dst)
+{
+	static const char xdigits_l[] = "0123456789abcdef",
+			  xdigits_u[] = "0123456789ABCDEF";
+	u_char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp;
+	const char *xdigits, *curtok;
+	int ch, seen_xdigits;
+	u_int val;
+
+	memset((tp = tmp), '\0', NS_IN6ADDRSZ);
+	endp = tp + NS_IN6ADDRSZ;
+	colonp = NULL;
+	/* Leading :: requires some special handling. */
+	if (*src == ':')
+		if (*++src != ':')
+			return (0);
+	curtok = src;
+	seen_xdigits = 0;
+	val = 0;
+	while ((ch = *src++) != '\0') {
+		const char *pch;
+
+		if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
+			pch = strchr((xdigits = xdigits_u), ch);
+		if (pch != NULL) {
+			val <<= 4;
+			val |= (pch - xdigits);
+			if (++seen_xdigits > 4)
+				return (0);
+			continue;
+		}
+		if (ch == ':') {
+			curtok = src;
+			if (!seen_xdigits) {
+				if (colonp)
+					return (0);
+				colonp = tp;
+				continue;
+			} else if (*src == '\0') {
+				return (0);
+			}
+			if (tp + NS_INT16SZ > endp)
+				return (0);
+			*tp++ = (u_char) (val >> 8) & 0xff;
+			*tp++ = (u_char) val & 0xff;
+			seen_xdigits = 0;
+			val = 0;
+			continue;
+		}
+		if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) &&
+		    inet_pton4(curtok, tp) > 0) {
+			tp += NS_INADDRSZ;
+			seen_xdigits = 0;
+			break;	/*%< '\\0' was seen by inet_pton4(). */
+		}
+		return (0);
+	}
+	if (seen_xdigits) {
+		if (tp + NS_INT16SZ > endp)
+			return (0);
+		*tp++ = (u_char) (val >> 8) & 0xff;
+		*tp++ = (u_char) val & 0xff;
+	}
+	if (colonp != NULL) {
+		/*
+		 * Since some memmove()'s erroneously fail to handle
+		 * overlapping regions, we'll do the shift by hand.
+		 */
+		const int n = tp - colonp;
+		int i;
+
+		if (tp == endp)
+			return (0);
+		for (i = 1; i <= n; i++) {
+			endp[- i] = colonp[n - i];
+			colonp[n - i] = 0;
+		}
+		tp = endp;
+	}
+	if (tp != endp)
+		return (0);
+	memcpy(dst, tmp, NS_IN6ADDRSZ);
+	return (1);
+}
+
+/*
+ * Weak aliases for applications that use certain private entry points,
+ * and fail to include <arpa/inet.h>.
+ */
+#undef inet_pton
+__weak_reference(__inet_pton, inet_pton);
+
+/*! \file */
diff --git a/freebsd-userspace/lib/libc/inet/nsap_addr.c b/freebsd-userspace/lib/libc/inet/nsap_addr.c
new file mode 100644
index 0000000..2947472
--- /dev/null
+++ b/freebsd-userspace/lib/libc/inet/nsap_addr.c
@@ -0,0 +1,122 @@
+/*
+ * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
+ * Copyright (c) 1996-1999 by Internet Software Consortium.
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
+ * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#if defined(LIBC_SCCS) && !defined(lint)
+static const char rcsid[] = "$Id: nsap_addr.c,v 1.3.18.2 2005/07/28 07:38:08 marka Exp $";
+#endif /* LIBC_SCCS and not lint */
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include "port_before.h"
+
+#include <sys/types.h>
+#include <sys/param.h>
+#include <sys/socket.h>
+
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <arpa/nameser.h>
+
+#include <ctype.h>
+#include <resolv.h>
+#include <resolv_mt.h>
+
+#include "port_after.h"
+
+static char
+xtob(int c) {
+	return (c - (((c >= '0') && (c <= '9')) ? '0' : '7'));
+}
+
+u_int
+inet_nsap_addr(const char *ascii, u_char *binary, int maxlen) {
+	u_char c, nib;
+	u_int len = 0;
+
+	if (ascii[0] != '0' || (ascii[1] != 'x' && ascii[1] != 'X'))
+		return (0);
+	ascii += 2;
+
+	while ((c = *ascii++) != '\0' && len < (u_int)maxlen) {
+		if (c == '.' || c == '+' || c == '/')
+			continue;
+		if (!isascii(c))
+			return (0);
+		if (islower(c))
+			c = toupper(c);
+		if (isxdigit(c)) {
+			nib = xtob(c);
+			c = *ascii++;
+			if (c != '\0') {
+				c = toupper(c);
+				if (isxdigit(c)) {
+					*binary++ = (nib << 4) | xtob(c);
+					len++;
+				} else
+					return (0);
+			}
+			else
+				return (0);
+		}
+		else
+			return (0);
+	}
+	return (len);
+}
+
+char *
+inet_nsap_ntoa(int binlen, const u_char *binary, char *ascii) {
+	int nib;
+	int i;
+	char *tmpbuf = inet_nsap_ntoa_tmpbuf;
+	char *start;
+
+	if (ascii)
+		start = ascii;
+	else {
+		ascii = tmpbuf;
+		start = tmpbuf;
+	}
+
+	*ascii++ = '0';
+	*ascii++ = 'x';
+
+	if (binlen > 255)
+		binlen = 255;
+
+	for (i = 0; i < binlen; i++) {
+		nib = *binary >> 4;
+		*ascii++ = nib + (nib < 10 ? '0' : '7');
+		nib = *binary++ & 0x0f;
+		*ascii++ = nib + (nib < 10 ? '0' : '7');
+		if (((i % 2) == 0 && (i + 1) < binlen))
+			*ascii++ = '.';
+	}
+	*ascii = '\0';
+	return (start);
+}
+
+/*
+ * Weak aliases for applications that use certain private entry points,
+ * and fail to include <arpa/inet.h>.
+ */
+#undef inet_nsap_addr
+__weak_reference(__inet_nsap_addr, inet_nsap_addr);
+#undef inet_nsap_ntoa
+__weak_reference(__inet_nsap_ntoa, inet_nsap_ntoa);
+
+/*! \file */
diff --git a/freebsd-userspace/lib/libc/net/base64.c b/freebsd-userspace/lib/libc/net/base64.c
new file mode 100644
index 0000000..6d3a79d
--- /dev/null
+++ b/freebsd-userspace/lib/libc/net/base64.c
@@ -0,0 +1,317 @@
+#include "port_before.h"
+
+/*
+ * Copyright (c) 1996, 1998 by Internet Software Consortium.
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
+ * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
+ * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
+ * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
+ * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
+ * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
+ * SOFTWARE.
+ */
+
+/*
+ * Portions Copyright (c) 1995 by International Business Machines, Inc.
+ *
+ * International Business Machines, Inc. (hereinafter called IBM) grants
+ * permission under its copyrights to use, copy, modify, and distribute this
+ * Software with or without fee, provided that the above copyright notice and
+ * all paragraphs of this notice appear in all copies, and that the name of IBM
+ * not be used in connection with the marketing of any product incorporating
+ * the Software or modifications thereof, without specific, written prior
+ * permission.
+ *
+ * To the extent it has a right to do so, IBM grants an immunity from suit
+ * under its patents, if any, for the use, sale or manufacture of products to
+ * the extent that such products are used for performing Domain Name System
+ * dynamic updates in TCP/IP networks by means of the Software.  No immunity is
+ * granted for any product per se or for any other function of any product.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", AND IBM DISCLAIMS ALL WARRANTIES,
+ * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE.  IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL,
+ * DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER ARISING
+ * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE, EVEN
+ * IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES.
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/types.h>
+#include <sys/param.h>
+#include <sys/socket.h>
+
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <arpa/nameser.h>
+
+#include <ctype.h>
+#include <resolv.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#define Assert(Cond) if (!(Cond)) abort()
+
+static const char Base64[] =
+	"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
+static const char Pad64 = '=';
+
+/* (From RFC1521 and draft-ietf-dnssec-secext-03.txt)
+   The following encoding technique is taken from RFC 1521 by Borenstein
+   and Freed.  It is reproduced here in a slightly edited form for
+   convenience.
+
+   A 65-character subset of US-ASCII is used, enabling 6 bits to be
+   represented per printable character. (The extra 65th character, "=",
+   is used to signify a special processing function.)
+
+   The encoding process represents 24-bit groups of input bits as output
+   strings of 4 encoded characters. Proceeding from left to right, a
+   24-bit input group is formed by concatenating 3 8-bit input groups.
+   These 24 bits are then treated as 4 concatenated 6-bit groups, each
+   of which is translated into a single digit in the base64 alphabet.
+
+   Each 6-bit group is used as an index into an array of 64 printable
+   characters. The character referenced by the index is placed in the
+   output string.
+
+                         Table 1: The Base64 Alphabet
+
+      Value Encoding  Value Encoding  Value Encoding  Value Encoding
+          0 A            17 R            34 i            51 z
+          1 B            18 S            35 j            52 0
+          2 C            19 T            36 k            53 1
+          3 D            20 U            37 l            54 2
+          4 E            21 V            38 m            55 3
+          5 F            22 W            39 n            56 4
+          6 G            23 X            40 o            57 5
+          7 H            24 Y            41 p            58 6
+          8 I            25 Z            42 q            59 7
+          9 J            26 a            43 r            60 8
+         10 K            27 b            44 s            61 9
+         11 L            28 c            45 t            62 +
+         12 M            29 d            46 u            63 /
+         13 N            30 e            47 v
+         14 O            31 f            48 w         (pad) =
+         15 P            32 g            49 x
+         16 Q            33 h            50 y
+
+   Special processing is performed if fewer than 24 bits are available
+   at the end of the data being encoded.  A full encoding quantum is
+   always completed at the end of a quantity.  When fewer than 24 input
+   bits are available in an input group, zero bits are added (on the
+   right) to form an integral number of 6-bit groups.  Padding at the
+   end of the data is performed using the '=' character.
+
+   Since all base64 input is an integral number of octets, only the
+         -------------------------------------------------                       
+   following cases can arise:
+   
+       (1) the final quantum of encoding input is an integral
+           multiple of 24 bits; here, the final unit of encoded
+	   output will be an integral multiple of 4 characters
+	   with no "=" padding,
+       (2) the final quantum of encoding input is exactly 8 bits;
+           here, the final unit of encoded output will be two
+	   characters followed by two "=" padding characters, or
+       (3) the final quantum of encoding input is exactly 16 bits;
+           here, the final unit of encoded output will be three
+	   characters followed by one "=" padding character.
+   */
+
+int
+b64_ntop(u_char const *src, size_t srclength, char *target, size_t targsize) {
+	size_t datalength = 0;
+	u_char input[3];
+	u_char output[4];
+	size_t i;
+
+	while (2 < srclength) {
+		input[0] = *src++;
+		input[1] = *src++;
+		input[2] = *src++;
+		srclength -= 3;
+
+		output[0] = input[0] >> 2;
+		output[1] = ((input[0] & 0x03) << 4) + (input[1] >> 4);
+		output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6);
+		output[3] = input[2] & 0x3f;
+		Assert(output[0] < 64);
+		Assert(output[1] < 64);
+		Assert(output[2] < 64);
+		Assert(output[3] < 64);
+
+		if (datalength + 4 > targsize)
+			return (-1);
+		target[datalength++] = Base64[output[0]];
+		target[datalength++] = Base64[output[1]];
+		target[datalength++] = Base64[output[2]];
+		target[datalength++] = Base64[output[3]];
+	}
+    
+	/* Now we worry about padding. */
+	if (0 != srclength) {
+		/* Get what's left. */
+		input[0] = input[1] = input[2] = '\0';
+		for (i = 0; i < srclength; i++)
+			input[i] = *src++;
+	
+		output[0] = input[0] >> 2;
+		output[1] = ((input[0] & 0x03) << 4) + (input[1] >> 4);
+		output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6);
+		Assert(output[0] < 64);
+		Assert(output[1] < 64);
+		Assert(output[2] < 64);
+
+		if (datalength + 4 > targsize)
+			return (-1);
+		target[datalength++] = Base64[output[0]];
+		target[datalength++] = Base64[output[1]];
+		if (srclength == 1)
+			target[datalength++] = Pad64;
+		else
+			target[datalength++] = Base64[output[2]];
+		target[datalength++] = Pad64;
+	}
+	if (datalength >= targsize)
+		return (-1);
+	target[datalength] = '\0';	/* Returned value doesn't count \0. */
+	return (datalength);
+}
+
+/* skips all whitespace anywhere.
+   converts characters, four at a time, starting at (or after)
+   src from base - 64 numbers into three 8 bit bytes in the target area.
+   it returns the number of data bytes stored at the target, or -1 on error.
+ */
+
+int
+b64_pton(src, target, targsize)
+	char const *src;
+	u_char *target;
+	size_t targsize;
+{
+	int tarindex, state, ch;
+	char *pos;
+
+	state = 0;
+	tarindex = 0;
+
+	while ((ch = *src++) != '\0') {
+		if (isspace((unsigned char)ch))        /* Skip whitespace anywhere. */
+			continue;
+
+		if (ch == Pad64)
+			break;
+
+		pos = strchr(Base64, ch);
+		if (pos == 0) 		/* A non-base64 character. */
+			return (-1);
+
+		switch (state) {
+		case 0:
+			if (target) {
+				if ((size_t)tarindex >= targsize)
+					return (-1);
+				target[tarindex] = (pos - Base64) << 2;
+			}
+			state = 1;
+			break;
+		case 1:
+			if (target) {
+				if ((size_t)tarindex + 1 >= targsize)
+					return (-1);
+				target[tarindex]   |=  (pos - Base64) >> 4;
+				target[tarindex+1]  = ((pos - Base64) & 0x0f)
+							<< 4 ;
+			}
+			tarindex++;
+			state = 2;
+			break;
+		case 2:
+			if (target) {
+				if ((size_t)tarindex + 1 >= targsize)
+					return (-1);
+				target[tarindex]   |=  (pos - Base64) >> 2;
+				target[tarindex+1]  = ((pos - Base64) & 0x03)
+							<< 6;
+			}
+			tarindex++;
+			state = 3;
+			break;
+		case 3:
+			if (target) {
+				if ((size_t)tarindex >= targsize)
+					return (-1);
+				target[tarindex] |= (pos - Base64);
+			}
+			tarindex++;
+			state = 0;
+			break;
+		default:
+			abort();
+		}
+	}
+
+	/*
+	 * We are done decoding Base-64 chars.  Let's see if we ended
+	 * on a byte boundary, and/or with erroneous trailing characters.
+	 */
+
+	if (ch == Pad64) {		/* We got a pad char. */
+		ch = *src++;		/* Skip it, get next. */
+		switch (state) {
+		case 0:		/* Invalid = in first position */
+		case 1:		/* Invalid = in second position */
+			return (-1);
+
+		case 2:		/* Valid, means one byte of info */
+			/* Skip any number of spaces. */
+			for ((void)NULL; ch != '\0'; ch = *src++)
+				if (!isspace((unsigned char)ch))
+					break;
+			/* Make sure there is another trailing = sign. */
+			if (ch != Pad64)
+				return (-1);
+			ch = *src++;		/* Skip the = */
+			/* Fall through to "single trailing =" case. */
+			/* FALLTHROUGH */
+
+		case 3:		/* Valid, means two bytes of info */
+			/*
+			 * We know this char is an =.  Is there anything but
+			 * whitespace after it?
+			 */
+			for ((void)NULL; ch != '\0'; ch = *src++)
+				if (!isspace((unsigned char)ch))
+					return (-1);
+
+			/*
+			 * Now make sure for cases 2 and 3 that the "extra"
+			 * bits that slopped past the last full byte were
+			 * zeros.  If we don't check them, they become a
+			 * subliminal channel.
+			 */
+			if (target && target[tarindex] != 0)
+				return (-1);
+		}
+	} else {
+		/*
+		 * We ended by seeing the end of the string.  Make sure we
+		 * have no partial bytes lying around.
+		 */
+		if (state != 0)
+			return (-1);
+	}
+
+	return (tarindex);
+}
diff --git a/freebsd-userspace/lib/libc/net/ether_addr.c b/freebsd-userspace/lib/libc/net/ether_addr.c
new file mode 100644
index 0000000..a16b97d
--- /dev/null
+++ b/freebsd-userspace/lib/libc/net/ether_addr.c
@@ -0,0 +1,232 @@
+#include "port_before.h"
+
+/*
+ * Copyright (c) 1995 Bill Paul <wpaul at ctr.columbia.edu>.
+ * Copyright (c) 2007 Robert N. M. Watson
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *	This product includes software developed by Bill Paul.
+ * 4. Neither the name of the author nor the names of any co-contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON 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 ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * ethernet address conversion and lookup routines
+ *
+ * Written by Bill Paul <wpaul at ctr.columbia.edu>
+ * Center for Telecommunications Research
+ * Columbia University, New York City
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/types.h>
+#include <sys/param.h>
+#include <sys/socket.h>
+
+#include <net/ethernet.h>
+
+#ifdef YP
+#include <rpc/rpc.h>
+#include <rpcsvc/yp_prot.h>
+#include <rpcsvc/ypclnt.h>
+#endif
+
+#include <paths.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#ifndef _PATH_ETHERS
+#define	_PATH_ETHERS	"/etc/ethers"
+#endif
+
+/*
+ * Parse a string of text containing an ethernet address and hostname and
+ * separate it into its component parts.
+ */
+int
+ether_line(const char *l, struct ether_addr *e, char *hostname)
+{
+	int i, o[6];
+
+	i = sscanf(l, "%x:%x:%x:%x:%x:%x %s", &o[0], &o[1], &o[2], &o[3],
+	    &o[4], &o[5], hostname);
+	if (i != 7)
+		return (i);
+	for (i=0; i<6; i++)
+		e->octet[i] = o[i];
+	return (0);
+}
+
+/*
+ * Convert an ASCII representation of an ethernet address to binary form.
+ */
+struct ether_addr *
+ether_aton_r(const char *a, struct ether_addr *e)
+{
+	int i;
+	unsigned int o0, o1, o2, o3, o4, o5;
+
+	i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o0, &o1, &o2, &o3, &o4, &o5);
+	if (i != 6)
+		return (NULL);
+	e->octet[0]=o0;
+	e->octet[1]=o1;
+	e->octet[2]=o2;
+	e->octet[3]=o3;
+	e->octet[4]=o4;
+	e->octet[5]=o5;
+	return (e);
+}
+
+struct ether_addr *
+ether_aton(const char *a)
+{
+	static struct ether_addr e;
+
+	return (ether_aton_r(a, &e));
+}
+
+/*
+ * Convert a binary representation of an ethernet address to an ASCII string.
+ */
+char *
+ether_ntoa_r(const struct ether_addr *n, char *a)
+{
+	int i;
+
+	i = sprintf(a, "%02x:%02x:%02x:%02x:%02x:%02x", n->octet[0],
+	    n->octet[1], n->octet[2], n->octet[3], n->octet[4], n->octet[5]);
+	if (i < 17)
+		return (NULL);
+	return (a);
+}
+
+char *
+ether_ntoa(const struct ether_addr *n)
+{
+	static char a[18];
+
+	return (ether_ntoa_r(n, a));
+}
+
+/*
+ * Map an ethernet address to a hostname. Use either /etc/ethers or NIS/YP.
+ */
+int
+ether_ntohost(char *hostname, const struct ether_addr *e)
+{
+	FILE *fp;
+	char buf[BUFSIZ + 2];
+	struct ether_addr local_ether;
+	char local_host[MAXHOSTNAMELEN];
+#ifdef YP
+	char *result;
+	int resultlen;
+	char *ether_a;
+	char *yp_domain;
+#endif
+
+	if ((fp = fopen(_PATH_ETHERS, "r")) == NULL)
+		return (1);
+	while (fgets(buf,BUFSIZ,fp)) {
+		if (buf[0] == '#')
+			continue;
+#ifdef YP
+		if (buf[0] == '+') {
+			if (yp_get_default_domain(&yp_domain))
+				continue;
+			ether_a = ether_ntoa(e);
+			if (yp_match(yp_domain, "ethers.byaddr", ether_a,
+			    strlen(ether_a), &result, &resultlen)) {
+				continue;
+			}
+			strncpy(buf, result, resultlen);
+			buf[resultlen] = '\0';
+			free(result);
+		}
+#endif
+		if (!ether_line(buf, &local_ether, local_host)) {
+			if (!bcmp((char *)&local_ether.octet[0],
+			    (char *)&e->octet[0], 6)) {
+				/* We have a match. */
+				strcpy(hostname, local_host);
+				fclose(fp);
+				return(0);
+			}
+		}
+	}
+	fclose(fp);
+	return (1);
+}
+
+/*
+ * Map a hostname to an ethernet address using /etc/ethers or NIS/YP.
+ */
+int
+ether_hostton(const char *hostname, struct ether_addr *e)
+{
+	FILE *fp;
+	char buf[BUFSIZ + 2];
+	struct ether_addr local_ether;
+	char local_host[MAXHOSTNAMELEN];
+#ifdef YP
+	char *result;
+	int resultlen;
+	char *yp_domain;
+#endif
+
+	if ((fp = fopen(_PATH_ETHERS, "r")) == NULL)
+		return (1);
+	while (fgets(buf,BUFSIZ,fp)) {
+		if (buf[0] == '#')
+			continue;
+#ifdef YP
+		if (buf[0] == '+') {
+			if (yp_get_default_domain(&yp_domain))
+				continue;
+			if (yp_match(yp_domain, "ethers.byname", hostname,
+			    strlen(hostname), &result, &resultlen)) {
+				continue;
+			}
+			strncpy(buf, result, resultlen);
+			buf[resultlen] = '\0';
+			free(result);
+		}
+#endif
+		if (!ether_line(buf, &local_ether, local_host)) {
+			if (!strcmp(hostname, local_host)) {
+				/* We have a match. */
+				bcopy((char *)&local_ether.octet[0],
+				    (char *)&e->octet[0], 6);
+				fclose(fp);
+				return(0);
+			}
+		}
+	}
+	fclose(fp);
+	return (1);
+}
diff --git a/freebsd-userspace/lib/libc/net/gethostbydns.c b/freebsd-userspace/lib/libc/net/gethostbydns.c
new file mode 100644
index 0000000..02853be
--- /dev/null
+++ b/freebsd-userspace/lib/libc/net/gethostbydns.c
@@ -0,0 +1,781 @@
+#include "port_before.h"
+
+/*
+ * ++Copyright++ 1985, 1988, 1993
+ * -
+ * Copyright (c) 1985, 1988, 1993
+ *	The Regents of the University of California.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON 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 ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ * -
+ * Portions Copyright (c) 1993 by Digital Equipment Corporation.
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies, and that
+ * the name of Digital Equipment Corporation not be used in advertising or
+ * publicity pertaining to distribution of the document or software without
+ * specific, written prior permission.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
+ * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
+ * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
+ * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
+ * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
+ * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
+ * SOFTWARE.
+ * -
+ * --Copyright--
+ */
+
+#if defined(LIBC_SCCS) && !defined(lint)
+static char sccsid[] = "@(#)gethostnamadr.c	8.1 (Berkeley) 6/4/93";
+static char fromrcsid[] = "From: Id: gethnamaddr.c,v 8.23 1998/04/07 04:59:46 vixie Exp $";
+#endif /* LIBC_SCCS and not lint */
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/types.h>
+#include <sys/param.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <arpa/nameser.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <netdb.h>
+#include <resolv.h>
+#include <ctype.h>
+#include <errno.h>
+#include <syslog.h>
+#include <stdarg.h>
+#include <nsswitch.h>
+
+#include "netdb_private.h"
+#include "res_config.h"
+
+#define SPRINTF(x) ((size_t)sprintf x)
+
+static const char AskedForGot[] =
+		"gethostby*.gethostanswer: asked for \"%s\", got \"%s\"";
+
+#ifdef RESOLVSORT
+static void addrsort(char **, int, res_state);
+#endif
+
+#ifdef DEBUG
+static void dprintf(char *, int, res_state) __printflike(1, 0);
+#endif
+
+#define MAXPACKET	(64*1024)
+
+typedef union {
+    HEADER hdr;
+    u_char buf[MAXPACKET];
+} querybuf;
+
+typedef union {
+    int32_t al;
+    char ac;
+} align;
+
+int _dns_ttl_;
+
+#ifdef DEBUG
+static void
+dprintf(msg, num, res)
+	char *msg;
+	int num;
+	res_state res;
+{
+	if (res->options & RES_DEBUG) {
+		int save = errno;
+
+		printf(msg, num);
+		errno = save;
+	}
+}
+#else
+# define dprintf(msg, num, res) /*nada*/
+#endif
+
+#define BOUNDED_INCR(x) \
+	do { \
+		cp += x; \
+		if (cp > eom) { \
+			RES_SET_H_ERRNO(statp, NO_RECOVERY); \
+			return (-1); \
+		} \
+	} while (0)
+
+#define BOUNDS_CHECK(ptr, count) \
+	do { \
+		if ((ptr) + (count) > eom) { \
+			RES_SET_H_ERRNO(statp, NO_RECOVERY); \
+			return (-1); \
+		} \
+	} while (0)
+
+static int
+gethostanswer(const querybuf *answer, int anslen, const char *qname, int qtype,
+    struct hostent *he, struct hostent_data *hed, res_state statp)
+{
+	const HEADER *hp;
+	const u_char *cp;
+	int n;
+	const u_char *eom, *erdata;
+	char *bp, *ep, **ap, **hap;
+	int type, class, ancount, qdcount;
+	int haveanswer, had_error;
+	int toobig = 0;
+	char tbuf[MAXDNAME];
+	const char *tname;
+	int (*name_ok)(const char *);
+
+	tname = qname;
+	he->h_name = NULL;
+	eom = answer->buf + anslen;
+	switch (qtype) {
+	case T_A:
+	case T_AAAA:
+		name_ok = res_hnok;
+		break;
+	case T_PTR:
+		name_ok = res_dnok;
+		break;
+	default:
+		RES_SET_H_ERRNO(statp, NO_RECOVERY);
+		return (-1);	/* XXX should be abort(); */
+	}
+	/*
+	 * find first satisfactory answer
+	 */
+	hp = &answer->hdr;
+	ancount = ntohs(hp->ancount);
+	qdcount = ntohs(hp->qdcount);
+	bp = hed->hostbuf;
+	ep = hed->hostbuf + sizeof hed->hostbuf;
+	cp = answer->buf;
+	BOUNDED_INCR(HFIXEDSZ);
+	if (qdcount != 1) {
+		RES_SET_H_ERRNO(statp, NO_RECOVERY);
+		return (-1);
+	}
+	n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
+	if ((n < 0) || !(*name_ok)(bp)) {
+		RES_SET_H_ERRNO(statp, NO_RECOVERY);
+		return (-1);
+	}
+	BOUNDED_INCR(n + QFIXEDSZ);
+	if (qtype == T_A || qtype == T_AAAA) {
+		/* res_send() has already verified that the query name is the
+		 * same as the one we sent; this just gets the expanded name
+		 * (i.e., with the succeeding search-domain tacked on).
+		 */
+		n = strlen(bp) + 1;		/* for the \0 */
+		if (n >= MAXHOSTNAMELEN) {
+			RES_SET_H_ERRNO(statp, NO_RECOVERY);
+			return (-1);
+		}
+		he->h_name = bp;
+		bp += n;
+		/* The qname can be abbreviated, but h_name is now absolute. */
+		qname = he->h_name;
+	}
+	ap = hed->host_aliases;
+	*ap = NULL;
+	he->h_aliases = hed->host_aliases;
+	hap = hed->h_addr_ptrs;
+	*hap = NULL;
+	he->h_addr_list = hed->h_addr_ptrs;
+	haveanswer = 0;
+	had_error = 0;
+	_dns_ttl_ = -1;
+	while (ancount-- > 0 && cp < eom && !had_error) {
+		n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
+		if ((n < 0) || !(*name_ok)(bp)) {
+			had_error++;
+			continue;
+		}
+		cp += n;			/* name */
+		BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
+		type = _getshort(cp);
+ 		cp += INT16SZ;			/* type */
+		class = _getshort(cp);
+ 		cp += INT16SZ;			/* class */
+		if (qtype == T_A  && type == T_A)
+			_dns_ttl_ = _getlong(cp);
+		cp += INT32SZ;			/* TTL */
+		n = _getshort(cp);
+		cp += INT16SZ;			/* len */
+		BOUNDS_CHECK(cp, n);
+		erdata = cp + n;
+		if (class != C_IN) {
+			/* XXX - debug? syslog? */
+			cp += n;
+			continue;		/* XXX - had_error++ ? */
+		}
+		if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) {
+			if (ap >= &hed->host_aliases[_MAXALIASES-1])
+				continue;
+			n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
+			if ((n < 0) || !(*name_ok)(tbuf)) {
+				had_error++;
+				continue;
+			}
+			cp += n;
+			if (cp != erdata) {
+				RES_SET_H_ERRNO(statp, NO_RECOVERY);
+				return (-1);
+			}
+			/* Store alias. */
+			*ap++ = bp;
+			n = strlen(bp) + 1;	/* for the \0 */
+			if (n >= MAXHOSTNAMELEN) {
+				had_error++;
+				continue;
+			}
+			bp += n;
+			/* Get canonical name. */
+			n = strlen(tbuf) + 1;	/* for the \0 */
+			if (n > ep - bp || n >= MAXHOSTNAMELEN) {
+				had_error++;
+				continue;
+			}
+			strcpy(bp, tbuf);
+			he->h_name = bp;
+			bp += n;
+			continue;
+		}
+		if (qtype == T_PTR && type == T_CNAME) {
+			n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
+			if (n < 0 || !res_dnok(tbuf)) {
+				had_error++;
+				continue;
+			}
+			cp += n;
+			if (cp != erdata) {
+				RES_SET_H_ERRNO(statp, NO_RECOVERY);
+				return (-1);
+			}
+			/* Get canonical name. */
+			n = strlen(tbuf) + 1;	/* for the \0 */
+			if (n > ep - bp || n >= MAXHOSTNAMELEN) {
+				had_error++;
+				continue;
+			}
+			strcpy(bp, tbuf);
+			tname = bp;
+			bp += n;
+			continue;
+		}
+		if (type != qtype) {
+			if (type != T_SIG && type != ns_t_dname)
+				syslog(LOG_NOTICE|LOG_AUTH,
+	"gethostby*.gethostanswer: asked for \"%s %s %s\", got type \"%s\"",
+				       qname, p_class(C_IN), p_type(qtype),
+				       p_type(type));
+			cp += n;
+			continue;		/* XXX - had_error++ ? */
+		}
+		switch (type) {
+		case T_PTR:
+			if (strcasecmp(tname, bp) != 0) {
+				syslog(LOG_NOTICE|LOG_AUTH,
+				       AskedForGot, qname, bp);
+				cp += n;
+				continue;	/* XXX - had_error++ ? */
+			}
+			n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
+			if ((n < 0) || !res_hnok(bp)) {
+				had_error++;
+				break;
+			}
+#if MULTI_PTRS_ARE_ALIASES
+			cp += n;
+			if (cp != erdata) {
+				RES_SET_H_ERRNO(statp, NO_RECOVERY);
+				return (-1);
+			}
+			if (!haveanswer)
+				he->h_name = bp;
+			else if (ap < &hed->host_aliases[_MAXALIASES-1])
+				*ap++ = bp;
+			else
+				n = -1;
+			if (n != -1) {
+				n = strlen(bp) + 1;	/* for the \0 */
+				if (n >= MAXHOSTNAMELEN) {
+					had_error++;
+					break;
+				}
+				bp += n;
+			}
+			break;
+#else
+			he->h_name = bp;
+			if (statp->options & RES_USE_INET6) {
+				n = strlen(bp) + 1;	/* for the \0 */
+				if (n >= MAXHOSTNAMELEN) {
+					had_error++;
+					break;
+				}
+				bp += n;
+				_map_v4v6_hostent(he, &bp, ep);
+			}
+			RES_SET_H_ERRNO(statp, NETDB_SUCCESS);
+			return (0);
+#endif
+		case T_A:
+		case T_AAAA:
+			if (strcasecmp(he->h_name, bp) != 0) {
+				syslog(LOG_NOTICE|LOG_AUTH,
+				       AskedForGot, he->h_name, bp);
+				cp += n;
+				continue;	/* XXX - had_error++ ? */
+			}
+			if (n != he->h_length) {
+				cp += n;
+				continue;
+			}
+			if (!haveanswer) {
+				int nn;
+
+				he->h_name = bp;
+				nn = strlen(bp) + 1;	/* for the \0 */
+				bp += nn;
+			}
+
+			bp += sizeof(align) - ((u_long)bp % sizeof(align));
+
+			if (bp + n >= ep) {
+				dprintf("size (%d) too big\n", n, statp);
+				had_error++;
+				continue;
+			}
+			if (hap >= &hed->h_addr_ptrs[_MAXADDRS-1]) {
+				if (!toobig++)
+					dprintf("Too many addresses (%d)\n",
+						_MAXADDRS, statp);
+				cp += n;
+				continue;
+			}
+			memcpy(*hap++ = bp, cp, n);
+			bp += n;
+			cp += n;
+			if (cp != erdata) {
+				RES_SET_H_ERRNO(statp, NO_RECOVERY);
+				return (-1);
+			}
+			break;
+		default:
+			dprintf("Impossible condition (type=%d)\n", type,
+			    statp);
+			RES_SET_H_ERRNO(statp, NO_RECOVERY);
+			return (-1);
+			/* BIND has abort() here, too risky on bad data */
+		}
+		if (!had_error)
+			haveanswer++;
+	}
+	if (haveanswer) {
+		*ap = NULL;
+		*hap = NULL;
+# if defined(RESOLVSORT)
+		/*
+		 * Note: we sort even if host can take only one address
+		 * in its return structures - should give it the "best"
+		 * address in that case, not some random one
+		 */
+		if (statp->nsort && haveanswer > 1 && qtype == T_A)
+			addrsort(hed->h_addr_ptrs, haveanswer, statp);
+# endif /*RESOLVSORT*/
+		if (!he->h_name) {
+			n = strlen(qname) + 1;	/* for the \0 */
+			if (n > ep - bp || n >= MAXHOSTNAMELEN)
+				goto no_recovery;
+			strcpy(bp, qname);
+			he->h_name = bp;
+			bp += n;
+		}
+		if (statp->options & RES_USE_INET6)
+			_map_v4v6_hostent(he, &bp, ep);
+		RES_SET_H_ERRNO(statp, NETDB_SUCCESS);
+		return (0);
+	}
+ no_recovery:
+	RES_SET_H_ERRNO(statp, NO_RECOVERY);
+	return (-1);
+}
+
+/* XXX: for async DNS resolver in ypserv */
+struct hostent *
+__dns_getanswer(const char *answer, int anslen, const char *qname, int qtype)
+{
+	struct hostent *he;
+	struct hostent_data *hed;
+	int error;
+	res_state statp;
+
+	statp = __res_state();
+	if ((he = __hostent_init()) == NULL ||
+	    (hed = __hostent_data_init()) == NULL) {
+		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
+		return (NULL);
+	}
+	switch (qtype) {
+	case T_AAAA:
+		he->h_addrtype = AF_INET6;
+		he->h_length = NS_IN6ADDRSZ;
+		break;
+	case T_A:
+	default:
+		he->h_addrtype = AF_INET;
+		he->h_length = NS_INADDRSZ;
+		break;
+	}
+
+	error = gethostanswer((const querybuf *)answer, anslen, qname, qtype,
+	    he, hed, statp);
+	return (error == 0) ? he : NULL;
+}
+
+int
+_dns_gethostbyname(void *rval, void *cb_data, va_list ap)
+{
+	const char *name;
+	int af;
+	char *buffer;
+	size_t buflen;
+	int *errnop, *h_errnop;
+	struct hostent *hptr, he;
+	struct hostent_data *hed;
+	querybuf *buf;
+	int n, type, error;
+	res_state statp;
+
+	name = va_arg(ap, const char *);
+	af = va_arg(ap, int);
+	hptr = va_arg(ap, struct hostent *);
+	buffer = va_arg(ap, char *);
+	buflen = va_arg(ap, size_t);
+	errnop = va_arg(ap, int *);
+	h_errnop = va_arg(ap, int *);
+
+	*((struct hostent **)rval) = NULL;
+
+	statp = __res_state();
+	if ((hed = __hostent_data_init()) == NULL) {
+		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
+		*h_errnop = statp->res_h_errno;
+		return (NS_NOTFOUND);
+	}
+
+	he.h_addrtype = af;
+	switch (af) {
+	case AF_INET:
+		he.h_length = NS_INADDRSZ;
+		type = T_A;
+		break;
+	case AF_INET6:
+		he.h_length = NS_IN6ADDRSZ;
+		type = T_AAAA;
+		break;
+	default:
+		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
+		*h_errnop = statp->res_h_errno;
+		errno = EAFNOSUPPORT;
+		return (NS_UNAVAIL);
+	}
+
+	if ((buf = malloc(sizeof(*buf))) == NULL) {
+		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
+		*h_errnop = statp->res_h_errno;
+		return (NS_NOTFOUND);
+	}
+	n = res_nsearch(statp, name, C_IN, type, buf->buf, sizeof(buf->buf));
+	if (n < 0) {
+		free(buf);
+		dprintf("res_nsearch failed (%d)\n", n, statp);
+		*h_errnop = statp->res_h_errno;
+		return (NS_NOTFOUND);
+	} else if (n > sizeof(buf->buf)) {
+		free(buf);
+		dprintf("static buffer is too small (%d)\n", n, statp);
+		*h_errnop = statp->res_h_errno;
+		return (NS_UNAVAIL);
+	}
+	error = gethostanswer(buf, n, name, type, &he, hed, statp);
+	free(buf);
+	if (error != 0) {
+		*h_errnop = statp->res_h_errno;
+		switch (statp->res_h_errno) {
+		case HOST_NOT_FOUND:
+			return (NS_NOTFOUND);
+		case TRY_AGAIN:
+			return (NS_TRYAGAIN);
+		default:
+			return (NS_UNAVAIL);
+		}
+		/*NOTREACHED*/
+	}
+	if (__copy_hostent(&he, hptr, buffer, buflen) != 0) {
+		*errnop = errno;
+		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
+		*h_errnop = statp->res_h_errno;
+		return (NS_RETURN);
+	}
+	RES_SET_H_ERRNO(statp, NETDB_SUCCESS);
+	*((struct hostent **)rval) = hptr;
+	return (NS_SUCCESS);
+}
+
+int
+_dns_gethostbyaddr(void *rval, void *cb_data, va_list ap)
+{
+	const void *addr;
+	socklen_t len;
+	int af;
+	char *buffer;
+	size_t buflen;
+	int *errnop, *h_errnop;
+	const u_char *uaddr;
+	struct hostent *hptr, he;
+	struct hostent_data *hed;
+	int n;
+	querybuf *buf;
+	char qbuf[MAXDNAME+1], *qp;
+	res_state statp;
+#ifdef SUNSECURITY
+	struct hostdata rhd;
+	struct hostent *rhe;
+	char **haddr;
+	u_long old_options;
+	char hname2[MAXDNAME+1], numaddr[46];
+	int ret_h_error;
+#endif /*SUNSECURITY*/
+
+	addr = va_arg(ap, const void *);
+	len = va_arg(ap, socklen_t);
+	af = va_arg(ap, int);
+	hptr = va_arg(ap, struct hostent *);
+	buffer = va_arg(ap, char *);
+	buflen = va_arg(ap, size_t);
+	errnop = va_arg(ap, int *);
+	h_errnop = va_arg(ap, int *);
+	uaddr = (const u_char *)addr;
+
+	*((struct hostent **)rval) = NULL;
+
+	statp = __res_state();
+	if ((hed = __hostent_data_init()) == NULL) {
+		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
+		*h_errnop = statp->res_h_errno;
+		return (NS_NOTFOUND);
+	}
+
+	switch (af) {
+	case AF_INET:
+		(void) sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa",
+			       (uaddr[3] & 0xff),
+			       (uaddr[2] & 0xff),
+			       (uaddr[1] & 0xff),
+			       (uaddr[0] & 0xff));
+		break;
+	case AF_INET6:
+		qp = qbuf;
+		for (n = NS_IN6ADDRSZ - 1; n >= 0; n--) {
+			qp += SPRINTF((qp, "%x.%x.",
+				       uaddr[n] & 0xf,
+				       (uaddr[n] >> 4) & 0xf));
+		}
+		strlcat(qbuf, "ip6.arpa", sizeof(qbuf));
+		break;
+	default:
+		abort();
+	}
+	if ((buf = malloc(sizeof(*buf))) == NULL) {
+		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
+		*h_errnop = statp->res_h_errno;
+		return NS_NOTFOUND;
+	}
+	n = res_nquery(statp, qbuf, C_IN, T_PTR, (u_char *)buf->buf,
+	    sizeof buf->buf);
+	if (n < 0) {
+		free(buf);
+		dprintf("res_nquery failed (%d)\n", n, statp);
+		*h_errnop = statp->res_h_errno;
+		return (NS_UNAVAIL);
+	}
+	if (n > sizeof buf->buf) {
+		free(buf);
+		dprintf("static buffer is too small (%d)\n", n, statp);
+		*h_errnop = statp->res_h_errno;
+		return (NS_UNAVAIL);
+	}
+	if (gethostanswer(buf, n, qbuf, T_PTR, &he, hed, statp) != 0) {
+		free(buf);
+		*h_errnop = statp->res_h_errno;
+		switch (statp->res_h_errno) {
+		case HOST_NOT_FOUND:
+			return (NS_NOTFOUND);
+		case TRY_AGAIN:
+			return (NS_TRYAGAIN);
+		default:
+			return (NS_UNAVAIL);
+		}
+		/*NOTREACHED*/
+	}
+	free(buf);
+#ifdef SUNSECURITY
+	if (af == AF_INET) {
+	    /*
+	     * turn off search as the name should be absolute,
+	     * 'localhost' should be matched by defnames
+	     */
+	    strncpy(hname2, he.h_name, MAXDNAME);
+	    hname2[MAXDNAME] = '\0';
+	    old_options = statp->options;
+	    statp->options &= ~RES_DNSRCH;
+	    statp->options |= RES_DEFNAMES;
+	    memset(&rhd, 0, sizeof rhd);
+	    rhe = gethostbyname_r(hname2, &rhd.host, &rhd.data,
+	        sizeof(rhd.data), &ret_h_error);
+	    if (rhe == NULL) {
+		if (inet_ntop(af, addr, numaddr, sizeof(numaddr)) == NULL)
+		    strlcpy(numaddr, "UNKNOWN", sizeof(numaddr));
+		syslog(LOG_NOTICE|LOG_AUTH,
+		       "gethostbyaddr: No A record for %s (verifying [%s])",
+		       hname2, numaddr);
+		statp->options = old_options;
+		RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
+		*h_errnop = statp->res_h_errno;
+		return (NS_NOTFOUND);
+	    }
+	    statp->options = old_options;
+	    for (haddr = rhe->h_addr_list; *haddr; haddr++)
+		if (!memcmp(*haddr, addr, NS_INADDRSZ))
+			break;
+	    if (!*haddr) {
+		if (inet_ntop(af, addr, numaddr, sizeof(numaddr)) == NULL)
+		    strlcpy(numaddr, "UNKNOWN", sizeof(numaddr));
+		syslog(LOG_NOTICE|LOG_AUTH,
+		       "gethostbyaddr: A record of %s != PTR record [%s]",
+		       hname2, numaddr);
+		RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
+		*h_errnop = statp->res_h_errno;
+		return (NS_NOTFOUND);
+	    }
+	}
+#endif /*SUNSECURITY*/
+	he.h_addrtype = af;
+	he.h_length = len;
+	memcpy(hed->host_addr, uaddr, len);
+	hed->h_addr_ptrs[0] = (char *)hed->host_addr;
+	hed->h_addr_ptrs[1] = NULL;
+	if (af == AF_INET && (statp->options & RES_USE_INET6)) {
+		_map_v4v6_address((char*)hed->host_addr, (char*)hed->host_addr);
+		he.h_addrtype = AF_INET6;
+		he.h_length = NS_IN6ADDRSZ;
+	}
+	if (__copy_hostent(&he, hptr, buffer, buflen) != 0) {
+		*errnop = errno;
+		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
+		*h_errnop = statp->res_h_errno;
+		return (NS_RETURN);
+	}
+	RES_SET_H_ERRNO(statp, NETDB_SUCCESS);
+	*((struct hostent **)rval) = hptr;
+	return (NS_SUCCESS);
+}
+
+#ifdef RESOLVSORT
+static void
+addrsort(char **ap, int num, res_state res)
+{
+	int i, j;
+	char **p;
+	short aval[_MAXADDRS];
+	int needsort = 0;
+
+	p = ap;
+	for (i = 0; i < num; i++, p++) {
+	    for (j = 0 ; (unsigned)j < res->nsort; j++)
+		if (res->sort_list[j].addr.s_addr == 
+		    (((struct in_addr *)(*p))->s_addr & res->sort_list[j].mask))
+			break;
+	    aval[i] = j;
+	    if (needsort == 0 && i > 0 && j < aval[i-1])
+		needsort = i;
+	}
+	if (!needsort)
+	    return;
+
+	while (needsort < num) {
+	    for (j = needsort - 1; j >= 0; j--) {
+		if (aval[j] > aval[j+1]) {
+		    char *hp;
+
+		    i = aval[j];
+		    aval[j] = aval[j+1];
+		    aval[j+1] = i;
+
+		    hp = ap[j];
+		    ap[j] = ap[j+1];
+		    ap[j+1] = hp;
+
+		} else
+		    break;
+	    }
+	    needsort++;
+	}
+}
+#endif
+
+void
+_sethostdnsent(int stayopen)
+{
+	res_state statp;
+
+	statp = __res_state();
+	if ((statp->options & RES_INIT) == 0 && res_ninit(statp) == -1)
+		return;
+	if (stayopen)
+		statp->options |= RES_STAYOPEN | RES_USEVC;
+}
+
+void
+_endhostdnsent()
+{
+	res_state statp;
+
+	statp = __res_state();
+	statp->options &= ~(RES_STAYOPEN | RES_USEVC);
+	res_nclose(statp);
+}
diff --git a/freebsd-userspace/lib/libc/net/gethostbyht.c b/freebsd-userspace/lib/libc/net/gethostbyht.c
new file mode 100644
index 0000000..cb88cf9
--- /dev/null
+++ b/freebsd-userspace/lib/libc/net/gethostbyht.c
@@ -0,0 +1,341 @@
+#include "port_before.h"
+
+/*-
+ * Copyright (c) 1985, 1988, 1993
+ *	The Regents of the University of California.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON 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 ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ * -
+ * Portions Copyright (c) 1993 by Digital Equipment Corporation.
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies, and that
+ * the name of Digital Equipment Corporation not be used in advertising or
+ * publicity pertaining to distribution of the document or software without
+ * specific, written prior permission.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
+ * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
+ * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
+ * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
+ * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
+ * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
+ * SOFTWARE.
+ * -
+ * --Copyright--
+ */
+
+#if defined(LIBC_SCCS) && !defined(lint)
+static char sccsid[] = "@(#)gethostnamadr.c	8.1 (Berkeley) 6/4/93";
+#endif /* LIBC_SCCS and not lint */
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/param.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <netdb.h>
+#include <stdio.h>
+#include <ctype.h>
+#include <errno.h>
+#include <string.h>
+#include <stdarg.h>
+#include <nsswitch.h>
+#include <arpa/nameser.h>	/* XXX */
+#include <resolv.h>		/* XXX */
+#include "netdb_private.h"
+
+void
+_sethosthtent(int f, struct hostent_data *hed)
+{
+	if (!hed->hostf)
+		hed->hostf = fopen(_PATH_HOSTS, "r");
+	else
+		rewind(hed->hostf);
+	hed->stayopen = f;
+}
+
+void
+_endhosthtent(struct hostent_data *hed)
+{
+	if (hed->hostf && !hed->stayopen) {
+		(void) fclose(hed->hostf);
+		hed->hostf = NULL;
+	}
+}
+
+static int
+gethostent_p(struct hostent *he, struct hostent_data *hed, int mapped,
+    res_state statp)
+{
+	char *p, *bp, *ep;
+	char *cp, **q;
+	int af, len;
+	char hostbuf[BUFSIZ + 1];
+
+	if (!hed->hostf && !(hed->hostf = fopen(_PATH_HOSTS, "r"))) {
+		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
+		return (-1);
+	}
+ again:
+	if (!(p = fgets(hostbuf, sizeof hostbuf, hed->hostf))) {
+		RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
+		return (-1);
+	}
+	if (*p == '#')
+		goto again;
+	cp = strpbrk(p, "#\n");
+	if (cp != NULL)
+		*cp = '\0';
+	if (!(cp = strpbrk(p, " \t")))
+		goto again;
+	*cp++ = '\0';
+	if (inet_pton(AF_INET6, p, hed->host_addr) > 0) {
+		af = AF_INET6;
+		len = IN6ADDRSZ;
+	} else if (inet_pton(AF_INET, p, hed->host_addr) > 0) {
+		if (mapped) {
+			_map_v4v6_address((char *)hed->host_addr,
+			    (char *)hed->host_addr);
+			af = AF_INET6;
+			len = IN6ADDRSZ;
+		} else {
+			af = AF_INET;
+			len = INADDRSZ;
+		}
+	} else {
+		goto again;
+	}
+	hed->h_addr_ptrs[0] = (char *)hed->host_addr;
+	hed->h_addr_ptrs[1] = NULL;
+	he->h_addr_list = hed->h_addr_ptrs;
+	he->h_length = len;
+	he->h_addrtype = af;
+	while (*cp == ' ' || *cp == '\t')
+		cp++;
+	bp = hed->hostbuf;
+	ep = hed->hostbuf + sizeof hed->hostbuf;
+	he->h_name = bp;
+	q = he->h_aliases = hed->host_aliases;
+	if ((p = strpbrk(cp, " \t")) != NULL)
+		*p++ = '\0';
+	len = strlen(cp) + 1;
+	if (ep - bp < len) {
+		RES_SET_H_ERRNO(statp, NO_RECOVERY);
+		return (-1);
+	}
+	strlcpy(bp, cp, ep - bp);
+	bp += len;
+	cp = p;
+	while (cp && *cp) {
+		if (*cp == ' ' || *cp == '\t') {
+			cp++;
+			continue;
+		}
+		if (q >= &hed->host_aliases[_MAXALIASES - 1])
+			break;
+		if ((p = strpbrk(cp, " \t")) != NULL)
+			*p++ = '\0';
+		len = strlen(cp) + 1;
+		if (ep - bp < len)
+			break;
+		strlcpy(bp, cp, ep - bp);
+		*q++ = bp;
+		bp += len;
+		cp = p;
+	}
+	*q = NULL;
+	RES_SET_H_ERRNO(statp, NETDB_SUCCESS);
+	return (0);
+}
+
+int
+gethostent_r(struct hostent *hptr, char *buffer, size_t buflen,
+    struct hostent **result, int *h_errnop)
+{
+	struct hostent_data *hed;
+	struct hostent he;
+	res_state statp;
+
+	statp = __res_state();
+	if ((statp->options & RES_INIT) == 0 && res_ninit(statp) == -1) {
+		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
+		*h_errnop = statp->res_h_errno;
+		return (-1);
+	}
+	if ((hed = __hostent_data_init()) == NULL) {
+		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
+		*h_errnop = statp->res_h_errno;
+		return (-1);
+	}
+	if (gethostent_p(&he, hed, statp->options & RES_USE_INET6, statp) != 0)
+		return (-1);
+	if (__copy_hostent(&he, hptr, buffer, buflen) != 0) {
+		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
+		*h_errnop = statp->res_h_errno;
+		return ((errno != 0) ? errno : -1);
+	}
+	*result = hptr;
+	return (0);
+}
+
+struct hostent *
+gethostent(void)
+{
+	struct hostdata *hd;
+	struct hostent *rval;
+	int ret_h_errno;
+
+	if ((hd = __hostdata_init()) == NULL)
+		return (NULL);
+	if (gethostent_r(&hd->host, hd->data, sizeof(hd->data), &rval,
+	    &ret_h_errno) != 0)
+		return (NULL);
+	return (rval);
+}
+
+int
+_ht_gethostbyname(void *rval, void *cb_data, va_list ap)
+{
+	const char *name;
+	int af;
+	char *buffer;
+	size_t buflen;
+	int *errnop, *h_errnop;
+	struct hostent *hptr, he;
+	struct hostent_data *hed;
+	char **cp;
+	res_state statp;
+	int error;
+
+	name = va_arg(ap, const char *);
+	af = va_arg(ap, int);
+	hptr = va_arg(ap, struct hostent *);
+	buffer = va_arg(ap, char *);
+	buflen = va_arg(ap, size_t);
+	errnop = va_arg(ap, int *);
+	h_errnop = va_arg(ap, int *);
+
+	*((struct hostent **)rval) = NULL;
+
+	statp = __res_state();
+	if ((hed = __hostent_data_init()) == NULL) {
+		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
+		*h_errnop = statp->res_h_errno;
+		return (NS_NOTFOUND);
+	}
+
+	_sethosthtent(0, hed);
+	while ((error = gethostent_p(&he, hed, 0, statp)) == 0) {
+		if (he.h_addrtype != af)
+			continue;
+		if (he.h_addrtype == AF_INET &&
+		    statp->options & RES_USE_INET6) {
+			_map_v4v6_address(he.h_addr, he.h_addr);
+			he.h_length = IN6ADDRSZ;
+			he.h_addrtype = AF_INET6;
+		}
+		if (strcasecmp(he.h_name, name) == 0)
+			break;
+		for (cp = he.h_aliases; *cp != 0; cp++)
+			if (strcasecmp(*cp, name) == 0)
+				goto found;
+	}
+found:
+	_endhosthtent(hed);
+
+	if (error != 0) {
+		*h_errnop = statp->res_h_errno;
+		return (NS_NOTFOUND);
+	}
+	if (__copy_hostent(&he, hptr, buffer, buflen) != 0) {
+		*errnop = errno;
+		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
+		*h_errnop = statp->res_h_errno;
+		return (NS_RETURN);
+	}
+	*((struct hostent **)rval) = hptr;
+	return (NS_SUCCESS);
+}
+
+int
+_ht_gethostbyaddr(void *rval, void *cb_data, va_list ap)
+{
+	const void *addr;
+	socklen_t len;
+	int af;
+	char *buffer;
+	size_t buflen;
+	int *errnop, *h_errnop;
+	struct hostent *hptr, he;
+	struct hostent_data *hed;
+	res_state statp;
+	int error;
+
+	addr = va_arg(ap, const void *);
+	len = va_arg(ap, socklen_t);
+	af = va_arg(ap, int);
+	hptr = va_arg(ap, struct hostent *);
+	buffer = va_arg(ap, char *);
+	buflen = va_arg(ap, size_t);
+	errnop = va_arg(ap, int *);
+	h_errnop = va_arg(ap, int *);
+
+	*((struct hostent **)rval) = NULL;
+
+	statp = __res_state();
+	if ((hed = __hostent_data_init()) == NULL) {
+		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
+		*h_errnop = statp->res_h_errno;
+		return (NS_NOTFOUND);
+	}
+
+	_sethosthtent(0, hed);
+	while ((error = gethostent_p(&he, hed, 0, statp)) == 0)
+		if (he.h_addrtype == af && !bcmp(he.h_addr, addr, len)) {
+			if (he.h_addrtype == AF_INET &&
+			    statp->options & RES_USE_INET6) {
+				_map_v4v6_address(he.h_addr, he.h_addr);
+				he.h_length = IN6ADDRSZ;
+				he.h_addrtype = AF_INET6;
+			}
+			break;
+		}
+	_endhosthtent(hed);
+
+	if (error != 0)
+		return (NS_NOTFOUND);
+	if (__copy_hostent(&he, hptr, buffer, buflen) != 0) {
+		*errnop = errno;
+		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
+		*h_errnop = statp->res_h_errno;
+		return (NS_RETURN);
+	}
+	*((struct hostent **)rval) = hptr;
+	return (NS_SUCCESS);
+}
diff --git a/freebsd-userspace/lib/libc/net/gethostbynis.c b/freebsd-userspace/lib/libc/net/gethostbynis.c
new file mode 100644
index 0000000..81f0093
--- /dev/null
+++ b/freebsd-userspace/lib/libc/net/gethostbynis.c
@@ -0,0 +1,354 @@
+#include "port_before.h"
+
+/*-
+ * Copyright (c) 1994, Garrett Wollman
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON 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 ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/param.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <arpa/nameser.h>
+#include <netdb.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <ctype.h>
+#include <errno.h>
+#include <string.h>
+#include <stdarg.h>
+#include <nsswitch.h>
+#include <resolv.h>		/* XXX */
+#ifdef YP
+#include <rpc/rpc.h>
+#include <rpcsvc/yp_prot.h>
+#include <rpcsvc/ypclnt.h>
+#endif
+#include "netdb_private.h"
+
+#ifdef YP
+static int
+_gethostbynis(const char *name, char *map, int af, struct hostent *he,
+    struct hostent_data *hed)
+{
+	char *p, *bp, *ep;
+	char *cp, **q;
+	char *result;
+	int resultlen, size, addrok = 0;
+	char ypbuf[YPMAXRECORD + 2];
+	res_state statp;
+
+	statp = __res_state();
+	switch(af) {
+	case AF_INET:
+		size = NS_INADDRSZ;
+		break;
+	case AF_INET6:
+		size = NS_IN6ADDRSZ;
+		break;
+	default:
+		errno = EAFNOSUPPORT;
+		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
+		return (-1);
+	}
+
+	if (hed->yp_domain == (char *)NULL)
+		if (yp_get_default_domain (&hed->yp_domain)) {
+			RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
+			return (-1);
+		}
+
+	if (yp_match(hed->yp_domain, map, name, strlen(name), &result,
+	    &resultlen)) {
+		RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
+		return (-1);
+	}
+
+	/* avoid potential memory leak */
+	bcopy((char *)result, (char *)&ypbuf, resultlen);
+	ypbuf[resultlen] = '\0';
+	free(result);
+	result = (char *)&ypbuf;
+
+	if ((cp = index(result, '\n')))
+		*cp = '\0';
+
+	cp = strpbrk(result, " \t");
+	*cp++ = '\0';
+	he->h_addr_list = hed->h_addr_ptrs;
+	he->h_addr = (char *)hed->host_addr;
+	switch (af) {
+	case AF_INET:
+		addrok = inet_aton(result, (struct in_addr *)hed->host_addr);
+		if (addrok != 1)
+			break;
+		if (statp->options & RES_USE_INET6) {
+			_map_v4v6_address((char *)hed->host_addr,
+			    (char *)hed->host_addr);
+			af = AF_INET6;
+			size = NS_IN6ADDRSZ;
+		}
+		break;
+	case AF_INET6:
+		addrok = inet_pton(af, result, hed->host_addr);
+		break;
+	}
+	if (addrok != 1) {
+		RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
+		return (-1);
+	}
+	he->h_addr_list[1] = NULL;
+	he->h_length = size;
+	he->h_addrtype = af;
+	while (*cp == ' ' || *cp == '\t')
+		cp++;
+	bp = hed->hostbuf;
+	ep = hed->hostbuf + sizeof hed->hostbuf;
+	he->h_name = bp;
+	q = he->h_aliases = hed->host_aliases;
+	p = strpbrk(cp, " \t");
+	if (p != NULL)
+		*p++ = '\0';
+	size = strlen(cp) + 1;
+	if (ep - bp < size) {
+		RES_SET_H_ERRNO(statp, NO_RECOVERY);
+		return (-1);
+	}
+	strlcpy(bp, cp, ep - bp);
+	bp += size;
+	cp = p;
+	while (cp && *cp) {
+		if (*cp == ' ' || *cp == '\t') {
+			cp++;
+			continue;
+		}
+		if (q >= &hed->host_aliases[_MAXALIASES - 1])
+			break;
+		p = strpbrk(cp, " \t");
+		if (p != NULL)
+			*p++ = '\0';
+		size = strlen(cp) + 1;
+		if (ep - bp < size)
+			break;
+		strlcpy(bp, cp, ep - bp);
+		*q++ = bp;
+		bp += size;
+		cp = p;
+	}
+	*q = NULL;
+	return (0);
+}
+
+static int
+_gethostbynisname_r(const char *name, int af, struct hostent *he,
+    struct hostent_data *hed)
+{
+	char *map;
+
+	switch (af) {
+	case AF_INET:
+		map = "hosts.byname";
+		break;
+	default:
+		map = "ipnodes.byname";
+		break;
+	}
+	return (_gethostbynis(name, map, af, he, hed));
+}
+
+static int
+_gethostbynisaddr_r(const void *addr, socklen_t len, int af,
+    struct hostent *he, struct hostent_data *hed)
+{
+	char *map;
+	char numaddr[46];
+
+	switch (af) {
+	case AF_INET:
+		map = "hosts.byaddr";
+		break;
+	default:
+		map = "ipnodes.byaddr";
+		break;
+	}
+	if (inet_ntop(af, addr, numaddr, sizeof(numaddr)) == NULL)
+		return (-1);
+	return (_gethostbynis(numaddr, map, af, he, hed));
+}
+#endif /* YP */
+
+/* XXX _gethostbynisname/_gethostbynisaddr only used by getipnodeby*() */
+struct hostent *
+_gethostbynisname(const char *name, int af)
+{
+#ifdef YP
+	struct hostent *he;
+	struct hostent_data *hed;
+	u_long oresopt;
+	int error;
+	res_state statp;
+
+	statp = __res_state();
+	if ((he = __hostent_init()) == NULL ||
+	    (hed = __hostent_data_init()) == NULL) {
+		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
+		return (NULL);
+	}
+
+	oresopt = statp->options;
+	statp->options &= ~RES_USE_INET6;
+	error = _gethostbynisname_r(name, af, he, hed);
+	statp->options = oresopt;
+	return (error == 0) ? he : NULL;
+#else
+	return (NULL);
+#endif
+}
+
+struct hostent *
+_gethostbynisaddr(const void *addr, socklen_t len, int af)
+{
+#ifdef YP
+	struct hostent *he;
+	struct hostent_data *hed;
+	u_long oresopt;
+	int error;
+	res_state statp;
+
+	statp = __res_state();
+	if ((he = __hostent_init()) == NULL ||
+	    (hed = __hostent_data_init()) == NULL) {
+		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
+		return (NULL);
+	}
+
+	oresopt = statp->options;
+	statp->options &= ~RES_USE_INET6;
+	error = _gethostbynisaddr_r(addr, len, af, he, hed);
+	statp->options = oresopt;
+	return (error == 0) ? he : NULL;
+#else
+	return (NULL);
+#endif
+}
+
+int
+_nis_gethostbyname(void *rval, void *cb_data, va_list ap)
+{
+#ifdef YP
+	const char *name;
+	int af;
+	char *buffer;
+	size_t buflen;
+	int *errnop, *h_errnop;
+	struct hostent *hptr, he;
+	struct hostent_data *hed;
+	res_state statp;
+
+	name = va_arg(ap, const char *);
+	af = va_arg(ap, int);
+	hptr = va_arg(ap, struct hostent *);
+	buffer = va_arg(ap, char *);
+	buflen = va_arg(ap, size_t);
+	errnop = va_arg(ap, int *);
+	h_errnop = va_arg(ap, int *);
+
+	*((struct hostent **)rval) = NULL;
+
+	statp = __res_state();
+	if ((hed = __hostent_data_init()) == NULL) {
+		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
+		*h_errnop = statp->res_h_errno;
+		return (NS_NOTFOUND);
+	}
+
+	if (_gethostbynisname_r(name, af, &he, hed) != 0) {
+		*h_errnop = statp->res_h_errno;
+		return (NS_NOTFOUND);
+	}
+	if (__copy_hostent(&he, hptr, buffer, buflen) != 0) {
+		*errnop = errno;
+		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
+		*h_errnop = statp->res_h_errno;
+		return (NS_RETURN);
+	}
+	*((struct hostent **)rval) = hptr;
+	return (NS_SUCCESS);
+#else
+	*((struct hostent **)rval) = NULL;
+	return (NS_UNAVAIL);
+#endif
+}
+
+int
+_nis_gethostbyaddr(void *rval, void *cb_data, va_list ap)
+{
+#ifdef YP
+	const void *addr;
+	socklen_t len;
+	int af;
+	char *buffer;
+	size_t buflen;
+	int *errnop, *h_errnop;
+	struct hostent *hptr, he;
+	struct hostent_data *hed;
+	res_state statp;
+
+	addr = va_arg(ap, const void *);
+	len = va_arg(ap, socklen_t);
+	af = va_arg(ap, int);
+	hptr = va_arg(ap, struct hostent *);
+	buffer = va_arg(ap, char *);
+	buflen = va_arg(ap, size_t);
+	errnop = va_arg(ap, int *);
+	h_errnop = va_arg(ap, int *);
+
+	*((struct hostent **)rval) = NULL;
+
+	statp = __res_state();
+	if ((hed = __hostent_data_init()) == NULL) {
+		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
+		*h_errnop = statp->res_h_errno;
+		return (NS_NOTFOUND);
+	}
+
+	if (_gethostbynisaddr_r(addr, len, af, &he, hed) != 0) {
+		*h_errnop = statp->res_h_errno;
+		return (NS_NOTFOUND);
+	}
+	if (__copy_hostent(&he, hptr, buffer, buflen) != 0) {
+		*errnop = errno;
+		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
+		*h_errnop = statp->res_h_errno;
+		return (NS_RETURN);
+	}
+	*((struct hostent **)rval) = hptr;
+	return (NS_SUCCESS);
+#else
+	*((struct hostent **)rval) = NULL;
+	return (NS_UNAVAIL);
+#endif
+}
diff --git a/freebsd-userspace/lib/libc/net/gethostnamadr.c b/freebsd-userspace/lib/libc/net/gethostnamadr.c
new file mode 100644
index 0000000..d5aaa38
--- /dev/null
+++ b/freebsd-userspace/lib/libc/net/gethostnamadr.c
@@ -0,0 +1,736 @@
+#include "port_before.h"
+
+/*-
+ * Copyright (c) 1994, Garrett Wollman
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON 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 ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include "namespace.h"
+#include "reentrant.h"
+#include <sys/param.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <netdb.h>
+#include <stdio.h>
+#include <ctype.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdarg.h>
+#include <nsswitch.h>
+#include <arpa/nameser.h>		/* XXX hack for _res */
+#include <resolv.h>			/* XXX hack for _res */
+#include "un-namespace.h"
+#include "netdb_private.h"
+#ifdef NS_CACHING
+#include "nscache.h"
+#endif
+
+extern int _ht_gethostbyname(void *, void *, va_list);
+extern int _dns_gethostbyname(void *, void *, va_list);
+extern int _nis_gethostbyname(void *, void *, va_list);
+extern int _ht_gethostbyaddr(void *, void *, va_list);
+extern int _dns_gethostbyaddr(void *, void *, va_list);
+extern int _nis_gethostbyaddr(void *, void *, va_list);
+
+static int gethostbyname_internal(const char *, int, struct hostent *, char *,
+    size_t, struct hostent **, int *, res_state);
+
+/* Host lookup order if nsswitch.conf is broken or nonexistant */
+static const ns_src default_src[] = {
+	{ NSSRC_FILES, NS_SUCCESS },
+	{ NSSRC_DNS, NS_SUCCESS },
+	{ 0 }
+};
+#ifdef NS_CACHING
+static int host_id_func(char *, size_t *, va_list, void *);
+static int host_marshal_func(char *, size_t *, void *, va_list, void *);
+static int host_unmarshal_func(char *, size_t, void *, va_list, void *);
+#endif
+
+NETDB_THREAD_ALLOC(hostent)
+NETDB_THREAD_ALLOC(hostent_data)
+NETDB_THREAD_ALLOC(hostdata)
+
+static void
+hostent_free(void *ptr)
+{
+	free(ptr);
+}
+
+static void
+hostent_data_free(void *ptr)
+{
+	struct hostent_data *hed = ptr;
+
+	if (hed == NULL)
+		return;
+	hed->stayopen = 0;
+	_endhosthtent(hed);
+	free(hed);
+}
+
+static void
+hostdata_free(void *ptr)
+{
+	free(ptr);
+}
+
+int
+__copy_hostent(struct hostent *he, struct hostent *hptr, char *buf,
+    size_t buflen)
+{
+	char *cp;
+	char **ptr;
+	int i, n;
+	int nptr, len;
+
+	/* Find out the amount of space required to store the answer. */
+	nptr = 2; /* NULL ptrs */
+	len = (char *)ALIGN(buf) - buf;
+	for (i = 0; he->h_addr_list[i]; i++, nptr++) {
+		len += he->h_length;
+	}
+	for (i = 0; he->h_aliases[i]; i++, nptr++) {
+		len += strlen(he->h_aliases[i]) + 1;
+	}
+	len += strlen(he->h_name) + 1;
+	len += nptr * sizeof(char*);
+
+	if (len > buflen) {
+		errno = ERANGE;
+		return (-1);
+	}
+
+	/* copy address size and type */
+	hptr->h_addrtype = he->h_addrtype;
+	n = hptr->h_length = he->h_length;
+
+	ptr = (char **)ALIGN(buf);
+	cp = (char *)ALIGN(buf) + nptr * sizeof(char *);
+
+	/* copy address list */
+	hptr->h_addr_list = ptr;
+	for (i = 0; he->h_addr_list[i]; i++ , ptr++) {
+		memcpy(cp, he->h_addr_list[i], n);
+		hptr->h_addr_list[i] = cp;
+		cp += n;
+	}
+	hptr->h_addr_list[i] = NULL;
+	ptr++;
+
+	/* copy official name */
+	n = strlen(he->h_name) + 1;
+	strcpy(cp, he->h_name);
+	hptr->h_name = cp;
+	cp += n;
+
+	/* copy aliases */
+	hptr->h_aliases = ptr;
+	for (i = 0 ; he->h_aliases[i]; i++) {
+		n = strlen(he->h_aliases[i]) + 1;
+		strcpy(cp, he->h_aliases[i]);
+		hptr->h_aliases[i] = cp;
+		cp += n;
+	}
+	hptr->h_aliases[i] = NULL;
+
+	return (0);
+}
+
+#ifdef NS_CACHING
+static int
+host_id_func(char *buffer, size_t *buffer_size, va_list ap, void *cache_mdata)
+{
+	res_state statp;
+	u_long res_options;
+
+	const int op_id = 1;
+	char *str;
+	void *addr;
+	socklen_t len;
+	int type;
+
+	size_t desired_size, size;
+	enum nss_lookup_type lookup_type;
+	char *p;
+	int res = NS_UNAVAIL;
+
+	statp = __res_state();
+	res_options = statp->options & (RES_RECURSE | RES_DEFNAMES |
+	    RES_DNSRCH | RES_NOALIASES | RES_USE_INET6);
+
+	lookup_type = (enum nss_lookup_type)cache_mdata;
+	switch (lookup_type) {
+	case nss_lt_name:
+		str = va_arg(ap, char *);
+		type = va_arg(ap, int);
+
+		size = strlen(str);
+		desired_size = sizeof(res_options) + sizeof(int) +
+		    sizeof(enum nss_lookup_type) + sizeof(int) + size + 1;
+
+		if (desired_size > *buffer_size) {
+			res = NS_RETURN;
+			goto fin;
+		}
+
+		p = buffer;
+
+		memcpy(p, &res_options, sizeof(res_options));
+		p += sizeof(res_options);
+
+		memcpy(p, &op_id, sizeof(int));
+		p += sizeof(int);
+
+		memcpy(p, &lookup_type, sizeof(enum nss_lookup_type));
+		p += sizeof(int);
+
+		memcpy(p, &type, sizeof(int));
+		p += sizeof(int);
+
+		memcpy(p, str, size + 1);
+
+		res = NS_SUCCESS;
+		break;
+	case nss_lt_id:
+		addr = va_arg(ap, void *);
+		len = va_arg(ap, socklen_t);
+		type = va_arg(ap, int);
+
+		desired_size = sizeof(res_options) + sizeof(int) +
+		    sizeof(enum nss_lookup_type) + sizeof(int) +
+		    sizeof(socklen_t) + len;
+
+		if (desired_size > *buffer_size) {
+			res = NS_RETURN;
+			goto fin;
+		}
+
+		p = buffer;
+		memcpy(p, &res_options, sizeof(res_options));
+		p += sizeof(res_options);
+
+		memcpy(p, &op_id, sizeof(int));
+		p += sizeof(int);
+
+		memcpy(p, &lookup_type, sizeof(enum nss_lookup_type));
+		p += sizeof(int);
+
+		memcpy(p, &type, sizeof(int));
+		p += sizeof(int);
+
+		memcpy(p, &len, sizeof(socklen_t));
+		p += sizeof(socklen_t);
+
+		memcpy(p, addr, len);
+
+		res = NS_SUCCESS;
+		break;
+	default:
+		/* should be unreachable */
+		return (NS_UNAVAIL);
+	}
+
+fin:
+	*buffer_size = desired_size;
+	return (res);
+}
+
+static int
+host_marshal_func(char *buffer, size_t *buffer_size, void *retval, va_list ap,
+    void *cache_mdata)
+{
+	char *str;
+	void *addr;
+	socklen_t len;
+	int type;
+	struct hostent *ht;
+
+	struct hostent new_ht;
+	size_t desired_size, aliases_size, addr_size, size;
+	char *p, **iter;
+
+	switch ((enum nss_lookup_type)cache_mdata) {
+	case nss_lt_name:
+		str = va_arg(ap, char *);
+		type = va_arg(ap, int);
+		break;
+	case nss_lt_id:
+		addr = va_arg(ap, void *);
+		len = va_arg(ap, socklen_t);
+		type = va_arg(ap, int);
+		break;
+	default:
+		/* should be unreachable */
+		return (NS_UNAVAIL);
+	}
+	ht = va_arg(ap, struct hostent *);
+
+	desired_size = _ALIGNBYTES + sizeof(struct hostent) + sizeof(char *);
+	if (ht->h_name != NULL)
+		desired_size += strlen(ht->h_name) + 1;
+
+	if (ht->h_aliases != NULL) {
+		aliases_size = 0;
+		for (iter = ht->h_aliases; *iter; ++iter) {
+			desired_size += strlen(*iter) + 1;
+			++aliases_size;
+		}
+
+		desired_size += _ALIGNBYTES +
+		    (aliases_size + 1) * sizeof(char *);
+	}
+
+	if (ht->h_addr_list != NULL) {
+		addr_size = 0;
+		for (iter = ht->h_addr_list; *iter; ++iter)
+			++addr_size;
+
+		desired_size += addr_size * _ALIGN(ht->h_length);
+		desired_size += _ALIGNBYTES + (addr_size + 1) * sizeof(char *);
+	}
+
+	if (desired_size > *buffer_size) {
+		/* this assignment is here for future use */
+		*buffer_size = desired_size;
+		return (NS_RETURN);
+	}
+
+	memcpy(&new_ht, ht, sizeof(struct hostent));
+	memset(buffer, 0, desired_size);
+
+	*buffer_size = desired_size;
+	p = buffer + sizeof(struct hostent) + sizeof(char *);
+	memcpy(buffer + sizeof(struct hostent), &p, sizeof(char *));
+	p = (char *)_ALIGN(p);
+
+	if (new_ht.h_name != NULL) {
+		size = strlen(new_ht.h_name);
+		memcpy(p, new_ht.h_name, size);
+		new_ht.h_name = p;
+		p += size + 1;
+	}
+
+	if (new_ht.h_aliases != NULL) {
+		p = (char *)_ALIGN(p);
+		memcpy(p, new_ht.h_aliases, sizeof(char *) * aliases_size);
+		new_ht.h_aliases = (char **)p;
+		p += sizeof(char *) * (aliases_size + 1);
+
+		for (iter = new_ht.h_aliases; *iter; ++iter) {
+			size = strlen(*iter);
+			memcpy(p, *iter, size);
+			*iter = p;
+			p += size + 1;
+		}
+	}
+
+	if (new_ht.h_addr_list != NULL) {
+		p = (char *)_ALIGN(p);
+		memcpy(p, new_ht.h_addr_list, sizeof(char *) * addr_size);
+		new_ht.h_addr_list = (char **)p;
+		p += sizeof(char *) * (addr_size + 1);
+
+		size = _ALIGN(new_ht.h_length);
+		for (iter = new_ht.h_addr_list; *iter; ++iter) {
+			memcpy(p, *iter, size);
+			*iter = p;
+			p += size + 1;
+		}
+	}
+	memcpy(buffer, &new_ht, sizeof(struct hostent));
+	return (NS_SUCCESS);
+}
+
+static int
+host_unmarshal_func(char *buffer, size_t buffer_size, void *retval, va_list ap,
+    void *cache_mdata)
+{
+	char *str;
+	void *addr;
+	socklen_t len;
+	int type;
+	struct hostent *ht;
+
+	char *p;
+	char **iter;
+	char *orig_buf;
+	size_t orig_buf_size;
+
+	switch ((enum nss_lookup_type)cache_mdata) {
+	case nss_lt_name:
+		str = va_arg(ap, char *);
+		type = va_arg(ap, int);
+		break;
+	case nss_lt_id:
+		addr = va_arg(ap, void *);
+		len = va_arg(ap, socklen_t);
+		type = va_arg(ap, int);
+		break;
+	default:
+		/* should be unreachable */
+		return (NS_UNAVAIL);
+	}
+
+	ht = va_arg(ap, struct hostent *);
+	orig_buf = va_arg(ap, char *);
+	orig_buf_size = va_arg(ap, size_t);
+
+	if (orig_buf_size <
+	    buffer_size - sizeof(struct hostent) - sizeof(char *)) {
+		errno = ERANGE;
+		return (NS_RETURN);
+	}
+
+	memcpy(ht, buffer, sizeof(struct hostent));
+	memcpy(&p, buffer + sizeof(struct hostent), sizeof(char *));
+
+	orig_buf = (char *)_ALIGN(orig_buf);
+	memcpy(orig_buf, buffer + sizeof(struct hostent) + sizeof(char *) +
+	    _ALIGN(p) - (size_t)p,
+	    buffer_size - sizeof(struct hostent) - sizeof(char *) -
+	    _ALIGN(p) + (size_t)p);
+	p = (char *)_ALIGN(p);
+
+	NS_APPLY_OFFSET(ht->h_name, orig_buf, p, char *);
+	if (ht->h_aliases != NULL) {
+		NS_APPLY_OFFSET(ht->h_aliases, orig_buf, p, char **);
+
+		for (iter = ht->h_aliases; *iter; ++iter)
+			NS_APPLY_OFFSET(*iter, orig_buf, p, char *);
+	}
+
+	if (ht->h_addr_list != NULL) {
+		NS_APPLY_OFFSET(ht->h_addr_list, orig_buf, p, char **);
+
+		for (iter = ht->h_addr_list; *iter; ++iter)
+			NS_APPLY_OFFSET(*iter, orig_buf, p, char *);
+	}
+
+	*((struct hostent **)retval) = ht;
+	return (NS_SUCCESS);
+}
+#endif /* NS_CACHING */
+
+static int
+fakeaddr(const char *name, int af, struct hostent *hp, char *buf,
+    size_t buflen, res_state statp)
+{
+	struct hostent_data *hed;
+	struct hostent he;
+
+	if ((hed = __hostent_data_init()) == NULL) {
+		errno = ENOMEM;
+		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
+		return (-1);
+	}
+
+	if ((af != AF_INET ||
+	    inet_aton(name, (struct in_addr *)hed->host_addr) != 1) &&
+	    inet_pton(af, name, hed->host_addr) != 1) {
+		RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
+		return (-1);
+	}
+	strncpy(hed->hostbuf, name, MAXDNAME);
+	hed->hostbuf[MAXDNAME] = '\0';
+	if (af == AF_INET && (statp->options & RES_USE_INET6) != 0U) {
+		_map_v4v6_address((char *)hed->host_addr,
+		    (char *)hed->host_addr);
+		af = AF_INET6;
+	}
+	he.h_addrtype = af;
+	switch(af) {
+	case AF_INET:
+		he.h_length = NS_INADDRSZ;
+		break;
+	case AF_INET6:
+		he.h_length = NS_IN6ADDRSZ;
+		break;
+	default:
+		errno = EAFNOSUPPORT;
+		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
+		return (-1);
+	}
+	he.h_name = hed->hostbuf;
+	he.h_aliases = hed->host_aliases;
+	hed->host_aliases[0] = NULL;
+	hed->h_addr_ptrs[0] = (char *)hed->host_addr;
+	hed->h_addr_ptrs[1] = NULL;
+	he.h_addr_list = hed->h_addr_ptrs;
+	if (__copy_hostent(&he, hp, buf, buflen) != 0) {
+		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
+		return (-1);
+	}
+	RES_SET_H_ERRNO(statp, NETDB_SUCCESS);
+	return (0);
+}
+
+int
+gethostbyname_r(const char *name, struct hostent *he, char *buffer,
+    size_t buflen, struct hostent **result, int *h_errnop)
+{
+	res_state statp;
+
+	statp = __res_state();
+	if ((statp->options & RES_INIT) == 0 && res_ninit(statp) == -1) {
+		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
+		return (-1);
+	}
+	if (statp->options & RES_USE_INET6) {
+		if (fakeaddr(name, AF_INET, he, buffer, buflen, statp) == 0) {
+			*result = he;
+			return (0);
+		}
+		if (gethostbyname_internal(name, AF_INET6, he, buffer, buflen,
+		    result, h_errnop, statp) == 0)
+			return (0);
+	}
+	return (gethostbyname_internal(name, AF_INET, he, buffer, buflen,
+	    result, h_errnop, statp));
+}
+
+int
+gethostbyname2_r(const char *name, int af, struct hostent *he, char *buffer,
+    size_t buflen, struct hostent **result, int *h_errnop)
+{
+	res_state statp;
+
+	statp = __res_state();
+	if ((statp->options & RES_INIT) == 0 && res_ninit(statp) == -1) {
+		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
+		return (-1);
+	}
+	return (gethostbyname_internal(name, af, he, buffer, buflen, result,
+	    h_errnop, statp));
+}
+
+int
+gethostbyname_internal(const char *name, int af, struct hostent *hp, char *buf,
+    size_t buflen, struct hostent **result, int *h_errnop, res_state statp)
+{
+	const char *cp;
+	int rval, ret_errno = 0;
+	char abuf[MAXDNAME];
+
+#ifdef NS_CACHING
+	static const nss_cache_info cache_info =
+		NS_COMMON_CACHE_INFO_INITIALIZER(
+		hosts, (void *)nss_lt_name,
+		host_id_func, host_marshal_func, host_unmarshal_func);
+#endif
+	static const ns_dtab dtab[] = {
+		NS_FILES_CB(_ht_gethostbyname, NULL)
+		{ NSSRC_DNS, _dns_gethostbyname, NULL },
+		NS_NIS_CB(_nis_gethostbyname, NULL) /* force -DHESIOD */
+#ifdef NS_CACHING
+		NS_CACHE_CB(&cache_info)
+#endif
+		{ 0 }
+	};
+
+	switch (af) {
+	case AF_INET:
+	case AF_INET6:
+		break;
+	default:
+		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
+		*h_errnop = statp->res_h_errno;
+		errno = EAFNOSUPPORT;
+		return (-1);
+	}
+
+	/*
+	 * if there aren't any dots, it could be a user-level alias.
+	 * this is also done in res_query() since we are not the only
+	 * function that looks up host names.
+	 */
+	if (!strchr(name, '.') &&
+	    (cp = res_hostalias(statp, name, abuf, sizeof abuf)))
+		name = cp;
+
+	if (fakeaddr(name, af, hp, buf, buflen, statp) == 0) {
+		*result = hp;
+		return (0);
+	}
+
+	rval = _nsdispatch((void *)result, dtab, NSDB_HOSTS,
+	    "gethostbyname2_r", default_src, name, af, hp, buf, buflen,
+	    &ret_errno, h_errnop);
+
+	if (rval != NS_SUCCESS) {
+		errno = ret_errno;
+		return ((ret_errno != 0) ? ret_errno : -1);
+	}
+	return (0);
+}
+
+int
+gethostbyaddr_r(const void *addr, socklen_t len, int af, struct hostent *hp,
+    char *buf, size_t buflen, struct hostent **result, int *h_errnop)
+{
+	const u_char *uaddr = (const u_char *)addr;
+	const struct in6_addr *addr6;
+	socklen_t size;
+	int rval, ret_errno = 0;
+	res_state statp;
+
+#ifdef NS_CACHING
+	static const nss_cache_info cache_info =
+		NS_COMMON_CACHE_INFO_INITIALIZER(
+		hosts, (void *)nss_lt_id,
+		host_id_func, host_marshal_func, host_unmarshal_func);
+#endif
+	static const ns_dtab dtab[] = {
+		NS_FILES_CB(_ht_gethostbyaddr, NULL)
+		{ NSSRC_DNS, _dns_gethostbyaddr, NULL },
+		NS_NIS_CB(_nis_gethostbyaddr, NULL) /* force -DHESIOD */
+#ifdef NS_CACHING
+		NS_CACHE_CB(&cache_info)
+#endif
+		{ 0 }
+	};
+
+	statp = __res_state();
+	if ((statp->options & RES_INIT) == 0 && res_ninit(statp) == -1) {
+		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
+		*h_errnop = statp->res_h_errno;
+		return (-1);
+	}
+
+	if (af == AF_INET6 && len == NS_IN6ADDRSZ) {
+		addr6 = (const struct in6_addr *)addr;
+		if (IN6_IS_ADDR_LINKLOCAL(addr6)) {
+			RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
+			*h_errnop = statp->res_h_errno;
+			return (-1);
+		}
+		if (IN6_IS_ADDR_V4MAPPED(addr6) ||
+		    IN6_IS_ADDR_V4COMPAT(addr6)) {
+			/* Unmap. */
+			uaddr += NS_IN6ADDRSZ - NS_INADDRSZ;
+			af = AF_INET;
+			len = NS_INADDRSZ;
+		}
+	}
+	switch (af) {
+	case AF_INET:
+		size = NS_INADDRSZ;
+		break;
+	case AF_INET6:
+		size = NS_IN6ADDRSZ;
+		break;
+	default:
+		errno = EAFNOSUPPORT;
+		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
+		*h_errnop = statp->res_h_errno;
+		return (-1);
+	}
+	if (size != len) {
+		errno = EINVAL;
+		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
+		*h_errnop = statp->res_h_errno;
+		return (-1);
+	}
+
+	rval = _nsdispatch((void *)result, dtab, NSDB_HOSTS,
+	    "gethostbyaddr_r", default_src, uaddr, len, af, hp, buf, buflen,
+	    &ret_errno, h_errnop);
+
+	if (rval != NS_SUCCESS) {
+		errno = ret_errno;
+		return ((ret_errno != 0) ? ret_errno : -1);
+	}
+	return (0);
+}
+
+struct hostent *
+gethostbyname(const char *name)
+{
+	struct hostdata *hd;
+	struct hostent *rval;
+	int ret_h_errno;
+
+	if ((hd = __hostdata_init()) == NULL)
+		return (NULL);
+	if (gethostbyname_r(name, &hd->host, hd->data, sizeof(hd->data), &rval,
+	    &ret_h_errno) != 0)
+		return (NULL);
+	return (rval);
+}
+
+struct hostent *
+gethostbyname2(const char *name, int af)
+{
+	struct hostdata *hd;
+	struct hostent *rval;
+	int ret_h_errno;
+
+	if ((hd = __hostdata_init()) == NULL)
+		return (NULL);
+	if (gethostbyname2_r(name, af, &hd->host, hd->data, sizeof(hd->data),
+	    &rval, &ret_h_errno) != 0)
+		return (NULL);
+	return (rval);
+}
+
+struct hostent *
+gethostbyaddr(const void *addr, socklen_t len, int af)
+{
+	struct hostdata *hd;
+	struct hostent *rval;
+	int ret_h_errno;
+
+	if ((hd = __hostdata_init()) == NULL)
+		return (NULL);
+	if (gethostbyaddr_r(addr, len, af, &hd->host, hd->data,
+	    sizeof(hd->data), &rval, &ret_h_errno) != 0)
+		return (NULL);
+	return (rval);
+}
+
+void
+sethostent(int stayopen)
+{
+	struct hostent_data *hed;
+
+	if ((hed = __hostent_data_init()) == NULL)
+		return;
+	_sethosthtent(stayopen, hed);
+	_sethostdnsent(stayopen);
+}
+
+void
+endhostent(void)
+{
+	struct hostent_data *hed;
+
+	if ((hed = __hostent_data_init()) == NULL)
+		return;
+	_endhosthtent(hed);
+	_endhostdnsent();
+}
diff --git a/freebsd-userspace/lib/libc/net/getifaddrs.c b/freebsd-userspace/lib/libc/net/getifaddrs.c
new file mode 100644
index 0000000..2eceadb
--- /dev/null
+++ b/freebsd-userspace/lib/libc/net/getifaddrs.c
@@ -0,0 +1,420 @@
+#include "port_before.h"
+
+/*	$KAME: getifaddrs.c,v 1.9 2001/08/20 02:31:20 itojun Exp $	*/
+
+/*
+ * Copyright (c) 1995, 1999
+ *	Berkeley Software Design, Inc.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *
+ * THIS SOFTWARE IS PROVIDED BY Berkeley Software Design, Inc. ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL Berkeley Software Design, Inc. BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON 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 ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ *	BSDI getifaddrs.c,v 2.12 2000/02/23 14:51:59 dab Exp
+ */
+/*
+ * NOTE: SIOCGIFCONF case is not LP64 friendly.  it also does not perform
+ * try-and-error for region size.
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include "namespace.h"
+#include <sys/types.h>
+#include <sys/ioctl.h>
+#include <sys/socket.h>
+#include <net/if.h>
+#ifdef	NET_RT_IFLIST
+#include <sys/param.h>
+#include <net/route.h>
+#include <sys/sysctl.h>
+#include <net/if_dl.h>
+#endif
+
+#include <errno.h>
+#include <ifaddrs.h>
+#include <stdlib.h>
+#include <string.h>
+#include "un-namespace.h"
+
+#if !defined(AF_LINK)
+#define	SA_LEN(sa)	sizeof(struct sockaddr)
+#endif
+
+#if !defined(SA_LEN)
+#define	SA_LEN(sa)	(sa)->sa_len
+#endif
+
+#define	SALIGN	(sizeof(long) - 1)
+#define	SA_RLEN(sa)	((sa)->sa_len ? (((sa)->sa_len + SALIGN) & ~SALIGN) : (SALIGN + 1))
+
+#ifndef	ALIGNBYTES
+/*
+ * On systems with a routing socket, ALIGNBYTES should match the value
+ * that the kernel uses when building the messages.
+ */
+#define	ALIGNBYTES	XXX
+#endif
+#ifndef	ALIGN
+#define	ALIGN(p)	(((u_long)(p) + ALIGNBYTES) &~ ALIGNBYTES)
+#endif
+
+#if	_BSDI_VERSION >= 199701
+#define	HAVE_IFM_DATA
+#endif
+
+#if	_BSDI_VERSION >= 199802
+/* ifam_data is very specific to recent versions of bsdi */
+#define	HAVE_IFAM_DATA
+#endif
+
+#if defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__)
+#define	HAVE_IFM_DATA
+#endif
+
+#define MAX_SYSCTL_TRY 5
+
+int
+getifaddrs(struct ifaddrs **pif)
+{
+	int icnt = 1;
+	int dcnt = 0;
+	int ncnt = 0;
+#ifdef	NET_RT_IFLIST
+	int ntry = 0;
+	int mib[6];
+	size_t needed;
+	char *buf;
+	char *next;
+	struct ifaddrs *cif = 0;
+	char *p, *p0;
+	struct rt_msghdr *rtm;
+	struct if_msghdr *ifm;
+	struct ifa_msghdr *ifam;
+	struct sockaddr_dl *dl;
+	struct sockaddr *sa;
+	struct ifaddrs *ifa, *ift;
+	u_short idx = 0;
+#else	/* NET_RT_IFLIST */
+	char buf[1024];
+	int m, sock;
+	struct ifconf ifc;
+	struct ifreq *ifr;
+	struct ifreq *lifr;
+#endif	/* NET_RT_IFLIST */
+	int i;
+	size_t len, alen;
+	char *data;
+	char *names;
+
+#ifdef	NET_RT_IFLIST
+	mib[0] = CTL_NET;
+	mib[1] = PF_ROUTE;
+	mib[2] = 0;             /* protocol */
+	mib[3] = 0;             /* wildcard address family */
+	mib[4] = NET_RT_IFLIST;
+	mib[5] = 0;             /* no flags */
+	do {
+		/*
+		 * We'll try to get addresses several times in case that
+		 * the number of addresses is unexpectedly increased during
+		 * the two sysctl calls.  This should rarely happen, but we'll
+		 * try to do our best for applications that assume success of
+		 * this library (which should usually be the case).
+		 * Portability note: since FreeBSD does not add margin of
+		 * memory at the first sysctl, the possibility of failure on
+		 * the second sysctl call is a bit higher.
+		 */
+
+		if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
+			return (-1);
+		if ((buf = malloc(needed)) == NULL)
+			return (-1);
+		if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
+			if (errno != ENOMEM || ++ntry >= MAX_SYSCTL_TRY) {
+				free(buf);
+				return (-1);
+			}
+			free(buf);
+			buf = NULL;
+		} 
+	} while (buf == NULL);
+
+	for (next = buf; next < buf + needed; next += rtm->rtm_msglen) {
+		rtm = (struct rt_msghdr *)(void *)next;
+		if (rtm->rtm_version != RTM_VERSION)
+			continue;
+		switch (rtm->rtm_type) {
+		case RTM_IFINFO:
+			ifm = (struct if_msghdr *)(void *)rtm;
+			if (ifm->ifm_addrs & RTA_IFP) {
+				idx = ifm->ifm_index;
+				++icnt;
+				dl = (struct sockaddr_dl *)(void *)(ifm + 1);
+				dcnt += SA_RLEN((struct sockaddr *)(void*)dl) +
+				    ALIGNBYTES;
+#ifdef	HAVE_IFM_DATA
+				dcnt += sizeof(ifm->ifm_data);
+#endif	/* HAVE_IFM_DATA */
+				ncnt += dl->sdl_nlen + 1;
+			} else
+				idx = 0;
+			break;
+
+		case RTM_NEWADDR:
+			ifam = (struct ifa_msghdr *)(void *)rtm;
+			if (idx && ifam->ifam_index != idx)
+				abort();	/* this cannot happen */
+
+#define	RTA_MASKS	(RTA_NETMASK | RTA_IFA | RTA_BRD)
+			if (idx == 0 || (ifam->ifam_addrs & RTA_MASKS) == 0)
+				break;
+			p = (char *)(void *)(ifam + 1);
+			++icnt;
+#ifdef	HAVE_IFAM_DATA
+			dcnt += sizeof(ifam->ifam_data) + ALIGNBYTES;
+#endif	/* HAVE_IFAM_DATA */
+			/* Scan to look for length of address */
+			alen = 0;
+			for (p0 = p, i = 0; i < RTAX_MAX; i++) {
+				if ((RTA_MASKS & ifam->ifam_addrs & (1 << i))
+				    == 0)
+					continue;
+				sa = (struct sockaddr *)(void *)p;
+				len = SA_RLEN(sa);
+				if (i == RTAX_IFA) {
+					alen = len;
+					break;
+				}
+				p += len;
+			}
+			for (p = p0, i = 0; i < RTAX_MAX; i++) {
+				if ((RTA_MASKS & ifam->ifam_addrs & (1 << i))
+				    == 0)
+					continue;
+				sa = (struct sockaddr *)(void *)p;
+				len = SA_RLEN(sa);
+				if (i == RTAX_NETMASK && SA_LEN(sa) == 0)
+					dcnt += alen;
+				else
+					dcnt += len;
+				p += len;
+			}
+			break;
+		}
+	}
+#else	/* NET_RT_IFLIST */
+	ifc.ifc_buf = buf;
+	ifc.ifc_len = sizeof(buf);
+
+	if ((sock = _socket(AF_INET, SOCK_STREAM, 0)) < 0)
+		return (-1);
+	i =  _ioctl(sock, SIOCGIFCONF, (char *)&ifc);
+	_close(sock);
+	if (i < 0)
+		return (-1);
+
+	ifr = ifc.ifc_req;
+	lifr = (struct ifreq *)&ifc.ifc_buf[ifc.ifc_len];
+
+	while (ifr < lifr) {
+		struct sockaddr *sa;
+
+		sa = &ifr->ifr_addr;
+		++icnt;
+		dcnt += SA_RLEN(sa);
+		ncnt += sizeof(ifr->ifr_name) + 1;
+		
+		if (SA_LEN(sa) < sizeof(*sa))
+			ifr = (struct ifreq *)(((char *)sa) + sizeof(*sa));
+		else
+			ifr = (struct ifreq *)(((char *)sa) + SA_LEN(sa));
+	}
+#endif	/* NET_RT_IFLIST */
+
+	if (icnt + dcnt + ncnt == 1) {
+		*pif = NULL;
+		free(buf);
+		return (0);
+	}
+	data = malloc(sizeof(struct ifaddrs) * icnt + dcnt + ncnt);
+	if (data == NULL) {
+		free(buf);
+		return(-1);
+	}
+
+	ifa = (struct ifaddrs *)(void *)data;
+	data += sizeof(struct ifaddrs) * icnt;
+	names = data + dcnt;
+
+	memset(ifa, 0, sizeof(struct ifaddrs) * icnt);
+	ift = ifa;
+
+#ifdef	NET_RT_IFLIST
+	idx = 0;
+	for (next = buf; next < buf + needed; next += rtm->rtm_msglen) {
+		rtm = (struct rt_msghdr *)(void *)next;
+		if (rtm->rtm_version != RTM_VERSION)
+			continue;
+		switch (rtm->rtm_type) {
+		case RTM_IFINFO:
+			ifm = (struct if_msghdr *)(void *)rtm;
+			if (ifm->ifm_addrs & RTA_IFP) {
+				idx = ifm->ifm_index;
+				dl = (struct sockaddr_dl *)(void *)(ifm + 1);
+
+				cif = ift;
+				ift->ifa_name = names;
+				ift->ifa_flags = (int)ifm->ifm_flags;
+				memcpy(names, dl->sdl_data,
+				    (size_t)dl->sdl_nlen);
+				names[dl->sdl_nlen] = 0;
+				names += dl->sdl_nlen + 1;
+
+				ift->ifa_addr = (struct sockaddr *)(void *)data;
+				memcpy(data, dl,
+				    (size_t)SA_LEN((struct sockaddr *)
+				    (void *)dl));
+				data += SA_RLEN((struct sockaddr *)(void *)dl);
+
+#ifdef	HAVE_IFM_DATA
+				/* ifm_data needs to be aligned */
+				ift->ifa_data = data = (void *)ALIGN(data);
+				memcpy(data, &ifm->ifm_data, sizeof(ifm->ifm_data));
+ 				data += sizeof(ifm->ifm_data);
+#else	/* HAVE_IFM_DATA */
+				ift->ifa_data = NULL;
+#endif	/* HAVE_IFM_DATA */
+
+				ift = (ift->ifa_next = ift + 1);
+			} else
+				idx = 0;
+			break;
+
+		case RTM_NEWADDR:
+			ifam = (struct ifa_msghdr *)(void *)rtm;
+			if (idx && ifam->ifam_index != idx)
+				abort();	/* this cannot happen */
+
+			if (idx == 0 || (ifam->ifam_addrs & RTA_MASKS) == 0)
+				break;
+			ift->ifa_name = cif->ifa_name;
+			ift->ifa_flags = cif->ifa_flags;
+			ift->ifa_data = NULL;
+			p = (char *)(void *)(ifam + 1);
+			/* Scan to look for length of address */
+			alen = 0;
+			for (p0 = p, i = 0; i < RTAX_MAX; i++) {
+				if ((RTA_MASKS & ifam->ifam_addrs & (1 << i))
+				    == 0)
+					continue;
+				sa = (struct sockaddr *)(void *)p;
+				len = SA_RLEN(sa);
+				if (i == RTAX_IFA) {
+					alen = len;
+					break;
+				}
+				p += len;
+			}
+			for (p = p0, i = 0; i < RTAX_MAX; i++) {
+				if ((RTA_MASKS & ifam->ifam_addrs & (1 << i))
+				    == 0)
+					continue;
+				sa = (struct sockaddr *)(void *)p;
+				len = SA_RLEN(sa);
+				switch (i) {
+				case RTAX_IFA:
+					ift->ifa_addr =
+					    (struct sockaddr *)(void *)data;
+					memcpy(data, p, len);
+					data += len;
+					break;
+
+				case RTAX_NETMASK:
+					ift->ifa_netmask =
+					    (struct sockaddr *)(void *)data;
+					if (SA_LEN(sa) == 0) {
+						memset(data, 0, alen);
+						data += alen;
+						break;
+					}
+					memcpy(data, p, len);
+					data += len;
+					break;
+
+				case RTAX_BRD:
+					ift->ifa_broadaddr =
+					    (struct sockaddr *)(void *)data;
+					memcpy(data, p, len);
+					data += len;
+					break;
+				}
+				p += len;
+			}
+
+#ifdef	HAVE_IFAM_DATA
+			/* ifam_data needs to be aligned */
+			ift->ifa_data = data = (void *)ALIGN(data);
+			memcpy(data, &ifam->ifam_data, sizeof(ifam->ifam_data));
+			data += sizeof(ifam->ifam_data);
+#endif	/* HAVE_IFAM_DATA */
+
+			ift = (ift->ifa_next = ift + 1);
+			break;
+		}
+	}
+
+	free(buf);
+#else	/* NET_RT_IFLIST */
+	ifr = ifc.ifc_req;
+	lifr = (struct ifreq *)&ifc.ifc_buf[ifc.ifc_len];
+
+	while (ifr < lifr) {
+		struct sockaddr *sa;
+
+		ift->ifa_name = names;
+		names[sizeof(ifr->ifr_name)] = 0;
+		strncpy(names, ifr->ifr_name, sizeof(ifr->ifr_name));
+		while (*names++)
+			;
+
+		ift->ifa_addr = (struct sockaddr *)data;
+		sa = &ifr->ifr_addr;
+		memcpy(data, sa, SA_LEN(sa));
+		data += SA_RLEN(sa);
+		
+		ifr = (struct ifreq *)(((char *)sa) + SA_LEN(sa));
+		ift = (ift->ifa_next = ift + 1);
+	}
+#endif	/* NET_RT_IFLIST */
+	if (--ift >= ifa) {
+		ift->ifa_next = NULL;
+		*pif = ifa;
+	} else {
+		*pif = NULL;
+		free(ifa);
+	}
+	return (0);
+}
+
+void
+freeifaddrs(struct ifaddrs *ifp)
+{
+
+	free(ifp);
+}
diff --git a/freebsd-userspace/lib/libc/net/getifmaddrs.c b/freebsd-userspace/lib/libc/net/getifmaddrs.c
new file mode 100644
index 0000000..708c7ae
--- /dev/null
+++ b/freebsd-userspace/lib/libc/net/getifmaddrs.c
@@ -0,0 +1,205 @@
+#include "port_before.h"
+
+/*
+ * Copyright (c) 2003 Bruce M. Simpson.
+ * All rights reserved
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *        This product includes software developed by Bruce M. Simpson.
+ * 4. Neither the name of Bruce M. Simpson nor the names of other
+ *    contributors may be used to endorse or promote products derived
+ *    from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY BRUCE M. SIMPSON AND AFFILIATES
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL BRUCE M. SIMPSON OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON 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 ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include "namespace.h"
+#include <sys/param.h>
+#include <sys/sysctl.h>
+#include <sys/ioctl.h>
+#include <sys/socket.h>
+#include <net/if.h>
+#include <net/if_dl.h>
+#include <net/route.h>
+
+#include <errno.h>
+#include <ifaddrs.h>
+#include <stdlib.h>
+#include <string.h>
+#include "un-namespace.h"
+
+#define	SALIGN	(sizeof(long) - 1)
+#define	SA_RLEN(sa)	((sa)->sa_len ? (((sa)->sa_len + SALIGN) & ~SALIGN) : \
+			    (SALIGN + 1))
+#define	MAX_SYSCTL_TRY	5
+#define	RTA_MASKS	(RTA_GATEWAY | RTA_IFP | RTA_IFA)
+
+int
+getifmaddrs(struct ifmaddrs **pif)
+{
+	int icnt = 1;
+	int dcnt = 0;
+	int ntry = 0;
+	size_t len;
+	size_t needed;
+	int mib[6];
+	int i;
+	char *buf;
+	char *data;
+	char *next;
+	char *p;
+	struct ifma_msghdr *ifmam;
+	struct ifmaddrs *ifa, *ift;
+	struct rt_msghdr *rtm;
+	struct sockaddr *sa;
+
+	mib[0] = CTL_NET;
+	mib[1] = PF_ROUTE;
+	mib[2] = 0;             /* protocol */
+	mib[3] = 0;             /* wildcard address family */
+	mib[4] = NET_RT_IFMALIST;
+	mib[5] = 0;             /* no flags */
+	do {
+		if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
+			return (-1);
+		if ((buf = malloc(needed)) == NULL)
+			return (-1);
+		if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
+			if (errno != ENOMEM || ++ntry >= MAX_SYSCTL_TRY) {
+				free(buf);
+				return (-1);
+			}
+			free(buf);
+			buf = NULL;
+		} 
+	} while (buf == NULL);
+
+	for (next = buf; next < buf + needed; next += rtm->rtm_msglen) {
+		rtm = (struct rt_msghdr *)(void *)next;
+		if (rtm->rtm_version != RTM_VERSION)
+			continue;
+		switch (rtm->rtm_type) {
+		case RTM_NEWMADDR:
+			ifmam = (struct ifma_msghdr *)(void *)rtm;
+			if ((ifmam->ifmam_addrs & RTA_IFA) == 0)
+				break;
+			icnt++;
+			p = (char *)(ifmam + 1);
+			for (i = 0; i < RTAX_MAX; i++) {
+				if ((RTA_MASKS & ifmam->ifmam_addrs &
+				    (1 << i)) == 0)
+					continue;
+				sa = (struct sockaddr *)(void *)p;
+				len = SA_RLEN(sa);
+				dcnt += len;
+				p += len;
+			}
+			break;
+		}
+	}
+
+	data = malloc(sizeof(struct ifmaddrs) * icnt + dcnt);
+	if (data == NULL) {
+		free(buf);
+		return (-1);
+	}
+
+	ifa = (struct ifmaddrs *)(void *)data;
+	data += sizeof(struct ifmaddrs) * icnt;
+
+	memset(ifa, 0, sizeof(struct ifmaddrs) * icnt);
+	ift = ifa;
+
+	for (next = buf; next < buf + needed; next += rtm->rtm_msglen) {
+		rtm = (struct rt_msghdr *)(void *)next;
+		if (rtm->rtm_version != RTM_VERSION)
+			continue;
+
+		switch (rtm->rtm_type) {
+		case RTM_NEWMADDR:
+			ifmam = (struct ifma_msghdr *)(void *)rtm;
+			if ((ifmam->ifmam_addrs & RTA_IFA) == 0)
+				break;
+
+			p = (char *)(ifmam + 1);
+			for (i = 0; i < RTAX_MAX; i++) {
+				if ((RTA_MASKS & ifmam->ifmam_addrs &
+				    (1 << i)) == 0)
+					continue;
+				sa = (struct sockaddr *)(void *)p;
+				len = SA_RLEN(sa);
+				switch (i) {
+				case RTAX_GATEWAY:
+					ift->ifma_lladdr =
+					    (struct sockaddr *)(void *)data;
+					memcpy(data, p, len);
+					data += len;
+					break;
+
+				case RTAX_IFP:
+					ift->ifma_name =
+					    (struct sockaddr *)(void *)data;
+					memcpy(data, p, len);
+					data += len;
+					break;
+
+				case RTAX_IFA:
+					ift->ifma_addr =
+					    (struct sockaddr *)(void *)data;
+					memcpy(data, p, len);
+					data += len;
+					break;
+
+				default:
+					data += len;
+					break;
+				}
+				p += len;
+			}
+			ift->ifma_next = ift + 1;
+			ift = ift->ifma_next;
+			break;
+		}
+	}
+
+	free(buf);
+
+	if (ift > ifa) {
+		ift--;
+		ift->ifma_next = NULL;
+		*pif = ifa;
+	} else {
+		*pif = NULL;
+		free(ifa);
+	}
+	return (0);
+}
+
+void
+freeifmaddrs(struct ifmaddrs *ifmp)
+{
+
+	free(ifmp);
+}
diff --git a/freebsd-userspace/lib/libc/net/getnameinfo.c b/freebsd-userspace/lib/libc/net/getnameinfo.c
new file mode 100644
index 0000000..4abd11f
--- /dev/null
+++ b/freebsd-userspace/lib/libc/net/getnameinfo.c
@@ -0,0 +1,455 @@
+#include "port_before.h"
+
+/*	$KAME: getnameinfo.c,v 1.61 2002/06/27 09:25:47 itojun Exp $	*/
+
+/*
+ * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
+ * Copyright (c) 2000 Ben Harris.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the project nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON 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 ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+/*
+ * Issues to be discussed:
+ * - Thread safe-ness must be checked
+ * - RFC2553 says that we should raise error on short buffer.  X/Open says
+ *   we need to truncate the result.  We obey RFC2553 (and X/Open should be
+ *   modified).  ipngwg rough consensus seems to follow RFC2553.
+ * - What is "local" in NI_FQDN?
+ * - NI_NAMEREQD and NI_NUMERICHOST conflict with each other.
+ * - (KAME extension) always attach textual scopeid (fe80::1%lo0), if
+ *   sin6_scope_id is filled - standardization status?
+ *   XXX breaks backward compat for code that expects no scopeid.
+ *   beware on merge.
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <net/if.h>
+#include <net/if_dl.h>
+#include <net/if_types.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <arpa/nameser.h>
+#include <netdb.h>
+#include <resolv.h>
+#include <string.h>
+#include <stddef.h>
+#include <errno.h>
+
+static int	getnameinfo_inet(const struct sockaddr *, socklen_t, char *,
+    size_t, char *, size_t, int);
+#ifdef INET6
+static int ip6_parsenumeric(const struct sockaddr *, const char *, char *,
+    size_t, int);
+static int ip6_sa2str(const struct sockaddr_in6 *, char *, size_t, int);
+#endif
+static int	getnameinfo_link(const struct sockaddr *, socklen_t, char *,
+    size_t, char *, size_t, int);
+static int	hexname(const u_int8_t *, size_t, char *, size_t);
+
+int
+getnameinfo(const struct sockaddr *sa, socklen_t salen,
+    char *host, size_t hostlen, char *serv, size_t servlen,
+    int flags)
+{
+
+	switch (sa->sa_family) {
+	case AF_INET:
+#ifdef INET6
+	case AF_INET6:
+#endif
+		return getnameinfo_inet(sa, salen, host, hostlen, serv,
+		    servlen, flags);
+	case AF_LINK:
+		return getnameinfo_link(sa, salen, host, hostlen, serv,
+		    servlen, flags);
+	default:
+		return EAI_FAMILY;
+	}
+}
+
+static const struct afd {
+	int a_af;
+	size_t a_addrlen;
+	socklen_t a_socklen;
+	int a_off;
+} afdl [] = {
+#ifdef INET6
+	{PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6),
+		offsetof(struct sockaddr_in6, sin6_addr)},
+#endif
+	{PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in),
+		offsetof(struct sockaddr_in, sin_addr)},
+	{0, 0, 0},
+};
+
+struct sockinet {
+	u_char	si_len;
+	u_char	si_family;
+	u_short	si_port;
+};
+
+static int
+getnameinfo_inet(const struct sockaddr *sa, socklen_t salen,
+    char *host, size_t hostlen, char *serv, size_t servlen,
+    int flags)
+{
+	const struct afd *afd;
+	struct servent *sp;
+	struct hostent *hp;
+	u_short port;
+	int family, i;
+	const char *addr;
+	u_int32_t v4a;
+	int h_error;
+	char numserv[512];
+	char numaddr[512];
+
+	if (sa == NULL)
+		return EAI_FAIL;
+
+	family = sa->sa_family;
+	for (i = 0; afdl[i].a_af; i++)
+		if (afdl[i].a_af == family) {
+			afd = &afdl[i];
+			goto found;
+		}
+	return EAI_FAMILY;
+
+ found:
+	if (salen != afd->a_socklen)
+		return EAI_FAIL;
+
+	/* network byte order */
+	port = ((const struct sockinet *)sa)->si_port;
+	addr = (const char *)sa + afd->a_off;
+
+	if (serv == NULL || servlen == 0) {
+		/*
+		 * do nothing in this case.
+		 * in case you are wondering if "&&" is more correct than
+		 * "||" here: rfc2553bis-03 says that serv == NULL OR
+		 * servlen == 0 means that the caller does not want the result.
+		 */
+	} else {
+		if (flags & NI_NUMERICSERV)
+			sp = NULL;
+		else {
+			sp = getservbyport(port,
+				(flags & NI_DGRAM) ? "udp" : "tcp");
+		}
+		if (sp) {
+			if (strlen(sp->s_name) + 1 > servlen)
+				return EAI_MEMORY;
+			strlcpy(serv, sp->s_name, servlen);
+		} else {
+			snprintf(numserv, sizeof(numserv), "%u", ntohs(port));
+			if (strlen(numserv) + 1 > servlen)
+				return EAI_MEMORY;
+			strlcpy(serv, numserv, servlen);
+		}
+	}
+
+	switch (sa->sa_family) {
+	case AF_INET:
+		v4a = (u_int32_t)
+		    ntohl(((const struct sockaddr_in *)sa)->sin_addr.s_addr);
+		if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a))
+			flags |= NI_NUMERICHOST;
+		v4a >>= IN_CLASSA_NSHIFT;
+		if (v4a == 0)
+			flags |= NI_NUMERICHOST;
+		break;
+#ifdef INET6
+	case AF_INET6:
+	    {
+		const struct sockaddr_in6 *sin6;
+		sin6 = (const struct sockaddr_in6 *)sa;
+		switch (sin6->sin6_addr.s6_addr[0]) {
+		case 0x00:
+			if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
+				;
+			else if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
+				;
+			else
+				flags |= NI_NUMERICHOST;
+			break;
+		default:
+			if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
+				flags |= NI_NUMERICHOST;
+			}
+			else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
+				flags |= NI_NUMERICHOST;
+			break;
+		}
+	    }
+		break;
+#endif
+	}
+	if (host == NULL || hostlen == 0) {
+		/*
+		 * do nothing in this case.
+		 * in case you are wondering if "&&" is more correct than
+		 * "||" here: rfc2553bis-03 says that host == NULL or
+		 * hostlen == 0 means that the caller does not want the result.
+		 */
+	} else if (flags & NI_NUMERICHOST) {
+		size_t numaddrlen;
+
+		/* NUMERICHOST and NAMEREQD conflicts with each other */
+		if (flags & NI_NAMEREQD)
+			return EAI_NONAME;
+
+		switch(afd->a_af) {
+#ifdef INET6
+		case AF_INET6:
+		{
+			int error;
+
+			if ((error = ip6_parsenumeric(sa, addr, host,
+						      hostlen, flags)) != 0)
+				return(error);
+			break;
+		}
+#endif
+		default:
+			if (inet_ntop(afd->a_af, addr, numaddr, sizeof(numaddr))
+			    == NULL)
+				return EAI_SYSTEM;
+			numaddrlen = strlen(numaddr);
+			if (numaddrlen + 1 > hostlen) /* don't forget terminator */
+				return EAI_MEMORY;
+			strlcpy(host, numaddr, hostlen);
+			break;
+		}
+	} else {
+		hp = getipnodebyaddr(addr, afd->a_addrlen, afd->a_af, &h_error);
+
+		if (hp) {
+#if 0
+			/*
+			 * commented out, since "for local host" is not
+			 * implemented here - see RFC2553 p30
+			 */
+			if (flags & NI_NOFQDN) {
+				char *p;
+				p = strchr(hp->h_name, '.');
+				if (p)
+					*p = '\0';
+			}
+#endif
+			if (strlen(hp->h_name) + 1 > hostlen) {
+				freehostent(hp);
+				return EAI_MEMORY;
+			}
+			strlcpy(host, hp->h_name, hostlen);
+			freehostent(hp);
+		} else {
+			if (flags & NI_NAMEREQD)
+				return EAI_NONAME;
+			switch(afd->a_af) {
+#ifdef INET6
+			case AF_INET6:
+			{
+				int error;
+
+				if ((error = ip6_parsenumeric(sa, addr, host,
+							      hostlen,
+							      flags)) != 0)
+					return(error);
+				break;
+			}
+#endif
+			default:
+				if (inet_ntop(afd->a_af, addr, host,
+				    hostlen) == NULL)
+					return EAI_SYSTEM;
+				break;
+			}
+		}
+	}
+	return(0);
+}
+
+#ifdef INET6
+static int
+ip6_parsenumeric(const struct sockaddr *sa, const char *addr,
+    char *host, size_t hostlen, int flags)
+{
+	size_t numaddrlen;
+	char numaddr[512];
+
+	if (inet_ntop(AF_INET6, addr, numaddr, sizeof(numaddr)) == NULL)
+		return EAI_SYSTEM;
+
+	numaddrlen = strlen(numaddr);
+	if (numaddrlen + 1 > hostlen) /* don't forget terminator */
+		return EAI_OVERFLOW;
+	strlcpy(host, numaddr, hostlen);
+
+	if (((const struct sockaddr_in6 *)sa)->sin6_scope_id) {
+		char zonebuf[MAXHOSTNAMELEN];
+		int zonelen;
+
+		zonelen = ip6_sa2str(
+		    (const struct sockaddr_in6 *)(const void *)sa,
+		    zonebuf, sizeof(zonebuf), flags);
+		if (zonelen < 0)
+			return EAI_OVERFLOW;
+		if (zonelen + 1 + numaddrlen + 1 > hostlen)
+			return EAI_OVERFLOW;
+
+		/* construct <numeric-addr><delim><zoneid> */
+		memcpy(host + numaddrlen + 1, zonebuf,
+		    (size_t)zonelen);
+		host[numaddrlen] = SCOPE_DELIMITER;
+		host[numaddrlen + 1 + zonelen] = '\0';
+	}
+
+	return 0;
+}
+
+/* ARGSUSED */
+static int
+ip6_sa2str(const struct sockaddr_in6 *sa6, char *buf, size_t bufsiz, int flags)
+{
+	unsigned int ifindex;
+	const struct in6_addr *a6;
+	int n;
+
+	ifindex = (unsigned int)sa6->sin6_scope_id;
+	a6 = &sa6->sin6_addr;
+
+#ifdef NI_NUMERICSCOPE
+	if ((flags & NI_NUMERICSCOPE) != 0) {
+		n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
+		if (n < 0 || n >= bufsiz)
+			return -1;
+		else
+			return n;
+	}
+#endif
+
+	/* if_indextoname() does not take buffer size.  not a good api... */
+	if ((IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6) ||
+	     IN6_IS_ADDR_MC_NODELOCAL(a6)) && bufsiz >= IF_NAMESIZE) {
+		char *p = if_indextoname(ifindex, buf);
+		if (p) {
+			return(strlen(p));
+		}
+	}
+
+	/* last resort */
+	n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
+	if (n < 0 || (size_t)n >= bufsiz)
+		return -1;
+	else
+		return n;
+}
+#endif /* INET6 */
+
+/*
+ * getnameinfo_link():
+ * Format a link-layer address into a printable format, paying attention to
+ * the interface type.
+ */
+/* ARGSUSED */
+static int
+getnameinfo_link(const struct sockaddr *sa, socklen_t salen,
+    char *host, size_t hostlen, char *serv, size_t servlen, int flags)
+{
+	const struct sockaddr_dl *sdl =
+	    (const struct sockaddr_dl *)(const void *)sa;
+	int n;
+
+	if (serv != NULL && servlen > 0)
+		*serv = '\0';
+
+	if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 && sdl->sdl_slen == 0) {
+		n = snprintf(host, hostlen, "link#%d", sdl->sdl_index);
+		if (n > hostlen) {
+			*host = '\0';
+			return EAI_MEMORY;
+		}
+		return 0;
+	}
+
+	switch (sdl->sdl_type) {
+	/*
+	 * The following have zero-length addresses.
+	 * IFT_ATM	(net/if_atmsubr.c)
+	 * IFT_FAITH	(net/if_faith.c)
+	 * IFT_GIF	(net/if_gif.c)
+	 * IFT_LOOP	(net/if_loop.c)
+	 * IFT_PPP	(net/if_ppp.c, net/if_spppsubr.c)
+	 * IFT_SLIP	(net/if_sl.c, net/if_strip.c)
+	 * IFT_STF	(net/if_stf.c)
+	 * IFT_L2VLAN	(net/if_vlan.c)
+	 * IFT_BRIDGE (net/if_bridge.h>
+	 */
+	/*
+	 * The following use IPv4 addresses as link-layer addresses:
+	 * IFT_OTHER	(net/if_gre.c)
+	 * IFT_OTHER	(netinet/ip_ipip.c)
+	 */
+	/* default below is believed correct for all these. */
+	case IFT_ARCNET:
+	case IFT_ETHER:
+	case IFT_FDDI:
+	case IFT_HIPPI:
+	case IFT_ISO88025:
+	default:
+		return hexname((u_int8_t *)LLADDR(sdl), (size_t)sdl->sdl_alen,
+		    host, hostlen);
+	}
+}
+
+static int
+hexname(cp, len, host, hostlen)
+	const u_int8_t *cp;
+	char *host;
+	size_t len, hostlen;
+{
+	int i, n;
+	char *outp = host;
+
+	*outp = '\0';
+	for (i = 0; i < len; i++) {
+		n = snprintf(outp, hostlen, "%s%02x",
+		    i ? ":" : "", cp[i]);
+		if (n < 0 || n >= hostlen) {
+			*host = '\0';
+			return EAI_MEMORY;
+		}
+		outp += n;
+		hostlen -= n;
+	}
+	return 0;
+}
diff --git a/freebsd-userspace/lib/libc/net/getnetbydns.c b/freebsd-userspace/lib/libc/net/getnetbydns.c
new file mode 100644
index 0000000..7dd2383
--- /dev/null
+++ b/freebsd-userspace/lib/libc/net/getnetbydns.c
@@ -0,0 +1,467 @@
+#include "port_before.h"
+
+/*-
+ * Copyright (c) 1985, 1988, 1993
+ *	The Regents of the University of California.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON 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 ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ * -
+ * Portions Copyright (c) 1993 by Digital Equipment Corporation.
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies, and that
+ * the name of Digital Equipment Corporation not be used in advertising or
+ * publicity pertaining to distribution of the document or software without
+ * specific, written prior permission.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
+ * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
+ * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
+ * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
+ * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
+ * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
+ * SOFTWARE.
+ * -
+ * --Copyright--
+ */
+/* Portions Copyright (c) 1993 Carlos Leandro and Rui Salgueiro
+ *	Dep. Matematica Universidade de Coimbra, Portugal, Europe
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ */
+
+#if defined(LIBC_SCCS) && !defined(lint)
+static char sccsid[] = "@(#)gethostnamadr.c	8.1 (Berkeley) 6/4/93";
+#endif /* LIBC_SCCS and not lint */
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/param.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <arpa/nameser.h>
+
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <netdb.h>
+#include <resolv.h>
+#include <ctype.h>
+#include <string.h>
+#include <unistd.h>
+#include <syslog.h>
+#include <stdarg.h>
+#include <nsswitch.h>
+
+#include "netdb_private.h"
+#include "res_config.h"
+
+#define BYADDR 0
+#define BYNAME 1
+
+#define MAXPACKET	(64*1024)
+
+typedef union {
+	HEADER	hdr;
+	u_char	buf[MAXPACKET];
+} querybuf;
+
+typedef union {
+	long	al;
+	char	ac;
+} align;
+
+/*
+ * Reverse the order of first four dotted entries of in.
+ * Out must contain space for at least strlen(in) characters.
+ * The result does not include any leading 0s of in.
+ */
+static void
+ipreverse(char *in, char *out)
+{
+	char *pos[4];
+	int len[4];
+	char *p, *start;
+	int i = 0;
+	int leading = 1;
+
+	/* Fill-in element positions and lengths: pos[], len[]. */
+	start = p = in;
+	for (;;) {
+		if (*p == '.' || *p == '\0') {
+			/* Leading 0? */
+			if (leading && p - start == 1 && *start == '0')
+				len[i] = 0;
+			else {
+				len[i] = p - start;
+				leading = 0;
+			}
+			pos[i] = start;
+			start = p + 1;
+			i++;
+		}
+		if (i == 4)
+			break;
+		if (*p == 0) {
+			for (; i < 4; i++) {
+				pos[i] = p;
+				len[i] = 0;
+			}
+			break;
+		}
+		p++;
+	}
+
+	/* Copy the entries in reverse order */
+	p = out;
+	leading = 1;
+	for (i = 3; i >= 0; i--) {
+		memcpy(p, pos[i], len[i]);
+		if (len[i])
+			leading = 0;
+		p += len[i];
+		/* Need a . separator? */
+		if (!leading && i > 0 && len[i - 1])
+			*p++ = '.';
+	}
+	*p = '\0';
+}
+
+static int
+getnetanswer(querybuf *answer, int anslen, int net_i, struct netent *ne,
+    struct netent_data *ned, res_state statp)
+{
+
+	HEADER *hp;
+	u_char *cp;
+	int n;
+	u_char *eom;
+	int type, class, ancount, qdcount, haveanswer;
+	char aux[MAXHOSTNAMELEN];
+	char ans[MAXHOSTNAMELEN];
+	char *in, *bp, *ep, **ap;
+
+	/*
+	 * find first satisfactory answer
+	 *
+	 *      answer --> +------------+  ( MESSAGE )
+	 *		   |   Header   |
+	 *		   +------------+
+	 *		   |  Question  | the question for the name server
+	 *		   +------------+
+	 *		   |   Answer   | RRs answering the question
+	 *		   +------------+
+	 *		   | Authority  | RRs pointing toward an authority
+	 *		   | Additional | RRs holding additional information
+	 *		   +------------+
+	 */
+	eom = answer->buf + anslen;
+	hp = &answer->hdr;
+	ancount = ntohs(hp->ancount); /* #/records in the answer section */
+	qdcount = ntohs(hp->qdcount); /* #/entries in the question section */
+	bp = ned->netbuf;
+	ep = ned->netbuf + sizeof(ned->netbuf);
+	cp = answer->buf + HFIXEDSZ;
+	if (!qdcount) {
+		if (hp->aa)
+			RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
+		else
+			RES_SET_H_ERRNO(statp, TRY_AGAIN);
+		return (-1);
+	}
+	while (qdcount-- > 0)
+		cp += __dn_skipname(cp, eom) + QFIXEDSZ;
+	ap = ned->net_aliases;
+	*ap = NULL;
+	ne->n_aliases = ned->net_aliases;
+	haveanswer = 0;
+	while (--ancount >= 0 && cp < eom) {
+		n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
+		if ((n < 0) || !res_dnok(bp))
+			break;
+		cp += n;
+		ans[0] = '\0';
+		(void)strncpy(&ans[0], bp, sizeof(ans) - 1);
+		ans[sizeof(ans) - 1] = '\0';
+		GETSHORT(type, cp);
+		GETSHORT(class, cp);
+		cp += INT32SZ;		/* TTL */
+		GETSHORT(n, cp);
+		if (class == C_IN && type == T_PTR) {
+			n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
+			if ((n < 0) || !res_hnok(bp)) {
+				cp += n;
+				return (-1);
+			}
+			cp += n;
+			*ap++ = bp;
+			n = strlen(bp) + 1;
+			bp += n;
+			ne->n_addrtype = (class == C_IN) ? AF_INET : AF_UNSPEC;
+			haveanswer++;
+		}
+	}
+	if (haveanswer) {
+		*ap = NULL;
+		switch (net_i) {
+		case BYADDR:
+			ne->n_name = *ne->n_aliases;
+			ne->n_net = 0L;
+			break;
+		case BYNAME:
+			in = *ne->n_aliases;
+			n = strlen(ans) + 1;
+			if (ep - bp < n) {
+				RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
+				errno = ENOBUFS;
+				return (-1);
+			}
+			strlcpy(bp, ans, ep - bp);
+			ne->n_name = bp;
+			if (strlen(in) + 1 > sizeof(aux)) {
+				RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
+				errno = ENOBUFS;
+				return (-1);
+			}
+			ipreverse(in, aux);
+			ne->n_net = inet_network(aux);
+			break;
+		}
+		ne->n_aliases++;
+		return (0);
+	}
+	RES_SET_H_ERRNO(statp, TRY_AGAIN);
+	return (-1);
+}
+
+int
+_dns_getnetbyaddr(void *rval, void *cb_data, va_list ap)
+{
+	uint32_t net;
+	int net_type;
+	char *buffer;
+	size_t buflen;
+	int *errnop, *h_errnop;
+	struct netent *nptr, ne;
+	struct netent_data *ned;
+	unsigned int netbr[4];
+	int nn, anslen, error;
+	querybuf *buf;
+	char qbuf[MAXDNAME];
+	uint32_t net2;
+	res_state statp;
+
+	net = va_arg(ap, uint32_t);
+	net_type = va_arg(ap, int);
+	nptr = va_arg(ap, struct netent *);
+	buffer = va_arg(ap, char *);
+	buflen = va_arg(ap, size_t);
+	errnop = va_arg(ap, int *);
+	h_errnop = va_arg(ap, int *);
+
+	statp = __res_state();
+	if ((statp->options & RES_INIT) == 0 && res_ninit(statp) == -1) {
+		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
+		*h_errnop = statp->res_h_errno;
+		return (NS_UNAVAIL);
+	}
+
+	if ((ned = __netent_data_init()) == NULL) {
+		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
+		*h_errnop = statp->res_h_errno;
+		return (NS_UNAVAIL);
+	}
+
+	*((struct netent **)rval) = NULL;
+
+	if (net_type != AF_INET) {
+		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
+		*h_errnop = statp->res_h_errno;
+		return (NS_UNAVAIL);
+	}
+
+	for (nn = 4, net2 = net; net2; net2 >>= 8)
+		netbr[--nn] = net2 & 0xff;
+	switch (nn) {
+	case 3: 	/* Class A */
+		sprintf(qbuf, "0.0.0.%u.in-addr.arpa", netbr[3]);
+		break;
+	case 2: 	/* Class B */
+		sprintf(qbuf, "0.0.%u.%u.in-addr.arpa", netbr[3], netbr[2]);
+		break;
+	case 1: 	/* Class C */
+		sprintf(qbuf, "0.%u.%u.%u.in-addr.arpa", netbr[3], netbr[2],
+		    netbr[1]);
+		break;
+	case 0: 	/* Class D - E */
+		sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa", netbr[3], netbr[2],
+		    netbr[1], netbr[0]);
+		break;
+	}
+	if ((buf = malloc(sizeof(*buf))) == NULL) {
+		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
+		*h_errnop = statp->res_h_errno;
+		return (NS_NOTFOUND);
+	}
+	anslen = res_nquery(statp, qbuf, C_IN, T_PTR, (u_char *)buf,
+	    sizeof(*buf));
+	if (anslen < 0) {
+		free(buf);
+#ifdef DEBUG
+		if (statp->options & RES_DEBUG)
+			printf("res_nsearch failed\n");
+#endif
+		*h_errnop = statp->res_h_errno;
+		return (NS_UNAVAIL);
+	} else if (anslen > sizeof(*buf)) {
+		free(buf);
+#ifdef DEBUG
+		if (statp->options & RES_DEBUG)
+			printf("res_nsearch static buffer too small\n");
+#endif
+		*h_errnop = statp->res_h_errno;
+		return (NS_UNAVAIL);
+	}
+	error = getnetanswer(buf, anslen, BYADDR, &ne, ned, statp);
+	free(buf);
+	if (error == 0) {
+		/* Strip trailing zeros */
+		while ((net & 0xff) == 0 && net != 0)
+			net >>= 8;
+		ne.n_net = net;
+		if (__copy_netent(&ne, nptr, buffer, buflen) != 0) {
+			*errnop = errno;
+			RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
+			*h_errnop = statp->res_h_errno;
+			return (NS_RETURN);
+		}
+		*((struct netent **)rval) = nptr;
+		return (NS_SUCCESS);
+	}
+	*h_errnop = statp->res_h_errno;
+	return (NS_NOTFOUND);
+}
+
+int
+_dns_getnetbyname(void *rval, void *cb_data, va_list ap)
+{
+	const char *net;
+	char *buffer;
+	size_t buflen;
+	int *errnop, *h_errnop;
+	struct netent *nptr, ne;
+	struct netent_data *ned;
+	int anslen, error;
+	querybuf *buf;
+	char qbuf[MAXDNAME];
+	res_state statp;
+
+	net = va_arg(ap, const char *);
+	nptr = va_arg(ap, struct netent *);
+	buffer = va_arg(ap, char *);
+	buflen = va_arg(ap, size_t);
+	errnop = va_arg(ap, int *);
+	h_errnop = va_arg(ap, int *);
+
+	statp = __res_state();
+	if ((statp->options & RES_INIT) == 0 && res_ninit(statp) == -1) {
+		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
+		*h_errnop = statp->res_h_errno;
+		return (NS_UNAVAIL);
+	}
+	if ((ned = __netent_data_init()) == NULL) {
+		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
+		*h_errnop = statp->res_h_errno;
+		return (NS_UNAVAIL);
+	}
+	if ((buf = malloc(sizeof(*buf))) == NULL) {
+		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
+		*h_errnop = statp->res_h_errno;
+		return (NS_NOTFOUND);
+	}
+
+	*((struct netent **)rval) = NULL;
+
+	strncpy(qbuf, net, sizeof(qbuf) - 1);
+	qbuf[sizeof(qbuf) - 1] = '\0';
+	anslen = res_nsearch(statp, qbuf, C_IN, T_PTR, (u_char *)buf,
+	    sizeof(*buf));
+	if (anslen < 0) {
+		free(buf);
+#ifdef DEBUG
+		if (statp->options & RES_DEBUG)
+			printf("res_nsearch failed\n");
+#endif
+		return (NS_UNAVAIL);
+	} else if (anslen > sizeof(*buf)) {
+		free(buf);
+#ifdef DEBUG
+		if (statp->options & RES_DEBUG)
+			printf("res_search static buffer too small\n");
+#endif
+		return (NS_UNAVAIL);
+	}
+	error = getnetanswer(buf, anslen, BYNAME, &ne, ned, statp);
+	free(buf);
+	if (error != 0) {
+		*h_errnop = statp->res_h_errno;
+		return (NS_NOTFOUND);
+	}
+	if (__copy_netent(&ne, nptr, buffer, buflen) != 0) {
+		*errnop = errno;
+		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
+		*h_errnop = statp->res_h_errno;
+		return (NS_RETURN);
+	}
+	*((struct netent **)rval) = nptr;
+	return (NS_SUCCESS);
+}
+
+void
+_setnetdnsent(int stayopen)
+{
+	res_state statp;
+
+	statp = __res_state();
+	if ((statp->options & RES_INIT) == 0 && res_ninit(statp) == -1)
+		return;
+	if (stayopen)
+		statp->options |= RES_STAYOPEN | RES_USEVC;
+}
+
+void
+_endnetdnsent()
+{
+	res_state statp;
+
+	statp = __res_state();
+	statp->options &= ~(RES_STAYOPEN | RES_USEVC);
+	res_nclose(statp);
+}
diff --git a/freebsd-userspace/lib/libc/net/getnetbyht.c b/freebsd-userspace/lib/libc/net/getnetbyht.c
new file mode 100644
index 0000000..1076cf2
--- /dev/null
+++ b/freebsd-userspace/lib/libc/net/getnetbyht.c
@@ -0,0 +1,290 @@
+#include "port_before.h"
+
+/*
+ * Copyright (c) 1983, 1993
+ *	The Regents of the University of California.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON 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 ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+/* Portions Copyright (c) 1993 Carlos Leandro and Rui Salgueiro
+ *	Dep. Matematica Universidade de Coimbra, Portugal, Europe
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * from getnetent.c	1.1 (Coimbra) 93/06/02
+ */
+
+#if defined(LIBC_SCCS) && !defined(lint)
+static char sccsid[] = "@(#)getnetent.c	8.1 (Berkeley) 6/4/93";
+static char orig_rcsid[] = "From: Id: getnetent.c,v 8.4 1997/06/01 20:34:37 vixie Exp";
+#endif /* LIBC_SCCS and not lint */
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <arpa/nameser.h>
+#include <errno.h>
+#include <netdb.h>
+#include <resolv.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdarg.h>
+#include <nsswitch.h>
+#include "netdb_private.h"
+
+void
+_setnethtent(int f, struct netent_data *ned)
+{
+
+	if (ned->netf == NULL)
+		ned->netf = fopen(_PATH_NETWORKS, "r");
+	else
+		rewind(ned->netf);
+	ned->stayopen |= f;
+}
+
+void
+_endnethtent(struct netent_data *ned)
+{
+
+	if (ned->netf) {
+		fclose(ned->netf);
+		ned->netf = NULL;
+	}
+	ned->stayopen = 0;
+}
+
+static int
+getnetent_p(struct netent *ne, struct netent_data *ned)
+{
+	char *p, *bp, *ep;
+	char *cp, **q;
+	int len;
+	char line[BUFSIZ + 1];
+
+	if (ned->netf == NULL &&
+	    (ned->netf = fopen(_PATH_NETWORKS, "r")) == NULL)
+		return (-1);
+again:
+	p = fgets(line, sizeof line, ned->netf);
+	if (p == NULL)
+		return (-1);
+	if (*p == '#')
+		goto again;
+	cp = strpbrk(p, "#\n");
+	if (cp != NULL)
+		*cp = '\0';
+	bp = ned->netbuf;
+	ep = ned->netbuf + sizeof ned->netbuf;
+	ne->n_name = bp;
+	cp = strpbrk(p, " \t");
+	if (cp == NULL)
+		goto again;
+	*cp++ = '\0';
+	len = strlen(p) + 1;
+	if (ep - bp < len) {
+		RES_SET_H_ERRNO(__res_state(), NO_RECOVERY);
+		return (-1);
+	}
+	strlcpy(bp, p, ep - bp);
+	bp += len;
+	while (*cp == ' ' || *cp == '\t')
+		cp++;
+	p = strpbrk(cp, " \t");
+	if (p != NULL)
+		*p++ = '\0';
+	ne->n_net = inet_network(cp);
+	ne->n_addrtype = AF_INET;
+	q = ne->n_aliases = ned->net_aliases;
+	if (p != NULL) {
+		cp = p;
+		while (cp && *cp) {
+			if (*cp == ' ' || *cp == '\t') {
+				cp++;
+				continue;
+			}
+			if (q >= &ned->net_aliases[_MAXALIASES - 1])
+				break;
+			p = strpbrk(cp, " \t");
+			if (p != NULL)
+				*p++ = '\0';
+			len = strlen(cp) + 1;
+			if (ep - bp < len)
+				break;
+			strlcpy(bp, cp, ep - bp);
+			*q++ = bp;
+			bp += len;
+			cp = p;
+		}
+	}
+	*q = NULL;
+	return (0);
+}
+
+int
+getnetent_r(struct netent *nptr, char *buffer, size_t buflen,
+    struct netent **result, int *h_errnop)
+{
+	struct netent_data *ned;
+	struct netent ne;
+	res_state statp;
+
+	statp = __res_state();
+	if ((ned = __netent_data_init()) == NULL) {
+		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
+		*h_errnop = statp->res_h_errno;
+		return (-1);
+	}
+	if (getnetent_p(&ne, ned) != 0)
+		return (-1);
+	if (__copy_netent(&ne, nptr, buffer, buflen) != 0) {
+		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
+		*h_errnop = statp->res_h_errno;
+		return ((errno != 0) ? errno : -1);
+	}
+	*result = nptr;
+	return (0);
+}
+
+struct netent *
+getnetent(void)
+{
+	struct netdata *nd;
+	struct netent *rval;
+	int ret_h_errno;
+
+	if ((nd = __netdata_init()) == NULL)
+		return (NULL);
+	if (getnetent_r(&nd->net, nd->data, sizeof(nd->data), &rval,
+	    &ret_h_errno) != 0)
+		return (NULL);
+	return (rval);
+}
+
+int
+_ht_getnetbyname(void *rval, void *cb_data, va_list ap)
+{
+	const char *name;
+	char *buffer;
+	size_t buflen;
+	int *errnop, *h_errnop;
+	struct netent *nptr, ne;
+	struct netent_data *ned;
+	char **cp;
+	res_state statp;
+	int error;
+
+	name = va_arg(ap, const char *);
+	nptr = va_arg(ap, struct netent *);
+	buffer = va_arg(ap, char *);
+	buflen = va_arg(ap, size_t);
+	errnop = va_arg(ap, int *);
+	h_errnop = va_arg(ap, int *);
+
+	statp = __res_state();
+	if ((ned = __netent_data_init()) == NULL) {
+		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
+		*h_errnop = statp->res_h_errno;
+		return (NS_UNAVAIL);
+	}
+
+	_setnethtent(ned->stayopen, ned);
+	while ((error = getnetent_p(&ne, ned)) == 0) {
+		if (strcasecmp(ne.n_name, name) == 0)
+			break;
+		for (cp = ne.n_aliases; *cp != 0; cp++)
+			if (strcasecmp(*cp, name) == 0)
+				goto found;
+	}
+found:
+	if (!ned->stayopen)
+		_endnethtent(ned);
+	if (error != 0) {
+		*h_errnop = statp->res_h_errno;
+		return (NS_NOTFOUND);
+	}
+	if (__copy_netent(&ne, nptr, buffer, buflen) != 0) {
+		*errnop = errno;
+		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
+		*h_errnop = statp->res_h_errno;
+		return (NS_RETURN);
+	}
+	*((struct netent **)rval) = nptr;
+	return (NS_SUCCESS);
+}
+
+int
+_ht_getnetbyaddr(void *rval, void *cb_data, va_list ap)
+{
+	uint32_t net;
+	int type;
+	char *buffer;
+	size_t buflen;
+	int *errnop, *h_errnop;
+	struct netent *nptr, ne;
+	struct netent_data *ned;
+	res_state statp;
+	int error;
+
+	net = va_arg(ap, uint32_t);
+	type = va_arg(ap, int);
+	nptr = va_arg(ap, struct netent *);
+	buffer = va_arg(ap, char *);
+	buflen = va_arg(ap, size_t);
+	errnop = va_arg(ap, int *);
+	h_errnop = va_arg(ap, int *);
+
+	statp = __res_state();
+	if ((ned = __netent_data_init()) == NULL) {
+		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
+		*h_errnop = statp->res_h_errno;
+		return (NS_UNAVAIL);
+	}
+
+	_setnethtent(ned->stayopen, ned);
+	while ((error = getnetent_p(&ne, ned)) == 0)
+		if (ne.n_addrtype == type && ne.n_net == net)
+			break;
+	if (!ned->stayopen)
+		_endnethtent(ned);
+	if (error != 0) {
+		*h_errnop = statp->res_h_errno;
+		return (NS_NOTFOUND);
+	}
+	if (__copy_netent(&ne, nptr, buffer, buflen) != 0) {
+		*errnop = errno;
+		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
+		*h_errnop = statp->res_h_errno;
+		return (NS_RETURN);
+	}
+	*((struct netent **)rval) = nptr;
+	return (NS_SUCCESS);
+}
diff --git a/freebsd-userspace/lib/libc/net/getnetbynis.c b/freebsd-userspace/lib/libc/net/getnetbynis.c
new file mode 100644
index 0000000..805bc9d
--- /dev/null
+++ b/freebsd-userspace/lib/libc/net/getnetbynis.c
@@ -0,0 +1,261 @@
+#include "port_before.h"
+
+/*-
+ * Copyright (c) 1994, Garrett Wollman
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON 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 ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/param.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <netdb.h>
+#include <resolv.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <ctype.h>
+#include <errno.h>
+#include <string.h>
+#include <stdarg.h>
+#include <nsswitch.h>
+#include <arpa/nameser.h>
+#ifdef YP
+#include <rpc/rpc.h>
+#include <rpcsvc/yp_prot.h>
+#include <rpcsvc/ypclnt.h>
+#endif
+#include "netdb_private.h"
+
+#ifdef YP
+static int
+_getnetbynis(const char *name, char *map, int af, struct netent *ne,
+    struct netent_data *ned)
+{
+	char *p, *bp, *ep;
+	char *cp, **q;
+	char *result;
+	int resultlen, len;
+	char ypbuf[YPMAXRECORD + 2];
+
+	switch(af) {
+	case AF_INET:
+		break;
+	default:
+	case AF_INET6:
+		errno = EAFNOSUPPORT;
+		return (-1);
+	}
+
+	if (ned->yp_domain == (char *)NULL)
+		if (yp_get_default_domain (&ned->yp_domain))
+			return (-1);
+
+	if (yp_match(ned->yp_domain, map, name, strlen(name), &result,
+	    &resultlen))
+		return (-1);
+
+	bcopy((char *)result, (char *)&ypbuf, resultlen);
+	ypbuf[resultlen] = '\0';
+	free(result);
+	result = (char *)&ypbuf;
+
+	if ((cp = index(result, '\n')))
+		*cp = '\0';
+
+	cp = strpbrk(result, " \t");
+	*cp++ = '\0';
+	bp = ned->netbuf;
+	ep = ned->netbuf + sizeof ned->netbuf;
+	len = strlen(result) + 1;
+	if (ep - bp < len) {
+		RES_SET_H_ERRNO(__res_state(), NO_RECOVERY);
+		return (-1);
+	}
+	strlcpy(bp, result, ep - bp);
+	ne->n_name = bp;
+	bp += len;
+
+	while (*cp == ' ' || *cp == '\t')
+		cp++;
+
+	ne->n_net = inet_network(cp);
+	ne->n_addrtype = AF_INET;
+
+	q = ne->n_aliases = ned->net_aliases;
+	cp = strpbrk(cp, " \t");
+	if (cp != NULL)
+		*cp++ = '\0';
+	while (cp && *cp) {
+		if (*cp == ' ' || *cp == '\t') {
+			cp++;
+			continue;
+		}
+		if (q > &ned->net_aliases[_MAXALIASES - 1])
+			break;
+		p = strpbrk(cp, " \t");
+		if (p != NULL)
+			*p++ = '\0';
+		len = strlen(cp) + 1;
+		if (ep - bp < len)
+			break;
+		strlcpy(bp, cp, ep - bp);
+		*q++ = bp;
+		bp += len;
+		cp = p;
+	}
+	*q = NULL;
+	return (0);
+}
+#endif /* YP */
+
+int
+_nis_getnetbyname(void *rval, void *cb_data, va_list ap)
+{
+#ifdef YP
+	const char *name;
+	char *buffer;
+	size_t buflen;
+	int *errnop, *h_errnop;
+	struct netent *nptr, ne;
+	struct netent_data *ned;
+	res_state statp;
+
+	name = va_arg(ap, const char *);
+	nptr = va_arg(ap, struct netent *);
+	buffer = va_arg(ap, char *);
+	buflen = va_arg(ap, size_t);
+	errnop = va_arg(ap, int *);
+	h_errnop = va_arg(ap, int *);
+
+	statp = __res_state();
+	if ((ned = __netent_data_init()) == NULL) {
+		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
+		*h_errnop = statp->res_h_errno;
+		return (NS_UNAVAIL);
+	}
+
+	if (_getnetbynis(name, "networks.byname", AF_INET, &ne, ned) != 0) {
+		*h_errnop = statp->res_h_errno;
+		return (NS_NOTFOUND);
+	}
+	if (__copy_netent(&ne, nptr, buffer, buflen) != 0) {
+		*errnop = errno;
+		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
+		*h_errnop = statp->res_h_errno;
+		return (NS_RETURN);
+	}
+	*((struct netent **)rval) = nptr;
+	return (NS_SUCCESS);
+#else
+	return (NS_UNAVAIL);
+#endif
+
+}
+
+int
+_nis_getnetbyaddr(void *rval, void *cb_data, va_list ap)
+{
+#ifdef YP
+	uint32_t addr;
+	int af;
+	char *buffer;
+	size_t buflen;
+	int *errnop, *h_errnop;
+	struct netent *nptr, ne;
+	struct netent_data *ned;
+	char *str, *cp;
+	uint32_t net2;
+	int nn;
+	unsigned int netbr[4];
+	char buf[MAXDNAME];
+	res_state statp;
+
+	addr = va_arg(ap, uint32_t);
+	af = va_arg(ap, int);
+	nptr = va_arg(ap, struct netent *);
+	buffer = va_arg(ap, char *);
+	buflen = va_arg(ap, size_t);
+	errnop = va_arg(ap, int *);
+	h_errnop = va_arg(ap, int *);
+
+	statp = __res_state();
+	if ((ned = __netent_data_init()) == NULL) {
+		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
+		*h_errnop = statp->res_h_errno;
+		return (NS_UNAVAIL);
+	}
+
+	if (af != AF_INET) {
+		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
+		*h_errnop = statp->res_h_errno;
+		errno = EAFNOSUPPORT;
+		return (NS_UNAVAIL);
+	}
+
+        for (nn = 4, net2 = addr; net2; net2 >>= 8) {
+                netbr[--nn] = net2 & 0xff;
+	}
+
+	switch (nn) {
+	case 3:		/* Class A */
+		sprintf(buf, "%u", netbr[3]);
+		break;
+        case 2:		/* Class B */
+		sprintf(buf, "%u.%u", netbr[2], netbr[3]);
+		break;
+        case 1:		/* Class C */
+		sprintf(buf, "%u.%u.%u", netbr[1], netbr[2], netbr[3]);
+                break;
+        case 0:		/* Class D - E */
+		sprintf(buf, "%u.%u.%u.%u", netbr[0], netbr[1],
+			netbr[2], netbr[3]);
+		break;
+	}
+
+	str = (char *)&buf;
+	cp = str + (strlen(str) - 2);
+
+	while(!strcmp(cp, ".0")) {
+		*cp = '\0';
+		cp = str + (strlen(str) - 2);
+	}
+
+	if (_getnetbynis(str, "networks.byaddr", af, &ne, ned) != 0) {
+		*h_errnop = statp->res_h_errno;
+		return (NS_NOTFOUND);
+	}
+	if (__copy_netent(&ne, nptr, buffer, buflen) != 0) {
+		*errnop = errno;
+		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
+		*h_errnop = statp->res_h_errno;
+		return (NS_RETURN);
+	}
+	*((struct netent **)rval) = nptr;
+	return (NS_SUCCESS);
+#else
+	return (NS_UNAVAIL);
+#endif /* YP */
+}
diff --git a/freebsd-userspace/lib/libc/net/getnetnamadr.c b/freebsd-userspace/lib/libc/net/getnetnamadr.c
new file mode 100644
index 0000000..7ae162c
--- /dev/null
+++ b/freebsd-userspace/lib/libc/net/getnetnamadr.c
@@ -0,0 +1,460 @@
+#include "port_before.h"
+
+/*-
+ * Copyright (c) 1994, Garrett Wollman
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON 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 ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include "namespace.h"
+#include "reentrant.h"
+#include <sys/param.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <netdb.h>
+#include <stdio.h>
+#include <ctype.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdarg.h>
+#include <nsswitch.h>
+#include "un-namespace.h"
+#include "netdb_private.h"
+#ifdef NS_CACHING
+#include "nscache.h"
+#endif
+
+extern int _ht_getnetbyname(void *, void *, va_list);
+extern int _dns_getnetbyname(void *, void *, va_list);
+extern int _nis_getnetbyname(void *, void *, va_list);
+extern int _ht_getnetbyaddr(void *, void *, va_list);
+extern int _dns_getnetbyaddr(void *, void *, va_list);
+extern int _nis_getnetbyaddr(void *, void *, va_list);
+
+/* Network lookup order if nsswitch.conf is broken or nonexistant */
+static const ns_src default_src[] = {
+	{ NSSRC_FILES, NS_SUCCESS },
+	{ NSSRC_DNS, NS_SUCCESS },
+	{ 0 }
+};
+
+NETDB_THREAD_ALLOC(netent_data)
+NETDB_THREAD_ALLOC(netdata)
+
+#ifdef NS_CACHING
+static int
+net_id_func(char *buffer, size_t *buffer_size, va_list ap, void *cache_mdata)
+{
+	char *name;
+	uint32_t net;
+	int type;
+
+	size_t desired_size, size;
+	enum nss_lookup_type lookup_type;
+	int res = NS_UNAVAIL;
+
+	lookup_type = (enum nss_lookup_type)cache_mdata;
+	switch (lookup_type) {
+	case nss_lt_name:
+		name = va_arg(ap, char *);
+
+		size = strlen(name);
+		desired_size = sizeof(enum nss_lookup_type) + size + 1;
+		if (desired_size > *buffer_size) {
+			res = NS_RETURN;
+			goto fin;
+		}
+
+		memcpy(buffer, &lookup_type, sizeof(enum nss_lookup_type));
+		memcpy(buffer + sizeof(enum nss_lookup_type), name, size + 1);
+
+		res = NS_SUCCESS;
+		break;
+	case nss_lt_id:
+		net = va_arg(ap, uint32_t);
+		type = va_arg(ap, int);
+
+		desired_size = sizeof(enum nss_lookup_type) +
+		    sizeof(uint32_t) + sizeof(int);
+		if (desired_size > *buffer_size) {
+			res = NS_RETURN;
+			goto fin;
+		}
+
+		memcpy(buffer, &lookup_type, sizeof(enum nss_lookup_type));
+		memcpy(buffer + sizeof(enum nss_lookup_type), &net,
+		    sizeof(uint32_t));
+		memcpy(buffer + sizeof(enum nss_lookup_type) + sizeof(uint32_t),
+		    &type, sizeof(int));
+
+		res = NS_SUCCESS;
+		break;
+	default:
+		/* should be unreachable */
+		return (NS_UNAVAIL);
+	}
+
+fin:
+	*buffer_size = desired_size;
+	return (res);
+}
+
+
+static int
+net_marshal_func(char *buffer, size_t *buffer_size, void *retval, va_list ap,
+    void *cache_mdata)
+{
+	char *name;
+	uint32_t net;
+	int type;
+	struct netent *ne;
+	char *orig_buf;
+	size_t orig_buf_size;
+
+	struct netent new_ne;
+	size_t desired_size, size, aliases_size;
+	char *p;
+	char **alias;
+
+	switch ((enum nss_lookup_type)cache_mdata) {
+	case nss_lt_name:
+		name = va_arg(ap, char *);
+		break;
+	case nss_lt_id:
+		net = va_arg(ap, uint32_t);
+		type = va_arg(ap, int);
+	break;
+	case nss_lt_all:
+		break;
+	default:
+		/* should be unreachable */
+		return (NS_UNAVAIL);
+	}
+
+	ne = va_arg(ap, struct netent *);
+	orig_buf = va_arg(ap, char *);
+	orig_buf_size = va_arg(ap, size_t);
+
+	desired_size = _ALIGNBYTES + sizeof(struct netent) + sizeof(char *);
+	if (ne->n_name != NULL)
+		desired_size += strlen(ne->n_name) + 1;
+
+	if (ne->n_aliases != NULL) {
+		aliases_size = 0;
+		for (alias = ne->n_aliases; *alias; ++alias) {
+			desired_size += strlen(*alias) + 1;
+			++aliases_size;
+		}
+
+		desired_size += _ALIGNBYTES +
+		    (aliases_size + 1) * sizeof(char *);
+	}
+
+	if (*buffer_size < desired_size) {
+		/* this assignment is here for future use */
+		*buffer_size = desired_size;
+		return (NS_RETURN);
+	}
+
+	memcpy(&new_ne, ne, sizeof(struct netent));
+
+	*buffer_size = desired_size;
+	memset(buffer, 0, desired_size);
+	p = buffer + sizeof(struct netent) + sizeof(char *);
+	memcpy(buffer + sizeof(struct netent), &p, sizeof(char *));
+	p = (char *)_ALIGN(p);
+
+	if (new_ne.n_name != NULL) {
+		size = strlen(new_ne.n_name);
+		memcpy(p, new_ne.n_name, size);
+		new_ne.n_name = p;
+		p += size + 1;
+	}
+
+	if (new_ne.n_aliases != NULL) {
+		p = (char *)_ALIGN(p);
+		memcpy(p, new_ne.n_aliases, sizeof(char *) * aliases_size);
+		new_ne.n_aliases = (char **)p;
+		p += sizeof(char *) * (aliases_size + 1);
+
+		for (alias = new_ne.n_aliases; *alias; ++alias) {
+			size = strlen(*alias);
+			memcpy(p, *alias, size);
+			*alias = p;
+			p += size + 1;
+		}
+	}
+
+	memcpy(buffer, &new_ne, sizeof(struct netent));
+	return (NS_SUCCESS);
+}
+
+static int
+net_unmarshal_func(char *buffer, size_t buffer_size, void *retval, va_list ap,
+    void *cache_mdata)
+{
+	char *name;
+	uint32_t net;
+	int type;
+	struct netent *ne;
+	char *orig_buf;
+	size_t orig_buf_size;
+	int *ret_errno;
+
+	char *p;
+	char **alias;
+
+	switch ((enum nss_lookup_type)cache_mdata) {
+	case nss_lt_name:
+		name = va_arg(ap, char *);
+		break;
+	case nss_lt_id:
+		net = va_arg(ap, uint32_t);
+		type = va_arg(ap, int);
+		break;
+	case nss_lt_all:
+		break;
+	default:
+		/* should be unreachable */
+		return (NS_UNAVAIL);
+	}
+
+	ne = va_arg(ap, struct netent *);
+	orig_buf = va_arg(ap, char *);
+	orig_buf_size = va_arg(ap, size_t);
+	ret_errno = va_arg(ap, int *);
+
+	if (orig_buf_size <
+	    buffer_size - sizeof(struct netent) - sizeof(char *)) {
+		*ret_errno = ERANGE;
+		return (NS_RETURN);
+	}
+
+	memcpy(ne, buffer, sizeof(struct netent));
+	memcpy(&p, buffer + sizeof(struct netent), sizeof(char *));
+
+	orig_buf = (char *)_ALIGN(orig_buf);
+	memcpy(orig_buf, buffer + sizeof(struct netent) + sizeof(char *) +
+	    _ALIGN(p) - (size_t)p,
+	    buffer_size - sizeof(struct netent) - sizeof(char *) -
+	    _ALIGN(p) + (size_t)p);
+	p = (char *)_ALIGN(p);
+
+	NS_APPLY_OFFSET(ne->n_name, orig_buf, p, char *);
+	if (ne->n_aliases != NULL) {
+		NS_APPLY_OFFSET(ne->n_aliases, orig_buf, p, char **);
+
+		for (alias = ne->n_aliases; *alias; ++alias)
+			NS_APPLY_OFFSET(*alias, orig_buf, p, char *);
+	}
+
+	if (retval != NULL)
+		*((struct netent **)retval) = ne;
+
+	return (NS_SUCCESS);
+}
+#endif /* NS_CACHING */
+
+static void
+netent_data_free(void *ptr)
+{
+	struct netent_data *ned = ptr;
+
+	if (ned == NULL)
+		return;
+	ned->stayopen = 0;
+	_endnethtent(ned);
+	free(ned);
+}
+
+static void
+netdata_free(void *ptr)
+{
+	free(ptr);
+}
+
+int
+__copy_netent(struct netent *ne, struct netent *nptr, char *buf, size_t buflen)
+{
+	char *cp;
+	int i, n;
+	int numptr, len;
+
+	/* Find out the amount of space required to store the answer. */
+	numptr = 1; /* NULL ptr */
+	len = (char *)ALIGN(buf) - buf;
+	for (i = 0; ne->n_aliases[i]; i++, numptr++) {
+		len += strlen(ne->n_aliases[i]) + 1;
+	}
+	len += strlen(ne->n_name) + 1;
+	len += numptr * sizeof(char*);
+
+	if (len > (int)buflen) {
+		errno = ERANGE;
+		return (-1);
+	}
+
+	/* copy net value and type */
+	nptr->n_addrtype = ne->n_addrtype;
+	nptr->n_net = ne->n_net;
+
+	cp = (char *)ALIGN(buf) + numptr * sizeof(char *);
+
+	/* copy official name */
+	n = strlen(ne->n_name) + 1;
+	strcpy(cp, ne->n_name);
+	nptr->n_name = cp;
+	cp += n;
+
+	/* copy aliases */
+	nptr->n_aliases = (char **)ALIGN(buf);
+	for (i = 0 ; ne->n_aliases[i]; i++) {
+		n = strlen(ne->n_aliases[i]) + 1;
+		strcpy(cp, ne->n_aliases[i]);
+		nptr->n_aliases[i] = cp;
+		cp += n;
+	}
+	nptr->n_aliases[i] = NULL;
+
+	return (0);
+}
+
+int
+getnetbyname_r(const char *name, struct netent *ne, char *buffer,
+    size_t buflen, struct netent **result, int *h_errorp)
+{
+#ifdef NS_CACHING
+	static const nss_cache_info cache_info =
+    		NS_COMMON_CACHE_INFO_INITIALIZER(
+		networks, (void *)nss_lt_name,
+		net_id_func, net_marshal_func, net_unmarshal_func);
+#endif
+	static const ns_dtab dtab[] = {
+		NS_FILES_CB(_ht_getnetbyname, NULL)
+		{ NSSRC_DNS, _dns_getnetbyname, NULL },
+		NS_NIS_CB(_nis_getnetbyname, NULL) /* force -DHESIOD */
+#ifdef NS_CACHING
+		NS_CACHE_CB(&cache_info)
+#endif
+		{ 0 }
+	};
+	int rval, ret_errno = 0;
+
+	rval = _nsdispatch((void *)result, dtab, NSDB_NETWORKS,
+	    "getnetbyname_r", default_src, name, ne, buffer, buflen,
+	    &ret_errno, h_errorp);
+
+	if (rval != NS_SUCCESS) {
+		errno = ret_errno;
+		return ((ret_errno != 0) ? ret_errno : -1);
+	}
+	return (0);
+}
+
+int
+getnetbyaddr_r(uint32_t addr, int af, struct netent *ne, char *buffer,
+    size_t buflen, struct netent **result, int *h_errorp)
+{
+#ifdef NS_CACHING
+	static const nss_cache_info cache_info =
+    		NS_COMMON_CACHE_INFO_INITIALIZER(
+		networks, (void *)nss_lt_id,
+		net_id_func, net_marshal_func, net_unmarshal_func);
+#endif
+	static const ns_dtab dtab[] = {
+		NS_FILES_CB(_ht_getnetbyaddr, NULL)
+		{ NSSRC_DNS, _dns_getnetbyaddr, NULL },
+		NS_NIS_CB(_nis_getnetbyaddr, NULL) /* force -DHESIOD */
+#ifdef NS_CACHING
+		NS_CACHE_CB(&cache_info)
+#endif
+		{ 0 }
+	};
+	int rval, ret_errno = 0;
+
+	rval = _nsdispatch((void *)result, dtab, NSDB_NETWORKS,
+	    "getnetbyaddr_r", default_src, addr, af, ne, buffer, buflen,
+	    &ret_errno, h_errorp);
+
+	if (rval != NS_SUCCESS) {
+		errno = ret_errno;
+		return ((ret_errno != 0) ? ret_errno : -1);
+	}
+	return (0);
+}
+
+struct netent *
+getnetbyname(const char *name)
+{
+	struct netdata *nd;
+	struct netent *rval;
+	int ret_h_errno;
+
+	if ((nd = __netdata_init()) == NULL)
+		return (NULL);
+	if (getnetbyname_r(name, &nd->net, nd->data, sizeof(nd->data), &rval,
+	    &ret_h_errno) != 0)
+		return (NULL);
+	return (rval);
+}
+
+struct netent *
+getnetbyaddr(uint32_t addr, int af)
+{
+	struct netdata *nd;
+	struct netent *rval;
+	int ret_h_errno;
+
+	if ((nd = __netdata_init()) == NULL)
+		return (NULL);
+	if (getnetbyaddr_r(addr, af, &nd->net, nd->data, sizeof(nd->data),
+	    &rval, &ret_h_errno) != 0)
+		return (NULL);
+	return (rval);
+}
+
+void
+setnetent(int stayopen)
+{
+	struct netent_data *ned;
+
+	if ((ned = __netent_data_init()) == NULL)
+		return;
+	_setnethtent(stayopen, ned);
+	_setnetdnsent(stayopen);
+}
+
+void
+endnetent(void)
+{
+	struct netent_data *ned;
+
+	if ((ned = __netent_data_init()) == NULL)
+		return;
+	_endnethtent(ned);
+	_endnetdnsent();
+}
diff --git a/freebsd-userspace/lib/libc/net/getproto.c b/freebsd-userspace/lib/libc/net/getproto.c
new file mode 100644
index 0000000..a679db6
--- /dev/null
+++ b/freebsd-userspace/lib/libc/net/getproto.c
@@ -0,0 +1,145 @@
+#include "port_before.h"
+
+/*
+ * Copyright (c) 1983, 1993
+ *	The Regents of the University of California.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON 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 ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#if defined(LIBC_SCCS) && !defined(lint)
+static char sccsid[] = "@(#)getproto.c	8.1 (Berkeley) 6/4/93";
+#endif /* LIBC_SCCS and not lint */
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <errno.h>
+#include <netdb.h>
+#include <nsswitch.h>
+#include "netdb_private.h"
+#ifdef NS_CACHING
+#include "nscache.h"
+#endif
+#include "nss_tls.h"
+
+static const ns_src defaultsrc[] = {
+	{ NSSRC_FILES, NS_SUCCESS },
+	{ NULL, 0 }
+};
+
+#ifdef NS_CACHING
+extern int __proto_id_func(char *, size_t *, va_list, void *);
+extern int __proto_marshal_func(char *, size_t *, void *, va_list, void *);
+extern int __proto_unmarshal_func(char *, size_t, void *, va_list, void *);
+#endif
+
+static int
+files_getprotobynumber(void *retval, void *mdata, va_list ap)
+{
+	struct protoent pe;
+	struct protoent_data *ped;
+	int error;
+
+	int number;
+	struct protoent	*pptr;
+	char *buffer;
+	size_t buflen;
+	int *errnop;
+
+	number = va_arg(ap, int);
+	pptr = va_arg(ap, struct protoent *);
+	buffer = va_arg(ap, char *);
+	buflen = va_arg(ap, size_t);
+	errnop = va_arg(ap, int *);
+
+	if ((ped = __protoent_data_init()) == NULL) {
+		*errnop = errno;
+		return (NS_NOTFOUND);
+	}
+
+	__setprotoent_p(ped->stayopen, ped);
+	while ((error = __getprotoent_p(&pe, ped)) == 0)
+		if (pe.p_proto == number)
+			break;
+	if (!ped->stayopen)
+		__endprotoent_p(ped);
+	if (error != 0) {
+		*errnop = errno;
+		return (NS_NOTFOUND);
+	}
+	if (__copy_protoent(&pe, pptr, buffer, buflen) != 0) {
+		*errnop = errno;
+		return (NS_RETURN);
+	}
+
+	*((struct protoent **)retval) = pptr;
+	return (NS_SUCCESS);
+}
+
+int
+getprotobynumber_r(int proto, struct protoent *pptr, char *buffer,
+    size_t buflen, struct protoent **result)
+{
+#ifdef NS_CACHING
+	static const nss_cache_info cache_info =
+    		NS_COMMON_CACHE_INFO_INITIALIZER(
+		protocols, (void *)nss_lt_id,
+		__proto_id_func, __proto_marshal_func, __proto_unmarshal_func);
+#endif
+
+	static const ns_dtab dtab[] = {
+		{ NSSRC_FILES, files_getprotobynumber, NULL },
+#ifdef NS_CACHING
+		NS_CACHE_CB(&cache_info)
+#endif
+		{ NULL, NULL, NULL }
+	};
+	int	rv, ret_errno;
+
+	ret_errno = 0;
+	*result = NULL;
+	rv = nsdispatch(result, dtab, NSDB_PROTOCOLS, "getprotobynumber_r",
+		defaultsrc, proto, pptr, buffer, buflen, &ret_errno);
+
+	if (rv != NS_SUCCESS) {
+		errno = ret_errno;
+		return (ret_errno);
+	}
+	return (0);
+}
+
+struct protoent *
+getprotobynumber(int proto)
+{
+	struct protodata *pd;
+	struct protoent *rval;
+
+	if ((pd = __protodata_init()) == NULL)
+		return (NULL);
+	if (getprotobynumber_r(proto, &pd->proto, pd->data, sizeof(pd->data),
+	    &rval) != 0)
+		return (NULL);
+	return (rval);
+}
diff --git a/freebsd-userspace/lib/libc/net/getprotoent.c b/freebsd-userspace/lib/libc/net/getprotoent.c
new file mode 100644
index 0000000..c817cb5
--- /dev/null
+++ b/freebsd-userspace/lib/libc/net/getprotoent.c
@@ -0,0 +1,556 @@
+#include "port_before.h"
+
+/*
+ * Copyright (c) 1983, 1993
+ *	The Regents of the University of California.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON 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 ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#if defined(LIBC_SCCS) && !defined(lint)
+static char sccsid[] = "@(#)getprotoent.c	8.1 (Berkeley) 6/4/93";
+#endif /* LIBC_SCCS and not lint */
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/param.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <errno.h>
+#include <limits.h>
+#include <netdb.h>
+#include <nsswitch.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "namespace.h"
+#include "reentrant.h"
+#include "un-namespace.h"
+#include "netdb_private.h"
+#ifdef NS_CACHING
+#include "nscache.h"
+#endif
+#include "nss_tls.h"
+
+static const ns_src defaultsrc[] = {
+	{ NSSRC_FILES, NS_SUCCESS },
+	{ NULL, 0 }
+};
+
+NETDB_THREAD_ALLOC(protoent_data)
+NETDB_THREAD_ALLOC(protodata)
+
+static void
+protoent_data_clear(struct protoent_data *ped)
+{
+	if (ped->fp) {
+		fclose(ped->fp);
+		ped->fp = NULL;
+	}
+}
+
+static void
+protoent_data_free(void *ptr)
+{
+	struct protoent_data *ped = ptr;
+
+	protoent_data_clear(ped);
+	free(ped);
+}
+
+static void
+protodata_free(void *ptr)
+{
+	free(ptr);
+}
+
+#ifdef NS_CACHING
+int
+__proto_id_func(char *buffer, size_t *buffer_size, va_list ap,
+    void *cache_mdata)
+{
+	char *name;
+	int proto;
+
+	size_t desired_size, size;
+	enum nss_lookup_type lookup_type;
+	int res = NS_UNAVAIL;
+
+	lookup_type = (enum nss_lookup_type)cache_mdata;
+	switch (lookup_type) {
+	case nss_lt_name:
+		name = va_arg(ap, char *);
+
+		size = strlen(name);
+		desired_size = sizeof(enum nss_lookup_type) + size + 1;
+		if (desired_size > *buffer_size) {
+			res = NS_RETURN;
+			goto fin;
+		}
+
+		memcpy(buffer, &lookup_type, sizeof(enum nss_lookup_type));
+		memcpy(buffer + sizeof(enum nss_lookup_type), name, size + 1);
+
+		res = NS_SUCCESS;
+		break;
+	case nss_lt_id:
+		proto = va_arg(ap, int);
+
+		desired_size = sizeof(enum nss_lookup_type) + sizeof(int);
+		if (desired_size > *buffer_size) {
+			res = NS_RETURN;
+			goto fin;
+		}
+
+		memcpy(buffer, &lookup_type, sizeof(enum nss_lookup_type));
+		memcpy(buffer + sizeof(enum nss_lookup_type), &proto,
+			sizeof(int));
+
+		res = NS_SUCCESS;
+		break;
+	default:
+		/* should be unreachable */
+		return (NS_UNAVAIL);
+	}
+
+fin:
+	*buffer_size = desired_size;
+	return (res);
+}
+
+
+int
+__proto_marshal_func(char *buffer, size_t *buffer_size, void *retval,
+    va_list ap, void *cache_mdata)
+{
+	char *name;
+	int num;
+	struct protoent *proto;
+	char *orig_buf;
+	size_t orig_buf_size;
+
+	struct protoent new_proto;
+	size_t desired_size, size, aliases_size;
+	char *p;
+	char **alias;
+
+	switch ((enum nss_lookup_type)cache_mdata) {
+	case nss_lt_name:
+		name = va_arg(ap, char *);
+		break;
+	case nss_lt_id:
+		num = va_arg(ap, int);
+		break;
+	case nss_lt_all:
+		break;
+	default:
+		/* should be unreachable */
+		return (NS_UNAVAIL);
+	}
+
+	proto = va_arg(ap, struct protoent *);
+	orig_buf = va_arg(ap, char *);
+	orig_buf_size = va_arg(ap, size_t);
+
+	desired_size = _ALIGNBYTES + sizeof(struct protoent) + sizeof(char *);
+	if (proto->p_name != NULL)
+		desired_size += strlen(proto->p_name) + 1;
+
+	if (proto->p_aliases != NULL) {
+		aliases_size = 0;
+		for (alias = proto->p_aliases; *alias; ++alias) {
+			desired_size += strlen(*alias) + 1;
+			++aliases_size;
+		}
+
+		desired_size += _ALIGNBYTES + (aliases_size + 1) *
+		    sizeof(char *);
+	}
+
+	if (*buffer_size < desired_size) {
+		/* this assignment is here for future use */
+		*buffer_size = desired_size;
+		return (NS_RETURN);
+	}
+
+	memcpy(&new_proto, proto, sizeof(struct protoent));
+
+	*buffer_size = desired_size;
+	memset(buffer, 0, desired_size);
+	p = buffer + sizeof(struct protoent) + sizeof(char *);
+	memcpy(buffer + sizeof(struct protoent), &p, sizeof(char *));
+	p = (char *)_ALIGN(p);
+
+	if (new_proto.p_name != NULL) {
+		size = strlen(new_proto.p_name);
+		memcpy(p, new_proto.p_name, size);
+		new_proto.p_name = p;
+		p += size + 1;
+	}
+
+	if (new_proto.p_aliases != NULL) {
+		p = (char *)_ALIGN(p);
+		memcpy(p, new_proto.p_aliases, sizeof(char *) * aliases_size);
+		new_proto.p_aliases = (char **)p;
+		p += sizeof(char *) * (aliases_size + 1);
+
+		for (alias = new_proto.p_aliases; *alias; ++alias) {
+			size = strlen(*alias);
+			memcpy(p, *alias, size);
+			*alias = p;
+			p += size + 1;
+		}
+	}
+
+	memcpy(buffer, &new_proto, sizeof(struct protoent));
+	return (NS_SUCCESS);
+}
+
+int
+__proto_unmarshal_func(char *buffer, size_t buffer_size, void *retval,
+    va_list ap, void *cache_mdata)
+{
+	char *name;
+	int num;
+	struct protoent *proto;
+	char *orig_buf;
+	size_t orig_buf_size;
+	int *ret_errno;
+
+	char *p;
+	char **alias;
+
+	switch ((enum nss_lookup_type)cache_mdata) {
+	case nss_lt_name:
+		name = va_arg(ap, char *);
+		break;
+	case nss_lt_id:
+		num = va_arg(ap, int);
+		break;
+	case nss_lt_all:
+		break;
+	default:
+		/* should be unreachable */
+		return (NS_UNAVAIL);
+	}
+
+	proto = va_arg(ap, struct protoent *);
+	orig_buf = va_arg(ap, char *);
+	orig_buf_size = va_arg(ap, size_t);
+	ret_errno = va_arg(ap, int *);
+
+	if (orig_buf_size <
+	    buffer_size - sizeof(struct protoent) - sizeof(char *)) {
+		*ret_errno = ERANGE;
+		return (NS_RETURN);
+	}
+
+	memcpy(proto, buffer, sizeof(struct protoent));
+	memcpy(&p, buffer + sizeof(struct protoent), sizeof(char *));
+
+	orig_buf = (char *)_ALIGN(orig_buf);
+	memcpy(orig_buf, buffer + sizeof(struct protoent) + sizeof(char *) +
+	    _ALIGN(p) - (size_t)p,
+	    buffer_size - sizeof(struct protoent) - sizeof(char *) -
+	    _ALIGN(p) + (size_t)p);
+	p = (char *)_ALIGN(p);
+
+	NS_APPLY_OFFSET(proto->p_name, orig_buf, p, char *);
+	if (proto->p_aliases != NULL) {
+		NS_APPLY_OFFSET(proto->p_aliases, orig_buf, p, char **);
+
+		for (alias = proto->p_aliases; *alias; ++alias)
+			NS_APPLY_OFFSET(*alias, orig_buf, p, char *);
+	}
+
+	if (retval != NULL)
+		*((struct protoent **)retval) = proto;
+
+	return (NS_SUCCESS);
+}
+
+NSS_MP_CACHE_HANDLING(protocols);
+#endif /* NS_CACHING */
+
+int
+__copy_protoent(struct protoent *pe, struct protoent *pptr, char *buf,
+    size_t buflen)
+{
+	char *cp;
+	int i, n;
+	int numptr, len;
+
+	/* Find out the amount of space required to store the answer. */
+	numptr = 1; /* NULL ptr */
+	len = (char *)ALIGN(buf) - buf;
+	for (i = 0; pe->p_aliases[i]; i++, numptr++) {
+		len += strlen(pe->p_aliases[i]) + 1;
+	}
+	len += strlen(pe->p_name) + 1;
+	len += numptr * sizeof(char*);
+
+	if (len > (int)buflen) {
+		errno = ERANGE;
+		return (-1);
+	}
+
+	/* copy protocol value*/
+	pptr->p_proto = pe->p_proto;
+
+	cp = (char *)ALIGN(buf) + numptr * sizeof(char *);
+
+	/* copy official name */
+	n = strlen(pe->p_name) + 1;
+	strcpy(cp, pe->p_name);
+	pptr->p_name = cp;
+	cp += n;
+
+	/* copy aliases */
+	pptr->p_aliases = (char **)ALIGN(buf);
+	for (i = 0 ; pe->p_aliases[i]; i++) {
+		n = strlen(pe->p_aliases[i]) + 1;
+		strcpy(cp, pe->p_aliases[i]);
+		pptr->p_aliases[i] = cp;
+		cp += n;
+	}
+	pptr->p_aliases[i] = NULL;
+
+	return (0);
+}
+
+void
+__setprotoent_p(int f, struct protoent_data *ped)
+{
+	if (ped->fp == NULL)
+		ped->fp = fopen(_PATH_PROTOCOLS, "r");
+	else
+		rewind(ped->fp);
+	ped->stayopen |= f;
+}
+
+void
+__endprotoent_p(struct protoent_data *ped)
+{
+	if (ped->fp) {
+		fclose(ped->fp);
+		ped->fp = NULL;
+	}
+	ped->stayopen = 0;
+}
+
+int
+__getprotoent_p(struct protoent *pe, struct protoent_data *ped)
+{
+	char *p;
+	char *cp, **q, *endp;
+	long l;
+
+	if (ped->fp == NULL && (ped->fp = fopen(_PATH_PROTOCOLS, "r")) == NULL)
+		return (-1);
+again:
+	if ((p = fgets(ped->line, sizeof ped->line, ped->fp)) == NULL)
+		return (-1);
+	if (*p == '#')
+		goto again;
+	cp = strpbrk(p, "#\n");
+	if (cp != NULL)
+		*cp = '\0';
+	pe->p_name = p;
+	cp = strpbrk(p, " \t");
+	if (cp == NULL)
+		goto again;
+	*cp++ = '\0';
+	while (*cp == ' ' || *cp == '\t')
+		cp++;
+	p = strpbrk(cp, " \t");
+	if (p != NULL)
+		*p++ = '\0';
+	l = strtol(cp, &endp, 10);
+	if (endp == cp || *endp != '\0' || l < 0 || l > USHRT_MAX)
+		goto again;
+	pe->p_proto = l;
+	q = pe->p_aliases = ped->aliases;
+	if (p != NULL) {
+		cp = p;
+		while (cp && *cp) {
+			if (*cp == ' ' || *cp == '\t') {
+				cp++;
+				continue;
+			}
+			if (q < &ped->aliases[_MAXALIASES - 1])
+				*q++ = cp;
+			cp = strpbrk(cp, " \t");
+			if (cp != NULL)
+				*cp++ = '\0';
+		}
+	}
+	*q = NULL;
+	return (0);
+}
+
+static int
+files_getprotoent_r(void *retval, void *mdata, va_list ap)
+{
+	struct protoent pe;
+	struct protoent_data *ped;
+
+	struct protoent	*pptr;
+	char *buffer;
+	size_t buflen;
+	int *errnop;
+
+	pptr = va_arg(ap, struct protoent *);
+	buffer = va_arg(ap, char *);
+	buflen = va_arg(ap, size_t);
+	errnop = va_arg(ap, int *);
+
+	if ((ped = __protoent_data_init()) == NULL) {
+		*errnop = errno;
+		return (NS_NOTFOUND);
+	}
+
+	if (__getprotoent_p(&pe, ped) != 0) {
+		*errnop = errno;
+		return (NS_NOTFOUND);
+	}
+
+	if (__copy_protoent(&pe, pptr, buffer, buflen) != 0) {
+		*errnop = errno;
+		return (NS_RETURN);
+	}
+
+	*((struct protoent **)retval) = pptr;
+	return (NS_SUCCESS);
+}
+
+static int
+files_setprotoent(void *retval, void *mdata, va_list ap)
+{
+	struct protoent_data *ped;
+	int f;
+
+	f = va_arg(ap, int);
+	if ((ped = __protoent_data_init()) == NULL)
+		return (NS_UNAVAIL);
+
+	__setprotoent_p(f, ped);
+	return (NS_UNAVAIL);
+}
+
+static int
+files_endprotoent(void *retval, void *mdata, va_list ap)
+{
+	struct protoent_data *ped;
+
+	if ((ped = __protoent_data_init()) == NULL)
+		return (NS_UNAVAIL);
+
+	__endprotoent_p(ped);
+	return (NS_UNAVAIL);
+}
+
+int
+getprotoent_r(struct protoent *pptr, char *buffer, size_t buflen,
+    struct protoent **result)
+{
+#ifdef NS_CACHING
+	static const nss_cache_info cache_info = NS_MP_CACHE_INFO_INITIALIZER(
+		protocols, (void *)nss_lt_all,
+		__proto_marshal_func, __proto_unmarshal_func);
+#endif
+	static const ns_dtab dtab[] = {
+		{ NSSRC_FILES, files_getprotoent_r, (void *)nss_lt_all },
+#ifdef NS_CACHING
+		NS_CACHE_CB(&cache_info)
+#endif
+		{ NULL, NULL, NULL }
+	};
+	int rv, ret_errno;
+
+	ret_errno = 0;
+	*result = NULL;
+	rv = nsdispatch(result, dtab, NSDB_PROTOCOLS, "getprotoent_r",
+	    defaultsrc, pptr, buffer, buflen, &ret_errno);
+
+	if (rv != NS_SUCCESS) {
+		errno = ret_errno;
+		return (ret_errno);
+	}
+	return (0);
+}
+
+void
+setprotoent(int stayopen)
+{
+#ifdef NS_CACHING
+	static const nss_cache_info cache_info = NS_MP_CACHE_INFO_INITIALIZER(
+		protocols, (void *)nss_lt_all,
+		NULL, NULL);
+#endif
+
+	static const ns_dtab dtab[] = {
+		{ NSSRC_FILES, files_setprotoent, NULL },
+#ifdef NS_CACHING
+		NS_CACHE_CB(&cache_info)
+#endif
+		{ NULL, NULL, NULL }
+	};
+
+	(void)nsdispatch(NULL, dtab, NSDB_PROTOCOLS, "setprotoent", defaultsrc,
+		stayopen);
+}
+
+void
+endprotoent(void)
+{
+#ifdef NS_CACHING
+	static const nss_cache_info cache_info = NS_MP_CACHE_INFO_INITIALIZER(
+		protocols, (void *)nss_lt_all,
+		NULL, NULL);
+#endif
+
+	static const ns_dtab dtab[] = {
+		{ NSSRC_FILES, files_endprotoent, NULL },
+#ifdef NS_CACHING
+		NS_CACHE_CB(&cache_info)
+#endif
+		{ NULL, NULL, NULL }
+	};
+
+	(void)nsdispatch(NULL, dtab, NSDB_PROTOCOLS, "endprotoent", defaultsrc);
+}
+
+struct protoent *
+getprotoent(void)
+{
+	struct protodata *pd;
+	struct protoent *rval;
+
+	if ((pd = __protodata_init()) == NULL)
+		return (NULL);
+	if (getprotoent_r(&pd->proto, pd->data, sizeof(pd->data), &rval) != 0)
+		return (NULL);
+	return (rval);
+}
diff --git a/freebsd-userspace/lib/libc/net/getprotoname.c b/freebsd-userspace/lib/libc/net/getprotoname.c
new file mode 100644
index 0000000..4199b21
--- /dev/null
+++ b/freebsd-userspace/lib/libc/net/getprotoname.c
@@ -0,0 +1,153 @@
+#include "port_before.h"
+
+/*
+ * Copyright (c) 1983, 1993
+ *	The Regents of the University of California.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON 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 ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#if defined(LIBC_SCCS) && !defined(lint)
+static char sccsid[] = "@(#)getprotoname.c	8.1 (Berkeley) 6/4/93";
+#endif /* LIBC_SCCS and not lint */
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <errno.h>
+#include <netdb.h>
+#include <nsswitch.h>
+#include <string.h>
+#include "netdb_private.h"
+#ifdef NS_CACHING
+#include "nscache.h"
+#endif
+#include "nss_tls.h"
+
+static const ns_src defaultsrc[] = {
+	{ NSSRC_FILES, NS_SUCCESS },
+	{ NULL, 0 }
+};
+
+#ifdef NS_CACHING
+extern int __proto_id_func(char *, size_t *, va_list, void *);
+extern int __proto_marshal_func(char *, size_t *, void *, va_list, void *);
+extern int __proto_unmarshal_func(char *, size_t, void *, va_list, void *);
+#endif
+
+static int
+files_getprotobyname(void *retval, void *mdata, va_list ap)
+{
+	struct protoent pe;
+	struct protoent_data *ped;
+	char **cp;
+	int error;
+
+	char *name;
+	struct protoent	*pptr;
+	char *buffer;
+	size_t buflen;
+	int *errnop;
+
+	name = va_arg(ap, char *);
+	pptr = va_arg(ap, struct protoent *);
+	buffer = va_arg(ap, char *);
+	buflen = va_arg(ap, size_t);
+	errnop = va_arg(ap, int *);
+
+
+	if ((ped = __protoent_data_init()) == NULL) {
+		*errnop = errno;
+		return (NS_NOTFOUND);
+	}
+
+	__setprotoent_p(ped->stayopen, ped);
+	while ((error = __getprotoent_p(&pe, ped)) == 0) {
+		if (strcmp(pe.p_name, name) == 0)
+			break;
+		for (cp = pe.p_aliases; *cp != 0; cp++)
+			if (strcmp(*cp, name) == 0)
+				goto found;
+	}
+found:
+	if (!ped->stayopen)
+		__endprotoent_p(ped);
+	if (error != 0) {
+		*errnop = errno;
+		return (NS_NOTFOUND);
+	}
+	if (__copy_protoent(&pe, pptr, buffer, buflen) != 0) {
+		*errnop = errno;
+		return (NS_RETURN);
+	}
+
+	*((struct protoent **)retval) = pptr;
+	return (NS_SUCCESS);
+}
+
+
+int
+getprotobyname_r(const char *name, struct protoent *pptr, char *buffer,
+    size_t buflen, struct protoent **result)
+{
+#ifdef NS_CACHING
+	static const nss_cache_info cache_info =
+		NS_COMMON_CACHE_INFO_INITIALIZER(
+		protocols, (void *)nss_lt_name,
+		__proto_id_func, __proto_marshal_func, __proto_unmarshal_func);
+#endif
+	static const ns_dtab dtab[] = {
+		{ NSSRC_FILES, files_getprotobyname, NULL },
+#ifdef NS_CACHING
+		NS_CACHE_CB(&cache_info)
+#endif
+		{ NULL, NULL, NULL }
+	};
+	int	rv, ret_errno;
+
+	ret_errno = 0;
+	*result = NULL;
+	rv = nsdispatch(result, dtab, NSDB_PROTOCOLS, "getprotobyname_r",
+	    defaultsrc, name, pptr, buffer, buflen, &ret_errno);
+
+	if (rv != NS_SUCCESS) {
+		errno = ret_errno;
+		return (ret_errno);
+	}
+	return (0);
+}
+
+struct protoent *
+getprotobyname(const char *name)
+{
+	struct protodata *pd;
+	struct protoent *rval;
+
+	if ((pd = __protodata_init()) == NULL)
+		return (NULL);
+	if (getprotobyname_r(name, &pd->proto, pd->data, sizeof(pd->data),
+	    &rval) != 0)
+		return (NULL);
+	return (rval);
+}
diff --git a/freebsd-userspace/lib/libc/net/getservent.c b/freebsd-userspace/lib/libc/net/getservent.c
new file mode 100644
index 0000000..65ffb2c
--- /dev/null
+++ b/freebsd-userspace/lib/libc/net/getservent.c
@@ -0,0 +1,1375 @@
+#include "port_before.h"
+
+/*
+ * Copyright (c) 1983, 1993
+ *	The Regents of the University of California.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON 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 ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#if defined(LIBC_SCCS) && !defined(lint)
+static char sccsid[] = "@(#)getservent.c	8.1 (Berkeley) 6/4/93";
+#endif /* LIBC_SCCS and not lint */
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/param.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <arpa/inet.h>
+#include <db.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <netdb.h>
+#include <nsswitch.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <unistd.h>
+#ifdef YP
+#include <rpc/rpc.h>
+#include <rpcsvc/yp_prot.h>
+#include <rpcsvc/ypclnt.h>
+#endif
+#include "namespace.h"
+#include "reentrant.h"
+#include "un-namespace.h"
+#include "netdb_private.h"
+#ifdef NS_CACHING
+#include "nscache.h"
+#endif
+#include "nss_tls.h"
+
+enum constants
+{
+	SETSERVENT		= 1,
+	ENDSERVENT		= 2,
+	SERVENT_STORAGE_INITIAL	= 1 << 10, /* 1 KByte */
+	SERVENT_STORAGE_MAX	= 1 << 20, /* 1 MByte */
+};
+
+struct servent_mdata
+{
+	enum nss_lookup_type how;
+	int compat_mode;
+};
+
+static const ns_src defaultsrc[] = {
+	{ NSSRC_COMPAT, NS_SUCCESS },
+	{ NULL, 0 }
+};
+
+static int servent_unpack(char *, struct servent *, char **, size_t, int *);
+
+/* files backend declarations */
+struct files_state
+{
+	FILE *fp;
+	int stayopen;
+
+	int compat_mode_active;
+};
+static void files_endstate(void *);
+NSS_TLS_HANDLING(files);
+
+static int files_servent(void *, void *, va_list);
+static int files_setservent(void *, void *, va_list);
+
+/* db backend declarations */
+struct db_state
+{
+        DB *db;
+	int stayopen;
+	int keynum;
+};
+static void db_endstate(void *);
+NSS_TLS_HANDLING(db);
+
+static int db_servent(void *, void *, va_list);
+static int db_setservent(void *, void *, va_list);
+
+#ifdef YP
+/* nis backend declarations */
+static 	int 	nis_servent(void *, void *, va_list);
+static 	int 	nis_setservent(void *, void *, va_list);
+
+struct nis_state
+{
+	int yp_stepping;
+	char yp_domain[MAXHOSTNAMELEN];
+	char *yp_key;
+	int yp_keylen;
+};
+static void nis_endstate(void *);
+NSS_TLS_HANDLING(nis);
+
+static int nis_servent(void *, void *, va_list);
+static int nis_setservent(void *, void *, va_list);
+#endif
+
+/* compat backend declarations */
+static int compat_setservent(void *, void *, va_list);
+
+/* get** wrappers for get**_r functions declarations */
+struct servent_state {
+	struct servent serv;
+	char *buffer;
+	size_t bufsize;
+};
+static	void	servent_endstate(void *);
+NSS_TLS_HANDLING(servent);
+
+struct key {
+	const char *proto;
+	union {
+		const char *name;
+		int port;
+	};
+};
+
+static int wrap_getservbyname_r(struct key, struct servent *, char *, size_t,
+    struct servent **);
+static int wrap_getservbyport_r(struct key, struct servent *, char *, size_t,
+    struct servent **);
+static int wrap_getservent_r(struct key, struct servent *, char *, size_t,
+    struct servent **);
+static struct servent *getserv(int (*fn)(struct key, struct servent *, char *,
+    size_t, struct servent **), struct key);
+
+#ifdef NS_CACHING
+static int serv_id_func(char *, size_t *, va_list, void *);
+static int serv_marshal_func(char *, size_t *, void *, va_list, void *);
+static int serv_unmarshal_func(char *, size_t, void *, va_list, void *);
+#endif
+
+static int
+servent_unpack(char *p, struct servent *serv, char **aliases,
+    size_t aliases_size, int *errnop)
+{
+	char *cp, **q, *endp;
+	long l;
+
+	if (*p == '#')
+		return -1;
+
+	memset(serv, 0, sizeof(struct servent));
+
+	cp = strpbrk(p, "#\n");
+	if (cp != NULL)
+		*cp = '\0';
+	serv->s_name = p;
+
+	p = strpbrk(p, " \t");
+	if (p == NULL)
+		return -1;
+	*p++ = '\0';
+	while (*p == ' ' || *p == '\t')
+		p++;
+	cp = strpbrk(p, ",/");
+	if (cp == NULL)
+		return -1;
+
+	*cp++ = '\0';
+	l = strtol(p, &endp, 10);
+	if (endp == p || *endp != '\0' || l < 0 || l > USHRT_MAX)
+		return -1;
+	serv->s_port = htons((in_port_t)l);
+	serv->s_proto = cp;
+
+	q = serv->s_aliases = aliases;
+	cp = strpbrk(cp, " \t");
+	if (cp != NULL)
+		*cp++ = '\0';
+	while (cp && *cp) {
+		if (*cp == ' ' || *cp == '\t') {
+			cp++;
+			continue;
+		}
+		if (q < &aliases[aliases_size - 1]) {
+			*q++ = cp;
+		} else {
+			*q = NULL;
+			*errnop = ERANGE;
+			return -1;
+		}
+		cp = strpbrk(cp, " \t");
+		if (cp != NULL)
+			*cp++ = '\0';
+	}
+	*q = NULL;
+
+	return 0;
+}
+
+static int
+parse_result(struct servent *serv, char *buffer, size_t bufsize,
+    char *resultbuf, size_t resultbuflen, int *errnop)
+{
+	char **aliases;
+	int aliases_size;
+
+	if (bufsize <= resultbuflen + _ALIGNBYTES + sizeof(char *)) {
+		*errnop = ERANGE;
+		return (NS_RETURN);
+	}
+	aliases = (char **)_ALIGN(&buffer[resultbuflen + 1]);
+	aliases_size = (buffer + bufsize - (char *)aliases) / sizeof(char *);
+	if (aliases_size < 1) {
+		*errnop = ERANGE;
+		return (NS_RETURN);
+	}
+
+	memcpy(buffer, resultbuf, resultbuflen);
+	buffer[resultbuflen] = '\0';
+
+	if (servent_unpack(buffer, serv, aliases, aliases_size, errnop) != 0)
+		return ((*errnop == 0) ? NS_NOTFOUND : NS_RETURN);
+	return (NS_SUCCESS);
+}
+
+/* files backend implementation */
+static	void
+files_endstate(void *p)
+{
+	FILE * f;
+
+	if (p == NULL)
+		return;
+
+	f = ((struct files_state *)p)->fp;
+	if (f != NULL)
+		fclose(f);
+
+	free(p);
+}
+
+/*
+ * compat structures. compat and files sources functionalities are almost
+ * equal, so they all are managed by files_servent function
+ */
+static int
+files_servent(void *retval, void *mdata, va_list ap)
+{
+	static const ns_src compat_src[] = {
+#ifdef YP
+		{ NSSRC_NIS, NS_SUCCESS },
+#endif
+		{ NULL, 0 }
+	};
+	ns_dtab compat_dtab[] = {
+		{ NSSRC_DB, db_servent,
+			(void *)((struct servent_mdata *)mdata)->how },
+#ifdef YP
+		{ NSSRC_NIS, nis_servent,
+			(void *)((struct servent_mdata *)mdata)->how },
+#endif
+		{ NULL, NULL, NULL }
+	};
+
+	struct files_state *st;
+	int rv;
+	int stayopen;
+
+	struct servent_mdata *serv_mdata;
+	char *name;
+	char *proto;
+	int port;
+
+	struct servent *serv;
+	char *buffer;
+	size_t bufsize;
+	int *errnop;
+
+	size_t linesize;
+	char *line;
+	char **cp;
+
+	name = NULL;
+	proto = NULL;
+	serv_mdata = (struct servent_mdata *)mdata;
+	switch (serv_mdata->how) {
+	case nss_lt_name:
+		name = va_arg(ap, char *);
+		proto = va_arg(ap, char *);
+		break;
+	case nss_lt_id:
+		port = va_arg(ap, int);
+		proto = va_arg(ap, char *);
+		break;
+	case nss_lt_all:
+		break;
+	default:
+		return NS_NOTFOUND;
+	};
+
+	serv = va_arg(ap, struct servent *);
+	buffer  = va_arg(ap, char *);
+	bufsize = va_arg(ap, size_t);
+	errnop = va_arg(ap,int *);
+
+	*errnop = files_getstate(&st);
+	if (*errnop != 0)
+		return (NS_UNAVAIL);
+
+	if (st->fp == NULL)
+		st->compat_mode_active = 0;
+
+	if (st->fp == NULL && (st->fp = fopen(_PATH_SERVICES, "r")) == NULL) {
+		*errnop = errno;
+		return (NS_UNAVAIL);
+	}
+
+	if (serv_mdata->how == nss_lt_all)
+		stayopen = 1;
+	else {
+		rewind(st->fp);
+		stayopen = st->stayopen;
+	}
+
+	rv = NS_NOTFOUND;
+	do {
+		if (!st->compat_mode_active) {
+			if ((line = fgetln(st->fp, &linesize)) == NULL) {
+				*errnop = errno;
+				rv = NS_RETURN;
+				break;
+			}
+
+			if (*line=='+' && serv_mdata->compat_mode != 0)
+				st->compat_mode_active = 1;
+		}
+
+		if (st->compat_mode_active != 0) {
+			switch (serv_mdata->how) {
+			case nss_lt_name:
+				rv = nsdispatch(retval, compat_dtab,
+				    NSDB_SERVICES_COMPAT, "getservbyname_r",
+				    compat_src, name, proto, serv, buffer,
+				    bufsize, errnop);
+				break;
+			case nss_lt_id:
+				rv = nsdispatch(retval, compat_dtab,
+				    NSDB_SERVICES_COMPAT, "getservbyport_r",
+				    compat_src, port, proto, serv, buffer,
+					bufsize, errnop);
+				break;
+			case nss_lt_all:
+				rv = nsdispatch(retval, compat_dtab,
+				    NSDB_SERVICES_COMPAT, "getservent_r",
+				    compat_src, serv, buffer, bufsize, errnop);
+				break;
+			}
+
+			if (!(rv & NS_TERMINATE) ||
+			    serv_mdata->how != nss_lt_all)
+				st->compat_mode_active = 0;
+
+			continue;
+		}
+
+		rv = parse_result(serv, buffer, bufsize, line, linesize,
+		    errnop);
+		if (rv == NS_NOTFOUND)
+			continue;
+		if (rv == NS_RETURN)
+			break;
+
+		rv = NS_NOTFOUND;
+		switch (serv_mdata->how) {
+		case nss_lt_name:
+			if (strcmp(name, serv->s_name) == 0)
+				goto gotname;
+			for (cp = serv->s_aliases; *cp; cp++)
+				if (strcmp(name, *cp) == 0)
+					goto gotname;
+
+			continue;
+		gotname:
+			if (proto == 0 || strcmp(serv->s_proto, proto) == 0)
+				rv = NS_SUCCESS;
+			break;
+		case nss_lt_id:
+			if (port != serv->s_port)
+				continue;
+
+			if (proto == 0 || strcmp(serv->s_proto, proto) == 0)
+				rv = NS_SUCCESS;
+			break;
+		case nss_lt_all:
+			rv = NS_SUCCESS;
+			break;
+		}
+
+	} while (!(rv & NS_TERMINATE));
+
+	if (!stayopen && st->fp != NULL) {
+		fclose(st->fp);
+		st->fp = NULL;
+	}
+
+	if ((rv == NS_SUCCESS) && (retval != NULL))
+		*(struct servent **)retval=serv;
+
+	return (rv);
+}
+
+static int
+files_setservent(void *retval, void *mdata, va_list ap)
+{
+	struct files_state *st;
+	int rv;
+	int f;
+
+	rv = files_getstate(&st);
+	if (rv != 0)
+		return (NS_UNAVAIL);
+
+	switch ((enum constants)mdata) {
+	case SETSERVENT:
+		f = va_arg(ap,int);
+		if (st->fp == NULL)
+			st->fp = fopen(_PATH_SERVICES, "r");
+		else
+			rewind(st->fp);
+		st->stayopen |= f;
+		break;
+	case ENDSERVENT:
+		if (st->fp != NULL) {
+			fclose(st->fp);
+			st->fp = NULL;
+		}
+		st->stayopen = 0;
+		break;
+	default:
+		break;
+	};
+
+	st->compat_mode_active = 0;
+	return (NS_UNAVAIL);
+}
+
+/* db backend implementation */
+static	void
+db_endstate(void *p)
+{
+	DB *db;
+
+	if (p == NULL)
+		return;
+
+	db = ((struct db_state *)p)->db;
+	if (db != NULL)
+		db->close(db);
+
+	free(p);
+}
+
+static int
+db_servent(void *retval, void *mdata, va_list ap)
+{
+	char buf[BUFSIZ];
+	DBT key, data, *result;
+	DB *db;
+
+	struct db_state *st;
+	int rv;
+	int stayopen;
+
+	enum nss_lookup_type how;
+	char *name;
+	char *proto;
+	int port;
+
+	struct servent *serv;
+	char *buffer;
+	size_t bufsize;
+	int *errnop;
+
+	name = NULL;
+	proto = NULL;
+	how = (enum nss_lookup_type)mdata;
+	switch (how) {
+	case nss_lt_name:
+		name = va_arg(ap, char *);
+		proto = va_arg(ap, char *);
+		break;
+	case nss_lt_id:
+		port = va_arg(ap, int);
+		proto = va_arg(ap, char *);
+		break;
+	case nss_lt_all:
+		break;
+	default:
+		return NS_NOTFOUND;
+	};
+
+	serv = va_arg(ap, struct servent *);
+	buffer  = va_arg(ap, char *);
+	bufsize = va_arg(ap, size_t);
+	errnop = va_arg(ap,int *);
+
+	*errnop = db_getstate(&st);
+	if (*errnop != 0)
+		return (NS_UNAVAIL);
+
+	if (how == nss_lt_all && st->keynum < 0)
+		return (NS_NOTFOUND);
+
+	if (st->db == NULL) {
+		st->db = dbopen(_PATH_SERVICES_DB, O_RDONLY, 0, DB_HASH, NULL);
+		if (st->db == NULL) {
+			*errnop = errno;
+			return (NS_UNAVAIL);
+		}
+	}
+
+	stayopen = (how == nss_lt_all) ? 1 : st->stayopen;
+	db = st->db;
+
+	do {
+		switch (how) {
+		case nss_lt_name:
+			key.data = buf;
+			if (proto == NULL)
+				key.size = snprintf(buf, sizeof(buf),
+				    "\376%s", name);
+			else
+				key.size = snprintf(buf, sizeof(buf),
+				    "\376%s/%s", name, proto);
+			key.size++;
+			if (db->get(db, &key, &data, 0) != 0 ||
+			    db->get(db, &data, &key, 0) != 0) {
+				rv = NS_NOTFOUND;
+				goto db_fin;
+			}
+			result = &key;
+			break;
+		case nss_lt_id:
+			key.data = buf;
+			port = htons(port);
+			if (proto == NULL)
+				key.size = snprintf(buf, sizeof(buf),
+				    "\377%d", port);
+			else
+				key.size = snprintf(buf, sizeof(buf),
+				    "\377%d/%s", port, proto);
+			key.size++;
+			if (db->get(db, &key, &data, 0) != 0 ||
+			    db->get(db, &data, &key, 0) != 0) {
+				rv = NS_NOTFOUND;
+				goto db_fin;
+			}
+			result = &key;
+			break;
+		case nss_lt_all:
+			key.data = buf;
+			key.size = snprintf(buf, sizeof(buf), "%d",
+			    st->keynum++);
+			key.size++;
+			if (db->get(db, &key, &data, 0) != 0) {
+				st->keynum = -1;
+				rv = NS_NOTFOUND;
+				goto db_fin;
+			}
+			result = &data;
+			break;
+		}
+
+		rv = parse_result(serv, buffer, bufsize, result->data,
+		    result->size - 1, errnop);
+
+	} while (!(rv & NS_TERMINATE) && how == nss_lt_all);
+
+db_fin:
+	if (!stayopen && st->db != NULL) {
+		db->close(db);
+		st->db = NULL;
+	}
+
+	if (rv == NS_SUCCESS && retval != NULL)
+		*(struct servent **)retval = serv;
+
+	return (rv);
+}
+
+static int
+db_setservent(void *retval, void *mdata, va_list ap)
+{
+	DB *db;
+	struct db_state *st;
+	int rv;
+	int f;
+
+	rv = db_getstate(&st);
+	if (rv != 0)
+		return (NS_UNAVAIL);
+
+	switch ((enum constants)mdata) {
+	case SETSERVENT:
+		f = va_arg(ap, int);
+		st->stayopen |= f;
+		st->keynum = 0;
+		break;
+	case ENDSERVENT:
+		db = st->db;
+		if (db != NULL) {
+			db->close(db);
+			st->db = NULL;
+		}
+		st->stayopen = 0;
+		break;
+	default:
+		break;
+	};
+
+	return (NS_UNAVAIL);
+}
+
+/* nis backend implementation */
+#ifdef YP
+static 	void
+nis_endstate(void *p)
+{
+	if (p == NULL)
+		return;
+
+	free(((struct nis_state *)p)->yp_key);
+	free(p);
+}
+
+static int
+nis_servent(void *retval, void *mdata, va_list ap)
+{
+	char *resultbuf, *lastkey;
+	int resultbuflen;
+	char buf[YPMAXRECORD + 2];
+
+	struct nis_state *st;
+	int rv;
+
+	enum nss_lookup_type how;
+	char *name;
+	char *proto;
+	int port;
+
+	struct servent *serv;
+	char *buffer;
+	size_t bufsize;
+	int *errnop;
+
+	name = NULL;
+	proto = NULL;
+	how = (enum nss_lookup_type)mdata;
+	switch (how) {
+	case nss_lt_name:
+		name = va_arg(ap, char *);
+		proto = va_arg(ap, char *);
+		break;
+	case nss_lt_id:
+		port = va_arg(ap, int);
+		proto = va_arg(ap, char *);
+		break;
+	case nss_lt_all:
+		break;
+	default:
+		return NS_NOTFOUND;
+	};
+
+	serv = va_arg(ap, struct servent *);
+	buffer  = va_arg(ap, char *);
+	bufsize = va_arg(ap, size_t);
+	errnop = va_arg(ap, int *);
+
+	*errnop = nis_getstate(&st);
+	if (*errnop != 0)
+		return (NS_UNAVAIL);
+
+	if (st->yp_domain[0] == '\0') {
+		if (getdomainname(st->yp_domain, sizeof st->yp_domain)) {
+			*errnop = errno;
+			return (NS_UNAVAIL);
+		}
+	}
+
+	do {
+		switch (how) {
+		case nss_lt_name:
+			snprintf(buf, sizeof(buf), "%s/%s", name, proto);
+			if (yp_match(st->yp_domain, "services.byname", buf,
+			    strlen(buf), &resultbuf, &resultbuflen)) {
+				rv = NS_NOTFOUND;
+				goto fin;
+			}
+			break;
+		case nss_lt_id:
+			snprintf(buf, sizeof(buf), "%d/%s", ntohs(port),
+			    proto);
+
+			/*
+			 * We have to be a little flexible
+			 * here. Ideally you're supposed to have both
+			 * a services.byname and a services.byport
+			 * map, but some systems have only
+			 * services.byname. FreeBSD cheats a little by
+			 * putting the services.byport information in
+			 * the same map as services.byname so that
+			 * either case will work. We allow for both
+			 * possibilities here: if there is no
+			 * services.byport map, we try services.byname
+			 * instead.
+			 */
+			rv = yp_match(st->yp_domain, "services.byport", buf,
+			    strlen(buf), &resultbuf, &resultbuflen);
+			if (rv) {
+				if (rv == YPERR_MAP) {
+					if (yp_match(st->yp_domain,
+					    "services.byname", buf,
+					    strlen(buf), &resultbuf,
+					    &resultbuflen)) {
+						rv = NS_NOTFOUND;
+						goto fin;
+					}
+				} else {
+					rv = NS_NOTFOUND;
+					goto fin;
+				}
+			}
+
+			break;
+		case nss_lt_all:
+			if (!st->yp_stepping) {
+				free(st->yp_key);
+				rv = yp_first(st->yp_domain, "services.byname",
+				    &st->yp_key, &st->yp_keylen, &resultbuf,
+				    &resultbuflen);
+				if (rv) {
+					rv = NS_NOTFOUND;
+					goto fin;
+				}
+				st->yp_stepping = 1;
+			} else {
+				lastkey = st->yp_key;
+				rv = yp_next(st->yp_domain, "services.byname",
+				    st->yp_key, st->yp_keylen, &st->yp_key,
+				    &st->yp_keylen, &resultbuf, &resultbuflen);
+				free(lastkey);
+				if (rv) {
+					st->yp_stepping = 0;
+					rv = NS_NOTFOUND;
+					goto fin;
+				}
+			}
+			break;
+		};
+
+		rv = parse_result(serv, buffer, bufsize, resultbuf,
+		    resultbuflen, errnop);
+		free(resultbuf);
+
+	} while (!(rv & NS_TERMINATE) && how == nss_lt_all);
+
+fin:
+	if (rv == NS_SUCCESS && retval != NULL)
+		*(struct servent **)retval = serv;
+
+	return (rv);
+}
+
+static int
+nis_setservent(void *result, void *mdata, va_list ap)
+{
+	struct nis_state *st;
+	int rv;
+
+	rv = nis_getstate(&st);
+	if (rv != 0)
+		return (NS_UNAVAIL);
+
+	switch ((enum constants)mdata) {
+	case SETSERVENT:
+	case ENDSERVENT:
+		free(st->yp_key);
+		st->yp_key = NULL;
+		st->yp_stepping = 0;
+		break;
+	default:
+		break;
+	};
+
+	return (NS_UNAVAIL);
+}
+#endif
+
+/* compat backend implementation */
+static int
+compat_setservent(void *retval, void *mdata, va_list ap)
+{
+	static const ns_src compat_src[] = {
+#ifdef YP
+		{ NSSRC_NIS, NS_SUCCESS },
+#endif
+		{ NULL, 0 }
+	};
+	ns_dtab compat_dtab[] = {
+		{ NSSRC_DB, db_setservent, mdata },
+#ifdef YP
+		{ NSSRC_NIS, nis_setservent, mdata },
+#endif
+		{ NULL, NULL, NULL }
+	};
+	int f;
+
+	(void)files_setservent(retval, mdata, ap);
+
+	switch ((enum constants)mdata) {
+	case SETSERVENT:
+		f = va_arg(ap,int);
+		(void)nsdispatch(retval, compat_dtab, NSDB_SERVICES_COMPAT,
+		    "setservent", compat_src, f);
+		break;
+	case ENDSERVENT:
+		(void)nsdispatch(retval, compat_dtab, NSDB_SERVICES_COMPAT,
+		    "endservent", compat_src);
+		break;
+	default:
+		break;
+	}
+
+	return (NS_UNAVAIL);
+}
+
+#ifdef NS_CACHING
+static int
+serv_id_func(char *buffer, size_t *buffer_size, va_list ap, void *cache_mdata)
+{
+	char *name;
+	char *proto;
+	int port;
+
+	size_t desired_size, size, size2;
+	enum nss_lookup_type lookup_type;
+	int res = NS_UNAVAIL;
+
+	lookup_type = (enum nss_lookup_type)cache_mdata;
+	switch (lookup_type) {
+	case nss_lt_name:
+		name = va_arg(ap, char *);
+		proto = va_arg(ap, char *);
+
+		size = strlen(name);
+		desired_size = sizeof(enum nss_lookup_type) + size + 1;
+		if (proto != NULL) {
+			size2 = strlen(proto);
+			desired_size += size2 + 1;
+		} else
+			size2 = 0;
+
+		if (desired_size > *buffer_size) {
+			res = NS_RETURN;
+			goto fin;
+		}
+
+		memcpy(buffer, &lookup_type, sizeof(enum nss_lookup_type));
+		memcpy(buffer + sizeof(enum nss_lookup_type), name, size + 1);
+
+		if (proto != NULL)
+			memcpy(buffer + sizeof(enum nss_lookup_type) + size + 1,
+			    proto, size2 + 1);
+
+		res = NS_SUCCESS;
+		break;
+	case nss_lt_id:
+		port = va_arg(ap, int);
+		proto = va_arg(ap, char *);
+
+		desired_size = sizeof(enum nss_lookup_type) + sizeof(int);
+		if (proto != NULL) {
+			size = strlen(proto);
+			desired_size += size + 1;
+		} else
+			size = 0;
+
+		if (desired_size > *buffer_size) {
+			res = NS_RETURN;
+			goto fin;
+		}
+
+		memcpy(buffer, &lookup_type, sizeof(enum nss_lookup_type));
+		memcpy(buffer + sizeof(enum nss_lookup_type), &port,
+		    sizeof(int));
+
+		if (proto != NULL)
+			memcpy(buffer + sizeof(enum nss_lookup_type) +
+			    sizeof(int), proto, size + 1);
+
+		res = NS_SUCCESS;
+		break;
+	default:
+		/* should be unreachable */
+		return (NS_UNAVAIL);
+	}
+
+fin:
+	*buffer_size = desired_size;
+	return (res);
+}
+
+int
+serv_marshal_func(char *buffer, size_t *buffer_size, void *retval, va_list ap,
+    void *cache_mdata)
+{
+	char *name;
+	char *proto;
+	int port;
+	struct servent *serv;
+	char *orig_buf;
+	size_t orig_buf_size;
+
+	struct servent new_serv;
+	size_t desired_size;
+	char **alias;
+	char *p;
+	size_t size;
+	size_t aliases_size;
+
+	switch ((enum nss_lookup_type)cache_mdata) {
+	case nss_lt_name:
+		name = va_arg(ap, char *);
+		proto = va_arg(ap, char *);
+		break;
+	case nss_lt_id:
+		port = va_arg(ap, int);
+		proto = va_arg(ap, char *);
+		break;
+	case nss_lt_all:
+		break;
+	default:
+		/* should be unreachable */
+		return (NS_UNAVAIL);
+	}
+
+	serv = va_arg(ap, struct servent *);
+	orig_buf = va_arg(ap, char *);
+	orig_buf_size = va_arg(ap, size_t);
+
+	desired_size = _ALIGNBYTES + sizeof(struct servent) + sizeof(char *);
+	if (serv->s_name != NULL)
+		desired_size += strlen(serv->s_name) + 1;
+	if (serv->s_proto != NULL)
+		desired_size += strlen(serv->s_proto) + 1;
+
+	aliases_size = 0;
+	if (serv->s_aliases != NULL) {
+		for (alias = serv->s_aliases; *alias; ++alias) {
+			desired_size += strlen(*alias) + 1;
+			++aliases_size;
+		}
+
+		desired_size += _ALIGNBYTES +
+		    sizeof(char *) * (aliases_size + 1);
+	}
+
+	if (*buffer_size < desired_size) {
+		/* this assignment is here for future use */
+		*buffer_size = desired_size;
+		return (NS_RETURN);
+	}
+
+	memcpy(&new_serv, serv, sizeof(struct servent));
+	memset(buffer, 0, desired_size);
+
+	*buffer_size = desired_size;
+	p = buffer + sizeof(struct servent) + sizeof(char *);
+	memcpy(buffer + sizeof(struct servent), &p, sizeof(char *));
+	p = (char *)_ALIGN(p);
+
+	if (new_serv.s_name != NULL) {
+		size = strlen(new_serv.s_name);
+		memcpy(p, new_serv.s_name, size);
+		new_serv.s_name = p;
+		p += size + 1;
+	}
+
+	if (new_serv.s_proto != NULL) {
+		size = strlen(new_serv.s_proto);
+		memcpy(p, new_serv.s_proto, size);
+		new_serv.s_proto = p;
+		p += size + 1;
+	}
+
+	if (new_serv.s_aliases != NULL) {
+		p = (char *)_ALIGN(p);
+		memcpy(p, new_serv.s_aliases, sizeof(char *) * aliases_size);
+		new_serv.s_aliases = (char **)p;
+		p += sizeof(char *) * (aliases_size + 1);
+
+		for (alias = new_serv.s_aliases; *alias; ++alias) {
+			size = strlen(*alias);
+			memcpy(p, *alias, size);
+			*alias = p;
+			p += size + 1;
+		}
+	}
+
+	memcpy(buffer, &new_serv, sizeof(struct servent));
+	return (NS_SUCCESS);
+}
+
+int
+serv_unmarshal_func(char *buffer, size_t buffer_size, void *retval, va_list ap,
+    void *cache_mdata)
+{
+	char *name;
+	char *proto;
+	int port;
+	struct servent *serv;
+	char *orig_buf;
+	char *p;
+	char **alias;
+	size_t orig_buf_size;
+	int *ret_errno;
+
+	switch ((enum nss_lookup_type)cache_mdata) {
+	case nss_lt_name:
+		name = va_arg(ap, char *);
+		proto = va_arg(ap, char *);
+		break;
+	case nss_lt_id:
+		port = va_arg(ap, int);
+		proto = va_arg(ap, char *);
+		break;
+	case nss_lt_all:
+		break;
+	default:
+		/* should be unreachable */
+		return (NS_UNAVAIL);
+	}
+
+	serv = va_arg(ap, struct servent *);
+	orig_buf = va_arg(ap, char *);
+	orig_buf_size = va_arg(ap, size_t);
+	ret_errno = va_arg(ap, int *);
+
+	if (orig_buf_size <
+	    buffer_size - sizeof(struct servent) - sizeof(char *)) {
+		*ret_errno = ERANGE;
+		return (NS_RETURN);
+	}
+
+	memcpy(serv, buffer, sizeof(struct servent));
+	memcpy(&p, buffer + sizeof(struct servent), sizeof(char *));
+
+	orig_buf = (char *)_ALIGN(orig_buf);
+	memcpy(orig_buf, buffer + sizeof(struct servent) + sizeof(char *) +
+	    (_ALIGN(p) - (size_t)p),
+	    buffer_size - sizeof(struct servent) - sizeof(char *) -
+	    (_ALIGN(p) - (size_t)p));
+	p = (char *)_ALIGN(p);
+
+	NS_APPLY_OFFSET(serv->s_name, orig_buf, p, char *);
+	NS_APPLY_OFFSET(serv->s_proto, orig_buf, p, char *);
+	if (serv->s_aliases != NULL) {
+		NS_APPLY_OFFSET(serv->s_aliases, orig_buf, p, char **);
+
+		for (alias = serv->s_aliases; *alias; ++alias)
+			NS_APPLY_OFFSET(*alias, orig_buf, p, char *);
+	}
+
+	if (retval != NULL)
+		*((struct servent **)retval) = serv;
+	return (NS_SUCCESS);
+}
+
+NSS_MP_CACHE_HANDLING(services);
+#endif /* NS_CACHING */
+
+/* get**_r functions implementation */
+int
+getservbyname_r(const char *name, const char *proto, struct servent *serv,
+    char *buffer, size_t bufsize, struct servent **result)
+{
+	static const struct servent_mdata mdata = { nss_lt_name, 0 };
+	static const struct servent_mdata compat_mdata = { nss_lt_name, 1 };
+#ifdef NS_CACHING
+	static const nss_cache_info cache_info =
+	NS_COMMON_CACHE_INFO_INITIALIZER(
+		services, (void *)nss_lt_name,
+		serv_id_func, serv_marshal_func, serv_unmarshal_func);
+#endif /* NS_CACHING */
+	static const ns_dtab dtab[] = {
+		{ NSSRC_FILES, files_servent, (void *)&mdata },
+		{ NSSRC_DB, db_servent, (void *)nss_lt_name },
+#ifdef YP
+		{ NSSRC_NIS, nis_servent, (void *)nss_lt_name },
+#endif
+		{ NSSRC_COMPAT, files_servent, (void *)&compat_mdata },
+#ifdef NS_CACHING
+		NS_CACHE_CB(&cache_info)
+#endif
+		{ NULL, NULL, NULL }
+	};
+	int	rv, ret_errno;
+
+	ret_errno = 0;
+	*result = NULL;
+	rv = nsdispatch(result, dtab, NSDB_SERVICES, "getservbyname_r",
+	    defaultsrc, name, proto, serv, buffer, bufsize, &ret_errno);
+
+	if (rv == NS_SUCCESS)
+		return (0);
+	else
+		return (ret_errno);
+}
+
+int
+getservbyport_r(int port, const char *proto, struct servent *serv,
+    char *buffer, size_t bufsize, struct servent **result)
+{
+	static const struct servent_mdata mdata = { nss_lt_id, 0 };
+	static const struct servent_mdata compat_mdata = { nss_lt_id, 1 };
+#ifdef NS_CACHING
+	static const nss_cache_info cache_info =
+	NS_COMMON_CACHE_INFO_INITIALIZER(
+		services, (void *)nss_lt_id,
+		serv_id_func, serv_marshal_func, serv_unmarshal_func);
+#endif
+	static const ns_dtab dtab[] = {
+		{ NSSRC_FILES, files_servent, (void *)&mdata },
+		{ NSSRC_DB, db_servent, (void *)nss_lt_id },
+#ifdef YP
+		{ NSSRC_NIS, nis_servent, (void *)nss_lt_id },
+#endif
+		{ NSSRC_COMPAT, files_servent, (void *)&compat_mdata },
+#ifdef NS_CACHING
+		NS_CACHE_CB(&cache_info)
+#endif
+		{ NULL, NULL, NULL }
+	};
+	int rv, ret_errno;
+
+	ret_errno = 0;
+	*result = NULL;
+	rv = nsdispatch(result, dtab, NSDB_SERVICES, "getservbyport_r",
+	    defaultsrc, port, proto, serv, buffer, bufsize, &ret_errno);
+
+	if (rv == NS_SUCCESS)
+		return (0);
+	else
+		return (ret_errno);
+}
+
+int
+getservent_r(struct servent *serv, char *buffer, size_t bufsize,
+    struct servent **result)
+{
+	static const struct servent_mdata mdata = { nss_lt_all, 0 };
+	static const struct servent_mdata compat_mdata = { nss_lt_all, 1 };
+#ifdef NS_CACHING
+	static const nss_cache_info cache_info = NS_MP_CACHE_INFO_INITIALIZER(
+		services, (void *)nss_lt_all,
+		serv_marshal_func, serv_unmarshal_func);
+#endif
+	static const ns_dtab dtab[] = {
+		{ NSSRC_FILES, files_servent, (void *)&mdata },
+		{ NSSRC_DB, db_servent, (void *)nss_lt_all },
+#ifdef YP
+		{ NSSRC_NIS, nis_servent, (void *)nss_lt_all },
+#endif
+		{ NSSRC_COMPAT, files_servent, (void *)&compat_mdata },
+#ifdef NS_CACHING
+		NS_CACHE_CB(&cache_info)
+#endif
+		{ NULL, NULL, NULL }
+	};
+	int rv, ret_errno;
+
+	ret_errno = 0;
+	*result = NULL;
+	rv = nsdispatch(result, dtab, NSDB_SERVICES, "getservent_r",
+	    defaultsrc, serv, buffer, bufsize, &ret_errno);
+
+	if (rv == NS_SUCCESS)
+		return (0);
+	else
+		return (ret_errno);
+}
+
+void
+setservent(int stayopen)
+{
+#ifdef NS_CACHING
+	static const nss_cache_info cache_info = NS_MP_CACHE_INFO_INITIALIZER(
+		services, (void *)nss_lt_all,
+		NULL, NULL);
+#endif
+	static const ns_dtab dtab[] = {
+		{ NSSRC_FILES, files_setservent, (void *)SETSERVENT },
+		{ NSSRC_DB, db_setservent, (void *)SETSERVENT },
+#ifdef YP
+		{ NSSRC_NIS, nis_setservent, (void *)SETSERVENT },
+#endif
+		{ NSSRC_COMPAT, compat_setservent, (void *)SETSERVENT },
+#ifdef NS_CACHING
+		NS_CACHE_CB(&cache_info)
+#endif
+		{ NULL, NULL, NULL }
+	};
+
+	(void)nsdispatch(NULL, dtab, NSDB_SERVICES, "setservent", defaultsrc,
+	    stayopen);
+}
+
+void
+endservent()
+{
+#ifdef NS_CACHING
+	static const nss_cache_info cache_info = NS_MP_CACHE_INFO_INITIALIZER(
+		services, (void *)nss_lt_all,
+		NULL, NULL);
+#endif
+	static const ns_dtab dtab[] = {
+		{ NSSRC_FILES, files_setservent, (void *)ENDSERVENT },
+		{ NSSRC_DB, db_setservent, (void *)ENDSERVENT },
+#ifdef YP
+		{ NSSRC_NIS, nis_setservent, (void *)ENDSERVENT },
+#endif
+		{ NSSRC_COMPAT, compat_setservent, (void *)ENDSERVENT },
+#ifdef NS_CACHING
+		NS_CACHE_CB(&cache_info)
+#endif
+		{ NULL, NULL, NULL }
+	};
+
+	(void)nsdispatch(NULL, dtab, NSDB_SERVICES, "endservent", defaultsrc);
+}
+
+/* get** wrappers for get**_r functions implementation */
+static void
+servent_endstate(void *p)
+{
+	if (p == NULL)
+		return;
+
+	free(((struct servent_state *)p)->buffer);
+	free(p);
+}
+
+static int
+wrap_getservbyname_r(struct key key, struct servent *serv, char *buffer,
+    size_t bufsize, struct servent **res)
+{
+	return (getservbyname_r(key.name, key.proto, serv, buffer, bufsize,
+	    res));
+}
+
+static int
+wrap_getservbyport_r(struct key key, struct servent *serv, char *buffer,
+    size_t bufsize, struct servent **res)
+{
+	return (getservbyport_r(key.port, key.proto, serv, buffer, bufsize,
+	    res));
+}
+
+static	int
+wrap_getservent_r(struct key key, struct servent *serv, char *buffer,
+    size_t bufsize, struct servent **res)
+{
+	return (getservent_r(serv, buffer, bufsize, res));
+}
+
+static struct servent *
+getserv(int (*fn)(struct key, struct servent *, char *, size_t,
+    struct servent **), struct key key)
+{
+	int rv;
+	struct servent *res;
+	struct servent_state * st;
+
+	rv = servent_getstate(&st);
+	if (rv != 0) {
+		errno = rv;
+		return NULL;
+	}
+
+	if (st->buffer == NULL) {
+		st->buffer = malloc(SERVENT_STORAGE_INITIAL);
+		if (st->buffer == NULL)
+			return (NULL);
+		st->bufsize = SERVENT_STORAGE_INITIAL;
+	}
+	do {
+		rv = fn(key, &st->serv, st->buffer, st->bufsize, &res);
+		if (res == NULL && rv == ERANGE) {
+			free(st->buffer);
+			if ((st->bufsize << 1) > SERVENT_STORAGE_MAX) {
+				st->buffer = NULL;
+				errno = ERANGE;
+				return (NULL);
+			}
+			st->bufsize <<= 1;
+			st->buffer = malloc(st->bufsize);
+			if (st->buffer == NULL)
+				return (NULL);
+		}
+	} while (res == NULL && rv == ERANGE);
+	if (rv != 0)
+		errno = rv;
+
+	return (res);
+}
+
+struct servent *
+getservbyname(const char *name, const char *proto)
+{
+	struct key key;
+
+	key.name = name;
+	key.proto = proto;
+
+	return (getserv(wrap_getservbyname_r, key));
+}
+
+struct servent *
+getservbyport(int port, const char *proto)
+{
+	struct key key;
+
+	key.port = port;
+	key.proto = proto;
+
+	return (getserv(wrap_getservbyport_r, key));
+}
+
+struct servent *
+getservent()
+{
+	struct key key;
+
+	key.proto = NULL;
+	key.port = 0;
+
+	return (getserv(wrap_getservent_r, key));
+}
diff --git a/freebsd-userspace/lib/libc/net/netdb_private.h b/freebsd-userspace/lib/libc/net/netdb_private.h
new file mode 100644
index 0000000..b48dd7b
--- /dev/null
+++ b/freebsd-userspace/lib/libc/net/netdb_private.h
@@ -0,0 +1,145 @@
+/*-
+ * Copyright (C) 2005 The FreeBSD Project.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON 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 ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+#ifndef _NETDB_PRIVATE_H_
+#define _NETDB_PRIVATE_H_
+
+#include <stdio.h>				/* XXX: for FILE */
+
+#define	NETDB_THREAD_ALLOC(name)					\
+static struct name name;						\
+static thread_key_t name##_key;						\
+static once_t name##_init_once = ONCE_INITIALIZER;			\
+static int name##_thr_keycreated = 0;					\
+\
+static void name##_free(void *);					\
+\
+static void								\
+name##_keycreate(void)							\
+{									\
+	name##_thr_keycreated =						\
+	    (thr_keycreate(&name##_key, name##_free) == 0);		\
+}									\
+\
+struct name *								\
+__##name##_init(void)							\
+{									\
+	struct name *he;						\
+									\
+	if (thr_main() != 0)						\
+		return (&name);						\
+	if (thr_once(&name##_init_once, name##_keycreate) != 0 ||	\
+	    !name##_thr_keycreated)					\
+		return (NULL);						\
+	if ((he = thr_getspecific(name##_key)) != NULL)			\
+		return (he);						\
+	if ((he = calloc(1, sizeof(*he))) == NULL)			\
+		return (NULL);						\
+	if (thr_setspecific(name##_key, he) == 0)			\
+		return (he);						\
+	free(he);							\
+	return (NULL);							\
+}
+
+#define	_MAXALIASES	35
+#define	_MAXLINELEN	1024
+#define	_MAXADDRS	35
+#define	_HOSTBUFSIZE	(8 * 1024)
+#define	_NETBUFSIZE	1025
+
+struct hostent_data {
+	uint32_t host_addr[4];			/* IPv4 or IPv6 */
+	char *h_addr_ptrs[_MAXADDRS + 1];
+	char *host_aliases[_MAXALIASES];
+	char hostbuf[_HOSTBUFSIZE];
+	FILE *hostf;
+	int stayopen;
+#ifdef YP
+	char *yp_domain;
+#endif
+};
+
+struct netent_data {
+	char *net_aliases[_MAXALIASES];
+	char netbuf[_NETBUFSIZE];
+	FILE *netf;
+	int stayopen;
+#ifdef YP
+	char *yp_domain;
+#endif
+};
+
+struct protoent_data {
+	FILE *fp;
+	char *aliases[_MAXALIASES];
+	int stayopen;
+	char line[_MAXLINELEN + 1];
+};
+
+struct hostdata {
+	struct hostent host;
+	char data[sizeof(struct hostent_data)];
+};
+
+struct netdata {
+	struct netent net;
+	char data[sizeof(struct netent_data)];
+};
+
+struct protodata {
+	struct protoent proto;
+	char data[sizeof(struct protoent_data)];
+};
+
+struct hostdata *__hostdata_init(void);
+struct hostent *__hostent_init(void);
+struct hostent_data *__hostent_data_init(void);
+struct netdata *__netdata_init(void);
+struct netent_data *__netent_data_init(void);
+struct protodata *__protodata_init(void);
+struct protoent_data *__protoent_data_init(void);
+int __copy_hostent(struct hostent *, struct hostent *, char *, size_t);
+int __copy_netent(struct netent *, struct netent *, char *, size_t);
+int __copy_protoent(struct protoent *, struct protoent *, char *, size_t);
+
+void __endprotoent_p(struct protoent_data *);
+int __getprotoent_p(struct protoent *, struct protoent_data *);
+void __setprotoent_p(int, struct protoent_data *);
+void _endhostdnsent(void);
+void _endhosthtent(struct hostent_data *);
+void _endnetdnsent(void);
+void _endnethtent(struct netent_data *);
+struct hostent *_gethostbynisaddr(const void *, socklen_t, int);
+struct hostent *_gethostbynisname(const char *, int);
+void _map_v4v6_address(const char *, char *);
+void _map_v4v6_hostent(struct hostent *, char **, char *);
+void _sethostdnsent(int);
+void _sethosthtent(int, struct hostent_data *);
+void _setnetdnsent(int);
+void _setnethtent(int, struct netent_data *);
+
+#endif /* _NETDB_PRIVATE_H_ */
diff --git a/freebsd-userspace/lib/libc/net/res_config.h b/freebsd-userspace/lib/libc/net/res_config.h
new file mode 100644
index 0000000..05909bc
--- /dev/null
+++ b/freebsd-userspace/lib/libc/net/res_config.h
@@ -0,0 +1,6 @@
+/* $FreeBSD$ */
+
+#define	DEBUG	1	/* enable debugging code (needed for dig) */
+#define	RESOLVSORT	/* allow sorting of addresses in gethostbyname */
+#undef	SUNSECURITY	/* verify gethostbyaddr() calls - WE DONT NEED IT  */
+#define MULTI_PTRS_ARE_ALIASES 1 /* fold multiple PTR records into aliases */
diff --git a/freebsd-userspace/local/port_after.h b/freebsd-userspace/local/port_after.h
new file mode 100644
index 0000000..e69de29
diff --git a/freebsd-userspace/local/port_before.h b/freebsd-userspace/local/port_before.h
new file mode 100644
index 0000000..385124b
--- /dev/null
+++ b/freebsd-userspace/local/port_before.h
@@ -0,0 +1 @@
+#include <freebsd/bsd.h>
diff --git a/freebsd-userspace/local/pthread_np.h b/freebsd-userspace/local/pthread_np.h
new file mode 100644
index 0000000..fa882cb
--- /dev/null
+++ b/freebsd-userspace/local/pthread_np.h
@@ -0,0 +1 @@
+/* empty file */
diff --git a/freebsd-userspace/local/sys/_pthreadtypes.h b/freebsd-userspace/local/sys/_pthreadtypes.h
new file mode 100644
index 0000000..fa882cb
--- /dev/null
+++ b/freebsd-userspace/local/sys/_pthreadtypes.h
@@ -0,0 +1 @@
+/* empty file */
diff --git a/freebsd-userspace/local/syslog.h b/freebsd-userspace/local/syslog.h
new file mode 100644
index 0000000..830b492
--- /dev/null
+++ b/freebsd-userspace/local/syslog.h
@@ -0,0 +1 @@
+#include <sys/syslog.h>
diff --git a/freebsd-userspace/sys/net/ethernet.h b/freebsd-userspace/sys/net/ethernet.h
new file mode 100644
index 0000000..ae7341e
--- /dev/null
+++ b/freebsd-userspace/sys/net/ethernet.h
@@ -0,0 +1,405 @@
+/*
+ * Fundamental constants relating to ethernet.
+ *
+ * $FreeBSD$
+ *
+ */
+
+#ifndef _NET_ETHERNET_H_
+#define _NET_ETHERNET_H_
+
+/*
+ * Some basic Ethernet constants.
+ */
+#define	ETHER_ADDR_LEN		6	/* length of an Ethernet address */
+#define	ETHER_TYPE_LEN		2	/* length of the Ethernet type field */
+#define	ETHER_CRC_LEN		4	/* length of the Ethernet CRC */
+#define	ETHER_HDR_LEN		(ETHER_ADDR_LEN*2+ETHER_TYPE_LEN)
+#define	ETHER_MIN_LEN		64	/* minimum frame len, including CRC */
+#define	ETHER_MAX_LEN		1518	/* maximum frame len, including CRC */
+#define	ETHER_MAX_LEN_JUMBO	9018	/* max jumbo frame len, including CRC */
+
+#define	ETHER_VLAN_ENCAP_LEN	4	/* len of 802.1Q VLAN encapsulation */
+/*
+ * Mbuf adjust factor to force 32-bit alignment of IP header.
+ * Drivers should do m_adj(m, ETHER_ALIGN) when setting up a
+ * receive so the upper layers get the IP header properly aligned
+ * past the 14-byte Ethernet header.
+ */
+#define	ETHER_ALIGN		2	/* driver adjust for IP hdr alignment */
+
+/*
+ * Compute the maximum frame size based on ethertype (i.e. possible
+ * encapsulation) and whether or not an FCS is present.
+ */
+#define	ETHER_MAX_FRAME(ifp, etype, hasfcs)				\
+	((ifp)->if_mtu + ETHER_HDR_LEN +				\
+	 ((hasfcs) ? ETHER_CRC_LEN : 0) +				\
+	 (((etype) == ETHERTYPE_VLAN) ? ETHER_VLAN_ENCAP_LEN : 0))
+
+/*
+ * Ethernet-specific mbuf flags.
+ */
+#define	M_HASFCS	M_PROTO5	/* FCS included at end of frame */
+
+/*
+ * Ethernet CRC32 polynomials (big- and little-endian verions).
+ */
+#define	ETHER_CRC_POLY_LE	0xedb88320
+#define	ETHER_CRC_POLY_BE	0x04c11db6
+
+/*
+ * A macro to validate a length with
+ */
+#define	ETHER_IS_VALID_LEN(foo)	\
+	((foo) >= ETHER_MIN_LEN && (foo) <= ETHER_MAX_LEN)
+
+/*
+ * Structure of a 10Mb/s Ethernet header.
+ */
+struct ether_header {
+	u_char	ether_dhost[ETHER_ADDR_LEN];
+	u_char	ether_shost[ETHER_ADDR_LEN];
+	u_short	ether_type;
+} __packed;
+
+/*
+ * Structure of a 48-bit Ethernet address.
+ */
+struct ether_addr {
+	u_char octet[ETHER_ADDR_LEN];
+} __packed;
+
+#define	ETHER_IS_MULTICAST(addr) (*(addr) & 0x01) /* is address mcast/bcast? */
+
+/*
+ *  NOTE: 0x0000-0x05DC (0..1500) are generally IEEE 802.3 length fields.
+ *  However, there are some conflicts.
+ */
+
+#define	ETHERTYPE_8023		0x0004	/* IEEE 802.3 packet */
+		   /* 0x0101 .. 0x1FF	   Experimental */
+#define	ETHERTYPE_PUP		0x0200	/* Xerox PUP protocol - see 0A00 */
+#define	ETHERTYPE_PUPAT		0x0200	/* PUP Address Translation - see 0A01 */
+#define	ETHERTYPE_SPRITE	0x0500	/* ??? */
+			     /* 0x0400	   Nixdorf */
+#define	ETHERTYPE_NS		0x0600	/* XNS */
+#define	ETHERTYPE_NSAT		0x0601	/* XNS Address Translation (3Mb only) */
+#define	ETHERTYPE_DLOG1 	0x0660	/* DLOG (?) */
+#define	ETHERTYPE_DLOG2 	0x0661	/* DLOG (?) */
+#define	ETHERTYPE_IP		0x0800	/* IP protocol */
+#define	ETHERTYPE_X75		0x0801	/* X.75 Internet */
+#define	ETHERTYPE_NBS		0x0802	/* NBS Internet */
+#define	ETHERTYPE_ECMA		0x0803	/* ECMA Internet */
+#define	ETHERTYPE_CHAOS 	0x0804	/* CHAOSnet */
+#define	ETHERTYPE_X25		0x0805	/* X.25 Level 3 */
+#define	ETHERTYPE_ARP		0x0806	/* Address resolution protocol */
+#define	ETHERTYPE_NSCOMPAT	0x0807	/* XNS Compatibility */
+#define	ETHERTYPE_FRARP 	0x0808	/* Frame Relay ARP (RFC1701) */
+			     /* 0x081C	   Symbolics Private */
+		    /* 0x0888 - 0x088A	   Xyplex */
+#define	ETHERTYPE_UBDEBUG	0x0900	/* Ungermann-Bass network debugger */
+#define	ETHERTYPE_IEEEPUP	0x0A00	/* Xerox IEEE802.3 PUP */
+#define	ETHERTYPE_IEEEPUPAT	0x0A01	/* Xerox IEEE802.3 PUP Address Translation */
+#define	ETHERTYPE_VINES 	0x0BAD	/* Banyan VINES */
+#define	ETHERTYPE_VINESLOOP	0x0BAE	/* Banyan VINES Loopback */
+#define	ETHERTYPE_VINESECHO	0x0BAF	/* Banyan VINES Echo */
+
+/*		       0x1000 - 0x100F	   Berkeley Trailer */
+/*
+ * The ETHERTYPE_NTRAILER packet types starting at ETHERTYPE_TRAIL have
+ * (type-ETHERTYPE_TRAIL)*512 bytes of data followed
+ * by an ETHER type (as given above) and then the (variable-length) header.
+ */
+#define	ETHERTYPE_TRAIL		0x1000	/* Trailer packet */
+#define	ETHERTYPE_NTRAILER	16
+
+#define	ETHERTYPE_DCA		0x1234	/* DCA - Multicast */
+#define	ETHERTYPE_VALID 	0x1600	/* VALID system protocol */
+#define	ETHERTYPE_DOGFIGHT	0x1989	/* Artificial Horizons ("Aviator" dogfight simulator [on Sun]) */
+#define	ETHERTYPE_RCL		0x1995	/* Datapoint Corporation (RCL lan protocol) */
+
+					/* The following 3C0x types
+					   are unregistered: */
+#define	ETHERTYPE_NBPVCD	0x3C00	/* 3Com NBP virtual circuit datagram (like XNS SPP) not registered */
+#define	ETHERTYPE_NBPSCD	0x3C01	/* 3Com NBP System control datagram not registered */
+#define	ETHERTYPE_NBPCREQ	0x3C02	/* 3Com NBP Connect request (virtual cct) not registered */
+#define	ETHERTYPE_NBPCRSP	0x3C03	/* 3Com NBP Connect response not registered */
+#define	ETHERTYPE_NBPCC		0x3C04	/* 3Com NBP Connect complete not registered */
+#define	ETHERTYPE_NBPCLREQ	0x3C05	/* 3Com NBP Close request (virtual cct) not registered */
+#define	ETHERTYPE_NBPCLRSP	0x3C06	/* 3Com NBP Close response not registered */
+#define	ETHERTYPE_NBPDG		0x3C07	/* 3Com NBP Datagram (like XNS IDP) not registered */
+#define	ETHERTYPE_NBPDGB	0x3C08	/* 3Com NBP Datagram broadcast not registered */
+#define	ETHERTYPE_NBPCLAIM	0x3C09	/* 3Com NBP Claim NetBIOS name not registered */
+#define	ETHERTYPE_NBPDLTE	0x3C0A	/* 3Com NBP Delete NetBIOS name not registered */
+#define	ETHERTYPE_NBPRAS	0x3C0B	/* 3Com NBP Remote adaptor status request not registered */
+#define	ETHERTYPE_NBPRAR	0x3C0C	/* 3Com NBP Remote adaptor response not registered */
+#define	ETHERTYPE_NBPRST	0x3C0D	/* 3Com NBP Reset not registered */
+
+#define	ETHERTYPE_PCS		0x4242	/* PCS Basic Block Protocol */
+#define	ETHERTYPE_IMLBLDIAG	0x424C	/* Information Modes Little Big LAN diagnostic */
+#define	ETHERTYPE_DIDDLE	0x4321	/* THD - Diddle */
+#define	ETHERTYPE_IMLBL		0x4C42	/* Information Modes Little Big LAN */
+#define	ETHERTYPE_SIMNET	0x5208	/* BBN Simnet Private */
+#define	ETHERTYPE_DECEXPER	0x6000	/* DEC Unassigned, experimental */
+#define	ETHERTYPE_MOPDL		0x6001	/* DEC MOP dump/load */
+#define	ETHERTYPE_MOPRC		0x6002	/* DEC MOP remote console */
+#define	ETHERTYPE_DECnet	0x6003	/* DEC DECNET Phase IV route */
+#define	ETHERTYPE_DN		ETHERTYPE_DECnet	/* libpcap, tcpdump */
+#define	ETHERTYPE_LAT		0x6004	/* DEC LAT */
+#define	ETHERTYPE_DECDIAG	0x6005	/* DEC diagnostic protocol (at interface initialization?) */
+#define	ETHERTYPE_DECCUST	0x6006	/* DEC customer protocol */
+#define	ETHERTYPE_SCA		0x6007	/* DEC LAVC, SCA */
+#define	ETHERTYPE_AMBER		0x6008	/* DEC AMBER */
+#define	ETHERTYPE_DECMUMPS	0x6009	/* DEC MUMPS */
+		    /* 0x6010 - 0x6014	   3Com Corporation */
+#define	ETHERTYPE_TRANSETHER	0x6558	/* Trans Ether Bridging (RFC1701)*/
+#define	ETHERTYPE_RAWFR		0x6559	/* Raw Frame Relay (RFC1701) */
+#define	ETHERTYPE_UBDL		0x7000	/* Ungermann-Bass download */
+#define	ETHERTYPE_UBNIU		0x7001	/* Ungermann-Bass NIUs */
+#define	ETHERTYPE_UBDIAGLOOP	0x7002	/* Ungermann-Bass diagnostic/loopback */
+#define	ETHERTYPE_UBNMC		0x7003	/* Ungermann-Bass ??? (NMC to/from UB Bridge) */
+#define	ETHERTYPE_UBBST		0x7005	/* Ungermann-Bass Bridge Spanning Tree */
+#define	ETHERTYPE_OS9		0x7007	/* OS/9 Microware */
+#define	ETHERTYPE_OS9NET	0x7009	/* OS/9 Net? */
+		    /* 0x7020 - 0x7029	   LRT (England) (now Sintrom) */
+#define	ETHERTYPE_RACAL		0x7030	/* Racal-Interlan */
+#define	ETHERTYPE_PRIMENTS	0x7031	/* Prime NTS (Network Terminal Service) */
+#define	ETHERTYPE_CABLETRON	0x7034	/* Cabletron */
+#define	ETHERTYPE_CRONUSVLN	0x8003	/* Cronus VLN */
+#define	ETHERTYPE_CRONUS	0x8004	/* Cronus Direct */
+#define	ETHERTYPE_HP		0x8005	/* HP Probe */
+#define	ETHERTYPE_NESTAR	0x8006	/* Nestar */
+#define	ETHERTYPE_ATTSTANFORD	0x8008	/* AT&T/Stanford (local use) */
+#define	ETHERTYPE_EXCELAN	0x8010	/* Excelan */
+#define	ETHERTYPE_SG_DIAG	0x8013	/* SGI diagnostic type */
+#define	ETHERTYPE_SG_NETGAMES	0x8014	/* SGI network games */
+#define	ETHERTYPE_SG_RESV	0x8015	/* SGI reserved type */
+#define	ETHERTYPE_SG_BOUNCE	0x8016	/* SGI bounce server */
+#define	ETHERTYPE_APOLLODOMAIN	0x8019	/* Apollo DOMAIN */
+#define	ETHERTYPE_TYMSHARE	0x802E	/* Tymeshare */
+#define	ETHERTYPE_TIGAN		0x802F	/* Tigan, Inc. */
+#define	ETHERTYPE_REVARP	0x8035	/* Reverse addr resolution protocol */
+#define	ETHERTYPE_AEONIC	0x8036	/* Aeonic Systems */
+#define	ETHERTYPE_IPXNEW	0x8037	/* IPX (Novell Netware?) */
+#define	ETHERTYPE_LANBRIDGE	0x8038	/* DEC LANBridge */
+#define	ETHERTYPE_DSMD	0x8039	/* DEC DSM/DDP */
+#define	ETHERTYPE_ARGONAUT	0x803A	/* DEC Argonaut Console */
+#define	ETHERTYPE_VAXELN	0x803B	/* DEC VAXELN */
+#define	ETHERTYPE_DECDNS	0x803C	/* DEC DNS Naming Service */
+#define	ETHERTYPE_ENCRYPT	0x803D	/* DEC Ethernet Encryption */
+#define	ETHERTYPE_DECDTS	0x803E	/* DEC Distributed Time Service */
+#define	ETHERTYPE_DECLTM	0x803F	/* DEC LAN Traffic Monitor */
+#define	ETHERTYPE_DECNETBIOS	0x8040	/* DEC PATHWORKS DECnet NETBIOS Emulation */
+#define	ETHERTYPE_DECLAST	0x8041	/* DEC Local Area System Transport */
+			     /* 0x8042	   DEC Unassigned */
+#define	ETHERTYPE_PLANNING	0x8044	/* Planning Research Corp. */
+		    /* 0x8046 - 0x8047	   AT&T */
+#define	ETHERTYPE_DECAM		0x8048	/* DEC Availability Manager for Distributed Systems DECamds (but someone at DEC says not) */
+#define	ETHERTYPE_EXPERDATA	0x8049	/* ExperData */
+#define	ETHERTYPE_VEXP		0x805B	/* Stanford V Kernel exp. */
+#define	ETHERTYPE_VPROD		0x805C	/* Stanford V Kernel prod. */
+#define	ETHERTYPE_ES		0x805D	/* Evans & Sutherland */
+#define	ETHERTYPE_LITTLE	0x8060	/* Little Machines */
+#define	ETHERTYPE_COUNTERPOINT	0x8062	/* Counterpoint Computers */
+		    /* 0x8065 - 0x8066	   Univ. of Mass @ Amherst */
+#define	ETHERTYPE_VEECO		0x8067	/* Veeco Integrated Auto. */
+#define	ETHERTYPE_GENDYN	0x8068	/* General Dynamics */
+#define	ETHERTYPE_ATT		0x8069	/* AT&T */
+#define	ETHERTYPE_AUTOPHON	0x806A	/* Autophon */
+#define	ETHERTYPE_COMDESIGN	0x806C	/* ComDesign */
+#define	ETHERTYPE_COMPUGRAPHIC	0x806D	/* Compugraphic Corporation */
+		    /* 0x806E - 0x8077	   Landmark Graphics Corp. */
+#define	ETHERTYPE_MATRA		0x807A	/* Matra */
+#define	ETHERTYPE_DDE		0x807B	/* Dansk Data Elektronik */
+#define	ETHERTYPE_MERIT		0x807C	/* Merit Internodal (or Univ of Michigan?) */
+		    /* 0x807D - 0x807F	   Vitalink Communications */
+#define	ETHERTYPE_VLTLMAN	0x8080	/* Vitalink TransLAN III Management */
+		    /* 0x8081 - 0x8083	   Counterpoint Computers */
+		    /* 0x8088 - 0x808A	   Xyplex */
+#define	ETHERTYPE_ATALK		0x809B	/* AppleTalk */
+#define	ETHERTYPE_AT		ETHERTYPE_ATALK		/* old NetBSD */
+#define	ETHERTYPE_APPLETALK	ETHERTYPE_ATALK		/* HP-UX */
+		    /* 0x809C - 0x809E	   Datability */
+#define	ETHERTYPE_SPIDER	0x809F	/* Spider Systems Ltd. */
+			     /* 0x80A3	   Nixdorf */
+		    /* 0x80A4 - 0x80B3	   Siemens Gammasonics Inc. */
+		    /* 0x80C0 - 0x80C3	   DCA (Digital Comm. Assoc.) Data Exchange Cluster */
+		    /* 0x80C4 - 0x80C5	   Banyan Systems */
+#define	ETHERTYPE_PACER		0x80C6	/* Pacer Software */
+#define	ETHERTYPE_APPLITEK	0x80C7	/* Applitek Corporation */
+		    /* 0x80C8 - 0x80CC	   Intergraph Corporation */
+		    /* 0x80CD - 0x80CE	   Harris Corporation */
+		    /* 0x80CF - 0x80D2	   Taylor Instrument */
+		    /* 0x80D3 - 0x80D4	   Rosemount Corporation */
+#define	ETHERTYPE_SNA		0x80D5	/* IBM SNA Services over Ethernet */
+#define	ETHERTYPE_VARIAN	0x80DD	/* Varian Associates */
+		    /* 0x80DE - 0x80DF	   TRFS (Integrated Solutions Transparent Remote File System) */
+		    /* 0x80E0 - 0x80E3	   Allen-Bradley */
+		    /* 0x80E4 - 0x80F0	   Datability */
+#define	ETHERTYPE_RETIX		0x80F2	/* Retix */
+#define	ETHERTYPE_AARP		0x80F3	/* AppleTalk AARP */
+		    /* 0x80F4 - 0x80F5	   Kinetics */
+#define	ETHERTYPE_APOLLO	0x80F7	/* Apollo Computer */
+#define ETHERTYPE_VLAN		0x8100	/* IEEE 802.1Q VLAN tagging (XXX conflicts) */
+		    /* 0x80FF - 0x8101	   Wellfleet Communications (XXX conflicts) */
+#define	ETHERTYPE_BOFL		0x8102	/* Wellfleet; BOFL (Breath OF Life) pkts [every 5-10 secs.] */
+#define	ETHERTYPE_WELLFLEET	0x8103	/* Wellfleet Communications */
+		    /* 0x8107 - 0x8109	   Symbolics Private */
+#define	ETHERTYPE_TALARIS	0x812B	/* Talaris */
+#define	ETHERTYPE_WATERLOO	0x8130	/* Waterloo Microsystems Inc. (XXX which?) */
+#define	ETHERTYPE_HAYES		0x8130	/* Hayes Microcomputers (XXX which?) */
+#define	ETHERTYPE_VGLAB		0x8131	/* VG Laboratory Systems */
+		    /* 0x8132 - 0x8137	   Bridge Communications */
+#define	ETHERTYPE_IPX		0x8137	/* Novell (old) NetWare IPX (ECONFIG E option) */
+#define	ETHERTYPE_NOVELL	0x8138	/* Novell, Inc. */
+		    /* 0x8139 - 0x813D	   KTI */
+#define	ETHERTYPE_MUMPS		0x813F	/* M/MUMPS data sharing */
+#define	ETHERTYPE_AMOEBA	0x8145	/* Vrije Universiteit (NL) Amoeba 4 RPC (obsolete) */
+#define	ETHERTYPE_FLIP		0x8146	/* Vrije Universiteit (NL) FLIP (Fast Local Internet Protocol) */
+#define	ETHERTYPE_VURESERVED	0x8147	/* Vrije Universiteit (NL) [reserved] */
+#define	ETHERTYPE_LOGICRAFT	0x8148	/* Logicraft */
+#define	ETHERTYPE_NCD		0x8149	/* Network Computing Devices */
+#define	ETHERTYPE_ALPHA		0x814A	/* Alpha Micro */
+#define	ETHERTYPE_SNMP		0x814C	/* SNMP over Ethernet (see RFC1089) */
+		    /* 0x814D - 0x814E	   BIIN */
+#define	ETHERTYPE_TEC	0x814F	/* Technically Elite Concepts */
+#define	ETHERTYPE_RATIONAL	0x8150	/* Rational Corp */
+		    /* 0x8151 - 0x8153	   Qualcomm */
+		    /* 0x815C - 0x815E	   Computer Protocol Pty Ltd */
+		    /* 0x8164 - 0x8166	   Charles River Data Systems */
+#define	ETHERTYPE_XTP		0x817D	/* Protocol Engines XTP */
+#define	ETHERTYPE_SGITW		0x817E	/* SGI/Time Warner prop. */
+#define	ETHERTYPE_HIPPI_FP	0x8180	/* HIPPI-FP encapsulation */
+#define	ETHERTYPE_STP		0x8181	/* Scheduled Transfer STP, HIPPI-ST */
+		    /* 0x8182 - 0x8183	   Reserved for HIPPI-6400 */
+		    /* 0x8184 - 0x818C	   SGI prop. */
+#define	ETHERTYPE_MOTOROLA	0x818D	/* Motorola */
+#define	ETHERTYPE_NETBEUI	0x8191	/* PowerLAN NetBIOS/NetBEUI (PC) */
+		    /* 0x819A - 0x81A3	   RAD Network Devices */
+		    /* 0x81B7 - 0x81B9	   Xyplex */
+		    /* 0x81CC - 0x81D5	   Apricot Computers */
+		    /* 0x81D6 - 0x81DD	   Artisoft Lantastic */
+		    /* 0x81E6 - 0x81EF	   Polygon */
+		    /* 0x81F0 - 0x81F2	   Comsat Labs */
+		    /* 0x81F3 - 0x81F5	   SAIC */
+		    /* 0x81F6 - 0x81F8	   VG Analytical */
+		    /* 0x8203 - 0x8205	   QNX Software Systems Ltd. */
+		    /* 0x8221 - 0x8222	   Ascom Banking Systems */
+		    /* 0x823E - 0x8240	   Advanced Encryption Systems */
+		    /* 0x8263 - 0x826A	   Charles River Data Systems */
+		    /* 0x827F - 0x8282	   Athena Programming */
+		    /* 0x829A - 0x829B	   Inst Ind Info Tech */
+		    /* 0x829C - 0x82AB	   Taurus Controls */
+		    /* 0x82AC - 0x8693	   Walker Richer & Quinn */
+#define	ETHERTYPE_ACCTON	0x8390	/* Accton Technologies (unregistered) */
+#define	ETHERTYPE_TALARISMC	0x852B	/* Talaris multicast */
+#define	ETHERTYPE_KALPANA	0x8582	/* Kalpana */
+		    /* 0x8694 - 0x869D	   Idea Courier */
+		    /* 0x869E - 0x86A1	   Computer Network Tech */
+		    /* 0x86A3 - 0x86AC	   Gateway Communications */
+#define	ETHERTYPE_SECTRA	0x86DB	/* SECTRA */
+#define	ETHERTYPE_IPV6		0x86DD	/* IP protocol version 6 */
+#define	ETHERTYPE_DELTACON	0x86DE	/* Delta Controls */
+#define	ETHERTYPE_ATOMIC	0x86DF	/* ATOMIC */
+		    /* 0x86E0 - 0x86EF	   Landis & Gyr Powers */
+		    /* 0x8700 - 0x8710	   Motorola */
+#define	ETHERTYPE_RDP		0x8739	/* Control Technology Inc. RDP Without IP */
+#define	ETHERTYPE_MICP		0x873A	/* Control Technology Inc. Mcast Industrial Ctrl Proto. */
+		    /* 0x873B - 0x873C	   Control Technology Inc. Proprietary */
+#define	ETHERTYPE_TCPCOMP	0x876B	/* TCP/IP Compression (RFC1701) */
+#define	ETHERTYPE_IPAS		0x876C	/* IP Autonomous Systems (RFC1701) */
+#define	ETHERTYPE_SECUREDATA	0x876D	/* Secure Data (RFC1701) */
+#define	ETHERTYPE_FLOWCONTROL	0x8808	/* 802.3x flow control packet */
+#define	ETHERTYPE_SLOW		0x8809	/* 802.3ad link aggregation (LACP) */
+#define	ETHERTYPE_PPP		0x880B	/* PPP (obsolete by PPPoE) */
+#define	ETHERTYPE_HITACHI	0x8820	/* Hitachi Cable (Optoelectronic Systems Laboratory) */
+#define	ETHERTYPE_MPLS		0x8847	/* MPLS Unicast */
+#define	ETHERTYPE_MPLS_MCAST	0x8848	/* MPLS Multicast */
+#define	ETHERTYPE_AXIS		0x8856	/* Axis Communications AB proprietary bootstrap/config */
+#define	ETHERTYPE_PPPOEDISC	0x8863	/* PPP Over Ethernet Discovery Stage */
+#define	ETHERTYPE_PPPOE		0x8864	/* PPP Over Ethernet Session Stage */
+#define	ETHERTYPE_LANPROBE	0x8888	/* HP LanProbe test? */
+#define	ETHERTYPE_PAE		0x888e	/* EAPOL PAE/802.1x */
+#define	ETHERTYPE_LOOPBACK	0x9000	/* Loopback: used to test interfaces */
+#define	ETHERTYPE_LBACK		ETHERTYPE_LOOPBACK	/* DEC MOP loopback */
+#define	ETHERTYPE_XNSSM		0x9001	/* 3Com (Formerly Bridge Communications), XNS Systems Management */
+#define	ETHERTYPE_TCPSM		0x9002	/* 3Com (Formerly Bridge Communications), TCP/IP Systems Management */
+#define	ETHERTYPE_BCLOOP	0x9003	/* 3Com (Formerly Bridge Communications), loopback detection */
+#define	ETHERTYPE_DEBNI		0xAAAA	/* DECNET? Used by VAX 6220 DEBNI */
+#define	ETHERTYPE_SONIX		0xFAF5	/* Sonix Arpeggio */
+#define	ETHERTYPE_VITAL		0xFF00	/* BBN VITAL-LanBridge cache wakeups */
+		    /* 0xFF00 - 0xFFOF	   ISC Bunker Ramo */
+
+#define	ETHERTYPE_MAX		0xFFFF	/* Maximum valid ethernet type, reserved */
+
+/*
+ * The ETHERTYPE_NTRAILER packet types starting at ETHERTYPE_TRAIL have
+ * (type-ETHERTYPE_TRAIL)*512 bytes of data followed
+ * by an ETHER type (as given above) and then the (variable-length) header.
+ */
+#define	ETHERTYPE_TRAIL		0x1000		/* Trailer packet */
+#define	ETHERTYPE_NTRAILER	16
+
+#define	ETHERMTU	(ETHER_MAX_LEN-ETHER_HDR_LEN-ETHER_CRC_LEN)
+#define	ETHERMIN	(ETHER_MIN_LEN-ETHER_HDR_LEN-ETHER_CRC_LEN)
+#define	ETHERMTU_JUMBO	(ETHER_MAX_LEN_JUMBO - ETHER_HDR_LEN - ETHER_CRC_LEN)
+/*
+ * The ETHER_BPF_MTAP macro should be used by drivers which support hardware
+ * offload for VLAN tag processing.  It will check the mbuf to see if it has
+ * M_VLANTAG set, and if it does, will pass the packet along to
+ * ether_vlan_mtap.  This function will re-insert VLAN tags for the duration
+ * of the tap, so they show up properly for network analyzers.
+ */
+#define ETHER_BPF_MTAP(_ifp, _m) do {					\
+	if (bpf_peers_present((_ifp)->if_bpf)) {			\
+		M_ASSERTVALID(_m);					\
+		if (((_m)->m_flags & M_VLANTAG) != 0)			\
+			ether_vlan_mtap((_ifp)->if_bpf, (_m), NULL, 0);	\
+		else							\
+			bpf_mtap((_ifp)->if_bpf, (_m));			\
+	}								\
+} while (0)
+
+#ifdef _KERNEL
+
+struct ifnet;
+struct mbuf;
+struct route;
+struct sockaddr;
+struct bpf_if;
+
+extern	uint32_t ether_crc32_le(const uint8_t *, size_t);
+extern	uint32_t ether_crc32_be(const uint8_t *, size_t);
+extern	void ether_demux(struct ifnet *, struct mbuf *);
+extern	void ether_ifattach(struct ifnet *, const u_int8_t *);
+extern	void ether_ifdetach(struct ifnet *);
+extern	int  ether_ioctl(struct ifnet *, u_long, caddr_t);
+extern	int  ether_output(struct ifnet *,
+		   struct mbuf *, struct sockaddr *, struct route *);
+extern	int  ether_output_frame(struct ifnet *, struct mbuf *);
+extern	char *ether_sprintf(const u_int8_t *);
+void	ether_vlan_mtap(struct bpf_if *, struct mbuf *,
+	    void *, u_int);
+struct mbuf  *ether_vlanencap(struct mbuf *, uint16_t);
+
+#else /* _KERNEL */
+
+#include <sys/cdefs.h>
+
+/*
+ * Ethernet address conversion/parsing routines.
+ */
+__BEGIN_DECLS
+struct	ether_addr *ether_aton(const char *);
+struct	ether_addr *ether_aton_r(const char *, struct ether_addr *);
+int	ether_hostton(const char *, struct ether_addr *);
+int	ether_line(const char *, struct ether_addr *, char *);
+char 	*ether_ntoa(const struct ether_addr *);
+char 	*ether_ntoa_r(const struct ether_addr *, char *);
+int	ether_ntohost(char *, const struct ether_addr *);
+__END_DECLS
+
+#endif /* !_KERNEL */
+
+#endif /* !_NET_ETHERNET_H_ */
diff --git a/freebsd-userspace/sys/sys/syslog.h b/freebsd-userspace/sys/sys/syslog.h
new file mode 100644
index 0000000..6f12831
--- /dev/null
+++ b/freebsd-userspace/sys/sys/syslog.h
@@ -0,0 +1,203 @@
+/*-
+ * Copyright (c) 1982, 1986, 1988, 1993
+ *	The Regents of the University of California.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON 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 ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ *	@(#)syslog.h	8.1 (Berkeley) 6/2/93
+ * $FreeBSD$
+ */
+
+#ifndef _SYS_SYSLOG_H_
+#define _SYS_SYSLOG_H_
+
+#define	_PATH_LOG	"/var/run/log"
+#define	_PATH_LOG_PRIV	"/var/run/logpriv"
+#define	_PATH_OLDLOG	"/dev/log"	/* backward compatibility */
+
+/*
+ * priorities/facilities are encoded into a single 32-bit quantity, where the
+ * bottom 3 bits are the priority (0-7) and the top 28 bits are the facility
+ * (0-big number).  Both the priorities and the facilities map roughly
+ * one-to-one to strings in the syslogd(8) source code.  This mapping is
+ * included in this file.
+ *
+ * priorities (these are ordered)
+ */
+#define	LOG_EMERG	0	/* system is unusable */
+#define	LOG_ALERT	1	/* action must be taken immediately */
+#define	LOG_CRIT	2	/* critical conditions */
+#define	LOG_ERR		3	/* error conditions */
+#define	LOG_WARNING	4	/* warning conditions */
+#define	LOG_NOTICE	5	/* normal but significant condition */
+#define	LOG_INFO	6	/* informational */
+#define	LOG_DEBUG	7	/* debug-level messages */
+
+#define	LOG_PRIMASK	0x07	/* mask to extract priority part (internal) */
+				/* extract priority */
+#define	LOG_PRI(p)	((p) & LOG_PRIMASK)
+#define	LOG_MAKEPRI(fac, pri)	((fac) | (pri))
+
+#ifdef SYSLOG_NAMES
+#define	INTERNAL_NOPRI	0x10	/* the "no priority" priority */
+				/* mark "facility" */
+#define	INTERNAL_MARK	LOG_MAKEPRI((LOG_NFACILITIES<<3), 0)
+typedef struct _code {
+	const char	*c_name;
+	int		c_val;
+} CODE;
+
+CODE prioritynames[] = {
+	{ "alert",	LOG_ALERT,	},
+	{ "crit",	LOG_CRIT,	},
+	{ "debug",	LOG_DEBUG,	},
+	{ "emerg",	LOG_EMERG,	},
+	{ "err",	LOG_ERR,	},
+	{ "error",	LOG_ERR,	},	/* DEPRECATED */
+	{ "info",	LOG_INFO,	},
+	{ "none",	INTERNAL_NOPRI,	},	/* INTERNAL */
+	{ "notice",	LOG_NOTICE,	},
+	{ "panic", 	LOG_EMERG,	},	/* DEPRECATED */
+	{ "warn",	LOG_WARNING,	},	/* DEPRECATED */
+	{ "warning",	LOG_WARNING,	},
+	{ NULL,		-1,		}
+};
+#endif
+
+/* facility codes */
+#define	LOG_KERN	(0<<3)	/* kernel messages */
+#define	LOG_USER	(1<<3)	/* random user-level messages */
+#define	LOG_MAIL	(2<<3)	/* mail system */
+#define	LOG_DAEMON	(3<<3)	/* system daemons */
+#define	LOG_AUTH	(4<<3)	/* authorization messages */
+#define	LOG_SYSLOG	(5<<3)	/* messages generated internally by syslogd */
+#define	LOG_LPR		(6<<3)	/* line printer subsystem */
+#define	LOG_NEWS	(7<<3)	/* network news subsystem */
+#define	LOG_UUCP	(8<<3)	/* UUCP subsystem */
+#define	LOG_CRON	(9<<3)	/* clock daemon */
+#define	LOG_AUTHPRIV	(10<<3)	/* authorization messages (private) */
+				/* Facility #10 clashes in DEC UNIX, where */
+				/* it's defined as LOG_MEGASAFE for AdvFS  */
+				/* event logging.                          */
+#define	LOG_FTP		(11<<3)	/* ftp daemon */
+#define	LOG_NTP		(12<<3)	/* NTP subsystem */
+#define	LOG_SECURITY	(13<<3) /* security subsystems (firewalling, etc.) */
+#define	LOG_CONSOLE	(14<<3) /* /dev/console output */
+
+	/* other codes through 15 reserved for system use */
+#define	LOG_LOCAL0	(16<<3)	/* reserved for local use */
+#define	LOG_LOCAL1	(17<<3)	/* reserved for local use */
+#define	LOG_LOCAL2	(18<<3)	/* reserved for local use */
+#define	LOG_LOCAL3	(19<<3)	/* reserved for local use */
+#define	LOG_LOCAL4	(20<<3)	/* reserved for local use */
+#define	LOG_LOCAL5	(21<<3)	/* reserved for local use */
+#define	LOG_LOCAL6	(22<<3)	/* reserved for local use */
+#define	LOG_LOCAL7	(23<<3)	/* reserved for local use */
+
+#define	LOG_NFACILITIES	24	/* current number of facilities */
+#define	LOG_FACMASK	0x03f8	/* mask to extract facility part */
+				/* facility of pri */
+#define	LOG_FAC(p)	(((p) & LOG_FACMASK) >> 3)
+
+#ifdef SYSLOG_NAMES
+CODE facilitynames[] = {
+	{ "auth",	LOG_AUTH,	},
+	{ "authpriv",	LOG_AUTHPRIV,	},
+	{ "console", 	LOG_CONSOLE,	},
+	{ "cron", 	LOG_CRON,	},
+	{ "daemon",	LOG_DAEMON,	},
+	{ "ftp",	LOG_FTP,	},
+	{ "kern",	LOG_KERN,	},
+	{ "lpr",	LOG_LPR,	},
+	{ "mail",	LOG_MAIL,	},
+	{ "mark", 	INTERNAL_MARK,	},	/* INTERNAL */
+	{ "news",	LOG_NEWS,	},
+	{ "ntp",	LOG_NTP,	},
+	{ "security",	LOG_SECURITY,	},
+	{ "syslog",	LOG_SYSLOG,	},
+	{ "user",	LOG_USER,	},
+	{ "uucp",	LOG_UUCP,	},
+	{ "local0",	LOG_LOCAL0,	},
+	{ "local1",	LOG_LOCAL1,	},
+	{ "local2",	LOG_LOCAL2,	},
+	{ "local3",	LOG_LOCAL3,	},
+	{ "local4",	LOG_LOCAL4,	},
+	{ "local5",	LOG_LOCAL5,	},
+	{ "local6",	LOG_LOCAL6,	},
+	{ "local7",	LOG_LOCAL7,	},
+	{ NULL,		-1,		}
+};
+#endif
+
+#ifdef _KERNEL
+#define	LOG_PRINTF	-1	/* pseudo-priority to indicate use of printf */
+#endif
+
+/*
+ * arguments to setlogmask.
+ */
+#define	LOG_MASK(pri)	(1 << (pri))		/* mask for one priority */
+#define	LOG_UPTO(pri)	((1 << ((pri)+1)) - 1)	/* all priorities through pri */
+
+/*
+ * Option flags for openlog.
+ *
+ * LOG_ODELAY no longer does anything.
+ * LOG_NDELAY is the inverse of what it used to be.
+ */
+#define	LOG_PID		0x01	/* log the pid with each message */
+#define	LOG_CONS	0x02	/* log on the console if errors in sending */
+#define	LOG_ODELAY	0x04	/* delay open until first syslog() (default) */
+#define	LOG_NDELAY	0x08	/* don't delay open */
+#define	LOG_NOWAIT	0x10	/* don't wait for console forks: DEPRECATED */
+#define	LOG_PERROR	0x20	/* log to stderr as well */
+
+#ifdef _KERNEL
+
+#else /* not _KERNEL */
+
+/*
+ * Don't use va_list in the vsyslog() prototype.   Va_list is typedef'd in two
+ * places (<machine/varargs.h> and <machine/stdarg.h>), so if we include one
+ * of them here we may collide with the utility's includes.  It's unreasonable
+ * for utilities to have to include one of them to include syslog.h, so we get
+ * __va_list from <sys/_types.h> and use it.
+ */
+#include <sys/cdefs.h>
+#include <sys/_types.h>
+
+__BEGIN_DECLS
+void	closelog(void);
+void	openlog(const char *, int, int);
+int	setlogmask(int);
+void	syslog(int, const char *, ...) __printflike(2, 3);
+#if __BSD_VISIBLE
+void	vsyslog(int, const char *, __va_list) __printflike(2, 0);
+#endif
+__END_DECLS
+
+#endif /* !_KERNEL */
+
+#endif




More information about the vc mailing list