1
# -*- test-case-name: twisted.pair.test.test_ethernet -*-
2
# Copyright (c) 2001-2004 Twisted Matrix Laboratories.
3
# See LICENSE for details.
8
"""Support for working directly with ethernet frames"""
13
from twisted.internet import protocol
14
from twisted.pair import raw
15
from twisted.python import components
16
from zope.interface import implements
19
class IEthernetProtocol(components.Interface):
20
"""An interface for protocols that handle Ethernet frames"""
22
"""Add an IRawPacketProtocol protocol"""
24
def datagramReceived():
25
"""An Ethernet frame has been received"""
28
def __init__(self, data):
30
(self.dest, self.source, self.proto) \
31
= struct.unpack("!6s6sH", data[:6+6+2])
33
class EthernetProtocol(protocol.AbstractDatagramProtocol):
35
implements(IEthernetProtocol)
40
def addProto(self, num, proto):
41
proto = raw.IRawPacketProtocol(proto)
43
raise TypeError, 'Added protocol must be positive or zero'
45
raise TypeError, 'Added protocol must fit in 16 bits'
46
if num not in self.etherProtos:
47
self.etherProtos[num] = []
48
self.etherProtos[num].append(proto)
50
def datagramReceived(self, data, partial=0):
51
header = EthernetHeader(data[:14])
52
for proto in self.etherProtos.get(header.proto, ()):
53
proto.datagramReceived(data=data[14:],
57
protocol=header.proto)
59
components.backwardsCompatImplements(EthernetProtocol)