Python socket.IPV6_MULTICAST_HOPS Examples

The following are 9 code examples of socket.IPV6_MULTICAST_HOPS(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module socket , or try the search function .
Example #1
Source File: mcast.py    From odoo13-x64 with GNU General Public License v3.0 6 votes vote down vote up
def sender(group):
    addrinfo = socket.getaddrinfo(group, None)[0]

    s = socket.socket(addrinfo[0], socket.SOCK_DGRAM)

    # Set Time-to-live (optional)
    ttl_bin = struct.pack('@i', MYTTL)
    if addrinfo[0] == socket.AF_INET: # IPv4
        s.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, ttl_bin)
    else:
        s.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_HOPS, ttl_bin)

    while True:
        data = repr(time.time()).encode('utf-8') + b'\0'
        s.sendto(data, (addrinfo[4][0], MYPORT))
        time.sleep(1) 
Example #2
Source File: mcast.py    From android_universal with MIT License 6 votes vote down vote up
def sender(group):
    addrinfo = socket.getaddrinfo(group, None)[0]

    s = socket.socket(addrinfo[0], socket.SOCK_DGRAM)

    # Set Time-to-live (optional)
    ttl_bin = struct.pack('@i', MYTTL)
    if addrinfo[0] == socket.AF_INET: # IPv4
        s.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, ttl_bin)
    else:
        s.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_HOPS, ttl_bin)

    while True:
        data = repr(time.time()).encode('utf-8') + b'\0'
        s.sendto(data, (addrinfo[4][0], MYPORT))
        time.sleep(1) 
Example #3
Source File: minusconf.py    From scoop with GNU Lesser General Public License v3.0 5 votes vote down vote up
def _multicast_configure_sender(sock, ttl=None):
    if ttl != None:
        ttl_bin = struct.pack('@I', ttl)

        sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, ttl_bin)
        if sock.family == socket.AF_INET6:
            sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_HOPS, ttl_bin) 
Example #4
Source File: recipe-442490.py    From code with MIT License 5 votes vote down vote up
def ipv6Multicast(host='::1', maddr=maddr):
    haddr = socket.getaddrinfo(host, maddr[1], socket.AF_INET6, socket.SOCK_DGRAM)[0][-1]
    maddr = socket.getaddrinfo(maddr[0], maddr[1], socket.AF_INET6, socket.SOCK_DGRAM)[0][-1]

    sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)

    sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    if hasattr(socket, "SO_REUSEPORT"):
        sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)

    sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_LOOP, 1)
    sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_HOPS, 5)

    ifn = haddr[3] or if_nametoindex('lo0')
    ifn = struct.pack("I", ifn)
    sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_IF, ifn)

    group = socket.inet_pton(socket.AF_INET6, maddr[0]) + ifn
    sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_JOIN_GROUP, group)

    sock.bind(haddr)
    sock.setblocking(False)

    return sock, maddr

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#~ Main 
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
Example #5
Source File: wsdd.py    From wsdd with MIT License 5 votes vote down vote up
def init_v6(self):
        idx = socket.if_nametoindex(self.interface.name)
        self.multicast_address = (WSD_MCAST_GRP_V6, WSD_UDP_PORT, 0x575C, idx)

        # v6: member_request = { multicast_addr, intf_idx }
        mreq = (
            socket.inet_pton(self.family, WSD_MCAST_GRP_V6) +
            struct.pack('@I', idx))
        self.recv_socket.setsockopt(
            socket.IPPROTO_IPV6, socket.IPV6_JOIN_GROUP, mreq)
        self.recv_socket.setsockopt(
            socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 1)

        # bind to network interface, i.e. scope and handle OS differences,
        # see Stevens: Unix Network Programming, Section 21.6, last paragraph
        try:
            self.recv_socket.bind((WSD_MCAST_GRP_V6, WSD_UDP_PORT, 0, idx))
        except OSError:
            self.recv_socket.bind(('::', 0, 0, idx))

        self.send_socket.setsockopt(
            socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_LOOP, 0)
        self.send_socket.setsockopt(
            socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_HOPS, args.hoplimit)
        self.send_socket.setsockopt(
            socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_IF, idx)

        self.transport_address = '[{0}]'.format(self.address)
        self.listen_address = (self.address, WSD_HTTP_PORT, 0, idx) 
