henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef RTC_BASE_NATSOCKETFACTORY_H_ |
| 12 | #define RTC_BASE_NATSOCKETFACTORY_H_ |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 13 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 14 | #include <string> |
| 15 | #include <map> |
| 16 | #include <memory> |
| 17 | #include <set> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 18 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 19 | #include "rtc_base/constructormagic.h" |
| 20 | #include "rtc_base/natserver.h" |
| 21 | #include "rtc_base/socketaddress.h" |
| 22 | #include "rtc_base/socketserver.h" |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 23 | |
| 24 | namespace rtc { |
| 25 | |
| 26 | const size_t kNATEncodedIPv4AddressSize = 8U; |
| 27 | const size_t kNATEncodedIPv6AddressSize = 20U; |
| 28 | |
| 29 | // Used by the NAT socket implementation. |
| 30 | class NATInternalSocketFactory { |
| 31 | public: |
| 32 | virtual ~NATInternalSocketFactory() {} |
| 33 | virtual AsyncSocket* CreateInternalSocket(int family, int type, |
| 34 | const SocketAddress& local_addr, SocketAddress* nat_addr) = 0; |
| 35 | }; |
| 36 | |
| 37 | // Creates sockets that will send all traffic through a NAT, using an existing |
| 38 | // NATServer instance running at nat_addr. The actual data is sent using sockets |
| 39 | // from a socket factory, given to the constructor. |
| 40 | class NATSocketFactory : public SocketFactory, public NATInternalSocketFactory { |
| 41 | public: |
| 42 | NATSocketFactory(SocketFactory* factory, const SocketAddress& nat_udp_addr, |
| 43 | const SocketAddress& nat_tcp_addr); |
| 44 | |
| 45 | // SocketFactory implementation |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 46 | Socket* CreateSocket(int family, int type) override; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 47 | AsyncSocket* CreateAsyncSocket(int family, int type) override; |
| 48 | |
| 49 | // NATInternalSocketFactory implementation |
| 50 | AsyncSocket* CreateInternalSocket(int family, |
| 51 | int type, |
| 52 | const SocketAddress& local_addr, |
| 53 | SocketAddress* nat_addr) override; |
| 54 | |
| 55 | private: |
| 56 | SocketFactory* factory_; |
| 57 | SocketAddress nat_udp_addr_; |
| 58 | SocketAddress nat_tcp_addr_; |
| 59 | RTC_DISALLOW_COPY_AND_ASSIGN(NATSocketFactory); |
| 60 | }; |
| 61 | |
| 62 | // Creates sockets that will send traffic through a NAT depending on what |
| 63 | // address they bind to. This can be used to simulate a client on a NAT sending |
| 64 | // to a client that is not behind a NAT. |
| 65 | // Note that the internal addresses of clients must be unique. This is because |
| 66 | // there is only one socketserver per thread, and the Bind() address is used to |
| 67 | // figure out which NAT (if any) the socket should talk to. |
| 68 | // |
| 69 | // Example with 3 NATs (2 cascaded), and 3 clients. |
| 70 | // ss->AddTranslator("1.2.3.4", "192.168.0.1", NAT_ADDR_RESTRICTED); |
| 71 | // ss->AddTranslator("99.99.99.99", "10.0.0.1", NAT_SYMMETRIC)-> |
| 72 | // AddTranslator("10.0.0.2", "192.168.1.1", NAT_OPEN_CONE); |
| 73 | // ss->GetTranslator("1.2.3.4")->AddClient("1.2.3.4", "192.168.0.2"); |
| 74 | // ss->GetTranslator("99.99.99.99")->AddClient("10.0.0.3"); |
| 75 | // ss->GetTranslator("99.99.99.99")->GetTranslator("10.0.0.2")-> |
| 76 | // AddClient("192.168.1.2"); |
| 77 | class NATSocketServer : public SocketServer, public NATInternalSocketFactory { |
| 78 | public: |
| 79 | class Translator; |
| 80 | // holds a list of NATs |
| 81 | class TranslatorMap : private std::map<SocketAddress, Translator*> { |
| 82 | public: |
| 83 | ~TranslatorMap(); |
| 84 | Translator* Get(const SocketAddress& ext_ip); |
| 85 | Translator* Add(const SocketAddress& ext_ip, Translator*); |
| 86 | void Remove(const SocketAddress& ext_ip); |
| 87 | Translator* FindClient(const SocketAddress& int_ip); |
| 88 | }; |
| 89 | |
| 90 | // a specific NAT |
| 91 | class Translator { |
| 92 | public: |
| 93 | Translator(NATSocketServer* server, NATType type, |
| 94 | const SocketAddress& int_addr, SocketFactory* ext_factory, |
| 95 | const SocketAddress& ext_addr); |
| 96 | ~Translator(); |
| 97 | |
| 98 | SocketFactory* internal_factory() { return internal_factory_.get(); } |
| 99 | SocketAddress internal_udp_address() const { |
| 100 | return nat_server_->internal_udp_address(); |
| 101 | } |
| 102 | SocketAddress internal_tcp_address() const { |
| 103 | return SocketAddress(); // nat_server_->internal_tcp_address(); |
| 104 | } |
| 105 | |
| 106 | Translator* GetTranslator(const SocketAddress& ext_ip); |
| 107 | Translator* AddTranslator(const SocketAddress& ext_ip, |
| 108 | const SocketAddress& int_ip, NATType type); |
| 109 | void RemoveTranslator(const SocketAddress& ext_ip); |
| 110 | |
| 111 | bool AddClient(const SocketAddress& int_ip); |
| 112 | void RemoveClient(const SocketAddress& int_ip); |
| 113 | |
| 114 | // Looks for the specified client in this or a child NAT. |
| 115 | Translator* FindClient(const SocketAddress& int_ip); |
| 116 | |
| 117 | private: |
| 118 | NATSocketServer* server_; |
| 119 | std::unique_ptr<SocketFactory> internal_factory_; |
| 120 | std::unique_ptr<NATServer> nat_server_; |
| 121 | TranslatorMap nats_; |
| 122 | std::set<SocketAddress> clients_; |
| 123 | }; |
| 124 | |
| 125 | explicit NATSocketServer(SocketServer* ss); |
| 126 | |
| 127 | SocketServer* socketserver() { return server_; } |
| 128 | MessageQueue* queue() { return msg_queue_; } |
| 129 | |
| 130 | Translator* GetTranslator(const SocketAddress& ext_ip); |
| 131 | Translator* AddTranslator(const SocketAddress& ext_ip, |
| 132 | const SocketAddress& int_ip, NATType type); |
| 133 | void RemoveTranslator(const SocketAddress& ext_ip); |
| 134 | |
| 135 | // SocketServer implementation |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 136 | Socket* CreateSocket(int family, int type) override; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 137 | AsyncSocket* CreateAsyncSocket(int family, int type) override; |
| 138 | |
| 139 | void SetMessageQueue(MessageQueue* queue) override; |
| 140 | bool Wait(int cms, bool process_io) override; |
| 141 | void WakeUp() override; |
| 142 | |
| 143 | // NATInternalSocketFactory implementation |
| 144 | AsyncSocket* CreateInternalSocket(int family, |
| 145 | int type, |
| 146 | const SocketAddress& local_addr, |
| 147 | SocketAddress* nat_addr) override; |
| 148 | |
| 149 | private: |
| 150 | SocketServer* server_; |
| 151 | MessageQueue* msg_queue_; |
| 152 | TranslatorMap nats_; |
| 153 | RTC_DISALLOW_COPY_AND_ASSIGN(NATSocketServer); |
| 154 | }; |
| 155 | |
| 156 | // Free-standing NAT helper functions. |
| 157 | size_t PackAddressForNAT(char* buf, size_t buf_size, |
| 158 | const SocketAddress& remote_addr); |
| 159 | size_t UnpackAddressFromNAT(const char* buf, size_t buf_size, |
| 160 | SocketAddress* remote_addr); |
| 161 | } // namespace rtc |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 162 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 163 | #endif // RTC_BASE_NATSOCKETFACTORY_H_ |