~ubuntu-branches/debian/experimental/kopete/experimental

« back to all changes in this revision

Viewing changes to protocols/jabber/libjingle/talk/p2p/base/p2ptransport.cc

  • Committer: Package Import Robot
  • Author(s): Maximiliano Curia
  • Date: 2015-02-24 11:32:57 UTC
  • mfrom: (1.1.41 vivid)
  • Revision ID: package-import@ubuntu.com-20150224113257-gnupg4v7lzz18ij0
Tags: 4:14.12.2-1
* New upstream release (14.12.2).
* Bump Standards-Version to 3.9.6, no changes needed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
// We only allow usernames to be this many characters or fewer.
 
49
const size_t kMaxUsernameSize = 16;
 
50
 
 
51
}  // namespace
 
52
 
 
53
namespace cricket {
 
54
 
 
55
P2PTransport::P2PTransport(talk_base::Thread* signaling_thread,
 
56
                           talk_base::Thread* worker_thread,
 
57
                           PortAllocator* allocator)
 
58
    : Transport(signaling_thread, worker_thread,
 
59
                NS_GINGLE_P2P, allocator) {
 
60
}
 
61
 
 
62
P2PTransport::~P2PTransport() {
 
63
  DestroyAllChannels();
 
64
}
 
65
 
 
66
void P2PTransport::OnTransportError(const buzz::XmlElement* error) {
 
67
  // Need to know if it was <unknown-channel name="xxx">.
 
68
  ASSERT(error->Name().Namespace() == type());
 
69
  if ((error->Name() == QN_GINGLE_P2P_UNKNOWN_CHANNEL_NAME)
 
70
      && error->HasAttr(buzz::QN_NAME)) {
 
71
    std::string channel_name = error->Attr(buzz::QN_NAME);
 
72
    if (HasChannel(channel_name)) {
 
73
      SignalChannelGone(this, channel_name);
 
74
    }
 
75
  }
 
76
}
 
77
 
 
78
 
 
79
bool P2PTransportParser::ParseCandidates(SignalingProtocol protocol,
 
80
                                         const buzz::XmlElement* elem,
 
81
                                         Candidates* candidates,
 
82
                                         ParseError* error) {
 
83
  // TODO: Once we implement standard ICE-UDP, parse the
 
84
  // candidates according to XEP-176.
 
85
  for (const buzz::XmlElement* candidate_elem = elem->FirstElement();
 
86
       candidate_elem != NULL;
 
87
       candidate_elem = candidate_elem->NextElement()) {
 
88
    // Only look at local part because it might be <session><candidate>
 
89
    //                                          or <tranport><candidate>.
 
90
    if (candidate_elem->Name().LocalPart() == LN_CANDIDATE) {
 
91
      Candidate candidate;
 
92
      if (!ParseCandidate(candidate_elem, &candidate, error))
 
93
        return false;
 
94
      candidates->push_back(candidate);
 
95
    }
 
96
  }
 
97
  return true;
 
98
}
 
99
 
 
100
bool P2PTransportParser::ParseCandidate(const buzz::XmlElement* elem,
 
101
                                        Candidate* candidate,
 
102
                                        ParseError* error) {
 
103
  if (!elem->HasAttr(buzz::QN_NAME) ||
 
104
      !elem->HasAttr(QN_ADDRESS) ||
 
105
      !elem->HasAttr(QN_PORT) ||
 
106
      !elem->HasAttr(QN_USERNAME) ||
 
107
      !elem->HasAttr(QN_PREFERENCE) ||
 
108
      !elem->HasAttr(QN_PROTOCOL) ||
 
109
      !elem->HasAttr(QN_GENERATION)) {
 
110
    return BadParse("candidate missing required attribute", error);
 
111
  }
 
112
 
 
113
  talk_base::SocketAddress address;
 
114
  if (!ParseAddress(elem, QN_ADDRESS, QN_PORT, &address, error))
 
115
    return false;
 
116
 
 
117
  candidate->set_name(elem->Attr(buzz::QN_NAME));
 
118
  candidate->set_address(address);
 
119
  candidate->set_username(elem->Attr(QN_USERNAME));
 
120
  candidate->set_preference_str(elem->Attr(QN_PREFERENCE));
 
121
  candidate->set_protocol(elem->Attr(QN_PROTOCOL));
 
122
  candidate->set_generation_str(elem->Attr(QN_GENERATION));
 
123
  if (elem->HasAttr(QN_PASSWORD))
 
124
    candidate->set_password(elem->Attr(QN_PASSWORD));
 
125
  if (elem->HasAttr(buzz::QN_TYPE))
 
126
    candidate->set_type(elem->Attr(buzz::QN_TYPE));
 
127
  if (elem->HasAttr(QN_NETWORK))
 
128
    candidate->set_network_name(elem->Attr(QN_NETWORK));
 
129
 
 
130
  if (!VerifyUsernameFormat(candidate->username(), error))
 
131
    return false;
 
132
 
 
133
  return true;
 
134
}
 
135
 
 
136
bool P2PTransportParser::VerifyUsernameFormat(const std::string& username,
 
137
                                              ParseError* error) {
 
138
  if (username.size() > kMaxUsernameSize)
 
139
    return BadParse("candidate username is too long", error);
 
140
  if (!talk_base::Base64::IsBase64Encoded(username))
 
141
    return BadParse(
 
142
        "candidate username has non-base64 encoded characters", error);
 
143
  return true;
 
144
}
 
145
 
 
146
static const buzz::StaticQName& GetCandidateQName(SignalingProtocol protocol) {
 
147
  if (protocol == PROTOCOL_GINGLE) {
 
148
    return QN_GINGLE_CANDIDATE;
 
149
  } else {
 
150
    // TODO: Once we implement standard ICE-UDP, use the
 
151
    // XEP-176 namespace.
 
152
    return QN_GINGLE_P2P_CANDIDATE;
 
153
  }
 
154
}
 
155
 
 
156
bool P2PTransportParser::WriteCandidates(SignalingProtocol protocol,
 
157
                                         const Candidates& candidates,
 
158
                                         XmlElements* candidate_elems,
 
159
                                         WriteError* error) {
 
160
  // TODO: Once we implement standard ICE-UDP, parse the
 
161
  // candidates according to XEP-176.
 
162
  for (std::vector<Candidate>::const_iterator iter = candidates.begin();
 
163
       iter != candidates.end(); ++iter) {
 
164
    buzz::XmlElement* cand_elem =
 
165
        new buzz::XmlElement(GetCandidateQName(protocol));
 
166
    if (!WriteCandidate(*iter, cand_elem, error))
 
167
      return false;
 
168
    candidate_elems->push_back(cand_elem);
 
169
  }
 
170
  return true;
 
171
}
 
172
 
 
173
bool P2PTransportParser::WriteCandidate(const Candidate& candidate,
 
174
                                        buzz::XmlElement* elem,
 
175
                                        WriteError* error) {
 
176
  elem->SetAttr(buzz::QN_NAME, candidate.name());
 
177
  elem->SetAttr(QN_ADDRESS, candidate.address().ipaddr().ToString());
 
178
  elem->SetAttr(QN_PORT, candidate.address().PortAsString());
 
179
  elem->SetAttr(QN_PREFERENCE, candidate.preference_str());
 
180
  elem->SetAttr(QN_USERNAME, candidate.username());
 
181
  elem->SetAttr(QN_PROTOCOL, candidate.protocol());
 
182
  elem->SetAttr(QN_GENERATION, candidate.generation_str());
 
183
  if (candidate.password().size() > 0)
 
184
    elem->SetAttr(QN_PASSWORD, candidate.password());
 
185
  if (candidate.type().size() > 0)
 
186
    elem->SetAttr(buzz::QN_TYPE, candidate.type());
 
187
  if (candidate.network_name().size() > 0)
 
188
    elem->SetAttr(QN_NETWORK, candidate.network_name());
 
189
  return true;
 
190
}
 
191
 
 
192
TransportChannelImpl* P2PTransport::CreateTransportChannel(
 
193
    const std::string& name, const std::string& content_type) {
 
194
  return new P2PTransportChannel(name, content_type, this, port_allocator());
 
195
}
 
196
 
 
197
void P2PTransport::DestroyTransportChannel(TransportChannelImpl* channel) {
 
198
  delete channel;
 
199
}
 
200
 
 
201
}  // namespace cricket