ntp: don't use IP_SENDSRCADDR on bound socket
On FreeBSD, sendmsg() fails when IP_SENDSRCADDR specifies a source address on a socket that is bound to the address. This prevents a server configured with the bindaddress directive from responding to clients. Add a new variable to check whether the server IPv4 socket is not bound before setting the source address.
This commit is contained in:
parent
cc8414b1b3
commit
6af39d63aa
1 changed files with 12 additions and 2 deletions
14
ntp_io.c
14
ntp_io.c
|
@ -105,6 +105,9 @@ static int separate_client_sockets;
|
||||||
disabled */
|
disabled */
|
||||||
static int permanent_server_sockets;
|
static int permanent_server_sockets;
|
||||||
|
|
||||||
|
/* Flag indicating the server IPv4 socket is bound to an address */
|
||||||
|
static int bound_server_sock_fd4;
|
||||||
|
|
||||||
/* Flag indicating that we have been initialised */
|
/* Flag indicating that we have been initialised */
|
||||||
static int initialised=0;
|
static int initialised=0;
|
||||||
|
|
||||||
|
@ -168,6 +171,9 @@ prepare_socket(int family, int port_number, int client_only)
|
||||||
my_addr.in4.sin_port = htons(port_number);
|
my_addr.in4.sin_port = htons(port_number);
|
||||||
my_addr_len = sizeof (my_addr.in4);
|
my_addr_len = sizeof (my_addr.in4);
|
||||||
|
|
||||||
|
if (!client_only)
|
||||||
|
bound_server_sock_fd4 = my_addr.in4.sin_addr.s_addr != htonl(INADDR_ANY);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
#ifdef FEAT_IPV6
|
#ifdef FEAT_IPV6
|
||||||
case AF_INET6:
|
case AF_INET6:
|
||||||
|
@ -821,8 +827,8 @@ NIO_SendPacket(NTP_Packet *packet, NTP_Remote_Address *remote_addr,
|
||||||
msg.msg_flags = 0;
|
msg.msg_flags = 0;
|
||||||
cmsglen = 0;
|
cmsglen = 0;
|
||||||
|
|
||||||
if (local_addr->ip_addr.family == IPADDR_INET4) {
|
|
||||||
#ifdef HAVE_IN_PKTINFO
|
#ifdef HAVE_IN_PKTINFO
|
||||||
|
if (local_addr->ip_addr.family == IPADDR_INET4) {
|
||||||
struct in_pktinfo *ipi;
|
struct in_pktinfo *ipi;
|
||||||
|
|
||||||
cmsg = CMSG_FIRSTHDR(&msg);
|
cmsg = CMSG_FIRSTHDR(&msg);
|
||||||
|
@ -837,7 +843,11 @@ NIO_SendPacket(NTP_Packet *packet, NTP_Remote_Address *remote_addr,
|
||||||
ipi->ipi_spec_dst.s_addr = htonl(local_addr->ip_addr.addr.in4);
|
ipi->ipi_spec_dst.s_addr = htonl(local_addr->ip_addr.addr.in4);
|
||||||
if (local_addr->if_index != INVALID_IF_INDEX)
|
if (local_addr->if_index != INVALID_IF_INDEX)
|
||||||
ipi->ipi_ifindex = local_addr->if_index;
|
ipi->ipi_ifindex = local_addr->if_index;
|
||||||
|
}
|
||||||
#elif defined(IP_SENDSRCADDR)
|
#elif defined(IP_SENDSRCADDR)
|
||||||
|
/* Specify the IPv4 source address only if the socket is not bound */
|
||||||
|
if (local_addr->ip_addr.family == IPADDR_INET4 &&
|
||||||
|
local_addr->sock_fd == server_sock_fd4 && !bound_server_sock_fd4) {
|
||||||
struct in_addr *addr;
|
struct in_addr *addr;
|
||||||
|
|
||||||
cmsg = CMSG_FIRSTHDR(&msg);
|
cmsg = CMSG_FIRSTHDR(&msg);
|
||||||
|
@ -850,8 +860,8 @@ NIO_SendPacket(NTP_Packet *packet, NTP_Remote_Address *remote_addr,
|
||||||
|
|
||||||
addr = (struct in_addr *)CMSG_DATA(cmsg);
|
addr = (struct in_addr *)CMSG_DATA(cmsg);
|
||||||
addr->s_addr = htonl(local_addr->ip_addr.addr.in4);
|
addr->s_addr = htonl(local_addr->ip_addr.addr.in4);
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifdef HAVE_IN6_PKTINFO
|
#ifdef HAVE_IN6_PKTINFO
|
||||||
if (local_addr->ip_addr.family == IPADDR_INET6) {
|
if (local_addr->ip_addr.family == IPADDR_INET6) {
|
||||||
|
|
Loading…
Reference in a new issue