diff --git a/bundles/org.openhab.core.config.discovery.addon.ip/src/main/java/org/openhab/core/config/discovery/addon/ip/IpAddonFinder.java b/bundles/org.openhab.core.config.discovery.addon.ip/src/main/java/org/openhab/core/config/discovery/addon/ip/IpAddonFinder.java index 8b5adde3b9c..329089a4f30 100644 --- a/bundles/org.openhab.core.config.discovery.addon.ip/src/main/java/org/openhab/core/config/discovery/addon/ip/IpAddonFinder.java +++ b/bundles/org.openhab.core.config.discovery.addon.ip/src/main/java/org/openhab/core/config/discovery/addon/ip/IpAddonFinder.java @@ -50,9 +50,12 @@ import org.openhab.core.config.discovery.addon.AddonFinder; import org.openhab.core.config.discovery.addon.BaseAddonFinder; import org.openhab.core.net.NetUtil; +import org.openhab.core.net.NetworkAddressService; import org.openhab.core.util.StringUtils; +import org.osgi.service.component.annotations.Activate; import org.osgi.service.component.annotations.Component; import org.osgi.service.component.annotations.Deactivate; +import org.osgi.service.component.annotations.Reference; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -173,6 +176,7 @@ public class IpAddonFinder extends BaseAddonFinder { public static final String SERVICE_TYPE = SERVICE_TYPE_IP; public static final String SERVICE_NAME = SERVICE_NAME_IP; + private static final int MAX_IP_ADDRESSES = 5; private static final String TYPE_IP_MULTICAST = "ipMulticast"; private static final String MATCH_PROPERTY_RESPONSE = "response"; private static final String PARAMETER_DEST_IP = "destIp"; @@ -190,9 +194,24 @@ public class IpAddonFinder extends BaseAddonFinder { .getScheduledPool(ThreadPoolManager.THREAD_POOL_NAME_COMMON); private @Nullable Future scanJob = null; Set suggestions = new HashSet<>(); + Set myIpAddresses; - public IpAddonFinder() { - logger.trace("IpAddonFinder::IpAddonFinder"); + @Activate + public IpAddonFinder(final @Reference NetworkAddressService networkAddressService) { + myIpAddresses = NetUtil.getAllInterfaceAddresses().stream().filter(a -> a.getAddress() instanceof Inet4Address) + .map(a -> a.getAddress().getHostAddress()).collect(Collectors.toSet()); + if (myIpAddresses.size() > MAX_IP_ADDRESSES) { + @Nullable + String myIp = networkAddressService.getPrimaryIpv4HostAddress(); + if (myIp != null) { + logger.info("Too many configured IP addresses, falling back to main IP {}", myIp); + myIpAddresses = Set.of(myIp); + } else { + logger.info("Too many configured IP addresses, falling back to IP {}", myIp); + myIpAddresses = Set.of(myIpAddresses.iterator().next()); + } + } + logger.trace("IpAddonFinder scanning on {}", myIpAddresses); // start of scan will be triggered by setAddonCandidates to ensure addonCandidates are available } @@ -299,11 +318,7 @@ private void scan() { try { switch (Objects.toString(type)) { case TYPE_IP_MULTICAST: - List ipAddresses = NetUtil.getAllInterfaceAddresses().stream() - .filter(a -> a.getAddress() instanceof Inet4Address) - .map(a -> a.getAddress().getHostAddress()).toList(); - - for (String localIp : ipAddresses) { + for (String localIp : myIpAddresses) { try (DatagramChannel channel = (DatagramChannel) DatagramChannel .open(StandardProtocolFamily.INET) .setOption(StandardSocketOptions.SO_REUSEADDR, true)