2013-07-10 02:45:36 +02:00
|
|
|
/*
|
|
|
|
* libjingle
|
|
|
|
* Copyright 2012, Google Inc.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
* 3. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
|
|
|
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
|
|
|
|
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
|
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
|
|
|
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
|
|
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
|
|
|
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
|
|
|
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef TALK_P2P_BASE_TURNPORT_H_
|
|
|
|
#define TALK_P2P_BASE_TURNPORT_H_
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <list>
|
2014-08-26 23:37:49 +02:00
|
|
|
#include <set>
|
2014-08-13 19:26:08 +02:00
|
|
|
#include <string>
|
2013-07-10 02:45:36 +02:00
|
|
|
|
|
|
|
#include "talk/p2p/base/port.h"
|
|
|
|
#include "talk/p2p/client/basicportallocator.h"
|
2014-08-13 19:26:08 +02:00
|
|
|
#include "webrtc/base/asyncpacketsocket.h"
|
2013-07-10 02:45:36 +02:00
|
|
|
|
2014-07-29 19:36:52 +02:00
|
|
|
namespace rtc {
|
2013-07-10 02:45:36 +02:00
|
|
|
class AsyncResolver;
|
|
|
|
class SignalThread;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace cricket {
|
|
|
|
|
|
|
|
extern const char TURN_PORT_TYPE[];
|
|
|
|
class TurnAllocateRequest;
|
|
|
|
class TurnEntry;
|
|
|
|
|
|
|
|
class TurnPort : public Port {
|
|
|
|
public:
|
2014-07-29 19:36:52 +02:00
|
|
|
static TurnPort* Create(rtc::Thread* thread,
|
|
|
|
rtc::PacketSocketFactory* factory,
|
|
|
|
rtc::Network* network,
|
|
|
|
rtc::AsyncPacketSocket* socket,
|
2014-04-14 18:06:21 +02:00
|
|
|
const std::string& username, // ice username.
|
|
|
|
const std::string& password, // ice password.
|
|
|
|
const ProtocolAddress& server_address,
|
2014-07-17 20:23:52 +02:00
|
|
|
const RelayCredentials& credentials,
|
|
|
|
int server_priority) {
|
2014-05-14 00:58:27 +02:00
|
|
|
return new TurnPort(thread, factory, network, socket,
|
2014-07-17 20:23:52 +02:00
|
|
|
username, password, server_address,
|
|
|
|
credentials, server_priority);
|
2014-04-14 18:06:21 +02:00
|
|
|
}
|
|
|
|
|
2014-07-29 19:36:52 +02:00
|
|
|
static TurnPort* Create(rtc::Thread* thread,
|
|
|
|
rtc::PacketSocketFactory* factory,
|
|
|
|
rtc::Network* network,
|
|
|
|
const rtc::IPAddress& ip,
|
2013-07-10 02:45:36 +02:00
|
|
|
int min_port, int max_port,
|
|
|
|
const std::string& username, // ice username.
|
|
|
|
const std::string& password, // ice password.
|
|
|
|
const ProtocolAddress& server_address,
|
2014-07-17 20:23:52 +02:00
|
|
|
const RelayCredentials& credentials,
|
|
|
|
int server_priority) {
|
2013-07-10 02:45:36 +02:00
|
|
|
return new TurnPort(thread, factory, network, ip, min_port, max_port,
|
2014-07-17 20:23:52 +02:00
|
|
|
username, password, server_address, credentials,
|
|
|
|
server_priority);
|
2013-07-10 02:45:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~TurnPort();
|
|
|
|
|
|
|
|
const ProtocolAddress& server_address() const { return server_address_; }
|
|
|
|
|
|
|
|
bool connected() const { return connected_; }
|
|
|
|
const RelayCredentials& credentials() const { return credentials_; }
|
|
|
|
|
|
|
|
virtual void PrepareAddress();
|
|
|
|
virtual Connection* CreateConnection(
|
|
|
|
const Candidate& c, PortInterface::CandidateOrigin origin);
|
|
|
|
virtual int SendTo(const void* data, size_t size,
|
2014-07-29 19:36:52 +02:00
|
|
|
const rtc::SocketAddress& addr,
|
|
|
|
const rtc::PacketOptions& options,
|
2013-07-10 02:45:36 +02:00
|
|
|
bool payload);
|
2014-07-29 19:36:52 +02:00
|
|
|
virtual int SetOption(rtc::Socket::Option opt, int value);
|
|
|
|
virtual int GetOption(rtc::Socket::Option opt, int* value);
|
2013-07-10 02:45:36 +02:00
|
|
|
virtual int GetError();
|
2014-04-14 18:06:21 +02:00
|
|
|
|
|
|
|
virtual bool HandleIncomingPacket(
|
2014-07-29 19:36:52 +02:00
|
|
|
rtc::AsyncPacketSocket* socket, const char* data, size_t size,
|
|
|
|
const rtc::SocketAddress& remote_addr,
|
|
|
|
const rtc::PacketTime& packet_time) {
|
2014-04-14 18:06:21 +02:00
|
|
|
OnReadPacket(socket, data, size, remote_addr, packet_time);
|
|
|
|
return true;
|
|
|
|
}
|
2014-07-29 19:36:52 +02:00
|
|
|
virtual void OnReadPacket(rtc::AsyncPacketSocket* socket,
|
2014-04-14 18:06:21 +02:00
|
|
|
const char* data, size_t size,
|
2014-07-29 19:36:52 +02:00
|
|
|
const rtc::SocketAddress& remote_addr,
|
|
|
|
const rtc::PacketTime& packet_time);
|
2014-04-14 18:06:21 +02:00
|
|
|
|
2014-07-29 19:36:52 +02:00
|
|
|
virtual void OnReadyToSend(rtc::AsyncPacketSocket* socket);
|
2013-07-10 02:45:36 +02:00
|
|
|
|
2014-07-29 19:36:52 +02:00
|
|
|
void OnSocketConnect(rtc::AsyncPacketSocket* socket);
|
|
|
|
void OnSocketClose(rtc::AsyncPacketSocket* socket, int error);
|
2013-07-10 02:45:36 +02:00
|
|
|
|
|
|
|
|
|
|
|
const std::string& hash() const { return hash_; }
|
|
|
|
const std::string& nonce() const { return nonce_; }
|
|
|
|
|
2014-07-17 23:55:04 +02:00
|
|
|
int error() const { return error_; }
|
|
|
|
|
2014-09-04 21:11:34 +02:00
|
|
|
void OnAllocateMismatch();
|
|
|
|
|
|
|
|
rtc::AsyncPacketSocket* socket() const {
|
|
|
|
return socket_;
|
|
|
|
}
|
|
|
|
|
2014-04-14 18:06:21 +02:00
|
|
|
// Signal with resolved server address.
|
|
|
|
// Parameters are port, server address and resolved server address.
|
|
|
|
// This signal will be sent only if server address is resolved successfully.
|
|
|
|
sigslot::signal3<TurnPort*,
|
2014-07-29 19:36:52 +02:00
|
|
|
const rtc::SocketAddress&,
|
|
|
|
const rtc::SocketAddress&> SignalResolvedServerAddress;
|
2014-04-14 18:06:21 +02:00
|
|
|
|
2013-07-10 02:45:36 +02:00
|
|
|
// This signal is only for testing purpose.
|
2014-07-29 19:36:52 +02:00
|
|
|
sigslot::signal3<TurnPort*, const rtc::SocketAddress&, int>
|
2013-07-10 02:45:36 +02:00
|
|
|
SignalCreatePermissionResult;
|
|
|
|
|
|
|
|
protected:
|
2014-07-29 19:36:52 +02:00
|
|
|
TurnPort(rtc::Thread* thread,
|
|
|
|
rtc::PacketSocketFactory* factory,
|
|
|
|
rtc::Network* network,
|
|
|
|
rtc::AsyncPacketSocket* socket,
|
2014-04-14 18:06:21 +02:00
|
|
|
const std::string& username,
|
|
|
|
const std::string& password,
|
|
|
|
const ProtocolAddress& server_address,
|
2014-07-17 20:23:52 +02:00
|
|
|
const RelayCredentials& credentials,
|
|
|
|
int server_priority);
|
2014-04-14 18:06:21 +02:00
|
|
|
|
2014-07-29 19:36:52 +02:00
|
|
|
TurnPort(rtc::Thread* thread,
|
|
|
|
rtc::PacketSocketFactory* factory,
|
|
|
|
rtc::Network* network,
|
|
|
|
const rtc::IPAddress& ip,
|
2013-07-10 02:45:36 +02:00
|
|
|
int min_port, int max_port,
|
|
|
|
const std::string& username,
|
|
|
|
const std::string& password,
|
|
|
|
const ProtocolAddress& server_address,
|
2014-07-17 20:23:52 +02:00
|
|
|
const RelayCredentials& credentials,
|
|
|
|
int server_priority);
|
2013-07-10 02:45:36 +02:00
|
|
|
|
|
|
|
private:
|
2014-09-04 21:11:34 +02:00
|
|
|
enum {
|
|
|
|
MSG_ERROR = MSG_FIRST_AVAILABLE,
|
|
|
|
MSG_ALLOCATE_MISMATCH
|
|
|
|
};
|
2013-09-23 22:34:45 +02:00
|
|
|
|
2013-07-10 02:45:36 +02:00
|
|
|
typedef std::list<TurnEntry*> EntryList;
|
2014-07-29 19:36:52 +02:00
|
|
|
typedef std::map<rtc::Socket::Option, int> SocketOptionsMap;
|
2014-08-26 23:37:49 +02:00
|
|
|
typedef std::set<rtc::SocketAddress> AttemptedServerSet;
|
2013-07-10 02:45:36 +02:00
|
|
|
|
2014-07-29 19:36:52 +02:00
|
|
|
virtual void OnMessage(rtc::Message* pmsg);
|
2013-07-10 02:45:36 +02:00
|
|
|
|
2014-07-17 23:55:04 +02:00
|
|
|
bool CreateTurnClientSocket();
|
|
|
|
|
2013-07-10 02:45:36 +02:00
|
|
|
void set_nonce(const std::string& nonce) { nonce_ = nonce; }
|
|
|
|
void set_realm(const std::string& realm) {
|
|
|
|
if (realm != realm_) {
|
|
|
|
realm_ = realm;
|
|
|
|
UpdateHash();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-26 23:37:49 +02:00
|
|
|
bool SetAlternateServer(const rtc::SocketAddress& address);
|
2014-07-29 19:36:52 +02:00
|
|
|
void ResolveTurnAddress(const rtc::SocketAddress& address);
|
|
|
|
void OnResolveResult(rtc::AsyncResolverInterface* resolver);
|
2013-07-10 02:45:36 +02:00
|
|
|
|
|
|
|
void AddRequestAuthInfo(StunMessage* msg);
|
|
|
|
void OnSendStunPacket(const void* data, size_t size, StunRequest* request);
|
|
|
|
// Stun address from allocate success response.
|
|
|
|
// Currently used only for testing.
|
2014-07-29 19:36:52 +02:00
|
|
|
void OnStunAddress(const rtc::SocketAddress& address);
|
|
|
|
void OnAllocateSuccess(const rtc::SocketAddress& address,
|
|
|
|
const rtc::SocketAddress& stun_address);
|
2013-07-10 02:45:36 +02:00
|
|
|
void OnAllocateError();
|
|
|
|
void OnAllocateRequestTimeout();
|
|
|
|
|
2013-12-13 01:21:03 +01:00
|
|
|
void HandleDataIndication(const char* data, size_t size,
|
2014-07-29 19:36:52 +02:00
|
|
|
const rtc::PacketTime& packet_time);
|
2013-12-13 01:21:03 +01:00
|
|
|
void HandleChannelData(int channel_id, const char* data, size_t size,
|
2014-07-29 19:36:52 +02:00
|
|
|
const rtc::PacketTime& packet_time);
|
2013-07-10 02:45:36 +02:00
|
|
|
void DispatchPacket(const char* data, size_t size,
|
2014-07-29 19:36:52 +02:00
|
|
|
const rtc::SocketAddress& remote_addr,
|
|
|
|
ProtocolType proto, const rtc::PacketTime& packet_time);
|
2013-07-10 02:45:36 +02:00
|
|
|
|
|
|
|
bool ScheduleRefresh(int lifetime);
|
|
|
|
void SendRequest(StunRequest* request, int delay);
|
2014-02-14 01:56:12 +01:00
|
|
|
int Send(const void* data, size_t size,
|
2014-07-29 19:36:52 +02:00
|
|
|
const rtc::PacketOptions& options);
|
2013-07-10 02:45:36 +02:00
|
|
|
void UpdateHash();
|
|
|
|
bool UpdateNonce(StunMessage* response);
|
|
|
|
|
2014-07-29 19:36:52 +02:00
|
|
|
bool HasPermission(const rtc::IPAddress& ipaddr) const;
|
|
|
|
TurnEntry* FindEntry(const rtc::SocketAddress& address) const;
|
2013-07-10 02:45:36 +02:00
|
|
|
TurnEntry* FindEntry(int channel_id) const;
|
2014-07-29 19:36:52 +02:00
|
|
|
TurnEntry* CreateEntry(const rtc::SocketAddress& address);
|
|
|
|
void DestroyEntry(const rtc::SocketAddress& address);
|
2013-07-10 02:45:36 +02:00
|
|
|
void OnConnectionDestroyed(Connection* conn);
|
|
|
|
|
|
|
|
ProtocolAddress server_address_;
|
|
|
|
RelayCredentials credentials_;
|
2014-08-26 23:37:49 +02:00
|
|
|
AttemptedServerSet attempted_server_addresses_;
|
2013-07-10 02:45:36 +02:00
|
|
|
|
2014-07-29 19:36:52 +02:00
|
|
|
rtc::AsyncPacketSocket* socket_;
|
2013-07-10 02:45:36 +02:00
|
|
|
SocketOptionsMap socket_options_;
|
2014-07-29 19:36:52 +02:00
|
|
|
rtc::AsyncResolverInterface* resolver_;
|
2013-07-10 02:45:36 +02:00
|
|
|
int error_;
|
|
|
|
|
|
|
|
StunRequestManager request_manager_;
|
|
|
|
std::string realm_; // From 401/438 response message.
|
|
|
|
std::string nonce_; // From 401/438 response message.
|
|
|
|
std::string hash_; // Digest of username:realm:password
|
|
|
|
|
|
|
|
int next_channel_number_;
|
|
|
|
EntryList entries_;
|
|
|
|
|
|
|
|
bool connected_;
|
2014-07-17 20:23:52 +02:00
|
|
|
// By default the value will be set to 0. This value will be used in
|
|
|
|
// calculating the candidate priority.
|
|
|
|
int server_priority_;
|
2013-07-10 02:45:36 +02:00
|
|
|
|
2014-09-04 21:11:34 +02:00
|
|
|
// The number of retries made due to allocate mismatch error.
|
|
|
|
size_t allocate_mismatch_retries_;
|
|
|
|
|
2013-07-10 02:45:36 +02:00
|
|
|
friend class TurnEntry;
|
|
|
|
friend class TurnAllocateRequest;
|
|
|
|
friend class TurnRefreshRequest;
|
|
|
|
friend class TurnCreatePermissionRequest;
|
|
|
|
friend class TurnChannelBindRequest;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace cricket
|
|
|
|
|
|
|
|
#endif // TALK_P2P_BASE_TURNPORT_H_
|