Sockets tun设备:服务器进程未接收到消息
我设置了两个tun设备。写入每个tun设备的数据通过UDP套接字使用简单循环转发到另一个tun设备:Sockets tun设备:服务器进程未接收到消息,sockets,udp,tun,Sockets,Udp,Tun,我设置了两个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,
// 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():");
}
}
我从一个设备向另一个设备发送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进程,以便它可以建立连接???看起来这是一个路由错误。 当我在两台不同的机器上运行该程序时,数据包分别通过每台机器上的tun0设备进行路由,工作正常。在一台机器上运行两次程序不起作用
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