Skip to content

Commit

Permalink
Merge pull request #12 from compscidr/jason/bug-fix-dump-partial-buffer
Browse files Browse the repository at this point in the history
bugfix, dumping partial buffer
  • Loading branch information
compscidr authored Aug 16, 2024
2 parents 7bede70 + 145e052 commit 15b26ed
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,15 @@ class PcapNgFilePacketDumper(
addresses: Boolean,
etherType: EtherType?,
) {
val startingPosition = buffer.position()
val originalLimit = buffer.limit()
// optionally prepend the ethernet dummy header
val conversionBuffer =
if (etherType != null) {
prependDummyHeader(buffer, offset, length, etherType)
} else {
buffer
val actualLimit = minOf(originalLimit, offset + length)
buffer.limit(actualLimit)
}

if (isSimple) {
Expand All @@ -67,5 +70,7 @@ class PcapNgFilePacketDumper(
outputStreamWriter.write(packetBlock.toBytes())
outputStreamWriter.flush()
}
buffer.position(startingPosition)
buffer.limit(originalLimit)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,14 @@ class PcapNgTcpServerPacketDumper(
// if there are no connected clients, no reason to dump
return
}
val startingPosition = buffer.position()
val originalLimit = buffer.limit()
val conversionBuffer =
if (etherType != null) {
prependDummyHeader(buffer, offset, length, etherType)
} else {
buffer
val actualLimit = minOf(originalLimit, offset + length)
buffer.limit(actualLimit)
}
val packetBlock =
if (isSimple) {
Expand All @@ -146,6 +149,8 @@ class PcapNgTcpServerPacketDumper(
// todo: timestamp
PcapNgEnhancedPacketBlock(conversionBuffer.array())
}
buffer.position(startingPosition)
buffer.limit(originalLimit)

with(connectionQueue.iterator()) {
forEach {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,14 @@ class StringPacketDumper(
etherType: EtherType? = null,
): String {
val startingPosition = buffer.position()
val originalLimit = buffer.limit()
// optionally prepend the ethernet dummy header
val conversionBuffer =
if (etherType != null) {
prependDummyHeader(buffer, offset, length, etherType)
} else {
buffer
val actualLimit = minOf(originalLimit, offset + length)
buffer.limit(actualLimit)
}

var totalLength = 0
Expand All @@ -103,6 +105,7 @@ class StringPacketDumper(
}
}
buffer.position(startingPosition)
buffer.limit(originalLimit)
return output.toString()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ class TestTextFilePacketDumper {
buffer: ByteBuffer,
addresses: Boolean = false,
etherType: EtherType? = null,
offset: Int = 0,
length: Int = buffer.limit(),
): ByteBuffer {
val stringDumper = StringPacketDumper()
logger.debug("write buffer: ${stringDumper.dumpBufferToString(buffer, 0, buffer.limit())}")
dumper.dumpBuffer(buffer, 0, buffer.limit(), addresses, etherType)
dumper.dumpBuffer(buffer, offset, length, addresses, etherType)
dumper.close()
return TextFilePacketDumper.parseFile(dumper.filename, addresses, etherType)
}
Expand Down Expand Up @@ -102,6 +104,14 @@ class TestTextFilePacketDumper {
assertEquals(buffer, readBuffer)
}

@Test fun shorterDumpThanLimit() {
dumper.open()
val buffer = ByteBuffer.wrap(byteArrayOf(0x00, 0x01, 0x02, 0x03, 0x04))
val readBuffer = writeReadBuffer(buffer, addresses = true, etherType = EtherType.IPv4, 0, buffer.limit() - 2)
buffer.limit(buffer.limit() - 2) // this will effectively remove the last two bytes
assertEquals(buffer, readBuffer)
}

@Test
fun testRealFileDump() {
// this file has shorter addresses than we normally output, make sure it can be handled
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -669,4 +669,10 @@ class TestStringPacketDumper {
val hexString = stringPacketDumper.dumpBufferToString(buffer, 0, buffer.limit(), true, EtherType.IPv4)
assertEquals("00000000 14 C0 3E 55 0B 35 74 D0 2B 29 A5 18 08 00 00 01\n00000010 02 03 04", hexString)
}

@Test fun shorterDumpThanLimit() {
val buffer = ByteBuffer.wrap(byteArrayOf(0x00, 0x01, 0x02, 0x03, 0x04))
val hexString = stringPacketDumper.dumpBufferToString(buffer, 0, buffer.limit() - 2, false)
assertEquals("00 01 02", hexString)
}
}

0 comments on commit 15b26ed

Please sign in to comment.