-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpacket.py
More file actions
59 lines (50 loc) · 1.85 KB
/
packet.py
File metadata and controls
59 lines (50 loc) · 1.85 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from .util import to_str
class RawPacket:
"""RawPacket is the resulting data container of the RawSocket class.
It reads raw data and stores the MAC source, MAC destination, the Ethernet type and the data payload.
RawPacket.success is true if the packet is successfuly read.
"""
def __init__(self, data):
"""A really simple class.
:param data: raw ethernet II frame coming from the socket library, either **bytes in Python3** or **str in Python2**
:type data: str or bytes or bytearray
"""
self.dest = ""
""":description: Destination MAC address
:type: str or bytes or bytearray"""
self.src = ""
""":description: Source MAC address
:type: str or bytes or bytearray"""
self.type = ""
""":description: Ethertype
:type: str or bytes or bytearray"""
self.data = ""
""":description: Payload received
:type: str or bytes or bytearray"""
self.success = False
""":description: True if the packet has been successfully unmarshalled
:type: bool"""
try:
self.dest, self.src, self.type = data[0:6], data[6:12], data[12:14]
self.data = data[14:]
self.success = True
except Exception as e:
print("rawsocket: ", e)
self.success = False
def __repr__(self):
return "".join(
[
to_str(self.src),
" == 0x",
to_str(self.type, separator=""),
" => ",
to_str(self.dest),
" - ",
"OK" if self.success else "FAILED",
]
)
def __str__(self):
return "".join([self.__repr__(), ":\n", self.data.decode("utf-8")])