Upstream version 11.40.271.0
[platform/framework/web/crosswalk.git] / src / third_party / webrtc / p2p / base / stunport.h
1 /*
2  *  Copyright 2004 The WebRTC Project Authors. All rights reserved.
3  *
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.
9  */
10
11 #ifndef WEBRTC_P2P_BASE_STUNPORT_H_
12 #define WEBRTC_P2P_BASE_STUNPORT_H_
13
14 #include <string>
15
16 #include "webrtc/p2p/base/port.h"
17 #include "webrtc/p2p/base/stunrequest.h"
18 #include "webrtc/base/asyncpacketsocket.h"
19
20 // TODO(mallinath) - Rename stunport.cc|h to udpport.cc|h.
21 namespace rtc {
22 class AsyncResolver;
23 class SignalThread;
24 }
25
26 namespace cricket {
27
28 // Communicates using the address on the outside of a NAT.
29 class UDPPort : public Port {
30  public:
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) {
37     UDPPort* port =
38         new UDPPort(thread, factory, network, socket, username, password);
39     if (!port->Init()) {
40       delete port;
41       port = NULL;
42     }
43     return port;
44   }
45
46   static UDPPort* Create(rtc::Thread* thread,
47                          rtc::PacketSocketFactory* factory,
48                          rtc::Network* network,
49                          const rtc::IPAddress& ip,
50                          uint16 min_port,
51                          uint16 max_port,
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);
56     if (!port->Init()) {
57       delete port;
58       port = NULL;
59     }
60     return port;
61   }
62   virtual ~UDPPort();
63
64   rtc::SocketAddress GetLocalAddress() const {
65     return socket_->GetLocalAddress();
66   }
67
68   const ServerAddresses& server_addresses() const {
69     return server_addresses_;
70   }
71   void
72   set_server_addresses(const ServerAddresses& addresses) {
73     server_addresses_ = addresses;
74   }
75
76   virtual void PrepareAddress();
77
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();
83
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);
90     return true;
91   }
92
93   void set_stun_keepalive_delay(int delay) {
94     stun_keepalive_delay_ = delay;
95   }
96   int stun_keepalive_delay() const {
97     return stun_keepalive_delay_;
98   }
99
100  protected:
101   UDPPort(rtc::Thread* thread,
102           rtc::PacketSocketFactory* factory,
103           rtc::Network* network,
104           const rtc::IPAddress& ip,
105           uint16 min_port,
106           uint16 max_port,
107           const std::string& username,
108           const std::string& password);
109
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);
116
117   bool Init();
118
119   virtual int SendTo(const void* data, size_t size,
120                      const rtc::SocketAddress& addr,
121                      const rtc::PacketOptions& options,
122                      bool payload);
123
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);
130
131   void OnReadyToSend(rtc::AsyncPacketSocket* socket);
132
133   // This method will send STUN binding request if STUN server address is set.
134   void MaybePrepareStunCandidate();
135
136   void SendStunBindingRequests();
137
138  private:
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<> {
143    public:
144     explicit AddressResolver(rtc::PacketSocketFactory* factory);
145     ~AddressResolver();
146
147     void Resolve(const rtc::SocketAddress& address);
148     bool GetResolvedAddress(const rtc::SocketAddress& input,
149                             int family,
150                             rtc::SocketAddress* output) const;
151
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;
156
157    private:
158     typedef std::map<rtc::SocketAddress,
159                      rtc::AsyncResolverInterface*> ResolverMap;
160
161     void OnResolveResult(rtc::AsyncResolverInterface* resolver);
162
163     rtc::PacketSocketFactory* socket_factory_;
164     ResolverMap resolvers_;
165   };
166
167   // DNS resolution of the STUN server.
168   void ResolveStunAddress(const rtc::SocketAddress& stun_addr);
169   void OnResolveResult(const rtc::SocketAddress& input, int error);
170
171   void SendStunBindingRequest(const rtc::SocketAddress& stun_addr);
172
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);
179
180   // Sends STUN requests to the server.
181   void OnSendPacket(const void* data, size_t size, StunRequest* req);
182
183   // TODO(mallinaht) - Move this up to cricket::Port when SignalAddressReady is
184   // changed to SignalPortReady.
185   void MaybeSetPortCompleteOrError();
186
187   bool HasCandidateWithAddress(const rtc::SocketAddress& addr) const;
188
189   ServerAddresses server_addresses_;
190   ServerAddresses bind_request_succeeded_servers_;
191   ServerAddresses bind_request_failed_servers_;
192   StunRequestManager requests_;
193   rtc::AsyncPacketSocket* socket_;
194   int error_;
195   rtc::scoped_ptr<AddressResolver> resolver_;
196   bool ready_;
197   int stun_keepalive_delay_;
198
199   friend class StunBindingRequest;
200 };
201
202 class StunPort : public UDPPort {
203  public:
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);
214     if (!port->Init()) {
215       delete port;
216       port = NULL;
217     }
218     return port;
219   }
220
221   virtual ~StunPort() {}
222
223   virtual void PrepareAddress() {
224     SendStunBindingRequests();
225   }
226
227  protected:
228   StunPort(rtc::Thread* thread,
229            rtc::PacketSocketFactory* factory,
230            rtc::Network* network,
231            const rtc::IPAddress& ip,
232            uint16 min_port,
233            uint16 max_port,
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,
238                password) {
239     // UDPPort will set these to local udp, updating these to STUN.
240     set_type(STUN_PORT_TYPE);
241     set_server_addresses(servers);
242   }
243 };
244
245 }  // namespace cricket
246
247 #endif  // WEBRTC_P2P_BASE_STUNPORT_H_