aboutsummaryrefslogtreecommitdiff
path: root/src/proxy_resolver_js_bindings.cc
blob: 0686f23ba5102b36c3428c9417c4a2121f9bfd94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "proxy_resolver_js_bindings.h"
#include "proxy_resolver_v8.h"

#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <cstddef>
#include <memory>
#include <string>

#include "net_util.h"

namespace net {

// ProxyResolverJSBindings implementation.
class DefaultJSBindings : public ProxyResolverJSBindings {
 public:
  DefaultJSBindings() {
  }

  // Handler for "myIpAddress()".
  // TODO: Perhaps enumerate the interfaces directly, using
  // getifaddrs().
  virtual bool MyIpAddress(std::string* first_ip_address) {
    return MyIpAddressImpl(first_ip_address);
  }

  // Handler for "myIpAddressEx()".
  virtual bool MyIpAddressEx(std::string* ip_address_list) {
    return MyIpAddressExImpl(ip_address_list);
  }

  // Handler for "dnsResolve(host)".
  virtual bool DnsResolve(const std::string& host,
                          std::string* first_ip_address) {
    return DnsResolveImpl(host, first_ip_address);
  }

  // Handler for "dnsResolveEx(host)".
  virtual bool DnsResolveEx(const std::string& host,
                            std::string* ip_address_list) {
    return DnsResolveExImpl(host, ip_address_list);
  }

 private:
  bool MyIpAddressImpl(std::string* first_ip_address) {
    std::string my_hostname = GetHostName();
    if (my_hostname.empty())
      return false;
    return DnsResolveImpl(my_hostname, first_ip_address);
  }

  bool MyIpAddressExImpl(std::string* ip_address_list) {
    std::string my_hostname = GetHostName();
    if (my_hostname.empty())
      return false;
    return DnsResolveExImpl(my_hostname, ip_address_list);
  }

  bool DnsResolveImpl(const std::string& host,
                      std::string* first_ip_address) {
    struct hostent* he = gethostbyname(host.c_str());

    if (he == NULL || he->h_addr == NULL || he->h_addrtype != AF_INET) {
      return false;
    }

    char tmp[INET_ADDRSTRLEN];
    if (inet_ntop(he->h_addrtype, he->h_addr, tmp, sizeof(tmp)) == NULL) {
        return false;
    }

    *first_ip_address = std::string(tmp);
    return true;
  }

  bool DnsResolveExImpl(const std::string& host,
                        std::string* ip_address_list) {
    struct hostent* he = gethostbyname(host.c_str());

    if (he == NULL) {
      return false;
    }
    std::string address_list_str;
    for (char** addr = &he->h_addr; *addr != NULL; ++addr) {
      if (!address_list_str.empty())
        address_list_str += ";";
      const std::string address_string = std::string(*addr);
      if (address_string.empty())
        return false;
      address_list_str += address_string;
    }
    *ip_address_list = std::string(he->h_addr);
    return true;
  }

  std::string GetHostName() {
    char buffer[256];
    if (gethostname(buffer, 256) != 0) {
      buffer[0] = '\0';
    }
    return std::string(buffer);
  }
};

// static
ProxyResolverJSBindings* ProxyResolverJSBindings::CreateDefault() {
  return new DefaultJSBindings();
}

}  // namespace net