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