Search code examples
cnetwork-programmingdnsposixgethostbyname

gethostbyname dual network interfaces, select which one to use


I have a c-program that needs to connect to a server and send a tcp payload and wait for the response. This works well for normal use but since I have two different network interfaces, let us call them if0 and if1 on the computer running the program, sometimes one of the network interfaces are not able to forward the traffic. This is a fact that I cannot change unfortunately.

To handle this gracefully since the OS cannot help me route the data to the correct interface, I send the payload using both if0 and if1. I create a socket and I use bind to bind the socket to a specific interface and I do that for both if0 and if1.

Unfortunately this did not work as I planned because gethostbyname that I use to resolve the host name used only one interface of course. Unfortunately I am not able to make changes to the OS config at this particular problem, I need to bypass any logic in the OS and make sure traffic goes to a specific interface.

I have the following code for address lookup:

struct hostent *remoteHostEnt = gethostbyname(hostName);

Is there any way that I can make gethostbyname using a specific interface as well? I would like to try using gethostbyname using both if0 and if1 and use any acceptable result.

Do I need to make my own DNS lookup implementation and use a socket for this or is there something in posix c I can use? Or maybe there is a library I can use?


Solution

  • If you cannot change the system configuration, you're pretty much condemned to implementing your own DNS resolver within your application. You could base your code on ADNS, or on the DNS resolver included in Polipo