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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
// Copyright (c) 2011 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.
//
// XmppConnectionGenerator does the following algorithm:
// proxy = ResolveProxyInformation(connection_options)
// for server in server_list
// get dns_addresses for server
// connection_list = (dns_addresses X connection methods X proxy).shuffle()
// for connection in connection_list
// yield connection
#include "jingle/notifier/communicator/xmpp_connection_generator.h"
#include <vector>
#include "base/bind.h"
#include "base/callback.h"
#include "base/compiler_specific.h"
#include "base/logging.h"
#include "base/sys_byteorder.h"
#include "jingle/notifier/base/server_information.h"
#include "jingle/notifier/communicator/connection_options.h"
#include "jingle/notifier/communicator/connection_settings.h"
#include "net/base/net_errors.h"
#include "net/base/sys_addrinfo.h"
#include "talk/base/httpcommon-inl.h"
#include "talk/base/task.h"
#include "talk/base/thread.h"
#include "talk/xmpp/prexmppauth.h"
#include "talk/xmpp/xmppclientsettings.h"
#include "talk/xmpp/xmppengine.h"
namespace notifier {
XmppConnectionGenerator::XmppConnectionGenerator(
Delegate* delegate,
net::HostResolver* host_resolver,
const ConnectionOptions* options,
bool try_ssltcp_first,
const ServerList& servers)
: delegate_(delegate),
host_resolver_(host_resolver),
settings_list_(new ConnectionSettingsList()),
settings_index_(0),
servers_(servers),
current_server_(servers_.end()),
try_ssltcp_first_(try_ssltcp_first),
successfully_resolved_dns_(false),
first_dns_error_(0),
should_resolve_dns_(true),
options_(options) {
DCHECK(delegate_);
DCHECK(host_resolver);
DCHECK(options_);
DCHECK_GT(servers_.size(), 0u);
}
XmppConnectionGenerator::~XmppConnectionGenerator() {
VLOG(1) << "XmppConnectionGenerator::~XmppConnectionGenerator";
}
// Starts resolving proxy information.
void XmppConnectionGenerator::StartGenerating() {
VLOG(1) << "XmppConnectionGenerator::StartGenerating";
// TODO(akalin): Detect proxy settings once we use Chrome sockets.
// Start iterating through the connections (which are generated on demand).
UseNextConnection();
}
namespace {
const char* const PROTO_NAMES[cricket::PROTO_LAST + 1] = {
"udp", "tcp", "ssltcp"
};
} // namespace
void XmppConnectionGenerator::UseNextConnection() {
DCHECK(settings_list_.get());
// Loop until we can use a connection or we run out of connections
// to try.
while (true) {
// Iterate to the next possible connection.
settings_index_++;
if (settings_index_ < settings_list_->GetCount()) {
// We have more connection settings in the settings_list_ to
// try, kick off the next one.
ConnectionSettings* settings =
settings_list_->GetSettings(settings_index_);
VLOG(1) << "*** Attempting " << PROTO_NAMES[settings->protocol()]
<< " connection to " << settings->server().IPAsString()
<< ":" << settings->server().port();
delegate_->OnNewSettings(*settings);
return;
}
// Iterate to the next possible server.
if (current_server_ == servers_.end()) {
current_server_ = servers_.begin();
} else {
++current_server_;
}
if (current_server_ == servers_.end()) {
// All out of possibilities.
VLOG(1) << "(" << buzz::XmppEngine::ERROR_SOCKET
<< ", " << first_dns_error_ << ")";
delegate_->OnExhaustedSettings(
successfully_resolved_dns_, first_dns_error_);
return;
}
if (should_resolve_dns_) {
// Resolve the server.
const net::HostPortPair& server = current_server_->server;
net::HostResolver::RequestInfo request_info(server);
int status =
host_resolver_.Resolve(
request_info, &address_list_,
base::Bind(&XmppConnectionGenerator::OnServerDNSResolved,
base::Unretained(this)),
bound_net_log_);
if (status == net::ERR_IO_PENDING) // OnServerDNSResolved will be called.
return;
HandleServerDNSResolved(status);
} else {
// We are not resolving DNS here (DNS will be resolved by a lower layer).
// Generate settings using an empty IP list (which will just use the
// host name for the current server).
std::vector<uint32> ip_list;
GenerateSettingsForIPList(ip_list);
}
}
}
void XmppConnectionGenerator::OnServerDNSResolved(int status) {
DCHECK_NE(status, net::ERR_IO_PENDING);
HandleServerDNSResolved(status);
// Reenter loop.
UseNextConnection();
}
void XmppConnectionGenerator::HandleServerDNSResolved(int status) {
DCHECK_NE(status, net::ERR_IO_PENDING);
VLOG(1) << "XmppConnectionGenerator::HandleServerDNSResolved";
// Print logging info.
VLOG(1) << " server: " << current_server_->server.ToString()
<< ", error: " << status;
if (status != net::OK) {
if (first_dns_error_ == 0)
first_dns_error_ = status;
return;
}
// Slurp the addresses into a vector.
std::vector<uint32> ip_list;
for (const addrinfo* addr = address_list_.head();
addr != NULL; addr = addr->ai_next) {
const sockaddr_in& sockaddr =
*reinterpret_cast<const sockaddr_in*>(addr->ai_addr);
uint32 ip = ntohl(sockaddr.sin_addr.s_addr);
ip_list.push_back(ip);
}
successfully_resolved_dns_ = !ip_list.empty();
for (int i = 0; i < static_cast<int>(ip_list.size()); ++i) {
VLOG(1) << " ip " << i
<< " : " << talk_base::SocketAddress::IPToString(ip_list[i]);
}
GenerateSettingsForIPList(ip_list);
}
void XmppConnectionGenerator::GenerateSettingsForIPList(
const std::vector<uint32>& ip_list) {
// Build the ip list.
DCHECK(settings_list_.get());
settings_index_ = -1;
settings_list_->ClearPermutations();
settings_list_->AddPermutations(
current_server_->server.host(),
ip_list,
current_server_->server.port(),
current_server_->special_port_magic,
try_ssltcp_first_);
}
} // namespace notifier
|