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

« back to all changes in this revision

Viewing changes to protocols/jabber/libjingle/talk/p2p/client/basicportallocator.h

  • 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
#ifndef TALK_P2P_CLIENT_BASICPORTALLOCATOR_H_
 
29
#define TALK_P2P_CLIENT_BASICPORTALLOCATOR_H_
 
30
 
 
31
#include <string>
 
32
#include <vector>
 
33
 
 
34
#include "talk/base/messagequeue.h"
 
35
#include "talk/base/network.h"
 
36
#include "talk/base/scoped_ptr.h"
 
37
#include "talk/base/thread.h"
 
38
#include "talk/p2p/base/portallocator.h"
 
39
 
 
40
namespace cricket {
 
41
 
 
42
class BasicPortAllocator : public PortAllocator {
 
43
 public:
 
44
  BasicPortAllocator(talk_base::NetworkManager* network_manager,
 
45
                     talk_base::PacketSocketFactory* socket_factory);
 
46
  explicit BasicPortAllocator(talk_base::NetworkManager* network_manager);
 
47
  BasicPortAllocator(talk_base::NetworkManager* network_manager,
 
48
                     const talk_base::SocketAddress& stun_server,
 
49
                     const talk_base::SocketAddress& relay_server_udp,
 
50
                     const talk_base::SocketAddress& relay_server_tcp,
 
51
                     const talk_base::SocketAddress& relay_server_ssl);
 
52
  virtual ~BasicPortAllocator();
 
53
 
 
54
  talk_base::NetworkManager* network_manager() { return network_manager_; }
 
55
 
 
56
  // If socket_factory() is set to NULL each PortAllocatorSession
 
57
  // creates its own socket factory.
 
58
  talk_base::PacketSocketFactory* socket_factory() { return socket_factory_; }
 
59
 
 
60
  const talk_base::SocketAddress& stun_address() const {
 
61
    return stun_address_;
 
62
  }
 
63
  const talk_base::SocketAddress& relay_address_udp() const {
 
64
    return relay_address_udp_;
 
65
  }
 
66
  const talk_base::SocketAddress& relay_address_tcp() const {
 
67
    return relay_address_tcp_;
 
68
  }
 
69
  const talk_base::SocketAddress& relay_address_ssl() const {
 
70
    return relay_address_ssl_;
 
71
  }
 
72
 
 
73
  // Returns the best (highest preference) phase that has produced a port that
 
74
  // produced a writable connection.  If no writable connections have been
 
75
  // produced, this returns -1.
 
76
  int best_writable_phase() const;
 
77
 
 
78
  virtual PortAllocatorSession* CreateSession(const std::string& name,
 
79
                                              const std::string& session_type);
 
80
 
 
81
  // Called whenever a connection becomes writable with the argument being the
 
82
  // phase that the corresponding port was created in.
 
83
  void AddWritablePhase(int phase);
 
84
 
 
85
  bool allow_tcp_listen() const {
 
86
    return allow_tcp_listen_;
 
87
  }
 
88
  void set_allow_tcp_listen(bool allow_tcp_listen) {
 
89
    allow_tcp_listen_ = allow_tcp_listen;
 
90
  }
 
91
 
 
92
 private:
 
93
  void Construct();
 
94
 
 
95
  talk_base::NetworkManager* network_manager_;
 
96
  talk_base::PacketSocketFactory* socket_factory_;
 
97
  const talk_base::SocketAddress stun_address_;
 
98
  const talk_base::SocketAddress relay_address_udp_;
 
99
  const talk_base::SocketAddress relay_address_tcp_;
 
100
  const talk_base::SocketAddress relay_address_ssl_;
 
101
  int best_writable_phase_;
 
102
  bool allow_tcp_listen_;
 
103
};
 
104
 
 
105
struct PortConfiguration;
 
106
class AllocationSequence;
 
107
 
 
108
class BasicPortAllocatorSession : public PortAllocatorSession,
 
109
                                  public talk_base::MessageHandler {
 
110
 public:
 
111
  BasicPortAllocatorSession(BasicPortAllocator* allocator,
 
112
                            const std::string& name,
 
113
                            const std::string& session_type);
 
114
  ~BasicPortAllocatorSession();
 
115
 
 
116
  virtual BasicPortAllocator* allocator() { return allocator_; }
 
117
  talk_base::Thread* network_thread() { return network_thread_; }
 
118
  talk_base::PacketSocketFactory* socket_factory() { return socket_factory_; }
 
119
 
 
120
  virtual void GetInitialPorts();
 
121
  virtual void StartGetAllPorts();
 
122
  virtual void StopGetAllPorts();
 
123
  virtual bool IsGettingAllPorts() { return running_; }
 
124
 
 
125
 protected:
 
126
  // Starts the process of getting the port configurations.
 
127
  virtual void GetPortConfigurations();
 
128
 
 
129
  // Adds a port configuration that is now ready.  Once we have one for each
 
130
  // network (or a timeout occurs), we will start allocating ports.
 
131
  virtual void ConfigReady(PortConfiguration* config);
 
132
 
 
133
  // MessageHandler.  Can be overriden if message IDs do not conflict.
 
134
  virtual void OnMessage(talk_base::Message *message);
 
135
 
 
136
 private:
 
137
  void OnConfigReady(PortConfiguration* config);
 
138
  void OnConfigTimeout();
 
139
  void AllocatePorts();
 
140
  void OnAllocate();
 
141
  void DoAllocate();
 
142
  void OnNetworksChanged();
 
143
  void OnAllocationSequenceObjectsCreated();
 
144
  void DisableEquivalentPhases(talk_base::Network* network,
 
145
      PortConfiguration* config, uint32* flags);
 
146
  void AddAllocatedPort(Port* port, AllocationSequence* seq, float pref,
 
147
      bool prepare_address = true);
 
148
  void OnAddressReady(Port* port);
 
149
  void OnProtocolEnabled(AllocationSequence* seq, ProtocolType proto);
 
150
  void OnPortDestroyed(Port* port);
 
151
  void OnAddressError(Port* port);
 
152
  void OnConnectionCreated(Port* port, Connection* conn);
 
153
  void OnConnectionStateChange(Connection* conn);
 
154
  void OnShake();
 
155
  void MaybeSignalCandidatesAllocationDone();
 
156
  void OnPortAllocationComplete(AllocationSequence* seq);
 
157
 
 
158
  BasicPortAllocator* allocator_;
 
159
  talk_base::Thread* network_thread_;
 
160
  talk_base::scoped_ptr<talk_base::PacketSocketFactory> owned_socket_factory_;
 
161
  talk_base::PacketSocketFactory* socket_factory_;
 
162
  bool configuration_done_;
 
163
  bool allocation_started_;
 
164
  bool network_manager_started_;
 
165
  bool running_;  // set when StartGetAllPorts is called
 
166
  bool allocation_sequences_created_;
 
167
  std::vector<PortConfiguration*> configs_;
 
168
  std::vector<AllocationSequence*> sequences_;
 
169
 
 
170
  struct PortData {
 
171
    Port* port;
 
172
    AllocationSequence* sequence;
 
173
    bool ready;
 
174
 
 
175
    bool operator==(Port* rhs) const { return (port == rhs); }
 
176
  };
 
177
  std::vector<PortData> ports_;
 
178
 
 
179
  friend class AllocationSequence;
 
180
};
 
181
 
 
182
// Records configuration information useful in creating ports.
 
183
struct PortConfiguration : public talk_base::MessageData {
 
184
  talk_base::SocketAddress stun_address;
 
185
  std::string username;
 
186
  std::string password;
 
187
  std::string magic_cookie;
 
188
 
 
189
  typedef std::vector<ProtocolAddress> PortList;
 
190
  struct RelayServer {
 
191
    PortList ports;
 
192
    float pref_modifier;  // added to the protocol modifier to get the
 
193
                          // preference for this particular server
 
194
  };
 
195
 
 
196
  typedef std::vector<RelayServer> RelayList;
 
197
  RelayList relays;
 
198
 
 
199
  PortConfiguration(const talk_base::SocketAddress& stun_address,
 
200
                    const std::string& username,
 
201
                    const std::string& password,
 
202
                    const std::string& magic_cookie);
 
203
 
 
204
  // Adds another relay server, with the given ports and modifier, to the list.
 
205
  void AddRelay(const PortList& ports, float pref_modifier);
 
206
 
 
207
  bool ResolveStunAddress();
 
208
 
 
209
  // Determines whether the given relay server supports the given protocol.
 
210
  static bool SupportsProtocol(const PortConfiguration::RelayServer& relay,
 
211
                               ProtocolType type);
 
212
};
 
213
 
 
214
}  // namespace cricket
 
215
 
 
216
#endif  // TALK_P2P_CLIENT_BASICPORTALLOCATOR_H_