Jonas Oreland | 9705011 | 2020-11-17 20:30:33 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2020 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 TEST_NETWORK_EMULATED_TURN_SERVER_H_ |
| 12 | #define TEST_NETWORK_EMULATED_TURN_SERVER_H_ |
| 13 | |
| 14 | #include <map> |
| 15 | #include <memory> |
| 16 | #include <string> |
| 17 | |
Ali Tofigh | de2ac5a | 2022-06-30 09:58:26 | [diff] [blame] | 18 | #include "absl/strings/string_view.h" |
Jonas Oreland | 9705011 | 2020-11-17 20:30:33 | [diff] [blame] | 19 | #include "api/test/network_emulation_manager.h" |
| 20 | #include "api/transport/stun.h" |
| 21 | #include "p2p/base/turn_server.h" |
| 22 | #include "rtc_base/async_packet_socket.h" |
| 23 | |
| 24 | namespace webrtc { |
| 25 | namespace test { |
| 26 | |
| 27 | // EmulatedTURNServer wraps cricket::TurnServer to be used inside |
| 28 | // a emulated network. |
| 29 | // |
| 30 | // Packets from EmulatedEndpoint (client or peer) are received in |
| 31 | // EmulatedTURNServer::OnPacketReceived which performs a map lookup |
| 32 | // and delivers them into cricket::TurnServer using |
| 33 | // AsyncPacketSocket::SignalReadPacket |
| 34 | // |
| 35 | // Packets from cricket::TurnServer to EmulatedEndpoint are sent into |
| 36 | // using a wrapper around AsyncPacketSocket (no lookup required as the |
| 37 | // wrapper around AsyncPacketSocket keep a pointer to the EmulatedEndpoint). |
| 38 | class EmulatedTURNServer : public EmulatedTURNServerInterface, |
| 39 | public cricket::TurnAuthInterface, |
| 40 | public webrtc::EmulatedNetworkReceiverInterface { |
| 41 | public: |
| 42 | // Create an EmulatedTURNServer. |
Artem Titov | 1ee563d | 2021-07-27 10:46:29 | [diff] [blame] | 43 | // `thread` is a thread that will be used to run cricket::TurnServer |
Jonas Oreland | 9705011 | 2020-11-17 20:30:33 | [diff] [blame] | 44 | // that expects all calls to be made from a single thread. |
| 45 | EmulatedTURNServer(std::unique_ptr<rtc::Thread> thread, |
| 46 | EmulatedEndpoint* client, |
| 47 | EmulatedEndpoint* peer); |
| 48 | ~EmulatedTURNServer() override; |
| 49 | |
| 50 | IceServerConfig GetIceServerConfig() const override { return ice_config_; } |
| 51 | |
| 52 | EmulatedEndpoint* GetClientEndpoint() const override { return client_; } |
| 53 | |
| 54 | rtc::SocketAddress GetClientEndpointAddress() const override { |
| 55 | return client_address_; |
| 56 | } |
| 57 | |
| 58 | EmulatedEndpoint* GetPeerEndpoint() const override { return peer_; } |
| 59 | |
| 60 | // cricket::TurnAuthInterface |
Ali Tofigh | de2ac5a | 2022-06-30 09:58:26 | [diff] [blame] | 61 | bool GetKey(absl::string_view username, |
| 62 | absl::string_view realm, |
Jonas Oreland | 9705011 | 2020-11-17 20:30:33 | [diff] [blame] | 63 | std::string* key) override { |
Ali Tofigh | de2ac5a | 2022-06-30 09:58:26 | [diff] [blame] | 64 | return cricket::ComputeStunCredentialHash( |
| 65 | std::string(username), std::string(realm), std::string(username), key); |
Jonas Oreland | 9705011 | 2020-11-17 20:30:33 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | rtc::AsyncPacketSocket* CreatePeerSocket() { return Wrap(peer_); } |
| 69 | |
| 70 | // This method is called by network emulation when a packet |
| 71 | // comes from an emulated link. |
| 72 | void OnPacketReceived(webrtc::EmulatedIpPacket packet) override; |
| 73 | |
| 74 | // This is called when the TURN server deletes a socket. |
| 75 | void Unbind(rtc::SocketAddress address); |
| 76 | |
| 77 | // Unbind all sockets. |
| 78 | void Stop(); |
| 79 | |
| 80 | private: |
| 81 | std::unique_ptr<rtc::Thread> thread_; |
| 82 | rtc::SocketAddress client_address_; |
| 83 | IceServerConfig ice_config_; |
| 84 | EmulatedEndpoint* const client_; |
| 85 | EmulatedEndpoint* const peer_; |
| 86 | std::unique_ptr<cricket::TurnServer> turn_server_ RTC_GUARDED_BY(&thread_); |
| 87 | std::map<rtc::SocketAddress, rtc::AsyncPacketSocket*> sockets_ |
| 88 | RTC_GUARDED_BY(&thread_); |
| 89 | |
| 90 | // Wraps a EmulatedEndpoint in a AsyncPacketSocket to bridge interaction |
| 91 | // with TurnServer. cricket::TurnServer gets ownership of the socket. |
| 92 | rtc::AsyncPacketSocket* Wrap(EmulatedEndpoint* endpoint); |
| 93 | }; |
| 94 | |
| 95 | } // namespace test |
| 96 | } // namespace webrtc |
| 97 | |
| 98 | #endif // TEST_NETWORK_EMULATED_TURN_SERVER_H_ |