krb5 commit: Constify some socket-utils.h functions

ghudson at mit.edu ghudson at mit.edu
Wed Nov 27 18:04:37 EST 2024


https://github.com/krb5/krb5/commit/87b7d86676bff0b64a9eea39849abc97fdbe2972
commit 87b7d86676bff0b64a9eea39849abc97fdbe2972
Author: Greg Hudson <ghudson at mit.edu>
Date:   Mon Nov 18 18:45:16 2024 -0500

    Constify some socket-utils.h functions
    
    Accept constant pointers in sa_getport(), sa_is_inet(),
    sa_is_wildcard(), and sa_socklen().
    
    Make sa2sin() and sa2sin6() accept and return constant pointers.  For
    the most part we write to sockaddr_storage and read from sockaddr.
    The biggest exception is udppktinfo.c; adjust its internal APIs to use
    sockaddr_storage for output and inferred lengths for input.  All of
    the other exceptions use sa_setport().

 src/include/socket-utils.h    |  24 +++----
 src/kprop/kprop_util.c        |   4 +-
 src/lib/apputils/net-server.c |  28 +++-----
 src/lib/apputils/udppktinfo.c | 144 ++++++++++++++++++------------------------
 src/lib/apputils/udppktinfo.h |   7 +-
 src/lib/krb5/os/hostaddr.c    |   2 +-
 src/lib/krb5/os/localaddr.c   |  12 ++--
 7 files changed, 93 insertions(+), 128 deletions(-)

diff --git a/src/include/socket-utils.h b/src/include/socket-utils.h
index e1f33aa31..d379b37e1 100644
--- a/src/include/socket-utils.h
+++ b/src/include/socket-utils.h
@@ -66,15 +66,17 @@
  * "sockaddr_in *".
  *
  * The casts to (void *) are to get GCC to shut up about alignment
- * increasing.
+ * increasing.  We assume that struct sockaddr pointers are generally
+ * read-only; there are a few exceptions, but they all go through
+ * sa_setport().
  */
-static inline struct sockaddr_in *sa2sin (struct sockaddr *sa)
+static inline const struct sockaddr_in *sa2sin(const struct sockaddr *sa)
 {
-    return (struct sockaddr_in *) (void *) sa;
+    return (const struct sockaddr_in *)(void *)sa;
 }
