ntp_io.c (199995) | ntp_io.c (200576) |
---|---|
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 --- 86 unchanged lines hidden (view full) --- 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 | 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 --- 86 unchanged lines hidden (view full) --- 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 | 103/* 104 * Windows C runtime ioctl() can't deal properly with sockets, 105 * map to ioctlsocket for this source file. 106 */ 107#define ioctl(fd, opt, val) ioctlsocket((fd), (opt), (u_long *)(val)) 108#endif /* SYS_WINNT */ |
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 --- 83 unchanged lines hidden (view full) --- 195static isc_boolean_t socket_multicast_enable P((struct interface *, int, struct sockaddr_storage *)); 196static isc_boolean_t socket_multicast_disable P((struct interface *, struct sockaddr_storage *)); 197#endif 198 199#ifdef DEBUG 200static 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 | 109 110/* 111 * We do asynchronous input using the SIGIO facility. A number of 112 * recvbuf buffers are preallocated for input. In the signal 113 * handler we poll to see which sockets are ready and read the 114 * packets from them into the recvbuf's along with a time stamp and 115 * an indication of the source host and the interface it was received 116 * through. This allows us to get as accurate receive time stamps --- 83 unchanged lines hidden (view full) --- 200static isc_boolean_t socket_multicast_enable P((struct interface *, int, struct sockaddr_storage *)); 201static isc_boolean_t socket_multicast_disable P((struct interface *, struct sockaddr_storage *)); 202#endif 203 204#ifdef DEBUG 205static void print_interface P((struct interface *, char *, char *)); 206#define DPRINT_INTERFACE(_LVL_, _ARGS_) do { if (debug >= (_LVL_)) { print_interface _ARGS_; } } while (0) 207#else |
203#define DPRINT_INTERFACE(_LVL_, _ARGS_) | 208#define DPRINT_INTERFACE(_LVL_, _ARGS_) do {} while (0) |
204#endif 205 206typedef struct vsock vsock_t; 207enum desc_type { FD_TYPE_SOCKET, FD_TYPE_FILE }; 208 209struct vsock { 210 SOCKET fd; 211 enum desc_type type; --- 78 unchanged lines hidden (view full) --- 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 */ | 209#endif 210 211typedef struct vsock vsock_t; 212enum desc_type { FD_TYPE_SOCKET, FD_TYPE_FILE }; 213 214struct vsock { 215 SOCKET fd; 216 enum desc_type type; --- 78 unchanged lines hidden (view full) --- 295 * to not work correctly, returning a WSACONNRESET error when a WSASendTo 296 * fails with an "ICMP port unreachable" response and preventing the 297 * socket from using the WSARecvFrom in subsequent operations. 298 * The function below fixes this, but requires that Windows 2000 299 * Service Pack 2 or later be installed on the system. NT 4.0 300 * systems are not affected by this and work correctly. 301 * See Microsoft Knowledge Base Article Q263823 for details of this. 302 */ |
298isc_result_t 299connection_reset_fix(SOCKET fd) { | 303void 304connection_reset_fix( 305 SOCKET fd, 306 struct sockaddr_storage *addr 307 ) 308{ |
300 DWORD dwBytesReturned = 0; 301 BOOL bNewBehavior = FALSE; 302 DWORD status; 303 | 309 DWORD dwBytesReturned = 0; 310 BOOL bNewBehavior = FALSE; 311 DWORD status; 312 |
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); | 313 /* 314 * disable bad behavior using IOCTL: SIO_UDP_CONNRESET 315 * NT 4.0 has no problem 316 */ 317 if (isc_win32os_majorversion() >= 5) { 318 status = WSAIoctl(fd, SIO_UDP_CONNRESET, &bNewBehavior, 319 sizeof(bNewBehavior), NULL, 0, 320 &dwBytesReturned, NULL, NULL); 321 if (SOCKET_ERROR == status) 322 netsyslog(LOG_ERR, "connection_reset_fix() " 323 "failed for address %s: %m", 324 stoa(addr)); 325 } |
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 --- 155 unchanged lines hidden (view full) --- 478 479/* 480 * init_io - initialize I/O data structures and call socket creation routine 481 */ 482void 483init_io(void) 484{ 485#ifdef SYS_WINNT | 326} 327#endif 328 329/* 330 * on Unix systems the stdio library typically 331 * makes use of file descriptors in the lower 332 * integer range. stdio usually will make use 333 * of the file descriptor in the range of --- 155 unchanged lines hidden (view full) --- 489 490/* 491 * init_io - initialize I/O data structures and call socket creation routine 492 */ 493void 494init_io(void) 495{ 496#ifdef SYS_WINNT |
497 init_io_completion_port(); 498 |
|
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 --- 255 unchanged lines hidden (view full) --- 749} 750 751/* 752 * link interface into list of known interfaces 753 */ 754static void 755add_interface(struct interface *interface) 756{ | 499 if (!Win32InitSockets()) 500 { 501 netsyslog(LOG_ERR, "No useable winsock.dll: %m"); 502 exit(1); 503 } 504 init_transmitbuff(); 505#endif /* SYS_WINNT */ 506 --- 255 unchanged lines hidden (view full) --- 762} 763 764/* 765 * link interface into list of known interfaces 766 */ 767static void 768add_interface(struct interface *interface) 769{ |
770 static struct interface *listhead = NULL; 771 |
|
757 /* | 772 /* |
773 * For ntpd, the first few interfaces (wildcard, localhost) 774 * will never be removed. This means inter_list.head is 775 * unchanging once initialized. Take advantage of that to 776 * watch for changes and catch corruption earlier. This 777 * helped track down corruption caused by using FD_SET with 778 * a descriptor numerically larger than FD_SETSIZE. 779 */ 780 if (NULL == listhead) 781 listhead = inter_list.head; 782 783 if (listhead != inter_list.head) { 784 msyslog(LOG_ERR, "add_interface inter_list.head corrupted: was %p now %p", 785 listhead, inter_list.head); 786 exit(1); 787 } 788 /* |
|
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 --- 449 unchanged lines hidden (view full) --- 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 | 789 * Calculate the address hash 790 */ 791 interface->addr_refid = addr2refid(&interface->sin); 792 793 ISC_LIST_APPEND(inter_list, interface, link); 794 ninterfaces++; 795} 796 --- 449 unchanged lines hidden (view full) --- 1246 1247 DPRINTF(3, ("update_interfaces(%d)\n", ntohs( (u_short) port))); 1248 1249#ifdef INCLUDE_IPV6_SUPPORT 1250 if (isc_net_probeipv6() == ISC_R_SUCCESS) 1251 scan_ipv6 = ISC_TRUE; 1252#if defined(DEBUG) 1253 else |
1223 if(debug) | 1254 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 | 1255 netsyslog(LOG_ERR, "no IPv6 interfaces found"); 1256#endif 1257#endif 1258 if (isc_net_probeipv6() == ISC_R_SUCCESS) 1259 scan_ipv6 = ISC_TRUE; 1260#if defined(ISC_PLATFORM_HAVEIPV6) && defined(DEBUG) 1261 else |
1231 if(debug) | 1262 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) --- 297 unchanged lines hidden (view full) --- 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 | 1263 netsyslog(LOG_ERR, "no IPv6 interfaces found"); 1264#endif 1265 1266 if (isc_net_probeipv4() == ISC_R_SUCCESS) 1267 scan_ipv4 = ISC_TRUE; 1268#ifdef DEBUG 1269 else 1270 if(debug) --- 297 unchanged lines hidden (view full) --- 1568 */ 1569 add_addr_to_list(&interface->sin, interface); 1570 add_interface(interface); 1571 1572 DPRINT_INTERFACE(2, (interface, "created ", "\n")); 1573 return interface; 1574} 1575 |
1576 1577#ifdef SO_EXCLUSIVEADDRUSE 1578static void 1579set_excladdruse(int fd) 1580{ 1581 int one = 1; 1582 int failed; 1583 1584 failed = setsockopt(fd, SOL_SOCKET, SO_EXCLUSIVEADDRUSE, 1585 (char *)&one, sizeof(one)); 1586 1587 if (failed) 1588 netsyslog(LOG_ERR, 1589 "setsockopt(%d, SO_EXCLUSIVEADDRUSE, on): %m", fd); 1590} 1591#endif /* SO_EXCLUSIVEADDRUSE */ 1592 1593 |
|
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 */ 1550static void 1551set_reuseaddr(int flag) { | 1594/* 1595 * set_reuseaddr() - set/clear REUSEADDR on all sockets 1596 * NB possible hole - should we be doing this on broadcast 1597 * fd's also? 1598 */ 1599static void 1600set_reuseaddr(int flag) { |
1552 struct interface *interf; | 1601 struct interface *interf; |
1553 | 1602 |
1603#ifndef SO_EXCLUSIVEADDRUSE 1604 |
|
1554 for (interf = ISC_LIST_HEAD(inter_list); 1555 interf != NULL; 1556 interf = ISC_LIST_NEXT(interf, link)) { | 1605 for (interf = ISC_LIST_HEAD(inter_list); 1606 interf != NULL; 1607 interf = ISC_LIST_NEXT(interf, link)) { |
1557 if (interf->flags & INT_WILDCARD) 1558 continue; | 1608 1609 if (interf->flags & INT_WILDCARD) 1610 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 } | 1611 1612 /* 1613 * if interf->fd is INVALID_SOCKET, we might have a adapter 1614 * configured but not present 1615 */ 1616 DPRINTF(4, ("setting SO_REUSEADDR on %.16s@%s to %s\n", interf->name, stoa(&interf->sin), flag ? "on" : "off")); 1617 1618 if (interf->fd != INVALID_SOCKET) { 1619 if (setsockopt(interf->fd, SOL_SOCKET, 1620 SO_REUSEADDR, (char *)&flag, 1621 sizeof(flag))) { 1622 netsyslog(LOG_ERR, "set_reuseaddr: setsockopt(SO_REUSEADDR, %s) failed: %m", flag ? "on" : "off"); 1623 } 1624 } 1625 } |
1626#endif /* ! SO_EXCLUSIVEADDRUSE */ |
|
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 */ 1580void 1581enable_broadcast(struct interface *iface, struct sockaddr_storage *baddr) --- 145 unchanged lines hidden (view full) --- 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, | 1627} 1628 1629/* 1630 * This is just a wrapper around an internal function so we can 1631 * make other changes as necessary later on 1632 */ 1633void 1634enable_broadcast(struct interface *iface, struct sockaddr_storage *baddr) --- 145 unchanged lines hidden (view full) --- 1780 DPRINTF(4, ("Added IPv4 multicast interface on socket %d, addr %s for multicast address %s\n", 1781 iface->fd, stoa(&iface->sin), 1782 stoa(maddr))); 1783 break; 1784 1785 case AF_INET6: 1786#ifdef INCLUDE_IPV6_MULTICAST_SUPPORT 1787 if (setsockopt(iface->fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, |
1735 &iface->scopeid, sizeof(iface->scopeid)) == -1) { | 1788 (char *) &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, | 1789 netsyslog(LOG_ERR, 1790 "setsockopt IPV6_MULTICAST_IF failure: %m on socket %d, addr %s, scope %d for multicast address %s", 1791 iface->fd, stoa(&iface->sin), iface->scopeid, 1792 stoa(maddr)); 1793 return; 1794 } 1795#ifdef IPV6_MULTICAST_LOOP 1796 /* 1797 * Don't send back to itself, but allow it to fail to set it 1798 */ 1799 if (setsockopt(iface->fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, |
1747 &off, sizeof(off)) == -1) { | 1800 (char *) &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))); --- 261 unchanged lines hidden (view full) --- 2017 * io_multicast_add() - add multicast group address 2018 */ 2019void 2020io_multicast_add( 2021 struct sockaddr_storage addr 2022 ) 2023{ 2024#ifdef MCAST | 1801 netsyslog(LOG_ERR, 1802 "setsockopt IP_MULTICAST_LOOP failure: %m on socket %d, addr %s for multicast address %s", 1803 iface->fd, stoa(&iface->sin), stoa(maddr)); 1804 } 1805#endif 1806 DPRINTF(4, ("Added IPv6 multicast interface on socket %d, addr %s, scope %d for multicast address %s\n", 1807 iface->fd, stoa(&iface->sin), iface->scopeid, 1808 stoa(maddr))); --- 261 unchanged lines hidden (view full) --- 2070 * io_multicast_add() - add multicast group address 2071 */ 2072void 2073io_multicast_add( 2074 struct sockaddr_storage addr 2075 ) 2076{ 2077#ifdef MCAST |
2025 struct interface *interface, *iface; | 2078 struct interface *interface; 2079#ifndef MULTICAST_NONEWSOCKET 2080 struct interface *iface; 2081#endif |
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 --- 60 unchanged lines hidden (view full) --- 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 { | 2082 int lscope = 0; 2083 2084 /* 2085 * Check to see if this is a multicast address 2086 */ 2087 if (addr_ismulticast(&addr) == ISC_FALSE) 2088 return; 2089 --- 60 unchanged lines hidden (view full) --- 2150 htonl(~(u_int32)0); 2151 DPRINT_INTERFACE(2, (interface, "multicast add ", "\n")); 2152 /* socket_multicast_enable() will add this address to the addresslist */ 2153 add_interface(interface); 2154 list_if_listening(interface, htons(NTP_PORT)); 2155 } 2156 else 2157 { |
2102 delete_interface(interface); /* re-use existing interface */ | 2158 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 */ --- 152 unchanged lines hidden (view full) --- 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; | 2159 interface = NULL; 2160 if (addr.ss_family == AF_INET) 2161 interface = wildipv4; 2162 else if (addr.ss_family == AF_INET6) 2163 interface = wildipv6; 2164 2165 if (interface != NULL) { 2166 /* HACK ! -- stuff in an address */ --- 152 unchanged lines hidden (view full) --- 2319 netsyslog(LOG_ERR, "fcntl(O_NDELAY) fails on fd #%d: %m", 2320 fd); 2321 exit(1); 2322 /*NOTREACHED*/ 2323 } 2324#elif defined(FIONBIO) 2325 { 2326 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) | 2327 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) --- 18 unchanged lines hidden (view full) --- 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; | 2328 { 2329 netsyslog(LOG_ERR, "ioctl(FIONBIO) fails on fd #%d: %m", 2330 fd); 2331 exit(1); 2332 /*NOTREACHED*/ 2333 } 2334 } 2335#elif defined(FIOSNBIO) --- 18 unchanged lines hidden (view full) --- 2354 struct sockaddr_storage *addr, 2355 int flags, 2356 int turn_off_reuse, 2357 struct interface *interf 2358 ) 2359{ 2360 int errval; 2361 SOCKET fd; |
2311 int on = 1, off = 0; /* int is OK for REUSEADR per */ 2312 /* http://www.kohala.com/start/mcast.api.txt */ | 2362 /* 2363 * int is OK for REUSEADR per 2364 * http://www.kohala.com/start/mcast.api.txt 2365 */ 2366 int on = 1; 2367 int off = 0; 2368 |
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 */ | 2369#if defined(IPTOS_LOWDELAY) && defined(IPPROTO_IP) && defined(IP_TOS) 2370 int tos; 2371#endif /* IPTOS_LOWDELAY && IPPROTO_IP && IP_TOS */ 2372 2373 if ((addr->ss_family == AF_INET6) && (isc_net_probeipv6() != ISC_R_SUCCESS)) 2374 return (INVALID_SOCKET); 2375 2376 /* create a datagram (UDP) socket */ |
2377 fd = socket(addr->ss_family, SOCK_DGRAM, 0); 2378 if (INVALID_SOCKET == fd) { |
|
2321#ifndef SYS_WINNT | 2379#ifndef SYS_WINNT |
2322 if ( (fd = socket(addr->ss_family, SOCK_DGRAM, 0)) < 0) { | |
2323 errval = errno; 2324#else | 2380 errval = errno; 2381#else |
2325 if ( (fd = socket(addr->ss_family, SOCK_DGRAM, 0)) == INVALID_SOCKET) { | |
2326 errval = WSAGetLastError(); 2327#endif | 2382 errval = WSAGetLastError(); 2383#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 || | 2384 netsyslog(LOG_ERR, 2385 "socket(AF_INET%s, SOCK_DGRAM, 0) failed on address %s: %m", 2386 (addr->ss_family == AF_INET6) ? "6" : "", 2387 stoa(addr)); 2388 2389 if (errval == EPROTONOSUPPORT || 2390 errval == EAFNOSUPPORT || |
2336 errval == EPFNOSUPPORT) | 2391 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 } | 2392 return (INVALID_SOCKET); 2393 msyslog(LOG_ERR, "unexpected error code %d (not PROTONOSUPPORT|AFNOSUPPORT|FPNOSUPPORT) - exiting", errval); 2394 exit(1); 2395 /*NOTREACHED*/ 2396 } |
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 | 2397 |
2398#ifdef SYS_WINNT 2399 connection_reset_fix(fd, addr); 2400#endif |
|
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 | 2401 /* 2402 * Fixup the file descriptor for some systems 2403 * See bug #530 for details of the issue. 2404 */ 2405 fd = move_fd(fd); 2406 2407 /* 2408 * set SO_REUSEADDR since we will be binding the same port |
2361 * number on each interface according to flag | 2409 * number on each interface according to turn_off_reuse. 2410 * This is undesirable on Windows versions starting with 2411 * Windows XP (numeric version 5.1). |
2362 */ | 2412 */ |
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)); | 2413#ifdef SYS_WINNT 2414 if (isc_win32os_versioncheck(5, 1, 0, 0) < 0) /* before 5.1 */ 2415#endif 2416 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, 2417 (char *)(turn_off_reuse 2418 ? &off 2419 : &on), 2420 sizeof(on))) { |
2368 | 2421 |
2369 closesocket(fd); | 2422 netsyslog(LOG_ERR, "setsockopt SO_REUSEADDR %s" 2423 " fails for address %s: %m", 2424 turn_off_reuse 2425 ? "off" 2426 : "on", 2427 stoa(addr)); 2428 closesocket(fd); 2429 return INVALID_SOCKET; 2430 } 2431#ifdef SO_EXCLUSIVEADDRUSE 2432 /* 2433 * setting SO_EXCLUSIVEADDRUSE on the wildcard we open 2434 * first will cause more specific binds to fail. 2435 */ 2436 if (!(interf->flags & INT_WILDCARD)) 2437 set_excladdruse(fd); 2438#endif |
2370 | 2439 |
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) --- 60 unchanged lines hidden (view full) --- 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, | 2440 /* 2441 * IPv4 specific options go here 2442 */ 2443 if (addr->ss_family == AF_INET) { 2444#if defined(IPTOS_LOWDELAY) && defined(IPPROTO_IP) && defined(IP_TOS) 2445 /* set IP_TOS to minimize packet delay */ 2446 tos = IPTOS_LOWDELAY; 2447 if (setsockopt(fd, IPPROTO_IP, IP_TOS, (char *) &tos, sizeof(tos)) < 0) --- 60 unchanged lines hidden (view full) --- 2508 */ 2509 if (turn_off_reuse == 0 2510#ifdef DEBUG 2511 || debug > 1 2512#endif 2513 ) { 2514 if (addr->ss_family == AF_INET) 2515 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), | 2516 "bind() fd %d, family AF_INET, port %d, addr %s, in_classd=%d flags=0x%x fails: %m", 2517 fd, (int)ntohs(((struct sockaddr_in*)addr)->sin_port), |
2452 stoa(addr), | 2518 stoa(addr), |
2453 IN_CLASSD(ntohl(((struct sockaddr_in*)addr)->sin_addr.s_addr)), flags); | 2519 IN_CLASSD(ntohl(((struct sockaddr_in*)addr)->sin_addr.s_addr)), 2520 flags); |
2454#ifdef INCLUDE_IPV6_SUPPORT 2455 else if (addr->ss_family == AF_INET6) | 2521#ifdef INCLUDE_IPV6_SUPPORT 2522 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), | 2523 netsyslog(LOG_ERR, 2524 "bind() fd %d, family AF_INET6, port %d, scope %d, addr %s, mcast=%d flags=0x%x fails: %m", 2525 fd, (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), | 2526# ifdef ISC_PLATFORM_HAVESCOPEID 2527 ((struct sockaddr_in6*)addr)->sin6_scope_id 2528# else 2529 -1 2530# endif 2531 , stoa(addr), |
2465 IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6*)addr)->sin6_addr), flags); | 2532 IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6*)addr)->sin6_addr), 2533 flags); |
2466#endif 2467 } 2468 2469 closesocket(fd); 2470 2471 return INVALID_SOCKET; 2472 } 2473 --- 14 unchanged lines hidden (view full) --- 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), | 2534#endif 2535 } 2536 2537 closesocket(fd); 2538 2539 return INVALID_SOCKET; 2540 } 2541 --- 14 unchanged lines hidden (view full) --- 2556#endif 2557 } 2558#endif 2559 DPRINTF(4, ("bind() fd %d, family %d, port %d, addr %s, flags=0x%x\n", 2560 fd, 2561 addr->ss_family, 2562 (int)ntohs(((struct sockaddr_in*)addr)->sin_port), 2563 stoa(addr), |
2496 flags)); | 2564 interf->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); --- 27 unchanged lines hidden (view full) --- 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; | 2565 2566 init_nonblocking_io(fd); 2567 2568#ifdef HAVE_SIGNALED_IO 2569 init_socket_sig(fd); 2570#endif /* not HAVE_SIGNALED_IO */ 2571 2572 add_fd_to_list(fd, FD_TYPE_SOCKET); --- 27 unchanged lines hidden (view full) --- 2600 struct sockaddr_storage *dest, 2601 struct interface *inter, 2602 int ttl, 2603 struct pkt *pkt, 2604 int len 2605 ) 2606{ 2607 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; --- 103 unchanged lines hidden (view full) --- 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) | 2608 2609 /* 2610 * Send error caches. Empty slots have port == 0 2611 * Set ERRORCACHESIZE to 0 to disable 2612 */ 2613 struct cache { 2614 u_short port; 2615 struct in_addr addr; --- 103 unchanged lines hidden (view full) --- 2719 else if (dest->ss_family == AF_INET6) { 2720 if (badaddrs6[slot].port == ((struct sockaddr_in6*)dest)->sin6_port && 2721 badaddrs6[slot].addr.s6_addr == ((struct sockaddr_in6*)dest)->sin6_addr.s6_addr) 2722 break; 2723 } 2724#endif /* INCLUDE_IPV6_SUPPORT */ 2725 2726#if defined(HAVE_IO_COMPLETION_PORT) |
2662 err = io_completion_port_sendto(inter, pkt, len, dest); 2663 if (err != ERROR_SUCCESS) | 2727 cc = io_completion_port_sendto(inter, pkt, len, dest); 2728 if (cc != 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++; | 2729#else 2730#ifdef SIM 2731 cc = srvr_rply(&ntp_node, dest, inter, pkt); 2732#else /* SIM */ 2733 cc = sendto(inter->fd, (char *)pkt, (unsigned int)len, 0, (struct sockaddr *)dest, 2734 SOCKLEN(dest)); 2735#endif /* SIM */ 2736 if (cc == -1) 2737#endif 2738 { 2739 inter->notsent++; 2740 packets_notsent++; |
2741 |
|
2676#if defined(HAVE_IO_COMPLETION_PORT) | 2742#if defined(HAVE_IO_COMPLETION_PORT) |
2677 err = WSAGetLastError(); 2678 if (err != WSAEWOULDBLOCK && err != WSAENOBUFS && slot < 0) | 2743 if (cc != WSAEWOULDBLOCK && cc != 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) { --- 8 unchanged lines hidden (view full) --- 2695 break; 2696 } 2697 break; 2698 2699#ifdef INCLUDE_IPV6_SUPPORT 2700 case AF_INET6 : 2701 2702 for (slot = ERRORCACHESIZE; --slot >= 0; ) | 2744#else 2745 if (errno != EWOULDBLOCK && errno != ENOBUFS && slot < 0) 2746#endif 2747 { 2748 /* 2749 * Remember this, if there's an empty slot 2750 */ 2751 switch (dest->ss_family) { --- 8 unchanged lines hidden (view full) --- 2760 break; 2761 } 2762 break; 2763 2764#ifdef INCLUDE_IPV6_SUPPORT 2765 case AF_INET6 : 2766 2767 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; | 2768 if (badaddrs6[slot].port == 0) 2769 { 2770 badaddrs6[slot].port = SRCPORT(dest); 2771 badaddrs6[slot].addr = ((struct sockaddr_in6*)dest)->sin6_addr; 2772 break; 2773 } 2774 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 } --- 525 unchanged lines hidden (view full) --- 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", | 2775#endif /* INCLUDE_IPV6_SUPPORT */ 2776 default: /* don't care if not supported */ 2777 break; 2778 } 2779 2780 netsyslog(LOG_ERR, "sendto(%s) (fd=%d): %m", 2781 stoa(dest), inter->fd); 2782 } --- 525 unchanged lines hidden (view full) --- 3308{ 3309 SOCKET s; 3310 int rtn; 3311 struct sockaddr_storage saddr; 3312 GETSOCKNAME_SOCKLEN_TYPE saddrlen = SOCKLEN(addr); 3313 struct interface *iface; 3314 3315 DPRINTF(4, ("Finding interface for addr %s in list of addresses\n", |
3251 stoa(addr));) | 3316 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 --- 400 unchanged lines hidden (view full) --- 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 | 3317 3318 memset(&saddr, 0, sizeof(saddr)); 3319 saddr.ss_family = addr->ss_family; 3320 if(addr->ss_family == AF_INET) { 3321 memcpy(&((struct sockaddr_in*)&saddr)->sin_addr, &((struct sockaddr_in*)addr)->sin_addr, sizeof(struct in_addr)); 3322 ((struct sockaddr_in*)&saddr)->sin_port = htons(NTP_PORT); 3323 } 3324#ifdef INCLUDE_IPV6_SUPPORT --- 400 unchanged lines hidden (view full) --- 3725 lsock->fd = fd; 3726 lsock->type = type; 3727 3728 ISC_LIST_APPEND(fd_list, lsock, link); 3729 /* 3730 * I/O Completion Ports don't care about the select and FD_SET 3731 */ 3732#ifndef HAVE_IO_COMPLETION_PORT |
3733 if (fd < 0 || fd >= FD_SETSIZE) { 3734 msyslog(LOG_ERR, "Too many sockets in use, FD_SETSIZE %d exceeded", 3735 FD_SETSIZE); 3736 exit(1); 3737 } |
|
3668 /* 3669 * keep activefds in sync 3670 */ 3671 if (fd > maxactivefd) 3672 maxactivefd = fd; 3673 FD_SET( (u_int)fd, &activefds); 3674#endif 3675} --- 279 unchanged lines hidden --- | 3738 /* 3739 * keep activefds in sync 3740 */ 3741 if (fd > maxactivefd) 3742 maxactivefd = fd; 3743 FD_SET( (u_int)fd, &activefds); 3744#endif 3745} --- 279 unchanged lines hidden --- |