henrike@webrtc.org | 0e118e7 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * libjingle |
| 3 | * Copyright 2004--2005, Google Inc. |
| 4 | * |
wu@webrtc.org | 95cabf5 | 2013-10-23 23:56:09 +0000 | [diff] [blame] | 5 | * Redistribution and use in source and binary forms, with or without |
henrike@webrtc.org | 0e118e7 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 6 | * modification, are permitted provided that the following conditions are met: |
| 7 | * |
wu@webrtc.org | 95cabf5 | 2013-10-23 23:56:09 +0000 | [diff] [blame] | 8 | * 1. Redistributions of source code must retain the above copyright notice, |
henrike@webrtc.org | 0e118e7 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 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. |
wu@webrtc.org | 95cabf5 | 2013-10-23 23:56:09 +0000 | [diff] [blame] | 13 | * 3. The name of the author may not be used to endorse or promote products |
henrike@webrtc.org | 0e118e7 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 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 |
wu@webrtc.org | 95cabf5 | 2013-10-23 23:56:09 +0000 | [diff] [blame] | 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
henrike@webrtc.org | 0e118e7 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 18 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
wu@webrtc.org | 95cabf5 | 2013-10-23 23:56:09 +0000 | [diff] [blame] | 19 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
henrike@webrtc.org | 0e118e7 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 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, |
wu@webrtc.org | 95cabf5 | 2013-10-23 23:56:09 +0000 | [diff] [blame] | 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 |
henrike@webrtc.org | 0e118e7 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 25 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #ifndef TALK_BASE_NATSERVER_H_ |
| 29 | #define TALK_BASE_NATSERVER_H_ |
| 30 | |
| 31 | #include <map> |
| 32 | #include <set> |
| 33 | |
| 34 | #include "talk/base/asyncudpsocket.h" |
| 35 | #include "talk/base/socketaddresspair.h" |
| 36 | #include "talk/base/thread.h" |
| 37 | #include "talk/base/socketfactory.h" |
| 38 | #include "talk/base/nattypes.h" |
| 39 | |
| 40 | namespace talk_base { |
| 41 | |
| 42 | // Change how routes (socketaddress pairs) are compared based on the type of |
| 43 | // NAT. The NAT server maintains a hashtable of the routes that it knows |
| 44 | // about. So these affect which routes are treated the same. |
| 45 | struct RouteCmp { |
| 46 | explicit RouteCmp(NAT* nat); |
| 47 | size_t operator()(const SocketAddressPair& r) const; |
| 48 | bool operator()( |
| 49 | const SocketAddressPair& r1, const SocketAddressPair& r2) const; |
| 50 | |
| 51 | bool symmetric; |
| 52 | }; |
| 53 | |
| 54 | // Changes how addresses are compared based on the filtering rules of the NAT. |
| 55 | struct AddrCmp { |
| 56 | explicit AddrCmp(NAT* nat); |
| 57 | size_t operator()(const SocketAddress& r) const; |
| 58 | bool operator()(const SocketAddress& r1, const SocketAddress& r2) const; |
| 59 | |
| 60 | bool use_ip; |
| 61 | bool use_port; |
| 62 | }; |
| 63 | |
| 64 | // Implements the NAT device. It listens for packets on the internal network, |
| 65 | // translates them, and sends them out over the external network. |
| 66 | |
| 67 | const int NAT_SERVER_PORT = 4237; |
| 68 | |
| 69 | class NATServer : public sigslot::has_slots<> { |
| 70 | public: |
| 71 | NATServer( |
| 72 | NATType type, SocketFactory* internal, const SocketAddress& internal_addr, |
| 73 | SocketFactory* external, const SocketAddress& external_ip); |
| 74 | ~NATServer(); |
| 75 | |
| 76 | SocketAddress internal_address() const { |
| 77 | return server_socket_->GetLocalAddress(); |
| 78 | } |
| 79 | |
| 80 | // Packets received on one of the networks. |
| 81 | void OnInternalPacket(AsyncPacketSocket* socket, const char* buf, |
wu@webrtc.org | f89a403 | 2013-12-13 00:21:03 +0000 | [diff] [blame] | 82 | size_t size, const SocketAddress& addr, |
| 83 | const PacketTime& packet_time); |
henrike@webrtc.org | 0e118e7 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 84 | void OnExternalPacket(AsyncPacketSocket* socket, const char* buf, |
wu@webrtc.org | f89a403 | 2013-12-13 00:21:03 +0000 | [diff] [blame] | 85 | size_t size, const SocketAddress& remote_addr, |
| 86 | const PacketTime& packet_time); |
henrike@webrtc.org | 0e118e7 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 87 | |
| 88 | private: |
| 89 | typedef std::set<SocketAddress, AddrCmp> AddressSet; |
| 90 | |
| 91 | /* Records a translation and the associated external socket. */ |
| 92 | struct TransEntry { |
| 93 | TransEntry(const SocketAddressPair& r, AsyncUDPSocket* s, NAT* nat); |
| 94 | ~TransEntry(); |
| 95 | |
wu@webrtc.org | 95cabf5 | 2013-10-23 23:56:09 +0000 | [diff] [blame] | 96 | void WhitelistInsert(const SocketAddress& addr); |
| 97 | bool WhitelistContains(const SocketAddress& ext_addr); |
| 98 | |
henrike@webrtc.org | 0e118e7 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 99 | SocketAddressPair route; |
| 100 | AsyncUDPSocket* socket; |
| 101 | AddressSet* whitelist; |
wu@webrtc.org | 95cabf5 | 2013-10-23 23:56:09 +0000 | [diff] [blame] | 102 | CriticalSection crit_; |
henrike@webrtc.org | 0e118e7 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | typedef std::map<SocketAddressPair, TransEntry*, RouteCmp> InternalMap; |
| 106 | typedef std::map<SocketAddress, TransEntry*> ExternalMap; |
| 107 | |
| 108 | /* Creates a new entry that translates the given route. */ |
| 109 | void Translate(const SocketAddressPair& route); |
| 110 | |
| 111 | /* Determines whether the NAT would filter out a packet from this address. */ |
wu@webrtc.org | 95cabf5 | 2013-10-23 23:56:09 +0000 | [diff] [blame] | 112 | bool ShouldFilterOut(TransEntry* entry, const SocketAddress& ext_addr); |
henrike@webrtc.org | 0e118e7 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 113 | |
| 114 | NAT* nat_; |
| 115 | SocketFactory* internal_; |
| 116 | SocketFactory* external_; |
| 117 | SocketAddress external_ip_; |
| 118 | AsyncUDPSocket* server_socket_; |
| 119 | AsyncSocket* tcp_server_socket_; |
| 120 | InternalMap* int_map_; |
| 121 | ExternalMap* ext_map_; |
| 122 | DISALLOW_EVIL_CONSTRUCTORS(NATServer); |
| 123 | }; |
| 124 | |
| 125 | } // namespace talk_base |
| 126 | |
| 127 | #endif // TALK_BASE_NATSERVER_H_ |