Source file src/net/sockoptip_posix.go

     1  // Copyright 2011 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build unix || windows
     6  
     7  package net
     8  
     9  import (
    10  	"runtime"
    11  	"syscall"
    12  )
    13  
    14  func joinIPv4Group(fd *netFD, ifi *Interface, ip IP) error {
    15  	mreq := &syscall.IPMreq{Multiaddr: [4]byte{ip[0], ip[1], ip[2], ip[3]}}
    16  	if err := setIPv4MreqToInterface(mreq, ifi); err != nil {
    17  		return err
    18  	}
    19  	err := fd.pfd.SetsockoptIPMreq(syscall.IPPROTO_IP, syscall.IP_ADD_MEMBERSHIP, mreq)
    20  	runtime.KeepAlive(fd)
    21  	return wrapSyscallError("setsockopt", err)
    22  }
    23  
    24  func setIPv6MulticastInterface(fd *netFD, ifi *Interface) error {
    25  	var v int
    26  	if ifi != nil {
    27  		v = ifi.Index
    28  	}
    29  	err := fd.pfd.SetsockoptInt(syscall.IPPROTO_IPV6, syscall.IPV6_MULTICAST_IF, v)
    30  	runtime.KeepAlive(fd)
    31  	return wrapSyscallError("setsockopt", err)
    32  }
    33  
    34  func setIPv6MulticastLoopback(fd *netFD, v bool) error {
    35  	err := fd.pfd.SetsockoptInt(syscall.IPPROTO_IPV6, syscall.IPV6_MULTICAST_LOOP, boolint(v))
    36  	runtime.KeepAlive(fd)
    37  	return wrapSyscallError("setsockopt", err)
    38  }
    39  
    40  func joinIPv6Group(fd *netFD, ifi *Interface, ip IP) error {
    41  	mreq := &syscall.IPv6Mreq{}
    42  	copy(mreq.Multiaddr[:], ip)
    43  	if ifi != nil {
    44  		mreq.Interface = uint32(ifi.Index)
    45  	}
    46  	err := fd.pfd.SetsockoptIPv6Mreq(syscall.IPPROTO_IPV6, syscall.IPV6_JOIN_GROUP, mreq)
    47  	runtime.KeepAlive(fd)
    48  	return wrapSyscallError("setsockopt", err)
    49  }
    50  

View as plain text