diff --git a/gns3server/modules/dynamips/__init__.py b/gns3server/modules/dynamips/__init__.py index 76a7679d..c02e7494 100644 --- a/gns3server/modules/dynamips/__init__.py +++ b/gns3server/modules/dynamips/__init__.py @@ -38,11 +38,13 @@ from pkg_resources import parse_version from uuid import UUID, uuid4 from ..base_manager import BaseManager from ..project_manager import ProjectManager +from ..port_manager import PortManager from .dynamips_error import DynamipsError from .hypervisor import Hypervisor from .nodes.router import Router from .dynamips_vm import DynamipsVM from .dynamips_device import DynamipsDevice +from gns3server.config import Config # NIOs from .nios.nio_udp import NIOUDP @@ -326,20 +328,26 @@ class Dynamips(BaseManager): if not working_dir: working_dir = tempfile.gettempdir() + # FIXME: hypervisor should always listen to 127.0.0.1 + # See https://github.com/GNS3/dynamips/issues/62 + server_config = Config.instance().get_section_config("Server") + server_host = server_config.get("host") + try: # let the OS find an unused port for the Dynamips hypervisor with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock: - sock.bind(("127.0.0.1", 0)) + sock.bind((server_host, 0)) port = sock.getsockname()[1] except OSError as e: raise DynamipsError("Could not find free port for the Dynamips hypervisor: {}".format(e)) - hypervisor = Hypervisor(self._dynamips_path, working_dir, "127.0.0.1", port) + port_manager = PortManager.instance() + hypervisor = Hypervisor(self._dynamips_path, working_dir, server_host, port, port_manager.console_host) log.info("Creating new hypervisor {}:{} with working directory {}".format(hypervisor.host, hypervisor.port, working_dir)) yield from hypervisor.start() - yield from self._wait_for_hypervisor("127.0.0.1", port) + yield from self._wait_for_hypervisor(server_host, port) log.info("Hypervisor {}:{} has successfully started".format(hypervisor.host, hypervisor.port)) yield from hypervisor.connect() diff --git a/gns3server/modules/dynamips/hypervisor.py b/gns3server/modules/dynamips/hypervisor.py index 5d1867d0..5a620357 100644 --- a/gns3server/modules/dynamips/hypervisor.py +++ b/gns3server/modules/dynamips/hypervisor.py @@ -38,13 +38,14 @@ class Hypervisor(DynamipsHypervisor): :param path: path to Dynamips executable :param working_dir: working directory - :param port: port for this hypervisor :param host: host/address for this hypervisor + :param port: port for this hypervisor + :param console_host: host/address for console connections """ _instance_count = 1 - def __init__(self, path, working_dir, host, port): + def __init__(self, path, working_dir, host, port, console_host): DynamipsHypervisor.__init__(self, working_dir, host, port) @@ -52,6 +53,7 @@ class Hypervisor(DynamipsHypervisor): self._id = Hypervisor._instance_count Hypervisor._instance_count += 1 + self._console_host = console_host self._path = path self._command = [] self._process = None @@ -182,7 +184,10 @@ class Hypervisor(DynamipsHypervisor): command = [self._path] command.extend(["-N1"]) # use instance IDs for filenames command.extend(["-l", "dynamips_i{}_log.txt".format(self._id)]) # log file - if self._host != "0.0.0.0" and self._host != "::": + # Dynamips cannot listen for hypervisor commands and for console connections on + # 2 different IP addresses. + # See https://github.com/GNS3/dynamips/issues/62 + if self._console_host != "0.0.0.0" and self._console_host != "::": command.extend(["-H", "{}:{}".format(self._host, self._port)]) else: command.extend(["-H", str(self._port)])