Question: import socket import struct import time # Fill in start ICMP _ ECHO _ REQUEST = 8 # Echo request type ICMP _ ECHO _

import socket import struct import time # Fill in start ICMP_ECHO_REQUEST =8 # Echo request type ICMP_ECHO_REPLY =0 # Echo reply type # Fill in end def checksum(source_string): """ A helper function that calculates the checksum of the ICMP packet """ sum =0 countTo =(len(source_string)/2)*2 count =0 while count < countTo: thisVal = ord(source_string[count +1])*256+ ord(source_string[count]) sum = sum + thisVal sum = sum & 0xffffffff count = count +2 if countTo < len(source_string): sum = sum + ord(source_string[len(source_string)-1]) sum = sum & 0xffffffff sum =(sum >>16)+(sum & 0xffff) sum = sum +(sum >>16) answer = ~sum answer = answer & 0xffff answer = answer >>8|(answer <<8 & 0xff00) return answer def sendOnePing(mySocket, destAddr, ID): """ Send one ping to the destAddr. """ destAddr = socket.gethostbyname(destAddr) # Header is type (8), code (8), checksum (16), id (16), sequence (16) myChecksum =0 # Make a dummy header with a 0 checksum header = struct.pack("bbHHh", ICMP_ECHO_REQUEST, 0, myChecksum, ID,1) data = struct.pack("d", time.time()) # Calculate the checksum on the data and the dummy header myChecksum = checksum(header + data) # Get the right checksum, and put in the header if sys.platform == 'darwin': myChecksum = htons(myChecksum) & 0xffff else: myChecksum = htons(myChecksum) header = struct.pack("bbHHh", ICMP_ECHO_REQUEST, 0, myChecksum, ID,1) packet = header + data mySocket.sendto(packet,(destAddr,1)) def receiveOnePing(mySocket, ID, timeout, destAddr): """ Receive the ping from the socket. """ timeLeft = timeout while True: startedSelect = time.time() whatReady = select.select([mySocket],[],[], timeLeft) howLongInSelect =(time.time()- startedSelect) if whatReady[0]==[]: # Timeout return "Request timed out." timeReceived = time.time() recPacket, addr = mySocket.recvfrom(1024) icmpHeader = recPacket[20:28] type, code, checksum, packetID, sequence =
return answer def createPacket(id): # Header is type (8), code (8), checksum (16), id (16), sequence (16) header = struct.pack("bbHHh", ICMP_ECHO_REQUEST, 0,0, id,1) data = struct.pack("d", time.time()) # Calculate the checksum on the data and the header my_checksum = checksum(header + data) # Put the checksum back into the header header = struct.pack("bbHHh", ICMP_ECHO_REQUEST, 0, socket.htons(my_checksum), id,1) return header + data def sendPing(my_socket, dest_addr, id): dest_addr = gethostbyname(dest_addr) # Create the packet to send packet = createPacket(id) my_socket.sendto(packet,(dest_addr, 1)) def receivePing(my_socket, id, timeout): timeLeft = timeout while True: startedSelect = time.time() whatReady = select.select([my_socket],[],[], timeLeft) howLongInSelect =(time.time()- startedSelect) if whatReady[0]==[]: # Timeout return timeReceived = time.time() recPacket, addr = my_socket.recvfrom(1024) icmpHeader = recPacket[20:28] type, code, checksum, packetID, sequence = struct.unpack("bbHHh", icmpHeader) if packetID == id: bytesInDouble = struct.calcsize("d") timeSent = struct.unpack("d", recPacket[28:28+ bytesInDouble])[0] return timeReceived - timeSent timeLeft = timeLeft - howLongInSelect if timeLeft <=0: return def ping(dest_addr, timeout=1): my_socket = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP) my_id = os.getpid() & 0xFFFF sendPing(my_socket, dest_addr, my_id) delay = receivePing(my_socket, my_id, timeout) my_socket.close() return delay

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!