1

I have hardware based on a microcontroller with Ethernet communication.

There is a TCP server in the microcontroller.

To configure the hardware, I have an application made in C# for android.

The application, in turn, has a TCP client.

For the app to find my hardware, what I'm doing is pinging each of the addresses of the same network segment of my mobile. That is, if the IP address of my mobile is 192.168.0.xx, I ping from the address 192.168.0.1 to the address 192.168.0.255.

Those addresses that respond, I try to open a socket and send a data frame, if the answer is correct, I assume that I have found a hardware in my local network (there could be more than one connected)

Obviously those IP addresses that don't respond, or that the socket cannot be opened or that they respond to something wrong are discarded.

Those valid addresses are displayed in a list for the user to choose with which to interact.

Also, these valid addresses are saved in the application so that the next time the app is opened, it will automatically connect to the stored addresses, avoiding the scanning of the IP addresses.

This seems correct to me the first time the user installs the hardware and configures with the app

The problem is that I was informed that there are users that their routers are configured to renew their IP addresses once a month.

If this happens, the app should again perform a scan of all the IP addresses again, and this is somewhat cumbersome, since scanning all the IPs takes some time, I don't think users are happy configuring their app and hardware once a month.

Another cumbersome solution could be to use static IP addresses, but I don't think that's a good idea either.

Any suggestions on how to improve this?

Plc Worker
  • 101
  • 7
  • 2
    Once a month? I though the DHCP lease time was something like 2 days. Though that depends entirely on the DHCP server. And Static IP assignments are made specifically for this reason. – gunr2171 Dec 22 '21 at 02:02
  • I have not checked, it is something that the person for whom I'm developing the hardware and the application told me. In the networks I have done the tests so far, it has not changed the IP addresses that were assigned to the devices. – Plc Worker Dec 22 '21 at 02:08
  • 1
    Doing something like that will get your application banned from company networks. The target should subscribe to a particular multicast group, and you can send a request to the multicast group. The device should the report its address on the LAN. – Ron Maupin Dec 22 '21 at 02:41
  • 1
    Sounds like you should be using a UDP broadcast or multicast for discovery. Broadcast a single UDP message on some port number, and the microcontroller should be configured to respond on that same port. This will pick up all devices on the same LAN – Charlieface Dec 22 '21 at 02:52
  • 1
    @gunr2171 Standard practice on most DHCP setups is to give the same IP address unless the lease expires without renewing – Charlieface Dec 22 '21 at 02:53
  • @Charlieface, broadcast is basically obsolete. Many companies will reject applications that make use of broadcast because it is a security risk. IPv6 does not have broadcast, so such applications cannot be ported to IPv6. The modern way is to use multicast. Only ARP should be using broadcast on a LAN, and it would have used multicast if that had existed when ARP was defined. IPv6 uses NDP instead of ARP, and it uses multicast. Broadcast interrupts _everything_ on a LAN, including things like router and printers that have no interest in the broadcast traffic. – Ron Maupin Dec 22 '21 at 03:12
  • Are you in control of the code in the microcontroller as well as the code in the client? – Caius Jard Dec 22 '21 at 07:07

2 Answers2

0

You need to take subnets into account. You are assuming that the subnet you are connected to is a /24. You are also pinging the broadcast address (.255) which is unnecessary. A host doesn't reside there.

DHCP assignments will renew their lease halfway through the lease period. If your hardware is still on the network it will most likely get the same IP assigned as it did prior from most modern DHCP implementations.

Finally, consider lowering any timeout values in your scan. Scanning a /24 for hosts to respond on a specific port should complete in seconds.

tajacks
  • 71
  • 6
0

The solution is explained by @Charlieface in the comments of the question.

I'm going to implement a UDP server on the microcontroller with an IP address within the range of 224.0.0.0-239.255.255.255 (Multicast IP addresses).

When connecting to said server and sending a data frame, the ip and other parameters will be returned so that the app can connect directly to the hardware without having to perform a scan of all the IP addresses.

More details of the solution, in the following freeRTOs thread:

How can I create a UDP server with static IP?

Plc Worker
  • 101
  • 7