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
.internal
.pcap
.core
.packet
.Packet
;
23 import org
.eclipse
.tracecompass
.internal
.pcap
.core
.protocol
.PcapProtocol
;
24 import org
.eclipse
.tracecompass
.internal
.pcap
.core
.trace
.PcapFile
;
25 import org
.eclipse
.tracecompass
.internal
.pcap
.core
.util
.ConversionHelper
;
27 import com
.google
.common
.collect
.ImmutableMap
;
28 import com
.google
.common
.collect
.ImmutableMap
.Builder
;
31 * Class that represents an Unknown packet. It is possible to get such a packet
32 * if the protocol has not been implemented in this library or if the parent
33 * packet was invalid (in certain cases only). The header of such a packet is
36 * @author Vincent Perot
38 public class UnknownPacket
extends Packet
{
40 private final @Nullable Packet fChildPacket
;
41 private final ByteBuffer fPayload
;
43 private @Nullable UnknownEndpoint fSourceEndpoint
;
44 private @Nullable UnknownEndpoint fDestinationEndpoint
;
46 private @Nullable Map
<String
, String
> fFields
;
49 * Constructor of an Unknown Packet object.
52 * The file to which this packet belongs.
54 * The parent packet of this packet.
56 * The entire packet (header and payload).
58 public UnknownPacket(PcapFile file
, @Nullable Packet parent
, ByteBuffer packet
) {
59 super(file
, parent
, PcapProtocol
.UNKNOWN
);
61 // The endpoints are lazy loaded. They are defined in the get*Endpoint()
63 fSourceEndpoint
= null;
64 fDestinationEndpoint
= null;
68 // Header is not used. All data go into payload.
71 fChildPacket
= findChildPacket();
75 public @Nullable Packet
getChildPacket() {
80 public @Nullable ByteBuffer
getPayload() {
85 protected @Nullable Packet
findChildPacket() {
90 public String
toString() {
91 byte[] array
= checkNotNull(fPayload
.array());
92 String string
= "Payload: " + ConversionHelper
.bytesToHex(array
, true); //$NON-NLS-1$
93 final Packet child
= fChildPacket
;
95 return string
+ child
.toString();
101 public boolean validate() {
102 // Not yet implemented. ATM, we consider that all packets are valid.
103 // This is the case for all packets.
104 // TODO Implement it.
109 public UnknownEndpoint
getSourceEndpoint() {
110 @Nullable UnknownEndpoint endpoint
= fSourceEndpoint
;
111 if (endpoint
== null) {
112 endpoint
= new UnknownEndpoint(this, true);
114 fSourceEndpoint
= endpoint
;
115 return fSourceEndpoint
;
119 public UnknownEndpoint
getDestinationEndpoint() {
120 @Nullable UnknownEndpoint endpoint
= fDestinationEndpoint
;
121 if (endpoint
== null) {
122 endpoint
= new UnknownEndpoint(this, false);
124 fDestinationEndpoint
= endpoint
;
125 return fDestinationEndpoint
;
129 public Map
<String
, String
> getFields() {
130 Map
<String
, String
> map
= fFields
;
132 byte[] array
= checkNotNull(fPayload
.array());
134 Builder
<String
, String
> builder
= ImmutableMap
.<String
, String
> builder()
135 .put("Binary", ConversionHelper
.bytesToHex(array
, true)); //$NON-NLS-1$
137 String s
= new String(array
, "UTF-8"); //$NON-NLS-1$
138 builder
.put("Character", s
); //$NON-NLS-1$
139 } catch (UnsupportedEncodingException e
) {
140 // Do nothing. The string won't be added to the map anyway.
142 fFields
= checkNotNull(builder
.build());
149 public String
getLocalSummaryString() {
150 return "Len: " + fPayload
.array().length
+ " bytes"; //$NON-NLS-1$ //$NON-NLS-2$
154 protected String
getSignificationString() {
155 return "Data: " + fPayload
.array().length
+ " bytes"; //$NON-NLS-1$ //$NON-NLS-2$
159 public Packet
getMostEcapsulatedPacket() {
160 Packet packet
= this.getParentPacket();
161 if (packet
== null) {
168 public int hashCode() {
169 final int prime
= 31;
171 final Packet child
= fChildPacket
;
173 result
= prime
* result
+ ((fChildPacket
== null) ?
0 : child
.hashCode());
175 result
= prime
* result
;
177 result
= prime
* result
+ fPayload
.hashCode();
182 public boolean equals(@Nullable Object obj
) {
189 if (getClass() != obj
.getClass()) {
192 UnknownPacket other
= (UnknownPacket
) obj
;
193 final Packet child
= fChildPacket
;
195 if (!child
.equals(other
.fChildPacket
)) {
199 if (other
.fChildPacket
!= null) {
204 if (!fPayload
.equals(other
.fPayload
)) {