0

I use funfd to read packets from a tun socket, which is registered as an even callback function with libevent.

at first I read the first 20 bytes to check the IP header, and then read the following bytes:

   nr_bytes = read(tunfd, buf, 20);
    assert(nr_bytes == 20);
    validate_ip(buf);   

    /* get the IP packet length and read the whole packet */
    iphdr = (struct ip *)buf;
    ip_len = ntohs(iphdr->ip_len);
    iphdr_len = iphdr->ip_hl*4;

    nr_bytes = read(tunfd, buf+20, ip_len-20);
    if(-1 == nr_bytes) perror("readtun() failed:\n");

but for the second read, read(tunfd, buf+20, ip_len-20), I get "Resource temporarily unavailable", what are potential problems? and how to deal with it?

lily
  • 515
  • 7
  • 20
  • Possible duplicate of [read() on a NON-BLOCKING tun/tap file descriptor gets EAGAIN error](http://stackoverflow.com/questions/17138626/read-on-a-non-blocking-tun-tap-file-descriptor-gets-eagain-error) – engineerC Oct 19 '15 at 17:58
  • I don't think you fully understand that you really are reading packets. There aren't two packets, so why should two reads succeed? – David Schwartz Oct 19 '15 at 18:03
  • Hm, it looks like the man page says nothing on the issue. Sorry about the misleading pointer. – n. m. could be an AI Oct 19 '15 at 19:26

1 Answers1

0

You really are reading packets, not bytes. So unless there are two packets, two reads won't succeed.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278