def testAddingBadProtos_WrongLevel(self): """Adding a wrong level protocol raises an exception.""" e = ip.IPProtocol() try: e.addProto(42, "silliness") except components.CannotAdapt: pass else: raise AssertionError("addProto must raise an exception for bad protocols")
def testAddingBadProtos_TooBig2(self): """Adding a protocol with a number >=2**32 raises an exception.""" e = ip.IPProtocol() try: e.addProto(2L**32 + 1, MyProtocol([])) except TypeError, e: if e.args == ('Added protocol must fit in 32 bits', ): pass else: raise
def testAddingBadProtos_TooSmall(self): """Adding a protocol with a negative number raises an exception.""" e = ip.IPProtocol() try: e.addProto(-1, MyProtocol([])) except TypeError, e: if e.args == ('Added protocol must be positive or zero', ): pass else: raise
def testAddingBadProtos_TooBig2(self): """Adding a protocol with a number >=2**32 raises an exception.""" e = ip.IPProtocol() try: e.addProto(2 ** 32 + 1, MyProtocol([])) except TypeError as e: if e.args == ("Added protocol must fit in 32 bits",): pass else: raise else: raise AssertionError("addProto must raise an exception for bad protocols")
def testAddingBadProtos_TooSmall(self): """Adding a protocol with a negative number raises an exception.""" e = ip.IPProtocol() 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")
def testPacketParsing(self): proto = ip.IPProtocol() p1 = MyProtocol( [ ( b"foobar", { "partial": 0, "dest": "1.2.3.4", "source": "5.6.7.8", "protocol": 0x0F, "version": 4, "ihl": 20, "tos": 7, "tot_len": 20 + 6, "fragment_id": 0xDEAD, "fragment_offset": 0x1EEF, "dont_fragment": 0, "more_fragments": 1, "ttl": 0xC0, }, ), ] ) proto.addProto(0x0F, p1) proto.datagramReceived( b"\x54" # ihl version + b"\x07" # tos + b"\x00\x1a" # tot_len + b"\xDE\xAD" # id + b"\xBE\xEF" # frag_off + b"\xC0" # ttl + b"\x0F" # protocol + b"FE" # checksum + b"\x05\x06\x07\x08" + b"\x01\x02\x03\x04" + b"foobar", partial=0, dest="dummy", source="dummy", protocol="dummy", ) assert not p1.expecting, ( "Should not expect any more packets, but still want %r" % p1.expecting )
def testWrongProtoNotSeen(self): proto = ip.IPProtocol() p1 = MyProtocol([]) proto.addProto(1, p1) proto.datagramReceived("\x54" #ihl version + "\x07" #tos + "\x00\x1a" #tot_len + "\xDE\xAD" #id + "\xBE\xEF" #frag_off + "\xC0" #ttl + "\x0F" #protocol + "FE" #checksum + "\x05\x06\x07\x08" + "\x01\x02\x03\x04" + "foobar", partial=0, dest='dummy', source='dummy', protocol='dummy', )
def testPacketParsing(self): proto = ip.IPProtocol() p1 = MyProtocol([ (b'foobar', { 'partial': 0, 'dest': '1.2.3.4', 'source': '5.6.7.8', 'protocol': 0x0F, 'version': 4, 'ihl': 20, 'tos': 7, 'tot_len': 20+6, 'fragment_id': 0xDEAD, 'fragment_offset': 0x1EEF, 'dont_fragment': 0, 'more_fragments': 1, 'ttl': 0xC0, }), ]) proto.addProto(0x0F, p1) proto.datagramReceived(b"\x54" #ihl version + b"\x07" #tos + b"\x00\x1a" #tot_len + b"\xDE\xAD" #id + b"\xBE\xEF" #frag_off + b"\xC0" #ttl + b"\x0F" #protocol + b"FE" #checksum + b"\x05\x06\x07\x08" + b"\x01\x02\x03\x04" + b"foobar", partial=0, dest='dummy', source='dummy', protocol='dummy', ) assert not p1.expecting, \ 'Should not expect any more packets, but still want %r' % p1.expecting
def testWrongProtoNotSeen(self): proto = ip.IPProtocol() p1 = MyProtocol([]) proto.addProto(1, p1) proto.datagramReceived( chr(84) #ihl version + chr(07) #tos + chr(0) + chr(26) #tot_len + chr(222) + chr(173) #id + chr(190) + chr(239) #frag_off + chr(192) #ttl + chr(15) #protocol + chr(254) #checksum + b"\x05\x06\x07\x08" + b"\x01\x02\x03\x04" + b"foobar", partial=0, dest='dummy', source='dummy', protocol='dummy', )
def testPacketParsing(self): proto = ip.IPProtocol() p1 = MyProtocol([ (b'foobar', { 'partial': 0, 'dest': '1.2.3.4', 'source': '5.6.7.8', 'protocol': 0x0F, 'version': 4, 'ihl': 20, 'tos': 7, 'tot_len': 20 + 6, 'fragment_id': 0xDEAD, 'fragment_offset': 0x1EEF, 'dont_fragment': 0, 'more_fragments': 1, 'ttl': 0xC0, }), ]) proto.addProto(0x0F, p1) proto.datagramReceived( chr(84) #ihl version + chr(07) #tos + chr(0) + chr(26) #tot_len + chr(222) + chr(173) #id + chr(190) + chr(239) #frag_off + chr(192) #ttl + chr(15) #protocol + chr(254) #checksum + b"\x05\x06\x07\x08" + b"\x01\x02\x03\x04" + b"foobar", partial=0, dest='dummy', source='dummy', protocol='dummy', ) assert not p1.expecting, \ 'Should not expect any more packets, but still want %r' % p1.expecting
# 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()