Added version info to individual classes, added more unit tests

connection_type_change
Robinson 2021-04-06 23:42:45 +02:00
parent fa42710ae8
commit 8f9294f295
21 changed files with 253 additions and 89 deletions

50
LICENSE
View File

@ -1,7 +1,7 @@
- NetworkUtils - Utilities for managing network configurations, IP/MAC address conversion, and ping (via OS native commands)
[The Apache Software License, Version 2.0]
https://git.dorkbox.com/dorkbox/NetworkUtils
Copyright 2020
Copyright 2021
Dorkbox LLC
Extra license information
@ -22,7 +22,7 @@
- Kotlin -
[The Apache Software License, Version 2.0]
https://github.com/JetBrains/kotlin
Copyright 1980
Copyright 2020
JetBrains s.r.o. and Kotlin Programming Language contributors
Kotlin Compiler, Test Data+Libraries, and Tools repository contain third-party code, to which different licenses may apply
See: https://github.com/JetBrains/kotlin/blob/master/license/README.md
@ -30,19 +30,31 @@
- kotlin-logging - Lightweight logging framework for Kotlin
[The Apache Software License, Version 2.0]
https://github.com/MicroUtils/kotlin-logging
Copyright 2020
Copyright 2021
Ohad Shai
- SLF4J - Simple facade or abstraction for various logging frameworks
[MIT License]
http://www.slf4j.org
Copyright 2019
Copyright 2021
QOS.ch
- Executor - Shell, JVM, and SSH command execution on Linux, MacOS, or Windows for Java 11+
- JNA - Simplified native library access for Java.
[The Apache Software License, Version 2.0]
https://github.com/twall/jna
Copyright 2021
Timothy Wall
- JNA-Platform - Mappings for a number of commonly used platform functions
[The Apache Software License, Version 2.0]
https://github.com/twall/jna
Copyright 2021
Timothy Wall
- Executor - Shell, JVM, and SSH command execution on Linux, MacOS, or Windows for Java 8+
[The Apache Software License, Version 2.0]
https://git.dorkbox.com/dorkbox/Executor
Copyright 2020
Copyright 2021
Dorkbox LLC
Extra license information
@ -58,36 +70,34 @@
Copyright 2014
The Apache Software Foundation
- Kotlin -
- Logback - Logback is a logging framework for Java applications
[The Apache Software License, Version 2.0]
https://github.com/JetBrains/kotlin
Copyright 1980
JetBrains s.r.o. and Kotlin Programming Language contributors
Kotlin Compiler, Test Data+Libraries, and Tools repository contain third-party code, to which different licenses may apply
See: https://github.com/JetBrains/kotlin/blob/master/license/README.md
http://logback.qos.ch
Copyright 2021
QOS.ch
- kotlinx.coroutines - Library support for Kotlin coroutines with multiplatform support
[The Apache Software License, Version 2.0]
https://github.com/Kotlin/kotlinx.coroutines
Copyright 2020
Copyright 2021
JetBrains s.r.o.
- kotlin-logging - Lightweight logging framework for Kotlin
[The Apache Software License, Version 2.0]
https://github.com/MicroUtils/kotlin-logging
Copyright 2020
Copyright 2021
Ohad Shai
- SLF4J - Simple facade or abstraction for various logging frameworks
[MIT License]
http://www.slf4j.org
Copyright 2019
Copyright 2021
QOS.ch
- SSHJ - SSHv2 library for Java
[The Apache Software License, Version 2.0]
https://github.com/hierynomus/sshj
Copyright 2020
Copyright 2021
Jeroen van Erp
SSHJ Contributors
@ -117,3 +127,11 @@
[Public Domain, per Creative Commons CC0]
https://github.com/str4d/ed25519-java
https://github.com/str4d
- Kotlin -
[The Apache Software License, Version 2.0]
https://github.com/JetBrains/kotlin
Copyright 2020
JetBrains s.r.o. and Kotlin Programming Language contributors
Kotlin Compiler, Test Data+Libraries, and Tools repository contain third-party code, to which different licenses may apply
See: https://github.com/JetBrains/kotlin/blob/master/license/README.md

View File

