Skip to content

Commit c3831a2

Browse files
committed
Fix ipv6 bug
1 parent 0528b44 commit c3831a2

File tree

2 files changed

+30
-24
lines changed

2 files changed

+30
-24
lines changed

source/socks5.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,17 @@ AWS_STATIC_STRING_FROM_LITERAL(s_socks_none_method, "NONE");
2525
AWS_STATIC_STRING_FROM_LITERAL(s_socks_username_password_method, "USERNAME_PASSWORD");
2626
AWS_STATIC_STRING_FROM_LITERAL(s_socks_gssapi_method, "GSSAPI");
2727

28+
static struct aws_byte_cursor s_trim_ipv6_brackets(struct aws_byte_cursor host_cursor) {
29+
if (host_cursor.len >= 2 && host_cursor.ptr && host_cursor.ptr[0] == '[') {
30+
size_t last_index = host_cursor.len - 1;
31+
if (host_cursor.ptr[last_index] == ']') {
32+
host_cursor.ptr += 1;
33+
host_cursor.len -= 2;
34+
}
35+
}
36+
return host_cursor;
37+
}
38+
2839
/* Buffer size constants for SOCKS5 protocol operations */
2940
#define AWS_SOCKS5_SEND_BUFFER_INITIAL_SIZE 256
3041
#define AWS_SOCKS5_RECV_BUFFER_INITIAL_SIZE 512
@@ -128,7 +139,8 @@ int aws_socks5_proxy_options_init(
128139

129140
aws_socks5_proxy_options_init_default(options);
130141
options->port = port;
131-
options->host = aws_string_new_from_cursor(allocator, &host);
142+
struct aws_byte_cursor normalized_host = s_trim_ipv6_brackets(host);
143+
options->host = aws_string_new_from_cursor(allocator, &normalized_host);
132144
if (options->host == NULL) {
133145
AWS_LOGF_ERROR(
134146
AWS_LS_IO_SOCKS5,

source/socks5_channel_handler.c

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,19 @@ void aws_socks5_channel_handler_set_system_vtable(const struct aws_socks5_system
7272
}
7373
}
7474

75+
/* SOCKS5 URIs follow RFC-3986, where IPv6 literals are enclosed in brackets.
76+
* DNS APIs expect bare literals, so strip a single leading '[' and trailing ']'. */
77+
static struct aws_byte_cursor s_normalize_proxy_host_cursor(struct aws_byte_cursor host_cursor) {
78+
if (host_cursor.len >= 2 && host_cursor.ptr[0] == '[') {
79+
size_t last_index = host_cursor.len - 1;
80+
if (host_cursor.ptr[last_index] == ']') {
81+
host_cursor.ptr += 1;
82+
host_cursor.len -= 2;
83+
}
84+
}
85+
return host_cursor;
86+
}
87+
7588
/**
7689
* State machine for the SOCKS5 channel handler
7790
*
@@ -119,26 +132,6 @@ static inline const char *s_socks5_channel_state_to_string(enum aws_socks5_chann
119132
}
120133
}
121134

122-
/**
123-
* Returns a human-readable name for SOCKS5 message types used in logging.
124-
* This aids in debugging by providing context about the type of message
125-
* being processed in the SOCKS5 protocol.
126-
*/
127-
static inline const char *s_get_socks5_message_type_name(int message_type) {
128-
switch (message_type) {
129-
case 0:
130-
return "INIT";
131-
case 1:
132-
return "GREETING";
133-
case 2:
134-
return "AUTH";
135-
case 3:
136-
return "CONNECT";
137-
default:
138-
return "UNKNOWN";
139-
}
140-
}
141-
142135
struct aws_socks5_channel_handler {
143136
/* Base handler data */
144137
struct aws_channel_handler handler;
@@ -2889,6 +2882,7 @@ static int s_socks5_bootstrap_set_socks5_proxy_options(
28892882
}
28902883

28912884
struct aws_byte_cursor endpoint_host_cursor = aws_byte_cursor_from_c_str(host_name);
2885+
struct aws_byte_cursor normalized_host_cursor = s_normalize_proxy_host_cursor(endpoint_host_cursor);
28922886

28932887
aws_string_destroy(socks5_bootstrap->endpoint_host);
28942888
socks5_bootstrap->endpoint_host = NULL;
@@ -2897,7 +2891,7 @@ static int s_socks5_bootstrap_set_socks5_proxy_options(
28972891

28982892
socks5_bootstrap->endpoint_port = port;
28992893
enum aws_socks5_address_type inferred_type =
2900-
aws_socks5_infer_address_type(endpoint_host_cursor, AWS_SOCKS5_ATYP_DOMAIN);
2894+
aws_socks5_infer_address_type(normalized_host_cursor, AWS_SOCKS5_ATYP_DOMAIN);
29012895
socks5_bootstrap->host_resolution_mode =
29022896
aws_socks5_proxy_options_get_host_resolution_mode(socks5_proxy_options);
29032897
socks5_bootstrap->resolution_error_code = AWS_ERROR_SUCCESS;
@@ -2908,7 +2902,7 @@ static int s_socks5_bootstrap_set_socks5_proxy_options(
29082902
if (socks5_bootstrap->host_resolution_mode == AWS_SOCKS5_HOST_RESOLUTION_CLIENT &&
29092903
inferred_type != AWS_SOCKS5_ATYP_DOMAIN) {
29102904
socks5_bootstrap->endpoint_host =
2911-
aws_string_new_from_cursor(allocator, &endpoint_host_cursor);
2905+
aws_string_new_from_cursor(allocator, &normalized_host_cursor);
29122906
if (!socks5_bootstrap->endpoint_host) {
29132907
aws_socks5_proxy_options_clean_up(socks5_proxy_options);
29142908
aws_mem_release(allocator, socks5_proxy_options);
@@ -2937,7 +2931,7 @@ static int s_socks5_bootstrap_set_socks5_proxy_options(
29372931
socks5_bootstrap->endpoint_ready = false;
29382932
} else {
29392933
socks5_bootstrap->endpoint_host =
2940-
aws_string_new_from_cursor(allocator, &endpoint_host_cursor);
2934+
aws_string_new_from_cursor(allocator, &normalized_host_cursor);
29412935
if (!socks5_bootstrap->endpoint_host) {
29422936
aws_socks5_proxy_options_clean_up(socks5_proxy_options);
29432937
aws_mem_release(allocator, socks5_proxy_options);

0 commit comments

Comments
 (0)