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