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 <string>
29 #include <vector>
30 #include "talk/p2p/base/rawtransport.h"
31 #include "talk/base/common.h"
32 #include "talk/p2p/base/constants.h"
33 #include "talk/p2p/base/parsing.h"
34 #include "talk/p2p/base/sessionmanager.h"
35 #include "talk/p2p/base/rawtransportchannel.h"
36 #include "talk/xmllite/qname.h"
37 #include "talk/xmllite/xmlelement.h"
38 #include "talk/xmpp/constants.h"
39
40 #if defined(FEATURE_ENABLE_PSTN)
41 namespace cricket {
42
RawTransport(talk_base::Thread * signaling_thread,talk_base::Thread * worker_thread,PortAllocator * allocator)43 RawTransport::RawTransport(talk_base::Thread* signaling_thread,
44 talk_base::Thread* worker_thread,
45 PortAllocator* allocator)
46 : Transport(signaling_thread, worker_thread,
47 NS_GINGLE_RAW, allocator) {
48 }
49
~RawTransport()50 RawTransport::~RawTransport() {
51 DestroyAllChannels();
52 }
53
ParseCandidates(SignalingProtocol protocol,const buzz::XmlElement * elem,Candidates * candidates,ParseError * error)54 bool RawTransport::ParseCandidates(SignalingProtocol protocol,
55 const buzz::XmlElement* elem,
56 Candidates* candidates,
57 ParseError* error) {
58 ASSERT(elem->FirstChild() == NULL);
59 for (const buzz::XmlElement* cand_elem = elem->FirstElement();
60 cand_elem != NULL;
61 cand_elem = cand_elem->NextElement()) {
62 if (cand_elem->Name() == QN_GINGLE_RAW_CHANNEL) {
63 talk_base::SocketAddress addr;
64 if (!ParseRawAddress(cand_elem, &addr, error))
65 return false;
66
67 Candidate candidate;
68 candidate.set_name(cand_elem->Attr(buzz::QN_NAME));
69 candidate.set_address(addr);
70 candidates->push_back(candidate);
71 }
72 }
73 return true;
74 }
75
WriteCandidates(SignalingProtocol protocol,const Candidates & candidates,XmlElements * candidate_elems,WriteError * error)76 bool RawTransport::WriteCandidates(SignalingProtocol protocol,
77 const Candidates& candidates,
78 XmlElements* candidate_elems,
79 WriteError* error) {
80 for (std::vector<Candidate>::const_iterator
81 cand = candidates.begin();
82 cand != candidates.end();
83 ++cand) {
84 ASSERT(cand->protocol() == "udp");
85 talk_base::SocketAddress addr = cand->address();
86
87 buzz::XmlElement* elem = new buzz::XmlElement(QN_GINGLE_RAW_CHANNEL);
88 elem->SetAttr(buzz::QN_NAME, type());
89 elem->SetAttr(QN_ADDRESS, addr.IPAsString());
90 elem->SetAttr(QN_PORT, addr.PortAsString());
91 candidate_elems->push_back(elem);
92 }
93 return true;
94 }
95
ParseRawAddress(const buzz::XmlElement * elem,talk_base::SocketAddress * addr,ParseError * error)96 bool RawTransport::ParseRawAddress(const buzz::XmlElement* elem,
97 talk_base::SocketAddress* addr,
98 ParseError* error) {
99 // Make sure the required attributes exist
100 if (!elem->HasAttr(buzz::QN_NAME) ||
101 !elem->HasAttr(QN_ADDRESS) ||
102 !elem->HasAttr(QN_PORT)) {
103 return BadParse("channel missing required attribute", error);
104 }
105
106 // Make sure the channel named actually exists.
107 if (!HasChannel(elem->Attr(buzz::QN_NAME)))
108 return BadParse("channel named does not exist", error);
109
110 // Parse the address.
111 if (!ParseAddress(elem, QN_ADDRESS, QN_PORT, addr, error))
112 return false;
113
114 return true;
115 }
116
CreateTransportChannel(const std::string & name,const std::string & content_type)117 TransportChannelImpl* RawTransport::CreateTransportChannel(
118 const std::string& name, const std::string& content_type) {
119 return new RawTransportChannel(name, content_type, this,
120 worker_thread(),
121 port_allocator());
122 }
123
DestroyTransportChannel(TransportChannelImpl * channel)124 void RawTransport::DestroyTransportChannel(TransportChannelImpl* channel) {
125 delete channel;
126 }
127
128 } // namespace cricket
129 #endif // defined(FEATURE_ENABLE_PSTN)
130