| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#include <aws/common/host_utils.h> |
| 6 |
#include <aws/common/string.h> |
| 7 |
#include <inttypes.h> |
| 8 |
|
| 9 |
#ifdef _MSC_VER |
| 10 |
# pragma warning(disable : 4204) |
| 11 |
# pragma warning(disable : 4706) |
| 12 |
# pragma warning(disable : 4996) |
| 13 |
#endif |
| 14 |
|
| 15 |
|
| 16 |
#define AWS_IPV4_STR_LEN 16 |
| 17 |
#define IP_CHAR_FMT "%03" SCNu16 |
| 18 |
|
| 19 |
static bool s_is_ipv6_char(uint8_t value) { |
| 20 |
return aws_isxdigit(value) || value == ':'; |
| 21 |
} |
| 22 |
|
| 23 |
bool aws_host_utils_is_ipv4(struct aws_byte_cursor host) { |
| 24 |
if (host.len > AWS_IPV4_STR_LEN - 1) { |
| 25 |
return false; |
| 26 |
} |
| 27 |
|
| 28 |
char copy[AWS_IPV4_STR_LEN] = {0}; |
| 29 |
memcpy(copy, host.ptr, host.len); |
| 30 |
|
| 31 |
uint16_t octet[4] = {0}; |
| 32 |
char remainder[2] = {0}; |
| 33 |
if (4 != sscanf( |
| 34 |
copy, |
| 35 |
IP_CHAR_FMT "." IP_CHAR_FMT "." IP_CHAR_FMT "." IP_CHAR_FMT "%1s", |
| 36 |
&octet[0], |
| 37 |
&octet[1], |
| 38 |
&octet[2], |
| 39 |
&octet[3], |
| 40 |
remainder)) { |
| 41 |
return false; |
| 42 |
} |
| 43 |
|
| 44 |
for (size_t i = 0; i < 4; ++i) { |
| 45 |
if (octet[i] > 255) { |
| 46 |
return false; |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
return true; |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
static struct aws_byte_cursor s_percent_uri_enc = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("25"); |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
bool aws_host_utils_is_ipv6(struct aws_byte_cursor host, bool is_uri_encoded) { |
| 71 |
if (host.len == 0) { |
| 72 |
return false; |
| 73 |
} |
| 74 |
|
| 75 |
struct aws_byte_cursor substr = {0}; |
| 76 |
|
| 77 |
bool is_split = aws_byte_cursor_next_split(&host, '%', &substr); |
| 78 |
AWS_ASSERT(is_split); |
| 79 |
|
| 80 |
if (!is_split || substr.len < 2 || substr.len > 39 || !aws_byte_cursor_satisfies_pred(&substr, s_is_ipv6_char)) { |
| 81 |
return false; |
| 82 |
} |
| 83 |
|
| 84 |
if ((substr.ptr[0] == ':' && substr.ptr[1] != ':') || |
| 85 |
(substr.ptr[substr.len - 1] == ':' && substr.ptr[substr.len - 2] != ':')) { |
| 86 |
return false; |
| 87 |
} |
| 88 |
|
| 89 |
uint8_t group_count = 1; |
| 90 |
uint8_t digit_count = 0; |
| 91 |
bool has_double_colon = false; |
| 92 |
|
| 93 |
for (size_t i = 0; i < substr.len; ++i) { |
| 94 |
if (substr.ptr[i] == ':') { |
| 95 |
++group_count; |
| 96 |
digit_count = 0; |
| 97 |
|
| 98 |
if (i > 0 && substr.ptr[i - 1] == ':') { |
| 99 |
if (has_double_colon) { |
| 100 |
return false; |
| 101 |
} |
| 102 |
has_double_colon = true; |
| 103 |
--group_count; |
| 104 |
} |
| 105 |
} else { |
| 106 |
++digit_count; |
| 107 |
} |
| 108 |
|
| 109 |
if (digit_count > 4 || |
| 110 |
group_count > 8) { |
| 111 |
return false; |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
if (aws_byte_cursor_next_split(&host, '%', &substr)) { |
| 117 |
if ((is_uri_encoded && |
| 118 |
(substr.len < 3 || |
| 119 |
!aws_byte_cursor_starts_with(&substr, &s_percent_uri_enc))) || |
| 120 |
(!is_uri_encoded && substr.len == 0) || |
| 121 |
!aws_byte_cursor_satisfies_pred(&substr, aws_isalnum)) { |
| 122 |
return false; |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
return has_double_colon ? group_count <= 8 : group_count == 8; |
| 127 |
} |