krb5 commit: Don't depend on sa_len sockaddr field

Greg Hudson ghudson at MIT.EDU
Fri Jul 4 15:21:22 EDT 2014


https://github.com/krb5/krb5/commit/1fde9a67030845d3cae0661595f5737a4db8b9dd
commit 1fde9a67030845d3cae0661595f5737a4db8b9dd
Author: Greg Hudson <ghudson at mit.edu>
Date:   Thu Jul 3 12:17:25 2014 -0400

    Don't depend on sa_len sockaddr field
    
    In socket-utils.h, replace the socklen macro with an inline function
    sa_socklen which always uses the address family, even on platforms
    with the sa_len sockaddr field.  This removes the need to set sa_len
    in socket addresses we construct.

 src/clients/klist/klist.c     |    2 +-
 src/include/socket-utils.h    |   20 ++++++++++++--------
 src/lib/apputils/net-server.c |    4 ++--
 src/lib/krb5/os/localaddr.c   |    6 +++---
 src/lib/rpc/bindresvport.c    |    2 +-
 src/lib/rpc/svc_tcp.c         |    2 +-
 src/lib/rpc/svc_udp.c         |    2 +-
 7 files changed, 21 insertions(+), 17 deletions(-)

diff --git a/src/clients/klist/klist.c b/src/clients/klist/klist.c
index 15e75bb..9388d14 100644
--- a/src/clients/klist/klist.c
+++ b/src/clients/klist/klist.c
@@ -846,7 +846,7 @@ void one_addr(a)
     }
 
     namebuf[0] = 0;
