github.com/gotranspile/cxgo@v0.3.7/libs/netdb.go (about)

     1  package libs
     2  
     3  import (
     4  	"github.com/gotranspile/cxgo/runtime/cnet"
     5  	"github.com/gotranspile/cxgo/types"
     6  )
     7  
     8  const (
     9  	netdbH = "netdb.h"
    10  )
    11  
    12  func init() {
    13  	RegisterLibrary(netdbH, func(c *Env) *Library {
    14  		strT := c.C().String()
    15  		intT := types.IntT(4)
    16  		hostentT := types.NamedTGo("hostent", "cnet.HostEnt", types.StructT([]*types.Field{
    17  			{Name: types.NewIdentGo("h_name", "Name", strT)},
    18  			{Name: types.NewIdentGo("h_aliases", "Aliases", c.PtrT(strT))},
    19  			{Name: types.NewIdentGo("h_addrtype", "AddrType", intT)},
    20  			{Name: types.NewIdentGo("h_length", "Length", intT)},
    21  			{Name: types.NewIdentGo("h_addr_list", "AddrList", c.PtrT(strT))},
    22  		}))
    23  		return &Library{
    24  			Imports: map[string]string{
    25  				"cnet": RuntimePrefix + "cnet",
    26  			},
    27  			Types: map[string]types.Type{
    28  				"hostent": hostentT,
    29  			},
    30  			Idents: map[string]*types.Ident{
    31  				"gethostbyname": c.NewIdent("gethostbyname", "cnet.GetHostByName", cnet.GetHostByName, c.FuncTT(c.PtrT(hostentT), strT)),
    32  			},
    33  			// TODO
    34  			Header: `
    35  #include <` + stdintH + `>
    36  
    37  struct hostent {
    38  	char  *h_name;      // Official name of the host. 
    39  	char **h_aliases;   // A pointer to an array of pointers to alternative host names, terminated by a null pointer. 
    40  	int32_t h_addrtype;  // Address type. 
    41  	int32_t h_length;    // The length, in bytes, of the address. 
    42  	char **h_addr_list; // A pointer to an array of pointers to network addresses (in network byte order) for the host, terminated by a null pointer.
    43  };
    44  
    45  struct netent {
    46  	char     *n_name;     // Official, fully-qualified (including the domain) name of the host. 
    47  	char    **n_aliases;  // A pointer to an array of pointers to alternative network names, terminated by a null pointer. 
    48  	int       n_addrtype; // The address type of the network. 
    49  	uint32_t  n_net;      // The network number, in host byte order.
    50  };
    51  
    52  struct protoent {
    53  	char   *p_name;     // Official name of the protocol. 
    54  	char  **p_aliases;  // A pointer to an array of pointers to alternative protocol names, terminated by a null pointer. 
    55  	int     p_proto;    // The protocol number.
    56  };
    57  
    58  struct servent {
    59  	char   *s_name;     // Official name of the service. 
    60  	char  **s_aliases;  // A pointer to an array of pointers to alternative service names, terminated by a null pointer. 
    61  	int     s_port;     // The port number at which the service resides, in network byte order. 
    62  	char   *s_proto;    // The name of the protocol to use when contacting the service.
    63  };
    64  
    65  void              endhostent(void);
    66  void              endnetent(void);
    67  void              endprotoent(void);
    68  void              endservent(void);
    69  void              freeaddrinfo(struct addrinfo *);
    70  const char       *gai_strerror(int);
    71  int               getaddrinfo(const char *restrict, const char *restrict,
    72                        const struct addrinfo *restrict,
    73                        struct addrinfo **restrict);
    74  struct hostent   *gethostbyaddr(const void *, socklen_t, int);
    75  struct hostent   *gethostbyname(const char *);
    76  struct hostent   *gethostent(void);
    77  int               getnameinfo(const struct sockaddr *restrict, socklen_t,
    78                        char *restrict, socklen_t, char *restrict,
    79                        socklen_t, int);
    80  struct netent    *getnetbyaddr(uint32_t, int);
    81  struct netent    *getnetbyname(const char *);
    82  struct netent    *getnetent(void);
    83  struct protoent  *getprotobyname(const char *);
    84  struct protoent  *getprotobynumber(int);
    85  struct protoent  *getprotoent(void);
    86  struct servent   *getservbyname(const char *, const char *);
    87  struct servent   *getservbyport(int, const char *);
    88  struct servent   *getservent(void);
    89  void              sethostent(int);
    90  void              setnetent(int);
    91  void              setprotoent(int);
    92  void              setservent(int);
    93  `,
    94  		}
    95  	})
    96  }