Source file src/net/cgo_unix_syscall.go

     1  // Copyright 2022 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 !netgo && darwin
     6  
     7  package net
     8  
     9  import (
    10  	"internal/syscall/unix"
    11  	"runtime"
    12  	"syscall"
    13  	"unsafe"
    14  )
    15  
    16  const (
    17  	_C_AF_INET      = syscall.AF_INET
    18  	_C_AF_INET6     = syscall.AF_INET6
    19  	_C_AF_UNSPEC    = syscall.AF_UNSPEC
    20  	_C_EAI_AGAIN    = unix.EAI_AGAIN
    21  	_C_EAI_NONAME   = unix.EAI_NONAME
    22  	_C_EAI_SERVICE  = unix.EAI_SERVICE
    23  	_C_EAI_NODATA   = unix.EAI_NODATA
    24  	_C_EAI_OVERFLOW = unix.EAI_OVERFLOW
    25  	_C_EAI_SYSTEM   = unix.EAI_SYSTEM
    26  	_C_IPPROTO_TCP  = syscall.IPPROTO_TCP
    27  	_C_IPPROTO_UDP  = syscall.IPPROTO_UDP
    28  	_C_SOCK_DGRAM   = syscall.SOCK_DGRAM
    29  	_C_SOCK_STREAM  = syscall.SOCK_STREAM
    30  )
    31  
    32  type (
    33  	_C_char               = byte
    34  	_C_int                = int32
    35  	_C_uchar              = byte
    36  	_C_uint               = uint32
    37  	_C_socklen_t          = int
    38  	_C_struct___res_state = unix.ResState
    39  	_C_struct_addrinfo    = unix.Addrinfo
    40  	_C_struct_sockaddr    = syscall.RawSockaddr
    41  )
    42  
    43  func _C_free(p unsafe.Pointer) { runtime.KeepAlive(p) }
    44  
    45  func _C_malloc(n uintptr) unsafe.Pointer {
    46  	if n <= 0 {
    47  		n = 1
    48  	}
    49  	return unsafe.Pointer(&make([]byte, n)[0])
    50  }
    51  
    52  func _C_ai_addr(ai *_C_struct_addrinfo) **_C_struct_sockaddr { return &ai.Addr }
    53  func _C_ai_family(ai *_C_struct_addrinfo) *_C_int            { return &ai.Family }
    54  func _C_ai_flags(ai *_C_struct_addrinfo) *_C_int             { return &ai.Flags }
    55  func _C_ai_next(ai *_C_struct_addrinfo) **_C_struct_addrinfo { return &ai.Next }
    56  func _C_ai_protocol(ai *_C_struct_addrinfo) *_C_int          { return &ai.Protocol }
    57  func _C_ai_socktype(ai *_C_struct_addrinfo) *_C_int          { return &ai.Socktype }
    58  
    59  func _C_freeaddrinfo(ai *_C_struct_addrinfo) {
    60  	unix.Freeaddrinfo(ai)
    61  }
    62  
    63  func _C_gai_strerror(eai _C_int) string {
    64  	return unix.GaiStrerror(int(eai))
    65  }
    66  
    67  func _C_getaddrinfo(hostname, servname *byte, hints *_C_struct_addrinfo, res **_C_struct_addrinfo) (int, error) {
    68  	return unix.Getaddrinfo(hostname, servname, hints, res)
    69  }
    70  
    71  func _C_res_ninit(state *_C_struct___res_state) error {
    72  	unix.ResNinit(state)
    73  	return nil
    74  }
    75  
    76  func _C_res_nsearch(state *_C_struct___res_state, dname *_C_char, class, typ int, ans *_C_char, anslen int) int {
    77  	x, _ := unix.ResNsearch(state, dname, class, typ, ans, anslen)
    78  	return x
    79  }
    80  
    81  func _C_res_nclose(state *_C_struct___res_state) {
    82  	unix.ResNclose(state)
    83  }
    84  
    85  func cgoNameinfoPTR(b []byte, sa *syscall.RawSockaddr, salen int) (int, error) {
    86  	gerrno, err := unix.Getnameinfo(sa, salen, &b[0], len(b), nil, 0, unix.NI_NAMEREQD)
    87  	return int(gerrno), err
    88  }
    89  
    90  func cgoSockaddrInet4(ip IP) *syscall.RawSockaddr {
    91  	sa := syscall.RawSockaddrInet4{Len: syscall.SizeofSockaddrInet4, Family: syscall.AF_INET}
    92  	copy(sa.Addr[:], ip)
    93  	return (*syscall.RawSockaddr)(unsafe.Pointer(&sa))
    94  }
    95  
    96  func cgoSockaddrInet6(ip IP, zone int) *syscall.RawSockaddr {
    97  	sa := syscall.RawSockaddrInet6{Len: syscall.SizeofSockaddrInet6, Family: syscall.AF_INET6, Scope_id: uint32(zone)}
    98  	copy(sa.Addr[:], ip)
    99  	return (*syscall.RawSockaddr)(unsafe.Pointer(&sa))
   100  }
   101  

View as plain text