]> git.ipfire.org Git - thirdparty/chrony.git/commitdiff
Use exact address size in bind and sendto calls
authorMiroslav Lichvar <mlichvar@redhat.com>
Mon, 7 Dec 2009 11:51:56 +0000 (12:51 +0100)
committerMiroslav Lichvar <mlichvar@redhat.com>
Mon, 7 Dec 2009 11:51:56 +0000 (12:51 +0100)
Apparently this is needed on some systems, otherwise the calls
return EINVAL.

acquire.c
client.c
cmdmon.c
ntp_io.c

index 87df3534e0a4c626690225b13bdc183da6fdca61..3f702b6e7af25f55f67bf239d31baee4ced728b7 100644 (file)
--- a/acquire.c
+++ b/acquire.c
@@ -160,6 +160,7 @@ prepare_socket(int family)
 {
   unsigned short port_number = CNF_GetAcquisitionPort();
   int sock_fd;
+  socklen_t addrlen;
 
   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_port = htons(port_number);
         my_addr.in4.sin_addr.s_addr = htonl(INADDR_ANY);
+        addrlen = sizeof (my_addr.in4);
         break;
 #ifdef HAVE_IPV6
       case AF_INET6:
         my_addr.in6.sin6_family = family;
         my_addr.in6.sin6_port = htons(port_number);
         my_addr.in6.sin6_addr = in6addr_any;
+        addrlen = sizeof (my_addr.in6);
         break;
 #endif
       default:
         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));
       /* but keep running */
     }
@@ -248,6 +251,7 @@ probe_source(SourceRecord *src)
   double local_time_err;
   union sockaddr_in46 his_addr;
   int sock_fd;
+  socklen_t addrlen;
 
 #if 0
   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_port = htons(123); /* Fixed for now */
       his_addr.in4.sin_family = AF_INET;
+      addrlen = sizeof (his_addr.in4);
       sock_fd = sock_fd4;
       break;
 #ifdef HAVE_IPV6
@@ -286,6 +291,7 @@ probe_source(SourceRecord *src)
           sizeof (his_addr.in6.sin6_addr.s6_addr));
       his_addr.in6.sin6_port = htons(123); /* Fixed for now */
       his_addr.in6.sin6_family = AF_INET6;
+      addrlen = sizeof (his_addr.in6);
       sock_fd = sock_fd6;
       break;
 #endif
