Skip to content

Commit

Permalink
Merge pull request #68 from MoshiBin/ipv6-discover
Browse files Browse the repository at this point in the history
Add ipv6 option for discover_ssdp
  • Loading branch information
mraineri authored Oct 10, 2019
2 parents 45d3867 + 79adddd commit 1b13366
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/redfish/discovery/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def sanitize(number, minimum, maximum=None):
return number


def discover_ssdp(port=1900, ttl=2, response_time=3, iface=None):
def discover_ssdp(port=1900, ttl=2, response_time=3, iface=None, protocol="ipv4"):
"""Discovers Redfish services via SSDP
:param port: the port to use for the SSDP request
Expand All @@ -52,15 +52,28 @@ def discover_ssdp(port=1900, ttl=2, response_time=3, iface=None):
:type response_time: int
:param iface: the interface to use for the request; None for all
:type iface: string
:param protocol: the type of protocol to use for the request; either 'ipv4' or 'ipv6'
:type protocol: string
:returns: a set of discovery data
"""
# Sanity check the inputs
valid_protocols = ("ipv4", "ipv6")
if protocol not in valid_protocols:
raise ValueError("Invalid protocol type. Expected one of: {}".format(valid_protocols))
ttl = sanitize(ttl, minimum=1, maximum=255)
response_time = sanitize(response_time, minimum=1)

if protocol == "ipv6":
mcast_ip = "ff02::c"
mcast_connection = (mcast_ip, port, 0, 0)
af_type = socket.AF_INET6
elif protocol == "ipv4":
mcast_ip = "239.255.255.250"
mcast_connection = (mcast_ip, port)
af_type = socket.AF_INET

# Initialize the multicast data
mcast_ip = "239.255.255.250"
msearch_str = (
"M-SEARCH * HTTP/1.1\r\n"
"Host: {}:{}\r\n"
Expand All @@ -71,12 +84,12 @@ def discover_ssdp(port=1900, ttl=2, response_time=3, iface=None):
socket.setdefaulttimeout(response_time + 2)

# Set up the socket and send the request
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock = socket.socket(af_type, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, ttl)
if iface:
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BINDTODEVICE, str(iface+"\0").encode("utf-8"))
sock.sendto(bytearray(msearch_str, "utf-8"), (mcast_ip, port))
sock.sendto(bytearray(msearch_str, "utf-8"), mcast_connection)

# On the same socket, wait for responses
discovered_services = {}
Expand Down

0 comments on commit 1b13366

Please sign in to comment.