• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 extern const std::string RELAY_PORT_TYPE;
42 class RelayEntry;
43 class RelayConnection;
44 
45 // Communicates using an allocated port on the relay server. For each
46 // remote candidate that we try to send data to a RelayEntry instance
47 // is created. The RelayEntry will try to reach the remote destination
48 // by connecting to all available server addresses in a pre defined
49 // order with a small delay in between. When a connection is
50 // successful all other connection attemts are aborted.
51 class RelayPort : public Port {
52  public:
53   typedef std::pair<talk_base::Socket::Option, int> OptionValue;
54 
55   // RelayPort doesn't yet do anything fancy in the ctor.
Create(talk_base::Thread * thread,talk_base::PacketSocketFactory * factory,talk_base::Network * network,uint32 ip,int min_port,int max_port,const std::string & username,const std::string & password,const std::string & magic_cookie)56   static RelayPort* Create(
57       talk_base::Thread* thread, talk_base::PacketSocketFactory* factory,
58       talk_base::Network* network, uint32 ip, int min_port, int max_port,
59       const std::string& username, const std::string& password,
60       const std::string& magic_cookie) {
61     return new RelayPort(thread, factory, network, ip, min_port, max_port,
62                          username, password, magic_cookie);
63   }
64   virtual ~RelayPort();
65 
66   void AddServerAddress(const ProtocolAddress& addr);
67   void AddExternalAddress(const ProtocolAddress& addr);
68 
options()69   const std::vector<OptionValue>& options() const { return options_; }
magic_cookie()70   const std::string& magic_cookie() const { return magic_cookie_; }
71   bool HasMagicCookie(const char* data, size_t size);
72 
73   virtual void PrepareAddress();
74   virtual Connection* CreateConnection(const Candidate& address,
75                                        CandidateOrigin origin);
76   virtual int SetOption(talk_base::Socket::Option opt, int value);
77   virtual int GetError();
78 
79   const ProtocolAddress * ServerAddress(size_t index) const;
IsReady()80   bool IsReady() { return ready_; }
81 
82   // Used for testing.
83   sigslot::signal1<const ProtocolAddress*> SignalConnectFailure;
84   sigslot::signal1<const ProtocolAddress*> SignalSoftTimeout;
85 
86  protected:
87   RelayPort(talk_base::Thread* thread, talk_base::PacketSocketFactory* factory,
88             talk_base::Network*, uint32 ip, int min_port, int max_port,
89             const std::string& username, const std::string& password,
90             const std::string& magic_cookie);
91   bool Init();
92 
93   void SetReady();
94 
95   virtual int SendTo(const void* data, size_t size,
96                      const talk_base::SocketAddress& addr, bool payload);
97 
98   // Dispatches the given packet to the port or connection as appropriate.
99   void OnReadPacket(const char* data, size_t size,
100                     const talk_base::SocketAddress& remote_addr);
101 
102  private:
103   friend class RelayEntry;
104 
105   std::deque<ProtocolAddress> server_addr_;
106   bool ready_;
107   std::vector<RelayEntry*> entries_;
108   std::vector<OptionValue> options_;
109   std::string magic_cookie_;
110   int error_;
111 };
112 
113 }  // namespace cricket
114 
115 #endif  // TALK_P2P_BASE_RELAYPORT_H_
116