Skip to content

Commit

Permalink
Lazy init default netif
Browse files Browse the repository at this point in the history
  • Loading branch information
bmalinowsky committed May 9, 2024
1 parent 78202c4 commit b194a31
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/io/calimero/knxnetip/Discoverer.java
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,7 @@ private CompletableFuture<Result<SearchResponse>> 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());

Expand Down Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/io/calimero/knxnetip/KNXnetIPRouting.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)
Expand Down
22 changes: 13 additions & 9 deletions src/io/calimero/knxnetip/Net.java
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b194a31

Please sign in to comment.