Skip to content

Commit 05d5c50

Browse files
committed
plot: fix destination filter for tcp plots
the filter_dataframe stopped filtering destination because of a change in the parser settings ("tcpstream" had become "stream").
1 parent 64f69b9 commit 05d5c50

File tree

2 files changed

+41
-38
lines changed

2 files changed

+41
-38
lines changed

mptcpanalyzer/plot.py

+24-22
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from mptcpanalyzer.data import load_into_pandas
88
from mptcpanalyzer.tshark import TsharkConfig
99
import enum
10-
from mptcpanalyzer.connection import MpTcpConnection
10+
from mptcpanalyzer.connection import MpTcpConnection, TcpConnection
1111
from typing import List, Tuple, Collection
1212
import copy
1313
import abc
@@ -56,9 +56,8 @@ def __init__(
5656
self.input_pcaps = input_pcaps
5757
# python shallow copies objects by default
5858
self.tshark_config = copy.deepcopy(exporter)
59-
self.protocol = kwargs.get("protocol", "mptcp")
6059
# self.tshark_config.read_filter = protocol + " and not icmp"
61-
60+
6261

6362
def default_parser(
6463
self,
@@ -91,10 +90,11 @@ def default_parser(
9190
parser.add_argument(name, action="store", type=str, help='Pcap file (or its associated csv)')
9291

9392
if bitfield & PreprocessingActions.FilterStream:
94-
# TODO generate metavar
93+
# difficult to change the varname here => change it everywhere
9594
protocol = "mptcp" if bitfield & PreprocessingActions.FilterMpTcpStream else "tcp"
95+
print("PROTOCOL", protocol)
9696
parser.add_argument(
97-
protocol + 'stream', action="store", type=int,
97+
protocol + 'stream', metavar= protocol + "stream", action="store", type=int,
9898
help= protocol + '.stream id')
9999

100100
if direction:
@@ -118,13 +118,6 @@ def default_parser(
118118
"not to take into account (because"
119119
"it was filtered by iptables or else)"))
120120

121-
# not implemented yet
122-
# if dst_host:
123-
# parser.add_argument(
124-
# 'ipdst_host', action="store",
125-
# help='Filter flows according to the destination hostnames')
126-
127-
128121
parser.add_argument('-o', '--out', action="store", default=None,
129122
help='Name of the output plot')
130123
parser.add_argument('--display', action="store_true",
@@ -149,7 +142,7 @@ def plot(self, rawdataframes, **kwargs):
149142
pass
150143

151144
def filter_dataframe(
152-
self, rawdf, filterstream=None, skipped_subflows=[],
145+
self, rawdf, tcpstream=None, mptcpstream=None, skipped_subflows=[],
153146
destination: mp.ConnectionRoles=None,
154147
extra_query: str=None, **kwargs
155148
):
@@ -163,7 +156,7 @@ def filter_dataframe(
163156
rawdf: Raw dataframe
164157
kwargs: expanded arguments returned by the parser
165158
destination: Filters packets depending on their :enum:`.ConnectionRoles`
166-
mptcpstream: keep only the packets related to mptcp.stream == mptcpstream
159+
stream: keep only the packets related to mptcp.stream == mptcpstream
167160
skipped_subflows: list of skipped subflows
168161
extra_query: Add some more filters to the pandas query
169162
@@ -178,21 +171,30 @@ def filter_dataframe(
178171
"""
179172
log.debug("Preprocessing dataframe with extra args %s" % kwargs)
180173
queries = []
174+
print("tcp.stream", tcpstream, "mptcp:", mptcpstream)
175+
stream = tcpstream if tcpstream is not None else mptcpstream
181176
dataframe = rawdf
182177

183178
for skipped_subflow in skipped_subflows:
184179
log.debug("Skipping subflow %d" % skipped_subflow)
185180
queries.append(" tcpstream!=%d " % skipped_subflow)
186181

187-
if filterstream is not None:
188-
log.debug("Filtering %s stream ." % self.protocol)
189-
queries.append(self.protocol + "stream==%d" % filterstream)
182+
if stream is not None:
183+
protocol = "mptcp" if mptcpstream is not None else "tcp"
184+
log.debug("Filtering %s stream #%d." % (protocol, stream))
185+
queries.append(protocol + "stream==%d" % stream)
190186
if destination is not None:
191187
log.debug("Filtering destination")
188+
192189
# Generate a filter for the connection
193-
con = MpTcpConnection.build_from_dataframe(dataframe, filterstream)
194-
q = con.generate_direction_query(destination)
195-
queries.append(q)
190+
if protocol == "mptcp":
191+
con = MpTcpConnection.build_from_dataframe(dataframe, stream)
192+
q = con.generate_direction_query(destination)
193+
queries.append(q)
194+
else:
195+
con2 = TcpConnection.build_from_dataframe(dataframe, stream)
196+
q = con2.generate_direction_query(destination)
197+
queries.append(q)
196198

197199
if extra_query:
198200
log.debug("Appending extra_query=%s" % extra_query)
@@ -221,7 +223,7 @@ def preprocess(self, **kwargs) -> Collection[ pd.DataFrame ]:
221223
"""
222224
dataframes = []
223225
for pcap_name, actions in self.input_pcaps:
224-
log.info("pcap_name=", pcap_name, "value=", kwargs.get(pcap_name))
226+
log.info("pcap_name=%s value=%r" % (pcap_name, kwargs.get(pcap_name)))
225227
if actions & PreprocessingActions.Preload:
226228
filename = kwargs.get(pcap_name)
227229
df = load_into_pandas(filename, self.tshark_config,)
@@ -348,7 +350,7 @@ def savefig(fig, filename, **kwargs):
348350
kwargs: Forwarded to :member:`matplotlib.Figure.savefig`.
349351
You can set *dpi* for instance (80 by default ?)
350352
"""
351-
print("Saving into %s" % (filename))
353+
logging.info("Saving into %s" % (filename))
352354
# most settings (dpi for instance) can be set from resource config
353355
fig.savefig(filename, format="png", **kwargs)
354356
return filename

mptcpanalyzer/plots/dsn.py

+17-16
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class PlotSubflowAttribute(plot.Matplotlib):
1212
"""
1313

1414
def __init__(self, *args, **kwargs):
15-
pcaps = [("pcap", plot.PreprocessingActions.Preload | plot.PreprocessingActions.FilterMpTcpStream), ]
15+
pcaps = kwargs.get("input_pcaps", [("pcap", plot.PreprocessingActions.Preload | plot.PreprocessingActions.FilterMpTcpStream), ])
1616
super().__init__(*args, input_pcaps=pcaps, **kwargs)
1717

1818
self._attributes = self.tshark_config.get_fields('name', 'label')
@@ -69,28 +69,29 @@ def plot(self, dat, mptcpstream, field=None, **kwargs):
6969
return fig
7070

7171

72-
class PlotTcpAttribute(PlotSubflowAttribute):
72+
class PlotTcpAttribute(plot.Matplotlib):
7373

7474
def __init__(self, *args, **kwargs):
7575

7676
pcaps = [("pcap", plot.PreprocessingActions.Preload | plot.PreprocessingActions.FilterTcpStream), ]
7777
super(plot.Matplotlib, self).__init__(*args, input_pcaps=pcaps, **kwargs)
7878
self._attributes = self.tshark_config.get_fields('name', 'label')
7979

80-
# def default_parser(self, *args, **kwargs):
81-
82-
# parent = argparse.ArgumentParser(
83-
# description="Plot tcp attributes over time"
84-
# )
85-
# parser = super(PlotSubflowAttribute,self).default_parser(
86-
# *args, parent_parsers=[parent],
87-
# filterstream=True,
88-
# direction=True,
89-
# skip_subflows=True,
90-
# **kwargs)
91-
# # parser.add_argument('field', choices=self.mptcp_attributes.keys(),
92-
# # help="Choose an mptcp attribute to plot")
93-
# return parser
80+
def default_parser(self, *args, **kwargs):
81+
82+
parent = argparse.ArgumentParser(
83+
description="Plot tcp attributes over time"
84+
)
85+
parser = super().default_parser(
86+
*args, parent_parsers=[parent],
87+
filterstream=True,
88+
direction=True,
89+
skip_subflows=True,
90+
**kwargs)
91+
parser.add_argument('field', choices=self._attributes.keys(),
92+
help="Choose an mptcp attribute to plot")
93+
return parser
94+
9495

9596
def plot(self, df, tcpstream, field=None, **kwargs):
9697
"""

0 commit comments

Comments
 (0)