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_BASE_RELAYSERVER_H_
29 #define TALK_P2P_BASE_RELAYSERVER_H_
35 #include "talk/p2p/base/port.h"
36 #include "talk/p2p/base/stun.h"
37 #include "webrtc/base/asyncudpsocket.h"
38 #include "webrtc/base/socketaddresspair.h"
39 #include "webrtc/base/thread.h"
40 #include "webrtc/base/timeutils.h"
44 class RelayServerBinding;
45 class RelayServerConnection;
47 // Relays traffic between connections to the server that are "bound" together.
48 // All connections created with the same username/password are bound together.
49 class RelayServer : public rtc::MessageHandler,
50 public sigslot::has_slots<> {
52 // Creates a server, which will use this thread to post messages to itself.
53 explicit RelayServer(rtc::Thread* thread);
56 rtc::Thread* thread() { return thread_; }
58 // Indicates whether we will print updates of the number of bindings.
59 bool log_bindings() const { return log_bindings_; }
60 void set_log_bindings(bool log_bindings) { log_bindings_ = log_bindings; }
62 // Updates the set of sockets that the server uses to talk to "internal"
63 // clients. These are clients that do the "port allocations".
64 void AddInternalSocket(rtc::AsyncPacketSocket* socket);
65 void RemoveInternalSocket(rtc::AsyncPacketSocket* socket);
67 // Updates the set of sockets that the server uses to talk to "external"
68 // clients. These are the clients that do not do allocations. They do not
69 // know that these addresses represent a relay server.
70 void AddExternalSocket(rtc::AsyncPacketSocket* socket);
71 void RemoveExternalSocket(rtc::AsyncPacketSocket* socket);
73 // Starts listening for connections on this sockets. When someone
74 // tries to connect, the connection will be accepted and a new
75 // internal socket will be added.
76 void AddInternalServerSocket(rtc::AsyncSocket* socket,
77 cricket::ProtocolType proto);
79 // Removes this server socket from the list.
80 void RemoveInternalServerSocket(rtc::AsyncSocket* socket);
82 // Methods for testing and debuging.
83 int GetConnectionCount() const;
84 rtc::SocketAddressPair GetConnection(int connection) const;
85 bool HasConnection(const rtc::SocketAddress& address) const;
88 typedef std::vector<rtc::AsyncPacketSocket*> SocketList;
89 typedef std::map<rtc::AsyncSocket*,
90 cricket::ProtocolType> ServerSocketMap;
91 typedef std::map<std::string, RelayServerBinding*> BindingMap;
92 typedef std::map<rtc::SocketAddressPair,
93 RelayServerConnection*> ConnectionMap;
97 SocketList internal_sockets_;
98 SocketList external_sockets_;
99 SocketList removed_sockets_;
100 ServerSocketMap server_sockets_;
101 BindingMap bindings_;
102 ConnectionMap connections_;
104 // Called when a packet is received by the server on one of its sockets.
105 void OnInternalPacket(rtc::AsyncPacketSocket* socket,
106 const char* bytes, size_t size,
107 const rtc::SocketAddress& remote_addr,
108 const rtc::PacketTime& packet_time);
109 void OnExternalPacket(rtc::AsyncPacketSocket* socket,
110 const char* bytes, size_t size,
111 const rtc::SocketAddress& remote_addr,
112 const rtc::PacketTime& packet_time);
114 void OnReadEvent(rtc::AsyncSocket* socket);
116 // Processes the relevant STUN request types from the client.
117 bool HandleStun(const char* bytes, size_t size,
118 const rtc::SocketAddress& remote_addr,
119 rtc::AsyncPacketSocket* socket,
120 std::string* username, StunMessage* msg);
121 void HandleStunAllocate(const char* bytes, size_t size,
122 const rtc::SocketAddressPair& ap,
123 rtc::AsyncPacketSocket* socket);
124 void HandleStun(RelayServerConnection* int_conn, const char* bytes,
126 void HandleStunAllocate(RelayServerConnection* int_conn,
127 const StunMessage& msg);
128 void HandleStunSend(RelayServerConnection* int_conn, const StunMessage& msg);
130 // Adds/Removes the a connection or binding.
131 void AddConnection(RelayServerConnection* conn);
132 void RemoveConnection(RelayServerConnection* conn);
133 void RemoveBinding(RelayServerBinding* binding);
135 // Handle messages in our worker thread.
136 void OnMessage(rtc::Message *pmsg);
138 // Called when the timer for checking lifetime times out.
139 void OnTimeout(RelayServerBinding* binding);
141 // Accept connections on this server socket.
142 void AcceptConnection(rtc::AsyncSocket* server_socket);
144 friend class RelayServerConnection;
145 friend class RelayServerBinding;
148 // Maintains information about a connection to the server. Each connection is
149 // part of one and only one binding.
150 class RelayServerConnection {
152 RelayServerConnection(RelayServerBinding* binding,
153 const rtc::SocketAddressPair& addrs,
154 rtc::AsyncPacketSocket* socket);
155 ~RelayServerConnection();
157 RelayServerBinding* binding() { return binding_; }
158 rtc::AsyncPacketSocket* socket() { return socket_; }
160 // Returns a pair where the source is the remote address and the destination
161 // is the local address.
162 const rtc::SocketAddressPair& addr_pair() { return addr_pair_; }
164 // Sends a packet to the connected client. If an address is provided, then
165 // we make sure the internal client receives it, wrapping if necessary.
166 void Send(const char* data, size_t size);
167 void Send(const char* data, size_t size,
168 const rtc::SocketAddress& ext_addr);
170 // Sends a STUN message to the connected client with no wrapping.
171 void SendStun(const StunMessage& msg);
172 void SendStunError(const StunMessage& request, int code, const char* desc);
174 // A locked connection is one for which we know the intended destination of
175 // any raw packet received.
176 bool locked() const { return locked_; }
180 // Records the address that raw packets should be forwarded to (for internal
181 // packets only; for external, we already know where they go).
182 const rtc::SocketAddress& default_destination() const {
183 return default_dest_;
185 void set_default_destination(const rtc::SocketAddress& addr) {
186 default_dest_ = addr;
190 RelayServerBinding* binding_;
191 rtc::SocketAddressPair addr_pair_;
192 rtc::AsyncPacketSocket* socket_;
194 rtc::SocketAddress default_dest_;
197 // Records a set of internal and external connections that we relay between,
198 // or in other words, that are "bound" together.
199 class RelayServerBinding : public rtc::MessageHandler {
202 RelayServer* server, const std::string& username,
203 const std::string& password, uint32 lifetime);
204 virtual ~RelayServerBinding();
206 RelayServer* server() { return server_; }
207 uint32 lifetime() { return lifetime_; }
208 const std::string& username() { return username_; }
209 const std::string& password() { return password_; }
210 const std::string& magic_cookie() { return magic_cookie_; }
212 // Adds/Removes a connection into the binding.
213 void AddInternalConnection(RelayServerConnection* conn);
214 void AddExternalConnection(RelayServerConnection* conn);
216 // We keep track of the use of each binding. If we detect that it was not
217 // used for longer than the lifetime, then we send a signal.
219 sigslot::signal1<RelayServerBinding*> SignalTimeout;
221 // Determines whether the given packet has the magic cookie present (in the
223 bool HasMagicCookie(const char* bytes, size_t size) const;
225 // Determines the connection to use to send packets to or from the given
227 RelayServerConnection* GetInternalConnection(
228 const rtc::SocketAddress& ext_addr);
229 RelayServerConnection* GetExternalConnection(
230 const rtc::SocketAddress& ext_addr);
233 void OnMessage(rtc::Message *pmsg);
236 RelayServer* server_;
238 std::string username_;
239 std::string password_;
240 std::string magic_cookie_;
242 std::vector<RelayServerConnection*> internal_connections_;
243 std::vector<RelayServerConnection*> external_connections_;
250 } // namespace cricket
252 #endif // TALK_P2P_BASE_RELAYSERVER_H_