Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor fixes #14

Merged
merged 3 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,19 @@ class EthernetHeader(
* ethertype, it will just leave the type as 0xFFFF (detect) which maps to RESERVED in the
* actual mapping.
*/
fun getEtherTypeFromIPVersionByte(ipVersion: UByte): EtherType =
when (ipVersion) {
fun getEtherTypeFromIPVersionByte(ipVersion: UByte): EtherType {
// in both ipv4 and ipv6, the version portion of the byte is actually the high 4 bits of
// the byte, so we need to zero out the bottom half and shift it right 4 bits
val shiftedVersion = ((ipVersion and 0xF0.toUByte()).toUInt() shr 4).toUByte()
return when (shiftedVersion) {
IP4_VERSION -> EtherType.IPv4
IP6_VERSION -> EtherType.IPv6
else -> {
logger.warn("Couldn't detect etherType, got $ipVersion")
EtherType.DETECT
}
}
}

fun dummyEthernet(etherType: EtherType): EthernetHeader =
EthernetHeader(MacAddress.DUMMY_MAC_SOURCE, MacAddress.DUMMY_MAC_DEST, etherType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class StringPacketDumper(
fun dumpBufferToString(
buffer: ByteBuffer,
offset: Int = 0,
length: Int = 0,
length: Int = buffer.remaining(),
addresses: Boolean = false,
etherType: EtherType? = null,
): String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ class TestEthernetHeader {

@Test fun etherTypeDetection() {
val ipv4Buffer = ByteBuffer.allocate(1)
ipv4Buffer.put(0, EthernetHeader.IP4_VERSION.toByte())
// need to shift left because its the high 4 bits of the byte that contain the version
ipv4Buffer.put(0, (EthernetHeader.IP4_VERSION.toUInt() shl 4).toByte())
ipv4Buffer.rewind()
assertEquals(EtherType.IPv4, EthernetHeader.getEtherTypeFromIPVersionByte(ipv4Buffer.get().toUByte()))

val ipv6Buffer = ByteBuffer.allocate(1)
ipv6Buffer.put(0, EthernetHeader.IP6_VERSION.toByte())
// need to shift left because its the high 4 bits of the byte that contain the version
ipv6Buffer.put(0, (EthernetHeader.IP6_VERSION.toUInt() shl 4).toByte())
ipv6Buffer.rewind()
assertEquals(EtherType.IPv6, EthernetHeader.getEtherTypeFromIPVersionByte(ipv6Buffer.get().toUByte()))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -675,4 +675,14 @@ class TestStringPacketDumper {
val hexString = stringPacketDumper.dumpBufferToString(buffer, 0, buffer.limit() - 2, false)
assertEquals("00 01 02", hexString)
}

@Test fun testTypeDetection() {
val bufferipv4 = ByteBuffer.wrap(byteArrayOf(0x45, 0x01, 0x02))
val hexStringv4 = stringPacketDumper.dumpBufferToString(bufferipv4, etherType = EtherType.DETECT)
assertEquals("14 C0 3E 55 0B 35 74 D0 2B 29 A5 18 08 00 45 01\n02", hexStringv4)

val bufferipv6 = ByteBuffer.wrap(byteArrayOf(0x60, 0x01, 0x02))
val hexStringv6 = stringPacketDumper.dumpBufferToString(bufferipv6, etherType = EtherType.DETECT)
assertEquals("14 C0 3E 55 0B 35 74 D0 2B 29 A5 18 86 DD 60 01\n02", hexStringv6)
}
}