| 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.tracecompass.internal.tmf.pcap.core.event; |
| 14 | |
| 15 | import java.nio.ByteBuffer; |
| 16 | import java.util.Collection; |
| 17 | import java.util.Collections; |
| 18 | import java.util.List; |
| 19 | import java.util.Map; |
| 20 | |
| 21 | import org.eclipse.jdt.annotation.NonNull; |
| 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; |
| 32 | |
| 33 | import com.google.common.collect.ImmutableList; |
| 34 | |
| 35 | /** |
| 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. |
| 38 | * |
| 39 | * @author Vincent Perot |
| 40 | */ |
| 41 | public class PcapEvent extends TmfEvent { |
| 42 | |
| 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$ |
| 49 | |
| 50 | private static final String EMPTY_STRING = ""; //$NON-NLS-1$ |
| 51 | |
| 52 | private final Packet fPacket; |
| 53 | |
| 54 | /** |
| 55 | * Lazy-loaded field representing all the protocols in this event |
| 56 | */ |
| 57 | private transient @Nullable Collection<TmfPcapProtocol> fProtocols; |
| 58 | |
| 59 | /** |
| 60 | * Full constructor. |
| 61 | * |
| 62 | * @param trace |
| 63 | * the parent trace |
| 64 | * @param rank |
| 65 | * the event rank (in the trace) |
| 66 | * @param timestamp |
| 67 | * the event timestamp |
| 68 | * @param source |
| 69 | * the event source |
| 70 | * @param type |
| 71 | * the event type |
| 72 | * @param content |
| 73 | * the event content (payload) |
| 74 | * @param reference |
| 75 | * the event reference |
| 76 | * @param packet |
| 77 | * The packet contained in this event |
| 78 | */ |
| 79 | public PcapEvent(ITmfTrace trace, |
| 80 | long rank, |
| 81 | ITmfTimestamp timestamp, |
| 82 | String source, |
| 83 | TmfEventType type, |
| 84 | ITmfEventField content, |
| 85 | String reference, |
| 86 | Packet packet) { |
| 87 | |
| 88 | super(trace, rank, timestamp, source, type, content, reference); |
| 89 | fPacket = packet; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Method that returns an immutable map containing all the fields of a |
| 94 | * packet at a certain protocol. For instance, to get the Source IP Address, |
| 95 | * use: |
| 96 | * <code>event.getFields(TmfProtocol.IPV4).get("Source IP Address");</code>. <br> |
| 97 | * It returns null if the protocol is inexistent in the PcapEvent. |
| 98 | * |
| 99 | * @param protocol |
| 100 | * The specified protocol |
| 101 | * @return A map containing the fields. |
| 102 | */ |
| 103 | public @Nullable Map<String, String> getFields(TmfPcapProtocol protocol) { |
| 104 | PcapProtocol p = ProtocolConversion.unwrap(protocol); |
| 105 | Packet packet = fPacket.getPacket(p); |
| 106 | if (packet == null) { |
| 107 | return null; |
| 108 | } |
| 109 | return packet.getFields(); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Method that returns the payload at a certain protocol level. It returns |
| 114 | * null if the protocol is inexistent in the PcapEvent. |
| 115 | * |
| 116 | * @param protocol |
| 117 | * The specified protocol |
| 118 | * @return The payload as a ByteBuffer. |
| 119 | */ |
| 120 | public @Nullable ByteBuffer getPayload(TmfPcapProtocol protocol) { |
| 121 | PcapProtocol p = ProtocolConversion.unwrap(protocol); |
| 122 | Packet packet = fPacket.getPacket(p); |
| 123 | if (packet == null) { |
| 124 | return null; |
| 125 | } |
| 126 | return packet.getPayload(); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Method that returns the source endpoint at a certain protocol level. It |
| 131 | * returns null if the protocol is inexistent in the PcapEvent. |
| 132 | * |
| 133 | * @param protocol |
| 134 | * The specified protocol |
| 135 | * @return The source endpoint. |
| 136 | */ |
| 137 | public @Nullable String getSourceEndpoint(TmfPcapProtocol protocol) { |
| 138 | PcapProtocol p = ProtocolConversion.unwrap(protocol); |
| 139 | Packet packet = fPacket.getPacket(p); |
| 140 | if (packet == null) { |
| 141 | return null; |
| 142 | } |
| 143 | return packet.getSourceEndpoint().toString(); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Method that returns the destination endpoint at a certain protocol level. |
| 148 | * It returns null if the protocol is inexistent in the PcapEvent. |
| 149 | * |
| 150 | * @param protocol |
| 151 | * The specified protocol |
| 152 | * @return The destination endpoint. |
| 153 | */ |
| 154 | public @Nullable String getDestinationEndpoint(TmfPcapProtocol protocol) { |
| 155 | PcapProtocol p = ProtocolConversion.unwrap(protocol); |
| 156 | Packet packet = fPacket.getPacket(p); |
| 157 | if (packet == null) { |
| 158 | return null; |
| 159 | } |
| 160 | return packet.getDestinationEndpoint().toString(); |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Method that returns the most encapsulated protocol in this PcapEvent. If |
| 165 | * it is an unknown protocol, it returns the last known protocol. |
| 166 | * |
| 167 | * @return The most encapsulated TmfProtocol. |
| 168 | */ |
| 169 | public TmfPcapProtocol getMostEncapsulatedProtocol() { |
| 170 | return ProtocolConversion.wrap(fPacket.getMostEcapsulatedPacket().getProtocol()); |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Method that returns all the protocols in this PcapEvent. |
| 175 | * |
| 176 | * @return A list containing all the TmfProtocol. |
| 177 | */ |
| 178 | public Collection<TmfPcapProtocol> getProtocols() { |
| 179 | if (fProtocols != null) { |
| 180 | return fProtocols; |
| 181 | } |
| 182 | ImmutableList.Builder<TmfPcapProtocol> builder = new ImmutableList.Builder<>(); |
| 183 | Packet packet = fPacket; |
| 184 | |
| 185 | // Go to start. |
| 186 | while (packet != null && packet.getParentPacket() != null) { |
| 187 | packet = packet.getParentPacket(); |
| 188 | } |
| 189 | |
| 190 | if (packet == null) { |
| 191 | @SuppressWarnings("null") |
| 192 | @NonNull List<TmfPcapProtocol> emptyList = Collections.EMPTY_LIST; |
| 193 | fProtocols = emptyList; |
| 194 | return fProtocols; |
| 195 | } |
| 196 | // Go through all the packets and add them to list. |
| 197 | builder.add(ProtocolConversion.wrap(packet.getProtocol())); |
| 198 | while (packet != null && packet.getChildPacket() != null) { |
| 199 | packet = packet.getChildPacket(); |
| 200 | if (packet != null) { |
| 201 | builder.add(ProtocolConversion.wrap(packet.getProtocol())); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | @SuppressWarnings("null") |
| 206 | @NonNull ImmutableList<TmfPcapProtocol> immutableList = builder.build(); |
| 207 | fProtocols = immutableList; |
| 208 | return immutableList; |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Getter method that returns the packet. This is default visible since it |
| 213 | * is only used by tmf.pcap.core and thus should not be visible to other |
| 214 | * packages |
| 215 | * |
| 216 | * @return The packet. |
| 217 | */ |
| 218 | Packet getPacket() { |
| 219 | return fPacket; |
| 220 | } |
| 221 | |
| 222 | @Override |
| 223 | public String toString() { |
| 224 | return fPacket.getGlobalSummaryString(); |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Return the signification of the PcapEvent at a specific protocol level. |
| 229 | * |
| 230 | * @param protocol |
| 231 | * The specified protocol. |
| 232 | * @return The signification as a String. |
| 233 | */ |
| 234 | public String toString(TmfPcapProtocol protocol) { |
| 235 | PcapProtocol p = ProtocolConversion.unwrap(protocol); |
| 236 | Packet packet = fPacket.getPacket(p); |
| 237 | if (packet == null) { |
| 238 | return EMPTY_STRING; |
| 239 | } |
| 240 | return packet.getLocalSummaryString(); |
| 241 | } |
| 242 | } |