1 /*
2 * libjingle
3 * Copyright 2004--2005, Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
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.
15 *
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.
26 */
27
28 #include "talk/p2p/base/p2ptransport.h"
29
30 #include <string>
31 #include <vector>
32
33 #include "talk/base/base64.h"
34 #include "talk/base/common.h"
35 #include "talk/base/stringencode.h"
36 #include "talk/base/stringutils.h"
37 #include "talk/p2p/base/constants.h"
38 #include "talk/p2p/base/p2ptransportchannel.h"
39 #include "talk/p2p/base/parsing.h"
40 #include "talk/p2p/base/sessionmanager.h"
41 #include "talk/p2p/base/sessionmessages.h"
42 #include "talk/xmllite/qname.h"
43 #include "talk/xmllite/xmlelement.h"
44 #include "talk/xmpp/constants.h"
45
46 namespace {
47
48 // Limits for GICE and ICE username sizes.
49 const size_t kMaxGiceUsernameSize = 16;
50 const size_t kMaxIceUsernameSize = 512;
51
52 } // namespace
53
54 namespace cricket {
55
NewTransportElement(const std::string & name)56 static buzz::XmlElement* NewTransportElement(const std::string& name) {
57 return new buzz::XmlElement(buzz::QName(name, LN_TRANSPORT), true);
58 }
59
P2PTransport(talk_base::Thread * signaling_thread,talk_base::Thread * worker_thread,const std::string & content_name,PortAllocator * allocator)60 P2PTransport::P2PTransport(talk_base::Thread* signaling_thread,
61 talk_base::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) {
66 }
67
~P2PTransport()68 P2PTransport::~P2PTransport() {
69 DestroyAllChannels();
70 }
71
CreateTransportChannel(int component)72 TransportChannelImpl* P2PTransport::CreateTransportChannel(int component) {
73 return new P2PTransportChannel(content_name(), component, this,
74 port_allocator());
75 }
76
DestroyTransportChannel(TransportChannelImpl * channel)77 void P2PTransport::DestroyTransportChannel(TransportChannelImpl* channel) {
78 delete channel;
79 }
80
ParseTransportDescription(const buzz::XmlElement * elem,const CandidateTranslator * translator,TransportDescription * desc,ParseError * error)81 bool P2PTransportParser::ParseTransportDescription(
82 const buzz::XmlElement* elem,
83 const CandidateTranslator* translator,
84 TransportDescription* desc,
85 ParseError* error) {
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);
90
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) {
97 Candidate candidate;
98 if (!ParseCandidate(ICEPROTO_GOOGLE, candidate_elem, translator,
99 &candidate, error)) {
100 return false;
101 }
102
103 desc->candidates.push_back(candidate);
104 }
105 }
106 return true;
107 }
108
WriteTransportDescription(const TransportDescription & desc,const CandidateTranslator * translator,buzz::XmlElement ** out_elem,WriteError * error)109 bool P2PTransportParser::WriteTransportDescription(
110 const TransportDescription& desc,
111 const CandidateTranslator* translator,
112 buzz::XmlElement** out_elem,
113 WriteError* error) {
114 TransportProtocol proto = TransportProtocolFromDescription(&desc);
115 talk_base::scoped_ptr<buzz::XmlElement> trans_elem(
116 NewTransportElement(desc.transport_type));
117
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";
122 return false;
123 }
124
125 for (std::vector<Candidate>::const_iterator iter = desc.candidates.begin();
126 iter != desc.candidates.end(); ++iter) {
127 talk_base::scoped_ptr<buzz::XmlElement> cand_elem(
128 new buzz::XmlElement(QN_GINGLE_P2P_CANDIDATE));
129 if (!WriteCandidate(proto, *iter, translator, cand_elem.get(), error)) {
130 return false;
131 }
132 trans_elem->AddElement(cand_elem.release());
133 }
134
135 *out_elem = trans_elem.release();
136 return true;
137 }
138
ParseGingleCandidate(const buzz::XmlElement * elem,const CandidateTranslator * translator,Candidate * candidate,ParseError * error)139 bool P2PTransportParser::ParseGingleCandidate(
140 const buzz::XmlElement* elem,
141 const CandidateTranslator* translator,
142 Candidate* candidate,
143 ParseError* error) {
144 return ParseCandidate(ICEPROTO_GOOGLE, elem, translator, candidate, error);
145 }
146
WriteGingleCandidate(const Candidate & candidate,const CandidateTranslator * translator,buzz::XmlElement ** out_elem,WriteError * error)147 bool P2PTransportParser::WriteGingleCandidate(
148 const Candidate& candidate,
149 const CandidateTranslator* translator,
150 buzz::XmlElement** out_elem,
151 WriteError* error) {
152 talk_base::scoped_ptr<buzz::XmlElement> elem(
153 new buzz::XmlElement(QN_GINGLE_CANDIDATE));
154 bool ret = WriteCandidate(ICEPROTO_GOOGLE, candidate, translator, elem.get(),
155 error);
156 if (ret) {
157 *out_elem = elem.release();
158 }
159 return ret;
160 }
161
VerifyUsernameFormat(TransportProtocol proto,const std::string & username,ParseError * error)162 bool P2PTransportParser::VerifyUsernameFormat(TransportProtocol proto,
163 const std::string& username,
164 ParseError* error) {
165 if (proto == ICEPROTO_GOOGLE || proto == ICEPROTO_HYBRID) {
166 if (username.size() > kMaxGiceUsernameSize)
167 return BadParse("candidate username is too long", error);
168 if (!talk_base::Base64::IsBase64Encoded(username))
169 return BadParse("candidate username has non-base64 encoded characters",
170 error);
171 } else if (proto == ICEPROTO_RFC5245) {
172 if (username.size() > kMaxIceUsernameSize)
173 return BadParse("candidate username is too long", error);
174 }
175 return true;
176 }
177
ParseCandidate(TransportProtocol proto,const buzz::XmlElement * elem,const CandidateTranslator * translator,Candidate * candidate,ParseError * error)178 bool P2PTransportParser::ParseCandidate(TransportProtocol proto,
179 const buzz::XmlElement* elem,
180 const CandidateTranslator* translator,
181 Candidate* candidate,
182 ParseError* error) {
183 ASSERT(proto == ICEPROTO_GOOGLE);
184 ASSERT(translator != NULL);
185
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);
193 }
194
195 talk_base::SocketAddress address;
196 if (!ParseAddress(elem, QN_ADDRESS, QN_PORT, &address, error))
197 return false;
198
199 std::string channel_name = elem->Attr(buzz::QN_NAME);
200 int component = 0;
201 if (!translator ||
202 !translator->GetComponentFromChannelName(channel_name, &component)) {
203 return BadParse("candidate has unknown channel name " + channel_name,
204 error);
205 }
206
207 float preference = 0.0;
208 if (!GetXmlAttr(elem, QN_PREFERENCE, 0.0f, &preference)) {
209 return BadParse("candidate has unknown preference", error);
210 }
211
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));
224
225 if (!VerifyUsernameFormat(proto, candidate->username(), error))
226 return false;
227
228 return true;
229 }
230
WriteCandidate(TransportProtocol proto,const Candidate & candidate,const CandidateTranslator * translator,buzz::XmlElement * elem,WriteError * error)231 bool P2PTransportParser::WriteCandidate(TransportProtocol proto,
232 const Candidate& candidate,
233 const CandidateTranslator* translator,
234 buzz::XmlElement* elem,
235 WriteError* error) {
236 ASSERT(proto == ICEPROTO_GOOGLE);
237 ASSERT(translator != NULL);
238
239 std::string channel_name;
240 if (!translator ||
241 !translator->GetChannelNameFromComponent(
242 candidate.component(), &channel_name)) {
243 return BadWrite("Cannot write candidate because of unknown component.",
244 error);
245 }
246
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());
259
260 return true;
261 }
262
263 } // namespace cricket
264