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