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
.internal
.tmf
.pcap
.core
.event
;
15 import java
.nio
.ByteBuffer
;
16 import java
.util
.ArrayList
;
17 import java
.util
.Collections
;
18 import java
.util
.List
;
21 import org
.eclipse
.jdt
.annotation
.NonNull
;
22 import org
.eclipse
.jdt
.annotation
.Nullable
;
23 import org
.eclipse
.linuxtools
.internal
.pcap
.core
.packet
.Packet
;
24 import org
.eclipse
.linuxtools
.internal
.pcap
.core
.protocol
.PcapProtocol
;
25 import org
.eclipse
.linuxtools
.internal
.tmf
.pcap
.core
.protocol
.TmfPcapProtocol
;
26 import org
.eclipse
.linuxtools
.internal
.tmf
.pcap
.core
.util
.ProtocolConversion
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEventField
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfEvent
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfEventType
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.ITmfTimestamp
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
33 import com
.google
.common
.collect
.ImmutableList
;
36 * Class that extends TmfEvent to allow TMF to use the packets from the parser.
37 * It is a simple TmfEvent that wraps a Packet.
39 * @author Vincent Perot
41 public class PcapEvent
extends TmfEvent
{
43 /** Packet Source Field ID */
44 public static final String EVENT_FIELD_PACKET_SOURCE
= ":packetsource:"; //$NON-NLS-1$
45 /** Packet Destination Field ID */
46 public static final String EVENT_FIELD_PACKET_DESTINATION
= ":packetdestination:"; //$NON-NLS-1$
47 /** Packet Protocol Field ID */
48 public static final String EVENT_FIELD_PACKET_PROTOCOL
= ":protocol:"; //$NON-NLS-1$
50 private static final String EMPTY_STRING
= ""; //$NON-NLS-1$
52 private final Packet fPacket
;
53 private @Nullable List
<TmfPcapProtocol
> fList
;
61 * the event rank (in the trace)
69 * the event content (payload)
73 * The packet contained in this event
75 public PcapEvent(ITmfTrace trace
,
77 ITmfTimestamp timestamp
,
80 ITmfEventField content
,
84 super(trace
, rank
, timestamp
, source
, type
, content
, reference
);
89 * Method that returns an immutable map containing all the fields of a
90 * packet at a certain protocol. For instance, to get the Source IP Address,
92 * <code>event.getFields(TmfProtocol.IPV4).get("Source IP Address");</code>. <br>
93 * It returns null if the protocol is inexistent in the PcapEvent.
96 * The specified protocol
97 * @return A map containing the fields.
99 public @Nullable Map
<String
, String
> getFields(TmfPcapProtocol protocol
) {
100 PcapProtocol p
= ProtocolConversion
.unwrap(protocol
);
101 Packet packet
= fPacket
.getPacket(p
);
102 if (packet
== null) {
105 return packet
.getFields();
109 * Method that returns the payload at a certain protocol level. It returns
110 * null if the protocol is inexistent in the PcapEvent.
113 * The specified protocol
114 * @return The payload as a ByteBuffer.
116 public @Nullable ByteBuffer
getPayload(TmfPcapProtocol protocol
) {
117 PcapProtocol p
= ProtocolConversion
.unwrap(protocol
);
118 Packet packet
= fPacket
.getPacket(p
);
119 if (packet
== null) {
122 return packet
.getPayload();
126 * Method that returns the source endpoint at a certain protocol level. It
127 * returns null if the protocol is inexistent in the PcapEvent.
130 * The specified protocol
131 * @return The source endpoint.
133 public @Nullable String
getSourceEndpoint(TmfPcapProtocol protocol
) {
134 PcapProtocol p
= ProtocolConversion
.unwrap(protocol
);
135 Packet packet
= fPacket
.getPacket(p
);
136 if (packet
== null) {
139 return packet
.getSourceEndpoint().toString();
143 * Method that returns the destination endpoint at a certain protocol level.
144 * It returns null if the protocol is inexistent in the PcapEvent.
147 * The specified protocol
148 * @return The destination endpoint.
150 public @Nullable String
getDestinationEndpoint(TmfPcapProtocol protocol
) {
151 PcapProtocol p
= ProtocolConversion
.unwrap(protocol
);
152 Packet packet
= fPacket
.getPacket(p
);
153 if (packet
== null) {
156 return packet
.getDestinationEndpoint().toString();
160 * Method that returns the most encapsulated protocol in this PcapEvent. If
161 * it is an unknown protocol, it returns the last known protocol.
163 * @return The most encapsulated TmfProtocol.
165 public TmfPcapProtocol
getMostEncapsulatedProtocol() {
166 return ProtocolConversion
.wrap(fPacket
.getMostEcapsulatedPacket().getProtocol());
170 * Method that returns a list of all the protocols in this PcapEvent.
172 * @return A list containing all the TmfProtocol.
174 public List
<TmfPcapProtocol
> getProtocols() {
178 List
<TmfPcapProtocol
> list
= new ArrayList
<>();
179 Packet packet
= fPacket
;
182 while (packet
!= null && packet
.getParentPacket() != null) {
183 packet
= packet
.getParentPacket();
186 if (packet
== null) {
187 @SuppressWarnings("null")
188 @NonNull List
<TmfPcapProtocol
> emptyList
= Collections
.EMPTY_LIST
;
192 // Go through all the packets and add them to list.
193 list
.add(ProtocolConversion
.wrap(packet
.getProtocol()));
194 while (packet
!= null && packet
.getChildPacket() != null) {
195 packet
= packet
.getChildPacket();
196 if (packet
!= null) {
197 list
.add(ProtocolConversion
.wrap(packet
.getProtocol()));
201 @SuppressWarnings("null")
202 @NonNull ImmutableList
<TmfPcapProtocol
> immutableList
= ImmutableList
.copyOf(list
);
203 fList
= immutableList
;
204 return immutableList
;
208 * Getter method that returns the packet. This is default visible since it
209 * is only used by tmf.pcap.core and thus should not be visible to other
212 * @return The packet.
219 public String
toString() {
220 return fPacket
.getGlobalSummaryString();
224 * Return the signification of the PcapEvent at a specific protocol level.
227 * The specified protocol.
228 * @return The signification as a String.
230 public String
toString(TmfPcapProtocol protocol
) {
231 PcapProtocol p
= ProtocolConversion
.unwrap(protocol
);
232 Packet packet
= fPacket
.getPacket(p
);
233 if (packet
== null) {
236 return packet
.getLocalSummaryString();