--- pcapobj.cc.orig 2008-12-22 03:29:10.000000000 +0000 +++ pcapobj.cc 2008-12-22 03:51:16.000000000 +0000 @@ -193,31 +193,44 @@ static PyObject* p_next(register pcapobject* pp, PyObject*) { - struct pcap_pkthdr hdr; - const unsigned char *buf; - if (pp->ob_type != &Pcaptype) { PyErr_SetString(PcapError, "Not a pcap object"); return NULL; } + struct pcap_pkthdr *hdr; + const unsigned char *buf; + int ret; + // allow threads as this might block Py_BEGIN_ALLOW_THREADS; - buf = pcap_next(pp->pcap, &hdr); + ret = pcap_next_ex(pp->pcap, &hdr, &buf); Py_END_ALLOW_THREADS; - if(!buf) + if(ret != 1) { - PyErr_SetString(PcapError, pcap_geterr(pp->pcap)); - return NULL; + // An error occurred while reading the packet + if(ret == -1) + { + PyErr_SetString(PcapError, pcap_geterr(pp->pcap)); + return NULL; + } + // ret equals to 0 if packets are being read from a live capture + // and the timeout expired, or -2 if packets are being read from + // a 'savefile' and there are no more packets to read + else + { + Py_INCREF(Py_None); + Py_None; + } } - PyObject *pkthdr = new_pcap_pkthdr(&hdr); - if (pkthdr) + PyObject *pkthdr = new_pcap_pkthdr(hdr); + if (pkthdr) { PyObject *ret = NULL; - ret = Py_BuildValue("(Os#)", pkthdr, buf, hdr.caplen); + ret = Py_BuildValue("(Os#)", pkthdr, buf, hdr->caplen); Py_DECREF(pkthdr); return ret; }