2
0
mirror of https://gitlab.isc.org/isc-projects/bind9 synced 2025-08-31 06:25:31 +00:00

new functions isc_netaddr_totext() and isc_netaddr_format()

This commit is contained in:
Andreas Gustafsson
2000-05-26 00:41:03 +00:00
parent 46e8a97c7c
commit 8eaa51a699
2 changed files with 72 additions and 0 deletions

View File

@@ -17,8 +17,11 @@
#include <config.h>
#include <stdio.h>
#include <isc/buffer.h>
#include <isc/netaddr.h>
#include <isc/print.h>
#include <isc/sockaddr.h>
#include <isc/string.h>
#include <isc/util.h>
@@ -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,
"<unknown address, family %u>",
na->family);
array[size - 1] = '\0';
}
}
isc_result_t
isc_netaddr_masktoprefixlen(const isc_netaddr_t *s, unsigned int *lenp)
{