Recreating Linux traceroute for an Android app - java

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.

Related

Get Wi-Fi name and interface in JAVA [duplicate]

We're doing a project coded in Java (compiled for JRE 1.6) and need some help with
a little but apparently complicated feature:
We want to do a certain action when a specific wireless network is connected e.g. when the connected SSID=="myNetworkAtHome" or similar.
After looking through this site, google and the Java documentation we have come a little closer.
After looking at the code here:
http://download.oracle.com/javase/tutorial/networking/nifs/retrieving.html
It seems we were getting close but it hits a deadend, all the interfaces seems to be connected to "net0" through "net13" (on my laptop that is.)
And we're unable to get the SSID out of any interface at all. I do realise the code in the example is only giving the interface names and not connected networks, but it doesn't seem to offer a way of fetching the connected network information.
Any help on this would be extremely helpfull!
You can't access this low-level details of the network in Java. You can get some details of the network interface with the NetworkInterface class but if you see at the provided methods, no one is related to Wifi networks nor any way to get the SSID is provided. As pointed below, you should use some native functionality through calling a native library with JNI or by calling a OS tool with Runtime.
Java is not designed to do that kind of things, is hard to implement in a platform-independent way and any hardware-level detail can not be managed in Java by principle.
Same applies to other networks like 3G, GPRS... the application should not be aware of the connection type nor its details. Java can only manage things at the Transport (TCP) level, not the network (IP) not Link (3G, Wifi, Ethernet...), so you can only manage sockets.
ArrayList<String>ssids=new ArrayList<String>();
ArrayList<String>signals=new ArrayList<String>();
ProcessBuilder builder = new ProcessBuilder(
"cmd.exe", "/c", "netsh wlan show all");
builder.redirectErrorStream(true);
Process p = builder.start();
BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while (r.read()!=-1) {
line = r.readLine();
if (line.contains("SSID")||line.contains("Signal")){
if(!line.contains("BSSID"))
if(line.contains("SSID")&&!line.contains("name")&&!line.contains("SSIDs"))
{
line=line.substring(8);
ssids.add(line);
}
if(line.contains("Signal"))
{
line=line.substring(30);
signals.add(line);
}
if(signals.size()==7)
{
break;
}
}
}
for (int i=0;i<ssids.size();i++)
{
System.out.println("SSID name == "+ssids.get(i)+" and its signal == "+signals.get(i) );
}
You'll have to resort to a JNI solution. There's something available at http://sourceforge.net/projects/jwlanscan, but that only works for Windows systems. Or you could do it the ugly way and use Runtime.getRuntime().exec(...) and use the command line tools available for your OS (*nix = iwconfig) and resort to parsing.

Saving btmon output

This is my first post here, although I've been reading stuff here for quite some time :)
I'm currently doing my bachelor thesis in computer science where we're doing an Indoors-positioning thing with iBeacons and Bluetooth. Where the device "being found" is currently a Raspberry Pi due to complications with smartphones.
I've got a bit of a problem with the btmon command on my Raspberry Pi. The full structure of our program is as follows:
Scan for bluetooth devices (iBeacons)
Save the output result <---- Here's the problem
Filter the desired data
Send to server node
As this is a part of a program we're building, the commands are executed through Java code that executes the terminal command, and returns the output as a String. The method looks like this:
private String executeCommand(String command) {
StringBuffer output = new StringBuffer();
Process p;
try {
p = Runtime.getRuntime().exec(command);
p.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
while ((line = reader.readLine()) != null) {
output.append(line + "\r\n");
}
} catch (Exception e) {
e.printStackTrace();
}
return output.toString();
}
I've got everything working when using another command (such as ifconfig) so the method works for ordinary commands, but I can't get it to work with btmon.
The full command I'm trying to run is this:
sudo btmon & sudo hcitool lescan
It seems to me as if btmon is running as a different process/runtime which would explain why the above Java code doesn't catch its output. I've been trying to "get" the btmon runtime but have failed to do so, and I've been trying to use ProcessBuilder with inheritIO() to change it's output source but couldn't get that to work either...
I've been googling this for a couple of days by now, and I've bumped into a couple of Python scripts that I can't get to work either (probably because I've never used Python before ^^).
So, in short: How do I save the output of sudo btmon & sudo hcitool lescan? Preferably to a String through Java code, but saving it as a file would also work!
Thanks in advance! :)
I got the same problem and spent many days to solve this issue. The key point was Thread and not using &. Here is what I discovered:
& literally runs in the background. Change sudo btmon & to sudo btmon
Use Thread.
Thread 1 - start btmon (You can get result this Thread)
Thread 2 - start hcitool lescan
(btmon should first - because "lescan" occur hci events so btmon can get them) Summary - do not use & and use independent thread.

how windows will do auto java update checks?

