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_OVERFLOW, make sure it has a value.
    26  #ifndef EAI_OVERFLOW
    27  #define EAI_OVERFLOW -12
    28  #endif
    29  */
    30  import "C"
    31  import "unsafe"
    32  
    33  const (
    34  	_C_AF_INET      = C.AF_INET
    35  	_C_AF_INET6     = C.AF_INET6
    36  	_C_AF_UNSPEC    = C.AF_UNSPEC
    37  	_C_EAI_AGAIN    = C.EAI_AGAIN
    38  	_C_EAI_NODATA   = C.EAI_NODATA
    39  	_C_EAI_NONAME   = C.EAI_NONAME
    40  	_C_EAI_SERVICE  = C.EAI_SERVICE
    41  	_C_EAI_OVERFLOW = C.EAI_OVERFLOW
    42  	_C_EAI_SYSTEM   = C.EAI_SYSTEM
    43  	_C_IPPROTO_TCP  = C.IPPROTO_TCP
    44  	_C_IPPROTO_UDP  = C.IPPROTO_UDP
    45  	_C_SOCK_DGRAM   = C.SOCK_DGRAM
    46  	_C_SOCK_STREAM  = C.SOCK_STREAM
    47  )
    48  
    49  type (
    50  	_C_char            = C.char
    51  	_C_uchar           = C.uchar
    52  	_C_int             = C.int
    53  	_C_uint            = C.uint
    54  	_C_socklen_t       = C.socklen_t
    55  	_C_struct_addrinfo = C.struct_addrinfo
    56  	_C_struct_sockaddr = C.struct_sockaddr
    57  )
    58  
    59  func _C_malloc(n uintptr) unsafe.Pointer { return C.malloc(C.size_t(n)) }
    60  func _C_free(p unsafe.Pointer)           { C.free(p) }
    61  
    62  func _C_ai_addr(ai *_C_struct_addrinfo) **_C_struct_sockaddr { return &ai.ai_addr }
    63  func _C_ai_family(ai *_C_struct_addrinfo) *_C_int            { return &ai.ai_family }
    64  func _C_ai_flags(ai *_C_struct_addrinfo) *_C_int             { return &ai.ai_flags }
    65  func _C_ai_next(ai *_C_struct_addrinfo) **_C_struct_addrinfo { return &ai.ai_next }
    66  func _C_ai_protocol(ai *_C_struct_addrinfo) *_C_int          { return &ai.ai_protocol }
    67  func _C_ai_socktype(ai *_C_struct_addrinfo) *_C_int          { return &ai.ai_socktype }
    68  
    69  func _C_freeaddrinfo(ai *_C_struct_addrinfo) {
    70  	C.freeaddrinfo(ai)
    71  }
    72  
    73  func _C_gai_strerror(eai _C_int) string {
    74  	return C.GoString(C.gai_strerror(eai))
    75  }
    76  
    77  func _C_getaddrinfo(hostname, servname *_C_char, hints *_C_struct_addrinfo, res **_C_struct_addrinfo) (int, error) {
    78  	x, err := C.getaddrinfo(hostname, servname, hints, res)
    79  	return int(x), err
    80  }
    81  

View as plain text