1
# -*- test-case-name: twisted.pair.test.test_rawudp -*-
2
# Copyright (c) 2001-2004 Twisted Matrix Laboratories.
3
# See LICENSE for details.
7
"""Implementation of raw packet interfaces for UDP"""
11
from twisted.internet import protocol
12
from twisted.pair import raw
13
from zope.interface import implements
16
def __init__(self, data):
18
(self.source, self.dest, self.len, self.check) \
19
= struct.unpack("!HHHH", data[:8])
21
class RawUDPProtocol(protocol.AbstractDatagramProtocol):
22
implements(raw.IRawDatagramProtocol)
26
def addProto(self, num, proto):
27
if not isinstance(proto, protocol.DatagramProtocol):
28
raise TypeError, 'Added protocol must be an instance of DatagramProtocol'
30
raise TypeError, 'Added protocol must be positive or zero'
32
raise TypeError, 'Added protocol must fit in 16 bits'
33
if num not in self.udpProtos:
34
self.udpProtos[num] = []
35
self.udpProtos[num].append(proto)
37
def datagramReceived(self,
52
header = UDPHeader(data)
53
for proto in self.udpProtos.get(header.dest, ()):
54
proto.datagramReceived(data[8:],
55
(source, header.source))