Implement Traceroute functionality in android

余生长醉 提交于 2019-12-05 11:12:16

The busybox utility includes traceroute. You can run busybox on your device without rooting your phone by following this youtube tutorial. You should then be able to use the first code segment you posted to query traceroute from within your app. Of course, you will need to make sure that you use the correct path when calling traceroute.

Traceroute works by sending 3 ICMP messages via UDP to an unlikely port number, starts a timer each time, and setting the TTL in the datagram to 1 (max 1 hop). When it reaches the first router, the datagram expires and the router will refuse to send the packet any farther and respond with a expired message (ICMP type 11 code 0) that includes the routers name, IP address, MTU, and a few other bits of data. The source will obtain the round trip time from the timer. Once traceroute gets the data from the first hop, it will repeat these steps but increment the TTL by one, and keep repeating these steps until it finally gets to the host itself.

In order for your code to work, you need to be able to manipulate the network layer datagram and set the TTL value in the header. After that it's just a matter of parsing out the responses from each router that returns a TTL expired message.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!