1 /* 2 * Copyright 2020 The Netty Project 3 * 4 * The Netty Project licenses this file to you under the Apache License, 5 * version 2.0 (the "License"); you may not use this file except in compliance 6 * with the License. You may obtain a copy of the License at: 7 * 8 * https://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations 14 * under the License. 15 */ 16 package io.netty.handler.pcap; 17 18 import io.netty.buffer.ByteBuf; 19 20 import java.io.IOException; 21 import java.io.OutputStream; 22 23 final class PcapHeaders { 24 25 /** 26 * Pcap Global Header built from: 27 * <ol> 28 * <li> magic_number </li> 29 * <li> version_major </li> 30 * <li> version_minor </li> 31 * <li> thiszone </li> 32 * <li> sigfigs </li> 33 * <li> snaplen </li> 34 * <li> network </li> 35 * </ol> 36 */ 37 private static final byte[] GLOBAL_HEADER = {-95, -78, -61, -44, 0, 2, 0, 4, 0, 0, 38 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, 0, 0, 0, 1}; 39 40 private PcapHeaders() { 41 // Prevent outside initialization 42 } 43 44 /** 45 * Writes the Pcap Global Header to the provided {@code OutputStream} 46 * 47 * @param outputStream OutputStream where Pcap data will be written. 48 * @throws IOException if there is an error writing to the {@code OutputStream} 49 */ 50 static void writeGlobalHeader(OutputStream outputStream) throws IOException { 51 outputStream.write(GLOBAL_HEADER); 52 } 53 54 /** 55 * Write Pcap Packet Header 56 * 57 * @param byteBuf ByteBuf where we'll write header data 58 * @param ts_sec timestamp seconds 59 * @param ts_usec timestamp microseconds 60 * @param incl_len number of octets of packet saved in file 61 * @param orig_len actual length of packet 62 */ 63 static void writePacketHeader(ByteBuf byteBuf, int ts_sec, int ts_usec, int incl_len, int orig_len) { 64 byteBuf.writeInt(ts_sec); 65 byteBuf.writeInt(ts_usec); 66 byteBuf.writeInt(incl_len); 67 byteBuf.writeInt(orig_len); 68 } 69 }