0

I've been scouring the web to see how I can program to utilize two separate ethernet ports (such as eth0 and eth1) in linux using C/C++

I understand how to use socket() and inet_pton but this seems to be only for a single ethernet port.

The first ethernet port is automatically mapped to 192.168.0.100 and the second is 192.168.0.101

user1257629
  • 45
  • 3
  • 7

1 Answers1

3

Two part answer.

  1. Normally, you don't have to worry about using multiple ethernet ports explicitly. The kernel automatically takes care of it. Here's how it works. For outgoing packets, the kernel sends the packets out over the appropriate ethernet port depending on the destination IP address. So, if network 10.0.0.0 is reachable via eth0, that's where the packet will be sent out from. And if network 11.0.0.0 is reachable via eth1, that's where the packet will be sent out from. The kernel determines network reachability by the netmask, IP address, and default gateway assigned to an interface (in conjunction with routing tables).
  2. For incoming packets, if you bind to ANY address, then all packets for the port you are listening on are delivered to you. You can also choose to bind to a specific IP address, in which case only the packets addressed to that IP address and port will be delivered to you.
Ziffusion
  • 8,779
  • 4
  • 29
  • 57
  • Interesting! is it possible to give a tiny code snippet as an example? (I understand best using examples) it doesn't have to be complex, just a general gist. – user1257629 Jun 07 '13 at 17:15
  • 1
    Well, for sending the destination address is what you specify in connect (TCP) or send (UDP). The kernel routes based on that address. For receiving its the address you specify in bind. ANY is 0. See examples here http://www.cs.odu.edu/~cs476/fall03/lectures/sockets.htm. There are million more on the net. Just search for "socket examples". This book is good http://www.amazon.com/UNIX-Network-Programming-Richard-Stevens/dp/0139498761/ref=pd_sim_b_4. – Ziffusion Jun 07 '13 at 19:19