Skip to content

Commit

Permalink
Allow PcapWriteHandler instances to write to the same OutputStream (#…
Browse files Browse the repository at this point in the history
…13118)

Motivation:
Currently, PcapWriteHandler expects a dedicated OutputStream, and cannot support multiple streams written to a single PCAP file. These modifications are intended to support sharing the OutputStream between multiple concurrent PcapWriteHandler instances.

Modification:
Added a "shareOutputStream" flag to PcapWriteHandler (and PcapWriter) to enable synchronization on the OutputStream in order to prevent interleaved writes. Also, provide a method to write the PCAP Global Header to the OutputStream.

Result:
Allows multiple channels to write to the same PCAP file.

This PR adds further improvement done in PR #12337 and supersedes it.
Original Author: @RoganDawes
  • Loading branch information
hyperxpro committed Feb 1, 2023
1 parent 9c35aa1 commit 5ee9da5
Show file tree
Hide file tree
Showing 7 changed files with 569 additions and 237 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ private EthernetPacket() {
* @param payload Payload of IPv4
*/
static void writeIPv4(ByteBuf byteBuf, ByteBuf payload) {
EthernetPacket.writePacket(byteBuf, payload, DUMMY_SOURCE_MAC_ADDRESS, DUMMY_DESTINATION_MAC_ADDRESS, V4);
writePacket(byteBuf, payload, DUMMY_SOURCE_MAC_ADDRESS, DUMMY_DESTINATION_MAC_ADDRESS, V4);
}

/**
Expand All @@ -60,7 +60,7 @@ static void writeIPv4(ByteBuf byteBuf, ByteBuf payload) {
* @param payload Payload of IPv6
*/
static void writeIPv6(ByteBuf byteBuf, ByteBuf payload) {
EthernetPacket.writePacket(byteBuf, payload, DUMMY_SOURCE_MAC_ADDRESS, DUMMY_DESTINATION_MAC_ADDRESS, V6);
writePacket(byteBuf, payload, DUMMY_SOURCE_MAC_ADDRESS, DUMMY_DESTINATION_MAC_ADDRESS, V6);
}

/**
Expand Down
14 changes: 9 additions & 5 deletions handler/src/main/java/io/netty/handler/pcap/PcapHeaders.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@

import io.netty.buffer.ByteBuf;

import java.io.IOException;
import java.io.OutputStream;

final class PcapHeaders {

/**
Expand All @@ -31,20 +34,21 @@ final class PcapHeaders {
* <li> network </li>
* </ol>
*/
private static final byte[] GLOBAL_HEADER = new byte[]{-95, -78, -61, -44, 0, 2, 0, 4, 0, 0,
private static final byte[] GLOBAL_HEADER = {-95, -78, -61, -44, 0, 2, 0, 4, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, -1, -1, 0, 0, 0, 1};

private PcapHeaders() {
// Prevent outside initialization
}

/**
* Write Pcap Global Header
* Writes the Pcap Global Header to the provided {@code OutputStream}
*
* @param byteBuf byteBuf where we'll write header data
* @param outputStream OutputStream where Pcap data will be written.
* @throws IOException if there is an error writing to the {@code OutputStream}
*/
public static void writeGlobalHeader(ByteBuf byteBuf) {
byteBuf.writeBytes(GLOBAL_HEADER);
static void writeGlobalHeader(OutputStream outputStream) throws IOException {
outputStream.write(GLOBAL_HEADER);
}

/**
Expand Down

0 comments on commit 5ee9da5

Please sign in to comment.