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.udp; |
5255c030 VP |
14 | |
15 | import java.nio.ByteBuffer; | |
16 | import java.nio.ByteOrder; | |
17 | import java.util.Map; | |
18 | ||
aa353506 | 19 | import org.eclipse.jdt.annotation.NonNull; |
5255c030 | 20 | import org.eclipse.jdt.annotation.Nullable; |
5db5a3a4 | 21 | import org.eclipse.tracecompass.common.core.NonNullUtils; |
71f2817f AM |
22 | import org.eclipse.tracecompass.internal.pcap.core.packet.BadPacketException; |
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.protocol.unknown.UnknownPacket; | |
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 | ||
31 | /** | |
32 | * Class that represents a UDP packet. | |
33 | * | |
34 | * @author Vincent Perot | |
35 | */ | |
36 | public class UDPPacket extends Packet { | |
37 | ||
38 | private final @Nullable Packet fChildPacket; | |
39 | private final @Nullable ByteBuffer fPayload; | |
40 | ||
41 | private final int fSourcePort; | |
42 | private final int fDestinationPort; | |
43 | private final int fTotalLength; | |
44 | private final int fChecksum; | |
45 | ||
46 | private @Nullable UDPEndpoint fSourceEndpoint; | |
47 | private @Nullable UDPEndpoint fDestinationEndpoint; | |
48 | ||
5db5a3a4 | 49 | private @Nullable Map<String, String> fFields; |
5255c030 VP |
50 | |
51 | /** | |
52 | * Constructor of the UDP Packet class. | |
53 | * | |
54 | * @param file | |
55 | * The file that contains this packet. | |
56 | * @param parent | |
57 | * The parent packet of this packet (the encapsulating packet). | |
58 | * @param packet | |
59 | * The entire packet (header and payload). | |
60 | * @throws BadPacketException | |
61 | * Thrown when the packet is erroneous. | |
62 | */ | |
63 | public UDPPacket(PcapFile file, @Nullable Packet parent, ByteBuffer packet) throws BadPacketException { | |
c88feda9 | 64 | super(file, parent, PcapProtocol.UDP); |
5255c030 VP |
65 | |
66 | // The endpoints are lazy loaded. They are defined in the get*Endpoint() | |
67 | // methods. | |
68 | fSourceEndpoint = null; | |
69 | fDestinationEndpoint = null; | |
70 | ||
71 | fFields = null; | |
72 | ||
73 | packet.order(ByteOrder.BIG_ENDIAN); | |
74 | packet.position(0); | |
75 | ||
76 | fSourcePort = ConversionHelper.unsignedShortToInt(packet.getShort()); | |
77 | fDestinationPort = ConversionHelper.unsignedShortToInt(packet.getShort()); | |
78 | fTotalLength = ConversionHelper.unsignedShortToInt(packet.getShort()); | |
79 | fChecksum = ConversionHelper.unsignedShortToInt(packet.getShort()); | |
80 | ||
81 | if (packet.array().length - packet.position() > 0) { | |
82 | byte[] array = new byte[packet.array().length - packet.position()]; | |
83 | packet.get(array); | |
84 | ||
85 | ByteBuffer payload = ByteBuffer.wrap(array); | |
86 | payload.order(ByteOrder.BIG_ENDIAN); | |
87 | payload.position(0); | |
88 | fPayload = payload; | |
89 | } else { | |
90 | fPayload = null; | |
91 | } | |
92 | ||
93 | // Find child | |
94 | fChildPacket = findChildPacket(); | |
95 | ||
96 | } | |
97 | ||
98 | @Override | |
99 | public @Nullable Packet getChildPacket() { | |
100 | return fChildPacket; | |
101 | } | |
102 | ||
103 | @Override | |
104 | public @Nullable ByteBuffer getPayload() { | |
105 | return fPayload; | |
106 | } | |
107 | ||
108 | /** | |
109 | * {@inheritDoc} | |
110 | * | |
111 | * See http://www.iana.org/assignments/service-names-port-numbers/service- | |
112 | * names-port-numbers.xhtml or | |
113 | * http://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers | |
114 | */ | |
115 | @Override | |
116 | protected @Nullable Packet findChildPacket() throws BadPacketException { | |
117 | // TODO implement further protocols and update this | |
118 | ByteBuffer payload = fPayload; | |
119 | if (payload == null) { | |
120 | return null; | |
121 | } | |
122 | ||
123 | return new UnknownPacket(getPcapFile(), this, payload); | |
124 | } | |
125 | ||
126 | @Override | |
127 | public String toString() { | |
128 | String string = getProtocol().getName() + ", Source Port: " + fSourcePort + ", Destination Port: " + fDestinationPort + //$NON-NLS-1$ //$NON-NLS-2$ | |
129 | ", Length: " + fTotalLength + ", Checksum: " + fChecksum + "\n"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ | |
130 | final Packet child = fChildPacket; | |
131 | if (child != null) { | |
132 | return string + child.toString(); | |
133 | } | |
134 | return string; | |
135 | } | |
136 | ||
137 | /** | |
138 | * Getter method that returns the UDP Source Port. | |
139 | * | |
140 | * @return The source Port. | |
141 | */ | |
142 | public int getSourcePort() { | |
143 | return fSourcePort; | |
144 | } | |
145 | ||
146 | /** | |
147 | * Getter method that returns the UDP Destination Port. | |
148 | * | |
149 | * @return The destination Port. | |
150 | */ | |
151 | public int getDestinationPort() { | |
152 | return fDestinationPort; | |
153 | } | |
154 | ||
155 | /** | |
156 | * Getter method that returns the total length of the packet in bytes. The | |
157 | * values it can take go from 8 to 65,515. | |
158 | * | |
159 | * @return The total length of the packet in bytes. | |
160 | */ | |
161 | public int getTotalLength() { | |
162 | return fTotalLength; | |
163 | } | |
164 | ||
165 | /** | |
166 | * Getter method that returns the checksum (on header and payload). If the | |
167 | * transmitter does not use this field, it is set to zero. This checksum | |
168 | * might be wrong if the packet is erroneous. | |
169 | * | |
170 | * @return The checksum received from the packet. | |
171 | */ | |
172 | public int getChecksum() { | |
173 | return fChecksum; | |
174 | } | |
175 | ||
176 | @Override | |
177 | public boolean validate() { | |
178 | // Not yet implemented. ATM, we consider that all packets are valid. | |
179 | // This is the case for all packets. | |
180 | // TODO Implement it. | |
181 | return true; | |
182 | } | |
183 | ||
184 | @Override | |
185 | public UDPEndpoint getSourceEndpoint() { | |
186 | @Nullable | |
187 | UDPEndpoint endpoint = fSourceEndpoint; | |
188 | if (endpoint == null) { | |
189 | endpoint = new UDPEndpoint(this, true); | |
190 | } | |
191 | fSourceEndpoint = endpoint; | |
192 | return fSourceEndpoint; | |
193 | } | |
194 | ||
195 | @Override | |
196 | public UDPEndpoint getDestinationEndpoint() { | |
197 | @Nullable UDPEndpoint endpoint = fDestinationEndpoint; | |
198 | if (endpoint == null) { | |
199 | endpoint = new UDPEndpoint(this, false); | |
200 | } | |
201 | fDestinationEndpoint = endpoint; | |
202 | return fDestinationEndpoint; | |
203 | } | |
204 | ||
205 | @Override | |
206 | public Map<String, String> getFields() { | |
5db5a3a4 | 207 | Map<String, String> map = fFields; |
5255c030 | 208 | if (map == null) { |
aa353506 | 209 | ImmutableMap.Builder<String, String> builder = ImmutableMap.<@NonNull String, @NonNull String> builder() |
5255c030 VP |
210 | .put("Source Port", String.valueOf(fSourcePort)) //$NON-NLS-1$ |
211 | .put("Destination Port", String.valueOf(fDestinationPort)) //$NON-NLS-1$ | |
212 | .put("Length", String.valueOf(fTotalLength) + " bytes") //$NON-NLS-1$ //$NON-NLS-2$ | |
5db5a3a4 AM |
213 | .put("Checksum", String.format("%s%04x", "0x", fChecksum)); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ |
214 | ||
215 | fFields = NonNullUtils.checkNotNull(builder.build()); | |
216 | return fFields; | |
5255c030 VP |
217 | } |
218 | return map; | |
219 | } | |
220 | ||
221 | @Override | |
222 | public String getLocalSummaryString() { | |
223 | return "Src Port: " + fSourcePort + ", Dst Port: " + fDestinationPort; //$NON-NLS-1$ //$NON-NLS-2$ | |
224 | } | |
225 | ||
226 | @Override | |
227 | protected String getSignificationString() { | |
228 | return "Source Port: " + fSourcePort + ", Destination Port: " + fDestinationPort; //$NON-NLS-1$ //$NON-NLS-2$ | |
229 | } | |
230 | ||
231 | @Override | |
232 | public int hashCode() { | |
233 | final int prime = 31; | |
234 | int result = 1; | |
235 | result = prime * result + fChecksum; | |
236 | final Packet child = fChildPacket; | |
237 | if (child != null) { | |
238 | result = prime * result + child.hashCode(); | |
239 | } else { | |
240 | result = prime * result; | |
241 | } | |
242 | result = prime * result + fDestinationPort; | |
243 | final ByteBuffer payload = fPayload; | |
244 | if (payload != null) { | |
245 | result = prime * result + payload.hashCode(); | |
246 | } else { | |
247 | result = prime * result; | |
248 | } | |
249 | result = prime * result + fSourcePort; | |
250 | result = prime * result + fTotalLength; | |
251 | return result; | |
252 | } | |
253 | ||
254 | @Override | |
255 | public boolean equals(@Nullable Object obj) { | |
256 | if (this == obj) { | |
257 | return true; | |
258 | } | |
259 | if (obj == null) { | |
260 | return false; | |
261 | } | |
262 | if (getClass() != obj.getClass()) { | |
263 | return false; | |
264 | } | |
265 | UDPPacket other = (UDPPacket) obj; | |
266 | if (fChecksum != other.fChecksum) { | |
267 | return false; | |
268 | } | |
c10923a9 MK |
269 | if(!NonNullUtils.equalsNullable(fChildPacket, other.fChildPacket)){ |
270 | return false; | |
5255c030 VP |
271 | } |
272 | if (fDestinationPort != other.fDestinationPort) { | |
273 | return false; | |
274 | } | |
c10923a9 MK |
275 | if(!NonNullUtils.equalsNullable(fPayload, other.fPayload)){ |
276 | return false; | |
5255c030 VP |
277 | } |
278 | if (fSourcePort != other.fSourcePort) { | |
279 | return false; | |
280 | } | |
281 | if (fTotalLength != other.fTotalLength) { | |
282 | return false; | |
283 | } | |
284 | return true; | |
285 | } | |
286 | ||
287 | } |