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