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
.unknown
;
15 import static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.checkNotNull
;
17 import java
.io
.UnsupportedEncodingException
;
18 import java
.nio
.ByteBuffer
;
21 import org
.eclipse
.jdt
.annotation
.NonNull
;
22 import org
.eclipse
.jdt
.annotation
.Nullable
;
23 import org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
;
24 import org
.eclipse
.tracecompass
.internal
.pcap
.core
.packet
.Packet
;
25 import org
.eclipse
.tracecompass
.internal
.pcap
.core
.protocol
.PcapProtocol
;
26 import org
.eclipse
.tracecompass
.internal
.pcap
.core
.trace
.PcapFile
;
27 import org
.eclipse
.tracecompass
.internal
.pcap
.core
.util
.ConversionHelper
;
29 import com
.google
.common
.collect
.ImmutableMap
;
30 import com
.google
.common
.collect
.ImmutableMap
.Builder
;
33 * Class that represents an Unknown packet. It is possible to get such a packet
34 * if the protocol has not been implemented in this library or if the parent
35 * packet was invalid (in certain cases only). The header of such a packet is
38 * @author Vincent Perot
40 public class UnknownPacket
extends Packet
{
42 private final @Nullable Packet fChildPacket
;
43 private final ByteBuffer fPayload
;
45 private @Nullable UnknownEndpoint fSourceEndpoint
;
46 private @Nullable UnknownEndpoint fDestinationEndpoint
;
48 private @Nullable Map
<String
, String
> fFields
;
51 * Constructor of an Unknown Packet object.
54 * The file to which this packet belongs.
56 * The parent packet of this packet.
58 * The entire packet (header and payload).
60 public UnknownPacket(PcapFile file
, @Nullable Packet parent
, ByteBuffer packet
) {
61 super(file
, parent
, PcapProtocol
.UNKNOWN
);
63 // The endpoints are lazy loaded. They are defined in the get*Endpoint()
65 fSourceEndpoint
= null;
66 fDestinationEndpoint
= null;
70 // Header is not used. All data go into payload.
73 fChildPacket
= findChildPacket();
77 public @Nullable Packet
getChildPacket() {
82 public @Nullable ByteBuffer
getPayload() {
87 protected @Nullable Packet
findChildPacket() {
92 public String
toString() {
93 byte[] array
= checkNotNull(fPayload
.array());
94 String string
= "Payload: " + ConversionHelper
.bytesToHex(array
, true); //$NON-NLS-1$
95 final Packet child
= fChildPacket
;
97 return string
+ child
.toString();
103 public boolean validate() {
104 // Not yet implemented. ATM, we consider that all packets are valid.
105 // This is the case for all packets.
106 // TODO Implement it.
111 public UnknownEndpoint
getSourceEndpoint() {
113 UnknownEndpoint endpoint
= fSourceEndpoint
;
114 if (endpoint
== null) {
115 endpoint
= new UnknownEndpoint(this, true);
117 fSourceEndpoint
= endpoint
;
118 return fSourceEndpoint
;
122 public UnknownEndpoint
getDestinationEndpoint() {
124 UnknownEndpoint endpoint
= fDestinationEndpoint
;
125 if (endpoint
== null) {
126 endpoint
= new UnknownEndpoint(this, false);
128 fDestinationEndpoint
= endpoint
;
129 return fDestinationEndpoint
;
133 public Map
<String
, String
> getFields() {
134 Map
<String
, String
> map
= fFields
;
136 byte[] array
= checkNotNull(fPayload
.array());
138 Builder
<String
, String
> builder
= ImmutableMap
.<@NonNull String
, @NonNull String
> builder()
139 .put("Binary", ConversionHelper
.bytesToHex(array
, true)); //$NON-NLS-1$
141 String s
= new String(array
, "UTF-8"); //$NON-NLS-1$
142 builder
.put("Character", s
); //$NON-NLS-1$
143 } catch (UnsupportedEncodingException e
) {
144 // Do nothing. The string won't be added to the map anyway.
146 fFields
= builder
.build();
153 public String
getLocalSummaryString() {
154 return "Len: " + fPayload
.array().length
+ " bytes"; //$NON-NLS-1$ //$NON-NLS-2$
158 protected String
getSignificationString() {
159 return "Data: " + fPayload
.array().length
+ " bytes"; //$NON-NLS-1$ //$NON-NLS-2$
163 public Packet
getMostEcapsulatedPacket() {
164 Packet packet
= this.getParentPacket();
165 if (packet
== null) {
172 public int hashCode() {
173 final int prime
= 31;
175 final Packet child
= fChildPacket
;
177 result
+= child
.hashCode();
179 result
= prime
* result
+ fPayload
.hashCode();
184 public boolean equals(@Nullable Object obj
) {
191 if (getClass() != obj
.getClass()) {
194 UnknownPacket other
= (UnknownPacket
) obj
;
195 if (!NonNullUtils
.equalsNullable(fChildPacket
, other
.fChildPacket
)) {
198 if (!fPayload
.equals(other
.fPayload
)) {