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
.pcap
.core
.stream
;
15 import java
.io
.IOException
;
16 import java
.nio
.file
.Path
;
17 import java
.util
.HashMap
;
18 import java
.util
.LinkedList
;
21 import org
.eclipse
.jdt
.annotation
.Nullable
;
22 import org
.eclipse
.tracecompass
.internal
.pcap
.core
.endpoint
.ProtocolEndpoint
;
23 import org
.eclipse
.tracecompass
.internal
.pcap
.core
.endpoint
.ProtocolEndpointPair
;
24 import org
.eclipse
.tracecompass
.internal
.pcap
.core
.filter
.IPacketFilter
;
25 import org
.eclipse
.tracecompass
.internal
.pcap
.core
.filter
.PacketFilterByProtocol
;
26 import org
.eclipse
.tracecompass
.internal
.pcap
.core
.packet
.BadPacketException
;
27 import org
.eclipse
.tracecompass
.internal
.pcap
.core
.packet
.Packet
;
28 import org
.eclipse
.tracecompass
.internal
.pcap
.core
.protocol
.PcapProtocol
;
29 import org
.eclipse
.tracecompass
.internal
.pcap
.core
.protocol
.pcap
.PcapPacket
;
30 import org
.eclipse
.tracecompass
.internal
.pcap
.core
.trace
.BadPcapFileException
;
31 import org
.eclipse
.tracecompass
.internal
.pcap
.core
.trace
.PcapFile
;
34 * Class that parse an entire pcap file to build the different streams.
36 * @author Vincent Perot
38 public class PacketStreamBuilder
{
40 private final IPacketFilter fPacketFilter
;
41 private final PcapProtocol fProtocol
;
43 private final Map
<Integer
, PacketStream
> fStreams
;
44 private final Map
<ProtocolEndpointPair
, Integer
> fIDs
;
45 private int fCurrentId
;
51 * The protocol of the builder.
53 public PacketStreamBuilder(PcapProtocol protocol
) {
56 fPacketFilter
= new PacketFilterByProtocol(protocol
);
57 fStreams
= new HashMap
<>();
58 fIDs
= new HashMap
<>();
62 * Method that returns a particular stream based on its ID.
65 * The ID of the stream.
66 * @return The stream that has the specified ID.
68 public synchronized @Nullable PacketStream
getStream(int id
) {
69 return fStreams
.get(id
);
73 * Method that returns a particular stream based on its endpoints. It
74 * returns null if no corresponding stream is found.
77 * The first endpoint of the stream.
79 * The second endpoint of the stream.
81 * @return The stream that has the specified endpoints. Return Null if no
82 * stream is found between the two endpoints.
84 public synchronized @Nullable PacketStream
getStream(ProtocolEndpoint endpointA
, ProtocolEndpoint endpointB
) {
85 ProtocolEndpointPair set
= new ProtocolEndpointPair(endpointA
, endpointB
);
86 int id
= fIDs
.get(set
);
87 return fStreams
.get(id
);
91 * Method that returns all the streams at the specified protocol level.
93 * @return The streams as a list.
95 public synchronized Iterable
<PacketStream
> getStreams() {
96 Iterable
<PacketStream
> iterable
= new LinkedList
<>(fStreams
.values());
101 * Method that is called when the filter accepts a packet. This methods add
102 * the packet to a stream based on its characteristics.
105 * The packet to be added.
107 public synchronized void addPacketToStream(PcapPacket packet
) {
108 if (fPacketFilter
.accepts(packet
)) {
109 @Nullable Packet newPacket
= packet
.getPacket(fProtocol
);
110 if (newPacket
== null) {
113 ProtocolEndpointPair endpointSet
= new ProtocolEndpointPair(newPacket
);
114 if (!fIDs
.containsKey(endpointSet
)) {
115 fIDs
.put(endpointSet
, fCurrentId
);
116 fStreams
.put(fCurrentId
, new PacketStream(fProtocol
, fCurrentId
, endpointSet
));
117 fStreams
.get(fCurrentId
).add(packet
);
120 Integer id
= fIDs
.get(endpointSet
);
121 fStreams
.get(id
).add(packet
);
128 * Getter method for the protocol of the stream builder.
130 * @return The protocol.
132 public PcapProtocol
getProtocol() {
137 * Method that clears the builder.
139 public void clear() {
146 * Method that returns the number of streams built.
148 * @return The number of streams built.
150 public synchronized int getNbStreams() {
151 return fStreams
.size();
155 * Method that parse an entire file and build the streams contained in the
160 * @throws IOException
161 * When an IO error occurs.
162 * @throws BadPcapFileException
163 * When the PcapFile is not valid.
165 public synchronized void parsePcapFile(Path filePath
) throws IOException
, BadPcapFileException
{
166 try (PcapFile pcapFile
= new PcapFile(filePath
);) {
167 while (pcapFile
.hasNextPacket()) { // not eof
170 packet
= pcapFile
.parseNextPacket();
171 if (packet
== null) {
174 addPacketToStream(packet
);
175 } catch (BadPacketException e
) {
176 // Ignore packet. Do nothing.