Use exact address size in bind and sendto calls
Apparently this is needed on some systems, otherwise the calls return EINVAL.
This commit is contained in:
parent
63ae72e009
commit
365834535e
4 changed files with 34 additions and 9 deletions
10
acquire.c
10
acquire.c
|
@ -160,6 +160,7 @@ prepare_socket(int family)
|
||||||
{
|
{
|
||||||
unsigned short port_number = CNF_GetAcquisitionPort();
|
unsigned short port_number = CNF_GetAcquisitionPort();
|
||||||
int sock_fd;
|
int sock_fd;
|
||||||
|
socklen_t addrlen;
|
||||||
|
|
||||||
sock_fd = socket(family, SOCK_DGRAM, 0);
|
sock_fd = socket(family, SOCK_DGRAM, 0);
|
||||||
|
|
||||||
|
@ -180,19 +181,21 @@ prepare_socket(int family)
|
||||||
my_addr.in4.sin_family = family;
|
my_addr.in4.sin_family = family;
|
||||||
my_addr.in4.sin_port = htons(port_number);
|
my_addr.in4.sin_port = htons(port_number);
|
||||||
my_addr.in4.sin_addr.s_addr = htonl(INADDR_ANY);
|
my_addr.in4.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||||
|
addrlen = sizeof (my_addr.in4);
|
||||||
break;
|
break;
|
||||||
#ifdef HAVE_IPV6
|
#ifdef HAVE_IPV6
|
||||||
case AF_INET6:
|
case AF_INET6:
|
||||||
my_addr.in6.sin6_family = family;
|
my_addr.in6.sin6_family = family;
|
||||||
my_addr.in6.sin6_port = htons(port_number);
|
my_addr.in6.sin6_port = htons(port_number);
|
||||||
my_addr.in6.sin6_addr = in6addr_any;
|
my_addr.in6.sin6_addr = in6addr_any;
|
||||||
|
addrlen = sizeof (my_addr.in6);
|
||||||
break;
|
break;
|
||||||
#endif
|
#endif
|
||||||
default:
|
default:
|
||||||
assert(0);
|
assert(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (bind(sock_fd, &my_addr.u, sizeof(my_addr)) < 0) {
|
if (bind(sock_fd, &my_addr.u, addrlen) < 0) {
|
||||||
LOG(LOGS_ERR, LOGF_Acquire, "Could not bind socket : %s\n", strerror(errno));
|
LOG(LOGS_ERR, LOGF_Acquire, "Could not bind socket : %s\n", strerror(errno));
|
||||||
/* but keep running */
|
/* but keep running */
|
||||||
}
|
}
|
||||||
|
@ -248,6 +251,7 @@ probe_source(SourceRecord *src)
|
||||||
double local_time_err;
|
double local_time_err;
|
||||||
union sockaddr_in46 his_addr;
|
union sockaddr_in46 his_addr;
|
||||||
int sock_fd;
|
int sock_fd;
|
||||||
|
socklen_t addrlen;
|
||||||
|
|
||||||
#if 0
|
#if 0
|
||||||
printf("Sending probe to %s sent=%d samples=%d\n", UTI_IPToString(&src->ip_addr), src->n_probes_sent, src->n_samples);
|
printf("Sending probe to %s sent=%d samples=%d\n", UTI_IPToString(&src->ip_addr), src->n_probes_sent, src->n_samples);
|
||||||
|
@ -278,6 +282,7 @@ probe_source(SourceRecord *src)
|
||||||
his_addr.in4.sin_addr.s_addr = htonl(src->ip_addr.addr.in4);
|
his_addr.in4.sin_addr.s_addr = htonl(src->ip_addr.addr.in4);
|
||||||
his_addr.in4.sin_port = htons(123); /* Fixed for now */
|
his_addr.in4.sin_port = htons(123); /* Fixed for now */
|
||||||
his_addr.in4.sin_family = AF_INET;
|
his_addr.in4.sin_family = AF_INET;
|
||||||
|
addrlen = sizeof (his_addr.in4);
|
||||||
sock_fd = sock_fd4;
|
sock_fd = sock_fd4;
|
||||||
break;
|
break;
|
||||||
#ifdef HAVE_IPV6
|
#ifdef HAVE_IPV6
|
||||||
|
@ -286,6 +291,7 @@ probe_source(SourceRecord *src)
|
||||||
sizeof (his_addr.in6.sin6_addr.s6_addr));
|
sizeof (his_addr.in6.sin6_addr.s6_addr));
|
||||||
his_addr.in6.sin6_port = htons(123); /* Fixed for now */
|
his_addr.in6.sin6_port = htons(123); /* Fixed for now */
|
||||||
his_addr.in6.sin6_family = AF_INET6;
|
his_addr.in6.sin6_family = AF_INET6;
|
||||||
|
addrlen = sizeof (his_addr.in6);
|
||||||
sock_fd = sock_fd6;
|
sock_fd = sock_fd6;
|
||||||
break;
|
break;
|
||||||
#endif
|
#endif
|
||||||
|
@ -299,7 +305,7 @@ probe_source(SourceRecord *src)
|
||||||
|
|
||||||
if (sendto(sock_fd, (void *) &pkt, NTP_NORMAL_PACKET_SIZE,
|
if (sendto(sock_fd, (void *) &pkt, NTP_NORMAL_PACKET_SIZE,
|
||||||
0,
|
0,
|
||||||
&his_addr.u, sizeof(his_addr)) < 0) {
|
&his_addr.u, addrlen) < 0) {
|
||||||
LOG(LOGS_WARN, LOGF_Acquire, "Could not send to %s : %s",
|
LOG(LOGS_WARN, LOGF_Acquire, "Could not send to %s : %s",
|
||||||
UTI_IPToString(&src->ip_addr),
|
UTI_IPToString(&src->ip_addr),
|
||||||
strerror(errno));
|
strerror(errno));
|
||||||
|
|
5
client.c
5
client.c
|
@ -69,6 +69,7 @@ union sockaddr_in46 {
|
||||||
|
|
||||||
static int sock_fd;
|
static int sock_fd;
|
||||||
union sockaddr_in46 his_addr;
|
union sockaddr_in46 his_addr;
|
||||||
|
static socklen_t his_addr_len;
|
||||||
|
|
||||||
static int on_terminal = 0;
|
static int on_terminal = 0;
|
||||||
|
|
||||||
|
@ -163,6 +164,7 @@ open_io(const char *hostname, int port)
|
||||||
his_addr.in4.sin_family = AF_INET;
|
his_addr.in4.sin_family = AF_INET;
|
||||||
his_addr.in4.sin_addr.s_addr = htonl(ip.addr.in4);
|
his_addr.in4.sin_addr.s_addr = htonl(ip.addr.in4);
|
||||||
his_addr.in4.sin_port = htons(port);
|
his_addr.in4.sin_port = htons(port);
|
||||||
|
his_addr_len = sizeof (his_addr.in4);
|
||||||
break;
|
break;
|
||||||
#ifdef HAVE_IPV6
|
#ifdef HAVE_IPV6
|
||||||
case IPADDR_INET6:
|
case IPADDR_INET6:
|
||||||
|
@ -172,6 +174,7 @@ open_io(const char *hostname, int port)
|
||||||
memcpy(his_addr.in6.sin6_addr.s6_addr, ip.addr.in6,
|
memcpy(his_addr.in6.sin6_addr.s6_addr, ip.addr.in6,
|
||||||
sizeof (his_addr.in6.sin6_addr.s6_addr));
|
sizeof (his_addr.in6.sin6_addr.s6_addr));
|
||||||
his_addr.in6.sin6_port = htons(port);
|
his_addr.in6.sin6_port = htons(port);
|
||||||
|
his_addr_len = sizeof (his_addr.in6);
|
||||||
break;
|
break;
|
||||||
#endif
|
#endif
|
||||||
default:
|
default:
|
||||||
|
@ -1207,7 +1210,7 @@ submit_request(CMD_Request *request, CMD_Reply *reply, int *reply_auth_ok)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (sendto(sock_fd, (void *) request, command_length, 0,
|
if (sendto(sock_fd, (void *) request, command_length, 0,
|
||||||
&his_addr.u, sizeof(his_addr)) < 0) {
|
&his_addr.u, his_addr_len) < 0) {
|
||||||
|
|
||||||
|
|
||||||
#if 0
|
#if 0
|
||||||
|
|
12
cmdmon.c
12
cmdmon.c
|
@ -178,6 +178,7 @@ static int
|
||||||
prepare_socket(int family)
|
prepare_socket(int family)
|
||||||
{
|
{
|
||||||
int port_number, sock_fd;
|
int port_number, sock_fd;
|
||||||
|
socklen_t my_addr_len;
|
||||||
union sockaddr_in46 my_addr;
|
union sockaddr_in46 my_addr;
|
||||||
IPAddr bind_address;
|
IPAddr bind_address;
|
||||||
int on_off = 1;
|
int on_off = 1;
|
||||||
|
@ -214,6 +215,7 @@ prepare_socket(int family)
|
||||||
|
|
||||||
switch (family) {
|
switch (family) {
|
||||||
case AF_INET:
|
case AF_INET:
|
||||||
|
my_addr_len = sizeof (my_addr.in4);
|
||||||
my_addr.in4.sin_family = family;
|
my_addr.in4.sin_family = family;
|
||||||
my_addr.in4.sin_port = htons((unsigned short)port_number);
|
my_addr.in4.sin_port = htons((unsigned short)port_number);
|
||||||
|
|
||||||
|
@ -226,6 +228,7 @@ prepare_socket(int family)
|
||||||
break;
|
break;
|
||||||
#ifdef HAVE_IPV6
|
#ifdef HAVE_IPV6
|
||||||
case AF_INET6:
|
case AF_INET6:
|
||||||
|
my_addr_len = sizeof (my_addr.in6);
|
||||||
my_addr.in6.sin6_family = family;
|
my_addr.in6.sin6_family = family;
|
||||||
my_addr.in6.sin6_port = htons((unsigned short)port_number);
|
my_addr.in6.sin6_port = htons((unsigned short)port_number);
|
||||||
|
|
||||||
|
@ -242,7 +245,7 @@ prepare_socket(int family)
|
||||||
assert(0);
|
assert(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (bind(sock_fd, &my_addr.u, sizeof(my_addr)) < 0) {
|
if (bind(sock_fd, &my_addr.u, my_addr_len) < 0) {
|
||||||
LOG_FATAL(LOGF_CmdMon, "Could not bind %s command socket : %s",
|
LOG_FATAL(LOGF_CmdMon, "Could not bind %s command socket : %s",
|
||||||
family == AF_INET ? "IPv4" : "IPv6", strerror(errno));
|
family == AF_INET ? "IPv4" : "IPv6", strerror(errno));
|
||||||
}
|
}
|
||||||
|
@ -722,14 +725,17 @@ transmit_reply(CMD_Reply *msg, union sockaddr_in46 *where_to)
|
||||||
int status;
|
int status;
|
||||||
int tx_message_length;
|
int tx_message_length;
|
||||||
int sock_fd;
|
int sock_fd;
|
||||||
|
socklen_t addrlen;
|
||||||
|
|
||||||
switch (where_to->u.sa_family) {
|
switch (where_to->u.sa_family) {
|
||||||
case AF_INET:
|
case AF_INET:
|
||||||
sock_fd = sock_fd4;
|
sock_fd = sock_fd4;
|
||||||
|
addrlen = sizeof (where_to->in4);
|
||||||
break;
|
break;
|
||||||
#ifdef HAVE_IPV6
|
#ifdef HAVE_IPV6
|
||||||
case AF_INET6:
|
case AF_INET6:
|
||||||
sock_fd = sock_fd6;
|
sock_fd = sock_fd6;
|
||||||
|
addrlen = sizeof (where_to->in6);
|
||||||
break;
|
break;
|
||||||
#endif
|
#endif
|
||||||
default:
|
default:
|
||||||
|
@ -738,7 +744,7 @@ transmit_reply(CMD_Reply *msg, union sockaddr_in46 *where_to)
|
||||||
|
|
||||||
tx_message_length = PKL_ReplyLength(msg);
|
tx_message_length = PKL_ReplyLength(msg);
|
||||||
status = sendto(sock_fd, (void *) msg, tx_message_length, 0,
|
status = sendto(sock_fd, (void *) msg, tx_message_length, 0,
|
||||||
&where_to->u, sizeof(union sockaddr_in46));
|
&where_to->u, addrlen);
|
||||||
|
|
||||||
if (status < 0) {
|
if (status < 0) {
|
||||||
unsigned short port;
|
unsigned short port;
|
||||||
|
@ -1931,7 +1937,7 @@ read_from_cmd_socket(void *anything)
|
||||||
/* Just send this message again */
|
/* Just send this message again */
|
||||||
tx_message_length = PKL_ReplyLength(prev_tx_message);
|
tx_message_length = PKL_ReplyLength(prev_tx_message);
|
||||||
status = sendto(sock_fd, (void *) prev_tx_message, tx_message_length, 0,
|
status = sendto(sock_fd, (void *) prev_tx_message, tx_message_length, 0,
|
||||||
(struct sockaddr *) &where_from, sizeof(where_from));
|
&where_from.u, from_length);
|
||||||
if (status < 0) {
|
if (status < 0) {
|
||||||
LOG(LOGS_WARN, LOGF_CmdMon, "Could not send response to %s:%hu", UTI_IPToString(&remote_ip), remote_port);
|
LOG(LOGS_WARN, LOGF_CmdMon, "Could not send response to %s:%hu", UTI_IPToString(&remote_ip), remote_port);
|
||||||
}
|
}
|
||||||
|
|
16
ntp_io.c
16
ntp_io.c
|
@ -99,6 +99,7 @@ static int
|
||||||
prepare_socket(int family)
|
prepare_socket(int family)
|
||||||
{
|
{
|
||||||
union sockaddr_in46 my_addr;
|
union sockaddr_in46 my_addr;
|
||||||
|
socklen_t my_addr_len;
|
||||||
int sock_fd;
|
int sock_fd;
|
||||||
unsigned short port_number;
|
unsigned short port_number;
|
||||||
IPAddr bind_address;
|
IPAddr bind_address;
|
||||||
|
@ -165,6 +166,7 @@ prepare_socket(int family)
|
||||||
|
|
||||||
switch (family) {
|
switch (family) {
|
||||||
case AF_INET:
|
case AF_INET:
|
||||||
|
my_addr_len = sizeof (my_addr.in4);
|
||||||
my_addr.in4.sin_family = family;
|
my_addr.in4.sin_family = family;
|
||||||
my_addr.in4.sin_port = htons(port_number);
|
my_addr.in4.sin_port = htons(port_number);
|
||||||
|
|
||||||
|
@ -177,6 +179,7 @@ prepare_socket(int family)
|
||||||
break;
|
break;
|
||||||
#ifdef HAVE_IPV6
|
#ifdef HAVE_IPV6
|
||||||
case AF_INET6:
|
case AF_INET6:
|
||||||
|
my_addr_len = sizeof (my_addr.in6);
|
||||||
my_addr.in6.sin6_family = family;
|
my_addr.in6.sin6_family = family;
|
||||||
my_addr.in6.sin6_port = htons(port_number);
|
my_addr.in6.sin6_port = htons(port_number);
|
||||||
|
|
||||||
|
@ -197,7 +200,7 @@ prepare_socket(int family)
|
||||||
LOG(LOGS_INFO, LOGF_NtpIO, "Initialising, socket fd=%d", sock_fd);
|
LOG(LOGS_INFO, LOGF_NtpIO, "Initialising, socket fd=%d", sock_fd);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (bind(sock_fd, &my_addr.u, sizeof(my_addr)) < 0) {
|
if (bind(sock_fd, &my_addr.u, my_addr_len) < 0) {
|
||||||
LOG_FATAL(LOGF_NtpIO, "Could not bind %s NTP socket : %s",
|
LOG_FATAL(LOGF_NtpIO, "Could not bind %s NTP socket : %s",
|
||||||
family == AF_INET ? "IPv4" : "IPv6", strerror(errno));
|
family == AF_INET ? "IPv4" : "IPv6", strerror(errno));
|
||||||
}
|
}
|
||||||
|
@ -387,12 +390,14 @@ send_packet(void *packet, int packetlen, NTP_Remote_Address *remote_addr)
|
||||||
char cmsgbuf[256];
|
char cmsgbuf[256];
|
||||||
int cmsglen;
|
int cmsglen;
|
||||||
int sock_fd;
|
int sock_fd;
|
||||||
|
socklen_t addrlen;
|
||||||
|
|
||||||
assert(initialised);
|
assert(initialised);
|
||||||
|
|
||||||
memset(&remote, 0, sizeof (remote));
|
|
||||||
switch (remote_addr->ip_addr.family) {
|
switch (remote_addr->ip_addr.family) {
|
||||||
case IPADDR_INET4:
|
case IPADDR_INET4:
|
||||||
|
memset(&remote.in4, 0, sizeof (remote.in4));
|
||||||
|
addrlen = sizeof (remote.in4);
|
||||||
remote.in4.sin_family = AF_INET;
|
remote.in4.sin_family = AF_INET;
|
||||||
remote.in4.sin_port = htons(remote_addr->port);
|
remote.in4.sin_port = htons(remote_addr->port);
|
||||||
remote.in4.sin_addr.s_addr = htonl(remote_addr->ip_addr.addr.in4);
|
remote.in4.sin_addr.s_addr = htonl(remote_addr->ip_addr.addr.in4);
|
||||||
|
@ -400,6 +405,8 @@ send_packet(void *packet, int packetlen, NTP_Remote_Address *remote_addr)
|
||||||
break;
|
break;
|
||||||
#ifdef HAVE_IPV6
|
#ifdef HAVE_IPV6
|
||||||
case IPADDR_INET6:
|
case IPADDR_INET6:
|
||||||
|
memset(&remote.in6, 0, sizeof (remote.in6));
|
||||||
|
addrlen = sizeof (remote.in6);
|
||||||
remote.in6.sin6_family = AF_INET6;
|
remote.in6.sin6_family = AF_INET6;
|
||||||
remote.in6.sin6_port = htons(remote_addr->port);
|
remote.in6.sin6_port = htons(remote_addr->port);
|
||||||
memcpy(&remote.in6.sin6_addr.s6_addr, &remote_addr->ip_addr.addr.in6,
|
memcpy(&remote.in6.sin6_addr.s6_addr, &remote_addr->ip_addr.addr.in6,
|
||||||
|
@ -417,7 +424,7 @@ send_packet(void *packet, int packetlen, NTP_Remote_Address *remote_addr)
|
||||||
iov.iov_base = packet;
|
iov.iov_base = packet;
|
||||||
iov.iov_len = packetlen;
|
iov.iov_len = packetlen;
|
||||||
msg.msg_name = &remote.u;
|
msg.msg_name = &remote.u;
|
||||||
msg.msg_namelen = sizeof(remote);
|
msg.msg_namelen = addrlen;
|
||||||
msg.msg_iov = &iov;
|
msg.msg_iov = &iov;
|
||||||
msg.msg_iovlen = 1;
|
msg.msg_iovlen = 1;
|
||||||
msg.msg_control = cmsgbuf;
|
msg.msg_control = cmsgbuf;
|
||||||
|
@ -449,6 +456,9 @@ send_packet(void *packet, int packetlen, NTP_Remote_Address *remote_addr)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
msg.msg_controllen = cmsglen;
|
msg.msg_controllen = cmsglen;
|
||||||
|
/* This is apparently required on some systems */
|
||||||
|
if (!cmsglen)
|
||||||
|
msg.msg_control = NULL;
|
||||||
|
|
||||||
if (sendmsg(sock_fd, &msg, 0) < 0) {
|
if (sendmsg(sock_fd, &msg, 0) < 0) {
|
||||||
LOG(LOGS_WARN, LOGF_NtpIO, "Could not send to %s:%d : %s",
|
LOG(LOGS_WARN, LOGF_NtpIO, "Could not send to %s:%d : %s",
|
||||||
|
|
Loading…
Reference in a new issue