10 #include "ntp_assert.h"
11 #include "ntp_stdlib.h"
14 /* Don't include ISC's version of IPv6 variables and structures */
16 #include <isc/netaddr.h>
17 #include <isc/sockaddr.h>
21 * Code to tell if we have an IP address
22 * If we have then return the sockaddr structure
23 * and set the return value
24 * see the bind9/getaddresses.c for details
34 struct addrinfo hints;
35 struct addrinfo *result;
36 struct sockaddr_in6 *resaddr6;
40 REQUIRE(host != NULL);
41 REQUIRE(addr != NULL);
46 * Try IPv4, then IPv6. In order to handle the extended format
47 * for IPv6 scoped addresses (address%scope_ID), we'll use a local
48 * working buffer of 128 bytes. The length is an ad-hoc value, but
49 * should be enough for this purpose; the buffer can contain a string
50 * of at least 80 bytes for scope_ID in addition to any IPv6 numeric
51 * addresses (up to 46 bytes), the delimiter character and the
52 * terminating NULL character.
54 if (AF_UNSPEC == af || AF_INET == af)
55 if (inet_pton(AF_INET, host, &in4) == 1) {
57 SET_ADDR4N(addr, in4.s_addr);
62 if (AF_UNSPEC == af || AF_INET6 == af)
63 if (sizeof(tmpbuf) > strlen(host)) {
65 strlcpy(tmpbuf, &host[1], sizeof(tmpbuf));
66 pch = strchr(tmpbuf, ']');
70 strlcpy(tmpbuf, host, sizeof(tmpbuf));
73 hints.ai_family = AF_INET6;
74 hints.ai_flags |= AI_NUMERICHOST;
75 if (getaddrinfo(tmpbuf, NULL, &hints, &result) == 0) {
77 resaddr6 = UA_PTR(struct sockaddr_in6, result->ai_addr);
78 SET_ADDR6N(addr, resaddr6->sin6_addr);
79 SET_SCOPE(addr, resaddr6->sin6_scope_id);
86 * If we got here it was not an IP address