Question: Tor can only handle TCP connections, but DNS is a UDP protocol. How does Tor route DNS requests over its TCP based network? Why can the same approach not be used to route all UDP traffic over Tor?

Answer: 
Tor clients do not, in general, directly do DNS requests. When you open a connection through the tor network you usually ask your client (on its socks ports) to connect you to a hostname and port, say www.example.com:80.

Your tor client, once it has created a circuit, will send that hostname and port to the exit node in its [RELAY\_BEGIN cell](https://gitweb.torproject.org/torspec.git/tree/tor-spec.txt?id=f9e111ead769d48441d99b52039ef0ccbd3f2c62#n1311).

The exit node will then do a DNS resolve and open a TCP connection to the target. Once that's established it will tell your client that the connection is open, and for informational purposes will also tell your client what address that hostname resolved to.

Tor, in addition to that, has some sort of "remote DNS resolve" protocol built in. This support allows clients to [query](https://gitweb.torproject.org/torspec.git/tree/tor-spec.txt?id=f9e111ead769d48441d99b52039ef0ccbd3f2c62#n1475) for certain resource types, like IPv4 or IPv6 addresses. Here too, the exit node does the actual DNS packet sending and receiving and just relays the answer to the client.

No UDP packets are actually ever routed from the client.

There's also a [proposal to add more full featured DNS support](https://gitweb.torproject.org/torspec.git/tree/proposals/219-expanded-dns.txt) to Tor. So far it hasn't been implemented.

---

Tor Protocol Specification references

* [6.2. Opening streams and transferring data](https://gitweb.torproject.org/torspec.git/tree/tor-spec.txt?id=f9e111ead769d48441d99b52039ef0ccbd3f2c62#n1311)
* [6.4. Remote hostname lookup](https://gitweb.torproject.org/torspec.git/tree/tor-spec.txt?id=f9e111ead769d48441d99b52039ef0ccbd3f2c62#n1475)
* [Proposal 219 - Support for full DNS and DNSSEC resolution in Tor](https://gitweb.torproject.org/torspec.git/tree/proposals/219-expanded-dns.txt)
TOR is not capable of routing UDP traffic as far as I know. But for DNS it makes an exception. This can be done because of the way SOCKS5 works.

When establishing a SOCKS5 connection the client sends either the IP address or a DNS. When TOR receives the DNS it uses the backend to resolve it (I don't really know exactly how it does it but I guess it's really similar to TCP and an endpoint resolves it for you) and then it can continue using the regular TCP/IP tor connection.

This way TOR is able to provide hidden services as .onion DNS, as it is receiving DNS connections over SOCKS5. This is the reason why you leak DNS (and it is not possible to use .onion DNS) over SOCKS4 (it does not support DNS resolution).