-    err = getnameinfo (ss2sa (&ss), socklen (ss2sa (&ss)),
+    err = getnameinfo (ss2sa (&ss), sa_socklen (ss2sa (&ss)),
                        namebuf, sizeof (namebuf), 0, 0,
                        no_resolve ? NI_NUMERICHOST : 0U);
     if (err) {
diff --git a/src/include/socket-utils.h b/src/include/socket-utils.h
index e3fa429..1566636 100644
--- a/src/include/socket-utils.h
+++ b/src/include/socket-utils.h
@@ -119,13 +119,17 @@ sa_is_inet(struct sockaddr *sa)
     return sa->sa_family == AF_INET || sa->sa_family == AF_INET6;
 }
 
-#if !defined (socklen)
-/* socklen_t socklen (struct sockaddr *) */
-#  ifdef HAVE_SA_LEN
-#    define socklen(X) ((X)->sa_len)
-#  else
-#    define socklen(X) ((X)->sa_family == AF_INET6 ? (socklen_t) sizeof (struct sockaddr_in6) : (X)->sa_family == AF_INET ? (socklen_t) sizeof (struct sockaddr_in) : (socklen_t) sizeof (struct sockaddr))
-#  endif
-#endif
+/* Return the length of an IPv4 or IPv6 socket structure; abort if it is
+ * neither. */
+static inline socklen_t
+sa_socklen(struct sockaddr *sa)
+{
+    if (sa->sa_family == AF_INET6)
+        return sizeof(struct sockaddr_in6);
+    else if (sa->sa_family == AF_INET)
+        return sizeof(struct sockaddr_in);
+    else
+        abort();
+}
 
 #endif /* SOCKET_UTILS_H */
diff --git a/src/lib/apputils/net-server.c b/src/lib/apputils/net-server.c
index 2dd435b..558eee3 100644
--- a/src/lib/apputils/net-server.c
+++ b/src/lib/apputils/net-server.c
@@ -139,7 +139,7 @@ paddr(struct sockaddr *sa)
 {
     static char buf[100];
     char portbuf[10];
-    if (getnameinfo(sa, socklen(sa),
+    if (getnameinfo(sa, sa_socklen(sa),
                     buf, sizeof(buf), portbuf, sizeof(portbuf),
                     NI_NUMERICHOST|NI_NUMERICSERV))
         strlcpy(buf, "<unprintable>", sizeof(buf));
@@ -592,7 +592,7 @@ create_server_socket(struct socksetup *data, struct sockaddr *addr, int type)
 #endif /* IPV6_V6ONLY */
     }
 
-    if (bind(sock, addr, socklen(addr)) == -1) {
+    if (bind(sock, addr, sa_socklen(addr)) == -1) {
         data->retval = errno;
         com_err(data->prog, errno, _("Cannot bind server socket on %s"),
                 paddr(addr));
diff --git a/src/lib/krb5/os/localaddr.c b/src/lib/krb5/os/localaddr.c
index f7eb2d2..0e6a05c 100644
--- a/src/lib/krb5/os/localaddr.c
+++ b/src/lib/krb5/os/localaddr.c
@@ -165,7 +165,7 @@ printaddr(struct sockaddr *sa)
     int err;
 
     printf ("%p ", (void *) sa);
-    err = getnameinfo (sa, socklen (sa), buf, sizeof (buf), 0, 0,
+    err = getnameinfo (sa, sa_socklen (sa), buf, sizeof (buf), 0, 0,
                        NI_NUMERICHOST);
     if (err)
         printf ("<getnameinfo error %d: %s> family=%d",
@@ -1085,7 +1085,7 @@ static int print_addr (/*@unused@*/ void *dataptr, struct sockaddr *sa)
     socklen_t len;
 
     printf ("  --> family %2d ", sa->sa_family);
-    len = socklen (sa);
+    len = sa_socklen (sa);
     err = getnameinfo (sa, len, hostbuf, (socklen_t) sizeof (hostbuf),
                        (char *) NULL, 0, NI_NUMERICHOST);
     if (err) {
@@ -1423,7 +1423,7 @@ get_localaddrs (krb5_context context, krb5_address ***addr, int use_profile)
             }
             if (addrp)
                 memcpy (addrp, addr[0][j]->contents, addr[0][j]->length);
-            err2 = getnameinfo (ss2sa(&ss), socklen (ss2sa (&ss)),
+            err2 = getnameinfo (ss2sa(&ss), sa_socklen (ss2sa (&ss)),
                                 namebuf, sizeof (namebuf), 0, 0,
                                 NI_NUMERICHOST);
             if (err2 == 0)
diff --git a/src/lib/rpc/bindresvport.c b/src/lib/rpc/bindresvport.c
index fa28b60..ba11e0a 100644
--- a/src/lib/rpc/bindresvport.c
+++ b/src/lib/rpc/bindresvport.c
@@ -81,7 +81,7 @@ bindresvport_sa(int sd, struct sockaddr *sa)
 		if (port > ENDPORT) {
 			port = STARTPORT;
 		}
-		res = bind(sd, sa, socklen(sa));
+		res = bind(sd, sa, sa_socklen(sa));
 	}
 	return (res);
 }
diff --git a/src/lib/rpc/svc_tcp.c b/src/lib/rpc/svc_tcp.c
index 7b69c33..3d02164 100644
--- a/src/lib/rpc/svc_tcp.c
+++ b/src/lib/rpc/svc_tcp.c
@@ -174,7 +174,7 @@ svctcp_create(
 
 	if (bindresvport_sa(sock, sa)) {
 		sa_setport(sa, 0);
-		(void)bind(sock, sa, socklen(sa));
+		(void)bind(sock, sa, sa_socklen(sa));
 	}
 	len = sizeof(struct sockaddr_storage);
 	if (getsockname(sock, sa, &len) != 0) {
diff --git a/src/lib/rpc/svc_udp.c b/src/lib/rpc/svc_udp.c
index e9f1c3d..603f56a 100644
--- a/src/lib/rpc/svc_udp.c
+++ b/src/lib/rpc/svc_udp.c
@@ -145,7 +145,7 @@ svcudp_bufcreate(
 
 	if (bindresvport_sa(sock, sa)) {
 		sa_setport(sa, 0);
-		(void)bind(sock, sa, socklen(sa));
+		(void)bind(sock, sa, sa_socklen(sa));
 	}
 	len = sizeof(struct sockaddr_storage);
 	if (getsockname(sock, sa, &len) != 0) {


More information about the cvs-krb5 mailing list