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
.internal
.pcap
.core
.protocol
.unknown
;
15 import java
.io
.UnsupportedEncodingException
;
16 import java
.nio
.ByteBuffer
;
19 import org
.eclipse
.jdt
.annotation
.NonNull
;
20 import org
.eclipse
.jdt
.annotation
.Nullable
;
21 import org
.eclipse
.linuxtools
.internal
.pcap
.core
.packet
.Packet
;
22 import org
.eclipse
.linuxtools
.internal
.pcap
.core
.protocol
.PcapProtocol
;
23 import org
.eclipse
.linuxtools
.internal
.pcap
.core
.trace
.PcapFile
;
24 import org
.eclipse
.linuxtools
.internal
.pcap
.core
.util
.ConversionHelper
;
26 import com
.google
.common
.collect
.ImmutableMap
;
27 import com
.google
.common
.collect
.ImmutableMap
.Builder
;
30 * Class that represents an Unknown packet. It is possible to get such a packet
31 * if the protocol has not been implemented in this library or if the parent
32 * packet was invalid (in certain cases only). The header of such a packet is
35 * @author Vincent Perot
37 public class UnknownPacket
extends Packet
{
39 private final @Nullable Packet fChildPacket
;
40 private final ByteBuffer fPayload
;
42 private @Nullable UnknownEndpoint fSourceEndpoint
;
43 private @Nullable UnknownEndpoint fDestinationEndpoint
;
45 private @Nullable ImmutableMap
<String
, String
> fFields
;
48 * Constructor of an Unknown Packet object.
51 * The file to which this packet belongs.
53 * The parent packet of this packet.
55 * The entire packet (header and payload).
57 public UnknownPacket(PcapFile file
, @Nullable Packet parent
, ByteBuffer packet
) {
58 super(file
, parent
, PcapProtocol
.UNKNOWN
);
60 // The endpoints are lazy loaded. They are defined in the get*Endpoint()
62 fSourceEndpoint
= null;
63 fDestinationEndpoint
= null;
67 // Header is not used. All data go into payload.
70 fChildPacket
= findChildPacket();
74 public @Nullable Packet
getChildPacket() {
79 public @Nullable ByteBuffer
getPayload() {
84 protected @Nullable Packet
findChildPacket() {
89 public String
toString() {
90 @SuppressWarnings("null")
91 @NonNull byte[] array
= 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 ImmutableMap
<String
, String
> map
= fFields
;
132 @SuppressWarnings("null")
133 @NonNull byte[] array
= fPayload
.array();
135 Builder
<String
, String
> builder
= ImmutableMap
.<String
, String
> builder()
136 .put("Binary", ConversionHelper
.bytesToHex(array
, true)); //$NON-NLS-1$
138 String s
= new String(array
, "UTF-8"); //$NON-NLS-1$
139 builder
.put("Character", s
); //$NON-NLS-1$
140 } catch (UnsupportedEncodingException e
) {
141 // Do nothing. The string won't be added to the map anyway.
143 @SuppressWarnings("null")
144 @NonNull ImmutableMap
<String
, String
> newMap
= 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
= prime
* result
+ ((fChildPacket
== null) ?
0 : child
.hashCode());
178 result
= prime
* result
;
180 result
= prime
* result
+ fPayload
.hashCode();
185 public boolean equals(@Nullable Object obj
) {
192 if (getClass() != obj
.getClass()) {
195 UnknownPacket other
= (UnknownPacket
) obj
;
196 final Packet child
= fChildPacket
;
198 if (!child
.equals(other
.fChildPacket
)) {
202 if (other
.fChildPacket
!= null) {
207 if (!fPayload
.equals(other
.fPayload
)) {