Skip to content
This repository has been archived by the owner on May 7, 2020. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
Core/net: Add getInterfaceAddresses. This completes getAllBroadcastAd…
…dresses() and is useful for discovery services (#4172)

Signed-off-by: David Gräff <david.graeff@web.de>
  • Loading branch information
David Gräff authored and kaikreuzer committed Sep 1, 2017
1 parent a27e6ef commit 7a8e742
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
Expand Up @@ -14,9 +14,12 @@
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;

import org.eclipse.jdt.annotation.NonNull;
Expand Down Expand Up @@ -186,6 +189,50 @@ public static String getBroadcastAddress() {
}
}

/**
* Gets every IPv4+IPv6 Address on each Interface except the loopback
* If addPrefix is true, the Address format is in the CIDR notation
* which is ip/prefix-length e.g. 129.31.31.1/24 otherwise only the IP is returned.
*
* @param addPrefix Add the prefix to each returned IP address
* @return The collected IPv4 and IPv6 Addresses
*/
public static Set<String> getInterfaceAddresses(boolean addPrefix) {
Set<String> interfaceIPs = new HashSet<>();

Enumeration<NetworkInterface> en;
try {
en = NetworkInterface.getNetworkInterfaces();
} catch (SocketException ex) {
LOGGER.error("Could not find interface IP addresses: {}", ex.getMessage(), ex);
return interfaceIPs;
}

while (en.hasMoreElements()) {
NetworkInterface networkInterface = en.nextElement();
boolean isLoopback = true;
try {
isLoopback = networkInterface.isLoopback();
} catch (SocketException ignored) {
}
if (isLoopback) {
continue;
}
for (Iterator<InterfaceAddress> it = networkInterface.getInterfaceAddresses().iterator(); it.hasNext();) {
InterfaceAddress interfaceAddress = it.next();
if (addPrefix) {
interfaceIPs.add(interfaceAddress.getAddress().getHostAddress() + "/"
+ interfaceAddress.getNetworkPrefixLength());
} else {
interfaceIPs.add(interfaceAddress.getAddress().getHostAddress());
}

}
}

return interfaceIPs;
}

/**
* Converts a netmask in bits into a string representation
* i.e. 24 bits -> 255.255.255.0
Expand Down
4 changes: 3 additions & 1 deletion docs/documentation/features/frameworkUtilities.md
Expand Up @@ -11,4 +11,6 @@ In this chapter useful services/utilities of the Eclipse SmartHome project are d
The `NetworkAddressService` is an OSGi service that can be used like any other OSGi service by adding a service reference to it. Its OSGi service name is `org.eclipse.smarthome.network`.
A user can configure his default network address via Paper UI under `Configuration -> System -> Network Settings`.
One can obtain the configured address via the `getPrimaryIpv4HostAddress()` method on the service.
This service is useful for example in the `ThingHandlerFactory` or an `AudioSink` where one needs a specific IP address of the host system to provide something like a `callback` URL.
This service is useful for example in the `ThingHandlerFactory` or an `AudioSink` where one needs a specific IP address of the host system to provide something like a `callback` URL.

Some static methods like `getAllBroadcastAddresses()` for retrieving all interface broadcast addresses or `getInterfaceAddresses()` for retrieving all assigned interface addresses might be usefull as well for discovery services.

0 comments on commit 7a8e742

Please sign in to comment.