[gnet-dev] [PATCH] Cygwin support added



Hi there,

the attached patch introduces replacement functions for inet_ntop and
inet_pton, taken from the ircii project, and published under a BSD license without an advertising clause.

These replacement functions are needed on cygwin (and apparently on hpux?).


Gerrit
--
=^..^=
diff -purN -x .build -x .inst -x .sinst gnet-2.0.5-orig/config.h.in gnet-2.0.5/config.h.in
--- gnet-2.0.5-orig/config.h.in	2003-10-28 00:51:40.000000000 +0100
+++ gnet-2.0.5/config.h.in	2004-10-10 01:08:03.254504000 +0200
@@ -6,6 +6,9 @@
 /* GNet binary age */
 #undef GNET_BINARY_AGE
 
+/* Define when compiling on Cygwin */
+#undef GNET_CYGWIN
+
 /* GNet interface age */
 #undef GNET_INTERFACE_AGE
 
@@ -21,6 +24,21 @@
 /* Define when compiling on Windows */
 #undef GNET_WIN32
 
+/* Define to 1 if you have the <arpa/inet.h> header file. */
+#undef HAVE_ARPA_INET_H
+
+/* Define to 1 if you have the <arpa/nameser.h> header file. */
+#undef HAVE_ARPA_NAMESER_H
+
+/* Define to 1 if you have the `atexit' function. */
+#undef HAVE_ATEXIT
+
+/* Define to 1 if you have the <dlfcn.h> header file. */
+#undef HAVE_DLFCN_H
+
+/* Define to 1 if you have the <fcntl.h> header file. */
+#undef HAVE_FCNTL_H
+
 /* Define if getaddrinfo() is available */
 #undef HAVE_GETADDRINFO
 
@@ -30,6 +48,12 @@
 /* Define if getaddrinfo is threadsafe */
 #undef HAVE_GETADDRINFO_THREADSAFE
 
+/* Define to 1 if you have the `gethostbyaddr' function. */
+#undef HAVE_GETHOSTBYADDR
+
+/* Define to 1 if you have the `gethostbyname' function. */
+#undef HAVE_GETHOSTBYNAME
+
 /* Define if glibc variant of gethostbyname() is available */
 #undef HAVE_GETHOSTBYNAME_R_GLIBC
 
@@ -45,12 +69,24 @@
 /* Define if gethostbyname is threadsafe */
 #undef HAVE_GETHOSTBYNAME_THREADSAFE
 
+/* Define to 1 if you have the `gethostname' function. */
+#undef HAVE_GETHOSTNAME
+
 /* Define if getifaddrs() is available */
 #undef HAVE_GETIFADDRS
 
 /* Define to 1 if you have the <ifaddrs.h> header file. */
 #undef HAVE_IFADDRS_H
 
+/* Define to 1 if you have the `inet_ntoa' function. */
+#undef HAVE_INET_NTOA
+
+/* Define to 1 if you have the `inet_ntop' function. */
+#undef HAVE_INET_NTOP
+
+/* Define to 1 if you have the `inet_pton' function. */
+#undef HAVE_INET_PTON
+
 /* Define to 1 if you have the <inttypes.h> header file. */
 #undef HAVE_INTTYPES_H
 
@@ -72,39 +108,84 @@
 /* Define if linux/netlink.h header exists */
 #undef HAVE_LINUX_NETLINK_H
 
+/* Define to 1 if you have the <malloc.h> header file. */
+#undef HAVE_MALLOC_H
+
 /* Define to 1 if you have the <memory.h> header file. */
 #undef HAVE_MEMORY_H
 
+/* Define to 1 if you have the `memset' function. */
+#undef HAVE_MEMSET
+
+/* Define to 1 if you have the <netdb.h> header file. */
+#undef HAVE_NETDB_H
+
+/* Define to 1 if you have the <netinet/in.h> header file. */
+#undef HAVE_NETINET_IN_H
+
+/* Define to 1 if you have the `select' function. */
+#undef HAVE_SELECT
+
 /* Define if struct sockaddr has sa_len field */
 #undef HAVE_SOCKADDR_SA_LEN
 
 /* Whether you have struct sockaddr_storage */
 #undef HAVE_SOCKADDR_STORAGE
 
