xref: /aosp_15_r20/external/webrtc/rtc_base/nat_socket_factory.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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 
11 #ifndef RTC_BASE_NAT_SOCKET_FACTORY_H_
12 #define RTC_BASE_NAT_SOCKET_FACTORY_H_
13 
14 #include <stddef.h>
15 
16 #include <map>
17 #include <memory>
18 #include <set>
19 
20 #include "rtc_base/nat_server.h"
21 #include "rtc_base/nat_types.h"
22 #include "rtc_base/socket.h"
23 #include "rtc_base/socket_address.h"
24 #include "rtc_base/socket_factory.h"
25 #include "rtc_base/socket_server.h"
26 #include "rtc_base/thread.h"
27 
28 namespace rtc {
29 
30 const size_t kNATEncodedIPv4AddressSize = 8U;
31 const size_t kNATEncodedIPv6AddressSize = 20U;
32 
33 // Used by the NAT socket implementation.
34 class NATInternalSocketFactory {
35  public:
~NATInternalSocketFactory()36   virtual ~NATInternalSocketFactory() {}
37   virtual Socket* CreateInternalSocket(int family,
38                                        int type,
39                                        const SocketAddress& local_addr,
40                                        SocketAddress* nat_addr) = 0;
41 };
42 
43 // Creates sockets that will send all traffic through a NAT, using an existing
44 // NATServer instance running at nat_addr. The actual data is sent using sockets
45 // from a socket factory, given to the constructor.
46 class NATSocketFactory : public SocketFactory, public NATInternalSocketFactory {
47  public:
48   NATSocketFactory(SocketFactory* factory,
49                    const SocketAddress& nat_udp_addr,
50                    const SocketAddress& nat_tcp_addr);
51 
52   NATSocketFactory(const NATSocketFactory&) = delete;
53   NATSocketFactory& operator=(const NATSocketFactory&) = delete;
54 
55   // SocketFactory implementation
56   Socket* CreateSocket(int family, int type) override;
57 
58   // NATInternalSocketFactory implementation
59   Socket* CreateInternalSocket(int family,
60                                int type,
61                                const SocketAddress& local_addr,
62                                SocketAddress* nat_addr) override;
63 
64  private:
65   SocketFactory* factory_;
66   SocketAddress nat_udp_addr_;
67   SocketAddress nat_tcp_addr_;
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;
88 
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:
102     Translator(NATSocketServer* server,
103                NATType type,
104                const SocketAddress& int_addr,
105                SocketFactory* ext_factory,
106                const SocketAddress& ext_addr);
107     ~Translator();
108 
internal_factory()109     SocketFactory* internal_factory() { return internal_server_.get(); }
internal_udp_address()110     SocketAddress internal_udp_address() const {
111       return nat_server_->internal_udp_address();
112     }
internal_tcp_address()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,
119                               const SocketAddress& int_ip,
120                               NATType type);
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<SocketServer> internal_server_;
132     std::unique_ptr<NATServer> nat_server_;
133     TranslatorMap nats_;
134     std::set<SocketAddress> clients_;
135   };
136 
137   explicit NATSocketServer(SocketServer* ss);
138 
139   NATSocketServer(const NATSocketServer&) = delete;
140   NATSocketServer& operator=(const NATSocketServer&) = delete;
141 
socketserver()142   SocketServer* socketserver() { return server_; }
queue()143   Thread* queue() { return msg_queue_; }
144 
145   Translator* GetTranslator(const SocketAddress& ext_ip);
146   Translator* AddTranslator(const SocketAddress& ext_ip,
147                             const SocketAddress& int_ip,
148                             NATType type);
149   void RemoveTranslator(const SocketAddress& ext_ip);
150 
151   // SocketServer implementation
152   Socket* CreateSocket(int family, int type) override;
153 
154   void SetMessageQueue(Thread* queue) override;
155   bool Wait(webrtc::TimeDelta max_wait_duration, bool process_io) override;
156   void WakeUp() override;
157 
158   // NATInternalSocketFactory implementation
159   Socket* CreateInternalSocket(int family,
160                                int type,
161                                const SocketAddress& local_addr,
162                                SocketAddress* nat_addr) override;
163 
164  private:
165   SocketServer* server_;
166   Thread* msg_queue_;
167   TranslatorMap nats_;
168 };
169 
170 // Free-standing NAT helper functions.
171 size_t PackAddressForNAT(char* buf,
172                          size_t buf_size,
173                          const SocketAddress& remote_addr);
174 size_t UnpackAddressFromNAT(const char* buf,
175                             size_t buf_size,
176                             SocketAddress* remote_addr);
177 }  // namespace rtc
178 
179 #endif  // RTC_BASE_NAT_SOCKET_FACTORY_H_
180