Merge pull request #2791 from pmarks-net/uri

Return normalized IPv4 addresses from grpc_sockaddr_to_uri().
pull/2396/head^2
Craig Tiller 10 years ago
commit 1dc978c386
  1. 5
      src/core/iomgr/sockaddr_utils.c
  2. 14
      test/core/iomgr/sockaddr_utils_test.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:

@ -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);
}

Loading…
Cancel
Save