From 31c272efa24484c019cf48362da77d4bd881459a Mon Sep 17 00:00:00 2001
From: gpotter2 <gabriel@potter.fr>
Date: Sat, 6 May 2017 02:16:13 +0200
Subject: [PATCH] Get ipv6 addresses using the pcap API

This will speed up the boot
---
 scapy/arch/pcapdnet.py         |  5 ++--
 scapy/arch/windows/__init__.py | 45 ++++++----------------------------
 2 files changed, 11 insertions(+), 39 deletions(-)

diff --git a/scapy/arch/pcapdnet.py b/scapy/arch/pcapdnet.py
index b52072c7..ff573fd5 100644
--- a/scapy/arch/pcapdnet.py
+++ b/scapy/arch/pcapdnet.py
@@ -18,6 +18,7 @@ from scapy.config import conf
 from scapy.utils import mac2str
 from scapy.supersocket import SuperSocket
 from scapy.error import Scapy_Exception, log_loading, warning
+from scapy.pton_ntop import inet_ntop
 import scapy.arch
 import scapy.consts
 
@@ -113,7 +114,7 @@ if conf.use_winpcapy:
       pcap_freealldevs(devs)
   if conf.use_winpcapy:
       get_if_list = winpcapy_get_if_list
-  def in6_getifaddr():
+  def in6_getifaddr_raw():
     err = create_string_buffer(PCAP_ERRBUF_SIZE)
     devs = POINTER(pcap_if_t)()
     ret = []
@@ -128,7 +129,7 @@ if conf.use_winpcapy:
           if a.contents.addr.contents.sa_family == socket.AF_INET6:
             ap = a.contents.addr
             val = cast(ap, POINTER(sockaddr_in6))
-            addr = socket.inet_ntop(socket.AF_INET6, str(val.contents.sin6_addr[:]))
+            addr = inet_ntop(socket.AF_INET6, "".join(chr(x) for x in val.contents.sin6_addr[:]))
             scope = scapy.utils6.in6_getscope(addr)
             ret.append((addr, scope, p.contents.name.decode('ascii')))
           a = a.contents.next
diff --git a/scapy/arch/windows/__init__.py b/scapy/arch/windows/__init__.py
index 1399e2a9..f2861a0c 100755
--- a/scapy/arch/windows/__init__.py
+++ b/scapy/arch/windows/__init__.py
@@ -293,7 +293,7 @@ def get_windows_if_list():
         # Name                      InterfaceDescription                    ifIndex Status       MacAddress             LinkSpeed
         # ----                      --------------------                    ------- ------       ----------             ---------
         # Ethernet                  Killer E2200 Gigabit Ethernet Contro...      13 Up           D0-50-99-56-DD-F9         1 Gbps
-        query = exec_query(['Get-NetAdapter -Physical'],
+        query = exec_query(['Get-NetAdapter'],
                            ['InterfaceDescription', 'InterfaceIndex', 'Name',
                             'InterfaceGuid', 'MacAddress']) # It is normal that it is in this order
     else:
@@ -647,42 +647,13 @@ def in6_getifaddr():
     """
     Returns all IPv6 addresses found on the computer
     """
-    if is_new_release():
-        ret = []
-        ps = sp.Popen([conf.prog.powershell, 'Get-NetRoute', '-AddressFamily IPV6', '|', 'select ifIndex, DestinationPrefix'], stdout = sp.PIPE, universal_newlines = True)
-        stdout, stdin = ps.communicate()
-        netstat_line = '\s+'.join(['(\d+)', ''.join(['([A-z|0-9|:]+)', '(\/\d+)'])])
-        pattern = re.compile(netstat_line)
-        for l in stdout.split('\n'):
-            match = re.search(pattern,l)
-            if match:
-                try:
-                    if_index = match.group(1)
-                    iface = dev_from_index(if_index)
-                except:
-                    continue
-                scope = scapy.utils6.in6_getscope(match.group(2))
-                ret.append((match.group(2), scope, iface)) # (addr,scope,iface)
-                continue
-        return ret
-    else:
-        ret = []
-        # Get-WmiObject Win32_NetworkAdapterConfiguration | select InterfaceIndex, IpAddress
-        for line in exec_query(['Get-WmiObject', 'Win32_NetworkAdapterConfiguration'], ['InterfaceIndex', 'IPAddress']):
-            try:
-                iface = dev_from_index(line[0])
-            except:
-                continue
-            _l_addresses = line[1]
-            _inline = []
-            if _l_addresses:
-                _inline = _l_addresses[1:-1].split(",")
-                for _address in _inline:
-                    _a = _address.strip()
-                    if "." not in _a:
-                        scope = scapy.utils6.in6_getscope(_a)
-                        ret.append((_a, scope, iface)) # (addr,scope,iface)
-        return ret
+    ifaddrs = []
+    for ifaddr in in6_getifaddr_raw():
+        try:
+            ifaddrs.append((ifaddr[0], ifaddr[1], dev_from_pcapname(ifaddr[2])))
+        except ValueError:
+            pass
+    return ifaddrs
 
 def _append_route6(routes, dpref, dp, nh, iface, lifaddr):
     cset = [] # candidate set (possible source addresses)
-- 
GitLab