How to Get Notified for Ip Address Changes Automatically

how to get notified for IP address changes automatically

You receive notifications from the kernel via netlink sockets.

You would need to create a NETLINK_ROUTE socket and subscribe it to IP changes via bind()ing it to the RTMGRP_IPV4_IFADDR group. Then, you'll receive netlink messages of type RTM_NEWADDR and RTM_DELADDR with a route attribute of IFA_LOCAL or IFA_ADDRESS.

How to detect IP address change programmatically in Linux?

In C, to get the current IP I use:

    int s;
struct ifreq ifr = {};

s = socket(PF_INET, SOCK_DGRAM, 0);

strncpy(ifr.ifr_name, "eth0", sizeof(ifr.ifr_name));

if (ioctl(s, SIOCGIFADDR, &ifr) >= 0)
printf("%s\n",
inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr));

Replace "eth0" with the interface you're looking at. All you now need to do is poll for a change.

Detect IP-Address change on an interface

Because you use Systemd you might already use systemd-networkd for managing your devices instead of relying on 3rd party code.

You could use the structured journal output to get the last 2 ADDRESS field of the current BOOD_ID.(sadly, there is no notification mechanism for address changes in systemd-networkd):

→ sudo journalctl -F ADDRESS -u systemd-networkd -n 2
192.168.178.29

So, if there is only one line output, there was no address change.

How can I detect when the device gets a new IP?

You can do this with the ConnectivityManager:

You can use this to query the current connection state:

ConnectivityManager connMananger = (ConnectivityManager) 
context.getSystemService(Context.CONNECTIVITY_SERVICE);

NetworkInfo netInfo = connMananger.getActiveNetworkInfo();

The current IP address of network interfaces can be acquired with NetworkInterface.getNetworkInterfaces()

And you can receive automatic notification of when the connection state changes via the CONNECTIVITY_ACTION broadcast

How to detect IP address change programmatically in Linux?

In C, to get the current IP I use:

    int s;
struct ifreq ifr = {};

s = socket(PF_INET, SOCK_DGRAM, 0);

strncpy(ifr.ifr_name, "eth0", sizeof(ifr.ifr_name));

if (ioctl(s, SIOCGIFADDR, &ifr) >= 0)
printf("%s\n",
inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr));

Replace "eth0" with the interface you're looking at. All you now need to do is poll for a change.

how to know if WAN IP has changed and receive massive IP address broadcasts

Erik, your problem is much simpler than it seems to have been made to sound.

This problem been around for a decade maybe two. No need to re-invent the wheel here.

Why Polling/Pinging is a Bad Idea

The dynamic IPs provided by ISPs can have a variable lease time, but will often be at least 24-72 hours. Pinging your server every 1-10m will be a horrible waist of resources potentially making over a 4,320 useless HTTP requests PER CLIENT in a 72 hour period. Each request will be say around 300 bytes * 4,320 wasted http requests equals 1.3mb wasted bandwidth multiplied by your target client count of 1 million clients, you are talking about a monthly wasted bandwidth of ~1.2 TB! And that's just the wasted bandwidth, not the other bandwidth you might need to run your app and provide useful info.

The clients need to be smarter than just pinging frequently. Rather they should be able to check if their IP address matches the DNS on startup, then only when the IP changes, send a notification to the server. This will cut down your bandwidth and server processing requirements by thousands of times.

What you are describing is Dynamic DNS

What you are talking about is "Dynamic DNS" (both a descriptive name for the technology and also the name of one company that provides a SaaS solution).

Dynamic DNS is quite simply a DNS server that allows you to very rapidly change the mapping between a name and an IP address. Normally this is useful for devices using an ISP which only provides dynamic IPs. Whenever the IP changes for the router/server on a dynamic IP it will inform the Dynamic DNS server of the change.

  • The defacto standard protocol for dynamic DNS is well documented. Start here: DNS Update API, I think the specifics you are looking for are here: DynDNS Perform Update. Most commercial implementations out there are very close to the same protocol due to the fact that router hardware usually has a built in DynDNS client which everyone wants to use.
  • Most routers (even cheap ones) already have Dynamic DNS clients built into them. (You can write your own soft client, but the router is likely the most efficient location for this as your clients are likely being NAT'd with a private IP - you can still do it but at a cost of more bandwidth for public IP discovery)
  • A quick google search for "dynamic DNS java client" brings up full source projects like this one: Java DynDNS client (untested, just illustrating the power of search)

Other Considerations for your System Architecture

Lets say the IP-client mapping thing gets resolved. You figured it all out and it works perfectly, you always knows the IP for each client. Would you then have a nice reliable system for transferring files to clients from mobile devices? I would say no.

Both mobiles and home computers can have multiple connection types, Wi-Fi, Cellular Data, maybe wired data. Each of these networks may have different security systems in place. So a connection from a cellular data mobile to a wifi laptop behind a home router is going to look very different than a wifi mobile device connecting to laptop on the same wifi network.

You may have physical router firewalls to contend with. Also home computers may have windows firewall enabled, maybe norton internet security, maybe symantec, maybe AVG, maybe zone alarm, etc... Do you know the firewall considerations for all these potential clients?



Related Topics



Leave a reply



Submit