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 if (payload
!= null) {
99 payload
.order(ByteOrder
.BIG_ENDIAN
);
109 fChildPacket
= findChildPacket();
114 public @Nullable Packet
getChildPacket() {
119 public @Nullable ByteBuffer
getPayload() {
124 * Getter method for the source MAC Address.
126 * @return The source MAC address.
128 public byte[] getSourceMacAddress() {
129 return checkNotNull(Arrays
.copyOf(fSourceMacAddress
, fSourceMacAddress
.length
));
133 * Getter method for the destination MAC Address.
135 * @return The destination MAC address.
137 public byte[] getDestinationMacAddress() {
138 return checkNotNull(Arrays
.copyOf(fDestinationMacAddress
, fDestinationMacAddress
.length
));
142 * Getter method for Ethertype. See
143 * http://standards.ieee.org/develop/regauth/ethertype/eth.txt
145 * @return The Ethertype. This is used to determine the child packet..
147 public int getEthertype() {
152 protected @Nullable Packet
findChildPacket() throws BadPacketException
{
153 // TODO Add more protocols.
154 ByteBuffer payload
= fPayload
;
155 if (payload
== null) {
159 case EthertypeHelper
.ETHERTYPE_IPV4
:
160 return new IPv4Packet(getPcapFile(), this, payload
);
162 return new UnknownPacket(getPcapFile(), this, payload
);
167 public String
toString() {
168 String string
= getProtocol().getName() + ", Source: " + ConversionHelper
.toMacAddress(fSourceMacAddress
) + //$NON-NLS-1$
169 ", Destination: " + ConversionHelper
.toMacAddress(fDestinationMacAddress
) + ", Type: " + //$NON-NLS-1$ //$NON-NLS-2$
170 EthertypeHelper
.toEtherType(fType
) + "\n"; //$NON-NLS-1$
171 final Packet child
= fChildPacket
;
173 return string
+ child
.toString();
179 public boolean validate() {
180 // Not yet implemented. ATM, we consider that all packets are valid.
181 // This is the case for all packets.
182 // TODO Implement it.
187 public EthernetIIEndpoint
getSourceEndpoint() {
188 @Nullable EthernetIIEndpoint endpoint
= fSourceEndpoint
;
189 if (endpoint
== null) {
190 endpoint
= new EthernetIIEndpoint(this, true);
192 fSourceEndpoint
= endpoint
;
193 return fSourceEndpoint
;
197 public EthernetIIEndpoint
getDestinationEndpoint() {
198 @Nullable EthernetIIEndpoint endpoint
= fDestinationEndpoint
;
200 if (endpoint
== null) {
201 endpoint
= new EthernetIIEndpoint(this, false);
203 fDestinationEndpoint
= endpoint
;
204 return fDestinationEndpoint
;
208 public Map
<String
, String
> getFields() {
209 Map
<String
, String
> map
= fFields
;
211 ImmutableMap
.Builder
<String
, String
> builder
= ImmutableMap
.builder();
212 builder
.put("Source MAC Address", ConversionHelper
.toMacAddress(fSourceMacAddress
)); //$NON-NLS-1$
213 builder
.put("Destination MAC Address", ConversionHelper
.toMacAddress(fDestinationMacAddress
)); //$NON-NLS-1$
214 builder
.put("Ethertype", String
.valueOf(EthertypeHelper
.toEtherType(fType
))); //$NON-NLS-1$
216 fFields
= checkNotNull(builder
.build());
223 public String
getLocalSummaryString() {
224 return "Src: " + ConversionHelper
.toMacAddress(fSourceMacAddress
) + " , Dst: " + ConversionHelper
.toMacAddress(fDestinationMacAddress
); //$NON-NLS-1$ //$NON-NLS-2$
228 protected String
getSignificationString() {
229 return "Source MAC: " + ConversionHelper
.toMacAddress(fSourceMacAddress
) + " , Destination MAC: " + ConversionHelper
.toMacAddress(fDestinationMacAddress
); //$NON-NLS-1$ //$NON-NLS-2$
233 public int hashCode() {
234 final int prime
= 31;
236 final Packet child
= fChildPacket
;
238 result
= prime
* result
+ child
.hashCode();
240 result
= prime
* result
;
242 result
= prime
* result
+ Arrays
.hashCode(fDestinationMacAddress
);
243 final ByteBuffer payload
= fPayload
;
244 if (payload
!= null) {
245 result
= prime
* result
+ payload
.hashCode();
247 result
= prime
* result
;
249 result
= prime
* result
+ Arrays
.hashCode(fSourceMacAddress
);
250 result
= prime
* result
+ fType
;
255 public boolean equals(@Nullable Object obj
) {
262 if (getClass() != obj
.getClass()) {
265 EthernetIIPacket other
= (EthernetIIPacket
) obj
;
266 if(!NonNullUtils
.equalsNullable(fChildPacket
, other
.fChildPacket
)) {
269 if (!Arrays
.equals(fDestinationMacAddress
, other
.fDestinationMacAddress
)) {
272 if(!NonNullUtils
.equalsNullable(fPayload
, other
.fPayload
)) {
276 if (!Arrays
.equals(fSourceMacAddress
, other
.fSourceMacAddress
)) {
279 if (fType
!= other
.fType
) {