1 /*******************************************************************************
2 * Copyright (c) 2014 Ericsson
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * Vincent Perot - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.pcap
.core
.protocol
.ethernet2
;
15 import java
.nio
.ByteBuffer
;
16 import java
.nio
.ByteOrder
;
17 import java
.util
.Arrays
;
20 import org
.eclipse
.jdt
.annotation
.NonNull
;
21 import org
.eclipse
.jdt
.annotation
.Nullable
;
22 import org
.eclipse
.linuxtools
.pcap
.core
.packet
.BadPacketException
;
23 import org
.eclipse
.linuxtools
.pcap
.core
.packet
.Packet
;
24 import org
.eclipse
.linuxtools
.pcap
.core
.protocol
.Protocol
;
25 import org
.eclipse
.linuxtools
.pcap
.core
.protocol
.ipv4
.IPv4Packet
;
26 import org
.eclipse
.linuxtools
.pcap
.core
.protocol
.unknown
.UnknownPacket
;
27 import org
.eclipse
.linuxtools
.pcap
.core
.trace
.PcapFile
;
28 import org
.eclipse
.linuxtools
.pcap
.core
.util
.ConversionHelper
;
29 import org
.eclipse
.linuxtools
.pcap
.core
.util
.EthertypeHelper
;
31 import com
.google
.common
.collect
.ImmutableMap
;
34 * Class that represents an Ethernet II packet. This should be called an
35 * Ethernet frame, but in order to keep the nomenclature consistent, this is
38 * @author Vincent Perot
40 public class EthernetIIPacket
extends Packet
{
42 private final @Nullable Packet fChildPacket
;
43 private final @Nullable ByteBuffer fPayload
;
45 private final byte[] fSourceMacAddress
;
46 private final byte[] fDestinationMacAddress
;
47 private final int fType
;
49 private @Nullable EthernetIIEndpoint fSourceEndpoint
;
50 private @Nullable EthernetIIEndpoint fDestinationEndpoint
;
52 private @Nullable ImmutableMap
<String
, String
> fFields
;
55 * Constructor of the Ethernet Packet class.
58 * The file that contains this packet.
60 * The parent packet of this packet (the encapsulating packet).
62 * The entire packet (header and payload).
63 * @throws BadPacketException
64 * Thrown when the packet is erroneous.
66 public EthernetIIPacket(PcapFile file
, @Nullable Packet parent
, ByteBuffer packet
) throws BadPacketException
{
67 super(file
, parent
, Protocol
.ETHERNET_II
);
69 if (packet
.array().length
<= EthernetIIValues
.ETHERNET_II_MIN_SIZE
) {
70 throw new BadPacketException("An Ethernet II packet can't be smaller than 14 bytes."); //$NON-NLS-1$
73 // The endpoints are lazy loaded. They are defined in the get*Endpoint()
75 fSourceEndpoint
= null;
76 fDestinationEndpoint
= null;
80 fDestinationMacAddress
= new byte[EthernetIIValues
.MAC_ADDRESS_SIZE
];
81 fSourceMacAddress
= new byte[EthernetIIValues
.MAC_ADDRESS_SIZE
];
82 packet
.order(ByteOrder
.BIG_ENDIAN
);
84 packet
.get(fDestinationMacAddress
);
85 packet
.get(fSourceMacAddress
);
86 fType
= ConversionHelper
.unsignedShortToInt(packet
.getShort());
88 // Get payload if it exists.
89 if (packet
.array().length
- packet
.position() > 0) {
90 byte[] array
= new byte[packet
.array().length
- packet
.position()];
92 ByteBuffer payload
= ByteBuffer
.wrap(array
);
93 if (payload
!= null) {
94 payload
.order(ByteOrder
.BIG_ENDIAN
);
104 fChildPacket
= findChildPacket();
109 public @Nullable Packet
getChildPacket() {
114 public @Nullable ByteBuffer
getPayload() {
119 * Getter method for the source MAC Address.
121 * @return The source MAC address.
123 public byte[] getSourceMacAddress() {
124 return fSourceMacAddress
;
128 * Getter method for the destination MAC Address.
130 * @return The destination MAC address.
132 public byte[] getDestinationMacAddress() {
133 return fDestinationMacAddress
;
137 * Getter method for Ethertype. See
138 * http://standards.ieee.org/develop/regauth/ethertype/eth.txt
140 * @return The Ethertype. This is used to determine the child packet..
142 public int getEthertype() {
147 protected @Nullable Packet
findChildPacket() throws BadPacketException
{
148 // TODO Add more protocols.
149 ByteBuffer payload
= fPayload
;
150 if (payload
== null) {
154 case EthertypeHelper
.ETHERTYPE_IPV4
:
155 return new IPv4Packet(getPcapFile(), this, payload
);
157 return new UnknownPacket(getPcapFile(), this, payload
);
162 public String
toString() {
163 String string
= getProtocol().getName() + ", Source: " + ConversionHelper
.toMacAddress(fSourceMacAddress
) + //$NON-NLS-1$
164 ", Destination: " + ConversionHelper
.toMacAddress(fDestinationMacAddress
) + ", Type: " + //$NON-NLS-1$ //$NON-NLS-2$
165 EthertypeHelper
.toEtherType(fType
) + "\n"; //$NON-NLS-1$
166 final Packet child
= fChildPacket
;
168 return string
+ child
.toString();
174 public boolean validate() {
175 // Not yet implemented. ATM, we consider that all packets are valid.
176 // This is the case for all packets.
177 // TODO Implement it.
182 public EthernetIIEndpoint
getSourceEndpoint() {
183 @Nullable EthernetIIEndpoint endpoint
= fSourceEndpoint
;
184 if (endpoint
== null) {
185 endpoint
= new EthernetIIEndpoint(this, true);
187 fSourceEndpoint
= endpoint
;
188 return fSourceEndpoint
;
192 public EthernetIIEndpoint
getDestinationEndpoint() {
193 @Nullable EthernetIIEndpoint endpoint
= fDestinationEndpoint
;
195 if (endpoint
== null) {
196 endpoint
= new EthernetIIEndpoint(this, false);
198 fDestinationEndpoint
= endpoint
;
199 return fDestinationEndpoint
;
203 public Map
<String
, String
> getFields() {
204 ImmutableMap
<String
, String
> map
= fFields
;
206 @SuppressWarnings("null")
207 @NonNull ImmutableMap
<String
, String
> newMap
= ImmutableMap
.<String
, String
> builder()
208 .put("Source MAC Address", ConversionHelper
.toMacAddress(fSourceMacAddress
)) //$NON-NLS-1$
209 .put("Destination MAC Address", ConversionHelper
.toMacAddress(fDestinationMacAddress
)) //$NON-NLS-1$
210 .put("Ethertype", String
.valueOf(EthertypeHelper
.toEtherType(fType
))) //$NON-NLS-1$
219 public String
getLocalSummaryString() {
220 return "Src: " + ConversionHelper
.toMacAddress(fSourceMacAddress
) + " , Dst: " + ConversionHelper
.toMacAddress(fDestinationMacAddress
); //$NON-NLS-1$ //$NON-NLS-2$
224 protected String
getSignificationString() {
225 return "Source MAC: " + ConversionHelper
.toMacAddress(fSourceMacAddress
) + " , Destination MAC: " + ConversionHelper
.toMacAddress(fDestinationMacAddress
); //$NON-NLS-1$ //$NON-NLS-2$
229 public int hashCode() {
230 final int prime
= 31;
232 final Packet child
= fChildPacket
;
234 result
= prime
* result
+ child
.hashCode();
236 result
= prime
* result
;
238 result
= prime
* result
+ Arrays
.hashCode(fDestinationMacAddress
);
239 final ByteBuffer payload
= fPayload
;
240 if (payload
!= null) {
241 result
= prime
* result
+ payload
.hashCode();
243 result
= prime
* result
;
245 result
= prime
* result
+ Arrays
.hashCode(fSourceMacAddress
);
246 result
= prime
* result
+ fType
;
251 public boolean equals(@Nullable Object obj
) {
258 if (getClass() != obj
.getClass()) {
261 EthernetIIPacket other
= (EthernetIIPacket
) obj
;
262 if (fChildPacket
== null) {
263 if (other
.fChildPacket
!= null) {
267 final Packet child
= fChildPacket
;
269 if (!child
.equals(other
.fChildPacket
)) {
273 if (other
.fChildPacket
!= null) {
278 if (!Arrays
.equals(fDestinationMacAddress
, other
.fDestinationMacAddress
)) {
281 if (fPayload
== null) {
282 if (other
.fPayload
!= null) {
286 final ByteBuffer payload
= fPayload
;
287 if (payload
!= null) {
288 if (!payload
.equals(other
.fPayload
)) {
292 if (other
.fPayload
!= null) {
297 if (!Arrays
.equals(fSourceMacAddress
, other
.fSourceMacAddress
)) {
300 if (fType
!= other
.fType
) {