github.com/Andyfoo/golang/x/net@v0.0.0-20190901054642-57c1bf301704/ipv6/sockopt_posix.go (about)

     1  // Copyright 2013 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 aix darwin dragonfly freebsd linux netbsd openbsd solaris windows
     6  
     7  package ipv6
     8  
     9  import (
    10  	"net"
    11  	"runtime"
    12  	"unsafe"
    13  
    14  	"github.com/Andyfoo/golang/x/net/bpf"
    15  	"github.com/Andyfoo/golang/x/net/internal/socket"
    16  )
    17  
    18  func (so *sockOpt) getMulticastInterface(c *socket.Conn) (*net.Interface, error) {
    19  	n, err := so.GetInt(c)
    20  	if err != nil {
    21  		return nil, err
    22  	}
    23  	return net.InterfaceByIndex(n)
    24  }
    25  
    26  func (so *sockOpt) setMulticastInterface(c *socket.Conn, ifi *net.Interface) error {
    27  	var n int
    28  	if ifi != nil {
    29  		n = ifi.Index
    30  	}
    31  	return so.SetInt(c, n)
    32  }
    33  
    34  func (so *sockOpt) getICMPFilter(c *socket.Conn) (*ICMPFilter, error) {
    35  	b := make([]byte, so.Len)
    36  	n, err := so.Get(c, b)
    37  	if err != nil {
    38  		return nil, err
    39  	}
    40  	if n != sizeofICMPv6Filter {
    41  		return nil, errNotImplemented
    42  	}
    43  	return (*ICMPFilter)(unsafe.Pointer(&b[0])), nil
    44  }
    45  
    46  func (so *sockOpt) setICMPFilter(c *socket.Conn, f *ICMPFilter) error {
    47  	b := (*[sizeofICMPv6Filter]byte)(unsafe.Pointer(f))[:sizeofICMPv6Filter]
    48  	return so.Set(c, b)
    49  }
    50  
    51  func (so *sockOpt) getMTUInfo(c *socket.Conn) (*net.Interface, int, error) {
    52  	b := make([]byte, so.Len)
    53  	n, err := so.Get(c, b)
    54  	if err != nil {
    55  		return nil, 0, err
    56  	}
    57  	if n != sizeofIPv6Mtuinfo {
    58  		return nil, 0, errNotImplemented
    59  	}
    60  	mi := (*ipv6Mtuinfo)(unsafe.Pointer(&b[0]))
    61  	if mi.Addr.Scope_id == 0 || runtime.GOOS == "aix" {
    62  		// AIX kernel might return a wrong address.
    63  		return nil, int(mi.Mtu), nil
    64  	}
    65  	ifi, err := net.InterfaceByIndex(int(mi.Addr.Scope_id))
    66  	if err != nil {
    67  		return nil, 0, err
    68  	}
    69  	return ifi, int(mi.Mtu), nil
    70  }
    71  
    72  func (so *sockOpt) setGroup(c *socket.Conn, ifi *net.Interface, grp net.IP) error {
    73  	switch so.typ {
    74  	case ssoTypeIPMreq:
    75  		return so.setIPMreq(c, ifi, grp)
    76  	case ssoTypeGroupReq:
    77  		return so.setGroupReq(c, ifi, grp)
    78  	default:
    79  		return errNotImplemented
    80  	}
    81  }
    82  
    83  func (so *sockOpt) setSourceGroup(c *socket.Conn, ifi *net.Interface, grp, src net.IP) error {
    84  	return so.setGroupSourceReq(c, ifi, grp, src)
    85  }
    86  
    87  func (so *sockOpt) setBPF(c *socket.Conn, f []bpf.RawInstruction) error {
    88  	return so.setAttachFilter(c, f)
    89  }