gethostbyname dual network interfaces, select which one to use

旧时模样 提交于 2019-12-10 18:56:27

问题


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?


回答1:


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




回答2:


You can make DNS queries with a specific network interface using c-ares (as shown in c-ares specifying network interface for the DNS resolves).

There is a c-ares example at How do I resolve an IP into host using c-ares?.



来源:https://stackoverflow.com/questions/30012920/gethostbyname-dual-network-interfaces-select-which-one-to-use

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!