Note that there are some explanatory texts on larger screens.

plurals
  1. POhandling a callback with ctypes
    primarykey
    data
    text
    <p>I'm attempting to map libpcap using ctypes to python3.2 and I'm having a problem using callbacks with pcap_loop... here is the code ..</p> <pre><code>class c_int_hack: def from_param(self, *args): return ctypes.c_void_p #void got_packet(u_char *args, const struct pcap_pkthdr *header, # const u_char *packet) CALLBACK=ctypes.CFUNCTYPE(ctypes.c_void_p,c_int_hack,ctypes.pointer(pkthdr),ctypes.POINTER(ctypes.c_ubyte)) #int pcap_loop(pcap_t *p, int cnt, pcap_handler callback, u_char *user) def process(user,pkthdr,packet): print("In callback:") print("pkthdr[0:7]:",pkthdr.contents.len) print("packet6:%2x",packet[6]) print("packet7:%2x",packet[7]) print("packet8:%2x",packet[8]) print("packet9:%2x",packet[9]) print("packet10:%2x",packet[10]) print("packet11:%2x",packet[11]) got_packet=CALLBACK(process) if(pcap_loop(handle,ctypes.c_int(10), got_packet,"what") == -1): err = pcap_geterr(handle) print("pcap_loop error: {0}".format(err)) </code></pre> <p>it seems to have a problem with the 2nd parameter which is the "c_int_hack"</p> <pre><code>Got Required netmask Pcap open live worked! Filter Compiled! Filter installed! Traceback (most recent call last): File "./libpcap.py", line 72, in &lt;module&gt; CALLBACK=ctypes.CFUNCTYPE(ctypes.c_void_p,c_int_hack,ctypes.pointer(pkthdr),ctypes.POINTER(ctypes.c_ubyte)) File "/usr/lib/python3.2/ctypes/__init__.py", line 99, in CFUNCTYPE return _c_functype_cache[(restype, argtypes, flags)] TypeError: unhashable type </code></pre> <p>THis is my first time doing work with ctypes so I'm rather confused at this point. </p> <p>...... EDIT..........</p> <p>So the prototype is trying to accept a pointer to a list of arguments. so I tried doing a pointer to a instance that can take a list but now I'm getting a new error..</p> <pre><code>class ListPOINTER(object): '''Just like a POINTER but accept a list of ctype as an argument''' def __init__(self, etype): self.etype = etype def from_param(self, param): if isinstance(param, (list,tuple)): return (self.etype * len(param))(*param) #void got_packet(u_char *args, const struct pcap_pkthdr *header, # const u_char *packet) args=ListPOINTER() CALLBACK = ctypes.CFUNCTYPE(ctypes.c_void_p,args(ctypes.c_char_p),ctypes.pointer(pkthdr),ctypes.POINTER(ctypes.c_ubyte)) </code></pre> <p>and this is the error</p> <pre><code>Got Required netmask Pcap open live worked! Filter Compiled! Filter installed! Traceback (most recent call last): File "./libpcap.py", line 81, in &lt;module&gt; args=ListPOINTER() TypeError: __init__() takes exactly 2 arguments (1 given) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload