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 <netdb.h>
      9 #include <unistd.h>
     10 #include <cstddef>
     11 #include <memory>
     12 #include <string>
     13 
     14 #include "net_util.h"
     15 
     16 namespace net {
     17 
     18 // ProxyResolverJSBindings implementation.
     19 class DefaultJSBindings : public ProxyResolverJSBindings {
     20  public:
     21   DefaultJSBindings() {
     22   }
     23 
     24   // Handler for "myIpAddress()".
     25   // TODO: Perhaps enumerate the interfaces directly, using
     26   // getifaddrs().
     27   virtual bool MyIpAddress(std::string* first_ip_address) {
     28     return MyIpAddressImpl(first_ip_address);
     29   }
     30 
     31   // Handler for "myIpAddressEx()".
     32   virtual bool MyIpAddressEx(std::string* ip_address_list) {
     33     return MyIpAddressExImpl(ip_address_list);
     34   }
     35 
     36   // Handler for "dnsResolve(host)".
     37   virtual bool DnsResolve(const std::string& host,
     38                           std::string* first_ip_address) {
     39     return DnsResolveImpl(host, first_ip_address);
     40   }
     41 
     42   // Handler for "dnsResolveEx(host)".
     43   virtual bool DnsResolveEx(const std::string& host,
     44                             std::string* ip_address_list) {
     45     return DnsResolveExImpl(host, ip_address_list);
     46   }
     47 
     48  private:
     49   bool MyIpAddressImpl(std::string* first_ip_address) {
     50     std::string my_hostname = GetHostName();
     51     if (my_hostname.empty())
     52       return false;
     53     return DnsResolveImpl(my_hostname, first_ip_address);
     54   }
     55 
     56   bool MyIpAddressExImpl(std::string* ip_address_list) {
     57     std::string my_hostname = GetHostName();
     58     if (my_hostname.empty())
     59       return false;
     60     return DnsResolveExImpl(my_hostname, ip_address_list);
     61   }
     62 
     63   bool DnsResolveImpl(const std::string& host,
     64                       std::string* first_ip_address) {
     65     struct hostent* he = gethostbyname(host.c_str());
     66 
     67     if (he == NULL) {
     68       return false;
     69     }
     70     *first_ip_address = std::string(he->h_addr);
     71     return true;
     72   }
     73 
     74   bool DnsResolveExImpl(const std::string& host,
     75                         std::string* ip_address_list) {
     76     struct hostent* he = gethostbyname(host.c_str());
     77 
     78     if (he == NULL) {
     79       return false;
     80     }
     81     std::string address_list_str;
     82     for (char** addr = &he->h_addr; *addr != NULL; ++addr) {
     83       if (!address_list_str.empty())
     84         address_list_str += ";";
     85       const std::string address_string = std::string(*addr);
     86       if (address_string.empty())
     87         return false;
     88       address_list_str += address_string;
     89     }
     90     *ip_address_list = std::string(he->h_addr);
     91     return true;
     92   }
     93 
     94   std::string GetHostName() {
     95     char buffer[256];
     96     if (gethostname(buffer, 256) != 0) {
     97       buffer[0] = '\0';
     98     }
     99     return std::string(buffer);
    100   }
    101 };
    102 
    103 // static
    104 ProxyResolverJSBindings* ProxyResolverJSBindings::CreateDefault() {
    105   return new DefaultJSBindings();
    106 }
    107 
    108 }  // namespace net
    109