Created
October 2, 2013 05:50
-
-
Save david415/6789612 to your computer and use it in GitHub Desktop.
Twisted Reader for Linux Netfilter Log
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from nflog_cffi import NFLOG, NFWouldBlock | |
from twisted.internet import main | |
from twisted.internet import reactor | |
class NFLogReader(object): | |
def __init__(self, dropPrivCallback = None, handlePacket = None, queues = (0,1), nflog_kwargs=dict()): | |
"""Setup the NFLOG generator. """ | |
self.nflog_kwargs = nflog_kwargs | |
self.queues = queues | |
self.handlePacket = handlePacket | |
self.nflog = NFLOG().generator(self.queues, **self.nflog_kwargs) | |
self.fd = self.nflog.next() | |
if dropPrivCallback is not None: | |
dropPrivCallback() | |
reactor.addReader(self) | |
def fileno(self): | |
return self.fd | |
def connectionLost(self, reason): | |
reactor.removeReader(self) | |
def doRead(self): | |
pkt = self.nflog.next() | |
while True: | |
self.handlePacket(pkt) | |
pkt = self.nflog.send(True) | |
if pkt is NFWouldBlock: break | |
def logPrefix(self): | |
return 'nflog' | |
def main(): | |
def printPacketLen(p): | |
print len(p) | |
nflog = NFLogReader(handlePacket=printPacketLen) | |
reactor.run() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment