OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "remoting/jingle_glue/jingle_info_request.h" | 5 #include "remoting/jingle_glue/jingle_info_request.h" |
6 | 6 |
7 #include "base/task.h" | 7 #include "base/task.h" |
8 #include "base/message_loop.h" | |
9 #include "base/stl_util.h" | |
8 #include "base/string_number_conversions.h" | 10 #include "base/string_number_conversions.h" |
11 #include "net/base/net_util.h" | |
12 #include "remoting/jingle_glue/host_address_resolver.h" | |
9 #include "remoting/jingle_glue/iq_request.h" | 13 #include "remoting/jingle_glue/iq_request.h" |
10 #include "third_party/libjingle/source/talk/base/socketaddress.h" | 14 #include "third_party/libjingle/source/talk/base/socketaddress.h" |
11 #include "third_party/libjingle/source/talk/xmllite/xmlelement.h" | 15 #include "third_party/libjingle/source/talk/xmllite/xmlelement.h" |
12 #include "third_party/libjingle/source/talk/xmpp/constants.h" | 16 #include "third_party/libjingle/source/talk/xmpp/constants.h" |
13 | 17 |
14 namespace remoting { | 18 namespace remoting { |
15 | 19 |
16 JingleInfoRequest::JingleInfoRequest(IqRequest* request) | 20 struct JingleInfoRequest::PendingDnsRequest { |
Wez
2011/08/08 23:07:59
If you made the HostAddressResolver interface more
Sergey Ulanov
2011/08/09 01:42:16
Done.
| |
17 : request_(request) { | 21 PendingDnsRequest(HostAddressRequest* request_value, int port_value) |
22 : request(request_value), | |
23 port(port_value) { | |
24 } | |
25 | |
26 scoped_ptr<HostAddressRequest> request; | |
27 int port; | |
28 | |
29 private: | |
30 DISALLOW_COPY_AND_ASSIGN(PendingDnsRequest); | |
31 }; | |
32 | |
33 JingleInfoRequest::JingleInfoRequest(IqRequest* request, | |
34 HostAddressResolver* host_resolver) | |
35 : host_resolver_(host_resolver), | |
36 request_(request) { | |
18 request_->set_callback(NewCallback(this, &JingleInfoRequest::OnResponse)); | 37 request_->set_callback(NewCallback(this, &JingleInfoRequest::OnResponse)); |
19 } | 38 } |
20 | 39 |
21 JingleInfoRequest::~JingleInfoRequest() { | 40 JingleInfoRequest::~JingleInfoRequest() { |
41 STLDeleteContainerPairSecondPointers(stun_dns_requests_.begin(), | |
42 stun_dns_requests_.end()); | |
43 STLDeleteContainerPointers(relay_dns_requests_.begin(), | |
44 relay_dns_requests_.end()); | |
22 } | 45 } |
23 | 46 |
24 void JingleInfoRequest::Send(const OnJingleInfoCallback& callback) { | 47 void JingleInfoRequest::Send(const OnJingleInfoCallback& callback) { |
25 on_jingle_info_cb_ = callback; | 48 on_jingle_info_cb_ = callback; |
26 request_->SendIq(buzz::STR_GET, buzz::STR_EMPTY, | 49 request_->SendIq(buzz::STR_GET, buzz::STR_EMPTY, |
27 new buzz::XmlElement(buzz::QN_JINGLE_INFO_QUERY, true)); | 50 new buzz::XmlElement(buzz::QN_JINGLE_INFO_QUERY, true)); |
28 } | 51 } |
29 | 52 |
30 void JingleInfoRequest::OnResponse(const buzz::XmlElement* stanza) { | 53 void JingleInfoRequest::OnResponse(const buzz::XmlElement* stanza) { |
31 std::vector<std::string> relay_hosts; | |
32 std::vector<talk_base::SocketAddress> stun_hosts; | |
33 std::string relay_token; | |
34 | |
35 const buzz::XmlElement* query = | 54 const buzz::XmlElement* query = |
36 stanza->FirstNamed(buzz::QN_JINGLE_INFO_QUERY); | 55 stanza->FirstNamed(buzz::QN_JINGLE_INFO_QUERY); |
37 if (query == NULL) { | 56 if (query == NULL) { |
38 LOG(WARNING) << "No Jingle info found in Jingle Info query response." | 57 LOG(WARNING) << "No Jingle info found in Jingle Info query response." |
39 << stanza->Str(); | 58 << stanza->Str(); |
40 return; | 59 return; |
41 } | 60 } |
42 | 61 |
43 const buzz::XmlElement* stun = query->FirstNamed(buzz::QN_JINGLE_INFO_STUN); | 62 const buzz::XmlElement* stun = query->FirstNamed(buzz::QN_JINGLE_INFO_STUN); |
44 if (stun) { | 63 if (stun) { |
45 for (const buzz::XmlElement* server = | 64 for (const buzz::XmlElement* server = |
46 stun->FirstNamed(buzz::QN_JINGLE_INFO_SERVER); | 65 stun->FirstNamed(buzz::QN_JINGLE_INFO_SERVER); |
47 server != NULL; | 66 server != NULL; |
48 server = server->NextNamed(buzz::QN_JINGLE_INFO_SERVER)) { | 67 server = server->NextNamed(buzz::QN_JINGLE_INFO_SERVER)) { |
49 std::string host = server->Attr(buzz::QN_JINGLE_INFO_HOST); | 68 std::string host = server->Attr(buzz::QN_JINGLE_INFO_HOST); |
50 std::string port_str = server->Attr(buzz::QN_JINGLE_INFO_UDP); | 69 std::string port_str = server->Attr(buzz::QN_JINGLE_INFO_UDP); |
51 if (host != buzz::STR_EMPTY && port_str != buzz::STR_EMPTY) { | 70 if (host != buzz::STR_EMPTY && port_str != buzz::STR_EMPTY) { |
52 int port; | 71 int port; |
53 if (!base::StringToInt(port_str, &port)) { | 72 if (!base::StringToInt(port_str, &port)) { |
54 LOG(WARNING) << "Unable to parse port in stanza" << stanza->Str(); | 73 LOG(WARNING) << "Unable to parse port in stanza" << stanza->Str(); |
55 } else { | 74 } else { |
56 stun_hosts.push_back(talk_base::SocketAddress(host, port)); | 75 net::IPAddressNumber ip_number; |
76 if (host_resolver_ == NULL || | |
77 net::ParseIPLiteralToNumber(host, &ip_number)) { | |
78 stun_hosts_.push_back(talk_base::SocketAddress(host, port)); | |
Wez
2011/08/08 23:07:59
It might be simpler to mandate that HostAddressReq
Sergey Ulanov
2011/08/09 01:42:16
Done.
| |
79 } else { | |
80 HostAddressRequest* request = host_resolver_->CreateRequest(); | |
81 stun_dns_requests_[request] = new PendingDnsRequest(request, port); | |
82 request->SignalDone.connect( | |
83 this, &JingleInfoRequest::OnStunAddressResponse); | |
84 request->Request(host); | |
85 } | |
57 } | 86 } |
58 } | 87 } |
59 } | 88 } |
60 } | 89 } |
61 | 90 |
62 const buzz::XmlElement* relay = query->FirstNamed(buzz::QN_JINGLE_INFO_RELAY); | 91 const buzz::XmlElement* relay = query->FirstNamed(buzz::QN_JINGLE_INFO_RELAY); |
63 if (relay) { | 92 if (relay) { |
64 relay_token = relay->TextNamed(buzz::QN_JINGLE_INFO_TOKEN); | 93 relay_token_ = relay->TextNamed(buzz::QN_JINGLE_INFO_TOKEN); |
65 for (const buzz::XmlElement* server = | 94 for (const buzz::XmlElement* server = |
66 relay->FirstNamed(buzz::QN_JINGLE_INFO_SERVER); | 95 relay->FirstNamed(buzz::QN_JINGLE_INFO_SERVER); |
67 server != NULL; | 96 server != NULL; |
68 server = server->NextNamed(buzz::QN_JINGLE_INFO_SERVER)) { | 97 server = server->NextNamed(buzz::QN_JINGLE_INFO_SERVER)) { |
69 std::string host = server->Attr(buzz::QN_JINGLE_INFO_HOST); | 98 std::string host = server->Attr(buzz::QN_JINGLE_INFO_HOST); |
70 if (host != buzz::STR_EMPTY) { | 99 if (host != buzz::STR_EMPTY) { |
71 relay_hosts.push_back(host); | 100 net::IPAddressNumber ip_number; |
101 if (host_resolver_ == NULL || | |
102 net::ParseIPLiteralToNumber(host, &ip_number)) { | |
103 relay_hosts_.push_back(host); | |
Wez
2011/08/08 23:07:59
Same here.
Sergey Ulanov
2011/08/09 01:42:16
Actually we don't need to resolve relay hosts.
| |
104 } else { | |
105 HostAddressRequest* request = host_resolver_->CreateRequest(); | |
106 relay_dns_requests_.insert(request); | |
107 request->SignalDone.connect( | |
108 this, &JingleInfoRequest::OnRelayAddressResponse); | |
109 request->Request(host); | |
110 } | |
72 } | 111 } |
73 } | 112 } |
74 } | 113 } |
75 | 114 |
76 on_jingle_info_cb_.Run(relay_token, relay_hosts, stun_hosts); | 115 VerifyAllAddressesResolved(); |
116 } | |
117 | |
118 void JingleInfoRequest::OnStunAddressResponse( | |
119 HostAddressRequest* request, | |
120 const talk_base::SocketAddress& address) { | |
121 RequestsMap::iterator it = stun_dns_requests_.find(request); | |
122 DCHECK(it != stun_dns_requests_.end()); | |
123 | |
124 if (!address.IsNil()) { | |
125 stun_hosts_.push_back(talk_base::SocketAddress( | |
126 address.ip(), it->second->port)); | |
127 } | |
128 | |
129 MessageLoop::current()->DeleteSoon(FROM_HERE, it->second); | |
130 stun_dns_requests_.erase(it); | |
131 | |
132 VerifyAllAddressesResolved(); | |
133 } | |
134 | |
135 void JingleInfoRequest::OnRelayAddressResponse( | |
136 HostAddressRequest* request, | |
137 const talk_base::SocketAddress& address) { | |
138 if (!address.IsNil()) | |
139 relay_hosts_.push_back(address.IPAsString()); | |
140 | |
141 MessageLoop::current()->DeleteSoon(FROM_HERE, request); | |
142 relay_dns_requests_.erase(request); | |
143 | |
144 VerifyAllAddressesResolved(); | |
145 } | |
146 | |
147 void JingleInfoRequest::VerifyAllAddressesResolved() { | |
148 if (stun_dns_requests_.empty() && relay_dns_requests_.empty()) | |
149 on_jingle_info_cb_.Run(relay_token_, relay_hosts_, stun_hosts_); | |
77 } | 150 } |
78 | 151 |
79 } // namespace remoting | 152 } // namespace remoting |
OLD | NEW |