gethostbyname双网络接口,选择使用哪一个 [英] gethostbyname dual network interfaces, select which one to use

查看:698
本文介绍了gethostbyname双网络接口,选择使用哪一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个c程序需要连接到服务器并发送一个tcp有效载荷并等待响应。这对于正常使用是很好的,但由于我有两个不同的网络接口,所以我们在运行程序的计算机上调用if0和if1,有时一个网络接口不能转发流量。这是一个事实,我不能改变不幸。



为了处理这个优雅,因为操作系统无法帮助我将数据路由到正确的接口,我发送有效载荷使用if0和if1。我创建一个套接字,我使用绑定将套接字绑定到一个特定的接口,我为这两个if0和if1。



不幸的是,这没有按照我的计划工作gethostbyname,我用来解析主机名当然只使用一个接口。不幸的是,我无法在这个特定问题上修改OS配置,我需要绕过操作系统中的任何逻辑,并确保流量到达特定的界面。



我有以下地址查找代码:

  struct hostent * remoteHostEnt = gethostbyname(hostName); 

有没有什么办法可以使用特定的界面gethostbyname?我想尝试使用gethostbyname,使用if0和if1,并使用任何可接受的结果。



我需要做自己的DNS查找实现,并使用套接字,我可以使用posix c有什么东西吗?或者也许有可以使用的图书馆?

解决方案

如果您无法更改系统配置,您几乎被谴责在您的应用程序中实现自己的DNS解析器。您可以将代码放在 ADNS 上,或者在 Polipo中包含DNS解析器


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?

解决方案

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

这篇关于gethostbyname双网络接口,选择使用哪一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