summaryrefslogtreecommitdiff
path: root/src/lib/libc/net/inet_net_ntop.c
diff options
context:
space:
mode:
authorgilles <>2012-06-22 19:13:37 +0000
committergilles <>2012-06-22 19:13:37 +0000
commit6e70d18fcf5e7b0cad16bf63732fbb9017ea068d (patch)
treeca1ac65788c249b5fe54daf2ce518ea1c2749161 /src/lib/libc/net/inet_net_ntop.c
parent8ab3be999733505905c43d1c6b1e23220e85f6b5 (diff)
downloadopenbsd-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 'src/lib/libc/net/inet_net_ntop.c')
-rw-r--r--src/lib/libc/net/inet_net_ntop.c29
1 files changed, 28 insertions, 1 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
30static char *inet_net_ntop_ipv4(const u_char *, int, char *, size_t); 31static char *inet_net_ntop_ipv4(const u_char *, int, char *, size_t);
32static 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
141static char *
142inet_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}