Source file src/net/cgo_unix_cgo.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 cgo && !netgo && unix && !darwin
     6  
     7  package net
     8  
     9  /*
    10  #define _GNU_SOURCE 1
    11  
    12  #cgo CFLAGS: -fno-stack-protector
    13  #include <sys/types.h>
    14  #include <sys/socket.h>
    15  #include <netinet/in.h>
    16  #include <netdb.h>
    17  #include <unistd.h>
    18  #include <string.h>
    19  #include <stdlib.h>
    20  
    21  #ifndef EAI_NODATA
    22  #define EAI_NODATA -5
    23  #endif
    24  
    25  // If nothing else defined EAI_ADDRFAMILY, make sure it has a value.
    26  #ifndef EAI_ADDRFAMILY
    27  #define EAI_ADDRFAMILY -9
    28  #endif
    29  
    30  // If nothing else defined EAI_OVERFLOW, make sure it has a value.
    31  #ifndef EAI_OVERFLOW
    32  #define EAI_OVERFLOW -12
    33  #endif
    34  */
    35  import "C"
    36  import "unsafe"
    37  
    38  const (
    39  	_C_AF_INET        = C.AF_INET
    40  	_C_AF_INET6       = C.AF_INET6
    41  	_C_AF_UNSPEC      = C.AF_UNSPEC
    42  	_C_EAI_ADDRFAMILY = C.EAI_ADDRFAMILY
    43  	_C_EAI_AGAIN      = C.EAI_AGAIN
    44  	_C_EAI_NODATA     = C.EAI_NODATA
    45  	_C_EAI_NONAME     = C.EAI_NONAME
    46  	_C_EAI_SERVICE    = C.EAI_SERVICE
    47  	_C_EAI_OVERFLOW   = C.EAI_OVERFLOW
    48  	_C_EAI_SYSTEM     = C.EAI_SYSTEM
    49  	_C_IPPROTO_TCP    = C.IPPROTO_TCP
    50  	_C_IPPROTO_UDP    = C.IPPROTO_UDP
    51  	_C_SOCK_DGRAM     = C.SOCK_DGRAM
    52  	_C_SOCK_STREAM    = C.SOCK_STREAM
    53  )
    54  
    55  type (
    56  	_C_char            = C.char
    57  	_C_uchar           = C.uchar
    58  	_C_int             = C.int
    59  	_C_uint            = C.uint
    60  	_C_socklen_t       = C.socklen_t
    61  	_C_struct_addrinfo = C.struct_addrinfo
    62  	_C_struct_sockaddr = C.struct_sockaddr
    63  )
    64  
    65  func _C_malloc(n uintptr) unsafe.Pointer { return C.malloc(C.size_t(n)) }
    66  func _C_free(p unsafe.Pointer)           { C.free(p) }
    67  
    68  func _C_ai_addr(ai *_C_struct_addrinfo) **_C_struct_sockaddr { return &ai.ai_addr }
    69  func _C_ai_family(ai *_C_struct_addrinfo) *_C_int            { return &ai.ai_family }
    70  func _C_ai_flags(ai *_C_struct_addrinfo) *_C_int             { return &ai.ai_flags }
    71  func _C_ai_next(ai *_C_struct_addrinfo) **_C_struct_addrinfo { return &ai.ai_next }
    72  func _C_ai_protocol(ai *_C_struct_addrinfo) *_C_int          { return &ai.ai_protocol }
    73  func _C_ai_socktype(ai *_C_struct_addrinfo) *_C_int          { return &ai.ai_socktype }
    74  
    75  func _C_freeaddrinfo(ai *_C_struct_addrinfo) {
    76  	C.freeaddrinfo(ai)
    77  }
    78  
    79  func _C_gai_strerror(eai _C_int) string {
    80  	return C.GoString(C.gai_strerror(eai))
    81  }
    82  
    83  func _C_getaddrinfo(hostname, servname *_C_char, hints *_C_struct_addrinfo, res **_C_struct_addrinfo) (int, error) {
    84  	x, err := C.getaddrinfo(hostname, servname, hints, res)
    85  	return int(x), err
    86  }
    87  

View as plain text