0

我设置了两个 tun 设备。写入每个 tun 设备的数据使用一个简单的循环通过 UDP 套接字转发到另一个 tun 设备:

// the tuntap device is created using these flags
ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
[...]

fd_set fd_list;


FD_ZERO(&fd_list);
FD_SET(fd1, &fd_list); // fd1 is the tun device
FD_SET(fd2, &fd_list); // fd2 is the udp socket

int fds[] = {fd1, fd2};
while(select(max(fd1, fd2)+1, &fd_list, NULL, NULL, NULL) > -1) {
    for(i = 0; i < 2; ++i)
        if(FD_ISSET(fds[i], &fd_list)) {
            nread = read(fds[i], buf, sizeof(buf));
            assert(nread > 0);

            ret = write(fds[(i+1)%2], buf, nread);
            if(ret == -1)
                perror("write():");
        }
}

使用设置接口后

ifconfig tun0 10.0.0.1
ifconfig tun1 10.0.0.2

我从一台设备向另一台设备发送 ping

ping -I tun1 10.0.0.1

我可以看到 tun0 的 UDP 套接字接收到 IPv4 数据包,并且该数据包已正确写入 tun0。还使用wireshark 观察tun0 上的流量表明该数据包是由tun0 接收的。但是,不会创建 ping 响应数据包。

我认为这可能是 ICMP 数据包的特殊情况,但是当我使用

socat -d -d -d - TCP-LISTEN:2000,so-bindtodevice=tun0 &
sleep 1
echo 2 | socat -d -d -d - TCP:10.0.0.1:2000,so-bindtodevice=tun1

再次没有建立连接。连接过程(第二次 socat 调用)仅继续触发 TCP-SYN 数据包并最终超时。同样,使用 wireshark 观察 tun0 上的流量表明 TCP-SYN 数据包已传递到 tun0 设备。

为什么这个数据包不转发到socat TCP-LISTEN进程以便建立连接?

4

1 回答 1

0

看起来这是一个路由错误。当我在两台不同的机器上运行程序时,数据包分别通过每台机器上的 tun0 设备路由,http://backreference.org/wp-content/uploads/2010/03/simpletun.tar.bz2工作正常。在一台机器上运行程序两次不起作用!

于 2013-10-05T10:36:40.697 回答