178 lines
5.3 KiB
HTML
178 lines
5.3 KiB
HTML
|
|
|
|
|
|
<!DOCTYPE html>
|
|
<html id="htmlId">
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
|
|
<title>Coverage Report > InterfaceResolver</title>
|
|
<style type="text/css">
|
|
@import "../../css/coverage.css";
|
|
@import "../../css/idea.min.css";
|
|
</style>
|
|
<script type="text/javascript" src="../../js/highlight.min.js"></script>
|
|
<script type="text/javascript" src="../../js/highlightjs-line-numbers.min.js"></script>
|
|
</head>
|
|
|
|
<body>
|
|
<div class="content">
|
|
<div class="breadCrumbs">
|
|
Current scope: <a href="../../index.html">all classes</a>
|
|
<span class="separator">|</span>
|
|
<a href="../index.html">it.polimi.ingsw.gc14.Network</a>
|
|
</div>
|
|
|
|
<h1>Coverage Summary for Class: InterfaceResolver (it.polimi.ingsw.gc14.Network)</h1>
|
|
|
|
<table class="coverageStats">
|
|
<tr>
|
|
<th class="name">Class</th>
|
|
<th class="coverageStat
|
|
">
|
|
Class, %
|
|
</th>
|
|
<th class="coverageStat
|
|
">
|
|
Method, %
|
|
</th>
|
|
<th class="coverageStat
|
|
">
|
|
Branch, %
|
|
</th>
|
|
<th class="coverageStat
|
|
">
|
|
Line, %
|
|
</th>
|
|
</tr>
|
|
<tr>
|
|
<td class="name">InterfaceResolver</td>
|
|
<td class="coverageStat">
|
|
<span class="percent">
|
|
0%
|
|
</span>
|
|
<span class="absValue">
|
|
(0/1)
|
|
</span>
|
|
</td>
|
|
<td class="coverageStat">
|
|
<span class="percent">
|
|
0%
|
|
</span>
|
|
<span class="absValue">
|
|
(0/3)
|
|
</span>
|
|
</td>
|
|
<td class="coverageStat">
|
|
<span class="percent">
|
|
0%
|
|
</span>
|
|
<span class="absValue">
|
|
(0/26)
|
|
</span>
|
|
</td>
|
|
<td class="coverageStat">
|
|
<span class="percent">
|
|
0%
|
|
</span>
|
|
<span class="absValue">
|
|
(0/22)
|
|
</span>
|
|
</td>
|
|
</tr>
|
|
|
|
</table>
|
|
|
|
<br/>
|
|
<br/>
|
|
|
|
|
|
<pre>
|
|
<code class="sourceCode" id="sourceCode"> package it.polimi.ingsw.gc14.Network;
|
|
|
|
import java.net.*;
|
|
import java.util.Enumeration;
|
|
|
|
/** Utility class for resolving the local network interface to use when connecting to a server. */
|
|
public class InterfaceResolver {
|
|
private InterfaceResolver() {}
|
|
|
|
/**
|
|
* Returns the local IPv4 address that can reach the given server IP.
|
|
*
|
|
* <p>If the server is localhost, returns {@code "127.0.0.1"}.
|
|
* Otherwise iterates active non-loopback interfaces to find one on the same subnet;
|
|
* falls back to a UDP connect trick if no matching subnet is found.
|
|
*
|
|
* @param serverIp the server IP address or hostname.
|
|
* @return the local IP address string to use for outbound connections.
|
|
* @throws Exception if no suitable interface can be determined.
|
|
*/
|
|
public static String resolveLocalInterface(String serverIp) throws Exception {
|
|
<b class="nc"> if (serverIp.equalsIgnoreCase("localhost") || serverIp.startsWith("127.") || serverIp.isEmpty()) {</b>
|
|
<b class="nc"> return "127.0.0.1";</b>
|
|
}
|
|
|
|
<b class="nc"> InetAddress serverAddr = InetAddress.getByName(serverIp);</b>
|
|
<b class="nc"> Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces();</b>
|
|
<b class="nc"> while (ifaces.hasMoreElements()) {</b>
|
|
<b class="nc"> NetworkInterface ni = ifaces.nextElement();</b>
|
|
<b class="nc"> if (!ni.isUp() || ni.isLoopback()) continue;</b>
|
|
|
|
<b class="nc"> for (InterfaceAddress ia : ni.getInterfaceAddresses()) {</b>
|
|
<b class="nc"> InetAddress localAddr = ia.getAddress();</b>
|
|
<b class="nc"> if (!(localAddr instanceof Inet4Address)) continue;</b>
|
|
|
|
<b class="nc"> int prefix = ia.getNetworkPrefixLength();</b>
|
|
<b class="nc"> if (sameSubnet(localAddr, serverAddr, prefix)) {</b>
|
|
<b class="nc"> return localAddr.getHostAddress();</b>
|
|
}
|
|
}
|
|
}
|
|
|
|
<b class="nc"> try (DatagramSocket socket = new DatagramSocket()) {</b>
|
|
<b class="nc"> socket.connect(serverAddr, 9);</b>
|
|
<b class="nc"> return socket.getLocalAddress().getHostAddress();</b>
|
|
}
|
|
}
|
|
private static boolean sameSubnet(InetAddress a, InetAddress b, int prefix) {
|
|
<b class="nc"> if (prefix < 0 || prefix > 32) return false;</b>
|
|
<b class="nc"> int maskBits = prefix == 0 ? 0 : (0xFFFFFFFF << (32 - prefix));</b>
|
|
<b class="nc"> int addrA = toInt(a.getAddress()) & maskBits;</b>
|
|
<b class="nc"> int addrB = toInt(b.getAddress()) & maskBits;</b>
|
|
<b class="nc"> return addrA == addrB;</b>
|
|
}
|
|
private static int toInt(byte[] bytes) {
|
|
<b class="nc"> return ((bytes[0] & 0xFF) << 24) |</b>
|
|
((bytes[1] & 0xFF) << 16) |
|
|
((bytes[2] & 0xFF) << 8) |
|
|
(bytes[3] & 0xFF);
|
|
}
|
|
}
|
|
</code>
|
|
</pre>
|
|
</div>
|
|
|
|
<script type="text/javascript">
|
|
(function() {
|
|
var msie = false, msie9 = false;
|
|
/*@cc_on
|
|
msie = true;
|
|
@if (@_jscript_version >= 9)
|
|
msie9 = true;
|
|
@end
|
|
@*/
|
|
|
|
if (!msie || msie && msie9) {
|
|
hljs.highlightAll()
|
|
hljs.initLineNumbersOnLoad();
|
|
}
|
|
})();
|
|
</script>
|
|
|
|
<div class="footer">
|
|
|
|
<div style="float:right;">generated on 2026-06-14 21:53</div>
|
|
</div>
|
|
</body>
|
|
</html>
|