-static inline struct sockaddr_in6 *sa2sin6 (struct sockaddr *sa)
+static inline const struct sockaddr_in6 *sa2sin6(const struct sockaddr *sa)
 {
-    return (struct sockaddr_in6 *) (void *) sa;
+    return (const struct sockaddr_in6 *)(void *)sa;
 }
 static inline struct sockaddr *ss2sa (struct sockaddr_storage *ss)
 {
@@ -95,14 +97,14 @@ static inline void
 sa_setport(struct sockaddr *sa, uint16_t port)
 {
     if (sa->sa_family == AF_INET)
-        sa2sin(sa)->sin_port = htons(port);
+        ((struct sockaddr_in *)sa2sin(sa))->sin_port = htons(port);
     else if (sa->sa_family == AF_INET6)
-        sa2sin6(sa)->sin6_port = htons(port);
+        ((struct sockaddr_in6 *)sa2sin6(sa))->sin6_port = htons(port);
 }
 
 /* Get the Internet port number of sa, or 0 if it is not an Internet socket. */
 static inline uint16_t
-sa_getport(struct sockaddr *sa)
+sa_getport(const struct sockaddr *sa)
 {
     if (sa->sa_family == AF_INET)
         return ntohs(sa2sin(sa)->sin_port);
@@ -114,14 +116,14 @@ sa_getport(struct sockaddr *sa)
 
 /* Return true if sa is an IPv4 or IPv6 socket address. */
 static inline int
-sa_is_inet(struct sockaddr *sa)
+sa_is_inet(const struct sockaddr *sa)
 {
     return sa->sa_family == AF_INET || sa->sa_family == AF_INET6;
 }
 
 /* Return true if sa is an IPv4 or IPv6 wildcard address. */
 static inline int
-sa_is_wildcard(struct sockaddr *sa)
+sa_is_wildcard(const struct sockaddr *sa)
 {
     if (sa->sa_family == AF_INET6)
         return IN6_IS_ADDR_UNSPECIFIED(&sa2sin6(sa)->sin6_addr);
@@ -133,7 +135,7 @@ sa_is_wildcard(struct sockaddr *sa)
 /* Return the length of an IPv4 or IPv6 socket structure; abort if it is
  * neither. */
 static inline socklen_t
-sa_socklen(struct sockaddr *sa)
+sa_socklen(const struct sockaddr *sa)
 {
     if (sa->sa_family == AF_INET6)
         return sizeof(struct sockaddr_in6);
diff --git a/src/kprop/kprop_util.c b/src/kprop/kprop_util.c
index c0aa2c89b..795c67648 100644
--- a/src/kprop/kprop_util.c
+++ b/src/kprop/kprop_util.c
@@ -45,12 +45,12 @@ sockaddr2krbaddr(krb5_context context, int family, struct sockaddr *sa,
 
     addr.magic = KV5M_ADDRESS;
     if (family == AF_INET) {
-        struct sockaddr_in *sa4 = sa2sin(sa);
+        const struct sockaddr_in *sa4 = sa2sin(sa);
         addr.addrtype = ADDRTYPE_INET;
         addr.length = sizeof(sa4->sin_addr);
         addr.contents = (krb5_octet *) &sa4->sin_addr;
     } else if (family == AF_INET6) {
-        struct sockaddr_in6 *sa6 = sa2sin6(sa);
+        const struct sockaddr_in6 *sa6 = sa2sin6(sa);
         if (IN6_IS_ADDR_V4MAPPED(&sa6->sin6_addr)) {
             addr.addrtype = ADDRTYPE_INET;
             addr.contents = (krb5_octet *) &sa6->sin6_addr + 12;
diff --git a/src/lib/apputils/net-server.c b/src/lib/apputils/net-server.c
index 60bd71bae..18a3392f9 100644
--- a/src/lib/apputils/net-server.c
+++ b/src/lib/apputils/net-server.c
@@ -963,8 +963,6 @@ struct udp_dispatch_state {
     krb5_fulladdr remote_addr;
     krb5_address local_addr_buf;
     krb5_fulladdr local_addr;
-    socklen_t saddr_len;
-    socklen_t daddr_len;
     struct sockaddr_storage saddr;
     struct sockaddr_storage daddr;
     aux_addressing_info auxaddr;
@@ -985,10 +983,8 @@ process_packet_response(void *arg, krb5_error_code code, krb5_data *response)
         goto out;
 
     cc = send_to_from(state->port_fd, response->data,
-                      (socklen_t) response->length, 0,
-                      (struct sockaddr *)&state->saddr, state->saddr_len,
-                      (struct sockaddr *)&state->daddr, state->daddr_len,
-                      &state->auxaddr);
+                      (socklen_t)response->length, 0, ss2sa(&state->saddr),
+                      ss2sa(&state->daddr), &state->auxaddr);
     if (cc == -1) {
         /* Note that the local address (daddr*) has no port number
          * info associated with it. */
@@ -996,13 +992,13 @@ process_packet_response(void *arg, krb5_error_code code, krb5_data *response)
         char daddrbuf[NI_MAXHOST];
         int e = errno;
 
-        if (getnameinfo((struct sockaddr *)&state->daddr, state->daddr_len,
+        if (getnameinfo(ss2sa(&state->daddr), sa_socklen(ss2sa(&state->daddr)),
                         daddrbuf, sizeof(daddrbuf), 0, 0,
                         NI_NUMERICHOST) != 0) {
             strlcpy(daddrbuf, "?", sizeof(daddrbuf));
         }
 
-        if (getnameinfo((struct sockaddr *)&state->saddr, state->saddr_len,
+        if (getnameinfo(ss2sa(&state->saddr), sa_socklen(ss2sa(&state->saddr)),
                         saddrbuf, sizeof(saddrbuf), sportbuf, sizeof(sportbuf),
                         NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
             strlcpy(saddrbuf, "?", sizeof(saddrbuf));
@@ -1029,6 +1025,7 @@ process_packet(verto_ctx *ctx, verto_ev *ev)
     int cc;
     struct connection *conn;
     struct udp_dispatch_state *state;
+    socklen_t slen;
 
     conn = verto_get_private(ev);
 
@@ -1043,13 +1040,9 @@ process_packet(verto_ctx *ctx, verto_ev *ev)
     state->port_fd = verto_get_fd(ev);
     assert(state->port_fd >= 0);
 
-    state->saddr_len = sizeof(state->saddr);
-    state->daddr_len = sizeof(state->daddr);
     memset(&state->auxaddr, 0, sizeof(state->auxaddr));
     cc = recv_from_to(state->port_fd, state->pktbuf, sizeof(state->pktbuf), 0,
-                      (struct sockaddr *)&state->saddr, &state->saddr_len,
-                      (struct sockaddr *)&state->daddr, &state->daddr_len,
-                      &state->auxaddr);
+                      &state->saddr, &state->daddr, &state->auxaddr);
     if (cc == -1) {
         if (errno != EINTR && errno != EAGAIN
             /*
@@ -1068,17 +1061,14 @@ process_packet(verto_ctx *ctx, verto_ev *ev)
         return;
     }
 
-    if (state->daddr_len == 0 && conn->type == CONN_UDP) {
+    if (state->daddr.ss_family == AF_UNSPEC && conn->type == CONN_UDP) {
         /*
          * An address couldn't be obtained, so the PKTINFO option probably
          * isn't available.  If the socket is bound to a specific address, then
          * try to get the address here.
          */
-        state->daddr_len = sizeof(state->daddr);
-        if (getsockname(state->port_fd, (struct sockaddr *)&state->daddr,
-                        &state->daddr_len) != 0)
-            state->daddr_len = 0;
-        /* On failure, keep going anyways. */
+        slen = sizeof(state->daddr);
+        (void)getsockname(state->port_fd, ss2sa(&state->daddr), &slen);
     }
 
     state->request.length = cc;
diff --git a/src/lib/apputils/udppktinfo.c b/src/lib/apputils/udppktinfo.c
index c6e86f693..8eed7a29d 100644
--- a/src/lib/apputils/udppktinfo.c
+++ b/src/lib/apputils/udppktinfo.c
@@ -167,20 +167,17 @@ cmsg2pktinfo(struct cmsghdr *cmsgptr)
 
 #define check_cmsg_v4_pktinfo check_cmsg_ip_pktinfo
 static int
-check_cmsg_ip_pktinfo(struct cmsghdr *cmsgptr, struct sockaddr *to,
-                      socklen_t *tolen, aux_addressing_info *auxaddr)
+check_cmsg_ip_pktinfo(struct cmsghdr *cmsgptr, struct sockaddr_in *to,
+                      aux_addressing_info *auxaddr)
 {
     struct in_pktinfo *pktinfo;
 
     if (cmsgptr->cmsg_level == IPPROTO_IP &&
-        cmsgptr->cmsg_type == IP_PKTINFO &&
-        *tolen >= sizeof(struct sockaddr_in)) {
-
-        memset(to, 0, sizeof(struct sockaddr_in));
+        cmsgptr->cmsg_type == IP_PKTINFO) {
+        memset(to, 0, sizeof(*to));
         pktinfo = cmsg2pktinfo(cmsgptr);
-        sa2sin(to)->sin_addr = pktinfo->ipi_addr;
-        sa2sin(to)->sin_family = AF_INET;
-        *tolen = sizeof(struct sockaddr_in);
+        to->sin_addr = pktinfo->ipi_addr;
+        to->sin_family = AF_INET;
         return 1;
     }
     return 0;
@@ -196,19 +193,17 @@ cmsg2sin(struct cmsghdr *cmsgptr)
 
 #define check_cmsg_v4_pktinfo check_cmsg_ip_recvdstaddr
 static int
-check_cmsg_ip_recvdstaddr(struct cmsghdr *cmsgptr, struct sockaddr *to,
-                          socklen_t *tolen, aux_addressing_info * auxaddr)
+check_cmsg_ip_recvdstaddr(struct cmsghdr *cmsgptr, struct sockaddr_in *to,
+                          aux_addressing_info *auxaddr)
 {
-    if (cmsgptr->cmsg_level == IPPROTO_IP &&
-        cmsgptr->cmsg_type == IP_RECVDSTADDR &&
-        *tolen >= sizeof(struct sockaddr_in)) {
-        struct in_addr *sin_addr;
+    struct in_addr *sin_addr;
 
-        memset(to, 0, sizeof(struct sockaddr_in));
+    if (cmsgptr->cmsg_level == IPPROTO_IP &&
+        cmsgptr->cmsg_type == IP_RECVDSTADDR) {
+        memset(to, 0, sizeof(*to));
         sin_addr = cmsg2sin(cmsgptr);
-        sa2sin(to)->sin_addr = *sin_addr;
-        sa2sin(to)->sin_family = AF_INET;
-        *tolen = sizeof(struct sockaddr_in);
+        to->sin_addr = *sin_addr;
+        to->sin_family = AF_INET;
         return 1;
     }
     return 0;
@@ -228,20 +223,17 @@ cmsg2pktinfo6(struct cmsghdr *cmsgptr)
 
 #define check_cmsg_v6_pktinfo check_cmsg_ipv6_pktinfo
 static int
-check_cmsg_ipv6_pktinfo(struct cmsghdr *cmsgptr, struct sockaddr *to,
-                        socklen_t *tolen, aux_addressing_info *auxaddr)
+check_cmsg_ipv6_pktinfo(struct cmsghdr *cmsgptr, struct sockaddr_in6 *to,
+                        aux_addressing_info *auxaddr)
 {
     struct in6_pktinfo *pktinfo;
 
     if (cmsgptr->cmsg_level == IPPROTO_IPV6 &&
-        cmsgptr->cmsg_type == IPV6_PKTINFO &&
-        *tolen >= sizeof(struct sockaddr_in6)) {
-
-        memset(to, 0, sizeof(struct sockaddr_in6));
+        cmsgptr->cmsg_type == IPV6_PKTINFO) {
+        memset(to, 0, sizeof(*to));
         pktinfo = cmsg2pktinfo6(cmsgptr);
-        sa2sin6(to)->sin6_addr = pktinfo->ipi6_addr;
-        sa2sin6(to)->sin6_family = AF_INET6;
-        *tolen = sizeof(struct sockaddr_in6);
+        to->sin6_addr = pktinfo->ipi6_addr;
+        to->sin6_family = AF_INET6;
         auxaddr->ipv6_ifindex = pktinfo->ipi6_ifindex;
         return 1;
     }
@@ -252,11 +244,11 @@ check_cmsg_ipv6_pktinfo(struct cmsghdr *cmsgptr, struct sockaddr *to,
 #endif /* HAVE_IPV6_PKTINFO */
 
 static int
-check_cmsg_pktinfo(struct cmsghdr *cmsgptr, struct sockaddr *to,
-                   socklen_t *tolen, aux_addressing_info *auxaddr)
+check_cmsg_pktinfo(struct cmsghdr *cmsgptr, struct sockaddr_storage *to,
+                   aux_addressing_info *auxaddr)
 {
-    return check_cmsg_v4_pktinfo(cmsgptr, to, tolen, auxaddr) ||
-           check_cmsg_v6_pktinfo(cmsgptr, to, tolen, auxaddr);
+    return check_cmsg_v4_pktinfo(cmsgptr, ss2sin(to), auxaddr) ||
+        check_cmsg_v6_pktinfo(cmsgptr, ss2sin6(to), auxaddr);
 }
 
 /*
@@ -268,19 +260,16 @@ check_cmsg_pktinfo(struct cmsghdr *cmsgptr, struct sockaddr *to,
  *  len     - buf length
  *  flags
  *  from    - Set to the address that sent the message
- *  fromlen
  *  to      - Set to the address that the message was sent to if possible.
  *            May not be set in certain cases such as if pktinfo support is
  *            missing. May be NULL.
- *  tolen
  *  auxaddr - Miscellaneous address information.
  *
  * Returns 0 on success, otherwise an error code.
  */
 krb5_error_code
 recv_from_to(int sock, void *buf, size_t len, int flags,
-             struct sockaddr *from, socklen_t * fromlen,
-             struct sockaddr *to, socklen_t * tolen,
+             struct sockaddr_storage *from, struct sockaddr_storage *to,
              aux_addressing_info *auxaddr)
 
 {
@@ -289,24 +278,26 @@ recv_from_to(int sock, void *buf, size_t len, int flags,
     char cmsg[CMSG_SPACE(sizeof(union pktinfo))];
     struct cmsghdr *cmsgptr;
     struct msghdr msg;
+    socklen_t fromlen = sizeof(*from);
 
     /* Don't use pktinfo if the socket isn't bound to a wildcard address. */
     r = is_socket_bound_to_wildcard(sock);
     if (r < 0)
         return errno;
 
-    if (!to || !tolen || !r)
-        return recvfrom(sock, buf, len, flags, from, fromlen);
+    if (to == NULL || !r)
+        return recvfrom(sock, buf, len, flags, ss2sa(from), &fromlen);
 
     /* Clobber with something recognizable in case we can't extract the address
      * but try to use it anyways. */
-    memset(to, 0x40, *tolen);
+    memset(to, 0x40, sizeof(*to));
+    to->ss_family = AF_UNSPEC;
 
     iov.iov_base = buf;
     iov.iov_len = len;
     memset(&msg, 0, sizeof(msg));
-    msg.msg_name = from;
-    msg.msg_namelen = *fromlen;
+    msg.msg_name = ss2sa(from);
+    msg.msg_namelen = sizeof(*from);
     msg.msg_iov = &iov;
     msg.msg_iovlen = 1;
     msg.msg_control = cmsg;
@@ -315,7 +306,6 @@ recv_from_to(int sock, void *buf, size_t len, int flags,
     r = recvmsg(sock, &msg, flags);
     if (r < 0)
         return r;
-    *fromlen = msg.msg_namelen;
 
     /*
      * On Darwin (and presumably all *BSD with KAME stacks), CMSG_FIRSTHDR
@@ -326,13 +316,12 @@ recv_from_to(int sock, void *buf, size_t len, int flags,
     if (msg.msg_controllen) {
         cmsgptr = CMSG_FIRSTHDR(&msg);
         while (cmsgptr) {
-            if (check_cmsg_pktinfo(cmsgptr, to, tolen, auxaddr))
+            if (check_cmsg_pktinfo(cmsgptr, to, auxaddr))
                 return r;
             cmsgptr = CMSG_NXTHDR(&msg, cmsgptr);
         }
     }
     /* No info about destination addr was available.  */
-    *tolen = 0;
     return r;
 }
 
@@ -341,18 +330,15 @@ recv_from_to(int sock, void *buf, size_t len, int flags,
 #define set_msg_from_ipv4 set_msg_from_ip_pktinfo
 static krb5_error_code
 set_msg_from_ip_pktinfo(struct msghdr *msg, struct cmsghdr *cmsgptr,
-                        struct sockaddr *from, socklen_t fromlen,
+                        const struct sockaddr_in *from,
                         aux_addressing_info *auxaddr)
 {
     struct in_pktinfo *p = cmsg2pktinfo(cmsgptr);
-    const struct sockaddr_in *from4 = sa2sin(from);
 
-    if (fromlen != sizeof(struct sockaddr_in))
-        return EINVAL;
     cmsgptr->cmsg_level = IPPROTO_IP;
     cmsgptr->cmsg_type = IP_PKTINFO;
     cmsgptr->cmsg_len = CMSG_LEN(sizeof(struct in_pktinfo));
-    p->ipi_spec_dst = from4->sin_addr;
+    p->ipi_spec_dst = from->sin_addr;
 
     msg->msg_controllen = CMSG_SPACE(sizeof(struct in_pktinfo));
     return 0;
@@ -363,18 +349,16 @@ set_msg_from_ip_pktinfo(struct msghdr *msg, struct cmsghdr *cmsgptr,
 #define set_msg_from_ipv4 set_msg_from_ip_sendsrcaddr
 static krb5_error_code
 set_msg_from_ip_sendsrcaddr(struct msghdr *msg, struct cmsghdr *cmsgptr,
-                            struct sockaddr *from, socklen_t fromlen,
+                            const struct sockaddr_in *from,
                             aux_addressing_info *auxaddr)
 {
     struct in_addr *sin_addr = cmsg2sin(cmsgptr);
-    const struct sockaddr_in *from4 = sa2sin(from);
-    if (fromlen != sizeof(struct sockaddr_in))
-        return EINVAL;
+
     cmsgptr->cmsg_level = IPPROTO_IP;
     cmsgptr->cmsg_type = IP_SENDSRCADDR;
     cmsgptr->cmsg_len = CMSG_LEN(sizeof(struct in_addr));
     msg->msg_controllen = CMSG_SPACE(sizeof(struct in_addr));
-    *sin_addr = from4->sin_addr;
+    *sin_addr = from->sin_addr;
     return 0;
 }
 
@@ -387,19 +371,16 @@ set_msg_from_ip_sendsrcaddr(struct msghdr *msg, struct cmsghdr *cmsgptr,
 #define set_msg_from_ipv6 set_msg_from_ipv6_pktinfo
 static krb5_error_code
 set_msg_from_ipv6_pktinfo(struct msghdr *msg, struct cmsghdr *cmsgptr,
-                          struct sockaddr *from, socklen_t fromlen,
+                          const struct sockaddr_in6 *from,
                           aux_addressing_info *auxaddr)
 {
     struct in6_pktinfo *p = cmsg2pktinfo6(cmsgptr);
-    const struct sockaddr_in6 *from6 = sa2sin6(from);
 
-    if (fromlen != sizeof(struct sockaddr_in6))
-        return EINVAL;
     cmsgptr->cmsg_level = IPPROTO_IPV6;
     cmsgptr->cmsg_type = IPV6_PKTINFO;
     cmsgptr->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
 
-    p->ipi6_addr = from6->sin6_addr;
+    p->ipi6_addr = from->sin6_addr;
     /*
      * Because of the possibility of asymmetric routing, we
      * normally don't want to specify an interface.  However,
@@ -408,7 +389,7 @@ set_msg_from_ipv6_pktinfo(struct msghdr *msg, struct cmsghdr *cmsgptr,
      * with a "foo.local" name) unless we do specify the
      * interface.
      */
-    if (IN6_IS_ADDR_LINKLOCAL(&from6->sin6_addr))
+    if (IN6_IS_ADDR_LINKLOCAL(&from->sin6_addr))
         p->ipi6_ifindex = auxaddr->ipv6_ifindex;
     /* otherwise, already zero */
 
@@ -421,15 +402,14 @@ set_msg_from_ipv6_pktinfo(struct msghdr *msg, struct cmsghdr *cmsgptr,
 #endif /* HAVE_IPV6_PKTINFO */
 
 static krb5_error_code
-set_msg_from(int family, struct msghdr *msg, struct cmsghdr *cmsgptr,
-             struct sockaddr *from, socklen_t fromlen,
-             aux_addressing_info *auxaddr)
+set_msg_from(struct msghdr *msg, struct cmsghdr *cmsgptr,
+             const struct sockaddr *from, aux_addressing_info *auxaddr)
 {
-    switch (family) {
+    switch (from->sa_family) {
     case AF_INET:
-        return set_msg_from_ipv4(msg, cmsgptr, from, fromlen, auxaddr);
+        return set_msg_from_ipv4(msg, cmsgptr, sa2sin(from), auxaddr);
     case AF_INET6:
-        return set_msg_from_ipv6(msg, cmsgptr, from, fromlen, auxaddr);
+        return set_msg_from_ipv6(msg, cmsgptr, sa2sin6(from), auxaddr);
     }
 
     return EINVAL;
@@ -444,17 +424,15 @@ set_msg_from(int family, struct msghdr *msg, struct cmsghdr *cmsgptr,
  *  len     - buf length
  *  flags
  *  to      - The address to send the message to.
- *  tolen
  *  from    - The address to attempt to send the message from. May be NULL.
- *  fromlen
  *  auxaddr - Miscellaneous address information.
  *
  * Returns 0 on success, otherwise an error code.
  */
 krb5_error_code
 send_to_from(int sock, void *buf, size_t len, int flags,
-             const struct sockaddr *to, socklen_t tolen, struct sockaddr *from,
-             socklen_t fromlen, aux_addressing_info *auxaddr)
+             const struct sockaddr *to, const struct sockaddr *from,
+             aux_addressing_info *auxaddr)
 {
     int r;
     struct iovec iov;
@@ -467,7 +445,7 @@ send_to_from(int sock, void *buf, size_t len, int flags,
     if (r < 0)
         return errno;
 
-    if (from == NULL || fromlen == 0 || from->sa_family != to->sa_family || !r)
+    if (from == NULL || from->sa_family != to->sa_family || !r)
         goto use_sendto;
 
     iov.iov_base = buf;
@@ -478,7 +456,7 @@ send_to_from(int sock, void *buf, size_t len, int flags,
     memset(cbuf, 0, sizeof(cbuf));
     memset(&msg, 0, sizeof(msg));
     msg.msg_name = (void *)to;
-    msg.msg_namelen = tolen;
+    msg.msg_namelen = sa_socklen(to);
     msg.msg_iov = &iov;
     msg.msg_iovlen = 1;
     msg.msg_control = cbuf;
@@ -488,39 +466,39 @@ send_to_from(int sock, void *buf, size_t len, int flags,
     cmsgptr = CMSG_FIRSTHDR(&msg);
     msg.msg_controllen = 0;
 
-    if (set_msg_from(from->sa_family, &msg, cmsgptr, from, fromlen, auxaddr))
+    if (set_msg_from(&msg, cmsgptr, from, auxaddr))
         goto use_sendto;
     return sendmsg(sock, &msg, flags);
 
 use_sendto:
-    return sendto(sock, buf, len, flags, to, tolen);
+    return sendto(sock, buf, len, flags, to, sa_socklen(to));
 }
 
 #else /* HAVE_PKTINFO_SUPPORT && CMSG_SPACE */
 
 krb5_error_code
 recv_from_to(int sock, void *buf, size_t len, int flags,
-             struct sockaddr *from, socklen_t *fromlen,
-             struct sockaddr *to, socklen_t *tolen,
+             struct sockaddr_storage *from, struct sockaddr_storage *to,
              aux_addressing_info *auxaddr)
 {
-    if (to && tolen) {
+    socklen_t fromlen = sizeof(*from);
+
+    if (to != NULL) {
         /* Clobber with something recognizable in case we try to use the
          * address. */
-        memset(to, 0x40, *tolen);
-        *tolen = 0;
+        memset(to, 0x40, sizeof(*to));
+        to->ss_family = AF_UNSPEC;
     }
 
-    return recvfrom(sock, buf, len, flags, from, fromlen);
+    return recvfrom(sock, buf, len, flags, ss2sa(from), &fromlen);
 }
 
 krb5_error_code
 send_to_from(int sock, void *buf, size_t len, int flags,
-             const struct sockaddr *to, socklen_t tolen,
-             struct sockaddr *from, socklen_t fromlen,
+             const struct sockaddr *to, const struct sockaddr *from,
              aux_addressing_info *auxaddr)
 {
-    return sendto(sock, buf, len, flags, to, tolen);
+    return sendto(sock, buf, len, flags, to, sa_socklen(to));
 }
 
 #endif /* HAVE_PKTINFO_SUPPORT && CMSG_SPACE */
diff --git a/src/lib/apputils/udppktinfo.h b/src/lib/apputils/udppktinfo.h
index ff5759ab7..5ece86ab1 100644
--- a/src/lib/apputils/udppktinfo.h
+++ b/src/lib/apputils/udppktinfo.h
@@ -46,13 +46,12 @@ set_pktinfo(int sock, int family);
 
 krb5_error_code
 recv_from_to(int sock, void *buf, size_t len, int flags,
-             struct sockaddr *from, socklen_t *fromlen,
-             struct sockaddr *to, socklen_t *tolen,
+             struct sockaddr_storage *from, struct sockaddr_storage *to,
              aux_addressing_info *auxaddr);
 
 krb5_error_code
 send_to_from(int sock, void *buf, size_t len, int flags,
-             const struct sockaddr *to, socklen_t tolen, struct sockaddr *from,
-             socklen_t fromlen, aux_addressing_info *auxaddr);
+             const struct sockaddr *to, const struct sockaddr *from,
+             aux_addressing_info *auxaddr);
 
 #endif /* UDPPKTINFO_H */
diff --git a/src/lib/krb5/os/hostaddr.c b/src/lib/krb5/os/hostaddr.c
index 8be861e65..b98d5acdc 100644
--- a/src/lib/krb5/os/hostaddr.c
+++ b/src/lib/krb5/os/hostaddr.c
@@ -77,7 +77,7 @@ k5_os_hostaddr(krb5_context context, const char *name,
         addrs[j] = 0;
 
     for (i = 0, aip = ai; aip; aip = aip->ai_next) {
-        void *ptr;
+        const void *ptr;
         size_t addrlen;
         int atype;
 
diff --git a/src/lib/krb5/os/localaddr.c b/src/lib/krb5/os/localaddr.c
index 46d2b2d4f..4c7343498 100644
--- a/src/lib/krb5/os/localaddr.c
+++ b/src/lib/krb5/os/localaddr.c
@@ -180,14 +180,10 @@ static int
 is_loopback_address(struct sockaddr *sa)
 {
     switch (sa->sa_family) {
-    case AF_INET: {
-        struct sockaddr_in *s4 = sa2sin(sa);
-        return s4->sin_addr.s_addr == htonl(INADDR_LOOPBACK);
-    }
-    case AF_INET6: {
-        struct sockaddr_in6 *s6 = sa2sin6(sa);
-        return IN6_IS_ADDR_LOOPBACK(&s6->sin6_addr);
-    }
+    case AF_INET:
+        return sa2sin(sa)->sin_addr.s_addr == htonl(INADDR_LOOPBACK);
+    case AF_INET6:
+        return IN6_IS_ADDR_LOOPBACK(&sa2sin6(sa)->sin6_addr);
     default:
         return 0;
     }


More information about the cvs-krb5 mailing list