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.

rawudp.py 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # -*- test-case-name: twisted.pair.test.test_rawudp -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. Implementation of raw packet interfaces for UDP
  6. """
  7. import struct
  8. from zope.interface import implementer
  9. from twisted.internet import protocol
  10. from twisted.pair import raw
  11. class UDPHeader:
  12. def __init__(self, data):
  13. (self.source, self.dest, self.len, self.check) = struct.unpack(
  14. "!HHHH", data[:8]
  15. )
  16. @implementer(raw.IRawDatagramProtocol)
  17. class RawUDPProtocol(protocol.AbstractDatagramProtocol):
  18. def __init__(self):
  19. self.udpProtos = {}
  20. def addProto(self, num, proto):
  21. if not isinstance(proto, protocol.DatagramProtocol):
  22. raise TypeError("Added protocol must be an instance of DatagramProtocol")
  23. if num < 0:
  24. raise TypeError("Added protocol must be positive or zero")
  25. if num >= 2 ** 16:
  26. raise TypeError("Added protocol must fit in 16 bits")
  27. if num not in self.udpProtos:
  28. self.udpProtos[num] = []
  29. self.udpProtos[num].append(proto)
  30. def datagramReceived(
  31. self,
  32. data,
  33. partial,
  34. source,
  35. dest,
  36. protocol,
  37. version,
  38. ihl,
  39. tos,
  40. tot_len,
  41. fragment_id,
  42. fragment_offset,
  43. dont_fragment,
  44. more_fragments,
  45. ttl,
  46. ):
  47. header = UDPHeader(data)
  48. for proto in self.udpProtos.get(header.dest, ()):
  49. proto.datagramReceived(data[8:], (source, header.source))