connect to ip:port usb-Ethernet adapter on android - android

I have an HTC One and i root it. I connect usb-Ethernet adapter with OTG cable to my phone and set ip address on it (eth0 192.168.9.1/24).
Then i connect it to my laptop with ethernet cable and set ip address from that subnet on my laptop(192.168.9.2/24). I can ping from both side(phone --> laptop and laptop --> phone)
then i write an android program to listen to port (33333):
private class SocketServerThread extends Thread {
static final int SocketServerPORT = 33333;
int count = 0;
#Override
public void run() {
try{
serverSocket = new ServerSocket(SocketServerPORT,50,deviceAddress);
socket = serverSocket.accept();
}
}
}
private String getIpAddress() {
String ip = "";
try {
Enumeration<NetworkInterface> enumNetworkInterfaces = NetworkInterface.getNetworkInterfaces();
while (enumNetworkInterfaces.hasMoreElements()) {
NetworkInterface networkInterface = enumNetworkInterfaces.nextElement();
Enumeration<InetAddress> enumInetAddress = networkInterface.getInetAddresses();
while (enumInetAddress.hasMoreElements()) {
InetAddress inetAddress = enumInetAddress.nextElement();
if (inetAddress.isSiteLocalAddress()) {
ip += "SiteLocalAddress: "+ inetAddress.getHostAddress() + "\n";
try {
deviceAddress=inetAddress.getByName(inetAddress.getHostAddress());
// deviceAddrgroup=inetAddress.getAllByName(inetAddress.getHostAddress());
}catch (UnknownHostException e){
e.printStackTrace();
}
}
}
}
When i want to connect to that port with (telnet 192.168.9.1 33333) from my laptop to my phone, it is not possible and telnet wait until timeout.
I start troubleshooting :
1 - use telnet 192.168.9.1 33333 on Terminal Emulator on my phone and i can connect to that port.
2 - i connect my phone to laptop with Wifi and i can connect to port 33333 without problem.
3 - i want to capture packet on android eth0 with Tcpdump. I copy Tcpdump on system/bin and system/xbin. When i want to execute tcpdump on Terminal Emulator, i have permission dined error! (my phone is root, and i use "su" before run that command)
4 - i use netstat on Terminal Emulator it show protocol tcp6 (::ffff:192.168.9.1)
!!!!
my question :
1 - any body had this problem before that can not connect to port on Ethernet-usb adapter?
2 - because protocol on android is tcp6, is it possible that cause the problem?
3 - any body known what is the problem of tcpdump on phone with root permission?

Related

Send UDP from Android Emulator to Host PC

I'm trying to send UDP packets from an emulated device (Nexus S 4.0", 480 x 800: hdpi) to my host PC for development and testing. The sending side seems correct and doesn't encounter any errors, but Wireshark indicates they are not arriving at the host PC. I've researched this problem and all the fixes that worked for others are not working for me:
I added "uses-permission android:name="android.permission.INTERNET" to the maifest XML file. (I also have ACCESS_NETWORK_STATE but I don't think that's necessary for this.)
I am sending the packets to the host loopback address 10.0.2.2. The port is 5006, so it's not one that I should need special privileges for.
I am calling DatagramSocket.send() in a dedicated thread, not in the main thread. (I think this would throw NetworkOnMainThreadException anyway, and I'm not getting any exceptions.)
I have Telnet-ed into "localhost 5444" and issued the "redir add udp:5006:5006" command to setup UDP port forwarding on the emulator's virtual router. The command returns "OK" without error, and "redir list" returns "udp:5006 => 5006".
I've also setup UDP port forwarding (port 5006) on my host PC's router (between PC and open internet). But I don't think that should be necessary, this router is not between the emulator and the host PC.
I have disabled Windows firewall and anti-virus on the host PC.
Here is the relevant code in my MainActivity.java. The start() and stop() methods are called from button clicks (omitted because they are not part of the problem):
private static String TAG = "MainActivity";
private volatile boolean running = false;
private String ip = "10.0.2.2";
private int port = 5006;
public void start(View view) {
new Thread() {
public void run() {
byte[] bytes = "Hi from UDPSender!".getBytes();
try {
InetAddress inetAddr = InetAddress.getByName(ip);
running = true;
while (running == true) {
DatagramPacket packet = new DatagramPacket(bytes, bytes.length, inetAddr, port);
DatagramSocket socket = new DatagramSocket();
socket.setBroadcast(false);
socket.send(packet);
socket.close();
Log.d(TAG, "Send packet to "+packet.getAddress().getHostAddress()+":"+packet.getPort());
Thread.sleep(1000);
}
} catch (Exception e) {
Log.e(TAG, e.getMessage(), e);
}
}
}.start();
}
public void stop(View view) {
running = false;
}

Can I get the IP address of the access point to I am connected to over wifi?

Hi I am new to android programming. I am basically trying to connect to an access point and send it come commands. After connecting to it over wifi, is it possible to programatically obtain it's IP address so that I can establish a http connection with it?
So far I know that we can obtain the device IP, but not sure if the access point IP can be obtained. Please help. Thanks in advance.
public static String getApIpAddr(Context context) {
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
DhcpInfo dhcpInfo = wifiManager.getDhcpInfo();
byte[] ipAddress = convert2Bytes(dhcpInfo.serverAddress);
try {
String apIpAddr = InetAddress.getByAddress(ipAddress).getHostAddress();
return apIpAddr;
} catch (UnknownHostException e) {
e.printStackTrace();
}
return null;
}
private static byte[] convert2Bytes(int hostAddress) {
byte[] addressBytes = { (byte)(0xff & hostAddress),
(byte)(0xff & (hostAddress >> 8)),
(byte)(0xff & (hostAddress >> 16)),
(byte)(0xff & (hostAddress >> 24)) };
return addressBytes;
}
I assume you mean the outside (public) IP address of the access point that the device is connected to. If so, yes there is a simple way to get the public IP address of the access point that a device is connected to. Simply setup a script on a web server that will echo back the IP address of any client that connects to it (similar to www.whatismyip.com). Then, your device just needs to do a GET request to the script, and this will return the outside IP of the access point that the device is connected to.
I am using this to get the IP address
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();)
{
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses();enumIpAddr.hasMoreElements();)
{
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress())
{
//My IP address
String Ip= inetAddress.getHostAddress().toString();
}
}
}
}
catch (SocketException e)
{
Log.e("Error occurred ", e.toString());
}

