github.com/gotranspile/cxgo@v0.3.8-0.20240118201721-29871598a6a2/runtime/cnet/netdb.go (about)

     1  package cnet
     2  
     3  import (
     4  	"log"
     5  	"net"
     6  
     7  	"github.com/gotranspile/cxgo/runtime/libc"
     8  )
     9  
    10  type HostEnt struct {
    11  	Name     *byte
    12  	Aliases  **byte
    13  	AddrType int32
    14  	Length   int32
    15  	AddrList **byte
    16  }
    17  
    18  func GetHostByName(name *byte) *HostEnt {
    19  	host := libc.GoString(name)
    20  	ips, err := net.LookupIP(host)
    21  	if err != nil {
    22  		log.Printf("gethostbyname(%q): %v", host, err)
    23  		libc.SetErr(err)
    24  		return nil
    25  	}
    26  	arr := make([]*byte, len(ips)+1)
    27  	for i, ip := range ips {
    28  		ip4 := ip.To4()
    29  		arr[i] = &ip4[0]
    30  	}
    31  	return &HostEnt{
    32  		Name:     name,
    33  		Aliases:  nil,
    34  		AddrType: AF_INET,
    35  		Length:   4,
    36  		AddrList: &arr[0],
    37  	}
    38  }