ICMP Ping/ICMPPing.py
def receive_one_ping(icmp_socket, port_id, timeout, send_time):
    """The socket waits for a reply and calculate latency.

    This function will measure and report different packet loss.

    Args:
        :param icmp_socket: the socket which is created from doOnePing function.
        :param port_id: current process id.
        :param timeout: configurable timeout, set using an optional argument.
        :param send_time: the time when ICMP packet is sent.

    Returns:
        :return: 1 (if Host unreachable error).
                 0 (if Network unreachable error).
                 byte size, latency and ttl (for successful reply).

    Raises:
        TimeoutError: An error occurred when a packet cannot be received within
                      a given time range.
    """
    while True:
        # 1. Wait for the socket to receive a reply.
        wait_for_data = select.select([icmp_socket], [], [], timeout)
        # 2. Once received, record the time.
        data_received = time.time()
        rec_packet, addr = icmp_socket.recvfrom(1024)
        ip_header = rec_packet[8: 12]
        icmp_header = rec_packet[20: 28]
        payload_size = struct.calcsize("!f")
        # 3. Unpack the packet header for useful information.
        type, code, checksum, id, sequence = struct.unpack("!bbHHh", icmp_header)
        # 4. Check that the ID matches between the request and reply.
        if type == 0 and id == port_id:  # type should be 0.
            ttl = struct.unpack("!b", ip_header[0:1])[0]
            delay_time = data_received - send_time
        # 5. Return byte size, latency and TTL.
            return payload_size * 8, delay_time, ttl
        elif type == 3 and code == 0:
            return 0  # Network unreachable error.
        elif type == 3 and code == 1:
            return 1
The socket waits for a reply and calculate latency.

    This function will measure and report different packet loss.

    Args:
        :param icmp_socket: the socket which is created from doOnePing function.
        :param port_id: current process id.
        :param timeout: configurable timeout, set using an optional argument.
        :param send_time: the time when ICMP packet is sent.

    Returns:
        :return: 1 (if Host unreachable error).
                 0 (if Network unreachable error).
                 byte size, latency and ttl (for successful reply).

    Raises:
        TimeoutError: An error occurred when a packet cannot be received within
                      a given time range.
    
The socket waits for a reply and calculate latency.
This function will measure and report different packet loss.
The socket waits for a reply and calculate latency . This function will measure and report different packet loss .
def receive_one_ping(icmp_socket, port_id, timeout, send_time):
    while True:
        wait_for_data = select.select([icmp_socket], [], [], timeout)
        data_received = time.time()
        rec_packet, addr = icmp_socket.recvfrom(1024)
        ip_header = rec_packet[8: 12]
        icmp_header = rec_packet[20: 28]
        payload_size = struct.calcsize("!f")
        type, code, checksum, id, sequence = struct.unpack("!bbHHh", icmp_header)
        if type == 0 and id == port_id:  
            ttl = struct.unpack("!b", ip_header[0:1])[0]
            delay_time = data_received - send_time
            return payload_size * 8, delay_time, ttl
        elif type == 3 and code == 0:
            return 0  
        elif type == 3 and code == 1:
            return 1
def receive_one_ping ( icmp_socket , port_id , timeout , send_time ) : while True : wait_for_data = select . select ( [ icmp_socket ] , [ ] , [ ] , timeout ) data_received = time . time ( ) rec_packet , addr = icmp_socket . recvfrom ( 1024 ) ip_header = rec_packet [ 8 : 12 ] icmp_header = rec_packet [ 20 : 28 ] payload_size = struct . calcsize ( "!f" ) type , code , checksum , id , sequence = struct . unpack ( "!bbHHh" , icmp_header ) if type == 0 and id == port_id : ttl = struct . unpack ( "!b" , ip_header [ 0 : 1 ] ) [ 0 ] delay_time = data_received - send_time return payload_size * 8 , delay_time , ttl elif type == 3 and code == 0 : return 0 elif type == 3 and code == 1 : return 1
The socket waits for a reply and calculate latency.
The socket waits for a reply and calculate latency .
"""The socket waits for a reply and calculate latency.

    This function will measure and report different packet loss.

    Args:
        :param icmp_socket: the socket which is created from doOnePing function.
        :param port_id: current process id.
        :param timeout: configurable timeout, set using an optional argument.
        :param send_time: the time when ICMP packet is sent.

    Returns:
        :return: 1 (if Host unreachable error).
                 0 (if Network unreachable error).
                 byte size, latency and ttl (for successful reply).

    Raises:
        TimeoutError: An error occurred when a packet cannot be received within
                      a given time range.
    """ # 1. Wait for the socket to receive a reply. # 2. Once received, record the time. # 3. Unpack the packet header for useful information. # 4. Check that the ID matches between the request and reply. # type should be 0. # 5. Return byte size, latency and TTL. # Network unreachable error.
:return: 1 (if Host unreachable error).
0 (if Network unreachable error).
byte size, latency and ttl (for successful reply).
: return : 1 ( if Host unreachable error ) . 0 ( if Network unreachable error ) . byte size latency and ttl ( for successful reply ) .
An error occurred when a packet cannot be received within
a given time range.
An error occurred when a packet cannot be received within a given time range .
TimeoutError
icmp_socket
port_id
timeout
send_time
param send_time: the time when ICMP packet is sent.
param send_time : the time when ICMP packet is sent .