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 ICMPECHOREQUEST # Echo request type ICMPECHOREPLY # Echo reply type # Fill in end def checksumsourcestring: A helper function that calculates the checksum of the ICMP packet sum countTo lensourcestring count while count countTo: thisVal ordsourcestringcount ordsourcestringcount sum sum thisVal sum sum & xffffffff count count if countTo lensourcestring: sum sum ordsourcestringlensourcestring sum sum & xffffffff sum sum sum & xffff sum sum sum answer ~sum answer answer & xffff answer answer answer & xff return answer def sendOnePingmySocket destAddr, ID: Send one ping to the destAddr. destAddr socket.gethostbynamedestAddr # Header is type code checksum id sequence myChecksum # Make a dummy header with a checksum header struct.packbbHHh ICMPECHOREQUEST, myChecksum, ID data struct.packd time.time # Calculate the checksum on the data and the dummy header myChecksum checksumheader data # Get the right checksum, and put in the header if sysplatform 'darwin': myChecksum htonsmyChecksum & xffff else: myChecksum htonsmyChecksum header struct.packbbHHh ICMPECHOREQUEST, myChecksum, ID packet header data mySocket.sendtopacketdestAddr def receiveOnePingmySocket ID timeout, destAddr: Receive the ping from the socket. timeLeft timeout while True: startedSelect time.time whatReady select.selectmySocket timeLeft howLongInSelect timetime startedSelect if whatReady: # Timeout return "Request timed out." timeReceived time.time recPacket, addr mySocket.recvfrom icmpHeader recPacket: type, code, checksum, packetID, sequence
return answer def createPacketid: # Header is type code checksum id sequence header struct.packbbHHh ICMPECHOREQUEST, id data struct.packd time.time # Calculate the checksum on the data and the header mychecksum checksumheader data # Put the checksum back into the header header struct.packbbHHh ICMPECHOREQUEST, socket.htonsmychecksum id return header data def sendPingmysocket, destaddr, id: destaddr gethostbynamedestaddr # Create the packet to send packet createPacketid mysocket.sendtopacketdestaddr, def receivePingmysocket, id timeout: timeLeft timeout while True: startedSelect time.time whatReady select.selectmysocket timeLeft howLongInSelect timetime startedSelect if whatReady: # Timeout return timeReceived time.time recPacket, addr mysocket.recvfrom icmpHeader recPacket: type, code, checksum, packetID, sequence struct.unpackbbHHh icmpHeader if packetID id: bytesInDouble struct.calcsized timeSent struct.unpackd recPacket: bytesInDouble return timeReceived timeSent timeLeft timeLeft howLongInSelect if timeLeft : return def pingdestaddr, timeout: mysocket socketAFINET, SOCKRAW, IPPROTOICMP myid osgetpid & xFFFF sendPingmysocket, destaddr, myid delay receivePingmysocket, myid timeout mysocket.close return delay
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
