diff options
author | H. Peter Anvin <hpa@zytor.com> | 2002-08-13 05:09:18 +0000 |
---|---|---|
committer | H. Peter Anvin <hpa@zytor.com> | 2002-08-13 05:09:18 +0000 |
commit | 1fe2a686111e246cf9e2189e99e523c68cccfb78 (patch) | |
tree | 6789a1ba6d592439ecbd535a0e6a815b68172f58 | |
parent | dae0fbea1ba63709e39f07e44676cdc249e721c3 (diff) | |
download | klibc-1fe2a686111e246cf9e2189e99e523c68cccfb78.tar.gz |
Files missing from previous checkin...klibc-0.27
-rw-r--r-- | inet/inet_ntop.c | 49 | ||||
-rw-r--r-- | inet/inet_pton.c | 74 | ||||
-rw-r--r-- | klibc/inet/inet_ntop.c | 49 | ||||
-rw-r--r-- | klibc/inet/inet_pton.c | 74 |
4 files changed, 246 insertions, 0 deletions
diff --git a/inet/inet_ntop.c b/inet/inet_ntop.c new file mode 100644 index 0000000000000..a34ff36a81689 --- /dev/null +++ b/inet/inet_ntop.c @@ -0,0 +1,49 @@ +/* + * inet/inet_ntop.c + */ + +#include <stdio.h> +#include <string.h> +#include <errno.h> +#include <arpa/inet.h> +#include <netinet/in6.h> + +const char *inet_ntop(int af, const void *cp, char *buf, size_t len) +{ + int xlen; + + switch ( af ) { + case AF_INET: + { + uint32_t v = ((const struct in_addr *)cp)->s_addr; + + xlen = snprintf(buf, len, "%u.%u.%u.%u", + (v >> 24), (v >> 16) & 0xff, (v >> 8) & 0xff, v & 0xff); + } + break; + + case AF_INET6: + { + const struct in6_addr *s = (const struct in6_addr *)cp; + + xlen = snprintf(buf, len, "%x:%x:%x:%x:%x:%x:%x:%x", + ntohs(s->s6_addr16[0]), ntohs(s->s6_addr16[1]), + ntohs(s->s6_addr16[2]), ntohs(s->s6_addr16[3]), + ntohs(s->s6_addr16[4]), ntohs(s->s6_addr16[5]), + ntohs(s->s6_addr16[6]), ntohs(s->s6_addr16[7])); + } + break; + + default: + errno = EAFNOSUPPORT; + return NULL; + } + + if ( xlen > len ) { + errno = ENOSPC; + return NULL; + } + + return buf; +} + diff --git a/inet/inet_pton.c b/inet/inet_pton.c new file mode 100644 index 0000000000000..6c14b3cfb6168 --- /dev/null +++ b/inet/inet_pton.c @@ -0,0 +1,74 @@ +/* + * inet/inet_pton.c + */ + +#include <stdio.h> +#include <stdint.h> +#include <errno.h> +#include <ctype.h> +#include <string.h> +#include <arpa/inet.h> +#include <netinet/in6.h> + +static inline int hexval(int ch) +{ + if ( ch >= '0' && ch <= '9' ) { + return ch-'0'; + } else if ( ch >= 'A' && ch <= 'F' ) { + return ch-'A'+10; + } else if ( ch >= 'a' && ch <= 'f' ) { + return ch-'a'+10; + } else { + return -1; + } +} + +int inet_pton(int af, const char *src, void *dst) +{ + switch ( af ) { + case AF_INET: + return inet_aton(src, (struct in_addr *)dst); + + case AF_INET6: + { + struct in6_addr *d = (struct in6_addr *)dst; + int colons = 0, dcolons = 0; + int i; + const char *p; + + /* A double colon will increment colons by 2, dcolons by 1 */ + for ( p = dst ; *p ; p++ ) { + if ( p[0] == ':' ) { + colons++; + if ( p[1] == ':' ) + dcolons++; + } else if ( !isxdigit(*p) ) + return 0; /* Not a valid address */ + } + + if ( colons > 7 || dcolons > 1 || (!dcolons && colons != 7) ) + return 0; /* Not a valid address */ + + memset(d, 0, sizeof(struct in6_addr)); + + i = 0; + for ( p = dst ; *p ; p++ ) { + if ( *p == ':' ) { + if ( p[1] == ':' ) { + i += (8-colons); + } else { + i++; + } + } else { + d->s6_addr16[i] = htons((ntohs(d->s6_addr16[i]) << 4) + hexval(*p)); + } + } + + return 1; + } + + default: + errno = EAFNOSUPPORT; + return -1; + } +} diff --git a/klibc/inet/inet_ntop.c b/klibc/inet/inet_ntop.c new file mode 100644 index 0000000000000..a34ff36a81689 --- /dev/null +++ b/klibc/inet/inet_ntop.c @@ -0,0 +1,49 @@ +/* + * inet/inet_ntop.c + */ + +#include <stdio.h> +#include <string.h> +#include <errno.h> +#include <arpa/inet.h> +#include <netinet/in6.h> + +const char *inet_ntop(int af, const void *cp, char *buf, size_t len) +{ + int xlen; + + switch ( af ) { + case AF_INET: + { + uint32_t v = ((const struct in_addr *)cp)->s_addr; + + xlen = snprintf(buf, len, "%u.%u.%u.%u", + (v >> 24), (v >> 16) & 0xff, (v >> 8) & 0xff, v & 0xff); + } + break; + + case AF_INET6: + { + const struct in6_addr *s = (const struct in6_addr *)cp; + + xlen = snprintf(buf, len, "%x:%x:%x:%x:%x:%x:%x:%x", + ntohs(s->s6_addr16[0]), ntohs(s->s6_addr16[1]), + ntohs(s->s6_addr16[2]), ntohs(s->s6_addr16[3]), + ntohs(s->s6_addr16[4]), ntohs(s->s6_addr16[5]), + ntohs(s->s6_addr16[6]), ntohs(s->s6_addr16[7])); + } + break; + + default: + errno = EAFNOSUPPORT; + return NULL; + } + + if ( xlen > len ) { + errno = ENOSPC; + return NULL; + } + + return buf; +} + diff --git a/klibc/inet/inet_pton.c b/klibc/inet/inet_pton.c new file mode 100644 index 0000000000000..6c14b3cfb6168 --- /dev/null +++ b/klibc/inet/inet_pton.c @@ -0,0 +1,74 @@ +/* + * inet/inet_pton.c + */ + +#include <stdio.h> +#include <stdint.h> +#include <errno.h> +#include <ctype.h> +#include <string.h> +#include <arpa/inet.h> +#include <netinet/in6.h> + +static inline int hexval(int ch) +{ + if ( ch >= '0' && ch <= '9' ) { + return ch-'0'; + } else if ( ch >= 'A' && ch <= 'F' ) { + return ch-'A'+10; + } else if ( ch >= 'a' && ch <= 'f' ) { + return ch-'a'+10; + } else { + return -1; + } +} + +int inet_pton(int af, const char *src, void *dst) +{ + switch ( af ) { + case AF_INET: + return inet_aton(src, (struct in_addr *)dst); + + case AF_INET6: + { + struct in6_addr *d = (struct in6_addr *)dst; + int colons = 0, dcolons = 0; + int i; + const char *p; + + /* A double colon will increment colons by 2, dcolons by 1 */ + for ( p = dst ; *p ; p++ ) { + if ( p[0] == ':' ) { + colons++; + if ( p[1] == ':' ) + dcolons++; + } else if ( !isxdigit(*p) ) + return 0; /* Not a valid address */ + } + + if ( colons > 7 || dcolons > 1 || (!dcolons && colons != 7) ) + return 0; /* Not a valid address */ + + memset(d, 0, sizeof(struct in6_addr)); + + i = 0; + for ( p = dst ; *p ; p++ ) { + if ( *p == ':' ) { + if ( p[1] == ':' ) { + i += (8-colons); + } else { + i++; + } + } else { + d->s6_addr16[i] = htons((ntohs(d->s6_addr16[i]) << 4) + hexval(*p)); + } + } + + return 1; + } + + default: + errno = EAFNOSUPPORT; + return -1; + } +} |