View | Details | Raw Unified | Return to bug 237305
Collapse All | Expand All

(-)Makefile (+1 lines)
Lines 4-9 Link Here
4
PORTNAME=		scapy
4
PORTNAME=		scapy
5
PORTVERSION=		2.4.2
5
PORTVERSION=		2.4.2
6
DISTVERSIONPREFIX=	v
6
DISTVERSIONPREFIX=	v
7
PORTREVISION=		1
7
CATEGORIES=		net
8
CATEGORIES=		net
8
PKGNAMEPREFIX=	${PYTHON_PKGNAMEPREFIX}
9
PKGNAMEPREFIX=	${PYTHON_PKGNAMEPREFIX}
9
10
(-)files/patch-scapy_arch_bpf_core.py (+88 lines)
Line 0 Link Here
1
--- scapy/arch/bpf/core.py.orig	2019-01-10 18:33:08 UTC
2
+++ scapy/arch/bpf/core.py
3
@@ -5,22 +5,22 @@ Scapy *BSD native support - core
4
 """
5
 
6
 from __future__ import absolute_import
7
-from scapy.config import conf
8
-from scapy.error import Scapy_Exception, warning
9
-from scapy.data import ARPHDR_LOOPBACK, ARPHDR_ETHER
10
-from scapy.arch.common import get_if, compile_filter
11
-from scapy.consts import LOOPBACK_NAME
12
 
13
-from scapy.arch.bpf.consts import BIOCSETF, SIOCGIFFLAGS, BIOCSETIF
14
-
15
+from ctypes import cdll, cast, pointer
16
+from ctypes import c_int, c_ulong, c_char_p
17
+from ctypes.util import find_library
18
+import fcntl
19
 import os
20
+import re
21
 import socket
22
-import fcntl
23
 import struct
24
 
25
-from ctypes import cdll, cast, pointer
26
-from ctypes import c_int, c_ulong, c_char_p
27
-from ctypes.util import find_library
28
+from scapy.arch.bpf.consts import BIOCSETF, SIOCGIFFLAGS, BIOCSETIF
29
+from scapy.arch.common import get_if, compile_filter
30
+from scapy.config import conf
31
+from scapy.consts import LOOPBACK_NAME
32
+from scapy.data import ARPHDR_LOOPBACK, ARPHDR_ETHER
33
+from scapy.error import Scapy_Exception, warning
34
 from scapy.modules.six.moves import range
35
 
36
 
37
@@ -124,6 +124,9 @@ def get_if_list():
38
     return interfaces
39
 
40
 
41
+_IFNUM = re.compile("([0-9]*)([ab]?)$")
42
+
43
+
44
 def get_working_ifaces():
45
     """
46
     Returns an ordered list of interfaces that could be used with BPF.
47
@@ -154,24 +157,27 @@ def get_working_ifaces():
48
         if ifflags & 0x1:  # IFF_UP
49
 
50
             # Get a BPF handle
51
-            fd, _ = get_dev_bpf()
52
+            fd = get_dev_bpf()[0]
53
             if fd is None:
54
                 raise Scapy_Exception("No /dev/bpf are available !")
55
 
56
             # Check if the interface can be used
57
             try:
58
-                fcntl.ioctl(fd, BIOCSETIF, struct.pack("16s16x", ifname.encode()))  # noqa: E501
59
-                interfaces.append((ifname, int(ifname[-1])))
60
+                fcntl.ioctl(fd, BIOCSETIF, struct.pack("16s16x",
61
+                                                       ifname.encode()))
62
             except IOError:
63
                 pass
64
+            else:
65
+                ifnum, ifab = _IFNUM.search(ifname).groups()
66
+                interfaces.append((ifname, int(ifnum) if ifnum else -1, ifab))
67
+            finally:
68
+                # Close the file descriptor
69
+                os.close(fd)
70
 
71
-            # Close the file descriptor
72
-            os.close(fd)
73
-
74
     # Sort to mimic pcap_findalldevs() order
75
-    interfaces.sort(key=lambda elt: elt[1])
76
+    interfaces.sort(key=lambda elt: (elt[1], elt[2], elt[0]))
77
 
78
-    return interfaces
79
+    return [iface[0] for iface in interfaces]
80
 
81
 
82
 def get_working_if():
83
@@ -181,4 +187,4 @@ def get_working_if():
84
     if not ifaces:
85
         # A better interface will be selected later using the routing table
86
         return LOOPBACK_NAME
87
-    return ifaces[0][0]
88
+    return ifaces[0]
(-)files/patch-test_bpf.uts (+36 lines)
Line 0 Link Here
1
--- test/bpf.uts.orig	2019-04-18 06:40:42 UTC
2
+++ test/bpf.uts
3
@@ -53,6 +53,33 @@ assert len(ifworking)
4
 assert get_working_if() == ifworking[0]
5
 
6
 
7
+= Get working network interfaces order
8
+
9
+import mock
10
+from scapy.arch.bpf.core import get_working_ifaces
11
+
12
+@mock.patch("scapy.arch.bpf.core.os.close")
13
+@mock.patch("scapy.arch.bpf.core.fcntl.ioctl")
14
+@mock.patch("scapy.arch.bpf.core.get_dev_bpf")
15
+@mock.patch("scapy.arch.bpf.core.get_if")
16
+@mock.patch("scapy.arch.bpf.core.get_if_list")
17
+@mock.patch("scapy.arch.bpf.core.os.getuid")
18
+def test_get_working_ifaces(mock_getuid, mock_get_if_list, mock_get_if,
19
+                            mock_get_dev_bpf, mock_ioctl, mock_close):
20
+    mock_getuid.return_value = 0
21
+    mock_get_if_list.return_value = ['igb0', 'em0', 'msk0', 'epair0a', 'igb1',
22
+                                     'vlan20', 'igb10', 'igb2']
23
+    mock_get_if.return_value = (b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
24
+                                b'\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00'
25
+                                b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
26
+    mock_get_dev_bpf.return_value = (31337,)
27
+    mock_ioctl.return_value = 0
28
+    mock_close.return_value = 0
29
+    return get_working_ifaces()
30
+
31
+assert test_get_working_ifaces() == ['em0', 'igb0', 'msk0', 'epair0a', 'igb1',
32
+                                     'igb2', 'igb10', 'vlan20']
33
+
34
 = Misc functions
35
 ~ needs_root
36
 

Return to bug 237305