コード例 #1
0
func gethostbyname(name string) (addrs []IP, err error) {
	h, err := syscall.GetHostByName(name)
	if err != nil {
		return nil, os.NewSyscallError("GetHostByName", err)
	}
	switch h.AddrType {
	case syscall.AF_INET:
		i := 0
		addrs = make([]IP, 100) // plenty of room to grow
		for p := (*[100](*[4]byte))(unsafe.Pointer(h.AddrList)); i < cap(addrs) && p[i] != nil; i++ {
			addrs[i] = IPv4(p[i][0], p[i][1], p[i][2], p[i][3])
		}
		addrs = addrs[0:i]
	default: // TODO(vcc): Implement non IPv4 address lookups.
		return nil, os.NewSyscallError("LookupIP", syscall.EWINDOWS)
	}
	return addrs, nil
}
コード例 #2
0
func LookupHost(name string) (cname string, addrs []string, err os.Error) {
	hostentLock.Lock()
	defer hostentLock.Unlock()
	h, e := syscall.GetHostByName(name)
	if e != 0 {
		return "", nil, os.NewSyscallError("GetHostByName", e)
	}
	cname = name
	switch h.AddrType {
	case syscall.AF_INET:
		i := 0
		addrs = make([]string, 100) // plenty of room to grow
		for p := (*[100](*[4]byte))(unsafe.Pointer(h.AddrList)); i < cap(addrs) && p[i] != nil; i++ {
			addrs[i] = IPv4(p[i][0], p[i][1], p[i][2], p[i][3]).String()
		}
		addrs = addrs[0:i]
	default: // TODO(vcc): Implement non IPv4 address lookups.
		return "", nil, os.NewSyscallError("LookupHost", syscall.EWINDOWS)
	}
	return cname, addrs, nil
}