From: Christos Sfakianakis <csfakian(a)redhat.com>
Add multicast address verification (to be used with vxlan recipes).
Signed-off-by: Christos Sfakianakis <csfakian(a)redhat.com>
---
lnst/Common/IpAddress.py | 13 ++++++++++++-
1 file changed, 12 insertions(+), 1 deletion(-)
diff --git a/lnst/Common/IpAddress.py b/lnst/Common/IpAddress.py
index 53dc047..b9fc949 100644
--- a/lnst/Common/IpAddress.py
+++ b/lnst/Common/IpAddress.py
@@ -13,7 +13,7 @@ olichtne(a)redhat.com (Ondrej Lichtner)
import re
from socket import inet_pton, inet_ntop, AF_INET, AF_INET6
from binascii import hexlify
-import socket
+import socket, struct
from lnst.Common.LnstError import LnstError
#TODO create various generators for IPNetworks and IPaddresses in the same
@@ -74,6 +74,13 @@ class Ip4Address(BaseIpAddress):
return addr, prefixlen
+ def is_multicast(self):
+ pack = socket.inet_aton
+ unpack = struct.unpack
+ triade = ["224.0.0.0", str(self), "239.255.255.255"]
+ triade = [unpack("!L", pack(val))[0] for val in triade]
+ return sorted(triade) == triade
+
class Ip6Address(BaseIpAddress):
def __init__(self, addr):
super(Ip6Address, self).__init__(addr)
@@ -104,6 +111,10 @@ class Ip6Address(BaseIpAddress):
left_half = hexlify(socket.inet_pton(socket.AF_INET6, str(self)))[:16]
return left_half == 'fe80000000000000'
+ def is_multicast(self):
+ prefix = hexlify(socket.inet_pton(socket.AF_INET6, str(self)))[:2]
+ return prefix == 'ff'
+
def ipaddress(addr):
"""Factory method to create a BaseIpAddress object"""
if isinstance(addr, BaseIpAddress):
--
2.17.1