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
#ifndef TALK_P2P_CLIENT_BASICPORTALLOCATOR_H_
29
#define TALK_P2P_CLIENT_BASICPORTALLOCATOR_H_
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"
42
class BasicPortAllocator : public PortAllocator {
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();
54
talk_base::NetworkManager* network_manager() { return network_manager_; }
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_; }
60
const talk_base::SocketAddress& stun_address() const {
63
const talk_base::SocketAddress& relay_address_udp() const {
64
return relay_address_udp_;
66
const talk_base::SocketAddress& relay_address_tcp() const {
67
return relay_address_tcp_;
69
const talk_base::SocketAddress& relay_address_ssl() const {
70
return relay_address_ssl_;
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;
78
virtual PortAllocatorSession* CreateSession(const std::string& name,
79
const std::string& session_type);
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);
85
bool allow_tcp_listen() const {
86
return allow_tcp_listen_;
88
void set_allow_tcp_listen(bool allow_tcp_listen) {
89
allow_tcp_listen_ = allow_tcp_listen;
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_;
105
struct PortConfiguration;
106
class AllocationSequence;
108
class BasicPortAllocatorSession : public PortAllocatorSession,
109
public talk_base::MessageHandler {
111
BasicPortAllocatorSession(BasicPortAllocator* allocator,
112
const std::string& name,
113
const std::string& session_type);
114
~BasicPortAllocatorSession();
116
virtual BasicPortAllocator* allocator() { return allocator_; }
117
talk_base::Thread* network_thread() { return network_thread_; }
118
talk_base::PacketSocketFactory* socket_factory() { return socket_factory_; }
120
virtual void GetInitialPorts();
121
virtual void StartGetAllPorts();
122
virtual void StopGetAllPorts();
123
virtual bool IsGettingAllPorts() { return running_; }
126
// Starts the process of getting the port configurations.
127
virtual void GetPortConfigurations();
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);
133
// MessageHandler. Can be overriden if message IDs do not conflict.
134
virtual void OnMessage(talk_base::Message *message);
137
void OnConfigReady(PortConfiguration* config);
138
void OnConfigTimeout();
139
void AllocatePorts();
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);
155
void MaybeSignalCandidatesAllocationDone();
156
void OnPortAllocationComplete(AllocationSequence* seq);
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_;
172
AllocationSequence* sequence;
175
bool operator==(Port* rhs) const { return (port == rhs); }
177
std::vector<PortData> ports_;
179
friend class AllocationSequence;
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;
189
typedef std::vector<ProtocolAddress> PortList;
192
float pref_modifier; // added to the protocol modifier to get the
193
// preference for this particular server
196
typedef std::vector<RelayServer> RelayList;
199
PortConfiguration(const talk_base::SocketAddress& stun_address,
200
const std::string& username,
201
const std::string& password,
202
const std::string& magic_cookie);
204
// Adds another relay server, with the given ports and modifier, to the list.
205
void AddRelay(const PortList& ports, float pref_modifier);
207
bool ResolveStunAddress();
209
// Determines whether the given relay server supports the given protocol.
210
static bool SupportsProtocol(const PortConfiguration::RelayServer& relay,
214
} // namespace cricket
216
#endif // TALK_P2P_CLIENT_BASICPORTALLOCATOR_H_