From 63541a1de15736df2a0e428f5c427581296f9773 Mon Sep 17 00:00:00 2001 From: Paul Marks Date: Tue, 4 Aug 2015 15:05:00 -0700 Subject: [PATCH] Return normalized IPv4 addresses from grpc_sockaddr_to_uri(). Users of the high-level API should not care whether we're using AF_INET or AF_INET6 sockets internally. --- src/core/iomgr/sockaddr_utils.c | 5 +++++ test/core/iomgr/sockaddr_utils_test.c | 14 ++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/src/core/iomgr/sockaddr_utils.c b/src/core/iomgr/sockaddr_utils.c index 71ac12e87b9..65ec1f94ac8 100644 --- a/src/core/iomgr/sockaddr_utils.c +++ b/src/core/iomgr/sockaddr_utils.c @@ -170,6 +170,11 @@ int grpc_sockaddr_to_string(char **out, const struct sockaddr *addr, char *grpc_sockaddr_to_uri(const struct sockaddr *addr) { char *temp; char *result; + struct sockaddr_in addr_normalized; + + if (grpc_sockaddr_is_v4mapped(addr, &addr_normalized)) { + addr = (const struct sockaddr *)&addr_normalized; + } switch (addr->sa_family) { case AF_INET: diff --git a/test/core/iomgr/sockaddr_utils_test.c b/test/core/iomgr/sockaddr_utils_test.c index dfab3409597..72a0f718358 100644 --- a/test/core/iomgr/sockaddr_utils_test.c +++ b/test/core/iomgr/sockaddr_utils_test.c @@ -187,6 +187,15 @@ static void expect_sockaddr_str(const char *expected, void *addr, gpr_free(str); } +static void expect_sockaddr_uri(const char *expected, void *addr) { + char *str; + gpr_log(GPR_INFO, " expect_sockaddr_uri(%s)", expected); + str = grpc_sockaddr_to_uri((struct sockaddr *)addr); + GPR_ASSERT(str != NULL); + GPR_ASSERT(strcmp(expected, str) == 0); + gpr_free(str); +} + static void test_sockaddr_to_string(void) { struct sockaddr_in input4; struct sockaddr_in6 input6; @@ -199,23 +208,28 @@ static void test_sockaddr_to_string(void) { input4 = make_addr4(kIPv4, sizeof(kIPv4)); expect_sockaddr_str("192.0.2.1:12345", &input4, 0); expect_sockaddr_str("192.0.2.1:12345", &input4, 1); + expect_sockaddr_uri("ipv4:192.0.2.1:12345", &input4); input6 = make_addr6(kIPv6, sizeof(kIPv6)); expect_sockaddr_str("[2001:db8::1]:12345", &input6, 0); expect_sockaddr_str("[2001:db8::1]:12345", &input6, 1); + expect_sockaddr_uri("ipv6:[2001:db8::1]:12345", &input6); input6 = make_addr6(kMapped, sizeof(kMapped)); expect_sockaddr_str("[::ffff:192.0.2.1]:12345", &input6, 0); expect_sockaddr_str("192.0.2.1:12345", &input6, 1); + expect_sockaddr_uri("ipv4:192.0.2.1:12345", &input6); input6 = make_addr6(kNotQuiteMapped, sizeof(kNotQuiteMapped)); expect_sockaddr_str("[::fffe:c000:263]:12345", &input6, 0); expect_sockaddr_str("[::fffe:c000:263]:12345", &input6, 1); + expect_sockaddr_uri("ipv6:[::fffe:c000:263]:12345", &input6); memset(&dummy, 0, sizeof(dummy)); dummy.sa_family = 123; expect_sockaddr_str("(sockaddr family=123)", &dummy, 0); expect_sockaddr_str("(sockaddr family=123)", &dummy, 1); + GPR_ASSERT(grpc_sockaddr_to_uri(&dummy) == NULL); GPR_ASSERT(errno == 0x7EADBEEF); }