From b7f3466be4e04db6796e2b7d6b8b017b0399feff Mon Sep 17 00:00:00 2001 From: gpotter2 <10530980+gpotter2@users.noreply.github.com> Date: Mon, 23 Oct 2023 22:27:37 +0200 Subject: [PATCH 1/2] More consistent sendpfast API (breaking) --- scapy/sendrecv.py | 29 ++++++++++++++++------------- scapy/supersocket.py | 2 +- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/scapy/sendrecv.py b/scapy/sendrecv.py index 5c7a5a2b3c5..6512665a766 100644 --- a/scapy/sendrecv.py +++ b/scapy/sendrecv.py @@ -485,15 +485,16 @@ def sendp(x, # type: _PacketIterable @conf.commands.register -def sendpfast(x, # type: _PacketIterable - pps=None, # type: Optional[float] - mbps=None, # type: Optional[float] - realtime=False, # type: bool - loop=None, # type: Optional[int] - file_cache=False, # type: bool - iface=None, # type: Optional[_GlobInterfaceType] - replay_args=None, # type: Optional[List[str]] - parse_results=False, # type: bool +def sendpfast(x: _PacketIterable, + pps: Optional[float] = None, + mbps: Optional[float] = None, + realtime: bool = False, + count: Optional[int] = None, + loop: int = 0, + file_cache: bool = False, + iface: Optional[_GlobInterfaceType] = None, + replay_args: Optional[List[str]] = None, + parse_results: bool = False, ): # type: (...) -> Optional[Dict[str, Any]] """Send packets at layer 2 using tcpreplay for performance @@ -501,8 +502,8 @@ def sendpfast(x, # type: _PacketIterable :param pps: packets per second :param mbps: MBits per second :param realtime: use packet's timestamp, bending time with real-time value - :param loop: number of times to process the packet list. 0 implies - infinite loop + :param loop: send the packet indefinitely (default 0) + :param count: number of packets to send (default None=1) :param file_cache: cache packets in RAM instead of reading from disk at each iteration :param iface: output interface @@ -523,8 +524,10 @@ def sendpfast(x, # type: _PacketIterable else: argv.append("--topspeed") - if loop is not None: - argv.append("--loop=%i" % loop) + if loop: + argv.append("--loop=0") + elif count: + argv.append("--loop=%i" % count) if file_cache: argv.append("--preload-pcap") diff --git a/scapy/supersocket.py b/scapy/supersocket.py index 06f7019a78f..0a05749f214 100644 --- a/scapy/supersocket.py +++ b/scapy/supersocket.py @@ -214,7 +214,7 @@ def close(self): def sr(self, *args, **kargs): # type: (Any, Any) -> Tuple[SndRcvList, PacketList] from scapy import sendrecv - ans, unans = sendrecv.sndrcv(self, *args, **kargs) # type: SndRcvList, PacketList # noqa: E501 + ans, unans = sendrecv.sndrcv(self, *args, **kargs) return ans, unans def sr1(self, *args, **kargs): From 2130ee3c5f23b607bb834be8b21eaa5fe35c55f7 Mon Sep 17 00:00:00 2001 From: gpotter2 <10530980+gpotter2@users.noreply.github.com> Date: Tue, 24 Oct 2023 20:37:12 +0200 Subject: [PATCH 2/2] Apply suggestion --- scapy/sendrecv.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/scapy/sendrecv.py b/scapy/sendrecv.py index 6512665a766..c62ecc6901e 100644 --- a/scapy/sendrecv.py +++ b/scapy/sendrecv.py @@ -524,10 +524,11 @@ def sendpfast(x: _PacketIterable, else: argv.append("--topspeed") - if loop: - argv.append("--loop=0") - elif count: + if count: + assert not loop, "Can't use loop and count at the same time in sendpfast" argv.append("--loop=%i" % count) + elif loop: + argv.append("--loop=0") if file_cache: argv.append("--preload-pcap")