commit 73fe964c1979e16672c14ccc652ef115634704ac Author: Isis Lovecruft isis@torproject.org Date: Thu Dec 13 21:17:28 2012 +0000
Patched upstream scapy, see /ooni/lib/001-scapy_missing-exc.sh.patch. Modified txscapy so that user's running unpatched versions will also work.
* Fixed timeout settings for ScapySender. * Fixed an error where scapy.utils.PcapWriter wasn't expecting unicode... * Cleaned up extra imports in /ooni/utils/txscapy.py. * Cleaned up a log statement which concatenated to strings (this causes errors for twisted sometimes). * Moved exception classes to beginning of txscapy.py file. --- ooni/lib/001-scapy_missing-exc.sh.patch | 78 +++++++++++++++++++++++++++++++ ooni/utils/txscapy.py | 68 ++++++++++++++++----------- 2 files changed, 119 insertions(+), 27 deletions(-)
diff --git a/ooni/lib/001-scapy_missing-exc.sh.patch b/ooni/lib/001-scapy_missing-exc.sh.patch new file mode 100644 index 0000000..3f5095c --- /dev/null +++ b/ooni/lib/001-scapy_missing-exc.sh.patch @@ -0,0 +1,78 @@ +# +# Add a missing Exception class to /scapy/arch/linux.py +# +# To apply this patch: +# STEP 1: Chdir to the source directory. If you have scapy installed, this is +# likely located at /usr/share/pyshared. +# STEP 2: Run the 'patch' program with this file as input, i.e.: +# +# /usr/share/pyshared$ patch -p1 ./scapy/arch/linux.py </path/to/this/file> +# +# To apply this patch with the use of 'applypatch': +# STEP 1: Chdir to the source directory. +# STEP 2: Run the 'applypatch' program with this patch file as input. +# +# @authors: Isis Lovecruft +# @license: This file is part of ooniprobe, see LICENSE file for details. +# @copyright: 2012 Isis Lovecruft +# +#### End of Preamble #### + +#### Patch data follows #### +diff -c 'scapy/arch/linux.py' 'scapy/arch/linux.py-patched' +Index: ./scapy/arch/linux.py +*** ./scapy/arch/linux.py Thu Dec 13 21:26:59 2012 +--- ./scapy/arch/linux.py-patched Thu Dec 13 21:27:53 2012 +*************** +*** 14,21 **** + from scapy.data import * + from scapy.supersocket import SuperSocket + import scapy.arch +! from scapy.error import warning +! + + + # From bits/ioctls.h +--- 14,20 ---- + from scapy.data import * + from scapy.supersocket import SuperSocket + import scapy.arch +! from scapy.error import warning,Scapy_Exception + + + # From bits/ioctls.h +#### End of Patch data #### + +#### ApplyPatch data follows #### +# Data version : 1.0 +# Date generated : Thu Dec 13 21:33:12 2012 +# Generated by : makepatch 2.03 +# Recurse directories : Yes +# Excluded files : (\A|/).*~\Z +# (\A|/).*.a\Z +# (\A|/).*.bak\Z +# (\A|/).*.BAK\Z +# (\A|/).*.elc\Z +# (\A|/).*.exe\Z +# (\A|/).*.gz\Z +# (\A|/).*.ln\Z +# (\A|/).*.o\Z +# (\A|/).*.obj\Z +# (\A|/).*.olb\Z +# (\A|/).*.old\Z +# (\A|/).*.orig\Z +# (\A|/).*.rej\Z +# (\A|/).*.so\Z +# (\A|/).*.Z\Z +# (\A|/).del-.*\Z +# (\A|/).make.state\Z +# (\A|/).nse_depinfo\Z +# (\A|/)core\Z +# (\A|/)tags\Z +# (\A|/)TAGS\Z +# p 'scapy/arch/linux.py' 16563 1355434073 0100644 +#### End of ApplyPatch data #### + +#### End of Patch kit [created: Thu Dec 13 21:33:12 2012] #### +#### Patch checksum: 56 1830 50583 #### +#### Checksum: 74 2507 41533 #### diff --git a/ooni/utils/txscapy.py b/ooni/utils/txscapy.py index e91da09..647f20e 100644 --- a/ooni/utils/txscapy.py +++ b/ooni/utils/txscapy.py @@ -18,35 +18,33 @@ from twisted.internet import reactor, threads, error from twisted.internet import defer, abstract from zope.interface import implements
-from scapy.config import conf -from scapy.all import PcapWriter, MTU from scapy.all import BasePacketList, conf, PcapReader -from scapy.all import Gen, SetGen +from scapy.all import Gen, SetGen, MTU +from scapy.error import Scapy_Exception
-from ooni.utils import log from ooni import config +from ooni.utils import log
try: - conf.use_pcap = True - conf.use_dnet = True - from scapy.all import PcapWriter from scapy.arch import pcapdnet - config.pcap_dnet = True - -except ImportError, e: - log.err("pypcap or dnet not installed. " - "Certain tests may not work.") - + conf.use_pcap = True + conf.use_dnet = True +except ImportError: + log.err("pypcap or dnet not installed. Certain tests may not work.") config.pcap_dnet = False conf.use_pcap = False conf.use_dnet = False - from scapy.all import PcapWriter
-from scapy.all import BasePacketList, PcapReader -from scapy.all import Gen, SetGen, MTU + +class ProtocolNotRegistered(Exception): + pass + +class ProtocolAlreadyRegistered(Exception): + pass +
def getNetworksFromRoutes(): from scapy.all import conf, ltoa, read_routes @@ -72,11 +70,6 @@ def getDefaultIface(): return net.iface raise IfaceError
-class ProtocolNotRegistered(Exception): - pass - -class ProtocolAlreadyRegistered(Exception): - pass
class ScapyFactory(abstract.FileDescriptor): """ @@ -88,9 +81,23 @@ class ScapyFactory(abstract.FileDescriptor): if interface == 'auto': interface = getDefaultIface() if not super_socket: - super_socket = conf.L3socket(iface=interface, - promisc=True, filter='') - #super_socket = conf.L2socket(iface=interface) + try: + # scapy is missing an import in /scapy/arch/linux.py + # see /ooni/lib/000-scapy-missing-exc.patch + super_socket = conf.L3socket(iface=interface, + promisc=True, filter='') + #super_socket = conf.L2socket(iface=interface) + except NameError, ne: + raise Scapy_Exception("Filter parse error") + except Scapy_Exception, se: + log.err("txscapy: %s" % se.message) + log.debug("txscapy: Trying socket setup again without filter") + try: + super_socket = conf.L3socket(iface=interface, + promisc=True) + except: + log.err("txscapy: Socket setup failed, giving up...") + raise sys.exit(1)
self.protocols = [] fdesc._setCloseOnExec(super_socket.ins.fileno()) @@ -149,9 +156,13 @@ class ScapyProtocol(object): raise NotImplementedError
class ScapySender(ScapyProtocol): - timeout = 5
- # This deferred will fire when we have finished sending a receiving packets. + # This deferred will fire when we have finished sending and receiving + # packets. + timeout = 5 + if config.advanced.default_timeout: + timeout = int(config.advanced.default_timeout) + # Should we look for multiple answers for the same sent packet? multi = False
@@ -232,7 +243,10 @@ class ScapySender(ScapyProtocol):
class ScapySniffer(ScapyProtocol): def __init__(self, pcap_filename, *arg, **kw): - self.pcapwriter = PcapWriter(pcap_filename, *arg, **kw) + # The "str(pcap_filename)" explicit typing is due to an error where + # scapy.utils.PcapWriter expects strings, and it's getting unicode + # due to the "# -*- coding: utf-8 -*-"...this might be a problem... + self.pcapwriter = PcapWriter(str(pcap_filename), *arg, **kw)
def packetReceived(self, packet): self.pcapwriter.write(packet)