SOCKS: shorten code with SOCKS_GO

This commit is contained in:
Přemysl Eric Janouch 2015-07-22 00:11:20 +02:00
parent 0fc8ea6a66
commit 7d9b05eb9a
1 changed files with 20 additions and 43 deletions

View File

@ -441,6 +441,11 @@ struct socks_connector
#define SOCKS_DATA_CB(name) static bool name \ #define SOCKS_DATA_CB(name) static bool name \
(struct socks_connector *self, struct msg_unpacker *unpacker) (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) SOCKS_DATA_CB (socks_4a_finish)
@ -512,9 +517,7 @@ socks_4a_start (struct socks_connector *self)
str_append_c (wb, '\0'); str_append_c (wb, '\0');
} }
self->on_data = socks_4a_finish; SOCKS_GO (socks_4a_finish, 8);
self->data_needed = 8;
return true;
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@ -528,41 +531,26 @@ SOCKS_DATA_CB (socks_5_request_port)
SOCKS_DATA_CB (socks_5_request_ipv4) SOCKS_DATA_CB (socks_5_request_ipv4)
{ {
memcpy (self->bound_address.data.ipv4, memcpy (self->bound_address.data.ipv4, unpacker->data, unpacker->len);
self->read_buffer.str, self->data_needed); SOCKS_GO (socks_5_request_port, 2);
self->on_data = socks_5_request_port;
self->data_needed = 2;
return true;
} }
SOCKS_DATA_CB (socks_5_request_ipv6) SOCKS_DATA_CB (socks_5_request_ipv6)
{ {
memcpy (self->bound_address.data.ipv6, memcpy (self->bound_address.data.ipv6, unpacker->data, unpacker->len);
self->read_buffer.str, self->data_needed); SOCKS_GO (socks_5_request_port, 2);
self->on_data = socks_5_request_port;
self->data_needed = 2;
return true;
} }
SOCKS_DATA_CB (socks_5_request_domain_data) SOCKS_DATA_CB (socks_5_request_domain_data)
{ {
self->bound_address.data.domain = self->bound_address.data.domain = xstrndup (unpacker->data, unpacker->len);
xstrndup (self->read_buffer.str, self->data_needed); SOCKS_GO (socks_5_request_port, 2);
self->on_data = socks_5_request_port;
self->data_needed = 2;
return true;
} }
SOCKS_DATA_CB (socks_5_request_domain) SOCKS_DATA_CB (socks_5_request_domain)
{ {
hard_assert (msg_unpacker_u8 (unpacker, &self->bound_address_len)); hard_assert (msg_unpacker_u8 (unpacker, &self->bound_address_len));
SOCKS_GO (socks_5_request_domain_data, self->bound_address_len);
self->on_data = socks_5_request_domain_data;
self->data_needed = self->bound_address_len;
return true;
} }
SOCKS_DATA_CB (socks_5_request_finish) SOCKS_DATA_CB (socks_5_request_finish)
@ -594,17 +582,11 @@ SOCKS_DATA_CB (socks_5_request_finish)
switch ((self->bound_address.type = type)) switch ((self->bound_address.type = type))
{ {
case SOCKS_IPV4: case SOCKS_IPV4:
self->on_data = socks_5_request_ipv4; SOCKS_GO (socks_5_request_ipv4, sizeof self->bound_address.data.ipv4);
self->data_needed = sizeof self->bound_address.data.ipv4;
return true;
case SOCKS_IPV6: case SOCKS_IPV6:
self->data_needed = sizeof self->bound_address.data.ipv6; SOCKS_GO (socks_5_request_ipv6, sizeof self->bound_address.data.ipv6);
self->on_data = socks_5_request_ipv6;
return true;
case SOCKS_DOMAIN: case SOCKS_DOMAIN:
self->on_data = socks_5_request_domain; SOCKS_GO (socks_5_request_domain, 1);
self->data_needed = 1;
return true;
default: default:
SOCKS_FAIL ("protocol error"); 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 >> 8);
str_append_c (wb, target->port); str_append_c (wb, target->port);
self->on_data = socks_5_request_finish; SOCKS_GO (socks_5_request_finish, 4);
self->data_needed = 4;
return true;
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@ -661,6 +641,7 @@ SOCKS_DATA_CB (socks_5_userpass_finish)
SOCKS_FAIL ("protocol error"); SOCKS_FAIL ("protocol error");
if (status != 0x00) if (status != 0x00)
SOCKS_FAIL ("authentication failure"); SOCKS_FAIL ("authentication failure");
return socks_5_request_start (self); 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_c (wb, plen); // password length
str_append_data (wb, self->password, plen); str_append_data (wb, self->password, plen);
self->on_data = socks_5_userpass_finish; SOCKS_GO (socks_5_userpass_finish, 2);
self->data_needed = 2;
return true;
} }
SOCKS_DATA_CB (socks_5_auth_finish) SOCKS_DATA_CB (socks_5_auth_finish)
@ -726,9 +705,7 @@ socks_5_auth_start (struct socks_connector *self)
if (can_auth) if (can_auth)
str_append_c (wb, 0x02); // username/password str_append_c (wb, 0x02); // username/password
self->on_data = socks_5_auth_finish; SOCKS_GO (socks_5_auth_finish, 2);
self->data_needed = 2;
return true;
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -