Link local question: selecting specific network adapter when creating socket

Nov 30, 2011 at 3:57pm
Hi all,

I am currently having an issue when creating a socket to connect to multiple tcp devices and udp devices using a specific adapter. Here's the scenario:

1. I have 1 pc with 2 network adapters eth0 and eth1.
2. I have 4 devices on eth0 through a network switch, 2 of the devices are using udp, and 2 devices are using tcp.
3. eth0 is setup to use the link local only through linux.
4. eth1 is DHCP on the network through linux.

The issue is, my 2 tcp devices that I am trying to communicate on eth0 (link-local) both require static IP address's (non link-local) on the link local network adapter...

So, I guess my question is, is there a way to create a socket linked to a specific network adapter (eth0) so that when I try to connect to the tcp devices the two adapters do not get confused...?

here's the code I am using to create the socket to connect to the tcp devices...



int length;

thissocket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
if ( thissocket < 0 )
	return -2;

// Connect
tcpsocket->sin_family = AF_INET;
tcpsocket->sin_addr.s_addr = inet_addr( strAddress );
tcpsocket->sin_port = htons( DEST_PORT );
length = sizeof(struct sockaddr_in);

if ( connect( thissocket, (struct sockaddr *)tcpsocket, length ) < 0)
	return -1;

return 0;



note: the ip address of the 2 tcp devices on the link local network are on the same gateway as the second network adapter set up for DHCP. I was under the assumption it would be ok to using a static IP with the link local configuration, but after testing, it failed to communicate with the devices...But in a way, it makes sense due to the difference in gateway address's.
Last edited on Nov 30, 2011 at 4:05pm
Nov 30, 2011 at 7:29pm
If I've understood the problem correctly, I think this is more of a networking issue than a programming issue. I also don't think the socket layer lets you force traffic over an interface as that is the job of the kernel's routing table.

I'm not a network guru by any definition, however, this is what I'd ask myself in your situation:

1) What are the ips of the two devices?
2) Is there a route (other than default gateway) on your box that goes to the ips in (1)?
3) Does the route in (2) send the packets to eth0?
4) Do the devices know that the link local address of eth0 doesn't require routing and is on the local ethernet?

1,2,3 will get data to the device, but you will also need 4 to get a reply (hence for tcp to work).

Why don't you give the devices and eth0 a static ip from rfc1918? That way everything will just work.
Nov 30, 2011 at 11:52pm
The IP address of the device you try to connect to will determine which network card is used via the routing table. As long as you have the correct IP address the correct network adapter will be used (assuming your routing table is okay).
Last edited on Nov 30, 2011 at 11:53pm
Dec 1, 2011 at 2:09pm
I have also wondered about having 2 network adapters on same pc. If you then create a udp socket on the pc, is it only tied to one of the network adapters, ie. usually when you create multiple udp sockets on same pc, each socket intstance is bound to a different port number.

So the theorectical maximum number of udp sockets one could start up on a pc is [65535] - with 2 network adapters one who think that this number would double, but I assume not, because then we would have 2 udp sockets having the same port number - is this the case?

Would this then imply that regardless of the number of network adapters we have connected to a pc, we are still constrained by the logic of the communication protocol as seen above?
Dec 1, 2011 at 3:58pm
I'm not a networks expert but here is how I think it all works:

Each network adapter is its own network with its own range of IP numbers segregated from the other network adapters' IP numbers via the netmask. The network adapter is assigned one IP number from the range available to it and any devices on its network must also have an IP assigned from the same range.

So when your computer has two adapters it has two networks each with an independent range of IP numbers. I assume that means that each network adapter gets the full range of ports to communicate over.

When your application tries to connect to an IP over a specific port the routing table finds which adapter provides the correct range of IP numbers to service the request and then passes it to the relevant network (adapter).

So if I have 2 adapters (networks) I can configure it like this:

eth0: IP RANGE 192.168.1.* MASK: 255.255.255.0

eth1: IP RANGE 192.168.2.* MASK: 255.255.255.0

So eth0 services all IP numbers starting with 192.168.1 and eth1 services all IP numbers starting with 192.168.2

Then when I try to connect to IP 192.168.2.25:20971 the routing table works out that this request goes out on eth1.

If the request is for an IP that is not contained in any of the adapters' ranges then the routing table can specify one of the adapters as the 'gateway' for requests bound for an external (unrecognised) network. One of the devices connected to the gateway network should properly route the request.

Dec 1, 2011 at 9:00pm
Galik, if your theory is true (which im sure it is), then if the network is set to link local on the switch hooked into eth0, static IP's are not possible due to the gateway mismatches between and the local IP configuration on the switch with the given configuration?

Which I guess proves the point that static IP's cannot be used on a link local network (that was really the underlying question in the back of my head).

So the specific network adapter that your socket is connected to is set by the default gateway that your specified IP address is on in your OS settings in the routing table?

Sounds logical to me! :)
Dec 2, 2011 at 1:55am
The issue is, my 2 tcp devices that I am trying to communicate on eth0 (link-local) both require static IP address's (non link-local) on the link local network adapter...

A link local address is a 169.254 address. There shouldn't be a route from that link-local adapter to a non-link-local address.

It would to try to go out on eth1 as local means local. Of course, there's no route to those non-link-local addresses you've connected eth0 to, so the connect should fail.

It's sort of like asking to get from Bermuda to New York by bus. There's no route. Your travel agent wouldn't consider the bus, they'd try to book a flight.
Last edited on Dec 2, 2011 at 2:05am
Topic archived. No new replies allowed.