First of all sorry for my poor English.
I want to know how the windows will do auto java update check behind the User Interface ?
The UI will just react based on our input which is in the link , http://java.com/en/download/help/java_update.xml#howto .
But , how windows checks the updates programmatically.
I wrote a small program in java ,
public class JavaLatestVersion {
public static void main(String[] args){
try {
BufferedReader br = new BufferedReader(new InputStreamReader(new URL(
"http://java.com/applet/JreCurrentVersion2.txt").openStream())) ;
String fullVersion = br.readLine();
System.out.println("fullVersion : "+fullVersion);
String version = fullVersion.split("_")[0];
String revision = fullVersion.split("_")[1];
System.out.println("Version " + version + " revision " + revision);
} catch (IOException e) {
e.printStackTrace();
}
}
}
My questions :
1. Is the above program is the reliable way to get the latest java version ? Or any other standard way to get the latest java version (Not in the computer) ?
2. Is windows use the same way to determine the latest java version ?
3. Is windows use this link for updates http://java.com/applet/JreCurrentVersion2.txt ?
Any one know the secret code behind how windows will check for latest java updates?
Thanks in advance.
Checking for Java updates is done by Java Auto Updater. It is ordinary application (which is ran when Windows starts up).
Yes, it is reliable way to get the latest version of Java (as updater can not only update Java, it can also update itself). But pay attention to firewall/group policy settings which can prohibit updater to access the Web.
Windows doesn't update Java.
Only debugging Java Auto Updater can help to determine what URL it uses.
Unfortunately, Java Auto Updater has only graphical interface and hides all work behind the scenes. So finding a "secret code" is not easy to do. All the more in many cases reverse-engineering non-open source software is illegal from a license point of view.
URL that you provided above doesn't works. Because it says 8.0_51. But latest version of Java on Downloads page is 8u65 / 8u66.
Seems that latest available version (as plain text) can be determined only by fetching http://www.oracle.com/technetwork/java/javase/downloads/index.html web page, then parsing it, handling cases when page is moved to another location, etc.

Root commands library

Right now I'm doing something like below to execute commands as root. But the whole thing seemed a bit manual so I googled a bit and found roottools. Is that the recommended way to go or is there something better?
I want to be able to wait for commands to finish, read output and ret val's.
public static void deleteSystemApp(Context context, String app)
{
final String MOUNT_RW = "mount -o remount,rw -t rfs /dev/stl5 /system; \n";
final String MOUNT_RO = "mount -o remount,ro -t rfs /dev/stl5 /system; \n";
final String RM_APP = "rm -rf " + app + "; \n";
Process process;
try
{
process = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(process.getOutputStream());
os.writeBytes(MOUNT_RW);
Toast.makeText(context, RM_APP, Toast.LENGTH_SHORT).show();
//os.writeBytes(RM_APP);
os.writeBytes(MOUNT_RO);
}
catch(IOException e)
{
e.printStackTrace();
}
}
Update regarding the issue pointed out by Chris Stratton:
I was already urlencoding my app argument to ensure that nobody could pass something like "something; rm -rf /" or "something && rm -rf /" and wipe the device. But upon further consideration I completely dropped the idea about sanitizing arguments. And deleted that part of my code. Why?
Why would anyone want to do this to their own device?
If someone else is able to pass malicious arguments the device is already compromised.
Original description:
RootTools provides rooted developers a standardized set of tools for
use in the development of rooted applications. In the end, we will
accomplish this by providing developers with robust, easy-to-use
libraries that will drastically improve development times as well as
promote code reuse. This project is open to any proven developer that
feels they have something to contribute. By pitching in together we
can streamline our own processes, improve the effectiveness of our
apps, learn new techniques, and provide a better experience for our
users.
As you might understand RootTools provides rooted developers a standardized set of tools for use in the development of rooted applications. If you need non-standard tools right your own or modify the source code of RootTools. Usually this library will cover all your needs. I used it in few projects and was very happy about it.
Good luck.

Java & Windows 7: Reliably getting IPv4 netmask?

I've run into a known bug with Java 6 on Windows. My understanding is that the normal way to get the netmask is to look up the network prefix length and do some bit shifts. The problem is that on Windows the prefix length is often returned incorrectly, so we get a 128 when we should get a 24 or 20.
In this solution, it is suggested to put -Djava.net.preferIPv4Stack=true on the Java command line. Unfortunately, on Windows 7, adding that as either a VM parameter or on the Java command line seems to have no effect.
(a) Does anyone know any OTHER work-arounds for this problem that might still work on Windows 7?
(b) Alternatively, is there an entirely different way to get the netmask that is reliable?
Thanks!
P.S. Here is the bug report that pertains to this.
The -Djava.net.preferIPv4Stack=true VM option should work under any OS. Alternatively, it can be put into Java code as System.setProperty("java.net.preferIPv4Stack","true");. Unless, something (library or whatever) is resetting its true state.
The code below displays the subnet mask. On a computer with more than one network connection (like a laptop with a wireless and Cat-5 Ethernet connection) it may write the subnet mask twice because there can be two different IP addresses for the client.
String os = System.getProperty("os.name");
try {
if(os.indexOf("Windows 7")>=0) {
Process process = Runtime.getRuntime().exec("ipconfig");
process.waitFor();
InputStream commandOut= process.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(commandOut));
String line;
while((line = in.readLine()) !=null) {
if(line.indexOf("Subnet Mask")>=0) {
int colon = line.indexOf(":");
System.out.println(line.substring(colon+2));
}
}
}
catch(IOException ioe) { }
catch(java.lang.InterruptedException utoh) { }
On my laptop with both a wired and wireless connection active, I get this output:
255.255.254.0
255.255.254.0
When I turn off my wireless connection, I only see one line of output for the wired Ethernet link, as expected.
Since the problem us just in Windows 7, why not look for an OS specific solution?
I know we can launch windows programs from Java, including the windows command line or bat files. There must be a way to re-direct the output of ipconfig to a text file in windows. Your program should be able to get the subnet mask by calling ipconfig and then reading the output.

Categories