henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2004 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_RELAYPORT_H_ |
| 12 | #define WEBRTC_P2P_BASE_RELAYPORT_H_ |
| 13 | |
| 14 | #include <deque> |
| 15 | #include <string> |
| 16 | #include <utility> |
| 17 | #include <vector> |
| 18 | |
| 19 | #include "webrtc/p2p/base/port.h" |
| 20 | #include "webrtc/p2p/base/stunrequest.h" |
| 21 | |
| 22 | namespace cricket { |
| 23 | |
| 24 | class RelayEntry; |
| 25 | class RelayConnection; |
| 26 | |
| 27 | // Communicates using an allocated port on the relay server. For each |
| 28 | // remote candidate that we try to send data to a RelayEntry instance |
| 29 | // is created. The RelayEntry will try to reach the remote destination |
| 30 | // by connecting to all available server addresses in a pre defined |
| 31 | // order with a small delay in between. When a connection is |
Stefan Holmer | 55674ff | 2016-01-14 15:49:16 +0100 | [diff] [blame] | 32 | // successful all other connection attempts are aborted. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 33 | class RelayPort : public Port { |
| 34 | public: |
| 35 | typedef std::pair<rtc::Socket::Option, int> OptionValue; |
| 36 | |
| 37 | // RelayPort doesn't yet do anything fancy in the ctor. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 38 | static RelayPort* Create(rtc::Thread* thread, |
| 39 | rtc::PacketSocketFactory* factory, |
| 40 | rtc::Network* network, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 41 | uint16_t min_port, |
| 42 | uint16_t max_port, |
| 43 | const std::string& username, |
| 44 | const std::string& password) { |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 45 | return new RelayPort(thread, factory, network, min_port, max_port, username, |
| 46 | password); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 47 | } |
Stefan Holmer | 55674ff | 2016-01-14 15:49:16 +0100 | [diff] [blame] | 48 | ~RelayPort() override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 49 | |
| 50 | void AddServerAddress(const ProtocolAddress& addr); |
| 51 | void AddExternalAddress(const ProtocolAddress& addr); |
| 52 | |
| 53 | const std::vector<OptionValue>& options() const { return options_; } |
| 54 | bool HasMagicCookie(const char* data, size_t size); |
| 55 | |
Stefan Holmer | 55674ff | 2016-01-14 15:49:16 +0100 | [diff] [blame] | 56 | void PrepareAddress() override; |
| 57 | Connection* CreateConnection(const Candidate& address, |
| 58 | CandidateOrigin origin) override; |
| 59 | int SetOption(rtc::Socket::Option opt, int value) override; |
| 60 | int GetOption(rtc::Socket::Option opt, int* value) override; |
| 61 | int GetError() override; |
| 62 | bool SupportsProtocol(const std::string& protocol) const override { |
Honghai Zhang | f9945b2 | 2015-12-15 12:20:13 -0800 | [diff] [blame] | 63 | // Relay port may create both TCP and UDP connections. |
| 64 | return true; |
| 65 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 66 | |
| 67 | const ProtocolAddress * ServerAddress(size_t index) const; |
| 68 | bool IsReady() { return ready_; } |
| 69 | |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 70 | ProtocolType GetProtocol() const override { |
| 71 | // We shouldn't be using RelayPort, but we need to provide an |
| 72 | // implementation here. |
| 73 | return PROTO_UDP; |
| 74 | } |
| 75 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 76 | // Used for testing. |
| 77 | sigslot::signal1<const ProtocolAddress*> SignalConnectFailure; |
| 78 | sigslot::signal1<const ProtocolAddress*> SignalSoftTimeout; |
| 79 | |
| 80 | protected: |
pkasting@chromium.org | 332331f | 2014-11-06 20:19:22 +0000 | [diff] [blame] | 81 | RelayPort(rtc::Thread* thread, |
| 82 | rtc::PacketSocketFactory* factory, |
| 83 | rtc::Network*, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 84 | uint16_t min_port, |
| 85 | uint16_t max_port, |
pkasting@chromium.org | 332331f | 2014-11-06 20:19:22 +0000 | [diff] [blame] | 86 | const std::string& username, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 87 | const std::string& password); |
| 88 | bool Init(); |
| 89 | |
| 90 | void SetReady(); |
| 91 | |
Stefan Holmer | 55674ff | 2016-01-14 15:49:16 +0100 | [diff] [blame] | 92 | int SendTo(const void* data, |
| 93 | size_t size, |
| 94 | const rtc::SocketAddress& addr, |
| 95 | const rtc::PacketOptions& options, |
| 96 | bool payload) override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 97 | |
| 98 | // Dispatches the given packet to the port or connection as appropriate. |
| 99 | void OnReadPacket(const char* data, size_t size, |
| 100 | const rtc::SocketAddress& remote_addr, |
| 101 | ProtocolType proto, |
| 102 | const rtc::PacketTime& packet_time); |
| 103 | |
Stefan Holmer | 55674ff | 2016-01-14 15:49:16 +0100 | [diff] [blame] | 104 | // The OnSentPacket callback is left empty here since they are handled by |
| 105 | // RelayEntry. |
| 106 | void OnSentPacket(rtc::AsyncPacketSocket* socket, |
| 107 | const rtc::SentPacket& sent_packet) override {} |
| 108 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 109 | private: |
| 110 | friend class RelayEntry; |
| 111 | |
| 112 | std::deque<ProtocolAddress> server_addr_; |
| 113 | std::vector<ProtocolAddress> external_addr_; |
| 114 | bool ready_; |
| 115 | std::vector<RelayEntry*> entries_; |
| 116 | std::vector<OptionValue> options_; |
| 117 | int error_; |
| 118 | }; |
| 119 | |
| 120 | } // namespace cricket |
| 121 | |
| 122 | #endif // WEBRTC_P2P_BASE_RELAYPORT_H_ |