Skip to content

Commit

Permalink
Merge pull request #14 from compscidr/jason/minor-fixes
Browse files Browse the repository at this point in the history
Minor fixes
  • Loading branch information
compscidr authored Aug 21, 2024
2 parents 54bded6 + e12af8b commit 6238840
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
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)
}
}

0 comments on commit 6238840

Please sign in to comment.