LookupIP.go 3.5 KB

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