3

I'm building a simple application which should send UDP datagram packets through a socks4/5 proxy. I use UDP approach so I don't have to keep the connection(s) opened.

However it wasn't as easy as I thought. According to this schema I conclude that I cannot send UDP data through a proxy without establishing TCP connection first with the proxy server. enter image description here

Nonetheless I couldn't find any suitable example of building such connection in CPP. I would be thankful for any resources :)

2 Answers 2

5

It is possible. You have to specify value 0x03 in field 2 of your client's connection request according to wikipedia description of SOCKS5 protocol.

The client's connection request is
field 1: SOCKS version number, 1 byte (must be 0x05 for this version)
field 2: command code, 1 byte:
0x01 = establish a TCP/IP stream connection
0x02 = establish a TCP/IP port binding
0x03 = associate a UDP port
field 3: reserved, must be 0x00
field 4: address type, 1 byte:
0x01 = IPv4 address
0x03 = Domain name
0x04 = IPv6 address
field 5: destination address of
4 bytes for IPv4 address
1 byte of name length followed by the name for Domain name
16 bytes for IPv6 address
field 6: port number in a network byte order, 2 bytes

As Hasturkun pointed out

Your code doesn't work because you aren't sending a connection request at all. You must send a UDP ASSOCIATE request (on the TCP connection), and you need to use the port and address from the response to get your datagrams relayed.

You should really take a look at Socks5 RFC

8
  • Hi. By saying "it is possible" are you implying that I can send datagram packet through UDP without ustablishing TCP SOCKS5 connection first? I handshaked successfully through SOCK_STREAM but not via SOCK_DGRAM. I simply do not receive anything while listening using recvfrom(). Perhaps I'm doing this wrong or I'm not supposed to receive anything. I assume that I should bind on the same port as I connect to the proxy server?
    – Mike
    Aug 25, 2013 at 13:06
  • You're right. Using UDP limits me to SOCKS5 only as version 4 does not support it. My app should check proxy availability basing on given hosts' list. I thought that by using UDP approach, I will not have to open lots of connections at the same time but simply send single packet and await response but perhaps this is uncommon practice.
    – Mike
    Aug 25, 2013 at 13:49
  • 1
    you might do it, so called Multicast. Wikipedia says: The most common transport layer protocol to use multicast addressing is User Datagram Protocol (UDP).
    – 4pie0
    Aug 25, 2013 at 13:55
  • Thanks. If that's not too much could you take a look at my snippet? I could establish a connection successfully via TCP (I received 0x05 0x00 as exptected) but using UDP I receive nothing (program awaits for some data to be sent) pastebin.com/SnP6drFX
    – Mike
    Aug 25, 2013 at 14:47
  • 1
    Your code doesn't work because you aren't sending a connection request at all. You must send a UDP ASSOCIATE request (on the TCP connection), and you need to use the port and address from the response to get your datagrams relayed. I highly recommend that you read the SOCKS5 RFC as that also defines how the encapsulation is performed (without which you'll get nothing relayed)
    – Hasturkun
    Aug 27, 2013 at 11:39
3

Mike, your code does not work because you are trying to send the UDP associate command through the a UDP datagram. The SOCKS5 handshake must be over a TCP control connection.

Your server may need to keep one open TCP connection per client, but each client does not need many TCP connections open - one TCP connection can handle any number of UDP associate commands.

If your sole purpose is to not use TCP at all on the server side, it will not achieve what you want. The TCP connection is needed so that the SOCKS proxy knows when to disassociate the UDP ports (namely, when the TCP connection goes down).

However, your server application does not need to worry about this at all. The TCP control connection terminates at the SOCKS server just as your diagram shows.

I advise reading the RFC.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.