problem with a timeout

Bartosz SKOWRON <[email protected]> Sat, 26 Sep 2009 04:55:07 +0200
Newsgroups gmane.comp.python.pyrex
Message-ID <[email protected]>
hello,

i know that this issue is probably more internal issue than overall
pyrex but still.
I'm not an author of this project (http://code.google.com/p/pypcap/)
but i use it extensive and wrote some patch for it (in pyrex).
this time, i have to give up and ask you for help.

here is a suitable pyrex code (i cut to the most important parts, you
can check all the source code at code.google.com):

cdef class pcap:
    cdef pcap_t *__pcap
    def __init__(self, name=3DNone, snaplen=3D65535, promisc=3DTrue,
                 timeout_ms=3D500, immediate=3DFalse):
        if not self.__pcap:
            self.__pcap =3D pcap_open_live(pcap_ex_name(p), snaplen, promis=
c,
                                         timeout_ms, self.__ebuf)
    def __next__(self):
        cdef pcap_pkthdr *hdr
        cdef char *pkt
        cdef int n
        while 1:
            Py_BEGIN_ALLOW_THREADS
            n =3D pcap_ex_next(self.__pcap, &hdr, &pkt)
            Py_END_ALLOW_THREADS
            self._hdr =3D hdr
            self._pkt =3D pkt
            if n =3D=3D 1:
                return (hdr.ts.tv_sec + (hdr.ts.tv_usec / 1000000.0),
                        PyBuffer_FromMemory(pkt, hdr.caplen))
            elif n =3D=3D -1:
                raise KeyboardInterrupt
            elif n =3D=3D -2:
                raise StopIteration


the problem is with a timeout. basically it doesn't work. it waits
till a packet arrive.

import pcap
descr =3D pcap.pcap("eth0", timeout_ms=3D10000)
descr.setfilter("host 1.1.1.1")
descr.next()

i checked in C:
#include <pcap.h>
int main(void) {
	pcap_t* descr;
	char errbuf[PCAP_ERRBUF_SIZE];
	struct pcap_pkthdr *hdr;
	struct bpf_program fp;
	bpf_u_int32 netp;
	const u_char *data;

	descr =3D pcap_open_live("eth0", BUFSIZ, 0, 10000, errbuf);
	pcap_compile(descr, &fp, "host 1.1.1.1", 0, netp);
	pcap_setfilter(descr, &fp);
	pcap_next_ex(descr, &hdr, &data);
	return 1;
}

and it's working

any idea how to fix it?