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