-
Notifications
You must be signed in to change notification settings - Fork 0
/
traceroute.py
1074 lines (923 loc) · 52.2 KB
/
traceroute.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
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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Patrick Stumbaugh
# Project 3 - Traceroute
# No additional sources uesed besides skeleton code
# I rebuilt the "sendEchoRequest()" function for the traceroute program, in order to change some of
# the print statements and variables used so that it's more readable as a traceroute and not a ping program
# #################################################################################################################### #
# Imports #
# #################################################################################################################### #
import os
from socket import *
import struct
import time
import select
from typing import Sequence
# global variables
minRTT = -1
maxRTT = -1
numberOfRTTs = 0
totalRTTtime = 0
packetsSent = 0
packetErrors = 0
# sets the min and max global RTT variables, depending upon the currentRTT
def setMinMaxRTT(currentRTT):
global minRTT
global maxRTT
global numberOfRTTs
global totalRTTtime
# if it is the first iteration:
if minRTT == -1:
minRTT = currentRTT
maxRTT = currentRTT
# update our min and max if applicable
if currentRTT < minRTT:
minRTT = currentRTT
if currentRTT > maxRTT:
maxRTT = currentRTT
# update our totals as well
totalRTTtime = totalRTTtime + currentRTT
numberOfRTTs = numberOfRTTs + 1
# returns the average RTT. If no RTT's, returns 0
def getAvgRTT():
if numberOfRTTs == 0: # no RTT's recorded, do nothing
return 0
else:
return totalRTTtime / numberOfRTTs # return the average
# prints out min, max and average RTT's
# the average RTT is calculated on exact RTT's, not the rounded that min and max will show
def printRTTs():
average = getAvgRTT()
print(" [*] PING RTT REPORT: ")
print(" Min RTT: ", round(minRTT))
print(" Max RTT: ", round(maxRTT))
print(
" Avg RTT: ", round(average, 2), " *based on exact RTT times (not rounded)"
)
# clears our RTT variables
def clearRTTs():
global minRTT
global maxRTT
global numberOfRTTs
global totalRTTtime
# reset variables
minRTT = -1
maxRTT = -1
numberOfRTTs = 0
totalRTTtime = 0
# clears our packet error sum variables
def clearPacketErrorSum():
global packetsSent
global packetErrors
# reset variables
packetsSent = 0
packetErrors = 0
# prints the number of packets lost during pings
def printPacketLossPercentage():
global packetsSent
global packetErrors
if packetErrors == 0:
percentLost = 0
else:
percentLost = (packetsSent / packetErrors) * 100
percentSuccess = 100 - percentLost
print(" [*] Packet Calculations: ")
print(" Total Packets Sent: ", packetsSent)
print(" Packets Lost: ", round(percentLost, 2), "%")
print(" Packet Success: ", round(percentSuccess, 2), "%")
# #################################################################################################################### #
# Class IcmpHelperLibrary #
# #################################################################################################################### #
class IcmpHelperLibrary:
# ################################################################################################################ #
# Class IcmpPacket #
# #
# References: #
# https://www.iana.org/assignments/icmp-parameters/icmp-parameters.xhtml #
# #
# ################################################################################################################ #
class IcmpPacket:
# ############################################################################################################ #
# IcmpPacket Class Scope Variables #
# #
# #
# #
# #
# ############################################################################################################ #
__icmpTarget = "" # Remote Host
__destinationIpAddress = "" # Remote Host IP Address
__header = b"" # Header after byte packing
__data = b"" # Data after encoding
__dataRaw = "" # Raw string data before encoding
# Valid values are 0-255 (unsigned int, 8 bits)
__icmpType = 0
# Valid values are 0-255 (unsigned int, 8 bits)
__icmpCode = 0
# Valid values are 0-65535 (unsigned short, 16 bits)
__packetChecksum = 0
# Valid values are 0-65535 (unsigned short, 16 bits)
__packetIdentifier = 0
# Valid values are 0-65535 (unsigned short, 16 bits)
__packetSequenceNumber = 0
__ipTimeout = 30
__ttl = 255 # Time to live
__DEBUG_IcmpPacket = False # Allows for debug output
# ############################################################################################################ #
# IcmpPacket Class Getters #
# ############################################################################################################ #
def getIcmpTarget(self):
return self.__icmpTarget
def getDataRaw(self):
return self.__dataRaw
def getIcmpType(self):
return self.__icmpType
def getIcmpCode(self):
return self.__icmpCode
def getPacketChecksum(self):
return self.__packetChecksum
def getPacketIdentifier(self):
return self.__packetIdentifier
def getPacketSequenceNumber(self):
return self.__packetSequenceNumber
def getTtl(self):
return self.__ttl
# ############################################################################################################ #
# IcmpPacket Class Setters #
# #
# #
# #
# #
# ############################################################################################################ #
def setIcmpTarget(self, icmpTarget):
self.__icmpTarget = icmpTarget
# Only attempt to get destination address if it is not whitespace
if len(self.__icmpTarget.strip()) > 0:
self.__destinationIpAddress = gethostbyname(self.__icmpTarget.strip())
def setIcmpType(self, icmpType):
self.__icmpType = icmpType
def setIcmpCode(self, icmpCode):
self.__icmpCode = icmpCode
def setPacketChecksum(self, packetChecksum):
self.__packetChecksum = packetChecksum
def setPacketIdentifier(self, packetIdentifier):
self.__packetIdentifier = packetIdentifier
def setPacketSequenceNumber(self, sequenceNumber):
self.__packetSequenceNumber = sequenceNumber
def setTtl(self, ttl):
self.__ttl = ttl
# ############################################################################################################ #
# IcmpPacket Class Private Functions #
# #
# #
# #
# #
# ############################################################################################################ #
def __recalculateChecksum(self):
print("calculateChecksum Started...") if self.__DEBUG_IcmpPacket else 0
packetAsByteData = b"".join([self.__header, self.__data])
checksum = 0
# This checksum function will work with pairs of values with two separate 16 bit segments. Any remaining
# 16 bit segment will be handled on the upper end of the 32 bit segment.
countTo = (len(packetAsByteData) // 2) * 2
# Calculate checksum for all paired segments
print(
f'{"Count":10} {"Value":10} {"Sum":10}'
) if self.__DEBUG_IcmpPacket else 0
count = 0
while count < countTo:
thisVal = packetAsByteData[count + 1] * 256 + packetAsByteData[count]
checksum = checksum + thisVal
checksum = (
checksum & 0xFFFFFFFF
) # Capture 16 bit checksum as 32 bit value
print(
f"{count:10} {hex(thisVal):10} {hex(checksum):10}"
) if self.__DEBUG_IcmpPacket else 0
count = count + 2
# Calculate checksum for remaining segment (if there are any)
if countTo < len(packetAsByteData):
thisVal = packetAsByteData[len(packetAsByteData) - 1]
checksum = checksum + thisVal
checksum = checksum & 0xFFFFFFFF # Capture as 32 bit value
print(
count, "\t", hex(thisVal), "\t", hex(checksum)
) if self.__DEBUG_IcmpPacket else 0
# Add 1's Complement Rotation to original checksum
# Rotate and add to base 16 bits
checksum = (checksum >> 16) + (checksum & 0xFFFF)
checksum = (checksum >> 16) + checksum # Rotate and add
answer = ~checksum # Invert bits
answer = answer & 0xFFFF # Trim to 16 bit value
answer = answer >> 8 | (answer << 8 & 0xFF00)
print("Checksum: ", hex(answer)) if self.__DEBUG_IcmpPacket else 0
self.setPacketChecksum(answer)
def __packHeader(self):
# The following header is based on http://www.networksorcery.com/enp/protocol/icmp/msg8.htm
# Type = 8 bits
# Code = 8 bits
# ICMP Header Checksum = 16 bits
# Identifier = 16 bits
# Sequence Number = 16 bits
self.__header = struct.pack(
"!BBHHH",
self.getIcmpType(), # 8 bits / 1 byte / Format code B
self.getIcmpCode(), # 8 bits / 1 byte / Format code B
self.getPacketChecksum(), # 16 bits / 2 bytes / Format code H
self.getPacketIdentifier(), # 16 bits / 2 bytes / Format code H
self.getPacketSequenceNumber(), # 16 bits / 2 bytes / Format code H
)
def __encodeData(self):
# Used to track overall round trip time
data_time = struct.pack("d", time.time())
# time.time() creates a 64 bit value of 8 bytes
dataRawEncoded = self.getDataRaw().encode("utf-8")
self.__data = data_time + dataRawEncoded
def __packAndRecalculateChecksum(self):
# Checksum is calculated with the following sequence to confirm data in up to date
# packHeader() and encodeData() transfer data to their respective bit
self.__packHeader()
# locations, otherwise, the bit sequences are empty or incorrect.
self.__encodeData()
self.__recalculateChecksum() # Result will set new checksum value
self.__packHeader() # Header is rebuilt to include new checksum value
# 1. Update the __validateIcmpReplyPacketWithOriginalPingData() function
def __validateIcmpReplyPacketWithOriginalPingData(self, icmpReplyPacket):
checkFlag = True
# check sequence number
if (
self.getPacketSequenceNumber()
!= icmpReplyPacket.getIcmpSequenceNumber()
):
checkFlag = False
print(
"Packet Sequence number expected: ",
self.getPacketSequenceNumber(),
", BUT - Packet Sequence number received: ",
icmpReplyPacket.getIcmpSequenceNumber(),
)
icmpReplyPacket.setIcmpIdentifier_isValid(False)
else:
icmpReplyPacket.setIcmpIdentifier_isValid(True)
# check Identifier
if self.getPacketIdentifier() != icmpReplyPacket.getIcmpIdentifier():
checkFlag = False
print(
"Packet Identifier expected: ",
self.getPacketIdentifier(),
", BUT - Packet Identifier received: ",
icmpReplyPacket.getIcmpIdentifier(),
)
icmpReplyPacket.setIcmpSequenceNumber_isValid(False)
else:
icmpReplyPacket.setIcmpSequenceNumber_isValid(True)
# check raw data
if self.getDataRaw() != icmpReplyPacket.getIcmpData():
checkFlag = False
print(
"Raw Data expected: ",
self.getDataRaw(),
", BUT - Raw Data received: ",
icmpReplyPacket.getDataRaw(),
)
icmpReplyPacket.setIcmpRawData_isValid_isValid(False)
else:
icmpReplyPacket.setIcmpRawData_isValid_isValid(True)
# There was some sort of an error:
if checkFlag == False:
icmpReplyPacket.setIsValidResponse(False)
# no error, continue on
else:
icmpReplyPacket.setIsValidResponse(True)
pass
# ############################################################################################################ #
# IcmpPacket Class Public Functions #
# #
# #
# #
# #
# ############################################################################################################ #
def buildPacket_echoRequest(self, packetIdentifier, packetSequenceNumber):
self.setIcmpType(8)
self.setIcmpCode(0)
self.setPacketIdentifier(packetIdentifier)
self.setPacketSequenceNumber(packetSequenceNumber)
self.__dataRaw = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
self.__packAndRecalculateChecksum()
def sendEchoRequest(self):
global packetsSent
global packetErrors
packetsSent = packetsSent + 1
if (
len(self.__icmpTarget.strip())
<= 0 | len(self.__destinationIpAddress.strip())
<= 0
):
self.setIcmpTarget("127.0.0.1")
print("Pinging (" + self.__icmpTarget + ") " + self.__destinationIpAddress)
mySocket = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)
mySocket.settimeout(self.__ipTimeout)
mySocket.bind(("", 0))
mySocket.setsockopt(
IPPROTO_IP, IP_TTL, struct.pack("I", self.getTtl())
) # Unsigned int - 4 bytes
try:
mySocket.sendto(
b"".join([self.__header, self.__data]),
(self.__destinationIpAddress, 0),
)
timeLeft = 30
pingStartTime = time.time()
startedSelect = time.time()
whatReady = select.select([mySocket], [], [], timeLeft)
endSelect = time.time()
howLongInSelect = endSelect - startedSelect
if whatReady[0] == []: # Timeout
packetErrors = packetErrors + 1
print(
" * * * * * Request timed out."
)
# recvPacket - bytes object representing data received
recvPacket, addr = mySocket.recvfrom(1024)
# addr - address of socket sending data
timeReceived = time.time()
timeLeft = timeLeft - howLongInSelect
if timeLeft <= 0:
packetErrors = packetErrors + 1
print(
" * * * * * Request timed out (By no remaining time left)."
)
else:
# Fetch the ICMP type and code from the received packet
icmpType, icmpCode = recvPacket[20:22]
# 4. Your program can only detect timeouts in receiving ICMP echo responses.
# Modify the Pinger program to parse the ICMP response error codes and display the corresponding error results to the user.
if icmpType == 3: # Destination Unreachable
packetErrors = packetErrors + 1
print(
" TTL=%d RTT=%.0f ms Type=%d Code=%d %s"
% (
self.getTtl(),
(timeReceived - pingStartTime) * 1000,
icmpType,
icmpCode,
addr[0],
)
)
if icmpCode == 0:
print(
"ICMP CODE: ",
icmpCode,
", Desination Network Unreachable",
)
elif icmpCode == 1:
print(
"ICMP CODE: ", icmpCode, ", Desination Host Unreachable"
)
elif icmpCode == 2:
print(
"ICMP CODE: ",
icmpCode,
", Desination Protocol Unreachable",
)
elif icmpCode == 3:
print(
"ICMP CODE: ", icmpCode, ", Desination Port Unreachable"
)
elif icmpCode == 6:
print(
"ICMP CODE: ", icmpCode, ", Desination Network Unknown"
)
elif icmpCode == 7:
print("ICMP CODE: ", icmpCode, ", Desination Host Unknown")
else: # generic error
print("ICMP CODE: ", icmpCode, ", unknown error")
elif icmpType == 4: # Source Quence
packetErrors = packetErrors + 1
print(
" TTL=%d RTT=%.0f ms Type=%d Code=%d %s"
% (
self.getTtl(),
(timeReceived - pingStartTime) * 1000,
icmpType,
icmpCode,
addr[0],
)
)
if icmpCode == 0:
print("ICMP CODE: ", icmpCode, ", Source Quench")
else: # generic error
print("ICMP CODE: ", icmpCode, ", unknown error")
elif icmpType == 11: # Time Exceeded
packetErrors = packetErrors + 1
print(
" TTL=%d RTT=%.0f ms Type=%d Code=%d %s"
% (
self.getTtl(),
(timeReceived - pingStartTime) * 1000,
icmpType,
icmpCode,
addr[0],
)
)
if icmpCode == 0:
print("ICMP CODE: ", icmpCode, ", TTL Expired")
else: # generic error
print("ICMP CODE: ", icmpCode, ", unknown error")
elif icmpType == 12: # bad IP header
packetErrors = packetErrors + 1
print(
" TTL=%d RTT=%.0f ms Type=%d Code=%d %s"
% (
self.getTtl(),
(timeReceived - pingStartTime) * 1000,
icmpType,
icmpCode,
addr[0],
)
)
if icmpCode == 0:
print("ICMP CODE: ", icmpCode, ", Bad IP Header")
else: # generic error
print("ICMP CODE: ", icmpCode, ", unknown error")
elif icmpType == 0: # Echo Reply
icmpReplyPacket = IcmpHelperLibrary.IcmpPacket_EchoReply(
recvPacket
)
self.__validateIcmpReplyPacketWithOriginalPingData(
icmpReplyPacket
)
icmpReplyPacket.printResultToConsole(
self.getTtl(), timeReceived, addr, self
)
return # Echo reply is the end and therefore should return
else:
print("unknown error")
except timeout:
print(
" * * * * * Request timed out (By Exception)."
)
finally:
mySocket.close()
# ----------
# I rebuilt the echo request to set some features I didn't want changing in the original
def sendTraceRouteEchoRequest(self):
if (
len(self.__icmpTarget.strip())
<= 0 | len(self.__destinationIpAddress.strip())
<= 0
):
self.setIcmpTarget("127.0.0.1")
print(
"Tracing to (" + self.__icmpTarget + ") " + self.__destinationIpAddress,
" with TTL of ",
self.getTtl(),
)
mySocket = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)
mySocket.settimeout(self.__ipTimeout)
mySocket.bind(("", 0))
mySocket.setsockopt(
IPPROTO_IP, IP_TTL, struct.pack("I", self.getTtl())
) # Unsigned int - 4 bytes
try:
mySocket.sendto(
b"".join([self.__header, self.__data]),
(self.__destinationIpAddress, 0),
)
timeLeft = 30
pingStartTime = time.time()
startedSelect = time.time()
whatReady = select.select([mySocket], [], [], timeLeft)
endSelect = time.time()
howLongInSelect = endSelect - startedSelect
if whatReady[0] == []: # Timeout
print(
" * * * * * Request timed out."
)
# recvPacket - bytes object representing data received
recvPacket, addr = mySocket.recvfrom(1024)
# addr - address of socket sending data
timeReceived = time.time()
timeLeft = timeLeft - howLongInSelect
if timeLeft <= 0:
print(
" * * * * * Request timed out (By no remaining time left)."
)
else:
# Fetch the ICMP type and code from the received packet
icmpType, icmpCode = recvPacket[20:22]
if icmpType == 3: # Destination Unreachable
print(
" TTL=%d RTT=%.0f ms Type=%d Code=%d %s"
% (
self.getTtl(),
(timeReceived - pingStartTime) * 1000,
icmpType,
icmpCode,
addr[0],
)
)
if icmpCode == 0:
print(
"ICMP CODE: ",
icmpCode,
", Desination Network Unreachable",
)
elif icmpCode == 1:
print(
"ICMP CODE: ", icmpCode, ", Desination Host Unreachable"
)
elif icmpCode == 2:
print(
"ICMP CODE: ",
icmpCode,
", Desination Protocol Unreachable",
)
elif icmpCode == 3:
print(
"ICMP CODE: ", icmpCode, ", Desination Port Unreachable"
)
elif icmpCode == 6:
print(
"ICMP CODE: ", icmpCode, ", Desination Network Unknown"
)
elif icmpCode == 7:
print("ICMP CODE: ", icmpCode, ", Desination Host Unknown")
else: # generic error
print("ICMP CODE: ", icmpCode, ", unknown error")
elif icmpType == 4: # Source Quence
print(
" TTL=%d RTT=%.0f ms Type=%d Code=%d %s"
% (
self.getTtl(),
(timeReceived - pingStartTime) * 1000,
icmpType,
icmpCode,
addr[0],
)
)
if icmpCode == 0:
print("ICMP CODE: ", icmpCode, ", Source Quench")
else: # generic error
print("ICMP CODE: ", icmpCode, ", unknown error")
elif icmpType == 11: # Time Exceeded
self.setIcmpType(icmpType)
self.setIcmpCode(icmpCode)
print(
" TTL=%d RTT=%.0f ms Type=%d Code=%d %s"
% (
self.getTtl(),
(timeReceived - pingStartTime) * 1000,
icmpType,
icmpCode,
addr[0],
)
)
if icmpCode == 0:
print("ICMP CODE: ", icmpCode, ", TTL Expired")
else: # generic error
print("ICMP CODE: ", icmpCode, ", unknown error")
elif icmpType == 12: # bad IP header
print(
" TTL=%d RTT=%.0f ms Type=%d Code=%d %s"
% (
self.getTtl(),
(timeReceived - pingStartTime) * 1000,
icmpType,
icmpCode,
addr[0],
)
)
if icmpCode == 0:
print("ICMP CODE: ", icmpCode, ", Bad IP Header")
else: # generic error
print("ICMP CODE: ", icmpCode, ", unknown error")
elif icmpType == 0: # Echo Reply
icmpReplyPacket = IcmpHelperLibrary.IcmpPacket_EchoReply(
recvPacket
)
self.__validateIcmpReplyPacketWithOriginalPingData(
icmpReplyPacket
)
icmpReplyPacket.printResultToConsole(
self.getTtl(), timeReceived, addr, self
)
return # Echo reply is the end and therefore should return
else:
print("error")
except timeout:
print(
" * * * * * Request timed out (By Exception)."
)
finally:
mySocket.close()
# -------
def printIcmpPacketHeader_hex(self):
print("Header Size: ", len(self.__header))
for i in range(len(self.__header)):
print("i=", i, " --> ", self.__header[i : i + 1].hex())
def printIcmpPacketData_hex(self):
print("Data Size: ", len(self.__data))
for i in range(len(self.__data)):
print("i=", i, " --> ", self.__data[i : i + 1].hex())
def printIcmpPacket_hex(self):
print("Printing packet in hex...")
self.printIcmpPacketHeader_hex()
self.printIcmpPacketData_hex()
# ################################################################################################################ #
# Class IcmpPacket_EchoReply #
# #
# References: #
# http://www.networksorcery.com/enp/protocol/icmp/msg0.htm #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# ################################################################################################################ #
class IcmpPacket_EchoReply:
# ############################################################################################################ #
# IcmpPacket_EchoReply Class Scope Variables #
# #
# #
# #
# #
# ############################################################################################################ #
__recvPacket = b""
__isValidResponse = False
__IcmpIdentifier_isValid = False
__IcmpSequenceNumber_isValid = False
__IcmpRawData_isValid = False
# ############################################################################################################ #
# IcmpPacket_EchoReply Constructors #
# #
# #
# #
# #
# ############################################################################################################ #
def __init__(self, recvPacket):
self.__recvPacket = recvPacket
# ############################################################################################################ #
# IcmpPacket_EchoReply Getters #
# #
# #
# #
# #
# ############################################################################################################ #
def getIcmpType(self):
# Method 1
# bytes = struct.calcsize("B") # Format code B is 1 byte
# return struct.unpack("!B", self.__recvPacket[20:20 + bytes])[0]
# Method 2
return self.__unpackByFormatAndPosition("B", 20)
def getIcmpCode(self):
# Method 1
# bytes = struct.calcsize("B") # Format code B is 1 byte
# return struct.unpack("!B", self.__recvPacket[21:21 + bytes])[0]
# Method 2
return self.__unpackByFormatAndPosition("B", 21)
def getIcmpHeaderChecksum(self):
# Method 1
# bytes = struct.calcsize("H") # Format code H is 2 bytes
# return struct.unpack("!H", self.__recvPacket[22:22 + bytes])[0]
# Method 2
return self.__unpackByFormatAndPosition("H", 22)
def getIcmpIdentifier(self):
# Method 1
# bytes = struct.calcsize("H") # Format code H is 2 bytes
# return struct.unpack("!H", self.__recvPacket[24:24 + bytes])[0]
# Method 2
return self.__unpackByFormatAndPosition("H", 24)
def getIcmpSequenceNumber(self):
# Method 1
# bytes = struct.calcsize("H") # Format code H is 2 bytes
# return struct.unpack("!H", self.__recvPacket[26:26 + bytes])[0]
# Method 2
return self.__unpackByFormatAndPosition("H", 26)
def getDateTimeSent(self):
# This accounts for bytes 28 through 35 = 64 bits
# Used to track overall round trip time
return self.__unpackByFormatAndPosition("d", 28)
# time.time() creates a 64 bit value of 8 bytes
def getIcmpData(self):
# This accounts for bytes 36 to the end of the packet.
return self.__recvPacket[36:].decode("utf-8")
def isValidResponse(self):
return self.__isValidResponse
# 1(c) New getters ->
def getIcmpIdentifier_isValid(self):
return self.__IcmpIdentifier_isValid
def getIcmpSequenceNumber_isValid(self):
return self.__IcmpSequenceNumber_isValid
def getIcmpRawData_isValid(self):
return self.__IcmpRawData_isValid
# ############################################################################################################ #
# IcmpPacket_EchoReply Setters #
# #
# #
# #
# #
# ############################################################################################################ #
def setIsValidResponse(self, booleanValue):
self.__isValidResponse = booleanValue
# 1(c) New Setters ->
def setIcmpIdentifier_isValid(self, booleanValue):
self.__IcmpIdentifier_isValid = booleanValue
def setIcmpSequenceNumber_isValid(self, booleanValue):
self.__IcmpSequenceNumber_isValid = booleanValue
def setIcmpRawData_isValid_isValid(self, booleanValue):
self.__IcmpRawData_isValid = booleanValue
# ############################################################################################################ #
# IcmpPacket_EchoReply Private Functions #
# #
# #
# #
# #
# ############################################################################################################ #
def __unpackByFormatAndPosition(self, formatCode, basePosition):
numberOfbytes = struct.calcsize(formatCode)
return struct.unpack(
"!" + formatCode,
self.__recvPacket[basePosition : basePosition + numberOfbytes],
)[0]
# ############################################################################################################ #
# IcmpPacket_EchoReply Public Functions #
# #
# #
# #
# #
# ############################################################################################################ #
# 2. Update the printResultToConsole() function:
# Identify if the echo response is valid and report the error information details.
# For example, if the raw data is different, print to the console what the expected value and the actual value.
# 3. Currently, the program calculates the round-trip time for each packet and prints it out individually.
# Modify the code to correspond to the way the standard ping program works.
# You will need to report the minimum, maximum, and average RTTs at the end of all pings from the client.
# In addition, calculate the packet loss rate (in percentage).
# It is recommended to create an output that is easily readable with the amount of data used for a trace route since a ping
# is the foundation for such functionality.
def printResultToConsole(self, ttl, timeReceived, addr, original):
bytes = struct.calcsize("d")
timeSent = struct.unpack("d", self.__recvPacket[28 : 28 + bytes])[0]
print(
" TTL=%d RTT=%.0f ms Type=%d Code=%d Identifier=%d Sequence Number=%d %s"
% (
ttl,
(timeReceived - timeSent) * 1000,
self.getIcmpType(),
self.getIcmpCode(),
self.getIcmpIdentifier(),
self.getIcmpSequenceNumber(),
addr[0],
)
)
if self.getIcmpIdentifier_isValid == False:
print(
"ICMP Identifier invalid. Received: ",
self.getIcmpIdentifier(),
"BUT - expected ",
original.getPacketIdentifier(),
)
if self.getIcmpSequenceNumber_isValid == False:
print(
"ICMP Sequence Number invalid. Received: ",
self.getIcmpSequenceNumber(),
"BUT - expected ",
original.getPacketSequenceNumber(),
)
if self.getIcmpRawData_isValid == False:
print(
"ICMP Raw Data invalid. Received: ",
self.getIcmpData(),
"BUT - expected ",
original.getDataRaw(),
)
currentRTT = (timeReceived - timeSent) * 1000
setMinMaxRTT(currentRTT) # set our min and max RTT's
# ################################################################################################################ #
# Class IcmpHelperLibrary #
# #
# #
# #
# #
# ################################################################################################################ #
# ################################################################################################################ #
# IcmpHelperLibrary Class Scope Variables #
# #
# #
# #
# #
# ################################################################################################################ #
__DEBUG_IcmpHelperLibrary = False # Allows for debug output
# ################################################################################################################ #
# IcmpHelperLibrary Private Functions #
# #
# #
# #
# #
# ################################################################################################################ #
def __sendIcmpEchoRequest(self, host):
print("sendIcmpEchoRequest Started...") if self.__DEBUG_IcmpHelperLibrary else 0
for i in range(4):
# Build packet
icmpPacket = IcmpHelperLibrary.IcmpPacket()
# Get as 16 bit number - Limit based on ICMP header standards
randomIdentifier = os.getpid() & 0xFFFF
# Some PIDs are larger than 16 bit
packetIdentifier = randomIdentifier
packetSequenceNumber = i
icmpPacket.buildPacket_echoRequest(
packetIdentifier, packetSequenceNumber
) # Build ICMP for IP payload
icmpPacket.setIcmpTarget(host)
# Build IP
icmpPacket.sendEchoRequest()
icmpPacket.printIcmpPacketHeader_hex() if self.__DEBUG_IcmpHelperLibrary else 0
icmpPacket.printIcmpPacket_hex() if self.__DEBUG_IcmpHelperLibrary else 0
# we should be confirming values are correct, such as identifier and sequence number and data
def __sendIcmpTraceRoute(self, host):
print("sendIcmpTraceRoute Started...") if self.__DEBUG_IcmpHelperLibrary else 0
# 5. The skeleton code currently has a placeholder for performing a trace route function.
# It starts with the traceRoute() function and uses private functions to carry out the implementation.
for ttl in range(1, 255): # assume 255 for our max ttl we will allow
# Build packet
icmpPacket = IcmpHelperLibrary.IcmpPacket()
# Get as 16 bit number - Limit based on ICMP header standards
randomIdentifier = os.getpid() & 0xFFFF
# Some PIDs are larger than 16 bit
packetIdentifier = randomIdentifier
packetSequenceNumber = ttl
icmpPacket.setTtl(ttl)
icmpPacket.buildPacket_echoRequest(
packetIdentifier, packetSequenceNumber
) # Build ICMP for IP payload
icmpPacket.setIcmpTarget(host)
# Build IP
icmpPacket.sendTraceRouteEchoRequest()
# if it has reached the final destination...
if icmpPacket.getIcmpCode() == 0 and icmpPacket.getIcmpType() == 8:
print(" [!] Destination reached!")
print()
break
if ttl == 255:
print(" [#] Destination not reachable in 255 stops")
break