<commit_before># -*- coding:utf8 -*-
# txscapy
# *******
# Here shall go functions related to using scapy with twisted.
#
# This software has been written to be part of OONI, the Open Observatory of
# Network Interference. More information on that here: http://ooni.nu/

import struct
import socket
import os
import sys
import time

from twisted.internet import protocol, base, fdesc
from twisted.internet import reactor, threads, error
from twisted.internet import defer, abstract
from zope.interface import implements

from scapy.config import conf

from ooni.utils import log
from ooni import config

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.")

    config.pcap_dnet = False
    conf.use_pcap = False
    conf.use_dnet = False

    from scapy.all import PcapWriter

from scapy.all import BasePacketList, conf, PcapReader
from scapy.all import conf, Gen, SetGen, MTU

def getNetworksFromRoutes():
    from scapy.all import conf, ltoa, read_routes
    from ipaddr    import IPNetwork, IPAddress

    ## Hide the 'no routes' warnings
    conf.verb = 0

    networks = []
    for nw, nm, gw, iface, addr in read_routes():
        n = IPNetwork( ltoa(nw) )
        (n.netmask, n.gateway, n.ipaddr) = [IPAddress(x) for x in [nm, gw, addr]]
        n.iface = iface
        if not n.compressed in networks:
            networks.append(n)

    return networks

class IfaceError(Exception):
    pass

def getDefaultIface():
    networks = getNetworksFromRoutes()
    for net in networks:
        if net.is_private:
            return net.iface
    raise IfaceError

class ProtocolNotRegistered(Exception):
    pass

class ProtocolAlreadyRegistered(Exception):
    pass
<commit_msg>Add docstrings to network helper functions<commit_after># -*- coding:utf8 -*-
# txscapy
# *******
# Here shall go functions related to using scapy with twisted.
#
# This software has been written to be part of OONI, the Open Observatory of
# Network Interference. More information on that here: http://ooni.nu/

import struct
import socket
import os
import sys
import time

from twisted.internet import protocol, base, fdesc
from twisted.internet import reactor, threads, error
from twisted.internet import defer, abstract
from zope.interface import implements

from scapy.config import conf

from ooni.utils import log
from ooni import config

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.")

    config.pcap_dnet = False
    conf.use_pcap = False
    conf.use_dnet = False

    from scapy.all import PcapWriter

from scapy.all import BasePacketList, conf, PcapReader
from scapy.all import conf, Gen, SetGen, MTU

def getNetworksFromRoutes():
    """ Return a list of networks from the routing table """
    from scapy.all import conf, ltoa, read_routes
    from ipaddr    import IPNetwork, IPAddress

    ## Hide the 'no routes' warnings
    conf.verb = 0

    networks = []
    for nw, nm, gw, iface, addr in read_routes():
        n = IPNetwork( ltoa(nw) )
        (n.netmask, n.gateway, n.ipaddr) = [IPAddress(x) for x in [nm, gw, addr]]
        n.iface = iface
        if not n.compressed in networks:
            networks.append(n)

    return networks

class IfaceError(Exception):
    pass

def getDefaultIface():
    """ Return the default interface or raise IfaceError """
    #XXX: currently broken on OpenVZ environments, because
    # the routing table does not contain a default route
    # Workaround: Set the default interface in ooniprobe.conf
    networks = getNetworksFromRoutes()
    for net in networks:
        if net.is_private:
            return net.iface
    raise IfaceError

class ProtocolNotRegistered(Exception):
    pass

class ProtocolAlreadyRegistered(Exception):
    pass

{'fraction_non_alphanumeric': 0.07628965851295713, 'fraction_numerical': 0.0009687575684185032, 'mean_word_length': 3.1717171717171717, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7352513', 'n_tokens_mistral': 1265, 'n_tokens_neox': 1261, 'n_words': 452}