How to get the default interface and its inet address? - android

"Default interface" refers to ppp0:
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 0.0.0.0 0.0.0.0 U 0 0 0 ppp0
0.0.0.0 192.168.1.1 0.0.0.0 UG 100 0 0 wlp3s0
172.30.0.1 0.0.0.0 255.255.255.255 UH 0 0 0 ppp0
192.168.1.0 0.0.0.0 255.255.255.0 U 100 0 0 wlp3s0
Which one is used by default when I access WAN instead of LAN.
I need to run it on Android,so I can't use getifaddrs,because ifaddrs.h is NOT in the Android NDK.

Uhh, I don't know if an interface can be marked as "default", but there are plenty way to retrieve interface list without getifaddrs.
You can execute "ip link show" command and parse the result (or "ifconfig").
you can go to "/sys/class/net" where each directory reprensent an interface (and there is file under those directory that can help you).
I don't know if Android work like Debian, and I recommand you to find a way through Android NDK or system command (like "ip" or "ifconfig") because directory like /net or /proc are subject to change between different OS version (even if it's not really common, I can't recall to have see that).

Related

Bonjour local host names in Android

How can I make an Android network request lookup a local domain name registered with Bonjour?
I get this error when trying to fetch data using Volley.
java.net.UnknownHostException: Unable to resolve host "xxxxx.local": No address associated with hostname
If you control the whole network stack, you can perform DNS service discovery (DNS-SD) through Android's NsdManager. I recommend this guide from the documentation:
https://developer.android.com/training/connect-devices-wirelessly/nsd
In short, it boils down to:
mNsdManager.discoverServices(
SERVICE_TYPE, NsdManager.PROTOCOL_DNS_SD, mDiscoveryListener);
You can cross-reference the service you wish to resolve to by inspecting the response from dns-sd on Mac:
$ dns-sd -Z . .
_http._tcp PTR myserver._http._tcp
myserver._http._tcp SRV 0 0 80 myserver.local. ; Replace with unicast FQDN of target host
myserver._http._tcp TXT ""
In the NsdManager.DiscoveryListener callback, you can obtain the corresponding IP address from NsdServiceInfo.getHost().
Have you tried with the IP of localhost ? 127.0.0.1 or 10.0.2.2:80 ?

How can I know which ports are open on my android device and how to close them?

