dan-s1 commented on code in PR #8691: URL: https://github.com/apache/nifi/pull/8691#discussion_r1608824023
########## nifi-extension-bundles/nifi-network-bundle/nifi-network-processors/src/main/java/org/apache/nifi/processors/network/pcap/PCAP.java: ########## @@ -0,0 +1,161 @@ +// MIT License + +// Copyright (c) 2015-2023 Kaitai Project + +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: + +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. + +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +package org.apache.nifi.processors.network.pcap; + +import java.nio.ByteBuffer; +import java.nio.ByteOrder; + +import java.util.ArrayList; +import java.util.List; + +/** + * PCAP (named after libpcap / winpcap) is a popular format for saving + * network traffic grabbed by network sniffers. It is typically + * produced by tools like [tcpdump](<a href="https://www.tcpdump.org/">...</a>) or + * [Wireshark](<a href="https://www.wireshark.org/">...</a>). + * + * @see <a href= + * "https://wiki.wireshark.org/Development/LibpcapFileFormat">Source</a> + */ +public class PCAP { + private ByteBufferInterface io; + + public PCAP(ByteBufferInterface io) { + this(io, null, null); + } + + public PCAP(ByteBufferInterface io, Object parent, PCAP root) { + + this.parent = parent; + this.root = root == null ? this : root; + this.io = io; + read(); + } + + public PCAP(Header hdr, List<Packet> packets) { + this.hdr = hdr; + this.packets = packets; + } + + public byte[] readBytesFull() { + + int headerBufferSize = 20 + this.hdr().magicNumber().length; + ByteBuffer headerBuffer = ByteBuffer.allocate(headerBufferSize); + headerBuffer.order(ByteOrder.LITTLE_ENDIAN); + + headerBuffer.put(this.hdr().magicNumber()); + headerBuffer.put(this.readIntToNBytes(this.hdr().versionMajor(), 2, false)); + headerBuffer.put(this.readIntToNBytes(this.hdr().versionMinor(), 2, false)); + headerBuffer.put(this.readIntToNBytes(this.hdr().thiszone(), 4, false)); + headerBuffer.put(this.readLongToNBytes(this.hdr().sigfigs(), 4, true)); + headerBuffer.put(this.readLongToNBytes(this.hdr().snaplen(), 4, true)); + headerBuffer.put(this.readLongToNBytes(this.hdr().network(), 4, true)); + + List<byte[]> packetByteArrays = new ArrayList<>(); + + int packetBufferSize = 0; + + for (Packet currentPacket : packets) { + ByteBuffer currentPacketBytes = ByteBuffer.allocate(16 + currentPacket.rawBody().length); + currentPacketBytes.put(readLongToNBytes(currentPacket.tsSec(), 4, false)); + currentPacketBytes.put(readLongToNBytes(currentPacket.tsUsec(), 4, false)); + currentPacketBytes.put(readLongToNBytes(currentPacket.inclLen(), 4, false)); + currentPacketBytes.put(readLongToNBytes(currentPacket.origLen(), 4, false)); + currentPacketBytes.put(currentPacket.rawBody()); + + packetByteArrays.add(currentPacketBytes.array()); + packetBufferSize += 16 + currentPacket.rawBody().length; + } Review Comment: Per earlier comment and also should probably do the adding in one place. Please rename the variable as you see fit ```suggestion for (Packet currentPacket : packets) { int packetHeaderAndBodySize = PACKET_HEADER_LENGTH + currentPacket.rawBody().length; ByteBuffer currentPacketBytes = ByteBuffer.allocate(packetHeaderAndBodySize); currentPacketBytes.put(readLongToNBytes(currentPacket.tsSec(), 4, false)); currentPacketBytes.put(readLongToNBytes(currentPacket.tsUsec(), 4, false)); currentPacketBytes.put(readLongToNBytes(currentPacket.inclLen(), 4, false)); currentPacketBytes.put(readLongToNBytes(currentPacket.origLen(), 4, false)); currentPacketBytes.put(currentPacket.rawBody()); packetByteArrays.add(currentPacketBytes.array()); packetBufferSize += packetHeaderAndBodySize; } ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
