Added proper WILDCARD addresses. Moved previous IPv4 WILDCARD -> WILDCARD_SAFE

This commit is contained in:
nathan 2020-09-08 23:54:16 +02:00
parent 89141a11f9
commit 092ad76820
4 changed files with 29 additions and 14 deletions

View File

@ -51,28 +51,35 @@ object IPv4 {
* The [Inet4Address] that represents the IPv4 loopback address '127.0.0.1' * The [Inet4Address] that represents the IPv4 loopback address '127.0.0.1'
*/ */
val LOCALHOST: Inet4Address by lazy { val LOCALHOST: Inet4Address by lazy {
// Create IPv4 loopback address. // Create IPv4 address, this will ALWAYS work
// this will ALWAYS work
InetAddress.getByAddress("localhost", byteArrayOf(127, 0, 0, 1)) as Inet4Address 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 * 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. * 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 * 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) { 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 // 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 // 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 // interface
var ip = "127.0.0.1" var ip = WILDCARD
runCatching { runCatching {
Socket().use { Socket().use {
it.connect(InetSocketAddress("1.1.1.1", 80)) it.connect(InetSocketAddress("1.1.1.1", 80))
ip = it.localAddress.hostAddress ip = it.localAddress as Inet4Address
} }
}.onFailure { }.onFailure {
Common.logger.error("Unable to determine outbound traffic local address. Using loopback instead.", it) Common.logger.error("Unable to determine outbound traffic local address. Using loopback instead.", it)
@ -81,7 +88,7 @@ object IPv4 {
ip ip
} else { } else {
// everyone else works correctly // everyone else works correctly
"0.0.0.0" WILDCARD
} }
} }
@ -672,8 +679,8 @@ object IPv4 {
* *
* @return `String` containing the text-formatted IP address * @return `String` containing the text-formatted IP address
*/ */
fun toString(ip: InetAddress): String { fun toString(ip: Inet4Address): String {
return (ip as Inet4Address).hostAddress return ip.hostAddress
} }

View File

@ -17,6 +17,7 @@
package dorkbox.netUtil package dorkbox.netUtil
import java.net.Inet4Address
import java.net.Inet6Address import java.net.Inet6Address
import java.net.InetAddress import java.net.InetAddress
import java.net.UnknownHostException import java.net.UnknownHostException
@ -85,11 +86,18 @@ object IPv6 {
* The [Inet6Address] that represents the IPv6 loopback address '::1' * The [Inet6Address] that represents the IPv6 loopback address '::1'
*/ */
val LOCALHOST: Inet6Address by lazy { val LOCALHOST: Inet6Address by lazy {
// Create IPv6 loopback address. // Create IPv6 address, this will ALWAYS work
// should never fail
InetAddress.getByAddress("localhost", byteArrayOf(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1)) as Inet6Address 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. * 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 * @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) return toAddressString(ip.address, 0, ipv4Mapped)
} }

View File

@ -58,7 +58,7 @@ internal object PingResultBuilder {
listOf( listOf(
/* Windows */ /* Windows */
ResultParser.of("Pinging (.*) with") { result, matcher -> 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.ip = matcher.group(1)
result result
}, },

View File

@ -681,7 +681,7 @@ class NetUtilTest {
@Throws(UnknownHostException::class) @Throws(UnknownHostException::class)
fun testIp6AddressToString() { fun testIp6AddressToString() {
for ((key, value) in ipv6ToAddressStrings) { 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) @Throws(UnknownHostException::class)
fun testIp4AddressToString() { fun testIp4AddressToString() {
for ((key, value) in validIpV4Hosts) { for ((key, value) in validIpV4Hosts) {
assertEquals(key, IPv4.toString(InetAddress.getByAddress(unhex(value)))) assertEquals(key, IP.toString(InetAddress.getByAddress(unhex(value))))
} }
} }