Refactor files

This commit is contained in:
Gabriel Tofvesson 2020-11-07 02:01:32 +01:00
parent d6444d8c60
commit 4927bfae31
7 changed files with 37 additions and 11 deletions

6
.idea/vcs.xml generated Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

View File

@ -1,5 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="FacetManager">
<facet type="kotlin-language" name="Kotlin">
<configuration version="3" platform="JVM 1.8" allPlatforms="JVM [1.8]" useProjectSettings="false">
<compilerSettings>
<option name="additionalArguments" value="-version -Xopt-in=kotlin.ExperimentalUnsignedTypes" />
</compilerSettings>
<compilerArguments>
<option name="jvmTarget" value="1.8" />
</compilerArguments>
</configuration>
</facet>
</component>
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">

View File

@ -3,7 +3,7 @@ package dev.w1zzrd.packets.link
import dev.w1zzrd.packets.Packet
import java.nio.ByteOrder
class EthernetPacket: Packet {
class MACPacket: Packet {
constructor(
data: ByteArray,
offset: UInt,

View File

@ -1,4 +1,4 @@
package dev.w1zzrd.packets.transport
package dev.w1zzrd.packets.network
enum class DatagramProtocol {
HOPOPT,

View File

@ -1,4 +1,4 @@
package dev.w1zzrd.packets.transport
package dev.w1zzrd.packets.network
enum class ExplicitCongestionNotification {
INCAPABLE, CAPABLE0, CAPABLE1, CONGESTION_ENCOUNTERED

View File

@ -1,17 +1,17 @@
package dev.w1zzrd.packets.transport
package dev.w1zzrd.packets.network
import dev.w1zzrd.packets.Packet
import dev.w1zzrd.packets.transport.DSCPType.Companion.getDSCPType
import dev.w1zzrd.packets.transport.DatagramProtocol.Companion.toDatagramProtocol
import dev.w1zzrd.packets.transport.IP4OptionType.Companion.toIP4OptionType
import dev.w1zzrd.packets.network.DSCPType.Companion.getDSCPType
import dev.w1zzrd.packets.network.DatagramProtocol.Companion.toDatagramProtocol
import dev.w1zzrd.packets.network.IP4OptionType.Companion.toIP4OptionType
import java.nio.ByteOrder
import kotlin.experimental.and
import kotlin.reflect.full.functions
inline fun <reified T> UInt.toEnum(): T where T: Enum<T> {
val values = (T::class.functions.first { it.name == "values" }.call() as Array<T>)
val values = (T::class.functions.first { it.name == "values" }.call() as Array<*>)
if (this < values.size.toUInt()) return values[this.toInt()]
if (this < values.size.toUInt()) return values[this.toInt()] as T
else throw IllegalArgumentException("Index out of bounds")
}
@ -95,7 +95,8 @@ class IP4Packet: Packet {
}
// IPv4 options field parser
inner class IP4Options(private var optionIndices: UIntArray = UIntArray(0)): Collection<UInt> by optionIndices {
inner class IP4Options: Collection<UInt> {
private val optionIndices: UIntArray
init {
if (headerLength == 20u) optionIndices = UIntArray(0)
else {
@ -123,6 +124,13 @@ class IP4Packet: Packet {
return IP4Option(optionIndices[index])
}
// Stupid delegation stuff
override val size by optionIndices::size
override fun contains(element: UInt) = optionIndices.contains(element)
override fun containsAll(elements: Collection<UInt>) = optionIndices.containsAll(elements)
override fun isEmpty() = optionIndices.isEmpty()
override fun iterator() = optionIndices.iterator()
}
// IPv4 option field

View File

@ -1,4 +1,4 @@
package dev.w1zzrd.packets.transport
package dev.w1zzrd.packets.network
import java.nio.ByteBuffer