1f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch/* 2f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch * libjingle 3f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch * Copyright 2004--2005, Google Inc. 4f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch * 5f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch * Redistribution and use in source and binary forms, with or without 6f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch * modification, are permitted provided that the following conditions are met: 7f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch * 8f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch * 1. Redistributions of source code must retain the above copyright notice, 9f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch * this list of conditions and the following disclaimer. 10f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch * 2. Redistributions in binary form must reproduce the above copyright notice, 11f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch * this list of conditions and the following disclaimer in the documentation 12f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch * and/or other materials provided with the distribution. 13f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch * 3. The name of the author may not be used to endorse or promote products 14f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch * derived from this software without specific prior written permission. 15f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch * 16f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 17f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 18f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 19f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 21f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 22f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 24f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 25f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch */ 27f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch 28f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch#ifndef TALK_P2P_BASE_STUNPORT_H_ 29f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch#define TALK_P2P_BASE_STUNPORT_H_ 30f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch 31f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch#include <string> 32dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen 33dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen#include "talk/base/asyncpacketsocket.h" 34f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch#include "talk/p2p/base/udpport.h" 35f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch#include "talk/p2p/base/stunrequest.h" 36f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch 37f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdochnamespace talk_base { 38f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdochclass AsyncResolver; 39f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdochclass SignalThread; 40f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch} 41f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch 42f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdochnamespace cricket { 43f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch 44f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdochextern const std::string STUN_PORT_TYPE; 45f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch 46f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch// Communicates using the address on the outside of a NAT. 47f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdochclass StunPort : public Port { 48f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch public: 49f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch static StunPort* Create(talk_base::Thread* thread, 50dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen talk_base::PacketSocketFactory* factory, 51f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch talk_base::Network* network, 52dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen uint32 ip, int min_port, int max_port, 53f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch const talk_base::SocketAddress& server_addr) { 54dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen StunPort* port = new StunPort(thread, factory, network, 55dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen ip, min_port, max_port, server_addr); 56dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen if (!port->Init()) { 57f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch delete port; 58f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch port = NULL; 59f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch } 60f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch return port; 61f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch } 62f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch virtual ~StunPort(); 63f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch 64f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch const talk_base::SocketAddress& server_addr() const { return server_addr_; } 65f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch void set_server_addr(const talk_base::SocketAddress& addr) { 66f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch server_addr_ = addr; 67f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch } 68f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch 69f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch const talk_base::SocketAddress& server_addr2() const { return server_addr2_; } 70f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch void set_server_addr2(const talk_base::SocketAddress& addr) { 71f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch server_addr2_ = addr; 72f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch } 73f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch 74f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch virtual void PrepareAddress(); 75f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch 76f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch // This will contact the secondary server and signal another candidate 77f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch // address for this port (which may be the same as the first address). 78f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch void PrepareSecondaryAddress(); 79f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch 80f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch virtual Connection* CreateConnection(const Candidate& address, 81f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch CandidateOrigin origin); 82f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch virtual int SetOption(talk_base::Socket::Option opt, int value); 83f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch virtual int GetError(); 84f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch 85f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch protected: 86dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen StunPort(talk_base::Thread* thread, talk_base::PacketSocketFactory* factory, 87dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen talk_base::Network* network, uint32 ip, int min_port, int max_port, 88dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen const talk_base::SocketAddress& server_addr); 89dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen bool Init(); 90dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen 91f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch virtual int SendTo(const void* data, size_t size, 92f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch const talk_base::SocketAddress& addr, bool payload); 93f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch 94dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen void OnReadPacket(talk_base::AsyncPacketSocket* socket, 95dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen const char* data, size_t size, 96dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen const talk_base::SocketAddress& remote_addr); 97f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch 98f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch private: 99f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch // DNS resolution of the STUN server. 100f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch void ResolveStunAddress(); 101f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch void OnResolveResult(talk_base::SignalThread* thread); 102f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch // Sends STUN requests to the server. 103f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch void OnSendPacket(const void* data, size_t size, StunRequest* req); 104f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch 105f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch talk_base::SocketAddress server_addr_; 106f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch talk_base::SocketAddress server_addr2_; 107f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch StunRequestManager requests_; 108f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch talk_base::AsyncPacketSocket* socket_; 109f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch int error_; 110f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch talk_base::AsyncResolver* resolver_; 111f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch 112f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch friend class StunPortBindingRequest; 113f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch}; 114f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch 115f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch} // namespace cricket 116f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch 117f74420b3285b9fe04a7e00aa3b8c0ab07ea344bcBen Murdoch#endif // TALK_P2P_BASE_STUNPORT_H_ 118