[Git][NTPsec/ntpsec][master] Hide elements of sockaddr_u behind accessor/mutator macros.

Eric S. Raymond gitlab at mg.gitlab.com
Sat Jan 7 18:48:45 UTC 2017


Eric S. Raymond pushed to branch master at NTPsec / ntpsec


Commits:
e6f41156 by Eric S. Raymond at 2017-01-07T13:48:22-05:00
Hide elements of sockaddr_u behind accessor/mutator macros.

This is preparatory to getting rid of the union in sockaddr_u.

- - - - -


9 changed files:

- include/ntp_net.h
- ntpd/ntp_control.c
- ntpd/ntp_io.c
- tests/common/sockaddrtest.c
- tests/libntp/a_md5encrypt.c
- tests/libntp/decodenetnum.c
- tests/libntp/netof.c
- tests/libntp/socktoa.c
- tests/ntpd/restrict.c


Changes:

=====================================
include/ntp_net.h
=====================================
--- a/include/ntp_net.h
+++ b/include/ntp_net.h
@@ -24,24 +24,29 @@ typedef union {
  * Utilities for manipulating sockaddr_u v4/v6 unions
  */
 #define SOCK_ADDR4(psau)	((psau)->sa4.sin_addr)
+#define SET_SOCK_ADDR4(psau, a)	((psau)->sa4.sin_addr = (a))
 #define SOCK_ADDR6(psau)	((psau)->sa6.sin6_addr)
+#define SET_SOCK_ADDR6(psau, a)	((psau)->sa6.sin6_addr = (a))
 
 #define PSOCK_ADDR4(psau)	(&SOCK_ADDR4(psau))
 #define PSOCK_ADDR6(psau)	(&SOCK_ADDR6(psau))
 
 #define AF(psau)		((psau)->sa.sa_family)
+#define SET_AF(psau, f)		((psau)->sa.sa_family = (f))
 
 #define IS_IPV4(psau)		(AF_INET == AF(psau))
 #define IS_IPV6(psau)		(AF_INET6 == AF(psau))
 
 /* sockaddr_u v4 address in network byte order */
 #define	NSRCADR(psau)		(SOCK_ADDR4(psau).s_addr)
+#define	SET_NSRCADR(psau, a)	(SOCK_ADDR4(psau).s_addr - (a))
 
 /* sockaddr_u v4 address in host byte order */
 #define	SRCADR(psau)		(ntohl(NSRCADR(psau)))
 
 /* sockaddr_u v6 address in network byte order */
 #define NSRCADR6(psau)		(SOCK_ADDR6(psau).s6_addr)
+#define SET_NSRCADR6(psau)	(SOCK_ADDR6(psau).s6_addr = (a))
 
 /* assign sockaddr_u v4 address from host byte order */
 #define	SET_ADDR4(psau, addr4)	(NSRCADR(psau) = htonl(addr4))
@@ -50,11 +55,11 @@ typedef union {
 #define SET_ADDR4N(psau, addr4n) (NSRCADR(psau) = (addr4n));
 
 /* assign sockaddr_u v6 address from network byte order */
-#define SET_ADDR6N(psau, s6_addr)				\
-	(SOCK_ADDR6(psau) = (s6_addr))
+#define SET_ADDR6N(psau, s6_addr)	(SOCK_ADDR6(psau) = (s6_addr))
 
 /* sockaddr_u v4/v6 port in network byte order */
 #define	NSRCPORT(psau)		((psau)->sa4.sin_port)
+#define	SET_NSRCPORT(psau, a)	((psau)->sa4.sin_port = (a))
 
 /* sockaddr_u v4/v6 port in host byte order */
 #define	SRCPORT(psau)		(ntohs(NSRCPORT(psau)))


=====================================
ntpd/ntp_control.c
=====================================
--- a/ntpd/ntp_control.c
+++ b/ntpd/ntp_control.c
@@ -3765,17 +3765,17 @@ sockaddrs_from_restrict_u(
 	ZERO(*psaA);
 	ZERO(*psaM);
 	if (!ipv6) {
-		psaA->sa.sa_family = AF_INET;
-		psaA->sa4.sin_addr.s_addr = htonl(pres->u.v4.addr);
-		psaM->sa.sa_family = AF_INET;
-		psaM->sa4.sin_addr.s_addr = htonl(pres->u.v4.mask);
+		SET_AF(psaA, AF_INET);
+		PSOCK_ADDR4(psaA)->s_addr = htonl(pres->u.v4.addr);
+		SET_AF(psaM, AF_INET);
+		PSOCK_ADDR4(psaM)->s_addr = htonl(pres->u.v4.mask);
 	} else {
-		psaA->sa.sa_family = AF_INET6;
-		memcpy(&psaA->sa6.sin6_addr, &pres->u.v6.addr,
-		       sizeof(psaA->sa6.sin6_addr));
-		psaM->sa.sa_family = AF_INET6;
-		memcpy(&psaM->sa6.sin6_addr, &pres->u.v6.mask,
-		       sizeof(psaA->sa6.sin6_addr));
+		SET_AF(psaA, AF_INET6);
+		memcpy(&SOCK_ADDR6(psaA), &pres->u.v6.addr,
+		       sizeof(SOCK_ADDR6(psaA)));
+		SET_AF(psaM, AF_INET6);
+		memcpy(&SOCK_ADDR6(psaM), &pres->u.v6.mask,
+		       sizeof(SOCK_ADDR6(psaA)));
 	}
 }
 


=====================================
ntpd/ntp_io.c
=====================================
--- a/ntpd/ntp_io.c
+++ b/ntpd/ntp_io.c
@@ -1284,13 +1284,13 @@ sau_from_netaddr(
 	switch (pna->family) {
 
 	case AF_INET:
-		memcpy(&psau->sa4.sin_addr, &pna->type.in,
-		       sizeof(psau->sa4.sin_addr));
+		memcpy(&SOCK_ADDR4(psau), &pna->type.in,
+		       sizeof(SOCK_ADDR4(psau)));
 		break;
 
 	case AF_INET6:
-		memcpy(&psau->sa6.sin6_addr, &pna->type.in6,
-		       sizeof(psau->sa6.sin6_addr));
+		memcpy(&SOCK_ADDR6(psau), &pna->type.in6,
+		       sizeof(SOCK_ADDR6(psau)));
 		break;
 	}
 }
@@ -1354,7 +1354,7 @@ check_flags6(
 	struct in6_ifreq ifr6;
 	int fd;
 
-	if (psau->sa.sa_family != AF_INET6)
+	if (AF(psau) != AF_INET6)
 		return false;
 	if ((fd = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
 		return false;


=====================================
tests/common/sockaddrtest.c
=====================================
--- a/tests/common/sockaddrtest.c
+++ b/tests/common/sockaddrtest.c
@@ -8,24 +8,24 @@
 #include "ntp.h"
 
 bool IsEqualS(const sockaddr_u *expected, const sockaddr_u *actual) {
-	if (expected->sa.sa_family != actual->sa.sa_family) {
+	if (AF(expected) != AF(actual)) {
 		printf("Expected sa_family: %" PRIuMAX " but got: %" PRIuMAX "\n",
-		       (uintmax_t)expected->sa.sa_family, (uintmax_t)actual->sa.sa_family);
+		       (uintmax_t)AF(expected), (uintmax_t)AF(actual));
 		return false;
 	}
 
-	if (actual->sa.sa_family == AF_INET) { // IPv4
-		if (expected->sa4.sin_port == actual->sa4.sin_port &&
-			memcmp(&expected->sa4.sin_addr, &actual->sa4.sin_addr,
+	if (AF(actual) == AF_INET) { // IPv4
+		if (NSRCPORT(expected) == NSRCPORT(actual) &&
+		    memcmp(&SOCK_ADDR4(expected), &SOCK_ADDR4(actual),
 				   sizeof(in_addr_t)) == 0) {
 			return true;
 		} else {
-			printf("IPv4 comparison failed, expected: %u (%s) but was: %u (%s)\n", expected->sa4.sin_addr.s_addr, socktoa(expected), actual->sa4.sin_addr.s_addr, socktoa(actual));
+			printf("IPv4 comparison failed, expected: %u (%s) but was: %u (%s)\n", SOCK_ADDR4(expected).s_addr, socktoa(expected), SOCK_ADDR4(actual).s_addr, socktoa(actual));
 			return false;
 		}
-	} else if (actual->sa.sa_family == AF_INET6) { //IPv6
+	} else if (AF(actual) == AF_INET6) { //IPv6
 		if (expected->sa6.sin6_port == actual->sa6.sin6_port &&
-			memcmp(&expected->sa6.sin6_addr, &actual->sa6.sin6_addr,
+			memcmp(&SOCK_ADDR6(expected), &SOCK_ADDR6(actual),
 				   sizeof(struct in6_addr)) == 0) {
 			return true;
 		} else {
@@ -33,29 +33,29 @@ bool IsEqualS(const sockaddr_u *expected, const sockaddr_u *actual) {
 			return false;
 		}
 	} else { // Unknown family
-		printf("Unknown sa_family: %" PRIuMAX "\n", (uintmax_t)actual->sa.sa_family);
+		printf("Unknown sa_family: %" PRIuMAX "\n", (uintmax_t)AF(actual));
 		return false;
 	}
 }
 
 /* Similar to IsEqualS, but doesn't print misleading messages */
 bool IsDiffS(const sockaddr_u *expected, const sockaddr_u *actual) {
-	if (expected->sa.sa_family != actual->sa.sa_family) {
+	if (AF(expected) != AF(actual)) {
 		return true;
 	}
 
-	if (actual->sa.sa_family == AF_INET) { // IPv4
-		if (expected->sa4.sin_port == actual->sa4.sin_port &&
-			memcmp(&expected->sa4.sin_addr, &actual->sa4.sin_addr,
+	if (AF(actual) == AF_INET) { // IPv4
+		if (NSRCPORT(expected) == NSRCPORT(actual) &&
+			memcmp(&SOCK_ADDR4(expected), &SOCK_ADDR4(actual),
 				   sizeof(in_addr_t)) == 0) {
-			printf("IPv4 address matches: %u (%s)\n", expected->sa4.sin_addr.s_addr, socktoa(expected));
+			printf("IPv4 address matches: %u (%s)\n", SOCK_ADDR4(expected).s_addr, socktoa(expected));
 			return false;
 		} else {
 			return true;
 		}
-	} else if (actual->sa.sa_family == AF_INET6) { //IPv6
+	} else if (AF(actual) == AF_INET6) { //IPv6
 		if (expected->sa6.sin6_port == actual->sa6.sin6_port &&
-			memcmp(&expected->sa6.sin6_addr, &actual->sa6.sin6_addr,
+			memcmp(&SOCK_ADDR6(expected), &SOCK_ADDR6(actual),
 				   sizeof(struct in6_addr)) == 0) {
 			printf("IPv6 address matches\n");
 			return false;
@@ -70,8 +70,8 @@ bool IsDiffS(const sockaddr_u *expected, const sockaddr_u *actual) {
 
 sockaddr_u CreateSockaddr4(const char* address, unsigned int port) {
 	sockaddr_u s;
-	s.sa4.sin_family = AF_INET;
-	s.sa4.sin_addr.s_addr = inet_addr(address);
+	SET_AF(&s, AF_INET);
+	PSOCK_ADDR4(&s)->s_addr = inet_addr(address);
 	SET_PORT(&s, port);
 	return s;
 }


=====================================
tests/libntp/a_md5encrypt.c
=====================================
--- a/tests/libntp/a_md5encrypt.c
+++ b/tests/libntp/a_md5encrypt.c
@@ -63,11 +63,10 @@ TEST(a_md5encrypt, DecryptInvalid) {
 
 TEST(a_md5encrypt, IPv4AddressToRefId) {
 	sockaddr_u addr;
-	addr.sa4.sin_family = AF_INET;
-	addr.sa4.sin_port = htons(80);
-
-	u_int32_t address = inet_addr("192.0.2.1");
-	addr.sa4.sin_addr.s_addr = address;
+	SET_AF(&addr, AF_INET);
+	SET_NSRCPORT(&addr, htons(80));
+	uint32_t address = inet_addr("192.0.2.1");
+	PSOCK_ADDR4(&addr)->s_addr = address;
 
 	TEST_ASSERT_EQUAL(address, addr2refid(&addr));
 }
@@ -82,9 +81,8 @@ TEST(a_md5encrypt, IPv6AddressToRefId) {
 
 
 	sockaddr_u addr;
-	addr.sa6.sin6_family = AF_INET6;
-
-	addr.sa6.sin6_addr = address;
+	SET_AF(&addr, AF_INET6);
+	SET_SOCK_ADDR6(&addr, address);
 
 	const int expected = 0x75cffd52;
 


=====================================
tests/libntp/decodenetnum.c
=====================================
--- a/tests/libntp/decodenetnum.c
+++ b/tests/libntp/decodenetnum.c
@@ -18,8 +18,8 @@ TEST(decodenetnum, IPv4AddressOnly) {
 	sockaddr_u actual;
 
 	sockaddr_u expected;
-	expected.sa4.sin_family = AF_INET;
-	expected.sa4.sin_addr.s_addr = inet_addr("192.0.2.1");
+	SET_AF(&expected, AF_INET);
+	PSOCK_ADDR4(&expected)->s_addr = inet_addr("192.0.2.1");
 	SET_PORT(&expected, NTP_PORT);
 
 	TEST_ASSERT_TRUE(decodenetnum(str, &actual));
@@ -31,8 +31,8 @@ TEST(decodenetnum, IPv4AddressWithPort) {
 	sockaddr_u actual;
 
 	sockaddr_u expected;
-	expected.sa4.sin_family = AF_INET;
-	expected.sa4.sin_addr.s_addr = inet_addr("192.0.2.2");
+	SET_AF(&expected, AF_INET);
+	PSOCK_ADDR4(&expected)->s_addr = inet_addr("192.0.2.2");
 	SET_PORT(&expected, 2000);
 
 	TEST_ASSERT_TRUE(decodenetnum(str, &actual));
@@ -51,8 +51,8 @@ TEST(decodenetnum, IPv6AddressOnly) {
 	sockaddr_u actual;
 
 	sockaddr_u expected;
-	expected.sa6.sin6_family = AF_INET6;
-	expected.sa6.sin6_addr = address;
+	SET_AF(&expected, AF_INET6);
+	SET_SOCK_ADDR6(&expected, address);
 	SET_PORT(&expected, NTP_PORT);
 
 	TEST_ASSERT_TRUE(decodenetnum(str, &actual));
@@ -71,8 +71,8 @@ TEST(decodenetnum, IPv6AddressWithPort) {
 	sockaddr_u actual;
 
 	sockaddr_u expected;
-	expected.sa6.sin6_family = AF_INET6;
-	expected.sa6.sin6_addr = address;
+	SET_AF(&expected, AF_INET6);
+	SET_SOCK_ADDR6(&expected, address);
 	SET_PORT(&expected, 3000);
 
 	TEST_ASSERT_TRUE(decodenetnum(str, &actual));
@@ -94,8 +94,8 @@ TEST(decodenetnum, IllegalCharInPort) {
 	sockaddr_u actual;
 
 	sockaddr_u expected;
-	expected.sa4.sin_family = AF_INET;
-	expected.sa4.sin_addr.s_addr = inet_addr("192.0.2.1");
+	SET_AF(&expected, AF_INET);
+	PSOCK_ADDR4(&expected)->s_addr = inet_addr("192.0.2.1");
 	SET_PORT(&expected, NTP_PORT);
 
 	TEST_ASSERT_FALSE(decodenetnum(str, &actual));


=====================================
tests/libntp/netof.c
=====================================
--- a/tests/libntp/netof.c
+++ b/tests/libntp/netof.c
@@ -63,13 +63,13 @@ TEST(netof, IPv6Address) {
 	}}}; // 2001:0db8:85a3:08d3:0000:0000:0000:0000
 
 	sockaddr_u input;
-	input.sa6.sin6_family = AF_INET6;
-	input.sa6.sin6_addr = input_address;
+	SET_AF(&input, AF_INET6);
+	SET_SOCK_ADDR6(&input, input_address);
 	SET_PORT(&input, 3000);
 
 	sockaddr_u expected;
-	expected.sa6.sin6_family = AF_INET6;
-	expected.sa6.sin6_addr = expected_address;
+	SET_AF(&expected, AF_INET6);
+	SET_SOCK_ADDR6(&expected, expected_address);
 	SET_PORT(&expected, 3000);
 
 	sockaddr_u* actual = netof(&input);


=====================================
tests/libntp/socktoa.c
=====================================
--- a/tests/libntp/socktoa.c
+++ b/tests/libntp/socktoa.c
@@ -94,13 +94,13 @@ TEST(socktoa, IgnoreIPv6Fields) {
 
 	sockaddr_u input1, input2;
 
-	input1.sa6.sin6_family = AF_INET6;
-	input1.sa6.sin6_addr = address;
+	SET_AF(&input1, AF_INET6);
+	SET_SOCK_ADDR6(&input1, address);
 	input1.sa6.sin6_flowinfo = 30L; // This value differs from input2.
 	SET_PORT(&input1, NTP_PORT);
 
-	input2.sa6.sin6_family = AF_INET6;
-	input2.sa6.sin6_addr = address;
+	SET_AF(&input2, AF_INET6);
+	SET_SOCK_ADDR6(&input2, address);
 	input2.sa6.sin6_flowinfo = 10L; // This value differs from input1.
 	SET_PORT(&input2, NTP_PORT);
 


=====================================
tests/ntpd/restrict.c
=====================================
--- a/tests/ntpd/restrict.c
+++ b/tests/ntpd/restrict.c
@@ -13,10 +13,10 @@ create_sockaddr_u(unsigned short sin_port, const char* ip_addr)
 {
 	sockaddr_u sockaddr;
 
-	sockaddr.sa4.sin_family = AF_INET;
-	sockaddr.sa4.sin_port = htons(sin_port);
-	memset(sockaddr.sa4.sin_zero, 0, 8);
-	sockaddr.sa4.sin_addr.s_addr = inet_addr(ip_addr);
+	memset(&sockaddr, 0, sizeof(sockaddr));
+	SET_AF(&sockaddr, AF_INET);
+	NSRCPORT(&sockaddr) = htons(sin_port);
+	PSOCK_ADDR4(&sockaddr)->s_addr = inet_addr(ip_addr);
 
 	return sockaddr;
 }



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/e6f411563e755348de5e6b318a89ed65e74aacf3
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ntpsec.org/pipermail/vc/attachments/20170107/9d847d42/attachment.html>


More information about the vc mailing list