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 EthernetPacket {
21  
22      /**
23       * MAC Address: 00:00:5E:00:53:00
24       */
25      private static final byte[] DUMMY_SOURCE_MAC_ADDRESS = new byte[]{0, 0, 94, 0, 83, 0};
26  
27      /**
28       * MAC Address: 00:00:5E:00:53:FF
29       */
30      private static final byte[] DUMMY_DESTINATION_MAC_ADDRESS = new byte[]{0, 0, 94, 0, 83, -1};
31  
32      /**
33       * IPv4
34       */
35      private static final int V4 = 0x0800;
36  
37      /**
38       * IPv6
39       */
40      private static final int V6 = 0x86dd;
41  
42      private EthernetPacket() {
43          // Prevent outside initialization
44      }
45  
46      /**
47       * Write IPv4 Ethernet Packet. It uses a dummy MAC address for both source and destination.
48       *
49       * @param byteBuf ByteBuf where Ethernet Packet data will be set
50       * @param payload Payload of IPv4
51       */
52      static void writeIPv4(ByteBuf byteBuf, ByteBuf payload) {
53          EthernetPacket.writePacket(byteBuf, payload, DUMMY_SOURCE_MAC_ADDRESS, DUMMY_DESTINATION_MAC_ADDRESS, V4);
54      }
55  
56      /**
57       * Write IPv6 Ethernet Packet. It uses a dummy MAC address for both source and destination.
58       *
59       * @param byteBuf ByteBuf where Ethernet Packet data will be set
60       * @param payload Payload of IPv6
61       */
62      static void writeIPv6(ByteBuf byteBuf, ByteBuf payload) {
63          EthernetPacket.writePacket(byteBuf, payload, DUMMY_SOURCE_MAC_ADDRESS, DUMMY_DESTINATION_MAC_ADDRESS, V6);
64      }
65  
66      /**
67       * Write IPv6 Ethernet Packet
68       *
69       * @param byteBuf    ByteBuf where Ethernet Packet data will be set
70       * @param payload    Payload of IPv6
71       * @param srcAddress Source MAC Address
72       * @param dstAddress Destination MAC Address
73       * @param type       Type of Frame
74       */
75      private static void writePacket(ByteBuf byteBuf, ByteBuf payload, byte[] srcAddress, byte[] dstAddress, int type) {
76          byteBuf.writeBytes(dstAddress); // Destination MAC Address
77          byteBuf.writeBytes(srcAddress); // Source MAC Address
78          byteBuf.writeShort(type);       // Frame Type (IPv4 or IPv6)
79          byteBuf.writeBytes(payload);    // Payload of L3
80      }
81  }