Example #6
Source File: services.py    From maas with GNU Affero General Public License v3.0 5 votes vote down vote up
def set_ipv6_multicast_source_ifindex(sock, ifindex):
    """Sets the given socket up to send multicast from the specified source.

    Ensures the multicast hop limit is set to 1, so that packets are not
    forwarded beyond the local link.

    :param sock: An opened IP socket.
    :param ifindex: An integer representing the interface index.
    """
    sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_HOPS, 1)
    packed_ifindex = struct.pack("I", ifindex)
    sock.setsockopt(
        socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_IF, packed_ifindex
    ) 
Example #7
Source File: test_ip_advertisement.py    From octavia with Apache License 2.0 4 votes vote down vote up
def test_neighbor_advertisement(self, mock_socket, mock_netns, mock_ioctl):
        ALL_NODES_ADDR = 'ff02::1'
        EXPECTED_PACKET_DATA = (b'\x88\x00\x1dk\xa0\x00\x00\x00 \x01\r\xb8\x00'
                                b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x003'
                                b'\x02\x01')
        FAKE_INTERFACE = 'fake0'
        FAKE_MAC = '00005E005333'
        FAKE_NETNS = 'fake_netns'
        ICMPV6_PROTO = socket.getprotobyname(constants.IPV6_ICMP)
        SIOCGIFHWADDR = 0x8927
        SOURCE_IP = '2001:db8::33'

        mock_na_socket = mock.MagicMock()
        mock_socket.return_value = mock_na_socket
        mock_ioctl.return_value = a2b_hex(FAKE_MAC)

        # Test with a network namespace
        ip_advertisement.neighbor_advertisement(FAKE_INTERFACE, SOURCE_IP,
                                                net_ns=FAKE_NETNS)

        mock_netns.assert_called_once_with(FAKE_NETNS)
        mock_socket.assert_called_once_with(socket.AF_INET6, socket.SOCK_RAW,
                                            ICMPV6_PROTO)
        mock_na_socket.setsockopt.assert_called_once_with(
            socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_HOPS, 255)
        mock_na_socket.bind.assert_called_once_with((SOURCE_IP, 0))
        mock_ioctl.assert_called_once_with(
            mock_na_socket.fileno(), SIOCGIFHWADDR,
            pack('256s', bytes(FAKE_INTERFACE, 'utf-8')))
        mock_na_socket.sendto.assert_called_once_with(
            EXPECTED_PACKET_DATA, (ALL_NODES_ADDR, 0, 0, 0))
        mock_na_socket.close.assert_called_once_with()

        # Test without a network namespace
        mock_na_socket.reset_mock()
        mock_netns.reset_mock()
        mock_ioctl.reset_mock()
        mock_socket.reset_mock()

        ip_advertisement.neighbor_advertisement(FAKE_INTERFACE, SOURCE_IP)

        mock_netns.assert_not_called()
        mock_socket.assert_called_once_with(socket.AF_INET6, socket.SOCK_RAW,
                                            ICMPV6_PROTO)
        mock_na_socket.setsockopt.assert_called_once_with(
            socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_HOPS, 255)
        mock_na_socket.bind.assert_called_once_with((SOURCE_IP, 0))
        mock_ioctl.assert_called_once_with(
            mock_na_socket.fileno(), SIOCGIFHWADDR,
            pack('256s', bytes(FAKE_INTERFACE, 'utf-8')))
        mock_na_socket.sendto.assert_called_once_with(
            EXPECTED_PACKET_DATA, (ALL_NODES_ADDR, 0, 0, 0))
        mock_na_socket.close.assert_called_once_with() 
