1 /*******************************************************************************
2 * Copyright (c) 2014 Ericsson
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
10 * Vincent Perot - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.internal
.pcap
.core
.protocol
.udp
;
15 import java
.nio
.ByteBuffer
;
16 import java
.nio
.ByteOrder
;
19 import org
.eclipse
.jdt
.annotation
.NonNull
;
20 import org
.eclipse
.jdt
.annotation
.Nullable
;
21 import org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
;
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
;
29 import com
.google
.common
.collect
.ImmutableMap
;
32 * Class that represents a UDP packet.
34 * @author Vincent Perot
36 public class UDPPacket
extends Packet
{
38 private final @Nullable Packet fChildPacket
;
39 private final @Nullable ByteBuffer fPayload
;
41 private final int fSourcePort
;
42 private final int fDestinationPort
;
43 private final int fTotalLength
;
44 private final int fChecksum
;
46 private @Nullable UDPEndpoint fSourceEndpoint
;
47 private @Nullable UDPEndpoint fDestinationEndpoint
;
49 private @Nullable Map
<String
, String
> fFields
;
52 * Constructor of the UDP Packet class.
55 * The file that contains this packet.
57 * The parent packet of this packet (the encapsulating packet).
59 * The entire packet (header and payload).
60 * @throws BadPacketException
61 * Thrown when the packet is erroneous.
63 public UDPPacket(PcapFile file
, @Nullable Packet parent
, ByteBuffer packet
) throws BadPacketException
{
64 super(file
, parent
, PcapProtocol
.UDP
);
66 // The endpoints are lazy loaded. They are defined in the get*Endpoint()
68 fSourceEndpoint
= null;
69 fDestinationEndpoint
= null;
73 packet
.order(ByteOrder
.BIG_ENDIAN
);
76 fSourcePort
= ConversionHelper
.unsignedShortToInt(packet
.getShort());
77 fDestinationPort
= ConversionHelper
.unsignedShortToInt(packet
.getShort());
78 fTotalLength
= ConversionHelper
.unsignedShortToInt(packet
.getShort());
79 fChecksum
= ConversionHelper
.unsignedShortToInt(packet
.getShort());
81 if (packet
.array().length
- packet
.position() > 0) {
82 byte[] array
= new byte[packet
.array().length
- packet
.position()];
85 ByteBuffer payload
= ByteBuffer
.wrap(array
);
86 payload
.order(ByteOrder
.BIG_ENDIAN
);
94 fChildPacket
= findChildPacket();
99 public @Nullable Packet
getChildPacket() {
104 public @Nullable ByteBuffer
getPayload() {
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
116 protected @Nullable Packet
findChildPacket() throws BadPacketException
{
117 // TODO implement further protocols and update this
118 ByteBuffer payload
= fPayload
;
119 if (payload
== null) {
123 return new UnknownPacket(getPcapFile(), this, payload
);
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
;
132 return string
+ child
.toString();
138 * Getter method that returns the UDP Source Port.
140 * @return The source Port.
142 public int getSourcePort() {
147 * Getter method that returns the UDP Destination Port.
149 * @return The destination Port.
151 public int getDestinationPort() {
152 return fDestinationPort
;
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.
159 * @return The total length of the packet in bytes.
161 public int getTotalLength() {
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.
170 * @return The checksum received from the packet.
172 public int getChecksum() {
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.
185 public UDPEndpoint
getSourceEndpoint() {
187 UDPEndpoint endpoint
= fSourceEndpoint
;
188 if (endpoint
== null) {
189 endpoint
= new UDPEndpoint(this, true);
191 fSourceEndpoint
= endpoint
;
192 return fSourceEndpoint
;
196 public UDPEndpoint
getDestinationEndpoint() {
197 @Nullable UDPEndpoint endpoint
= fDestinationEndpoint
;
198 if (endpoint
== null) {
199 endpoint
= new UDPEndpoint(this, false);
201 fDestinationEndpoint
= endpoint
;
202 return fDestinationEndpoint
;
206 public Map
<String
, String
> getFields() {
207 Map
<String
, String
> map
= fFields
;
209 ImmutableMap
.Builder
<String
, String
> builder
= ImmutableMap
.<@NonNull String
, @NonNull 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$
215 fFields
= builder
.build();
222 public String
getLocalSummaryString() {
223 return "Src Port: " + fSourcePort
+ ", Dst Port: " + fDestinationPort
; //$NON-NLS-1$ //$NON-NLS-2$
227 protected String
getSignificationString() {
228 return "Source Port: " + fSourcePort
+ ", Destination Port: " + fDestinationPort
; //$NON-NLS-1$ //$NON-NLS-2$
232 public int hashCode() {
233 final int prime
= 31;
235 result
= prime
* result
+ fChecksum
;
236 final Packet child
= fChildPacket
;
238 result
= prime
* result
+ child
.hashCode();
240 result
= prime
* result
;
242 result
= prime
* result
+ fDestinationPort
;
243 final ByteBuffer payload
= fPayload
;
244 if (payload
!= null) {
245 result
= prime
* result
+ payload
.hashCode();
247 result
= prime
* result
;
249 result
= prime
* result
+ fSourcePort
;
250 result
= prime
* result
+ fTotalLength
;
255 public boolean equals(@Nullable Object obj
) {
262 if (getClass() != obj
.getClass()) {
265 UDPPacket other
= (UDPPacket
) obj
;
266 if (fChecksum
!= other
.fChecksum
) {
269 if(!NonNullUtils
.equalsNullable(fChildPacket
, other
.fChildPacket
)){
272 if (fDestinationPort
!= other
.fDestinationPort
) {
275 if(!NonNullUtils
.equalsNullable(fPayload
, other
.fPayload
)){
278 if (fSourcePort
!= other
.fSourcePort
) {
281 if (fTotalLength
!= other
.fTotalLength
) {