blob: 43d363cb35b41aeae09245b7c2a474cad77b43b6 [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
Taylor Brandstetteref184702016-06-23 17:35:47 -070074 void set_enable_permission_checks(bool enable) {
75 server_.set_enable_permission_checks(enable);
76 }
77
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000078 void AddInternalSocket(const rtc::SocketAddress& int_addr,
79 ProtocolType proto) {
80 rtc::Thread* thread = rtc::Thread::Current();
81 if (proto == cricket::PROTO_UDP) {
82 server_.AddInternalSocket(rtc::AsyncUDPSocket::Create(
83 thread->socketserver(), int_addr), proto);
84 } else if (proto == cricket::PROTO_TCP) {
85 // For TCP we need to create a server socket which can listen for incoming
86 // new connections.
87 rtc::AsyncSocket* socket =
88 thread->socketserver()->CreateAsyncSocket(SOCK_STREAM);
89 socket->Bind(int_addr);
90 socket->Listen(5);
91 server_.AddInternalServerSocket(socket, proto);
92 }
93 }
94
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +000095 // Finds the first allocation in the server allocation map with a source
96 // ip and port matching the socket address provided.
97 TurnServerAllocation* FindAllocation(const rtc::SocketAddress& src) {
98 const TurnServer::AllocationMap& map = server_.allocations();
99 for (TurnServer::AllocationMap::const_iterator it = map.begin();
100 it != map.end(); ++it) {
101 if (src == it->first.src()) {
deadbeef97943662016-07-12 11:04:50 -0700102 return it->second.get();
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000103 }
104 }
105 return NULL;
106 }
107
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000108 private:
109 // For this test server, succeed if the password is the same as the username.
110 // Obviously, do not use this in a production environment.
111 virtual bool GetKey(const std::string& username, const std::string& realm,
112 std::string* key) {
113 return ComputeStunCredentialHash(username, realm, username, key);
114 }
115
116 TurnServer server_;
117};
118
119} // namespace cricket
120
121#endif // WEBRTC_P2P_BASE_TESTTURNSERVER_H_