diff options
Diffstat (limited to 'libgo/go/net/lookup_windows.go')
-rw-r--r-- | libgo/go/net/lookup_windows.go | 122 |
1 files changed, 65 insertions, 57 deletions
diff --git a/libgo/go/net/lookup_windows.go b/libgo/go/net/lookup_windows.go index 6a925b0a7ad..1b6d392f660 100644 --- a/libgo/go/net/lookup_windows.go +++ b/libgo/go/net/lookup_windows.go @@ -19,13 +19,13 @@ var ( func getprotobyname(name string) (proto int, err error) { p, err := syscall.GetProtoByName(name) if err != nil { - return 0, os.NewSyscallError("GetProtoByName", err) + return 0, os.NewSyscallError("getorotobyname", err) } return int(p.Proto), nil } // lookupProtocol looks up IP protocol name and returns correspondent protocol number. -func lookupProtocol(name string) (proto int, err error) { +func lookupProtocol(name string) (int, error) { // GetProtoByName return value is stored in thread local storage. // Start new os thread before the call to prevent races. type result struct { @@ -46,47 +46,48 @@ func lookupProtocol(name string) (proto int, err error) { if proto, ok := protocols[name]; ok { return proto, nil } + r.err = &DNSError{Err: r.err.Error(), Name: name} } return r.proto, r.err } -func lookupHost(name string) (addrs []string, err error) { +func lookupHost(name string) ([]string, error) { ips, err := LookupIP(name) if err != nil { - return + return nil, err } - addrs = make([]string, 0, len(ips)) + addrs := make([]string, 0, len(ips)) for _, ip := range ips { addrs = append(addrs, ip.String()) } - return + return addrs, nil } -func gethostbyname(name string) (addrs []IP, err error) { +func gethostbyname(name string) (addrs []IPAddr, err error) { // caller already acquired thread h, err := syscall.GetHostByName(name) if err != nil { - return nil, os.NewSyscallError("GetHostByName", err) + return nil, os.NewSyscallError("gethostbyname", err) } switch h.AddrType { case syscall.AF_INET: i := 0 - addrs = make([]IP, 100) // plenty of room to grow + addrs = make([]IPAddr, 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[i] = IPAddr{IP: 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 nil, syscall.EWINDOWS } return addrs, nil } -func oldLookupIP(name string) (addrs []IP, err error) { +func oldLookupIP(name string) ([]IPAddr, error) { // GetHostByName return value is stored in thread local storage. // Start new os thread before the call to prevent races. type result struct { - addrs []IP + addrs []IPAddr err error } ch := make(chan result) @@ -99,10 +100,13 @@ func oldLookupIP(name string) (addrs []IP, err error) { ch <- result{addrs: addrs, err: err} }() r := <-ch + if r.err != nil { + r.err = &DNSError{Err: r.err.Error(), Name: name} + } return r.addrs, r.err } -func newLookupIP(name string) (addrs []IP, err error) { +func newLookupIP(name string) ([]IPAddr, error) { acquireThread() defer releaseThread() hints := syscall.AddrinfoW{ @@ -113,27 +117,28 @@ func newLookupIP(name string) (addrs []IP, err error) { var result *syscall.AddrinfoW e := syscall.GetAddrInfoW(syscall.StringToUTF16Ptr(name), nil, &hints, &result) if e != nil { - return nil, os.NewSyscallError("GetAddrInfoW", e) + return nil, &DNSError{Err: os.NewSyscallError("getaddrinfow", e).Error(), Name: name} } defer syscall.FreeAddrInfoW(result) - addrs = make([]IP, 0, 5) + addrs := make([]IPAddr, 0, 5) for ; result != nil; result = result.Next { addr := unsafe.Pointer(result.Addr) switch result.Family { case syscall.AF_INET: a := (*syscall.RawSockaddrInet4)(addr).Addr - addrs = append(addrs, IPv4(a[0], a[1], a[2], a[3])) + addrs = append(addrs, IPAddr{IP: IPv4(a[0], a[1], a[2], a[3])}) case syscall.AF_INET6: a := (*syscall.RawSockaddrInet6)(addr).Addr - addrs = append(addrs, IP{a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8], a[9], a[10], a[11], a[12], a[13], a[14], a[15]}) + zone := zoneToString(int((*syscall.RawSockaddrInet6)(addr).Scope_id)) + addrs = append(addrs, IPAddr{IP: IP{a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8], a[9], a[10], a[11], a[12], a[13], a[14], a[15]}, Zone: zone}) default: - return nil, os.NewSyscallError("LookupIP", syscall.EWINDOWS) + return nil, &DNSError{Err: syscall.EWINDOWS.Error(), Name: name} } } return addrs, nil } -func getservbyname(network, service string) (port int, err error) { +func getservbyname(network, service string) (int, error) { acquireThread() defer releaseThread() switch network { @@ -144,12 +149,12 @@ func getservbyname(network, service string) (port int, err error) { } s, err := syscall.GetServByName(service, network) if err != nil { - return 0, os.NewSyscallError("GetServByName", err) + return 0, os.NewSyscallError("getservbyname", err) } return int(syscall.Ntohs(s.Port)), nil } -func oldLookupPort(network, service string) (port int, err error) { +func oldLookupPort(network, service string) (int, error) { // GetServByName return value is stored in thread local storage. // Start new os thread before the call to prevent races. type result struct { @@ -166,10 +171,13 @@ func oldLookupPort(network, service string) (port int, err error) { ch <- result{port: port, err: err} }() r := <-ch + if r.err != nil { + r.err = &DNSError{Err: r.err.Error(), Name: network + "/" + service} + } return r.port, r.err } -func newLookupPort(network, service string) (port int, err error) { +func newLookupPort(network, service string) (int, error) { acquireThread() defer releaseThread() var stype int32 @@ -187,11 +195,11 @@ func newLookupPort(network, service string) (port int, err error) { var result *syscall.AddrinfoW e := syscall.GetAddrInfoW(nil, syscall.StringToUTF16Ptr(service), &hints, &result) if e != nil { - return 0, os.NewSyscallError("GetAddrInfoW", e) + return 0, &DNSError{Err: os.NewSyscallError("getaddrinfow", e).Error(), Name: network + "/" + service} } defer syscall.FreeAddrInfoW(result) if result == nil { - return 0, os.NewSyscallError("LookupPort", syscall.EINVAL) + return 0, &DNSError{Err: syscall.EINVAL.Error(), Name: network + "/" + service} } addr := unsafe.Pointer(result.Addr) switch result.Family { @@ -202,10 +210,10 @@ func newLookupPort(network, service string) (port int, err error) { a := (*syscall.RawSockaddrInet6)(addr) return int(syscall.Ntohs(a.Port)), nil } - return 0, os.NewSyscallError("LookupPort", syscall.EINVAL) + return 0, &DNSError{Err: syscall.EINVAL.Error(), Name: network + "/" + service} } -func lookupCNAME(name string) (cname string, err error) { +func lookupCNAME(name string) (string, error) { acquireThread() defer releaseThread() var r *syscall.DNSRecord @@ -219,16 +227,16 @@ func lookupCNAME(name string) (cname string, err error) { return name, nil } if e != nil { - return "", os.NewSyscallError("LookupCNAME", e) + return "", &DNSError{Err: os.NewSyscallError("dnsquery", e).Error(), Name: name} } defer syscall.DnsRecordListFree(r, 1) resolved := resolveCNAME(syscall.StringToUTF16Ptr(name), r) - cname = syscall.UTF16ToString((*[256]uint16)(unsafe.Pointer(resolved))[:]) + "." - return + cname := syscall.UTF16ToString((*[256]uint16)(unsafe.Pointer(resolved))[:]) + "." + return cname, nil } -func lookupSRV(service, proto, name string) (cname string, addrs []*SRV, err error) { +func lookupSRV(service, proto, name string) (string, []*SRV, error) { acquireThread() defer releaseThread() var target string @@ -240,78 +248,78 @@ func lookupSRV(service, proto, name string) (cname string, addrs []*SRV, err err var r *syscall.DNSRecord e := syscall.DnsQuery(target, syscall.DNS_TYPE_SRV, 0, nil, &r, nil) if e != nil { - return "", nil, os.NewSyscallError("LookupSRV", e) + return "", nil, &DNSError{Err: os.NewSyscallError("dnsquery", e).Error(), Name: target} } defer syscall.DnsRecordListFree(r, 1) - addrs = make([]*SRV, 0, 10) + srvs := make([]*SRV, 0, 10) for _, p := range validRecs(r, syscall.DNS_TYPE_SRV, target) { v := (*syscall.DNSSRVData)(unsafe.Pointer(&p.Data[0])) - addrs = append(addrs, &SRV{syscall.UTF16ToString((*[256]uint16)(unsafe.Pointer(v.Target))[:]), v.Port, v.Priority, v.Weight}) + srvs = append(srvs, &SRV{syscall.UTF16ToString((*[256]uint16)(unsafe.Pointer(v.Target))[:]), v.Port, v.Priority, v.Weight}) } - byPriorityWeight(addrs).sort() - return name, addrs, nil + byPriorityWeight(srvs).sort() + return name, srvs, nil } -func lookupMX(name string) (mx []*MX, err error) { +func lookupMX(name string) ([]*MX, error) { acquireThread() defer releaseThread() var r *syscall.DNSRecord e := syscall.DnsQuery(name, syscall.DNS_TYPE_MX, 0, nil, &r, nil) if e != nil { - return nil, os.NewSyscallError("LookupMX", e) + return nil, &DNSError{Err: os.NewSyscallError("dnsquery", e).Error(), Name: name} } defer syscall.DnsRecordListFree(r, 1) - mx = make([]*MX, 0, 10) + mxs := make([]*MX, 0, 10) for _, p := range validRecs(r, syscall.DNS_TYPE_MX, name) { v := (*syscall.DNSMXData)(unsafe.Pointer(&p.Data[0])) - mx = append(mx, &MX{syscall.UTF16ToString((*[256]uint16)(unsafe.Pointer(v.NameExchange))[:]) + ".", v.Preference}) + mxs = append(mxs, &MX{syscall.UTF16ToString((*[256]uint16)(unsafe.Pointer(v.NameExchange))[:]) + ".", v.Preference}) } - byPref(mx).sort() - return mx, nil + byPref(mxs).sort() + return mxs, nil } -func lookupNS(name string) (ns []*NS, err error) { +func lookupNS(name string) ([]*NS, error) { acquireThread() defer releaseThread() var r *syscall.DNSRecord e := syscall.DnsQuery(name, syscall.DNS_TYPE_NS, 0, nil, &r, nil) if e != nil { - return nil, os.NewSyscallError("LookupNS", e) + return nil, &DNSError{Err: os.NewSyscallError("dnsquery", e).Error(), Name: name} } defer syscall.DnsRecordListFree(r, 1) - ns = make([]*NS, 0, 10) + nss := make([]*NS, 0, 10) for _, p := range validRecs(r, syscall.DNS_TYPE_NS, name) { v := (*syscall.DNSPTRData)(unsafe.Pointer(&p.Data[0])) - ns = append(ns, &NS{syscall.UTF16ToString((*[256]uint16)(unsafe.Pointer(v.Host))[:]) + "."}) + nss = append(nss, &NS{syscall.UTF16ToString((*[256]uint16)(unsafe.Pointer(v.Host))[:]) + "."}) } - return ns, nil + return nss, nil } -func lookupTXT(name string) (txt []string, err error) { +func lookupTXT(name string) ([]string, error) { acquireThread() defer releaseThread() var r *syscall.DNSRecord e := syscall.DnsQuery(name, syscall.DNS_TYPE_TEXT, 0, nil, &r, nil) if e != nil { - return nil, os.NewSyscallError("LookupTXT", e) + return nil, &DNSError{Err: os.NewSyscallError("dnsquery", e).Error(), Name: name} } defer syscall.DnsRecordListFree(r, 1) - txt = make([]string, 0, 10) + txts := make([]string, 0, 10) for _, p := range validRecs(r, syscall.DNS_TYPE_TEXT, name) { d := (*syscall.DNSTXTData)(unsafe.Pointer(&p.Data[0])) for _, v := range (*[1 << 10]*uint16)(unsafe.Pointer(&(d.StringArray[0])))[:d.StringCount] { s := syscall.UTF16ToString((*[1 << 20]uint16)(unsafe.Pointer(v))[:]) - txt = append(txt, s) + txts = append(txts, s) } } - return + return txts, nil } -func lookupAddr(addr string) (name []string, err error) { +func lookupAddr(addr string) ([]string, error) { acquireThread() defer releaseThread() arpa, err := reverseaddr(addr) @@ -321,16 +329,16 @@ func lookupAddr(addr string) (name []string, err error) { var r *syscall.DNSRecord e := syscall.DnsQuery(arpa, syscall.DNS_TYPE_PTR, 0, nil, &r, nil) if e != nil { - return nil, os.NewSyscallError("LookupAddr", e) + return nil, &DNSError{Err: os.NewSyscallError("dnsquery", e).Error(), Name: addr} } defer syscall.DnsRecordListFree(r, 1) - name = make([]string, 0, 10) + ptrs := make([]string, 0, 10) for _, p := range validRecs(r, syscall.DNS_TYPE_PTR, arpa) { v := (*syscall.DNSPTRData)(unsafe.Pointer(&p.Data[0])) - name = append(name, syscall.UTF16ToString((*[256]uint16)(unsafe.Pointer(v.Host))[:])) + ptrs = append(ptrs, syscall.UTF16ToString((*[256]uint16)(unsafe.Pointer(v.Host))[:])) } - return name, nil + return ptrs, nil } const dnsSectionMask = 0x0003 |