blob: e2e6800710dd538a9e4eef6a119f23c905722a57 [file] [log] [blame]
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001/*
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
22namespace cricket {
23
24class RelayEntry;
25class 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 Holmer55674ff2016-01-14 15:49:16 +010032// successful all other connection attempts are aborted.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000033class 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öm0c4e06b2015-10-07 12:23:21 +020038 static RelayPort* Create(rtc::Thread* thread,
39 rtc::PacketSocketFactory* factory,
40 rtc::Network* network,
Peter Boström0c4e06b2015-10-07 12:23:21 +020041 uint16_t min_port,
42 uint16_t max_port,
43 const std::string& username,
44 const std::string& password) {
deadbeef5c3c1042017-08-04 15:01:57 -070045 return new RelayPort(thread, factory, network, min_port, max_port, username,
46 password);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000047 }
Stefan Holmer55674ff2016-01-14 15:49:16 +010048 ~RelayPort() override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000049
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 Holmer55674ff2016-01-14 15:49:16 +010056 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 Zhangf9945b22015-12-15 12:20:13 -080063 // Relay port may create both TCP and UDP connections.
64 return true;
65 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000066
67 const ProtocolAddress * ServerAddress(size_t index) const;
68 bool IsReady() { return ready_; }
69
Honghai Zhangb9e7b4a2016-06-30 20:52:02 -070070 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.org269fb4b2014-10-28 22:20:11 +000076 // Used for testing.
77 sigslot::signal1<const ProtocolAddress*> SignalConnectFailure;
78 sigslot::signal1<const ProtocolAddress*> SignalSoftTimeout;
79
80 protected:
pkasting@chromium.org332331f2014-11-06 20:19:22 +000081 RelayPort(rtc::Thread* thread,
82 rtc::PacketSocketFactory* factory,
83 rtc::Network*,
Peter Boström0c4e06b2015-10-07 12:23:21 +020084 uint16_t min_port,
85 uint16_t max_port,
pkasting@chromium.org332331f2014-11-06 20:19:22 +000086 const std::string& username,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000087 const std::string& password);
88 bool Init();
89
90 void SetReady();
91
Stefan Holmer55674ff2016-01-14 15:49:16 +010092 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.org269fb4b2014-10-28 22:20:11 +000097
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 Holmer55674ff2016-01-14 15:49:16 +0100104 // 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.org269fb4b2014-10-28 22:20:11 +0000109 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_