2013-10-02 36 views
0

我设置了两个tun设备。被写入到每个tun设备的数据是使用一个简单的循环转发通过UDP套接字到另一tun设备: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 

我可以看到UDP数据包由tun0的UDP套接字接收,并且此数据包正确写入到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进程,所以它可以建立连接?

回答

相关问题