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

     1  package libs
     2  
     3  import (
     4  	"github.com/gotranspile/cxgo/runtime/cnet"
     5  	"github.com/gotranspile/cxgo/types"
     6  )
     7  
     8  // https://pubs.opengroup.org/onlinepubs/000095399/basedefs/arpa/inet.h.html
     9  
    10  const (
    11  	arpaInetH = "arpa/inet.h"
    12  )
    13  
    14  func init() {
    15  	RegisterLibrary(arpaInetH, func(c *Env) *Library {
    16  		inAddrT := types.NamedTGo("in_addr_t", "cnet.Addr", types.UintT(8))
    17  		inPortT := types.NamedTGo("in_port_t", "cnet.Port", types.UintT(4))
    18  		inAddr := types.NamedTGo("in_addr", "cnet.Address", types.StructT([]*types.Field{
    19  			{Name: types.NewIdentGo("s_addr", "Addr", types.UintT(4))},
    20  		}))
    21  		sockLenT := types.UintT(4)
    22  		strT := c.C().String()
    23  		gstrT := c.Go().String()
    24  		return &Library{
    25  			Imports: map[string]string{
    26  				"cnet": RuntimePrefix + "cnet",
    27  			},
    28  			Types: map[string]types.Type{
    29  				"in_addr":   inAddr,
    30  				"in_addr_t": inAddrT,
    31  				"in_port_t": inPortT,
    32  			},
    33  			Idents: map[string]*types.Ident{
    34  				"inet_addr": c.NewIdent("inet_addr", "cnet.ParseAddr", cnet.ParseAddr, c.FuncTT(inAddrT, gstrT)),
    35  				"htonl":     c.NewIdent("htonl", "cnet.Htonl", cnet.Htonl, c.FuncTT(types.UintT(4), types.UintT(4))),
    36  				"htons":     c.NewIdent("htons", "cnet.Htons", cnet.Htons, c.FuncTT(types.UintT(2), types.UintT(2))),
    37  				"ntohl":     c.NewIdent("ntohl", "cnet.Ntohl", cnet.Ntohl, c.FuncTT(types.UintT(4), types.UintT(4))),
    38  				"ntohs":     c.NewIdent("ntohs", "cnet.Ntohs", cnet.Ntohs, c.FuncTT(types.UintT(2), types.UintT(2))),
    39  				"inet_ntoa": c.NewIdent("inet_ntoa", "cnet.Ntoa", cnet.Ntoa, c.FuncTT(strT, inAddr)),
    40  				"inet_ntop": c.NewIdent("inet_ntop", "cnet.Ntop", cnet.Ntop, c.FuncTT(strT, types.IntT(4), c.PtrT(nil), strT, sockLenT)),
    41  				"inet_pton": c.NewIdent("inet_pton", "cnet.Pton", cnet.Pton, c.FuncTT(strT, types.IntT(4), strT, c.PtrT(nil))),
    42  			},
    43  			// TODO: split the file as per the spec
    44  			Header: `
    45  #include <` + stdintH + `>
    46  
    47  uint32_t htonl(uint32_t);
    48  uint16_t htons(uint16_t);
    49  uint32_t ntohl(uint32_t);
    50  uint16_t ntohs(uint16_t);
    51  
    52  struct in_addr {
    53      uint32_t s_addr;
    54  };
    55  
    56  typedef uint64_t in_addr_t;
    57  typedef uint32_t in_port_t;
    58  #define socklen_t uint32_t
    59  
    60  in_addr_t    inet_addr(const char *);
    61  char        *inet_ntoa(struct in_addr);
    62  const char  *inet_ntop(int32_t, const void *restrict, char *restrict, socklen_t);
    63  int32_t      inet_pton(int32_t, const char *restrict, void *restrict);
    64  `,
    65  		}
    66  	})
    67  }