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<rtc::Socket::Option, int> OptionValue; 53 54 // RelayPort doesn't yet do anything fancy in the ctor. 55 static RelayPort* Create( 56 rtc::Thread* thread, rtc::PacketSocketFactory* factory, 57 rtc::Network* network, const rtc::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(rtc::Socket::Option opt, int value); 75 virtual int GetOption(rtc::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(rtc::Thread* thread, rtc::PacketSocketFactory* factory, 87 rtc::Network*, const rtc::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, 95 const rtc::SocketAddress& addr, 96 const rtc::PacketOptions& options, 97 bool payload); 98 99 // Dispatches the given packet to the port or connection as appropriate. 100 void OnReadPacket(const char* data, size_t size, 101 const rtc::SocketAddress& remote_addr, 102 ProtocolType proto, 103 const rtc::PacketTime& packet_time); 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_ 119