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] |