forked from pratishcs/pyicmp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
messages.py
247 lines (180 loc) · 7.01 KB
/
messages.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
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
"""
Most names are self-explaining and all is defined in RFC 792, I do not
see reason to write it all here. If you want to know details, read rfc.
It's not so long.
"""
from . import ip as ip_m
import struct
import os
import datetime
"""Skeleton message class
Basic attributes and needed method, setting unified API for all
messages."""
class Message:
packed = ''
ptype = 0
code = 0
checksum = 0
identifier = 0
sequence = 0
original_header = None
original_message = None
def __init__(self, sequence = 0, identifier = os.getpid()):
self.identifier = identifier
self.sequence = sequence
"""Function packing data into bytes.
Inheriting class MUST override this if is expected to send data
(e.g. Echo request)."""
def pack(self):
raise NotImplementedError
"""Function unpacking data from bytes.
Inheriting class MUST override this if is expected to receive data
(e.g. Echo reply).
args:
data should by data directly from socket
"""
def unpack(self, data):
raise NotImplementedError
def _carry_around_add(self, a, b):
c = a + b
return (c & 0xffff) + (c >> 16)
def _calcchecksum(self, msg):
s = 0
for i in range(0, len(msg), 2):
w = msg[i] + (msg[i+1] << 8)
s = self._carry_around_add(s, w)
return ~s & 0xffff
def __repr__(self):
return "<" + self.__class__.__name__ + ": type: %d, code: %d, idf: %d, seq: %d>" % (self.ptype, self.code, self.identifier, self.sequence)
class DestinationUnreachable(Message):
NET_UNREACHABLE = 0
HOST_UNREACHABLE = 1
PROTOCOL_UNREACHABLE = 2
PORT_UNREACHABLE = 3
FRAGMENTATION_NEEDED = 4
SOURCE_ROUTE_FAILED = 5
_size = 4
def __repr__(self):
return "<" + self.__class__.__name__ + ": type: %d, code: %d>" % (self.ptype, self.code)
def unpack(self, data):
(self.ptype, self.code, self.checksum) = struct.unpack('BBH', data[0:self._size])
try:
self.original_header = ip_m.Header(data[8:28])
if data[28] in types:
self.original_message = types[data[28]]()
self.original_message.unpack(data[28:28+types[data[28]]._size])
except Exception:
pass
class TimeExceeded(Message):
TIME_TO_LIVE_EXCEEDED_IN_TRANSIT = 0
FRAGMENT_REASSEMBLY_TIME_EXCEEDED = 1
_size = 4
def __repr__(self):
return "<" + self.__class__.__name__ + ": type: %d, code: %d>" % (self.ptype, self.code)
def unpack(self, data):
(self.ptype, self.code, self.checksum) = struct.unpack('BBH', data[:self._size])
try:
self.original_header = ip_m.Header(data[8:28])
if data[28] in types:
self.original_message = types[data[28]]()
self.original_message.unpack(data[28:28+types[data[28]]._size])
except Exception:
pass
class ParameterProblem(Message):
_size = 5
def __repr__(self):
return "<" + self.__class__.__name__ + ": type: %d, pointer: %d>" % (self.ptype, self._pointer)
def unpack(self, data):
(self.ptype, self.code, self.checksum, self._pointer) = struct.unpack('BBHB', data[:self._size])
self.original_header = ip_m.Header(data[8:28])
self.original_message = types[data[28]]()
self.original_message.unpack(data[28:28+types[data[28]]._size])
class SourceQuench(Message):
_size = 4
def __repr__(self):
return "<" + self.__class__.__name__ + ": type: %d, code: %d>" % (self.ptype, self.code)
def unpack(self, data):
(self.ptype, self.code, self.checksum) = struct.unpack('BBH', data[:self._size])
self.original_header = ip_m.Header(data[8:28])
self.original_message = types[data[28]]()
self.original_message.unpack(data[28:28+types[data[28]]._size])
class Redirect(Message):
REDIRECT_DATAGRAMS_FOR_THE_NETWORK = 0
REDIRECT_DATAGRAMS_FOR_THE_HOST = 1
REDIRECT_DATAGRAMS_FOR_THE_TYPE_OF_SERVICE_AND_NETWORK = 2
REDIRECT_DATAGRAMS_FOR_THE_TYPE_OF_SERVICE_AND_HOST = 3
_size = 8
def __repr__(self):
return "<" + self.__class__.__name__ + ": type: %d, code: %d, gateway: %d>" % (self.ptype, self.code, self._gateway)
def unpack(self, data):
(self.ptype, self.code, self.checksum) = struct.unpack('BBHI', data[:4])
self.gateway = socket.inet_ntoa(data[4:8])
self.original_header = ip_m.Header(data[8:28])
self.original_message = types[data[28]]()
self.original_message.unpack(data[28:28+types[data[28]]._size])
class Timestamp(Message):
ptype = 13
receive_timestamp = 0
transmit_timestamp = 0
_size = 20
def pack(self):
date = datetime.date.today()
delta = datetime.datetime.now() - datetime.datetime(date.year, date.month, date.day)
self.originate_timestamp = round(delta.seconds*1000 + delta.microseconds/1000)
self.packed = struct.pack('BBHHHIII', self.ptype, self.code, self.checksum, self.identifier, self.sequence, self.originate_timestamp, self.receive_timestamp, self.transmit_timestamp)
self.checksum = self._calcchecksum(self.packed)
self.packed = struct.pack('BBHHHIII', self.ptype, self.code, self.checksum, self.identifier, self.sequence, self.originate_timestamp, self.receive_timestamp, self.transmit_timestamp)
return self.packed
#TODO: implement unpack
class TimestampReply(Message):
_size = 20
def __repr__(self):
return "<" + self.__class__.__name__ + ": type: %d, code: %d>" % (self.ptype, self.code)
def unpack(self, data):
(self.ptype, self.code, self.checksum, self.identifier, self.sequence, self.originate_timestamp, self.receive_timestamp, self.transmit_timestamp) = struct.unpack('BBHHHIII', data[:self._size])
class InformationRequest(Message):
ptype = 15
_size = 8
def pack(self):
self.packed = struct.pack('BBHHH', self.ptype, self.code, self.checksum, self.identifier, self.sequence)
self.checksum = self._calcchecksum(self.packed)
self.packed = struct.pack('BBHHH', self.ptype, self.code, self.checksum, self.identifier, self.sequence)
return self.packed
#TODO: implement unpack
class InformationReply(Message):
def unpack(self, data):
(self.ptype, self.code, self.checksum, self.identifier, self.sequence) = struct.unpack('BBHHH', data)
"""Echo Request message
Ping - request"""
class EchoRequest(Message):
ptype = 8
_size = 8
def pack(self):
self.packed = struct.pack('BBHHH', self.ptype, self.code, self.checksum, self.identifier, self.sequence)
self.checksum = self._calcchecksum(self.packed)
self.packed = struct.pack('BBHHH', self.ptype, self.code, self.checksum, self.identifier, self.sequence)
return self.packed
def unpack(self, data):
(self.ptype, self.code, self.checksum, self.identifier, self.sequence) = struct.unpack('BBHHH', data[:self._size])
"""Echo Reply message
Ping - reply"""
class EchoReply(Message):
_size = 8
def unpack(self, data):
(self.ptype, self.code, self.checksum, self.identifier, self.sequence) = struct.unpack('BBHHH', data[:self._size])
types = {
3: DestinationUnreachable,
11: TimeExceeded,
12: ParameterProblem,
4: SourceQuench,
5: Redirect,
8: EchoRequest,
0: EchoReply,
13: Timestamp,
14: TimestampReply,
15: InformationRequest,
16: InformationReply,
}
error_messages = (DestinationUnreachable, TimeExceeded, ParameterProblem, SourceQuench, Redirect)
reply_messages = (EchoReply, TimestampReply, InformationReply)
request_messages = (EchoRequest, Timestamp, InformationRequest)