Added checks to see if a network stack is available
parent
2872a46442
commit
0ea4ed19b2
|
@ -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.4"
|
||||
const val version = "1.5"
|
||||
|
||||
// set as project.ext
|
||||
const val name = "NetworkUtils"
|
||||
|
|
|
@ -22,7 +22,9 @@ import java.io.Writer
|
|||
import java.net.Inet4Address
|
||||
import java.net.InetAddress
|
||||
import java.net.InetSocketAddress
|
||||
import java.net.NetworkInterface
|
||||
import java.net.Socket
|
||||
import java.net.SocketException
|
||||
import java.util.*
|
||||
import kotlin.math.floor
|
||||
import kotlin.math.ln
|
||||
|
@ -47,6 +49,44 @@ object IPv4 {
|
|||
*/
|
||||
val isPreferred = Common.getBoolean("java.net.preferIPv4Stack", false)
|
||||
|
||||
/**
|
||||
* true if there is an IPv4 network interface available
|
||||
*/
|
||||
val isAvailable: Boolean by lazy {
|
||||
// Retrieve the list of available network interfaces.
|
||||
val netInterfaces = mutableListOf<NetworkInterface>()
|
||||
try {
|
||||
val interfaces = NetworkInterface.getNetworkInterfaces()
|
||||
if (interfaces != null) {
|
||||
while (interfaces.hasMoreElements()) {
|
||||
val iface: NetworkInterface = interfaces.nextElement()
|
||||
// Use the interface with proper INET addresses only.
|
||||
if (SocketUtils.addressesFromNetworkInterface(iface).hasMoreElements()) {
|
||||
netInterfaces.add(iface)
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (e: SocketException) {
|
||||
Common.logger.warn("Failed to retrieve the list of available network interfaces", e)
|
||||
}
|
||||
|
||||
// check if IPv4 is possible.
|
||||
var ipv4Possible = false
|
||||
loop@ for (iface in netInterfaces) {
|
||||
val i = SocketUtils.addressesFromNetworkInterface(iface)
|
||||
while (i.hasMoreElements()) {
|
||||
val addr: InetAddress = i.nextElement()
|
||||
if (addr is Inet4Address) {
|
||||
ipv4Possible = true
|
||||
break@loop
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ipv4Possible
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* The [Inet4Address] that represents the IPv4 loopback address '127.0.0.1'
|
||||
*/
|
||||
|
|
|
@ -20,6 +20,8 @@ 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
|
||||
|
||||
/**
|
||||
|
@ -82,6 +84,44 @@ object IPv6 {
|
|||
*/
|
||||
val isPreferred = Common.getBoolean("java.net.preferIPv6Addresses", false)
|
||||
|
||||
/**
|
||||
* true if there is an IPv6 network interface available
|
||||
*/
|
||||
val isAvailable: Boolean by lazy {
|
||||
// Retrieve the list of available network interfaces.
|
||||
val netInterfaces = mutableListOf<NetworkInterface>()
|
||||
try {
|
||||
val interfaces = NetworkInterface.getNetworkInterfaces()
|
||||
if (interfaces != null) {
|
||||
while (interfaces.hasMoreElements()) {
|
||||
val iface: NetworkInterface = interfaces.nextElement()
|
||||
// Use the interface with proper INET addresses only.
|
||||
if (SocketUtils.addressesFromNetworkInterface(iface).hasMoreElements()) {
|
||||
netInterfaces.add(iface)
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (e: SocketException) {
|
||||
Common.logger.warn("Failed to retrieve the list of available network interfaces", e)
|
||||
}
|
||||
|
||||
// check if IPv4 is possible.
|
||||
var ipv6Possible = false
|
||||
loop@ for (iface in netInterfaces) {
|
||||
val i = SocketUtils.addressesFromNetworkInterface(iface)
|
||||
while (i.hasMoreElements()) {
|
||||
val addr: InetAddress = i.nextElement()
|
||||
if (addr is Inet6Address) {
|
||||
ipv6Possible = true
|
||||
break@loop
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ipv6Possible
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* The [Inet6Address] that represents the IPv6 loopback address '::1'
|
||||
*/
|
||||
|
|
Loading…
Reference in New Issue