Home | History | Annotate | Download | only in src
      1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "proxy_resolver_js_bindings.h"
      6 #include "proxy_resolver_v8.h"
      7 
      8 #include <arpa/inet.h>
      9 #include <netdb.h>
     10 #include <unistd.h>
     11 #include <cstddef>
     12 #include <memory>
     13 #include <string>
     14 
     15 #include "net_util.h"
     16 
     17 namespace net {
     18 
     19 // ProxyResolverJSBindings implementation.
     20 class DefaultJSBindings : public ProxyResolverJSBindings {
     21  public:
     22   DefaultJSBindings() {
     23   }
     24 
     25   // Handler for "myIpAddress()".
     26   // TODO: Perhaps enumerate the interfaces directly, using
     27   // getifaddrs().
     28   virtual bool MyIpAddress(std::string* first_ip_address) {
     29     return MyIpAddressImpl(first_ip_address);
     30   }
     31 
     32   // Handler for "myIpAddressEx()".
     33   virtual bool MyIpAddressEx(std::string* ip_address_list) {
     34     return MyIpAddressExImpl(ip_address_list);
     35   }
     36 
     37   // Handler for "dnsResolve(host)".
     38   virtual bool DnsResolve(const std::string& host,
     39                           std::string* first_ip_address) {
     40     return DnsResolveImpl(host, first_ip_address);
     41   }
     42 
     43   // Handler for "dnsResolveEx(host)".
     44   virtual bool DnsResolveEx(const std::string& host,
     45                             std::string* ip_address_list) {
     46     return DnsResolveExImpl(host, ip_address_list);
     47   }
     48 
     49  private:
     50   bool MyIpAddressImpl(std::string* first_ip_address) {
     51     std::string my_hostname = GetHostName();
     52     if (my_hostname.empty())
     53       return false;
     54     return DnsResolveImpl(my_hostname, first_ip_address);
     55   }
     56 
     57   bool MyIpAddressExImpl(std::string* ip_address_list) {
     58     std::string my_hostname = GetHostName();
     59     if (my_hostname.empty())
     60       return false;
     61     return DnsResolveExImpl(my_hostname, ip_address_list);
     62   }
     63 
     64   bool DnsResolveImpl(const std::string& host,
     65                       std::string* first_ip_address) {
     66     struct hostent* he = gethostbyname(host.c_str());
     67 
     68     if (he == NULL || he->h_addr == NULL || he->h_addrtype != AF_INET) {
     69       return false;
     70     }
     71 
     72     char tmp[INET_ADDRSTRLEN];
     73     if (inet_ntop(he->h_addrtype, he->h_addr, tmp, sizeof(tmp)) == NULL) {
     74         return false;
     75     }
     76 
     77     *first_ip_address = std::string(tmp);
     78     return true;
     79   }
     80 
     81   bool DnsResolveExImpl(const std::string& host,
     82                         std::string* ip_address_list) {
     83     struct hostent* he = gethostbyname(host.c_str());
     84 
     85     if (he == NULL) {
     86       return false;
     87     }
     88     std::string address_list_str;
     89     for (char** addr = &he->h_addr; *addr != NULL; ++addr) {
     90       if (!address_list_str.empty())
     91         address_list_str += ";";
     92       const std::string address_string = std::string(*addr);
     93       if (address_string.empty())
     94         return false;
     95       address_list_str += address_string;
     96     }
     97     *ip_address_list = std::string(he->h_addr);
     98     return true;
     99   }
    100 
    101   std::string GetHostName() {
    102     char buffer[256];
    103     if (gethostname(buffer, 256) != 0) {
    104       buffer[0] = '\0';
    105     }
    106     return std::string(buffer);
    107   }
    108 };
    109 
    110 // static
    111 ProxyResolverJSBindings* ProxyResolverJSBindings::CreateDefault() {
    112   return new DefaultJSBindings();
    113 }
    114 
    115 }  // namespace net
    116