Skip to content

Commit

Permalink
Restrict scan to max 5 addresses, fallback to main IP
Browse files Browse the repository at this point in the history
Signed-off-by: Holger Friedrich <[email protected]>
  • Loading branch information
holgerfriedrich committed Dec 28, 2023
1 parent e3f4254 commit 1ec1821
Showing 1 changed file with 22 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,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;

Expand Down Expand Up @@ -175,6 +178,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";
Expand All @@ -192,9 +196,24 @@ public class IpAddonFinder extends BaseAddonFinder {
.getScheduledPool(ThreadPoolManager.THREAD_POOL_NAME_COMMON);
private @Nullable Future<?> scanJob = null;
Set<AddonInfo> suggestions = new HashSet<>();
Set<String> 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
}

Expand Down Expand Up @@ -301,11 +320,7 @@ private void scan() {
try {
switch (Objects.toString(type)) {
case TYPE_IP_MULTICAST:
List<String> 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)
Expand Down

0 comments on commit 1ec1821

Please sign in to comment.