Example #8
Source File: zeroconf.py    From apple_bleee with GNU General Public License v3.0 4 votes vote down vote up
def new_socket(interface_number=None, interface_name=None):
    """
    Create a new socket for a given interface_number. 
    interface_name and interface_number have to be for the same interface 

    """
    #Default is IPv4
    address_family = socket.AF_INET

    intf = None #The interface used to transmit data

    if interface_number is None: 
        #No ipv6 interface given. Stay in ipv4 
        intf = socket.gethostbyname(socket.gethostname())
    else: #IPv6 
        intf = int(interface_number)
        address_family = socket.AF_INET6

    #Create socket 
    s = socket.socket(address_family, socket.SOCK_DGRAM)

    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

    # SO_REUSEADDR should be equivalent to SO_REUSEPORT for
    # multicast UDP sockets (p 731, "TCP/IP Illustrated,
    # Volume 2"), but some BSD-derived systems require
    # SO_REUSEPORT to be specified explicity.  Also, not all
    # versions of Python have SO_REUSEPORT available.
    # Catch OSError and socket.error for kernel versions <3.9 because lacking
    # SO_REUSEPORT support.
    try:
        reuseport = socket.SO_REUSEPORT
    except AttributeError:
        pass
    else:
        try:
            s.setsockopt(socket.SOL_SOCKET, reuseport, 1)
        except (OSError, socket.error) as err:
            # OSError on python 3, socket.error on python 2
            if not err.errno == errno.ENOPROTOOPT:
                raise


    # OpenBSD needs the ttl and loop values for the IP_MULTICAST_TTL and
    # IP_MULTICAST_LOOP socket options as an unsigned char.
    if address_family is socket.AF_INET: 
        ttl = struct.pack(b'B', 255)
        s.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, ttl)
        loop = struct.pack(b'B', 1)
        s.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_LOOP, loop)
    
    else: #IPv6
        s.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_HOPS, 255)
        s.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_LOOP, 1)

    if interface_name is "awdl0" and platform.system() == 'Darwin':
        s.setsockopt(socket.SOL_SOCKET, 0x1104, 1)

    #Bind the Socket
    s.bind(('', _MDNS_PORT))
    return s 
Example #9
Source File: zeroconf.py    From HomePWN with GNU General Public License v3.0 4 votes vote down vote up
def new_socket(interface_number=None, interface_name=None):
    """
    Create a new socket for a given interface_number. 
    interface_name and interface_number have to be for the same interface 

    """
    #Default is IPv4
    address_family = socket.AF_INET

    intf = None #The interface used to transmit data

    if interface_number is None: 
        #No ipv6 interface given. Stay in ipv4 
        intf = socket.gethostbyname(socket.gethostname())
    else: #IPv6 
        intf = int(interface_number)
        address_family = socket.AF_INET6

    #Create socket 
    s = socket.socket(address_family, socket.SOCK_DGRAM)

    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

    # SO_REUSEADDR should be equivalent to SO_REUSEPORT for
    # multicast UDP sockets (p 731, "TCP/IP Illustrated,
    # Volume 2"), but some BSD-derived systems require
    # SO_REUSEPORT to be specified explicity.  Also, not all
    # versions of Python have SO_REUSEPORT available.
    # Catch OSError and socket.error for kernel versions <3.9 because lacking
    # SO_REUSEPORT support.
    try:
        reuseport = socket.SO_REUSEPORT
    except AttributeError:
        pass
    else:
        try:
            s.setsockopt(socket.SOL_SOCKET, reuseport, 1)
        except (OSError, socket.error) as err:
            # OSError on python 3, socket.error on python 2
            if not err.errno == errno.ENOPROTOOPT:
                raise


    # OpenBSD needs the ttl and loop values for the IP_MULTICAST_TTL and
    # IP_MULTICAST_LOOP socket options as an unsigned char.
    if address_family is socket.AF_INET: 
        ttl = struct.pack(b'B', 255)
        s.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, ttl)
        loop = struct.pack(b'B', 1)
        s.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_LOOP, loop)
    
    else: #IPv6
        s.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_HOPS, 255)
        s.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_LOOP, 1)

    if interface_name is "awdl0" and platform.system() == 'Darwin':
        s.setsockopt(socket.SOL_SOCKET, 0x1104, 1)

    #Bind the Socket
    s.bind(('', _MDNS_PORT))
    return s