View Javadoc
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  final class PcapHeaders {
21  
22      /**
23       * Pcap Global Header built from:
24       * <ol>
25       *      <li> magic_number </li>
26       *      <li> version_major </li>
27       *      <li> version_minor </li>
28       *      <li> thiszone </li>
29       *      <li> sigfigs </li>
30       *      <li> snaplen </li>
31       *      <li> network </li>
32       * </ol>
33       */
34      private static final byte[] GLOBAL_HEADER = new byte[]{-95, -78, -61, -44, 0, 2, 0, 4, 0, 0,
35              0, 0, 0, 0, 0, 0, 0, 0, -1, -1, 0, 0, 0, 1};
36  
37      private PcapHeaders() {
38          // Prevent outside initialization
39      }
40  
41      /**
42       * Write Pcap Global Header
43       *
44       * @param byteBuf byteBuf where we'll write header data
45       */
46      public static void writeGlobalHeader(ByteBuf byteBuf) {
47          byteBuf.writeBytes(GLOBAL_HEADER);
48      }
49  
50      /**
51       * Write Pcap Packet Header
52       *
53       * @param byteBuf  ByteBuf where we'll write header data
54       * @param ts_sec   timestamp seconds
55       * @param ts_usec  timestamp microseconds
56       * @param incl_len number of octets of packet saved in file
57       * @param orig_len actual length of packet
58       */
59      static void writePacketHeader(ByteBuf byteBuf, int ts_sec, int ts_usec, int incl_len, int orig_len) {
60          byteBuf.writeInt(ts_sec);
61          byteBuf.writeInt(ts_usec);
62          byteBuf.writeInt(incl_len);
63          byteBuf.writeInt(orig_len);
64      }
65  }