LookupIP.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // +build android linux darwin
  2. /*
  3. * Copyright (c) 2015, Psiphon Inc.
  4. * All rights reserved.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. package psiphon
  21. import (
  22. "errors"
  23. "fmt"
  24. "net"
  25. "os"
  26. "syscall"
  27. "time"
  28. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common"
  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(host string, config *DialConfig) (addrs []net.IP, err error) {
  36. if config.DeviceBinder != nil {
  37. addrs, err = bindLookupIP(host, config.DnsServerGetter.GetPrimaryDnsServer(), config)
  38. if err == nil {
  39. if len(addrs) == 0 {
  40. err = errors.New("empty address list")
  41. } else {
  42. return addrs, err
  43. }
  44. }
  45. NoticeAlert("retry resolve host %s: %s", host, err)
  46. dnsServer := config.DnsServerGetter.GetSecondaryDnsServer()
  47. if dnsServer == "" {
  48. return addrs, err
  49. }
  50. return bindLookupIP(host, dnsServer, config)
  51. }
  52. return net.LookupIP(host)
  53. }
  54. // bindLookupIP implements the BindToDevice LookupIP case.
  55. // To implement socket device binding, the lower-level syscall APIs are used.
  56. // The sequence of syscalls in this implementation are taken from:
  57. // https://code.google.com/p/go/issues/detail?id=6966
  58. func bindLookupIP(host, dnsServer string, config *DialConfig) (addrs []net.IP, err error) {
  59. // When the input host is an IP address, echo it back
  60. ipAddr := net.ParseIP(host)
  61. if ipAddr != nil {
  62. return []net.IP{ipAddr}, nil
  63. }
  64. socketFd, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_DGRAM, 0)
  65. if err != nil {
  66. return nil, common.ContextError(err)
  67. }
  68. defer syscall.Close(socketFd)
  69. err = config.DeviceBinder.BindToDevice(socketFd)
  70. if err != nil {
  71. return nil, common.ContextError(fmt.Errorf("BindToDevice failed: %s", err))
  72. }
  73. // config.DnsServerGetter.GetDnsServers() must return IP addresses
  74. ipAddr = net.ParseIP(dnsServer)
  75. if ipAddr == nil {
  76. return nil, common.ContextError(errors.New("invalid IP address"))
  77. }
  78. // TODO: IPv6 support
  79. var ip [4]byte
  80. copy(ip[:], ipAddr.To4())
  81. sockAddr := syscall.SockaddrInet4{Addr: ip, Port: DNS_PORT}
  82. // Note: no timeout or interrupt for this connect, as it's a datagram socket
  83. err = syscall.Connect(socketFd, &sockAddr)
  84. if err != nil {
  85. return nil, common.ContextError(err)
  86. }
  87. // Convert the syscall socket to a net.Conn, for use in the dns package
  88. file := os.NewFile(uintptr(socketFd), "")
  89. defer file.Close()
  90. conn, err := net.FileConn(file)
  91. if err != nil {
  92. return nil, common.ContextError(err)
  93. }
  94. // Set DNS query timeouts, using the ConnectTimeout from the overall Dial
  95. if config.ConnectTimeout != 0 {
  96. conn.SetReadDeadline(time.Now().Add(config.ConnectTimeout))
  97. conn.SetWriteDeadline(time.Now().Add(config.ConnectTimeout))
  98. }
  99. addrs, _, err = ResolveIP(host, conn)
  100. return
  101. }