blob: 0edc47651137c344f65f552d2880294c9547470c [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef P2P_BASE_TESTTURNSERVER_H_
12#define P2P_BASE_TESTTURNSERVER_H_
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000013
14#include <string>
15#include <vector>
16
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "p2p/base/basicpacketsocketfactory.h"
18#include "p2p/base/stun.h"
19#include "p2p/base/turnserver.h"
20#include "rtc_base/asyncudpsocket.h"
21#include "rtc_base/ssladapter.h"
22#include "rtc_base/sslidentity.h"
23#include "rtc_base/thread.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000024
25namespace cricket {
26
27static const char kTestRealm[] = "example.org";
28static const char kTestSoftware[] = "TestTurnServer";
29
30class TestTurnRedirector : public TurnRedirectInterface {
31 public:
32 explicit TestTurnRedirector(const std::vector<rtc::SocketAddress>& addresses)
33 : alternate_server_addresses_(addresses),
34 iter_(alternate_server_addresses_.begin()) {
35 }
36
37 virtual bool ShouldRedirect(const rtc::SocketAddress&,
38 rtc::SocketAddress* out) {
39 if (!out || iter_ == alternate_server_addresses_.end()) {
40 return false;
41 }
42 *out = *iter_++;
43 return true;
44 }
45
46 private:
47 const std::vector<rtc::SocketAddress>& alternate_server_addresses_;
48 std::vector<rtc::SocketAddress>::const_iterator iter_;
49};
50
51class TestTurnServer : public TurnAuthInterface {
52 public:
53 TestTurnServer(rtc::Thread* thread,
Honghai Zhang80f1db92016-01-27 11:54:45 -080054 const rtc::SocketAddress& int_addr,
55 const rtc::SocketAddress& udp_ext_addr,
56 ProtocolType int_protocol = PROTO_UDP)
Taylor Brandstettere5835f52016-09-16 15:07:50 -070057 : server_(thread), thread_(thread) {
Honghai Zhang80f1db92016-01-27 11:54:45 -080058 AddInternalSocket(int_addr, int_protocol);
Taylor Brandstettere5835f52016-09-16 15:07:50 -070059 server_.SetExternalSocketFactory(new rtc::BasicPacketSocketFactory(thread),
60 udp_ext_addr);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000061 server_.set_realm(kTestRealm);
62 server_.set_software(kTestSoftware);
63 server_.set_auth_hook(this);
64 }
65
66 void set_enable_otu_nonce(bool enable) {
67 server_.set_enable_otu_nonce(enable);
68 }
69
70 TurnServer* server() { return &server_; }
71
72 void set_redirect_hook(TurnRedirectInterface* redirect_hook) {
73 server_.set_redirect_hook(redirect_hook);
74 }
75
Taylor Brandstetteref184702016-06-23 17:35:47 -070076 void set_enable_permission_checks(bool enable) {
77 server_.set_enable_permission_checks(enable);
78 }
79
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000080 void AddInternalSocket(const rtc::SocketAddress& int_addr,
81 ProtocolType proto) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000082 if (proto == cricket::PROTO_UDP) {
Taylor Brandstettere5835f52016-09-16 15:07:50 -070083 server_.AddInternalSocket(
84 rtc::AsyncUDPSocket::Create(thread_->socketserver(), int_addr),
85 proto);
Steve Anton786de702017-08-17 15:15:46 -070086 } else if (proto == cricket::PROTO_TCP || proto == cricket::PROTO_TLS) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000087 // For TCP we need to create a server socket which can listen for incoming
88 // new connections.
89 rtc::AsyncSocket* socket =
Taylor Brandstettere5835f52016-09-16 15:07:50 -070090 thread_->socketserver()->CreateAsyncSocket(SOCK_STREAM);
Steve Anton786de702017-08-17 15:15:46 -070091 if (proto == cricket::PROTO_TLS) {
92 // For TLS, wrap the TCP socket with an SSL adapter. The adapter must
93 // be configured with a self-signed certificate for testing.
94 // Additionally, the client will not present a valid certificate, so we
95 // must not fail when checking the peer's identity.
96 rtc::SSLAdapter* adapter = rtc::SSLAdapter::Create(socket);
97 adapter->SetRole(rtc::SSL_SERVER);
98 adapter->SetIdentity(
99 rtc::SSLIdentity::Generate("test turn server", rtc::KeyParams()));
Diogo Real1dca9d52017-08-29 12:18:32 -0700100 adapter->SetIgnoreBadCert(true);
Steve Anton786de702017-08-17 15:15:46 -0700101 socket = adapter;
102 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000103 socket->Bind(int_addr);
104 socket->Listen(5);
105 server_.AddInternalServerSocket(socket, proto);
Steve Anton786de702017-08-17 15:15:46 -0700106 } else {
107 RTC_NOTREACHED() << "Unknown protocol type: " << proto;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000108 }
109 }
110
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000111 // Finds the first allocation in the server allocation map with a source
112 // ip and port matching the socket address provided.
113 TurnServerAllocation* FindAllocation(const rtc::SocketAddress& src) {
114 const TurnServer::AllocationMap& map = server_.allocations();
115 for (TurnServer::AllocationMap::const_iterator it = map.begin();
116 it != map.end(); ++it) {
117 if (src == it->first.src()) {
deadbeef97943662016-07-12 11:04:50 -0700118 return it->second.get();
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000119 }
120 }
121 return NULL;
122 }
123
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000124 private:
125 // For this test server, succeed if the password is the same as the username.
126 // Obviously, do not use this in a production environment.
127 virtual bool GetKey(const std::string& username, const std::string& realm,
128 std::string* key) {
129 return ComputeStunCredentialHash(username, realm, username, key);
130 }
131
132 TurnServer server_;
Taylor Brandstettere5835f52016-09-16 15:07:50 -0700133 rtc::Thread* thread_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000134};
135
136} // namespace cricket
137
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200138#endif // P2P_BASE_TESTTURNSERVER_H_