Debian Codegraph @

crt/aws-c-common/source/host_utils.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5 #include <aws/common/host_utils.h>
6 #include <aws/common/string.h>
7 #include <inttypes.h>
8
9 #ifdef _MSC_VER /* Disable sscanf warnings on windows. */
10 # pragma warning(disable : 4204)
11 # pragma warning(disable : 4706)
12 # pragma warning(disable : 4996)
13 #endif
14
15 /* 4 octets of 3 chars max + 3 separators + null terminator */
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 /* actual encoding is %25, but % is omitted for simplicity, since split removes it */
54 static struct aws_byte_cursor s_percent_uri_enc = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("25");
55 /*
56 * IPv6 format:
57 * 8 groups of 4 hex chars separated by colons (:)
58 * leading 0s in each group can be skipped
59 * 2 or more consecutive zero groups can be replaced by double colon (::),
60 * but only once.
61 * ipv6 literal can be scoped by to zone by appending % followed by zone name
62 * ( does not look like there is length reqs on zone name length. this
63 * implementation enforces that its > 1 )
64 * ipv6 can be embedded in url, in which case % must be uri encoded as %25.
65 * Implementation is fairly trivial and just iterates through the string
66 * keeping track of the spec above.
67 * Note: there is no single rfc for IPv6 address - base format defined in RFC 5952,
68 * zoneId and uri extensions defined in RFC 6874 and RFC 3986
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 /* first split is required ipv6 part */
77 bool is_split = aws_byte_cursor_next_split(&host, '%', &substr);
78 AWS_ASSERT(is_split); /* function is guaranteed to return at least one 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] != ':') || /* no single colon at start */
85 (substr.ptr[substr.len - 1] == ':' && substr.ptr[substr.len - 2] != ':')) { /* no single colon at end */
86 return false;
87 }
88
89 uint8_t group_count = 1; /* string itself is the first group and then every new : we encounter is new group */
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) { /* one double colon max */
100 return false;
101 }
102 has_double_colon = true;
103 --group_count; /* avoid double counting groups */
104 }
105 } else {
106 ++digit_count;
107 }
108
109 if (digit_count > 4 || /* too many digits in group */
110 group_count > 8) { /* too many groups */
111 return false;
112 }
113 }
114
115 /* second split is optional zone part */
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))) || /* encoding for % + 1 extra char */
120 (!is_uri_encoded && substr.len == 0) || /* at least 1 char */
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 }