Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not resolve the interface name globally #3209

Closed
wants to merge 1 commit into from

Conversation

guedou
Copy link
Member

@guedou guedou commented May 10, 2021

This PR fixes #3191. I do not know how to test it =/

@polybassa
Copy link
Contributor

If you need to create multiple interfaces to create a unit test, you could use virtual CAN interfaces. At least on Linux machines.
You can create as many devices as you want:

sudo modprobe vcan
sudo ip link add dev vcan0 type vcan
sudo ip link set up vcan0
sudo ip link add dev anotherinterface type vcan
sudo ip link set up anotherinterface
sudo ip link add dev onemoreinterface type vcan
sudo ip link set up onemoreinterface
sniff(["vcan0", "anotherinterface", "onemoreinterface"])

If you think that could work for a testcase, I could provide one.

if L2socket is None:
L2socket = iface.l2listen()
L2socket = resolve_iface(iface or conf.iface).l2listen()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we need to put this in the isinstance()s to make sure it always work?

if isinstance(iface, list):
    L2socket = resolve_iface(iface[0] or conf.iface).l2listen()
...
elif isinstance(iface, dict):
    L2socket = resolve_iface(next(iter(iface.values()) or conf.iface).l2listen()
....
.....

@gpotter2 gpotter2 added the bug label May 13, 2021
@gpotter2
Copy link
Member

We can also just use TUN/TAPS polybassa, but good call.

@gpotter2
Copy link
Member

gpotter2 commented May 14, 2021

I'd go for

diff --git a/scapy/sendrecv.py b/scapy/sendrecv.py
index 7a847a03..130f8700 100644
--- a/scapy/sendrecv.py
+++ b/scapy/sendrecv.py
@@ -1108,22 +1108,22 @@ class AsyncSniffer(object):
                             quiet=quiet)
                 )] = offline
         if not sniff_sockets or iface is not None:
-            if L2socket is None:
-                L2socket = resolve_iface(iface or conf.iface).l2listen()
+            _RL2 = lambda i: L2socket or resolve_iface(i).l2listen()
             if isinstance(iface, list):
                 sniff_sockets.update(
-                    (L2socket(type=ETH_P_ALL, iface=ifname, **karg),
+                    (_RL2(ifname)(type=ETH_P_ALL, iface=ifname, **karg),
                      ifname)
                     for ifname in iface
                 )
             elif isinstance(iface, dict):
                 sniff_sockets.update(
-                    (L2socket(type=ETH_P_ALL, iface=ifname, **karg),
+                    (_RL2(ifname)(type=ETH_P_ALL, iface=ifname, **karg),
                      iflabel)
                     for ifname, iflabel in six.iteritems(iface)
                 )
             else:
-                sniff_sockets[L2socket(type=ETH_P_ALL, iface=iface,
+                sniff_sockets[_RL2(iface)(type=ETH_P_ALL, iface=iface,
                                        **karg)] = iface

         # Get select information from the sockets

@p-l-
Copy link
Member

p-l- commented May 27, 2021

I totally missed your PR and took a different approach (#3234). Feel free to use this idea if you want.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Exception when calling sniff for multiple interfaces
4 participants