Commit | Line | Data |
---|---|---|
5255c030 VP |
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 | ||
71f2817f | 13 | package org.eclipse.tracecompass.internal.pcap.core.protocol.unknown; |
5255c030 | 14 | |
5db5a3a4 AM |
15 | import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull; |
16 | ||
5255c030 VP |
17 | import java.io.UnsupportedEncodingException; |
18 | import java.nio.ByteBuffer; | |
19 | import java.util.Map; | |
20 | ||
aa353506 | 21 | import org.eclipse.jdt.annotation.NonNull; |
5255c030 | 22 | import org.eclipse.jdt.annotation.Nullable; |
c10923a9 | 23 | import org.eclipse.tracecompass.common.core.NonNullUtils; |
71f2817f AM |
24 | import org.eclipse.tracecompass.internal.pcap.core.packet.Packet; |
25 | import org.eclipse.tracecompass.internal.pcap.core.protocol.PcapProtocol; | |
26 | import org.eclipse.tracecompass.internal.pcap.core.trace.PcapFile; | |
27 | import org.eclipse.tracecompass.internal.pcap.core.util.ConversionHelper; | |
5255c030 VP |
28 | |
29 | import com.google.common.collect.ImmutableMap; | |
30 | import com.google.common.collect.ImmutableMap.Builder; | |
31 | ||
32 | /** | |
33 | * Class that represents an Unknown packet. It is possible to get such a packet | |
34 | * if the protocol has not been implemented in this library or if the parent | |
35 | * packet was invalid (in certain cases only). The header of such a packet is | |
36 | * inexistent. | |
37 | * | |
38 | * @author Vincent Perot | |
39 | */ | |
40 | public class UnknownPacket extends Packet { | |
41 | ||
42 | private final @Nullable Packet fChildPacket; | |
43 | private final ByteBuffer fPayload; | |
44 | ||
45 | private @Nullable UnknownEndpoint fSourceEndpoint; | |
46 | private @Nullable UnknownEndpoint fDestinationEndpoint; | |
47 | ||
5db5a3a4 | 48 | private @Nullable Map<String, String> fFields; |
5255c030 VP |
49 | |
50 | /** | |
51 | * Constructor of an Unknown Packet object. | |
52 | * | |
53 | * @param file | |
54 | * The file to which this packet belongs. | |
55 | * @param parent | |
56 | * The parent packet of this packet. | |
57 | * @param packet | |
58 | * The entire packet (header and payload). | |
59 | */ | |
60 | public UnknownPacket(PcapFile file, @Nullable Packet parent, ByteBuffer packet) { | |
c88feda9 | 61 | super(file, parent, PcapProtocol.UNKNOWN); |
5255c030 VP |
62 | |
63 | // The endpoints are lazy loaded. They are defined in the get*Endpoint() | |
64 | // methods. | |
65 | fSourceEndpoint = null; | |
66 | fDestinationEndpoint = null; | |
67 | ||
68 | fFields = null; | |
69 | ||
70 | // Header is not used. All data go into payload. | |
71 | fPayload = packet; | |
72 | ||
73 | fChildPacket = findChildPacket(); | |
74 | } | |
75 | ||
76 | @Override | |
77 | public @Nullable Packet getChildPacket() { | |
78 | return fChildPacket; | |
79 | } | |
80 | ||
81 | @Override | |
82 | public @Nullable ByteBuffer getPayload() { | |
83 | return fPayload; | |
84 | } | |
85 | ||
86 | @Override | |
87 | protected @Nullable Packet findChildPacket() { | |
88 | return null; | |
89 | } | |
90 | ||
91 | @Override | |
92 | public String toString() { | |
5db5a3a4 | 93 | byte[] array = checkNotNull(fPayload.array()); |
5255c030 VP |
94 | String string = "Payload: " + ConversionHelper.bytesToHex(array, true); //$NON-NLS-1$ |
95 | final Packet child = fChildPacket; | |
96 | if (child != null) { | |
97 | return string + child.toString(); | |
98 | } | |
99 | return string; | |
100 | } | |
101 | ||
102 | @Override | |
103 | public boolean validate() { | |
104 | // Not yet implemented. ATM, we consider that all packets are valid. | |
105 | // This is the case for all packets. | |
106 | // TODO Implement it. | |
107 | return true; | |
108 | } | |
109 | ||
110 | @Override | |
111 | public UnknownEndpoint getSourceEndpoint() { | |
3be6b318 MK |
112 | @Nullable |
113 | UnknownEndpoint endpoint = fSourceEndpoint; | |
5255c030 VP |
114 | if (endpoint == null) { |
115 | endpoint = new UnknownEndpoint(this, true); | |
116 | } | |
117 | fSourceEndpoint = endpoint; | |
118 | return fSourceEndpoint; | |
119 | } | |
120 | ||
121 | @Override | |
122 | public UnknownEndpoint getDestinationEndpoint() { | |
3be6b318 MK |
123 | @Nullable |
124 | UnknownEndpoint endpoint = fDestinationEndpoint; | |
5255c030 VP |
125 | if (endpoint == null) { |
126 | endpoint = new UnknownEndpoint(this, false); | |
127 | } | |
128 | fDestinationEndpoint = endpoint; | |
129 | return fDestinationEndpoint; | |
130 | } | |
131 | ||
132 | @Override | |
133 | public Map<String, String> getFields() { | |
5db5a3a4 | 134 | Map<String, String> map = fFields; |
5255c030 | 135 | if (map == null) { |
5db5a3a4 | 136 | byte[] array = checkNotNull(fPayload.array()); |
5255c030 | 137 | |
aa353506 | 138 | Builder<String, String> builder = ImmutableMap.<@NonNull String, @NonNull String> builder() |
5255c030 VP |
139 | .put("Binary", ConversionHelper.bytesToHex(array, true)); //$NON-NLS-1$ |
140 | try { | |
141 | String s = new String(array, "UTF-8"); //$NON-NLS-1$ | |
142 | builder.put("Character", s); //$NON-NLS-1$ | |
143 | } catch (UnsupportedEncodingException e) { | |
144 | // Do nothing. The string won't be added to the map anyway. | |
145 | } | |
0e4f957e | 146 | fFields = builder.build(); |
5db5a3a4 | 147 | return fFields; |
5255c030 VP |
148 | } |
149 | return map; | |
150 | } | |
151 | ||
152 | @Override | |
153 | public String getLocalSummaryString() { | |
154 | return "Len: " + fPayload.array().length + " bytes"; //$NON-NLS-1$ //$NON-NLS-2$ | |
155 | } | |
156 | ||
157 | @Override | |
158 | protected String getSignificationString() { | |
159 | return "Data: " + fPayload.array().length + " bytes"; //$NON-NLS-1$ //$NON-NLS-2$ | |
160 | } | |
161 | ||
162 | @Override | |
163 | public Packet getMostEcapsulatedPacket() { | |
164 | Packet packet = this.getParentPacket(); | |
165 | if (packet == null) { | |
166 | return this; | |
167 | } | |
168 | return packet; | |
169 | } | |
170 | ||
171 | @Override | |
172 | public int hashCode() { | |
173 | final int prime = 31; | |
3be6b318 | 174 | int result = prime; |
5255c030 VP |
175 | final Packet child = fChildPacket; |
176 | if (child != null) { | |
3be6b318 | 177 | result += child.hashCode(); |
5255c030 VP |
178 | } |
179 | result = prime * result + fPayload.hashCode(); | |
180 | return result; | |
181 | } | |
182 | ||
183 | @Override | |
184 | public boolean equals(@Nullable Object obj) { | |
185 | if (this == obj) { | |
186 | return true; | |
187 | } | |
188 | if (obj == null) { | |
189 | return false; | |
190 | } | |
191 | if (getClass() != obj.getClass()) { | |
192 | return false; | |
193 | } | |
194 | UnknownPacket other = (UnknownPacket) obj; | |
3be6b318 | 195 | if (!NonNullUtils.equalsNullable(fChildPacket, other.fChildPacket)) { |
c10923a9 | 196 | return false; |
5255c030 | 197 | } |
5255c030 VP |
198 | if (!fPayload.equals(other.fPayload)) { |
199 | return false; | |
200 | } | |
201 | return true; | |
202 | } | |
203 | ||
204 | } |