예제 #1
0
    def testMultipleSameProtos(self):
        proto = ethernet.EthernetProtocol()
        p1 = MyProtocol([
            ('foobar', {
                'partial': 0,
                'dest': "123456",
                'source': "987654",
                'protocol': 0x0800,
            }),
        ])

        p2 = MyProtocol([
            ('foobar', {
                'partial': 0,
                'dest': "123456",
                'source': "987654",
                'protocol': 0x0800,
            }),
        ])

        proto.addProto(0x0800, p1)
        proto.addProto(0x0800, p2)

        proto.datagramReceived("123456987654\x08\x00foobar", partial=0)

        assert not p1.expecting, \
               'Should not expect any more packets, but still want %r' % p1.expecting
        assert not p2.expecting, \
               'Should not expect any more packets, but still want %r' % p2.expecting
예제 #2
0
    def testWrongProtoNotSeen(self):
        proto = ethernet.EthernetProtocol()
        p1 = MyProtocol([])
        proto.addProto(0x0801, p1)

        proto.datagramReceived("123456987654\x08\x00foobar", partial=0)
        proto.datagramReceived("012345abcdef\x08\x00quux", partial=1)
예제 #3
0
    def testMultiplePackets(self):
        proto = ethernet.EthernetProtocol()
        p1 = MyProtocol([
            (
                b"foobar",
                {
                    "partial": 0,
                    "dest": b"123456",
                    "source": b"987654",
                    "protocol": 0x0800,
                },
            ),
            (
                b"quux",
                {
                    "partial": 1,
                    "dest": b"012345",
                    "source": b"abcdef",
                    "protocol": 0x0800,
                },
            ),
        ])
        proto.addProto(0x0800, p1)

        proto.datagramReceived(b"123456987654\x08\x00foobar", partial=0)
        proto.datagramReceived(b"012345abcdef\x08\x00quux", partial=1)

        assert not p1.expecting, (
            "Should not expect any more packets, but still want %r" %
            p1.expecting)
예제 #4
0
    def testMultiplePackets(self):
        proto = ethernet.EthernetProtocol()
        p1 = MyProtocol([

            (b'foobar', {
            'partial': 0,
            'dest': b"123456",
            'source': b"987654",
            'protocol': 0x0800,
            }),

            (b'quux', {
            'partial': 1,
            'dest': b"012345",
            'source': b"abcdef",
            'protocol': 0x0800,
            }),

            ])
        proto.addProto(0x0800, p1)

        proto.datagramReceived(b"123456987654\x08\x00foobar",
                               partial=0)
        proto.datagramReceived(b"012345abcdef\x08\x00quux",
                               partial=1)

        assert not p1.expecting, \
               'Should not expect any more packets, but still want %r' % p1.expecting
예제 #5
0
 def testAddingBadProtos_WrongLevel(self):
     """Adding a wrong level protocol raises an exception."""
     e = ethernet.EthernetProtocol()
     try:
         e.addProto(42, "silliness")
     except components.CannotAdapt:
         pass
     else:
         raise AssertionError, 'addProto must raise an exception for bad protocols'
예제 #6
0
 def testAddingBadProtos_TooBig2(self):
     """Adding a protocol with a number >=2**16 raises an exception."""
     e = ethernet.EthernetProtocol()
     try:
         e.addProto(2**16 + 1, MyProtocol([]))
     except TypeError, e:
         if e.args == ('Added protocol must fit in 16 bits', ):
             pass
         else:
             raise
예제 #7
0
 def testAddingBadProtos_TooSmall(self):
     """Adding a protocol with a negative number raises an exception."""
     e = ethernet.EthernetProtocol()
     try:
         e.addProto(-1, MyProtocol([]))
     except TypeError, e:
         if e.args == ('Added protocol must be positive or zero', ):
             pass
         else:
             raise
예제 #8
0
 def testAddingBadProtos_TooBig(self):
     """Adding a protocol with a number >=2**16 raises an exception."""
     e = ethernet.EthernetProtocol()
     try:
         e.addProto(2**16, MyProtocol([]))
     except TypeError as e:
         if e.args == ('Added protocol must fit in 16 bits',):
             pass
         else:
             raise
     else:
         raise AssertionError('addProto must raise an exception for bad protocols')
예제 #9
0
 def testAddingBadProtos_TooSmall(self):
     """Adding a protocol with a negative number raises an exception."""
     e = ethernet.EthernetProtocol()
     try:
         e.addProto(-1, MyProtocol([]))
     except TypeError as e:
         if e.args == ("Added protocol must be positive or zero", ):
             pass
         else:
             raise
     else:
         raise AssertionError(
             "addProto must raise an exception for bad protocols")
예제 #10
0
    def testDemuxing(self):
        proto = ethernet.EthernetProtocol()
        p1 = MyProtocol([
            ('foobar', {
                'partial': 0,
                'dest': "123456",
                'source': "987654",
                'protocol': 0x0800,
            }),
            ('quux', {
                'partial': 1,
                'dest': "012345",
                'source': "abcdef",
                'protocol': 0x0800,
            }),
        ])
        proto.addProto(0x0800, p1)

        p2 = MyProtocol([
            ('quux', {
                'partial': 1,
                'dest': "012345",
                'source': "abcdef",
                'protocol': 0x0806,
            }),
            ('foobar', {
                'partial': 0,
                'dest': "123456",
                'source': "987654",
                'protocol': 0x0806,
            }),
        ])
        proto.addProto(0x0806, p2)

        proto.datagramReceived("123456987654\x08\x00foobar", partial=0)
        proto.datagramReceived("012345abcdef\x08\x06quux", partial=1)
        proto.datagramReceived("123456987654\x08\x06foobar", partial=0)
        proto.datagramReceived("012345abcdef\x08\x00quux", partial=1)

        assert not p1.expecting, \
               'Should not expect any more packets, but still want %r' % p1.expecting
        assert not p2.expecting, \
               'Should not expect any more packets, but still want %r' % p2.expecting
예제 #11
0
    def testMultipleSameProtos(self):
        proto = ethernet.EthernetProtocol()
        p1 = MyProtocol([
            (
                b"foobar",
                {
                    "partial": 0,
                    "dest": b"123456",
                    "source": b"987654",
                    "protocol": 0x0800,
                },
            ),
        ])

        p2 = MyProtocol([
            (
                b"foobar",
                {
                    "partial": 0,
                    "dest": b"123456",
                    "source": b"987654",
                    "protocol": 0x0800,
                },
            ),
        ])

        proto.addProto(0x0800, p1)
        proto.addProto(0x0800, p2)

        proto.datagramReceived(b"123456987654\x08\x00foobar", partial=0)

        assert (
            not p1.expecting
        ), "Should not expect any more packets, " "but still want {!r}".format(
            p1.expecting)
        assert (
            not p2.expecting
        ), "Should not expect any more packets," " but still want {!r}".format(
            p2.expecting)
예제 #12
0
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.

from twisted.internet import reactor, protocol
from twisted.pair import ethernet, rawudp, ip
from twisted.pair import tuntap


class MyProto(protocol.DatagramProtocol):
    def datagramReceived(self, *a, **kw):
        print a, kw


p_udp = rawudp.RawUDPProtocol()
p_udp.addProto(42, MyProto())
p_ip = ip.IPProtocol()
p_ip.addProto(17, p_udp)
p_eth = ethernet.EthernetProtocol()
p_eth.addProto(0x800, p_ip)

port = tuntap.TuntapPort(interface='tap0', proto=p_eth, reactor=reactor)
port.startListening()
reactor.run()