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