mirror of
https://github.com/tailscale/tailscale.git
synced 2024-12-04 07:25:39 +00:00
Properly expose cancellation
Signed-off-by: Aaron Klotz <aaron@tailscale.com>
This commit is contained in:
parent
17abf201a3
commit
ef99c55d1b
@ -10,13 +10,14 @@
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
result, err := winutil.DnsQuery("www.tailscale.com", windows.DNS_TYPE_A, winutil.DNS_QUERY_STANDARD, nil, 0)
|
result, err := winutil.DNSQuery("www.tailscale.com", windows.DNS_TYPE_A, winutil.DNS_QUERY_STANDARD, nil, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintln(os.Stderr, err)
|
fmt.Fprintln(os.Stderr, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
finalStatus := result.QueryStatus
|
qresult := result.Wait()
|
||||||
|
finalStatus := qresult.QueryStatus
|
||||||
fmt.Printf("Query status: %v", finalStatus)
|
fmt.Printf("Query status: %v", finalStatus)
|
||||||
if finalStatus != 0 {
|
if finalStatus != 0 {
|
||||||
fmt.Printf(" (%v)\n", windows.Errno(finalStatus))
|
fmt.Printf(" (%v)\n", windows.Errno(finalStatus))
|
||||||
@ -25,7 +26,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
count := 0
|
count := 0
|
||||||
for rec := result.QueryRecords; rec != nil; rec = rec.Next {
|
for rec := qresult.QueryRecords; rec != nil; rec = rec.Next {
|
||||||
name := windows.UTF16PtrToString(rec.Name)
|
name := windows.UTF16PtrToString(rec.Name)
|
||||||
fmt.Printf("Record %d: %s, type %v", count, name, rec.Type)
|
fmt.Printf("Record %d: %s, type %v", count, name, rec.Type)
|
||||||
switch rec.Type {
|
switch rec.Type {
|
||||||
@ -42,5 +43,5 @@ func main() {
|
|||||||
count++
|
count++
|
||||||
}
|
}
|
||||||
|
|
||||||
result.Close()
|
qresult.Close()
|
||||||
}
|
}
|
||||||
|
@ -41,7 +41,7 @@ type DNSServerList struct {
|
|||||||
apcCallback uintptr
|
apcCallback uintptr
|
||||||
)
|
)
|
||||||
|
|
||||||
func newDnsInvoker(qname string, qtype uint16, qoptions uint64, srvList *DNSServerList, ifaceIdx uint32) (*invoker, error) {
|
func newDNSInvoker(qname string, qtype uint16, qoptions uint64, srvList *DNSServerList, ifaceIdx uint32) (*invoker, error) {
|
||||||
once.Do(func() {
|
once.Do(func() {
|
||||||
cbInfo := APCCallbackInfo{reflect.TypeOf(dnsQueryExApc), resolver{}}
|
cbInfo := APCCallbackInfo{reflect.TypeOf(dnsQueryExApc), resolver{}}
|
||||||
apcCallback = RegisterAPCCallback(cbInfo)
|
apcCallback = RegisterAPCCallback(cbInfo)
|
||||||
@ -87,16 +87,22 @@ func (i *invoker) Begin() *APCChannel {
|
|||||||
return &i.done
|
return &i.done
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *invoker) Wait() {
|
func (i *invoker) Wait() *DNSQueryResult {
|
||||||
<-i.done
|
<-i.done
|
||||||
|
return &i.result
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *invoker) Cancel() error {
|
func (i *invoker) Cancel() error {
|
||||||
return DnsCancelQuery(&i.cancel)
|
return DnsCancelQuery(&i.cancel)
|
||||||
}
|
}
|
||||||
|
|
||||||
func DnsQuery(qname string, qtype uint16, qoptions uint64, srvList *DNSServerList, interfaceIdx uint32) (*DNSQueryResult, error) {
|
type DNSResult interface {
|
||||||
inv, err := newDnsInvoker(qname, qtype, qoptions, srvList, interfaceIdx)
|
Wait() *DNSQueryResult
|
||||||
|
Cancel() error
|
||||||
|
}
|
||||||
|
|
||||||
|
func DNSQuery(qname string, qtype uint16, qoptions uint64, srvList *DNSServerList, interfaceIdx uint32) (DNSResult, error) {
|
||||||
|
inv, err := newDNSInvoker(qname, qtype, qoptions, srvList, interfaceIdx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("Failed creating DNS invoker: %w", err)
|
return nil, fmt.Errorf("Failed creating DNS invoker: %w", err)
|
||||||
}
|
}
|
||||||
@ -106,6 +112,5 @@ func DnsQuery(qname string, qtype uint16, qoptions uint64, srvList *DNSServerLis
|
|||||||
return nil, fmt.Errorf("Failed submitting work: %w", err)
|
return nil, fmt.Errorf("Failed submitting work: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
inv.Wait()
|
return inv, nil
|
||||||
return &inv.result, nil
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user