aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPřemysl Janouch <p.janouch@gmail.com>2015-07-22 00:11:20 +0200
committerPřemysl Janouch <p.janouch@gmail.com>2015-07-22 00:11:20 +0200
commit7d9b05eb9a7ea5af21242274fbe55dc9f89f7240 (patch)
treee5813d2fb7b36465ae13ebdee322d1dba59bf1f5
parent0fc8ea6a66001c32c1c9f865c1ae5b921aaff681 (diff)
downloadxK-7d9b05eb9a7ea5af21242274fbe55dc9f89f7240.tar.gz
xK-7d9b05eb9a7ea5af21242274fbe55dc9f89f7240.tar.xz
xK-7d9b05eb9a7ea5af21242274fbe55dc9f89f7240.zip
SOCKS: shorten code with SOCKS_GO
-rw-r--r--common.c63
1 files changed, 20 insertions, 43 deletions
diff --git a/common.c b/common.c
index 530a367..99b3b8e 100644
--- a/common.c
+++ b/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);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -