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 /* We store MAC addresses as byte arrays since
46 * there is no standard java class to store them. */
47 private final byte[] fSourceMacAddress
;
48 private final byte[] fDestinationMacAddress
;
50 private final int fType
;
52 private @Nullable EthernetIIEndpoint fSourceEndpoint
;
53 private @Nullable EthernetIIEndpoint fDestinationEndpoint
;
55 private @Nullable ImmutableMap
<String
, String
> fFields
;
58 * Constructor of the Ethernet Packet class.
61 * The file that contains this packet.
63 * The parent packet of this packet (the encapsulating packet).
65 * The entire packet (header and payload).
66 * @throws BadPacketException
67 * Thrown when the packet is erroneous.
69 public EthernetIIPacket(PcapFile file
, @Nullable Packet parent
, ByteBuffer packet
) throws BadPacketException
{
70 super(file
, parent
, Protocol
.ETHERNET_II
);
72 if (packet
.array().length
<= EthernetIIValues
.ETHERNET_II_MIN_SIZE
) {
73 throw new BadPacketException("An Ethernet II packet can't be smaller than 14 bytes."); //$NON-NLS-1$
76 // The endpoints are lazy loaded. They are defined in the get*Endpoint()
78 fSourceEndpoint
= null;
79 fDestinationEndpoint
= null;
83 fDestinationMacAddress
= new byte[EthernetIIValues
.MAC_ADDRESS_SIZE
];
84 fSourceMacAddress
= new byte[EthernetIIValues
.MAC_ADDRESS_SIZE
];
85 packet
.order(ByteOrder
.BIG_ENDIAN
);
87 packet
.get(fDestinationMacAddress
);
88 packet
.get(fSourceMacAddress
);
89 fType
= ConversionHelper
.unsignedShortToInt(packet
.getShort());
91 // Get payload if it exists.
92 if (packet
.array().length
- packet
.position() > 0) {
93 byte[] array
= new byte[packet
.array().length
- packet
.position()];
95 ByteBuffer payload
= ByteBuffer
.wrap(array
);
96 if (payload
!= null) {
97 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 @SuppressWarnings("null")
128 @NonNull byte[] mac
= Arrays
.copyOf(fSourceMacAddress
, fSourceMacAddress
.length
);
133 * Getter method for the destination MAC Address.
135 * @return The destination MAC address.
137 public byte[] getDestinationMacAddress() {
138 @SuppressWarnings("null")
139 @NonNull byte[] mac
= Arrays
.copyOf(fDestinationMacAddress
, fDestinationMacAddress
.length
);
144 * Getter method for Ethertype. See
145 * http://standards.ieee.org/develop/regauth/ethertype/eth.txt
147 * @return The Ethertype. This is used to determine the child packet..
149 public int getEthertype() {
154 protected @Nullable Packet
findChildPacket() throws BadPacketException
{
155 // TODO Add more protocols.
156 ByteBuffer payload
= fPayload
;
157 if (payload
== null) {
161 case EthertypeHelper
.ETHERTYPE_IPV4
:
162 return new IPv4Packet(getPcapFile(), this, payload
);
164 return new UnknownPacket(getPcapFile(), this, payload
);
169 public String
toString() {
170 String string
= getProtocol().getName() + ", Source: " + ConversionHelper
.toMacAddress(fSourceMacAddress
) + //$NON-NLS-1$
171 ", Destination: " + ConversionHelper
.toMacAddress(fDestinationMacAddress
) + ", Type: " + //$NON-NLS-1$ //$NON-NLS-2$
172 EthertypeHelper
.toEtherType(fType
) + "\n"; //$NON-NLS-1$
173 final Packet child
= fChildPacket
;
175 return string
+ child
.toString();
181 public boolean validate() {
182 // Not yet implemented. ATM, we consider that all packets are valid.
183 // This is the case for all packets.
184 // TODO Implement it.
189 public EthernetIIEndpoint
getSourceEndpoint() {
190 @Nullable EthernetIIEndpoint endpoint
= fSourceEndpoint
;
191 if (endpoint
== null) {
192 endpoint
= new EthernetIIEndpoint(this, true);
194 fSourceEndpoint
= endpoint
;
195 return fSourceEndpoint
;
199 public EthernetIIEndpoint
getDestinationEndpoint() {
200 @Nullable EthernetIIEndpoint endpoint
= fDestinationEndpoint
;
202 if (endpoint
== null) {
203 endpoint
= new EthernetIIEndpoint(this, false);
205 fDestinationEndpoint
= endpoint
;
206 return fDestinationEndpoint
;
210 public Map
<String
, String
> getFields() {
211 ImmutableMap
<String
, String
> map
= fFields
;
213 @SuppressWarnings("null")
214 @NonNull ImmutableMap
<String
, String
> newMap
= ImmutableMap
.<String
, String
> builder()
215 .put("Source MAC Address", ConversionHelper
.toMacAddress(fSourceMacAddress
)) //$NON-NLS-1$
216 .put("Destination MAC Address", ConversionHelper
.toMacAddress(fDestinationMacAddress
)) //$NON-NLS-1$
217 .put("Ethertype", String
.valueOf(EthertypeHelper
.toEtherType(fType
))) //$NON-NLS-1$
226 public String
getLocalSummaryString() {
227 return "Src: " + ConversionHelper
.toMacAddress(fSourceMacAddress
) + " , Dst: " + ConversionHelper
.toMacAddress(fDestinationMacAddress
); //$NON-NLS-1$ //$NON-NLS-2$
231 protected String
getSignificationString() {
232 return "Source MAC: " + ConversionHelper
.toMacAddress(fSourceMacAddress
) + " , Destination MAC: " + ConversionHelper
.toMacAddress(fDestinationMacAddress
); //$NON-NLS-1$ //$NON-NLS-2$
236 public int hashCode() {
237 final int prime
= 31;
239 final Packet child
= fChildPacket
;
241 result
= prime
* result
+ child
.hashCode();
243 result
= prime
* result
;
245 result
= prime
* result
+ Arrays
.hashCode(fDestinationMacAddress
);
246 final ByteBuffer payload
= fPayload
;
247 if (payload
!= null) {
248 result
= prime
* result
+ payload
.hashCode();
250 result
= prime
* result
;
252 result
= prime
* result
+ Arrays
.hashCode(fSourceMacAddress
);
253 result
= prime
* result
+ fType
;
258 public boolean equals(@Nullable Object obj
) {
265 if (getClass() != obj
.getClass()) {
268 EthernetIIPacket other
= (EthernetIIPacket
) obj
;
269 if (fChildPacket
== null) {
270 if (other
.fChildPacket
!= null) {
274 final Packet child
= fChildPacket
;
276 if (!child
.equals(other
.fChildPacket
)) {
280 if (other
.fChildPacket
!= null) {
285 if (!Arrays
.equals(fDestinationMacAddress
, other
.fDestinationMacAddress
)) {
288 if (fPayload
== null) {
289 if (other
.fPayload
!= null) {
293 final ByteBuffer payload
= fPayload
;
294 if (payload
!= null) {
295 if (!payload
.equals(other
.fPayload
)) {
299 if (other
.fPayload
!= null) {
304 if (!Arrays
.equals(fSourceMacAddress
, other
.fSourceMacAddress
)) {
307 if (fType
!= other
.fType
) {