How can the Android device programatically infer the IP address of the Linux device it's tethered to? - networking

Hi! Asked this question on many forums and nobody could answer me so PLEASE help!
I have Android device connected to Linux device via USB tethering.
On Android device I have front-end(html, js, css) browser app and on Linux device I have back-end(node.js).
There is a socket connection between front-end and back-end with defined IP:PORT.
Androids gateway is Linux device. Linux device is connected to local network.
When Android device is connected to Linux device, Linux device initializes new network interface called usb0 with static IP address.
Sockets are based on that network IP that is same on every instance of front-end+back-end devices.
I have more than 5 instances of same front-end+back-end devices connected to local network.
Is there a way to somehow find the back-end device IP address without setting it static?
My problem starts when starting all devices at the same time. Sometimes Android device in instance 1 connects to Linux device from other instance and I want to prevent that by not connection to usb0 with Android but to eth0 DHCP of Linux device. I can never know which IP address will be on Linux device but front-end app needs a IP:PORT to connect.
PORT is defined so the string for connecting is {IP + ":3000"}
I know I can see all the IP addresses on network with arp or similar but I need to do that automatic in front-end app on app start up.
I want to find first device IP address that android device is connected to. That is always a front-ends pair Linux device.
Or if it is not possible to prevent Android device to be able to connect another instance with IP tables on Linux device.
P.S I know that Android is also Linux but someone who wants to answer will know what I'm talking about.

Here is a solution describing how to listen for tethering state changes:
First you need to be familiar with BroadcastReceiver. You can find a lot of tutorials describing in great detail how this works (try googling how to listen for connectivity changes in Android).
In order to get the Tethering state update, you need to use a hidden filter action of Android (see ConnectivityManager) and in your BroadcastReceiver class:
IntentFilter filter = new IntentFilter("android.net.conn.TETHER_STATE_CHANGED");
then register the filter to your BroadcastReceiver:
myApplicationContext.registerReceiver(this, filter);
In your onReceive(final Context context, final Intent intent) method, the Intent.extras information contains 3 arrays filled with the corresponding tethered network interface:
erroredArray / availableArray / activeArray
It's a little bit tricky but you can get the tethering status information.
In addition, you can do some reflexion on a hidden function of Android code:
Search for getTetherableIfaces() in the Connectivity Manager.
Here is a link: https://github.com/android/platform_frameworks_base/blob/master/core/java/android/net/ConnectivityManager.java#L1604

Related

Is it possible to access virtual serial ports via Web Serial API?

I seem to only be able to access physical ports. I do want to access virtual ones like ones created with virtual null modem or virtual serial port driver.
Does anyone know if this is possible.
Chrome enumerates serial ports by calling SetupDiGetClassDevs with GUID_DEVINTERFACE_SERENUM_BUS_ENUMERATOR. As I discovered when implementing this enumeration logic there are a surprisingly large number of ways to ask Windows for a list of all available COM ports. The advantage of this approach being that it provides device information which can be used to get the human-readable name of the device and USB product identifiers if it is a USB device. It is likely that a virtual COM port driver may be creating the device in a way which prevents it from being discovered this way.
I've created https://bugs.chromium.org/p/chromium/issues/detail?id=1238369 to track this as a Chrome bug. Please comment on whether you are using a different virtual serial port driver than the one mentioned there.
I managed to get Chrome to see the virtual serial ports using com0com on Windows 10 21H1 (OS Build 19043.1110). Chrome connects to the port successfully and it works as expected:

How to switch connection between cloud and local network in ESP8266

I would like to understand it is possible to switch the ESP8266 connection between the cloud and WiFi LAN.
There are two paths to control the sonoff switch, connected to a wifi router. In case the phone is connected to the router, Path 1 acts as LAN network and ESP8266 can be controlled even without internet. In case I am not at home and would like to control the switch remotely via cloud, path 2 would be followed. Thus, I could get the best of both worlds.
From all the examples that i have seen so far ESP8266 could be operated in only one mode at one point of time, either Lan or cloud (both via the WiFi router).
So kindly help me with the switching code/logic, that will wait to work upon the commands provided via LAN mode if there are none left. Now it should work upon the instructions after switching to cloud mode and finally return to LAN mode after sometime and so on.
For security reasons your Sonoff device should not be reachable from the internet directly - ever. It should only be accessible within your local (home) network. That's your path 1.
To enable path 2 - without affecting the Sonoff device at all - you need a way to build a VPN from your smartphone to your local network. For the node it will look like all traffic is coming from your local network even though you are actually connecting into it from remote.
A fairly simple and cost-effective way to achieve this is to run PiVPN on a Raspberry Pi. See e.g. this video for instructions: https://www.youtube.com/watch?v=15VjDVCISj0

QtNetwork active interface change detection

I am aware this question is howto oriented, but I am getting desperate. Lets say some computer has more than one hardware network interface, eth0 and wlan0, the first one is onboard Ethernet port, the other one represents PCI WiFi card. What I would like to achieve is that the my Qt app while running, can detect if someone (for testing purposes, I myself) in operating system disconnects computer from LAN. Once app detects link failure, it tries to connect to same LAN via other available interface, wlan0 or vice versa.
I am reading Qt 5 Bearer management docs, but I simply do not get needed info. Now, should I open QNetworkSession with open() and connect to QNetworkSession::stateChanged(QNetworkSession::State state) or are there any other ways to do it in Qt fashion?

How to check port is up or down with SNMP

I develop a app which discovers a network topology and draw it programmaticly with java and SNMP4j
I send CDP and LLDP requests to devices and analyze result of this. However there are devices which has not support CDP or LLDP(like Access Point) therefor I cannot discover it. I must show it as unkown device.
Thanks to CDP and LLDP I know which device is on which port. To understand unknown device is on which port,
I need a oid to learn up and down ports of the Switch with SNMP or another method.
I use SNMP4J
The table dot1dTpFdbTable (1.3.6.1.2.1.17.4.3) shows which MAC addresses can be reached on which ports.
Two things to watch out for: it is not supported by all devices and it does not only list the connected devices but also everything connected to those devices.

Can the MAC address of a mobile device be easily determined?

This is somewhat of a general question, is it realistic to assume that a router can resolve any mobile device's MAC address without being already connected via wifi or bluetooth?
If the device is broadcasting, or if it's connected to another access point, then capturing the wifi data would allow you to capture the device's MAC address. Routers aren't generally set up to listen in that way, and I don't think it would be simple to configure that kind of behavior.
In general, if a device isn't connected to a wireless router/access point, you aren't going to be able to capture its MAC.

Resources