Android get IP-Address of a hotspot providing device

I'm currently using
public static String getLocalIPAddress(WifiManager wm){
return Formatter.formatIpAddress(wm.getConnectionInfo().getIpAddress());
}
to get the IP-Address of the executing devices. That works fine if the device is connected to a "common" wlan-network as well as the device is connected to a wifi network which is hosted by an other android device via hotspot. If the device is not connected to any wifi network "0.0.0.0" is returned (correct). But if the device is hosting a wifi network by providing a hotspot the methode is still returning "0.0.0.0". How can I get the real IP-Address of a hotspot providing device "in its own wifi-network"?
thx & regards
You're almost right, the default IP address of hotspot is 192.168.43.1 (If device maker didn't change.)
You can check the source code of Android framework (AOSP).
/frameworks/base/services/java/com/android/server/connectivity/Tethering.java
/frameworks/base/wifi/java/android/net/wifi/WifiStateMachine.java
In the Tethering.java,
private static final String USB_NEAR_IFACE_ADDR = "192.168.42.129";
private static final int USB_PREFIX_LENGTH = 24;
// USB is 192.168.42.1 and 255.255.255.0
// Wifi is 192.168.43.1 and 255.255.255.0
// BT is limited to max default of 5 connections. 192.168.44.1 to 192.168.48.1
// with 255.255.255.0
private String[] mDhcpRange;
private static final String[] DHCP_DEFAULT_RANGE = {
"192.168.42.2", "192.168.42.254", "192.168.43.2", "192.168.43.254",
"192.168.44.2", "192.168.44.254", "192.168.45.2", "192.168.45.254",
"192.168.46.2", "192.168.46.254", "192.168.47.2", "192.168.47.254",
"192.168.48.2", "192.168.48.254",
};
Also, in the WifiStateMachine.java
private boolean startTethering(ArrayList<String> available) {
boolean wifiAvailable = false;
checkAndSetConnectivityInstance();
String[] wifiRegexs = mCm.getTetherableWifiRegexs();
for (String intf : available) {
for (String regex : wifiRegexs) {
if (intf.matches(regex)) {
InterfaceConfiguration ifcg = null;
try {
ifcg = mNwService.getInterfaceConfig(intf);
if (ifcg != null) {
/* IP/netmask: 192.168.43.1/255.255.255.0 */
ifcg.setLinkAddress(new LinkAddress(
NetworkUtils.numericToInetAddress("192.168.43.1"), 24));
ifcg.setInterfaceUp();
mNwService.setInterfaceConfig(intf, ifcg);
}
} catch (Exception e) {
loge("Error configuring interface " + intf + ", :" + e);
return false;
}
if(mCm.tether(intf) != ConnectivityManager.TETHER_ERROR_NO_ERROR) {
loge("Error tethering on " + intf);
return false;
}
mTetherInterfaceName = intf;
return true;
}
}
}
// We found no interfaces to tether
return false;
}
Therefore, the default value is 192.168.43.1 .
I tested a small couple of different devices and it seems that the hotspot providing device has always the IP 192.168.43.1 on its network. Can somebody please check/confirm this assumption?
Though this is an old question, but this might help someone. This will return the ip address of your device, as long as you've turned on the hotspot.
private String getIpAddress() {
String ip = "";
try {
Enumeration<NetworkInterface> enumNetworkInterfaces = NetworkInterface
.getNetworkInterfaces();
while (enumNetworkInterfaces.hasMoreElements()) {
NetworkInterface networkInterface = enumNetworkInterfaces
.nextElement();
Enumeration<InetAddress> enumInetAddress = networkInterface
.getInetAddresses();
while (enumInetAddress.hasMoreElements()) {
InetAddress inetAddress = enumInetAddress.nextElement();
if (inetAddress.isSiteLocalAddress()) {
ip += "SiteLocalAddress: "
+ inetAddress.getHostAddress() + "\n";
}
}
}
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
ip += "Something Wrong! " + e.toString() + "\n";
}
return ip;
}
The Hotspot likely acts as a DHCP server. so,
To get IP address (server) of wifi hotspot after getting connected to it call method from remote (client)
intToInetAddress(wifiManager.getDhcpInfo().serverAddress);// get hotspot ip
then
public InetAddress intToInetAddress(int hostAddress)
{
byte[] addressBytes = {(byte) (0xff & hostAddress),
(byte) (0xff & (hostAddress >> 8)),
(byte) (0xff & (hostAddress >> 16)),
(byte) (0xff & (hostAddress >> 24))};
try
{
return InetAddress.getByAddress(addressBytes);
}
catch (UnknownHostException e)
{
throw new AssertionError();
}
}
will return ip address of connected hotspot, and yes most default IP address of hotspot is 192.168.43.1
open termux and run
ip -4 route get 8.8.8.8 | grep via
You'll something like this:
8.8.8.8 via 192.168.43.248 dev wlan0 table 1030 src 192.168.43.20 uid 12345
I was also checked several number of devices all the devices have same ip that is 192.168.43.1
you can try this address
but in android pie it becomes 192.168.43.68

