2014-05-08 01:31:53 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# Copyright (C) 2014 GNS3 Technologies Inc.
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
2016-11-04 16:32:16 +00:00
|
|
|
import os
|
2014-05-08 01:31:53 +00:00
|
|
|
import sys
|
2015-01-24 22:32:58 +00:00
|
|
|
import aiohttp
|
2015-06-07 19:51:33 +00:00
|
|
|
import socket
|
|
|
|
import struct
|
2015-11-08 20:34:27 +00:00
|
|
|
import psutil
|
2014-05-08 01:31:53 +00:00
|
|
|
|
2015-12-01 08:42:36 +00:00
|
|
|
if psutil.version_info < (3, 0, 0):
|
2016-03-15 00:27:51 +00:00
|
|
|
raise Exception("psutil version should >= 3.0.0. If you are under Ubuntu/Debian install gns3 via apt instead of pip")
|
2015-12-01 08:42:36 +00:00
|
|
|
|
2014-05-08 01:31:53 +00:00
|
|
|
import logging
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2015-01-24 22:32:58 +00:00
|
|
|
def _get_windows_interfaces_from_registry():
|
2014-12-10 02:17:25 +00:00
|
|
|
|
|
|
|
import winreg
|
|
|
|
|
2015-11-09 11:28:00 +00:00
|
|
|
# HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces
|
2014-12-10 02:17:25 +00:00
|
|
|
interfaces = []
|
|
|
|
try:
|
|
|
|
hkey = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, r"SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards")
|
|
|
|
for index in range(winreg.QueryInfoKey(hkey)[0]):
|
|
|
|
network_card_id = winreg.EnumKey(hkey, index)
|
|
|
|
hkeycard = winreg.OpenKey(hkey, network_card_id)
|
|
|
|
guid, _ = winreg.QueryValueEx(hkeycard, "ServiceName")
|
2015-06-12 23:54:02 +00:00
|
|
|
netcard, _ = winreg.QueryValueEx(hkeycard, "Description")
|
2014-12-10 02:17:25 +00:00
|
|
|
connection = r"SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}" + "\{}\Connection".format(guid)
|
|
|
|
hkeycon = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, connection)
|
|
|
|
name, _ = winreg.QueryValueEx(hkeycon, "Name")
|
2015-06-12 23:54:02 +00:00
|
|
|
interface = r"SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\{}".format(guid)
|
|
|
|
hkeyinterface = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, interface)
|
|
|
|
is_dhcp_enabled, _ = winreg.QueryValueEx(hkeyinterface, "EnableDHCP")
|
|
|
|
if is_dhcp_enabled:
|
|
|
|
ip_address, _ = winreg.QueryValueEx(hkeyinterface, "DhcpIPAddress")
|
2016-09-28 08:48:28 +00:00
|
|
|
netmask, _ = winreg.QueryValueEx(hkeyinterface, "DhcpSubnetMask")
|
2015-06-12 23:54:02 +00:00
|
|
|
else:
|
|
|
|
ip_address, _ = winreg.QueryValueEx(hkeyinterface, "IPAddress")
|
2016-09-28 08:48:28 +00:00
|
|
|
netmask, _ = winreg.QueryValueEx(hkeyinterface, "SubnetMask")
|
2015-06-12 23:54:02 +00:00
|
|
|
if ip_address:
|
|
|
|
# get the first IPv4 address only
|
|
|
|
ip_address = ip_address[0]
|
2014-12-10 02:17:25 +00:00
|
|
|
npf_interface = "\\Device\\NPF_{guid}".format(guid=guid)
|
2014-07-08 20:01:45 +00:00
|
|
|
interfaces.append({"id": npf_interface,
|
2015-06-12 23:54:02 +00:00
|
|
|
"name": name,
|
|
|
|
"ip_address": ip_address,
|
2016-03-15 00:27:51 +00:00
|
|
|
"mac_address": "", # TODO: find MAC address in registry
|
2016-07-10 20:54:31 +00:00
|
|
|
"netcard": netcard,
|
2016-09-28 08:48:28 +00:00
|
|
|
"netmask": netmask,
|
2016-07-10 20:54:31 +00:00
|
|
|
"type": "ethernet"})
|
2015-06-12 23:54:02 +00:00
|
|
|
winreg.CloseKey(hkeyinterface)
|
2014-12-10 02:17:25 +00:00
|
|
|
winreg.CloseKey(hkeycon)
|
|
|
|
winreg.CloseKey(hkeycard)
|
|
|
|
winreg.CloseKey(hkey)
|
|
|
|
except OSError as e:
|
|
|
|
log.error("could not read registry information: {}".format(e))
|
|
|
|
|
2014-05-08 01:31:53 +00:00
|
|
|
return interfaces
|
|
|
|
|
2014-05-08 20:20:12 +00:00
|
|
|
|
2015-02-12 02:21:34 +00:00
|
|
|
def get_windows_interfaces():
|
2014-05-08 01:31:53 +00:00
|
|
|
"""
|
2015-01-24 22:32:58 +00:00
|
|
|
Get Windows interfaces.
|
2014-05-08 01:31:53 +00:00
|
|
|
|
2015-01-24 22:32:58 +00:00
|
|
|
:returns: list of windows interfaces
|
|
|
|
"""
|
|
|
|
|
|
|
|
import win32com.client
|
|
|
|
import pywintypes
|
2015-04-23 06:03:44 +00:00
|
|
|
|
2015-01-24 22:32:58 +00:00
|
|
|
interfaces = []
|
|
|
|
try:
|
2015-04-23 06:03:44 +00:00
|
|
|
locator = win32com.client.Dispatch("WbemScripting.SWbemLocator")
|
|
|
|
service = locator.ConnectServer(".", "root\cimv2")
|
2016-11-16 11:28:02 +00:00
|
|
|
network_configs = service.InstancesOf("Win32_NetworkAdapterConfiguration")
|
2015-01-24 22:32:58 +00:00
|
|
|
# more info on Win32_NetworkAdapter: http://msdn.microsoft.com/en-us/library/aa394216%28v=vs.85%29.aspx
|
|
|
|
for adapter in service.InstancesOf("Win32_NetworkAdapter"):
|
|
|
|
if adapter.NetConnectionStatus == 2 or adapter.NetConnectionStatus == 7:
|
|
|
|
# adapter is connected or media disconnected
|
2015-06-12 23:54:02 +00:00
|
|
|
ip_address = ""
|
2016-09-28 08:48:28 +00:00
|
|
|
netmask = ""
|
2016-11-16 11:28:02 +00:00
|
|
|
for network_config in network_configs:
|
2015-06-12 23:54:02 +00:00
|
|
|
if network_config.InterfaceIndex == adapter.InterfaceIndex:
|
|
|
|
if network_config.IPAddress:
|
|
|
|
# get the first IPv4 address only
|
|
|
|
ip_address = network_config.IPAddress[0]
|
2016-09-28 08:48:28 +00:00
|
|
|
netmask = network_config.IPSubnet[0]
|
2015-06-12 23:54:02 +00:00
|
|
|
break
|
2015-01-24 22:32:58 +00:00
|
|
|
npf_interface = "\\Device\\NPF_{guid}".format(guid=adapter.GUID)
|
|
|
|
interfaces.append({"id": npf_interface,
|
2015-05-16 01:09:48 +00:00
|
|
|
"name": adapter.NetConnectionID,
|
2015-06-12 23:54:02 +00:00
|
|
|
"ip_address": ip_address,
|
2016-03-15 00:27:51 +00:00
|
|
|
"mac_address": adapter.MACAddress,
|
2016-07-10 20:54:31 +00:00
|
|
|
"netcard": adapter.name,
|
2016-09-28 08:48:28 +00:00
|
|
|
"netmask": netmask,
|
2016-07-10 20:54:31 +00:00
|
|
|
"type": "ethernet"})
|
2015-01-24 22:32:58 +00:00
|
|
|
except (AttributeError, pywintypes.com_error):
|
2015-04-23 06:03:44 +00:00
|
|
|
log.warn("Could not use the COM service to retrieve interface info, trying using the registry...")
|
2015-01-24 22:32:58 +00:00
|
|
|
return _get_windows_interfaces_from_registry()
|
|
|
|
|
|
|
|
return interfaces
|
|
|
|
|
|
|
|
|
2016-09-27 09:47:18 +00:00
|
|
|
def has_netmask(interface_name):
|
|
|
|
"""
|
|
|
|
Checks if an interface has a netmask.
|
|
|
|
|
|
|
|
:param interface: interface name
|
|
|
|
|
|
|
|
:returns: boolean
|
|
|
|
"""
|
|
|
|
for interface in interfaces():
|
|
|
|
if interface["name"] == interface_name:
|
|
|
|
if interface["netmask"] and len(interface["netmask"]) > 0:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2015-06-07 19:51:33 +00:00
|
|
|
def is_interface_up(interface):
|
|
|
|
"""
|
|
|
|
Checks if an interface is up.
|
|
|
|
|
|
|
|
:param interface: interface name
|
|
|
|
|
|
|
|
:returns: boolean
|
|
|
|
"""
|
|
|
|
|
|
|
|
if sys.platform.startswith("linux"):
|
2015-07-08 20:51:21 +00:00
|
|
|
|
2015-11-08 20:34:27 +00:00
|
|
|
if interface not in psutil.net_if_addrs():
|
2015-07-08 20:51:21 +00:00
|
|
|
return False
|
|
|
|
|
2015-06-07 19:51:33 +00:00
|
|
|
import fcntl
|
|
|
|
SIOCGIFFLAGS = 0x8913
|
|
|
|
try:
|
|
|
|
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
|
|
|
|
result = fcntl.ioctl(s.fileno(), SIOCGIFFLAGS, interface + '\0' * 256)
|
|
|
|
flags, = struct.unpack('H', result[16:18])
|
|
|
|
if flags & 1: # check if the up bit is set
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
except OSError as e:
|
2015-06-09 14:35:21 +00:00
|
|
|
raise aiohttp.web.HTTPInternalServerError(text="Exception when checking if {} is up: {}".format(interface, e))
|
2015-06-07 19:51:33 +00:00
|
|
|
else:
|
2015-06-09 08:10:06 +00:00
|
|
|
# TODO: Windows & OSX support
|
2015-06-07 19:51:33 +00:00
|
|
|
return True
|
|
|
|
|
2016-07-12 11:09:08 +00:00
|
|
|
|
2016-11-04 16:32:16 +00:00
|
|
|
def is_interface_bridge(interface):
|
|
|
|
"""
|
|
|
|
:returns: True if interface is a bridge
|
|
|
|
"""
|
|
|
|
return os.path.exists(os.path.join("/sys/class/net/", interface, "bridge"))
|
|
|
|
|
|
|
|
|
2016-05-21 02:19:28 +00:00
|
|
|
def _check_windows_service(service_name):
|
|
|
|
|
|
|
|
import pywintypes
|
|
|
|
import win32service
|
|
|
|
import win32serviceutil
|
|
|
|
|
|
|
|
try:
|
|
|
|
if win32serviceutil.QueryServiceStatus(service_name, None)[1] != win32service.SERVICE_RUNNING:
|
|
|
|
return False
|
|
|
|
except pywintypes.error as e:
|
|
|
|
if e.winerror == 1060:
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
raise aiohttp.web.HTTPInternalServerError(text="Could not check if the {} service is running: {}".format(service_name, e.strerror))
|
|
|
|
return True
|
2015-06-09 08:10:06 +00:00
|
|
|
|
2016-07-12 11:09:08 +00:00
|
|
|
|
2015-01-24 22:32:58 +00:00
|
|
|
def interfaces():
|
2014-05-08 01:31:53 +00:00
|
|
|
"""
|
2015-01-24 22:32:58 +00:00
|
|
|
Gets the network interfaces on this server.
|
2014-05-08 01:31:53 +00:00
|
|
|
|
2015-01-24 22:32:58 +00:00
|
|
|
:returns: list of network interfaces
|
|
|
|
"""
|
|
|
|
|
|
|
|
results = []
|
2014-05-08 01:31:53 +00:00
|
|
|
if not sys.platform.startswith("win"):
|
2015-11-08 20:34:27 +00:00
|
|
|
for interface in sorted(psutil.net_if_addrs().keys()):
|
2015-06-12 23:54:02 +00:00
|
|
|
ip_address = ""
|
2016-03-15 00:27:51 +00:00
|
|
|
mac_address = ""
|
2016-08-25 15:22:22 +00:00
|
|
|
netmask = ""
|
2016-07-10 20:54:31 +00:00
|
|
|
interface_type = "ethernet"
|
2015-11-08 20:34:27 +00:00
|
|
|
for addr in psutil.net_if_addrs()[interface]:
|
|
|
|
# get the first available IPv4 address only
|
|
|
|
if addr.family == socket.AF_INET:
|
|
|
|
ip_address = addr.address
|
2016-08-25 15:22:22 +00:00
|
|
|
netmask = addr.netmask
|
2016-03-15 00:27:51 +00:00
|
|
|
if addr.family == psutil.AF_LINK:
|
|
|
|
mac_address = addr.address
|
2016-07-10 20:54:31 +00:00
|
|
|
if interface.startswith("tap"):
|
|
|
|
# found no way to reliably detect a TAP interface
|
|
|
|
interface_type = "tap"
|
2015-06-09 14:35:21 +00:00
|
|
|
results.append({"id": interface,
|
2015-06-12 23:54:02 +00:00
|
|
|
"name": interface,
|
2016-03-15 00:27:51 +00:00
|
|
|
"ip_address": ip_address,
|
2016-08-25 15:22:22 +00:00
|
|
|
"netmask": netmask,
|
2016-07-10 20:54:31 +00:00
|
|
|
"mac_address": mac_address,
|
|
|
|
"type": interface_type})
|
2014-05-08 01:31:53 +00:00
|
|
|
else:
|
|
|
|
try:
|
2016-07-12 11:09:08 +00:00
|
|
|
service_installed = True
|
2016-05-21 02:19:28 +00:00
|
|
|
if not _check_windows_service("npf") and not _check_windows_service("npcap"):
|
2016-07-12 11:09:08 +00:00
|
|
|
service_installed = False
|
|
|
|
else:
|
|
|
|
results = get_windows_interfaces()
|
2014-05-08 01:31:53 +00:00
|
|
|
except ImportError:
|
|
|
|
message = "pywin32 module is not installed, please install it on the server to get the available interface names"
|
2015-01-24 22:32:58 +00:00
|
|
|
raise aiohttp.web.HTTPInternalServerError(text=message)
|
2014-05-08 01:31:53 +00:00
|
|
|
except Exception as e:
|
|
|
|
log.error("uncaught exception {type}".format(type=type(e)), exc_info=1)
|
2015-01-24 22:32:58 +00:00
|
|
|
raise aiohttp.web.HTTPInternalServerError(text="uncaught exception: {}".format(e))
|
2016-07-12 11:09:08 +00:00
|
|
|
|
|
|
|
if service_installed is False:
|
|
|
|
raise aiohttp.web.HTTPInternalServerError(text="The Winpcap or Npcap is not installed or running")
|
|
|
|
|
2016-09-14 15:15:06 +00:00
|
|
|
# This interface have special behavior
|
|
|
|
for result in results:
|
|
|
|
result["special"] = False
|
2016-11-16 11:21:43 +00:00
|
|
|
for special_interface in ("lo", "vmnet", "vboxnet", "docker", "lxcbr", "virbr", "ovs-system", "veth", "fw", "p2p", "bridge", "vmware", "virtualbox"):
|
2016-09-14 15:15:06 +00:00
|
|
|
if result["name"].lower().startswith(special_interface):
|
|
|
|
result["special"] = True
|
2015-01-24 22:32:58 +00:00
|
|
|
return results
|