Skip to content

Commit

Permalink
Fix: wrong size and address type in socks5-udp
Browse files Browse the repository at this point in the history
  • Loading branch information
semigodking committed Nov 23, 2019
1 parent 3a46f2f commit 4cc0fef
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
26 changes: 21 additions & 5 deletions socks5-udp.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ static struct evbuffer* socks5_mkassociate(void *p)
}

static void socks5_fill_preamble(
socks5_udp_preabmle *preamble,
socks5_udp_preamble *preamble,
struct sockaddr * addr,
size_t *preamble_len)
{
Expand Down Expand Up @@ -125,11 +125,25 @@ static int socks5_ready_to_fwd(struct redudp_client_t *client)
static void socks5_forward_pkt(redudp_client *client, struct sockaddr *destaddr, void *buf, size_t pktlen)
{
socks5_client *socks5client = (void*)(client + 1);
socks5_udp_preabmle req;
socks5_udp_preamble req;
struct msghdr msg;
struct iovec io[2];
size_t preamble_len;

if (socks5client->udprelayaddr.sin_family == AF_INET) {
preamble_len = SOCKS5_UDP_PREAMBLE_SIZE_V4;
}
/*
else if (socks5client->udprelayaddr.ss_family == AF_INET6) {
preamble_len = SOCKS5_UDP_PREAMBLE_SIZE_V6;
}
*/
else {
redudp_log_errno(client, LOG_WARNING, "Unknown address type %d",
socks5client->udprelayaddr.sin_family);
return;
}

socks5_fill_preamble(&req, destaddr, &preamble_len);

ssize_t outgoing, fwdlen = pktlen + preamble_len;
Expand Down Expand Up @@ -161,7 +175,7 @@ static void socks5_pkt_from_socks(int fd, short what, void *_arg)
socks5_client *socks5client = (void*)(client + 1);
union {
char buf[MAX_UDP_PACKET_SIZE];
socks5_udp_preabmle header;
socks5_udp_preamble header;
} * pkt = client->instance->shared_buff;
ssize_t pktlen, fwdlen;
struct sockaddr_storage udprelayaddr;
Expand All @@ -172,7 +186,9 @@ static void socks5_pkt_from_socks(int fd, short what, void *_arg)
if (pktlen == -1)
return;

if (memcmp(&udprelayaddr, &socks5client->udprelayaddr, sizeof(udprelayaddr)) != 0) {
if (evutil_sockaddr_cmp((struct sockaddr *)&udprelayaddr,
(struct sockaddr *)&socks5client->udprelayaddr,
1) != 0) {
char buf[RED_INET_ADDRSTRLEN];
redudp_log_error(client, LOG_NOTICE, "Got packet from unexpected address %s.",
red_inet_ntop(&udprelayaddr, buf, sizeof(buf)));
Expand Down Expand Up @@ -211,7 +227,7 @@ static void socks5_pkt_from_socks(int fd, short what, void *_arg)
}
// TODO: Support domain addr

fwdlen = pktlen - sizeof(pkt->header);
fwdlen = pktlen - header_size;
redudp_fwd_pkt_to_sender(client, pkt->buf + header_size, fwdlen, &src_addr);
}

Expand Down
7 changes: 5 additions & 2 deletions socks5.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ typedef struct socks5_reply_t {
/* socks5_addr_* */
} PACKED socks5_reply;

typedef struct socks5_udp_preabmle_t {
typedef struct socks5_udp_preamble_t {
uint16_t reserved;
uint8_t frag_no;
uint8_t addrtype; /* 0x01 for IPv4 */
Expand All @@ -72,7 +72,10 @@ typedef struct socks5_udp_preabmle_t {
socks5_addr_ipv4 v4;
socks5_addr_ipv6 v6;
} addr;
} PACKED socks5_udp_preabmle;
} PACKED socks5_udp_preamble;

#define SOCKS5_UDP_PREAMBLE_SIZE_V4 (4 + sizeof(socks5_addr_ipv4))
#define SOCKS5_UDP_PREAMBLE_SIZE_V6 (4 + sizeof(socks5_addr_ipv6))

static const int socks5_reply_maxlen = 512; // as domain name can't be longer than 256 bytes
static const int socks5_addrtype_ipv4 = 1;
Expand Down

0 comments on commit 4cc0fef

Please sign in to comment.