Skip to main content
summaryrefslogtreecommitdiffstats
blob: 705c52198499b76df33ef0f49ea02237b77078e2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*******************************************************************************
 * Copyright (c) 2014 Ericsson
 *
 * All rights reserved. This program and the accompanying materials are
 * made available under the terms of the Eclipse Public License v1.0 which
 * accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Vincent Perot - Initial API and implementation
 *******************************************************************************/

package org.eclipse.linuxtools.internal.tmf.pcap.core.event;

import java.nio.ByteBuffer;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.linuxtools.internal.pcap.core.packet.Packet;
import org.eclipse.linuxtools.internal.pcap.core.protocol.PcapProtocol;
import org.eclipse.linuxtools.internal.tmf.pcap.core.protocol.TmfPcapProtocol;
import org.eclipse.linuxtools.internal.tmf.pcap.core.util.ProtocolConversion;
import org.eclipse.linuxtools.tmf.core.event.ITmfEventField;
import org.eclipse.linuxtools.tmf.core.event.TmfEvent;
import org.eclipse.linuxtools.tmf.core.event.TmfEventType;
import org.eclipse.linuxtools.tmf.core.timestamp.ITmfTimestamp;
import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;

import com.google.common.collect.ImmutableList;

/**
 * Class that extends TmfEvent to allow TMF to use the packets from the parser.
 * It is a simple TmfEvent that wraps a Packet.
 *
 * @author Vincent Perot
 */
public class PcapEvent extends TmfEvent {

    /** Packet Source Field ID */
    public static final String EVENT_FIELD_PACKET_SOURCE = ":packetsource:"; //$NON-NLS-1$
    /** Packet Destination Field ID */
    public static final String EVENT_FIELD_PACKET_DESTINATION = ":packetdestination:"; //$NON-NLS-1$
    /** Packet Protocol Field ID */
    public static final String EVENT_FIELD_PACKET_PROTOCOL = ":protocol:"; //$NON-NLS-1$

    private static final String EMPTY_STRING = ""; //$NON-NLS-1$

    private final Packet fPacket;

    /**
     * Lazy-loaded field representing all the protocols in this event
     */
    private transient @Nullable Collection<TmfPcapProtocol> fProtocols;

    /**
     * Full constructor.
     *
     * @param trace
     *            the parent trace
     * @param rank
     *            the event rank (in the trace)
     * @param timestamp
     *            the event timestamp
     * @param source
     *            the event source
     * @param type
     *            the event type
     * @param content
     *            the event content (payload)
     * @param reference
     *            the event reference
     * @param packet
     *            The packet contained in this event
     */
    public PcapEvent(ITmfTrace trace,
            long rank,
            ITmfTimestamp timestamp,
            String source,
            TmfEventType type,
            ITmfEventField content,
            String reference,
            Packet packet) {

        super(trace, rank, timestamp, source, type, content, reference);
        fPacket = packet;
    }

    /**
     * Method that returns an immutable map containing all the fields of a
     * packet at a certain protocol. For instance, to get the Source IP Address,
     * use:
     * <code>event.getFields(TmfProtocol.IPV4).get("Source IP Address");</code>. <br>
     * It returns null if the protocol is inexistent in the PcapEvent.
     *
     * @param protocol
     *            The specified protocol
     * @return A map containing the fields.
     */
    public @Nullable Map<String, String> getFields(TmfPcapProtocol protocol) {
        PcapProtocol p = ProtocolConversion.unwrap(protocol);
        Packet packet = fPacket.getPacket(p);
        if (packet == null) {
            return null;
        }
        return packet.getFields();
    }

    /**
     * Method that returns the payload at a certain protocol level. It returns
     * null if the protocol is inexistent in the PcapEvent.
     *
     * @param protocol
     *            The specified protocol
     * @return The payload as a ByteBuffer.
     */
    public @Nullable ByteBuffer getPayload(TmfPcapProtocol protocol) {
        PcapProtocol p = ProtocolConversion.unwrap(protocol);
        Packet packet = fPacket.getPacket(p);
        if (packet == null) {
            return null;
        }
        return packet.getPayload();
    }

    /**
     * Method that returns the source endpoint at a certain protocol level. It
     * returns null if the protocol is inexistent in the PcapEvent.
     *
     * @param protocol
     *            The specified protocol
     * @return The source endpoint.
     */
    public @Nullable String getSourceEndpoint(TmfPcapProtocol protocol) {
        PcapProtocol p = ProtocolConversion.unwrap(protocol);
        Packet packet = fPacket.getPacket(p);
        if (packet == null) {
            return null;
        }
        return packet.getSourceEndpoint().toString();
    }

    /**
     * Method that returns the destination endpoint at a certain protocol level.
     * It returns null if the protocol is inexistent in the PcapEvent.
     *
     * @param protocol
     *            The specified protocol
     * @return The destination endpoint.
     */
    public @Nullable String getDestinationEndpoint(TmfPcapProtocol protocol) {
        PcapProtocol p = ProtocolConversion.unwrap(protocol);
        Packet packet = fPacket.getPacket(p);
        if (packet == null) {
            return null;
        }
        return packet.getDestinationEndpoint().toString();
    }

    /**
     * Method that returns the most encapsulated protocol in this PcapEvent. If
     * it is an unknown protocol, it returns the last known protocol.
     *
     * @return The most encapsulated TmfProtocol.
     */
    public TmfPcapProtocol getMostEncapsulatedProtocol() {
        return ProtocolConversion.wrap(fPacket.getMostEcapsulatedPacket().getProtocol());
    }

    /**
     * Method that returns all the protocols in this PcapEvent.
     *
     * @return A list containing all the TmfProtocol.
     */
    public Collection<TmfPcapProtocol> getProtocols() {
        if (fProtocols != null) {
            return fProtocols;
        }
        ImmutableList.Builder<TmfPcapProtocol> builder = new ImmutableList.Builder<>();
        Packet packet = fPacket;

        // Go to start.
        while (packet != null && packet.getParentPacket() != null) {
            packet = packet.getParentPacket();
        }

        if (packet == null) {
            @SuppressWarnings("null")
            @NonNull List<TmfPcapProtocol> emptyList = Collections.EMPTY_LIST;
            fProtocols = emptyList;
            return fProtocols;
        }
        // Go through all the packets and add them to list.
        builder.add(ProtocolConversion.wrap(packet.getProtocol()));
        while (packet != null && packet.getChildPacket() != null) {
            packet = packet.getChildPacket();
            if (packet != null) {
                builder.add(ProtocolConversion.wrap(packet.getProtocol()));
            }
        }

        @SuppressWarnings("null")
        @NonNull ImmutableList<TmfPcapProtocol> immutableList = builder.build();
        fProtocols = immutableList;
        return immutableList;
    }

    /**
     * Getter method that returns the packet. This is default visible since it
     * is only used by tmf.pcap.core and thus should not be visible to other
     * packages
     *
     * @return The packet.
     */
    Packet getPacket() {
        return fPacket;
    }

    @Override
    public String toString() {
        return fPacket.getGlobalSummaryString();
    }

    /**
     * Return the signification of the PcapEvent at a specific protocol level.
     *
     * @param protocol
     *            The specified protocol.
     * @return The signification as a String.
     */
    public String toString(TmfPcapProtocol protocol) {
        PcapProtocol p = ProtocolConversion.unwrap(protocol);
        Packet packet = fPacket.getPacket(p);
        if (packet == null) {
            return EMPTY_STRING;
        }
        return packet.getLocalSummaryString();
    }
}

Back to the top