Commit 8123cdd8 authored by Pekka Pessi's avatar Pekka Pessi

Added inet_pton() and inet_ntop() replacement functions.

darcs-hash:20051101112459-65a35-89dc271aaacbb0e06aeaca2303e369f71c4e1c99.gz
parent 3e0e1b3d
......@@ -76,7 +76,8 @@ endif
EXTRA_DIST = Doxyfile su.docs \
run_addrinfo run_localinfo run_su_test \
memmem.c strtoull.c strcasestr.c inet_ntop.c \
memmem.c strtoull.c strcasestr.c \
inet_ntop.c inet_pton.c \
su_tag_ref.c
dist_sofialibexec_SCRIPTS = tag_dll.awk
......
......@@ -14,26 +14,7 @@
* SOFTWARE.
*/
#include "setup.h"
#include <sys/types.h>
#if defined(WIN32) && !defined(WATT32)
#include "nameser.h"
#else
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef HAVE_ARPA_NAMESER_H
#include <arpa/nameser.h>
#endif
#ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h>
#endif
#endif
#include "config.h"
#include <ctype.h>
#include <errno.h>
......@@ -41,17 +22,9 @@
#include <string.h>
#include <stdlib.h>
#include "ares_ipv6.h"
#include "inet_ntop.h"
#if !defined(HAVE_INET_NTOP) || !defined(HAVE_INET_NTOP_IPV6)
#include <sys/types.h>
#ifdef SPRINTF_CHAR
# define SPRINTF(x) strlen(sprintf/**/x)
#else
# define SPRINTF(x) ((size_t)sprintf x)
#endif
#include "su.h"
/*
* WARNING: Don't even consider trying to compile this on a system where
......@@ -59,7 +32,9 @@
*/
static const char *inet_ntop4(const unsigned char *src, char *dst, size_t size);
#if HAVE_SIN6
static const char *inet_ntop6(const unsigned char *src, char *dst, size_t size);
#endif
/* char *
* inet_ntop(af, src, dst, size)
......@@ -70,19 +45,19 @@ static const char *inet_ntop6(const unsigned char *src, char *dst, size_t size);
* Paul Vixie, 1996.
*/
const char *
inet_ntop(int af, const void *src, char *dst, size_t size)
inet_ntop(int af, void const *src, char *dst, size_t size)
{
switch (af) {
case AF_INET:
return (inet_ntop4(src, dst, size));
#ifdef INET6
return inet_ntop4(src, dst, size);
#if HAVE_SIN6
case AF_INET6:
return (inet_ntop6(src, dst, size));
return inet_ntop6(src, dst, size);
#endif
default:
errno = EAFNOSUPPORT;
return (NULL);
su_seterrno(EAFNOSUPPORT);
return NULL;
}
/* NOTREACHED */
}
......@@ -104,15 +79,16 @@ inet_ntop4(const unsigned 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);
if (snprintf(tmp, sizeof tmp, fmt,
src[0], src[1], src[2], src[3]) > size) {
su_seterrno(ENOSPC);
return NULL;
}
strcpy(dst, tmp);
return (dst);
return strcpy(dst, tmp);
}
#ifdef INET6
#if HAVE_SIN6
/* const char *
* inet_ntop6(src, dst, size)
* convert IPv6 binary address into presentation (printable) format
......@@ -120,7 +96,7 @@ inet_ntop4(const unsigned char *src, char *dst, size_t size)
* Paul Vixie, 1996.
*/
static const char *
inet_ntop6(const unsigned char *src, char *dst, size_t size)
inet_ntop6(unsigned char const *src, char *dst, size_t size)
{
/*
* Note that int32_t and int16_t need only be "at least" large enough
......@@ -131,7 +107,7 @@ inet_ntop6(const unsigned char *src, char *dst, size_t size)
*/
char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"], *tp;
struct { int base, len; } best, cur;
unsigned int words[IN6ADDRSZ / INT16SZ];
unsigned int words[8];
int i;
/*
......@@ -139,12 +115,11 @@ inet_ntop6(const unsigned char *src, char *dst, size_t size)
* 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));
for (i = 0; i < 16; i += 2)
words[i / 2] = (src[i] << 8) | (src[i + 1]);
best.base = -1;
cur.base = -1;
for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++) {
for (i = 0; i < 8; i++) {
if (words[i] == 0) {
if (cur.base == -1)
cur.base = i, cur.len = 1;
......@@ -169,7 +144,7 @@ inet_ntop6(const unsigned char *src, char *dst, size_t size)
* Format the result.
*/
tp = tmp;
for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++) {
for (i = 0; i < 8; i++) {
/* Are we inside the best run of 0x00's? */
if (best.base != -1 && i >= best.base &&
i < (best.base + best.len)) {
......@@ -188,10 +163,10 @@ inet_ntop6(const unsigned char *src, char *dst, size_t size)
tp += strlen(tp);
break;
}
tp += SPRINTF((tp, "%x", words[i]));
tp += sprintf(tp, "%x", words[i]);
}
/* Was it a trailing run of 0x00's? */
if (best.base != -1 && (best.base + best.len) == (IN6ADDRSZ / INT16SZ))
if (best.base != -1 && (best.base + best.len) == 8)
*tp++ = ':';
*tp++ = '\0';
......@@ -199,12 +174,10 @@ inet_ntop6(const unsigned char *src, char *dst, size_t size)
* Check for overflow, copy, and we're done.
*/
if ((size_t)(tp - tmp) > size) {
errno = ENOSPC;
return (NULL);
su_seterrno(ENOSPC);
return NULL;
}
strcpy(dst, tmp);
return (dst);
}
#endif
return strcpy(dst, tmp);
}
#endif
/*
* 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.
*/
#include "config.h"
#include <sys/types.h>
#include <string.h>
#include <errno.h>
#include "su.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);
#if HAVE_SIN6
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(af, src, dst)
int af;
const char *src;
void *dst;
{
switch (af) {
case AF_INET:
return (inet_pton4(src, dst));
#if HAVE_SIN6
case AF_INET6:
return (inet_pton6(src, dst));
#endif
default:
su_seterrno(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(src, dst)
const char *src;
u_char *dst;
{
static const char digits[] = "0123456789";
int saw_digit, octets, ch;
u_char tmp[4], *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, 4);
return (1);
}
#if HAVE_SIN6
/* 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(src, dst)
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, saw_xdigit;
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;
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 + 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_pton4(curtok, tp) > 0) {
tp += NS_INADDRSZ;
saw_xdigit = 0;
break; /* '\0' was seen by inet_pton4(). */
}
return (0);
}
if (saw_xdigit) {
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);
}
#endif
......@@ -272,17 +272,8 @@ char *su_gai_strerror(int errcode);
#endif
#if SU_HAVE_WINSOCK
#if !SU_HAVE_NT
int WSAAPI inet_pton(
int AddressFamily, // Address family to which the address belongs.
const char *InputString, // Address (numeric string) to convert.
void *Address); // Where to return the binary address.
const char * WSAAPI inet_ntop(
int AddressFamily, // Address family to which the address belongs.
const void *Address, // Address (binary) to convert.
char *OutputString, // Where to return the output string.
int OutputBufferSize); // Size of above buffer.
#endif /* !SU_HAVE_NT */
int inet_pton(int af, char const *src, void *dst);
const char *inet_ntop(int af, void const *src, char *dst, size_t size);
#endif
/* ---------------------------------------------------------------------- */
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment