├── LICENSE ├── README └── traceroute.php /LICENSE: -------------------------------------------------------------------------------- 1 | This is free and unencumbered software released into the public domain. 2 | 3 | Anyone is free to copy, modify, publish, use, compile, sell, or 4 | distribute this software, either in source code form or as a compiled 5 | binary, for any purpose, commercial or non-commercial, and by any 6 | means. 7 | 8 | In jurisdictions that recognize copyright laws, the author or authors 9 | of this software dedicate any and all copyright interest in the 10 | software to the public domain. We make this dedication for the benefit 11 | of the public at large and to the detriment of our heirs and 12 | successors. We intend this dedication to be an overt act of 13 | relinquishment in perpetuity of all present and future rights to this 14 | software under copyright law. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. 23 | 24 | For more information, please refer to 25 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | A simple traceroute example in PHP. 2 | 3 | Original by Joshua Thijssen (jaytaph) 4 | Edited by Igor Scheller (MyIgel) 5 | -------------------------------------------------------------------------------- /traceroute.php: -------------------------------------------------------------------------------- 1 | country_code.": ".$data->region_name.": ".$data->city; 65 | } else { 66 | // An error has accourred 67 | return "(No geo info found)"; 68 | } 69 | } 70 | 71 | // Get IP from URL 72 | $dest_addr = gethostbyname ($dest_url); 73 | print "Tracerouting to destination: $dest_addr\n"; 74 | 75 | $ttl = 1; 76 | while ($ttl < $maximum_hops) { 77 | // Create ICMP and UDP sockets 78 | $recv_socket = socket_create (AF_INET, SOCK_RAW, getprotobyname ('icmp')); 79 | $send_socket = socket_create (AF_INET, SOCK_DGRAM, getprotobyname ('udp')); 80 | 81 | // Set TTL to current lifetime 82 | socket_set_option ($send_socket, SOL_IP, IP_TTL, $ttl); 83 | 84 | // Bind receiving ICMP socket to default IP (no port needed since it's ICMP) 85 | socket_bind ($recv_socket, 0, 0); 86 | 87 | // Save the current time for roundtrip calculation 88 | $t1 = microtime (true); 89 | 90 | // Send a zero sized UDP packet towards the destination 91 | socket_sendto ($send_socket, "", 0, 0, $dest_addr, $port); 92 | 93 | // Wait for an event to occur on the socket or timeout after 5 seconds. This will take care of the 94 | // hanging when no data is received (packet is dropped silently for example) 95 | $r = array ($recv_socket); 96 | $w = $e = array (); 97 | socket_select ($r, $w, $e, 5, 0); 98 | 99 | // Nothing to read, which means a timeout has occurred. 100 | if (count ($r)) { 101 | // Receive data from socket (and fetch destination address from where this data was found) 102 | socket_recvfrom ($recv_socket, $buf, 512, 0, $recv_addr, $recv_port); 103 | 104 | // Calculate the roundtrip time 105 | $roundtrip_time = ( microtime(true) - $t1 ) * 1000; 106 | 107 | // No decent address found, display a * instead 108 | if (empty ($recv_addr)) { 109 | $recv_addr = "*"; 110 | $recv_name = "*"; 111 | } else { 112 | // Otherwise, fetch the hostname and geoinfo for the address found 113 | $recv_name = gethostbyaddr ($recv_addr); 114 | if ($argv[1] == "-g") { 115 | $recv_geo = ip2geo ($recv_addr); 116 | } 117 | } 118 | 119 | // Print statistics 120 | if ($argv[1] == "-g") { 121 | printf ("%3d %-15s %.3f ms %-30s %s\n", $ttl, $recv_addr, $roundtrip_time, $recv_geo, $recv_name); 122 | } else { 123 | printf ("%3d %-15s %.3f ms %s\n", $ttl, $recv_addr, $roundtrip_time, $recv_name); 124 | } 125 | } else { 126 | // A timeout has occurred, display a timeout 127 | printf ("%3d (timeout)\n", $ttl); 128 | } 129 | 130 | // Close sockets 131 | socket_close ($recv_socket); 132 | socket_close ($send_socket); 133 | 134 | // Increase TTL so we can fetch the next hop 135 | $ttl++; 136 | 137 | // When we have hit our destination, stop the traceroute 138 | if ($recv_addr == $dest_addr) break; 139 | } 140 | 141 | echo "\n ======================================================================\n\n"; 142 | 143 | ?> 144 | --------------------------------------------------------------------------------