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
.Nullable
;
22 import org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
;
23 import org
.eclipse
.tracecompass
.internal
.pcap
.core
.packet
.Packet
;
24 import org
.eclipse
.tracecompass
.internal
.pcap
.core
.protocol
.PcapProtocol
;
25 import org
.eclipse
.tracecompass
.internal
.pcap
.core
.trace
.PcapFile
;
26 import org
.eclipse
.tracecompass
.internal
.pcap
.core
.util
.ConversionHelper
;
28 import com
.google
.common
.collect
.ImmutableMap
;
29 import com
.google
.common
.collect
.ImmutableMap
.Builder
;
32 * Class that represents an Unknown packet. It is possible to get such a packet
33 * if the protocol has not been implemented in this library or if the parent
34 * packet was invalid (in certain cases only). The header of such a packet is
37 * @author Vincent Perot
39 public class UnknownPacket
extends Packet
{
41 private final @Nullable Packet fChildPacket
;
42 private final ByteBuffer fPayload
;
44 private @Nullable UnknownEndpoint fSourceEndpoint
;
45 private @Nullable UnknownEndpoint fDestinationEndpoint
;
47 private @Nullable Map
<String
, String
> fFields
;
50 * Constructor of an Unknown Packet object.
53 * The file to which this packet belongs.
55 * The parent packet of this packet.
57 * The entire packet (header and payload).
59 public UnknownPacket(PcapFile file
, @Nullable Packet parent
, ByteBuffer packet
) {
60 super(file
, parent
, PcapProtocol
.UNKNOWN
);
62 // The endpoints are lazy loaded. They are defined in the get*Endpoint()
64 fSourceEndpoint
= null;
65 fDestinationEndpoint
= null;
69 // Header is not used. All data go into payload.
72 fChildPacket
= findChildPacket();
76 public @Nullable Packet
getChildPacket() {
81 public @Nullable ByteBuffer
getPayload() {
86 protected @Nullable Packet
findChildPacket() {
91 public String
toString() {
92 byte[] array
= checkNotNull(fPayload
.array());
93 String string
= "Payload: " + ConversionHelper
.bytesToHex(array
, true); //$NON-NLS-1$
94 final Packet child
= fChildPacket
;
96 return string
+ child
.toString();
102 public boolean validate() {
103 // Not yet implemented. ATM, we consider that all packets are valid.
104 // This is the case for all packets.
105 // TODO Implement it.
110 public UnknownEndpoint
getSourceEndpoint() {
112 UnknownEndpoint endpoint
= fSourceEndpoint
;
113 if (endpoint
== null) {
114 endpoint
= new UnknownEndpoint(this, true);
116 fSourceEndpoint
= endpoint
;
117 return fSourceEndpoint
;
121 public UnknownEndpoint
getDestinationEndpoint() {
123 UnknownEndpoint endpoint
= fDestinationEndpoint
;
124 if (endpoint
== null) {
125 endpoint
= new UnknownEndpoint(this, false);
127 fDestinationEndpoint
= endpoint
;
128 return fDestinationEndpoint
;
132 public Map
<String
, String
> getFields() {
133 Map
<String
, String
> map
= fFields
;
135 byte[] array
= checkNotNull(fPayload
.array());
137 Builder
<String
, String
> builder
= ImmutableMap
.<String
, String
> builder()
138 .put("Binary", ConversionHelper
.bytesToHex(array
, true)); //$NON-NLS-1$
140 String s
= new String(array
, "UTF-8"); //$NON-NLS-1$
141 builder
.put("Character", s
); //$NON-NLS-1$
142 } catch (UnsupportedEncodingException e
) {
143 // Do nothing. The string won't be added to the map anyway.
145 fFields
= checkNotNull(builder
.build());
152 public String
getLocalSummaryString() {
153 return "Len: " + fPayload
.array().length
+ " bytes"; //$NON-NLS-1$ //$NON-NLS-2$
157 protected String
getSignificationString() {
158 return "Data: " + fPayload
.array().length
+ " bytes"; //$NON-NLS-1$ //$NON-NLS-2$
162 public Packet
getMostEcapsulatedPacket() {
163 Packet packet
= this.getParentPacket();
164 if (packet
== null) {
171 public int hashCode() {
172 final int prime
= 31;
174 final Packet child
= fChildPacket
;
176 result
+= child
.hashCode();
178 result
= prime
* result
+ fPayload
.hashCode();
183 public boolean equals(@Nullable Object obj
) {
190 if (getClass() != obj
.getClass()) {
193 UnknownPacket other
= (UnknownPacket
) obj
;
194 if (!NonNullUtils
.equalsNullable(fChildPacket
, other
.fChildPacket
)) {
197 if (!fPayload
.equals(other
.fPayload
)) {