Mon, Sep 03, 2012 at 05:07:40PM CEST, olichtne(a)redhat.com wrote:
On Mon, Sep 03, 2012 at 04:53:34PM +0200, Jiri Pirko wrote:
> Mon, Sep 03, 2012 at 02:51:14PM CEST, olichtne(a)redhat.com wrote:
> >From: Ondrej Lichtner <olichtne(a)redhat.com>
> >
> >This commit adds functions verify_ip_address and verify_mac_address that
> >return a boolean value depending on the validity of given ip or mac
> >addresses.
> >
> >These will be used in the new Config parsing module.
> >I also added their usage to the address generation code in the same
> >module.
> >
> >Signed-off-by: Ondrej Lichtner <olichtne(a)redhat.com>
> >---
> > Common/NetUtils.py | 27 +++++++++++++++++++++------
> > 1 file changed, 21 insertions(+), 6 deletions(-)
> >
> >diff --git a/Common/NetUtils.py b/Common/NetUtils.py
> >index 8594a23..d7db077 100644
> >--- a/Common/NetUtils.py
> >+++ b/Common/NetUtils.py
> >@@ -37,6 +37,21 @@ def scan_netdevs():
> > scan.append({"name": d, "hwaddr": addr})
> > return scan
> >
> >+def verify_ip_address(addr):
> >+ if len(addr.split('.')) != 4:
> >+ return False
>
> This check is not necessary. inet_aton takes care of it as well.
inet_aton also accepts addresses that are incomplete e.g. '127' is accepted
and fills the blanks with zeros. I put this check here because the
IpPool class doesn't do this and will not work correctly in these cases.
Ah okay
Should I still make the fix, and include the fix for IpPool as well
while I'm at it?
Nope, that's fine.
>
> >+ try:
> >+ socket.inet_aton(addr)
> >+ return True
> >+ except:
> >+ return False
> >+
> >+def verify_mac_address(addr):
> >+ if re.match("^[0-9a-f]{2}([:][0-9a-f]{2}){5}$", addr, re.I):
> >+ return True
> >+ else:
> >+ return False
> >+
> > def get_corespond_local_ip(query_ip):
> > """
> > Get ip address in local system which can communicate with query_ip.
> >@@ -85,10 +100,10 @@ class AddressPool:
> >
> > class MacPool(AddressPool):
> > def _addr_to_byte_string(self, addr):
> >- bs = [int(byte, 16) for byte in addr.split(":")]
> >+ if not verify_mac_address(addr):
> >+ raise Exception("Invalid MAC address")
> >
> >- if len(bs) != 6:
> >- raise Exception("Malformed MAC address")
> >+ bs = [int(byte, 16) for byte in addr.split(":")]
> >
> > return bs
> >
> >@@ -97,10 +112,10 @@ class MacPool(AddressPool):
> >
> > class IpPool(AddressPool):
> > def _addr_to_byte_string(self, addr):
> >- bs = [int(byte) for byte in addr.split(".")]
> >+ if not verify_ip_address(addr):
> >+ raise Exception("Invalid IP address")
> >
> >- if len(bs) != 4:
> >- raise Exception("Malformed IP address")
> >+ bs = [int(byte) for byte in addr.split(".")]
> >
> > return bs
> >
> >--
> >1.7.11.4
> >
> >_______________________________________________
> >LNST-developers mailing list
> >LNST-developers(a)lists.fedorahosted.org
> >https://lists.fedorahosted.org/mailman/listinfo/lnst-developers