github.com/JimmyHuang454/JLS-go@v0.0.0-20230831150107-90d536585ba0/internal/syscall/unix/net_darwin.go (about)

     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  package unix
     6  
     7  import (
     8  	"internal/abi"
     9  	"syscall"
    10  	"unsafe"
    11  )
    12  
    13  const (
    14  	AI_CANONNAME = 0x2
    15  	AI_ALL       = 0x100
    16  	AI_V4MAPPED  = 0x800
    17  	AI_MASK      = 0x1407
    18  
    19  	EAI_AGAIN    = 2
    20  	EAI_NONAME   = 8
    21  	EAI_SYSTEM   = 11
    22  	EAI_OVERFLOW = 14
    23  
    24  	NI_NAMEREQD = 4
    25  )
    26  
    27  type Addrinfo struct {
    28  	Flags     int32
    29  	Family    int32
    30  	Socktype  int32
    31  	Protocol  int32
    32  	Addrlen   uint32
    33  	Canonname *byte
    34  	Addr      *syscall.RawSockaddr
    35  	Next      *Addrinfo
    36  }
    37  
    38  //go:cgo_ldflag "-lresolv"
    39  
    40  //go:cgo_import_dynamic libc_getaddrinfo getaddrinfo "/usr/lib/libSystem.B.dylib"
    41  func libc_getaddrinfo_trampoline()
    42  
    43  func Getaddrinfo(hostname, servname *byte, hints *Addrinfo, res **Addrinfo) (int, error) {
    44  	gerrno, _, errno := syscall_syscall6(abi.FuncPCABI0(libc_getaddrinfo_trampoline),
    45  		uintptr(unsafe.Pointer(hostname)),
    46  		uintptr(unsafe.Pointer(servname)),
    47  		uintptr(unsafe.Pointer(hints)),
    48  		uintptr(unsafe.Pointer(res)),
    49  		0,
    50  		0)
    51  	var err error
    52  	if errno != 0 {
    53  		err = errno
    54  	}
    55  	return int(gerrno), err
    56  }
    57  
    58  //go:cgo_import_dynamic libc_freeaddrinfo freeaddrinfo "/usr/lib/libSystem.B.dylib"
    59  func libc_freeaddrinfo_trampoline()
    60  
    61  func Freeaddrinfo(ai *Addrinfo) {
    62  	syscall_syscall6(abi.FuncPCABI0(libc_freeaddrinfo_trampoline),
    63  		uintptr(unsafe.Pointer(ai)),
    64  		0, 0, 0, 0, 0)
    65  }
    66  
    67  //go:cgo_import_dynamic libc_getnameinfo getnameinfo "/usr/lib/libSystem.B.dylib"
    68  func libc_getnameinfo_trampoline()
    69  
    70  func Getnameinfo(sa *syscall.RawSockaddr, salen int, host *byte, hostlen int, serv *byte, servlen int, flags int) (int, error) {
    71  	gerrno, _, errno := syscall_syscall9(abi.FuncPCABI0(libc_getnameinfo_trampoline),
    72  		uintptr(unsafe.Pointer(sa)),
    73  		uintptr(salen),
    74  		uintptr(unsafe.Pointer(host)),
    75  		uintptr(hostlen),
    76  		uintptr(unsafe.Pointer(serv)),
    77  		uintptr(servlen),
    78  		uintptr(flags),
    79  		0,
    80  		0)
    81  	var err error
    82  	if errno != 0 {
    83  		err = errno
    84  	}
    85  	return int(gerrno), err
    86  }
    87  
    88  //go:cgo_import_dynamic libc_gai_strerror gai_strerror "/usr/lib/libSystem.B.dylib"
    89  func libc_gai_strerror_trampoline()
    90  
    91  func GaiStrerror(ecode int) string {
    92  	r1, _, _ := syscall_syscall(abi.FuncPCABI0(libc_gai_strerror_trampoline),
    93  		uintptr(ecode),
    94  		0, 0)
    95  	return GoString((*byte)(unsafe.Pointer(r1)))
    96  }
    97  
    98  // Implemented in the runtime package.
    99  func gostring(*byte) string
   100  
   101  func GoString(p *byte) string {
   102  	return gostring(p)
   103  }
   104  
   105  //go:linkname syscall_syscall syscall.syscall
   106  func syscall_syscall(fn, a1, a2, a3 uintptr) (r1, r2 uintptr, err syscall.Errno)
   107  
   108  //go:linkname syscall_syscallPtr syscall.syscallPtr
   109  func syscall_syscallPtr(fn, a1, a2, a3 uintptr) (r1, r2 uintptr, err syscall.Errno)
   110  
   111  //go:linkname syscall_syscall6 syscall.syscall6
   112  func syscall_syscall6(fn, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err syscall.Errno)
   113  
   114  //go:linkname syscall_syscall6X syscall.syscall6X
   115  func syscall_syscall6X(fn, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err syscall.Errno)
   116  
   117  //go:linkname syscall_syscall9 syscall.syscall9
   118  func syscall_syscall9(fn, a1, a2, a3, a4, a5, a6, a7, a8, a9 uintptr) (r1, r2 uintptr, err syscall.Errno)
   119  
   120  type ResState struct {
   121  	unexported [70]uintptr
   122  }
   123  
   124  //go:cgo_import_dynamic libresolv_res_9_ninit res_9_ninit "/usr/lib/libresolv.9.dylib"
   125  func libresolv_res_9_ninit_trampoline()
   126  
   127  func ResNinit(state *ResState) error {
   128  	_, _, errno := syscall_syscall(abi.FuncPCABI0(libresolv_res_9_ninit_trampoline),
   129  		uintptr(unsafe.Pointer(state)),
   130  		0, 0)
   131  	if errno != 0 {
   132  		return errno
   133  	}
   134  	return nil
   135  }
   136  
   137  //go:cgo_import_dynamic libresolv_res_9_nclose res_9_nclose "/usr/lib/libresolv.9.dylib"
   138  func libresolv_res_9_nclose_trampoline()
   139  
   140  func ResNclose(state *ResState) {
   141  	syscall_syscall(abi.FuncPCABI0(libresolv_res_9_nclose_trampoline),
   142  		uintptr(unsafe.Pointer(state)),
   143  		0, 0)
   144  }
   145  
   146  //go:cgo_import_dynamic libresolv_res_9_nsearch res_9_nsearch "/usr/lib/libresolv.9.dylib"
   147  func libresolv_res_9_nsearch_trampoline()
   148  
   149  func ResNsearch(state *ResState, dname *byte, class, typ int, ans *byte, anslen int) (int, error) {
   150  	r1, _, errno := syscall_syscall6(abi.FuncPCABI0(libresolv_res_9_nsearch_trampoline),
   151  		uintptr(unsafe.Pointer(state)),
   152  		uintptr(unsafe.Pointer(dname)),
   153  		uintptr(class),
   154  		uintptr(typ),
   155  		uintptr(unsafe.Pointer(ans)),
   156  		uintptr(anslen))
   157  	if errno != 0 {
   158  		return 0, errno
   159  	}
   160  	return int(int32(r1)), nil
   161  }