Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / libjingle / source / talk / p2p / base / testturnserver.h
1 /*
2  * libjingle
3  * Copyright 2012, Google Inc.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
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.
15  *
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.
26  */
27
28 #ifndef WEBRTC_P2P_BASE_TESTTURNSERVER_H_
29 #define WEBRTC_P2P_BASE_TESTTURNSERVER_H_
30
31 #include <string>
32 #include <vector>
33
34 #include "webrtc/p2p/base/basicpacketsocketfactory.h"
35 #include "webrtc/p2p/base/stun.h"
36 #include "webrtc/p2p/base/turnserver.h"
37 #include "webrtc/base/asyncudpsocket.h"
38 #include "webrtc/base/thread.h"
39
40 namespace cricket {
41
42 static const char kTestRealm[] = "example.org";
43 static const char kTestSoftware[] = "TestTurnServer";
44
45 class TestTurnRedirector : public TurnRedirectInterface {
46  public:
47   explicit TestTurnRedirector(const std::vector<rtc::SocketAddress>& addresses)
48       : alternate_server_addresses_(addresses),
49         iter_(alternate_server_addresses_.begin()) {
50   }
51
52   virtual bool ShouldRedirect(const rtc::SocketAddress&,
53                               rtc::SocketAddress* out) {
54     if (!out || iter_ == alternate_server_addresses_.end()) {
55       return false;
56     }
57     *out = *iter_++;
58     return true;
59   }
60
61  private:
62   const std::vector<rtc::SocketAddress>& alternate_server_addresses_;
63   std::vector<rtc::SocketAddress>::const_iterator iter_;
64 };
65
66 class TestTurnServer : public TurnAuthInterface {
67  public:
68   TestTurnServer(rtc::Thread* thread,
69                  const rtc::SocketAddress& udp_int_addr,
70                  const rtc::SocketAddress& udp_ext_addr)
71       : server_(thread) {
72     AddInternalSocket(udp_int_addr, cricket::PROTO_UDP);
73     server_.SetExternalSocketFactory(new rtc::BasicPacketSocketFactory(),
74         udp_ext_addr);
75     server_.set_realm(kTestRealm);
76     server_.set_software(kTestSoftware);
77     server_.set_auth_hook(this);
78   }
79
80   void set_enable_otu_nonce(bool enable) {
81     server_.set_enable_otu_nonce(enable);
82   }
83
84   TurnServer* server() { return &server_; }
85
86   void set_redirect_hook(TurnRedirectInterface* redirect_hook) {
87     server_.set_redirect_hook(redirect_hook);
88   }
89
90   void AddInternalSocket(const rtc::SocketAddress& int_addr,
91                          ProtocolType proto) {
92     rtc::Thread* thread = rtc::Thread::Current();
93     if (proto == cricket::PROTO_UDP) {
94       server_.AddInternalSocket(rtc::AsyncUDPSocket::Create(
95           thread->socketserver(), int_addr), proto);
96     } else if (proto == cricket::PROTO_TCP) {
97       // For TCP we need to create a server socket which can listen for incoming
98       // new connections.
99       rtc::AsyncSocket* socket =
100           thread->socketserver()->CreateAsyncSocket(SOCK_STREAM);
101       socket->Bind(int_addr);
102       socket->Listen(5);
103       server_.AddInternalServerSocket(socket, proto);
104     }
105   }
106
107  private:
108   // For this test server, succeed if the password is the same as the username.
109   // Obviously, do not use this in a production environment.
110   virtual bool GetKey(const std::string& username, const std::string& realm,
111                       std::string* key) {
112     return ComputeStunCredentialHash(username, realm, username, key);
113   }
114
115   TurnServer server_;
116 };
117
118 }  // namespace cricket
119
120 #endif  // WEBRTC_P2P_BASE_TESTTURNSERVER_H_