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
.linuxtools
.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
.linuxtools
.pcap
.core
.packet
.BadPacketException
;
22 import org
.eclipse
.linuxtools
.pcap
.core
.packet
.Packet
;
23 import org
.eclipse
.linuxtools
.pcap
.core
.protocol
.Protocol
;
24 import org
.eclipse
.linuxtools
.pcap
.core
.protocol
.unknown
.UnknownPacket
;
25 import org
.eclipse
.linuxtools
.pcap
.core
.trace
.PcapFile
;
26 import org
.eclipse
.linuxtools
.pcap
.core
.util
.ConversionHelper
;
28 import com
.google
.common
.collect
.ImmutableMap
;
31 * Class that represents a UDP packet.
33 * @author Vincent Perot
35 public class UDPPacket
extends Packet
{
37 private final @Nullable Packet fChildPacket
;
38 private final @Nullable ByteBuffer fPayload
;
40 private final int fSourcePort
;
41 private final int fDestinationPort
;
42 private final int fTotalLength
;
43 private final int fChecksum
;
45 private @Nullable UDPEndpoint fSourceEndpoint
;
46 private @Nullable UDPEndpoint fDestinationEndpoint
;
48 private @Nullable ImmutableMap
<String
, String
> fFields
;
51 * Constructor of the UDP Packet class.
54 * The file that contains this packet.
56 * The parent packet of this packet (the encapsulating packet).
58 * The entire packet (header and payload).
59 * @throws BadPacketException
60 * Thrown when the packet is erroneous.
62 public UDPPacket(PcapFile file
, @Nullable Packet parent
, ByteBuffer packet
) throws BadPacketException
{
63 super(file
, parent
, Protocol
.UDP
);
65 // The endpoints are lazy loaded. They are defined in the get*Endpoint()
67 fSourceEndpoint
= null;
68 fDestinationEndpoint
= null;
72 packet
.order(ByteOrder
.BIG_ENDIAN
);
75 fSourcePort
= ConversionHelper
.unsignedShortToInt(packet
.getShort());
76 fDestinationPort
= ConversionHelper
.unsignedShortToInt(packet
.getShort());
77 fTotalLength
= ConversionHelper
.unsignedShortToInt(packet
.getShort());
78 fChecksum
= ConversionHelper
.unsignedShortToInt(packet
.getShort());
80 if (packet
.array().length
- packet
.position() > 0) {
81 byte[] array
= new byte[packet
.array().length
- packet
.position()];
84 ByteBuffer payload
= ByteBuffer
.wrap(array
);
85 payload
.order(ByteOrder
.BIG_ENDIAN
);
93 fChildPacket
= findChildPacket();
98 public @Nullable Packet
getChildPacket() {
103 public @Nullable ByteBuffer
getPayload() {
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
115 protected @Nullable Packet
findChildPacket() throws BadPacketException
{
116 // TODO implement further protocols and update this
117 ByteBuffer payload
= fPayload
;
118 if (payload
== null) {
122 return new UnknownPacket(getPcapFile(), this, payload
);
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
;
131 return string
+ child
.toString();
137 * Getter method that returns the UDP Source Port.
139 * @return The source Port.
141 public int getSourcePort() {
146 * Getter method that returns the UDP Destination Port.
148 * @return The destination Port.
150 public int getDestinationPort() {
151 return fDestinationPort
;
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.
158 * @return The total length of the packet in bytes.
160 public int getTotalLength() {
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.
169 * @return The checksum received from the packet.
171 public int getChecksum() {
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.
184 public UDPEndpoint
getSourceEndpoint() {
186 UDPEndpoint endpoint
= fSourceEndpoint
;
187 if (endpoint
== null) {
188 endpoint
= new UDPEndpoint(this, true);
190 fSourceEndpoint
= endpoint
;
191 return fSourceEndpoint
;
195 public UDPEndpoint
getDestinationEndpoint() {
196 @Nullable UDPEndpoint endpoint
= fDestinationEndpoint
;
197 if (endpoint
== null) {
198 endpoint
= new UDPEndpoint(this, false);
200 fDestinationEndpoint
= endpoint
;
201 return fDestinationEndpoint
;
205 public Map
<String
, String
> getFields() {
206 ImmutableMap
<String
, String
> map
= fFields
;
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$
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 final Packet child
= fChildPacket
;
271 if (!child
.equals(other
.fChildPacket
)) {
275 if (other
.fChildPacket
!= null) {
279 if (fDestinationPort
!= other
.fDestinationPort
) {
282 final ByteBuffer payload
= fPayload
;
283 if (payload
!= null) {
284 if (!payload
.equals(other
.fPayload
)) {
288 if (other
.fPayload
!= null) {
292 if (fSourcePort
!= other
.fSourcePort
) {
295 if (fTotalLength
!= other
.fTotalLength
) {