golang.org/x/net@v0.25.1-0.20240516223405-c87a5b62e243/ipv4/sys_asmreqn.go (about)

     1  // Copyright 2014 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 darwin || freebsd || linux
     6  
     7  package ipv4
     8  
     9  import (
    10  	"net"
    11  	"unsafe"
    12  
    13  	"golang.org/x/net/internal/socket"
    14  
    15  	"golang.org/x/sys/unix"
    16  )
    17  
    18  func (so *sockOpt) getIPMreqn(c *socket.Conn) (*net.Interface, error) {
    19  	b := make([]byte, so.Len)
    20  	if _, err := so.Get(c, b); err != nil {
    21  		return nil, err
    22  	}
    23  	mreqn := (*unix.IPMreqn)(unsafe.Pointer(&b[0]))
    24  	if mreqn.Ifindex == 0 {
    25  		return nil, nil
    26  	}
    27  	ifi, err := net.InterfaceByIndex(int(mreqn.Ifindex))
    28  	if err != nil {
    29  		return nil, err
    30  	}
    31  	return ifi, nil
    32  }
    33  
    34  func (so *sockOpt) setIPMreqn(c *socket.Conn, ifi *net.Interface, grp net.IP) error {
    35  	var mreqn unix.IPMreqn
    36  	if ifi != nil {
    37  		mreqn.Ifindex = int32(ifi.Index)
    38  	}
    39  	if grp != nil {
    40  		mreqn.Multiaddr = [4]byte{grp[0], grp[1], grp[2], grp[3]}
    41  	}
    42  	b := (*[unix.SizeofIPMreqn]byte)(unsafe.Pointer(&mreqn))[:unix.SizeofIPMreqn]
    43  	return so.Set(c, b)
    44  }