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 | ||
93d1d135 | 13 | package org.eclipse.linuxtools.internal.pcap.core.protocol.unknown; |
5255c030 VP |
14 | |
15 | import java.io.UnsupportedEncodingException; | |
16 | import java.nio.ByteBuffer; | |
17 | import java.util.Map; | |
18 | ||
19 | import org.eclipse.jdt.annotation.NonNull; | |
20 | import org.eclipse.jdt.annotation.Nullable; | |
93d1d135 | 21 | import org.eclipse.linuxtools.internal.pcap.core.packet.Packet; |
c88feda9 | 22 | import org.eclipse.linuxtools.internal.pcap.core.protocol.PcapProtocol; |
93d1d135 AM |
23 | import org.eclipse.linuxtools.internal.pcap.core.trace.PcapFile; |
24 | import org.eclipse.linuxtools.internal.pcap.core.util.ConversionHelper; | |
5255c030 VP |
25 | |
26 | import com.google.common.collect.ImmutableMap; | |
27 | import com.google.common.collect.ImmutableMap.Builder; | |
28 | ||
29 | /** | |
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 | |
33 | * inexistent. | |
34 | * | |
35 | * @author Vincent Perot | |
36 | */ | |
37 | public class UnknownPacket extends Packet { | |
38 | ||
39 | private final @Nullable Packet fChildPacket; | |
40 | private final ByteBuffer fPayload; | |
41 | ||
42 | private @Nullable UnknownEndpoint fSourceEndpoint; | |
43 | private @Nullable UnknownEndpoint fDestinationEndpoint; | |
44 | ||
45 | private @Nullable ImmutableMap<String, String> fFields; | |
46 | ||
47 | /** | |
48 | * Constructor of an Unknown Packet object. | |
49 | * | |
50 | * @param file | |
51 | * The file to which this packet belongs. | |
52 | * @param parent | |
53 | * The parent packet of this packet. | |
54 | * @param packet | |
55 | * The entire packet (header and payload). | |
56 | */ | |
57 | public UnknownPacket(PcapFile file, @Nullable Packet parent, ByteBuffer packet) { | |
c88feda9 | 58 | super(file, parent, PcapProtocol.UNKNOWN); |
5255c030 VP |
59 | |
60 | // The endpoints are lazy loaded. They are defined in the get*Endpoint() | |
61 | // methods. | |
62 | fSourceEndpoint = null; | |
63 | fDestinationEndpoint = null; | |
64 | ||
65 | fFields = null; | |
66 | ||
67 | // Header is not used. All data go into payload. | |
68 | fPayload = packet; | |
69 | ||
70 | fChildPacket = findChildPacket(); | |
71 | } | |
72 | ||
73 | @Override | |
74 | public @Nullable Packet getChildPacket() { | |
75 | return fChildPacket; | |
76 | } | |
77 | ||
78 | @Override | |
79 | public @Nullable ByteBuffer getPayload() { | |
80 | return fPayload; | |
81 | } | |
82 | ||
83 | @Override | |
84 | protected @Nullable Packet findChildPacket() { | |
85 | return null; | |
86 | } | |
87 | ||
88 | @Override | |
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; | |
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 | ImmutableMap<String, String> map = fFields; | |
131 | if (map == null) { | |
132 | @SuppressWarnings("null") | |
133 | @NonNull byte[] array = fPayload.array(); | |
134 | ||
135 | Builder<String, String> builder = ImmutableMap.<String, String> builder() | |
136 | .put("Binary", ConversionHelper.bytesToHex(array, true)); //$NON-NLS-1$ | |
137 | try { | |
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. | |
142 | } | |
143 | @SuppressWarnings("null") | |
144 | @NonNull ImmutableMap<String, String> newMap = builder.build(); | |
145 | fFields = newMap; | |
146 | return newMap; | |
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 = 1; | |
174 | final Packet child = fChildPacket; | |
175 | if (child != null) { | |
176 | result = prime * result + ((fChildPacket == null) ? 0 : child.hashCode()); | |
177 | } else { | |
178 | result = prime * result; | |
179 | } | |
180 | result = prime * result + fPayload.hashCode(); | |
181 | return result; | |
182 | } | |
183 | ||
184 | @Override | |
185 | public boolean equals(@Nullable Object obj) { | |
186 | if (this == obj) { | |
187 | return true; | |
188 | } | |
189 | if (obj == null) { | |
190 | return false; | |
191 | } | |
192 | if (getClass() != obj.getClass()) { | |
193 | return false; | |
194 | } | |
195 | UnknownPacket other = (UnknownPacket) obj; | |
196 | final Packet child = fChildPacket; | |
197 | if (child != null) { | |
198 | if (!child.equals(other.fChildPacket)) { | |
199 | return false; | |
200 | } | |
201 | } else { | |
202 | if (other.fChildPacket != null) { | |
203 | return false; | |
204 | } | |
205 | } | |
206 | ||
207 | if (!fPayload.equals(other.fPayload)) { | |
208 | return false; | |
209 | } | |
210 | return true; | |
211 | } | |
212 | ||
213 | } |