henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * libjingle |
| 3 | * Copyright 2004--2005, 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 TALK_P2P_BASE_RELAYPORT_H_ |
| 29 | #define TALK_P2P_BASE_RELAYPORT_H_ |
| 30 | |
| 31 | #include <deque> |
| 32 | #include <string> |
| 33 | #include <utility> |
| 34 | #include <vector> |
| 35 | |
| 36 | #include "talk/p2p/base/port.h" |
| 37 | #include "talk/p2p/base/stunrequest.h" |
| 38 | |
| 39 | namespace cricket { |
| 40 | |
| 41 | class RelayEntry; |
| 42 | class RelayConnection; |
| 43 | |
| 44 | // Communicates using an allocated port on the relay server. For each |
| 45 | // remote candidate that we try to send data to a RelayEntry instance |
| 46 | // is created. The RelayEntry will try to reach the remote destination |
| 47 | // by connecting to all available server addresses in a pre defined |
| 48 | // order with a small delay in between. When a connection is |
| 49 | // successful all other connection attemts are aborted. |
| 50 | class RelayPort : public Port { |
| 51 | public: |
| 52 | typedef std::pair<talk_base::Socket::Option, int> OptionValue; |
| 53 | |
| 54 | // RelayPort doesn't yet do anything fancy in the ctor. |
| 55 | static RelayPort* Create( |
| 56 | talk_base::Thread* thread, talk_base::PacketSocketFactory* factory, |
| 57 | talk_base::Network* network, const talk_base::IPAddress& ip, |
| 58 | int min_port, int max_port, const std::string& username, |
| 59 | const std::string& password) { |
| 60 | return new RelayPort(thread, factory, network, ip, min_port, max_port, |
| 61 | username, password); |
| 62 | } |
| 63 | virtual ~RelayPort(); |
| 64 | |
| 65 | void AddServerAddress(const ProtocolAddress& addr); |
| 66 | void AddExternalAddress(const ProtocolAddress& addr); |
| 67 | |
| 68 | const std::vector<OptionValue>& options() const { return options_; } |
| 69 | bool HasMagicCookie(const char* data, size_t size); |
| 70 | |
| 71 | virtual void PrepareAddress(); |
| 72 | virtual Connection* CreateConnection(const Candidate& address, |
| 73 | CandidateOrigin origin); |
| 74 | virtual int SetOption(talk_base::Socket::Option opt, int value); |
| 75 | virtual int GetOption(talk_base::Socket::Option opt, int* value); |
| 76 | virtual int GetError(); |
| 77 | |
| 78 | const ProtocolAddress * ServerAddress(size_t index) const; |
| 79 | bool IsReady() { return ready_; } |
| 80 | |
| 81 | // Used for testing. |
| 82 | sigslot::signal1<const ProtocolAddress*> SignalConnectFailure; |
| 83 | sigslot::signal1<const ProtocolAddress*> SignalSoftTimeout; |
| 84 | |
| 85 | protected: |
| 86 | RelayPort(talk_base::Thread* thread, talk_base::PacketSocketFactory* factory, |
| 87 | talk_base::Network*, const talk_base::IPAddress& ip, |
| 88 | int min_port, int max_port, const std::string& username, |
| 89 | const std::string& password); |
| 90 | bool Init(); |
| 91 | |
| 92 | void SetReady(); |
| 93 | |
| 94 | virtual int SendTo(const void* data, size_t size, |
mallinath@webrtc.org | 1112c30 | 2013-09-23 20:34:45 +0000 | [diff] [blame] | 95 | const talk_base::SocketAddress& addr, |
mallinath@webrtc.org | 385857d | 2014-02-14 00:56:12 +0000 | [diff] [blame] | 96 | const talk_base::PacketOptions& options, |
mallinath@webrtc.org | 1112c30 | 2013-09-23 20:34:45 +0000 | [diff] [blame] | 97 | bool payload); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 98 | |
| 99 | // Dispatches the given packet to the port or connection as appropriate. |
| 100 | void OnReadPacket(const char* data, size_t size, |
| 101 | const talk_base::SocketAddress& remote_addr, |
wu@webrtc.org | a989080 | 2013-12-13 00:21:03 +0000 | [diff] [blame] | 102 | ProtocolType proto, |
| 103 | const talk_base::PacketTime& packet_time); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 104 | |
| 105 | private: |
| 106 | friend class RelayEntry; |
| 107 | |
| 108 | std::deque<ProtocolAddress> server_addr_; |
| 109 | std::vector<ProtocolAddress> external_addr_; |
| 110 | bool ready_; |
| 111 | std::vector<RelayEntry*> entries_; |
| 112 | std::vector<OptionValue> options_; |
| 113 | int error_; |
| 114 | }; |
| 115 | |
| 116 | } // namespace cricket |
| 117 | |
| 118 | #endif // TALK_P2P_BASE_RELAYPORT_H_ |