非阻塞tun/tap文件描述符上的read()收到EAGAIN错误 [英] read() on a NON-BLOCKING tun/tap file descriptor gets EAGAIN error

查看:212
本文介绍了非阻塞tun/tap文件描述符上的read()收到EAGAIN错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从无阻塞的tun/tap文件描述符tunfd中读取IP数据包 我将tunfd设置为非阻塞,并在libevent中为其注册了READ_EV事件.

I want to read IP packets from a non-blocking tun/tap file descriptor tunfd I set the tunfd as non-blocking and register a READ_EV event for it in libevent.

触发事件时,我先读取前20个字节以获取IP标头,然后 阅读其余内容.

when the event is triggered, I read the first 20 bytes first to get the IP header, and then read the rest.

nr_bytes = read(tunfd, buf, 20);
...
ip_len = .... // here I get the IP length
....
nr_bytes = read(tunfd, buf+20, ip_len-20);

,但对于read(tunfd, buf+20, ip_len-20) 我收到EAGAIN错误,实际上应该有一个完整的数据包, 所以应该有一些字节, 为什么我会收到这样的错误?

but for the read(tunfd, buf+20, ip_len-20) I got EAGAIN error, actually there should be a full packet, so there should be some bytes, why I get such an error?

tunfd与非阻塞模式或libevent不兼容吗?

tunfd is not compatible with non-blocking mode or libevent?

谢谢!

推荐答案

使用TUN/TAP进行读写,就像在数据报套接字上进行读写一样,必须用于完整的数据包.如果读入的缓冲区太小而无法容纳完整的数据包,则缓冲区将被填满,其余的数据包将被丢弃.对于写操作,如果您写入部分数据包,驱动程序会认为它是完整数据包,并通过隧道设备传递截断的数据包.

Reads and writes with TUN/TAP, much like reads and writes on datagram sockets, must be for complete packets. If you read into a buffer that is too small to fit a full packet, the buffer will be filled up and the rest of the packet will be discarded. For writes, if you write a partial packet, the driver will think it's a full packet and deliver the truncated packet through the tunnel device.

因此,当您读取TUN/TAP设备时,必须提供至少与tuntap接口上配置的MTU一样大的缓冲区.

Therefore, when you read a TUN/TAP device, you must supply a buffer that is at least as large as the configured MTU on the tun or tap interface.

这篇关于非阻塞tun/tap文件描述符上的read()收到EAGAIN错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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