LookupIP.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. )
  29. // LookupIP resolves a hostname. When BindToDevice is not required, it
  30. // simply uses net.LookupIP.
  31. // When BindToDevice is required, LookupIP explicitly creates a UDP
  32. // socket, binds it to the device, and makes an explicit DNS request
  33. // to the specified DNS resolver.
  34. func LookupIP(host string, config *DialConfig) (addrs []net.IP, err error) {
  35. if config.DeviceBinder != nil {
  36. addrs, err = bindLookupIP(host, config.DnsServerGetter.GetPrimaryDnsServer(), config)
  37. if err == nil {
  38. if len(addrs) == 0 {
  39. err = errors.New("empty address list")
  40. } else {
  41. return addrs, err
  42. }
  43. }
  44. NoticeAlert("retry resolve host %s: %s", host, err)
  45. dnsServer := config.DnsServerGetter.GetSecondaryDnsServer()
  46. if dnsServer == "" {
  47. return addrs, err
  48. }
  49. return bindLookupIP(host, dnsServer, config)
  50. }
  51. return net.LookupIP(host)
  52. }
  53. // bindLookupIP implements the BindToDevice LookupIP case.
  54. // To implement socket device binding, the lower-level syscall APIs are used.
  55. // The sequence of syscalls in this implementation are taken from:
  56. // https://code.google.com/p/go/issues/detail?id=6966
  57. func bindLookupIP(host, dnsServer string, config *DialConfig) (addrs []net.IP, err error) {
  58. // When the input host is an IP address, echo it back
  59. ipAddr := net.ParseIP(host)
  60. if ipAddr != nil {
  61. return []net.IP{ipAddr}, nil
  62. }
  63. socketFd, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_DGRAM, 0)
  64. if err != nil {
  65. return nil, ContextError(err)
  66. }
  67. defer syscall.Close(socketFd)
  68. err = config.DeviceBinder.BindToDevice(socketFd)
  69. if err != nil {
  70. return nil, ContextError(fmt.Errorf("BindToDevice failed: %s", err))
  71. }
  72. // config.DnsServerGetter.GetDnsServers() must return IP addresses
  73. ipAddr = net.ParseIP(dnsServer)
  74. if ipAddr == nil {
  75. return nil, ContextError(errors.New("invalid IP address"))
  76. }
  77. // TODO: IPv6 support
  78. var ip [4]byte
  79. copy(ip[:], ipAddr.To4())
  80. sockAddr := syscall.SockaddrInet4{Addr: ip, Port: DNS_PORT}
  81. // Note: no timeout or interrupt for this connect, as it's a datagram socket
  82. err = syscall.Connect(socketFd, &sockAddr)
  83. if err != nil {
  84. return nil, ContextError(err)
  85. }
  86. // Convert the syscall socket to a net.Conn, for use in the dns package
  87. file := os.NewFile(uintptr(socketFd), "")
  88. defer file.Close()
  89. conn, err := net.FileConn(file)
  90. if err != nil {
  91. return nil, ContextError(err)
  92. }
  93. // Set DNS query timeouts, using the ConnectTimeout from the overall Dial
  94. if config.ConnectTimeout != 0 {
  95. conn.SetReadDeadline(time.Now().Add(config.ConnectTimeout))
  96. conn.SetWriteDeadline(time.Now().Add(config.ConnectTimeout))
  97. }
  98. addrs, _, err = ResolveIP(host, conn)
  99. return
  100. }