@@ -299,7 +305,7 @@ probe_source(SourceRecord *src)
 
   if (sendto(sock_fd, (void *) &pkt, NTP_NORMAL_PACKET_SIZE,
              0,
-             &his_addr.u, sizeof(his_addr)) < 0) {
+             &his_addr.u, addrlen) < 0) {
     LOG(LOGS_WARN, LOGF_Acquire, "Could not send to %s : %s",
         UTI_IPToString(&src->ip_addr),
         strerror(errno));
index 00df07c8c5ff4d7b779fdd5a2f401dd8626253b0..1872d12c2b2a2defed80c2de111632b617fbb452 100644 (file)
--- a/client.c
+++ b/client.c
@@ -69,6 +69,7 @@ union sockaddr_in46 {
 
 static int sock_fd;
 union sockaddr_in46 his_addr;
+static socklen_t his_addr_len;
 
 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_addr.s_addr = htonl(ip.addr.in4);
       his_addr.in4.sin_port = htons(port);
+      his_addr_len = sizeof (his_addr.in4);
       break;
 #ifdef HAVE_IPV6
     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,
           sizeof (his_addr.in6.sin6_addr.s6_addr));
       his_addr.in6.sin6_port = htons(port);
+      his_addr_len = sizeof (his_addr.in6);
       break;
 #endif
     default:
@@ -1207,7 +1210,7 @@ submit_request(CMD_Request *request, CMD_Reply *reply, int *reply_auth_ok)
 #endif
 
     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
index df52d163719886855344146af65e96c208d16e2b..d1e69e4b6efda953a18914dbf28bf0a7fed0079d 100644 (file)
--- a/cmdmon.c
+++ b/cmdmon.c
@@ -178,6 +178,7 @@ static int
 prepare_socket(int family)
 {
   int port_number, sock_fd;
+  socklen_t my_addr_len;
   union sockaddr_in46 my_addr;
   IPAddr bind_address;
   int on_off = 1;
@@ -214,6 +215,7 @@ prepare_socket(int family)
 
   switch (family) {
     case AF_INET:
+      my_addr_len = sizeof (my_addr.in4);
       my_addr.in4.sin_family = family;
       my_addr.in4.sin_port = htons((unsigned short)port_number);
 
@@ -226,6 +228,7 @@ prepare_socket(int family)
       break;
 #ifdef HAVE_IPV6
     case AF_INET6:
+      my_addr_len = sizeof (my_addr.in6);
       my_addr.in6.sin6_family = family;
       my_addr.in6.sin6_port = htons((unsigned short)port_number);
 
@@ -242,7 +245,7 @@ prepare_socket(int family)
       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",
         family == AF_INET ? "IPv4" : "IPv6", strerror(errno));
   }
@@ -722,14 +725,17 @@ transmit_reply(CMD_Reply *msg, union sockaddr_in46 *where_to)
   int status;
   int tx_message_length;
   int sock_fd;
+  socklen_t addrlen;
   
   switch (where_to->u.sa_family) {
     case AF_INET:
       sock_fd = sock_fd4;
+      addrlen = sizeof (where_to->in4);
       break;
 #ifdef HAVE_IPV6
     case AF_INET6:
       sock_fd = sock_fd6;
+      addrlen = sizeof (where_to->in6);
       break;
 #endif
     default:
@@ -738,7 +744,7 @@ transmit_reply(CMD_Reply *msg, union sockaddr_in46 *where_to)
 
   tx_message_length = PKL_ReplyLength(msg);
   status = sendto(sock_fd, (void *) msg, tx_message_length, 0,
-                  &where_to->u, sizeof(union sockaddr_in46));
+                  &where_to->u, addrlen);
 
   if (status < 0) {
     unsigned short port;
@@ -1931,7 +1937,7 @@ read_from_cmd_socket(void *anything)
       /* Just send this message again */
       tx_message_length = PKL_ReplyLength(prev_tx_message);
       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) {
         LOG(LOGS_WARN, LOGF_CmdMon, "Could not send response to %s:%hu", UTI_IPToString(&remote_ip), remote_port);
       }
index cbabc7762224b4efa70693c99de84227c0366783..66693a4976c1ff10563875f138fe9eabf929ffb2 100644 (file)
--- a/ntp_io.c
+++ b/ntp_io.c
@@ -99,6 +99,7 @@ static int
 prepare_socket(int family)
 {
   union sockaddr_in46 my_addr;
+  socklen_t my_addr_len;
   int sock_fd;
   unsigned short port_number;
   IPAddr bind_address;
@@ -165,6 +166,7 @@ prepare_socket(int family)
 
   switch (family) {
     case AF_INET:
+      my_addr_len = sizeof (my_addr.in4);
       my_addr.in4.sin_family = family;
       my_addr.in4.sin_port = htons(port_number);
 
@@ -177,6 +179,7 @@ prepare_socket(int family)
       break;
 #ifdef HAVE_IPV6
     case AF_INET6:
+      my_addr_len = sizeof (my_addr.in6);
       my_addr.in6.sin6_family = family;
       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);
 #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",
         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];
   int cmsglen;
   int sock_fd;
+  socklen_t addrlen;
 
   assert(initialised);
 
-  memset(&remote, 0, sizeof (remote));
   switch (remote_addr->ip_addr.family) {
     case IPADDR_INET4:
+      memset(&remote.in4, 0, sizeof (remote.in4));
+      addrlen = sizeof (remote.in4);
       remote.in4.sin_family = AF_INET;
       remote.in4.sin_port = htons(remote_addr->port);
       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;
 #ifdef HAVE_IPV6
     case IPADDR_INET6:
+      memset(&remote.in6, 0, sizeof (remote.in6));
+      addrlen = sizeof (remote.in6);
       remote.in6.sin6_family = AF_INET6;
       remote.in6.sin6_port = htons(remote_addr->port);
       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_len = packetlen;
   msg.msg_name = &remote.u;
-  msg.msg_namelen = sizeof(remote);
+  msg.msg_namelen = addrlen;
   msg.msg_iov = &iov;
   msg.msg_iovlen = 1;
   msg.msg_control = cmsgbuf;
@@ -449,6 +456,9 @@ send_packet(void *packet, int packetlen, NTP_Remote_Address *remote_addr)
 #endif
 
   msg.msg_controllen = cmsglen;
+  /* This is apparently required on some systems */
+  if (!cmsglen)
+    msg.msg_control = NULL;
 
   if (sendmsg(sock_fd, &msg, 0) < 0) {
     LOG(LOGS_WARN, LOGF_NtpIO, "Could not send to %s:%d : %s",