-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathframe_protocol.py
More file actions
90 lines (76 loc) · 2.41 KB
/
frame_protocol.py
File metadata and controls
90 lines (76 loc) · 2.41 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
"""
:author: Shane Boissevain
:date: 2019-01-07
"""
##
# Python Imports
import sys
import binascii
import collections
##
# Project Imports
from frame_internet import FRAME_LENGTH_INTERNET
##
# Error Handling
from errors import GenericException
class Protocol_Frame_Error(GenericException):
""" Errors relating to Protocol Frame problems.
"""
pass
##
# Global Variables
DEBUG = False
PROTO_ICMP = 1
FRAME_LENGTH_ICMP = 8
PROTO_TCP = 6
FRAME_LENGTH_TCP = 1000
PROTO_UDP = 17
FRAME_LENGTH_UDP = 1000
class Protocol_Frame(object):
""" Abstracts the handling of Protocol Frame for easier manipulation. Supported types are:
* ``ICMP``
Not yet implemented:
* ``TCP``
* ``UDP``
"""
@classmethod
def parse_Protocol_Frame(cls, internet_frame, bytes):
obj = cls(internet_frame, bytes)
return (obj, bytes[len(obj.raw_bytes):],)
def __init__(self, internet_frame, bytes):
# Ensure Supported Protocol
if PROTO_ICMP == int(binascii.hexlify(internet_frame.protocol), 16):
total_bytes = int(binascii.hexlify(internet_frame.total_len), 16)
self.header = bytes[:FRAME_LENGTH_ICMP]
self.payload = bytes[FRAME_LENGTH_ICMP:total_bytes - FRAME_LENGTH_INTERNET]
else:
raise Protocol_Frame_Error("Only ICMP is implemented")
# If in Debug Mode - Print this object
if DEBUG:
print str(self.__class__)
for byte in bytes[:total_bytes - FRAME_LENGTH_INTERNET]:
sys.stdout.write(binascii.hexlify(byte) + " ")
print ""
for key, value in collections.OrderedDict(self).iteritems():
print str(key) + " " + str(value)
print ""
def __iter__(self):
me = collections.OrderedDict()
me["bytes "] = binascii.hexlify(self.raw_bytes)
me["header "] = binascii.hexlify(self.header)
me["payload"] = binascii.hexlify(self.payload)
return me.iteritems()
@property
def raw_bytes(self):
""" Returns the "raw bytes" **binary string** that comprises the Protocol Frame.
"""
return self.header + self.payload
@property
def length(self):
return len(self.header)
@property
def payload_length(self):
return len(self.payload)
@property
def total_length(self):
return self.length + self.payload_length