似乎没有调用ip规则(ip rule does not seem to be invoked)

编程入门 行业动态 更新时间:2024-10-24 20:15:12
似乎没有调用ip规则(ip rule does not seem to be invoked)

我在本地机器上有两个连接,一个是通过eth0(有一个静态IP,让它是10.10.10.10),另一个是通过ppp0(有一个dinamic IP,例如10.20.30.40)。 两者都可以访问互联网。 我还有一个远程服务器(让我们假设它有IP 1.2.3.4)我想从我的本地机器连接到这样的方式,使得源地址为10.10.10.10(eth0)的数据包应该通过eth0,而那些有10.20的数据包。 .30.40(ppp0)作为源地址应该通过ppp0。

初步我删除了主路由表的ip规则,以防止此规则处理数据包。

之后,我创建了两个路由表,一个用于eth0(名为ETH),另一个用于ppp0(名为PPP)。 然后我添加了这些表的路由,如下所示:

ip route add default dev ppp0 table PPP ip route add default dev eth0 table ETH

然后我添加了ip规则如下:

ip rule add from 10.10.10.10 lookup ETH ip rule add from 10.20.30.40 lookup PPP

它不起作用。 但是,当我使用ip rule add from all而不是指向特定的IP时,它可以工作(当然,在这种情况下,数据包只通过一个接口)。 所以,当我指定IP时,似乎没有调用ip规则。

这种奇怪行为可能是什么原因?

有没有人有想法?

I have two connections on my local machine, one is via eth0 (has a static IP, let it be 10.10.10.10), another is via ppp0 (has a dinamic IP, for example 10.20.30.40). Both have access to the Internet. I also have a remote server (let's assume it has IP 1.2.3.4) to which I want to connect from my local machine in such a way that packets having source address 10.10.10.10 (eth0) should go through eth0, while those having 10.20.30.40 (ppp0) as a source address should go through ppp0.

Preliminary I deleted ip rule for main route table in order to prevent packets being treated by this rule.

After that, I created two route tables, one for eth0 (named ETH) and another for ppp0 (named PPP). Then I added routes to these tables as follows:

ip route add default dev ppp0 table PPP ip route add default dev eth0 table ETH

Then I added ip rules as followings:

ip rule add from 10.10.10.10 lookup ETH ip rule add from 10.20.30.40 lookup PPP

And it doesn't work. However, when I use ip rule add from all instead of pointing to a specific IP it works (sure, packets go through only one interface in this case). So, it seems like ip rule is not invoked when I specify IP.

What could be a reason of such strange behavior?

Does anyone have ideas?

最满意答案

我找到了原因。 此问题的原因是一个程序(使用QT QTcpSocket :: bind()编写),它使用适当的接口绑定套接字,但不绑定路由设置。 由于未知原因,它不能正确地做到这一点。 我尝试使用原生的linux套接字函数,现在它像发条一样运行。

I found the reason. The cause of this problem is a program (written with usage of QT QTcpSocket::bind()) that binds a socket with an appropriate interface, but not the routing settings. It does not do that properly for unknown reason. I tried using native linux socket functions and now it runs like clockwork.

更多推荐

本文发布于:2023-08-06 16:13:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1451569.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:规则   ip   invoked   rule

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!