@@ -74,25 +74,72 @@ class debug:
74
74
# Send / Receive #
75
75
####################
76
76
77
- _DOC_SNDRCV_PARAMS = """
77
+ _DOC_SNDRCV_PARAMS_HEAD = """
78
78
:param pks: SuperSocket instance to send/receive packets
79
- :param pkt: the packet to send
80
- :param timeout: how much time to wait after the last packet has been sent
81
- :param inter: delay between two packets during sending
82
- :param verbose: set verbosity level
83
- :param chainCC: if True, KeyboardInterrupts will be forwarded
84
- :param retry: if positive, how many times to resend unanswered packets
85
- if negative, how many times to retry when no more packets
86
- are answered
87
- :param multi: whether to accept multiple answers for the same stimulus
88
- :param rcv_pks: if set, will be used instead of pks to receive packets.
89
- packets will still be sent through pks
90
- :param prebuild: pre-build the packets before starting to send them.
91
- Automatically enabled when a generator is passed as the packet
92
- :param _flood:
93
- :param threaded: if True, packets will be sent in an individual thread
94
- :param session: a flow decoder used to handle stream of packets
95
- :param chainEX: if True, exceptions during send will be forwarded
79
+ :type pkt: SuperSocket
80
+ """
81
+
82
+ _DOC_SNDRCV_PARAMS_BODY = """
83
+ :param pkt: Packet or iterable of packets to be sent.
84
+ :type pkt: _PacketIterable
85
+ :param timeout: How much time to wait after the last packet
86
+ has been sent. Defaults to None.
87
+ :type timeout: Optional[int]
88
+ :param inter: Delay between two packets during sending. Defaults to 0.
89
+ :type inter: Optional[int]
90
+
91
+ :param verbose: Set verbosity level. Defaults to None.
92
+ :type verbose: Optional[int]
93
+
94
+ :param chainCC: If True, KeyboardInterrupts will be forwarded.
95
+ Defaults to False.
96
+ :type chainCC: Optional[bool]
97
+
98
+ :param retry: If positive, how many times to resend unanswered packets.
99
+ If negative, how many times to retry when no more packets
100
+ are answered. Defaults to 0.
101
+ :type retry: Optional[int]
102
+
103
+ :param multi: Whether to accept multiple answers for the same stimulus.
104
+ Defaults to False.
105
+ :type multi: Optional[bool]
106
+
107
+ :param rcv_pks: If set, will be used instead of pks to receive packets.
108
+ Packets will still be sent through pks.
109
+ Defaults to None.
110
+ :type rcv_pks: Optional[SuperSocket]
111
+
112
+ :param prebuild: Pre-build the packets before starting to send them.
113
+ Automatically enabled when a generator is passed as the
114
+ packet. Defaults to False.
115
+ :type prebuild: Optional[bool]
116
+
117
+ :param _flood: _FloodGenerator object, internally used by `flood()`
118
+ methods. Defaults to None.
119
+ :type _flood: Optional[_FloodGenerator]
120
+
121
+ :param threaded: If True, packets will be sent in an individual thread.
122
+ Defaults to False.
123
+ :type threaded: Optional[bool]
124
+
125
+ :param session: A flow decoder used to handle the stream of packets.
126
+ Defaults to None.
127
+ :type session: Optional[_GlobSessionType]
128
+
129
+ :param chainEX: If True, exceptions during send will be forwarded.
130
+ Defaults to False.
131
+ :type chainEX: Optional[bool]
132
+ """
133
+
134
+ _DOC_SNDRCV_PARAMS_TAIL = """
135
+ :return: A tuple, consisting of two packet lists, one with
136
+ answered packets, the other with unanswered packets
137
+ :rtype: Tuple[SndRcvList, PacketList]
138
+ """
139
+
140
+ _DOC_SNDRCV1_PARAMS_TAIL = """
141
+ :return: A received Packet answering the sent packet, or None
142
+ :rtype: Optional[Packet]
96
143
"""
97
144
98
145
@@ -713,9 +760,26 @@ def srp1(*args, **kargs):
713
760
714
761
715
762
# Append doc
716
- for sr_func in [srp , srp1 , sr , sr1 ]:
763
+ for sr_func in [srp , sr ]:
717
764
if sr_func .__doc__ is not None :
718
- sr_func .__doc__ += _DOC_SNDRCV_PARAMS
765
+ sr_func .__doc__ += (_DOC_SNDRCV_PARAMS_HEAD +
766
+ _DOC_SNDRCV_PARAMS_BODY +
767
+ _DOC_SNDRCV_PARAMS_TAIL )
768
+
769
+ for sr_func in [srp1 , sr1 ]:
770
+ if sr_func .__doc__ is not None :
771
+ sr_func .__doc__ += (_DOC_SNDRCV_PARAMS_HEAD +
772
+ _DOC_SNDRCV_PARAMS_BODY +
773
+ _DOC_SNDRCV1_PARAMS_TAIL )
774
+
775
+ # Append doc in SuperSocket
776
+ for sr_func in [SuperSocket .sr ]:
777
+ if sr_func .__doc__ is not None :
778
+ sr_func .__doc__ += _DOC_SNDRCV_PARAMS_BODY + _DOC_SNDRCV_PARAMS_TAIL
779
+
780
+ for sr_func in [SuperSocket .sr1 ]:
781
+ if sr_func .__doc__ is not None :
782
+ sr_func .__doc__ += _DOC_SNDRCV_PARAMS_BODY + _DOC_SNDRCV1_PARAMS_TAIL
719
783
720
784
721
785
# SEND/RECV LOOP METHODS
@@ -995,40 +1059,49 @@ def srp1flood(x, # type: _PacketIterable
995
1059
# SNIFF METHODS
996
1060
997
1061
998
- class AsyncSniffer (object ):
999
- """
1000
- Sniff packets and return a list of packets.
1001
-
1002
- Args:
1003
- count: number of packets to capture. 0 means infinity.
1004
- store: whether to store sniffed packets or discard them
1005
- prn: function to apply to each packet. If something is returned, it
1006
- is displayed.
1007
- --Ex: prn = lambda x: x.summary()
1008
- session: a session = a flow decoder used to handle stream of packets.
1009
- --Ex: session=TCPSession
1010
- See below for more details.
1011
- filter: BPF filter to apply.
1012
- lfilter: Python function applied to each packet to determine if
1013
- further action may be done.
1014
- --Ex: lfilter = lambda x: x.haslayer(Padding)
1015
- offline: PCAP file (or list of PCAP files) to read packets from,
1016
- instead of sniffing them
1017
- quiet: when set to True, the process stderr is discarded
1018
- (default: False).
1019
- timeout: stop sniffing after a given time (default: None).
1020
- L2socket: use the provided L2socket (default: use conf.L2listen).
1021
- opened_socket: provide an object (or a list of objects) ready to use
1022
- .recv() on.
1023
- stop_filter: Python function applied to each packet to determine if
1024
- we have to stop the capture after this packet.
1025
- --Ex: stop_filter = lambda x: x.haslayer(TCP)
1026
- iface: interface or list of interfaces (default: None for sniffing
1027
- on all interfaces).
1028
- monitor: use monitor mode. May not be available on all OS
1029
- started_callback: called as soon as the sniffer starts sniffing
1030
- (default: None).
1062
+ _DOC_SNIFF_PARAMS = """
1063
+ :param count: Number of packets to capture. 0 means infinity.
1064
+ :type count: int
1065
+ :param store: Whether to store sniffed packets or discard them.
1066
+ :type store: bool
1067
+ :param offline: PCAP file (or list of PCAP files) to read packets from,
1068
+ instead of sniffing them.
1069
+ :type offline: Any
1070
+ :param quiet: When set to True, the process stderr is discarded.
1071
+ (default: False).
1072
+ :type quiet: bool
1073
+ :param prn: Function to apply to each packet. If something is returned,
1074
+ it is displayed.
1075
+ --Ex: prn = lambda x: x.summary()
1076
+ :type prn: Optional[Callable[[Packet], Any]]
1077
+ :param lfilter: Python function applied to each packet to determine if
1078
+ further action may be done.
1079
+ :type lfilter: Optional[Callable[[Packet], bool]]
1080
+ :param L2socket: Use the provided L2socket (default: use conf.L2listen).
1081
+ :type L2socket: Optional[Type[SuperSocket]]
1082
+ :param timeout: Stop sniffing after a given time (default: None).
1083
+ :type timeout: Optional[int]
1084
+ :param opened_socket: Provide an object (or a list of objects) ready to
1085
+ use .recv() on.
1086
+ :type opened_socket: Optional[SuperSocket]
1087
+ :param stop_filter: Python function applied to each packet to determine if
1088
+ we have to stop the capture after this packet.
1089
+ :type stop_filter: Optional[Callable[[Packet], bool]]
1090
+ :param iface: Interface or list of interfaces (default: None for sniffing
1091
+ on all interfaces).
1092
+ :type iface: Optional[_GlobInterfaceType]
1093
+ :param started_callback: Called as soon as the sniffer starts sniffing
1094
+ (default: None).
1095
+ :type started_callback: Optional[Callable[[], Any]]
1096
+ :param session: A session, which is a flow decoder used to handle a stream
1097
+ of packets. See the documentation for more details.
1098
+ :type session: Optional[_GlobSessionType]
1099
+ :param session_kwargs: Additional keyword arguments for session initialization.
1100
+ :type session_kwargs: Dict[str, Any]
1101
+
1102
+ """
1031
1103
1104
+ _DOC_ASYNC_SNIFF = """
1032
1105
The iface, offline and opened_socket parameters can be either an
1033
1106
element, a list of elements, or a dict object mapping an element to a
1034
1107
label (see examples below).
@@ -1058,6 +1131,11 @@ class AsyncSniffer(object):
1058
1131
>>> t.stop()
1059
1132
"""
1060
1133
1134
+
1135
+ class AsyncSniffer (object ):
1136
+ """Sniff packets and return a list of packets.
1137
+ """
1138
+
1061
1139
def __init__ (self , * args , ** kwargs ):
1062
1140
# type: (*Any, **Any) -> None
1063
1141
# Store keyword arguments
@@ -1321,6 +1399,10 @@ def join(self, *args, **kwargs):
1321
1399
self .thread .join (* args , ** kwargs )
1322
1400
1323
1401
1402
+ AsyncSniffer .__doc__ = ((AsyncSniffer .__doc__ or "" ) + _DOC_SNIFF_PARAMS +
1403
+ _DOC_ASYNC_SNIFF )
1404
+
1405
+
1324
1406
@conf .commands .register
1325
1407
def sniff (* args , ** kwargs ):
1326
1408
# type: (*Any, **Any) -> PacketList
@@ -1329,7 +1411,7 @@ def sniff(*args, **kwargs):
1329
1411
return cast (PacketList , sniffer .results )
1330
1412
1331
1413
1332
- sniff .__doc__ = AsyncSniffer .__doc__
1414
+ SuperSocket . sniff . __doc__ = sniff .__doc__ = AsyncSniffer .__doc__
1333
1415
1334
1416
1335
1417
@conf .commands .register
@@ -1346,18 +1428,21 @@ def bridge_and_sniff(if1, # type: _GlobInterfaceType
1346
1428
"""Forward traffic between interfaces if1 and if2, sniff and return
1347
1429
the exchanged packets.
1348
1430
1349
- :param if1: the interfaces to use (interface names or opened sockets).
1350
- :param if2:
1351
- :param xfrm12: a function to call when forwarding a packet from if1 to
1431
+ :param if1: The interfaces to use (interface names or opened sockets).
1432
+ :type if1: _GlobInterfaceType
1433
+
1434
+ :param if2: The interfaces to use (interface names or opened sockets).
1435
+ :type if2: _GlobInterfaceType
1436
+
1437
+ :param xfrm12: A function to call when forwarding a packet from if1 to
1352
1438
if2. If it returns True, the packet is forwarded as it. If it
1353
1439
returns False or None, the packet is discarded. If it returns a
1354
- packet, this packet is forwarded instead of the original packet
1355
- one.
1356
- :param xfrm21: same as xfrm12 for packets forwarded from if2 to if1.
1440
+ packet, this packet is forwarded instead of the original packet.
1441
+ :type xfrm12: Optional[Callable[[Packet], Union[Packet, bool]]]
1442
+
1443
+ :param xfrm21: Same as xfrm12 for packets forwarded from if2 to if1.
1444
+ :type xfrm21: Optional[Callable[[Packet], Union[Packet, bool]]]
1357
1445
1358
- The other arguments are the same than for the function sniff(),
1359
- except for offline, opened_socket and iface that are ignored.
1360
- See help(sniff) for more.
1361
1446
"""
1362
1447
for arg in ['opened_socket' , 'offline' , 'iface' ]:
1363
1448
if arg in kargs :
@@ -1430,11 +1515,15 @@ def prn(pkt):
1430
1515
* args , ** kargs )
1431
1516
1432
1517
1518
+ bridge_and_sniff .__doc__ = (bridge_and_sniff .__doc__ or "" ) + _DOC_SNIFF_PARAMS
1519
+
1520
+
1433
1521
@conf .commands .register
1434
1522
def tshark (* args , ** kargs ):
1435
1523
# type: (Any, Any) -> None
1436
1524
"""Sniff packets and print them calling pkt.summary().
1437
- This tries to replicate what text-wireshark (tshark) would look like"""
1525
+ This tries to replicate what text-wireshark (tshark) would look like.
1526
+ """
1438
1527
1439
1528
if 'iface' in kargs :
1440
1529
iface = kargs .get ('iface' )
@@ -1455,3 +1544,7 @@ def _cb(pkt):
1455
1544
1456
1545
sniff (prn = _cb , store = False , * args , ** kargs )
1457
1546
print ("\n %d packet%s captured" % (i [0 ], 's' if i [0 ] > 1 else '' ))
1547
+
1548
+
1549
+ tshark .__doc__ = (tshark .__doc__ or "" ) + _DOC_SNIFF_PARAMS
1550
+ SuperSocket .tshark .__doc__ = tshark .__doc__
0 commit comments