3 * Copyright 2004--2005, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
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.
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.
28 #ifndef TALK_P2P_BASE_RELAYPORT_H_
29 #define TALK_P2P_BASE_RELAYPORT_H_
36 #include "talk/p2p/base/port.h"
37 #include "talk/p2p/base/stunrequest.h"
42 class RelayConnection;
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 {
52 typedef std::pair<rtc::Socket::Option, int> OptionValue;
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,
65 void AddServerAddress(const ProtocolAddress& addr);
66 void AddExternalAddress(const ProtocolAddress& addr);
68 const std::vector<OptionValue>& options() const { return options_; }
69 bool HasMagicCookie(const char* data, size_t size);
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();
78 const ProtocolAddress * ServerAddress(size_t index) const;
79 bool IsReady() { return ready_; }
82 sigslot::signal1<const ProtocolAddress*> SignalConnectFailure;
83 sigslot::signal1<const ProtocolAddress*> SignalSoftTimeout;
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);
94 virtual int SendTo(const void* data, size_t size,
95 const rtc::SocketAddress& addr,
96 const rtc::PacketOptions& options,
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,
103 const rtc::PacketTime& packet_time);
106 friend class RelayEntry;
108 std::deque<ProtocolAddress> server_addr_;
109 std::vector<ProtocolAddress> external_addr_;
111 std::vector<RelayEntry*> entries_;
112 std::vector<OptionValue> options_;
116 } // namespace cricket
118 #endif // TALK_P2P_BASE_RELAYPORT_H_