From 092ad76820f3d7a548bb81d018b6db0208f18524 Mon Sep 17 00:00:00 2001 From: nathan Date: Tue, 8 Sep 2020 23:54:16 +0200 Subject: [PATCH] Added proper WILDCARD addresses. Moved previous IPv4 WILDCARD -> WILDCARD_SAFE --- src/dorkbox/netUtil/IPv4.kt | 23 ++++++++++++------- src/dorkbox/netUtil/IPv6.kt | 14 ++++++++--- src/dorkbox/netUtil/ping/PingResultBuilder.kt | 2 +- test/dorkbox/netUtil/NetUtilTest.kt | 4 ++-- 4 files changed, 29 insertions(+), 14 deletions(-) diff --git a/src/dorkbox/netUtil/IPv4.kt b/src/dorkbox/netUtil/IPv4.kt index d77dc9a..c007898 100644 --- a/src/dorkbox/netUtil/IPv4.kt +++ b/src/dorkbox/netUtil/IPv4.kt @@ -51,28 +51,35 @@ object IPv4 { * The [Inet4Address] that represents the IPv4 loopback address '127.0.0.1' */ val LOCALHOST: Inet4Address by lazy { - // Create IPv4 loopback address. - // this will ALWAYS work + // Create IPv4 address, this will ALWAYS work InetAddress.getByAddress("localhost", byteArrayOf(127, 0, 0, 1)) as Inet4Address } + /** + * The [Inet4Address] that represents the IPv4 wildcard address '0.0.0.0' + */ + val WILDCARD: Inet4Address by lazy { + // Create IPv4 address, this will ALWAYS work + InetAddress.getByAddress("", byteArrayOf(0, 0, 0, 0)) as Inet4Address + } + /** * Windows is unable to work with 0.0.0.0 directly, and if you use LOOPBACK, you might not be able to access the server from another * machine. * * What this does is open a connection to 1.1.1.1 and see get the interface this traffic was on, and use that interface IP address */ - val WILDCARD: String by lazy { + val WILDCARD_SAFE: Inet4Address by lazy { if (Common.OS_WINDOWS) { // silly windows can't work with 0.0.0.0, BUT we can't use loopback because we might need to reach this machine from a different host // what we do is open a connection to 1.1.1.1 and see what interface this happened on, and this is used as the accessible // interface - var ip = "127.0.0.1" + var ip = WILDCARD runCatching { Socket().use { it.connect(InetSocketAddress("1.1.1.1", 80)) - ip = it.localAddress.hostAddress + ip = it.localAddress as Inet4Address } }.onFailure { Common.logger.error("Unable to determine outbound traffic local address. Using loopback instead.", it) @@ -81,7 +88,7 @@ object IPv4 { ip } else { // everyone else works correctly - "0.0.0.0" + WILDCARD } } @@ -672,8 +679,8 @@ object IPv4 { * * @return `String` containing the text-formatted IP address */ - fun toString(ip: InetAddress): String { - return (ip as Inet4Address).hostAddress + fun toString(ip: Inet4Address): String { + return ip.hostAddress } diff --git a/src/dorkbox/netUtil/IPv6.kt b/src/dorkbox/netUtil/IPv6.kt index e72922e..0207064 100644 --- a/src/dorkbox/netUtil/IPv6.kt +++ b/src/dorkbox/netUtil/IPv6.kt @@ -17,6 +17,7 @@ package dorkbox.netUtil +import java.net.Inet4Address import java.net.Inet6Address import java.net.InetAddress import java.net.UnknownHostException @@ -85,11 +86,18 @@ object IPv6 { * The [Inet6Address] that represents the IPv6 loopback address '::1' */ val LOCALHOST: Inet6Address by lazy { - // Create IPv6 loopback address. - // should never fail + // Create IPv6 address, this will ALWAYS work InetAddress.getByAddress("localhost", byteArrayOf(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1)) as Inet6Address } + /** + * The [Inet4Address] that represents the IPv4 wildcard address '0.0.0.0' + */ + val WILDCARD: Inet6Address by lazy { + // Create IPv6 address, this will ALWAYS work + InetAddress.getByAddress("", byteArrayOf(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)) as Inet6Address + } + /** * Takes a [String] and parses it to see if it is a valid IPV6 address. * @@ -568,7 +576,7 @@ object IPv6 { * * @return `String` containing the text-formatted IP address */ - fun toString(ip: InetAddress, ipv4Mapped: Boolean = false): String { + fun toString(ip: Inet6Address, ipv4Mapped: Boolean = false): String { return toAddressString(ip.address, 0, ipv4Mapped) } diff --git a/src/dorkbox/netUtil/ping/PingResultBuilder.kt b/src/dorkbox/netUtil/ping/PingResultBuilder.kt index 5b53a12..3970181 100644 --- a/src/dorkbox/netUtil/ping/PingResultBuilder.kt +++ b/src/dorkbox/netUtil/ping/PingResultBuilder.kt @@ -58,7 +58,7 @@ internal object PingResultBuilder { listOf( /* Windows */ ResultParser.of("Pinging (.*) with") { result, matcher -> - result.host = IPv4.WILDCARD // note: this is REALLY the host used for default traffic + result.host = IPv4.WILDCARD_SAFE.hostAddress // note: this is REALLY the host used for default traffic result.ip = matcher.group(1) result }, diff --git a/test/dorkbox/netUtil/NetUtilTest.kt b/test/dorkbox/netUtil/NetUtilTest.kt index 6f639e9..b7bc035 100644 --- a/test/dorkbox/netUtil/NetUtilTest.kt +++ b/test/dorkbox/netUtil/NetUtilTest.kt @@ -681,7 +681,7 @@ class NetUtilTest { @Throws(UnknownHostException::class) fun testIp6AddressToString() { for ((key, value) in ipv6ToAddressStrings) { - assertEquals(value, IPv6.toString(InetAddress.getByAddress(key))) + assertEquals(value, IP.toString(InetAddress.getByAddress(key))) } } @@ -689,7 +689,7 @@ class NetUtilTest { @Throws(UnknownHostException::class) fun testIp4AddressToString() { for ((key, value) in validIpV4Hosts) { - assertEquals(key, IPv4.toString(InetAddress.getByAddress(unhex(value)))) + assertEquals(key, IP.toString(InetAddress.getByAddress(unhex(value)))) } }