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
.tmf
.pcap
.core
.event
;
15 import static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.checkNotNull
;
17 import java
.nio
.ByteBuffer
;
18 import java
.util
.Collection
;
19 import java
.util
.Collections
;
22 import org
.eclipse
.jdt
.annotation
.Nullable
;
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
.tmf
.pcap
.core
.protocol
.TmfPcapProtocol
;
26 import org
.eclipse
.tracecompass
.internal
.tmf
.pcap
.core
.util
.ProtocolConversion
;
27 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEventField
;
28 import org
.eclipse
.tracecompass
.tmf
.core
.event
.TmfEvent
;
29 import org
.eclipse
.tracecompass
.tmf
.core
.event
.TmfEventType
;
30 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.ITmfTimestamp
;
31 import org
.eclipse
.tracecompass
.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 final String fSource
;
54 private final String fReference
;
57 * Lazy-loaded field representing all the protocols in this event
59 private transient @Nullable Collection
<TmfPcapProtocol
> fProtocols
;
67 * the event rank (in the trace)
75 * the event content (payload)
79 * The packet contained in this event
81 public PcapEvent(ITmfTrace trace
,
83 ITmfTimestamp timestamp
,
86 ITmfEventField content
,
90 super(trace
, rank
, timestamp
, type
, content
);
93 fReference
= reference
;
97 * Return this event's source.
99 * @return The event's source
101 public String
getSource() {
106 * Return this event's reference
108 * @return The event's reference
110 public String
getReference() {
115 * Method that returns an immutable map containing all the fields of a
116 * packet at a certain protocol. For instance, to get the Source IP Address,
118 * <code>event.getFields(TmfProtocol.IPV4).get("Source IP Address");</code>. <br>
119 * It returns null if the protocol is inexistent in the PcapEvent.
122 * The specified protocol
123 * @return A map containing the fields.
125 public @Nullable Map
<String
, String
> getFields(TmfPcapProtocol protocol
) {
126 PcapProtocol p
= ProtocolConversion
.unwrap(protocol
);
127 Packet packet
= fPacket
.getPacket(p
);
128 if (packet
== null) {
131 return packet
.getFields();
135 * Method that returns the payload at a certain protocol level. It returns
136 * null if the protocol is inexistent in the PcapEvent.
139 * The specified protocol
140 * @return The payload as a ByteBuffer.
142 public @Nullable ByteBuffer
getPayload(TmfPcapProtocol protocol
) {
143 PcapProtocol p
= ProtocolConversion
.unwrap(protocol
);
144 Packet packet
= fPacket
.getPacket(p
);
145 if (packet
== null) {
148 return packet
.getPayload();
152 * Method that returns the source endpoint at a certain protocol level. It
153 * returns null if the protocol is inexistent in the PcapEvent.
156 * The specified protocol
157 * @return The source endpoint.
159 public @Nullable String
getSourceEndpoint(TmfPcapProtocol protocol
) {
160 PcapProtocol p
= ProtocolConversion
.unwrap(protocol
);
161 Packet packet
= fPacket
.getPacket(p
);
162 if (packet
== null) {
165 return packet
.getSourceEndpoint().toString();
169 * Method that returns the destination endpoint at a certain protocol level.
170 * It returns null if the protocol is inexistent in the PcapEvent.
173 * The specified protocol
174 * @return The destination endpoint.
176 public @Nullable String
getDestinationEndpoint(TmfPcapProtocol protocol
) {
177 PcapProtocol p
= ProtocolConversion
.unwrap(protocol
);
178 Packet packet
= fPacket
.getPacket(p
);
179 if (packet
== null) {
182 return packet
.getDestinationEndpoint().toString();
186 * Method that returns the most encapsulated protocol in this PcapEvent. If
187 * it is an unknown protocol, it returns the last known protocol.
189 * @return The most encapsulated TmfProtocol.
191 public TmfPcapProtocol
getMostEncapsulatedProtocol() {
192 return ProtocolConversion
.wrap(fPacket
.getMostEcapsulatedPacket().getProtocol());
196 * Method that returns all the protocols in this PcapEvent.
198 * @return A list containing all the TmfProtocol.
200 public Collection
<TmfPcapProtocol
> getProtocols() {
201 if (fProtocols
!= null) {
204 ImmutableList
.Builder
<TmfPcapProtocol
> builder
= new ImmutableList
.Builder
<>();
205 Packet packet
= fPacket
;
208 while (packet
!= null && packet
.getParentPacket() != null) {
209 packet
= packet
.getParentPacket();
212 if (packet
== null) {
213 fProtocols
= checkNotNull(Collections
.EMPTY_LIST
);
216 // Go through all the packets and add them to list.
217 builder
.add(ProtocolConversion
.wrap(packet
.getProtocol()));
218 while (packet
!= null && packet
.getChildPacket() != null) {
219 packet
= packet
.getChildPacket();
220 if (packet
!= null) {
221 builder
.add(ProtocolConversion
.wrap(packet
.getProtocol()));
225 fProtocols
= checkNotNull(builder
.build());
230 * Getter method that returns the packet. This is default visible since it
231 * is only used by tmf.pcap.core and thus should not be visible to other
234 * @return The packet.
241 public String
toString() {
242 return fPacket
.getGlobalSummaryString();
246 * Return the signification of the PcapEvent at a specific protocol level.
249 * The specified protocol.
250 * @return The signification as a String.
252 public String
toString(TmfPcapProtocol protocol
) {
253 PcapProtocol p
= ProtocolConversion
.unwrap(protocol
);
254 Packet packet
= fPacket
.getPacket(p
);
255 if (packet
== null) {
258 return packet
.getLocalSummaryString();