1

When you write something like:

   s=socket(AF_INET,SOCK_STREAM)
   s.connect((siteIp,80))
   s.send(...

How does the linux kernel know from which nic it is supposed to send the packets when no information about binding the socket is provided.

How can i for example replace this default nic with my own tun/tap interface?

Also where can i get more in depth information about this topic?

sia
  • 133
  • 1
  • 7

1 Answers1

0

The OS uses its internal routing tables to make a best guess about which NIC has the best chance of reaching the IP you are connecting to. Routes are associated with NICs and priorities. If an outbound packet has a destination IP that matches a given route, the associated NIC is used. Otherwise, a route with an appropriate priority (such as a gateway or VPN) is used, since the packet IP may be on another network reached through that route.

If you want to specify which NIC is used, you have to bind() before you connect().

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770