get the internet ip address

I need to get the internet ip address. For now i want to get it using the linux terminal, so i type "ifconfig". I'm connected by my android phone via thetering, and i've noticed there's not my internet ip address in the output of "ifconfig".
usb0 Link encap:Ethernet HWaddr 6a:22:38:4d:92:36
indirizzo inet:192.168.42.79 Bcast:192.168.42.255 Maschera:255.255.255.0
indirizzo inet6: fe80::6822:38ff:fe4d:9236/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:31359 errors:4 dropped:0 overruns:0 frame:4
TX packets:27688 errors:0 dropped:0 overruns:0 carrier:0
collisioni:0 txqueuelen:1000
Byte RX:30033107 (30.0 MB) Byte TX:4855114 (4.8 MB)
This is the output of the command "ifconfig".
Is there a universal way to get the ip address, by script commands or by "c" functions?
Here's a Java snippet that might help:
WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int ipAddress = wifiInfo.getIpAddress();
Below is the code to get IP address via Java code (whether its connected via 3G or WIFI)
public String getLocalIpAddress() {
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
return inetAddress.getHostAddress().toString();
}
}
}
} catch (SocketException ex) {
Log.e(LOG_TAG, ex.toString());
}
return null;
}
Reference: Get the ip address of your device
If you want to get IP address in C, probably this thread would help:
using C code to get same info as ifconfig
Your question is a bit vague. What sort of IP address are you looking for? If you're looking for your LAN IP, ifconfig or iwconfig will suffice. If you're looking for the WAN IP use wget -qO- http://cmyip.com | grep "My IP Address Is" and you should be able to see your IP. I don't know whether the Android terminal you're using supports wget but it's worth a shot.
The IP address after "inet:" i.e. 192.168.42.79 IS your IP address. But, that being said, the shortest python script is...
#!/usr/bin/python
import socket
s=socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(('google.com',80))
return s.getsockname()[0]

