[haiku-development] DHCP and binding to a non-configured interface.

After a long time with DHCP not working for me, I decided to 
investigate why this was happening. It turns out that I have the 2 
network cards and only the second one is connected to the network and 
the problem will only show up in this configuration.

What happens is that when binding to interfaces, DHCPClient is actually 
trying to bind to INADDR_ANY (i.e. all  available interfaces). This 
works fine when you have only one interface or the first interface is 
the only one connected to the network.

In my specific case what happens is that it bind() succeeds in the for 
the first interface (but it has no cable connected to it) but will fail 
on the second one with "Address already in use", as it is trying again 
to bind to the same port on all interfaces.

Just after the bind call there is some code that tries to bind to a 
specific device using SO_BINDTODEVICE but, in my case, this code is 
never reached on teh second interface because the bind() call fails.

I tried the obvious things like removing the bind call (does not work 
as we need a local port and I am not sure we can somehow use SO_
BINDTODEVICE to bind to a specific port) or setting SO_REUSEADDR on the 
socket (which is obviously a hack but, interesting enough, resulted in 
the same error which was unexpected).

So, to make a long story short, how does one bind to a device 
(specifying a port) directly using the Haiku stack? if there is no way 
to do that, the solution will be to serialize interface configuration.

-Bruno

Other related posts: