| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/sync/notifier/base/async_dns_lookup.h" | 5 #include "chrome/browser/sync/notifier/base/async_dns_lookup.h" |
| 6 | 6 |
| 7 #ifdef POSIX | 7 #ifdef POSIX |
| 8 #include <arpa/inet.h> | 8 #include <arpa/inet.h> |
| 9 #include <netdb.h> | 9 #include <netdb.h> |
| 10 #include <netinet/in.h> | 10 #include <netinet/in.h> |
| 11 #include <netinet/ip.h> | 11 #include <netinet/ip.h> |
| 12 #include <sys/socket.h> | 12 #include <sys/socket.h> |
| 13 #include <sys/types.h> | 13 #include <sys/types.h> |
| 14 #endif // POSIX | 14 #endif // POSIX |
| 15 | 15 |
| 16 #include <vector> | 16 #include <vector> |
| 17 | 17 |
| 18 #include "base/logging.h" |
| 18 #include "chrome/browser/sync/notifier/base/nethelpers.h" | 19 #include "chrome/browser/sync/notifier/base/nethelpers.h" |
| 19 #include "chrome/browser/sync/notifier/gaia_auth/inet_aton.h" | 20 #include "chrome/browser/sync/notifier/gaia_auth/inet_aton.h" |
| 20 #include "talk/base/byteorder.h" | 21 #include "talk/base/byteorder.h" |
| 21 #include "talk/base/common.h" | 22 #include "talk/base/common.h" |
| 22 #include "talk/base/logging.h" | |
| 23 #include "talk/base/socketaddress.h" | 23 #include "talk/base/socketaddress.h" |
| 24 #include "talk/base/thread.h" | 24 #include "talk/base/thread.h" |
| 25 | 25 |
| 26 enum { MSG_TIMEOUT = talk_base::SignalThread::ST_MSG_FIRST_AVAILABLE }; | 26 enum { MSG_TIMEOUT = talk_base::SignalThread::ST_MSG_FIRST_AVAILABLE }; |
| 27 | 27 |
| 28 #ifndef WIN32 | 28 #ifndef WIN32 |
| 29 const int WSAHOST_NOT_FOUND = 11001; // Follows the format in winsock2.h. | 29 const int WSAHOST_NOT_FOUND = 11001; // Follows the format in winsock2.h. |
| 30 #endif // WIN32 | 30 #endif // WIN32 |
| 31 | 31 |
| 32 namespace notifier { | 32 namespace notifier { |
| 33 | 33 |
| 34 AsyncDNSLookup::AsyncDNSLookup(const talk_base::SocketAddress& server) | 34 AsyncDNSLookup::AsyncDNSLookup(const talk_base::SocketAddress& server) |
| 35 : server_(new talk_base::SocketAddress(server)), | 35 : server_(new talk_base::SocketAddress(server)), |
| 36 error_(0) { | 36 error_(0) { |
| 37 // Timeout after 5 seconds. | 37 // Timeout after 5 seconds. |
| 38 talk_base::Thread::Current()->PostDelayed(5000, this, MSG_TIMEOUT); | 38 talk_base::Thread::Current()->PostDelayed(5000, this, MSG_TIMEOUT); |
| 39 } | 39 } |
| 40 | 40 |
| 41 AsyncDNSLookup::~AsyncDNSLookup() { | 41 AsyncDNSLookup::~AsyncDNSLookup() { |
| 42 } | 42 } |
| 43 | 43 |
| 44 void AsyncDNSLookup::DoWork() { | 44 void AsyncDNSLookup::DoWork() { |
| 45 std::string hostname(server_->IPAsString()); | 45 std::string hostname(server_->IPAsString()); |
| 46 | 46 |
| 47 in_addr addr; | 47 in_addr addr; |
| 48 if (inet_aton(hostname.c_str(), &addr)) { | 48 if (inet_aton(hostname.c_str(), &addr)) { |
| 49 talk_base::CritScope scope(&cs_); | 49 talk_base::CritScope scope(&cs_); |
| 50 ip_list_.push_back(talk_base::NetworkToHost32(addr.s_addr)); | 50 ip_list_.push_back(talk_base::NetworkToHost32(addr.s_addr)); |
| 51 } else { | 51 } else { |
| 52 LOG_F(LS_VERBOSE) << "(" << hostname << ")"; | 52 LOG(INFO) << "(" << hostname << ")"; |
| 53 hostent ent; | 53 hostent ent; |
| 54 char buffer[8192]; | 54 char buffer[8192]; |
| 55 int errcode = 0; | 55 int errcode = 0; |
| 56 hostent* host = SafeGetHostByName(hostname.c_str(), &ent, | 56 hostent* host = SafeGetHostByName(hostname.c_str(), &ent, |
| 57 buffer, sizeof(buffer), | 57 buffer, sizeof(buffer), |
| 58 &errcode); | 58 &errcode); |
| 59 talk_base::Thread::Current()->Clear(this, MSG_TIMEOUT); | 59 talk_base::Thread::Current()->Clear(this, MSG_TIMEOUT); |
| 60 if (host) { | 60 if (host) { |
| 61 talk_base::CritScope scope(&cs_); | 61 talk_base::CritScope scope(&cs_); |
| 62 | 62 |
| 63 // Check to see if this already timed out. | 63 // Check to see if this already timed out. |
| 64 if (error_ == 0) { | 64 if (error_ == 0) { |
| 65 for (int index = 0; true; ++index) { | 65 for (int index = 0; true; ++index) { |
| 66 uint32* addr = reinterpret_cast<uint32*>(host->h_addr_list[index]); | 66 uint32* addr = reinterpret_cast<uint32*>(host->h_addr_list[index]); |
| 67 if (addr == 0) { // 0 = end of list. | 67 if (addr == 0) { // 0 = end of list. |
| 68 break; | 68 break; |
| 69 } | 69 } |
| 70 uint32 ip = talk_base::NetworkToHost32(*addr); | 70 uint32 ip = talk_base::NetworkToHost32(*addr); |
| 71 LOG_F(LS_VERBOSE) << "(" << hostname << ") resolved to: " | 71 LOG(INFO) << "(" << hostname << ") resolved to: " |
| 72 << talk_base::SocketAddress::IPToString(ip); | 72 << talk_base::SocketAddress::IPToString(ip); |
| 73 ip_list_.push_back(ip); | 73 ip_list_.push_back(ip); |
| 74 } | 74 } |
| 75 // Maintain the invariant that either the list is not empty or the | 75 // Maintain the invariant that either the list is not empty or the |
| 76 // error is non zero when we are done with processing the dnslookup. | 76 // error is non zero when we are done with processing the dnslookup. |
| 77 if (ip_list_.empty() && error_ == 0) { | 77 if (ip_list_.empty() && error_ == 0) { |
| 78 error_ = WSAHOST_NOT_FOUND; | 78 error_ = WSAHOST_NOT_FOUND; |
| 79 } | 79 } |
| 80 } | 80 } |
| 81 FreeHostEnt(host); | 81 FreeHostEnt(host); |
| 82 } else { | 82 } else { |
| 83 { // Scoping for the critical section. | 83 { // Scoping for the critical section. |
| 84 talk_base::CritScope scope(&cs_); | 84 talk_base::CritScope scope(&cs_); |
| 85 | 85 |
| 86 // Check to see if this already timed out. | 86 // Check to see if this already timed out. |
| 87 if (error_ == 0) { | 87 if (error_ == 0) { |
| 88 error_ = errcode; | 88 error_ = errcode; |
| 89 } | 89 } |
| 90 } | 90 } |
| 91 LOG_F(LS_ERROR) << "(" << hostname << ") error: " << error_; | 91 LOG(ERROR) << "(" << hostname << ") error: " << error_; |
| 92 } | 92 } |
| 93 } | 93 } |
| 94 } | 94 } |
| 95 | 95 |
| 96 void AsyncDNSLookup::OnMessage(talk_base::Message* message) { | 96 void AsyncDNSLookup::OnMessage(talk_base::Message* message) { |
| 97 ASSERT(message); | 97 ASSERT(message); |
| 98 if (message->message_id == MSG_TIMEOUT) { | 98 if (message->message_id == MSG_TIMEOUT) { |
| 99 OnTimeout(); | 99 OnTimeout(); |
| 100 } else { | 100 } else { |
| 101 talk_base::SignalThread::OnMessage(message); | 101 talk_base::SignalThread::OnMessage(message); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 123 // AsyncDNSLookup::DoWork thread doesn't have any locks at this point, and it | 123 // AsyncDNSLookup::DoWork thread doesn't have any locks at this point, and it |
| 124 // is the only thread being held up by this. | 124 // is the only thread being held up by this. |
| 125 SignalWorkDone(this); | 125 SignalWorkDone(this); |
| 126 | 126 |
| 127 // Ensure that no more "WorkDone" signaling is done. | 127 // Ensure that no more "WorkDone" signaling is done. |
| 128 // Don't call Release or Destroy since that was already done by the callback. | 128 // Don't call Release or Destroy since that was already done by the callback. |
| 129 SignalWorkDone.disconnect_all(); | 129 SignalWorkDone.disconnect_all(); |
| 130 } | 130 } |
| 131 | 131 |
| 132 } // namespace notifier | 132 } // namespace notifier |
| OLD | NEW |