[virt-tools-list] [virt-manager PATCH 2/2] createinterface: check ip address format
Chen Hanxiao
chen_han_xiao at 126.com
Sat Mar 29 23:55:26 UTC 2014
On 03/30/2014 12:01 AM, Cole Robinson wrote:
> On 03/28/2014 02:57 AM, Chen Hanxiao wrote:
>> 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)
>>
>>
> We use python-ipaddr for the virtual network code, which makes validating an
> IP address a oneline I think. And this validation should actually be in
> virtinst/interface.py, if it fits there.
>
> - Cole
Thanks, I'll see and check.
-Chen
> _______________________________________________
> virt-tools-list mailing list
> virt-tools-list at redhat.com
> https://www.redhat.com/mailman/listinfo/virt-tools-list
>
More information about the virt-tools-list
mailing list