SOCKS: shorten code with SOCKS_GO
This commit is contained in:
parent
0fc8ea6a66
commit
7d9b05eb9a
63
common.c
63
common.c
|
@ -441,6 +441,11 @@ struct socks_connector
|
|||
#define SOCKS_DATA_CB(name) static bool name \
|
||||
(struct socks_connector *self, struct msg_unpacker *unpacker)
|
||||
|
||||
#define SOCKS_GO(name, data_needed_) \
|
||||
self->on_data = name; \
|
||||
self->data_needed = data_needed_; \
|
||||
return true
|
||||
|
||||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||
|
||||
SOCKS_DATA_CB (socks_4a_finish)
|
||||
|
@ -512,9 +517,7 @@ socks_4a_start (struct socks_connector *self)
|
|||
str_append_c (wb, '\0');
|
||||
}
|
||||
|
||||
self->on_data = socks_4a_finish;
|
||||
self->data_needed = 8;
|
||||
return true;
|
||||
SOCKS_GO (socks_4a_finish, 8);
|
||||
}
|
||||
|
||||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||
|
@ -528,41 +531,26 @@ SOCKS_DATA_CB (socks_5_request_port)
|
|||
|
||||
SOCKS_DATA_CB (socks_5_request_ipv4)
|
||||
{
|
||||
memcpy (self->bound_address.data.ipv4,
|
||||
self->read_buffer.str, self->data_needed);
|
||||
|
||||
self->on_data = socks_5_request_port;
|
||||
self->data_needed = 2;
|
||||
return true;
|
||||
memcpy (self->bound_address.data.ipv4, unpacker->data, unpacker->len);
|
||||
SOCKS_GO (socks_5_request_port, 2);
|
||||
}
|
||||
|
||||
SOCKS_DATA_CB (socks_5_request_ipv6)
|
||||
{
|
||||
memcpy (self->bound_address.data.ipv6,
|
||||
self->read_buffer.str, self->data_needed);
|
||||
|
||||
self->on_data = socks_5_request_port;
|
||||
self->data_needed = 2;
|
||||
return true;
|
||||
memcpy (self->bound_address.data.ipv6, unpacker->data, unpacker->len);
|
||||
SOCKS_GO (socks_5_request_port, 2);
|
||||
}
|
||||
|
||||
SOCKS_DATA_CB (socks_5_request_domain_data)
|
||||
{
|
||||
self->bound_address.data.domain =
|
||||
xstrndup (self->read_buffer.str, self->data_needed);
|
||||
|
||||
self->on_data = socks_5_request_port;
|
||||
self->data_needed = 2;
|
||||
return true;
|
||||
self->bound_address.data.domain = xstrndup (unpacker->data, unpacker->len);
|
||||
SOCKS_GO (socks_5_request_port, 2);
|
||||
}
|
||||
|
||||
SOCKS_DATA_CB (socks_5_request_domain)
|
||||
{
|
||||
hard_assert (msg_unpacker_u8 (unpacker, &self->bound_address_len));
|
||||
|
||||
self->on_data = socks_5_request_domain_data;
|
||||
self->data_needed = self->bound_address_len;
|
||||
return true;
|
||||
SOCKS_GO (socks_5_request_domain_data, self->bound_address_len);
|
||||
}
|
||||
|
||||
SOCKS_DATA_CB (socks_5_request_finish)
|
||||
|
@ -594,17 +582,11 @@ SOCKS_DATA_CB (socks_5_request_finish)
|
|||
switch ((self->bound_address.type = type))
|
||||
{
|
||||
case SOCKS_IPV4:
|
||||
self->on_data = socks_5_request_ipv4;
|
||||
self->data_needed = sizeof self->bound_address.data.ipv4;
|
||||
return true;
|
||||
SOCKS_GO (socks_5_request_ipv4, sizeof self->bound_address.data.ipv4);
|
||||
case SOCKS_IPV6:
|
||||
self->data_needed = sizeof self->bound_address.data.ipv6;
|
||||
self->on_data = socks_5_request_ipv6;
|
||||
return true;
|
||||
SOCKS_GO (socks_5_request_ipv6, sizeof self->bound_address.data.ipv6);
|
||||
case SOCKS_DOMAIN:
|
||||
self->on_data = socks_5_request_domain;
|
||||
self->data_needed = 1;
|
||||
return true;
|
||||
SOCKS_GO (socks_5_request_domain, 1);
|
||||
default:
|
||||
SOCKS_FAIL ("protocol error");
|
||||
}
|
||||
|
@ -644,9 +626,7 @@ socks_5_request_start (struct socks_connector *self)
|
|||
str_append_c (wb, target->port >> 8);
|
||||
str_append_c (wb, target->port);
|
||||
|
||||
self->on_data = socks_5_request_finish;
|
||||
self->data_needed = 4;
|
||||
return true;
|
||||
SOCKS_GO (socks_5_request_finish, 4);
|
||||
}
|
||||
|
||||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||
|
@ -661,6 +641,7 @@ SOCKS_DATA_CB (socks_5_userpass_finish)
|
|||
SOCKS_FAIL ("protocol error");
|
||||
if (status != 0x00)
|
||||
SOCKS_FAIL ("authentication failure");
|
||||
|
||||
return socks_5_request_start (self);
|
||||
}
|
||||
|
||||
|
@ -682,9 +663,7 @@ socks_5_userpass_start (struct socks_connector *self)
|
|||
str_append_c (wb, plen); // password length
|
||||
str_append_data (wb, self->password, plen);
|
||||
|
||||
self->on_data = socks_5_userpass_finish;
|
||||
self->data_needed = 2;
|
||||
return true;
|
||||
SOCKS_GO (socks_5_userpass_finish, 2);
|
||||
}
|
||||
|
||||
SOCKS_DATA_CB (socks_5_auth_finish)
|
||||
|
@ -726,9 +705,7 @@ socks_5_auth_start (struct socks_connector *self)
|
|||
if (can_auth)
|
||||
str_append_c (wb, 0x02); // username/password
|
||||
|
||||
self->on_data = socks_5_auth_finish;
|
||||
self->data_needed = 2;
|
||||
return true;
|
||||
SOCKS_GO (socks_5_auth_finish, 2);
|
||||
}
|
||||
|
||||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||
|
|
Loading…
Reference in New Issue