2

I am extremely new the Android app development and Stack Overflow. I am trying to recreate traceroute in an Android app since Android devices do not come with traceroute by default. I've encountered a couple stack overflow posts talking about solutions to this, but I have still run into challenges.

Traceroute on android - the top post on this thread links an Android Studio project that implements traceroute using ping. If I understand the algorithm correctly, it continually pings the destination IP, incrementing the time-to-live field to obtain information about intermediary routers. I've tried to recreate this behavior, but for certain values of TTL, the ping stalls and doesn't retrieve any router information. I'm not really sure why this happens. Here's a quick demo function I spun up... at some point in the loop the pings stall.

    public static void smallTracerouteDemoShowingThatTheProgramStallsAtCertainTTLs() {
        try {
            String host = "google.com";
            int maxTTL = 20;

            for (int i = 1; i < maxTTL; i++) {
                // Create a process that executes the ping command
                Process p = Runtime.getRuntime().exec("ping -c 1 -t " + i + " " + host);

                // Get a buffered reader with the information returned by the ping
                BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));

                // Convert the BufferedReader to a string
                String dataReturnedByPing = "";
                for (String line; (line = br.readLine()) != null; dataReturnedByPing += "\n"+line);

                // Print out information about each TTL
                System.out.println("TTL = " + i + " out of " + maxTTL);
                System.out.println(dataReturnedByPing);
                System.out.println("========================================");


            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

how to run traceroute command through your application? - The solution on this thread suggests using BusyBox. I've not used BusyBox as yet, but it seems like I would have to embed BusyBox into my app to get things to work. After doing some research it looks like BusyBox provides numerous Linux commands through one executable. I'm a bit hesitant to explore this option because I really only need the traceroute command. In addition, I know that Android targets a few different CPU architectures, and I'm not sure if one executable will support them all.

I've also run into a github repository that takes another approach to running traceroute:

https://github.com/wangjing53406/traceroute-for-android - In this repository the author embeds the traceroute source code into the project and uses the NDK to build the source code along with the rest of his app. I really like this approach because it feels the most "correct." It uses a built traceroute instead of a Java-based implementation, so you can't find yourself in a situation where the Java implementation gives you one thing and the actual traceroute gives you another. When I open this project to experiment with it, my build fails. The top line says: org.gradle.initialization.ReportedException: org.gradle.internal.exceptions.LocationAwareException: A problem occurred configuring root project 'traceroute-for-android-master'.

Any help on why this happens or ways to troubleshoot it would be fantastic.

For reference, the minimum SDK I am targeting is API 21 and I am running on Android Studio 3.3.0.

So, at this point I'm stumped. If you were trying to make an app that would let you execute traceroute commands, how would you do it? I really like the NDK approach because it guarantees you're getting true traceroute behavior. If you have any guides to getting that set up for my Android version/SDK, I would appreciate if you would post them. If you'd take another approach I'd to hear about it as well.

Thank you in advance.

0 Answers0