blob: 922a25622115c352ca955cc44947e5271ba00dd8 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
3 * Copyright 2004--2005, Google Inc.
4 *
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00005 * Redistribution and use in source and binary forms, with or without
henrike@webrtc.org28e20752013-07-10 00:45:36 +00006 * modification, are permitted provided that the following conditions are met:
7 *
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00008 * 1. Redistributions of source code must retain the above copyright notice,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 * 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.
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000013 * 3. The name of the author may not be used to endorse or promote products
henrike@webrtc.org28e20752013-07-10 00:45:36 +000014 * 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
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000017 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
henrike@webrtc.org28e20752013-07-10 00:45:36 +000018 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000019 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000020 * 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,
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000023 * 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
henrike@webrtc.org28e20752013-07-10 00:45:36 +000025 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#ifndef TALK_P2P_BASE_RELAYSERVER_H_
29#define TALK_P2P_BASE_RELAYSERVER_H_
30
31#include <string>
32#include <vector>
33#include <map>
34
35#include "talk/base/asyncudpsocket.h"
36#include "talk/base/socketaddresspair.h"
37#include "talk/base/thread.h"
38#include "talk/base/timeutils.h"
39#include "talk/p2p/base/port.h"
40#include "talk/p2p/base/stun.h"
41
42namespace cricket {
43
44class RelayServerBinding;
45class RelayServerConnection;
46
47// Relays traffic between connections to the server that are "bound" together.
48// All connections created with the same username/password are bound together.
49class RelayServer : public talk_base::MessageHandler,
50 public sigslot::has_slots<> {
51 public:
52 // Creates a server, which will use this thread to post messages to itself.
53 explicit RelayServer(talk_base::Thread* thread);
54 ~RelayServer();
55
56 talk_base::Thread* thread() { return thread_; }
57
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; }
61
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(talk_base::AsyncPacketSocket* socket);
65 void RemoveInternalSocket(talk_base::AsyncPacketSocket* socket);
66
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(talk_base::AsyncPacketSocket* socket);
71 void RemoveExternalSocket(talk_base::AsyncPacketSocket* socket);
72
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(talk_base::AsyncSocket* socket,
77 cricket::ProtocolType proto);
78
79 // Removes this server socket from the list.
80 void RemoveInternalServerSocket(talk_base::AsyncSocket* socket);
81
82 // Methods for testing and debuging.
83 int GetConnectionCount() const;
84 talk_base::SocketAddressPair GetConnection(int connection) const;
85 bool HasConnection(const talk_base::SocketAddress& address) const;
86
87 private:
88 typedef std::vector<talk_base::AsyncPacketSocket*> SocketList;
89 typedef std::map<talk_base::AsyncSocket*,
90 cricket::ProtocolType> ServerSocketMap;
91 typedef std::map<std::string, RelayServerBinding*> BindingMap;
92 typedef std::map<talk_base::SocketAddressPair,
93 RelayServerConnection*> ConnectionMap;
94
95 talk_base::Thread* thread_;
96 bool log_bindings_;
97 SocketList internal_sockets_;
98 SocketList external_sockets_;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000099 SocketList removed_sockets_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000100 ServerSocketMap server_sockets_;
101 BindingMap bindings_;
102 ConnectionMap connections_;
103
104 // Called when a packet is received by the server on one of its sockets.
105 void OnInternalPacket(talk_base::AsyncPacketSocket* socket,
106 const char* bytes, size_t size,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000107 const talk_base::SocketAddress& remote_addr,
108 const talk_base::PacketTime& packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000109 void OnExternalPacket(talk_base::AsyncPacketSocket* socket,
110 const char* bytes, size_t size,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000111 const talk_base::SocketAddress& remote_addr,
112 const talk_base::PacketTime& packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000113
114 void OnReadEvent(talk_base::AsyncSocket* socket);
115
116 // Processes the relevant STUN request types from the client.
117 bool HandleStun(const char* bytes, size_t size,
118 const talk_base::SocketAddress& remote_addr,
119 talk_base::AsyncPacketSocket* socket,
120 std::string* username, StunMessage* msg);
121 void HandleStunAllocate(const char* bytes, size_t size,
122 const talk_base::SocketAddressPair& ap,
123 talk_base::AsyncPacketSocket* socket);
124 void HandleStun(RelayServerConnection* int_conn, const char* bytes,
125 size_t size);
126 void HandleStunAllocate(RelayServerConnection* int_conn,
127 const StunMessage& msg);
128 void HandleStunSend(RelayServerConnection* int_conn, const StunMessage& msg);
129
130 // Adds/Removes the a connection or binding.
131 void AddConnection(RelayServerConnection* conn);
132 void RemoveConnection(RelayServerConnection* conn);
133 void RemoveBinding(RelayServerBinding* binding);
134
135 // Handle messages in our worker thread.
136 void OnMessage(talk_base::Message *pmsg);
137
138 // Called when the timer for checking lifetime times out.
139 void OnTimeout(RelayServerBinding* binding);
140
141 // Accept connections on this server socket.
142 void AcceptConnection(talk_base::AsyncSocket* server_socket);
143
144 friend class RelayServerConnection;
145 friend class RelayServerBinding;
146};
147
148// Maintains information about a connection to the server. Each connection is
149// part of one and only one binding.
150class RelayServerConnection {
151 public:
152 RelayServerConnection(RelayServerBinding* binding,
153 const talk_base::SocketAddressPair& addrs,
154 talk_base::AsyncPacketSocket* socket);
155 ~RelayServerConnection();
156
157 RelayServerBinding* binding() { return binding_; }
158 talk_base::AsyncPacketSocket* socket() { return socket_; }
159
160 // Returns a pair where the source is the remote address and the destination
161 // is the local address.
162 const talk_base::SocketAddressPair& addr_pair() { return addr_pair_; }
163
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 talk_base::SocketAddress& ext_addr);
169
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);
173
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_; }
177 void Lock();
178 void Unlock();
179
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 talk_base::SocketAddress& default_destination() const {
183 return default_dest_;
184 }
185 void set_default_destination(const talk_base::SocketAddress& addr) {
186 default_dest_ = addr;
187 }
188
189 private:
190 RelayServerBinding* binding_;
191 talk_base::SocketAddressPair addr_pair_;
192 talk_base::AsyncPacketSocket* socket_;
193 bool locked_;
194 talk_base::SocketAddress default_dest_;
195};
196
197// Records a set of internal and external connections that we relay between,
198// or in other words, that are "bound" together.
199class RelayServerBinding : public talk_base::MessageHandler {
200 public:
201 RelayServerBinding(
202 RelayServer* server, const std::string& username,
203 const std::string& password, uint32 lifetime);
204 virtual ~RelayServerBinding();
205
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_; }
211
212 // Adds/Removes a connection into the binding.
213 void AddInternalConnection(RelayServerConnection* conn);
214 void AddExternalConnection(RelayServerConnection* conn);
215
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.
218 void NoteUsed();
219 sigslot::signal1<RelayServerBinding*> SignalTimeout;
220
221 // Determines whether the given packet has the magic cookie present (in the
222 // right place).
223 bool HasMagicCookie(const char* bytes, size_t size) const;
224
225 // Determines the connection to use to send packets to or from the given
226 // external address.
227 RelayServerConnection* GetInternalConnection(
228 const talk_base::SocketAddress& ext_addr);
229 RelayServerConnection* GetExternalConnection(
230 const talk_base::SocketAddress& ext_addr);
231
232 // MessageHandler:
233 void OnMessage(talk_base::Message *pmsg);
234
235 private:
236 RelayServer* server_;
237
238 std::string username_;
239 std::string password_;
240 std::string magic_cookie_;
241
242 std::vector<RelayServerConnection*> internal_connections_;
243 std::vector<RelayServerConnection*> external_connections_;
244
245 uint32 lifetime_;
246 uint32 last_used_;
247 // TODO: bandwidth
248};
249
250} // namespace cricket
251
252#endif // TALK_P2P_BASE_RELAYSERVER_H_