Skip to content

Commit

Permalink
fix unexpect exception from NetworkInterface.ifUp (alibaba#12325)
Browse files Browse the repository at this point in the history
  • Loading branch information
shalk authored Jul 10, 2024
1 parent 8aba80d commit 2aa9fc5
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
23 changes: 19 additions & 4 deletions sys/src/main/java/com/alibaba/nacos/sys/utils/InetUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Enumeration;
Expand Down Expand Up @@ -192,12 +193,12 @@ public static InetAddress findFirstNonLoopbackAddress() {
for (Enumeration<NetworkInterface> nics = NetworkInterface.getNetworkInterfaces();
nics.hasMoreElements(); ) {
NetworkInterface ifc = nics.nextElement();
if (ifc.isUp()) {
if (isUp(ifc)) {
LOG.debug("Testing interface: " + ifc.getDisplayName());
if (ifc.getIndex() < lowest || result == null) {
lowest = ifc.getIndex();
} else {
if (ifc.getIndex() >= lowest && result != null) {
continue;
} else {
lowest = ifc.getIndex();
}

if (!ignoreInterface(ifc.getDisplayName())) {
Expand Down Expand Up @@ -231,6 +232,20 @@ public static InetAddress findFirstNonLoopbackAddress() {
return null;
}

/**
* check network intreface isUp, not throw SocketException.
* @param ifc network interface
* @return true or false;
*/
public static boolean isUp(NetworkInterface ifc) {
try {
return ifc.isUp();
} catch (SocketException e) {
LOG.debug("Network interface can not get isUp, exception: ", e);
}
return false;
}

private static boolean isPreferredAddress(InetAddress address) {
if (useOnlySiteLocalInterface) {
final boolean siteLocalAddress = address.isSiteLocalAddress();
Expand Down
17 changes: 17 additions & 0 deletions sys/src/test/java/com/alibaba/nacos/sys/utils/InetUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,17 @@
import org.springframework.mock.env.MockEnvironment;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.concurrent.TimeUnit;

import static com.alibaba.nacos.sys.env.Constants.NACOS_SERVER_IP;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

class InetUtilsTest {

Expand Down Expand Up @@ -71,4 +75,17 @@ void findFirstNonLoopbackAddress() {
assertNotNull(address);
assertFalse(address.isLoopbackAddress());
}

@Test
void testisUp() throws SocketException {
NetworkInterface nic = mock(NetworkInterface.class);
when(nic.isUp()).thenReturn(true);
assertTrue(InetUtils.isUp(nic));

when(nic.isUp()).thenReturn(false);
assertFalse(InetUtils.isUp(nic));

when(nic.isUp()).thenThrow(new SocketException());
assertFalse(InetUtils.isUp(nic));
}
}

0 comments on commit 2aa9fc5

Please sign in to comment.