Skip to content

Instantly share code, notes, and snippets.

@david415
Last active December 24, 2015 11:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save david415/6790543 to your computer and use it in GitHub Desktop.
Save david415/6790543 to your computer and use it in GitHub Desktop.
twisted nflog protocol?
"""
Implementation of the Hush protocol.
"""
from nflog_cffi import NFLOG, NFWouldBlock
from twisted.internet import reactor, protocol, defer
from twisted.python import log
class NFLogReader(object):
def __init__(self, fd, nflogIter, nflogProto):
self.fd = fd
self.nflogIter = nflogIter
self.nflogProto = nflogProto
def fileno(self):
return self.fd
def connectionLost(self, reason):
reactor.removeReader(self)
return reason
def doRead(self):
pkt = self.nflogIter.next()
while True:
self.nflogProto.dataReceived(pkt)
pkt = self.nflogIter.send(True)
if pkt is NFWouldBlock: break
def logPrefix(self):
return 'nflog'
class NFLogProtocol(Protocol):
def __init_(self, factory):
self.factory = factory
def dataReceived(pkt):
self.factory.handlePacket(pkt)
def connectionLost(self, reason):
self.factory.nflogReader.connectionLost(self, reason)
return reason
class NFLogFactory(Factory):
protocol = NFLogProtocol
def buildProtocol(handlePacket = None, queues = (0,1), nflog_kwargs=dict()):
self.nflog_kwargs = nflog_kwargs
self.queues = queues
self.handlePacket = handlePacket
self.nflog_iter = NFLOG().generator(self.queues, **self.nflog_kwargs)
self.fd = self.nflog.next()
self.nflogProto = Factory.buildProtocol(self, handlePacket)
self.nflogReader = NFLogReader(handlePacket = self.handlePacket,
fd = self.factory.fd,
nflogIter = self.nflogIter,
nflogProto = self.nflogProto)
return self.nflogProto
def clientConnectionFailed(self, connector, reason):
print 'Failed to connect to:', connector.getDestination()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment