diff --git a/src/dorkbox/netUtil/Inet4.kt b/src/dorkbox/netUtil/Inet4.kt new file mode 100644 index 0000000..24bd72d --- /dev/null +++ b/src/dorkbox/netUtil/Inet4.kt @@ -0,0 +1,47 @@ +/* + * Copyright 2021 dorkbox, llc + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dorkbox.netUtil + +import java.net.Inet4Address +import java.net.InetAddress +import java.net.UnknownHostException + +object Inet4 { + /** + * Gets the version number. + */ + const val version = "2.6" + + /** + * Returns the [Inet4Address] representation of a [String] IP or host address. + * + * @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 toAddress(ip: String): Inet4Address? { + return if (IPv4.isValid(ip)) { + IPv4.toAddressUnsafe(ip) + } else { + // maybe this is a host name and not an IP address? + try { + InetAddress.getAllByName(ip).find { it is Inet4Address } as Inet4Address + } catch (e: UnknownHostException) { + null + } + } + } +} diff --git a/src/dorkbox/netUtil/Inet6.kt b/src/dorkbox/netUtil/Inet6.kt new file mode 100644 index 0000000..235f35b --- /dev/null +++ b/src/dorkbox/netUtil/Inet6.kt @@ -0,0 +1,55 @@ +/* + * Copyright 2021 dorkbox, llc + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dorkbox.netUtil + +import java.net.Inet6Address +import java.net.InetAddress +import java.net.UnknownHostException + +object Inet6 { + /** + * Gets the version number. + */ + const val version = "2.6" + + /** + * Returns the [Inet6Address] representation of a [String] IP or host address. + *

+ * The [ipv4Mapped] parameter specifies how IPv4 addresses should be treated. The "IPv4 mapped" format is + * defined in rfc 4291 section 2 is supported. + * + * @param ip [String] IP address to be converted to a [Inet6Address] + * @param ipv4Mapped + * + * * [true] To allow IPv4 mapped inputs to be translated into {@link Inet6Address} + * * [false] Consider IPv4 mapped addresses as invalid. + * + * @return [Inet6Address] representation of the [ip] or [null] if not a valid IP address. + */ + fun toAddress(ip: String, ipv4Mapped: Boolean = true): Inet6Address? { + return if (IPv6.isValid(ip)) { + IPv6.toAddress(ip, ipv4Mapped) + } else { + // maybe this is a host name and not an IP address? + try { + InetAddress.getAllByName(ip).find { it is Inet6Address } as Inet6Address + } catch (e: UnknownHostException) { + null + } + } + } +}