diff options
author | João Valverde <joao.valverde@tecnico.ulisboa.pt> | 2016-02-10 09:11:12 +0000 |
---|---|---|
committer | João Valverde <j@v6e.pt> | 2016-02-19 15:55:09 +0000 |
commit | 8bee8bad813446bbf75428a8cdd756fe6063ca6f (patch) | |
tree | d6a335bcf1bf745c36fcefefab3356f6c1705a05 /wsutil | |
parent | 5fec8fa74619a69e3faeca01fd483157d39f7b13 (diff) |
Add inet_pton/inet_ntop interface to libwsutil
Change-Id: Ifc344ed33f2f7ca09a6912a5adb49dc35f07c81f
Reviewed-on: https://code.wireshark.org/review/13881
Petri-Dish: João Valverde <j@v6e.pt>
Tested-by: Petri Dish Buildbot <buildbot-no-reply@wireshark.org>
Reviewed-by: João Valverde <j@v6e.pt>
Diffstat (limited to 'wsutil')
-rw-r--r-- | wsutil/CMakeLists.txt | 1 | ||||
-rw-r--r-- | wsutil/Makefile.am | 2 | ||||
-rw-r--r-- | wsutil/Makefile.common | 2 | ||||
-rw-r--r-- | wsutil/inet_addr-int.h (renamed from wsutil/inet_v6defs.h) | 47 | ||||
-rw-r--r-- | wsutil/inet_addr.c | 60 | ||||
-rw-r--r-- | wsutil/inet_addr.h | 43 | ||||
-rw-r--r-- | wsutil/inet_aton.c | 2 | ||||
-rw-r--r-- | wsutil/inet_ntop.c | 59 | ||||
-rw-r--r-- | wsutil/inet_pton.c | 52 |
9 files changed, 155 insertions, 113 deletions
diff --git a/wsutil/CMakeLists.txt b/wsutil/CMakeLists.txt index 02e7823649..7d63757132 100644 --- a/wsutil/CMakeLists.txt +++ b/wsutil/CMakeLists.txt @@ -53,6 +53,7 @@ set(WSUTIL_FILES filesystem.c frequency-utils.c g711.c + inet_addr.c jsmn.c md4.c md5.c diff --git a/wsutil/Makefile.am b/wsutil/Makefile.am index 23c171d104..e7b0813093 100644 --- a/wsutil/Makefile.am +++ b/wsutil/Makefile.am @@ -82,7 +82,7 @@ EXTRA_libwsutil_la_SOURCES = \ inet_aton.h \ inet_ntop.c \ inet_pton.c \ - inet_v6defs.h \ + inet_addr-int.h \ popcount.c \ popcount.h \ strptime.c \ diff --git a/wsutil/Makefile.common b/wsutil/Makefile.common index 9f69cee14c..6e93833de2 100644 --- a/wsutil/Makefile.common +++ b/wsutil/Makefile.common @@ -50,6 +50,7 @@ LIBWSUTIL_COMMON_SRC = \ filesystem.c \ frequency-utils.c \ g711.c \ + inet_addr.c \ jsmn.c \ md4.c \ md5.c \ @@ -100,6 +101,7 @@ libwsutil_nonrepl_INCLUDES = \ filesystem.h \ frequency-utils.h \ g711.h \ + inet_addr.h \ jsmn.h \ md4.h \ md5.h \ diff --git a/wsutil/inet_v6defs.h b/wsutil/inet_addr-int.h index 9e1f6f5caf..631787dfca 100644 --- a/wsutil/inet_v6defs.h +++ b/wsutil/inet_addr-int.h @@ -1,4 +1,4 @@ -/* inet_v6defs.h +/* inet_addr-int.h * * Wireshark - Network traffic analyzer * By Gerald Combs <gerald@wireshark.org> @@ -19,30 +19,36 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ -#ifndef __INET_V6DEFS_H__ -#define __INET_V6DEFS_H__ +#ifndef __WS_INET_ADDR_INT_H__ +#define __WS_INET_ADDR_INT_H__ -#include "ws_symbol_export.h" +#include "config.h" + +#ifdef HAVE_ARPA_INET_H +#include <arpa/inet.h> +#endif + +#ifdef HAVE_SYS_SOCKET_H +#include <sys/socket.h> /* needed to define AF_ values on UNIX */ +#endif + +#ifdef HAVE_WINSOCK2_H +#include <winsock2.h> /* needed to define AF_ values on Windows */ +#if _MSC_VER < 1600 /* errno.h defines EAFNOSUPPORT in Windows VC10 (and presumably eventually in VC11 ...) */ +#define EAFNOSUPPORT WSAEAFNOSUPPORT +#endif +#endif /* * Versions of "inet_pton()" and "inet_ntop()", for the benefit of OSes that * don't have it. */ - -/* Windows does not have inet_pton() and inet_ntop() until Vista. In order - * to allow binaries compiled on Vista or later to work on pre-Vista Windows - * (without resorting to fragile link ordering tricks), we give our versions - * of those functions Wireshark-specific names. - */ -#ifdef _WIN32 -#define inet_pton ws_inet_pton -#define inet_ntop ws_inet_ntop -WS_DLL_PUBLIC int inet_pton(int af, const char *src, void *dst); +#ifndef HAVE_INET_PTON +extern int inet_pton(int af, const char *src, void *dst); #endif #ifndef HAVE_INET_NTOP_PROTO -WS_DLL_PUBLIC const char *inet_ntop(int af, const void *src, char *dst, - size_t size); +extern const char *inet_ntop(int af, const void *src, char *dst, size_t size); #endif /* @@ -53,13 +59,4 @@ WS_DLL_PUBLIC const char *inet_ntop(int af, const void *src, char *dst, #define AF_INET6 127 /* pick a value unlikely to duplicate an existing AF_ value */ #endif -/* - * And if __P isn't defined, define it here, so we can use it in - * "inet_ntop.c" and "inet_pton.c" (rather than having to change them - * not to use it). - */ -#ifndef __P -#define __P(args) args -#endif - #endif diff --git a/wsutil/inet_addr.c b/wsutil/inet_addr.c new file mode 100644 index 0000000000..b5f4cea62e --- /dev/null +++ b/wsutil/inet_addr.c @@ -0,0 +1,60 @@ +/* inet_addr.c + * + * Wireshark - Network traffic analyzer + * By Gerald Combs <gerald@wireshark.org> + * Copyright 1998 Gerald Combs + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "inet_addr.h" + +#include "inet_addr-int.h" + + + +static inline gboolean +_inet_pton(int af, const gchar *src, gpointer dst) +{ + gint ret; + + ret = inet_pton(af, src, dst); + g_assert(ret >= 0); + return ret == 1; +} + +const gchar * +ws_inet_ntop4(gconstpointer src, gchar *dst, gsize dst_size) +{ + return inet_ntop(AF_INET, src, dst, dst_size); +} + +gboolean +ws_inet_pton4(const gchar *src, guint32 *dst) +{ + return _inet_pton(AF_INET, src, dst); +} + +const gchar * +ws_inet_ntop6(gconstpointer src, gchar *dst, gsize dst_size) +{ + return inet_ntop(AF_INET6, src, dst, dst_size); +} + +gboolean +ws_inet_pton6(const gchar *src, struct e_in6_addr *dst) +{ + return _inet_pton(AF_INET6, src, dst); +} diff --git a/wsutil/inet_addr.h b/wsutil/inet_addr.h new file mode 100644 index 0000000000..330f6e0d88 --- /dev/null +++ b/wsutil/inet_addr.h @@ -0,0 +1,43 @@ +/* inet_addr.h + * + * Wireshark - Network traffic analyzer + * By Gerald Combs <gerald@wireshark.org> + * Copyright 1998 Gerald Combs + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef __WS_INET_ADDR_H__ +#define __WS_INET_ADDR_H__ + +#include "ws_symbol_export.h" + +#include <glib.h> + +#include <epan/ipv6.h> + +WS_DLL_PUBLIC const gchar * +ws_inet_ntop4(gconstpointer src, gchar *dst, gsize dst_size); + +WS_DLL_PUBLIC gboolean +ws_inet_pton4(const gchar *src, guint32 *dst); + +WS_DLL_PUBLIC const gchar * +ws_inet_ntop6(gconstpointer src, gchar *dst, gsize dst_size); + +WS_DLL_PUBLIC gboolean +ws_inet_pton6(const gchar *src, struct e_in6_addr *dst); + +#endif diff --git a/wsutil/inet_aton.c b/wsutil/inet_aton.c index 296b225109..08939f7929 100644 --- a/wsutil/inet_aton.c +++ b/wsutil/inet_aton.c @@ -35,6 +35,8 @@ static char sccsid[] = "@(#)inet_addr.c 8.1 (Berkeley) 6/17/93"; #endif /* LIBC_SCCS and not lint */ +#include <ctype.h> + #ifdef HAVE_SYS_PARAM_H #include <sys/param.h> #endif diff --git a/wsutil/inet_ntop.c b/wsutil/inet_ntop.c index c4e080a255..cbb9c227dd 100644 --- a/wsutil/inet_ntop.c +++ b/wsutil/inet_ntop.c @@ -15,48 +15,21 @@ * SOFTWARE. */ - #include "config.h" -#ifdef HAVE_SYS_PARAM_H -#include <sys/param.h> -#endif - -#ifdef HAVE_SYS_TYPES_H -#include <sys/types.h> -#endif - -#ifdef HAVE_SYS_SOCKET_H -#include <sys/socket.h> /* needed to define AF_ values on UNIX */ -#endif +#include "inet_addr-int.h" -#ifdef HAVE_WINSOCK2_H -#include <winsock2.h> /* needed to define AF_ values on Windows */ -#if _MSC_VER < 1600 /* errno.h defines EAFNOSUPPORT in Windows VC10 (and presumably eventually in VC11 ...) */ -#define EAFNOSUPPORT WSAEAFNOSUPPORT -#endif -#endif +#include <string.h> +#include <errno.h> -#ifdef HAVE_NETINET_IN_H -#include <netinet/in.h> -#endif +#include <glib.h> -#ifdef HAVE_ARPA_INET_H -#include <arpa/inet.h> -#endif +#include <wsutil/ws_diag_control.h> -#ifdef HAVE_ARPA_NAMESER_H -#include <arpa/nameser.h> +#ifndef __P +#define __P(args) args #endif -#include <errno.h> -#include <stdio.h> -#include <string.h> - -#include "inet_v6defs.h" - -#include <glib.h> - #ifndef NS_INADDRSZ #define NS_INADDRSZ 4 #endif @@ -67,6 +40,8 @@ #define NS_INT16SZ 2 #endif +DIAG_OFF(c++-compat) + /* * 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. @@ -84,11 +59,7 @@ static const char *inet_ntop6 __P((const u_char *src, char *dst, size_t size)); * Paul Vixie, 1996. */ const char * -inet_ntop(af, src, dst, size) - int af; - const void *src; - char *dst; - size_t size; +inet_ntop(int af, const void *src, char *dst, size_t size) { switch (af) { case AF_INET: @@ -114,10 +85,7 @@ inet_ntop(af, src, dst, size) * Paul Vixie, 1996. */ static const char * -inet_ntop4(src, dst, size) - const u_char *src; - char *dst; - size_t size; +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"]; @@ -140,10 +108,7 @@ inet_ntop4(src, dst, size) * Paul Vixie, 1996. */ static const char * -inet_ntop6(src, dst, size) - const u_char *src; - char *dst; - size_t size; +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 diff --git a/wsutil/inet_pton.c b/wsutil/inet_pton.c index 28c295abb0..78b14fd3d6 100644 --- a/wsutil/inet_pton.c +++ b/wsutil/inet_pton.c @@ -17,42 +17,19 @@ #include "config.h" -#ifdef HAVE_SYS_PARAM_H -#include <sys/param.h> -#endif - -#ifdef HAVE_SYS_TYPES_H -#include <sys/types.h> -#endif - -#ifdef HAVE_SYS_SOCKET_H -#include <sys/socket.h> /* needed to define AF_ values on UNIX */ -#endif +#include "inet_addr-int.h" -#ifdef HAVE_WINSOCK2_H -#include <winsock2.h> /* needed to define AF_ values on Windows */ -#if _MSC_VER < 1600 /* errno.h defines EAFNOSUPPORT in Windows VC10 (and presumably eventually in VC11 ...) */ -#define EAFNOSUPPORT WSAEAFNOSUPPORT -#endif -#endif +#include <string.h> +#include <errno.h> -#ifdef HAVE_NETINET_IN_H -#include <netinet/in.h> -#endif +#include <glib.h> -#ifdef HAVE_ARPA_INET_H -#include <arpa/inet.h> -#endif +#include <wsutil/ws_diag_control.h> -#ifdef HAVE_ARPA_NAMESER_H -#include <arpa/nameser.h> +#ifndef __P +#define __P(args) args #endif -#include <string.h> -#include <errno.h> - -#include "inet_v6defs.h" - #ifndef NS_INADDRSZ #define NS_INADDRSZ 4 #endif @@ -63,6 +40,8 @@ #define NS_INT16SZ 2 #endif +DIAG_OFF(c++-compat) + /* * 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. @@ -87,10 +66,7 @@ static int inet_pton6 __P((const char *src, u_char *dst)); * Paul Vixie, 1996. */ int -inet_pton(af, src, dst) - int af; - const char *src; - void *dst; +inet_pton(int af, const char *src, void *dst) { switch (af) { #ifdef AF_INET @@ -120,9 +96,7 @@ inet_pton(af, src, dst) * Paul Vixie, 1996. */ static int -inet_pton4(src, dst) - const char *src; - u_char *dst; +inet_pton4(const char *src, u_char *dst) { static const char digits[] = "0123456789"; int saw_digit, octets, ch; @@ -175,9 +149,7 @@ inet_pton4(src, dst) * Paul Vixie, 1996. */ static int -inet_pton6(src, dst) - const char *src; - u_char *dst; +inet_pton6(const char *src, u_char *dst) { static const char xdigits_l[] = "0123456789abcdef", xdigits_u[] = "0123456789ABCDEF"; |