diff --git a/src/io/calimero/knxnetip/Discoverer.java b/src/io/calimero/knxnetip/Discoverer.java index 4c1a9f62..a53bf1fe 100644 --- a/src/io/calimero/knxnetip/Discoverer.java +++ b/src/io/calimero/knxnetip/Discoverer.java @@ -723,7 +723,7 @@ private CompletableFuture> receiveAsync(final DatagramCha final InetSocketAddress local = (InetSocketAddress) dc.getLocalAddress(); final NetworkInterface netif; if (local.getAddress().isAnyLocalAddress()) - netif = Net.defaultNetif; + netif = Net.defaultNetif(); else netif = NetworkInterface.getByInetAddress(local.getAddress()); @@ -765,7 +765,7 @@ private final class ReceiverLoop extends UdpSocketLooper implements Runnable super(null, false, receiveBufferSize, 0, (int) timeout.toMillis()); final var mcastIf = dc.getOption(StandardSocketOptions.IP_MULTICAST_IF); - nif = mcastIf == null ? Net.defaultNetif : mcastIf; + nif = mcastIf == null ? Net.defaultNetif() : mcastIf; this.localEndpoint = localEndpoint; multicast = true; server = null; diff --git a/src/io/calimero/knxnetip/KNXnetIPRouting.java b/src/io/calimero/knxnetip/KNXnetIPRouting.java index 26fe34cc..91ed4d9b 100644 --- a/src/io/calimero/knxnetip/KNXnetIPRouting.java +++ b/src/io/calimero/knxnetip/KNXnetIPRouting.java @@ -316,7 +316,7 @@ public final int getHopCount() public final NetworkInterface networkInterface() { try { final NetworkInterface netif = dc.getOption(StandardSocketOptions.IP_MULTICAST_IF); - return netif == null ? Net.defaultNetif : netif; + return netif == null ? Net.defaultNetif() : netif; } catch (final IOException e) { throw new KnxRuntimeException("socket error getting network interface", e); @@ -387,7 +387,7 @@ protected void init(final NetworkInterface netIf, final boolean useMulticastLoop dcSysBcast.setOption(StandardSocketOptions.IP_MULTICAST_IF, setNetif); } else - setNetif = Net.defaultNetif; + setNetif = Net.defaultNetif(); logger.log(DEBUG, "join multicast group {0} on {1}", multicast.getHostAddress(), setNetif.getName()); dc.join(multicast, setNetif); @@ -720,7 +720,7 @@ private void updateRoutingFlowControl(final RoutingBusy busy, final InetSocketAd private boolean sentByUs(final InetSocketAddress sender) { final var netif = networkInterface(); - if (netif == Net.defaultNetif) { + if (netif == Net.defaultNetif()) { // check addresses of all netifs, in case no outgoing mcast netif was configured try { return NetworkInterface.networkInterfaces().flatMap(NetworkInterface::inetAddresses) diff --git a/src/io/calimero/knxnetip/Net.java b/src/io/calimero/knxnetip/Net.java index 9431a96e..4d6f899e 100644 --- a/src/io/calimero/knxnetip/Net.java +++ b/src/io/calimero/knxnetip/Net.java @@ -1,6 +1,6 @@ /* Calimero 2 - A library for KNX network access - Copyright (c) 2021, 2023 B. Malinowsky + Copyright (c) 2021, 2024 B. Malinowsky This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -58,15 +58,19 @@ private Net() {} private static final Logger logger = LogService.getLogger("io.calimero.knxnetip.Net"); - - static final NetworkInterface defaultNetif; - static { - try (var s = new MulticastSocket()) { - defaultNetif = s.getNetworkInterface(); - } - catch (final IOException e) { - throw new ExceptionInInitializerError(e); + static NetworkInterface defaultNetif() { + class NetIf { + static final NetworkInterface defaultNetif; + static { + try (var s = new MulticastSocket()) { + defaultNetif = s.getNetworkInterface(); + } + catch (final IOException e) { + throw new ExceptionInInitializerError(e); + } + } } + return NetIf.defaultNetif; } // finds a local IPv4 address with its network prefix "matching" the remote address