[Twisted-Python] PcapIO

Neil Blakey-Milner nbm at mithrandr.moria.org
Sat Dec 21 10:11:07 EST 2002


Hi again,

I'm currently using this PcapIO class in a traffic analyser, and thought
people might find it useful, possibly for inclusion in twisted.  It
relies on pylibpcap (http://pylibpcap.sourceforge.net/).  If interested,
it's easy enough to use the example code in the pylibpcap distribution
in your protocol's dataReceived.

Neil
-- 
Neil Blakey-Milner
nbm at mithrandr.moria.org
-------------- next part --------------
#!/usr/local/bin/python
#
# Copyright (c) 2002 Neil Blakey-Milner
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#

# system imports
import pcap
import sys

# Twisted Imports
from twisted.internet import abstract, protocol

# Sibling Imports
import pcapprotocol

class PcapIO(abstract.FileDescriptor):
    def __init__(self, protocol, dev):
        """Create me with a device dev to listen on.  Should probably
        accept files too, and allow choice of device promiscuity and
        snap lengths.
        """
        abstract.FileDescriptor.__init__(self)

        p = pcap.pcapObject()
        p.open_live(dev, 1600, 0, 100)
        p.setnonblock(1)

        self.fn = p.fileno()
        self.pcap = p
        self.protocol = protocol
        self.protocol.makeConnection(self)
        self.startReading()

    def fileno(self):
        return self.fn

    def doRead(self):
        try:
            output = self.pcap.next()
        except IOError, ioe:
            if ioe.args[0] == errno.EAGAIN:
                return
            else:
                return CONNECTION_LOST
        if not output:
            return CONNECTION_LOST
        self.protocol.dataReceived(output)

    def connectionLost(self, reason):
        self.protocol.connectionLost()


More information about the Twisted-Python mailing list