@ -30,20 +30,20 @@ gradle.startParameter.warningMode = WarningMode.All
plugins {
java
id("com.dorkbox.GradleUtils") version "1.10"
id("com.dorkbox.Licensing") version "2.2"
id("com.dorkbox.VersionUpdate") version "2.0"
id("com.dorkbox.GradlePublish") version "1.6"
id("com.dorkbox.GradleModuleInfo") version "1.0"
id("com.dorkbox.GradleUtils") version "1.16"
id("com.dorkbox.Licensing") version "2.5.5"
id("com.dorkbox.VersionUpdate") version "2.1"
id("com.dorkbox.GradlePublish") version "1.10"
// id("com.dorkbox.GradleModuleInfo") version "1.1"
kotlin("jvm") version "1.4.0"
kotlin("jvm") version "1.4.32"
}
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 = "2.0"
const val version = "2.1"
// set as project.ext
const val name = "NetworkUtils"
@ -61,7 +61,7 @@ object Extras {
GradleUtils.load("$projectDir/../../gradle.properties", Extras)
GradleUtils.fixIntellijPaths()
GradleUtils.defaultResolutionStrategy()
GradleUtils.compileConfiguration(JavaVersion.VERSION_11)
GradleUtils.compileConfiguration(JavaVersion.VERSION_1_8)
licensing {
@ -69,12 +69,14 @@ licensing {
description(Extras.description)
url(Extras.url)
author(Extras.vendor)
extra("Netty", License.APACHE_2) {
it.copyright(2014)
it.author("The Netty Project")
it.url("https://netty.io/")
it.note("This product contains a modified portion of Netty Network Utils")
}
extra("Apache Harmony", License.APACHE_2) {
it.copyright(2010)
it.author("The Apache Software Foundation")
@ -143,15 +145,20 @@ tasks.jar.get().apply {
}
dependencies {
implementation(kotlin("stdlib-jdk8"))
// https://github.com/MicroUtils/kotlin-logging
implementation("io.github.microutils:kotlin-logging:1.8.3") // slick kotlin wrapper for slf4j
implementation("io.github.microutils:kotlin-logging:2.0.6") // slick kotlin wrapper for slf4j
implementation("org.slf4j:slf4j-api:1.7.30")
implementation("com.dorkbox:Executor:1.1")
testImplementation("junit:junit:4.13")
implementation("com.dorkbox:Executor:3.0")
val jnaVersion = "5.8.0"
implementation("net.java.dev.jna:jna:$jnaVersion")
implementation("net.java.dev.jna:jna-platform:$jnaVersion")
testImplementation("junit:junit:4.13.2")
testImplementation("ch.qos.logback:logback-classic:1.2.3")
testImplementation("com.dorkbox:Utilities:1.6")
testImplementation("com.dorkbox:Utilities:1.9")
}
publishToSonatype {

View File

@ -4,6 +4,11 @@ import dorkbox.executor.Executor
import java.io.File
object Arp {
/**
* Gets the version number.
*/
const val version = "2.1"
// Now setup ARP Proxy for this interface (so ARP requests are answered correctly)
fun proxyAdd(interfaceName: String) {
if (Common.OS_LINUX) {

View File

@ -7,6 +7,11 @@ import java.io.File
*
*/
object Dhcp {
/**
* Gets the version number.
*/
const val version = "2.1"
fun start(id: String, interfaceName: String) {
if (Common.OS_LINUX) {
stop(id, interfaceName)

View File

@ -19,6 +19,11 @@ import javax.naming.directory.InitialDirContext
object Dns {
/**
* Gets the version number.
*/
const val version = "2.1"
/**
* @throws IOException if the DNS resolve.conf file cannot be read
*/

View File

@ -17,6 +17,11 @@ import java.util.regex.Pattern
*/
object IP {
/**
* Gets the version number.
*/
const val version = "2.1"
/**
* The [InetAddress] that represents the loopback address. If IPv6 stack is available, it will refer to
* [.LOCALHOST6]. Otherwise, [.LOCALHOST4].
@ -247,7 +252,7 @@ object IP {
*
* @return the public IP address if found, or null if it didn't find it
*/
fun getPublicIpViaHttp(): String? {
fun publicIpViaHttp(): String? {
// method 1: use DNS servers
// dig +short myip.opendns.com @resolver1.opendns.com

View File

@ -6,10 +6,12 @@ import java.io.FileWriter
import java.io.IOException
import java.util.*
/**
*
*/
object IPRoute {
/**
* Gets the version number.
*/
const val version = "2.1"
private val reservedTable = StringBuilder(2048)
private val tableNames: MutableMap<Int, String?> = HashMap(256)

View File

@ -36,6 +36,11 @@ import kotlin.math.pow
*/
@Suppress("EXPERIMENTAL_API_USAGE")
object IPv4 {
/**
* Gets the version number.
*/
const val version = "2.1"
/**
* Returns `true` if IPv4 should be used even if the system supports both IPv4 and IPv6. Setting this
* property to `true` will disable IPv6 support. The default value of this property is `false`.
@ -278,7 +283,7 @@ object IPv4 {
}
fun toBytesorNull(ip: String): ByteArray? {
fun toBytesOrNull(ip: String): ByteArray? {
if (!isValid(ip)) {
return null
}

View File

@ -34,6 +34,11 @@ import java.net.UnknownHostException
* (http://svn.apache.org/repos/asf/harmony/enhanced/java/branches/java6/classlib/modules/luni/src/main/java/org/apache/harmony/luni/util/Inet6Util.java) which was part of Apache Harmony.
*/
object IPv6 {
/**
* Gets the version number.
*/
const val version = "2.1"
/**
* Maximum amount of value adding characters in between IPV6 separators
*/

View File

@ -6,6 +6,11 @@ import dorkbox.executor.Executor
*
*/
object IfConfig {
/**
* Gets the version number.
*/
const val version = "2.1"
fun assignMac(interfaceName: String, interfaceMac: String) {
if (Common.OS_LINUX) {
Executor.run("/sbin/ifconfig", interfaceName, "hw", "ether", interfaceMac)

View File

@ -7,6 +7,11 @@ import java.util.*
*
*/
object Iface {
/**
* Gets the version number.
*/
const val version = "2.1"
private val ifToIp: MutableMap<String, String> = HashMap()
/**

View File

@ -16,6 +16,11 @@ import java.util.regex.Pattern
*
*/
object Mac {
/**
* Gets the version number.
*/
const val version = "2.1"
enum class MacDelimiter(val delimiter: String) {
COLON(":"),
PERIOD("."),

View File

@ -8,6 +8,11 @@ import java.util.*
*
*/
object NameSpace {
/**
* Gets the version number.
*/
const val version = "2.1"
object Route {
fun flush(nameSpace: String) {
if (Common.OS_LINUX) {

View File

@ -6,6 +6,11 @@ import dorkbox.executor.Executor
*
*/
object Route {
/**
* Gets the version number.
*/
const val version = "2.1"
fun flush() {
Executor.run("/sbin/ip", "route", "flush", "cache")
}

View File

@ -7,27 +7,35 @@ import java.util.*
import java.util.concurrent.TimeUnit
/**
* SNTP client for retrieving time.
*
* https://tools.ietf.org/html/rfc2030
*
* Sample usage:
* ```
* val adjustedSystemTime = Sntp.update("time.mit.edu").now
*```
*/
object Sntp {
/**
* Simple SNTP client class for retrieving network time.
* Gets the version number.
*/
const val version = "2.1"
/**
* SNTP client for retrieving time.
*
* https://tools.ietf.org/html/rfc2030
*
* Sample usage:
* <pre>SntpClient client = new SntpClient();
* if (client.requestTime("time.foo.com")) {
* long now = client.getNtpTime() + SystemClock.elapsedRealtime() - client.getNtpTimeReference();
* }
</pre> *
* ```
* val adjustedSystemTime = Sntp.update("time.mit.edu").now
*```
*/
class SntpClient {
companion object {
private class InvalidServerReplyException(message: String) : Exception(message)
private const val TAG = "SntpClient"
private const val DBG = true
private const val REFERENCE_TIME_OFFSET = 16
private const val ORIGINATE_TIME_OFFSET = 24
private const val RECEIVE_TIME_OFFSET = 32
@ -211,6 +219,12 @@ object Sntp {
var localClockOffset: Long = 0
private set
/**
* adjusted system time (via values from the NTP server) in milliseconds since January 1, 1970
*/
val now: Long
get() = System.currentTimeMillis() + this.localClockOffset
/**
* Sends an SNTP request to the given host and processes the response.
*
@ -299,7 +313,7 @@ object Sntp {
roundTripDelay = (destinationTimestamp - originateTimestamp) - (transmitTimestamp - receiveTimestamp)
localClockOffset = ((receiveTimestamp - originateTimestamp) + (transmitTimestamp - destinationTimestamp)) / 2L
} catch (e: Exception) {
System.err.println("Error with NTP to $address. $e")
System.err.println("Error with NTP to $address. $e")
} finally {
socket?.close()
}
@ -387,24 +401,4 @@ object Sntp {
fun update(server: String, timeoutInMS: Int = 10_000): SntpClient {
return SntpClient().requestTime(server, timeoutInMS)
}
// @JvmStatic
// fun main(args: Array<String>) {
// val ntp = update("time.mit.edu")
//
// // Display response
// val now = System.currentTimeMillis()
// val cor = now + ntp.localClockOffset
//
// System.out.printf ("Originate time: %1\$ta, %1\$td %1\$tb %1\$tY, %1\$tI:%1\$tm:%1\$tS.%1\$tL %1\$tp %1\$tZ%n", ntp.originateTimestamp);
// System.out.printf ("Receive time: %1\$ta, %1\$td %1\$tb %1\$tY, %1\$tI:%1\$tm:%1\$tS.%1\$tL %1\$tp %1\$tZ%n", ntp.receiveTimestamp)
// System.out.printf ("Transmit time: %1\$ta, %1\$td %1\$tb %1\$tY, %1\$tI:%1\$tm:%1\$tS.%1\$tL %1\$tp %1\$tZ%n", ntp.transmitTimestamp)
// System.out.printf ("Destination time: %1\$ta, %1\$td %1\$tb %1\$tY, %1\$tI:%1\$tm:%1\$tS.%1\$tL %1\$tp %1\$tZ%n", ntp.destinationTimestamp)
//
// System.out.println("RoundTripDelay : ${ntp.roundTripDelay}")
// System.out.println("ClockOffset : ${ntp.localClockOffset}")
//
// System.out.printf ("Local time: %1\$ta, %1\$td %1\$tb %1\$tY, %1\$tI:%1\$tm:%1\$tS.%1\$tL %1\$tp %1\$tZ%n", now)
// System.out.printf ("Corrected time: %1\$ta, %1\$td %1\$tb %1\$tY, %1\$tI:%1\$tm:%1\$tS.%1\$tL %1\$tp %1\$tZ%n", cor)
// }
}

View File

@ -16,15 +16,7 @@
package dorkbox.netUtil
import java.io.IOException
import java.net.InetAddress
import java.net.InetSocketAddress
import java.net.NetworkInterface
import java.net.ServerSocket
import java.net.Socket
import java.net.SocketAddress
import java.net.SocketException
import java.net.SocketPermission
import java.net.UnknownHostException
import java.net.*
import java.nio.channels.DatagramChannel
import java.nio.channels.ServerSocketChannel
import java.nio.channels.SocketChannel
@ -42,6 +34,11 @@ import java.util.*
* the appropriate [SocketPermission].
*/
object SocketUtils {
/**
* Gets the version number.
*/
const val version = "2.1"
private val EMPTY = Collections.enumeration(emptyList<Any>())
@Suppress("UNCHECKED_CAST")

View File

@ -6,6 +6,11 @@ import dorkbox.executor.Executor
*
*/
object VirtualEth {
/**
* Gets the version number.
*/
const val version = "2.1"
fun add(host: String, guest: String) {
if (Common.OS_LINUX) {
Executor().command("/sbin/ip", "link", "add", "name", host, "type", "veth", "peer", "name", guest).startBlocking()

View File

@ -12,6 +12,11 @@ import java.time.Duration
class Ping {
companion object {
private val logger = LoggerFactory.getLogger(Ping::class.java.simpleName)
/**
* Gets the version number.
*/
const val version = "2.1"
}
private val count = 4

View File

@ -0,0 +1,48 @@
/*
* Copyright 2012 The Netty Project
*
* The Netty Project licenses this file to you 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 org.junit.Assert
import org.junit.Test
class DnsTest {
@Test
fun testLanAddress() {
Assert.assertNotNull(IP.lanAddress())
}
@Test
fun testPublicViaHttpAddress() {
val publicIpViaHttp = IP.publicIpViaHttp()
Assert.assertNotNull(publicIpViaHttp)
// println(IP.fromString(publicIpViaHttp!!))
}
@Test
fun testDnsNameServers() {
Assert.assertTrue(Dns.defaultNameServers.isNotEmpty())
}
@Test
fun testDnsNdots() {
Assert.assertTrue(Dns.numberDots >= 1)
}
@Test
fun testLocalhost() {
Assert.assertNotNull(IP.LOCALHOST)
}
}

View File

@ -24,7 +24,7 @@ import java.net.InetAddress
import java.net.InetSocketAddress
import java.net.UnknownHostException
class NetUtilTest {
class IpValidationTests {
companion object {
private val validIpV4Hosts = mapOf(
// "192.168.1.0" to "c0a80100",
@ -581,21 +581,6 @@ class NetUtilTest {
}
}
@Test
fun testLanAddress() {
Assert.assertNotNull(IP.lanAddress())
}
@Test
fun testDnsNameServers() {
Assert.assertTrue(Dns.defaultNameServers.isNotEmpty())
}
@Test
fun testDnsNdots() {
Assert.assertTrue(Dns.numberDots >= 1)
}
@Test
fun testLocalhost() {
Assert.assertNotNull(IP.LOCALHOST)
@ -668,7 +653,7 @@ class NetUtilTest {
assertHexDumpEquals(value, IPv4.toBytes(ip), ip)
}
for ((ip, value) in invalidIpV4Hosts) {
assertHexDumpEquals(value, IPv4.toBytesorNull(ip), ip)
assertHexDumpEquals(value, IPv4.toBytesOrNull(ip), ip)
}
for ((ip, value) in validIpV6Hosts) {
assertHexDumpEquals(value, IPv6.toBytes(ip), ip)

View File

@ -0,0 +1,48 @@
/*
* Copyright 2012 The Netty Project
*
* The Netty Project licenses this file to you 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 org.junit.Assert
import org.junit.Test
class SntpTest {
@Test
fun testToString() {
val ntp = Sntp.update("time.mit.edu")
// simple test that we got something
Assert.assertTrue(ntp.roundTripDelay > 0)
// Display response
val now = System.currentTimeMillis()
val cor = now + ntp.localClockOffset
val cor2 = ntp.now
System.out.printf ("Originate time: %1\$ta, %1\$td %1\$tb %1\$tY, %1\$tI:%1\$tm:%1\$tS.%1\$tL %1\$tp %1\$tZ%n", ntp.originateTimestamp);
System.out.printf ("Receive time: %1\$ta, %1\$td %1\$tb %1\$tY, %1\$tI:%1\$tm:%1\$tS.%1\$tL %1\$tp %1\$tZ%n", ntp.receiveTimestamp)
System.out.printf ("Transmit time: %1\$ta, %1\$td %1\$tb %1\$tY, %1\$tI:%1\$tm:%1\$tS.%1\$tL %1\$tp %1\$tZ%n", ntp.transmitTimestamp)
System.out.printf ("Destination time: %1\$ta, %1\$td %1\$tb %1\$tY, %1\$tI:%1\$tm:%1\$tS.%1\$tL %1\$tp %1\$tZ%n", ntp.destinationTimestamp)
System.out.println("RoundTripDelay : ${ntp.roundTripDelay}")
System.out.println("ClockOffset : ${ntp.localClockOffset}")
System.out.printf ("Local time: %1\$ta, %1\$td %1\$tb %1\$tY, %1\$tI:%1\$tm:%1\$tS.%1\$tL %1\$tp %1\$tZ%n", now)
System.out.printf ("Corrected time: %1\$ta, %1\$td %1\$tb %1\$tY, %1\$tI:%1\$tm:%1\$tS.%1\$tL %1\$tp %1\$tZ%n", cor)
System.out.printf ("Corrected time2: %1\$ta, %1\$td %1\$tb %1\$tY, %1\$tI:%1\$tm:%1\$tS.%1\$tL %1\$tp %1\$tZ%n", cor2)
Assert.assertEquals(cor, cor2)
}
}