xref: /freebsd/contrib/ntp/ntpd/ntp_io.c (revision 0761549550dad58bfcc0f227ba6ffa78b4d8c017)
1 /*
2  * ntp_io.c - input/output routines for ntpd.	The socket-opening code
3  *		   was shamelessly stolen from ntpd.
4  */
5 
6 #ifdef HAVE_CONFIG_H
7 # include <config.h>
8 #endif
9 
10 #include "ntp_machine.h"
11 #include "ntpd.h"
12 #include "ntp_io.h"
13 #include "iosignal.h"
14 #include "ntp_refclock.h"
15 #include "ntp_stdlib.h"
16 #include "ntp_request.h"
17 #include "ntp.h"
18 #include "ntp_unixtime.h"
19 
20 /* Don't include ISC's version of IPv6 variables and structures */
21 #define ISC_IPV6_H 1
22 #include <isc/interfaceiter.h>
23 #include <isc/list.h>
24 #include <isc/result.h>
25 
26 #ifdef SIM
27 #include "ntpsim.h"
28 #endif
29 
30 #include <stdio.h>
31 #include <signal.h>
32 #ifdef HAVE_SYS_PARAM_H
33 # include <sys/param.h>
34 #endif /* HAVE_SYS_PARAM_H */
35 #ifdef HAVE_SYS_IOCTL_H
36 # include <sys/ioctl.h>
37 #endif
38 #ifdef HAVE_SYS_SOCKIO_H	/* UXPV: SIOC* #defines (Frank Vance <fvance@waii.com>) */
39 # include <sys/sockio.h>
40 #endif
41 #ifdef HAVE_SYS_UIO_H
42 # include <sys/uio.h>
43 #endif
44 
45 /*
46  * setsockopt does not always have the same arg declaration
47  * across all platforms. If it's not defined we make it empty
48  */
49 
50 #ifndef SETSOCKOPT_ARG_CAST
51 #define SETSOCKOPT_ARG_CAST
52 #endif
53 
54 /*
55  * Set up some macros to look for IPv6 and IPv6 multicast
56  */
57 
58 #if defined(ISC_PLATFORM_HAVEIPV6) && !defined(DISABLE_IPV6)
59 
60 #define INCLUDE_IPV6_SUPPORT
61 
62 #if defined(INCLUDE_IPV6_SUPPORT) && defined(IPV6_JOIN_GROUP) && defined(IPV6_LEAVE_GROUP)
63 #define INCLUDE_IPV6_MULTICAST_SUPPORT
64 
65 #endif	/* IPV6 Multicast Support */
66 #endif  /* IPv6 Support */
67 
68 #ifdef INCLUDE_IPV6_SUPPORT
69 #include <netinet/in.h>
70 #include <net/if_var.h>
71 #include <netinet/in_var.h>
72 #endif /* !INCLUDE_IPV6_SUPPORT */
73 
74 extern int listen_to_virtual_ips;
75 extern const char *specific_interface;
76 
77 #if defined(SO_TIMESTAMP) && defined(SCM_TIMESTAMP)
78 #if defined(CMSG_FIRSTHDR)
79 #define HAVE_TIMESTAMP
80 #define USE_TIMESTAMP_CMSG
81 #ifndef TIMESTAMP_CTLMSGBUF_SIZE
82 #define TIMESTAMP_CTLMSGBUF_SIZE 1536 /* moderate default */
83 #endif
84 #else
85 /* fill in for old/other timestamp interfaces */
86 #endif
87 #endif
88 
89 #if defined(SYS_WINNT)
90 #include <transmitbuff.h>
91 #include <isc/win32os.h>
92 /*
93  * Define this macro to control the behavior of connection
94  * resets on UDP sockets.  See Microsoft KnowledgeBase Article Q263823
95  * for details.
96  * NOTE: This requires that Windows 2000 systems install Service Pack 2
97  * or later.
98  */
99 #ifndef SIO_UDP_CONNRESET
100 #define SIO_UDP_CONNRESET _WSAIOW(IOC_VENDOR,12)
101 #endif
102 
103 /*
104  * Windows C runtime ioctl() can't deal properly with sockets,
105  * map to ioctlsocket for this source file.
106  */
107 #define ioctl(fd, opt, val)  ioctlsocket((fd), (opt), (u_long *)(val))
108 #endif  /* SYS_WINNT */
109 
110 /*
111  * We do asynchronous input using the SIGIO facility.  A number of
112  * recvbuf buffers are preallocated for input.	In the signal
113  * handler we poll to see which sockets are ready and read the
114  * packets from them into the recvbuf's along with a time stamp and
115  * an indication of the source host and the interface it was received
116  * through.  This allows us to get as accurate receive time stamps
117  * as possible independent of other processing going on.
118  *
119  * We watch the number of recvbufs available to the signal handler
120  * and allocate more when this number drops below the low water
121  * mark.  If the signal handler should run out of buffers in the
122  * interim it will drop incoming frames, the idea being that it is
123  * better to drop a packet than to be inaccurate.
124  */
125 
126 
127 /*
128  * Other statistics of possible interest
129  */
130 volatile u_long packets_dropped;	/* total number of packets dropped on reception */
131 volatile u_long packets_ignored;	/* packets received on wild card interface */
132 volatile u_long packets_received;	/* total number of packets received */
133 u_long packets_sent;	/* total number of packets sent */
134 u_long packets_notsent; /* total number of packets which couldn't be sent */
135 
136 volatile u_long handler_calls;	/* number of calls to interrupt handler */
137 volatile u_long handler_pkts;	/* number of pkts received by handler */
138 u_long io_timereset;		/* time counters were reset */
139 
140 /*
141  * Interface stuff
142  */
143 struct interface *any_interface;	/* default ipv4 interface */
144 struct interface *any6_interface;	/* default ipv6 interface */
145 struct interface *loopback_interface;	/* loopback ipv4 interface */
146 
147 int ninterfaces;			/* Total number of interfaces */
148 
149 volatile int disable_dynamic_updates;   /* when set to != 0 dynamic updates won't happen */
150 
151 #ifdef REFCLOCK
152 /*
153  * Refclock stuff.	We keep a chain of structures with data concerning
154  * the guys we are doing I/O for.
155  */
156 static	struct refclockio *refio;
157 #endif /* REFCLOCK */
158 
159 
160 /*
161  * Define what the possible "soft" errors can be.  These are non-fatal returns
162  * of various network related functions, like recv() and so on.
163  *
164  * For some reason, BSDI (and perhaps others) will sometimes return <0
165  * from recv() but will have errno==0.  This is broken, but we have to
166  * work around it here.
167  */
168 #define SOFT_ERROR(e)	((e) == EAGAIN || \
169 			 (e) == EWOULDBLOCK || \
170 			 (e) == EINTR || \
171 			 (e) == 0)
172 
173 /*
174  * File descriptor masks etc. for call to select
175  * Not needed for I/O Completion Ports
176  */
177 fd_set activefds;
178 int maxactivefd;
179 /*
180  * bit alternating value to detect verified interfaces during an update cycle
181  */
182 static  u_char          sys_interphase = 0;
183 
184 static  struct interface *new_interface P((struct interface *));
185 static  void add_interface P((struct interface *));
186 static  int update_interfaces P((u_short, interface_receiver_t, void *));
187 static  void remove_interface P((struct interface *));
188 static  struct interface *create_interface P((u_short, struct interface *));
189 
190 static int	move_fd		P((SOCKET));
191 
192 /*
193  * Multicast functions
194  */
195 static	isc_boolean_t	addr_ismulticast	 P((struct sockaddr_storage *));
196 /*
197  * Not all platforms support multicast
198  */
199 #ifdef MCAST
200 static	isc_boolean_t	socket_multicast_enable	 P((struct interface *, int, struct sockaddr_storage *));
201 static	isc_boolean_t	socket_multicast_disable P((struct interface *, struct sockaddr_storage *));
202 #endif
203 
204 #ifdef DEBUG
205 static void print_interface	P((struct interface *, char *, char *));
206 #define DPRINT_INTERFACE(_LVL_, _ARGS_) do { if (debug >= (_LVL_)) { print_interface _ARGS_; } } while (0)
207 #else
208 #define DPRINT_INTERFACE(_LVL_, _ARGS_) do {} while (0)
209 #endif
210 
211 typedef struct vsock vsock_t;
212 enum desc_type { FD_TYPE_SOCKET, FD_TYPE_FILE };
213 
214 struct vsock {
215 	SOCKET				fd;
216 	enum desc_type                  type;
217 	ISC_LINK(vsock_t)		link;
218 };
219 
220 #if !defined(HAVE_IO_COMPLETION_PORT) && defined(HAS_ROUTING_SOCKET)
221 /*
222  * async notification processing (e. g. routing sockets)
223  */
224 /*
225  * support for receiving data on fd that is not a refclock or a socket
226  * like e. g. routing sockets
227  */
228 struct asyncio_reader {
229 	SOCKET fd;		                    /* fd to be read */
230 	void  *data;		                    /* possibly local data */
231 	void (*receiver)(struct asyncio_reader *);  /* input handler */
232 	ISC_LINK(struct asyncio_reader) link;       /* the list this is being kept in */
233 };
234 
235 ISC_LIST(struct asyncio_reader) asyncio_reader_list;
236 
237 static void delete_asyncio_reader P((struct asyncio_reader *));
238 static struct asyncio_reader *new_asyncio_reader P((void));
239 static void add_asyncio_reader P((struct asyncio_reader *, enum desc_type));
240 static void remove_asyncio_reader P((struct asyncio_reader *));
241 
242 #endif /* !defined(HAVE_IO_COMPLETION_PORT) && defined(HAS_ROUTING_SOCKET) */
243 
244 static void init_async_notifications P((void));
245 
246 static	int create_sockets	P((u_short));
247 static	SOCKET	open_socket	P((struct sockaddr_storage *, int, int, struct interface *));
248 static	char *	fdbits		P((int, fd_set *));
249 static	void	set_reuseaddr	P((int));
250 static	isc_boolean_t	socket_broadcast_enable	 P((struct interface *, SOCKET, struct sockaddr_storage *));
251 static	isc_boolean_t	socket_broadcast_disable P((struct interface *, struct sockaddr_storage *));
252 
253 ISC_LIST(vsock_t)	fd_list;
254 
255 typedef struct remaddr remaddr_t;
256 
257 struct remaddr {
258       struct sockaddr_storage	 addr;
259       struct interface               *interface;
260       ISC_LINK(remaddr_t)	 link;
261 };
262 
263 ISC_LIST(remaddr_t)       remoteaddr_list;
264 
265 ISC_LIST(struct interface)     inter_list;
266 
267 static struct interface *wildipv4 = NULL;
268 static struct interface *wildipv6 = NULL;
269 
270 static void	add_fd_to_list	P((SOCKET, enum desc_type));
271 static void	close_and_delete_fd_from_list	P((SOCKET));
272 static void	add_addr_to_list	P((struct sockaddr_storage *, struct interface *));
273 static void	delete_addr_from_list	P((struct sockaddr_storage *));
274 static struct interface *find_addr_in_list	P((struct sockaddr_storage *));
275 static struct interface *find_flagged_addr_in_list P((struct sockaddr_storage *, int));
276 static void	create_wildcards	P((u_short));
277 static isc_boolean_t	address_okay	P((struct interface *));
278 static void		convert_isc_if		P((isc_interface_t *, struct interface *, u_short));
279 static void	delete_interface_from_list	P((struct interface *));
280 static struct interface *getinterface	P((struct sockaddr_storage *, int));
281 static struct interface *findlocalinterface	P((struct sockaddr_storage *, int));
282 static struct interface *findlocalcastinterface	P((struct sockaddr_storage *, int));
283 
284 /*
285  * Routines to read the ntp packets
286  */
287 #if !defined(HAVE_IO_COMPLETION_PORT)
288 static inline int     read_network_packet	P((SOCKET, struct interface *, l_fp));
289 static inline int     read_refclock_packet	P((SOCKET, struct refclockio *, l_fp));
290 #endif
291 
292 #ifdef SYS_WINNT
293 /*
294  * Windows 2000 systems incorrectly cause UDP sockets using WASRecvFrom
295  * to not work correctly, returning a WSACONNRESET error when a WSASendTo
296  * fails with an "ICMP port unreachable" response and preventing the
297  * socket from using the WSARecvFrom in subsequent operations.
298  * The function below fixes this, but requires that Windows 2000
299  * Service Pack 2 or later be installed on the system.  NT 4.0
300  * systems are not affected by this and work correctly.
301  * See Microsoft Knowledge Base Article Q263823 for details of this.
302  */
303 void
304 connection_reset_fix(
305 	SOCKET fd,
306 	struct sockaddr_storage *addr
307 	)
308 {
309 	DWORD dwBytesReturned = 0;
310 	BOOL  bNewBehavior = FALSE;
311 	DWORD status;
312 
313 	/*
314 	 * disable bad behavior using IOCTL: SIO_UDP_CONNRESET
315 	 * NT 4.0 has no problem
316 	 */
317 	if (isc_win32os_majorversion() >= 5) {
318 		status = WSAIoctl(fd, SIO_UDP_CONNRESET, &bNewBehavior,
319 				  sizeof(bNewBehavior), NULL, 0,
320 				  &dwBytesReturned, NULL, NULL);
321 		if (SOCKET_ERROR == status)
322 			netsyslog(LOG_ERR, "connection_reset_fix() "
323 					   "failed for address %s: %m",
324 					   stoa(addr));
325 	}
326 }
327 #endif
328 
329 /*
330  * on Unix systems the stdio library typically
331  * makes use of file descriptors in the lower
332  * integer range. stdio usually will make use
333  * of the file descriptor in the range of
334  * [0..FOPEN_MAX)
335  * in order to keep this range clean for socket
336  * file descriptors we attempt to move them above
337  * FOPEM_MAX. This is not as easy as it sounds as
338  * FOPEN_MAX changes from implementation to implementation
339  * and may exceed to current file decriptor limits.
340  * We are using following strategy:
341  * - keep a current socket fd boundary initialized with
342  *   max(0, min(getdtablesize() - FD_CHUNK, FOPEN_MAX))
343  * - attempt to move the descriptor to the boundary or
344  *   above.
345  *   - if that fails and boundary > 0 set boundary
346  *     to min(0, socket_fd_boundary - FD_CHUNK)
347  *     -> retry
348  *     if failure and boundary == 0 return old fd
349  *   - on success close old fd return new fd
350  *
351  * effects:
352  *   - fds will be moved above the socket fd boundary
353  *     if at all possible.
354  *   - the socket boundary will be reduced until
355  *     allocation is possible or 0 is reached - at this
356  *     point the algrithm will be disabled
357  */
358 static int move_fd(SOCKET fd)
359 {
360 #if !defined(SYS_WINNT) && defined(F_DUPFD)
361 #ifndef FD_CHUNK
362 #define FD_CHUNK	10
363 #endif
364 /*
365  * number of fds we would like to have for
366  * stdio FILE* available.
367  * we can pick a "low" number as our use of
368  * FILE* is limited to log files and temporarily
369  * to data and config files. Except for log files
370  * we don't keep the other FILE* open beyond the
371  * scope of the function that opened it.
372  */
373 #ifndef FD_PREFERRED_SOCKBOUNDARY
374 #define FD_PREFERRED_SOCKBOUNDARY 48
375 #endif
376 
377 #ifndef HAVE_GETDTABLESIZE
378 /*
379  * if we have no idea about the max fd value set up things
380  * so we will start at FOPEN_MAX
381  */
382 #define getdtablesize() (FOPEN_MAX+FD_CHUNK)
383 #endif
384 
385 #ifndef FOPEN_MAX
386 #define FOPEN_MAX	20	/* assume that for the lack of anything better */
387 #endif
388 	static SOCKET socket_boundary = -1;
389 	SOCKET newfd;
390 
391 	/*
392 	 * check whether boundary has be set up
393 	 * already
394 	 */
395 	if (socket_boundary == -1) {
396 		socket_boundary = max(0, min(getdtablesize() - FD_CHUNK,
397 					     min(FOPEN_MAX, FD_PREFERRED_SOCKBOUNDARY)));
398 #ifdef DEBUG
399 		msyslog(LOG_DEBUG, "ntp_io: estimated max descriptors: %d, initial socket boundary: %d",
400 			getdtablesize(), socket_boundary);
401 #endif
402 	}
403 
404 	/*
405 	 * Leave a space for stdio to work in. potentially moving the
406 	 * socket_boundary lower until allocation succeeds.
407 	 */
408 	do {
409 		if (fd >= 0 && fd < socket_boundary) {
410 			/* inside reserved range: attempt to move fd */
411 			newfd = fcntl(fd, F_DUPFD, socket_boundary);
412 
413 			if (newfd != -1) {
414 				/* success: drop the old one - return the new one */
415 				(void)close(fd);
416 				return (newfd);
417 			}
418 		} else {
419 			/* outside reserved range: no work - return the original one */
420 			return (fd);
421 		}
422 		socket_boundary = max(0, socket_boundary - FD_CHUNK);
423 #ifdef DEBUG
424 		msyslog(LOG_DEBUG, "ntp_io: selecting new socket boundary: %d",
425 			socket_boundary);
426 #endif
427 	} while (socket_boundary > 0);
428 #endif /* !defined(SYS_WINNT) && defined(F_DUPFD) */
429 	return (fd);
430 }
431 
432 #ifdef DEBUG_TIMING
433 /*
434  * collect timing information for various processing
435  * paths. currently we only pass then on to the file
436  * for later processing. this could also do histogram
437  * based analysis in other to reduce the load (and skew)
438  * dur to the file output
439  */
440 void
441 collect_timing(struct recvbuf *rb, const char *tag, int count, l_fp *dts)
442 {
443 	char buf[2048];
444 
445 	snprintf(buf, sizeof(buf), "%s %d %s %s",
446 		 (rb != NULL) ?
447 		 ((rb->dstadr) ? stoa(&rb->recv_srcadr) : "-REFCLOCK-") : "-",
448 		 count, lfptoa(dts, 9), tag);
449 	record_timing_stats(buf);
450 }
451 #endif
452 
453 /*
454  * About dynamic interfaces, sockets, reception and more...
455  *
456  * the code solves following tasks:
457  *
458  *   - keep a current list of active interfaces in order
459  *     to bind to to the interface address on NTP_PORT so that
460  *     all wild and specific bindings for NTP_PORT are taken by ntpd
461  *     to avoid other daemons messing with the time or sockets.
462  *   - all interfaces keep a list of peers that are referencing
463  *     the interface in order to quickly re-assign the peers to
464  *     new interface in case an interface is deleted (=> gone from system or
465  *     down)
466  *   - have a preconfigured socket ready with the right local address
467  *     for transmission and reception
468  *   - have an address list for all destination addresses used within ntpd
469  *     to find the "right" preconfigured socket.
470  *   - facilitate updating the internal interface list with respect to
471  *     the current kernel state
472  *
473  * special issues:
474  *
475  *   - mapping of multicast addresses to the interface affected is not always
476  *     one to one - especially on hosts with multiple interfaces
477  *     the code here currently allocates a separate interface entry for those
478  *     multicast addresses
479  *     iff it is able to bind to a *new* socket with the multicast address (flags |= MCASTIF)
480  *     in case of failure the multicast address is bound to an existing interface.
481  *   - on some systems it is perfectly legal to assign the same address to
482  *     multiple interfaces. Therefore this code does not keep a list of interfaces
483  *     but a list of interfaces that represent a unique address as determined by the kernel
484  *     by the procedure in findlocalinterface. Thus it is perfectly legal to see only
485  *     one representative of a group of real interfaces if they share the same address.
486  *
487  * Frank Kardel 20050910
488  */
489 
490 /*
491  * init_io - initialize I/O data structures and call socket creation routine
492  */
493 void
494 init_io(void)
495 {
496 #ifdef SYS_WINNT
497 	init_io_completion_port();
498 
499 	if (!Win32InitSockets())
500 	{
501 		netsyslog(LOG_ERR, "No useable winsock.dll: %m");
502 		exit(1);
503 	}
504 	init_transmitbuff();
505 #endif /* SYS_WINNT */
506 
507 	/*
508 	 * Init buffer free list and stat counters
509 	 */
510 	init_recvbuff(RECV_INIT);
511 
512 	packets_dropped = packets_received = 0;
513 	packets_ignored = 0;
514 	packets_sent = packets_notsent = 0;
515 	handler_calls = handler_pkts = 0;
516 	io_timereset = 0;
517 	loopback_interface = NULL;
518 	any_interface = NULL;
519 	any6_interface = NULL;
520 
521 #ifdef REFCLOCK
522 	refio = NULL;
523 #endif
524 
525 #if defined(HAVE_SIGNALED_IO)
526 	(void) set_signal();
527 #endif
528 
529 	ISC_LIST_INIT(fd_list);
530 
531 #if !defined(HAVE_IO_COMPLETION_PORT) && defined(HAS_ROUTING_SOCKET)
532 	ISC_LIST_INIT(asyncio_reader_list);
533 #endif
534 
535         ISC_LIST_INIT(remoteaddr_list);
536 
537 	ISC_LIST_INIT(inter_list);
538 
539 	/*
540 	 * Create the sockets
541 	 */
542 	BLOCKIO();
543 	(void) create_sockets(htons(NTP_PORT));
544 	UNBLOCKIO();
545 
546 	init_async_notifications();
547 
548 	DPRINTF(3, ("init_io: maxactivefd %d\n", maxactivefd));
549 }
550 
551 #ifdef DEBUG
552 /*
553  * function to dump the contents of the interface structure
554  * for debugging use only.
555  */
556 void
557 interface_dump(struct interface *itf)
558 {
559 	u_char* cp;
560 	int i;
561 	/* Limit the size of the sockaddr_storage hex dump */
562 	int maxsize = min(32, sizeof(struct sockaddr_storage));
563 
564 	printf("Dumping interface: %p\n", itf);
565 	printf("fd = %d\n", itf->fd);
566 	printf("bfd = %d\n", itf->bfd);
567 	printf("sin = %s,\n", stoa(&(itf->sin)));
568 	cp = (u_char*) &(itf->sin);
569 	for(i = 0; i < maxsize; i++)
570 	{
571 		printf("%02x", *cp++);
572 		if((i+1)%4 == 0)
573 			printf(" ");
574 	}
575 	printf("\n");
576 	printf("bcast = %s,\n", stoa(&(itf->bcast)));
577 	cp = (u_char*) &(itf->bcast);
578 	for(i = 0; i < maxsize; i++)
579 	{
580 		printf("%02x", *cp++);
581 		if((i+1)%4 == 0)
582 			printf(" ");
583 	}
584 	printf("\n");
585 	printf("mask = %s,\n", stoa(&(itf->mask)));
586 	cp = (u_char*) &(itf->mask);
587 	for(i = 0; i < maxsize; i++)
588 	{
589 		printf("%02x", *cp++);
590 		if((i+1)%4 == 0)
591 			printf(" ");
592 	}
593 	printf("\n");
594 	printf("name = %s\n", itf->name);
595 	printf("flags = 0x%08x\n", itf->flags);
596 	printf("last_ttl = %d\n", itf->last_ttl);
597 	printf("addr_refid = %08x\n", itf->addr_refid);
598 	printf("num_mcast = %d\n", itf->num_mcast);
599 	printf("received = %ld\n", itf->received);
600 	printf("sent = %ld\n", itf->sent);
601 	printf("notsent = %ld\n", itf->notsent);
602 	printf("ifindex = %u\n", itf->ifindex);
603 	printf("scopeid = %u\n", itf->scopeid);
604 	printf("peercnt = %u\n", itf->peercnt);
605 	printf("phase = %u\n", itf->phase);
606 }
607 
608 /*
609  * print_interface - helper to output debug information
610  */
611 static void
612 print_interface(struct interface *iface, char *pfx, char *sfx)
613 {
614 	printf("%sinterface #%d: fd=%d, bfd=%d, name=%s, flags=0x%x, scope=%d, ifindex=%d",
615 	       pfx,
616 	       iface->ifnum,
617 	       iface->fd,
618 	       iface->bfd,
619 	       iface->name,
620 	       iface->flags,
621 	       iface->scopeid,
622 	       iface->ifindex);
623 	/* Leave these as three printf calls. */
624 	printf(", sin=%s",
625 	       stoa((&iface->sin)));
626 	if (iface->flags & INT_BROADCAST)
627 		printf(", bcast=%s,",
628 		       stoa((&iface->bcast)));
629 	if (iface->family == AF_INET)
630 	  printf(", mask=%s",
631 		 stoa((&iface->mask)));
632 	printf(", %s:%s", iface->ignore_packets == ISC_FALSE ? "Enabled" : "Disabled", sfx);
633 	if (debug > 4)	/* in-depth debugging only */
634 		interface_dump(iface);
635 }
636 
637 #endif
638 
639 #if !defined(HAVE_IO_COMPLETION_PORT) && defined(HAS_ROUTING_SOCKET)
640 /*
641  * create an asyncio_reader structure
642  */
643 static struct asyncio_reader *
644 new_asyncio_reader()
645 {
646 	struct asyncio_reader *reader;
647 
648 	reader = (struct asyncio_reader *)emalloc(sizeof(struct asyncio_reader));
649 
650 	memset((char *)reader, 0, sizeof(*reader));
651 	ISC_LINK_INIT(reader, link);
652 	reader->fd = INVALID_SOCKET;
653 	return reader;
654 }
655 
656 /*
657  * delete a reader
658  */
659 static void
660 delete_asyncio_reader(struct asyncio_reader *reader)
661 {
662 	free(reader);
663 }
664 
665 /*
666  * add asynchio_reader
667  */
668 static void
669 add_asyncio_reader(struct asyncio_reader *reader, enum desc_type type)
670 {
671 	ISC_LIST_APPEND(asyncio_reader_list, reader, link);
672 	add_fd_to_list(reader->fd, type);
673 }
674 
675 /*
676  * remove asynchio_reader
677  */
678 static void
679 remove_asyncio_reader(struct asyncio_reader *reader)
680 {
681 	ISC_LIST_UNLINK_TYPE(asyncio_reader_list, reader, link, struct asyncio_reader);
682 
683 	if (reader->fd != INVALID_SOCKET)
684 		close_and_delete_fd_from_list(reader->fd);
685 
686 	reader->fd = INVALID_SOCKET;
687 }
688 #endif /* !defined(HAVE_IO_COMPLETION_PORT) && defined(HAS_ROUTING_SOCKET) */
689 
690 /*
691  * interface list enumerator - visitor pattern
692  */
693 void
694 interface_enumerate(interface_receiver_t receiver, void *data)
695 {
696 	interface_info_t ifi;
697         struct interface *interf;
698 
699 	ifi.action = IFS_EXISTS;
700 
701 	for (interf = ISC_LIST_HEAD(inter_list);
702 	     interf != NULL;
703 	     interf = ISC_LIST_NEXT(interf, link)) {
704 		ifi.interface = interf;
705 		receiver(data, &ifi);
706 	}
707 }
708 
709 /*
710  * do standard initialization of interface structure
711  */
712 static void
713 init_interface(struct interface *interface)
714 {
715 	memset((char *)interface, 0, sizeof(struct interface));
716 	ISC_LINK_INIT(interface, link);
717 	ISC_LIST_INIT(interface->peers);
718 	interface->fd = INVALID_SOCKET;
719 	interface->bfd = INVALID_SOCKET;
720 	interface->num_mcast = 0;
721 	interface->received = 0;
722 	interface->sent = 0;
723 	interface->notsent = 0;
724 	interface->peercnt = 0;
725 	interface->phase = sys_interphase;
726 }
727 
728 /*
729  * create new interface structure initialize from
730  * template structure or via standard initialization
731  * function
732  */
733 static struct interface *
734 new_interface(struct interface *interface)
735 {
736 	static u_int sys_ifnum = 0;
737 
738 	struct interface *iface = (struct interface *)emalloc(sizeof(struct interface));
739 
740 	if (interface != NULL)
741 	{
742 		memcpy((char*)iface, (char*)interface, sizeof(*interface));
743 	}
744 	else
745 	{
746 		init_interface(iface);
747 	}
748 
749 	iface->ifnum = sys_ifnum++;  /* count every new instance of an interface in the system */
750 	iface->starttime = current_time;
751 
752 	return iface;
753 }
754 
755 /*
756  * return interface storage into free memory pool
757  */
758 static void
759 delete_interface(struct interface *interface)
760 {
761 	free(interface);
762 }
763 
764 /*
765  * link interface into list of known interfaces
766  */
767 static void
768 add_interface(struct interface *interface)
769 {
770 	static struct interface *listhead = NULL;
771 
772 	/*
773 	 * For ntpd, the first few interfaces (wildcard, localhost)
774 	 * will never be removed.  This means inter_list.head is
775 	 * unchanging once initialized.  Take advantage of that to
776 	 * watch for changes and catch corruption earlier.  This
777 	 * helped track down corruption caused by using FD_SET with
778 	 * a descriptor numerically larger than FD_SETSIZE.
779 	 */
780 	if (NULL == listhead)
781 		listhead = inter_list.head;
782 
783 	if (listhead != inter_list.head) {
784 		msyslog(LOG_ERR, "add_interface inter_list.head corrupted: was %p now %p",
785 			listhead, inter_list.head);
786 		exit(1);
787 	}
788 	/*
789 	 * Calculate the address hash
790 	 */
791 	interface->addr_refid = addr2refid(&interface->sin);
792 
793 	ISC_LIST_APPEND(inter_list, interface, link);
794 	ninterfaces++;
795 }
796 
797 /*
798  * remove interface from known interface list and clean up
799  * associated resources
800  */
801 static void
802 remove_interface(struct interface *interface)
803 {
804 	struct sockaddr_storage resmask;
805 
806 	ISC_LIST_UNLINK_TYPE(inter_list, interface, link, struct interface);
807 
808 	delete_interface_from_list(interface);
809 
810 	if (interface->fd != INVALID_SOCKET)
811 	{
812 		msyslog(LOG_INFO, "Deleting interface #%d %s, %s#%d, interface stats: received=%ld, sent=%ld, dropped=%ld, active_time=%ld secs",
813 			interface->ifnum,
814 			interface->name,
815 			stoa((&interface->sin)),
816 			NTP_PORT,  /* XXX should extract port from sin structure */
817 			interface->received,
818 			interface->sent,
819 			interface->notsent,
820 			current_time - interface->starttime);
821 
822 		close_and_delete_fd_from_list(interface->fd);
823 	}
824 
825 	if (interface->bfd != INVALID_SOCKET)
826 	{
827 		msyslog(LOG_INFO, "Deleting interface #%d %s, broadcast address %s#%d",
828 			interface->ifnum,
829 			interface->name,
830 			stoa((&interface->bcast)),
831 			(u_short) NTP_PORT);  /* XXX extract port from sin structure */
832 		close_and_delete_fd_from_list(interface->bfd);
833 	}
834 
835 	ninterfaces--;
836 	ntp_monclearinterface(interface);
837 
838 	/* remove restrict interface entry */
839 
840 	/*
841 	 * Blacklist bound interface address
842 	 */
843 	SET_HOSTMASK(&resmask, interface->sin.ss_family);
844 	hack_restrict(RESTRICT_REMOVEIF, &interface->sin, &resmask,
845 		      RESM_NTPONLY|RESM_INTERFACE, RES_IGNORE);
846 }
847 
848 static void
849 list_if_listening(struct interface *interface, u_short port)
850 {
851 	msyslog(LOG_INFO, "Listening on interface #%d %s, %s#%d %s",
852 		interface->ifnum,
853 		interface->name,
854 		stoa((&interface->sin)),
855 		ntohs( (u_short) port),
856 		(interface->ignore_packets == ISC_FALSE) ?
857 		"Enabled": "Disabled");
858 }
859 
860 static void
861 create_wildcards(u_short port) {
862 	isc_boolean_t okipv4 = ISC_TRUE;
863 	/*
864 	 * create pseudo-interface with wildcard IPv4 address
865 	 */
866 #ifdef IPV6_V6ONLY
867 	if(isc_net_probeipv4() != ISC_R_SUCCESS)
868 		okipv4 = ISC_FALSE;
869 #endif
870 
871 	if(okipv4 == ISC_TRUE) {
872 	        struct interface *interface = new_interface(NULL);
873 
874 		interface->family = AF_INET;
875 		interface->sin.ss_family = AF_INET;
876 		((struct sockaddr_in*)&interface->sin)->sin_addr.s_addr = htonl(INADDR_ANY);
877 		((struct sockaddr_in*)&interface->sin)->sin_port = port;
878 		(void) strncpy(interface->name, "wildcard", sizeof(interface->name));
879 		interface->mask.ss_family = AF_INET;
880 		((struct sockaddr_in*)&interface->mask)->sin_addr.s_addr = htonl(~(u_int32)0);
881 		interface->flags = INT_BROADCAST | INT_UP | INT_WILDCARD;
882 		interface->ignore_packets = ISC_TRUE;
883 #if defined(MCAST)
884 		/*
885 		 * enable possible multicast reception on the broadcast socket
886 		 */
887 		interface->bcast.ss_family = AF_INET;
888 		((struct sockaddr_in*)&interface->bcast)->sin_port = port;
889 		((struct sockaddr_in*)&interface->bcast)->sin_addr.s_addr = htonl(INADDR_ANY);
890 #endif /* MCAST */
891 		interface->fd = open_socket(&interface->sin,
892 				 interface->flags, 1, interface);
893 
894 		if (interface->fd != INVALID_SOCKET) {
895 			wildipv4 = interface;
896 			any_interface = interface;
897 
898 			add_addr_to_list(&interface->sin, interface);
899 			add_interface(interface);
900 			list_if_listening(interface, port);
901 		} else {
902 			msyslog(LOG_ERR, "unable to bind to wildcard socket address %s - another process may be running - EXITING",
903 				stoa((&interface->sin)));
904 			exit(1);
905 		}
906 	}
907 
908 #ifdef INCLUDE_IPV6_SUPPORT
909 	/*
910 	 * create pseudo-interface with wildcard IPv6 address
911 	 */
912 	if (isc_net_probeipv6() == ISC_R_SUCCESS) {
913 	        struct interface *interface = new_interface(NULL);
914 
915 		interface->family = AF_INET6;
916 		interface->sin.ss_family = AF_INET6;
917 		((struct sockaddr_in6*)&interface->sin)->sin6_addr = in6addr_any;
918  		((struct sockaddr_in6*)&interface->sin)->sin6_port = port;
919 # ifdef ISC_PLATFORM_HAVESCOPEID
920  		((struct sockaddr_in6*)&interface->sin)->sin6_scope_id = 0;
921 # endif
922 		(void) strncpy(interface->name, "wildcard", sizeof(interface->name));
923 		interface->mask.ss_family = AF_INET6;
924 		memset(&((struct sockaddr_in6*)&interface->mask)->sin6_addr.s6_addr, 0xff, sizeof(struct in6_addr));
925 		interface->flags = INT_UP | INT_WILDCARD;
926 		interface->ignore_packets = ISC_TRUE;
927 
928 		interface->fd = open_socket(&interface->sin,
929 				 interface->flags, 1, interface);
930 
931 		if (interface->fd != INVALID_SOCKET) {
932 			wildipv6 = interface;
933 			any6_interface = interface;
934 			add_addr_to_list(&interface->sin, interface);
935 			add_interface(interface);
936 			list_if_listening(interface, port);
937 		} else {
938 			msyslog(LOG_ERR, "unable to bind to wildcard socket address %s - another process may be running - EXITING",
939 				stoa((&interface->sin)));
940 			exit(1);
941 		}
942 	}
943 #endif
944 }
945 
946 
947 static isc_boolean_t
948 address_okay(struct interface *iface) {
949 
950 	DPRINTF(4, ("address_okay: listen Virtual: %d, IF name: %s\n",
951 		    listen_to_virtual_ips, iface->name));
952 
953 	/*
954 	 * Always allow the loopback
955 	 */
956 	if((iface->flags & INT_LOOPBACK) != 0) {
957 		DPRINTF(4, ("address_okay: loopback - OK\n"));
958 		return (ISC_TRUE);
959 	}
960 
961 	/*
962 	 * Check if the interface is specified
963 	 */
964 	if (specific_interface != NULL) {
965 		if (strcasecmp(iface->name, specific_interface) == 0) {
966 			DPRINTF(4, ("address_okay: specific interface name matched - OK\n"));
967 			return (ISC_TRUE);
968 		} else {
969 			DPRINTF(4, ("address_okay: specific interface name NOT matched - FAIL\n"));
970 			return (ISC_FALSE);
971 		}
972 	}
973 	else {
974 		if (listen_to_virtual_ips == 0  &&
975 		    (strchr(iface->name, (int)':') != NULL)) {
976 			DPRINTF(4, ("address_okay: virtual ip/alias - FAIL\n"));
977 			return (ISC_FALSE);
978 		}
979 	}
980 
981 	DPRINTF(4, ("address_okay: OK\n"));
982 	return (ISC_TRUE);
983 }
984 
985 static void
986 convert_isc_if(isc_interface_t *isc_if, struct interface *itf, u_short port)
987 {
988 	itf->scopeid = 0;
989 	itf->family = (short) isc_if->af;
990 	strcpy(itf->name, isc_if->name);
991 
992 	if(isc_if->af == AF_INET) {
993 		itf->sin.ss_family = (u_short) isc_if->af;
994 		memcpy(&(((struct sockaddr_in*)&itf->sin)->sin_addr),
995 		       &(isc_if->address.type.in),
996 		       sizeof(struct in_addr));
997 		((struct sockaddr_in*)&itf->sin)->sin_port = port;
998 
999 		if((isc_if->flags & INTERFACE_F_BROADCAST) != 0) {
1000 			itf->flags |= INT_BROADCAST;
1001 			itf->bcast.ss_family = itf->sin.ss_family;
1002 			memcpy(&(((struct sockaddr_in*)&itf->bcast)->sin_addr),
1003 			       &(isc_if->broadcast.type.in),
1004 				 sizeof(struct in_addr));
1005 			((struct sockaddr_in*)&itf->bcast)->sin_port = port;
1006 		}
1007 
1008 		itf->mask.ss_family = itf->sin.ss_family;
1009 		memcpy(&(((struct sockaddr_in*)&itf->mask)->sin_addr),
1010 		       &(isc_if->netmask.type.in),
1011 		       sizeof(struct in_addr));
1012 		((struct sockaddr_in*)&itf->mask)->sin_port = port;
1013 	}
1014 #ifdef INCLUDE_IPV6_SUPPORT
1015 	else if (isc_if->af == AF_INET6) {
1016 		itf->sin.ss_family = (u_short) isc_if->af;
1017 		memcpy(&(((struct sockaddr_in6 *)&itf->sin)->sin6_addr),
1018 		       &(isc_if->address.type.in6),
1019 		       sizeof(((struct sockaddr_in6 *)&itf->sin)->sin6_addr));
1020 		((struct sockaddr_in6 *)&itf->sin)->sin6_port = port;
1021 
1022 #ifdef ISC_PLATFORM_HAVESCOPEID
1023 		((struct sockaddr_in6 *)&itf->sin)->sin6_scope_id = isc_netaddr_getzone(&isc_if->address);
1024 		itf->scopeid = isc_netaddr_getzone(&isc_if->address);
1025 #endif
1026 		itf->mask.ss_family = itf->sin.ss_family;
1027 		memcpy(&(((struct sockaddr_in6 *)&itf->mask)->sin6_addr),
1028 		       &(isc_if->netmask.type.in6),
1029 		       sizeof(struct in6_addr));
1030 		((struct sockaddr_in6 *)&itf->mask)->sin6_port = port;
1031 		/* Copy the interface index */
1032 		itf->ifindex = isc_if->ifindex;
1033 	}
1034 #endif /* INCLUDE_IPV6_SUPPORT */
1035 
1036 
1037 	/* Process the rest of the flags */
1038 
1039 	if((isc_if->flags & INTERFACE_F_UP) != 0)
1040 		itf->flags |= INT_UP;
1041 	if((isc_if->flags & INTERFACE_F_LOOPBACK) != 0)
1042 		itf->flags |= INT_LOOPBACK;
1043 	if((isc_if->flags & INTERFACE_F_POINTTOPOINT) != 0)
1044 		itf->flags |= INT_PPP;
1045 	if((isc_if->flags & INTERFACE_F_MULTICAST) != 0)
1046 		itf->flags |= INT_MULTICAST;
1047 
1048 }
1049 
1050 /*
1051  * refresh_interface
1052  *
1053  * some OSes have been observed to keep
1054  * cached routes even when more specific routes
1055  * become available.
1056  * this can be mitigated by re-binding
1057  * the socket.
1058  */
1059 static int
1060 refresh_interface(struct interface * interface)
1061 {
1062 #ifdef  OS_MISSES_SPECIFIC_ROUTE_UPDATES
1063 	if (interface->fd != INVALID_SOCKET)
1064 	{
1065 		close_and_delete_fd_from_list(interface->fd);
1066 		interface->fd = open_socket(&interface->sin,
1067 					    interface->flags, 0, interface);
1068 		 /*
1069 		  * reset TTL indication so TTL is is set again
1070 		  * next time around
1071 		  */
1072 		interface->last_ttl = 0;
1073 		return interface->fd != INVALID_SOCKET;
1074 	}
1075 	else
1076 	{
1077 		return 0;	/* invalid sockets are not refreshable */
1078 	}
1079 #else /* !OS_MISSES_SPECIFIC_ROUTE_UPDATES */
1080 	return interface->fd != INVALID_SOCKET;
1081 #endif /* !OS_MISSES_SPECIFIC_ROUTE_UPDATES */
1082 }
1083 
1084 /*
1085  * interface_update - externally callable update function
1086  */
1087 void
1088 interface_update(interface_receiver_t receiver, void *data)
1089 {
1090 	if (!disable_dynamic_updates) {
1091 		int new_interface_found;
1092 
1093 		BLOCKIO();
1094 		new_interface_found = update_interfaces(htons(NTP_PORT), receiver, data);
1095 		UNBLOCKIO();
1096 
1097 		if (new_interface_found) {
1098 #ifdef DEBUG
1099 			msyslog(LOG_DEBUG, "new interface(s) found: waking up resolver");
1100 #endif
1101 #ifdef SYS_WINNT
1102 			/* wake up the resolver thread */
1103 			if (ResolverEventHandle != NULL)
1104 				SetEvent(ResolverEventHandle);
1105 #else
1106 			/* write any single byte to the pipe to wake up the resolver process */
1107 			write( resolver_pipe_fd[1], &new_interface_found, 1 );
1108 #endif
1109 		}
1110 	}
1111 }
1112 
1113 /*
1114  * find out if a given interface structure contains
1115  * a wildcard address
1116  */
1117 static int
1118 is_wildcard_addr(struct sockaddr_storage *sas)
1119 {
1120 	if (sas->ss_family == AF_INET &&
1121 	    ((struct sockaddr_in*)sas)->sin_addr.s_addr == htonl(INADDR_ANY))
1122 		return 1;
1123 
1124 #ifdef INCLUDE_IPV6_SUPPORT
1125 	if (sas->ss_family == AF_INET6 &&
1126 	    memcmp(&((struct sockaddr_in6*)sas)->sin6_addr, &in6addr_any,
1127 		   sizeof(in6addr_any)) == 0)
1128 		return 1;
1129 #endif
1130 
1131 	return 0;
1132 }
1133 
1134 #ifdef OS_NEEDS_REUSEADDR_FOR_IFADDRBIND
1135 /*
1136  * enable/disable re-use of wildcard address socket
1137  */
1138 static void
1139 set_wildcard_reuse(int family, int on)
1140 {
1141 	int onvalue = 1;
1142 	int offvalue = 0;
1143 	int *onoff;
1144 	SOCKET fd = INVALID_SOCKET;
1145 
1146 	onoff = on ? &onvalue : &offvalue;
1147 
1148 	switch (family) {
1149 	case AF_INET:
1150 		if (any_interface) {
1151 			fd = any_interface->fd;
1152 		}
1153 		break;
1154 
1155 #ifdef INCLUDE_IPV6_SUPPORT
1156 	case AF_INET6:
1157 		if (any6_interface) {
1158 			fd = any6_interface->fd;
1159 		}
1160 		break;
1161 #endif /* !INCLUDE_IPV6_SUPPORT */
1162 	}
1163 
1164 	if (fd != INVALID_SOCKET) {
1165 		if (setsockopt(fd, SOL_SOCKET,
1166 			       SO_REUSEADDR, (char *)onoff,
1167 			       sizeof(*onoff))) {
1168 			netsyslog(LOG_ERR, "set_wildcard_reuse: setsockopt(SO_REUSEADDR, %s) failed: %m", *onoff ? "on" : "off");
1169 		}
1170 		DPRINTF(4, ("set SO_REUSEADDR to %s on %s\n", *onoff ? "ON" : "OFF",
1171 			    stoa((family == AF_INET) ?
1172 				  &any_interface->sin : &any6_interface->sin)));
1173 	}
1174 }
1175 #endif /* OS_NEEDS_REUSEADDR_FOR_IFADDRBIND */
1176 
1177 #ifdef INCLUDE_IPV6_SUPPORT
1178 static isc_boolean_t
1179 is_not_bindable(struct sockaddr *sa, char *name)
1180 {
1181 #if defined(SIOCGIFAFLAG_IN6) && \
1182 	   (defined(IN6_IFF_ANYCAST) || defined(IN6_IFF_NOTREADY))
1183 	struct in6_ifreq ifr6;
1184 	int fd;
1185 	u_int32_t flags6, exclude = 0;
1186 
1187 	if (sa->sa_family != AF_INET6)
1188 		return ISC_FALSE;
1189 	if ((fd = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1190 		return ISC_FALSE;
1191 	memset(&ifr6, 0, sizeof(ifr6));
1192 	memcpy(&ifr6.ifr_addr, (struct sockaddr_in6 *)sa,
1193 	    sizeof(struct sockaddr_in6));
1194 	strlcpy(ifr6.ifr_name, name, IF_NAMESIZE);
1195 	if (ioctl(fd, SIOCGIFAFLAG_IN6, &ifr6) < 0) {
1196 		close(fd);
1197 		return ISC_FALSE;
1198 	}
1199 	close(fd);
1200 	flags6 = ifr6.ifr_ifru.ifru_flags6;
1201 #if defined(IN6_IFF_ANYCAST)
1202 	exclude |= IN6_IFF_ANYCAST;
1203 #endif /* !IN6_IFF_ANYCAST */
1204 #if defined(IN6_IFF_NOTREADY)
1205 	exclude |= IN6_IFF_NOTREADY;
1206 #endif /* !IN6_IFF_NOTREADY */
1207 	if ((flags6 & exclude) != 0)
1208 		return ISC_TRUE;
1209 #endif /* !SIOCGIFAFLAG_IN6 || !(IN6_IFF_ANYCAST && IN6_IFF_NOTREADY) */
1210 	return ISC_FALSE;
1211 }
1212 #endif /* !INCLUDE_IPV6_SUPPORT */
1213 
1214 /*
1215  * update_interface strategy
1216  *
1217  * toggle configuration phase
1218  *
1219  * Phase 1:
1220  * forall currently existing interfaces
1221  *   if address is known:
1222  *       drop socket - rebind again
1223  *
1224  *   if address is NOT known:
1225  *     attempt to create a new interface entry
1226  *
1227  * Phase 2:
1228  * forall currently known non MCAST and WILDCARD interfaces
1229  *   if interface does not match configuration phase (not seen in phase 1):
1230  *     remove interface from known interface list
1231  *     forall peers associated with this interface
1232  *       disconnect peer from this interface
1233  *
1234  * Phase 3:
1235  *   attempt to re-assign interfaces to peers
1236  *
1237  */
1238 
1239 static int
1240 update_interfaces(
1241 	u_short port,
1242 	interface_receiver_t receiver,
1243 	void *data
1244 	)
1245 {
1246 	interface_info_t ifi;
1247 	isc_mem_t *mctx = NULL;
1248 	isc_interfaceiter_t *iter = NULL;
1249 	isc_boolean_t scan_ipv4 = ISC_FALSE;
1250 	isc_boolean_t scan_ipv6 = ISC_FALSE;
1251 	isc_result_t result;
1252 	int new_interface_found = 0;
1253 
1254 	DPRINTF(3, ("update_interfaces(%d)\n", ntohs( (u_short) port)));
1255 
1256 #ifdef INCLUDE_IPV6_SUPPORT
1257 	if (isc_net_probeipv6() == ISC_R_SUCCESS)
1258 		scan_ipv6 = ISC_TRUE;
1259 #if defined(DEBUG)
1260 	else
1261 		if (debug)
1262 			netsyslog(LOG_ERR, "no IPv6 interfaces found");
1263 #endif
1264 #endif
1265 	if (isc_net_probeipv6() == ISC_R_SUCCESS)
1266 		scan_ipv6 = ISC_TRUE;
1267 #if defined(ISC_PLATFORM_HAVEIPV6) && defined(DEBUG)
1268 	else
1269 		if (debug)
1270 			netsyslog(LOG_ERR, "no IPv6 interfaces found");
1271 #endif
1272 
1273 	if (isc_net_probeipv4() == ISC_R_SUCCESS)
1274 		scan_ipv4 = ISC_TRUE;
1275 #ifdef DEBUG
1276 	else
1277 		if(debug)
1278 			netsyslog(LOG_ERR, "no IPv4 interfaces found");
1279 #endif
1280 	/*
1281 	 * phase one - scan interfaces
1282 	 * - create those that are not found
1283 	 * - update those that are found
1284 	 */
1285 
1286 	result = isc_interfaceiter_create(mctx, &iter);
1287 
1288 	if (result != ISC_R_SUCCESS)
1289 		return 0;
1290 
1291 	sys_interphase ^= 0x1;	/* toggle system phase for finding untouched (to be deleted) interfaces */
1292 
1293 	for (result = isc_interfaceiter_first(iter);
1294 	     result == ISC_R_SUCCESS;
1295 	     result = isc_interfaceiter_next(iter))
1296 	{
1297 		isc_interface_t isc_if;
1298 		unsigned int family;
1299 		struct interface interface;
1300 		struct interface *iface;
1301 
1302 		result = isc_interfaceiter_current(iter, &isc_if);
1303 
1304 		if (result != ISC_R_SUCCESS)
1305 			break;
1306 
1307 		/* See if we have a valid family to use */
1308 		family = isc_if.address.family;
1309 		if (family != AF_INET && family != AF_INET6)
1310 			continue;
1311 		if (scan_ipv4 == ISC_FALSE && family == AF_INET)
1312 			continue;
1313 		if (scan_ipv6 == ISC_FALSE && family == AF_INET6)
1314 			continue;
1315 
1316 		/*
1317 		 * create prototype
1318 		 */
1319 		init_interface(&interface);
1320 
1321 		convert_isc_if(&isc_if, &interface, port);
1322 
1323 		/*
1324 		 * Check to see if we are going to use the interface
1325 		 * If we don't use it we mark it to drop any packet
1326 		 * received but we still must create the socket and
1327 		 * bind to it. This prevents other apps binding to it
1328 		 * and potentially causing problems with more than one
1329 		 * process fiddling with the clock
1330 		 */
1331 		if (address_okay(&interface) == ISC_TRUE) {
1332 			interface.ignore_packets = ISC_FALSE;
1333 		}
1334 		else {
1335 			interface.ignore_packets = ISC_TRUE;
1336 		}
1337 
1338 		DPRINT_INTERFACE(4, (&interface, "examining ", "\n"));
1339 
1340 		if (!(interface.flags & INT_UP))  { /* interfaces must be UP to be usable */
1341 			DPRINTF(4, ("skipping interface %s (%s) - DOWN\n", interface.name, stoa(&interface.sin)));
1342 			continue;
1343 		}
1344 
1345 		/*
1346 		 * skip any interfaces UP and bound to a wildcard
1347 		 * address - some dhcp clients produce that in the
1348 		 * wild
1349 		 */
1350 		if (is_wildcard_addr(&interface.sin))
1351 			continue;
1352 
1353 #ifdef INCLUDE_IPV6_SUPPORT
1354 		if (is_not_bindable((struct sockaddr *)&interface.sin, isc_if.name))
1355 			continue;
1356 #endif /* !INCLUDE_IPV6_SUPPORT */
1357 
1358 		/*
1359 		 * map to local *address* in order
1360 		 * to map all duplicate interfaces to an interface structure
1361 		 * with the appropriate socket (our name space is
1362 		 * (ip-address) - NOT (interface name, ip-address))
1363 		 */
1364 		iface = getinterface(&interface.sin, INT_WILDCARD);
1365 
1366 		if (iface && refresh_interface(iface))
1367 		{
1368 			/*
1369 			 * found existing and up to date interface - mark present
1370 			 */
1371 
1372 			iface->phase = sys_interphase;
1373 			DPRINT_INTERFACE(4, (iface, "updating ", " present\n"));
1374 			ifi.action = IFS_EXISTS;
1375 			ifi.interface = iface;
1376 			if (receiver)
1377 				receiver(data, &ifi);
1378 		}
1379 		else
1380 		{
1381 			/*
1382 			 * this is new or refreshing failed - add to our interface list
1383 			 * if refreshing failed we will delete the interface structure in
1384 			 * phase 2 as the interface was not marked current. We can bind to
1385 			 * the address as the refresh code already closed the offending socket
1386 			 */
1387 
1388 			iface = create_interface(port, &interface);
1389 
1390 			if (iface)
1391 			{
1392 				ifi.action = IFS_CREATED;
1393 				ifi.interface = iface;
1394 				if (receiver)
1395 					receiver(data, &ifi);
1396 
1397 				new_interface_found = 1;
1398 
1399 				DPRINT_INTERFACE(3, (iface, "updating ", " new - created\n"));
1400 			}
1401 			else
1402 			{
1403 				DPRINT_INTERFACE(3, (&interface, "updating ", " new - creation FAILED"));
1404 
1405 				msyslog(LOG_INFO, "failed to initialize interface for address %s", stoa(&interface.sin));
1406 				continue;
1407 			}
1408 		}
1409 	}
1410 
1411 	isc_interfaceiter_destroy(&iter);
1412 
1413 	/*
1414 	 * phase 2 - delete gone interfaces - reassigning peers to other interfaces
1415 	 */
1416 	{
1417 		struct interface *interf = ISC_LIST_HEAD(inter_list);
1418 
1419 		while (interf != NULL)
1420 		{
1421 			struct interface *next = ISC_LIST_NEXT(interf, link);
1422 
1423 			if (!(interf->flags & (INT_WILDCARD|INT_MCASTIF))) {
1424 				/*
1425 				 * if phase does not match sys_phase this interface was not
1426 				 * enumerated during interface scan - so it is gone and
1427 				 * will be deleted here unless it is solely an MCAST/WILDCARD interface
1428 				 */
1429 				if (interf->phase != sys_interphase) {
1430 					struct peer *peer;
1431 					DPRINT_INTERFACE(3, (interf, "updating ", "GONE - deleting\n"));
1432 					remove_interface(interf);
1433 
1434 					ifi.action = IFS_DELETED;
1435 					ifi.interface = interf;
1436 					if (receiver)
1437 						receiver(data, &ifi);
1438 
1439 					peer = ISC_LIST_HEAD(interf->peers);
1440 					/*
1441 					 * disconnect peer from deleted interface
1442 					 */
1443 					while (peer != NULL) {
1444 						struct peer *npeer = ISC_LIST_NEXT(peer, ilink);
1445 
1446 						/*
1447 						 * this one just lost it's interface
1448 						 */
1449 						set_peerdstadr(peer, NULL);
1450 
1451 						peer = npeer;
1452 					}
1453 
1454 					/*
1455 					 * update globals in case we lose
1456 					 * a loopback interface
1457 					 */
1458 					if (interf == loopback_interface)
1459 						loopback_interface = NULL;
1460 
1461 					delete_interface(interf);
1462 				}
1463 			}
1464 			interf = next;
1465 		}
1466 	}
1467 
1468 	/*
1469 	 * phase 3 - re-configure as the world has changed if necessary
1470 	 */
1471 	refresh_all_peerinterfaces();
1472 	return new_interface_found;
1473 }
1474 
1475 
1476 /*
1477  * create_sockets - create a socket for each interface plus a default
1478  *			socket for when we don't know where to send
1479  */
1480 static int
1481 create_sockets(
1482 	u_short port
1483 	)
1484 {
1485 #ifndef HAVE_IO_COMPLETION_PORT
1486 	/*
1487 	 * I/O Completion Ports don't care about the select and FD_SET
1488 	 */
1489 	maxactivefd = 0;
1490 	FD_ZERO(&activefds);
1491 #endif
1492 
1493 	DPRINTF(2, ("create_sockets(%d)\n", ntohs( (u_short) port)));
1494 
1495 	create_wildcards(port);
1496 
1497 	update_interfaces(port, NULL, NULL);
1498 
1499 	/*
1500 	 * Now that we have opened all the sockets, turn off the reuse
1501 	 * flag for security.
1502 	 */
1503 	set_reuseaddr(0);
1504 
1505 	DPRINTF(2, ("create_sockets: Total interfaces = %d\n", ninterfaces));
1506 
1507 	return ninterfaces;
1508 }
1509 
1510 /*
1511  * create_interface - create a new interface for a given prototype
1512  *		      binding the socket.
1513  */
1514 static struct interface *
1515 create_interface(
1516 		 u_short port,
1517 		 struct interface *iface
1518 		 )
1519 {
1520 	struct sockaddr_storage resmask;
1521 	struct interface *interface;
1522 
1523 	DPRINTF(2, ("create_interface(%s#%d)\n", stoa(&iface->sin), ntohs( (u_short) port)));
1524 
1525 	/* build an interface */
1526 	interface = new_interface(iface);
1527 
1528 	/*
1529 	 * create socket
1530 	 */
1531 	interface->fd = open_socket(&interface->sin,
1532 				 interface->flags, 0, interface);
1533 
1534 	if (interface->fd != INVALID_SOCKET)
1535 		list_if_listening(interface, port);
1536 
1537 	if ((interface->flags & INT_BROADCAST) &&
1538 	    interface->bfd != INVALID_SOCKET)
1539 	  msyslog(LOG_INFO, "Listening on broadcast address %s#%d",
1540 		  stoa((&interface->bcast)),
1541 		  ntohs( (u_short) port));
1542 
1543 	if (interface->fd == INVALID_SOCKET &&
1544 	    interface->bfd == INVALID_SOCKET) {
1545 		msyslog(LOG_ERR, "unable to create socket on %s (%d) for %s#%d",
1546 			interface->name,
1547 			interface->ifnum,
1548 			stoa((&interface->sin)),
1549 			ntohs( (u_short) port));
1550 		delete_interface(interface);
1551 		return NULL;
1552 	}
1553 
1554         /*
1555 	 * Blacklist bound interface address
1556 	 */
1557 
1558 	SET_HOSTMASK(&resmask, interface->sin.ss_family);
1559 	hack_restrict(RESTRICT_FLAGS, &interface->sin, &resmask,
1560 		      RESM_NTPONLY|RESM_INTERFACE, RES_IGNORE);
1561 
1562 	/*
1563 	 * set globals with the first found
1564 	 * loopback interface of the appropriate class
1565 	 */
1566 	if ((loopback_interface == NULL) &&
1567 	    (interface->family == AF_INET) &&
1568 	    ((interface->flags & INT_LOOPBACK) != 0))
1569 	{
1570 		loopback_interface = interface;
1571 	}
1572 
1573 	/*
1574 	 * put into our interface list
1575 	 */
1576 	add_addr_to_list(&interface->sin, interface);
1577 	add_interface(interface);
1578 
1579 	DPRINT_INTERFACE(2, (interface, "created ", "\n"));
1580 	return interface;
1581 }
1582 
1583 
1584 #ifdef SO_EXCLUSIVEADDRUSE
1585 static void
1586 set_excladdruse(int fd)
1587 {
1588 	int one = 1;
1589 	int failed;
1590 
1591 	failed = setsockopt(fd, SOL_SOCKET, SO_EXCLUSIVEADDRUSE,
1592 			    (char *)&one, sizeof(one));
1593 
1594 	if (failed)
1595 		netsyslog(LOG_ERR,
1596 			  "setsockopt(%d, SO_EXCLUSIVEADDRUSE, on): %m", fd);
1597 }
1598 #endif  /* SO_EXCLUSIVEADDRUSE */
1599 
1600 
1601 /*
1602  * set_reuseaddr() - set/clear REUSEADDR on all sockets
1603  *			NB possible hole - should we be doing this on broadcast
1604  *			fd's also?
1605  */
1606 static void
1607 set_reuseaddr(int flag) {
1608 	struct interface *interf;
1609 
1610 #ifndef SO_EXCLUSIVEADDRUSE
1611 
1612 	for (interf = ISC_LIST_HEAD(inter_list);
1613 	     interf != NULL;
1614 	     interf = ISC_LIST_NEXT(interf, link)) {
1615 
1616 		if (interf->flags & INT_WILDCARD)
1617 			continue;
1618 
1619 		/*
1620 		 * if interf->fd  is INVALID_SOCKET, we might have a adapter
1621 		 * configured but not present
1622 		 */
1623 		DPRINTF(4, ("setting SO_REUSEADDR on %.16s@%s to %s\n", interf->name, stoa(&interf->sin), flag ? "on" : "off"));
1624 
1625 		if (interf->fd != INVALID_SOCKET) {
1626 			if (setsockopt(interf->fd, SOL_SOCKET,
1627 					SO_REUSEADDR, (char *)&flag,
1628 					sizeof(flag))) {
1629 				netsyslog(LOG_ERR, "set_reuseaddr: setsockopt(SO_REUSEADDR, %s) failed: %m", flag ? "on" : "off");
1630 			}
1631 		}
1632 	}
1633 #endif /* ! SO_EXCLUSIVEADDRUSE */
1634 }
1635 
1636 /*
1637  * This is just a wrapper around an internal function so we can
1638  * make other changes as necessary later on
1639  */
1640 void
1641 enable_broadcast(struct interface *iface, struct sockaddr_storage *baddr)
1642 {
1643 #ifdef SO_BROADCAST
1644 	socket_broadcast_enable(iface, iface->fd, baddr);
1645 #endif
1646 }
1647 
1648 #ifdef OPEN_BCAST_SOCKET
1649 /*
1650  * Enable a broadcast address to a given socket
1651  * The socket is in the inter_list all we need to do is enable
1652  * broadcasting. It is not this function's job to select the socket
1653  */
1654 static isc_boolean_t
1655 socket_broadcast_enable(struct interface *iface, SOCKET fd, struct sockaddr_storage *maddr)
1656 {
1657 #ifdef SO_BROADCAST
1658 	int on = 1;
1659 
1660 	if (maddr->ss_family == AF_INET)
1661 	{
1662 		/* if this interface can support broadcast, set SO_BROADCAST */
1663 		if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST,
1664 			       (char *)&on, sizeof(on)))
1665 		{
1666 			netsyslog(LOG_ERR, "setsockopt(SO_BROADCAST) enable failure on address %s: %m",
1667 				stoa(maddr));
1668 		}
1669 #ifdef DEBUG
1670 		else if (debug > 1) {
1671 			printf("Broadcast enabled on socket %d for address %s\n",
1672 				fd, stoa(maddr));
1673 		}
1674 #endif
1675 	}
1676 	iface->flags |= INT_BCASTOPEN;
1677 	return ISC_TRUE;
1678 #else
1679 	return ISC_FALSE;
1680 #endif /* SO_BROADCAST */
1681 }
1682 
1683 /*
1684  * Remove a broadcast address from a given socket
1685  * The socket is in the inter_list all we need to do is disable
1686  * broadcasting. It is not this function's job to select the socket
1687  */
1688 static isc_boolean_t
1689 socket_broadcast_disable(struct interface *iface, struct sockaddr_storage *maddr)
1690 {
1691 #ifdef SO_BROADCAST
1692 	int off = 0;	/* This seems to be OK as an int */
1693 
1694 	if (maddr->ss_family == AF_INET)
1695 	{
1696 		if (setsockopt(iface->fd, SOL_SOCKET, SO_BROADCAST,
1697 			       (char *)&off, sizeof(off)))
1698 		{
1699 			netsyslog(LOG_ERR, "setsockopt(SO_BROADCAST) disable failure on address %s: %m",
1700 				stoa(maddr));
1701 		}
1702 	}
1703 	iface->flags &= ~INT_BCASTOPEN;
1704 	return ISC_TRUE;
1705 #else
1706 	return ISC_FALSE;
1707 #endif /* SO_BROADCAST */
1708 }
1709 
1710 #endif /* OPEN_BCAST_SOCKET */
1711 /*
1712  * Check to see if the address is a multicast address
1713  */
1714 static isc_boolean_t
1715 addr_ismulticast(struct sockaddr_storage *maddr)
1716 {
1717 	switch (maddr->ss_family)
1718 	{
1719 	case AF_INET :
1720 		if (!IN_CLASSD(ntohl(((struct sockaddr_in*)maddr)->sin_addr.s_addr))) {
1721 			DPRINTF(4, ("multicast address %s not class D\n", stoa(maddr)));
1722 			return (ISC_FALSE);
1723 		}
1724 		else
1725 		{
1726 			return (ISC_TRUE);
1727 		}
1728 
1729 	case AF_INET6 :
1730 #ifdef INCLUDE_IPV6_MULTICAST_SUPPORT
1731 		if (!IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6*)maddr)->sin6_addr)) {
1732 			DPRINTF(4, ("address %s not IPv6 multicast address\n", stoa(maddr)));
1733 			return (ISC_FALSE);
1734 		}
1735 		else
1736 		{
1737 			return (ISC_TRUE);
1738 		}
1739 
1740 /*
1741  * If we don't have IPV6 support any IPV6 address is not multicast
1742  */
1743 #else
1744 		return (ISC_FALSE);
1745 #endif
1746 	/*
1747 	 * Never valid
1748 	 */
1749 	default:
1750 		return (ISC_FALSE);
1751 	}
1752 }
1753 
1754 /*
1755  * Multicast servers need to set the appropriate Multicast interface
1756  * socket option in order for it to know which interface to use for
1757  * send the multicast packet.
1758  */
1759 void
1760 enable_multicast_if(struct interface *iface, struct sockaddr_storage *maddr)
1761 {
1762 #ifdef MCAST
1763 #ifdef IP_MULTICAST_LOOP
1764 	/*u_char*/ TYPEOF_IP_MULTICAST_LOOP off = 0;
1765 #endif
1766 #ifdef IPV6_MULTICAST_LOOP
1767 	u_int off6 = 0;		/* RFC 3493, 5.2. defines type unsigned int */
1768 #endif
1769 
1770 	switch (maddr->ss_family)
1771 	{
1772 	case AF_INET:
1773 		if (setsockopt(iface->fd, IPPROTO_IP, IP_MULTICAST_IF,
1774 		   (char *)&(((struct sockaddr_in*)&iface->sin)->sin_addr.s_addr),
1775 		    sizeof(struct in_addr)) == -1) {
1776 			netsyslog(LOG_ERR,
1777 			"setsockopt IP_MULTICAST_IF failure: %m on socket %d, addr %s for multicast address %s",
1778 			iface->fd, stoa(&iface->sin), stoa(maddr));
1779 			return;
1780 		}
1781 #ifdef IP_MULTICAST_LOOP
1782 		/*
1783 		 * Don't send back to itself, but allow it to fail to set it
1784 		 */
1785 		if (setsockopt(iface->fd, IPPROTO_IP, IP_MULTICAST_LOOP,
1786 		       SETSOCKOPT_ARG_CAST &off, sizeof(off)) == -1) {
1787 			netsyslog(LOG_ERR,
1788 			"setsockopt IP_MULTICAST_LOOP failure: %m on socket %d, addr %s for multicast address %s",
1789 			iface->fd, stoa(&iface->sin), stoa(maddr));
1790 		}
1791 #endif
1792 	DPRINTF(4, ("Added IPv4 multicast interface on socket %d, addr %s for multicast address %s\n",
1793 			    iface->fd, stoa(&iface->sin),
1794 			    stoa(maddr)));
1795 		break;
1796 
1797 	case AF_INET6:
1798 #ifdef INCLUDE_IPV6_MULTICAST_SUPPORT
1799 		if (setsockopt(iface->fd, IPPROTO_IPV6, IPV6_MULTICAST_IF,
1800 		    (char *) &iface->scopeid, sizeof(iface->scopeid)) == -1) {
1801 			netsyslog(LOG_ERR,
1802 			"setsockopt IPV6_MULTICAST_IF failure: %m on socket %d, addr %s, scope %d for multicast address %s",
1803 			iface->fd, stoa(&iface->sin), iface->scopeid,
1804 			stoa(maddr));
1805 			return;
1806 		}
1807 #ifdef IPV6_MULTICAST_LOOP
1808 		/*
1809 		 * Don't send back to itself, but allow it to fail to set it
1810 		 */
1811 		if (setsockopt(iface->fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
1812 		       (char *) &off6, sizeof(off6)) == -1) {
1813 			netsyslog(LOG_ERR,
1814 			"setsockopt IPV6_MULTICAST_LOOP failure: %m on socket %d, addr %s for multicast address %s",
1815 			iface->fd, stoa(&iface->sin), stoa(maddr));
1816 		}
1817 #endif
1818 		DPRINTF(4, ("Added IPv6 multicast interface on socket %d, addr %s, scope %d for multicast address %s\n",
1819 			    iface->fd,  stoa(&iface->sin), iface->scopeid,
1820 			    stoa(maddr)));
1821 		break;
1822 #else
1823 		return;
1824 #endif	/* INCLUDE_IPV6_MULTICAST_SUPPORT */
1825 	}
1826 	return;
1827 #endif
1828 }
1829 
1830 /*
1831  * Add a multicast address to a given socket
1832  * The socket is in the inter_list all we need to do is enable
1833  * multicasting. It is not this function's job to select the socket
1834  */
1835 static isc_boolean_t
1836 socket_multicast_enable(struct interface *iface, int lscope, struct sockaddr_storage *maddr)
1837 {
1838 #ifdef INCLUDE_IPV6_MULTICAST_SUPPORT
1839 	struct ipv6_mreq mreq6;
1840 	struct in6_addr iaddr6;
1841 #endif /* INCLUDE_IPV6_MULTICAST_SUPPORT */
1842 
1843 	struct ip_mreq mreq;
1844 
1845 	if (find_addr_in_list(maddr)) {
1846 		DPRINTF(4, ("socket_multicast_enable(%s): already enabled\n", stoa(maddr)));
1847 		return ISC_TRUE;
1848 	}
1849 
1850 	switch (maddr->ss_family)
1851 	{
1852 	case AF_INET:
1853 		memset((char *)&mreq, 0, sizeof(mreq));
1854 		mreq.imr_multiaddr = (((struct sockaddr_in*)maddr)->sin_addr);
1855 		mreq.imr_interface.s_addr = htonl(INADDR_ANY);
1856 		if (setsockopt(iface->fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
1857 			(char *)&mreq, sizeof(mreq)) == -1) {
1858 			netsyslog(LOG_ERR,
1859 			"setsockopt IP_ADD_MEMBERSHIP failure: %m on socket %d, addr %s for %x / %x (%s)",
1860 			iface->fd, stoa(&iface->sin),
1861 			mreq.imr_multiaddr.s_addr,
1862 			mreq.imr_interface.s_addr, stoa(maddr));
1863 			return ISC_FALSE;
1864 		}
1865 		DPRINTF(4, ("Added IPv4 multicast membership on socket %d, addr %s for %x / %x (%s)\n",
1866 			    iface->fd, stoa(&iface->sin),
1867 			    mreq.imr_multiaddr.s_addr,
1868 			    mreq.imr_interface.s_addr, stoa(maddr)));
1869 		break;
1870 
1871 	case AF_INET6:
1872 #ifdef INCLUDE_IPV6_MULTICAST_SUPPORT
1873 		/*
1874 		 * Enable reception of multicast packets
1875 		 * If the address is link-local we can get the interface index
1876 		 * from the scope id. Don't do this for other types of multicast
1877 		 * addresses. For now let the kernel figure it out.
1878 		 */
1879 		memset((char *)&mreq6, 0, sizeof(mreq6));
1880 		iaddr6 = ((struct sockaddr_in6*)maddr)->sin6_addr;
1881 		mreq6.ipv6mr_multiaddr = iaddr6;
1882 		mreq6.ipv6mr_interface = lscope;
1883 
1884 		if (setsockopt(iface->fd, IPPROTO_IPV6, IPV6_JOIN_GROUP,
1885 			(char *)&mreq6, sizeof(mreq6)) == -1) {
1886 			netsyslog(LOG_ERR,
1887 			 "setsockopt IPV6_JOIN_GROUP failure: %m on socket %d, addr %s for interface %d(%s)",
1888 			iface->fd, stoa(&iface->sin),
1889 			mreq6.ipv6mr_interface, stoa(maddr));
1890 			return ISC_FALSE;
1891 		}
1892 		DPRINTF(4, ("Added IPv6 multicast group on socket %d, addr %s for interface %d(%s)\n",
1893 			    iface->fd, stoa(&iface->sin),
1894 			    mreq6.ipv6mr_interface, stoa(maddr)));
1895 		break;
1896 #else
1897 		return ISC_FALSE;
1898 #endif	/* INCLUDE_IPV6_MULTICAST_SUPPORT */
1899 	}
1900 	iface->flags |= INT_MCASTOPEN;
1901 	iface->num_mcast++;
1902 	add_addr_to_list(maddr, iface);
1903 	return ISC_TRUE;
1904 }
1905 
1906 /*
1907  * Remove a multicast address from a given socket
1908  * The socket is in the inter_list all we need to do is disable
1909  * multicasting. It is not this function's job to select the socket
1910  */
1911 static isc_boolean_t
1912 socket_multicast_disable(struct interface *iface, struct sockaddr_storage *maddr)
1913 {
1914 #ifdef INCLUDE_IPV6_MULTICAST_SUPPORT
1915 	struct ipv6_mreq mreq6;
1916 	struct in6_addr iaddr6;
1917 #endif /* INCLUDE_IPV6_MULTICAST_SUPPORT */
1918 
1919 	struct ip_mreq mreq;
1920 	memset((char *)&mreq, 0, sizeof(mreq));
1921 
1922 	if (find_addr_in_list(maddr) == NULL) {
1923 		DPRINTF(4, ("socket_multicast_disable(%s): not enabled\n", stoa(maddr)));
1924 		return ISC_TRUE;
1925 	}
1926 
1927 	switch (maddr->ss_family)
1928 	{
1929 	case AF_INET:
1930 		mreq.imr_multiaddr = (((struct sockaddr_in*)&maddr)->sin_addr);
1931 		mreq.imr_interface.s_addr = ((struct sockaddr_in*)&iface->sin)->sin_addr.s_addr;
1932 		if (setsockopt(iface->fd, IPPROTO_IP, IP_DROP_MEMBERSHIP,
1933 			(char *)&mreq, sizeof(mreq)) == -1) {
1934 			netsyslog(LOG_ERR,
1935 			"setsockopt IP_DROP_MEMBERSHIP failure: %m on socket %d, addr %s for %x / %x (%s)",
1936 			iface->fd, stoa(&iface->sin),
1937 			mreq.imr_multiaddr.s_addr,
1938 			mreq.imr_interface.s_addr, stoa(maddr));
1939 			return ISC_FALSE;
1940 		}
1941 		break;
1942 	case AF_INET6:
1943 #ifdef INCLUDE_IPV6_MULTICAST_SUPPORT
1944 		/*
1945 		 * Disable reception of multicast packets
1946 		 * If the address is link-local we can get the interface index
1947 		 * from the scope id. Don't do this for other types of multicast
1948 		 * addresses. For now let the kernel figure it out.
1949 		 */
1950 		iaddr6 = ((struct sockaddr_in6*)&maddr)->sin6_addr;
1951 		mreq6.ipv6mr_multiaddr = iaddr6;
1952 		mreq6.ipv6mr_interface = iface->scopeid;
1953 
1954 		if (setsockopt(iface->fd, IPPROTO_IPV6, IPV6_LEAVE_GROUP,
1955 			(char *)&mreq6, sizeof(mreq6)) == -1) {
1956 			netsyslog(LOG_ERR,
1957 			"setsockopt IPV6_LEAVE_GROUP failure: %m on socket %d, addr %s for %d(%s)",
1958 			iface->fd, stoa(&iface->sin),
1959 			mreq6.ipv6mr_interface, stoa(maddr));
1960 			return ISC_FALSE;
1961 		}
1962 		break;
1963 #else
1964 		return ISC_FALSE;
1965 #endif	/* INCLUDE_IPV6_MULTICAST_SUPPORT */
1966 
1967 	}
1968 	iface->num_mcast--;
1969 	if (iface->num_mcast <= 0) {
1970                 iface->num_mcast = 0;
1971 		iface->flags &= ~INT_MCASTOPEN;
1972 	}
1973 	return ISC_TRUE;
1974 }
1975 
1976 /*
1977  * io_setbclient - open the broadcast client sockets
1978  */
1979 void
1980 io_setbclient(void)
1981 {
1982 #ifdef OPEN_BCAST_SOCKET
1983         struct interface *interf;
1984 	int nif = 0;
1985 	isc_boolean_t jstatus;
1986 	SOCKET fd;
1987 
1988 	set_reuseaddr(1);
1989 
1990 	for (interf = ISC_LIST_HEAD(inter_list);
1991 	     interf != NULL;
1992 	     interf = ISC_LIST_NEXT(interf, link)) {
1993 	        if (interf->flags & INT_WILDCARD)
1994 		        continue;
1995 
1996 		/* use only allowed addresses */
1997 		if (interf->ignore_packets == ISC_TRUE)
1998 			continue;
1999 		/* Only IPv4 addresses are valid for broadcast */
2000 		if (interf->sin.ss_family != AF_INET)
2001 			continue;
2002 
2003 		/* Is this a broadcast address? */
2004 		if (!(interf->flags & INT_BROADCAST))
2005 			continue;
2006 
2007 		/* Skip the loopback addresses */
2008 		if (interf->flags & INT_LOOPBACK)
2009 			continue;
2010 
2011 		/* Do we already have the broadcast address open? */
2012 		if (interf->flags & INT_BCASTOPEN) {
2013 		/* account for already open interfaces to aviod misleading warning below */
2014 			nif++;
2015 			continue;
2016 		}
2017 
2018 		/*
2019 		 * Try to open the broadcast address
2020 		 */
2021 		interf->family = AF_INET;
2022 		interf->bfd = open_socket(&interf->bcast,
2023 				    INT_BROADCAST, 0, interf);
2024 
2025 		 /*
2026 		 * If we succeeded then we use it otherwise
2027 		 * enable the underlying address
2028 		 */
2029 		if (interf->bfd == INVALID_SOCKET) {
2030 			fd = interf->fd;
2031 		}
2032 		else {
2033 			fd = interf->bfd;
2034 		}
2035 
2036 		/* Enable Broadcast on socket */
2037 		jstatus = socket_broadcast_enable(interf, fd, &interf->sin);
2038 		if (jstatus == ISC_TRUE)
2039 		{
2040 			nif++;
2041 			netsyslog(LOG_INFO,"io_setbclient: Opened broadcast client on interface #%d %s, socket: %d",
2042 				  interf->ifnum, interf->name, fd);
2043 			interf->addr_refid = addr2refid(&interf->sin);
2044 		}
2045 	}
2046 	set_reuseaddr(0);
2047 #ifdef DEBUG
2048 	if (debug)
2049 		if (nif > 0)
2050 			printf("io_setbclient: Opened broadcast clients\n");
2051 #endif
2052 	if (nif == 0)
2053 		netsyslog(LOG_ERR, "Unable to listen for broadcasts, no broadcast interfaces available");
2054 #else
2055 	netsyslog(LOG_ERR, "io_setbclient: Broadcast Client disabled by build");
2056 #endif
2057 }
2058 
2059 /*
2060  * io_unsetbclient - close the broadcast client sockets
2061  */
2062 void
2063 io_unsetbclient(void)
2064 {
2065         struct interface *interf;
2066 	isc_boolean_t lstatus;
2067 
2068 	for (interf = ISC_LIST_HEAD(inter_list);
2069 	     interf != NULL;
2070 	     interf = ISC_LIST_NEXT(interf, link))
2071 	{
2072 	        if (interf->flags & INT_WILDCARD)
2073 		    continue;
2074 
2075 		if (!(interf->flags & INT_BCASTOPEN))
2076 		    continue;
2077 		lstatus = socket_broadcast_disable(interf, &interf->sin);
2078 	}
2079 }
2080 
2081 /*
2082  * io_multicast_add() - add multicast group address
2083  */
2084 void
2085 io_multicast_add(
2086 	struct sockaddr_storage addr
2087 	)
2088 {
2089 #ifdef MCAST
2090 	struct interface *interface;
2091 #ifndef MULTICAST_NONEWSOCKET
2092 	struct interface *iface;
2093 #endif
2094 	int lscope = 0;
2095 
2096 	/*
2097 	 * Check to see if this is a multicast address
2098 	 */
2099 	if (addr_ismulticast(&addr) == ISC_FALSE)
2100 		return;
2101 
2102 	/* If we already have it we can just return */
2103 	if (find_flagged_addr_in_list(&addr, INT_MCASTOPEN|INT_MCASTIF) != NULL)
2104 	{
2105 		netsyslog(LOG_INFO, "Duplicate request found for multicast address %s",
2106 			stoa(&addr));
2107 		return;
2108 	}
2109 
2110 #ifndef MULTICAST_NONEWSOCKET
2111 	interface = new_interface(NULL);
2112 
2113 	/*
2114 	 * Open a new socket for the multicast address
2115 	 */
2116 	interface->sin.ss_family = addr.ss_family;
2117 	interface->family = addr.ss_family;
2118 
2119 	switch(addr.ss_family) {
2120 	case AF_INET:
2121 		memcpy(&(((struct sockaddr_in *)&interface->sin)->sin_addr),
2122 		       &(((struct sockaddr_in*)&addr)->sin_addr),
2123 		       sizeof(struct in_addr));
2124 		((struct sockaddr_in*)&interface->sin)->sin_port = htons(NTP_PORT);
2125 		memset(&((struct sockaddr_in*)&interface->mask)->sin_addr.s_addr, 0xff, sizeof(struct in_addr));
2126 		break;
2127 	case AF_INET6:
2128 #ifdef INCLUDE_IPV6_MULTICAST_SUPPORT
2129 		memcpy(&(((struct sockaddr_in6 *)&interface->sin)->sin6_addr),
2130 		       &((struct sockaddr_in6*)&addr)->sin6_addr,
2131 		       sizeof(struct in6_addr));
2132 		((struct sockaddr_in6*)&interface->sin)->sin6_port = htons(NTP_PORT);
2133 #ifdef ISC_PLATFORM_HAVESCOPEID
2134 		((struct sockaddr_in6*)&interface->sin)->sin6_scope_id = ((struct sockaddr_in6*)&addr)->sin6_scope_id;
2135 #endif
2136 		memset(&((struct sockaddr_in6*)&interface->mask)->sin6_addr.s6_addr, 0xff, sizeof(struct in6_addr));
2137 #endif
2138 		iface = findlocalcastinterface(&addr, INT_MULTICAST);
2139 		if (iface) {
2140 # ifdef ISC_PLATFORM_HAVESCOPEID
2141 			lscope = ((struct sockaddr_in6*)&iface->sin)->sin6_scope_id;
2142 # endif
2143 			DPRINTF(4, ("Found interface #%d %s, scope: %d for address %s\n", iface->ifnum, iface->name, lscope, stoa(&addr)));
2144 		}
2145 		break;
2146 	}
2147 
2148 	set_reuseaddr(1);
2149 	interface->bfd = INVALID_SOCKET;
2150 	interface->fd = open_socket(&interface->sin,
2151 			    INT_MULTICAST, 0, interface);
2152 
2153 	if (interface->fd != INVALID_SOCKET)
2154 	{
2155 		interface->bfd = INVALID_SOCKET;
2156 		interface->ignore_packets = ISC_FALSE;
2157 		interface->flags |= INT_MCASTIF;
2158 
2159 		(void) strncpy(interface->name, "multicast",
2160 			sizeof(interface->name));
2161 		((struct sockaddr_in*)&interface->mask)->sin_addr.s_addr =
2162 						htonl(~(u_int32)0);
2163 		DPRINT_INTERFACE(2, (interface, "multicast add ", "\n"));
2164 		/* socket_multicast_enable() will add this address to the addresslist */
2165 		add_interface(interface);
2166 		list_if_listening(interface, htons(NTP_PORT));
2167 	}
2168 	else
2169 	{
2170 		delete_interface(interface);  /* re-use existing interface */
2171 		interface = NULL;
2172 		if (addr.ss_family == AF_INET)
2173 			interface = wildipv4;
2174 		else if (addr.ss_family == AF_INET6)
2175 			interface = wildipv6;
2176 
2177 		if (interface != NULL) {
2178 			/* HACK ! -- stuff in an address */
2179 			interface->bcast = addr;
2180 			netsyslog(LOG_ERR,
2181 			 "...multicast address %s using wildcard interface #%d %s",
2182 				  stoa(&addr), interface->ifnum, interface->name);
2183 		} else {
2184 			netsyslog(LOG_ERR,
2185 			"No multicast socket available to use for address %s",
2186 			stoa(&addr));
2187 			return;
2188 		}
2189 	}
2190 #else
2191 	/*
2192 	 * For the case where we can't use a separate socket
2193 	 */
2194 	interface = findlocalcastinterface(&addr, INT_MULTICAST);
2195 	/*
2196 	 * If we don't have a valid socket, just return
2197 	 */
2198 	if (!interface)
2199 	{
2200 		netsyslog(LOG_ERR,
2201 		"Cannot add multicast address %s: Cannot find slot",
2202 		stoa(&addr));
2203 		return;
2204 	}
2205 
2206 #endif
2207 	{
2208 		isc_boolean_t jstatus;
2209 		jstatus = socket_multicast_enable(interface, lscope, &addr);
2210 
2211 		if (jstatus == ISC_TRUE)
2212 			netsyslog(LOG_INFO, "Added Multicast Listener %s on interface #%d %s\n", stoa(&addr), interface->ifnum, interface->name);
2213 		else
2214 			netsyslog(LOG_ERR, "Failed to add Multicast Listener %s\n", stoa(&addr));
2215 	}
2216 #else /* MCAST */
2217 	netsyslog(LOG_ERR,
2218 		  "Cannot add multicast address %s: no Multicast support",
2219 		  stoa(&addr));
2220 #endif /* MCAST */
2221 	return;
2222 }
2223 
2224 /*
2225  * io_multicast_del() - delete multicast group address
2226  */
2227 void
2228 io_multicast_del(
2229 	struct sockaddr_storage addr
2230 	)
2231 {
2232 #ifdef MCAST
2233         struct interface *interface;
2234 	isc_boolean_t lstatus;
2235 
2236 	/*
2237 	 * Check to see if this is a multicast address
2238 	 */
2239 	if (addr_ismulticast(&addr) == ISC_FALSE)
2240 	{
2241 		netsyslog(LOG_ERR,
2242 			 "invalid multicast address %s", stoa(&addr));
2243 		return;
2244 	}
2245 
2246 	switch (addr.ss_family)
2247 	{
2248 	case AF_INET :
2249 		/*
2250 		 * Disable reception of multicast packets
2251 		 */
2252 		interface = find_flagged_addr_in_list(&addr, INT_MCASTOPEN);
2253 		while ( interface != NULL) {
2254 			lstatus = socket_multicast_disable(interface, &addr);
2255 			interface = find_flagged_addr_in_list(&addr, INT_MCASTOPEN);
2256 		}
2257 		break;
2258 
2259 #ifdef INCLUDE_IPV6_MULTICAST_SUPPORT
2260 	case AF_INET6 :
2261 		/*
2262 		 * Disable reception of multicast packets
2263 		 */
2264 		for (interface = ISC_LIST_HEAD(inter_list);
2265 		     interface != NULL;
2266 		     interface = ISC_LIST_NEXT(interface, link))
2267 		{
2268                         if (interface->flags & INT_WILDCARD)
2269 			        continue;
2270 
2271 			/* Be sure it's the correct family */
2272 			if (interface->sin.ss_family != AF_INET6)
2273 				continue;
2274 			if (!(interface->flags & INT_MCASTOPEN))
2275 				continue;
2276 			if (!(interface->fd < 0))
2277 				continue;
2278 			if (!SOCKCMP(&addr, &interface->sin))
2279 				continue;
2280 			lstatus = socket_multicast_disable(interface, &addr);
2281 		}
2282 		break;
2283 #endif /* INCLUDE_IPV6_MULTICAST_SUPPORT */
2284 
2285 	}/* switch */
2286 
2287         delete_addr_from_list(&addr);
2288 
2289 #else /* not MCAST */
2290 	netsyslog(LOG_ERR, "this function requires multicast kernel");
2291 #endif /* not MCAST */
2292 }
2293 
2294 /*
2295  * init_nonblocking_io() - set up descriptor to be non blocking
2296  */
2297 static void init_nonblocking_io(SOCKET fd)
2298 {
2299 	/*
2300 	 * set non-blocking,
2301 	 */
2302 
2303 #ifdef USE_FIONBIO
2304 	/* in vxWorks we use FIONBIO, but the others are defined for old systems, so
2305 	 * all hell breaks loose if we leave them defined
2306 	 */
2307 #undef O_NONBLOCK
2308 #undef FNDELAY
2309 #undef O_NDELAY
2310 #endif
2311 
2312 #if defined(O_NONBLOCK) /* POSIX */
2313 	if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
2314 	{
2315 		netsyslog(LOG_ERR, "fcntl(O_NONBLOCK) fails on fd #%d: %m",
2316 			fd);
2317 		exit(1);
2318 		/*NOTREACHED*/
2319 	}
2320 #elif defined(FNDELAY)
2321 	if (fcntl(fd, F_SETFL, FNDELAY) < 0)
2322 	{
2323 		netsyslog(LOG_ERR, "fcntl(FNDELAY) fails on fd #%d: %m",
2324 			fd);
2325 		exit(1);
2326 		/*NOTREACHED*/
2327 	}
2328 #elif defined(O_NDELAY) /* generally the same as FNDELAY */
2329 	if (fcntl(fd, F_SETFL, O_NDELAY) < 0)
2330 	{
2331 		netsyslog(LOG_ERR, "fcntl(O_NDELAY) fails on fd #%d: %m",
2332 			fd);
2333 		exit(1);
2334 		/*NOTREACHED*/
2335 	}
2336 #elif defined(FIONBIO)
2337 	{
2338 		int on = 1;
2339 		if (ioctl(fd,FIONBIO,&on) < 0)
2340 		{
2341 			netsyslog(LOG_ERR, "ioctl(FIONBIO) fails on fd #%d: %m",
2342 				fd);
2343 			exit(1);
2344 			/*NOTREACHED*/
2345 		}
2346 	}
2347 #elif defined(FIOSNBIO)
2348 	if (ioctl(fd,FIOSNBIO,&on) < 0)
2349 	{
2350 		netsyslog(LOG_ERR, "ioctl(FIOSNBIO) fails on fd #%d: %m",
2351 			fd);
2352 		exit(1);
2353 		/*NOTREACHED*/
2354 	}
2355 #else
2356 # include "Bletch: Need non-blocking I/O!"
2357 #endif
2358 }
2359 
2360 /*
2361  * open_socket - open a socket, returning the file descriptor
2362  */
2363 
2364 static SOCKET
2365 open_socket(
2366 	struct sockaddr_storage *addr,
2367 	int flags,
2368 	int turn_off_reuse,
2369 	struct interface *interf
2370 	)
2371 {
2372 	int errval;
2373 	SOCKET fd;
2374 	/*
2375 	 * int is OK for REUSEADR per
2376 	 * http://www.kohala.com/start/mcast.api.txt
2377 	 */
2378 	int on = 1;
2379 	int off = 0;
2380 
2381 #if defined(IPTOS_LOWDELAY) && defined(IPPROTO_IP) && defined(IP_TOS)
2382 	int tos;
2383 #endif /* IPTOS_LOWDELAY && IPPROTO_IP && IP_TOS */
2384 
2385 	if ((addr->ss_family == AF_INET6) && (isc_net_probeipv6() != ISC_R_SUCCESS))
2386 		return (INVALID_SOCKET);
2387 
2388 	/* create a datagram (UDP) socket */
2389 	fd = socket(addr->ss_family, SOCK_DGRAM, 0);
2390 	if (INVALID_SOCKET == fd) {
2391 #ifndef SYS_WINNT
2392 		errval = errno;
2393 #else
2394 		errval = WSAGetLastError();
2395 #endif
2396 		netsyslog(LOG_ERR,
2397 			  "socket(AF_INET%s, SOCK_DGRAM, 0) failed on address %s: %m",
2398 			  (addr->ss_family == AF_INET6) ? "6" : "",
2399 			  stoa(addr));
2400 
2401 		if (errval == EPROTONOSUPPORT ||
2402 		    errval == EAFNOSUPPORT ||
2403 		    errval == EPFNOSUPPORT)
2404 			return (INVALID_SOCKET);
2405 		msyslog(LOG_ERR, "unexpected error code %d (not PROTONOSUPPORT|AFNOSUPPORT|FPNOSUPPORT) - exiting", errval);
2406 		exit(1);
2407 		/*NOTREACHED*/
2408 	}
2409 
2410 #ifdef SYS_WINNT
2411 	connection_reset_fix(fd, addr);
2412 #endif
2413 	/*
2414 	 * Fixup the file descriptor for some systems
2415 	 * See bug #530 for details of the issue.
2416 	 */
2417 	fd = move_fd(fd);
2418 
2419 	/*
2420 	 * set SO_REUSEADDR since we will be binding the same port
2421 	 * number on each interface according to turn_off_reuse.
2422 	 * This is undesirable on Windows versions starting with
2423 	 * Windows XP (numeric version 5.1).
2424 	 */
2425 #ifdef SYS_WINNT
2426 	if (isc_win32os_versioncheck(5, 1, 0, 0) < 0)  /* before 5.1 */
2427 #endif
2428 		if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
2429 			       (char *)(turn_off_reuse
2430 					? &off
2431 					: &on),
2432 			       sizeof(on))) {
2433 
2434 			netsyslog(LOG_ERR, "setsockopt SO_REUSEADDR %s"
2435 					   " fails for address %s: %m",
2436 					   turn_off_reuse
2437 						? "off"
2438 						: "on",
2439 					   stoa(addr));
2440 			closesocket(fd);
2441 			return INVALID_SOCKET;
2442 		}
2443 #ifdef SO_EXCLUSIVEADDRUSE
2444 	/*
2445 	 * setting SO_EXCLUSIVEADDRUSE on the wildcard we open
2446 	 * first will cause more specific binds to fail.
2447 	 */
2448 	if (!(interf->flags & INT_WILDCARD))
2449 		set_excladdruse(fd);
2450 #endif
2451 
2452 	/*
2453 	 * IPv4 specific options go here
2454 	 */
2455 	if (addr->ss_family == AF_INET) {
2456 #if defined(IPTOS_LOWDELAY) && defined(IPPROTO_IP) && defined(IP_TOS)
2457 	/* set IP_TOS to minimize packet delay */
2458 		tos = IPTOS_LOWDELAY;
2459 		if (setsockopt(fd, IPPROTO_IP, IP_TOS, (char *) &tos, sizeof(tos)) < 0)
2460 		{
2461 			netsyslog(LOG_ERR, "setsockopt IPTOS_LOWDELAY on fails on address %s: %m",
2462 				stoa(addr));
2463 		}
2464 #endif /* IPTOS_LOWDELAY && IPPROTO_IP && IP_TOS */
2465 	}
2466 
2467 	/*
2468 	 * IPv6 specific options go here
2469 	 */
2470         if (addr->ss_family == AF_INET6) {
2471 #if defined(IPV6_V6ONLY)
2472                 if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY,
2473                 	(char*)&on, sizeof(on)))
2474                 {
2475                 	netsyslog(LOG_ERR, "setsockopt IPV6_V6ONLY on fails on address %s: %m",
2476 				stoa(addr));
2477 		}
2478 #endif /* IPV6_V6ONLY */
2479 #if defined(IPV6_BINDV6ONLY)
2480                 if (setsockopt(fd, IPPROTO_IPV6, IPV6_BINDV6ONLY,
2481                 	(char*)&on, sizeof(on)))
2482                 {
2483                 	netsyslog(LOG_ERR,
2484 			    "setsockopt IPV6_BINDV6ONLY on fails on address %s: %m",
2485 			    stoa(addr));
2486 		}
2487 #endif /* IPV6_BINDV6ONLY */
2488 	}
2489 
2490 #ifdef OS_NEEDS_REUSEADDR_FOR_IFADDRBIND
2491 	/*
2492 	 * some OSes don't allow binding to more specific
2493 	 * addresses if a wildcard address already bound
2494 	 * to the port and SO_REUSEADDR is not set
2495 	 */
2496 	if (!is_wildcard_addr(addr)) {
2497 		set_wildcard_reuse(addr->ss_family, 1);
2498 	}
2499 #endif
2500 
2501 	/*
2502 	 * bind the local address.
2503 	 */
2504 	errval = bind(fd, (struct sockaddr *)addr, SOCKLEN(addr));
2505 
2506 #ifdef OS_NEEDS_REUSEADDR_FOR_IFADDRBIND
2507 	/*
2508 	 * some OSes don't allow binding to more specific
2509 	 * addresses if a wildcard address already bound
2510 	 * to the port and REUSE_ADDR is not set
2511 	 */
2512 	if (!is_wildcard_addr(addr)) {
2513 		set_wildcard_reuse(addr->ss_family, 0);
2514 	}
2515 #endif
2516 
2517 	if (errval < 0) {
2518 		/*
2519 		 * Don't log this under all conditions
2520 		 */
2521 		if (turn_off_reuse == 0
2522 #ifdef DEBUG
2523 		    || debug > 1
2524 #endif
2525 			) {
2526 			if (addr->ss_family == AF_INET)
2527 				netsyslog(LOG_ERR,
2528 					  "bind() fd %d, family AF_INET, port %d, addr %s, in_classd=%d flags=0x%x fails: %m",
2529 					  fd, (int)ntohs(((struct sockaddr_in*)addr)->sin_port),
2530 					  stoa(addr),
2531 					  IN_CLASSD(ntohl(((struct sockaddr_in*)addr)->sin_addr.s_addr)),
2532 					  flags);
2533 #ifdef INCLUDE_IPV6_SUPPORT
2534 			else if (addr->ss_family == AF_INET6)
2535 				netsyslog(LOG_ERR,
2536 					  "bind() fd %d, family AF_INET6, port %d, scope %d, addr %s, mcast=%d flags=0x%x fails: %m",
2537 					  fd, (int)ntohs(((struct sockaddr_in6*)addr)->sin6_port),
2538 # ifdef ISC_PLATFORM_HAVESCOPEID
2539 					  ((struct sockaddr_in6*)addr)->sin6_scope_id
2540 # else
2541 					  -1
2542 # endif
2543 					  , stoa(addr),
2544 					  IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6*)addr)->sin6_addr),
2545 					  flags);
2546 #endif
2547 		}
2548 
2549 		closesocket(fd);
2550 
2551 		return INVALID_SOCKET;
2552 	}
2553 
2554 #ifdef HAVE_TIMESTAMP
2555 	{
2556 		if (setsockopt(fd, SOL_SOCKET, SO_TIMESTAMP,
2557 			       (char*)&on, sizeof(on)))
2558 		{
2559 			netsyslog(LOG_DEBUG,
2560 				  "setsockopt SO_TIMESTAMP on fails on address %s: %m",
2561 				  stoa(addr));
2562 		}
2563 #ifdef DEBUG
2564 		else
2565 		{
2566 			DPRINTF(4, ("setsockopt SO_TIMESTAMP enabled on fd %d address %s\n", fd, stoa(addr)));
2567 		}
2568 #endif
2569 	}
2570 #endif
2571 	DPRINTF(4, ("bind() fd %d, family %d, port %d, addr %s, flags=0x%x\n",
2572 		   fd,
2573 		   addr->ss_family,
2574 		   (int)ntohs(((struct sockaddr_in*)addr)->sin_port),
2575 		   stoa(addr),
2576 		   interf->flags));
2577 
2578 	init_nonblocking_io(fd);
2579 
2580 #ifdef HAVE_SIGNALED_IO
2581 	init_socket_sig(fd);
2582 #endif /* not HAVE_SIGNALED_IO */
2583 
2584 	add_fd_to_list(fd, FD_TYPE_SOCKET);
2585 
2586 #if !defined(SYS_WINNT) && !defined(VMS)
2587 	DPRINTF(4, ("flags for fd %d: 0x%x\n", fd,
2588 		    fcntl(fd, F_GETFL, 0)));
2589 #endif /* SYS_WINNT || VMS */
2590 
2591 #if defined (HAVE_IO_COMPLETION_PORT)
2592 /*
2593  * Add the socket to the completion port
2594  */
2595 	if (io_completion_port_add_socket(fd, interf))
2596 	{
2597 		msyslog(LOG_ERR, "unable to set up io completion port - EXITING");
2598 		exit(1);
2599 	}
2600 #endif
2601 	return fd;
2602 }
2603 
2604 /* XXX ELIMINATE sendpkt similar in ntpq.c, ntpdc.c, ntp_io.c, ntptrace.c */
2605 /*
2606  * sendpkt - send a packet to the specified destination. Maintain a
2607  * send error cache so that only the first consecutive error for a
2608  * destination is logged.
2609  */
2610 void
2611 sendpkt(
2612 	struct sockaddr_storage *dest,
2613 	struct interface *inter,
2614 	int ttl,
2615 	struct pkt *pkt,
2616 	int len
2617 	)
2618 {
2619 	int cc, slot;
2620 
2621 	/*
2622 	 * Send error caches. Empty slots have port == 0
2623 	 * Set ERRORCACHESIZE to 0 to disable
2624 	 */
2625 	struct cache {
2626 		u_short port;
2627 		struct	in_addr addr;
2628 	};
2629 
2630 #ifdef INCLUDE_IPV6_SUPPORT
2631 	struct cache6 {
2632 		u_short port;
2633 		struct in6_addr addr;
2634 	};
2635 #endif /* INCLUDE_IPV6_SUPPORT */
2636 
2637 
2638 #ifndef ERRORCACHESIZE
2639 #define ERRORCACHESIZE 8
2640 #endif
2641 #if ERRORCACHESIZE > 0
2642 	static struct cache badaddrs[ERRORCACHESIZE];
2643 #ifdef INCLUDE_IPV6_SUPPORT
2644 	static struct cache6 badaddrs6[ERRORCACHESIZE];
2645 #endif /* INCLUDE_IPV6_SUPPORT */
2646 #else
2647 #define badaddrs ((struct cache *)0)		/* Only used in empty loops! */
2648 #ifdef INCLUDE_IPV6_SUPPORT
2649 #define badaddrs6 ((struct cache6 *)0)		/* Only used in empty loops! */
2650 #endif /* INCLUDE_IPV6_SUPPORT */
2651 #endif
2652 #ifdef DEBUG
2653 	if (debug > 1)
2654 	  {
2655 	    if (inter != NULL)
2656 	      {
2657 		printf("%ssendpkt(fd=%d dst=%s, src=%s, ttl=%d, len=%d)\n",
2658 		       (ttl > 0) ? "\tMCAST\t***** " : "",
2659 		       inter->fd, stoa(dest),
2660 		       stoa(&inter->sin), ttl, len);
2661 	      }
2662 	    else
2663 	      {
2664 		printf("%ssendpkt(dst=%s, ttl=%d, len=%d): no interface - IGNORED\n",
2665 		       (ttl > 0) ? "\tMCAST\t***** " : "",
2666 		       stoa(dest),
2667 		       ttl, len);
2668 	      }
2669 	  }
2670 #endif
2671 
2672 	if (inter == NULL)	/* unbound peer - drop request and wait for better network conditions */
2673 	  return;
2674 
2675 #ifdef MCAST
2676 
2677 	/*
2678 	 * for the moment we use the bcast option to set multicast ttl
2679 	 */
2680 	if (ttl > 0 && ttl != inter->last_ttl) {
2681 
2682 		/*
2683 		 * set the multicast ttl for outgoing packets
2684 		 */
2685 		int rtc;
2686 
2687 		switch (inter->sin.ss_family) {
2688 
2689 		case AF_INET :
2690 		{
2691 			u_char mttl = (u_char) ttl;
2692 
2693 			rtc = setsockopt(inter->fd, IPPROTO_IP, IP_MULTICAST_TTL,
2694 					 (const void *) &mttl, sizeof(mttl));
2695 			break;
2696 		}
2697 
2698 #ifdef INCLUDE_IPV6_SUPPORT
2699 		case AF_INET6 :
2700 		{
2701 			u_int ittl = (u_char) ttl;
2702 
2703 			rtc = setsockopt(inter->fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
2704 					 (const void *) &ittl, sizeof(ittl));
2705 			break;
2706 		}
2707 
2708 #endif /* INCLUDE_IPV6_SUPPORT */
2709 		default:	/* just NOP if not supported */
2710 			rtc = 0;
2711 			break;
2712 		}
2713 
2714 		if (rtc != 0) {
2715 			netsyslog(LOG_ERR, "setsockopt IP_MULTICAST_TTL/IPV6_MULTICAST_HOPS fails on address %s: %m",
2716 				  stoa(&inter->sin));
2717 		}
2718 		else
2719 			inter->last_ttl = ttl;
2720 	}
2721 
2722 #endif /* MCAST */
2723 
2724 	for (slot = ERRORCACHESIZE; --slot >= 0; )
2725 		if(dest->ss_family == AF_INET) {
2726 			if (badaddrs[slot].port == SRCPORT(dest) &&
2727 				badaddrs[slot].addr.s_addr == ((struct sockaddr_in*)dest)->sin_addr.s_addr)
2728 			break;
2729 		}
2730 #ifdef INCLUDE_IPV6_SUPPORT
2731 		else if (dest->ss_family == AF_INET6) {
2732 			if (badaddrs6[slot].port == SRCPORT(dest) &&
2733 				!memcmp(&badaddrs6[slot].addr, &((struct sockaddr_in6*)dest)->sin6_addr, sizeof(struct in6_addr)))
2734 			break;
2735 		}
2736 #endif /* INCLUDE_IPV6_SUPPORT */
2737 
2738 #if defined(HAVE_IO_COMPLETION_PORT)
2739         cc = io_completion_port_sendto(inter, pkt, len, dest);
2740 	if (cc != ERROR_SUCCESS)
2741 #else
2742 #ifdef SIM
2743         cc = srvr_rply(&ntp_node,  dest, inter, pkt);
2744 #else /* SIM */
2745 	cc = sendto(inter->fd, (char *)pkt, (unsigned int)len, 0, (struct sockaddr *)dest,
2746 		    SOCKLEN(dest));
2747 #endif /* SIM */
2748 	if (cc == -1)
2749 #endif
2750 	{
2751 		inter->notsent++;
2752 		packets_notsent++;
2753 
2754 #if defined(HAVE_IO_COMPLETION_PORT)
2755 		if (cc != WSAEWOULDBLOCK && cc != WSAENOBUFS && slot < 0)
2756 #else
2757 		if (errno != EWOULDBLOCK && errno != ENOBUFS && slot < 0)
2758 #endif
2759 		{
2760 			/*
2761 			 * Remember this, if there's an empty slot
2762 			 */
2763 			switch (dest->ss_family) {
2764 
2765 			case AF_INET :
2766 
2767 				for (slot = ERRORCACHESIZE; --slot >= 0; )
2768 					if (badaddrs[slot].port == 0)
2769 					{
2770 						badaddrs[slot].port = SRCPORT(dest);
2771 						badaddrs[slot].addr = ((struct sockaddr_in*)dest)->sin_addr;
2772 						break;
2773 					}
2774 				break;
2775 
2776 #ifdef INCLUDE_IPV6_SUPPORT
2777 			case AF_INET6 :
2778 
2779 				for (slot = ERRORCACHESIZE; --slot >= 0; )
2780 					if (badaddrs6[slot].port == 0)
2781 					{
2782 						badaddrs6[slot].port = SRCPORT(dest);
2783 						badaddrs6[slot].addr = ((struct sockaddr_in6*)dest)->sin6_addr;
2784 						break;
2785 					}
2786 				break;
2787 #endif /* INCLUDE_IPV6_SUPPORT */
2788 			default:  /* don't care if not supported */
2789 				break;
2790 			}
2791 
2792 			netsyslog(LOG_ERR, "sendto(%s) (fd=%d): %m",
2793 				  stoa(dest), inter->fd);
2794 		}
2795 	}
2796 	else
2797 	{
2798 		inter->sent++;
2799 		packets_sent++;
2800 		/*
2801 		 * He's not bad any more
2802 		 */
2803 		if (slot >= 0)
2804 		{
2805 			netsyslog(LOG_INFO, "Connection re-established to %s", stoa(dest));
2806 			switch (dest->ss_family) {
2807 			case AF_INET :
2808 				badaddrs[slot].port = 0;
2809 				break;
2810 #ifdef INCLUDE_IPV6_SUPPORT
2811 			case AF_INET6 :
2812 				badaddrs6[slot].port = 0;
2813 				break;
2814 #endif /* INCLUDE_IPV6_SUPPORT */
2815 			default:  /* don't care if not supported */
2816 				break;
2817 			}
2818 		}
2819 	}
2820 }
2821 
2822 #if !defined(HAVE_IO_COMPLETION_PORT)
2823 /*
2824  * fdbits - generate ascii representation of fd_set (FAU debug support)
2825  * HFDF format - highest fd first.
2826  */
2827 static char *
2828 fdbits(
2829 	int count,
2830 	fd_set *set
2831 	)
2832 {
2833 	static char buffer[256];
2834 	char * buf = buffer;
2835 
2836 	count = (count < 256) ? count : 255;
2837 
2838 	while (count >= 0)
2839 	{
2840 		*buf++ = FD_ISSET(count, set) ? '#' : '-';
2841 		count--;
2842 	}
2843 	*buf = '\0';
2844 
2845 	return buffer;
2846 }
2847 
2848 /*
2849  * Routine to read the refclock packets for a specific interface
2850  * Return the number of bytes read. That way we know if we should
2851  * read it again or go on to the next one if no bytes returned
2852  */
2853 static inline int
2854 read_refclock_packet(SOCKET fd, struct refclockio *rp, l_fp ts)
2855 {
2856 	int i;
2857 	int buflen;
2858 	register struct recvbuf *rb;
2859 
2860 	rb = get_free_recv_buffer();
2861 
2862 	if (rb == NULL)
2863 	{
2864 		/*
2865 		 * No buffer space available - just drop the packet
2866 		 */
2867 		char buf[RX_BUFF_SIZE];
2868 
2869 		buflen = read(fd, buf, sizeof buf);
2870 		packets_dropped++;
2871 		return (buflen);
2872 	}
2873 
2874 	i = (rp->datalen == 0
2875 	    || rp->datalen > sizeof(rb->recv_space))
2876 	    ? sizeof(rb->recv_space) : rp->datalen;
2877 	buflen = read(fd, (char *)&rb->recv_space, (unsigned)i);
2878 
2879 	if (buflen < 0)
2880 	{
2881 		if (errno != EINTR && errno != EAGAIN) {
2882 			netsyslog(LOG_ERR, "clock read fd %d: %m", fd);
2883 		}
2884 		freerecvbuf(rb);
2885 		return (buflen);
2886 	}
2887 
2888 	/*
2889 	 * Got one. Mark how and when it got here,
2890 	 * put it on the full list and do bookkeeping.
2891 	 */
2892 	rb->recv_length = buflen;
2893 	rb->recv_srcclock = rp->srcclock;
2894 	rb->dstadr = 0;
2895 	rb->fd = fd;
2896 	rb->recv_time = ts;
2897 	rb->receiver = rp->clock_recv;
2898 
2899 	if (rp->io_input)
2900 	{
2901 		/*
2902 		 * have direct input routine for refclocks
2903 		 */
2904 		if (rp->io_input(rb) == 0)
2905 		{
2906 			/*
2907 			 * data was consumed - nothing to pass up
2908 			 * into block input machine
2909 			 */
2910 			freerecvbuf(rb);
2911 			return (buflen);
2912 		}
2913 	}
2914 
2915 	add_full_recv_buffer(rb);
2916 
2917 	rp->recvcount++;
2918 	packets_received++;
2919 	return (buflen);
2920 }
2921 
2922 #ifdef HAVE_TIMESTAMP
2923 /*
2924  * extract timestamps from control message buffer
2925  */
2926 static l_fp
2927 	fetch_timestamp(struct recvbuf *rb, struct msghdr *msghdr, l_fp ts)
2928 {
2929 #ifdef USE_TIMESTAMP_CMSG
2930 	struct cmsghdr *cmsghdr;
2931 
2932 	cmsghdr = CMSG_FIRSTHDR(msghdr);
2933 	while (cmsghdr != NULL) {
2934 		switch (cmsghdr->cmsg_type)
2935 		{
2936 		case SCM_TIMESTAMP:
2937 		{
2938 			struct timeval *tvp = (struct timeval *)CMSG_DATA(cmsghdr);
2939 			double dtemp;
2940 			l_fp nts;
2941 			DPRINTF(4, ("fetch_timestamp: system network time stamp: %ld.%06ld\n", tvp->tv_sec, tvp->tv_usec));
2942 			nts.l_i = tvp->tv_sec + JAN_1970;
2943 			dtemp = tvp->tv_usec / 1e6;
2944 
2945  			/* fuzz lower bits not covered by precision */
2946  			if (sys_precision != 0)
2947  				dtemp += (ntp_random() / FRAC - .5) / (1 <<
2948  								       -sys_precision);
2949 
2950 			nts.l_uf = (u_int32)(dtemp*FRAC);
2951 #ifdef DEBUG_TIMING
2952 			{
2953 				l_fp dts = ts;
2954 				L_SUB(&dts, &nts);
2955 				collect_timing(rb, "input processing delay", 1, &dts);
2956 				DPRINTF(4, ("fetch_timestamp: timestamp delta: %s (incl. prec fuzz)\n", lfptoa(&dts, 9)));
2957 			}
2958 #endif
2959 			ts = nts;  /* network time stamp */
2960 			break;
2961 		}
2962 		default:
2963 			DPRINTF(4, ("fetch_timestamp: skipping control message 0x%x\n", cmsghdr->cmsg_type));
2964 			break;
2965 		}
2966 		cmsghdr = CMSG_NXTHDR(msghdr, cmsghdr);
2967 	}
2968 #endif
2969 	return ts;
2970 }
2971 #endif
2972 
2973 /*
2974  * Routine to read the network NTP packets for a specific interface
2975  * Return the number of bytes read. That way we know if we should
2976  * read it again or go on to the next one if no bytes returned
2977  */
2978 static inline int
2979 read_network_packet(SOCKET fd, struct interface *itf, l_fp ts)
2980 {
2981 	GETSOCKNAME_SOCKLEN_TYPE fromlen;
2982 	int buflen;
2983 	register struct recvbuf *rb;
2984 #ifdef HAVE_TIMESTAMP
2985 	struct msghdr msghdr;
2986 	struct iovec iovec;
2987 	char control[TIMESTAMP_CTLMSGBUF_SIZE];	/* pick up control messages */
2988 #endif
2989 
2990 	/*
2991 	 * Get a buffer and read the frame.  If we
2992 	 * haven't got a buffer, or this is received
2993 	 * on a disallowed socket, just dump the
2994 	 * packet.
2995 	 */
2996 
2997 	rb = get_free_recv_buffer();
2998 
2999 	if (rb == NULL || itf->ignore_packets == ISC_TRUE)
3000 	{
3001 		char buf[RX_BUFF_SIZE];
3002 		struct sockaddr_storage from;
3003 		if (rb != NULL)
3004 			freerecvbuf(rb);
3005 
3006 		fromlen = sizeof(from);
3007 		buflen = recvfrom(fd, buf, sizeof(buf), 0,
3008 				(struct sockaddr*)&from, &fromlen);
3009 		DPRINTF(4, ("%s on (%lu) fd=%d from %s\n",
3010 			(itf->ignore_packets == ISC_TRUE) ? "ignore" : "drop",
3011 			free_recvbuffs(), fd,
3012 			stoa(&from)));
3013 		if (itf->ignore_packets == ISC_TRUE)
3014 			packets_ignored++;
3015 		else
3016 			packets_dropped++;
3017 		return (buflen);
3018 	}
3019 
3020 	fromlen = sizeof(struct sockaddr_storage);
3021 
3022 #ifndef HAVE_TIMESTAMP
3023 	rb->recv_length = recvfrom(fd,
3024 			  (char *)&rb->recv_space,
3025 			   sizeof(rb->recv_space), 0,
3026 			   (struct sockaddr *)&rb->recv_srcadr,
3027 			   &fromlen);
3028 #else
3029 	iovec.iov_base        = (void *)&rb->recv_space;
3030 	iovec.iov_len         = sizeof(rb->recv_space);
3031 	msghdr.msg_name       = (void *)&rb->recv_srcadr;
3032 	msghdr.msg_namelen    = sizeof(rb->recv_srcadr);
3033 	msghdr.msg_iov        = &iovec;
3034 	msghdr.msg_iovlen     = 1;
3035 	msghdr.msg_control    = (void *)&control;
3036 	msghdr.msg_controllen = sizeof(control);
3037 	msghdr.msg_flags      = 0;
3038 	rb->recv_length       = recvmsg(fd, &msghdr, 0);
3039 #endif
3040 
3041 	buflen = rb->recv_length;
3042 
3043 	if (buflen == 0 || (buflen == -1 &&
3044 	    (errno==EWOULDBLOCK
3045 #ifdef EAGAIN
3046 	   || errno==EAGAIN
3047 #endif
3048 	 ))) {
3049 		freerecvbuf(rb);
3050 		return (buflen);
3051 	}
3052 	else if (buflen < 0)
3053 	{
3054 		netsyslog(LOG_ERR, "recvfrom(%s) fd=%d: %m",
3055 		stoa(&rb->recv_srcadr), fd);
3056 		DPRINTF(5, ("read_network_packet: fd=%d dropped (bad recvfrom)\n", fd));
3057 		freerecvbuf(rb);
3058 		return (buflen);
3059 	}
3060 
3061 #ifdef DEBUG
3062 	if (debug > 2) {
3063 		if(rb->recv_srcadr.ss_family == AF_INET)
3064 			printf("read_network_packet: fd=%d length %d from %08lx %s\n",
3065 				fd, buflen,
3066 				(u_long)ntohl(((struct sockaddr_in*)&rb->recv_srcadr)->sin_addr.s_addr) &
3067 				0x00000000ffffffff,
3068 				stoa(&rb->recv_srcadr));
3069 		else
3070 			printf("read_network_packet: fd=%d length %d from %s\n",
3071 				fd, buflen,
3072 				stoa(&rb->recv_srcadr));
3073 	}
3074 #endif
3075 
3076 	/*
3077 	 * Got one.  Mark how and when it got here,
3078 	 * put it on the full list and do bookkeeping.
3079 	 */
3080 	rb->dstadr = itf;
3081 	rb->fd = fd;
3082 #ifdef HAVE_TIMESTAMP
3083 	ts = fetch_timestamp(rb, &msghdr, ts);  /* pick up a network time stamp if possible */
3084 #endif
3085 	rb->recv_time = ts;
3086 	rb->receiver = receive;
3087 
3088 	add_full_recv_buffer(rb);
3089 
3090 	itf->received++;
3091 	packets_received++;
3092 	return (buflen);
3093 }
3094 
3095 /*
3096  * input_handler - receive packets asynchronously
3097  */
3098 void
3099 input_handler(
3100 	l_fp *cts
3101 	)
3102 {
3103 
3104 	int buflen;
3105 	int n;
3106 	int doing;
3107 	SOCKET fd;
3108 	struct timeval tvzero;
3109 	l_fp ts;			/* Timestamp at BOselect() gob */
3110 #ifdef DEBUG_TIMING
3111 	l_fp ts_e;			/* Timestamp at EOselect() gob */
3112 #endif
3113 	fd_set fds;
3114 	int select_count = 0;
3115 	struct interface *interface;
3116 #if defined(HAS_ROUTING_SOCKET)
3117 	struct asyncio_reader *asyncio_reader;
3118 #endif
3119 
3120 	handler_calls++;
3121 
3122 	/*
3123 	 * If we have something to do, freeze a timestamp.
3124 	 * See below for the other cases (nothing (left) to do or error)
3125 	 */
3126 	ts = *cts;
3127 
3128 	/*
3129 	 * Do a poll to see who has data
3130 	 */
3131 
3132 	fds = activefds;
3133 	tvzero.tv_sec = tvzero.tv_usec = 0;
3134 
3135 	n = select(maxactivefd+1, &fds, (fd_set *)0, (fd_set *)0, &tvzero);
3136 
3137 	/*
3138 	 * If there are no packets waiting just return
3139 	 */
3140 	if (n < 0)
3141 	{
3142 		int err = errno;
3143 		/*
3144 		 * extended FAU debugging output
3145 		 */
3146 		if (err != EINTR)
3147 		    netsyslog(LOG_ERR,
3148 			      "select(%d, %s, 0L, 0L, &0.0) error: %m",
3149 			      maxactivefd+1,
3150 			      fdbits(maxactivefd, &activefds));
3151 		if (err == EBADF) {
3152 			int j, b;
3153 			fds = activefds;
3154 			for (j = 0; j <= maxactivefd; j++)
3155 			    if ((FD_ISSET(j, &fds) && (read(j, &b, 0) == -1)))
3156 				netsyslog(LOG_ERR, "Bad file descriptor %d", j);
3157 		}
3158 		return;
3159 	}
3160 	else if (n == 0)
3161 		return;
3162 
3163 	++handler_pkts;
3164 
3165 #ifdef REFCLOCK
3166 	/*
3167 	 * Check out the reference clocks first, if any
3168 	 */
3169 
3170 	if (refio != NULL)
3171 	{
3172 		register struct refclockio *rp;
3173 
3174 		for (rp = refio; rp != NULL; rp = rp->next)
3175 		{
3176 			fd = rp->fd;
3177 
3178 			if (FD_ISSET(fd, &fds))
3179 			{
3180 				do {
3181 					++select_count;
3182 					buflen = read_refclock_packet(fd, rp, ts);
3183 				} while (buflen > 0);
3184 
3185 			} /* End if (FD_ISSET(fd, &fds)) */
3186 		} /* End for (rp = refio; rp != 0 && n > 0; rp = rp->next) */
3187 	} /* End if (refio != 0) */
3188 
3189 #endif /* REFCLOCK */
3190 
3191 	/*
3192 	 * Loop through the interfaces looking for data to read.
3193 	 */
3194 	for (interface = ISC_LIST_TAIL(inter_list);
3195 	     interface != NULL;
3196 	     interface = ISC_LIST_PREV(interface, link))
3197 	{
3198 		for (doing = 0; (doing < 2); doing++)
3199 		{
3200 			if (doing == 0)
3201 			{
3202 				fd = interface->fd;
3203 			}
3204 			else
3205 			{
3206 				if (!(interface->flags & INT_BCASTOPEN))
3207 				    break;
3208 				fd = interface->bfd;
3209 			}
3210 			if (fd < 0) continue;
3211 			if (FD_ISSET(fd, &fds))
3212 			{
3213 				do {
3214 					++select_count;
3215 					buflen = read_network_packet(fd, interface, ts);
3216 				} while (buflen > 0);
3217 			}
3218 		/* Check more interfaces */
3219 		}
3220 	}
3221 
3222 #ifdef HAS_ROUTING_SOCKET
3223 	/*
3224 	 * scan list of asyncio readers - currently only used for routing sockets
3225 	 */
3226 	asyncio_reader = ISC_LIST_TAIL(asyncio_reader_list);
3227 
3228 	while (asyncio_reader != NULL)
3229 	{
3230 	        struct asyncio_reader *next = ISC_LIST_PREV(asyncio_reader, link);
3231 		if (FD_ISSET(asyncio_reader->fd, &fds)) {
3232 			++select_count;
3233 			asyncio_reader->receiver(asyncio_reader);
3234 		}
3235 		asyncio_reader = next;
3236 	}
3237 #endif /* HAS_ROUTING_SOCKET */
3238 
3239 	/*
3240 	 * Done everything from that select.
3241 	 */
3242 
3243 	/*
3244 	 * If nothing to do, just return.
3245 	 * If an error occurred, complain and return.
3246 	 */
3247 	if (select_count == 0) /* We really had nothing to do */
3248 	{
3249 #ifdef DEBUG
3250 		if (debug)
3251 		    netsyslog(LOG_DEBUG, "input_handler: select() returned 0");
3252 #endif
3253 		return;
3254 	}
3255 		/* We've done our work */
3256 #ifdef DEBUG_TIMING
3257 	get_systime(&ts_e);
3258 	/*
3259 	 * (ts_e - ts) is the amount of time we spent
3260 	 * processing this gob of file descriptors.  Log
3261 	 * it.
3262 	 */
3263 	L_SUB(&ts_e, &ts);
3264 	collect_timing(NULL, "input handler", 1, &ts_e);
3265 	if (debug > 3)
3266 	    netsyslog(LOG_INFO, "input_handler: Processed a gob of fd's in %s msec", lfptoms(&ts_e, 6));
3267 #endif
3268 	/* just bail. */
3269 	return;
3270 }
3271 
3272 #endif
3273 
3274 /*
3275  * findinterface - find local interface corresponding to address
3276  */
3277 struct interface *
3278 findinterface(
3279 	struct sockaddr_storage *addr
3280 	)
3281 {
3282 	struct interface *interface;
3283 
3284 	interface = findlocalinterface(addr, INT_WILDCARD);
3285 
3286 	if (interface == NULL)
3287 	{
3288 		DPRINTF(4, ("Found no interface for address %s - returning wildcard\n",
3289 			    stoa(addr)));
3290 
3291 		return (ANY_INTERFACE_CHOOSE(addr));
3292 	}
3293 	else
3294 	{
3295 		DPRINTF(4, ("Found interface #%d %s for address %s\n",
3296 			    interface->ifnum, interface->name, stoa(addr)));
3297 
3298 		return (interface);
3299 	}
3300 }
3301 
3302 /*
3303  * findlocalinterface - find local interface index corresponding to address
3304  *
3305  * This code attempts to find the local sending address for an outgoing
3306  * address by connecting a new socket to destinationaddress:NTP_PORT
3307  * and reading the sockname of the resulting connect.
3308  * the complicated sequence simulates the routing table lookup
3309  * for to first hop without duplicating any of the routing logic into
3310  * ntpd. preferably we would have used an API call - but its not there -
3311  * so this is the best we can do here short of duplicating to entire routing
3312  * logic in ntpd which would be a silly and really unportable thing to do.
3313  *
3314  */
3315 static struct interface *
3316 findlocalinterface(
3317 	struct sockaddr_storage *addr,
3318 	int flags
3319 	)
3320 {
3321 	SOCKET s;
3322 	int rtn;
3323 	struct sockaddr_storage saddr;
3324 	GETSOCKNAME_SOCKLEN_TYPE saddrlen = SOCKLEN(addr);
3325 	struct interface *iface;
3326 
3327 	DPRINTF(4, ("Finding interface for addr %s in list of addresses\n",
3328 		    stoa(addr)));
3329 
3330 	memset(&saddr, 0, sizeof(saddr));
3331 	saddr.ss_family = addr->ss_family;
3332 	if(addr->ss_family == AF_INET) {
3333 		memcpy(&((struct sockaddr_in*)&saddr)->sin_addr, &((struct sockaddr_in*)addr)->sin_addr, sizeof(struct in_addr));
3334 		((struct sockaddr_in*)&saddr)->sin_port = htons(NTP_PORT);
3335 	}
3336 #ifdef INCLUDE_IPV6_SUPPORT
3337 	else if(addr->ss_family == AF_INET6) {
3338  		memcpy(&((struct sockaddr_in6*)&saddr)->sin6_addr, &((struct sockaddr_in6*)addr)->sin6_addr, sizeof(struct in6_addr));
3339 		((struct sockaddr_in6*)&saddr)->sin6_port = htons(NTP_PORT);
3340 # ifdef ISC_PLATFORM_HAVESCOPEID
3341 		((struct sockaddr_in6*)&saddr)->sin6_scope_id = ((struct sockaddr_in6*)addr)->sin6_scope_id;
3342 # endif
3343 	}
3344 #endif
3345 
3346 	s = socket(addr->ss_family, SOCK_DGRAM, 0);
3347 	if (s == INVALID_SOCKET)
3348 		return NULL;
3349 
3350 	rtn = connect(s, (struct sockaddr *)&saddr, SOCKLEN(&saddr));
3351 #ifndef SYS_WINNT
3352 	if (rtn < 0)
3353 #else
3354 	if (rtn == SOCKET_ERROR)
3355 #endif
3356 	{
3357 		closesocket(s);
3358 		return NULL;
3359 	}
3360 
3361 	rtn = getsockname(s, (struct sockaddr *)&saddr, &saddrlen);
3362 	closesocket(s);
3363 #ifndef SYS_WINNT
3364 	if (rtn < 0)
3365 #else
3366 	if (rtn == SOCKET_ERROR)
3367 #endif
3368 		return NULL;
3369 
3370 	DPRINTF(4, ("findlocalinterface: kernel maps %s to %s\n", stoa(addr), stoa(&saddr)));
3371 
3372 	iface = getinterface(&saddr, flags);
3373 
3374 	/* Don't both with ignore interfaces */
3375 	if (iface != NULL && iface->ignore_packets == ISC_TRUE)
3376 	{
3377 		return NULL;
3378 	}
3379 	else
3380 	{
3381 		return iface;
3382 	}
3383 }
3384 
3385 /*
3386  * fetch an interface structure the matches the
3387  * address is has the given flags not set
3388  */
3389 static struct interface *
3390 getinterface(struct sockaddr_storage *addr, int flags)
3391 {
3392 	struct interface *interface = find_addr_in_list(addr);
3393 
3394 	if (interface != NULL && interface->flags & flags)
3395 	{
3396 		return NULL;
3397 	}
3398 	else
3399 	{
3400 		return interface;
3401 	}
3402 }
3403 
3404 /*
3405  * findlocalcastinterface - find local *cast interface index corresponding to address
3406  * depending on the flags passed
3407  */
3408 static struct interface *
3409 findlocalcastinterface(
3410 	struct sockaddr_storage *addr, int flags
3411 	)
3412 {
3413 	struct interface *interface;
3414 	struct interface *nif = NULL;
3415 #ifdef INCLUDE_IPV6_MULTICAST_SUPPORT
3416 	isc_boolean_t want_linklocal;
3417 #endif
3418 
3419 	/*
3420 	 * see how kernel maps the mcast address
3421 	 */
3422         nif = findlocalinterface(addr, 0);
3423 
3424 	if (nif) {
3425 		DPRINTF(2, ("findlocalcastinterface: kernel recommends interface #%d %s\n", nif->ifnum, nif->name));
3426 		return nif;
3427 	}
3428 
3429 #ifdef INCLUDE_IPV6_MULTICAST_SUPPORT
3430 	want_linklocal = ISC_FALSE;
3431 	if (addr_ismulticast(addr) && flags == INT_MULTICAST)
3432 	{
3433 		if (IN6_IS_ADDR_MC_LINKLOCAL(&((struct sockaddr_in6*)addr)->sin6_addr))
3434 		{
3435 			want_linklocal = ISC_TRUE;
3436 		}
3437 		else if (IN6_IS_ADDR_MC_SITELOCAL(&((struct sockaddr_in6*)addr)->sin6_addr))
3438 		{
3439 			want_linklocal = ISC_TRUE;
3440 		}
3441 	}
3442 #endif
3443 
3444 	for (interface = ISC_LIST_HEAD(inter_list);
3445 	     interface != NULL;
3446 	     interface = ISC_LIST_NEXT(interface, link))
3447 	  {
3448 		/* use only allowed addresses */
3449 		if (interface->ignore_packets == ISC_TRUE)
3450 			continue;
3451 
3452 		/* Skip the loopback and wildcard addresses */
3453 		if (interface->flags & (INT_LOOPBACK|INT_WILDCARD))
3454 			continue;
3455 
3456 		/* Skip if different family */
3457 		if(interface->sin.ss_family != addr->ss_family)
3458 			continue;
3459 
3460 		/* Is this it one of these based on flags? */
3461 		if (!(interface->flags & flags))
3462 			continue;
3463 
3464 		/* for IPv6 multicast check the address for linklocal */
3465 #ifdef INCLUDE_IPV6_MULTICAST_SUPPORT
3466 		if (flags == INT_MULTICAST && interface->sin.ss_family == AF_INET6 &&
3467 		   (IN6_IS_ADDR_LINKLOCAL(&((struct sockaddr_in6*)&interface->sin)->sin6_addr))
3468 		   && want_linklocal == ISC_TRUE)
3469 		{
3470 			nif = interface;
3471 			break;
3472 		}
3473 		/* If we want a linklocal address and this isn't it, skip */\
3474 		if (want_linklocal == ISC_TRUE)
3475 			continue;
3476 #endif
3477 		/* Otherwise just look for the flag */
3478 		if((interface->flags & flags))
3479 		{
3480 			nif = interface;
3481 			break;
3482 		}
3483 	}
3484 #ifdef DEBUG
3485 	if (debug > 2)
3486 	{
3487 		if (nif)
3488 			printf("findlocalcastinterface: found interface #%d %s\n", nif->ifnum, nif->name);
3489 		else
3490 			printf("findlocalcastinterface: no interface found for %s flags 0x%x\n", stoa(addr), flags);
3491 	}
3492 #endif
3493 	return (nif);
3494 }
3495 
3496 /*
3497  * findbcastinter - find broadcast interface corresponding to address
3498  */
3499 struct interface *
3500 findbcastinter(
3501 	struct sockaddr_storage *addr
3502 	)
3503 {
3504 #if !defined(MPE) && (defined(SIOCGIFCONF) || defined(SYS_WINNT))
3505         struct interface *interface;
3506 
3507 
3508 	DPRINTF(4, ("Finding broadcast/multicast interface for addr %s in list of addresses\n",
3509 		    stoa(addr)));
3510 
3511 	interface = findlocalinterface(addr, INT_LOOPBACK|INT_WILDCARD);
3512 
3513 	if (interface != NULL)
3514 	{
3515 		DPRINTF(4, ("Found bcast-/mcast- interface index #%d %s\n", interface->ifnum, interface->name));
3516 		return interface;
3517 	}
3518 
3519 	/* plan B - try to find something reasonable in our lists in case kernel lookup doesn't help */
3520 
3521 	for (interface = ISC_LIST_HEAD(inter_list);
3522 	     interface != NULL;
3523 	     interface = ISC_LIST_NEXT(interface, link))
3524 	{
3525 	        if (interface->flags & INT_WILDCARD)
3526 		        continue;
3527 
3528 		/* Don't bother with ignored interfaces */
3529 		if (interface->ignore_packets == ISC_TRUE)
3530 			continue;
3531 
3532 		/*
3533 		 * First look if this is the correct family
3534 		 */
3535 		if(interface->sin.ss_family != addr->ss_family)
3536 	  		continue;
3537 
3538 		/* Skip the loopback addresses */
3539 		if (interface->flags & INT_LOOPBACK)
3540 			continue;
3541 
3542 		/*
3543 		 * If we are looking to match a multicast address grab it.
3544 		 */
3545 		if (addr_ismulticast(addr) == ISC_TRUE && interface->flags & INT_MULTICAST)
3546 		{
3547 #ifdef INCLUDE_IPV6_SUPPORT
3548 			if(addr->ss_family == AF_INET6) {
3549 				/* Only use link-local address for link-scope mcast */
3550 				if(IN6_IS_ADDR_MC_LINKLOCAL(&((struct sockaddr_in6*)addr)->sin6_addr) &&
3551 				  !IN6_IS_ADDR_LINKLOCAL(&((struct sockaddr_in6*)&interface->sin)->sin6_addr)) {
3552 					continue;
3553 				}
3554 			}
3555 #endif
3556 			break;
3557 		}
3558 
3559 		/*
3560 		 * We match only those interfaces marked as
3561 		 * broadcastable and either the explicit broadcast
3562 		 * address or the network portion of the IP address.
3563 		 * Sloppy.
3564 		 */
3565 		if(addr->ss_family == AF_INET) {
3566 			if (SOCKCMP(&interface->bcast, addr)) {
3567 				break;
3568 			}
3569 			if ((NSRCADR(&interface->sin) &
3570 			     NSRCADR(&interface->mask)) == (NSRCADR(addr) &
3571 							    NSRCADR(&interface->mask)))
3572 				break;
3573 		}
3574 #ifdef INCLUDE_IPV6_SUPPORT
3575 		else if(addr->ss_family == AF_INET6) {
3576 			if (SOCKCMP(&interface->bcast, addr)) {
3577 				break;
3578 			}
3579 			if (SOCKCMP(netof(&interface->sin), netof(addr))) {
3580 				break;
3581 			}
3582 		}
3583 #endif
3584 	}
3585 #endif /* SIOCGIFCONF */
3586 	if (interface == NULL) {
3587 		DPRINTF(4, ("No bcast interface found for %s\n", stoa(addr)));
3588 		return ANY_INTERFACE_CHOOSE(addr);
3589 	} else {
3590 		DPRINTF(4, ("Found bcast-/mcast- interface index #%d %s\n", interface->ifnum, interface->name));
3591 		return interface;
3592 	}
3593 }
3594 
3595 
3596 /*
3597  * io_clr_stats - clear I/O module statistics
3598  */
3599 void
3600 io_clr_stats(void)
3601 {
3602 	packets_dropped = 0;
3603 	packets_ignored = 0;
3604 	packets_received = 0;
3605 	packets_sent = 0;
3606 	packets_notsent = 0;
3607 
3608 	handler_calls = 0;
3609 	handler_pkts = 0;
3610 	io_timereset = current_time;
3611 }
3612 
3613 
3614 #ifdef REFCLOCK
3615 /*
3616  * io_addclock - add a reference clock to the list and arrange that we
3617  *				 get SIGIO interrupts from it.
3618  */
3619 int
3620 io_addclock(
3621 	struct refclockio *rio
3622 	)
3623 {
3624 	BLOCKIO();
3625 	/*
3626 	 * Stuff the I/O structure in the list and mark the descriptor
3627 	 * in use.	There is a harmless (I hope) race condition here.
3628 	 */
3629 	rio->next = refio;
3630 
3631 # ifdef HAVE_SIGNALED_IO
3632 	if (init_clock_sig(rio))
3633 	{
3634 		UNBLOCKIO();
3635 		return 0;
3636 	}
3637 # elif defined(HAVE_IO_COMPLETION_PORT)
3638 	if (io_completion_port_add_clock_io(rio))
3639 	{
3640 		UNBLOCKIO();
3641 		return 0;
3642 	}
3643 # endif
3644 
3645 	/*
3646 	 * enqueue
3647 	 */
3648 	refio = rio;
3649 
3650         /*
3651 	 * register fd
3652 	 */
3653 	add_fd_to_list(rio->fd, FD_TYPE_FILE);
3654 
3655 	UNBLOCKIO();
3656 	return 1;
3657 }
3658 
3659 /*
3660  * io_closeclock - close the clock in the I/O structure given
3661  */
3662 void
3663 io_closeclock(
3664 	struct refclockio *rio
3665 	)
3666 {
3667 	BLOCKIO();
3668 	/*
3669 	 * Remove structure from the list
3670 	 */
3671 	if (refio == rio)
3672 	{
3673 		refio = rio->next;
3674 	}
3675 	else
3676 	{
3677 		register struct refclockio *rp;
3678 
3679 		for (rp = refio; rp != NULL; rp = rp->next)
3680 		    if (rp->next == rio)
3681 		    {
3682 			    rp->next = rio->next;
3683 			    break;
3684 		    }
3685 
3686 		if (rp == NULL) {
3687 			UNBLOCKIO();
3688 			return;
3689 		}
3690 	}
3691 
3692 	/*
3693 	 * Close the descriptor.
3694 	 */
3695 	close_and_delete_fd_from_list(rio->fd);
3696 	UNBLOCKIO();
3697 }
3698 #endif	/* REFCLOCK */
3699 
3700 /*
3701  * On NT a SOCKET is an unsigned int so we cannot possibly keep it in
3702  * an array. So we use one of the ISC_LIST functions to hold the
3703  * socket value and use that when we want to enumerate it.
3704  */
3705 void
3706 kill_asyncio(int startfd)
3707 {
3708 	vsock_t *lsock;
3709 	vsock_t *next;
3710 
3711 	BLOCKIO();
3712 
3713 	lsock = ISC_LIST_HEAD(fd_list);
3714 	while (lsock != NULL) {
3715 		/*
3716 		 * careful here - list is being dismantled while
3717 		 * we scan it - setting next here insures that
3718 		 * we are able to correctly scan the list
3719 		 */
3720 		next = ISC_LIST_NEXT(lsock, link);
3721 		/*
3722 		 * will remove socket from list
3723 		 */
3724 		close_and_delete_fd_from_list(lsock->fd);
3725 		lsock = next;
3726 	}
3727 
3728 	UNBLOCKIO();
3729 }
3730 
3731 /*
3732  * Add and delete functions for the list of open sockets
3733  */
3734 static void
3735 add_fd_to_list(SOCKET fd, enum desc_type type) {
3736 	vsock_t *lsock = (vsock_t *)emalloc(sizeof(vsock_t));
3737 	lsock->fd = fd;
3738 	lsock->type = type;
3739 
3740 	ISC_LIST_APPEND(fd_list, lsock, link);
3741 	/*
3742 	 * I/O Completion Ports don't care about the select and FD_SET
3743 	 */
3744 #ifndef HAVE_IO_COMPLETION_PORT
3745 	if (fd < 0 || fd >= FD_SETSIZE) {
3746 		msyslog(LOG_ERR, "Too many sockets in use, FD_SETSIZE %d exceeded",
3747 			FD_SETSIZE);
3748 		exit(1);
3749 	}
3750 	/*
3751 	 * keep activefds in sync
3752 	 */
3753 	if (fd > maxactivefd)
3754 	    maxactivefd = fd;
3755 	FD_SET( (u_int)fd, &activefds);
3756 #endif
3757 }
3758 
3759 static void
3760 close_and_delete_fd_from_list(SOCKET fd) {
3761 
3762 	vsock_t *next;
3763 	vsock_t *lsock = ISC_LIST_HEAD(fd_list);
3764 
3765 	while(lsock != NULL) {
3766 		next = ISC_LIST_NEXT(lsock, link);
3767 		if(lsock->fd == fd) {
3768 			ISC_LIST_DEQUEUE_TYPE(fd_list, lsock, link, vsock_t);
3769 
3770 			switch (lsock->type) {
3771 			case FD_TYPE_SOCKET:
3772 #ifdef SYS_WINNT
3773 				closesocket(lsock->fd);
3774 				break;
3775 #endif
3776 			case FD_TYPE_FILE:
3777 				(void) close(lsock->fd);
3778 				break;
3779 			default:
3780 				msyslog(LOG_ERR, "internal error - illegal descriptor type %d - EXITING", (int)lsock->type);
3781 				exit(1);
3782 			}
3783 
3784 			free(lsock);
3785 			/*
3786 			 * I/O Completion Ports don't care about select and fd_set
3787 			 */
3788 #ifndef HAVE_IO_COMPLETION_PORT
3789 			/*
3790 			 * remove from activefds
3791 			 */
3792 			FD_CLR( (u_int) fd, &activefds);
3793 
3794 			if (fd == maxactivefd) {
3795 				int i, newmax = 0;
3796 				for (i = 0; i < maxactivefd; i++)
3797 					if (FD_ISSET(i, &activefds))
3798 						newmax = i;
3799 				maxactivefd = newmax;
3800 			}
3801 #endif
3802 			break;
3803 		}
3804 		lsock = next;
3805 	}
3806 }
3807 
3808 static void
3809 add_addr_to_list(struct sockaddr_storage *addr, struct interface *interface){
3810 #ifdef DEBUG
3811 	if (find_addr_in_list(addr) == NULL) {
3812 #endif
3813 		/* not there yet - add to list */
3814 		remaddr_t *laddr = (remaddr_t *)emalloc(sizeof(remaddr_t));
3815 		memcpy(&laddr->addr, addr, sizeof(struct sockaddr_storage));
3816 		laddr->interface = interface;
3817 
3818 		ISC_LIST_APPEND(remoteaddr_list, laddr, link);
3819 
3820 		DPRINTF(4, ("Added addr %s to list of addresses\n",
3821 			    stoa(addr)));
3822 #ifdef DEBUG
3823 	} else {
3824 		DPRINTF(4, ("WARNING: Attempt to add duplicate addr %s to address list\n",
3825 			    stoa(addr)));
3826 	}
3827 #endif
3828 }
3829 
3830 static void
3831 delete_addr_from_list(struct sockaddr_storage *addr) {
3832 
3833 	remaddr_t *next;
3834 	remaddr_t *laddr = ISC_LIST_HEAD(remoteaddr_list);
3835 
3836 	while(laddr != NULL) {
3837 		next = ISC_LIST_NEXT(laddr, link);
3838 		if(SOCKCMP(&laddr->addr, addr)) {
3839 			ISC_LIST_DEQUEUE_TYPE(remoteaddr_list, laddr, link, remaddr_t);
3840 			DPRINTF(4, ("Deleted addr %s from list of addresses\n",
3841 				    stoa(addr)));
3842 			free(laddr);
3843 			break;
3844 		}
3845 		laddr = next;
3846 	}
3847 }
3848 
3849 static void
3850 delete_interface_from_list(struct interface *iface) {
3851 	remaddr_t *next;
3852 	remaddr_t *laddr = ISC_LIST_HEAD(remoteaddr_list);
3853 
3854 	while(laddr != NULL) {
3855 		next = ISC_LIST_NEXT(laddr, link);
3856 		if (laddr->interface == iface) {
3857 			ISC_LIST_DEQUEUE_TYPE(remoteaddr_list, laddr, link, remaddr_t);
3858 			DPRINTF(4, ("Deleted addr %s for interface #%d %s from list of addresses\n",
3859 				    stoa(&laddr->addr), iface->ifnum, iface->name));
3860 			free(laddr);
3861 		}
3862 		laddr = next;
3863 	}
3864 }
3865 
3866 static struct interface *
3867 find_addr_in_list(struct sockaddr_storage *addr) {
3868 
3869 	remaddr_t *next;
3870 	remaddr_t *laddr = ISC_LIST_HEAD(remoteaddr_list);
3871 	DPRINTF(4, ("Searching for addr %s in list of addresses - ",
3872 		    stoa(addr)));
3873 
3874 	while(laddr != NULL) {
3875 		next = ISC_LIST_NEXT(laddr, link);
3876 		if(SOCKCMP(&laddr->addr, addr)) {
3877 			DPRINTF(4, ("FOUND\n"));
3878 			return laddr->interface;
3879 		}
3880 		else
3881 			laddr = next;
3882 	}
3883 	DPRINTF(4, ("NOT FOUND\n"));
3884 	return NULL; /* Not found */
3885 }
3886 
3887 /*
3888  * Find the given address with the associated flag in the list
3889  */
3890 static struct interface *
3891 find_flagged_addr_in_list(struct sockaddr_storage *addr, int flag) {
3892 
3893 	remaddr_t *next;
3894 	remaddr_t *laddr = ISC_LIST_HEAD(remoteaddr_list);
3895 	DPRINTF(4, ("Finding addr %s in list of addresses\n",
3896 		    stoa(addr)));
3897 
3898 	while(laddr != NULL) {
3899 		next = ISC_LIST_NEXT(laddr, link);
3900 		if(SOCKCMP(&laddr->addr, addr) && (laddr->interface->flags & flag)) {
3901 			return laddr->interface;
3902 			break;
3903 		}
3904 		else
3905 			laddr = next;
3906 	}
3907 	return NULL; /* Not found */
3908 }
3909 
3910 #ifdef HAS_ROUTING_SOCKET
3911 #include <net/route.h>
3912 
3913 #ifndef UPDATE_GRACE
3914 #define UPDATE_GRACE	2	/* wait UPDATE_GRACE seconds before scanning */
3915 #endif
3916 
3917 static void
3918 process_routing_msgs(struct asyncio_reader *reader)
3919 {
3920 	char buffer[5120];
3921 	char *p = buffer;
3922 
3923 	int cnt;
3924 
3925 	if (disable_dynamic_updates) {
3926 		/*
3927 		 * discard ourselves if we are not need any more
3928 		 * usually happens when running unprivileged
3929 		 */
3930 		remove_asyncio_reader(reader);
3931 		delete_asyncio_reader(reader);
3932 		return;
3933 	}
3934 
3935 	cnt = read(reader->fd, buffer, sizeof(buffer));
3936 
3937 	if (cnt < 0) {
3938 		msyslog(LOG_ERR, "i/o error on routing socket %m - disabling");
3939 		remove_asyncio_reader(reader);
3940 		delete_asyncio_reader(reader);
3941 		return;
3942 	}
3943 
3944 	/*
3945 	 * process routing message
3946 	 */
3947 	while ((p + sizeof(struct rt_msghdr)) <= (buffer + cnt))
3948 	{
3949 		struct rt_msghdr *rtm;
3950 
3951 		rtm = (struct rt_msghdr *)p;
3952 		if (rtm->rtm_version != RTM_VERSION) {
3953 			msyslog(LOG_ERR, "version mismatch on routing socket %m - disabling");
3954 			remove_asyncio_reader(reader);
3955 			delete_asyncio_reader(reader);
3956 			return;
3957 		}
3958 
3959 		switch (rtm->rtm_type) {
3960 #ifdef RTM_NEWADDR
3961 		case RTM_NEWADDR:
3962 #endif
3963 #ifdef RTM_DELADDR
3964 		case RTM_DELADDR:
3965 #endif
3966 #ifdef RTM_ADD
3967 		case RTM_ADD:
3968 #endif
3969 #ifdef RTM_DELETE
3970 		case RTM_DELETE:
3971 #endif
3972 #ifdef RTM_REDIRECT
3973 		case RTM_REDIRECT:
3974 #endif
3975 #ifdef RTM_CHANGE
3976 		case RTM_CHANGE:
3977 #endif
3978 #ifdef RTM_LOSING
3979 		case RTM_LOSING:
3980 #endif
3981 #ifdef RTM_IFINFO
3982 		case RTM_IFINFO:
3983 #endif
3984 #ifdef RTM_IFANNOUNCE
3985 		case RTM_IFANNOUNCE:
3986 #endif
3987 			/*
3988 			 * we are keen on new and deleted addresses and if an interface goes up and down or routing changes
3989 			 */
3990 			DPRINTF(3, ("routing message op = %d: scheduling interface update\n", rtm->rtm_type));
3991 			timer_interfacetimeout(current_time + UPDATE_GRACE);
3992 			break;
3993 		default:
3994 			/*
3995 			 * the rest doesn't bother us.
3996 			 */
3997 			DPRINTF(4, ("routing message op = %d: ignored\n", rtm->rtm_type));
3998 			break;
3999 		}
4000 		p += rtm->rtm_msglen;
4001 	}
4002 }
4003 
4004 /*
4005  * set up routing notifications
4006  */
4007 static void
4008 init_async_notifications()
4009 {
4010 	struct asyncio_reader *reader;
4011 	int fd = socket(PF_ROUTE, SOCK_RAW, 0);
4012 
4013 	if (fd >= 0) {
4014 		fd = move_fd(fd);
4015 		init_nonblocking_io(fd);
4016 #if defined(HAVE_SIGNALED_IO)
4017 		init_socket_sig(fd);
4018 #endif /* HAVE_SIGNALED_IO */
4019 
4020 		reader = new_asyncio_reader();
4021 
4022 		reader->fd = fd;
4023 		reader->receiver = process_routing_msgs;
4024 
4025 		add_asyncio_reader(reader, FD_TYPE_SOCKET);
4026 		msyslog(LOG_INFO, "Listening on routing socket on fd #%d for interface updates", fd);
4027 	} else {
4028 		msyslog(LOG_ERR, "unable to open routing socket (%m) - using polled interface update");
4029 	}
4030 }
4031 #else
4032 static void
4033 init_async_notifications()
4034 {
4035 }
4036 #endif
4037