pcap: Make all the packages internal
[deliverable/tracecompass.git] / org.eclipse.linuxtools.pcap.core / src / org / eclipse / linuxtools / internal / pcap / core / protocol / udp / UDPPacket.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.linuxtools.internal.pcap.core.protocol.udp;
14
15 import java.nio.ByteBuffer;
16 import java.nio.ByteOrder;
17 import java.util.Map;
18
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.eclipse.linuxtools.internal.pcap.core.packet.BadPacketException;
22 import org.eclipse.linuxtools.internal.pcap.core.packet.Packet;
23 import org.eclipse.linuxtools.internal.pcap.core.protocol.Protocol;
24 import org.eclipse.linuxtools.internal.pcap.core.protocol.unknown.UnknownPacket;
25 import org.eclipse.linuxtools.internal.pcap.core.trace.PcapFile;
26 import org.eclipse.linuxtools.internal.pcap.core.util.ConversionHelper;
27
28 import com.google.common.collect.ImmutableMap;
29
30 /**
31 * Class that represents a UDP packet.
32 *
33 * @author Vincent Perot
34 */
35 public class UDPPacket extends Packet {
36
37 private final @Nullable Packet fChildPacket;
38 private final @Nullable ByteBuffer fPayload;
39
40 private final int fSourcePort;
41 private final int fDestinationPort;
42 private final int fTotalLength;
43 private final int fChecksum;
44
45 private @Nullable UDPEndpoint fSourceEndpoint;
46 private @Nullable UDPEndpoint fDestinationEndpoint;
47
48 private @Nullable ImmutableMap<String, String> fFields;
49
50 /**
51 * Constructor of the UDP Packet class.
52 *
53 * @param file
54 * The file that contains this packet.
55 * @param parent
56 * The parent packet of this packet (the encapsulating packet).
57 * @param packet
58 * The entire packet (header and payload).
59 * @throws BadPacketException
60 * Thrown when the packet is erroneous.
61 */
62 public UDPPacket(PcapFile file, @Nullable Packet parent, ByteBuffer packet) throws BadPacketException {
63 super(file, parent, Protocol.UDP);
64
65 // The endpoints are lazy loaded. They are defined in the get*Endpoint()
66 // methods.
67 fSourceEndpoint = null;
68 fDestinationEndpoint = null;
69
70 fFields = null;
71
72 packet.order(ByteOrder.BIG_ENDIAN);
73 packet.position(0);
74
75 fSourcePort = ConversionHelper.unsignedShortToInt(packet.getShort());
76 fDestinationPort = ConversionHelper.unsignedShortToInt(packet.getShort());
77 fTotalLength = ConversionHelper.unsignedShortToInt(packet.getShort());
78 fChecksum = ConversionHelper.unsignedShortToInt(packet.getShort());
79
80 if (packet.array().length - packet.position() > 0) {
81 byte[] array = new byte[packet.array().length - packet.position()];
82 packet.get(array);
83
84 ByteBuffer payload = ByteBuffer.wrap(array);
85 payload.order(ByteOrder.BIG_ENDIAN);
86 payload.position(0);
87 fPayload = payload;
88 } else {
89 fPayload = null;
90 }
91
92 // Find child
93 fChildPacket = findChildPacket();
94
95 }
96
97 @Override
98 public @Nullable Packet getChildPacket() {
99 return fChildPacket;
100 }
101
102 @Override
103 public @Nullable ByteBuffer getPayload() {
104 return fPayload;
105 }
106
107 /**
108 * {@inheritDoc}
109 *
110 * See http://www.iana.org/assignments/service-names-port-numbers/service-
111 * names-port-numbers.xhtml or
112 * http://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers
113 */
114 @Override
115 protected @Nullable Packet findChildPacket() throws BadPacketException {
116 // TODO implement further protocols and update this
117 ByteBuffer payload = fPayload;
118 if (payload == null) {
119 return null;
120 }
121
122 return new UnknownPacket(getPcapFile(), this, payload);
123 }
124
125 @Override
126 public String toString() {
127 String string = getProtocol().getName() + ", Source Port: " + fSourcePort + ", Destination Port: " + fDestinationPort + //$NON-NLS-1$ //$NON-NLS-2$
128 ", Length: " + fTotalLength + ", Checksum: " + fChecksum + "\n"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
129 final Packet child = fChildPacket;
130 if (child != null) {
131 return string + child.toString();
132 }
133 return string;
134 }
135
136 /**
137 * Getter method that returns the UDP Source Port.
138 *
139 * @return The source Port.
140 */
141 public int getSourcePort() {
142 return fSourcePort;
143 }
144
145 /**
146 * Getter method that returns the UDP Destination Port.
147 *
148 * @return The destination Port.
149 */
150 public int getDestinationPort() {
151 return fDestinationPort;
152 }
153
154 /**
155 * Getter method that returns the total length of the packet in bytes. The
156 * values it can take go from 8 to 65,515.
157 *
158 * @return The total length of the packet in bytes.
159 */
160 public int getTotalLength() {
161 return fTotalLength;
162 }
163
164 /**
165 * Getter method that returns the checksum (on header and payload). If the
166 * transmitter does not use this field, it is set to zero. This checksum
167 * might be wrong if the packet is erroneous.
168 *
169 * @return The checksum received from the packet.
170 */
171 public int getChecksum() {
172 return fChecksum;
173 }
174
175 @Override
176 public boolean validate() {
177 // Not yet implemented. ATM, we consider that all packets are valid.
178 // This is the case for all packets.
179 // TODO Implement it.
180 return true;
181 }
182
183 @Override
184 public UDPEndpoint getSourceEndpoint() {
185 @Nullable
186 UDPEndpoint endpoint = fSourceEndpoint;
187 if (endpoint == null) {
188 endpoint = new UDPEndpoint(this, true);
189 }
190 fSourceEndpoint = endpoint;
191 return fSourceEndpoint;
192 }
193
194 @Override
195 public UDPEndpoint getDestinationEndpoint() {
196 @Nullable UDPEndpoint endpoint = fDestinationEndpoint;
197 if (endpoint == null) {
198 endpoint = new UDPEndpoint(this, false);
199 }
200 fDestinationEndpoint = endpoint;
201 return fDestinationEndpoint;
202 }
203
204 @Override
205 public Map<String, String> getFields() {
206 ImmutableMap<String, String> map = fFields;
207 if (map == null) {
208 @SuppressWarnings("null")
209 @NonNull ImmutableMap<String, String> newMap = ImmutableMap.<String, String> builder()
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$
213 .put("Checksum", String.format("%s%04x", "0x", fChecksum)) //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
214 .build();
215 fFields = newMap;
216 return newMap;
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 }
269 final Packet child = fChildPacket;
270 if (child != null) {
271 if (!child.equals(other.fChildPacket)) {
272 return false;
273 }
274 } else {
275 if (other.fChildPacket != null) {
276 return false;
277 }
278 }
279 if (fDestinationPort != other.fDestinationPort) {
280 return false;
281 }
282 final ByteBuffer payload = fPayload;
283 if (payload != null) {
284 if (!payload.equals(other.fPayload)) {
285 return false;
286 }
287 } else {
288 if (other.fPayload != null) {
289 return false;
290 }
291 }
292 if (fSourcePort != other.fSourcePort) {
293 return false;
294 }
295 if (fTotalLength != other.fTotalLength) {
296 return false;
297 }
298 return true;
299 }
300
301 }
This page took 0.037508 seconds and 5 git commands to generate.