Skip to content

Commit

Permalink
Fix aws_host_utils_is_ipv6 function (#1114)
Browse files Browse the repository at this point in the history
  • Loading branch information
waahm7 committed May 10, 2024
1 parent 24e2396 commit 36a716e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 22 deletions.
15 changes: 1 addition & 14 deletions source/host_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ static bool s_is_ipv6_char(uint8_t value) {
return aws_isxdigit(value) || value == ':';
}

static bool s_starts_with(struct aws_byte_cursor cur, uint8_t ch) {
return cur.len > 0 && cur.ptr[0] == ch;
}

static bool s_ends_with(struct aws_byte_cursor cur, uint8_t ch) {
return cur.len > 0 && cur.ptr[cur.len - 1] == ch;
}
Expand Down Expand Up @@ -69,8 +65,7 @@ static struct aws_byte_cursor s_percent_uri_enc = AWS_BYTE_CUR_INIT_FROM_STRING_
* ipv6 literal can be scoped by to zone by appending % followed by zone name
* ( does not look like there is length reqs on zone name length. this
* implementation enforces that its > 1 )
* ipv6 can be embedded in url, in which case it must be wrapped inside []
* and % be uri encoded as %25.
* ipv6 can be embedded in url, in which case % must be uri encoded as %25.
* Implementation is fairly trivial and just iterates through the string
* keeping track of the spec above.
*/
Expand All @@ -79,14 +74,6 @@ bool aws_host_utils_is_ipv6(struct aws_byte_cursor host, bool is_uri_encoded) {
return false;
}

if (is_uri_encoded) {
if (!s_starts_with(host, '[') || !s_ends_with(host, ']')) {
return false;
}
aws_byte_cursor_advance(&host, 1);
--host.len;
}

struct aws_byte_cursor substr = {0};
/* first split is required ipv6 part */
bool is_split = aws_byte_cursor_next_split(&host, '%', &substr);
Expand Down
15 changes: 7 additions & 8 deletions tests/host_util_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ static int s_test_is_ipv6(struct aws_allocator *allocator, void *ctx) {
ASSERT_TRUE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("0:0:0:0:0:0:0:1"), false));
ASSERT_TRUE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("fd00:ec2::23"), false));
ASSERT_TRUE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("fd00:ec2:0:0:0:0:0:23"), false));
ASSERT_TRUE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("[2001:0db8:0000:0000:0000:8a2e:0370:7334]"), true));
ASSERT_TRUE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("[fe80::1]"), true));
ASSERT_TRUE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("[fe80::1%25en0]"), true));
ASSERT_TRUE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("[2001:db8:85a3:8d3:1319:8a2e:370:7348]"), true));
ASSERT_TRUE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("2001:0db8:0000:0000:0000:8a2e:0370:7334"), true));
ASSERT_TRUE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("fe80::1"), true));
ASSERT_TRUE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("fe80::1%25en0"), true));
ASSERT_TRUE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("2001:db8:85a3:8d3:1319:8a2e:370:7348"), true));

ASSERT_FALSE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("2001:0db8:0000:0000:0000:8a2e:0370"), false));
ASSERT_FALSE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("2001:0db8:0000:0000:0000:8a2e:0370:"), false));
Expand All @@ -63,12 +63,11 @@ static int s_test_is_ipv6(struct aws_allocator *allocator, void *ctx) {
ASSERT_FALSE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("z001::8a2e::8745"), false));
ASSERT_FALSE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("::2001:0db8:0000:0000:8a2e:0370:7334"), false));

ASSERT_FALSE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("fe80::1%25en0"), true));
ASSERT_FALSE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("[fe80::1%en0]"), true));
ASSERT_FALSE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("[fe80::1%24en0]"), true));
ASSERT_FALSE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("fe80::1%en0"), true));
ASSERT_FALSE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("fe80::1%24en0"), true));
ASSERT_FALSE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("[fe80::1%25en0"), true));
ASSERT_FALSE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("fe80::1%25en0]"), true));
ASSERT_FALSE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("[fe80::1%25]"), true));
ASSERT_FALSE(aws_host_utils_is_ipv6(aws_byte_cursor_from_c_str("fe80::1%25"), true));

return AWS_OP_SUCCESS;
}

0 comments on commit 36a716e

Please sign in to comment.