$NetBSD: patch-ac,v 1.1.1.1 2010/03/13 10:20:53 pettai Exp $ # Apply in SVN repo fixes # --- pcap.pyx.orig 2005-10-17 01:00:11.000000000 +0200 +++ pcap.pyx 2010-02-14 17:49:27.000000000 +0100 @@ -20,6 +20,7 @@ cdef extern from "Python.h": object PyBuffer_FromMemory(char *s, int len) + int PyObject_AsCharBuffer(object obj, char **buffer, int *buffer_len) int PyGILState_Ensure() void PyGILState_Release(int gil) void Py_BEGIN_ALLOW_THREADS() @@ -134,16 +135,18 @@ raise IOError, 'bad filter' def filter(self, buf): """Return boolean match for buf against our filter.""" + cdef char *p cdef int n - n = len(buf) - if bpf_filter(self.fcode.bf_insns, buf, n, n) == 0: + if PyObject_AsCharBuffer(buf, &p, &n) < 0: + raise TypeError + if bpf_filter(self.fcode.bf_insns, p, n, n) == 0: return False return True def __dealloc__(self): pcap_freecode(&self.fcode) cdef class pcap: - """pcap(name=None, snaplen=65535, promisc=True, immediate=False) -> packet capture object + """pcap(name=None, snaplen=65535, promisc=True, immediate=False, timeout_ms=None) -> packet capture object Open a handle to a packet capture descriptor. @@ -153,15 +156,19 @@ snaplen -- maximum number of bytes to capture for each packet promisc -- boolean to specify promiscuous mode sniffing immediate -- disable buffering, if possible + timeout_ms -- requests for the next packet will return None if the timeout + (in milliseconds) is reached and no packets were received + (Default: no timeout) """ cdef pcap_t *__pcap cdef char *__name cdef char *__filter cdef char __ebuf[256] cdef int __dloff + cdef int __timeout_returns_none def __init__(self, name=None, snaplen=65535, promisc=True, - timeout_ms=500, immediate=False): + timeout_ms=None, immediate=False): global dltoff cdef char *p @@ -172,6 +179,12 @@ else: p = name + if timeout_ms is None: + timeout_ms = 500 + self.__timeout_returns_none = 0 + else: + self.__timeout_returns_none = 1 + self.__pcap = pcap_open_offline(p, self.__ebuf) if not self.__pcap: self.__pcap = pcap_open_live(pcap_ex_name(p), snaplen, promisc, @@ -340,6 +353,8 @@ if n == 1: return (hdr.ts.tv_sec + (hdr.ts.tv_usec / 1000000.0), PyBuffer_FromMemory(pkt, hdr.caplen)) + elif n == 0 and self.__timeout_returns_none == 1: + return (hdr.ts.tv_sec + (hdr.ts.tv_usec / 1000000.0), None) elif n == -1: raise KeyboardInterrupt elif n == -2: