3 * Copyright 2004--2005, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include "talk/p2p/base/p2ptransport.h"
33 #include "talk/p2p/base/constants.h"
34 #include "talk/p2p/base/p2ptransportchannel.h"
35 #include "talk/p2p/base/parsing.h"
36 #include "talk/p2p/base/sessionmanager.h"
37 #include "talk/p2p/base/sessionmessages.h"
38 #include "webrtc/libjingle/xmllite/qname.h"
39 #include "webrtc/libjingle/xmllite/xmlelement.h"
40 #include "talk/xmpp/constants.h"
41 #include "webrtc/base/base64.h"
42 #include "webrtc/base/common.h"
43 #include "webrtc/base/stringencode.h"
44 #include "webrtc/base/stringutils.h"
48 // Limits for GICE and ICE username sizes.
49 const size_t kMaxGiceUsernameSize = 16;
50 const size_t kMaxIceUsernameSize = 512;
56 static buzz::XmlElement* NewTransportElement(const std::string& name) {
57 return new buzz::XmlElement(buzz::QName(name, LN_TRANSPORT), true);
60 P2PTransport::P2PTransport(rtc::Thread* signaling_thread,
61 rtc::Thread* worker_thread,
62 const std::string& content_name,
63 PortAllocator* allocator)
64 : Transport(signaling_thread, worker_thread,
65 content_name, NS_GINGLE_P2P, allocator) {
68 P2PTransport::~P2PTransport() {
72 TransportChannelImpl* P2PTransport::CreateTransportChannel(int component) {
73 return new P2PTransportChannel(content_name(), component, this,
77 void P2PTransport::DestroyTransportChannel(TransportChannelImpl* channel) {
81 bool P2PTransportParser::ParseTransportDescription(
82 const buzz::XmlElement* elem,
83 const CandidateTranslator* translator,
84 TransportDescription* desc,
86 ASSERT(elem->Name().LocalPart() == LN_TRANSPORT);
87 desc->transport_type = elem->Name().Namespace();
88 if (desc->transport_type != NS_GINGLE_P2P)
89 return BadParse("Unsupported transport type", error);
91 for (const buzz::XmlElement* candidate_elem = elem->FirstElement();
92 candidate_elem != NULL;
93 candidate_elem = candidate_elem->NextElement()) {
94 // Only look at local part because the namespace might (eventually)
95 // be NS_GINGLE_P2P or NS_JINGLE_ICE_UDP.
96 if (candidate_elem->Name().LocalPart() == LN_CANDIDATE) {
98 if (!ParseCandidate(ICEPROTO_GOOGLE, candidate_elem, translator,
103 desc->candidates.push_back(candidate);
109 bool P2PTransportParser::WriteTransportDescription(
110 const TransportDescription& desc,
111 const CandidateTranslator* translator,
112 buzz::XmlElement** out_elem,
114 TransportProtocol proto = TransportProtocolFromDescription(&desc);
115 rtc::scoped_ptr<buzz::XmlElement> trans_elem(
116 NewTransportElement(desc.transport_type));
118 // Fail if we get HYBRID or ICE right now.
119 // TODO(juberti): Add ICE and HYBRID serialization.
120 if (proto != ICEPROTO_GOOGLE) {
121 LOG(LS_ERROR) << "Failed to serialize non-GICE TransportDescription";
125 for (std::vector<Candidate>::const_iterator iter = desc.candidates.begin();
126 iter != desc.candidates.end(); ++iter) {
127 rtc::scoped_ptr<buzz::XmlElement> cand_elem(
128 new buzz::XmlElement(QN_GINGLE_P2P_CANDIDATE));
129 if (!WriteCandidate(proto, *iter, translator, cand_elem.get(), error)) {
132 trans_elem->AddElement(cand_elem.release());
135 *out_elem = trans_elem.release();
139 bool P2PTransportParser::ParseGingleCandidate(
140 const buzz::XmlElement* elem,
141 const CandidateTranslator* translator,
142 Candidate* candidate,
144 return ParseCandidate(ICEPROTO_GOOGLE, elem, translator, candidate, error);
147 bool P2PTransportParser::WriteGingleCandidate(
148 const Candidate& candidate,
149 const CandidateTranslator* translator,
150 buzz::XmlElement** out_elem,
152 rtc::scoped_ptr<buzz::XmlElement> elem(
153 new buzz::XmlElement(QN_GINGLE_CANDIDATE));
154 bool ret = WriteCandidate(ICEPROTO_GOOGLE, candidate, translator, elem.get(),
157 *out_elem = elem.release();
162 bool P2PTransportParser::VerifyUsernameFormat(TransportProtocol proto,
163 const std::string& username,
165 if (proto == ICEPROTO_GOOGLE || proto == ICEPROTO_HYBRID) {
166 if (username.size() > kMaxGiceUsernameSize)
167 return BadParse("candidate username is too long", error);
168 if (!rtc::Base64::IsBase64Encoded(username))
169 return BadParse("candidate username has non-base64 encoded characters",
171 } else if (proto == ICEPROTO_RFC5245) {
172 if (username.size() > kMaxIceUsernameSize)
173 return BadParse("candidate username is too long", error);
178 bool P2PTransportParser::ParseCandidate(TransportProtocol proto,
179 const buzz::XmlElement* elem,
180 const CandidateTranslator* translator,
181 Candidate* candidate,
183 ASSERT(proto == ICEPROTO_GOOGLE);
184 ASSERT(translator != NULL);
186 if (!elem->HasAttr(buzz::QN_NAME) ||
187 !elem->HasAttr(QN_ADDRESS) ||
188 !elem->HasAttr(QN_PORT) ||
189 !elem->HasAttr(QN_USERNAME) ||
190 !elem->HasAttr(QN_PROTOCOL) ||
191 !elem->HasAttr(QN_GENERATION)) {
192 return BadParse("candidate missing required attribute", error);
195 rtc::SocketAddress address;
196 if (!ParseAddress(elem, QN_ADDRESS, QN_PORT, &address, error))
199 std::string channel_name = elem->Attr(buzz::QN_NAME);
202 !translator->GetComponentFromChannelName(channel_name, &component)) {
203 return BadParse("candidate has unknown channel name " + channel_name,
207 float preference = 0.0;
208 if (!GetXmlAttr(elem, QN_PREFERENCE, 0.0f, &preference)) {
209 return BadParse("candidate has unknown preference", error);
212 candidate->set_component(component);
213 candidate->set_address(address);
214 candidate->set_username(elem->Attr(QN_USERNAME));
215 candidate->set_preference(preference);
216 candidate->set_protocol(elem->Attr(QN_PROTOCOL));
217 candidate->set_generation_str(elem->Attr(QN_GENERATION));
218 if (elem->HasAttr(QN_PASSWORD))
219 candidate->set_password(elem->Attr(QN_PASSWORD));
220 if (elem->HasAttr(buzz::QN_TYPE))
221 candidate->set_type(elem->Attr(buzz::QN_TYPE));
222 if (elem->HasAttr(QN_NETWORK))
223 candidate->set_network_name(elem->Attr(QN_NETWORK));
225 if (!VerifyUsernameFormat(proto, candidate->username(), error))
231 bool P2PTransportParser::WriteCandidate(TransportProtocol proto,
232 const Candidate& candidate,
233 const CandidateTranslator* translator,
234 buzz::XmlElement* elem,
236 ASSERT(proto == ICEPROTO_GOOGLE);
237 ASSERT(translator != NULL);
239 std::string channel_name;
241 !translator->GetChannelNameFromComponent(
242 candidate.component(), &channel_name)) {
243 return BadWrite("Cannot write candidate because of unknown component.",
247 elem->SetAttr(buzz::QN_NAME, channel_name);
248 elem->SetAttr(QN_ADDRESS, candidate.address().ipaddr().ToString());
249 elem->SetAttr(QN_PORT, candidate.address().PortAsString());
250 AddXmlAttr(elem, QN_PREFERENCE, candidate.preference());
251 elem->SetAttr(QN_USERNAME, candidate.username());
252 elem->SetAttr(QN_PROTOCOL, candidate.protocol());
253 elem->SetAttr(QN_GENERATION, candidate.generation_str());
254 if (!candidate.password().empty())
255 elem->SetAttr(QN_PASSWORD, candidate.password());
256 elem->SetAttr(buzz::QN_TYPE, candidate.type());
257 if (!candidate.network_name().empty())
258 elem->SetAttr(QN_NETWORK, candidate.network_name());
263 } // namespace cricket