blob: 8660ae1ff27b5add65846ea50ac18e71b73ff765 [file] [log] [blame]
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001/*
2 * Copyright 2012 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#ifndef WEBRTC_P2P_BASE_TESTTURNSERVER_H_
12#define WEBRTC_P2P_BASE_TESTTURNSERVER_H_
13
14#include <string>
15#include <vector>
16
17#include "webrtc/p2p/base/basicpacketsocketfactory.h"
18#include "webrtc/p2p/base/stun.h"
19#include "webrtc/p2p/base/turnserver.h"
20#include "webrtc/base/asyncudpsocket.h"
21#include "webrtc/base/thread.h"
22
23namespace cricket {
24
25static const char kTestRealm[] = "example.org";
26static const char kTestSoftware[] = "TestTurnServer";
27
28class TestTurnRedirector : public TurnRedirectInterface {
29 public:
30 explicit TestTurnRedirector(const std::vector<rtc::SocketAddress>& addresses)
31 : alternate_server_addresses_(addresses),
32 iter_(alternate_server_addresses_.begin()) {
33 }
34
35 virtual bool ShouldRedirect(const rtc::SocketAddress&,
36 rtc::SocketAddress* out) {
37 if (!out || iter_ == alternate_server_addresses_.end()) {
38 return false;
39 }
40 *out = *iter_++;
41 return true;
42 }
43
44 private:
45 const std::vector<rtc::SocketAddress>& alternate_server_addresses_;
46 std::vector<rtc::SocketAddress>::const_iterator iter_;
47};
48
49class TestTurnServer : public TurnAuthInterface {
50 public:
51 TestTurnServer(rtc::Thread* thread,
Honghai Zhang80f1db92016-01-27 11:54:45 -080052 const rtc::SocketAddress& int_addr,
53 const rtc::SocketAddress& udp_ext_addr,
54 ProtocolType int_protocol = PROTO_UDP)
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000055 : server_(thread) {
Honghai Zhang80f1db92016-01-27 11:54:45 -080056 AddInternalSocket(int_addr, int_protocol);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000057 server_.SetExternalSocketFactory(new rtc::BasicPacketSocketFactory(),
58 udp_ext_addr);
59 server_.set_realm(kTestRealm);
60 server_.set_software(kTestSoftware);
61 server_.set_auth_hook(this);
62 }
63
64 void set_enable_otu_nonce(bool enable) {
65 server_.set_enable_otu_nonce(enable);
66 }
67
68 TurnServer* server() { return &server_; }
69
70 void set_redirect_hook(TurnRedirectInterface* redirect_hook) {
71 server_.set_redirect_hook(redirect_hook);
72 }
73
74 void AddInternalSocket(const rtc::SocketAddress& int_addr,
75 ProtocolType proto) {
76 rtc::Thread* thread = rtc::Thread::Current();
77 if (proto == cricket::PROTO_UDP) {
78 server_.AddInternalSocket(rtc::AsyncUDPSocket::Create(
79 thread->socketserver(), int_addr), proto);
80 } else if (proto == cricket::PROTO_TCP) {
81 // For TCP we need to create a server socket which can listen for incoming
82 // new connections.
83 rtc::AsyncSocket* socket =
84 thread->socketserver()->CreateAsyncSocket(SOCK_STREAM);
85 socket->Bind(int_addr);
86 socket->Listen(5);
87 server_.AddInternalServerSocket(socket, proto);
88 }
89 }
90
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +000091 // Finds the first allocation in the server allocation map with a source
92 // ip and port matching the socket address provided.
93 TurnServerAllocation* FindAllocation(const rtc::SocketAddress& src) {
94 const TurnServer::AllocationMap& map = server_.allocations();
95 for (TurnServer::AllocationMap::const_iterator it = map.begin();
96 it != map.end(); ++it) {
97 if (src == it->first.src()) {
98 return it->second;
99 }
100 }
101 return NULL;
102 }
103
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000104 private:
105 // For this test server, succeed if the password is the same as the username.
106 // Obviously, do not use this in a production environment.
107 virtual bool GetKey(const std::string& username, const std::string& realm,
108 std::string* key) {
109 return ComputeStunCredentialHash(username, realm, username, key);
110 }
111
112 TurnServer server_;
113};
114
115} // namespace cricket
116
117#endif // WEBRTC_P2P_BASE_TESTTURNSERVER_H_