-
Notifications
You must be signed in to change notification settings - Fork 1
/
icmp.cc
550 lines (440 loc) · 13.8 KB
/
icmp.cc
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
#include "icmp.h"
#include <netinet/in.h>
// ***** ICMPHHeader DECLARATIONS *****
ICMPHeader::ICMPHeader() : Header(Headers::ICMPHeader)
{
}
ICMPHeader::ICMPHeader(const ICMPHeader &rhs) : Header(Headers::ICMPHeader,rhs)
{}
ICMPHeader::ICMPHeader(const Header &rhs) : Header(Headers::ICMPHeader,rhs)
{}
ICMPHeader::ICMPHeader(const Buffer &rhs) : Header(Headers::ICMPHeader,rhs)
{}
ICMPHeader::ICMPHeader(const char *buf, const unsigned len) : Header(Headers::ICMPHeader,buf,len)
{}
ICMPHeader::~ICMPHeader()
{}
ICMPHeader & ICMPHeader::operator=(const ICMPHeader &rhs)
{
Header::operator=(rhs);
return *this;
}
// ***** ICMPHeader ACCESSOR FUNCTIONS *****
void ICMPHeader::GetType(unsigned char &type) const
{
GetData((char*)&type,1,0);
}
void ICMPHeader::SetType(const unsigned char &type, const Packet &p)
{
SetData((char*)&type,1,0);
RecomputeChecksum(p);
}
void ICMPHeader::GetCode(unsigned char &code) const
{
GetData((char*)&code,1,1);
}
void ICMPHeader::SetCode(const unsigned char &code, const Packet &p)
{
SetData((char*)&code,1,1);
RecomputeChecksum(p);
}
void ICMPHeader::GetChecksum(unsigned short &checksum) const
{
GetData((char*)&checksum,2,2);
checksum=ntohs(checksum);
}
void ICMPHeader::SetChecksum(const unsigned short &checksum)
{
unsigned short set_checksum=htons(checksum);
SetData((char*)&set_checksum,2,2);
}
void ICMPHeader::GetIdentifier(unsigned short &identifier) const
{
GetData((char*)&identifier,2,4);
//identifier=ntohs(identifier);
}
void ICMPHeader::SetIdentifier(const unsigned short &identifier, const Packet &p)
{
//unsigned short set_identifier=htons(identifier);
SetData((char*)&identifier,2,4);
RecomputeChecksum(p);
}
void ICMPHeader::GetSequenceNumber(unsigned short &seqnumber) const
{
GetData((char*)&seqnumber,2,6);
// seqnumber=ntohs(seqnumber);
}
void ICMPHeader::SetSequenceNumber(const unsigned short &seqnumber, const Packet &p)
{
// unsigned short set_seqnumber=htons(seqnumber);
SetData((char*)&seqnumber,2,6);
RecomputeChecksum(p);
}
// ***** ICMPHeader ACCESSOR FUNCTIONS SPECIFIC TO TYPE *****
void ICMPHeader::GetIphandIcmphEightBytes(const Packet &p, Buffer &data)
{
data.Clear();
IPHeader iph = p.FindHeader(Headers::IPHeader);
ICMPHeader icmph = p.FindHeader(Headers::ICMPHeader);
// add the ip header and the 8 bytes of the icmp header
data.AddFront(iph);
data.AddBack(icmph);
}
void ICMPHeader::GetIphandEightBytes(const Packet &p, Buffer &data)
{
data.Clear();
Packet pkt(p);
IPHeader iph = p.FindHeader(Headers::IPHeader);
// add the ip header and the first 8 bytes
data.AddFront(iph);
data.AddBack( pkt.GetPayload().ExtractFront(8));
}
void ICMPHeader::SetIphandEightBytes(Buffer &payload, Buffer &data)
{
payload = data;
}
void ICMPHeader::GetGatewayAddress(IPAddress &gateway_address) const
{
GetData((char*)&gateway_address,4,4);
gateway_address=ntohl(gateway_address);
}
void ICMPHeader::SetGatewayAddress(const IPAddress &gateway_address, const Packet &p)
{
unsigned long set_gateway_address = htonl(gateway_address);
SetData((char*)&set_gateway_address,4,4);
RecomputeChecksum(p);
}
void ICMPHeader::GetPointer(unsigned char &pointer) const
{
GetData((char*)&pointer,1,4);
}
void ICMPHeader::SetPointer(const unsigned char &pointer, const Packet &p)
{
SetData((char*)&pointer,1,4);
RecomputeChecksum(p);
}
void ICMPHeader::GetOriginateTimestamp(const Buffer &payload, unsigned long &originate)
{
payload.GetData((char*)&originate,4,0);
originate=ntohl(originate);
}
void ICMPHeader::SetOriginateTimestamp(Buffer &payload, unsigned long &originate)
{
unsigned long set_originate=htonl(originate);
payload.SetData((char*)&set_originate,4,0);
}
void ICMPHeader::GetReceiveTimestamp(const Buffer &payload, unsigned long &receive)
{
payload.GetData((char*)&receive,4,4);
receive=ntohl(receive);
}
void ICMPHeader::SetReceiveTimestamp(Buffer &payload, unsigned long &receive)
{
unsigned long set_receive=htonl(receive);
payload.SetData((char*)&set_receive,4,4);
}
void ICMPHeader::GetTransmitTimestamp(const Buffer &payload, unsigned long &transmit)
{
payload.GetData((char*)&transmit,4,8);
transmit=ntohl(transmit);
}
void ICMPHeader::SetTransmitTimestamp(Buffer &payload, unsigned long &transmit)
{
unsigned long set_transmit=htonl(transmit);
payload.SetData((char*)&set_transmit,4,8);
}
void ICMPHeader::GetAddressMask(const Buffer &payload, IPAddress &address_mask)
{
payload.GetData((char*)&address_mask,4,0);
address_mask=ntohl(address_mask);
}
void ICMPHeader::SetAddressMask(Buffer &payload, const IPAddress &address_mask)
{
unsigned long set_address_mask = htonl(address_mask);
payload.SetData((char*)&set_address_mask,4,0);
}
// ***** ICMPHeader HELPER FUNCTIONS *****
unsigned short ICMPHeader::ComputeChecksum(const Packet &p) const
{
size_t data_length = ((Packet&)p).GetPayload().GetSize();
size_t total_length = ICMP_HEADER_LENGTH + data_length;
unsigned short buffer[total_length];
memset((char*)buffer, 0, 2*total_length);
GetData((char*)&(buffer[0]), ICMP_HEADER_LENGTH, 0);
((Packet&)p).GetPayload().GetData((char*)&(buffer[ICMP_HEADER_LENGTH]), data_length, 0);
return ~(OnesComplementSum(buffer, total_length));
}
bool ICMPHeader::IsCorrectChecksum(const Packet &p) const
{
unsigned short c;
GetChecksum(c);
if (c==0) {
return true;
} else {
return ComputeChecksum(p)==0;
}
}
void ICMPHeader::RecomputeChecksum(const Packet &p)
{
SetChecksum(0);
unsigned short check=ComputeChecksum(p);
SetChecksum(check);
}
void ICMPHeader::GetCurrentTimeInMilliseconds(unsigned long ¤t)
{
tm *timeptr;
time_t time_value;
// get time elapsed since 01/01/1970
time_value = time(NULL);
// convert to local time
timeptr = localtime(&time_value);
int conversion = 1000;
int seconds = timeptr->tm_sec;
int minutes = timeptr->tm_min;
int hours = timeptr->tm_hour;
// calculate milliseconds after midnight
current = (long)((seconds*conversion) +
(minutes*60*conversion) +
(hours*60*60*conversion));
}
ostream & ICMPHeader::Print(ostream &os) const
{
unsigned char type, code;
unsigned short checksum, identifier, seqnumber;
GetType(type);
GetCode(code);
GetChecksum(checksum);
GetIdentifier(identifier);
GetSequenceNumber(seqnumber);
os << "ICMPHeader(type=" << unsigned(type)
<< ", code=" << unsigned(code)
<< ", checksum=" << checksum
<< ", identifier=" << identifier
<< ", seqnumber=" << seqnumber
<< ")";
return os;
}
// ***** ICMPPacket DECLARATIONS *****
ICMPPacket::ICMPPacket() : Packet()
{}
ICMPPacket::ICMPPacket(const Packet &rhs) : Packet(rhs)
{}
ICMPPacket::ICMPPacket(const Packet &original,
const IPAddress &source, const IPAddress &destination,
const unsigned char &type, const unsigned char &code,
const unsigned short &identifier, const unsigned short &seqnumber,
Buffer &data) : Packet()
{
// create headers, modify data
IPHeader iph;
iph.SetTOS(0);
iph.SetTotalLength(48);
iph.SetProtocol(IP_PROTO_ICMP);
iph.SetSourceIP(source);
iph.SetDestIP(destination);
ICMPHeader icmph;
icmph.SetType(type, (*this));
if (unsigned(type) == PARAMETER_PROBLEM)
{
// code is interpreted as the place where the error is
icmph.SetPointer(code, (*this));
}
else if (unsigned(type) == TIMESTAMP_REQUEST)
{
unsigned long current_time;
icmph.GetCurrentTimeInMilliseconds(current_time);
icmph.SetOriginateTimestamp(data, current_time);
}
else if (unsigned(type) == ADDRESSMASK_REQUEST)
{
// set to broadcast ip
iph.SetDestIP("IP_ADDRESS_BROADCAST");
}
payload = data;
icmph.SetCode(code, (*this));
icmph.SetIdentifier(identifier, (*this));
icmph.SetSequenceNumber(seqnumber, (*this));
PushHeader(icmph);
PushHeader(iph);
}
//request / reply constructors
ICMPPacket::ICMPPacket(const IPAddress &destination,
const unsigned char &type, const unsigned char &code,
const unsigned short &identifier, const unsigned short &seqnumber)
{
// create new payload
unsigned short buffer[56];
memset((char*)buffer, 0, 2*56);
Buffer payload((char*)buffer, 56);
ICMPPacket p((*this), getenv("MINET_IPADDR"), destination, type, code, identifier, seqnumber, payload);
*this = p;
}
ICMPPacket::ICMPPacket(const IPAddress &destination,
const unsigned char &type, const unsigned char &code)
{
// create new payload
unsigned short buffer[56];
memset((char*)buffer, 0, 2*56);
Buffer payload((char*)buffer, 56);
ICMPPacket p((*this), getenv("MINET_IPADDR"), destination, type, code, 0, 0, payload);
*this = p;
}
ICMPPacket::ICMPPacket(const IPAddress &destination,
const unsigned char &type)
{
// create new payload
unsigned short buffer[56];
memset((char*)buffer, 0, 2*56);
Buffer payload((char*)buffer, 56);
ICMPPacket p((*this), getenv("MINET_IPADDR"), destination, type, 0, 0, 0, payload);
*this = p;
}
// error constructors
ICMPPacket::ICMPPacket(const IPAddress &destination,
const unsigned char &type, const unsigned char &code,
const unsigned short &identifier, const unsigned short &seqnumber,
const Packet &p)
{
// extract necessary data
Packet pkt(p);
unsigned short buffer[56];
memset((char*)buffer, 0, 2*56);
Buffer data((char*)buffer, 56);
ExtractIphandEightBytes(pkt, data);
ICMPPacket error((*this), getenv("MINET_IPADDR"), destination, type, code, identifier, seqnumber, data);
*this = error;
}
ICMPPacket::ICMPPacket(const IPAddress &destination,
const unsigned char &type, const unsigned char &code,
const Packet &p)
{
// extract necessary data
Packet pkt(p);
unsigned short buffer[56];
memset((char*)buffer, 0, 2*56);
Buffer data((char*)buffer, 56);
ExtractIphandEightBytes(pkt, data);
ICMPPacket error((*this), getenv("MINET_IPADDR"), destination, type, code, 0, 0, data);
*this = error;
}
// ***** ICMPPacket ACCESSOR FUNCTIONS *****
void ICMPPacket::ExtractIphandIcmphEightBytes(const Packet &p, Buffer &data)
{
data.Clear();
IPHeader iph = p.FindHeader(Headers::IPHeader);
ICMPHeader icmph = p.FindHeader(Headers::ICMPHeader);
// add the ip header and the 8 bytes of the icmp header
data.AddFront(iph);
data.AddBack(icmph);
}
void ICMPPacket::ExtractIphandEightBytes(const Packet &p, Buffer &data)
{
data.Clear();
Packet pkt(p);
IPHeader iph = p.FindHeader(Headers::IPHeader);
// add the ip header and the first 8 bytes
data.AddFront(iph);
data.AddBack( pkt.GetPayload().ExtractFront(8));
}
void ICMPPacket::SetIphandEightBytes(Buffer &payload, Buffer &data)
{
payload = data;
}
// ***** ICMPPacket HELPER FUNCTIONS *****
void ICMPPacket::respond(const Packet &p)
{
ICMPPacket response(p);
*this = response;
// get header information
EthernetHeader eh = PopHeader();
IPHeader iph = FindHeader(Headers::IPHeader);
ICMPHeader icmph = FindHeader(Headers::ICMPHeader);
// reverse source/ destination ip addresses
IPAddress source, destination;
iph.GetSourceIP(source); iph.GetDestIP(destination);
iph.SetSourceIP(destination); iph.SetDestIP(source);
// handle response/error messaging
unsigned char icmp_type; unsigned char icmp_code;
icmph.GetType(icmp_type); icmph.GetCode(icmp_code);
if ((unsigned(icmp_type) == ECHO_REQUEST) ||
(unsigned(icmp_type) == TIMESTAMP_REQUEST) ||
(unsigned(icmp_type) == ADDRESSMASK_REQUEST))
{
handle_response(iph, icmph, payload, icmp_type);
// modify outgoing packet
SetHeader(iph);
SetHeader(icmph);
}
else if ((unsigned(icmp_type) == ECHO_REPLY) ||
(unsigned(icmp_type) == TIMESTAMP_REPLY) ||
(unsigned(icmp_type) == ADDRESSMASK_REPLY) ||
(unsigned(icmp_type) == DESTINATION_UNREACHABLE) ||
(unsigned(icmp_type) == SOURCE_QUENCH) ||
(unsigned(icmp_type) == REDIRECT) ||
(unsigned(icmp_type) == TIME_EXCEEDED) ||
(unsigned(icmp_type) == PARAMETER_PROBLEM))
handle_error(iph, icmph, payload, icmp_type);
else
cout << "Invalid ICMP Packet" << endl;
}
void ICMPPacket::respond(const RawEthernetPacket &rp)
{
Packet p(rp);
p.ExtractHeaderFromPayload<EthernetHeader>(ETHERNET_HEADER_LEN);
p.ExtractHeaderFromPayload<IPHeader>(IPHeader::EstimateIPHeaderLength(p));
p.ExtractHeaderFromPayload<ICMPHeader>(ICMP_HEADER_LENGTH);
respond(p);
}
void ICMPPacket::respond_in_ip_module(const Packet &p)
{
Packet pkt(p);
pkt.ExtractHeaderFromPayload<ICMPHeader>(ICMP_HEADER_LENGTH);
respond(pkt);
}
bool ICMPPacket::requires_reply()
{
return requires_response;
}
void ICMPPacket::handle_response(IPHeader &iph, ICMPHeader &icmph,
Buffer &payload, unsigned char &icmp_type)
{
if (unsigned(icmp_type) == ECHO_REQUEST) {
// change type to ECHO_REPLY
icmph.SetType(ECHO_REPLY, (*this));
requires_response = true;
}
else if (unsigned(icmp_type) == TIMESTAMP_REQUEST) {
// change type to TIMESTAMP_REPLY
icmph.SetType(TIMESTAMP_REPLY, (*this));
unsigned long current_time;
icmph.GetCurrentTimeInMilliseconds(current_time);
icmph.SetOriginateTimestamp(payload, current_time);
}
else if (unsigned(icmp_type) == ADDRESSMASK_REQUEST) {
// change type to ADDRESSMASK_REPLY
icmph.SetType(ADDRESSMASK_REPLY, (*this));
icmph.SetAddressMask(payload, "255.255.0.0");
icmph.RecomputeChecksum( (*this));
requires_response = true;
}
}
void ICMPPacket::handle_error(IPHeader &iph, ICMPHeader &icmph, Buffer &payload, unsigned char &icmp_type)
{
if (unsigned(icmp_type) == REDIRECT) {
; // modify route table
}
requires_response = false;
}
// ***** GENERAL HELPER FUNCTIONS *****
void DebugDump(Packet p)
{
//EthernetHeader eh = p.FindHeader(Headers::EthernetHeader);
IPHeader iph = p.FindHeader(Headers::IPHeader);
ICMPHeader icmph = p.FindHeader(Headers::ICMPHeader);
Buffer payload = p.GetPayload();
//eh.Print(cerr); cerr << endl;
iph.Print(cerr); cerr << endl;
icmph.Print(cerr); cerr<< endl;
p.Print(cerr); cerr << endl;
payload.Print(cerr); cerr << endl;
}