Is there an easy way, to see which ports are open on my Android device and a way to close the open ports?
Create a wifi hotspot on your phone
Connect your computer to the hotspot
use ipconfig or ifconfig to know the gateway IP adresse ( ie: your phone's IP adress )
Download nmap : http://nmap.org/
Use the nmap command : nmap -sS -Pn -p- your_phone_ip_adress
the open TCP ports will be shown as follow :
65531 closed ports PORT
STATE SERVICE
53/tcp open domain
8187/tcp open unknown
38647/tcp open unknown
42761/tcp open
unknown MAC Address: A4:9A:58:::** (Samsung Electronics Co.)
PS : For UDP ports use: nmap -sU -Pn -p- your_phone_ip_adress
You can determine the currently open ports by reading the textual /proc pseudo-files such as
/proc/net/tcp
/proc/net/udp
This is basically what a netstat command (where implemented) does - you may wish to find and examine the source of a simple netstat implementation (it should be possible to port such to java)
However, when running as an unprivileged app userid, you will only be able to close sockets belonging to your own process (or by roundabout means involving ptrace or process killing, other processes belonging to your userid). Also note that closing a socket does not necessarily make that port available for immediate re-use.
You can try different network commands through runtime and check the results
// netstat -lptu
// netstat -vat
Process su = Runtime.getRuntime().exec("netstat -vat ");

why the gateway to be bogon?

I connect my Android phone with my PC through USB. And open the usb tether option on the phone.
So, there is a new remote NDIS network adapter displayed in the PC's network connections, and there is a rnndis0 IFface on the android.
I do the following configurations:
PC: ip: 192.168.42.1 netmask: 255.255.255.0
Phone: ip 192.168.42.2 netmask: 255.255.255.0
And when ping each other, both work ok.
But when I configure the default gateway of my phone to be the address of the PC, it failed.
busybox route add default gw 192.168.42.1
busybox route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default bogon 0.0.0.0 UG 0 0 0 rndis0
192.168.42.0 * 255.255.255.0 U 0 0 0 rndis0
can anyone tell me the reason?
I believe it's marked as bogon as it is an address which should not appear in a routing table, ie a bogon route.
This is because it is a private network address. It's only a warning though.
see http://packetlife.net/blog/2009/jan/21/whats-bogon/
What is the output of
$ ip route
on Android?
SivlerZhao
Yesterday, I was unable to reproduce the problem.
Now, it reproduced.
busybox route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default bogon 0.0.0.0 UG 0 0 0 rndis0
192.168.137.0 * 255.255.255.0 U 0 0 0 rndis0
And the output of ip route is:
busybox ip route
default via 192.168.137.1 dev rndis0
192.168.137.0/24 dev rndis0 src 192.168.137.2
Actually, it works ok, but the "bogon" word apears

How to know the MTU size of Android Smartphone

Any command to know the MTU size of Android?
You should use the NetworkInterface class to query and obtain the network interfaces, then call getMTU().
Today, looking into the code of netcfg I saw that the configuration of the interfaces is located into /sys/class/net.. and then I thought of you! (I read your question yesterday)
If you have root access, open a terminal and run
cat /sys/class/net/<interface>/mtu
Methods to know the MTU size of Android:
from terminal: ifconfig $DEVICE | egrep addr\|MTU
through Android Debug Bridge (adb):
adb shell netcfg | grep UP to find the desired address and
adb shell ip addr show rmnet0 in case of rmnet0 or
adb shell cat /sys/class/net/rmnet0/mtu in case of rmnet0 (as described by #patedit)
Without ROOTING your phone, you may use a ping command from a Windows/Mac/Unix system. Though, the syntax of ping-options is very different for different OS.
For Windows
try this:
ping /l 1473 /f 10.68.34.75
/l <Size> — Specifies the length, in bytes, of the Data field in the echo Request messages sent. The default is 32.
/f — Specifies that echo Request messages are sent with the Do not Fragment flag in the IP header set to 1 (available on IPv4 only).
Adjust the payload using the -l command-line option. When you reach the higher limit, you will see this message and you will find the MTU size :
> The packet needs to be fragmented but DF set.
More details: https://kb.netgear.com/19863/Ping-Test-to-determine-Optimal-MTU-Size-on-Router
1480, I believe, but you can check by using ifconfig $DEVICE with a rooted device, and checking the MTU there.
For most network access, MTU could be resolved by MTU Discovery. You can use Ping command with different payload size and don't fragment to find aChrysler value. Good luck
Without ROOTING your phone, you may use a ping command from a Windows/Mac/Unix system. Though, the syntax of ping-options is very different for different OS.
From most Unix/Linux/Mac systems (Without ROOTING the phone)
You might share the internet connection from your phone, and then from any PC connected to your android-phone run ping commands:
ping www.yahoo.com -s 1413 -M do
man ping says:
-s <packetsize> — Specifies the number of data bytes to be sent. The default is 56, which translates into 64 ICMP data bytes when combined with the 8 bytes of ICMP header data.
-M <pmtudisc_opt> — Select Path MTU Discovery strategy. <pmtudisc_option> may be either do (prohibit fragmentation, even local one), want (do PMTU discovery, fragment locally when packet size is large), or dont (do not set DF flag).
Adjust the payload using the -s command-line option (for example: 1200, 1300, 1400, 1500, 1450, 1425, 1440, ...). When you reach the higher limit, you will see a message like this and you will find the MTU size :
> From 192.168.1.1 icmp_seq=1 Frag needed and DF set (mtu = 1500)
ping: local error: Message too long, mtu=1500
My answer is based on this one for windows: answer #25165641

Why does my service always bind to ipv6 localhost instead of ipv4?

I have a service that creates a ServerSocket and binds to localhost:7060. When I did "netstat -an" on my android device, I see it is using ipV6 localhost instead of ipv4 localhost interface.
The output is like this:
tcp6 0 0 ::ffff:127.0.0.1:7060 :::* LISTEN
The ports that use ipV4 are listed like this:
tcp 0 0 127.0.0.1:5060 0.0.0.0:* LISTEN
What is the trick to force it to use IPv4 always?
I am setting up a port forward rule using iptables. The version I have supports ipv4 destination addresses.
This is how I am creating my Java code for listening on the port.
InetAddress localAddress = Inet4Address.getByName("localhost");
//InetAddress localAddress = Inet4Address.getLocalHost();
sockServer = new ServerSocket(nPort, 20, localAddress);
I followed other advice like setting system property to prefer ipV4 in the startup of my service. That didn't make any difference.
System.setProperty("java.net.preferIPv4Stack", "true");
I am running this on Android 2.3 built for an embedded device.
Update:
I checked InetAddress.java sources in android tree. It is reading the above flag with a line like below.
static boolean preferIPv6Addresses() {
String propertyName = "java.net.preferIPv6Addresses";
String propertyValue = AccessController.doPrivileged(new PriviAction<String>(propertyName));
return Boolean.parseBoolean(propertyValue);
}
Now I am not sure System.setProperty() call is really changing the value read by above code.
In theory a IPv6 server listens to IPv4 as well, since IPv4 address space is a subset of IPv6, is this causing real problems to you?
A trick that might work is to use "127.0.0.1" instead of "localhost", which has IPv4 and IPv6 addresses associated.

Categories

Resources