Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ethernet.py 1.6KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # -*- test-case-name: twisted.pair.test.test_ethernet -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. #
  5. """Support for working directly with ethernet frames"""
  6. import struct
  7. from zope.interface import Interface, implementer
  8. from twisted.internet import protocol
  9. from twisted.pair import raw
  10. class IEthernetProtocol(Interface):
  11. """An interface for protocols that handle Ethernet frames"""
  12. def addProto(num, proto):
  13. """Add an IRawPacketProtocol protocol"""
  14. def datagramReceived(data, partial):
  15. """An Ethernet frame has been received"""
  16. class EthernetHeader:
  17. def __init__(self, data):
  18. (self.dest, self.source, self.proto) = struct.unpack(
  19. "!6s6sH", data[: 6 + 6 + 2]
  20. )
  21. @implementer(IEthernetProtocol)
  22. class EthernetProtocol(protocol.AbstractDatagramProtocol):
  23. def __init__(self):
  24. self.etherProtos = {}
  25. def addProto(self, num, proto):
  26. proto = raw.IRawPacketProtocol(proto)
  27. if num < 0:
  28. raise TypeError("Added protocol must be positive or zero")
  29. if num >= 2 ** 16:
  30. raise TypeError("Added protocol must fit in 16 bits")
  31. if num not in self.etherProtos:
  32. self.etherProtos[num] = []
  33. self.etherProtos[num].append(proto)
  34. def datagramReceived(self, data, partial=0):
  35. header = EthernetHeader(data[:14])
  36. for proto in self.etherProtos.get(header.proto, ()):
  37. proto.datagramReceived(
  38. data=data[14:],
  39. partial=partial,
  40. dest=header.dest,
  41. source=header.source,
  42. protocol=header.proto,
  43. )