C++ – How to tunnel TCP over reliable UDP

cnetwork-programmingnetworkingsockets

Assume I have a reliable UDP library, and want to tunnel arbitrary TCP connections over it. This is my current approach to doing so, but I feel that it may not be very efficient. Any suggestions are very welcome.

  1. Client establishes a reliable UDP connection to server.
  2. Client runs a local SOCKS5 proxy, which receives data from any app that connects to it and forwards it through the reliable UDP connection. Each packet includes a 4-byte id unique to each SOCKS connection.
  3. Server receives data. If the 4-byte id is new, it makes a new connection to its local TCP socket and sends the data, and spawns a new thread which receives any replies from the server and forwards them through the reliable UDP connection with the appropriate id. If the 4-byte id is old, it simply sends the data over the existing TCP connection.
  4. Client receives data, sending it over the existing SOCKS connection to whatever app started it.

Right now, this seems to work for making simple HTML requests from a browser, but since the server isn't directly connected to the client, it is unable to tell when the client terminates a connection. Is there a better way to do this?

EDIT: No, this is not homework. And please don't bother replying if you aren't aware of the advantages of reliable UDP libraries, or for that matter, haven't heard of them before. Thanks.

Best Answer

there are a few ready to use options:

  • OpenVPN: tunnels either IP or ethernet Frames on top of UDP
  • Teredo: tunnels IPv6 on top of UDPv4, manages both NAT traversing and full compatibility with IPv6
  • UDT: non-standard, reliable, high-performance, multi-transport, TCP-like protocol on top of UDP. Optionally lets you manage NAT traversal and then takes it from there