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
.pcap
.core
.stream
;
15 import java
.io
.IOException
;
16 import java
.util
.HashMap
;
17 import java
.util
.LinkedList
;
20 import org
.eclipse
.jdt
.annotation
.Nullable
;
21 import org
.eclipse
.linuxtools
.pcap
.core
.endpoint
.ProtocolEndpoint
;
22 import org
.eclipse
.linuxtools
.pcap
.core
.endpoint
.ProtocolEndpointPair
;
23 import org
.eclipse
.linuxtools
.pcap
.core
.filter
.IPacketFilter
;
24 import org
.eclipse
.linuxtools
.pcap
.core
.filter
.PacketFilterByProtocol
;
25 import org
.eclipse
.linuxtools
.pcap
.core
.packet
.BadPacketException
;
26 import org
.eclipse
.linuxtools
.pcap
.core
.packet
.Packet
;
27 import org
.eclipse
.linuxtools
.pcap
.core
.protocol
.Protocol
;
28 import org
.eclipse
.linuxtools
.pcap
.core
.protocol
.pcap
.PcapPacket
;
29 import org
.eclipse
.linuxtools
.pcap
.core
.trace
.BadPcapFileException
;
30 import org
.eclipse
.linuxtools
.pcap
.core
.trace
.PcapFile
;
33 * Class that parse an entire pcap file to build the different streams.
35 * @author Vincent Perot
37 public class PacketStreamBuilder
{
39 private final IPacketFilter fPacketFilter
;
40 private final Protocol fProtocol
;
42 private final Map
<Integer
, PacketStream
> fStreams
;
43 private final Map
<ProtocolEndpointPair
, Integer
> fIDs
;
44 private int fCurrentId
;
50 * The protocol of the builder.
52 public PacketStreamBuilder(Protocol protocol
) {
55 fPacketFilter
= new PacketFilterByProtocol(protocol
);
56 fStreams
= new HashMap
<>();
57 fIDs
= new HashMap
<>();
61 * Method that returns a particular stream based on its ID.
64 * The ID of the stream.
65 * @return The stream that has the specified ID.
67 public synchronized @Nullable PacketStream
getStream(int id
) {
68 return fStreams
.get(id
);
72 * Method that returns a particular stream based on its endpoints. It
73 * returns null if no corresponding stream is found.
76 * The first endpoint of the stream.
78 * The second endpoint of the stream.
80 * @return The stream that has the specified endpoints. Return Null if no
81 * stream is found between the two endpoints.
83 public synchronized @Nullable PacketStream
getStream(ProtocolEndpoint endpointA
, ProtocolEndpoint endpointB
) {
84 ProtocolEndpointPair set
= new ProtocolEndpointPair(endpointA
, endpointB
);
85 int id
= fIDs
.get(set
);
86 return fStreams
.get(id
);
90 * Method that returns all the streams at the specified protocol level.
92 * @return The streams as a list.
94 public synchronized Iterable
<PacketStream
> getStreams() {
95 Iterable
<PacketStream
> iterable
= new LinkedList
<>(fStreams
.values());
100 * Method that is called when the filter accepts a packet. This methods add
101 * the packet to a stream based on its characteristics.
104 * The packet to be added.
106 public synchronized void addPacketToStream(PcapPacket packet
) {
107 if (fPacketFilter
.accepts(packet
)) {
108 @Nullable Packet newPacket
= packet
.getPacket(fProtocol
);
109 if (newPacket
== null) {
112 ProtocolEndpointPair endpointSet
= new ProtocolEndpointPair(newPacket
);
113 if (!fIDs
.containsKey(endpointSet
)) {
114 fIDs
.put(endpointSet
, fCurrentId
);
115 fStreams
.put(fCurrentId
, new PacketStream(fProtocol
, fCurrentId
, endpointSet
));
116 fStreams
.get(fCurrentId
).add(packet
);
119 Integer id
= fIDs
.get(endpointSet
);
120 fStreams
.get(id
).add(packet
);
127 * Getter method for the protocol of the stream builder.
129 * @return The protocol.
131 public Protocol
getProtocol() {
136 * Method that clears the builder.
138 public void clear() {
145 * Method that returns the number of streams built.
147 * @return The number of streams built.
149 public synchronized int getNbStreams() {
150 return fStreams
.size();
154 * Method that parse an entire file and build the streams contained in the
159 * @throws IOException
160 * When an IO error occurs.
161 * @throws BadPcapFileException
162 * When the PcapFile is not valid.
164 public void parsePcapFile(String file
) throws IOException
, BadPcapFileException
{
165 try (PcapFile pcapFile
= new PcapFile(file
);) {
166 while (pcapFile
.hasNextPacket()) { // not eof
169 packet
= pcapFile
.parseNextPacket();
170 if (packet
== null) {
173 addPacketToStream(packet
);
174 } catch (BadPacketException e
) {
175 // Ignore packet. Do nothing.