How to get the system ip address after usb tethering of android phone?

I'm developing a mobile application in android.
Here I want to detect the IP address of the computer,system,etc after the usb tethering of the any android phone
I cannot find the solution.
If I put the following code then it takes the only the IP address of phone ,I need IP address of system
The following are code
ArrayList<InetAddress> arrayList=new ArrayList<InetAddress>();
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
arrayList.add(inetAddress);
inetAddress=null;
}
}
} catch (SocketException ex) {
Log.e("SALMAN", ex.toString());
}
return arrayList;
Please help me to get the system's IP address,If we cannot able to get means so please mention me. Because I'm new to android.
I'm using android 1.6 .
There is server side application in the windows xp system. That application is a windows service which is developed by C# .net.
That windows service listen to some port such like 234,etc.If some data comes to port then it will process the data and send response through that port.
In android the android application is send the data to the windows service via socket.
The android phone is USB tethered to the system in which windows service is running.Then system assume android phone is modem and additional IP address is generated for the system.This ip address is dynamically generated when the android phone is tethered.
For data transfer form mobile to system via socket .I will need to give the ip address of the system (after tethered) in my android coding.
If there is any method in android coding to get this IP address.
All are please give your ideas on regarding this.
Its not possible to find IP address created in PC from android after tethering. There is no API or other way to find it.
If you use InetAddress , it will return 192.168.42.129 - which is a DHCP address created by USB Tethering. It wont help you to communicate.
The other way is to scan the list of IP. USB Tethering will create ip ranging for 192.168.42.1 to 192.168.42.255 . You can write a simple scanner to find which one is active. But it will take some time.
Thanks to 'Swim N Swim' above. I found a code at
Retrieve IP and MAC addresses from /proc/net/arp (Android)
and modified a bit to get first IP having valid mac address. Works great when developing as a single user on your PC with tethered. You may follow above link for further selective IPs based on company name etc.
public static String getUSBThetheredIP() {
BufferedReader bufferedReader = null;
String ips="";
try {
bufferedReader = new BufferedReader(new FileReader("/proc/net/arp"));
String line;
while ((line = bufferedReader.readLine()) != null) {
String[] splitted = line.split(" +");
if (splitted != null && splitted.length >= 4) {
String ip = splitted[0];
String mac = splitted[3];
if (mac.matches("..:..:..:..:..:..")) {
if (mac.matches("00:00:00:00:00:00")) {
//Log.d("DEBUG", "Wrong:" + mac + ":" + ip);
} else {
//Log.d("DEBUG", "Correct:" + mac + ":" + ip);
ips = ip;
break;
}
}
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return ips;
}
Note that each time you tether after untether, you must start your apache or other processes on PC to take new IP effective. THis is what I experienced.

Categories

Resources