From a779fa7462d29791c3d1ccf2c19f5df83c8be438 Mon Sep 17 00:00:00 2001
From: grossmj <grossmj@gns3.net>
Date: Tue, 8 Sep 2015 02:00:39 -0600
Subject: [PATCH] Check for valid FR or ATM switch mappings. Fixes #300.

---
 gns3server/modules/dynamips/nodes/atm_switch.py         | 8 +++++++-
 gns3server/modules/dynamips/nodes/frame_relay_switch.py | 8 +++++++-
 2 files changed, 14 insertions(+), 2 deletions(-)

diff --git a/gns3server/modules/dynamips/nodes/atm_switch.py b/gns3server/modules/dynamips/nodes/atm_switch.py
index 141293af..86dc2305 100644
--- a/gns3server/modules/dynamips/nodes/atm_switch.py
+++ b/gns3server/modules/dynamips/nodes/atm_switch.py
@@ -51,10 +51,14 @@ class ATMSwitch(Device):
 
     def __json__(self):
 
+        mappings = {}
+        for source, destination in self._mappings.items():
+            mappings[str(source)] = str(destination)
+
         return {"name": self.name,
                 "device_id": self.id,
                 "project_id": self.project.id,
-                "mappings": self._mappings}
+                "mappings": mappings}
 
     @asyncio.coroutine
     def create(self):
@@ -199,6 +203,8 @@ class ATMSwitch(Device):
 
         pvc_entry = re.compile(r"""^([0-9]*):([0-9]*):([0-9]*)$""")
         for source, destination in mappings.items():
+            if not isinstance(source, str) or not isinstance(destination, str):
+                raise DynamipsError("Invalid ATM mappings")
             match_source_pvc = pvc_entry.search(source)
             match_destination_pvc = pvc_entry.search(destination)
             if match_source_pvc and match_destination_pvc:
diff --git a/gns3server/modules/dynamips/nodes/frame_relay_switch.py b/gns3server/modules/dynamips/nodes/frame_relay_switch.py
index c1f06c84..9039dca6 100644
--- a/gns3server/modules/dynamips/nodes/frame_relay_switch.py
+++ b/gns3server/modules/dynamips/nodes/frame_relay_switch.py
@@ -50,10 +50,14 @@ class FrameRelaySwitch(Device):
 
     def __json__(self):
 
+        mappings = {}
+        for source, destination in self._mappings.items():
+            mappings[str(source)] = str(destination)
+
         return {"name": self.name,
                 "device_id": self.id,
                 "project_id": self.project.id,
-                "mappings": self._mappings}
+                "mappings": mappings}
 
     @asyncio.coroutine
     def create(self):
@@ -191,6 +195,8 @@ class FrameRelaySwitch(Device):
         """
 
         for source, destination in mappings.items():
+            if not isinstance(source, str) or not isinstance(destination, str):
+                raise DynamipsError("Invalid Frame-Relay mappings")
             source_port, source_dlci = map(int, source.split(':'))
             destination_port, destination_dlci = map(int, destination.split(':'))
             if self.has_port(destination_port):