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