Skip to content
Snippets Groups Projects
Commit 84fa27ac authored by Pierre LALET's avatar Pierre LALET
Browse files

sndrcv(): fix timeout handling

parent 87073693
No related branches found
No related tags found
No related merge requests found
...@@ -67,14 +67,15 @@ def _sndrcv_snd(pks, timeout, inter, verbose, tobesent, all_stimuli, stopevent): ...@@ -67,14 +67,15 @@ def _sndrcv_snd(pks, timeout, inter, verbose, tobesent, all_stimuli, stopevent):
log_runtime.info("--- Error sending packets") log_runtime.info("--- Error sending packets")
if timeout is not None: if timeout is not None:
stopevent.wait(timeout) stopevent.wait(timeout)
pks.close() stopevent.set()
def sndrcv(pks, pkt, timeout=None, inter=0, verbose=None, chainCC=False, def sndrcv(pks, pkt, timeout=None, inter=0, verbose=None, chainCC=False,
retry=0, multi=False): retry=0, multi=False):
if conf.use_bpf:
from scapy.arch.bpf.supersocket import bpf_select
if not isinstance(pkt, Gen): if not isinstance(pkt, Gen):
pkt = SetGen(pkt) pkt = SetGen(pkt)
if verbose is None: if verbose is None:
verbose = conf.verb verbose = conf.verb
debug.recv = plist.PacketList([],"Unanswered") debug.recv = plist.PacketList([],"Unanswered")
...@@ -108,25 +109,20 @@ def sndrcv(pks, pkt, timeout=None, inter=0, verbose=None, chainCC=False, ...@@ -108,25 +109,20 @@ def sndrcv(pks, pkt, timeout=None, inter=0, verbose=None, chainCC=False,
stopevent), stopevent),
) )
thread.start() thread.start()
stoptime = 0
remaintime = None
try: try:
try: try:
while True: while True:
if stoptime:
remaintime = stoptime-time.time()
if remaintime <= 0:
break
r = None r = None
if WINDOWS: if WINDOWS:
r = pks.recv(MTU) r = pks.recv(MTU)
elif conf.use_bpf: elif conf.use_bpf:
from scapy.arch.bpf.supersocket import bpf_select
inp = bpf_select([pks]) inp = bpf_select([pks])
if pks in inp: if pks in inp:
r = pks.recv() r = pks.recv()
elif conf.use_pcap: elif conf.use_pcap:
r = pks.nonblock_recv() r = pks.nonblock_recv()
if r is None:
time.sleep(0.05)
elif not isinstance(pks, StreamSocket) and ( elif not isinstance(pks, StreamSocket) and (
FREEBSD or DARWIN or OPENBSD FREEBSD or DARWIN or OPENBSD
): ):
...@@ -136,17 +132,17 @@ def sndrcv(pks, pkt, timeout=None, inter=0, verbose=None, chainCC=False, ...@@ -136,17 +132,17 @@ def sndrcv(pks, pkt, timeout=None, inter=0, verbose=None, chainCC=False,
else: else:
inp = [] inp = []
try: try:
inp, _, _ = select([pks], [], [], remaintime) inp, _, _ = select([pks], [], [], 0.05)
except (IOError, select_error) as exc: except (IOError, select_error) as exc:
# select.error has no .errno attribute # select.error has no .errno attribute
if exc.args[0] != errno.EINTR: if exc.args[0] != errno.EINTR:
raise raise
if len(inp) == 0: if not inp and stopevent.is_set():
break break
if pks in inp: if pks in inp:
r = pks.recv(MTU) r = pks.recv(MTU)
if r is None: if r is None:
if pks.closed: if stopevent.is_set():
break break
continue continue
ok = 0 ok = 0
...@@ -181,6 +177,7 @@ def sndrcv(pks, pkt, timeout=None, inter=0, verbose=None, chainCC=False, ...@@ -181,6 +177,7 @@ def sndrcv(pks, pkt, timeout=None, inter=0, verbose=None, chainCC=False,
finally: finally:
stopevent.set() stopevent.set()
thread.join() thread.join()
pks.close()
remain = list(itertools.chain(*six.itervalues(hsent))) remain = list(itertools.chain(*six.itervalues(hsent)))
if multi: if multi:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment