From 8eaa51a6991ea6bc6c9db0b907beacde8dd1fed2 Mon Sep 17 00:00:00 2001 From: Andreas Gustafsson Date: Fri, 26 May 2000 00:41:03 +0000 Subject: [PATCH] new functions isc_netaddr_totext() and isc_netaddr_format() --- lib/isc/include/isc/netaddr.h | 20 ++++++++++++++ lib/isc/netaddr.c | 52 +++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) diff --git a/lib/isc/include/isc/netaddr.h b/lib/isc/include/isc/netaddr.h index 2ea972fa97..b252432482 100644 --- a/lib/isc/include/isc/netaddr.h +++ b/lib/isc/include/isc/netaddr.h @@ -57,6 +57,26 @@ isc_netaddr_masktoprefixlen(const isc_netaddr_t *s, unsigned int *lenp); * ISC_R_MASKNONCONTIG */ +isc_result_t +isc_netaddr_totext(const isc_netaddr_t *netaddr, isc_buffer_t *target); +/* + * Append a text representation of 'sockaddr' to the buffer 'target'. + * The text is NOT null terminated. Handles IPv4 and IPv6 addresses. + * + * Returns: + * ISC_R_SUCCESS + * ISC_R_NOSPACE The text or the null termination did not fit. + * ISC_R_FAILURE Unspecified failure + */ + +void +isc_netaddr_format(isc_netaddr_t *na, char *array, unsigned int size); +/* + * Format a human-readable representation of the network address '*na' + * into the character array 'array', which is of size 'size'. + * The resulting string is guaranteed to be null-terminated. + */ + void isc_netaddr_fromsockaddr(isc_netaddr_t *netaddr, const isc_sockaddr_t *source); diff --git a/lib/isc/netaddr.c b/lib/isc/netaddr.c index 7b3e1f6cdf..84b9ddb0d2 100644 --- a/lib/isc/netaddr.c +++ b/lib/isc/netaddr.c @@ -17,8 +17,11 @@ #include +#include +#include #include +#include #include #include #include @@ -101,6 +104,55 @@ isc_netaddr_eqprefix(const isc_netaddr_t *a, const isc_netaddr_t *b, return (ISC_TRUE); } +isc_result_t +isc_netaddr_totext(const isc_netaddr_t *netaddr, isc_buffer_t *target) { + char abuf[sizeof "xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255"]; + unsigned int alen; + const char *r; + + REQUIRE(netaddr != NULL); + + r = inet_ntop(netaddr->family, &netaddr->type, abuf, sizeof abuf); + if (r == NULL) + return (ISC_R_FAILURE); + + alen = strlen(abuf); + INSIST(alen < sizeof(abuf)); + + if (alen > isc_buffer_availablelength(target)) + return (ISC_R_NOSPACE); + + isc_buffer_putmem(target, (unsigned char *)abuf, alen); + + return (ISC_R_SUCCESS); +} + +void +isc_netaddr_format(isc_netaddr_t *na, char *array, unsigned int size) { + isc_result_t result; + isc_buffer_t buf; + + isc_buffer_init(&buf, array, size); + result = isc_netaddr_totext(na, &buf); + + /* + * Null terminate. + */ + if (result == ISC_R_SUCCESS) { + if (isc_buffer_availablelength(&buf) >= 1) + isc_buffer_putuint8(&buf, 0); + else + result = ISC_R_NOSPACE; + } + + if (result != ISC_R_SUCCESS) { + snprintf(array, size, + "", + na->family); + array[size - 1] = '\0'; + } +} + isc_result_t isc_netaddr_masktoprefixlen(const isc_netaddr_t *s, unsigned int *lenp) {