我在 python 中通过 ctypes 使用 libpcap。我写了一个小包装器,所以我可以更方便地访问 pcap 函数。例如 pcap_geterr 它看起来像
# char  *pcap_geterr(pcap_t *p);
geterr = _pcap.pcap_geterr
geterr.argtypes = [ctypes.c_void_p]
geterr.restype = ctypes.c_char_p
但是现在我需要使用 pcap_stats 并且在将 pcap_stat 结构传递给函数时遇到了麻烦。
代码看起来像
class pcap_stat(ctypes.Structure):
    '''
    struct pcap_stat {
        u_int ps_recv;        /* number of packets received */
        u_int ps_drop;        /* number of packets dropped */
        u_int ps_ifdrop;    /* drops by interface, not yet supported */
        #ifdef WIN32
        u_int bs_capt;        /* number of packets that reach the application */
        #endif /* WIN32 */
    };
    '''
    _fields_ = [("ps_recv", ctypes.c_uint), ("ps_drop", ctypes.c_uint), ("ps_ifdrop", ctypes.c_uint)]
# int   pcap_stats(pcap_t *p, struct pcap_stat *ps);
stats = _pcap.pcap_stats
stats.argtypes = [ctypes.c_void_p, ctypes.c_void_p] # is the second argument right?
stats.restype = ctypes.c_uint
我不确定 stats 的第二个参数类型是否正确,然后如何将 C 结构传递给 python 中的函数。在 C 中,它可能看起来像
struct pcap_stat stat;
pcap_stats(handle, &stat);
但是在 python 中到底是怎么回事?