54a7c1cca6e8709d81d434954e5cc5247f515282
[deliverable/tracecompass.git] / org.eclipse.tracecompass.pcap.core / src / org / eclipse / tracecompass / internal / pcap / core / protocol / unknown / UnknownPacket.java
1 /*******************************************************************************
2 * Copyright (c) 2014 Ericsson
3 *
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
8 *
9 * Contributors:
10 * Vincent Perot - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.tracecompass.internal.pcap.core.protocol.unknown;
14
15 import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
16
17 import java.io.UnsupportedEncodingException;
18 import java.nio.ByteBuffer;
19 import java.util.Map;
20
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;
27
28 import com.google.common.collect.ImmutableMap;
29 import com.google.common.collect.ImmutableMap.Builder;
30
31 /**
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
35 * inexistent.
36 *
37 * @author Vincent Perot
38 */
39 public class UnknownPacket extends Packet {
40
41 private final @Nullable Packet fChildPacket;
42 private final ByteBuffer fPayload;
43
44 private @Nullable UnknownEndpoint fSourceEndpoint;
45 private @Nullable UnknownEndpoint fDestinationEndpoint;
46
47 private @Nullable Map<String, String> fFields;
48
49 /**
50 * Constructor of an Unknown Packet object.
51 *
52 * @param file
53 * The file to which this packet belongs.
54 * @param parent
55 * The parent packet of this packet.
56 * @param packet
57 * The entire packet (header and payload).
58 */
59 public UnknownPacket(PcapFile file, @Nullable Packet parent, ByteBuffer packet) {
60 super(file, parent, PcapProtocol.UNKNOWN);
61
62 // The endpoints are lazy loaded. They are defined in the get*Endpoint()
63 // methods.
64 fSourceEndpoint = null;
65 fDestinationEndpoint = null;
66
67 fFields = null;
68
69 // Header is not used. All data go into payload.
70 fPayload = packet;
71
72 fChildPacket = findChildPacket();
73 }
74
75 @Override
76 public @Nullable Packet getChildPacket() {
77 return fChildPacket;
78 }
79
80 @Override
81 public @Nullable ByteBuffer getPayload() {
82 return fPayload;
83 }
84
85 @Override
86 protected @Nullable Packet findChildPacket() {
87 return null;
88 }
89
90 @Override
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;
95 if (child != null) {
96 return string + child.toString();
97 }
98 return string;
99 }
100
101 @Override
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.
106 return true;
107 }
108
109 @Override
110 public UnknownEndpoint getSourceEndpoint() {
111 @Nullable
112 UnknownEndpoint endpoint = fSourceEndpoint;
113 if (endpoint == null) {
114 endpoint = new UnknownEndpoint(this, true);
115 }
116 fSourceEndpoint = endpoint;
117 return fSourceEndpoint;
118 }
119
120 @Override
121 public UnknownEndpoint getDestinationEndpoint() {
122 @Nullable
123 UnknownEndpoint endpoint = fDestinationEndpoint;
124 if (endpoint == null) {
125 endpoint = new UnknownEndpoint(this, false);
126 }
127 fDestinationEndpoint = endpoint;
128 return fDestinationEndpoint;
129 }
130
131 @Override
132 public Map<String, String> getFields() {
133 Map<String, String> map = fFields;
134 if (map == null) {
135 byte[] array = checkNotNull(fPayload.array());
136
137 Builder<String, String> builder = ImmutableMap.<String, String> builder()
138 .put("Binary", ConversionHelper.bytesToHex(array, true)); //$NON-NLS-1$
139 try {
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.
144 }
145 fFields = checkNotNull(builder.build());
146 return fFields;
147 }
148 return map;
149 }
150
151 @Override
152 public String getLocalSummaryString() {
153 return "Len: " + fPayload.array().length + " bytes"; //$NON-NLS-1$ //$NON-NLS-2$
154 }
155
156 @Override
157 protected String getSignificationString() {
158 return "Data: " + fPayload.array().length + " bytes"; //$NON-NLS-1$ //$NON-NLS-2$
159 }
160
161 @Override
162 public Packet getMostEcapsulatedPacket() {
163 Packet packet = this.getParentPacket();
164 if (packet == null) {
165 return this;
166 }
167 return packet;
168 }
169
170 @Override
171 public int hashCode() {
172 final int prime = 31;
173 int result = prime;
174 final Packet child = fChildPacket;
175 if (child != null) {
176 result += child.hashCode();
177 }
178 result = prime * result + fPayload.hashCode();
179 return result;
180 }
181
182 @Override
183 public boolean equals(@Nullable Object obj) {
184 if (this == obj) {
185 return true;
186 }
187 if (obj == null) {
188 return false;
189 }
190 if (getClass() != obj.getClass()) {
191 return false;
192 }
193 UnknownPacket other = (UnknownPacket) obj;
194 if (!NonNullUtils.equalsNullable(fChildPacket, other.fChildPacket)) {
195 return false;
196 }
197 if (!fPayload.equals(other.fPayload)) {
198 return false;
199 }
200 return true;
201 }
202
203 }
This page took 0.035441 seconds and 4 git commands to generate.