Revision e7b3ad26 lib/netutils.py
b/lib/netutils.py | ||
---|---|---|
85 | 85 |
"""Class implementing resolver and hostname functionality. |
86 | 86 |
|
87 | 87 |
""" |
88 |
_VALID_NAME_RE = re.compile("^[a-z0-9._-]{1,255}$") |
|
89 |
|
|
88 | 90 |
def __init__(self, name=None, family=None): |
89 | 91 |
"""Initialize the host name object. |
90 | 92 |
|
... | ... | |
139 | 141 |
# getaddrinfo() returns a list of 5-tupes (family, socktype, proto, |
140 | 142 |
# canonname, sockaddr). We return the first tuple's first address in |
141 | 143 |
# sockaddr |
142 |
return result[0][4][0] |
|
144 |
try: |
|
145 |
return result[0][4][0] |
|
146 |
except IndexError, err: |
|
147 |
raise errors.ResolverError("Unknown error in getaddrinfo(): %s" % err) |
|
143 | 148 |
|
144 |
@staticmethod
|
|
145 |
def GetNormalizedName(hostname): |
|
149 |
@classmethod
|
|
150 |
def GetNormalizedName(cls, hostname):
|
|
146 | 151 |
"""Validate and normalize the given hostname. |
147 | 152 |
|
148 | 153 |
@attention: the validation is a bit more relaxed than the standards |
... | ... | |
150 | 155 |
@raise errors.OpPrereqError: when the name is not valid |
151 | 156 |
|
152 | 157 |
""" |
153 |
valid_name_re = re.compile("^[a-z0-9._-]{1,255}$") |
|
154 | 158 |
hostname = hostname.lower() |
155 |
if (not valid_name_re.match(hostname) or
|
|
159 |
if (not cls._VALID_NAME_RE.match(hostname) or
|
|
156 | 160 |
# double-dots, meaning empty label |
157 | 161 |
".." in hostname or |
158 | 162 |
# empty initial label |
Also available in: Unified diff