Exemple #1
0
    def __init__(self, target, **settings):
        self.target = target
        self.settings = settings
        self.verbose = settings.get("verbose")
        self.proto = settings.get("proto")
        self.rfd = socket.socket(socket.AF_INET, socket.SOCK_RAW,
                                 socket.IPPROTO_ICMP)
        if self.proto == "icmp":
            self.sfd = socket.socket(socket.AF_INET, socket.SOCK_RAW,
                                     socket.IPPROTO_ICMP)
        elif self.proto == "udp":
            self.sfd = socket.socket(socket.AF_INET, socket.SOCK_RAW,
                                     socket.IPPROTO_UDP)
        elif self.proto == "tcp":
            self.sfd = socket.socket(socket.AF_INET, socket.SOCK_RAW,
                                     socket.IPPROTO_TCP)

        self.rfd.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)
        self.sfd.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)

        self.hops = []
        self.out_queue = []
        self.waiting = True
        self.deferred = defer.Deferred()

        reactor.addReader(self)
        reactor.addWriter(self)

        # send 1st probe packet
        self.out_queue.append(
            Hop(self.target, 1, settings.get("proto"),
                self.settings.get("dport"), self.settings.get("sport")))
    def __init__(self, target, **settings):
        self.target = target
        self.settings = settings
        self.verbose = settings.get("verbose")
        self.proto = settings.get("proto")
        self.rfd = socket.socket(socket.AF_INET, socket.SOCK_RAW,
                                socket.IPPROTO_ICMP)
        if self.proto == "icmp":
            self.sfd = socket.socket(socket.AF_INET, socket.SOCK_RAW,
                                    socket.IPPROTO_ICMP)
        elif self.proto == "udp":
            self.sfd = socket.socket(socket.AF_INET, socket.SOCK_RAW,
                                    socket.IPPROTO_UDP)
        elif self.proto == "tcp":
            self.sfd = socket.socket(socket.AF_INET, socket.SOCK_RAW,
                                    socket.IPPROTO_TCP)

        self.rfd.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)
        self.sfd.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)

        self.hops = []
        self.out_queue = []
        self.waiting = True
        self.deferred = defer.Deferred()

        reactor.addReader(self)
        reactor.addWriter(self)

        # send 1st probe packet
        self.out_queue.append(Hop(self.target, 1,
                                  settings.get("proto"),
                                  self.settings.get("dport"),
                                  self.settings.get("sport")))
 def add(self, evtype, fileno, cb):
     from twisted.internet import reactor
     descriptor = socket_rwdescriptor(evtype, fileno, cb)
     if evtype is READ:
         reactor.addReader(descriptor)
     if evtype is WRITE:
         reactor.addWriter(descriptor)
     return descriptor
Exemple #4
0
 def add(self, evtype, fileno, cb):
     from twisted.internet import reactor
     descriptor = socket_rwdescriptor(evtype, fileno, cb)
     if evtype is READ:
         reactor.addReader(descriptor)
     if evtype is WRITE:
         reactor.addWriter(descriptor)
     return descriptor
Exemple #5
0
 def __init__(self, sock, poetry_file, num_bytes, delay):
     self.sock = sock
     self.num_bytes = num_bytes
     self.delay = delay
     self.poetry_file = poetry_file
     self.inputf = open(poetry_file)
     from twisted.internet import reactor
     reactor.addWriter(self)
Exemple #6
0
    def __init__(self, target, **settings):

        self.target = target
        self.settings = settings
        self.verbose = settings.get("verbose")
        self.proto = settings.get("proto")
        self.rfd = socket.socket(socket.AF_INET, socket.SOCK_RAW,
                                 socket.IPPROTO_ICMP)
        self.sfd = {}

        # Create the data structures to contain the test results
        self.traceroute = {}
        self.traceroute["tcp"] = TracerouteResult("tcp")
        self.traceroute["udp"] = TracerouteResult("udp")
        self.traceroute["icmp"] = TracerouteResult("icmp")

        if self.settings.get("ooni"):
            self.sfd["tcp"] = socket.socket(socket.AF_INET, socket.SOCK_RAW,
                                    socket.IPPROTO_TCP)
            self.sfd["icmp"] = socket.socket(socket.AF_INET, socket.SOCK_RAW,
                                    socket.IPPROTO_ICMP)
            self.sfd["udp"] = socket.socket(socket.AF_INET, socket.SOCK_RAW,
                                    socket.IPPROTO_UDP)
        elif self.proto == "icmp":
            self.sfd["icmp"] = socket.socket(socket.AF_INET, socket.SOCK_RAW,
                                    socket.IPPROTO_ICMP)
        elif self.proto == "udp":
            self.sfd["udp"] = socket.socket(socket.AF_INET, socket.SOCK_RAW,
                                    socket.IPPROTO_UDP)
        elif self.proto == "tcp":
            self.sfd["tcp"] = socket.socket(socket.AF_INET, socket.SOCK_RAW,
                                    socket.IPPROTO_TCP)

        # Let me add IP Headers myself, just give me a socket!
        self.rfd.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)
        for fd in self.sfd:
            self.sfd[fd].setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)

        self.out_queue = []
        self.waiting = True
        self.deferred = defer.Deferred()

        reactor.addReader(self)
        reactor.addWriter(self)

        # send 1st probe packet(s)
        if self.settings.get("ooni"):
            hops = list(TracerouteResult.hops(self.target, 1))
        else:
            hops = [Hop(self.target, 1,
                        settings.get("proto"),
                        self.settings.get("sport"),
                        self.settings.get("dport"))]
        for hop in hops:
            # Store the to be completed items inside of a dictionary
            self.traceroute[hop.proto].add_to_current_probes(hop)
            self.out_queue.append(hop)
Exemple #7
0
 def add_descriptor(self, fileno, read=None, write=None, exc=None):
     from twisted.internet import reactor
     descriptor = socket_rwdescriptor(fileno, read, write, exc)
     if read:
         reactor.addReader(descriptor)
     if write:
         reactor.addWriter(descriptor)
     # XXX exc will not work if no read nor write
     return descriptor
Exemple #8
0
 def add_descriptor(self, fileno, read=None, write=None, exc=None):
     from twisted.internet import reactor
     descriptor = socket_rwdescriptor(fileno, read, write, exc)
     if read:
         reactor.addReader(descriptor)
     if write:
         reactor.addWriter(descriptor)
     # XXX exc will not work if no read nor write
     return descriptor
 def test_removeAllReturnsRemovedDescriptors(self):
     """
     L{PosixReactorBase._removeAll} returns a list of removed
     L{IReadDescriptor} and L{IWriteDescriptor} objects.
     """
     reactor = TrivialReactor()
     reader = object()
     writer = object()
     reactor.addReader(reader)
     reactor.addWriter(writer)
     removed = reactor._removeAll(reactor._readers, reactor._writers)
     self.assertEqual(set(removed), set([reader, writer]))
     self.assertNotIn(reader, reactor._readers)
     self.assertNotIn(writer, reactor._writers)
Exemple #10
0
 def test_removeAllReturnsRemovedDescriptors(self):
     """
     L{PosixReactorBase._removeAll} returns a list of removed
     L{IReadDescriptor} and L{IWriteDescriptor} objects.
     """
     reactor = TrivialReactor()
     reader = object()
     writer = object()
     reactor.addReader(reader)
     reactor.addWriter(writer)
     removed = reactor._removeAll(reactor._readers, reactor._writers)
     self.assertEqual(set(removed), set([reader, writer]))
     self.assertNotIn(reader, reactor._readers)
     self.assertNotIn(writer, reactor._writers)
    def test_getWriters(self):
        """
        Check that L{interfaces.IReactorFDSet.getWriters} reflects the actions
        made with L{interfaces.IReactorFDSet.addWriter} and
        L{interfaces.IReactorFDSet.removeWriter}.
        """
        s = socket.socket()
        self.addCleanup(s.close)

        c = Connection(s, protocol.Protocol())
        self.assertNotIn(c, reactor.getWriters())

        reactor.addWriter(c)
        self.assertIn(c, reactor.getWriters())

        reactor.removeWriter(c)
        self.assertNotIn(c, reactor.getWriters())
Exemple #12
0
    def test_getWriters(self):
        """
        Check that L{interfaces.IReactorFDSet.getWriters} reflects the actions
        made with L{interfaces.IReactorFDSet.addWriter} and
        L{interfaces.IReactorFDSet.removeWriter}.
        """
        s = socket.socket()
        self.addCleanup(s.close)

        c = Connection(s, protocol.Protocol())
        self.assertNotIn(c, reactor.getWriters())

        reactor.addWriter(c)
        self.assertIn(c, reactor.getWriters())

        reactor.removeWriter(c)
        self.assertNotIn(c, reactor.getWriters())
Exemple #13
0
    def __init__(self, target, **settings):
        self.target = target
        self.settings = settings
        self.rfd = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_ICMP)
        self.sfd = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_ICMP)

        self.rfd.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)
        self.sfd.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)

        self.hops = []
        self.out_queue = []
        self.waiting = True
        self.deferred = defer.Deferred()

        reactor.addReader(self)
        reactor.addWriter(self)

        # send 1st probe packet
        self.out_queue.append(Hop(self.target, 1))
Exemple #14
0
def socket_add(id, method, condition = IO_READ):
    """
    The first argument specifies a socket, the second argument has to be a
    function that is called whenever there is data ready in the socket.

    Objects that implement Twisted's IRead/WriteDescriptor interfaces get
    passed to the reactor to monitor.
    """
    global __sockobjs

    if condition == IO_READ:
        s = SocketReadCB(id, method)
        reactor.addReader(s)

    elif condition == IO_WRITE:
        s = SocketWriteCB(id, method)
        reactor.addWriter(s)

    else:
        return

    __sockobjs[condition][id] = s
Exemple #15
0
def socket_add(id, method, condition=IO_READ):
    """
    The first argument specifies a socket, the second argument has to be a
    function that is called whenever there is data ready in the socket.

    Objects that implement Twisted's IRead/WriteDescriptor interfaces get
    passed to the reactor to monitor.
    """
    global __sockobjs

    if condition == IO_READ:
        s = SocketReadCB(id, method)
        reactor.addReader(s)

    elif condition == IO_WRITE:
        s = SocketWriteCB(id, method)
        reactor.addWriter(s)

    else:
        return

    __sockobjs[condition][id] = s
Exemple #16
0
 def startWriting(self):
     if not self.writing:
         reactor.addWriter(self)
         self.writing = True
 def startWriting(self):
     if not self.writing:
         reactor.addWriter(self)
         self.writing = True
Exemple #18
0
 def start(self):
     reactor.addWriter(self)
Exemple #19
0
 def write(self, packet):
     if len(self.packets) == 0:
         reactor.addWriter(self)
     self.packets.append(packet)
Exemple #20
0
 def startWriting(self):
     """Add writer to the reactor"""
     if self._fd is not None:
         reactor.addWriter(self)