Traceroute/Traceroute.py
def receive_one_trace(icmp_socket, send_time, timeout):
    """The socket waits for a reply and calculate latency for each node.

        This function will measure and report different packet loss.

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

        Returns:
        :return: the IP address of the current node.

        Raises:
            TimeoutError: An error occurred when a packet cannot be received within
                          a given time range.
        """
    print_str = ""
    retr_addr = ""
    try:
        # 1. Wait for the socket to receive a reply.
        start_time = time.time()
        wait_for_data = select.select([icmp_socket], [], [], timeout)
        end_time = time.time()
        if end_time == start_time:
            #wait 1 ms
            time.sleep(0.001)
        # 2. Once received, record the time.
        data_received = time.time()
        rec_packet, retr_addr = icmp_socket.recvfrom(1024)
        icmp_header = rec_packet[20: 28]
        # 3. Unpack the packet header for useful information.
        type, code, checksum, id, sequence = struct.unpack("!bbHHh", icmp_header)
        # 4. Check whether packet loss or not by its type and code.
        if type == 0 or type == 11:
            # When TTL reaches 0 but it haven't reach the destination.
            delay_time = int((data_received - send_time) * 1000)
            print_str = str(delay_time) + " ms"
        elif type == 3 and code == 0:
            # Network unreachable error.
            print_str = "Network unreachable"
        elif type == 3 and code == 1:
            # Host unreachable error.
            print_str = "Host unreachable"
    except TimeoutError :
        print_str = "*   "  # timeout.
    finally:
        sys.stdout.write("{0: >18}".format(print_str))
    # return IP address of the current node.
    # If request timeout, just return the string.
    try:
        retre_ip = retr_addr[0]
    except IndexError:
        retre_ip = "Request timeout"
    finally:
        return retre_ip
The socket waits for a reply and calculate latency for each node.

        This function will measure and report different packet loss.

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

        Returns:
        :return: the IP address of the current node.

        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 for each node.
This function will measure and report different packet loss.
The socket waits for a reply and calculate latency for each node . This function will measure and report different packet loss .
def receive_one_trace(icmp_socket, send_time, timeout):
    print_str = ""
    retr_addr = ""
    try:
        start_time = time.time()
        wait_for_data = select.select([icmp_socket], [], [], timeout)
        end_time = time.time()
        if end_time == start_time:
            time.sleep(0.001)
        data_received = time.time()
        rec_packet, retr_addr = icmp_socket.recvfrom(1024)
        icmp_header = rec_packet[20: 28]
        type, code, checksum, id, sequence = struct.unpack("!bbHHh", icmp_header)
        if type == 0 or type == 11:
            delay_time = int((data_received - send_time) * 1000)
            print_str = str(delay_time) + " ms"
        elif type == 3 and code == 0:
            print_str = "Network unreachable"
        elif type == 3 and code == 1:
            print_str = "Host unreachable"
    except TimeoutError :
        print_str = "*   "  
    finally:
        sys.stdout.write("{0: >18}".format(print_str))
    try:
        retre_ip = retr_addr[0]
    except IndexError:
        retre_ip = "Request timeout"
    finally:
        return retre_ip
def receive_one_trace ( icmp_socket , send_time , timeout ) : print_str = "" retr_addr = "" try : start_time = time . time ( ) wait_for_data = select . select ( [ icmp_socket ] , [ ] , [ ] , timeout ) end_time = time . time ( ) if end_time == start_time : time . sleep ( 0.001 ) data_received = time . time ( ) rec_packet , retr_addr = icmp_socket . recvfrom ( 1024 ) icmp_header = rec_packet [ 20 : 28 ] type , code , checksum , id , sequence = struct . unpack ( "!bbHHh" , icmp_header ) if type == 0 or type == 11 : delay_time = int ( ( data_received - send_time ) * 1000 ) print_str = str ( delay_time ) + " ms" elif type == 3 and code == 0 : print_str = "Network unreachable" elif type == 3 and code == 1 : print_str = "Host unreachable" except TimeoutError : print_str = "*   " finally : sys . stdout . write ( "{0: >18}" . format ( print_str ) ) try : retre_ip = retr_addr [ 0 ] except IndexError : retre_ip = "Request timeout" finally : return retre_ip
The socket waits for a reply and calculate latency for each node.
The socket waits for a reply and calculate latency for each node .
"""The socket waits for a reply and calculate latency for each node.

        This function will measure and report different packet loss.

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

        Returns:
        :return: the IP address of the current node.

        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. #wait 1 ms # 2. Once received, record the time. # 3. Unpack the packet header for useful information. # 4. Check whether packet loss or not by its type and code. # When TTL reaches 0 but it haven't reach the destination. # Network unreachable error. # Host unreachable error. # timeout. # return IP address of the current node. # If request timeout, just return the string.
None
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
send_time
timeout
param send_time: the time when ICMP packet is sent.
param send_time : the time when ICMP packet is sent .