github.com/Andyfoo/golang/x/net@v0.0.0-20190901054642-57c1bf301704/ipv4/sys_freebsd.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  package ipv4
     6  
     7  import (
     8  	"net"
     9  	"runtime"
    10  	"strings"
    11  	"syscall"
    12  	"unsafe"
    13  
    14  	"github.com/Andyfoo/golang/x/net/internal/iana"
    15  	"github.com/Andyfoo/golang/x/net/internal/socket"
    16  )
    17  
    18  var (
    19  	ctlOpts = [ctlMax]ctlOpt{
    20  		ctlTTL:       {sysIP_RECVTTL, 1, marshalTTL, parseTTL},
    21  		ctlDst:       {sysIP_RECVDSTADDR, net.IPv4len, marshalDst, parseDst},
    22  		ctlInterface: {sysIP_RECVIF, syscall.SizeofSockaddrDatalink, marshalInterface, parseInterface},
    23  	}
    24  
    25  	sockOpts = map[int]*sockOpt{
    26  		ssoTOS:                {Option: socket.Option{Level: iana.ProtocolIP, Name: sysIP_TOS, Len: 4}},
    27  		ssoTTL:                {Option: socket.Option{Level: iana.ProtocolIP, Name: sysIP_TTL, Len: 4}},
    28  		ssoMulticastTTL:       {Option: socket.Option{Level: iana.ProtocolIP, Name: sysIP_MULTICAST_TTL, Len: 1}},
    29  		ssoMulticastInterface: {Option: socket.Option{Level: iana.ProtocolIP, Name: sysIP_MULTICAST_IF, Len: 4}},
    30  		ssoMulticastLoopback:  {Option: socket.Option{Level: iana.ProtocolIP, Name: sysIP_MULTICAST_LOOP, Len: 4}},
    31  		ssoReceiveTTL:         {Option: socket.Option{Level: iana.ProtocolIP, Name: sysIP_RECVTTL, Len: 4}},
    32  		ssoReceiveDst:         {Option: socket.Option{Level: iana.ProtocolIP, Name: sysIP_RECVDSTADDR, Len: 4}},
    33  		ssoReceiveInterface:   {Option: socket.Option{Level: iana.ProtocolIP, Name: sysIP_RECVIF, Len: 4}},
    34  		ssoHeaderPrepend:      {Option: socket.Option{Level: iana.ProtocolIP, Name: sysIP_HDRINCL, Len: 4}},
    35  		ssoJoinGroup:          {Option: socket.Option{Level: iana.ProtocolIP, Name: sysMCAST_JOIN_GROUP, Len: sizeofGroupReq}, typ: ssoTypeGroupReq},
    36  		ssoLeaveGroup:         {Option: socket.Option{Level: iana.ProtocolIP, Name: sysMCAST_LEAVE_GROUP, Len: sizeofGroupReq}, typ: ssoTypeGroupReq},
    37  		ssoJoinSourceGroup:    {Option: socket.Option{Level: iana.ProtocolIP, Name: sysMCAST_JOIN_SOURCE_GROUP, Len: sizeofGroupSourceReq}, typ: ssoTypeGroupSourceReq},
    38  		ssoLeaveSourceGroup:   {Option: socket.Option{Level: iana.ProtocolIP, Name: sysMCAST_LEAVE_SOURCE_GROUP, Len: sizeofGroupSourceReq}, typ: ssoTypeGroupSourceReq},
    39  		ssoBlockSourceGroup:   {Option: socket.Option{Level: iana.ProtocolIP, Name: sysMCAST_BLOCK_SOURCE, Len: sizeofGroupSourceReq}, typ: ssoTypeGroupSourceReq},
    40  		ssoUnblockSourceGroup: {Option: socket.Option{Level: iana.ProtocolIP, Name: sysMCAST_UNBLOCK_SOURCE, Len: sizeofGroupSourceReq}, typ: ssoTypeGroupSourceReq},
    41  	}
    42  )
    43  
    44  func init() {
    45  	freebsdVersion, _ = syscall.SysctlUint32("kern.osreldate")
    46  	if freebsdVersion >= 1000000 {
    47  		sockOpts[ssoMulticastInterface] = &sockOpt{Option: socket.Option{Level: iana.ProtocolIP, Name: sysIP_MULTICAST_IF, Len: sizeofIPMreqn}, typ: ssoTypeIPMreqn}
    48  	}
    49  	if runtime.GOOS == "freebsd" && runtime.GOARCH == "386" {
    50  		archs, _ := syscall.Sysctl("kern.supported_archs")
    51  		for _, s := range strings.Fields(archs) {
    52  			if s == "amd64" {
    53  				compatFreeBSD32 = true
    54  				break
    55  			}
    56  		}
    57  	}
    58  }
    59  
    60  func (gr *groupReq) setGroup(grp net.IP) {
    61  	sa := (*sockaddrInet)(unsafe.Pointer(&gr.Group))
    62  	sa.Len = sizeofSockaddrInet
    63  	sa.Family = syscall.AF_INET
    64  	copy(sa.Addr[:], grp)
    65  }
    66  
    67  func (gsr *groupSourceReq) setSourceGroup(grp, src net.IP) {
    68  	sa := (*sockaddrInet)(unsafe.Pointer(&gsr.Group))
    69  	sa.Len = sizeofSockaddrInet
    70  	sa.Family = syscall.AF_INET
    71  	copy(sa.Addr[:], grp)
    72  	sa = (*sockaddrInet)(unsafe.Pointer(&gsr.Source))
    73  	sa.Len = sizeofSockaddrInet
    74  	sa.Family = syscall.AF_INET
    75  	copy(sa.Addr[:], src)
    76  }