+/* Define to 1 if you have the `socket' function. */
+#undef HAVE_SOCKET
+
+/* Define to 1 if you have the <stddef.h> header file. */
+#undef HAVE_STDDEF_H
+
 /* Define to 1 if you have the <stdint.h> header file. */
 #undef HAVE_STDINT_H
 
 /* Define to 1 if you have the <stdlib.h> header file. */
 #undef HAVE_STDLIB_H
 
+/* Define to 1 if you have the `strcasecmp' function. */
+#undef HAVE_STRCASECMP
+
+/* Define to 1 if you have the `strchr' function. */
+#undef HAVE_STRCHR
+
 /* Define to 1 if you have the <strings.h> header file. */
 #undef HAVE_STRINGS_H
 
 /* Define to 1 if you have the <string.h> header file. */
 #undef HAVE_STRING_H
 
+/* Define to 1 if you have the `strtoul' function. */
+#undef HAVE_STRTOUL
+
+/* Define to 1 if you have the <sys/ioctl.h> header file. */
+#undef HAVE_SYS_IOCTL_H
+
 /* Define to 1 if you have the <sys/param.h> header file. */
 #undef HAVE_SYS_PARAM_H
 
+/* Define to 1 if you have the <sys/socket.h> header file. */
+#undef HAVE_SYS_SOCKET_H
+
 /* Define to 1 if you have the <sys/sockio.h> header file. */
 #undef HAVE_SYS_SOCKIO_H
 
 /* Define to 1 if you have the <sys/stat.h> header file. */
 #undef HAVE_SYS_STAT_H
 
+/* Define to 1 if you have the <sys/time.h> header file. */
+#undef HAVE_SYS_TIME_H
+
 /* Define to 1 if you have the <sys/types.h> header file. */
 #undef HAVE_SYS_TYPES_H
 
+/* Define to 1 if you have <sys/wait.h> that is POSIX.1 compatible. */
+#undef HAVE_SYS_WAIT_H
+
+/* Define to 1 if you have the `uname' function. */
+#undef HAVE_UNAME
+
 /* Define to 1 if you have the <unistd.h> header file. */
 #undef HAVE_UNISTD_H
 
@@ -126,6 +207,9 @@
 /* Define to the version of this package. */
 #undef PACKAGE_VERSION
 
+/* Define as the return type of signal handlers (`int' or `void'). */
+#undef RETSIGTYPE
+
 /* The size of a `long', as computed by sizeof. */
 #undef SIZEOF_LONG
 
@@ -135,8 +219,17 @@
 /* Define to 1 if you have the ANSI C header files. */
 #undef STDC_HEADERS
 
+/* Define to 1 if you can safely include both <sys/time.h> and <time.h>. */
+#undef TIME_WITH_SYS_TIME
+
 /* Version number of package */
 #undef VERSION
 
 /* Define to empty if `const' does not conform to ANSI C. */
 #undef const
+
+/* Define to `int' if <sys/types.h> does not define. */
+#undef pid_t
+
+/* Define to `unsigned' if <sys/types.h> does not define. */
+#undef size_t
diff -purN -x .build -x .inst -x .sinst gnet-2.0.5-orig/configure.ac gnet-2.0.5/configure.ac
--- gnet-2.0.5-orig/configure.ac	2004-03-02 01:10:49.000000000 +0100
+++ gnet-2.0.5/configure.ac	2004-10-10 01:05:36.463428800 +0200
@@ -66,6 +66,20 @@ esac
 AC_MSG_RESULT([$gnet_native_win32])
 AM_CONDITIONAL(OS_WIN32, [test "$gnet_native_win32" = "yes"])
 
