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/udpport.h" 35 #include "talk/p2p/base/stunrequest.h" 36 37 namespace talk_base { 38 class AsyncResolver; 39 class SignalThread; 40 } 41 42 namespace cricket { 43 44 extern const std::string STUN_PORT_TYPE; 45 46 // Communicates using the address on the outside of a NAT. 47 class StunPort : public Port { 48 public: 49 static StunPort* Create(talk_base::Thread* thread, 50 talk_base::PacketSocketFactory* factory, 51 talk_base::Network* network, 52 uint32 ip, int min_port, int max_port, 53 const talk_base::SocketAddress& server_addr) { 54 StunPort* port = new StunPort(thread, factory, network, 55 ip, min_port, max_port, server_addr); 56 if (!port->Init()) { 57 delete port; 58 port = NULL; 59 } 60 return port; 61 } 62 virtual ~StunPort(); 63 64 const talk_base::SocketAddress& server_addr() const { return server_addr_; } 65 void set_server_addr(const talk_base::SocketAddress& addr) { 66 server_addr_ = addr; 67 } 68 69 const talk_base::SocketAddress& server_addr2() const { return server_addr2_; } 70 void set_server_addr2(const talk_base::SocketAddress& addr) { 71 server_addr2_ = addr; 72 } 73 74 virtual void PrepareAddress(); 75 76 // This will contact the secondary server and signal another candidate 77 // address for this port (which may be the same as the first address). 78 void PrepareSecondaryAddress(); 79 80 virtual Connection* CreateConnection(const Candidate& address, 81 CandidateOrigin origin); 82 virtual int SetOption(talk_base::Socket::Option opt, int value); 83 virtual int GetError(); 84 85 protected: 86 StunPort(talk_base::Thread* thread, talk_base::PacketSocketFactory* factory, 87 talk_base::Network* network, uint32 ip, int min_port, int max_port, 88 const talk_base::SocketAddress& server_addr); 89 bool Init(); 90 91 virtual int SendTo(const void* data, size_t size, 92 const talk_base::SocketAddress& addr, bool payload); 93 94 void OnReadPacket(talk_base::AsyncPacketSocket* socket, 95 const char* data, size_t size, 96 const talk_base::SocketAddress& remote_addr); 97 98 private: 99 // DNS resolution of the STUN server. 100 void ResolveStunAddress(); 101 void OnResolveResult(talk_base::SignalThread* thread); 102 // Sends STUN requests to the server. 103 void OnSendPacket(const void* data, size_t size, StunRequest* req); 104 105 talk_base::SocketAddress server_addr_; 106 talk_base::SocketAddress server_addr2_; 107 StunRequestManager requests_; 108 talk_base::AsyncPacketSocket* socket_; 109 int error_; 110 talk_base::AsyncResolver* resolver_; 111 112 friend class StunPortBindingRequest; 113 }; 114 115 } // namespace cricket 116 117 #endif // TALK_P2P_BASE_STUNPORT_H_ 118