From dd3dbbf7f9bc5362de9f4e348aeb4aa9e3ee5b6c Mon Sep 17 00:00:00 2001 From: zzz Date: Wed, 5 Feb 2020 16:43:58 +0000 Subject: [PATCH] SSU: Skip down interfaces when looking for MTU, handle bad values better --- .../src/net/i2p/router/transport/udp/MTU.java | 32 +++++++++++++++++-- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/router/java/src/net/i2p/router/transport/udp/MTU.java b/router/java/src/net/i2p/router/transport/udp/MTU.java index 15e80a3489..d7268f87c2 100644 --- a/router/java/src/net/i2p/router/transport/udp/MTU.java +++ b/router/java/src/net/i2p/router/transport/udp/MTU.java @@ -58,6 +58,16 @@ public class MTU { if (ifcs != null) { while (ifcs.hasMoreElements()) { NetworkInterface ifc = ifcs.nextElement(); + try { + if (!ifc.isUp()) { + // This is super-important on Windows which has 40+ down interfaces + // and will also deliver IP addresses for down interfaces, + // for example recently disconnected wifi. + continue; + } + } catch (SocketException e) { + continue; + } for(Enumeration addrs = ifc.getInetAddresses(); addrs.hasMoreElements();) { InetAddress addr = addrs.nextElement(); if (ia.equals(addr)) { @@ -66,8 +76,12 @@ public class MTU { //return ifc.getMTU(); boolean isIPv6 = addr instanceof Inet6Address; int mtu = ifc.getMTU(); - if ((isIPv6 && mtu < PeerState.MIN_IPV6_MTU) || - (!isIPv6 && mtu < PeerState.MIN_MTU)) { + // can be -1 on Windows + if (mtu < 0) + mtu = isIPv6 ? PeerState.MIN_IPV6_MTU : 1500; + if (mtu > 0 && + ((isIPv6 && mtu < PeerState.MIN_IPV6_MTU) || + (!isIPv6 && mtu < PeerState.MIN_MTU))) { Log log = I2PAppContext.getGlobalContext().logManager().getLog(MTU.class); log.logAlways(Log.WARN, "Unusually low MTU " + mtu + " for interface " + ia + ", consider disabling"); @@ -122,11 +136,23 @@ public class MTU { try { Enumeration ifcs = NetworkInterface.getNetworkInterfaces(); if (ifcs != null) { + // this is O(n**2) through the interfaces and very slow on windows while (ifcs.hasMoreElements()) { NetworkInterface ifc = ifcs.nextElement(); + try { + if (!ifc.isUp()) { + // This is super-important on Windows which has 40+ down interfaces + // and will also deliver IP addresses for down interfaces, + // for example recently disconnected wifi. + continue; + } + } catch (SocketException e) { + continue; + } for(Enumeration addrs = ifc.getInetAddresses(); addrs.hasMoreElements();) { InetAddress addr = addrs.nextElement(); - System.out.println("I2P MTU for " + addr.getHostAddress() + " is " + getMTU(addr)); + System.out.println("MTU for " + addr.getHostAddress() + " is " + ifc.getMTU() + + " I2P MTU is " + getMTU(addr)); } } }