Source file
src/net/lookup_windows.go
Documentation: net
1
2
3
4
5 package net
6
7 import (
8 "context"
9 "internal/syscall/windows"
10 "os"
11 "runtime"
12 "syscall"
13 "time"
14 "unsafe"
15 )
16
17
18
19
20 const cgoAvailable = true
21
22 const (
23 _DNS_ERROR_RCODE_NAME_ERROR = syscall.Errno(9003)
24 _DNS_INFO_NO_RECORDS = syscall.Errno(9501)
25
26 _WSAHOST_NOT_FOUND = syscall.Errno(11001)
27 _WSATRY_AGAIN = syscall.Errno(11002)
28 _WSATYPE_NOT_FOUND = syscall.Errno(10109)
29 )
30
31 func winError(call string, err error) error {
32 switch err {
33 case _WSAHOST_NOT_FOUND, _DNS_ERROR_RCODE_NAME_ERROR, _DNS_INFO_NO_RECORDS:
34 return errNoSuchHost
35 }
36 return os.NewSyscallError(call, err)
37 }
38
39 func getprotobyname(name string) (proto int, err error) {
40 p, err := syscall.GetProtoByName(name)
41 if err != nil {
42 return 0, winError("getprotobyname", err)
43 }
44 return int(p.Proto), nil
45 }
46
47
48 func lookupProtocol(ctx context.Context, name string) (int, error) {
49
50
51 type result struct {
52 proto int
53 err error
54 }
55 ch := make(chan result)
56 go func() {
57 acquireThread()
58 defer releaseThread()
59 runtime.LockOSThread()
60 defer runtime.UnlockOSThread()
61 proto, err := getprotobyname(name)
62 select {
63 case ch <- result{proto: proto, err: err}:
64 case <-ctx.Done():
65 }
66 }()
67 select {
68 case r := <-ch:
69 if r.err != nil {
70 if proto, err := lookupProtocolMap(name); err == nil {
71 return proto, nil
72 }
73
74 dnsError := &DNSError{Err: r.err.Error(), Name: name}
75 if r.err == errNoSuchHost {
76 dnsError.IsNotFound = true
77 }
78 r.err = dnsError
79 }
80 return r.proto, r.err
81 case <-ctx.Done():
82 return 0, mapErr(ctx.Err())
83 }
84 }
85
86 func (r *Resolver) lookupHost(ctx context.Context, name string) ([]string, error) {
87 ips, err := r.lookupIP(ctx, "ip", name)
88 if err != nil {
89 return nil, err
90 }
91 addrs := make([]string, 0, len(ips))
92 for _, ip := range ips {
93 addrs = append(addrs, ip.String())
94 }
95 return addrs, nil
96 }
97
98 func (r *Resolver) lookupIP(ctx context.Context, network, name string) ([]IPAddr, error) {
99 if order, conf := systemConf().hostLookupOrder(r, name); order != hostLookupCgo {
100 return r.goLookupIP(ctx, network, name, order, conf)
101 }
102
103
104
105 var family int32 = syscall.AF_UNSPEC
106 switch ipVersion(network) {
107 case '4':
108 family = syscall.AF_INET
109 case '6':
110 family = syscall.AF_INET6
111 }
112
113 getaddr := func() ([]IPAddr, error) {
114 acquireThread()
115 defer releaseThread()
116 hints := syscall.AddrinfoW{
117 Family: family,
118 Socktype: syscall.SOCK_STREAM,
119 Protocol: syscall.IPPROTO_IP,
120 }
121 var result *syscall.AddrinfoW
122 name16p, err := syscall.UTF16PtrFromString(name)
123 if err != nil {
124 return nil, &DNSError{Name: name, Err: err.Error()}
125 }
126
127 dnsConf := getSystemDNSConfig()
128 start := time.Now()
129
130 var e error
131 for i := 0; i < dnsConf.attempts; i++ {
132 e = syscall.GetAddrInfoW(name16p, nil, &hints, &result)
133 if e == nil || e != _WSATRY_AGAIN || time.Since(start) > dnsConf.timeout {
134 break
135 }
136 }
137 if e != nil {
138 err := winError("getaddrinfow", e)
139 dnsError := &DNSError{Err: err.Error(), Name: name}
140 if err == errNoSuchHost {
141 dnsError.IsNotFound = true
142 }
143 return nil, dnsError
144 }
145 defer syscall.FreeAddrInfoW(result)
146 addrs := make([]IPAddr, 0, 5)
147 for ; result != nil; result = result.Next {
148 addr := unsafe.Pointer(result.Addr)
149 switch result.Family {
150 case syscall.AF_INET:
151 a := (*syscall.RawSockaddrInet4)(addr).Addr
152 addrs = append(addrs, IPAddr{IP: copyIP(a[:])})
153 case syscall.AF_INET6:
154 a := (*syscall.RawSockaddrInet6)(addr).Addr
155 zone := zoneCache.name(int((*syscall.RawSockaddrInet6)(addr).Scope_id))
156 addrs = append(addrs, IPAddr{IP: copyIP(a[:]), Zone: zone})
157 default:
158 return nil, &DNSError{Err: syscall.EWINDOWS.Error(), Name: name}
159 }
160 }
161 return addrs, nil
162 }
163
164 type ret struct {
165 addrs []IPAddr
166 err error
167 }
168
169 var ch chan ret
170 if ctx.Err() == nil {
171 ch = make(chan ret, 1)
172 go func() {
173 addr, err := getaddr()
174 ch <- ret{addrs: addr, err: err}
175 }()
176 }
177
178 select {
179 case r := <-ch:
180 return r.addrs, r.err
181 case <-ctx.Done():
182
183
184
185
186
187
188
189
190 return nil, &DNSError{
191 Name: name,
192 Err: ctx.Err().Error(),
193 IsTimeout: ctx.Err() == context.DeadlineExceeded,
194 }
195 }
196 }
197
198 func (r *Resolver) lookupPort(ctx context.Context, network, service string) (int, error) {
199 if systemConf().mustUseGoResolver(r) {
200 return lookupPortMap(network, service)
201 }
202
203
204 acquireThread()
205 defer releaseThread()
206
207 var hints syscall.AddrinfoW
208
209 switch network {
210 case "ip":
211 case "tcp", "tcp4", "tcp6":
212 hints.Socktype = syscall.SOCK_STREAM
213 hints.Protocol = syscall.IPPROTO_TCP
214 case "udp", "udp4", "udp6":
215 hints.Socktype = syscall.SOCK_DGRAM
216 hints.Protocol = syscall.IPPROTO_UDP
217 default:
218 return 0, &DNSError{Err: "unknown network", Name: network + "/" + service}
219 }
220
221 switch ipVersion(network) {
222 case '4':
223 hints.Family = syscall.AF_INET
224 case '6':
225 hints.Family = syscall.AF_INET6
226 }
227
228 var result *syscall.AddrinfoW
229 e := syscall.GetAddrInfoW(nil, syscall.StringToUTF16Ptr(service), &hints, &result)
230 if e != nil {
231 if port, err := lookupPortMap(network, service); err == nil {
232 return port, nil
233 }
234
235
236
237
238
239 if e == _WSATYPE_NOT_FOUND || e == _WSAHOST_NOT_FOUND {
240 return 0, &DNSError{Err: "unknown port", Name: network + "/" + service, IsNotFound: true}
241 }
242 err := os.NewSyscallError("getaddrinfow", e)
243 return 0, &DNSError{Err: err.Error(), Name: network + "/" + service}
244 }
245 defer syscall.FreeAddrInfoW(result)
246 if result == nil {
247 return 0, &DNSError{Err: syscall.EINVAL.Error(), Name: network + "/" + service}
248 }
249 addr := unsafe.Pointer(result.Addr)
250 switch result.Family {
251 case syscall.AF_INET:
252 a := (*syscall.RawSockaddrInet4)(addr)
253 return int(syscall.Ntohs(a.Port)), nil
254 case syscall.AF_INET6:
255 a := (*syscall.RawSockaddrInet6)(addr)
256 return int(syscall.Ntohs(a.Port)), nil
257 }
258 return 0, &DNSError{Err: syscall.EINVAL.Error(), Name: network + "/" + service}
259 }
260
261 func (r *Resolver) lookupCNAME(ctx context.Context, name string) (string, error) {
262 if order, conf := systemConf().hostLookupOrder(r, name); order != hostLookupCgo {
263 return r.goLookupCNAME(ctx, name, order, conf)
264 }
265
266
267 acquireThread()
268 defer releaseThread()
269 var rec *syscall.DNSRecord
270 e := syscall.DnsQuery(name, syscall.DNS_TYPE_CNAME, 0, nil, &rec, nil)
271
272 if errno, ok := e.(syscall.Errno); ok && errno == syscall.DNS_INFO_NO_RECORDS {
273
274 return absDomainName(name), nil
275 }
276 if e != nil {
277 err := winError("dnsquery", e)
278 return "", &DNSError{Err: err.Error(), Name: name, IsNotFound: err == errNoSuchHost}
279 }
280 defer syscall.DnsRecordListFree(rec, 1)
281
282 resolved := resolveCNAME(syscall.StringToUTF16Ptr(name), rec)
283 cname := windows.UTF16PtrToString(resolved)
284 return absDomainName(cname), nil
285 }
286
287 func (r *Resolver) lookupSRV(ctx context.Context, service, proto, name string) (string, []*SRV, error) {
288 if systemConf().mustUseGoResolver(r) {
289 return r.goLookupSRV(ctx, service, proto, name)
290 }
291
292 acquireThread()
293 defer releaseThread()
294 var target string
295 if service == "" && proto == "" {
296 target = name
297 } else {
298 target = "_" + service + "._" + proto + "." + name
299 }
300 var rec *syscall.DNSRecord
301 e := syscall.DnsQuery(target, syscall.DNS_TYPE_SRV, 0, nil, &rec, nil)
302 if e != nil {
303 err := winError("dnsquery", e)
304 return "", nil, &DNSError{Err: err.Error(), Name: name, IsNotFound: err == errNoSuchHost}
305 }
306 defer syscall.DnsRecordListFree(rec, 1)
307
308 srvs := make([]*SRV, 0, 10)
309 for _, p := range validRecs(rec, syscall.DNS_TYPE_SRV, target) {
310 v := (*syscall.DNSSRVData)(unsafe.Pointer(&p.Data[0]))
311 srvs = append(srvs, &SRV{absDomainName(syscall.UTF16ToString((*[256]uint16)(unsafe.Pointer(v.Target))[:])), v.Port, v.Priority, v.Weight})
312 }
313 byPriorityWeight(srvs).sort()
314 return absDomainName(target), srvs, nil
315 }
316
317 func (r *Resolver) lookupMX(ctx context.Context, name string) ([]*MX, error) {
318 if systemConf().mustUseGoResolver(r) {
319 return r.goLookupMX(ctx, name)
320 }
321
322 acquireThread()
323 defer releaseThread()
324 var rec *syscall.DNSRecord
325 e := syscall.DnsQuery(name, syscall.DNS_TYPE_MX, 0, nil, &rec, nil)
326 if e != nil {
327 err := winError("dnsquery", e)
328 return nil, &DNSError{Err: err.Error(), Name: name, IsNotFound: err == errNoSuchHost}
329 }
330 defer syscall.DnsRecordListFree(rec, 1)
331
332 mxs := make([]*MX, 0, 10)
333 for _, p := range validRecs(rec, syscall.DNS_TYPE_MX, name) {
334 v := (*syscall.DNSMXData)(unsafe.Pointer(&p.Data[0]))
335 mxs = append(mxs, &MX{absDomainName(windows.UTF16PtrToString(v.NameExchange)), v.Preference})
336 }
337 byPref(mxs).sort()
338 return mxs, nil
339 }
340
341 func (r *Resolver) lookupNS(ctx context.Context, name string) ([]*NS, error) {
342 if systemConf().mustUseGoResolver(r) {
343 return r.goLookupNS(ctx, name)
344 }
345
346 acquireThread()
347 defer releaseThread()
348 var rec *syscall.DNSRecord
349 e := syscall.DnsQuery(name, syscall.DNS_TYPE_NS, 0, nil, &rec, nil)
350 if e != nil {
351 err := winError("dnsquery", e)
352 return nil, &DNSError{Err: err.Error(), Name: name, IsNotFound: err == errNoSuchHost}
353 }
354 defer syscall.DnsRecordListFree(rec, 1)
355
356 nss := make([]*NS, 0, 10)
357 for _, p := range validRecs(rec, syscall.DNS_TYPE_NS, name) {
358 v := (*syscall.DNSPTRData)(unsafe.Pointer(&p.Data[0]))
359 nss = append(nss, &NS{absDomainName(syscall.UTF16ToString((*[256]uint16)(unsafe.Pointer(v.Host))[:]))})
360 }
361 return nss, nil
362 }
363
364 func (r *Resolver) lookupTXT(ctx context.Context, name string) ([]string, error) {
365 if systemConf().mustUseGoResolver(r) {
366 return r.goLookupTXT(ctx, name)
367 }
368
369 acquireThread()
370 defer releaseThread()
371 var rec *syscall.DNSRecord
372 e := syscall.DnsQuery(name, syscall.DNS_TYPE_TEXT, 0, nil, &rec, nil)
373 if e != nil {
374 err := winError("dnsquery", e)
375 return nil, &DNSError{Err: err.Error(), Name: name, IsNotFound: err == errNoSuchHost}
376 }
377 defer syscall.DnsRecordListFree(rec, 1)
378
379 txts := make([]string, 0, 10)
380 for _, p := range validRecs(rec, syscall.DNS_TYPE_TEXT, name) {
381 d := (*syscall.DNSTXTData)(unsafe.Pointer(&p.Data[0]))
382 s := ""
383 for _, v := range (*[1 << 10]*uint16)(unsafe.Pointer(&(d.StringArray[0])))[:d.StringCount:d.StringCount] {
384 s += windows.UTF16PtrToString(v)
385 }
386 txts = append(txts, s)
387 }
388 return txts, nil
389 }
390
391 func (r *Resolver) lookupAddr(ctx context.Context, addr string) ([]string, error) {
392 if order, conf := systemConf().addrLookupOrder(r, addr); order != hostLookupCgo {
393 return r.goLookupPTR(ctx, addr, order, conf)
394 }
395
396
397 acquireThread()
398 defer releaseThread()
399 arpa, err := reverseaddr(addr)
400 if err != nil {
401 return nil, err
402 }
403 var rec *syscall.DNSRecord
404 e := syscall.DnsQuery(arpa, syscall.DNS_TYPE_PTR, 0, nil, &rec, nil)
405 if e != nil {
406 err := winError("dnsquery", e)
407 return nil, &DNSError{Err: err.Error(), Name: addr, IsNotFound: err == errNoSuchHost}
408 }
409 defer syscall.DnsRecordListFree(rec, 1)
410
411 ptrs := make([]string, 0, 10)
412 for _, p := range validRecs(rec, syscall.DNS_TYPE_PTR, arpa) {
413 v := (*syscall.DNSPTRData)(unsafe.Pointer(&p.Data[0]))
414 ptrs = append(ptrs, absDomainName(windows.UTF16PtrToString(v.Host)))
415 }
416 return ptrs, nil
417 }
418
419 const dnsSectionMask = 0x0003
420
421
422 func validRecs(r *syscall.DNSRecord, dnstype uint16, name string) []*syscall.DNSRecord {
423 cname := syscall.StringToUTF16Ptr(name)
424 if dnstype != syscall.DNS_TYPE_CNAME {
425 cname = resolveCNAME(cname, r)
426 }
427 rec := make([]*syscall.DNSRecord, 0, 10)
428 for p := r; p != nil; p = p.Next {
429
430 if p.Dw&dnsSectionMask != syscall.DnsSectionAnswer && p.Dw&dnsSectionMask != syscall.DnsSectionQuestion {
431 continue
432 }
433 if p.Type != dnstype {
434 continue
435 }
436 if !syscall.DnsNameCompare(cname, p.Name) {
437 continue
438 }
439 rec = append(rec, p)
440 }
441 return rec
442 }
443
444
445 func resolveCNAME(name *uint16, r *syscall.DNSRecord) *uint16 {
446
447 Cname:
448 for cnameloop := 0; cnameloop < 10; cnameloop++ {
449 for p := r; p != nil; p = p.Next {
450 if p.Dw&dnsSectionMask != syscall.DnsSectionAnswer {
451 continue
452 }
453 if p.Type != syscall.DNS_TYPE_CNAME {
454 continue
455 }
456 if !syscall.DnsNameCompare(name, p.Name) {
457 continue
458 }
459 name = (*syscall.DNSPTRData)(unsafe.Pointer(&r.Data[0])).Host
460 continue Cname
461 }
462 break
463 }
464 return name
465 }
466
467
468
469 func concurrentThreadsLimit() int {
470 return 500
471 }
472
View as plain text