Added isPrivate check for ipv4

connection_type_change
nathan 2020-08-11 16:24:02 +02:00
parent b7d62e4242
commit 0074adf5ea
1 changed files with 29 additions and 0 deletions

View File

@ -87,6 +87,35 @@ object IPv4 {
private val SLASH_REGEX = "\\.".toRegex()
private val private10 = toInt("10.0.0.0")
private val private172 = toInt("172.16.0.0")
private val private192 = toInt("192.168.0.0")
/**
* Determines if this IP address is a private address or not.
*
* Private addresses are defined as
* 10.0.0.0/8
* 172.16.0.0/12
* 192.168.0.0/16
*/
fun isPrivate(ipAsString: String): Boolean {
return isPrivate(toInt(ipAsString))
}
/**
* Determines if this IP address is a private address or not.
*
* Private addresses are defined as
* 10.0.0.0/8
* 172.16.0.0/12
* 192.168.0.0/16
*/
fun isPrivate(ipAsInt: Int): Boolean {
// check from smaller to larger
return isInRange(ipAsInt, private192, 16) || isInRange(ipAsInt, private172, 12) || isInRange(ipAsInt, private10, 8)
}
/**
* Determine whether a given string is a valid CIDR IP address. Accepts only 1.2.3.4/24
*