[virt-tools-list] [virt-manager PATCH 2/2] createinterface: check ip address format
Chen Hanxiao
chenhanxiao at cn.fujitsu.com
Fri Mar 28 06:57:14 UTC 2014
This patch will use socket module to check
whether an ip address is valid or not.
Signed-off-by: Chen Hanxiao <chenhanxiao at cn.fujitsu.com>
---
virtManager/createinterface.py | 26 +++++++++++++++++++++++---
1 file changed, 23 insertions(+), 3 deletions(-)
diff --git a/virtManager/createinterface.py b/virtManager/createinterface.py
index 4588bba..ac83291 100644
--- a/virtManager/createinterface.py
+++ b/virtManager/createinterface.py
@@ -24,6 +24,7 @@ from gi.repository import Gdk
# pylint: enable=E0611
import logging
+import socket
from virtinst import Interface, InterfaceProtocol
@@ -762,7 +763,23 @@ class vmmCreateInterface(vmmGObjectUI):
return self.build_ip_info()
def build_ip_info(self):
- def build_ip(addr_str):
+ def ipFormatCheck(addr, version):
+ if version == "IPv4":
+ try:
+ socket.inet_pton(socket.AF_INET, addr)
+ except socket.error:
+ return False
+ else:
+ return True
+ if version == "IPv6":
+ try:
+ socket.inet_pton(socket.AF_INET6, addr)
+ except socket.error:
+ return False
+ else:
+ return True
+
+ def build_ip(addr_str, version):
if not addr_str:
raise ValueError(_("Please enter an IP address"))
ret = addr_str.rsplit("/", 1)
@@ -770,6 +787,9 @@ class vmmCreateInterface(vmmGObjectUI):
prefix = None
if len(ret) > 1:
prefix = ret[1]
+
+ if not ipFormatCheck(address, version):
+ raise ValueError(_("Bad IP address: '%s'") % address)
return address, prefix
is_manual = self.widget("ip-do-manual").get_active()
@@ -803,7 +823,7 @@ class vmmCreateInterface(vmmGObjectUI):
ipv4.family = "ipv4"
ipv4.dhcp = bool(v4_mode == IP_DHCP)
if not ipv4.dhcp:
- addr, prefix = build_ip(v4_addr)
+ addr, prefix = build_ip(v4_addr, "IPv4")
if addr:
ipv4.add_ip(addr, prefix)
if v4_gate:
@@ -819,7 +839,7 @@ class vmmCreateInterface(vmmGObjectUI):
if v6_gate:
ipv6.gateway = v6_gate
for v6_addr in v6_addrlist:
- addr, prefix = build_ip(v6_addr)
+ addr, prefix = build_ip(v6_addr, "IPv6")
if addr:
ipv6.add_ip(addr, prefix)
--
1.8.5.3
More information about the virt-tools-list
mailing list