2 * ntp_io.c - input/output routines for ntpd. The socket-opening code
3 * was shamelessly stolen from ntpd.
10 #include "ntp_machine.h"
14 #include "ntp_refclock.h"
15 #include "ntp_stdlib.h"
16 #include "ntp_request.h"
18 #include "ntp_unixtime.h"
20 /* Don't include ISC's version of IPv6 variables and structures */
22 #include <isc/interfaceiter.h>
24 #include <isc/result.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>
38 #ifdef HAVE_SYS_SOCKIO_H /* UXPV: SIOC* #defines (Frank Vance <fvance@waii.com>) */
39 # include <sys/sockio.h>
46 * setsockopt does not always have the same arg declaration
47 * across all platforms. If it's not defined we make it empty
50 #ifndef SETSOCKOPT_ARG_CAST
51 #define SETSOCKOPT_ARG_CAST
55 * Set up some macros to look for IPv6 and IPv6 multicast
58 #if defined(ISC_PLATFORM_HAVEIPV6) && !defined(DISABLE_IPV6)
60 #define INCLUDE_IPV6_SUPPORT
62 #if defined(INCLUDE_IPV6_SUPPORT) && defined(IPV6_JOIN_GROUP) && defined(IPV6_LEAVE_GROUP)
63 #define INCLUDE_IPV6_MULTICAST_SUPPORT
65 #endif /* IPV6 Multicast Support */
66 #endif /* IPv6 Support */
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 */
74 extern int listen_to_virtual_ips;
75 extern const char *specific_interface;
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 */
85 /* fill in for old/other timestamp interfaces */
89 #if defined(SYS_WINNT)
90 #include <transmitbuff.h>
91 #include <isc/win32os.h>
93 * Define this macro to control the behavior of connection
94 * resets on UDP sockets. See Microsoft KnowledgeBase Article Q263823
96 * NOTE: This requires that Windows 2000 systems install Service Pack 2
99 #ifndef SIO_UDP_CONNRESET
100 #define SIO_UDP_CONNRESET _WSAIOW(IOC_VENDOR,12)
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.
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.
123 * Other statistics of possible interest
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 */
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 */
138 struct interface *any_interface; /* default ipv4 interface */
139 struct interface *any6_interface; /* default ipv6 interface */
140 struct interface *loopback_interface; /* loopback ipv4 interface */
142 int ninterfaces; /* Total number of interfaces */
144 volatile int disable_dynamic_updates; /* when set to != 0 dynamic updates won't happen */
148 * Refclock stuff. We keep a chain of structures with data concerning
149 * the guys we are doing I/O for.
151 static struct refclockio *refio;
152 #endif /* REFCLOCK */
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.
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.
163 #define SOFT_ERROR(e) ((e) == EAGAIN || \
164 (e) == EWOULDBLOCK || \
169 * File descriptor masks etc. for call to select
170 * Not needed for I/O Completion Ports
175 * bit alternating value to detect verified interfaces during an update cycle
177 static u_char sys_interphase = 0;
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 *));
185 static int move_fd P((SOCKET));
188 * Multicast functions
190 static isc_boolean_t addr_ismulticast P((struct sockaddr_storage *));
192 * Not all platforms support multicast
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 *));
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)
203 #define DPRINT_INTERFACE(_LVL_, _ARGS_)
206 typedef struct vsock vsock_t;
207 enum desc_type { FD_TYPE_SOCKET, FD_TYPE_FILE };
212 ISC_LINK(vsock_t) link;
215 #if !defined(HAVE_IO_COMPLETION_PORT) && defined(HAS_ROUTING_SOCKET)
217 * async notification processing (e. g. routing sockets)
220 * support for receiving data on fd that is not a refclock or a socket
221 * like e. g. routing sockets
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 */
230 ISC_LIST(struct asyncio_reader) asyncio_reader_list;
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 *));
237 #endif /* !defined(HAVE_IO_COMPLETION_PORT) && defined(HAS_ROUTING_SOCKET) */
239 static void init_async_notifications P((void));
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 *));
248 ISC_LIST(vsock_t) fd_list;
250 typedef struct remaddr remaddr_t;
253 struct sockaddr_storage addr;
254 struct interface *interface;
255 ISC_LINK(remaddr_t) link;
258 ISC_LIST(remaddr_t) remoteaddr_list;
260 ISC_LIST(struct interface) inter_list;
262 static struct interface *wildipv4 = NULL;
263 static struct interface *wildipv6 = NULL;
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));
280 * Routines to read the ntp packets
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));
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.
299 connection_reset_fix(SOCKET fd) {
300 DWORD dwBytesReturned = 0;
301 BOOL bNewBehavior = FALSE;
304 if(isc_win32os_majorversion() < 5)
305 return (ISC_R_SUCCESS); /* NT 4.0 has no problem */
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);
314 return (ISC_R_UNEXPECTED);
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
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
334 * - if that fails and boundary > 0 set boundary
335 * to min(0, socket_fd_boundary - FD_CHUNK)
337 * if failure and boundary == 0 return old fd
338 * - on success close old fd return new fd
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
347 static int move_fd(SOCKET fd)
349 #if !defined(SYS_WINNT) && defined(F_DUPFD)
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.
362 #ifndef FD_PREFERRED_SOCKBOUNDARY
363 #define FD_PREFERRED_SOCKBOUNDARY 48
366 #ifndef HAVE_GETDTABLESIZE
368 * if we have no idea about the max fd value set up things
369 * so we will start at FOPEN_MAX
371 #define getdtablesize() (FOPEN_MAX+FD_CHUNK)
375 #define FOPEN_MAX 20 /* assume that for the lack of anything better */
377 static SOCKET socket_boundary = -1;
381 * check whether boundary has be set up
384 if (socket_boundary == -1) {
385 socket_boundary = max(0, min(getdtablesize() - FD_CHUNK,
386 min(FOPEN_MAX, FD_PREFERRED_SOCKBOUNDARY)));
388 msyslog(LOG_DEBUG, "ntp_io: estimated max descriptors: %d, initial socket boundary: %d",
389 getdtablesize(), socket_boundary);
394 * Leave a space for stdio to work in. potentially moving the
395 * socket_boundary lower until allocation succeeds.
398 if (fd >= 0 && fd < socket_boundary) {
399 /* inside reserved range: attempt to move fd */
400 newfd = fcntl(fd, F_DUPFD, socket_boundary);
403 /* success: drop the old one - return the new one */
408 /* outside reserved range: no work - return the original one */
411 socket_boundary = max(0, socket_boundary - FD_CHUNK);
413 msyslog(LOG_DEBUG, "ntp_io: selecting new socket boundary: %d",
416 } while (socket_boundary > 0);
417 #endif /* !defined(SYS_WINNT) && defined(F_DUPFD) */
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
430 collect_timing(struct recvbuf *rb, const char *tag, int count, l_fp *dts)
434 snprintf(buf, sizeof(buf), "%s %d %s %s",
436 ((rb->dstadr) ? stoa(&rb->recv_srcadr) : "-REFCLOCK-") : "-",
437 count, lfptoa(dts, 9), tag);
438 record_timing_stats(buf);
443 * About dynamic interfaces, sockets, reception and more...
445 * the code solves following tasks:
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
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
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.
476 * Frank Kardel 20050910
480 * init_io - initialize I/O data structures and call socket creation routine
486 if (!Win32InitSockets())
488 netsyslog(LOG_ERR, "No useable winsock.dll: %m");
492 #endif /* SYS_WINNT */
495 * Init buffer free list and stat counters
497 init_recvbuff(RECV_INIT);
499 packets_dropped = packets_received = 0;
501 packets_sent = packets_notsent = 0;
502 handler_calls = handler_pkts = 0;
504 loopback_interface = NULL;
505 any_interface = NULL;
506 any6_interface = NULL;
512 #if defined(HAVE_SIGNALED_IO)
516 ISC_LIST_INIT(fd_list);
518 #if !defined(HAVE_IO_COMPLETION_PORT) && defined(HAS_ROUTING_SOCKET)
519 ISC_LIST_INIT(asyncio_reader_list);
522 ISC_LIST_INIT(remoteaddr_list);
524 ISC_LIST_INIT(inter_list);
530 (void) create_sockets(htons(NTP_PORT));
533 init_async_notifications();
535 DPRINTF(3, ("init_io: maxactivefd %d\n", maxactivefd));
540 * function to dump the contents of the interface structure
541 * for debugging use only.
544 interface_dump(struct interface *itf)
548 /* Limit the size of the sockaddr_storage hex dump */
549 int maxsize = min(32, sizeof(struct sockaddr_storage));
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++)
558 printf("%02x", *cp++);
563 printf("bcast = %s,\n", stoa(&(itf->bcast)));
564 cp = (u_char*) &(itf->bcast);
565 for(i = 0; i < maxsize; i++)
567 printf("%02x", *cp++);
572 printf("mask = %s,\n", stoa(&(itf->mask)));
573 cp = (u_char*) &(itf->mask);
574 for(i = 0; i < maxsize; i++)
576 printf("%02x", *cp++);
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);
596 * print_interface - helper to output debug information
599 print_interface(struct interface *iface, char *pfx, char *sfx)
601 printf("%sinterface #%d: fd=%d, bfd=%d, name=%s, flags=0x%x, scope=%d, ifindex=%d",
610 /* Leave these as three printf calls. */
612 stoa((&iface->sin)));
613 if (iface->flags & INT_BROADCAST)
614 printf(", bcast=%s,",
615 stoa((&iface->bcast)));
616 if (iface->family == AF_INET)
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);
626 #if !defined(HAVE_IO_COMPLETION_PORT) && defined(HAS_ROUTING_SOCKET)
628 * create an asyncio_reader structure
630 static struct asyncio_reader *
633 struct asyncio_reader *reader;
635 reader = (struct asyncio_reader *)emalloc(sizeof(struct asyncio_reader));
637 memset((char *)reader, 0, sizeof(*reader));
638 ISC_LINK_INIT(reader, link);
639 reader->fd = INVALID_SOCKET;
647 delete_asyncio_reader(struct asyncio_reader *reader)
653 * add asynchio_reader
656 add_asyncio_reader(struct asyncio_reader *reader, enum desc_type type)
658 ISC_LIST_APPEND(asyncio_reader_list, reader, link);
659 add_fd_to_list(reader->fd, type);
663 * remove asynchio_reader
666 remove_asyncio_reader(struct asyncio_reader *reader)
668 ISC_LIST_UNLINK_TYPE(asyncio_reader_list, reader, link, struct asyncio_reader);
670 if (reader->fd != INVALID_SOCKET)
671 close_and_delete_fd_from_list(reader->fd);
673 reader->fd = INVALID_SOCKET;
675 #endif /* !defined(HAVE_IO_COMPLETION_PORT) && defined(HAS_ROUTING_SOCKET) */
678 * interface list enumerator - visitor pattern
681 interface_enumerate(interface_receiver_t receiver, void *data)
683 interface_info_t ifi;
684 struct interface *interf;
686 ifi.action = IFS_EXISTS;
688 for (interf = ISC_LIST_HEAD(inter_list);
690 interf = ISC_LIST_NEXT(interf, link)) {
691 ifi.interface = interf;
692 receiver(data, &ifi);
697 * do standard initialization of interface structure
700 init_interface(struct interface *interface)
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;
710 interface->notsent = 0;
711 interface->peercnt = 0;
712 interface->phase = sys_interphase;
716 * create new interface structure initialize from
717 * template structure or via standard initialization
720 static struct interface *
721 new_interface(struct interface *interface)
723 static u_int sys_ifnum = 0;
725 struct interface *iface = (struct interface *)emalloc(sizeof(struct interface));
727 if (interface != NULL)
729 memcpy((char*)iface, (char*)interface, sizeof(*interface));
733 init_interface(iface);
736 iface->ifnum = sys_ifnum++; /* count every new instance of an interface in the system */
737 iface->starttime = current_time;
743 * return interface storage into free memory pool
746 delete_interface(struct interface *interface)
752 * link interface into list of known interfaces
755 add_interface(struct interface *interface)
758 * Calculate the address hash
760 interface->addr_refid = addr2refid(&interface->sin);
762 ISC_LIST_APPEND(inter_list, interface, link);
767 * remove interface from known interface list and clean up
768 * associated resources
771 remove_interface(struct interface *interface)
773 struct sockaddr_storage resmask;
775 ISC_LIST_UNLINK_TYPE(inter_list, interface, link, struct interface);
777 delete_interface_from_list(interface);
779 if (interface->fd != INVALID_SOCKET)
781 msyslog(LOG_INFO, "Deleting interface #%d %s, %s#%d, interface stats: received=%ld, sent=%ld, dropped=%ld, active_time=%ld secs",
784 stoa((&interface->sin)),
785 NTP_PORT, /* XXX should extract port from sin structure */
789 current_time - interface->starttime);
791 close_and_delete_fd_from_list(interface->fd);
794 if (interface->bfd != INVALID_SOCKET)
796 msyslog(LOG_INFO, "Deleting interface #%d %s, broadcast address %s#%d",
799 stoa((&interface->bcast)),
800 (u_short) NTP_PORT); /* XXX extract port from sin structure */
801 close_and_delete_fd_from_list(interface->bfd);
805 ntp_monclearinterface(interface);
807 /* remove restrict interface entry */
810 * Blacklist bound interface address
812 SET_HOSTMASK(&resmask, interface->sin.ss_family);
813 hack_restrict(RESTRICT_REMOVEIF, &interface->sin, &resmask,
814 RESM_NTPONLY|RESM_INTERFACE, RES_IGNORE);
818 list_if_listening(struct interface *interface, u_short port)
820 msyslog(LOG_INFO, "Listening on interface #%d %s, %s#%d %s",
823 stoa((&interface->sin)),
824 ntohs( (u_short) port),
825 (interface->ignore_packets == ISC_FALSE) ?
826 "Enabled": "Disabled");
830 create_wildcards(u_short port) {
831 isc_boolean_t okipv4 = ISC_TRUE;
833 * create pseudo-interface with wildcard IPv4 address
836 if(isc_net_probeipv4() != ISC_R_SUCCESS)
840 if(okipv4 == ISC_TRUE) {
841 struct interface *interface = new_interface(NULL);
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;
854 * enable possible multicast reception on the broadcast socket
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);
860 interface->fd = open_socket(&interface->sin,
861 interface->flags, 1, interface);
863 if (interface->fd != INVALID_SOCKET) {
864 wildipv4 = interface;
865 any_interface = interface;
867 add_addr_to_list(&interface->sin, interface);
868 add_interface(interface);
869 list_if_listening(interface, port);
871 msyslog(LOG_ERR, "unable to bind to wildcard socket address %s - another process may be running - EXITING",
872 stoa((&interface->sin)));
877 #ifdef INCLUDE_IPV6_SUPPORT
879 * create pseudo-interface with wildcard IPv6 address
881 if (isc_net_probeipv6() == ISC_R_SUCCESS) {
882 struct interface *interface = new_interface(NULL);
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;
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;
897 interface->fd = open_socket(&interface->sin,
898 interface->flags, 1, interface);
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);
907 msyslog(LOG_ERR, "unable to bind to wildcard socket address %s - another process may be running - EXITING",
908 stoa((&interface->sin)));
917 address_okay(struct interface *iface) {
919 DPRINTF(4, ("address_okay: listen Virtual: %d, IF name: %s\n",
920 listen_to_virtual_ips, iface->name));
923 * Always allow the loopback
925 if((iface->flags & INT_LOOPBACK) != 0) {
926 DPRINTF(4, ("address_okay: loopback - OK\n"));
931 * Check if the interface is specified
933 if (specific_interface != NULL) {
934 if (strcasecmp(iface->name, specific_interface) == 0) {
935 DPRINTF(4, ("address_okay: specific interface name matched - OK\n"));
938 DPRINTF(4, ("address_okay: specific interface name NOT matched - FAIL\n"));
943 if (listen_to_virtual_ips == 0 &&
944 (strchr(iface->name, (int)':') != NULL)) {
945 DPRINTF(4, ("address_okay: virtual ip/alias - FAIL\n"));
950 DPRINTF(4, ("address_okay: OK\n"));
955 convert_isc_if(isc_interface_t *isc_if, struct interface *itf, u_short port)
958 itf->family = (short) isc_if->af;
959 strcpy(itf->name, isc_if->name);
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;
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;
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;
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;
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);
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;
1003 #endif /* INCLUDE_IPV6_SUPPORT */
1006 /* Process the rest of the flags */
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;
1022 * some OSes have been observed to keep
1023 * cached routes even when more specific routes
1025 * this can be mitigated by re-binding
1029 refresh_interface(struct interface * interface)
1031 #ifdef OS_MISSES_SPECIFIC_ROUTE_UPDATES
1032 if (interface->fd != INVALID_SOCKET)
1034 close_and_delete_fd_from_list(interface->fd);
1035 interface->fd = open_socket(&interface->sin,
1036 interface->flags, 0, interface);
1038 * reset TTL indication so TTL is is set again
1041 interface->last_ttl = 0;
1042 return interface->fd != INVALID_SOCKET;
1046 return 0; /* invalid sockets are not refreshable */
1048 #else /* !OS_MISSES_SPECIFIC_ROUTE_UPDATES */
1049 return interface->fd != INVALID_SOCKET;
1050 #endif /* !OS_MISSES_SPECIFIC_ROUTE_UPDATES */
1054 * interface_update - externally callable update function
1057 interface_update(interface_receiver_t receiver, void *data)
1059 if (!disable_dynamic_updates) {
1060 int new_interface_found;
1063 new_interface_found = update_interfaces(htons(NTP_PORT), receiver, data);
1066 if (new_interface_found) {
1068 msyslog(LOG_DEBUG, "new interface(s) found: waking up resolver");
1071 /* wake up the resolver thread */
1072 if (ResolverEventHandle != NULL)
1073 SetEvent(ResolverEventHandle);
1075 /* write any single byte to the pipe to wake up the resolver process */
1076 write( resolver_pipe_fd[1], &new_interface_found, 1 );
1083 * find out if a given interface structure contains
1084 * a wildcard address
1087 is_wildcard_addr(struct sockaddr_storage *sas)
1089 if (sas->ss_family == AF_INET &&
1090 ((struct sockaddr_in*)sas)->sin_addr.s_addr == htonl(INADDR_ANY))
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))
1103 #ifdef OS_NEEDS_REUSEADDR_FOR_IFADDRBIND
1105 * enable/disable re-use of wildcard address socket
1108 set_wildcard_reuse(int family, int on)
1113 SOCKET fd = INVALID_SOCKET;
1115 onoff = on ? &onvalue : &offvalue;
1119 if (any_interface) {
1120 fd = any_interface->fd;
1124 #ifdef INCLUDE_IPV6_SUPPORT
1126 if (any6_interface) {
1127 fd = any6_interface->fd;
1130 #endif /* !INCLUDE_IPV6_SUPPORT */
1133 if (fd != INVALID_SOCKET) {
1134 if (setsockopt(fd, SOL_SOCKET,
1135 SO_REUSEADDR, (char *)onoff,
1137 netsyslog(LOG_ERR, "set_wildcard_reuse: setsockopt(SO_REUSEADDR, %s) failed: %m", *onoff ? "on" : "off");
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)));
1144 #endif /* OS_NEEDS_REUSEADDR_FOR_IFADDRBIND */
1146 #ifdef INCLUDE_IPV6_SUPPORT
1147 static isc_boolean_t
1148 is_anycast(struct sockaddr *sa, char *name)
1150 #if defined(SIOCGIFAFLAG_IN6) && defined(IN6_IFF_ANYCAST)
1151 struct in6_ifreq ifr6;
1155 if (sa->sa_family != AF_INET6)
1157 if ((fd = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
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) {
1168 flags6 = ifr6.ifr_ifru.ifru_flags6;
1169 if ((flags6 & IN6_IFF_ANYCAST) != 0)
1171 #endif /* !SIOCGIFAFLAG_IN6 || !IN6_IFF_ANYCAST */
1174 #endif /* !INCLUDE_IPV6_SUPPORT */
1177 * update_interface strategy
1179 * toggle configuration phase
1182 * forall currently existing interfaces
1183 * if address is known:
1184 * drop socket - rebind again
1186 * if address is NOT known:
1187 * attempt to create a new interface entry
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
1197 * attempt to re-assign interfaces to peers
1204 interface_receiver_t receiver,
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;
1216 DPRINTF(3, ("update_interfaces(%d)\n", ntohs( (u_short) port)));
1218 #ifdef INCLUDE_IPV6_SUPPORT
1219 if (isc_net_probeipv6() == ISC_R_SUCCESS)
1220 scan_ipv6 = ISC_TRUE;
1224 netsyslog(LOG_ERR, "no IPv6 interfaces found");
1227 if (isc_net_probeipv6() == ISC_R_SUCCESS)
1228 scan_ipv6 = ISC_TRUE;
1229 #if defined(ISC_PLATFORM_HAVEIPV6) && defined(DEBUG)
1232 netsyslog(LOG_ERR, "no IPv6 interfaces found");
1235 if (isc_net_probeipv4() == ISC_R_SUCCESS)
1236 scan_ipv4 = ISC_TRUE;
1240 netsyslog(LOG_ERR, "no IPv4 interfaces found");
1243 * phase one - scan interfaces
1244 * - create those that are not found
1245 * - update those that are found
1248 result = isc_interfaceiter_create(mctx, &iter);
1250 if (result != ISC_R_SUCCESS)
1253 sys_interphase ^= 0x1; /* toggle system phase for finding untouched (to be deleted) interfaces */
1255 for (result = isc_interfaceiter_first(iter);
1256 result == ISC_R_SUCCESS;
1257 result = isc_interfaceiter_next(iter))
1259 isc_interface_t isc_if;
1260 unsigned int family;
1261 struct interface interface;
1262 struct interface *iface;
1264 result = isc_interfaceiter_current(iter, &isc_if);
1266 if (result != ISC_R_SUCCESS)
1269 /* See if we have a valid family to use */
1270 family = isc_if.address.family;
1271 if (family != AF_INET && family != AF_INET6)
1273 if (scan_ipv4 == ISC_FALSE && family == AF_INET)
1275 if (scan_ipv6 == ISC_FALSE && family == AF_INET6)
1281 init_interface(&interface);
1283 convert_isc_if(&isc_if, &interface, port);
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
1293 if (address_okay(&interface) == ISC_TRUE) {
1294 interface.ignore_packets = ISC_FALSE;
1297 interface.ignore_packets = ISC_TRUE;
1300 DPRINT_INTERFACE(4, (&interface, "examining ", "\n"));
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)));
1308 * skip any interfaces UP and bound to a wildcard
1309 * address - some dhcp clients produce that in the
1312 if (is_wildcard_addr(&interface.sin))
1315 #ifdef INCLUDE_IPV6_SUPPORT
1316 if (is_anycast((struct sockaddr *)&interface.sin, isc_if.name))
1318 #endif /* !INCLUDE_IPV6_SUPPORT */
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))
1326 iface = getinterface(&interface.sin, INT_WILDCARD);
1328 if (iface && refresh_interface(iface))
1331 * found existing and up to date interface - mark present
1334 iface->phase = sys_interphase;
1335 DPRINT_INTERFACE(4, (iface, "updating ", " present\n"));
1336 ifi.action = IFS_EXISTS;
1337 ifi.interface = iface;
1339 receiver(data, &ifi);
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
1350 iface = create_interface(port, &interface);
1354 ifi.action = IFS_CREATED;
1355 ifi.interface = iface;
1357 receiver(data, &ifi);
1359 new_interface_found = 1;
1361 DPRINT_INTERFACE(3, (iface, "updating ", " new - created\n"));
1365 DPRINT_INTERFACE(3, (&interface, "updating ", " new - creation FAILED"));
1367 msyslog(LOG_INFO, "failed to initialize interface for address %s", stoa(&interface.sin));
1373 isc_interfaceiter_destroy(&iter);
1376 * phase 2 - delete gone interfaces - reassigning peers to other interfaces
1379 struct interface *interf = ISC_LIST_HEAD(inter_list);
1381 while (interf != NULL)
1383 struct interface *next = ISC_LIST_NEXT(interf, link);
1385 if (!(interf->flags & (INT_WILDCARD|INT_MCASTIF))) {
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
1391 if (interf->phase != sys_interphase) {
1393 DPRINT_INTERFACE(3, (interf, "updating ", "GONE - deleting\n"));
1394 remove_interface(interf);
1396 ifi.action = IFS_DELETED;
1397 ifi.interface = interf;
1399 receiver(data, &ifi);
1401 peer = ISC_LIST_HEAD(interf->peers);
1403 * disconnect peer from deleted interface
1405 while (peer != NULL) {
1406 struct peer *npeer = ISC_LIST_NEXT(peer, ilink);
1409 * this one just lost it's interface
1411 set_peerdstadr(peer, NULL);
1417 * update globals in case we lose
1418 * a loopback interface
1420 if (interf == loopback_interface)
1421 loopback_interface = NULL;
1423 delete_interface(interf);
1431 * phase 3 - re-configure as the world has changed if necessary
1433 refresh_all_peerinterfaces();
1434 return new_interface_found;
1439 * create_sockets - create a socket for each interface plus a default
1440 * socket for when we don't know where to send
1447 #ifndef HAVE_IO_COMPLETION_PORT
1449 * I/O Completion Ports don't care about the select and FD_SET
1452 FD_ZERO(&activefds);
1455 DPRINTF(2, ("create_sockets(%d)\n", ntohs( (u_short) port)));
1457 create_wildcards(port);
1459 update_interfaces(port, NULL, NULL);
1462 * Now that we have opened all the sockets, turn off the reuse
1463 * flag for security.
1467 DPRINTF(2, ("create_sockets: Total interfaces = %d\n", ninterfaces));
1473 * create_interface - create a new interface for a given prototype
1474 * binding the socket.
1476 static struct interface *
1479 struct interface *iface
1482 struct sockaddr_storage resmask;
1483 struct interface *interface;
1485 DPRINTF(2, ("create_interface(%s#%d)\n", stoa(&iface->sin), ntohs( (u_short) port)));
1487 /* build an interface */
1488 interface = new_interface(iface);
1493 interface->fd = open_socket(&interface->sin,
1494 interface->flags, 0, interface);
1496 if (interface->fd != INVALID_SOCKET)
1497 list_if_listening(interface, port);
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));
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",
1510 stoa((&interface->sin)),
1511 ntohs( (u_short) port));
1512 delete_interface(interface);
1517 * Blacklist bound interface address
1520 SET_HOSTMASK(&resmask, interface->sin.ss_family);
1521 hack_restrict(RESTRICT_FLAGS, &interface->sin, &resmask,
1522 RESM_NTPONLY|RESM_INTERFACE, RES_IGNORE);
1525 * set globals with the first found
1526 * loopback interface of the appropriate class
1528 if ((loopback_interface == NULL) &&
1529 (interface->family == AF_INET) &&
1530 ((interface->flags & INT_LOOPBACK) != 0))
1532 loopback_interface = interface;
1536 * put into our interface list
1538 add_addr_to_list(&interface->sin, interface);
1539 add_interface(interface);
1541 DPRINT_INTERFACE(2, (interface, "created ", "\n"));
1546 * set_reuseaddr() - set/clear REUSEADDR on all sockets
1547 * NB possible hole - should we be doing this on broadcast
1551 set_reuseaddr(int flag) {
1552 struct interface *interf;
1554 for (interf = ISC_LIST_HEAD(inter_list);
1556 interf = ISC_LIST_NEXT(interf, link)) {
1557 if (interf->flags & INT_WILDCARD)
1561 * if interf->fd is INVALID_SOCKET, we might have a adapter
1562 * configured but not present
1564 DPRINTF(4, ("setting SO_REUSEADDR on %.16s@%s to %s\n", interf->name, stoa(&interf->sin), flag ? "on" : "off"));
1566 if (interf->fd != INVALID_SOCKET) {
1567 if (setsockopt(interf->fd, SOL_SOCKET,
1568 SO_REUSEADDR, (char *)&flag,
1570 netsyslog(LOG_ERR, "set_reuseaddr: setsockopt(SO_REUSEADDR, %s) failed: %m", flag ? "on" : "off");
1577 * This is just a wrapper around an internal function so we can
1578 * make other changes as necessary later on
1581 enable_broadcast(struct interface *iface, struct sockaddr_storage *baddr)
1584 socket_broadcast_enable(iface, iface->fd, baddr);
1588 #ifdef OPEN_BCAST_SOCKET
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
1594 static isc_boolean_t
1595 socket_broadcast_enable(struct interface *iface, SOCKET fd, struct sockaddr_storage *maddr)
1600 if (maddr->ss_family == AF_INET)
1602 /* if this interface can support broadcast, set SO_BROADCAST */
1603 if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST,
1604 (char *)&on, sizeof(on)))
1606 netsyslog(LOG_ERR, "setsockopt(SO_BROADCAST) enable failure on address %s: %m",
1610 else if (debug > 1) {
1611 printf("Broadcast enabled on socket %d for address %s\n",
1616 iface->flags |= INT_BCASTOPEN;
1620 #endif /* SO_BROADCAST */
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
1628 static isc_boolean_t
1629 socket_broadcast_disable(struct interface *iface, struct sockaddr_storage *maddr)
1632 int off = 0; /* This seems to be OK as an int */
1634 if (maddr->ss_family == AF_INET)
1636 if (setsockopt(iface->fd, SOL_SOCKET, SO_BROADCAST,
1637 (char *)&off, sizeof(off)))
1639 netsyslog(LOG_ERR, "setsockopt(SO_BROADCAST) disable failure on address %s: %m",
1643 iface->flags &= ~INT_BCASTOPEN;
1647 #endif /* SO_BROADCAST */
1650 #endif /* OPEN_BCAST_SOCKET */
1652 * Check to see if the address is a multicast address
1654 static isc_boolean_t
1655 addr_ismulticast(struct sockaddr_storage *maddr)
1657 switch (maddr->ss_family)
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)));
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)));
1681 * If we don't have IPV6 support any IPV6 address is not multicast
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.
1700 enable_multicast_if(struct interface *iface, struct sockaddr_storage *maddr)
1703 /*u_char*/ TYPEOF_IP_MULTICAST_LOOP off = 0;
1705 switch (maddr->ss_family)
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) {
1712 "setsockopt IP_MULTICAST_IF failure: %m on socket %d, addr %s for multicast address %s",
1713 iface->fd, stoa(&iface->sin), stoa(maddr));
1716 #ifdef IP_MULTICAST_LOOP
1718 * Don't send back to itself, but allow it to fail to set it
1720 if (setsockopt(iface->fd, IPPROTO_IP, IP_MULTICAST_LOOP,
1721 SETSOCKOPT_ARG_CAST &off, sizeof(off)) == -1) {
1723 "setsockopt IP_MULTICAST_LOOP failure: %m on socket %d, addr %s for multicast address %s",
1724 iface->fd, stoa(&iface->sin), stoa(maddr));
1727 DPRINTF(4, ("Added IPv4 multicast interface on socket %d, addr %s for multicast address %s\n",
1728 iface->fd, stoa(&iface->sin),
1733 #ifdef INCLUDE_IPV6_MULTICAST_SUPPORT
1734 if (setsockopt(iface->fd, IPPROTO_IPV6, IPV6_MULTICAST_IF,
1735 &iface->scopeid, sizeof(iface->scopeid)) == -1) {
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,
1742 #ifdef IPV6_MULTICAST_LOOP
1744 * Don't send back to itself, but allow it to fail to set it
1746 if (setsockopt(iface->fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
1747 &off, sizeof(off)) == -1) {
1749 "setsockopt IP_MULTICAST_LOOP failure: %m on socket %d, addr %s for multicast address %s",
1750 iface->fd, stoa(&iface->sin), stoa(maddr));
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,
1759 #endif /* INCLUDE_IPV6_MULTICAST_SUPPORT */
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
1770 static isc_boolean_t
1771 socket_multicast_enable(struct interface *iface, int lscope, struct sockaddr_storage *maddr)
1773 #ifdef INCLUDE_IPV6_MULTICAST_SUPPORT
1774 struct ipv6_mreq mreq6;
1775 struct in6_addr iaddr6;
1776 #endif /* INCLUDE_IPV6_MULTICAST_SUPPORT */
1778 struct ip_mreq mreq;
1780 if (find_addr_in_list(maddr)) {
1781 DPRINTF(4, ("socket_multicast_enable(%s): already enabled\n", stoa(maddr)));
1785 switch (maddr->ss_family)
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) {
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));
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)));
1807 #ifdef INCLUDE_IPV6_MULTICAST_SUPPORT
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.
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;
1819 if (setsockopt(iface->fd, IPPROTO_IPV6, IPV6_JOIN_GROUP,
1820 (char *)&mreq6, sizeof(mreq6)) == -1) {
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));
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)));
1833 #endif /* INCLUDE_IPV6_MULTICAST_SUPPORT */
1835 iface->flags |= INT_MCASTOPEN;
1837 add_addr_to_list(maddr, iface);
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
1846 static isc_boolean_t
1847 socket_multicast_disable(struct interface *iface, struct sockaddr_storage *maddr)
1849 #ifdef INCLUDE_IPV6_MULTICAST_SUPPORT
1850 struct ipv6_mreq mreq6;
1851 struct in6_addr iaddr6;
1852 #endif /* INCLUDE_IPV6_MULTICAST_SUPPORT */
1854 struct ip_mreq mreq;
1855 memset((char *)&mreq, 0, sizeof(mreq));
1857 if (find_addr_in_list(maddr) == NULL) {
1858 DPRINTF(4, ("socket_multicast_disable(%s): not enabled\n", stoa(maddr)));
1862 switch (maddr->ss_family)
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) {
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));
1878 #ifdef INCLUDE_IPV6_MULTICAST_SUPPORT
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.
1885 iaddr6 = ((struct sockaddr_in6*)&maddr)->sin6_addr;
1886 mreq6.ipv6mr_multiaddr = iaddr6;
1887 mreq6.ipv6mr_interface = iface->scopeid;
1889 if (setsockopt(iface->fd, IPPROTO_IPV6, IPV6_LEAVE_GROUP,
1890 (char *)&mreq6, sizeof(mreq6)) == -1) {
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));
1900 #endif /* INCLUDE_IPV6_MULTICAST_SUPPORT */
1904 if (iface->num_mcast <= 0) {
1905 iface->num_mcast = 0;
1906 iface->flags &= ~INT_MCASTOPEN;
1912 * io_setbclient - open the broadcast client sockets
1917 #ifdef OPEN_BCAST_SOCKET
1918 struct interface *interf;
1920 isc_boolean_t jstatus;
1925 for (interf = ISC_LIST_HEAD(inter_list);
1927 interf = ISC_LIST_NEXT(interf, link)) {
1928 if (interf->flags & INT_WILDCARD)
1931 /* use only allowed addresses */
1932 if (interf->ignore_packets == ISC_TRUE)
1934 /* Only IPv4 addresses are valid for broadcast */
1935 if (interf->sin.ss_family != AF_INET)
1938 /* Is this a broadcast address? */
1939 if (!(interf->flags & INT_BROADCAST))
1942 /* Skip the loopback addresses */
1943 if (interf->flags & INT_LOOPBACK)
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 */
1954 * Try to open the broadcast address
1956 interf->family = AF_INET;
1957 interf->bfd = open_socket(&interf->bcast,
1958 INT_BROADCAST, 0, interf);
1961 * If we succeeded then we use it otherwise
1962 * enable the underlying address
1964 if (interf->bfd == INVALID_SOCKET) {
1971 /* Enable Broadcast on socket */
1972 jstatus = socket_broadcast_enable(interf, fd, &interf->sin);
1973 if (jstatus == ISC_TRUE)
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);
1985 printf("io_setbclient: Opened broadcast clients\n");
1988 netsyslog(LOG_ERR, "Unable to listen for broadcasts, no broadcast interfaces available");
1990 netsyslog(LOG_ERR, "io_setbclient: Broadcast Client disabled by build");
1995 * io_unsetbclient - close the broadcast client sockets
1998 io_unsetbclient(void)
2000 struct interface *interf;
2001 isc_boolean_t lstatus;
2003 for (interf = ISC_LIST_HEAD(inter_list);
2005 interf = ISC_LIST_NEXT(interf, link))
2007 if (interf->flags & INT_WILDCARD)
2010 if (!(interf->flags & INT_BCASTOPEN))
2012 lstatus = socket_broadcast_disable(interf, &interf->sin);
2017 * io_multicast_add() - add multicast group address
2021 struct sockaddr_storage addr
2025 struct interface *interface, *iface;
2029 * Check to see if this is a multicast address
2031 if (addr_ismulticast(&addr) == ISC_FALSE)
2034 /* If we already have it we can just return */
2035 if (find_flagged_addr_in_list(&addr, INT_MCASTOPEN|INT_MCASTIF) != NULL)
2037 netsyslog(LOG_INFO, "Duplicate request found for multicast address %s",
2042 #ifndef MULTICAST_NONEWSOCKET
2043 interface = new_interface(NULL);
2046 * Open a new socket for the multicast address
2048 interface->sin.ss_family = addr.ss_family;
2049 interface->family = addr.ss_family;
2051 switch(addr.ss_family) {
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));
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;
2068 memset(&((struct sockaddr_in6*)&interface->mask)->sin6_addr.s6_addr, 0xff, sizeof(struct in6_addr));
2070 iface = findlocalcastinterface(&addr, INT_MULTICAST);
2072 # ifdef ISC_PLATFORM_HAVESCOPEID
2073 lscope = ((struct sockaddr_in6*)&iface->sin)->sin6_scope_id;
2075 DPRINTF(4, ("Found interface #%d %s, scope: %d for address %s\n", iface->ifnum, iface->name, lscope, stoa(&addr)));
2081 interface->bfd = INVALID_SOCKET;
2082 interface->fd = open_socket(&interface->sin,
2083 INT_MULTICAST, 0, interface);
2085 if (interface->fd != INVALID_SOCKET)
2087 interface->bfd = INVALID_SOCKET;
2088 interface->ignore_packets = ISC_FALSE;
2089 interface->flags |= INT_MCASTIF;
2091 (void) strncpy(interface->name, "multicast",
2092 sizeof(interface->name));
2093 ((struct sockaddr_in*)&interface->mask)->sin_addr.s_addr =
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));
2102 delete_interface(interface); /* re-use existing interface */
2104 if (addr.ss_family == AF_INET)
2105 interface = wildipv4;
2106 else if (addr.ss_family == AF_INET6)
2107 interface = wildipv6;
2109 if (interface != NULL) {
2110 /* HACK ! -- stuff in an address */
2111 interface->bcast = addr;
2113 "...multicast address %s using wildcard interface #%d %s",
2114 stoa(&addr), interface->ifnum, interface->name);
2117 "No multicast socket available to use for address %s",
2124 * For the case where we can't use a separate socket
2126 interface = findlocalcastinterface(&addr, INT_MULTICAST);
2128 * If we don't have a valid socket, just return
2133 "Cannot add multicast address %s: Cannot find slot",
2140 isc_boolean_t jstatus;
2141 jstatus = socket_multicast_enable(interface, lscope, &addr);
2143 if (jstatus == ISC_TRUE)
2144 netsyslog(LOG_INFO, "Added Multicast Listener %s on interface #%d %s\n", stoa(&addr), interface->ifnum, interface->name);
2146 netsyslog(LOG_ERR, "Failed to add Multicast Listener %s\n", stoa(&addr));
2150 "Cannot add multicast address %s: no Multicast support",
2157 * io_multicast_del() - delete multicast group address
2161 struct sockaddr_storage addr
2165 struct interface *interface;
2166 isc_boolean_t lstatus;
2169 * Check to see if this is a multicast address
2171 if (addr_ismulticast(&addr) == ISC_FALSE)
2174 "invalid multicast address %s", stoa(&addr));
2178 switch (addr.ss_family)
2182 * Disable reception of multicast packets
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);
2191 #ifdef INCLUDE_IPV6_MULTICAST_SUPPORT
2194 * Disable reception of multicast packets
2196 for (interface = ISC_LIST_HEAD(inter_list);
2198 interface = ISC_LIST_NEXT(interface, link))
2200 if (interface->flags & INT_WILDCARD)
2203 /* Be sure it's the correct family */
2204 if (interface->sin.ss_family != AF_INET6)
2206 if (!(interface->flags & INT_MCASTOPEN))
2208 if (!(interface->fd < 0))
2210 if (!SOCKCMP(&addr, &interface->sin))
2212 lstatus = socket_multicast_disable(interface, &addr);
2215 #endif /* INCLUDE_IPV6_MULTICAST_SUPPORT */
2219 delete_addr_from_list(&addr);
2221 #else /* not MCAST */
2222 netsyslog(LOG_ERR, "this function requires multicast kernel");
2223 #endif /* not MCAST */
2227 * init_nonblocking_io() - set up descriptor to be non blocking
2229 static void init_nonblocking_io(SOCKET fd)
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
2244 #if defined(O_NONBLOCK) /* POSIX */
2245 if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
2247 netsyslog(LOG_ERR, "fcntl(O_NONBLOCK) fails on fd #%d: %m",
2252 #elif defined(FNDELAY)
2253 if (fcntl(fd, F_SETFL, FNDELAY) < 0)
2255 netsyslog(LOG_ERR, "fcntl(FNDELAY) fails on fd #%d: %m",
2260 #elif defined(O_NDELAY) /* generally the same as FNDELAY */
2261 if (fcntl(fd, F_SETFL, O_NDELAY) < 0)
2263 netsyslog(LOG_ERR, "fcntl(O_NDELAY) fails on fd #%d: %m",
2268 #elif defined(FIONBIO)
2271 # if defined(SYS_WINNT)
2273 if (ioctlsocket(fd,FIONBIO,(u_long *) &on) == SOCKET_ERROR)
2275 if (ioctl(fd,FIONBIO,&on) < 0)
2278 netsyslog(LOG_ERR, "ioctl(FIONBIO) fails on fd #%d: %m",
2284 #elif defined(FIOSNBIO)
2285 if (ioctl(fd,FIOSNBIO,&on) < 0)
2287 netsyslog(LOG_ERR, "ioctl(FIOSNBIO) fails on fd #%d: %m",
2293 # include "Bletch: Need non-blocking I/O!"
2298 * open_socket - open a socket, returning the file descriptor
2303 struct sockaddr_storage *addr,
2306 struct interface *interf
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)
2315 #endif /* IPTOS_LOWDELAY && IPPROTO_IP && IP_TOS */
2317 if ((addr->ss_family == AF_INET6) && (isc_net_probeipv6() != ISC_R_SUCCESS))
2318 return (INVALID_SOCKET);
2320 /* create a datagram (UDP) socket */
2322 if ( (fd = socket(addr->ss_family, SOCK_DGRAM, 0)) < 0) {
2325 if ( (fd = socket(addr->ss_family, SOCK_DGRAM, 0)) == INVALID_SOCKET) {
2326 errval = WSAGetLastError();
2328 if(addr->ss_family == AF_INET)
2329 netsyslog(LOG_ERR, "socket(AF_INET, SOCK_DGRAM, 0) failed on address %s: %m",
2331 else if(addr->ss_family == AF_INET6)
2332 netsyslog(LOG_ERR, "socket(AF_INET6, SOCK_DGRAM, 0) failed on address %s: %m",
2335 if (errval == EPROTONOSUPPORT || errval == EAFNOSUPPORT ||
2336 errval == EPFNOSUPPORT)
2338 if (errval == WSAEPROTONOSUPPORT || errval == WSAEAFNOSUPPORT ||
2339 errval == WSAEPFNOSUPPORT)
2341 return (INVALID_SOCKET);
2342 msyslog(LOG_ERR, "unexpected error code %d (not PROTONOSUPPORT|AFNOSUPPORT|FPNOSUPPORT) - exiting", errval);
2347 if (connection_reset_fix(fd) != ISC_R_SUCCESS) {
2348 netsyslog(LOG_ERR, "connection_reset_fix(fd) failed on address %s: %m",
2351 #endif /* SYS_WINNT */
2354 * Fixup the file descriptor for some systems
2355 * See bug #530 for details of the issue.
2360 * set SO_REUSEADDR since we will be binding the same port
2361 * number on each interface according to flag
2363 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
2364 turn_off_reuse ? (char *)&off : (char *)&on, sizeof(on)))
2366 netsyslog(LOG_ERR, "setsockopt SO_REUSEADDR %s on fails on address %s: %m",
2367 turn_off_reuse ? "off" : "on", stoa(addr));
2371 return INVALID_SOCKET;
2375 * IPv4 specific options go here
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)
2383 netsyslog(LOG_ERR, "setsockopt IPTOS_LOWDELAY on fails on address %s: %m",
2386 #endif /* IPTOS_LOWDELAY && IPPROTO_IP && IP_TOS */
2390 * IPv6 specific options go here
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)))
2397 netsyslog(LOG_ERR, "setsockopt IPV6_V6ONLY on fails on address %s: %m",
2400 #endif /* IPV6_V6ONLY */
2401 #if defined(IPV6_BINDV6ONLY)
2402 if (setsockopt(fd, IPPROTO_IPV6, IPV6_BINDV6ONLY,
2403 (char*)&on, sizeof(on)))
2406 "setsockopt IPV6_BINDV6ONLY on fails on address %s: %m",
2409 #endif /* IPV6_BINDV6ONLY */
2412 #ifdef OS_NEEDS_REUSEADDR_FOR_IFADDRBIND
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
2418 if (!is_wildcard_addr(addr)) {
2419 set_wildcard_reuse(addr->ss_family, 1);
2424 * bind the local address.
2426 errval = bind(fd, (struct sockaddr *)addr, SOCKLEN(addr));
2428 #ifdef OS_NEEDS_REUSEADDR_FOR_IFADDRBIND
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
2434 if (!is_wildcard_addr(addr)) {
2435 set_wildcard_reuse(addr->ss_family, 0);
2441 * Don't log this under all conditions
2443 if (turn_off_reuse == 0
2448 if (addr->ss_family == AF_INET)
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),
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)
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
2465 IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6*)addr)->sin6_addr), flags);
2471 return INVALID_SOCKET;
2474 #ifdef HAVE_TIMESTAMP
2476 if (setsockopt(fd, SOL_SOCKET, SO_TIMESTAMP,
2477 (char*)&on, sizeof(on)))
2479 netsyslog(LOG_DEBUG,
2480 "setsockopt SO_TIMESTAMP on fails on address %s: %m",
2486 DPRINTF(4, ("setsockopt SO_TIMESTAMP enabled on fd %d address %s\n", fd, stoa(addr)));
2491 DPRINTF(4, ("bind() fd %d, family %d, port %d, addr %s, flags=0x%x\n",
2494 (int)ntohs(((struct sockaddr_in*)addr)->sin_port),
2498 init_nonblocking_io(fd);
2500 #ifdef HAVE_SIGNALED_IO
2501 init_socket_sig(fd);
2502 #endif /* not HAVE_SIGNALED_IO */
2504 add_fd_to_list(fd, FD_TYPE_SOCKET);
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 */
2511 #if defined (HAVE_IO_COMPLETION_PORT)
2513 * Add the socket to the completion port
2515 if (io_completion_port_add_socket(fd, interf))
2517 msyslog(LOG_ERR, "unable to set up io completion port - EXITING");
2524 /* XXX ELIMINATE sendpkt similar in ntpq.c, ntpdc.c, ntp_io.c, ntptrace.c */
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.
2532 struct sockaddr_storage *dest,
2533 struct interface *inter,
2542 #endif /* SYS_WINNT */
2545 * Send error caches. Empty slots have port == 0
2546 * Set ERRORCACHESIZE to 0 to disable
2550 struct in_addr addr;
2553 #ifdef INCLUDE_IPV6_SUPPORT
2556 struct in6_addr addr;
2558 #endif /* INCLUDE_IPV6_SUPPORT */
2561 #ifndef ERRORCACHESIZE
2562 #define ERRORCACHESIZE 8
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 */
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 */
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);
2587 printf("%ssendpkt(dst=%s, ttl=%d, len=%d): no interface - IGNORED\n",
2588 (ttl > 0) ? "\tMCAST\t***** " : "",
2595 if (inter == NULL) /* unbound peer - drop request and wait for better network conditions */
2601 * for the moment we use the bcast option to set multicast ttl
2603 if (ttl > 0 && ttl != inter->last_ttl) {
2606 * set the multicast ttl for outgoing packets
2610 switch (inter->sin.ss_family) {
2614 u_char mttl = (u_char) ttl;
2616 rtc = setsockopt(inter->fd, IPPROTO_IP, IP_MULTICAST_TTL,
2617 (const void *) &mttl, sizeof(mttl));
2621 #ifdef INCLUDE_IPV6_SUPPORT
2624 u_int ittl = (u_char) ttl;
2626 rtc = setsockopt(inter->fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
2627 (const void *) &ittl, sizeof(ittl));
2631 #endif /* INCLUDE_IPV6_SUPPORT */
2632 default: /* just NOP if not supported */
2638 netsyslog(LOG_ERR, "setsockopt IP_MULTICAST_TTL/IPV6_MULTICAST_HOPS fails on address %s: %m",
2642 inter->last_ttl = ttl;
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)
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)
2659 #endif /* INCLUDE_IPV6_SUPPORT */
2661 #if defined(HAVE_IO_COMPLETION_PORT)
2662 err = io_completion_port_sendto(inter, pkt, len, dest);
2663 if (err != ERROR_SUCCESS)
2666 cc = srvr_rply(&ntp_node, dest, inter, pkt);
2668 cc = sendto(inter->fd, (char *)pkt, (unsigned int)len, 0, (struct sockaddr *)dest,
2676 #if defined(HAVE_IO_COMPLETION_PORT)
2677 err = WSAGetLastError();
2678 if (err != WSAEWOULDBLOCK && err != WSAENOBUFS && slot < 0)
2680 if (errno != EWOULDBLOCK && errno != ENOBUFS && slot < 0)
2684 * Remember this, if there's an empty slot
2686 switch (dest->ss_family) {
2690 for (slot = ERRORCACHESIZE; --slot >= 0; )
2691 if (badaddrs[slot].port == 0)
2693 badaddrs[slot].port = SRCPORT(dest);
2694 badaddrs[slot].addr = ((struct sockaddr_in*)dest)->sin_addr;
2699 #ifdef INCLUDE_IPV6_SUPPORT
2702 for (slot = ERRORCACHESIZE; --slot >= 0; )
2703 if (badaddrs6[slot].port == 0)
2705 badaddrs6[slot].port = SRCPORT(dest);
2706 badaddrs6[slot].addr = ((struct sockaddr_in6*)dest)->sin6_addr;
2710 #endif /* INCLUDE_IPV6_SUPPORT */
2711 default: /* don't care if not supported */
2715 netsyslog(LOG_ERR, "sendto(%s) (fd=%d): %m",
2716 stoa(dest), inter->fd);
2724 * He's not bad any more
2728 netsyslog(LOG_INFO, "Connection re-established to %s", stoa(dest));
2729 switch (dest->ss_family) {
2731 badaddrs[slot].port = 0;
2733 #ifdef INCLUDE_IPV6_SUPPORT
2735 badaddrs6[slot].port = 0;
2737 #endif /* INCLUDE_IPV6_SUPPORT */
2738 default: /* don't care if not supported */
2745 #if !defined(HAVE_IO_COMPLETION_PORT)
2747 * fdbits - generate ascii representation of fd_set (FAU debug support)
2748 * HFDF format - highest fd first.
2756 static char buffer[256];
2757 char * buf = buffer;
2759 count = (count < 256) ? count : 255;
2763 *buf++ = FD_ISSET(count, set) ? '#' : '-';
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
2777 read_refclock_packet(SOCKET fd, struct refclockio *rp, l_fp ts)
2781 register struct recvbuf *rb;
2783 rb = get_free_recv_buffer();
2788 * No buffer space available - just drop the packet
2790 char buf[RX_BUFF_SIZE];
2792 buflen = read(fd, buf, sizeof buf);
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);
2804 if (errno != EINTR && errno != EAGAIN) {
2805 netsyslog(LOG_ERR, "clock read fd %d: %m", fd);
2812 * Got one. Mark how and when it got here,
2813 * put it on the full list and do bookkeeping.
2815 rb->recv_length = buflen;
2816 rb->recv_srcclock = rp->srcclock;
2820 rb->receiver = rp->clock_recv;
2825 * have direct input routine for refclocks
2827 if (rp->io_input(rb) == 0)
2830 * data was consumed - nothing to pass up
2831 * into block input machine
2838 add_full_recv_buffer(rb);
2845 #ifdef HAVE_TIMESTAMP
2847 * extract timestamps from control message buffer
2850 fetch_timestamp(struct recvbuf *rb, struct msghdr *msghdr, l_fp ts)
2852 #ifdef USE_TIMESTAMP_CMSG
2853 struct cmsghdr *cmsghdr;
2855 cmsghdr = CMSG_FIRSTHDR(msghdr);
2856 while (cmsghdr != NULL) {
2857 switch (cmsghdr->cmsg_type)
2861 struct timeval *tvp = (struct timeval *)CMSG_DATA(cmsghdr);
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;
2868 /* fuzz lower bits not covered by precision */
2869 if (sys_precision != 0)
2870 dtemp += (ntp_random() / FRAC - .5) / (1 <<
2873 nts.l_uf = (u_int32)(dtemp*FRAC);
2878 collect_timing(rb, "input processing delay", 1, &dts);
2879 DPRINTF(4, ("fetch_timestamp: timestamp delta: %s (incl. prec fuzz)\n", lfptoa(&dts, 9)));
2882 ts = nts; /* network time stamp */
2886 DPRINTF(4, ("fetch_timestamp: skipping control message 0x%x\n", cmsghdr->cmsg_type));
2889 cmsghdr = CMSG_NXTHDR(msghdr, cmsghdr);
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
2902 read_network_packet(SOCKET fd, struct interface *itf, l_fp ts)
2904 GETSOCKNAME_SOCKLEN_TYPE fromlen;
2906 register struct recvbuf *rb;
2907 #ifdef HAVE_TIMESTAMP
2908 struct msghdr msghdr;
2910 char control[TIMESTAMP_CTLMSGBUF_SIZE]; /* pick up control messages */
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
2920 rb = get_free_recv_buffer();
2922 if (rb == NULL || itf->ignore_packets == ISC_TRUE)
2924 char buf[RX_BUFF_SIZE];
2925 struct sockaddr_storage from;
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,
2936 if (itf->ignore_packets == ISC_TRUE)
2943 fromlen = sizeof(struct sockaddr_storage);
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,
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);
2964 buflen = rb->recv_length;
2966 if (buflen == 0 || (buflen == -1 &&
2975 else if (buflen < 0)
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));
2986 if(rb->recv_srcadr.ss_family == AF_INET)
2987 printf("read_network_packet: fd=%d length %d from %08lx %s\n",
2989 (u_long)ntohl(((struct sockaddr_in*)&rb->recv_srcadr)->sin_addr.s_addr) &
2991 stoa(&rb->recv_srcadr));
2993 printf("read_network_packet: fd=%d length %d from %s\n",
2995 stoa(&rb->recv_srcadr));
3000 * Got one. Mark how and when it got here,
3001 * put it on the full list and do bookkeeping.
3005 #ifdef HAVE_TIMESTAMP
3006 ts = fetch_timestamp(rb, &msghdr, ts); /* pick up a network time stamp if possible */
3009 rb->receiver = receive;
3011 add_full_recv_buffer(rb);
3019 * input_handler - receive packets asynchronously
3031 struct timeval tvzero;
3032 l_fp ts; /* Timestamp at BOselect() gob */
3034 l_fp ts_e; /* Timestamp at EOselect() gob */
3037 int select_count = 0;
3038 struct interface *interface;
3039 #if defined(HAS_ROUTING_SOCKET)
3040 struct asyncio_reader *asyncio_reader;
3046 * If we have something to do, freeze a timestamp.
3047 * See below for the other cases (nothing (left) to do or error)
3052 * Do a poll to see who has data
3056 tvzero.tv_sec = tvzero.tv_usec = 0;
3058 n = select(maxactivefd+1, &fds, (fd_set *)0, (fd_set *)0, &tvzero);
3061 * If there are no packets waiting just return
3067 * extended FAU debugging output
3071 "select(%d, %s, 0L, 0L, &0.0) error: %m",
3073 fdbits(maxactivefd, &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);
3090 * Check out the reference clocks first, if any
3095 register struct refclockio *rp;
3097 for (rp = refio; rp != NULL; rp = rp->next)
3101 if (FD_ISSET(fd, &fds))
3105 buflen = read_refclock_packet(fd, rp, ts);
3106 } while (buflen > 0);
3108 } /* End if (FD_ISSET(fd, &fds)) */
3109 } /* End for (rp = refio; rp != 0 && n > 0; rp = rp->next) */
3110 } /* End if (refio != 0) */
3112 #endif /* REFCLOCK */
3115 * Loop through the interfaces looking for data to read.
3117 for (interface = ISC_LIST_TAIL(inter_list);
3119 interface = ISC_LIST_PREV(interface, link))
3121 for (doing = 0; (doing < 2); doing++)
3129 if (!(interface->flags & INT_BCASTOPEN))
3131 fd = interface->bfd;
3133 if (fd < 0) continue;
3134 if (FD_ISSET(fd, &fds))
3138 buflen = read_network_packet(fd, interface, ts);
3139 } while (buflen > 0);
3141 /* Check more interfaces */
3145 #ifdef HAS_ROUTING_SOCKET
3147 * scan list of asyncio readers - currently only used for routing sockets
3149 asyncio_reader = ISC_LIST_TAIL(asyncio_reader_list);
3151 while (asyncio_reader != NULL)
3153 struct asyncio_reader *next = ISC_LIST_PREV(asyncio_reader, link);
3154 if (FD_ISSET(asyncio_reader->fd, &fds)) {
3156 asyncio_reader->receiver(asyncio_reader);
3158 asyncio_reader = next;
3160 #endif /* HAS_ROUTING_SOCKET */
3163 * Done everything from that select.
3167 * If nothing to do, just return.
3168 * If an error occurred, complain and return.
3170 if (select_count == 0) /* We really had nothing to do */
3174 netsyslog(LOG_DEBUG, "input_handler: select() returned 0");
3178 /* We've done our work */
3182 * (ts_e - ts) is the amount of time we spent
3183 * processing this gob of file descriptors. Log
3187 collect_timing(NULL, "input handler", 1, &ts_e);
3189 netsyslog(LOG_INFO, "input_handler: Processed a gob of fd's in %s msec", lfptoms(&ts_e, 6));
3198 * findinterface - find local interface corresponding to address
3202 struct sockaddr_storage *addr
3205 struct interface *interface;
3207 interface = findlocalinterface(addr, INT_WILDCARD);
3209 if (interface == NULL)
3211 DPRINTF(4, ("Found no interface for address %s - returning wildcard\n",
3214 return (ANY_INTERFACE_CHOOSE(addr));
3218 DPRINTF(4, ("Found interface #%d %s for address %s\n",
3219 interface->ifnum, interface->name, stoa(addr)));
3226 * findlocalinterface - find local interface index corresponding to address
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.
3238 static struct interface *
3240 struct sockaddr_storage *addr,
3246 struct sockaddr_storage saddr;
3247 GETSOCKNAME_SOCKLEN_TYPE saddrlen = SOCKLEN(addr);
3248 struct interface *iface;
3250 DPRINTF(4, ("Finding interface for addr %s in list of addresses\n",
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);
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;
3269 s = socket(addr->ss_family, SOCK_DGRAM, 0);
3270 if (s == INVALID_SOCKET)
3273 rtn = connect(s, (struct sockaddr *)&saddr, SOCKLEN(&saddr));
3277 if (rtn == SOCKET_ERROR)
3284 rtn = getsockname(s, (struct sockaddr *)&saddr, &saddrlen);
3289 if (rtn == SOCKET_ERROR)
3293 DPRINTF(4, ("findlocalinterface: kernel maps %s to %s\n", stoa(addr), stoa(&saddr)));
3295 iface = getinterface(&saddr, flags);
3297 /* Don't both with ignore interfaces */
3298 if (iface != NULL && iface->ignore_packets == ISC_TRUE)
3309 * fetch an interface structure the matches the
3310 * address is has the given flags not set
3312 static struct interface *
3313 getinterface(struct sockaddr_storage *addr, int flags)
3315 struct interface *interface = find_addr_in_list(addr);
3317 if (interface != NULL && interface->flags & flags)
3328 * findlocalcastinterface - find local *cast interface index corresponding to address
3329 * depending on the flags passed
3331 static struct interface *
3332 findlocalcastinterface(
3333 struct sockaddr_storage *addr, int flags
3336 struct interface *interface;
3337 struct interface *nif = NULL;
3338 #ifdef INCLUDE_IPV6_MULTICAST_SUPPORT
3339 isc_boolean_t want_linklocal;
3343 * see how kernel maps the mcast address
3345 nif = findlocalinterface(addr, 0);
3348 DPRINTF(2, ("findlocalcastinterface: kernel recommends interface #%d %s\n", nif->ifnum, nif->name));
3352 #ifdef INCLUDE_IPV6_MULTICAST_SUPPORT
3353 want_linklocal = ISC_FALSE;
3354 if (addr_ismulticast(addr) && flags == INT_MULTICAST)
3356 if (IN6_IS_ADDR_MC_LINKLOCAL(&((struct sockaddr_in6*)addr)->sin6_addr))
3358 want_linklocal = ISC_TRUE;
3360 else if (IN6_IS_ADDR_MC_SITELOCAL(&((struct sockaddr_in6*)addr)->sin6_addr))
3362 want_linklocal = ISC_TRUE;
3367 for (interface = ISC_LIST_HEAD(inter_list);
3369 interface = ISC_LIST_NEXT(interface, link))
3371 /* use only allowed addresses */
3372 if (interface->ignore_packets == ISC_TRUE)
3375 /* Skip the loopback and wildcard addresses */
3376 if (interface->flags & (INT_LOOPBACK|INT_WILDCARD))
3379 /* Skip if different family */
3380 if(interface->sin.ss_family != addr->ss_family)
3383 /* Is this it one of these based on flags? */
3384 if (!(interface->flags & flags))
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)
3396 /* If we want a linklocal address and this isn't it, skip */\
3397 if (want_linklocal == ISC_TRUE)
3400 /* Otherwise just look for the flag */
3401 if((interface->flags & flags))
3411 printf("findlocalcastinterface: found interface #%d %s\n", nif->ifnum, nif->name);
3413 printf("findlocalcastinterface: no interface found for %s flags 0x%x\n", stoa(addr), flags);
3420 * findbcastinter - find broadcast interface corresponding to address
3424 struct sockaddr_storage *addr
3427 #if !defined(MPE) && (defined(SIOCGIFCONF) || defined(SYS_WINNT))
3428 struct interface *interface;
3431 DPRINTF(4, ("Finding broadcast/multicast interface for addr %s in list of addresses\n",
3434 interface = findlocalinterface(addr, INT_LOOPBACK|INT_WILDCARD);
3436 if (interface != NULL)
3438 DPRINTF(4, ("Found bcast-/mcast- interface index #%d %s\n", interface->ifnum, interface->name));
3442 /* plan B - try to find something reasonable in our lists in case kernel lookup doesn't help */
3444 for (interface = ISC_LIST_HEAD(inter_list);
3446 interface = ISC_LIST_NEXT(interface, link))
3448 if (interface->flags & INT_WILDCARD)
3451 /* Don't bother with ignored interfaces */
3452 if (interface->ignore_packets == ISC_TRUE)
3456 * First look if this is the correct family
3458 if(interface->sin.ss_family != addr->ss_family)
3461 /* Skip the loopback addresses */
3462 if (interface->flags & INT_LOOPBACK)
3466 * If we are looking to match a multicast address grab it.
3468 if (addr_ismulticast(addr) == ISC_TRUE && interface->flags & INT_MULTICAST)
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)) {
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.
3488 if(addr->ss_family == AF_INET) {
3489 if (SOCKCMP(&interface->bcast, addr)) {
3492 if ((NSRCADR(&interface->sin) &
3493 NSRCADR(&interface->mask)) == (NSRCADR(addr) &
3494 NSRCADR(&interface->mask)))
3497 #ifdef INCLUDE_IPV6_SUPPORT
3498 else if(addr->ss_family == AF_INET6) {
3499 if (SOCKCMP(&interface->bcast, addr)) {
3502 if (SOCKCMP(netof(&interface->sin), netof(addr))) {
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);
3513 DPRINTF(4, ("Found bcast-/mcast- interface index #%d %s\n", interface->ifnum, interface->name));
3520 * io_clr_stats - clear I/O module statistics
3525 packets_dropped = 0;
3526 packets_ignored = 0;
3527 packets_received = 0;
3529 packets_notsent = 0;
3533 io_timereset = current_time;
3539 * io_addclock - add a reference clock to the list and arrange that we
3540 * get SIGIO interrupts from it.
3544 struct refclockio *rio
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.
3554 # ifdef HAVE_SIGNALED_IO
3555 if (init_clock_sig(rio))
3560 # elif defined(HAVE_IO_COMPLETION_PORT)
3561 if (io_completion_port_add_clock_io(rio))
3576 add_fd_to_list(rio->fd, FD_TYPE_FILE);
3583 * io_closeclock - close the clock in the I/O structure given
3587 struct refclockio *rio
3592 * Remove structure from the list
3600 register struct refclockio *rp;
3602 for (rp = refio; rp != NULL; rp = rp->next)
3603 if (rp->next == rio)
3605 rp->next = rio->next;
3616 * Close the descriptor.
3618 close_and_delete_fd_from_list(rio->fd);
3621 #endif /* REFCLOCK */
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.
3629 kill_asyncio(int startfd)
3636 lsock = ISC_LIST_HEAD(fd_list);
3637 while (lsock != NULL) {
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
3643 next = ISC_LIST_NEXT(lsock, link);
3645 * will remove socket from list
3647 close_and_delete_fd_from_list(lsock->fd);
3655 * Add and delete functions for the list of open sockets
3658 add_fd_to_list(SOCKET fd, enum desc_type type) {
3659 vsock_t *lsock = (vsock_t *)emalloc(sizeof(vsock_t));
3663 ISC_LIST_APPEND(fd_list, lsock, link);
3665 * I/O Completion Ports don't care about the select and FD_SET
3667 #ifndef HAVE_IO_COMPLETION_PORT
3669 * keep activefds in sync
3671 if (fd > maxactivefd)
3673 FD_SET( (u_int)fd, &activefds);
3678 close_and_delete_fd_from_list(SOCKET fd) {
3681 vsock_t *lsock = ISC_LIST_HEAD(fd_list);
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);
3688 switch (lsock->type) {
3689 case FD_TYPE_SOCKET:
3691 closesocket(lsock->fd);
3695 (void) close(lsock->fd);
3698 msyslog(LOG_ERR, "internal error - illegal descriptor type %d - EXITING", (int)lsock->type);
3704 * I/O Completion Ports don't care about select and fd_set
3706 #ifndef HAVE_IO_COMPLETION_PORT
3708 * remove from activefds
3710 FD_CLR( (u_int) fd, &activefds);
3712 if (fd == maxactivefd) {
3714 for (i = 0; i < maxactivefd; i++)
3715 if (FD_ISSET(i, &activefds))
3717 maxactivefd = newmax;
3727 add_addr_to_list(struct sockaddr_storage *addr, struct interface *interface){
3729 if (find_addr_in_list(addr) == NULL) {
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;
3736 ISC_LIST_APPEND(remoteaddr_list, laddr, link);
3738 DPRINTF(4, ("Added addr %s to list of addresses\n",
3742 DPRINTF(4, ("WARNING: Attempt to add duplicate addr %s to address list\n",
3749 delete_addr_from_list(struct sockaddr_storage *addr) {
3752 remaddr_t *laddr = ISC_LIST_HEAD(remoteaddr_list);
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",
3768 delete_interface_from_list(struct interface *iface) {
3770 remaddr_t *laddr = ISC_LIST_HEAD(remoteaddr_list);
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));
3784 static struct interface *
3785 find_addr_in_list(struct sockaddr_storage *addr) {
3788 remaddr_t *laddr = ISC_LIST_HEAD(remoteaddr_list);
3789 DPRINTF(4, ("Searching for addr %s in list of addresses - ",
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;
3801 DPRINTF(4, ("NOT FOUND\n"));
3802 return NULL; /* Not found */
3806 * Find the given address with the associated flag in the list
3808 static struct interface *
3809 find_flagged_addr_in_list(struct sockaddr_storage *addr, int flag) {
3812 remaddr_t *laddr = ISC_LIST_HEAD(remoteaddr_list);
3813 DPRINTF(4, ("Finding addr %s in list of addresses\n",
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;
3825 return NULL; /* Not found */
3828 #ifdef HAS_ROUTING_SOCKET
3829 #include <net/route.h>
3831 #ifndef UPDATE_GRACE
3832 #define UPDATE_GRACE 2 /* wait UPDATE_GRACE seconds before scanning */
3836 process_routing_msgs(struct asyncio_reader *reader)
3843 if (disable_dynamic_updates) {
3845 * discard ourselves if we are not need any more
3846 * usually happens when running unprivileged
3848 remove_asyncio_reader(reader);
3849 delete_asyncio_reader(reader);
3853 cnt = read(reader->fd, buffer, sizeof(buffer));
3856 msyslog(LOG_ERR, "i/o error on routing socket %m - disabling");
3857 remove_asyncio_reader(reader);
3858 delete_asyncio_reader(reader);
3863 * process routing message
3865 while ((p + sizeof(struct rt_msghdr)) <= (buffer + cnt))
3867 struct rt_msghdr *rtm;
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);
3877 switch (rtm->rtm_type) {
3902 #ifdef RTM_IFANNOUNCE
3903 case RTM_IFANNOUNCE:
3906 * we are keen on new and deleted addresses and if an interface goes up and down or routing changes
3908 DPRINTF(3, ("routing message op = %d: scheduling interface update\n", rtm->rtm_type));
3909 timer_interfacetimeout(current_time + UPDATE_GRACE);
3913 * the rest doesn't bother us.
3915 DPRINTF(4, ("routing message op = %d: ignored\n", rtm->rtm_type));
3918 p += rtm->rtm_msglen;
3923 * set up routing notifications
3926 init_async_notifications()
3928 struct asyncio_reader *reader;
3929 int fd = socket(PF_ROUTE, SOCK_RAW, 0);
3933 init_nonblocking_io(fd);
3934 #if defined(HAVE_SIGNALED_IO)
3935 init_socket_sig(fd);
3936 #endif /* HAVE_SIGNALED_IO */
3938 reader = new_asyncio_reader();
3941 reader->receiver = process_routing_msgs;
3943 add_asyncio_reader(reader, FD_TYPE_SOCKET);
3944 msyslog(LOG_INFO, "Listening on routing socket on fd #%d for interface updates", fd);
3946 msyslog(LOG_ERR, "unable to open routing socket (%m) - using polled interface update");
3951 init_async_notifications()