From 4927bfae3107b056bdf4772b8d1b7e558415d5e3 Mon Sep 17 00:00:00 2001 From: Gabriel Tofvesson Date: Sat, 7 Nov 2020 02:01:32 +0100 Subject: [PATCH] Refactor files --- .idea/vcs.xml | 6 +++++ NetObserve.iml | 12 ++++++++++ .../link/{EthernetPacket.kt => MACPacket.kt} | 2 +- .../DatagramProtocol.kt | 2 +- .../ExplicitCongestionNotification.kt | 2 +- .../{transport => network}/IP4Packet.kt | 22 +++++++++++++------ .../{transport => network}/IPAddress.kt | 2 +- 7 files changed, 37 insertions(+), 11 deletions(-) create mode 100644 .idea/vcs.xml rename src/dev/w1zzrd/packets/link/{EthernetPacket.kt => MACPacket.kt} (95%) rename src/dev/w1zzrd/packets/{transport => network}/DatagramProtocol.kt (98%) rename src/dev/w1zzrd/packets/{transport => network}/ExplicitCongestionNotification.kt (73%) rename src/dev/w1zzrd/packets/{transport => network}/IP4Packet.kt (88%) rename src/dev/w1zzrd/packets/{transport => network}/IPAddress.kt (95%) diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/NetObserve.iml b/NetObserve.iml index 245d342..3d7162a 100644 --- a/NetObserve.iml +++ b/NetObserve.iml @@ -1,5 +1,17 @@ + + + + + + + + + + diff --git a/src/dev/w1zzrd/packets/link/EthernetPacket.kt b/src/dev/w1zzrd/packets/link/MACPacket.kt similarity index 95% rename from src/dev/w1zzrd/packets/link/EthernetPacket.kt rename to src/dev/w1zzrd/packets/link/MACPacket.kt index a86d572..ec27a2b 100644 --- a/src/dev/w1zzrd/packets/link/EthernetPacket.kt +++ b/src/dev/w1zzrd/packets/link/MACPacket.kt @@ -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, diff --git a/src/dev/w1zzrd/packets/transport/DatagramProtocol.kt b/src/dev/w1zzrd/packets/network/DatagramProtocol.kt similarity index 98% rename from src/dev/w1zzrd/packets/transport/DatagramProtocol.kt rename to src/dev/w1zzrd/packets/network/DatagramProtocol.kt index acc124a..15c3cb0 100644 --- a/src/dev/w1zzrd/packets/transport/DatagramProtocol.kt +++ b/src/dev/w1zzrd/packets/network/DatagramProtocol.kt @@ -1,4 +1,4 @@ -package dev.w1zzrd.packets.transport +package dev.w1zzrd.packets.network enum class DatagramProtocol { HOPOPT, diff --git a/src/dev/w1zzrd/packets/transport/ExplicitCongestionNotification.kt b/src/dev/w1zzrd/packets/network/ExplicitCongestionNotification.kt similarity index 73% rename from src/dev/w1zzrd/packets/transport/ExplicitCongestionNotification.kt rename to src/dev/w1zzrd/packets/network/ExplicitCongestionNotification.kt index 69b8a1e..8491dfa 100644 --- a/src/dev/w1zzrd/packets/transport/ExplicitCongestionNotification.kt +++ b/src/dev/w1zzrd/packets/network/ExplicitCongestionNotification.kt @@ -1,4 +1,4 @@ -package dev.w1zzrd.packets.transport +package dev.w1zzrd.packets.network enum class ExplicitCongestionNotification { INCAPABLE, CAPABLE0, CAPABLE1, CONGESTION_ENCOUNTERED diff --git a/src/dev/w1zzrd/packets/transport/IP4Packet.kt b/src/dev/w1zzrd/packets/network/IP4Packet.kt similarity index 88% rename from src/dev/w1zzrd/packets/transport/IP4Packet.kt rename to src/dev/w1zzrd/packets/network/IP4Packet.kt index 49ff554..0b8b238 100644 --- a/src/dev/w1zzrd/packets/transport/IP4Packet.kt +++ b/src/dev/w1zzrd/packets/network/IP4Packet.kt @@ -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 UInt.toEnum(): T where T: Enum { - val values = (T::class.functions.first { it.name == "values" }.call() as Array) + 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 by optionIndices { + inner class IP4Options: Collection { + 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) = optionIndices.containsAll(elements) + override fun isEmpty() = optionIndices.isEmpty() + override fun iterator() = optionIndices.iterator() } // IPv4 option field diff --git a/src/dev/w1zzrd/packets/transport/IPAddress.kt b/src/dev/w1zzrd/packets/network/IPAddress.kt similarity index 95% rename from src/dev/w1zzrd/packets/transport/IPAddress.kt rename to src/dev/w1zzrd/packets/network/IPAddress.kt index f08a991..9b182a1 100644 --- a/src/dev/w1zzrd/packets/transport/IPAddress.kt +++ b/src/dev/w1zzrd/packets/network/IPAddress.kt @@ -1,4 +1,4 @@ -package dev.w1zzrd.packets.transport +package dev.w1zzrd.packets.network import java.nio.ByteBuffer