LookupIP.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. //go:build android || linux || darwin
  2. // +build android linux darwin
  3. /*
  4. * Copyright (c) 2015, Psiphon Inc.
  5. * All rights reserved.
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. package psiphon
  22. import (
  23. "context"
  24. std_errors "errors"
  25. "net"
  26. "strconv"
  27. "syscall"
  28. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/errors"
  29. )
  30. // LookupIP resolves a hostname. When BindToDevice is not required, it
  31. // simply uses net.LookupIP.
  32. // When BindToDevice is required, LookupIP explicitly creates a UDP
  33. // socket, binds it to the device, and makes an explicit DNS request
  34. // to the specified DNS resolver.
  35. func LookupIP(ctx context.Context, host string, config *DialConfig) ([]net.IP, error) {
  36. ip := net.ParseIP(host)
  37. if ip != nil {
  38. return []net.IP{ip}, nil
  39. }
  40. if config.DeviceBinder != nil {
  41. dnsServer := config.DnsServerGetter.GetPrimaryDnsServer()
  42. ips, err := bindLookupIP(ctx, host, dnsServer, config)
  43. if err == nil {
  44. if len(ips) > 0 {
  45. return ips, nil
  46. }
  47. err = std_errors.New("empty address list")
  48. }
  49. if ctx.Err() != nil {
  50. // Don't fall through to secondary when the context is cancelled.
  51. return ips, errors.Trace(err)
  52. }
  53. dnsServer = config.DnsServerGetter.GetSecondaryDnsServer()
  54. if dnsServer == "" {
  55. return ips, errors.Trace(err)
  56. }
  57. if GetEmitNetworkParameters() {
  58. NoticeWarning("retry resolve host %s: %s", host, err)
  59. }
  60. ips, err = bindLookupIP(ctx, host, dnsServer, config)
  61. if err != nil {
  62. return nil, errors.Trace(err)
  63. }
  64. return ips, nil
  65. }
  66. addrs, err := net.DefaultResolver.LookupIPAddr(ctx, host)
  67. // Remove domain names from "net" error messages.
  68. if err != nil && !GetEmitNetworkParameters() {
  69. err = RedactNetError(err)
  70. }
  71. if err != nil {
  72. return nil, errors.Trace(err)
  73. }
  74. ips := make([]net.IP, len(addrs))
  75. for i, addr := range addrs {
  76. ips[i] = addr.IP
  77. }
  78. return ips, nil
  79. }
  80. // bindLookupIP implements the BindToDevice LookupIP case.
  81. // To implement socket device binding, the lower-level syscall APIs are used.
  82. func bindLookupIP(
  83. ctx context.Context, host, dnsServer string, config *DialConfig) ([]net.IP, error) {
  84. // config.DnsServerGetter.GetDnsServers() must return IP addresses
  85. ipAddr := net.ParseIP(dnsServer)
  86. if ipAddr == nil {
  87. return nil, errors.TraceNew("invalid IP address")
  88. }
  89. // When configured, attempt to synthesize an IPv6 address from
  90. // an IPv4 address for compatibility on DNS64/NAT64 networks.
  91. // If synthesize fails, try the original address.
  92. if config.IPv6Synthesizer != nil && ipAddr.To4() != nil {
  93. synthesizedIPAddress := config.IPv6Synthesizer.IPv6Synthesize(dnsServer)
  94. if synthesizedIPAddress != "" {
  95. synthesizedAddr := net.ParseIP(synthesizedIPAddress)
  96. if synthesizedAddr != nil {
  97. ipAddr = synthesizedAddr
  98. }
  99. }
  100. }
  101. dialer := &net.Dialer{
  102. Control: func(_, _ string, c syscall.RawConn) error {
  103. var controlErr error
  104. err := c.Control(func(fd uintptr) {
  105. socketFD := int(fd)
  106. _, err := config.DeviceBinder.BindToDevice(socketFD)
  107. if err != nil {
  108. controlErr = errors.Tracef("BindToDevice failed: %s", err)
  109. return
  110. }
  111. })
  112. if controlErr != nil {
  113. return errors.Trace(controlErr)
  114. }
  115. return errors.Trace(err)
  116. },
  117. }
  118. netConn, err := dialer.DialContext(
  119. ctx, "udp", net.JoinHostPort(ipAddr.String(), strconv.Itoa(DNS_PORT)))
  120. if err != nil {
  121. return nil, errors.Trace(err)
  122. }
  123. type resolveIPResult struct {
  124. ips []net.IP
  125. err error
  126. }
  127. resultChannel := make(chan resolveIPResult)
  128. go func() {
  129. ips, _, err := ResolveIP(host, netConn)
  130. netConn.Close()
  131. resultChannel <- resolveIPResult{ips: ips, err: err}
  132. }()
  133. var result resolveIPResult
  134. select {
  135. case result = <-resultChannel:
  136. case <-ctx.Done():
  137. result.err = ctx.Err()
  138. // Interrupt the goroutine
  139. netConn.Close()
  140. <-resultChannel
  141. }
  142. if result.err != nil {
  143. return nil, errors.Trace(err)
  144. }
  145. return result.ips, nil
  146. }