Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I use TUN/TAP to create a tun NIC tun0 with IP, and I have a real NIC eth0 with IP

Then I run a TCP client which binds its socket to and a TCP server binding but I can't establish a tcp connection between them.

So can a TCP client on one NIC connect to a tcp server on the other NIC?

share|improve this question
up vote 0 down vote accepted

Packets to local addresses will always run on the lo interface, they will never leave the machine. (Packets to local addresses arriving on non-lo interfaces are regarded as martians.)

In other words your packets must get stuck on the lo interface somewhere, possibly due to a firewall. Check your iptables-save output and to a tcpdump on the lo interface for those packets to debug this issue.

share|improve this answer
ah, I got it, since the IPs are all local, the packets will be sent to lo but not tun0. If I use raw socket to modify the source IP to an non-local IP, for example, I use raw socket to send a packet to the TCP server on, will the server react a TCP SYN/ACK destined to that non-local IP. Can I hook/intercept the SYN/ACK packet(including TCP/IP Header)? thanks – misteryes May 17 '13 at 22:30
The packets destined to remote destinations will be sent according to the machine's routing table. You could of course use promisc mode on the network interface or set up a NAT to a local address, however you may want to take a look at something like LXC. It allows you to run two minimalistic virtual machines with separate network stacks. – Janoszen May 18 '13 at 13:49
no, what I want to do is: I have a tcp server listening on is the tun0), and I open a socket descriptor to the tun0. Then I can use the socket descriptor to write TCP SYN packet to the socket descriptor and read the responding TCP SYN/ACK packet. Is this usage correct? thanks! – misteryes May 18 '13 at 17:30

Neither the TCP client nor the TCP server are "on" any NICs. Individual interfaces are considered at layers 1 and 2 while TCP operates at layer 4. Except for very special socket options like SO_BINDTODEVICE, TCP endpoints are not attached to any network interface.

Your TCP server and client just happen to be bound to different local IP addresses. Connecting between them should work just fine.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .