Note: We no longer publish the latest version of our code here. We primarily use a kumc-bmi github organization. The heron ETL repository, in particular, is not public. Peers in the informatics community should see MultiSiteDev for details on requesting access.

source: webrtc/talk/p2p/base/stunport.h @ 0:4bda6873e34c

pub_scrub_3792 tip
Last change on this file since 0:4bda6873e34c was 0:4bda6873e34c, checked in by Michael Prittie <mprittie@…>, 6 years ago

Scrubbed password for publication.

File size: 7.6 KB
Line 
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_STUNPORT_H_
29#define TALK_P2P_BASE_STUNPORT_H_
30
31#include <string>
32
33#include "talk/base/asyncpacketsocket.h"
34#include "talk/p2p/base/port.h"
35#include "talk/p2p/base/stunrequest.h"
36
37// TODO(mallinath) - Rename stunport.cc|h to udpport.cc|h.
38namespace talk_base {
39class AsyncResolver;
40class SignalThread;
41}
42
43namespace cricket {
44
45// Communicates using the address on the outside of a NAT.
46class UDPPort : public Port {
47 public:
48  static UDPPort* Create(talk_base::Thread* thread,
49                         talk_base::PacketSocketFactory* factory,
50                         talk_base::Network* network,
51                         talk_base::AsyncPacketSocket* socket,
52                         const std::string& username,
53                         const std::string& password) {
54    UDPPort* port = new UDPPort(thread, factory, network, socket,
55                                username, password);
56    if (!port->Init()) {
57      delete port;
58      port = NULL;
59    }
60    return port;
61  }
62
63  static UDPPort* Create(talk_base::Thread* thread,
64                         talk_base::PacketSocketFactory* factory,
65                         talk_base::Network* network,
66                         const talk_base::IPAddress& ip,
67                         int min_port, int max_port,
68                         const std::string& username,
69                         const std::string& password) {
70    UDPPort* port = new UDPPort(thread, factory, network,
71                                ip, min_port, max_port,
72                                username, password);
73    if (!port->Init()) {
74      delete port;
75      port = NULL;
76    }
77    return port;
78  }
79  virtual ~UDPPort();
80
81  talk_base::SocketAddress GetLocalAddress() const {
82    return socket_->GetLocalAddress();
83  }
84
85  const talk_base::SocketAddress& server_addr() const { return server_addr_; }
86  void set_server_addr(const talk_base::SocketAddress& addr) {
87    server_addr_ = addr;
88  }
89
90  virtual void PrepareAddress();
91
92  virtual Connection* CreateConnection(const Candidate& address,
93                                       CandidateOrigin origin);
94  virtual int SetOption(talk_base::Socket::Option opt, int value);
95  virtual int GetOption(talk_base::Socket::Option opt, int* value);
96  virtual int GetError();
97
98  virtual bool HandleIncomingPacket(
99      talk_base::AsyncPacketSocket* socket, const char* data, size_t size,
100      const talk_base::SocketAddress& remote_addr) {
101    // All packets given to UDP port will be consumed.
102    OnReadPacket(socket, data, size, remote_addr);
103    return true;
104  }
105
106  void set_stun_keepalive_delay(int delay) {
107    stun_keepalive_delay_ = delay;
108  }
109  int stun_keepalive_delay() const {
110    return stun_keepalive_delay_;
111  }
112
113 protected:
114  UDPPort(talk_base::Thread* thread, talk_base::PacketSocketFactory* factory,
115          talk_base::Network* network, const talk_base::IPAddress& ip,
116          int min_port, int max_port,
117          const std::string& username, const std::string& password);
118
119  UDPPort(talk_base::Thread* thread, talk_base::PacketSocketFactory* factory,
120          talk_base::Network* network, talk_base::AsyncPacketSocket* socket,
121          const std::string& username, const std::string& password);
122
123  bool Init();
124
125  virtual int SendTo(const void* data, size_t size,
126                     const talk_base::SocketAddress& addr,
127                     talk_base::DiffServCodePoint dscp,
128                     bool payload);
129
130  void OnLocalAddressReady(talk_base::AsyncPacketSocket* socket,
131                           const talk_base::SocketAddress& address);
132  void OnReadPacket(talk_base::AsyncPacketSocket* socket,
133                    const char* data, size_t size,
134                    const talk_base::SocketAddress& remote_addr);
135  void OnReadyToSend(talk_base::AsyncPacketSocket* socket);
136
137  // This method will send STUN binding request if STUN server address is set.
138  void MaybePrepareStunCandidate();
139
140  void SendStunBindingRequest();
141
142
143 private:
144  // DNS resolution of the STUN server.
145  void ResolveStunAddress();
146  void OnResolveResult(talk_base::AsyncResolverInterface* resolver);
147
148  // Below methods handles binding request responses.
149  void OnStunBindingRequestSucceeded(const talk_base::SocketAddress& stun_addr);
150  void OnStunBindingOrResolveRequestFailed();
151
152  // Sends STUN requests to the server.
153  void OnSendPacket(const void* data, size_t size, StunRequest* req);
154
155  // TODO(mallinaht) - Move this up to cricket::Port when SignalAddressReady is
156  // changed to SignalPortReady.
157  void SetResult(bool success);
158
159  talk_base::SocketAddress server_addr_;
160  StunRequestManager requests_;
161  talk_base::AsyncPacketSocket* socket_;
162  int error_;
163  talk_base::AsyncResolverInterface* resolver_;
164  bool ready_;
165  int stun_keepalive_delay_;
166
167  friend class StunBindingRequest;
168};
169
170class StunPort : public UDPPort {
171 public:
172  static StunPort* Create(talk_base::Thread* thread,
173                          talk_base::PacketSocketFactory* factory,
174                          talk_base::Network* network,
175                          const talk_base::IPAddress& ip,
176                          int min_port, int max_port,
177                          const std::string& username,
178                          const std::string& password,
179                          const talk_base::SocketAddress& server_addr) {
180    StunPort* port = new StunPort(thread, factory, network,
181                                  ip, min_port, max_port,
182                                  username, password, server_addr);
183    if (!port->Init()) {
184      delete port;
185      port = NULL;
186    }
187    return port;
188  }
189
190  virtual ~StunPort() {}
191
192  virtual void PrepareAddress() {
193    SendStunBindingRequest();
194  }
195
196 protected:
197  StunPort(talk_base::Thread* thread, talk_base::PacketSocketFactory* factory,
198           talk_base::Network* network, const talk_base::IPAddress& ip,
199           int min_port, int max_port,
200           const std::string& username, const std::string& password,
201           const talk_base::SocketAddress& server_address)
202     : UDPPort(thread, factory, network, ip, min_port, max_port, username,
203               password) {
204    // UDPPort will set these to local udp, updating these to STUN.
205    set_type(STUN_PORT_TYPE);
206    set_server_addr(server_address);
207  }
208};
209
210}  // namespace cricket
211
212#endif  // TALK_P2P_BASE_STUNPORT_H_
Note: See TracBrowser for help on using the repository browser.