2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
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.
11 #ifndef WEBRTC_P2P_BASE_STUNPORT_H_
12 #define WEBRTC_P2P_BASE_STUNPORT_H_
16 #include "webrtc/p2p/base/port.h"
17 #include "webrtc/p2p/base/stunrequest.h"
18 #include "webrtc/base/asyncpacketsocket.h"
20 // TODO(mallinath) - Rename stunport.cc|h to udpport.cc|h.
28 // Communicates using the address on the outside of a NAT.
29 class UDPPort : public Port {
31 static UDPPort* Create(rtc::Thread* thread,
32 rtc::PacketSocketFactory* factory,
33 rtc::Network* network,
34 rtc::AsyncPacketSocket* socket,
35 const std::string& username,
36 const std::string& password) {
38 new UDPPort(thread, factory, network, socket, username, password);
46 static UDPPort* Create(rtc::Thread* thread,
47 rtc::PacketSocketFactory* factory,
48 rtc::Network* network,
49 const rtc::IPAddress& ip,
52 const std::string& username,
53 const std::string& password) {
54 UDPPort* port = new UDPPort(thread, factory, network, ip, min_port,
55 max_port, username, password);
64 rtc::SocketAddress GetLocalAddress() const {
65 return socket_->GetLocalAddress();
68 const ServerAddresses& server_addresses() const {
69 return server_addresses_;
72 set_server_addresses(const ServerAddresses& addresses) {
73 server_addresses_ = addresses;
76 virtual void PrepareAddress();
78 virtual Connection* CreateConnection(const Candidate& address,
79 CandidateOrigin origin);
80 virtual int SetOption(rtc::Socket::Option opt, int value);
81 virtual int GetOption(rtc::Socket::Option opt, int* value);
82 virtual int GetError();
84 virtual bool HandleIncomingPacket(
85 rtc::AsyncPacketSocket* socket, const char* data, size_t size,
86 const rtc::SocketAddress& remote_addr,
87 const rtc::PacketTime& packet_time) {
88 // All packets given to UDP port will be consumed.
89 OnReadPacket(socket, data, size, remote_addr, packet_time);
93 void set_stun_keepalive_delay(int delay) {
94 stun_keepalive_delay_ = delay;
96 int stun_keepalive_delay() const {
97 return stun_keepalive_delay_;
101 UDPPort(rtc::Thread* thread,
102 rtc::PacketSocketFactory* factory,
103 rtc::Network* network,
104 const rtc::IPAddress& ip,
107 const std::string& username,
108 const std::string& password);
110 UDPPort(rtc::Thread* thread,
111 rtc::PacketSocketFactory* factory,
112 rtc::Network* network,
113 rtc::AsyncPacketSocket* socket,
114 const std::string& username,
115 const std::string& password);
119 virtual int SendTo(const void* data, size_t size,
120 const rtc::SocketAddress& addr,
121 const rtc::PacketOptions& options,
124 void OnLocalAddressReady(rtc::AsyncPacketSocket* socket,
125 const rtc::SocketAddress& address);
126 void OnReadPacket(rtc::AsyncPacketSocket* socket,
127 const char* data, size_t size,
128 const rtc::SocketAddress& remote_addr,
129 const rtc::PacketTime& packet_time);
131 void OnReadyToSend(rtc::AsyncPacketSocket* socket);
133 // This method will send STUN binding request if STUN server address is set.
134 void MaybePrepareStunCandidate();
136 void SendStunBindingRequests();
139 // A helper class which can be called repeatedly to resolve multiple
140 // addresses, as opposed to rtc::AsyncResolverInterface, which can only
141 // resolve one address per instance.
142 class AddressResolver : public sigslot::has_slots<> {
144 explicit AddressResolver(rtc::PacketSocketFactory* factory);
147 void Resolve(const rtc::SocketAddress& address);
148 bool GetResolvedAddress(const rtc::SocketAddress& input,
150 rtc::SocketAddress* output) const;
152 // The signal is sent when resolving the specified address is finished. The
153 // first argument is the input address, the second argument is the error
154 // or 0 if it succeeded.
155 sigslot::signal2<const rtc::SocketAddress&, int> SignalDone;
158 typedef std::map<rtc::SocketAddress,
159 rtc::AsyncResolverInterface*> ResolverMap;
161 void OnResolveResult(rtc::AsyncResolverInterface* resolver);
163 rtc::PacketSocketFactory* socket_factory_;
164 ResolverMap resolvers_;
167 // DNS resolution of the STUN server.
168 void ResolveStunAddress(const rtc::SocketAddress& stun_addr);
169 void OnResolveResult(const rtc::SocketAddress& input, int error);
171 void SendStunBindingRequest(const rtc::SocketAddress& stun_addr);
173 // Below methods handles binding request responses.
174 void OnStunBindingRequestSucceeded(
175 const rtc::SocketAddress& stun_server_addr,
176 const rtc::SocketAddress& stun_reflected_addr);
177 void OnStunBindingOrResolveRequestFailed(
178 const rtc::SocketAddress& stun_server_addr);
180 // Sends STUN requests to the server.
181 void OnSendPacket(const void* data, size_t size, StunRequest* req);
183 // TODO(mallinaht) - Move this up to cricket::Port when SignalAddressReady is
184 // changed to SignalPortReady.
185 void MaybeSetPortCompleteOrError();
187 bool HasCandidateWithAddress(const rtc::SocketAddress& addr) const;
189 ServerAddresses server_addresses_;
190 ServerAddresses bind_request_succeeded_servers_;
191 ServerAddresses bind_request_failed_servers_;
192 StunRequestManager requests_;
193 rtc::AsyncPacketSocket* socket_;
195 rtc::scoped_ptr<AddressResolver> resolver_;
197 int stun_keepalive_delay_;
199 friend class StunBindingRequest;
202 class StunPort : public UDPPort {
204 static StunPort* Create(rtc::Thread* thread,
205 rtc::PacketSocketFactory* factory,
206 rtc::Network* network,
207 const rtc::IPAddress& ip,
208 uint16 min_port, uint16 max_port,
209 const std::string& username,
210 const std::string& password,
211 const ServerAddresses& servers) {
212 StunPort* port = new StunPort(thread, factory, network, ip, min_port,
213 max_port, username, password, servers);
221 virtual ~StunPort() {}
223 virtual void PrepareAddress() {
224 SendStunBindingRequests();
228 StunPort(rtc::Thread* thread,
229 rtc::PacketSocketFactory* factory,
230 rtc::Network* network,
231 const rtc::IPAddress& ip,
234 const std::string& username,
235 const std::string& password,
236 const ServerAddresses& servers)
237 : UDPPort(thread, factory, network, ip, min_port, max_port, username,
239 // UDPPort will set these to local udp, updating these to STUN.
240 set_type(STUN_PORT_TYPE);
241 set_server_addresses(servers);
245 } // namespace cricket
247 #endif // WEBRTC_P2P_BASE_STUNPORT_H_