Files
2026-06-19 22:57:43 +02:00

192 lines
5.9 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!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">&nbsp;package it.polimi.ingsw.gc14.Network;
&nbsp;
&nbsp;import java.net.*;
&nbsp;import java.util.Enumeration;
&nbsp;
&nbsp;/** Utility class for resolving the local network interface to use when connecting to a server. */
&nbsp;public class InterfaceResolver {
&nbsp; private InterfaceResolver() {}
&nbsp;
&nbsp; /**
&nbsp; * Returns the local IPv4 address that can reach the given server IP.
&nbsp; *
&nbsp; * &lt;p&gt;If the server is localhost, returns {@code &quot;127.0.0.1&quot;}.
&nbsp; * Otherwise, iterates active non-loopback interfaces to find one on the same subnet;
&nbsp; * falls back to a UDP connect trick if no matching subnet is found.
&nbsp; *
&nbsp; * @param serverIp the server IP address or hostname.
&nbsp; * @return the local IP address string to use for outbound connections.
&nbsp; * @throws Exception if no suitable interface can be determined.
&nbsp; */
&nbsp; public static String resolveLocalInterface(String serverIp) throws Exception {
<b class="nc">&nbsp; if (serverIp.equalsIgnoreCase(&quot;localhost&quot;) || serverIp.startsWith(&quot;127.&quot;) || serverIp.isEmpty()) {</b>
<b class="nc">&nbsp; return &quot;127.0.0.1&quot;;</b>
&nbsp; }
&nbsp;
<b class="nc">&nbsp; InetAddress serverAddr = InetAddress.getByName(serverIp);</b>
<b class="nc">&nbsp; Enumeration&lt;NetworkInterface&gt; ifaces = NetworkInterface.getNetworkInterfaces();</b>
<b class="nc">&nbsp; while (ifaces.hasMoreElements()) {</b>
<b class="nc">&nbsp; NetworkInterface ni = ifaces.nextElement();</b>
<b class="nc">&nbsp; if (!ni.isUp() || ni.isLoopback()) continue;</b>
&nbsp;
<b class="nc">&nbsp; for (InterfaceAddress ia : ni.getInterfaceAddresses()) {</b>
<b class="nc">&nbsp; InetAddress localAddr = ia.getAddress();</b>
<b class="nc">&nbsp; if (!(localAddr instanceof Inet4Address)) continue;</b>
&nbsp;
<b class="nc">&nbsp; int prefix = ia.getNetworkPrefixLength();</b>
<b class="nc">&nbsp; if (sameSubnet(localAddr, serverAddr, prefix)) {</b>
<b class="nc">&nbsp; return localAddr.getHostAddress();</b>
&nbsp; }
&nbsp; }
&nbsp; }
&nbsp;
<b class="nc">&nbsp; try (DatagramSocket socket = new DatagramSocket()) {</b>
<b class="nc">&nbsp; socket.connect(serverAddr, 9);</b>
<b class="nc">&nbsp; return socket.getLocalAddress().getHostAddress();</b>
&nbsp; }
&nbsp; }
&nbsp; /**
&nbsp; * Returns {@code true} if {@code a} and {@code b} share the same subnet given a CIDR {@code prefix} length.
&nbsp; *
&nbsp; * @param a first IPv4 address.
&nbsp; * @param b second IPv4 address.
&nbsp; * @param prefix CIDR prefix length (032).
&nbsp; * @return {@code true} if both addresses belong to the same subnet.
&nbsp; */
&nbsp; private static boolean sameSubnet(InetAddress a, InetAddress b, int prefix) {
<b class="nc">&nbsp; if (prefix &lt; 0 || prefix &gt; 32) return false;</b>
<b class="nc">&nbsp; int maskBits = prefix == 0 ? 0 : (0xFFFFFFFF &lt;&lt; (32 - prefix));</b>
<b class="nc">&nbsp; int addrA = toInt(a.getAddress()) &amp; maskBits;</b>
<b class="nc">&nbsp; int addrB = toInt(b.getAddress()) &amp; maskBits;</b>
<b class="nc">&nbsp; return addrA == addrB;</b>
&nbsp; }
&nbsp; /**
&nbsp; * Converts a 4-byte big-endian IPv4 address array to a signed 32-bit integer.
&nbsp; *
&nbsp; * @param bytes the 4-byte address array.
&nbsp; * @return the corresponding signed integer.
&nbsp; */
&nbsp; private static int toInt(byte[] bytes) {
<b class="nc">&nbsp; return ((bytes[0] &amp; 0xFF) &lt;&lt; 24) |</b>
&nbsp; ((bytes[1] &amp; 0xFF) &lt;&lt; 16) |
&nbsp; ((bytes[2] &amp; 0xFF) &lt;&lt; 8) |
&nbsp; (bytes[3] &amp; 0xFF);
&nbsp; }
&nbsp;}
</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-19 22:53</div>
</div>
</body>
</html>