+AC_MSG_CHECKING([for Cygwin])
+case "$host" in
+  *-*-cygwin*)
+    gnet_native_cygwin=yes
+    # Why not use G_OS_CYGWIN?
+    AC_DEFINE_UNQUOTED(GNET_CYGWIN, 1, [Define when compiling on Cygwin])
+    ;;
+  *)
+    gnet_native_cygwin=no
+    ;;
+esac
+AC_MSG_RESULT([$gnet_native_cygwin])
+AM_CONDITIONAL(OS_CYGWIN, [test "$gnet_native_cygwin" = "yes"])
+
 ####################
 # Glib1
 
@@ -126,6 +140,7 @@ fi
 # Check for programs
 AC_PROG_CC
 AM_PROG_CC_STDC
+AC_PROG_GCC_TRADITIONAL dnl wanted by: src/inetaddr.c:3537
 AC_PROG_INSTALL
 AC_PROG_MAKE_SET
 AC_CHECK_PROG(PKGCONFIG, [pkg-config], [yes])
@@ -266,8 +281,9 @@ AC_TRY_LINK([ #include <sys/types.h>
 ###############################
 # Check for headers
 AC_HEADER_STDC
-AC_CHECK_HEADERS([sys/sockio.h sys/param.h ifaddrs.h])
-
+AC_HEADER_SYS_WAIT dnl wanted by: src/gnet-private.h:61
+AC_HEADER_TIME dnl wanted by: src/tcp.c:1109
+AC_CHECK_HEADERS([arpa/inet.h arpa/nameser.h fcntl.h ifaddrs.h malloc.h memory.h netdb.h netinet/in.h stddef.h stdlib.h string.h sys/ioctl.h sys/param.h sys/socket.h sys/sockio.h sys/time.h unistd.h])
 
 AC_MSG_CHECKING([for linux/netlink.h])
 AC_TRY_CPP([#include <linux/netlink.h>],
@@ -283,6 +299,9 @@ AC_TRY_CPP([#include <linux/netlink.h>],
 
 ###############################
 # Check for types
+AC_TYPE_PID_T dnl wanted by: src/gnet-private.h:300
+AC_TYPE_SIGNAL dnl wanted by: examples/echoserver-async.c:83
+AC_TYPE_SIZE_T dnl wanted by: src/inetaddr.c:41
 
 
 ###############################
@@ -316,6 +335,19 @@ CFLAGS="$CFLAGS -D_REENTRANT -DGNET_EXPE
 ###############################
 # Check for library functions
 
+AC_CHECK_FUNCS([atexit]) dnl wanted by: src/gnet.c:110
+AC_CHECK_FUNCS([gethostbyaddr]) dnl wanted by: src/inetaddr.c:514
+AC_CHECK_FUNCS([gethostbyname]) dnl wanted by: src/inetaddr.c:262
+AC_CHECK_FUNCS([gethostname]) dnl wanted by: src/inetaddr.c:3084
+AC_CHECK_FUNCS([inet_ntoa]) dnl wanted by: src/inetaddr.c:63
+AC_CHECK_FUNCS([memset]) dnl wanted by: src/gnet-private.c:79
+AC_CHECK_FUNCS([select]) dnl wanted by: src/tcp.c:1049
+AC_CHECK_FUNCS([socket]) dnl wanted by: src/gnet-private.c:99
+AC_CHECK_FUNCS([strcasecmp]) dnl wanted by: tests/inetaddr_test.c:137
+AC_CHECK_FUNCS([strchr]) dnl wanted by: src/gnet.c:138
+AC_CHECK_FUNCS([strtoul]) dnl wanted by: src/socks.c:100
+AC_CHECK_FUNCS([uname]) dnl wanted by: src/inetaddr.c:3055
+
 # Most of this code checks for variants of gethostbyname().  For
 # simplicity and curiosity, we check for all variants, though only one
 # working variant is needed.
@@ -324,7 +356,6 @@ CFLAGS="$CFLAGS -D_REENTRANT -DGNET_EXPE
 AC_CHECK_FUNC(getifaddrs, AC_DEFINE(HAVE_GETIFADDRS, 1, 
     [Define if getifaddrs() is available]))
 
-
 # The user may be able to tell us if a function is thread-safe.  We
 # know of no good way to test this programaticly.
 
@@ -472,6 +503,8 @@ it is thread-safe or not.
 ])
 fi
 
+AC_REPLACE_FUNCS(inet_pton inet_ntop)
+
 
 # If the compiler is gcc, turn on as many warnings as possible
 if test "x$GCC" = "xyes"; then
@@ -521,6 +554,7 @@ AM_CONFIG_HEADER(config.h)
 AC_CONFIG_FILES([
 	Makefile
 	src/Makefile
+	src/replacements/Makefile
 	examples/Makefile
 	tests/Makefile
 	doc/Makefile
diff -purN -x .build -x .inst -x .sinst gnet-2.0.5-orig/src/Makefile.am gnet-2.0.5/src/Makefile.am
--- gnet-2.0.5-orig/src/Makefile.am	2003-10-28 00:47:53.000000000 +0100
+++ gnet-2.0.5/src/Makefile.am	2004-10-10 00:00:03.470737600 +0200
@@ -1,9 +1,14 @@
 ## Process this file with automake to produce Makefile.in
 
+SUBDIRS = replacements
+
 if OS_WIN32
 no_undefined = -no-undefined
 lws2_32 = -lws2_32
 endif
+if OS_CYGWIN
+no_undefined = -no-undefined
+endif
 
 @SET_MAKE@
 
@@ -19,6 +24,8 @@ libgnet_2_0_la_LDFLAGS = \
 	-version-info $(LT_CURRENT):$(LT_REVISION):$(LT_AGE) \
 	$(GLIB_LIBS) $(GTHREAD_LIBS) $(lws2_32)
 
+libgnet_2_0_la_LIBADD = replacements/libreplacements.la
+
 libgnet_2_0_la_SOURCES = 	\
 	gnet.c			\
 	gnet-private.c		\
diff -purN -x .build -x .inst -x .sinst gnet-2.0.5-orig/src/replacements/Makefile.am gnet-2.0.5/src/replacements/Makefile.am
--- gnet-2.0.5-orig/src/replacements/Makefile.am	1970-01-01 01:00:00.000000000 +0100
+++ gnet-2.0.5/src/replacements/Makefile.am	2004-10-09 23:58:14.578331200 +0200
@@ -0,0 +1,5 @@
+noinst_LTLIBRARIES=		libreplacements.la
+libreplacements_la_SOURCES=	
+libreplacements_la_LIBADD=	@LTLIBOBJS@
+INCLUDES=			-I$(top_srcdir)/include
+EXTRA_DIST=			inet_ntop.c inet_pton.c 
diff -purN -x .build -x .inst -x .sinst gnet-2.0.5-orig/src/replacements/inet_ntop.c gnet-2.0.5/src/replacements/inet_ntop.c
--- gnet-2.0.5-orig/src/replacements/inet_ntop.c	1970-01-01 01:00:00.000000000 +0100
+++ gnet-2.0.5/src/replacements/inet_ntop.c	2004-10-09 23:56:46.201251200 +0200
@@ -0,0 +1,205 @@
+/*	Id: inet_ntop.c,v 1.4 2001/04/17 07:53:47 lukem Exp 	*/
+/*	$NetBSD: inet_ntop.c,v 1.9 2000/01/22 22:19:16 mycroft Exp $	*/
+
+/* Copyright (c) 1996 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.
+ */
+
+#include "config.h"
+
+#ifndef HAVE_INET_NTOP
+
+#include <arpa/inet.h>
+#include <ctype.h>
+#include <errno.h>
+#include <string.h>
+#include <sys/socket.h>
+#include <stdio.h>
+
+#if HAVE_ARPA_NAMESER_H
+#include <arpa/nameser.h>
+#endif
+
+#ifndef IN6ADDRSZ
+#define	IN6ADDRSZ	16
+#endif
+
+#ifndef INT16SZ
+#define	INT16SZ		2
+#endif
+
+#ifdef SPRINTF_CHAR
+# define SPRINTF(x) strlen(sprintf/**/x)
+#else
+# define SPRINTF(x) ((size_t)sprintf x)
+#endif
+
+/*
+ * 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, size_t size);
+#ifdef INET6
+static const char *inet_ntop6 (const u_char *src, char *dst, size_t size);
+#endif /* INET6 */
+
+/* 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 * src, char * dst, size_t size)
+{
+
+	switch (af) {
+	case AF_INET:
+		return (inet_ntop4(src, dst, size));
+#ifdef INET6
+	case AF_INET6:
+		return (inet_ntop6(src, dst, size));
+#endif
+	default:
+		errno = EAFNOSUPPORT;
+		return (NULL);
+	}
+	/* NOTREACHED */
+}
+
+/* const char *
+ * inet_ntop4(src, dst, size)
+ *	format an IPv4 address, more or less like inet_ntoa()
+ * 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, size_t size)
+{
+	static const char fmt[] = "%u.%u.%u.%u";
+	char tmp[sizeof "255.255.255.255"];
+
+	if (SPRINTF((tmp, fmt, src[0], src[1], src[2], src[3])) > size) {
+		errno = ENOSPC;
+		return (NULL);
+	}
+	strcpy(dst, tmp);
+	return (dst);
+}
+
+#ifdef INET6
+/* 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, 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"], *tp;
+	struct { int base, len; } best, cur;
+	u_int words[IN6ADDRSZ / 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 < IN6ADDRSZ; i++)
+		words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
+	best.base = -1;
+	cur.base = -1;
+	for (i = 0; i < (IN6ADDRSZ / 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 < (IN6ADDRSZ / 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 == 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) == (IN6ADDRSZ / INT16SZ))
+		*tp++ = ':';
+	*tp++ = '\0';
+
+	/*
+	 * Check for overflow, copy, and we're done.
+	 */
+	if ((size_t)(tp - tmp) > size) {
+		errno = ENOSPC;
+		return (NULL);
+	}
+	strcpy(dst, tmp);
+	return (dst);
+}
+#endif
+
+#endif /* HAVE_INET_NTOP */
diff -purN -x .build -x .inst -x .sinst gnet-2.0.5-orig/src/replacements/inet_pton.c gnet-2.0.5/src/replacements/inet_pton.c
--- gnet-2.0.5-orig/src/replacements/inet_pton.c	1970-01-01 01:00:00.000000000 +0100
+++ gnet-2.0.5/src/replacements/inet_pton.c	2004-10-09 23:57:16.304537600 +0200
@@ -0,0 +1,296 @@
+/*	Id: inet_pton.c,v 1.5 2001/04/13 15:24:35 lukem Exp 	*/
+/*	$NetBSD: inet_pton.c,v 1.16 2000/02/07 18:51:02 itojun Exp $	*/
+
+/* Copyright (c) 1996 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.
+ */
+
+#include "config.h"
+
+#ifndef HAVE_INET_PTON
+
+#include <arpa/inet.h>
+#include <ctype.h>
+#include <errno.h>
+#include <string.h>
+#include <sys/socket.h>
+
+#if HAVE_ARPA_NAMESER_H
+#include <arpa/nameser.h>
+#endif
+
+#ifndef INADDRSZ
+#define	INADDRSZ	4
+#endif
+
+/*
+ * 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, int pton);
+#ifdef INET6
+static int	inet_pton6 (const char *src, u_char *dst);
+#endif
+
+/* 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 * src, void * dst)
+{
+
+	switch (af) {
+	case AF_INET:
+		return (inet_pton4(src, dst, 1));
+#ifdef INET6
+	case AF_INET6:
+		return (inet_pton6(src, dst));
+#endif
+	default:
+		errno = EAFNOSUPPORT;
+		return (-1);
+	}
+	/* NOTREACHED */
+}
+
+/* int
+ * inet_pton4(src, dst, pton)
+ *	when last arg is 0: inet_aton(). with hexadecimal, octal and shorthand.
+ *	when last arg is 1: inet_pton(). decimal dotted-quad only.
+ * return:
+ *	1 if `src' is a valid input, 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, int  pton)
+{
+	u_int val;
+	u_int digit;
+	int base, n;
+	unsigned char c;
+	u_int parts[4];
+	register u_int *pp = parts;
+
+	c = *src;
+	for (;;) {
+		/*
+		 * Collect number up to ``.''.
+		 * Values are specified as for C:
+		 * 0x=hex, 0=octal, isdigit=decimal.
+		 */
+		if (!isdigit(c))
+			return (0);
+		val = 0; base = 10;
+		if (c == '0') {
+			c = *++src;
+			if (c == 'x' || c == 'X')
+				base = 16, c = *++src;
+			else if (isdigit(c) && c != '9')
+				base = 8;
+		}
+		/* inet_pton() takes decimal only */
+		if (pton && base != 10)
+			return (0);
+		for (;;) {
+			if (isdigit(c)) {
+				digit = c - '0';
+				if (digit >= base)
+					break;
+				val = (val * base) + digit;
+				c = *++src;
+			} else if (base == 16 && isxdigit(c)) {
+				digit = c + 10 - (islower(c) ? 'a' : 'A');
+				if (digit >= 16)
+					break;
+				val = (val << 4) | digit;
+				c = *++src;
+			} 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)
+			 *	a	(with a treated as 32 bits)
+			 */
+			if (pp >= parts + 3)
+				return (0);
+			*pp++ = val;
+			c = *++src;
+		} else
+			break;
+	}
+	/*
+	 * Check for trailing characters.
+	 */
+	if (c != '\0' && !isspace(c))
+		return (0);
+	/*
+	 * Concoct the address according to
+	 * the number of parts specified.
+	 */
+	n = pp - parts + 1;
+	/* inet_pton() takes dotted-quad only.  it does not take shorthand. */
+	if (pton && n != 4)
+		return (0);
+	switch (n) {
+
+	case 0:
+		return (0);		/* initial nondigit */
+
+	case 1:				/* a -- 32 bits */
+		break;
+
+	case 2:				/* a.b -- 8.24 bits */
+		if (parts[0] > 0xff || val > 0xffffff)
+			return (0);
+		val |= parts[0] << 24;
+		break;
+
+	case 3:				/* a.b.c -- 8.8.16 bits */
+		if ((parts[0] | parts[1]) > 0xff || val > 0xffff)
+			return (0);
+		val |= (parts[0] << 24) | (parts[1] << 16);
+		break;
+
+	case 4:				/* a.b.c.d -- 8.8.8.8 bits */
+		if ((parts[0] | parts[1] | parts[2] | val) > 0xff)
+			return (0);
+		val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
+		break;
+	}
+	if (dst) {
+		val = htonl(val);
+		memcpy(dst, &val, INADDRSZ);
+	}
+	return (1);
+}
+
+#ifdef INET6
+/* 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[IN6ADDRSZ], *tp, *endp, *colonp;
+	const char *xdigits, *curtok;
+	int ch, saw_xdigit;
+	u_int val;
+
+	memset((tp = tmp), '\0', IN6ADDRSZ);
+	endp = tp + IN6ADDRSZ;
+	colonp = NULL;
+	/* Leading :: requires some special handling. */
+	if (*src == ':')
+		if (*++src != ':')
+			return (0);
+	curtok = src;
+	saw_xdigit = 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 (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 + INT16SZ > endp)
+				return (0);
+			*tp++ = (u_char) (val >> 8) & 0xff;
+			*tp++ = (u_char) val & 0xff;
+			saw_xdigit = 0;
+			val = 0;
+			continue;
+		}
+		if (ch == '.' && ((tp + INADDRSZ) <= endp) &&
+		    inet_pton4(curtok, tp, 1) > 0) {
+			tp += INADDRSZ;
+			saw_xdigit = 0;
+			break;	/* '\0' was seen by inet_pton4(). */
+		}
+		return (0);
+	}
+	if (saw_xdigit) {
+		if (tp + 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, IN6ADDRSZ);
+	return (1);
+}
+#endif
+
+#endif /* HAVE_INET_PTON */


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]