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