How to know the MTU size of Android Smartphone - android

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

Related

Is there a way to programatically check for used ports without looping?

I'm familiar with couple of methods to check for used ports on Android.
Loop over all possible ports, try creating a socket on each port, if socket creation fails port is used.
Use netstat command from shell which will list all open connections and used ports can be parsed from there.
Use cat /proc/net/tcp command from shell which is similar as netstat command.
Problem with 1st option is that looping over all possible ports is taking too much time, it's not efficient enough if I want to make sure that I'm getting all open ports.
Problem with 2nd and 3rd option is that (on non-rooted device) although shell command can be executed in shell (adb shell) and output is clearly seen, while trying to execute command from Java code in Android application, command output is empty string for cat /proc/net/tcp and only header is outputted from netstat command. I'm guessing that the problem are application permissions which are insufficient to run above commands.
Is there any other way of checking for used ports or am I doing something wrong by using commands from option 2 or 3?
EDIT:
To clarify, while using any command that should list connection info from adb shell this will work fine. However, if I'm trying to invoke any of the commands (same syntax as in adb shell) from my application's Java code, output of the command is empty string.
To get the process I tried using:
Runtime.getRuntime().exec(command);
new ProcessBuilder().command("sh", "-c", command).start();
E.g. output of netstat command (with any arguments) is as following:
Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State Active UNIX domain sockets (only servers) Proto RefCnt Flags Type State I-Node Path
How about:
netstat -ln | awk '/^tcp/ { split($4,arr,":");prts[arr[2]]="" } END { for (i in prts) { print i } }'
Take the netstat output and then using awk, concentrate on all lines beginning with tcp. Then split the 4th delimited field into a array called arr based on ":" as the delimiter. Put the second index of the array (port number) in another array prts as the index. At the end, loop through the prts array and print the indexes (ports)

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 ");

How to use adb socket to get the result from the phone?

I have written an executable file, and push it into /system/bin.
After run the file, it will give a result in float.
Now on PC side, I want to get this result.
At first I write this float number into a file and use 'adb pull' to pull this file, then read file.
Because I need to do this operation frequently, may 2 times per sec. This cause bad performance of the phone.
Wheather it will be little influence when I use adb socket?
Where my executable file should output?
How adb socket get the result?
Thanks.
James.
If you're leaving the phone connected, you could probably just do
adb shell /system/bin/myexecutable
and just have your binary print its output to stdout. As long as your program runs quickly, twice per second shouldn't be too fast. Otherwise, you could do
adb shell cat /somewhere/myoutfile
to see what's in a file currently.
You could potentially use logcat as a medium for getting data from your Android app to your desktop machine provided there is an ADB connection available.
My thinking is that there are two pieces:
Log your app output with logcat to a unique TAG on the Android side. For example,
Log.d("MyAppOutput", "This is the output I am looking for");
On the desktop side, you could run a command line that looks specifically for that TAG, something like:
adb logcat -s MyAppOutput
I believe this would allow you to read the results from the Android app in near realtime. If you need to know the timestamp of the log message, you could add the -v time parameter to prefix each message with a timestamp.
I had the same question before, you don't need output result to file, just output your result to a socket port, and use adb forward to get the result on your pc by adb socket. this is what you need solution.
adb forward tcp:18000 tcp:19000
this command means,pc's tcp port 18000 bind to device's tcp port 19000, if you send data to 18000 port on pc, you can get data from 19000 on device.vice versa.

Is there a way to change android phone's mtu size?

My phone is having problem with sending data to the network. I think I have to change its MTU size to make it work.
To change MTU settings you need a to root your phone. Search in foruns how to do it.
After that your need to install a terminal emulator and check what is your network interface
Run the command:
netcfg
and look at the IP address what is your desired device (wlan0, tiwlan0, etc)
then use this command
ifconfig $DEVICE mtu $MTU_VALUE
and substitute $DEVICE with device name and the desired MTU value in $MTU_VALUE
You can also see the value at this file:
cat /sys/class/net/$DEVICE/mtu

How to change 3g dns setting on Android?

I want to change 3G dns setting on Android 2.1 device. I managed to install busybox on my device, i can also get dns information by using adb shell getprop | grep dns. The only problem is that it shows me net.pdp0.dns1 and net.pdp0.dns2, not net.rmnet0.dns1 and net.rmnet0.dns1 so i can't change the setting.
I know that net.rmnet0.dns1 is for 3G connection, so what about net.pdp0.dns1? How can i change to net.rmnet0.dns1?
Thanks
Android DSN file contains in following directory:
In android file system
system/etc/dhcpcd/dhcpcd-hooks/20-dns.conf
20-dns.conf file contains dns setting, you can modify this file by following way:
# Set net.<iface>.dnsN properties that contain the
# DNS server addresses given by the DHCP server.
set_dns_props()
{
case "${new_domain_name_servers}" in
"") return 0;;
esac
count=1
for i in 1 2 3 4; do
setprop dhcp.${interface}.dns${i} ""
done
count=1
for dnsaddr in ${new_domain_name_servers}; do
setprop dhcp.${interface}.dns${count} ${dnsaddr}
count=$(($count + 1))
done
setprop dhcp.eth0.dns1 8.8.8.8
setprop dhcp.eth0.dns2 8.8.8.4
}
unset_dns_props()
{
for i in 1 2 3 4; do
setprop dhcp.${interface}.dns${i} ""
done
}
case "${reason}" in
BOUND|INFORM|REBIND|REBOOT|RENEW|TIMEOUT) set_dns_props;;
EXPIRE|FAIL|IPV4LL|RELEASE|STOP) unset_dns_props;;
esac
(Note:Please take backup of origin file , if you need origin file)
set your dns in following line
setprop dhcp.eth0.dns1 8.8.8.8
setprop dhcp.eth0.dns2 8.8.8.4
It may help if you state the kind of device you have. From what I've read online, Samsung devices use the pdp0 interface names (it may be that this is the way Samsung refers to the 3G connections). Personally I have been using all HTC devices and the 3G interface is always rmnet0.
Is your phone rooted? You may not be able to set properties in the "net" category without root permissions.
If you are root, have you tried "setprop net.pdp0.dns1 "? Also, does "adb shell getprop | grep dns" give you "net.dns1"? I believe this is the default way Android looks up DNS servers. You may want to set this property as well.
You can test if it is working by running nslookup, it will show you the server it is querying.
Good luck,
B-Rad

Categories

Resources