1
0
mirror of https://github.com/rofl0r/proxychains-ng synced 2025-01-08 22:22:52 +08:00

change unsocksify_udp_packet() and

decapsulate_udp_packet() signature.
Moved udp data copy outside these functions
This commit is contained in:
hugoc 2024-02-02 00:08:03 +01:00
parent 81092b1f4b
commit a47f9c0576
3 changed files with 21 additions and 18 deletions

View File

@ -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
@ -794,22 +794,19 @@ int decapsulate_check_udp_packet(void* in_buffer, size_t in_buffer_len, udp_rela
}
// 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;

View File

@ -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);

View File

@ -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,24 +1975,30 @@ 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
// la retrouver ? -> done in unsocksify_udp_packet()