diff options
| author | gilles <> | 2012-06-22 19:13:37 +0000 | 
|---|---|---|
| committer | gilles <> | 2012-06-22 19:13:37 +0000 | 
| commit | 6e70d18fcf5e7b0cad16bf63732fbb9017ea068d (patch) | |
| tree | ca1ac65788c249b5fe54daf2ce518ea1c2749161 /src/lib | |
| parent | 8ab3be999733505905c43d1c6b1e23220e85f6b5 (diff) | |
| download | openbsd-6e70d18fcf5e7b0cad16bf63732fbb9017ea068d.tar.gz openbsd-6e70d18fcf5e7b0cad16bf63732fbb9017ea068d.tar.bz2 openbsd-6e70d18fcf5e7b0cad16bf63732fbb9017ea068d.zip | |
add support for AF_INET6 to inet_net_pton() and inet_net_ntop()
using inet_pton() and inet_ntop() as suggested by claudio
ok claudio@
Diffstat (limited to '')
| -rw-r--r-- | src/lib/libc/net/inet_net_ntop.c | 29 | ||||
| -rw-r--r-- | src/lib/libc/net/inet_net_pton.c | 41 | 
2 files changed, 68 insertions, 2 deletions
| diff --git a/src/lib/libc/net/inet_net_ntop.c b/src/lib/libc/net/inet_net_ntop.c index fc9071798a..35ae7909d3 100644 --- a/src/lib/libc/net/inet_net_ntop.c +++ b/src/lib/libc/net/inet_net_ntop.c | |||
| @@ -1,6 +1,7 @@ | |||
| 1 | /* $OpenBSD: inet_net_ntop.c,v 1.6 2005/08/06 20:30:03 espie Exp $ */ | 1 | /* $OpenBSD: inet_net_ntop.c,v 1.7 2012/06/22 19:13:37 gilles Exp $ */ | 
| 2 | 2 | ||
| 3 | /* | 3 | /* | 
| 4 | * Copyright (c) 2012 by Gilles Chehade <gilles@openbsd.org> | ||
| 4 | * Copyright (c) 1996 by Internet Software Consortium. | 5 | * Copyright (c) 1996 by Internet Software Consortium. | 
| 5 | * | 6 | * | 
| 6 | * Permission to use, copy, modify, and distribute this software for any | 7 | * Permission to use, copy, modify, and distribute this software for any | 
| @@ -28,6 +29,7 @@ | |||
| 28 | #include <stdlib.h> | 29 | #include <stdlib.h> | 
| 29 | 30 | ||
| 30 | static char *inet_net_ntop_ipv4(const u_char *, int, char *, size_t); | 31 | static char *inet_net_ntop_ipv4(const u_char *, int, char *, size_t); | 
| 32 | static char *inet_net_ntop_ipv6(const u_char *, int, char *, size_t); | ||
| 31 | 33 | ||
| 32 | /* | 34 | /* | 
| 33 | * char * | 35 | * char * | 
| @@ -45,6 +47,8 @@ inet_net_ntop(int af, const void *src, int bits, char *dst, size_t size) | |||
| 45 | switch (af) { | 47 | switch (af) { | 
| 46 | case AF_INET: | 48 | case AF_INET: | 
| 47 | return (inet_net_ntop_ipv4(src, bits, dst, size)); | 49 | return (inet_net_ntop_ipv4(src, bits, dst, size)); | 
| 50 | case AF_INET6: | ||
| 51 | return (inet_net_ntop_ipv6(src, bits, dst, size)); | ||
| 48 | default: | 52 | default: | 
| 49 | errno = EAFNOSUPPORT; | 53 | errno = EAFNOSUPPORT; | 
| 50 | return (NULL); | 54 | return (NULL); | 
| @@ -133,3 +137,26 @@ inet_net_ntop_ipv4(const u_char *src, int bits, char *dst, size_t size) | |||
| 133 | errno = EMSGSIZE; | 137 | errno = EMSGSIZE; | 
| 134 | return (NULL); | 138 | return (NULL); | 
| 135 | } | 139 | } | 
| 140 | |||
| 141 | static char * | ||
| 142 | inet_net_ntop_ipv6(const u_char *src, int bits, char *dst, size_t size) | ||
| 143 | { | ||
| 144 | int ret; | ||
| 145 | char buf[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255:255:255:255/128")]; | ||
| 146 | |||
| 147 | if (bits < 0 || bits > 128) { | ||
| 148 | errno = EINVAL; | ||
| 149 | return (NULL); | ||
| 150 | } | ||
| 151 | |||
| 152 | if (inet_ntop(AF_INET6, src, buf, size) == NULL) | ||
| 153 | return (NULL); | ||
| 154 | |||
| 155 | ret = snprintf(dst, size, "%s/%d", buf, bits); | ||
| 156 | if (ret == -1 || ret >= size) { | ||
| 157 | errno = EMSGSIZE; | ||
| 158 | return (NULL); | ||
| 159 | } | ||
| 160 | |||
| 161 | return (dst); | ||
| 162 | } | ||
| diff --git a/src/lib/libc/net/inet_net_pton.c b/src/lib/libc/net/inet_net_pton.c index 3105c33b43..74261399f7 100644 --- a/src/lib/libc/net/inet_net_pton.c +++ b/src/lib/libc/net/inet_net_pton.c | |||
| @@ -1,6 +1,7 @@ | |||
| 1 | /* $OpenBSD: inet_net_pton.c,v 1.6 2008/09/01 09:40:43 markus Exp $ */ | 1 | /* $OpenBSD: inet_net_pton.c,v 1.7 2012/06/22 19:13:37 gilles Exp $ */ | 
| 2 | 2 | ||
| 3 | /* | 3 | /* | 
| 4 | * Copyright (c) 2012 by Gilles Chehade <gilles@openbsd.org> | ||
| 4 | * Copyright (c) 1996 by Internet Software Consortium. | 5 | * Copyright (c) 1996 by Internet Software Consortium. | 
| 5 | * | 6 | * | 
| 6 | * Permission to use, copy, modify, and distribute this software for any | 7 | * Permission to use, copy, modify, and distribute this software for any | 
| @@ -30,6 +31,7 @@ | |||
| 30 | #include <stdlib.h> | 31 | #include <stdlib.h> | 
| 31 | 32 | ||
| 32 | static int inet_net_pton_ipv4(const char *, u_char *, size_t); | 33 | static int inet_net_pton_ipv4(const char *, u_char *, size_t); | 
| 34 | static int inet_net_pton_ipv6(const char *, u_char *, size_t); | ||
| 33 | 35 | ||
| 34 | /* | 36 | /* | 
| 35 | * static int | 37 | * static int | 
| @@ -50,6 +52,8 @@ inet_net_pton(int af, const char *src, void *dst, size_t size) | |||
| 50 | switch (af) { | 52 | switch (af) { | 
| 51 | case AF_INET: | 53 | case AF_INET: | 
| 52 | return (inet_net_pton_ipv4(src, dst, size)); | 54 | return (inet_net_pton_ipv4(src, dst, size)); | 
| 55 | case AF_INET6: | ||
| 56 | return (inet_net_pton_ipv6(src, dst, size)); | ||
| 53 | default: | 57 | default: | 
| 54 | errno = EAFNOSUPPORT; | 58 | errno = EAFNOSUPPORT; | 
| 55 | return (-1); | 59 | return (-1); | 
| @@ -189,3 +193,38 @@ inet_net_pton_ipv4(const char *src, u_char *dst, size_t size) | |||
| 189 | errno = EMSGSIZE; | 193 | errno = EMSGSIZE; | 
| 190 | return (-1); | 194 | return (-1); | 
| 191 | } | 195 | } | 
| 196 | |||
| 197 | |||
| 198 | static int | ||
| 199 | inet_net_pton_ipv6(const char *src, u_char *dst, size_t size) | ||
| 200 | { | ||
| 201 | int ret; | ||
| 202 | int bits; | ||
| 203 | char buf[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255:255:255:255/128")]; | ||
| 204 | char *sep; | ||
| 205 | const char *errstr; | ||
| 206 | |||
| 207 | if (strlcpy(buf, src, sizeof buf) >= sizeof buf) { | ||
| 208 | errno = EMSGSIZE; | ||
| 209 | return (-1); | ||
| 210 | } | ||
| 211 | |||
| 212 | sep = strchr(buf, '/'); | ||
| 213 | if (sep != NULL) | ||
| 214 | *sep++ = '\0'; | ||
| 215 | |||
| 216 | ret = inet_pton(AF_INET6, buf, dst); | ||
| 217 | if (ret != 1) | ||
| 218 | return (-1); | ||
| 219 | |||
| 220 | if (sep == NULL) | ||
| 221 | return 128; | ||
| 222 | |||
| 223 | bits = strtonum(sep, 0, 128, &errstr); | ||
| 224 | if (errstr) { | ||
| 225 | errno = EINVAL; | ||
| 226 | return (-1); | ||
| 227 | } | ||
| 228 | |||
| 229 | return bits; | ||
| 230 | } | ||
