diff --git a/build.gradle.kts b/build.gradle.kts index 532465f..207ea5e 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -43,7 +43,7 @@ object Extras { // set for the project const val description = "Utilities for managing network configurations, IP/MAC address conversion, and ping (via OS native commands)" const val group = "com.dorkbox" - const val version = "1.3" + const val version = "1.4" // set as project.ext const val name = "NetworkUtils" diff --git a/src/dorkbox/netUtil/IP.kt b/src/dorkbox/netUtil/IP.kt index 03a9484..4c9cf29 100644 --- a/src/dorkbox/netUtil/IP.kt +++ b/src/dorkbox/netUtil/IP.kt @@ -1,7 +1,12 @@ package dorkbox.netUtil import dorkbox.netUtil.Common.logger -import java.net.* +import java.net.Inet4Address +import java.net.Inet6Address +import java.net.InetAddress +import java.net.InetSocketAddress +import java.net.NetworkInterface +import java.net.SocketException /** * A class that holds a number of network-related constants, also from: @@ -213,7 +218,7 @@ object IP { val sb: StringBuilder sb = if (addr.isUnresolved) { - val hostname = getHostname(addr) + val hostname = addr.hostString newSocketAddressStringBuilder(hostname, port, !IPv6.isValid(hostname)) } else { val address = addr.address @@ -257,19 +262,9 @@ object IP { */ fun getByName(ip: String): InetAddress? { return if (IPv4.isValid(ip)) { - IPv4.getByName(ip) + IPv4.getByNameUnsafe(ip) } else { IPv6.getByName(ip) } } - - /** - * Returns [InetSocketAddress.getHostString] if Java >= 7, - * or [InetSocketAddress.getHostName] otherwise. - * @param addr The address - * @return the host string - */ - fun getHostname(addr: InetSocketAddress): String { - return addr.hostString - } } diff --git a/src/dorkbox/netUtil/IPv4.kt b/src/dorkbox/netUtil/IPv4.kt index 35185ff..d77dc9a 100644 --- a/src/dorkbox/netUtil/IPv4.kt +++ b/src/dorkbox/netUtil/IPv4.kt @@ -727,6 +727,19 @@ object IPv4 { return address } + /** + * Returns the [Inet4Address] representation of a [String] IP address. + * + * This method will treat all IPv4 type addresses as "IPv4 mapped" (see [.getByName]) + * + * @param ip [String] IP address to be converted to a [Inet4Address] + * @return [Inet4Address] representation of the `ip` or `null` if not a valid IP address. + */ + fun getByNameUnsafe(ip: String): Inet4Address { + val asBytes = toBytes(ip) + return Inet4Address.getByAddress(ip, asBytes) as Inet4Address + } + /** * Returns the [Inet4Address] representation of a [String] IP address. * @@ -737,8 +750,7 @@ object IPv4 { */ fun getByName(ip: String): Inet4Address? { return if (isValid(ip)) { - val asBytes = toBytes(ip) - return Inet4Address.getByAddress(ip, asBytes) as Inet4Address + return getByNameUnsafe(ip) } else { null }