开发者

Why thread is interrupted before the changes are complete

I'm attempting to create python module for getting MAC adresses by IP addresses.

def getMACs(addressesList):
    def _processArp(pkt):
        spa = _inet_ntoa(pkt.spa) 
        if pkt.op == dpkt.arp.ARP_OP_REPLY and spa in _cache.macTable:
            lock.acquire()
            try:
                _cache.macTable[spa] = _packedToMacStr(pkt.sha)
                _cache.notFilledMacs -= 1
            finally:
                lock.release()
            if _cache.notFilledMacs == 0:
                thrd.stop()

    addresses = _parseAddresses(addressesList)

    _cache.registerCacheEntry("macTable", {})
    _cache.registerCacheEntry("notFilledMacs", 0)
    _events.arpPacket += _processArp

    lock = threading.Lock()

    thrd = _CaptureThread(promisc=False, timeout_ms=30, filter="arp")
    thrd.start()
    for addr in addresses:
        if _sendArpQuery(addr):
            _cache.macTable[str(addr)] = None
            _cache.notFilledMacs += 1
    thrd.join(125)
    thrd.stop()
    return _cache.macTable

if __name__ == "__main__":
    macTable = getMACs([IPAddress("192.168.1.1"), IPAddress("192.168.1.3")])
    _pprint.pprint(macTable)

When I run this module I get

{'192.168.1.1': '00:11:95:9E:25:B1', '192.168.1.3': None}

When I debug _processArp step by step I get

{'192.168.1.1': '00:11:95:9E:25:B1', '192.168.1.3': '00:21:63:78:98:8E'}

Class CaptureThread:

class CaptureThread(threading.Thread):
    def __init__ (self, name=None, snaplen=65开发者_运维问答535, promisc=True, timeout_ms=0, immediate=False, filter=None):
        threading.Thread.__init__(self)
        self.__running = True
        self.__name = name 
        self.__snaplen = snaplen 
        self.__promisc = promisc 
        self.__timeout_ms = timeout_ms 
        self.__immediate = immediate 
        self.__filter = filter 

    def stop(self):
        self.__running = False

    def run(self):
        self.__pc = pcap.pcap(self.__name, self.__snaplen, self.__promisc, self.__timeout_ms, self.__immediate)
        if self.__filter:
            self.__pc.setfilter(self.__filter)

        while self.__running:
            self.__pc.dispatch(1, self.__processPacket)

    def __processPacket(self, timestamp, pkt):
        peth = dpkt.ethernet.Ethernet(pkt)
        if isinstance(peth.data, dpkt.arp.ARP):
            _events.arpPacket(peth.data)


Stupid error. As always when working with threads - because of thread synchronization.

One of my conditions for interrupting thread is "_cache.notFilledMacs == 0". In the main thread _cache.notFilledMacs did not have time to get the value of 2 when in the CaptureThread value is decreased.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