1a835b8fdelphij/*
2a835b8fdelphij * is_ip_address
3a835b8fdelphij *
4a835b8fdelphij */
5a835b8fdelphij
6a835b8fdelphij#ifdef HAVE_CONFIG_H
7a835b8fdelphij# include <config.h>
8a835b8fdelphij#endif
9a835b8fdelphij
10a835b8fdelphij#include "ntp_assert.h"
11a835b8fdelphij#include "ntp_stdlib.h"
12a835b8fdelphij#include "safecast.h"
13a835b8fdelphij
14a835b8fdelphij/* Don't include ISC's version of IPv6 variables and structures */
15a835b8fdelphij#define ISC_IPV6_H 1
16a835b8fdelphij#include <isc/netaddr.h>
17a835b8fdelphij#include <isc/sockaddr.h>
18a835b8fdelphij
19a835b8fdelphij
20a835b8fdelphij/*
21a835b8fdelphij * Code to tell if we have an IP address
22a835b8fdelphij * If we have then return the sockaddr structure
23a835b8fdelphij * and set the return value
24a835b8fdelphij * see the bind9/getaddresses.c for details
25a835b8fdelphij */
26a835b8fdelphijint
27a835b8fdelphijis_ip_address(
28a835b8fdelphij	const char *	host,
29a835b8fdelphij	u_short		af,
30a835b8fdelphij	sockaddr_u *	addr
31a835b8fdelphij	)
32a835b8fdelphij{
33a835b8fdelphij	struct in_addr in4;
34a835b8fdelphij	struct addrinfo hints;
35a835b8fdelphij	struct addrinfo *result;
36a835b8fdelphij	struct sockaddr_in6 *resaddr6;
37a835b8fdelphij	char tmpbuf[128];
38a835b8fdelphij	char *pch;
39a835b8fdelphij
40a835b8fdelphij	REQUIRE(host != NULL);
41a835b8fdelphij	REQUIRE(addr != NULL);
42a835b8fdelphij
43a835b8fdelphij	ZERO_SOCK(addr);
44a835b8fdelphij
45a835b8fdelphij	/*
46a835b8fdelphij	 * Try IPv4, then IPv6.  In order to handle the extended format
47a835b8fdelphij	 * for IPv6 scoped addresses (address%scope_ID), we'll use a local
48a835b8fdelphij	 * working buffer of 128 bytes.  The length is an ad-hoc value, but
49a835b8fdelphij	 * should be enough for this purpose; the buffer can contain a string
50a835b8fdelphij	 * of at least 80 bytes for scope_ID in addition to any IPv6 numeric
51a835b8fdelphij	 * addresses (up to 46 bytes), the delimiter character and the
52a835b8fdelphij	 * terminating NULL character.
53a835b8fdelphij	 */
54a835b8fdelphij	if (AF_UNSPEC == af || AF_INET == af)
55a835b8fdelphij		if (inet_pton(AF_INET, host, &in4) == 1) {
56a835b8fdelphij			AF(addr) = AF_INET;
57a835b8fdelphij			SET_ADDR4N(addr, in4.s_addr);
58a835b8fdelphij
59a835b8fdelphij			return TRUE;
60a835b8fdelphij		}
61a835b8fdelphij
62a835b8fdelphij	if (AF_UNSPEC == af || AF_INET6 == af)
63a835b8fdelphij		if (sizeof(tmpbuf) > strlen(host)) {
64a835b8fdelphij			if ('[' == host[0]) {
65a835b8fdelphij				strlcpy(tmpbuf, &host[1], sizeof(tmpbuf));
66a835b8fdelphij				pch = strchr(tmpbuf, ']');
67a835b8fdelphij				if (pch != NULL)
68a835b8fdelphij					*pch = '\0';
69a835b8fdelphij			} else {
70a835b8fdelphij				strlcpy(tmpbuf, host, sizeof(tmpbuf));
71a835b8fdelphij			}
72a835b8fdelphij			ZERO(hints);
73a835b8fdelphij			hints.ai_family = AF_INET6;
74a835b8fdelphij			hints.ai_flags |= AI_NUMERICHOST;
75a835b8fdelphij			if (getaddrinfo(tmpbuf, NULL, &hints, &result) == 0) {
76a835b8fdelphij				AF(addr) = AF_INET6;
77a835b8fdelphij				resaddr6 = UA_PTR(struct sockaddr_in6, result->ai_addr);
78a835b8fdelphij				SET_ADDR6N(addr, resaddr6->sin6_addr);
79a835b8fdelphij				SET_SCOPE(addr, resaddr6->sin6_scope_id);
80a835b8fdelphij
81a835b8fdelphij				freeaddrinfo(result);
82a835b8fdelphij				return TRUE;
83a835b8fdelphij			}
84a835b8fdelphij		}
85a835b8fdelphij	/*
86a835b8fdelphij	 * If we got here it was not an IP address
87a835b8fdelphij	 */
88a835b8fdelphij	return FALSE;
89a835b8fdelphij}
90