1d54cfbdroberto/*
2d54cfbdroberto * Copyright (C) 2004, 2007-2009  Internet Systems Consortium, Inc. ("ISC")
3d54cfbdroberto * Copyright (C) 1999-2001  Internet Software Consortium.
4d54cfbdroberto *
5d54cfbdroberto * Permission to use, copy, modify, and/or distribute this software for any
6d54cfbdroberto * purpose with or without fee is hereby granted, provided that the above
7d54cfbdroberto * copyright notice and this permission notice appear in all copies.
8d54cfbdroberto *
9d54cfbdroberto * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10d54cfbdroberto * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11d54cfbdroberto * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12d54cfbdroberto * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13d54cfbdroberto * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14d54cfbdroberto * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15d54cfbdroberto * PERFORMANCE OF THIS SOFTWARE.
16d54cfbdroberto */
17d54cfbdroberto
18047f369cy/* $Id: interfaceiter.c,v 1.15 2009/01/18 23:48:14 tbox Exp $ */
19d54cfbdroberto
20d54cfbdroberto#include <config.h>
21d54cfbdroberto
22d54cfbdroberto#include <stdio.h>
23d54cfbdroberto#include <stdlib.h>
24d54cfbdroberto#include <errno.h>
25047f369cy#include <sys/types.h>
26047f369cy#include <winsock2.h>
27047f369cy#include <ws2tcpip.h>
28047f369cy#include <gaa_compat.h>
29d54cfbdroberto
30d54cfbdroberto#include <isc/interfaceiter.h>
31d54cfbdroberto#include <isc/mem.h>
32d54cfbdroberto#include <isc/result.h>
33d54cfbdroberto#include <isc/string.h>
34d54cfbdroberto#include <isc/strerror.h>
35d54cfbdroberto#include <isc/types.h>
36d54cfbdroberto#include <isc/util.h>
37d54cfbdroberto#include <isc/win32os.h>
38d54cfbdroberto
39d54cfbdrobertovoid InitSockets(void);
40d54cfbdroberto
41d54cfbdroberto
42d54cfbdroberto#define IFITER_MAGIC		0x49464954U	/* IFIT. */
43d54cfbdroberto#define VALID_IFITER(t)		((t) != NULL && (t)->magic == IFITER_MAGIC)
44d54cfbdroberto
45d54cfbdrobertostruct isc_interfaceiter {
46d54cfbdroberto	unsigned int		magic;		/* Magic number. */
47d54cfbdroberto	/* common fields */
48d54cfbdroberto	isc_mem_t		*mctx;
49d54cfbdroberto	isc_interface_t		current;	/* Current interface data. */
50d54cfbdroberto	isc_result_t		result;		/* Last result code. */
51d54cfbdroberto	/* fields used if GetAdaptersAddresses is available at runtime */
52d54cfbdroberto	IP_ADAPTER_ADDRESSES *	ipaa;		/* GAA() result buffer */
53d54cfbdroberto	ULONG			ipaasize;	/* Bytes allocated */
54d54cfbdroberto	IP_ADAPTER_ADDRESSES *	ipaaCur;	/* enumeration position */
55d54cfbdroberto	IP_ADAPTER_UNICAST_ADDRESS *ipuaCur;	/* enumeration subposition */
56d54cfbdroberto	/* fields used for the older address enumeration ioctls */
578518518delphij	SOCKET			socket;
58d54cfbdroberto	INTERFACE_INFO		IFData;		/* Current Interface Info */
59d54cfbdroberto	int			numIF;		/* Current Interface count */
60d54cfbdroberto	int			v4IF;		/* Number of IPv4 Interfaces */
61d54cfbdroberto	INTERFACE_INFO		*buf4;		/* Buffer for WSAIoctl data. */
62d54cfbdroberto	unsigned int		buf4size;	/* Bytes allocated. */
63d54cfbdroberto	INTERFACE_INFO		*pos4;		/* Current offset in IF List */
64d54cfbdroberto	SOCKET_ADDRESS_LIST	*buf6;
65d54cfbdroberto	unsigned int		buf6size;	/* Bytes allocated. */
66d54cfbdroberto	unsigned int		pos6;		/* buf6 index, counts down */
67d54cfbdroberto	struct in6_addr		loop__1;	/* ::1 node-scope localhost */
68d54cfbdroberto	struct in6_addr		loopfe80__1;	/* fe80::1 link-scope localhost */
69d54cfbdroberto};
70d54cfbdroberto
71d54cfbdrobertotypedef ULONG (WINAPI *PGETADAPTERSADDRESSES)(
72d54cfbdroberto    ULONG Family,
73d54cfbdroberto    ULONG Flags,
74d54cfbdroberto    PVOID Reserved,
75d54cfbdroberto    PIP_ADAPTER_ADDRESSES AdapterAddresses,
76d54cfbdroberto    PULONG SizePointer
77d54cfbdroberto);
78d54cfbdroberto
79d54cfbdrobertostatic	isc_boolean_t		use_GAA;
80d54cfbdrobertostatic	isc_boolean_t		use_GAA_determined;
81d54cfbdrobertostatic	HMODULE			hmod_iphlpapi;
82d54cfbdrobertostatic	PGETADAPTERSADDRESSES	pGAA;
83d54cfbdroberto
84d54cfbdroberto
85d54cfbdroberto/*
86d54cfbdroberto * Size of buffer for SIO_GET_INTERFACE_LIST, in number of interfaces.
87d54cfbdroberto * We assume no sane system will have more than than 1K of IP addresses on
88d54cfbdroberto * all of its adapters.
89d54cfbdroberto */
90d54cfbdroberto#define IFCONF_SIZE_INITIAL	  16
91d54cfbdroberto#define IFCONF_SIZE_INCREMENT	  64
92d54cfbdroberto#define IFCONF_SIZE_MAX		1040
93d54cfbdroberto
94d54cfbdroberto
95d54cfbdroberto/* Common utility functions */
96d54cfbdroberto
97d54cfbdroberto/*
98d54cfbdroberto * Windows always provides 255.255.255.255 as the the broadcast
99d54cfbdroberto * address.  ntpd needs to know the broadcast address which will target
100d54cfbdroberto * only that network interface, not all.  Reconstruct it from the
101d54cfbdroberto * address and mask.
102d54cfbdroberto */
103d54cfbdrobertostatic void
104d54cfbdrobertoget_broadcastaddr(isc_netaddr_t *bcastaddr, isc_netaddr_t *addr, isc_netaddr_t *netmask) {
105d54cfbdroberto
106d54cfbdroberto	isc_uint32_t *	b;
107d54cfbdroberto	isc_uint32_t	a, n;
108d54cfbdroberto
109d54cfbdroberto	b = (isc_uint32_t *)&bcastaddr->type.in;
110d54cfbdroberto	a = *(isc_uint32_t *)&addr->type.in;
111d54cfbdroberto	n = *(isc_uint32_t *)&netmask->type.in;
112d54cfbdroberto
113d54cfbdroberto	*b = a | ~n;
114d54cfbdroberto}
115d54cfbdroberto
116d54cfbdrobertoisc_result_t
117d54cfbdrobertoisc_interfaceiter_create(isc_mem_t *mctx, isc_interfaceiter_t **iterp) {
118d54cfbdroberto	char strbuf[ISC_STRERRORSIZE];
119d54cfbdroberto	isc_interfaceiter_t *iter;
120d54cfbdroberto	isc_result_t result;
121d54cfbdroberto	unsigned int major;
122d54cfbdroberto	unsigned int minor;
123d54cfbdroberto	unsigned int spmajor;
124d54cfbdroberto	ULONG err;
125d54cfbdroberto	int tries;
126d54cfbdroberto	int error;
127d54cfbdroberto	unsigned long bytesReturned = 0;
128d54cfbdroberto
129d54cfbdroberto	REQUIRE(mctx != NULL);
130d54cfbdroberto	REQUIRE(iterp != NULL);
131d54cfbdroberto	REQUIRE(*iterp == NULL);
132d54cfbdroberto
133d54cfbdroberto	iter = isc_mem_get(mctx, sizeof(*iter));
134d54cfbdroberto	if (iter == NULL)
135d54cfbdroberto		return (ISC_R_NOMEMORY);
136d54cfbdroberto
137d54cfbdroberto	InitSockets();
138d54cfbdroberto
139d54cfbdroberto	iter->mctx = mctx;
140d54cfbdroberto	iter->ipaa = NULL;
141d54cfbdroberto	iter->buf4 = NULL;
142d54cfbdroberto	iter->buf6 = NULL;
143d54cfbdroberto	iter->pos4 = NULL;
144d54cfbdroberto	iter->ipaaCur = NULL;
145d54cfbdroberto	iter->ipuaCur = NULL;
146d54cfbdroberto	iter->ipaasize = 0;
147d54cfbdroberto	iter->pos6 = 0;
148d54cfbdroberto	iter->buf6size = 0;
149d54cfbdroberto	iter->buf4size = 0;
150d54cfbdroberto	iter->result = ISC_R_FAILURE;
151d54cfbdroberto	iter->numIF = 0;
152d54cfbdroberto	iter->v4IF = 0;
153d54cfbdroberto
154d54cfbdroberto	/*
155d54cfbdroberto	 * Use GetAdaptersAddresses in preference to ioctls when running
156d54cfbdroberto	 * on Windows XP SP1 or later.  Earlier GetAdaptersAddresses do
157d54cfbdroberto	 * not appear to provide enough information to associate unicast
158d54cfbdroberto	 * addresses with their prefixes.
159d54cfbdroberto	 */
160d54cfbdroberto	if (!use_GAA_determined) {
161d54cfbdroberto		major = isc_win32os_majorversion();
162d54cfbdroberto		minor = isc_win32os_minorversion();
163d54cfbdroberto		spmajor = isc_win32os_servicepackmajor();
164d54cfbdroberto		if (major > 5 || (5 == major &&
165d54cfbdroberto		    (minor > 1 || (1 == minor && spmajor >= 1)))) {
166d54cfbdroberto			if (NULL == hmod_iphlpapi)
167d54cfbdroberto				hmod_iphlpapi = LoadLibrary("iphlpapi");
168d54cfbdroberto			if (NULL != hmod_iphlpapi)
169d54cfbdroberto				pGAA = (PGETADAPTERSADDRESSES)
170d54cfbdroberto				    GetProcAddress(
171d54cfbdroberto					hmod_iphlpapi,
172d54cfbdroberto					"GetAdaptersAddresses");
173d54cfbdroberto			if (NULL != pGAA)
174d54cfbdroberto				use_GAA = ISC_TRUE;
175d54cfbdroberto		}
176d54cfbdroberto		use_GAA_determined = ISC_TRUE;
177d54cfbdroberto	}
178d54cfbdroberto
179d54cfbdroberto	if (!use_GAA)
180d54cfbdroberto		goto use_ioctls;
181d54cfbdroberto
182d54cfbdroberto	iter->ipaasize = 16 * 1024;
183d54cfbdroberto
184d54cfbdroberto	for (tries = 0; tries < 5; tries++) {
185d54cfbdroberto		iter->ipaa = isc_mem_reallocate(mctx, iter->ipaa,
186d54cfbdroberto						 iter->ipaasize);
187d54cfbdroberto		if (NULL == iter->ipaa) {
188d54cfbdroberto			result = ISC_R_NOMEMORY;
189d54cfbdroberto			goto put_iter;
190d54cfbdroberto		}
191d54cfbdroberto		err = (*pGAA)(
192d54cfbdroberto			AF_UNSPEC,
193d54cfbdroberto			GAA_FLAG_INCLUDE_PREFIX | GAA_FLAG_SKIP_ANYCAST,
194d54cfbdroberto			NULL,
195d54cfbdroberto			iter->ipaa,
196d54cfbdroberto			&iter->ipaasize);
197d54cfbdroberto		if (NO_ERROR == err || ERROR_BUFFER_OVERFLOW != err)
198d54cfbdroberto			break;
199d54cfbdroberto	}
200d54cfbdroberto
201d54cfbdroberto	if (NO_ERROR != err) {
202d54cfbdroberto		isc__strerror(err, strbuf, sizeof(strbuf));
203d54cfbdroberto		UNEXPECTED_ERROR(__FILE__, __LINE__,
204d54cfbdroberto				"GetAdaptersAddresses: %s",
205d54cfbdroberto				strbuf);
206d54cfbdroberto		result = ISC_R_UNEXPECTED;
207d54cfbdroberto		goto gaa_failure;
208d54cfbdroberto	}
209d54cfbdroberto
210d54cfbdroberto	iter->ipaaCur = iter->ipaa;
211d54cfbdroberto	goto success;
212d54cfbdroberto
213d54cfbdroberto use_ioctls:
214d54cfbdroberto	/*
215d54cfbdroberto	 * Create an unbound datagram socket to do the
216d54cfbdroberto	 * SIO_GET_INTERFACE_LIST WSAIoctl on.
217d54cfbdroberto	 */
218d54cfbdroberto	if ((iter->socket = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
219d54cfbdroberto		error = WSAGetLastError();
220d54cfbdroberto		if (error == WSAEAFNOSUPPORT)
221d54cfbdroberto			goto inet6_only;
222d54cfbdroberto		isc__strerror(error, strbuf, sizeof(strbuf));
223d54cfbdroberto		UNEXPECTED_ERROR(__FILE__, __LINE__,
224d54cfbdroberto				"making interface scan socket: %s",
225d54cfbdroberto				strbuf);
226d54cfbdroberto		result = ISC_R_UNEXPECTED;
227d54cfbdroberto		goto put_iter;
228d54cfbdroberto	}
229d54cfbdroberto
230d54cfbdroberto	/*
231d54cfbdroberto	 * Get the interface configuration, allocating more memory if
232d54cfbdroberto	 * necessary.
233d54cfbdroberto	 */
234d54cfbdroberto	iter->buf4size = IFCONF_SIZE_INITIAL*sizeof(INTERFACE_INFO);
235d54cfbdroberto
236d54cfbdroberto	for (;;) {
237d54cfbdroberto		iter->buf4 = isc_mem_get(mctx, iter->buf4size);
238d54cfbdroberto		if (iter->buf4 == NULL) {
239d54cfbdroberto			result = ISC_R_NOMEMORY;
240d54cfbdroberto			goto alloc_failure;
241d54cfbdroberto		}
242d54cfbdroberto
243d54cfbdroberto		if (WSAIoctl(iter->socket, SIO_GET_INTERFACE_LIST,
244d54cfbdroberto			     0, 0, iter->buf4, iter->buf4size,
245d54cfbdroberto			     &bytesReturned, 0, 0) == SOCKET_ERROR)
246d54cfbdroberto		{
247d54cfbdroberto			error = WSAGetLastError();
248d54cfbdroberto			if (error != WSAEFAULT && error != WSAENOBUFS) {
249d54cfbdroberto				errno = error;
250d54cfbdroberto				isc__strerror(error, strbuf, sizeof(strbuf));
251d54cfbdroberto				UNEXPECTED_ERROR(__FILE__, __LINE__,
252d54cfbdroberto						"get interface configuration: %s",
253d54cfbdroberto						strbuf);
254d54cfbdroberto				result = ISC_R_UNEXPECTED;
255d54cfbdroberto				goto ioctl_failure;
256d54cfbdroberto			}
257d54cfbdroberto			/*
258d54cfbdroberto			 * EINVAL.  Retry with a bigger buffer.
259d54cfbdroberto			 */
260d54cfbdroberto		} else {
261d54cfbdroberto			/*
262d54cfbdroberto			 * The WSAIoctl succeeded.
263d54cfbdroberto			 * If the number of the returned bytes is the same
264d54cfbdroberto			 * as the buffer size, we will grow it just in
265d54cfbdroberto			 * case and retry.
266d54cfbdroberto			 */
267d54cfbdroberto			if (bytesReturned > 0 &&
268d54cfbdroberto			    (bytesReturned < iter->buf4size))
269d54cfbdroberto				break;
270d54cfbdroberto		}
271d54cfbdroberto		if (iter->buf4size >= IFCONF_SIZE_MAX*sizeof(INTERFACE_INFO)) {
272d54cfbdroberto			UNEXPECTED_ERROR(__FILE__, __LINE__,
273d54cfbdroberto					 "get interface configuration: "
274d54cfbdroberto					 "maximum buffer size exceeded");
275d54cfbdroberto			result = ISC_R_UNEXPECTED;
276d54cfbdroberto			goto ioctl_failure;
277d54cfbdroberto		}
278d54cfbdroberto		isc_mem_put(mctx, iter->buf4, iter->buf4size);
279d54cfbdroberto
280d54cfbdroberto		iter->buf4size += IFCONF_SIZE_INCREMENT *
281d54cfbdroberto			sizeof(INTERFACE_INFO);
282d54cfbdroberto	}
283d54cfbdroberto
284d54cfbdroberto	/*
285d54cfbdroberto	 * A newly created iterator has an undefined position
286d54cfbdroberto	 * until isc_interfaceiter_first() is called.
287d54cfbdroberto	 */
288d54cfbdroberto	iter->v4IF = bytesReturned/sizeof(INTERFACE_INFO);
289d54cfbdroberto
290d54cfbdroberto	/* We don't need the socket any more, so close it */
291d54cfbdroberto	closesocket(iter->socket);
292d54cfbdroberto
293d54cfbdroberto inet6_only:
294d54cfbdroberto	/*
295d54cfbdroberto	 * Create an unbound datagram socket to do the
296d54cfbdroberto	 * SIO_ADDRESS_LIST_QUERY WSAIoctl on.
297d54cfbdroberto	 */
298d54cfbdroberto	if ((iter->socket = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
299d54cfbdroberto		error = WSAGetLastError();
300d54cfbdroberto		if (error == WSAEAFNOSUPPORT)
301d54cfbdroberto			goto success;
302d54cfbdroberto		isc__strerror(error, strbuf, sizeof(strbuf));
303d54cfbdroberto		UNEXPECTED_ERROR(__FILE__, __LINE__,
304d54cfbdroberto				"making interface scan socket: %s",
305d54cfbdroberto				strbuf);
306d54cfbdroberto		result = ISC_R_UNEXPECTED;
307d54cfbdroberto		goto put_iter;
308d54cfbdroberto	}
309d54cfbdroberto
310d54cfbdroberto	/*
311d54cfbdroberto	 * Get the interface configuration, allocating more memory if
312d54cfbdroberto	 * necessary.
313d54cfbdroberto	 */
314d54cfbdroberto	iter->buf6size = sizeof(SOCKET_ADDRESS_LIST) +
315d54cfbdroberto			 IFCONF_SIZE_INITIAL*sizeof(SOCKET_ADDRESS);
316d54cfbdroberto
317d54cfbdroberto	for (;;) {
318d54cfbdroberto		iter->buf6 = isc_mem_get(mctx, iter->buf6size);
319d54cfbdroberto		if (iter->buf6 == NULL) {
320d54cfbdroberto			result = ISC_R_NOMEMORY;
321d54cfbdroberto			goto ioctl_failure;
322d54cfbdroberto		}
323d54cfbdroberto
324d54cfbdroberto		if (WSAIoctl(iter->socket, SIO_ADDRESS_LIST_QUERY,
325d54cfbdroberto			     0, 0, iter->buf6, iter->buf6size,
326d54cfbdroberto			     &bytesReturned, 0, 0) == SOCKET_ERROR)
327d54cfbdroberto		{
328d54cfbdroberto			error = WSAGetLastError();
329d54cfbdroberto			if (error != WSAEFAULT && error != WSAENOBUFS) {
330d54cfbdroberto				errno = error;
331d54cfbdroberto				isc__strerror(error, strbuf, sizeof(strbuf));
332d54cfbdroberto				UNEXPECTED_ERROR(__FILE__, __LINE__,
333d54cfbdroberto						 "sio address list query: %s",
334d54cfbdroberto						 strbuf);
335d54cfbdroberto				result = ISC_R_UNEXPECTED;
336d54cfbdroberto				goto ioctl6_failure;
337d54cfbdroberto			}
338d54cfbdroberto			/*
339d54cfbdroberto			 * EINVAL.  Retry with a bigger buffer.
340d54cfbdroberto			 */
341d54cfbdroberto		} else
342d54cfbdroberto			break;
343d54cfbdroberto
344d54cfbdroberto		if (iter->buf6size >= IFCONF_SIZE_MAX*sizeof(SOCKET_ADDRESS)) {
345d54cfbdroberto			UNEXPECTED_ERROR(__FILE__, __LINE__,
346d54cfbdroberto					 "get interface configuration: "
347d54cfbdroberto					 "maximum buffer size exceeded");
348d54cfbdroberto			result = ISC_R_UNEXPECTED;
349d54cfbdroberto			goto ioctl6_failure;
350d54cfbdroberto		}
351d54cfbdroberto		isc_mem_put(mctx, iter->buf6, iter->buf6size);
352d54cfbdroberto
353d54cfbdroberto		iter->buf6size += IFCONF_SIZE_INCREMENT *
354d54cfbdroberto			sizeof(SOCKET_ADDRESS);
355d54cfbdroberto	}
356d54cfbdroberto
357d54cfbdroberto	/*
358d54cfbdroberto	 * initialize loop__1 to [::1] and loopfe80__1 to [fe80::1].
359d54cfbdroberto	 * used by internal_current6().
360d54cfbdroberto	 */
361d54cfbdroberto	memset(&iter->loop__1, 0, sizeof(iter->loop__1));
362d54cfbdroberto	memset(&iter->loopfe80__1, 0, sizeof(iter->loopfe80__1));
363d54cfbdroberto	iter->loop__1.s6_addr[15] = 1;
364d54cfbdroberto	iter->loopfe80__1.s6_addr[15] = 1;
365d54cfbdroberto	iter->loopfe80__1.s6_addr[0] = 0xfe;
366d54cfbdroberto	iter->loopfe80__1.s6_addr[1] = 0x80;
367d54cfbdroberto
368d54cfbdroberto	closesocket(iter->socket);
369d54cfbdroberto
370d54cfbdroberto success:
371d54cfbdroberto	iter->magic = IFITER_MAGIC;
372d54cfbdroberto	*iterp = iter;
373d54cfbdroberto	return (ISC_R_SUCCESS);
374d54cfbdroberto
375d54cfbdroberto gaa_failure:
376d54cfbdroberto	isc_mem_put(mctx, iter->ipaa, iter->ipaasize);
377d54cfbdroberto	goto put_iter;
378d54cfbdroberto
379d54cfbdroberto ioctl6_failure:
380d54cfbdroberto	isc_mem_put(mctx, iter->buf6, iter->buf6size);
381d54cfbdroberto
382d54cfbdroberto ioctl_failure:
383d54cfbdroberto	if (iter->buf4 != NULL)
384d54cfbdroberto		isc_mem_put(mctx, iter->buf4, iter->buf4size);
385d54cfbdroberto
386d54cfbdroberto alloc_failure:
387d54cfbdroberto	if (iter->socket >= 0)
388d54cfbdroberto		(void) closesocket(iter->socket);
389d54cfbdroberto
390d54cfbdroberto put_iter:
391d54cfbdroberto	isc_mem_put(mctx, iter, sizeof(*iter));
392d54cfbdroberto	return (result);
393d54cfbdroberto}
394d54cfbdroberto
395d54cfbdrobertostatic unsigned char
396d54cfbdrobertoGAA_find_prefix(isc_interfaceiter_t *iter) {
397d54cfbdroberto	IP_ADAPTER_PREFIX *	ipap;
398d54cfbdroberto	IP_ADAPTER_PREFIX *	ipap_match;
399d54cfbdroberto	int			match_len;
400d54cfbdroberto	int			max_len;
401d54cfbdroberto	isc_netaddr_t		target;
402d54cfbdroberto	u_short			af;
403d54cfbdroberto	isc_netaddr_t		pfx;
404d54cfbdroberto	int			pfx_len;
405d54cfbdroberto	size_t			nbytes;
406d54cfbdroberto	unsigned char		nbits;
407d54cfbdroberto	unsigned char *		pbits;
408d54cfbdroberto	unsigned int		octets;
409d54cfbdroberto
410d54cfbdroberto	match_len = 0;
411d54cfbdroberto	ipap_match = NULL;
412d54cfbdroberto	isc_netaddr_fromsockaddr(&target,
413d54cfbdroberto	    (isc_sockaddr_t *)iter->ipuaCur->Address.lpSockaddr);
414d54cfbdroberto	af = (u_short)target.family;
415d54cfbdroberto	INSIST(AF_INET == af || AF_INET6 == af);
416d54cfbdroberto	max_len = (AF_INET6 == af) ? 128 : 32;
417d54cfbdroberto	iter->current.netmask.family = af;
418d54cfbdroberto	for (ipap = iter->ipaaCur->FirstPrefix;
419d54cfbdroberto	     ipap != NULL;
420d54cfbdroberto	     ipap = ipap->Next) {
421d54cfbdroberto		if (ipap->Address.lpSockaddr->sa_family != af)
422d54cfbdroberto			continue;
423d54cfbdroberto		isc_netaddr_fromsockaddr(&pfx,
424d54cfbdroberto		    (isc_sockaddr_t *)ipap->Address.lpSockaddr);
425d54cfbdroberto		pfx_len = ipap->PrefixLength;
426d54cfbdroberto		INSIST(0 <= pfx_len && pfx_len <= max_len);
427d54cfbdroberto		if (pfx_len > match_len && pfx_len < max_len &&
428d54cfbdroberto		    isc_netaddr_eqprefix(&target, &pfx, pfx_len)) {
429d54cfbdroberto			ipap_match = ipap;
430d54cfbdroberto			match_len = pfx_len;
431d54cfbdroberto		}
432d54cfbdroberto	}
433d54cfbdroberto	if (NULL == ipap_match) {
434d54cfbdroberto		/* presume all-ones mask */
435d54cfbdroberto		if (AF_INET6 == af)
436d54cfbdroberto			octets = sizeof(iter->current.netmask.type.in6);
437d54cfbdroberto		else
438d54cfbdroberto			octets = sizeof(iter->current.netmask.type.in);
439d54cfbdroberto		memset(&iter->current.netmask.type, 0xFF, octets);
440d54cfbdroberto		return (8 * (unsigned char)octets);
441d54cfbdroberto	}
442d54cfbdroberto	nbytes = match_len / 8;
443d54cfbdroberto	nbits = match_len % 8;
444d54cfbdroberto	memset(&iter->current.netmask.type.in6, 0xFF, nbytes);
445d54cfbdroberto	pbits = (void *)&iter->current.netmask.type.in6;
446d54cfbdroberto	pbits += nbytes;
447d54cfbdroberto	*pbits |= 0xFF << (8 - nbits);
448d54cfbdroberto	return ((unsigned char)match_len);
449d54cfbdroberto}
450d54cfbdroberto
451d54cfbdrobertostatic isc_result_t
452d54cfbdrobertointernal_current_GAA(isc_interfaceiter_t *iter) {
453d54cfbdroberto	IP_ADAPTER_ADDRESSES *adap;
454d54cfbdroberto	IP_ADAPTER_UNICAST_ADDRESS *addr;
455d54cfbdroberto	unsigned char prefix_len;
456d54cfbdroberto
457d54cfbdroberto	REQUIRE(iter->ipaaCur != NULL);
458d54cfbdroberto	REQUIRE(iter->ipuaCur != NULL);
459d54cfbdroberto	adap = iter->ipaaCur;
460d54cfbdroberto	addr = iter->ipuaCur;
461d54cfbdroberto	if (IpDadStatePreferred != addr->DadState)
462d54cfbdroberto		return (ISC_R_IGNORE);
463d54cfbdroberto	memset(&iter->current, 0, sizeof(iter->current));
464d54cfbdroberto	iter->current.af = addr->Address.lpSockaddr->sa_family;
465d54cfbdroberto	isc_netaddr_fromsockaddr(&iter->current.address,
466d54cfbdroberto	    (isc_sockaddr_t *)addr->Address.lpSockaddr);
467d54cfbdroberto	if (AF_INET6 == iter->current.af)
468d54cfbdroberto		iter->current.ifindex = adap->Ipv6IfIndex;
469d54cfbdroberto	iter->current.name[0] = '\0';
470d54cfbdroberto	WideCharToMultiByte(
471d54cfbdroberto		CP_ACP,
472d54cfbdroberto		0,
473d54cfbdroberto		adap->FriendlyName,
474d54cfbdroberto		-1,
475d54cfbdroberto		iter->current.name,
476d54cfbdroberto		sizeof(iter->current.name),
477d54cfbdroberto		NULL,
478d54cfbdroberto		NULL);
479d54cfbdroberto	iter->current.name[sizeof(iter->current.name) - 1] = '\0';
480d54cfbdroberto	if (IfOperStatusUp == adap->OperStatus)
481d54cfbdroberto		iter->current.flags |= INTERFACE_F_UP;
482d54cfbdroberto	if (IF_TYPE_PPP == adap->IfType)
483d54cfbdroberto		iter->current.flags |= INTERFACE_F_POINTTOPOINT;
484d54cfbdroberto	else if (IF_TYPE_SOFTWARE_LOOPBACK == adap->IfType)
485d54cfbdroberto		iter->current.flags |= INTERFACE_F_LOOPBACK;
486d54cfbdroberto	if ((IP_ADAPTER_NO_MULTICAST & adap->Flags) == 0)
487d54cfbdroberto		iter->current.flags |= INTERFACE_F_MULTICAST;
488d54cfbdroberto	if (IpSuffixOriginRandom == addr->SuffixOrigin)
489d54cfbdroberto		iter->current.flags |= INTERFACE_F_PRIVACY;
490d54cfbdroberto
491d54cfbdroberto	prefix_len = GAA_find_prefix(iter);
492d54cfbdroberto	/* I'm failing to see a broadcast flag via GAA */
493d54cfbdroberto	if (AF_INET == iter->current.af && prefix_len < 32 &&
494d54cfbdroberto	    (INTERFACE_F_LOOPBACK & iter->current.flags) == 0) {
495d54cfbdroberto		iter->current.flags |= INTERFACE_F_BROADCAST;
496d54cfbdroberto		get_broadcastaddr(&iter->current.broadcast,
497d54cfbdroberto				  &iter->current.address,
498d54cfbdroberto				  &iter->current.netmask);
499d54cfbdroberto	}
500d54cfbdroberto	return (ISC_R_SUCCESS);
501d54cfbdroberto}
502d54cfbdroberto
503d54cfbdroberto/*
504d54cfbdroberto * Get information about the current interface to iter->current.
505d54cfbdroberto * If successful, return ISC_R_SUCCESS.
506d54cfbdroberto * If the interface has an unsupported address family, or if
507d54cfbdroberto * some operation on it fails, return ISC_R_IGNORE to make
508d54cfbdroberto * the higher-level iterator code ignore it.
509d54cfbdroberto */
510d54cfbdroberto
511d54cfbdrobertostatic isc_result_t
512d54cfbdrobertointernal_current(isc_interfaceiter_t *iter) {
513d54cfbdroberto	BOOL ifNamed = FALSE;
514d54cfbdroberto	unsigned long flags;
515d54cfbdroberto
516d54cfbdroberto	REQUIRE(VALID_IFITER(iter));
517d54cfbdroberto	REQUIRE(iter->numIF >= 0);
518d54cfbdroberto
519d54cfbdroberto	memset(&iter->current, 0, sizeof(iter->current));
520d54cfbdroberto	iter->current.af = AF_INET;
521d54cfbdroberto
522d54cfbdroberto	isc_netaddr_fromsockaddr(&iter->current.address,
523d54cfbdroberto	    (isc_sockaddr_t *)&(iter->IFData.iiAddress));
524d54cfbdroberto
525d54cfbdroberto	/*
526d54cfbdroberto	 * Get interface flags.
527d54cfbdroberto	 */
528d54cfbdroberto
529d54cfbdroberto	iter->current.flags = 0;
530d54cfbdroberto	flags = iter->IFData.iiFlags;
531d54cfbdroberto
532d54cfbdroberto	if ((flags & IFF_UP) != 0)
533d54cfbdroberto		iter->current.flags |= INTERFACE_F_UP;
534d54cfbdroberto
535d54cfbdroberto	if ((flags & IFF_BROADCAST) != 0)
536d54cfbdroberto		iter->current.flags |= INTERFACE_F_BROADCAST;
537d54cfbdroberto
538d54cfbdroberto	if ((flags & IFF_MULTICAST) != 0)
539d54cfbdroberto		iter->current.flags |= INTERFACE_F_MULTICAST;
540d54cfbdroberto
541d54cfbdroberto	if ((flags & IFF_POINTTOPOINT) != 0) {
542d54cfbdroberto		iter->current.flags |= INTERFACE_F_POINTTOPOINT;
543d54cfbdroberto		snprintf(iter->current.name, sizeof(iter->current.name),
544d54cfbdroberto			 "PPP %d", iter->numIF);
545d54cfbdroberto		ifNamed = TRUE;
546d54cfbdroberto	}
547d54cfbdroberto
548d54cfbdroberto	if ((flags & IFF_LOOPBACK) != 0) {
549d54cfbdroberto		iter->current.flags |= INTERFACE_F_LOOPBACK;
550d54cfbdroberto		snprintf(iter->current.name, sizeof(iter->current.name),
551d54cfbdroberto			"v4loop %d", iter->numIF);
552d54cfbdroberto		ifNamed = TRUE;
553d54cfbdroberto	}
554d54cfbdroberto
555d54cfbdroberto	/*
556d54cfbdroberto	 * If the interface is point-to-point, get the destination address.
557d54cfbdroberto	 */
558d54cfbdroberto	if ((iter->current.flags & INTERFACE_F_POINTTOPOINT) != 0)
559d54cfbdroberto		isc_netaddr_fromsockaddr(&iter->current.dstaddress,
560d54cfbdroberto		    (isc_sockaddr_t *)&(iter->IFData.iiBroadcastAddress));
561d54cfbdroberto
562d54cfbdroberto	/*
563d54cfbdroberto	 * Get the network mask.
564d54cfbdroberto	 */
565d54cfbdroberto	isc_netaddr_fromsockaddr(&iter->current.netmask,
566d54cfbdroberto	    (isc_sockaddr_t *)&(iter->IFData.iiNetmask));
567d54cfbdroberto
568d54cfbdroberto	/*
569d54cfbdroberto	 * If the interface is broadcast, get the broadcast address,
570d54cfbdroberto	 * based on the unicast address and network mask.
571d54cfbdroberto	 */
572d54cfbdroberto	if ((iter->current.flags & INTERFACE_F_BROADCAST) != 0)
573d54cfbdroberto		get_broadcastaddr(&iter->current.broadcast,
574d54cfbdroberto				  &iter->current.address,
575d54cfbdroberto				  &iter->current.netmask);
576d54cfbdroberto
577d54cfbdroberto	if (ifNamed == FALSE)
578d54cfbdroberto		snprintf(iter->current.name, sizeof(iter->current.name),
579d54cfbdroberto			"IPv4 %d", iter->numIF);
580d54cfbdroberto
581d54cfbdroberto	return (ISC_R_SUCCESS);
582d54cfbdroberto}
583d54cfbdroberto
584d54cfbdrobertostatic isc_result_t
585d54cfbdrobertointernal_current6(isc_interfaceiter_t *iter) {
586d54cfbdroberto	BOOL ifNamed = FALSE;
587d54cfbdroberto	struct sockaddr_in6 *psa6;
588d54cfbdroberto	BOOL localhostSeen;
589d54cfbdroberto	int i;
590d54cfbdroberto
591d54cfbdroberto	REQUIRE(VALID_IFITER(iter));
592d54cfbdroberto	REQUIRE(iter->pos6 >= 0);
593d54cfbdroberto	REQUIRE(iter->buf6 != 0);
594d54cfbdroberto
595d54cfbdroberto	memset(&iter->current, 0, sizeof(iter->current));
596d54cfbdroberto	iter->current.af = AF_INET6;
597d54cfbdroberto
598d54cfbdroberto	/*
599d54cfbdroberto	 * synthesize localhost ::1 before returning the rest, if ::1
600d54cfbdroberto	 * is not on the list.
601d54cfbdroberto	 */
602d54cfbdroberto	if (iter->pos6 >= (unsigned)iter->buf6->iAddressCount) {
603d54cfbdroberto		localhostSeen = FALSE;
604d54cfbdroberto		for (i = 0; i < iter->buf6->iAddressCount; i++) {
605d54cfbdroberto			psa6 = (struct sockaddr_in6 *)
606d54cfbdroberto			       iter->buf6->Address[i].lpSockaddr;
607d54cfbdroberto			if (!memcmp(&iter->loop__1, &psa6->sin6_addr,
608d54cfbdroberto				    sizeof(iter->loop__1))) {
609d54cfbdroberto				localhostSeen = TRUE;
610d54cfbdroberto				break;
611d54cfbdroberto			}
612d54cfbdroberto		}
613d54cfbdroberto		if (localhostSeen)
614d54cfbdroberto			iter->pos6 = iter->buf6->iAddressCount - 1;
615d54cfbdroberto	}
616d54cfbdroberto
617d54cfbdroberto	if (iter->pos6 < (unsigned)iter->buf6->iAddressCount) {
618d54cfbdroberto		isc_netaddr_fromsockaddr(&iter->current.address,
619d54cfbdroberto		    (isc_sockaddr_t *)iter->buf6->Address[iter->pos6].lpSockaddr);
620d54cfbdroberto	} else {
621d54cfbdroberto		iter->current.address.family = AF_INET6;
622d54cfbdroberto		memcpy(&iter->current.address.type.in6, &iter->loop__1,
623d54cfbdroberto		       sizeof(iter->current.address.type.in6));
624d54cfbdroberto	}
625d54cfbdroberto
626d54cfbdroberto	/*
627d54cfbdroberto	 * Get interface flags.
628d54cfbdroberto	 */
629d54cfbdroberto
630d54cfbdroberto	iter->current.flags = INTERFACE_F_UP | INTERFACE_F_MULTICAST;
631d54cfbdroberto
632d54cfbdroberto	if (!memcmp(&iter->current.address.type.in6, &iter->loop__1,
633d54cfbdroberto		    sizeof(iter->current.address.type.in6)) ||
634d54cfbdroberto	    !memcmp(&iter->current.address.type.in6, &iter->loopfe80__1,
635d54cfbdroberto	            sizeof(iter->current.address.type.in6))) {
636d54cfbdroberto
637d54cfbdroberto		iter->current.flags |= INTERFACE_F_LOOPBACK;
638d54cfbdroberto		snprintf(iter->current.name, sizeof(iter->current.name),
639d54cfbdroberto			 "v6loop %d",
640d54cfbdroberto			 iter->buf6->iAddressCount - iter->pos6);
641d54cfbdroberto		ifNamed = TRUE;
642d54cfbdroberto	}
643d54cfbdroberto
644d54cfbdroberto	if (ifNamed == FALSE)
645d54cfbdroberto		snprintf(iter->current.name, sizeof(iter->current.name),
646d54cfbdroberto			 "IPv6 %d",
647d54cfbdroberto			 iter->buf6->iAddressCount - iter->pos6);
648d54cfbdroberto
649d54cfbdroberto	memset(iter->current.netmask.type.in6.s6_addr, 0xff,
650d54cfbdroberto	       sizeof(iter->current.netmask.type.in6.s6_addr));
651d54cfbdroberto	iter->current.netmask.family = AF_INET6;
652d54cfbdroberto	return (ISC_R_SUCCESS);
653d54cfbdroberto}
654d54cfbdroberto
655d54cfbdrobertostatic isc_result_t
656d54cfbdrobertointernal_next_GAA(isc_interfaceiter_t *iter) {
657d54cfbdroberto	REQUIRE(use_GAA);
658d54cfbdroberto	if (NULL == iter->ipaaCur)
659d54cfbdroberto		return (ISC_R_NOMORE);
660d54cfbdroberto	if (NULL == iter->ipuaCur)
661d54cfbdroberto		iter->ipuaCur = iter->ipaaCur->FirstUnicastAddress;
662d54cfbdroberto	else
663d54cfbdroberto		iter->ipuaCur = iter->ipuaCur->Next;
664d54cfbdroberto	while (NULL == iter->ipuaCur) {
665d54cfbdroberto		iter->ipaaCur = iter->ipaaCur->Next;
666d54cfbdroberto		if (NULL == iter->ipaaCur)
667d54cfbdroberto			return (ISC_R_NOMORE);
668d54cfbdroberto		iter->ipuaCur = iter->ipaaCur->FirstUnicastAddress;
669d54cfbdroberto	}
670d54cfbdroberto	return (ISC_R_SUCCESS);
671d54cfbdroberto}
672d54cfbdroberto
673d54cfbdroberto/*
674d54cfbdroberto * Step the iterator to the next interface.  Unlike
675d54cfbdroberto * isc_interfaceiter_next(), this may leave the iterator
676d54cfbdroberto * positioned on an interface that will ultimately
677d54cfbdroberto * be ignored.  Return ISC_R_NOMORE if there are no more
678d54cfbdroberto * interfaces, otherwise ISC_R_SUCCESS.
679d54cfbdroberto */
680d54cfbdrobertostatic isc_result_t
681d54cfbdrobertointernal_next(isc_interfaceiter_t *iter) {
682d54cfbdroberto	if (iter->numIF >= iter->v4IF)
683d54cfbdroberto		return (ISC_R_NOMORE);
684d54cfbdroberto
685d54cfbdroberto	/*
686d54cfbdroberto	 * The first one needs to be set up to point to the last
687d54cfbdroberto	 * Element of the array.  Go to the end and back up
688d54cfbdroberto	 * Microsoft's implementation is peculiar for returning
689d54cfbdroberto	 * the list in reverse order
690d54cfbdroberto	 */
691d54cfbdroberto
692d54cfbdroberto	if (iter->numIF == 0)
693d54cfbdroberto		iter->pos4 = (INTERFACE_INFO *)(iter->buf4 + (iter->v4IF));
694d54cfbdroberto
695d54cfbdroberto	iter->pos4--;
696d54cfbdroberto	if (&(iter->pos4) < &(iter->buf4))
697d54cfbdroberto		return (ISC_R_NOMORE);
698d54cfbdroberto
699d54cfbdroberto	memset(&(iter->IFData), 0, sizeof(INTERFACE_INFO));
700d54cfbdroberto	memcpy(&(iter->IFData), iter->pos4, sizeof(INTERFACE_INFO));
701d54cfbdroberto	iter->numIF++;
702d54cfbdroberto
703d54cfbdroberto	return (ISC_R_SUCCESS);
704d54cfbdroberto}
705d54cfbdroberto
706d54cfbdrobertostatic isc_result_t
707d54cfbdrobertointernal_next6(isc_interfaceiter_t *iter) {
708d54cfbdroberto	if (iter->pos6 == 0)
709d54cfbdroberto		return (ISC_R_NOMORE);
710d54cfbdroberto	iter->pos6--;
711d54cfbdroberto	return (ISC_R_SUCCESS);
712d54cfbdroberto}
713d54cfbdroberto
714d54cfbdrobertoisc_result_t
715d54cfbdrobertoisc_interfaceiter_current(isc_interfaceiter_t *iter,
716d54cfbdroberto			  isc_interface_t *ifdata) {
717d54cfbdroberto	REQUIRE(iter->result == ISC_R_SUCCESS);
718d54cfbdroberto	memcpy(ifdata, &iter->current, sizeof(*ifdata));
719d54cfbdroberto	return (ISC_R_SUCCESS);
720d54cfbdroberto}
721d54cfbdroberto
722d54cfbdrobertoisc_result_t
723d54cfbdrobertoisc_interfaceiter_first(isc_interfaceiter_t *iter) {
724d54cfbdroberto	REQUIRE(VALID_IFITER(iter));
725d54cfbdroberto	REQUIRE(use_GAA_determined);
726d54cfbdroberto	/*
727d54cfbdroberto	 * SIO_ADDRESS_LIST_QUERY (used to query IPv6 addresses)
728d54cfbdroberto	 * intentionally omits localhost addresses [::1] and [::fe80] in
729d54cfbdroberto	 * some cases.  ntpd depends on enumerating [::1] to listen on
730d54cfbdroberto	 * it, and ntpq and ntpdc default to "localhost" as the target,
731d54cfbdroberto	 * so they will attempt to talk to [::1]:123 and fail. This
732d54cfbdroberto	 * means we need to synthesize ::1, which we will do first,
733d54cfbdroberto	 * hence iAddressCount + 1.  internal_next6() will decrement
734d54cfbdroberto	 * it before the first use as an index, and internal_current6()
735d54cfbdroberto	 * will treat pos6 == iAddressCount as a sign to synthesize
736d54cfbdroberto	 * [::1] if needed.
737d54cfbdroberto	 */
738d54cfbdroberto	if (!use_GAA && iter->buf6 != NULL)
739d54cfbdroberto		iter->pos6 = iter->buf6->iAddressCount + 1;
740d54cfbdroberto	iter->result = ISC_R_SUCCESS;
741d54cfbdroberto	return (isc_interfaceiter_next(iter));
742d54cfbdroberto}
743d54cfbdroberto
744d54cfbdrobertoisc_result_t
745d54cfbdrobertoisc_interfaceiter_next(isc_interfaceiter_t *iter) {
746d54cfbdroberto	isc_result_t result;
747d54cfbdroberto
748d54cfbdroberto	REQUIRE(VALID_IFITER(iter));
749d54cfbdroberto	REQUIRE(iter->result == ISC_R_SUCCESS);
750d54cfbdroberto	REQUIRE(use_GAA_determined);
751d54cfbdroberto
752d54cfbdroberto	if (use_GAA) {
753d54cfbdroberto		do {
754d54cfbdroberto			result = internal_next_GAA(iter);
755d54cfbdroberto			if (ISC_R_NOMORE == result)
756d54cfbdroberto				goto set_result;
757d54cfbdroberto			result = internal_current_GAA(iter);
758d54cfbdroberto		} while (ISC_R_IGNORE == result);
759d54cfbdroberto		goto set_result;
760d54cfbdroberto	}
761d54cfbdroberto
762d54cfbdroberto	for (;;) {
763d54cfbdroberto		result = internal_next(iter);
764d54cfbdroberto		if (result == ISC_R_NOMORE) {
765d54cfbdroberto			result = internal_next6(iter);
766d54cfbdroberto			if (result != ISC_R_SUCCESS)
767d54cfbdroberto				break;
768d54cfbdroberto			result = internal_current6(iter);
769d54cfbdroberto			if (result != ISC_R_IGNORE)
770d54cfbdroberto				break;
771d54cfbdroberto		} else if (result != ISC_R_SUCCESS)
772d54cfbdroberto			break;
773d54cfbdroberto		result = internal_current(iter);
774d54cfbdroberto		if (result != ISC_R_IGNORE)
775d54cfbdroberto			break;
776d54cfbdroberto	}
777d54cfbdroberto set_result:
778d54cfbdroberto	iter->result = result;
779d54cfbdroberto	return (result);
780d54cfbdroberto}
781d54cfbdroberto
782d54cfbdrobertovoid
783d54cfbdrobertoisc_interfaceiter_destroy(isc_interfaceiter_t **iterp) {
784d54cfbdroberto	isc_interfaceiter_t *iter;
785d54cfbdroberto
786d54cfbdroberto	REQUIRE(iterp != NULL);
787d54cfbdroberto	iter = *iterp;
788d54cfbdroberto	REQUIRE(VALID_IFITER(iter));
789d54cfbdroberto	REQUIRE(use_GAA_determined);
790d54cfbdroberto
791d54cfbdroberto	if (use_GAA) {
792d54cfbdroberto		REQUIRE(NULL == iter->buf4);
793d54cfbdroberto		REQUIRE(NULL == iter->buf4);
794d54cfbdroberto		if (iter->ipaa != NULL)
795d54cfbdroberto			isc_mem_put(iter->mctx, iter->ipaa, iter->ipaasize);
796d54cfbdroberto	} else {
797d54cfbdroberto		REQUIRE(NULL == iter->ipaa);
798d54cfbdroberto		if (iter->buf4 != NULL)
799d54cfbdroberto			isc_mem_put(iter->mctx, iter->buf4, iter->buf4size);
800d54cfbdroberto		if (iter->buf6 != NULL)
801d54cfbdroberto			isc_mem_put(iter->mctx, iter->buf6, iter->buf6size);
802d54cfbdroberto	}
803d54cfbdroberto
804d54cfbdroberto	iter->magic = 0;
805d54cfbdroberto	isc_mem_put(iter->mctx, iter, sizeof(*iter));
806d54cfbdroberto	*iterp = NULL;
807d54cfbdroberto}
808