Why my tracer is only working when i run the tracert?

40 Views Asked by At

I am currently studying Computer Networking using the Kurose and Ross book. While working on the traceroute code, I encountered no issues when testing it with both Wi-Fi and cable connections.

The problem started when I switched to my mobile network (4G - using my phone as mobile router) while being away from home. The script would get stuck after one or two loops. Then I observed that it would work fine if I ran the tracert command to the same address. Short video as explanation .

I'm using a Windows 11 and running everything as administrator, my Firewall was also disabled while executing the script. Is there something I'm missing? I'm having trouble to understand why this is happening.

My Tracer code below:


import sys
import socket
import folium
import geocoder


def tracerout(destination, port, ttl):
    s_sender = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s_receiver = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.getprotobyname('ICMP'))
    s_receiver.bind((socket.gethostbyname(socket.gethostname()), 0))
    s_receiver.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)
    s_receiver.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON)

    ip_location_list = [geocoder.ip('me')]

    destination_address = socket.gethostbyname(destination)
    print("\n")
    print("=" * 60)
    print("Tracing the path to %r - IP: %s" % (destination, destination_address))
    print("=" * 60)

    try:
        for ttl_counter in range(1, ttl):
            s_sender.setsockopt(socket.IPPROTO_IP, socket.IP_TTL, ttl_counter)
            s_sender.sendto(b"", (destination_address, port))

            data, address = s_receiver.recvfrom(1024)

            print(f"{ttl_counter} - received {len(data)} bytes from {address[0]}")
            if data[9] == 1:
                print('Protocol: ', data[9])
                print('ICMP Type', data[20])
                print('ICMP Code', data[21])
                ip_location_list.append(geocoder.ip(address[0]))

            if address[0] == destination_address:
                break
    finally:
        s_sender.close()
        s_receiver.close()

    return ip_location_list


def build_map(location_list, map_name):
    # Builds a map with the IP addresses obtained in the above function.
    pass


def main():
    destination_host = sys.argv[1] if sys.argv[1:] else 'www.example.com'  # gaia.cs.umass.edu
    destination_ttl = int(sys.argv[2]) if sys.argv[2:] else 64
    destination_port = int(sys.argv[3]) if sys.argv[3:] else 33434
    path_map_name = "tracerout_map"

    locations = tracerout(destination_host, destination_port, destination_ttl)
    build_map(locations, path_map_name)


if __name__ == "__main__":
    main()
0

There are 0 best solutions below