LookupIP.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // +build android linux
  2. /*
  3. * Copyright (c) 2014, 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. dns "github.com/Psiphon-Inc/dns"
  24. "net"
  25. "os"
  26. "syscall"
  27. "time"
  28. )
  29. const DNS_PORT = 53
  30. // LookupIP resolves a hostname. When BindToDevice is not required, it
  31. // simply uses net.LookuIP.
  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.BindToDeviceServiceAddress != "" {
  37. return bindLookupIP(host, config)
  38. }
  39. return net.LookupIP(host)
  40. }
  41. // bindLookupIP implements the BindToDevice LookupIP case.
  42. // To implement socket device binding, the lower-level syscall APIs are used.
  43. // The sequence of syscalls in this implementation are taken from:
  44. // https://code.google.com/p/go/issues/detail?id=6966
  45. func bindLookupIP(host string, config *DialConfig) (addrs []net.IP, err error) {
  46. socketFd, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_DGRAM, 0)
  47. if err != nil {
  48. return nil, ContextError(err)
  49. }
  50. defer syscall.Close(socketFd)
  51. err = bindToDevice(socketFd, config)
  52. if err != nil {
  53. return nil, ContextError(err)
  54. }
  55. // config.BindToDeviceDnsServer must be an IP address
  56. ipAddr := net.ParseIP(config.BindToDeviceDnsServer)
  57. if ipAddr == nil {
  58. return nil, ContextError(errors.New("invalid IP address"))
  59. }
  60. // TODO: IPv6 support
  61. var ip [4]byte
  62. copy(ip[:], ipAddr.To4())
  63. sockAddr := syscall.SockaddrInet4{Addr: ip, Port: DNS_PORT}
  64. // Note: no timeout or interrupt for this connect, as it's a datagram socket
  65. err = syscall.Connect(socketFd, &sockAddr)
  66. if err != nil {
  67. return nil, ContextError(err)
  68. }
  69. // Convert the syscall socket to a net.Conn, for use in the dns package
  70. file := os.NewFile(uintptr(socketFd), "")
  71. defer file.Close()
  72. conn, err := net.FileConn(file)
  73. if err != nil {
  74. return nil, ContextError(err)
  75. }
  76. // Set DNS query timeouts, using the ConnectTimeout from the overall Dial
  77. conn.SetReadDeadline(time.Now().Add(config.ConnectTimeout))
  78. conn.SetWriteDeadline(time.Now().Add(config.ConnectTimeout))
  79. // Make the DNS query
  80. // TODO: make interruptible?
  81. dnsConn := &dns.Conn{Conn: conn}
  82. defer dnsConn.Close()
  83. query := new(dns.Msg)
  84. query.SetQuestion(dns.Fqdn(host), dns.TypeA)
  85. query.RecursionDesired = true
  86. dnsConn.WriteMsg(query)
  87. // Process the response
  88. response, err := dnsConn.ReadMsg()
  89. if err != nil {
  90. return nil, ContextError(err)
  91. }
  92. addrs = make([]net.IP, 0)
  93. for _, answer := range response.Answer {
  94. if a, ok := answer.(*dns.A); ok {
  95. addrs = append(addrs, a.A)
  96. }
  97. }
  98. return addrs, nil
  99. }