2013-07-10 02:45:36 +02:00
|
|
|
/*
|
|
|
|
* libjingle
|
|
|
|
* Copyright 2004--2005, Google Inc.
|
|
|
|
*
|
2013-07-26 21:17:59 +02:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
2013-07-10 02:45:36 +02:00
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
2013-07-26 21:17:59 +02:00
|
|
|
* 1. Redistributions of source code must retain the above copyright notice,
|
2013-07-10 02:45:36 +02:00
|
|
|
* 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.
|
2013-07-26 21:17:59 +02:00
|
|
|
* 3. The name of the author may not be used to endorse or promote products
|
2013-07-10 02:45:36 +02:00
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
2013-07-26 21:17:59 +02:00
|
|
|
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
2013-07-10 02:45:36 +02:00
|
|
|
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
|
2013-07-26 21:17:59 +02:00
|
|
|
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
2013-07-10 02:45:36 +02:00
|
|
|
* 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,
|
2013-07-26 21:17:59 +02:00
|
|
|
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
|
|
|
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
2013-07-10 02:45:36 +02:00
|
|
|
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2014-10-18 00:03:39 +02:00
|
|
|
#ifndef TALK_P2P_BASE_RELAYSERVER_H_
|
|
|
|
#define TALK_P2P_BASE_RELAYSERVER_H_
|
2013-07-10 02:45:36 +02:00
|
|
|
|
2014-08-13 19:26:08 +02:00
|
|
|
#include <map>
|
2013-07-10 02:45:36 +02:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2014-10-18 00:03:39 +02:00
|
|
|
#include "talk/p2p/base/port.h"
|
|
|
|
#include "talk/p2p/base/stun.h"
|
2014-07-29 19:36:52 +02:00
|
|
|
#include "webrtc/base/asyncudpsocket.h"
|
|
|
|
#include "webrtc/base/socketaddresspair.h"
|
|
|
|
#include "webrtc/base/thread.h"
|
|
|
|
#include "webrtc/base/timeutils.h"
|
2013-07-10 02:45:36 +02:00
|
|
|
|
|
|
|
namespace cricket {
|
|
|
|
|
|
|
|
class RelayServerBinding;
|
|
|
|
class RelayServerConnection;
|
|
|
|
|
|
|
|
// Relays traffic between connections to the server that are "bound" together.
|
|
|
|
// All connections created with the same username/password are bound together.
|
2014-07-29 19:36:52 +02:00
|
|
|
class RelayServer : public rtc::MessageHandler,
|
2013-07-10 02:45:36 +02:00
|
|
|
public sigslot::has_slots<> {
|
|
|
|
public:
|
|
|
|
// Creates a server, which will use this thread to post messages to itself.
|
2014-07-29 19:36:52 +02:00
|
|
|
explicit RelayServer(rtc::Thread* thread);
|
2013-07-10 02:45:36 +02:00
|
|
|
~RelayServer();
|
|
|
|
|
2014-07-29 19:36:52 +02:00
|
|
|
rtc::Thread* thread() { return thread_; }
|
2013-07-10 02:45:36 +02:00
|
|
|
|
|
|
|
// Indicates whether we will print updates of the number of bindings.
|
|
|
|
bool log_bindings() const { return log_bindings_; }
|
|
|
|
void set_log_bindings(bool log_bindings) { log_bindings_ = log_bindings; }
|
|
|
|
|
|
|
|
// Updates the set of sockets that the server uses to talk to "internal"
|
|
|
|
// clients. These are clients that do the "port allocations".
|
2014-07-29 19:36:52 +02:00
|
|
|
void AddInternalSocket(rtc::AsyncPacketSocket* socket);
|
|
|
|
void RemoveInternalSocket(rtc::AsyncPacketSocket* socket);
|
2013-07-10 02:45:36 +02:00
|
|
|
|
|
|
|
// Updates the set of sockets that the server uses to talk to "external"
|
|
|
|
// clients. These are the clients that do not do allocations. They do not
|
|
|
|
// know that these addresses represent a relay server.
|
2014-07-29 19:36:52 +02:00
|
|
|
void AddExternalSocket(rtc::AsyncPacketSocket* socket);
|
|
|
|
void RemoveExternalSocket(rtc::AsyncPacketSocket* socket);
|
2013-07-10 02:45:36 +02:00
|
|
|
|
|
|
|
// Starts listening for connections on this sockets. When someone
|
|
|
|
// tries to connect, the connection will be accepted and a new
|
|
|
|
// internal socket will be added.
|
2014-07-29 19:36:52 +02:00
|
|
|
void AddInternalServerSocket(rtc::AsyncSocket* socket,
|
2013-07-10 02:45:36 +02:00
|
|
|
cricket::ProtocolType proto);
|
|
|
|
|
|
|
|
// Removes this server socket from the list.
|
2014-07-29 19:36:52 +02:00
|
|
|
void RemoveInternalServerSocket(rtc::AsyncSocket* socket);
|
2013-07-10 02:45:36 +02:00
|
|
|
|
|
|
|
// Methods for testing and debuging.
|
|
|
|
int GetConnectionCount() const;
|
2014-07-29 19:36:52 +02:00
|
|
|
rtc::SocketAddressPair GetConnection(int connection) const;
|
|
|
|
bool HasConnection(const rtc::SocketAddress& address) const;
|
2013-07-10 02:45:36 +02:00
|
|
|
|
|
|
|
private:
|
2014-07-29 19:36:52 +02:00
|
|
|
typedef std::vector<rtc::AsyncPacketSocket*> SocketList;
|
|
|
|
typedef std::map<rtc::AsyncSocket*,
|
2013-07-10 02:45:36 +02:00
|
|
|
cricket::ProtocolType> ServerSocketMap;
|
|
|
|
typedef std::map<std::string, RelayServerBinding*> BindingMap;
|
2014-07-29 19:36:52 +02:00
|
|
|
typedef std::map<rtc::SocketAddressPair,
|
2013-07-10 02:45:36 +02:00
|
|
|
RelayServerConnection*> ConnectionMap;
|
|
|
|
|
2014-07-29 19:36:52 +02:00
|
|
|
rtc::Thread* thread_;
|
2013-07-10 02:45:36 +02:00
|
|
|
bool log_bindings_;
|
|
|
|
SocketList internal_sockets_;
|
|
|
|
SocketList external_sockets_;
|
2013-07-26 21:17:59 +02:00
|
|
|
SocketList removed_sockets_;
|
2013-07-10 02:45:36 +02:00
|
|
|
ServerSocketMap server_sockets_;
|
|
|
|
BindingMap bindings_;
|
|
|
|
ConnectionMap connections_;
|
|
|
|
|
|
|
|
// Called when a packet is received by the server on one of its sockets.
|
2014-07-29 19:36:52 +02:00
|
|
|
void OnInternalPacket(rtc::AsyncPacketSocket* socket,
|
2013-07-10 02:45:36 +02:00
|
|
|
const char* bytes, size_t size,
|
2014-07-29 19:36:52 +02:00
|
|
|
const rtc::SocketAddress& remote_addr,
|
|
|
|
const rtc::PacketTime& packet_time);
|
|
|
|
void OnExternalPacket(rtc::AsyncPacketSocket* socket,
|
2013-07-10 02:45:36 +02:00
|
|
|
const char* bytes, size_t size,
|
2014-07-29 19:36:52 +02:00
|
|
|
const rtc::SocketAddress& remote_addr,
|
|
|
|
const rtc::PacketTime& packet_time);
|
2013-07-10 02:45:36 +02:00
|
|
|
|
2014-07-29 19:36:52 +02:00
|
|
|
void OnReadEvent(rtc::AsyncSocket* socket);
|
2013-07-10 02:45:36 +02:00
|
|
|
|
|
|
|
// Processes the relevant STUN request types from the client.
|
|
|
|
bool HandleStun(const char* bytes, size_t size,
|
2014-07-29 19:36:52 +02:00
|
|
|
const rtc::SocketAddress& remote_addr,
|
|
|
|
rtc::AsyncPacketSocket* socket,
|
2013-07-10 02:45:36 +02:00
|
|
|
std::string* username, StunMessage* msg);
|
|
|
|
void HandleStunAllocate(const char* bytes, size_t size,
|
2014-07-29 19:36:52 +02:00
|
|
|
const rtc::SocketAddressPair& ap,
|
|
|
|
rtc::AsyncPacketSocket* socket);
|
2013-07-10 02:45:36 +02:00
|
|
|
void HandleStun(RelayServerConnection* int_conn, const char* bytes,
|
|
|
|
size_t size);
|
|
|
|
void HandleStunAllocate(RelayServerConnection* int_conn,
|
|
|
|
const StunMessage& msg);
|
|
|
|
void HandleStunSend(RelayServerConnection* int_conn, const StunMessage& msg);
|
|
|
|
|
|
|
|
// Adds/Removes the a connection or binding.
|
|
|
|
void AddConnection(RelayServerConnection* conn);
|
|
|
|
void RemoveConnection(RelayServerConnection* conn);
|
|
|
|
void RemoveBinding(RelayServerBinding* binding);
|
|
|
|
|
|
|
|
// Handle messages in our worker thread.
|
2014-07-29 19:36:52 +02:00
|
|
|
void OnMessage(rtc::Message *pmsg);
|
2013-07-10 02:45:36 +02:00
|
|
|
|
|
|
|
// Called when the timer for checking lifetime times out.
|
|
|
|
void OnTimeout(RelayServerBinding* binding);
|
|
|
|
|
|
|
|
// Accept connections on this server socket.
|
2014-07-29 19:36:52 +02:00
|
|
|
void AcceptConnection(rtc::AsyncSocket* server_socket);
|
2013-07-10 02:45:36 +02:00
|
|
|
|
|
|
|
friend class RelayServerConnection;
|
|
|
|
friend class RelayServerBinding;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Maintains information about a connection to the server. Each connection is
|
|
|
|
// part of one and only one binding.
|
|
|
|
class RelayServerConnection {
|
|
|
|
public:
|
|
|
|
RelayServerConnection(RelayServerBinding* binding,
|
2014-07-29 19:36:52 +02:00
|
|
|
const rtc::SocketAddressPair& addrs,
|
|
|
|
rtc::AsyncPacketSocket* socket);
|
2013-07-10 02:45:36 +02:00
|
|
|
~RelayServerConnection();
|
|
|
|
|
|
|
|
RelayServerBinding* binding() { return binding_; }
|
2014-07-29 19:36:52 +02:00
|
|
|
rtc::AsyncPacketSocket* socket() { return socket_; }
|
2013-07-10 02:45:36 +02:00
|
|
|
|
|
|
|
// Returns a pair where the source is the remote address and the destination
|
|
|
|
// is the local address.
|
2014-07-29 19:36:52 +02:00
|
|
|
const rtc::SocketAddressPair& addr_pair() { return addr_pair_; }
|
2013-07-10 02:45:36 +02:00
|
|
|
|
|
|
|
// Sends a packet to the connected client. If an address is provided, then
|
|
|
|
// we make sure the internal client receives it, wrapping if necessary.
|
|
|
|
void Send(const char* data, size_t size);
|
|
|
|
void Send(const char* data, size_t size,
|
2014-07-29 19:36:52 +02:00
|
|
|
const rtc::SocketAddress& ext_addr);
|
2013-07-10 02:45:36 +02:00
|
|
|
|
|
|
|
// Sends a STUN message to the connected client with no wrapping.
|
|
|
|
void SendStun(const StunMessage& msg);
|
|
|
|
void SendStunError(const StunMessage& request, int code, const char* desc);
|
|
|
|
|
|
|
|
// A locked connection is one for which we know the intended destination of
|
|
|
|
// any raw packet received.
|
|
|
|
bool locked() const { return locked_; }
|
|
|
|
void Lock();
|
|
|
|
void Unlock();
|
|
|
|
|
|
|
|
// Records the address that raw packets should be forwarded to (for internal
|
|
|
|
// packets only; for external, we already know where they go).
|
2014-07-29 19:36:52 +02:00
|
|
|
const rtc::SocketAddress& default_destination() const {
|
2013-07-10 02:45:36 +02:00
|
|
|
return default_dest_;
|
|
|
|
}
|
2014-07-29 19:36:52 +02:00
|
|
|
void set_default_destination(const rtc::SocketAddress& addr) {
|
2013-07-10 02:45:36 +02:00
|
|
|
default_dest_ = addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
RelayServerBinding* binding_;
|
2014-07-29 19:36:52 +02:00
|
|
|
rtc::SocketAddressPair addr_pair_;
|
|
|
|
rtc::AsyncPacketSocket* socket_;
|
2013-07-10 02:45:36 +02:00
|
|
|
bool locked_;
|
2014-07-29 19:36:52 +02:00
|
|
|
rtc::SocketAddress default_dest_;
|
2013-07-10 02:45:36 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// Records a set of internal and external connections that we relay between,
|
|
|
|
// or in other words, that are "bound" together.
|
2014-07-29 19:36:52 +02:00
|
|
|
class RelayServerBinding : public rtc::MessageHandler {
|
2013-07-10 02:45:36 +02:00
|
|
|
public:
|
|
|
|
RelayServerBinding(
|
|
|
|
RelayServer* server, const std::string& username,
|
|
|
|
const std::string& password, uint32 lifetime);
|
|
|
|
virtual ~RelayServerBinding();
|
|
|
|
|
|
|
|
RelayServer* server() { return server_; }
|
|
|
|
uint32 lifetime() { return lifetime_; }
|
|
|
|
const std::string& username() { return username_; }
|
|
|
|
const std::string& password() { return password_; }
|
|
|
|
const std::string& magic_cookie() { return magic_cookie_; }
|
|
|
|
|
|
|
|
// Adds/Removes a connection into the binding.
|
|
|
|
void AddInternalConnection(RelayServerConnection* conn);
|
|
|
|
void AddExternalConnection(RelayServerConnection* conn);
|
|
|
|
|
|
|
|
// We keep track of the use of each binding. If we detect that it was not
|
|
|
|
// used for longer than the lifetime, then we send a signal.
|
|
|
|
void NoteUsed();
|
|
|
|
sigslot::signal1<RelayServerBinding*> SignalTimeout;
|
|
|
|
|
|
|
|
// Determines whether the given packet has the magic cookie present (in the
|
|
|
|
// right place).
|
|
|
|
bool HasMagicCookie(const char* bytes, size_t size) const;
|
|
|
|
|
|
|
|
// Determines the connection to use to send packets to or from the given
|
|
|
|
// external address.
|
|
|
|
RelayServerConnection* GetInternalConnection(
|
2014-07-29 19:36:52 +02:00
|
|
|
const rtc::SocketAddress& ext_addr);
|
2013-07-10 02:45:36 +02:00
|
|
|
RelayServerConnection* GetExternalConnection(
|
2014-07-29 19:36:52 +02:00
|
|
|
const rtc::SocketAddress& ext_addr);
|
2013-07-10 02:45:36 +02:00
|
|
|
|
|
|
|
// MessageHandler:
|
2014-07-29 19:36:52 +02:00
|
|
|
void OnMessage(rtc::Message *pmsg);
|
2013-07-10 02:45:36 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
RelayServer* server_;
|
|
|
|
|
|
|
|
std::string username_;
|
|
|
|
std::string password_;
|
|
|
|
std::string magic_cookie_;
|
|
|
|
|
|
|
|
std::vector<RelayServerConnection*> internal_connections_;
|
|
|
|
std::vector<RelayServerConnection*> external_connections_;
|
|
|
|
|
|
|
|
uint32 lifetime_;
|
|
|
|
uint32 last_used_;
|
|
|
|
// TODO: bandwidth
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace cricket
|
|
|
|
|
2014-10-18 00:03:39 +02:00
|
|
|
#endif // TALK_P2P_BASE_RELAYSERVER_H_
|