From a47f9c05767de2e27461e011dc762f47fe1912d9 Mon Sep 17 00:00:00 2001 From: hugoc Date: Fri, 2 Feb 2024 00:08:03 +0100 Subject: [PATCH] change unsocksify_udp_packet() and decapsulate_udp_packet() signature. Moved udp data copy outside these functions --- src/core.c | 17 +++++++---------- src/core.h | 2 +- src/libproxychains.c | 20 +++++++++++++------- 3 files changed, 21 insertions(+), 18 deletions(-) diff --git a/src/core.c b/src/core.c index 6993ccf..003e90e 100644 --- a/src/core.c +++ b/src/core.c @@ -740,7 +740,7 @@ int is_from_chain_head(udp_relay_chain chain, struct sockaddr* src_addr){ -int decapsulate_check_udp_packet(void* in_buffer, size_t in_buffer_len, udp_relay_chain chain, socks5_addr* src_addr, unsigned short* src_port, void* udp_data, size_t* udp_data_len){ +int decapsulate_check_udp_packet(void* in_buffer, size_t in_buffer_len, udp_relay_chain chain, socks5_addr* src_addr, unsigned short* src_port, void** udp_data){ PFUNC(); // Go through the whole proxy chain, decapsulate each header and check that the addresses match @@ -793,23 +793,20 @@ int decapsulate_check_udp_packet(void* in_buffer, size_t in_buffer_len, udp_rela printf("WARNING: received UDP packet with frag != 0 while fragmentation is unsupported.\n"); } - - // Copy the UDP data to the provided buffer. If the provided buffer is too small, data is truncated - int min = ((in_buffer_len-read)>*udp_data_len)?*udp_data_len:(in_buffer_len-read); - memcpy(udp_data,in_buffer+read, min); - - // Write back the length of written UDP data in the input/output parameter udp_data_len - *udp_data_len = min; + + // Point udp_data to the position of the UDP data inside in_buffer + *udp_data = in_buffer+read; return 0; } -int unsocksify_udp_packet(void* in_buffer, size_t in_buffer_len, udp_relay_chain chain, ip_type* src_ip, unsigned short* src_port, void* udp_data, size_t* udp_data_len){ +//Takes an in_buffer of size in_buffer_len, checks that all UDP headers are correct (against chain), fills src_ip and src_port with address of the peer sending the packet through the relay, and fills udp_data with the address of the udp data inside in_buff +int unsocksify_udp_packet(void* in_buffer, size_t in_buffer_len, udp_relay_chain chain, ip_type* src_ip, unsigned short* src_port, void** udp_data){ PFUNC(); // Decapsulate all the UDP headers and check that the packet came from the right proxy nodes int rc; socks5_addr src_addr; - rc = decapsulate_check_udp_packet(in_buffer, in_buffer_len, chain, &src_addr, src_port, udp_data, udp_data_len); + rc = decapsulate_check_udp_packet(in_buffer, in_buffer_len, chain, &src_addr, src_port, udp_data); if(rc != SUCCESS){ PDEBUG("error decapsulating the packet\n"); return -1; diff --git a/src/core.h b/src/core.h index b2c4bec..88211fc 100644 --- a/src/core.h +++ b/src/core.h @@ -222,7 +222,7 @@ size_t get_msg_iov_total_len(struct iovec* iov, size_t iov_len); size_t write_buf_to_iov(void* buff, size_t buff_len, struct iovec* iov, size_t iov_len); size_t write_iov_to_buf(void* buff, size_t buff_len, struct iovec* iov, size_t iov_len); int is_from_chain_head(udp_relay_chain chain, struct sockaddr* src_addr); -int unsocksify_udp_packet(void* in_buffer, size_t in_buffer_len, udp_relay_chain chain, ip_type* src_ip, unsigned short* src_port, void* udp_data, size_t* udp_data_len); +int unsocksify_udp_packet(void* in_buffer, size_t in_buffer_len, udp_relay_chain chain, ip_type* src_ip, unsigned short* src_port, void** udp_data); int socksify_udp_packet(void* udp_data, size_t udp_data_len, udp_relay_chain chain, ip_type dst_ip, unsigned short dst_port, void* buffer, size_t* buffer_len); int encapsulate_udp_packet(udp_relay_chain chain, socks5_addr dst_addr, unsigned short dst_port, void* buffer, size_t* buffer_len); void set_connected_peer_addr(udp_relay_chain* chain, struct sockaddr* addr, socklen_t addrlen); diff --git a/src/libproxychains.c b/src/libproxychains.c index f1c54bd..cf2121b 100644 --- a/src/libproxychains.c +++ b/src/libproxychains.c @@ -1811,16 +1811,16 @@ HOOKFUNC(ssize_t, recvmsg, int sockfd, struct msghdr *msg, int flags){ int rc; ip_type src_ip; unsigned short src_port; - char udp_data[RECV_BUFFER_SIZE]; - size_t udp_data_len = sizeof(udp_data); + void* udp_data = NULL; + size_t udp_data_len = 0; - rc = unsocksify_udp_packet(buffer, bytes_received, *relay_chain, &src_ip, &src_port, udp_data, &udp_data_len); + rc = unsocksify_udp_packet(buffer, bytes_received, *relay_chain, &src_ip, &src_port, &udp_data); if(rc != SUCCESS){ PDEBUG("error unSOCKSing the UDP packet\n"); return -1; } PDEBUG("UDP packet successfully unSOCKified\n"); - + udp_data_len = bytes_received - (udp_data - (void*)buffer); /*debug*/ DEBUGDECL(char str[256]); @@ -1975,23 +1975,29 @@ HOOKFUNC(ssize_t, recvfrom, int sockfd, void *buf, size_t len, int flags, PDEBUG("packet received from the proxy chain's head\n"); int rc; - size_t udp_data_len = len; - rc = unsocksify_udp_packet(tmp_buffer, bytes_received, *relay_chain, &src_ip, &src_port, buf, &udp_data_len); + void* udp_data = NULL; + size_t udp_data_len = 0; + rc = unsocksify_udp_packet(tmp_buffer, bytes_received, *relay_chain, &src_ip, &src_port, &udp_data); if(rc != SUCCESS){ PDEBUG("error unsocksifying the UDP packet\n"); return -1; } PDEBUG("UDP packet successfully unsocksifyied\n"); + udp_data_len = bytes_received - (udp_data - (void*)tmp_buffer); PDEBUG("received %d bytes through receive_udp_packet()\n", udp_data_len); PDEBUG("data: "); - DUMP_BUFFER(buf, udp_data_len); + DUMP_BUFFER(udp_data, udp_data_len); PDEBUG("from_addr: "); DUMP_BUFFER(src_ip.addr.v6, src_ip.is_v6?16:4); PDEBUG("from_addr: %s\n", inet_ntop(src_ip.is_v6 ? AF_INET6 : AF_INET, src_ip.is_v6 ? (void*)src_ip.addr.v6 : (void*)src_ip.addr.v4.octet, str, sizeof(str))); PDEBUG("from_port: %hu\n", ntohs(src_port)); + + // Copy received UDP data to the buffer provided by the client + size_t min = (udp_data_len < len)?udp_data_len:len; + memcpy(buf, udp_data, min); // WARNING : Est ce que si le client avait envoyé des packets UDP avec resolution DNS dans le socks, // on doit lui filer comme address source pour les packets recu l'addresse de mapping DNS ? Si oui comment