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

View as plain text