-
Notifications
You must be signed in to change notification settings - Fork 0
/
ethernet_frame.py
31 lines (26 loc) · 1.06 KB
/
ethernet_frame.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import struct
class EthernetFrame:
def __init__(self, dest_mac, src_mac, eth_type, payload):
self.dest_mac = dest_mac
self.src_mac = src_mac
self.eth_type = eth_type
self.payload = payload
def create_frame(self):
"""Crée une trame Ethernet."""
dest_mac_bytes = bytes.fromhex(self.dest_mac.replace(':', ''))
src_mac_bytes = bytes.fromhex(self.src_mac.replace(':', ''))
eth_type_bytes = struct.pack('!H', self.eth_type)
return dest_mac_bytes + src_mac_bytes + eth_type_bytes + self.payload.encode()
@staticmethod
def parse_frame(frame):
"""Analyse une trame Ethernet et retourne ses composants."""
dest_mac = ':'.join(format(b, '02x') for b in frame[:6])
src_mac = ':'.join(format(b, '02x') for b in frame[6:12])
eth_type = struct.unpack('!H', frame[12:14])[0]
payload = frame[14:].decode()
return {
'dest_mac': dest_mac,
'src_mac': src_mac,
'eth_type': eth_type,
'payload': payload
}