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
.tracecompass
.internal
.pcap
.core
.protocol
.ethernet2
;
15 import static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.checkNotNull
;
17 import java
.nio
.ByteBuffer
;
18 import java
.nio
.ByteOrder
;
19 import java
.util
.Arrays
;
22 import org
.eclipse
.jdt
.annotation
.Nullable
;
23 import org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
;
24 import org
.eclipse
.tracecompass
.internal
.pcap
.core
.packet
.BadPacketException
;
25 import org
.eclipse
.tracecompass
.internal
.pcap
.core
.packet
.Packet
;
26 import org
.eclipse
.tracecompass
.internal
.pcap
.core
.protocol
.PcapProtocol
;
27 import org
.eclipse
.tracecompass
.internal
.pcap
.core
.protocol
.ipv4
.IPv4Packet
;
28 import org
.eclipse
.tracecompass
.internal
.pcap
.core
.protocol
.unknown
.UnknownPacket
;
29 import org
.eclipse
.tracecompass
.internal
.pcap
.core
.trace
.PcapFile
;
30 import org
.eclipse
.tracecompass
.internal
.pcap
.core
.util
.ConversionHelper
;
31 import org
.eclipse
.tracecompass
.internal
.pcap
.core
.util
.EthertypeHelper
;
33 import com
.google
.common
.collect
.ImmutableMap
;
36 * Class that represents an Ethernet II packet. This should be called an
37 * Ethernet frame, but in order to keep the nomenclature consistent, this is
40 * @author Vincent Perot
42 public class EthernetIIPacket
extends Packet
{
44 private final @Nullable Packet fChildPacket
;
45 private final @Nullable ByteBuffer fPayload
;
47 /* We store MAC addresses as byte arrays since
48 * there is no standard java class to store them. */
49 private final byte[] fSourceMacAddress
;
50 private final byte[] fDestinationMacAddress
;
52 private final int fType
;
54 private @Nullable EthernetIIEndpoint fSourceEndpoint
;
55 private @Nullable EthernetIIEndpoint fDestinationEndpoint
;
57 private @Nullable Map
<String
, String
> fFields
;
60 * Constructor of the Ethernet Packet class.
63 * The file that contains this packet.
65 * The parent packet of this packet (the encapsulating packet).
67 * The entire packet (header and payload).
68 * @throws BadPacketException
69 * Thrown when the packet is erroneous.
71 public EthernetIIPacket(PcapFile file
, @Nullable Packet parent
, ByteBuffer packet
) throws BadPacketException
{
72 super(file
, parent
, PcapProtocol
.ETHERNET_II
);
74 if (packet
.array().length
<= EthernetIIValues
.ETHERNET_II_MIN_SIZE
) {
75 throw new BadPacketException("An Ethernet II packet can't be smaller than 14 bytes."); //$NON-NLS-1$
78 // The endpoints are lazy loaded. They are defined in the get*Endpoint()
80 fSourceEndpoint
= null;
81 fDestinationEndpoint
= null;
85 fDestinationMacAddress
= new byte[EthernetIIValues
.MAC_ADDRESS_SIZE
];
86 fSourceMacAddress
= new byte[EthernetIIValues
.MAC_ADDRESS_SIZE
];
87 packet
.order(ByteOrder
.BIG_ENDIAN
);
89 packet
.get(fDestinationMacAddress
);
90 packet
.get(fSourceMacAddress
);
91 fType
= ConversionHelper
.unsignedShortToInt(packet
.getShort());
93 // Get payload if it exists.
94 if (packet
.array().length
- packet
.position() > 0) {
95 byte[] array
= new byte[packet
.array().length
- packet
.position()];
97 ByteBuffer payload
= ByteBuffer
.wrap(array
);
98 payload
.order(ByteOrder
.BIG_ENDIAN
);
107 fChildPacket
= findChildPacket();
112 public @Nullable Packet
getChildPacket() {
117 public @Nullable ByteBuffer
getPayload() {
122 * Getter method for the source MAC Address.
124 * @return The source MAC address.
126 public byte[] getSourceMacAddress() {
127 return checkNotNull(Arrays
.copyOf(fSourceMacAddress
, fSourceMacAddress
.length
));
131 * Getter method for the destination MAC Address.
133 * @return The destination MAC address.
135 public byte[] getDestinationMacAddress() {
136 return checkNotNull(Arrays
.copyOf(fDestinationMacAddress
, fDestinationMacAddress
.length
));
140 * Getter method for Ethertype. See
141 * http://standards.ieee.org/develop/regauth/ethertype/eth.txt
143 * @return The Ethertype. This is used to determine the child packet..
145 public int getEthertype() {
150 protected @Nullable Packet
findChildPacket() throws BadPacketException
{
151 // TODO Add more protocols.
152 ByteBuffer payload
= fPayload
;
153 if (payload
== null) {
157 case EthertypeHelper
.ETHERTYPE_IPV4
:
158 return new IPv4Packet(getPcapFile(), this, payload
);
160 return new UnknownPacket(getPcapFile(), this, payload
);
165 public String
toString() {
166 String string
= getProtocol().getName() + ", Source: " + ConversionHelper
.toMacAddress(fSourceMacAddress
) + //$NON-NLS-1$
167 ", Destination: " + ConversionHelper
.toMacAddress(fDestinationMacAddress
) + ", Type: " + //$NON-NLS-1$ //$NON-NLS-2$
168 EthertypeHelper
.toEtherType(fType
) + "\n"; //$NON-NLS-1$
169 final Packet child
= fChildPacket
;
171 return string
+ child
.toString();
177 public boolean validate() {
178 // Not yet implemented. ATM, we consider that all packets are valid.
179 // This is the case for all packets.
180 // TODO Implement it.
185 public EthernetIIEndpoint
getSourceEndpoint() {
186 @Nullable EthernetIIEndpoint endpoint
= fSourceEndpoint
;
187 if (endpoint
== null) {
188 endpoint
= new EthernetIIEndpoint(this, true);
190 fSourceEndpoint
= endpoint
;
191 return fSourceEndpoint
;
195 public EthernetIIEndpoint
getDestinationEndpoint() {
196 @Nullable EthernetIIEndpoint endpoint
= fDestinationEndpoint
;
198 if (endpoint
== null) {
199 endpoint
= new EthernetIIEndpoint(this, false);
201 fDestinationEndpoint
= endpoint
;
202 return fDestinationEndpoint
;
206 public Map
<String
, String
> getFields() {
207 Map
<String
, String
> map
= fFields
;
209 ImmutableMap
.Builder
<String
, String
> builder
= ImmutableMap
.builder();
210 builder
.put("Source MAC Address", ConversionHelper
.toMacAddress(fSourceMacAddress
)); //$NON-NLS-1$
211 builder
.put("Destination MAC Address", ConversionHelper
.toMacAddress(fDestinationMacAddress
)); //$NON-NLS-1$
212 builder
.put("Ethertype", String
.valueOf(EthertypeHelper
.toEtherType(fType
))); //$NON-NLS-1$
214 fFields
= builder
.build();
221 public String
getLocalSummaryString() {
222 return "Src: " + ConversionHelper
.toMacAddress(fSourceMacAddress
) + " , Dst: " + ConversionHelper
.toMacAddress(fDestinationMacAddress
); //$NON-NLS-1$ //$NON-NLS-2$
226 protected String
getSignificationString() {
227 return "Source MAC: " + ConversionHelper
.toMacAddress(fSourceMacAddress
) + " , Destination MAC: " + ConversionHelper
.toMacAddress(fDestinationMacAddress
); //$NON-NLS-1$ //$NON-NLS-2$
231 public int hashCode() {
232 final int prime
= 31;
234 final Packet child
= fChildPacket
;
236 result
= prime
* result
+ child
.hashCode();
238 result
= prime
* result
;
240 result
= prime
* result
+ Arrays
.hashCode(fDestinationMacAddress
);
241 final ByteBuffer payload
= fPayload
;
242 if (payload
!= null) {
243 result
= prime
* result
+ payload
.hashCode();
245 result
= prime
* result
;
247 result
= prime
* result
+ Arrays
.hashCode(fSourceMacAddress
);
248 result
= prime
* result
+ fType
;
253 public boolean equals(@Nullable Object obj
) {
260 if (getClass() != obj
.getClass()) {
263 EthernetIIPacket other
= (EthernetIIPacket
) obj
;
264 if(!NonNullUtils
.equalsNullable(fChildPacket
, other
.fChildPacket
)) {
267 if (!Arrays
.equals(fDestinationMacAddress
, other
.fDestinationMacAddress
)) {
270 if(!NonNullUtils
.equalsNullable(fPayload
, other
.fPayload
)) {
274 if (!Arrays
.equals(fSourceMacAddress
, other
.fSourceMacAddress
)) {
277 if (fType
!= other
.fType
) {