Hi.

Guy Harris wrote:
> 
> On Tue, Jul 16, 2002 at 09:21:17AM -0700, Fulvio Risso wrote:
> > We're going to release a preview of the new WinPcap which has a new
> > pcap_read_ex(), which does what you're looking for.
> 
> It should also be possible to implement "pcap_setnonblock()" and
> "pcap_getnonblock()" for WinPcap.

They are already implemented.
The fact is that we believe the actual API is a bit confusing, so in our
docs we suggest not to use these calls.
Instead, we defined a pcap_read_ex() which reads a packet at a time
(like pcap_next() ), it is non blocking, and returns 

- (1) if a packet has been read without problems
- (0) if the timeout set with pcap_open_live() has elapsed. In this case
pkt_header and pkt_data don't point to a valid packet
- (-1) if an error occurred
- (-2) if EOF was reached reading from an offline capture

We believe this is much simpler than having to deal with noblocking and
such things.

        fulvio


> 
> Add "int" fields "timeout" and "nonblock" to the set of Win32-specific
> fields in a "struct pcap".
> 
> In "pcap_open_live()", set "timeout" to the "to_ms" argument and set
> "nonblock" to 1 if "to_ms" is -1 and 0 otherwise.
> 
> "pcap_setnonblock()" and "pcap_getnonblock()" would be
> 
> /*
>  * NOTE: in the future, these may need to call platform-dependent routines,
>  * e.g. on platforms with memory-mapped packet-capture mechanisms where
>  * "pcap_read()" uses "select()" or "poll()" to wait for packets to arrive.
>  */
> int
> pcap_getnonblock(pcap_t *p, char *errbuf)
> {
> #ifndef WIN32
>         int fdflags;
> #endif
> 
>         if (p->sf.rfile != NULL) {
>                 /*
>                  * This is a savefile, not a live capture file, so
>                  * never say it's in non-blocking mode.
>                  */
>                 return (0);
>         }
> #ifndef WIN32
>         fdflags = fcntl(p->fd, F_GETFL, 0);
>         if (fdflags == -1) {
>                 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "F_GETFL: %s",
>                     pcap_strerror(errno));
>                 return (-1);
>         }
>         if (fdflags & O_NONBLOCK)
>                 return (1);
>         else
>                 return (0);
> #else
>         return (p->nonblock);
> #endif
> }
> 
> int
> pcap_setnonblock(pcap_t *p, int nonblock, char *errbuf)
> {
> #ifndef WIN32
>         int fdflags;
> #else
>         int newtimeout;
> #endif
> 
>         if (p->sf.rfile != NULL) {
>                 /*
>                  * This is a savefile, not a live capture file, so
>                  * ignore requests to put it in non-blocking mode.
>                  */
>                 return (0);
>         }
> #ifndef WIN32
>         fdflags = fcntl(p->fd, F_GETFL, 0);
>         if (fdflags == -1) {
>                 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "F_GETFL: %s",
>                     pcap_strerror(errno));
>                 return (-1);
>         }
>         if (nonblock)
>                 fdflags |= O_NONBLOCK;
>         else
>                 fdflags &= ~O_NONBLOCK;
>         if (fcntl(p->fd, F_SETFL, fdflags) == -1) {
>                 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "F_SETFL: %s",
>                     pcap_strerror(errno));
>                 return (-1);
>         }
> #else
>         if (nonblock) {
>                 /*
>                  * Set the read timeout to -1 for non-blocking mode.
>                  */
>                 newtimeout = -1;
>         } else {
>                 /*
>                  * Restore the timeout set when the device was opened.
>                  * (Note that this may be -1, in which case we're not
>                  * really leaving non-blocking mode.)
>                  */
>                 newtimeout = p->timeout;
>         }
>         if (!PacketSetReadTimeout(p->adapter, newtimeout)) {
>                 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
>                     "PacketSetReadTimeout: %s", pcap_win32strerror());
>                 return (-1);
>         }
>         p->nonblock = (newtimeout == -1);
> #endif
>         return (0);
> }
> -
> This is the TCPDUMP workers list. It is archived at
> http://www.tcpdump.org/lists/workers/index.html
> To unsubscribe use mailto:[EMAIL PROTECTED]?body=unsubscribe
-
This is the TCPDUMP workers list. It is archived at
http://www.tcpdump.org/lists/workers/index.html
To unsubscribe use mailto:[EMAIL PROTECTED]?body=unsubscribe

Reply via email to