karpierz / libpcap

Python binding for the libpcap C library.
BSD 3-Clause "New" or "Revised" License
54 stars 14 forks source link

Problem with set_immediate_mode for a not activated pcap_t struct. Error: Struct already activated. #13

Open Kroptokin opened 1 year ago

Kroptokin commented 1 year ago

Hi! I am trying to program a capture script using libpcap library for python. The problem is when I try to activate immediate mode, if gives me the following error:

ERROR = b"The setting can't be changed after the pcap_t is activated"

Some of my code:

`

 #Initialize pcap_t
 func_pcap_t = libpcap.pcap_t()

#Set immediate mode to make timeout work:
if func_wrongSystemConfig == False:
    intRes = libpcap.set_immediate_mode(ctypes.byref(func_pcap_t), ctypes.c_int(1))

    if intRes != 0: #Error
        print("ERROR -  Not possible to set immediate mode!")
        p_errBuff = libpcap.statustostr(intRes)
        print("ERROR = ", repr(p_errBuff))

        func_wrongSystemConfig = True

  #Open Live Interface with promiscuous mode    
if func_wrongSystemConfig == False:

    #pcap_t *pcap_open_live(char *device, int snaplen, int promisc, int to_ms, char *ebuf)
    pcap_device = ctypes.c_char_p(if_name.encode(gl_pcap_encode))
    pcap_snaplen = ctypes.c_int(func_pcap_snaplen)
    pcap_promisc = ctypes.c_int(func_pacp_promisc)
    pcap_timeout = ctypes.c_int(func_pcap_timeout)

    func_pcap_t = libpcap.open_live(pcap_device, pcap_snaplen, pcap_promisc, pcap_timeout, p_errBuff)

    if not func_pcap_t:
        print("ERROR - Could not open live device: ", if_name)
        print("DEBUG - errBuf = ", repr(p_errBuff.value))
        func_wrongSystemConfig = True
    else:
        pass
        #print("SUCCESS - Opening live device: ", if_name) 

`

I also tried to set immediate mode after open_live but gives the same error... What am I doing wrong and how can I fix this issue? The struct pcap_t seems to do not activated but the function does not think so.

Kroptokin commented 1 year ago

More info.. I am working in Pyhton and I create a Process using Python3 Process Library in order to call the function that waits for an arp answer. It seems that the first time the function is called it does not give the error: b"The setting can't be changed after the pcap_t is activated" But all the other calls result in this error. It seems that the function uses the same address memory for the pcap_t struct for all calls of the function. Do you know any way of fixing it? In order to create a new variable for each call to the function where the pcap_t struct is created?

Not even creating a pcap_t struct, closing it witch libpcap.close(pcap_t) and creating another fixes the problem. I have tried with:

 func_pcap_t = libpcap.pcap_t()

AND

  func_pcap_t = libpcap.open_dead(libpcap.DLT_EN10MB, func_pcap_snaplen)

But none of them seem to work... always after the first call, which seems to work, gives the error message given above.

Kroptokin commented 1 year ago

Okay... it seems solved using libpcap.create() function instead of open_dead() or simply creating an struct with libpcap.pcap_t()