void test_ScopedIPv6AddressWithPort(void) { #ifdef ISC_PLATFORM_HAVESCOPEID const struct in6_addr address = { { { 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x12, 0x3f, 0xff, 0xfe, 0x29, 0xff, 0xfa } } }; const char* expected = "fe80::212:3fff:fe29:fffa%5"; const char* expected_port = "[fe80::212:3fff:fe29:fffa%5]:123"; sockaddr_u input; memset(&input, 0, sizeof(input)); AF(&input) = AF_INET6; SET_ADDR6N(&input, address); SET_PORT(&input, 123); SCOPE_VAR(&input) = 5; TEST_ASSERT_EQUAL_STRING(expected, socktoa(&input)); TEST_ASSERT_EQUAL_STRING(expected_port, sockporttoa(&input)); #else TEST_IGNORE_MESSAGE("Skipping because ISC_PLATFORM does not have Scope ID"); #endif }
/* * socktoa - return a numeric host name from a sockaddr_storage structure */ const char * socktoa( const sockaddr_u *sock ) { int saved_errno; char * res; char * addr; u_long scope; saved_errno = errno; LIB_GETBUF(res); if (NULL == sock) { strlcpy(res, "(null)", LIB_BUFLENGTH); } else { switch(AF(sock)) { case AF_INET: case AF_UNSPEC: inet_ntop(AF_INET, PSOCK_ADDR4(sock), res, LIB_BUFLENGTH); break; case AF_INET6: inet_ntop(AF_INET6, PSOCK_ADDR6(sock), res, LIB_BUFLENGTH); scope = SCOPE_VAR(sock); if (0 != scope && !strchr(res, '%')) { addr = res; LIB_GETBUF(res); snprintf(res, LIB_BUFLENGTH, "%s%%%lu", addr, scope); res[LIB_BUFLENGTH - 1] = '\0'; } break; default: snprintf(res, LIB_BUFLENGTH, "(socktoa unknown family %d)", AF(sock)); } } errno = saved_errno; return res; }
TEST(socktoa, ScopedIPv6AddressWithPort) { const struct in6_addr address = {{{ 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x12, 0x3f, 0xff, 0xfe, 0x29, 0xff, 0xfa }}}; const char* expected = "fe80::212:3fff:fe29:fffa%5"; const char* expected_port = "[fe80::212:3fff:fe29:fffa%5]:123"; sockaddr_u input; memset(&input, 0, sizeof(input)); AF(&input) = AF_INET6; SET_ADDR6N(&input, address); SET_PORT(&input, 123); SCOPE_VAR(&input) = 5; TEST_ASSERT_EQUAL_STRING(expected, socktoa(&input)); TEST_ASSERT_EQUAL_STRING(expected_port, sockporttoa(&input)); }