[Pyrex] problem with a timeout

Bartosz SKOWRON getxsick at gmail.com
Sat Sep 26 04:55:07 CEST 2009


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=None, snaplen=65535, promisc=True,
                 timeout_ms=500, immediate=False):
        if not self.__pcap:
            self.__pcap = pcap_open_live(pcap_ex_name(p), snaplen, promisc,
                                         timeout_ms, self.__ebuf)
    def __next__(self):
        cdef pcap_pkthdr *hdr
        cdef char *pkt
        cdef int n
        while 1:
            Py_BEGIN_ALLOW_THREADS
            n = pcap_ex_next(self.__pcap, &hdr, &pkt)
            Py_END_ALLOW_THREADS
            self._hdr = hdr
            self._pkt = pkt
            if n == 1:
                return (hdr.ts.tv_sec + (hdr.ts.tv_usec / 1000000.0),
                        PyBuffer_FromMemory(pkt, hdr.caplen))
            elif n == -1:
                raise KeyboardInterrupt
            elif n == -2:
                raise StopIteration


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

import pcap
descr = pcap.pcap("eth0", timeout_ms=10000)
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 = 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?



More information about the Pyrex mailing list