9
9
tcpdest_from_connections , mptcpdest_from_connections )
10
10
from mptcpanalyzer import (PreprocessingActions , ConnectionRoles , DestinationChoice ,
11
11
CustomConnectionRolesChoices , TcpStreamId , MpTcpStreamId )
12
+ import mptcpanalyzer as mp
12
13
from functools import partial
13
14
from mptcpanalyzer .connection import MpTcpConnection , TcpConnection
14
15
@@ -40,6 +41,9 @@ def _add_dataframe(namespace, dest, df):
40
41
41
42
42
43
class DataframeAction (argparse .Action ):
44
+ '''
45
+ If you need the action to act on a specific dataframe
46
+ '''
43
47
44
48
def __init__ (self , df_name : str , ** kwargs ) -> None :
45
49
argparse .Action .__init__ (self , ** kwargs )
@@ -52,10 +56,6 @@ def add_dataframe(self, namespace, df):
52
56
53
57
54
58
55
- # class StreamId(x):
56
- # return int(x)
57
-
58
-
59
59
class LoadSinglePcap (DataframeAction ):
60
60
'''
61
61
Test action !!
@@ -75,33 +75,6 @@ def __call__(self, parser, namespace, values, option_string=None):
75
75
76
76
self .add_dataframe (namespace , df )
77
77
78
- # def with_argparser_test(argparser: argparse.ArgumentParser,
79
- # preserve_quotes: bool=False) -> Callable[[argparse.Namespace], Optional[bool]]:
80
- # import functools
81
-
82
- # # noinspection PyProtectedMember
83
- # def arg_decorator(func: Callable[[cmd2.Statement], Optional[bool]]):
84
- # @functools.wraps(func)
85
- # def cmd_wrapper(instance, cmdline):
86
- # lexed_arglist = cmd2.cmd2.parse_quoted_string(cmdline, preserve_quotes)
87
- # return func(instance, argparser, lexed_arglist)
88
-
89
- # # argparser defaults the program name to sys.argv[0]
90
- # # we want it to be the name of our command
91
- # # argparser.prog = func.__name__[len(COMMAND_FUNC_PREFIX):]
92
-
93
- # # If the description has not been set, then use the method docstring if one exists
94
- # if argparser.description is None and func.__doc__:
95
- # argparser.description = func.__doc__
96
-
97
- # # Set the command's help text as argparser.description (which can be None)
98
- # # cmd_wrapper.__doc__ = argparser.description
99
-
100
- # # Mark this function as having an argparse ArgumentParser
101
- # setattr(cmd_wrapper, 'argparser', argparser)
102
-
103
- # return cmd_wrapper
104
-
105
78
# return arg_decorator
106
79
# def with_argparser(argparser: argparse.ArgumentParser,
107
80
# preserve_quotes: bool=False) -> Callable[[argparse.Namespace], Optional[bool]]:
@@ -153,6 +126,10 @@ def with_argparser_test(
153
126
preserve_quotes : bool = False ,
154
127
preload_pcap : bool = False ,
155
128
) -> Callable [[argparse .Namespace , List ], Optional [bool ]]:
129
+ """
130
+ Arguments:
131
+ preload_pcap: Use the preloaded pcap as a dataframe
132
+ """
156
133
import functools
157
134
158
135
# noinspection PyProtectedMember
@@ -203,7 +180,6 @@ class AppendDestination(DataframeAction):
203
180
assume convention on naming
204
181
"""
205
182
206
- # query
207
183
def __init__ (self , * args , ** kwargs ) -> None :
208
184
self .already_called = False
209
185
# self.destinations = list(ConnectionRoles)
@@ -254,7 +230,8 @@ class MergePcaps(DataframeAction):
254
230
"""
255
231
assume convention on naming
256
232
"""
257
- def __init__ (self ,
233
+ def __init__ (
234
+ self ,
258
235
name : str ,
259
236
protocol : str , # mptcp or tcp ?
260
237
loader = TsharkConfig (),
@@ -327,18 +304,13 @@ def __call__(self, parser, namespace, values, option_string=None):
327
304
# def __
328
305
329
306
# don't need the Mptcp flag anymore
330
- def exclude_stream (df_name , mptcp : bool = False ):
331
- query = "tcpstream"
332
- if mptcp :
333
- query = "mp" + query
334
- query = query + "!={streamid}"
307
+ def exclude_stream (df_name ):
308
+ query = "{field}!={streamid}"
335
309
return partial (FilterStream , query , df_name )
336
310
337
- def retain_stream (df_name , mptcp : bool = False ):
338
- query = "tcpstream"
339
- if mptcp :
340
- query = "mp" + query
341
- query = query + "=={streamid}"
311
+ # TODO va dependre du type en fait
312
+ def retain_stream (df_name ):
313
+ query = "{field}=={streamid}"
342
314
return partial (FilterStream , query , df_name )
343
315
344
316
@@ -424,7 +396,6 @@ class FilterStream(DataframeAction):
424
396
def __init__ (self , query : str , df_name : str , ** kwargs ) -> None :
425
397
# self.df_name = df_name
426
398
self .query_tpl = query
427
- # self.mptcp = mptcp
428
399
super ().__init__ (df_name , ** kwargs )
429
400
430
401
def __call__ (self , parser , namespace , values , option_string = None ):
@@ -437,31 +408,32 @@ def __call__(self, parser, namespace, values, option_string=None):
437
408
# make sure result
438
409
df = namespace ._dataframes [self .df_name ]
439
410
440
- # streamid = values
441
-
442
411
log .debug ("Filtering stream %s" % (values ))
443
412
444
413
# if type(values) != list:
445
414
# streamids = list(values)
415
+ print ("received values %r" % values )
446
416
447
- # TODO build a query
448
- mptcp = False
449
417
field = "tcpstream"
450
- if isinstance (values , TcpStreamId ):
451
- pass
452
-
453
- elif isinstance (values , MpTcpStreamId ):
454
- mptcp = True
418
+ if isinstance (values , MpTcpStreamId ):
455
419
field = "mptcpstream"
420
+ print ("mptcp instance type " )
421
+ elif isinstance (values , TcpStreamId ):
422
+ pass
456
423
else :
457
- parser .error ("Unsupported type %s" % type (values ))
424
+ parser .error ("Unsupported ' type' %s. Set it to TcpStreamId or MpTcpStreamId " % type (values ))
458
425
459
426
# super(argparse.Action).__call__(parser, namespace, values, option_string)
460
427
setattr (namespace , self .dest , values )
461
- query = self .query_tpl .format (streamid = values )
428
+ query = self .query_tpl .format (field = field , streamid = values )
462
429
463
- log .debug ("Applying query %s" % query )
464
- df .query (query , inplace = True )
430
+ log .log (mp .TRACE , "Applying query [%s]" % query )
431
+ print (df .head (5 ))
432
+ print (df .dtypes )
433
+
434
+ import pandas as pd
435
+ print ("use numexpr?" , pd .get_option ('compute.use_numexpr' , False ))
436
+ df .query (query , inplace = True , )
465
437
466
438
467
439
def gen_bicap_parser (protocol , dest = False ):
@@ -550,12 +522,11 @@ def _pcap(name, pcapAction="store", filterAction="store"):
550
522
# help=argparse.SUPPRESS)
551
523
# merge_pcap.default = "TEST"
552
524
else :
553
- # print("PreprocessingActions.Merge:")
554
- # TODO pas forcement
555
525
filterClass = FilterStream
556
526
_pcap (df_name , pcapAction = LoadSinglePcap ,
557
527
filterAction = retain_stream (df_name ,
558
- mptcp = bool (bitfield & PreprocessingActions .FilterMpTcpStream ))
528
+ # mptcp = bool(bitfield & PreprocessingActions.FilterMpTcpStream)
529
+ )
559
530
)
560
531
561
532
if bitfield & PreprocessingActions .FilterDestination or direction :
@@ -582,7 +553,7 @@ def _pcap(name, pcapAction="store", filterAction="store"):
582
553
if skip_subflows :
583
554
parser .add_argument (
584
555
'--skip' , dest = df_name + "skipped_subflows" , type = TcpStreamId ,
585
- action = exclude_stream (df_name , mptcp = False ),
556
+ action = exclude_stream (df_name ,),
586
557
default = [],
587
558
help = ("You can type here the tcp.stream of a subflow "
588
559
"not to take into account (because"
@@ -601,8 +572,6 @@ class MpTcpAnalyzerParser(argparse_completer.ACArgumentParser):
601
572
602
573
'''
603
574
604
- # def __init__():
605
-
606
575
# def _parse_known_args(self, arg_strings, namespace):
607
576
def parse_known_args (self , args = None , namespace = None ):
608
577
"""
0 commit comments