Commit e7b3ad26 authored by Manuel Franceschini's avatar Manuel Franceschini
Browse files

netutils: make re class attribute and catch IndexError

These missing changes were initially agreed upon but then forgotten.
First, we move the valid name regex to the class-level such that it
won't be compiled for every invocation of GetIP() and we wrap the result
of getaddrinfo() into a try/except to catch a possible IndexError.
Signed-off-by: default avatarManuel Franceschini <>
Reviewed-by: default avatarIustin Pop <>
parent 0f979a34
......@@ -85,6 +85,8 @@ class Hostname:
"""Class implementing resolver and hostname functionality.
_VALID_NAME_RE = re.compile("^[a-z0-9._-]{1,255}$")
def __init__(self, name=None, family=None):
"""Initialize the host name object.
......@@ -139,10 +141,13 @@ class Hostname:
# getaddrinfo() returns a list of 5-tupes (family, socktype, proto,
# canonname, sockaddr). We return the first tuple's first address in
# sockaddr
return result[0][4][0]
return result[0][4][0]
except IndexError, err:
raise errors.ResolverError("Unknown error in getaddrinfo(): %s" % err)
def GetNormalizedName(hostname):
def GetNormalizedName(cls, hostname):
"""Validate and normalize the given hostname.
@attention: the validation is a bit more relaxed than the standards
......@@ -150,9 +155,8 @@ class Hostname:
@raise errors.OpPrereqError: when the name is not valid
valid_name_re = re.compile("^[a-z0-9._-]{1,255}$")
hostname = hostname.lower()
if (not valid_name_re.match(hostname) or
if (not cls._VALID_NAME_RE.match(hostname) or
# double-dots, meaning empty label
".." in hostname or
# empty initial label
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment