Added IPv 4/6 `toAddress`
parent
af23b76a26
commit
bfc66ef492
|
@ -20,7 +20,6 @@ package dorkbox.netUtil
|
|||
import java.io.IOException
|
||||
import java.io.Writer
|
||||
import java.net.*
|
||||
import java.util.*
|
||||
import kotlin.math.floor
|
||||
import kotlin.math.ln
|
||||
import kotlin.math.pow
|
||||
|
@ -295,6 +294,9 @@ object IPv4 {
|
|||
ipv4WordToByte(ip, i + 1, ip.length))
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an byte[] based on an ipAddressString. No error handling is performed here.
|
||||
*/
|
||||
fun toBytes(ip: String): ByteArray {
|
||||
var i: Int
|
||||
|
||||
|
@ -313,6 +315,24 @@ object IPv4 {
|
|||
return byteArrayOf(a, b, c, ipv4WordToByte(ip, i + 1, ip.length))
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an Inet4Address based on an ip string. No error handling is performed here.
|
||||
*/
|
||||
fun toAddress(ip: String): Inet4Address {
|
||||
return InetAddress.getByAddress(ip, toBytes(ip)) as Inet4Address
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an Inet4Address based on an ip string. No error handling is performed here.
|
||||
*/
|
||||
fun toAddressOrNull(ip: String): Inet4Address? {
|
||||
if (!isValid(ip)) {
|
||||
return null
|
||||
}
|
||||
|
||||
return InetAddress.getByAddress(ip, toBytes(ip)) as Inet4Address
|
||||
}
|
||||
|
||||
private fun decimalDigit(str: CharSequence, pos: Int): Int {
|
||||
return str[pos] - '0'
|
||||
}
|
||||
|
@ -334,9 +354,10 @@ object IPv4 {
|
|||
} else (ret * 10 + decimalDigit(ip, newFrom)).toByte()
|
||||
}
|
||||
|
||||
fun findFreeSubnet24(): ByteArray? {
|
||||
Common.logger.info("Scanning for available cidr...")
|
||||
|
||||
/**
|
||||
* Finds the first available subnet (as CIDR) with a corresponding gateway
|
||||
*/
|
||||
fun findFreeSubnet24Cidr(): ByteArray? {
|
||||
// have to find a free cidr
|
||||
// start with 10.x.x.x /24 and just march through starting at 0 -> 200 for each, ping to see if there is a gateway (should be)
|
||||
// and go from there.
|
||||
|
|
|
@ -17,12 +17,7 @@
|
|||
|
||||
package dorkbox.netUtil
|
||||
|
||||
import java.net.Inet4Address
|
||||
import java.net.Inet6Address
|
||||
import java.net.InetAddress
|
||||
import java.net.NetworkInterface
|
||||
import java.net.SocketException
|
||||
import java.net.UnknownHostException
|
||||
import java.net.*
|
||||
|
||||
/**
|
||||
* A class that holds a number of network-related constants, also from:
|
||||
|
@ -521,7 +516,7 @@ object IPv6 {
|
|||
}
|
||||
|
||||
/**
|
||||
* Creates an byte[] based on an ipAddressString. No error handling is performed here.
|
||||
* Creates an byte[] based on an ip string. No error handling is performed here.
|
||||
*/
|
||||
fun toBytesOrNull(ipAddress: String): ByteArray? {
|
||||
if (isValid(ipAddress)) {
|
||||
|
@ -530,8 +525,9 @@ object IPv6 {
|
|||
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an byte[] based on an ipAddressString. No error handling is performed here.
|
||||
* Creates an byte[] based on an ip string.
|
||||
*/
|
||||
fun toBytes(ipAddress: String): ByteArray {
|
||||
// always return a byte array
|
||||
|
@ -547,6 +543,23 @@ object IPv6 {
|
|||
return fromString(fixedIp)?.address ?: ByteArray(32)
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an Inet6Address based on an ipAddressString.
|
||||
*/
|
||||
fun toAddressOrNull(ip: String): Inet6Address? {
|
||||
if (!isValid(ip)) {
|
||||
return null
|
||||
}
|
||||
|
||||
return toAddress(ip)
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an Inet6Address based on an ipAddressString. No error handling is performed here.
|
||||
*/
|
||||
fun toAddress(ip: String): Inet6Address {
|
||||
return InetAddress.getByAddress(ip, toBytes(ip)) as Inet6Address
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the [String] representation of an [InetAddress].
|
||||
|
|
Loading…
Reference in New Issue