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