Revision c0c3fa27
b/lib/bootstrap.py | ||
---|---|---|
259 | 259 |
errors.ECODE_NOTUNIQUE) |
260 | 260 |
|
261 | 261 |
if secondary_ip: |
262 |
if not utils.IsValidIP(secondary_ip): |
|
262 |
if not utils.IsValidIP4(secondary_ip):
|
|
263 | 263 |
raise errors.OpPrereqError("Invalid secondary ip given", |
264 | 264 |
errors.ECODE_INVAL) |
265 | 265 |
if (secondary_ip != hostname.ip and |
b/lib/cmdlib.py | ||
---|---|---|
3594 | 3594 |
primary_ip = self.op.primary_ip = dns_data.ip |
3595 | 3595 |
if self.op.secondary_ip is None: |
3596 | 3596 |
self.op.secondary_ip = primary_ip |
3597 |
if not utils.IsValidIP(self.op.secondary_ip): |
|
3597 |
if not utils.IsValidIP4(self.op.secondary_ip):
|
|
3598 | 3598 |
raise errors.OpPrereqError("Invalid secondary IP given", |
3599 | 3599 |
errors.ECODE_INVAL) |
3600 | 3600 |
secondary_ip = self.op.secondary_ip |
... | ... | |
6839 | 6839 |
errors.ECODE_INVAL) |
6840 | 6840 |
nic_ip = self.hostname1.ip |
6841 | 6841 |
else: |
6842 |
if not utils.IsValidIP(ip): |
|
6842 |
if not utils.IsValidIP4(ip):
|
|
6843 | 6843 |
raise errors.OpPrereqError("Given IP address '%s' doesn't look" |
6844 | 6844 |
" like a valid IP" % ip, |
6845 | 6845 |
errors.ECODE_INVAL) |
... | ... | |
8513 | 8513 |
if nic_ip.lower() == constants.VALUE_NONE: |
8514 | 8514 |
nic_dict['ip'] = None |
8515 | 8515 |
else: |
8516 |
if not utils.IsValidIP(nic_ip): |
|
8516 |
if not utils.IsValidIP4(nic_ip):
|
|
8517 | 8517 |
raise errors.OpPrereqError("Invalid IP address '%s'" % nic_ip, |
8518 | 8518 |
errors.ECODE_INVAL) |
8519 | 8519 |
|
b/lib/hypervisor/hv_kvm.py | ||
---|---|---|
71 | 71 |
constants.HV_ACPI: hv_base.NO_CHECK, |
72 | 72 |
constants.HV_SERIAL_CONSOLE: hv_base.NO_CHECK, |
73 | 73 |
constants.HV_VNC_BIND_ADDRESS: |
74 |
(False, lambda x: (utils.IsValidIP(x) or utils.IsNormAbsPath(x)), |
|
74 |
(False, lambda x: (utils.IsValidIP4(x) or utils.IsNormAbsPath(x)),
|
|
75 | 75 |
"the VNC bind address must be either a valid IP address or an absolute" |
76 | 76 |
" pathname", None, None), |
77 | 77 |
constants.HV_VNC_TLS: hv_base.NO_CHECK, |
... | ... | |
514 | 514 |
|
515 | 515 |
vnc_bind_address = hvp[constants.HV_VNC_BIND_ADDRESS] |
516 | 516 |
if vnc_bind_address: |
517 |
if utils.IsValidIP(vnc_bind_address): |
|
517 |
if utils.IsValidIP4(vnc_bind_address):
|
|
518 | 518 |
if instance.network_port > constants.VNC_BASE_PORT: |
519 | 519 |
display = instance.network_port - constants.VNC_BASE_PORT |
520 | 520 |
if vnc_bind_address == constants.IP4_ADDRESS_ANY: |
b/lib/hypervisor/hv_xen.py | ||
---|---|---|
549 | 549 |
hv_base.ParamInSet(True, constants.HT_HVM_VALID_NIC_TYPES), |
550 | 550 |
constants.HV_PAE: hv_base.NO_CHECK, |
551 | 551 |
constants.HV_VNC_BIND_ADDRESS: |
552 |
(False, utils.IsValidIP, |
|
552 |
(False, utils.IsValidIP4,
|
|
553 | 553 |
"VNC bind address is not a valid IP address", None, None), |
554 | 554 |
constants.HV_KERNEL_PATH: hv_base.REQ_FILE_CHECK, |
555 | 555 |
constants.HV_DEVICE_MODEL: hv_base.REQ_FILE_CHECK, |
b/lib/utils.py | ||
---|---|---|
1292 | 1292 |
return nv |
1293 | 1293 |
|
1294 | 1294 |
|
1295 |
def _GenericIsValidIP(family, ip): |
|
1296 |
"""Generic internal version of ip validation. |
|
1297 |
|
|
1298 |
@type family: int |
|
1299 |
@param family: socket.AF_INET | socket.AF_INET6 |
|
1300 |
@type ip: str |
|
1301 |
@param ip: the address to be checked |
|
1302 |
@rtype: boolean |
|
1303 |
@return: True if ip is valid, False otherwise |
|
1304 |
|
|
1305 |
""" |
|
1306 |
try: |
|
1307 |
socket.inet_pton(family, ip) |
|
1308 |
return True |
|
1309 |
except socket.error: |
|
1310 |
return False |
|
1311 |
|
|
1312 |
|
|
1313 |
def IsValidIP4(ip): |
|
1314 |
"""Verifies an IPv4 address. |
|
1315 |
|
|
1316 |
This function checks if the given address is a valid IPv4 address. |
|
1317 |
|
|
1318 |
@type ip: str |
|
1319 |
@param ip: the address to be checked |
|
1320 |
@rtype: boolean |
|
1321 |
@return: True if ip is valid, False otherwise |
|
1322 |
|
|
1323 |
""" |
|
1324 |
return _GenericIsValidIP(socket.AF_INET, ip) |
|
1325 |
|
|
1326 |
|
|
1327 |
def IsValidIP6(ip): |
|
1328 |
"""Verifies an IPv6 address. |
|
1329 |
|
|
1330 |
This function checks if the given address is a valid IPv6 address. |
|
1331 |
|
|
1332 |
@type ip: str |
|
1333 |
@param ip: the address to be checked |
|
1334 |
@rtype: boolean |
|
1335 |
@return: True if ip is valid, False otherwise |
|
1336 |
|
|
1337 |
""" |
|
1338 |
return _GenericIsValidIP(socket.AF_INET6, ip) |
|
1339 |
|
|
1340 |
|
|
1295 | 1341 |
def IsValidIP(ip): |
1296 |
"""Verifies the syntax of an IPv4 address.
|
|
1342 |
"""Verifies an IP address.
|
|
1297 | 1343 |
|
1298 |
This function checks if the IPv4 address passes is valid or not based |
|
1299 |
on syntax (not IP range, class calculations, etc.). |
|
1344 |
This function checks if the given IP address (both IPv4 and IPv6) is valid. |
|
1300 | 1345 |
|
1301 | 1346 |
@type ip: str |
1302 | 1347 |
@param ip: the address to be checked |
1303 |
@rtype: a regular expression match object |
|
1304 |
@return: a regular expression match object, or None if the |
|
1305 |
address is not valid |
|
1348 |
@rtype: boolean |
|
1349 |
@return: True if ip is valid, False otherwise |
|
1306 | 1350 |
|
1307 | 1351 |
""" |
1308 |
unit = "(0|[1-9]\d{0,2})" |
|
1309 |
#TODO: convert and return only boolean |
|
1310 |
return re.match("^%s\.%s\.%s\.%s$" % (unit, unit, unit, unit), ip) |
|
1352 |
return IsValidIP4(ip) or IsValidIP6(ip) |
|
1311 | 1353 |
|
1312 | 1354 |
|
1313 | 1355 |
def IsValidShellParam(word): |
b/scripts/gnt-instance | ||
---|---|---|
1191 | 1191 |
vnc_console_port = "%s:%s (display %s)" % (instance["pnode"], |
1192 | 1192 |
port, |
1193 | 1193 |
display) |
1194 |
elif display > 0 and utils.IsValidIP(vnc_bind_address): |
|
1194 |
elif display > 0 and utils.IsValidIP4(vnc_bind_address):
|
|
1195 | 1195 |
vnc_console_port = ("%s:%s (node %s) (display %s)" % |
1196 | 1196 |
(vnc_bind_address, port, |
1197 | 1197 |
instance["pnode"], display)) |
b/test/ganeti.utils_unittest.py | ||
---|---|---|
2426 | 2426 |
self.assertFalse(utils.IgnoreProcessNotFound(os.kill, pid, 0)) |
2427 | 2427 |
|
2428 | 2428 |
|
2429 |
class TestIsValidIP4(unittest.TestCase): |
|
2430 |
def test(self): |
|
2431 |
self.assert_(utils.IsValidIP4("127.0.0.1")) |
|
2432 |
self.assert_(utils.IsValidIP4("0.0.0.0")) |
|
2433 |
self.assert_(utils.IsValidIP4("255.255.255.255")) |
|
2434 |
self.assertFalse(utils.IsValidIP4("0")) |
|
2435 |
self.assertFalse(utils.IsValidIP4("1")) |
|
2436 |
self.assertFalse(utils.IsValidIP4("1.1.1")) |
|
2437 |
self.assertFalse(utils.IsValidIP4("255.255.255.256")) |
|
2438 |
self.assertFalse(utils.IsValidIP4("::1")) |
|
2439 |
|
|
2440 |
|
|
2441 |
class TestIsValidIP6(unittest.TestCase): |
|
2442 |
def test(self): |
|
2443 |
self.assert_(utils.IsValidIP6("::")) |
|
2444 |
self.assert_(utils.IsValidIP6("::1")) |
|
2445 |
self.assert_(utils.IsValidIP6("1" + (":1" * 7))) |
|
2446 |
self.assert_(utils.IsValidIP6("ffff" + (":ffff" * 7))) |
|
2447 |
self.assertFalse(utils.IsValidIP6("0")) |
|
2448 |
self.assertFalse(utils.IsValidIP6(":1")) |
|
2449 |
self.assertFalse(utils.IsValidIP6("f" + (":f" * 6))) |
|
2450 |
self.assertFalse(utils.IsValidIP6("fffg" + (":ffff" * 7))) |
|
2451 |
self.assertFalse(utils.IsValidIP6("fffff" + (":ffff" * 7))) |
|
2452 |
self.assertFalse(utils.IsValidIP6("1" + (":1" * 8))) |
|
2453 |
self.assertFalse(utils.IsValidIP6("127.0.0.1")) |
|
2454 |
|
|
2455 |
|
|
2456 |
class TestIsValidIP(unittest.TestCase): |
|
2457 |
def test(self): |
|
2458 |
self.assert_(utils.IsValidIP("0.0.0.0")) |
|
2459 |
self.assert_(utils.IsValidIP("127.0.0.1")) |
|
2460 |
self.assert_(utils.IsValidIP("::")) |
|
2461 |
self.assert_(utils.IsValidIP("::1")) |
|
2462 |
self.assertFalse(utils.IsValidIP("0")) |
|
2463 |
self.assertFalse(utils.IsValidIP("1.1.1.256")) |
|
2464 |
self.assertFalse(utils.IsValidIP("a:g::1")) |
|
2465 |
|
|
2466 |
|
|
2429 | 2467 |
if __name__ == '__main__': |
2430 | 2468 |
testutils.GanetiTestProgram() |
Also available in: Unified diff