svn commit: r184683 - head/lib/libutil

Dag-Erling Smorgrav des at FreeBSD.org
Wed Nov 5 04:13:11 PST 2008


Author: des
Date: Wed Nov  5 12:13:10 2008
New Revision: 184683
URL: http://svn.freebsd.org/changeset/base/184683

Log:
  Like many other functions that handle sockaddrs, realhostname_sa() takes a
  struct sockaddr * that it casts internally to the appropriate type based on
  sa_family.  However, struct sockaddr has very lax alignment requirements,
  which causes the compiler to complain when you cast a struct sockaddr * to,
  say, a struct sockaddr_in6 *.
  
  I find it reasonable to assume that the pointer we received is in fact
  correctly aligned.  Therefore, we can work around the compiler warnings by
  casting to void * before casting to the desired type.  For readability's
  sake, this is done with macros.
  
  The same technique should prove useful in other parts of the tree that
  deal with socket addresses.
  
  MFC after:	3 weeks

Modified:
  head/lib/libutil/realhostname.c

Modified: head/lib/libutil/realhostname.c
==============================================================================
--- head/lib/libutil/realhostname.c	Wed Nov  5 12:02:25 2008	(r184682)
+++ head/lib/libutil/realhostname.c	Wed Nov  5 12:13:10 2008	(r184683)
@@ -83,6 +83,18 @@ realhostname(char *host, size_t hsize, c
 	return result;
 }
 
+/*
+ * struct sockaddr has very lax alignment requirements, since all its
+ * members are char or equivalent.  This is a problem when trying to
+ * dereference a struct sockaddr_in6 * that was passed in as a struct
+ * sockaddr *.  Although we know (or trust) that the passed-in struct was
+ * properly aligned, the compiler doesn't, and (rightly) complains.  These
+ * macros perform the cast in a way that the compiler will accept.
+ */
+#define SOCKADDR_IN6(p) ((struct sockaddr_in6 *)(void *)(p))
+#define SOCKADDR_IN(p) ((struct sockaddr_in *)(void *)(p))
+#define SOCKINET(p) ((struct sockinet *)(void *)(p))
+
 int
 realhostname_sa(char *host, size_t hsize, struct sockaddr *addr, int addrlen)
 {
@@ -96,10 +108,10 @@ realhostname_sa(char *host, size_t hsize
 	/* IPv4 mapped IPv6 addr consideraton, specified in rfc2373. */
 	if (addr->sa_family == AF_INET6 &&
 	    addrlen == sizeof(struct sockaddr_in6) &&
-	    IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)addr)->sin6_addr)) {
+	    IN6_IS_ADDR_V4MAPPED(&SOCKADDR_IN6(addr)->sin6_addr)) {
 		struct sockaddr_in6 *sin6;
 
-		sin6 = (struct sockaddr_in6 *)addr;
+		sin6 = SOCKADDR_IN6(addr);
 
 		memset(&lsin, 0, sizeof(lsin));
 		lsin.sin_len = sizeof(struct sockaddr_in);
@@ -142,15 +154,16 @@ realhostname_sa(char *host, size_t hsize
 			}
 			if (sa->sa_len == addrlen &&
 			    sa->sa_family == addr->sa_family) {
-				((struct sockinet *)sa)->si_port = ((struct sockinet *)addr)->si_port;
+				SOCKINET(sa)->si_port = SOCKINET(addr)->si_port;
 #ifdef INET6
 				/*
 				 * XXX: sin6_socpe_id may not been
 				 * filled by DNS
 				 */
 				if (sa->sa_family == AF_INET6 &&
-				    ((struct sockaddr_in6 *)sa)->sin6_scope_id == 0)
-					((struct sockaddr_in6 *)sa)->sin6_scope_id = ((struct sockaddr_in6 *)addr)->sin6_scope_id;
+				    SOCKADDR_IN6(sa)->sin6_scope_id == 0)
+					SOCKADDR_IN6(sa)->sin6_scope_id =
+					    SOCKADDR_IN6(addr)->sin6_scope_id;
 #endif
 				if (!memcmp(sa, addr, sa->sa_len)) {
 					result = HOSTNAME_FOUND;


More information about the svn-src-head mailing list