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_NATSOCKETFACTORY_H_
12 #define RTC_BASE_NATSOCKETFACTORY_H_
13 
14 #include <string>
15 #include <map>
16 #include <memory>
17 #include <set>
18 
19 #include "rtc_base/constructormagic.h"
20 #include "rtc_base/natserver.h"
21 #include "rtc_base/socketaddress.h"
22 #include "rtc_base/socketserver.h"
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:
~NATInternalSocketFactory()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
46   Socket* CreateSocket(int type) override;
47   Socket* CreateSocket(int family, int type) override;
48   AsyncSocket* CreateAsyncSocket(int type) override;
49   AsyncSocket* CreateAsyncSocket(int family, int type) override;
50 
51   // NATInternalSocketFactory implementation
52   AsyncSocket* CreateInternalSocket(int family,
53                                     int type,
54                                     const SocketAddress& local_addr,
55                                     SocketAddress* nat_addr) override;
56 
57  private:
58   SocketFactory* factory_;
59   SocketAddress nat_udp_addr_;
60   SocketAddress nat_tcp_addr_;
61   RTC_DISALLOW_COPY_AND_ASSIGN(NATSocketFactory);
62 };
63 
64 // Creates sockets that will send traffic through a NAT depending on what
65 // address they bind to. This can be used to simulate a client on a NAT sending
66 // to a client that is not behind a NAT.
67 // Note that the internal addresses of clients must be unique. This is because
68 // there is only one socketserver per thread, and the Bind() address is used to
69 // figure out which NAT (if any) the socket should talk to.
70 //
71 // Example with 3 NATs (2 cascaded), and 3 clients.
72 // ss->AddTranslator("1.2.3.4", "192.168.0.1", NAT_ADDR_RESTRICTED);
73 // ss->AddTranslator("99.99.99.99", "10.0.0.1", NAT_SYMMETRIC)->
74 //     AddTranslator("10.0.0.2", "192.168.1.1", NAT_OPEN_CONE);
75 // ss->GetTranslator("1.2.3.4")->AddClient("1.2.3.4", "192.168.0.2");
76 // ss->GetTranslator("99.99.99.99")->AddClient("10.0.0.3");
77 // ss->GetTranslator("99.99.99.99")->GetTranslator("10.0.0.2")->
78 //     AddClient("192.168.1.2");
79 class NATSocketServer : public SocketServer, public NATInternalSocketFactory {
80  public:
81   class Translator;
82   // holds a list of NATs
83   class TranslatorMap : private std::map<SocketAddress, Translator*> {
84    public:
85     ~TranslatorMap();
86     Translator* Get(const SocketAddress& ext_ip);
87     Translator* Add(const SocketAddress& ext_ip, Translator*);
88     void Remove(const SocketAddress& ext_ip);
89     Translator* FindClient(const SocketAddress& int_ip);
90   };
91 
92   // a specific NAT
93   class Translator {
94    public:
95     Translator(NATSocketServer* server, NATType type,
96                const SocketAddress& int_addr, SocketFactory* ext_factory,
97                const SocketAddress& ext_addr);
98     ~Translator();
99 
internal_factory()100     SocketFactory* internal_factory() { return internal_factory_.get(); }
internal_udp_address()101     SocketAddress internal_udp_address() const {
102       return nat_server_->internal_udp_address();
103     }
internal_tcp_address()104     SocketAddress internal_tcp_address() const {
105       return SocketAddress();  // nat_server_->internal_tcp_address();
106     }
107 
108     Translator* GetTranslator(const SocketAddress& ext_ip);
109     Translator* AddTranslator(const SocketAddress& ext_ip,
110                               const SocketAddress& int_ip, NATType type);
111     void RemoveTranslator(const SocketAddress& ext_ip);
112 
113     bool AddClient(const SocketAddress& int_ip);
114     void RemoveClient(const SocketAddress& int_ip);
115 
116     // Looks for the specified client in this or a child NAT.
117     Translator* FindClient(const SocketAddress& int_ip);
118 
119    private:
120     NATSocketServer* server_;
121     std::unique_ptr<SocketFactory> internal_factory_;
122     std::unique_ptr<NATServer> nat_server_;
123     TranslatorMap nats_;
124     std::set<SocketAddress> clients_;
125   };
126 
127   explicit NATSocketServer(SocketServer* ss);
128 
socketserver()129   SocketServer* socketserver() { return server_; }
queue()130   MessageQueue* queue() { return msg_queue_; }
131 
132   Translator* GetTranslator(const SocketAddress& ext_ip);
133   Translator* AddTranslator(const SocketAddress& ext_ip,
134                             const SocketAddress& int_ip, NATType type);
135   void RemoveTranslator(const SocketAddress& ext_ip);
136 
137   // SocketServer implementation
138   Socket* CreateSocket(int type) override;
139   Socket* CreateSocket(int family, int type) override;
140 
141   AsyncSocket* CreateAsyncSocket(int type) override;
142   AsyncSocket* CreateAsyncSocket(int family, int type) override;
143 
144   void SetMessageQueue(MessageQueue* queue) override;
145   bool Wait(int cms, bool process_io) override;
146   void WakeUp() override;
147 
148   // NATInternalSocketFactory implementation
149   AsyncSocket* CreateInternalSocket(int family,
150                                     int type,
151                                     const SocketAddress& local_addr,
152                                     SocketAddress* nat_addr) override;
153 
154  private:
155   SocketServer* server_;
156   MessageQueue* msg_queue_;
157   TranslatorMap nats_;
158   RTC_DISALLOW_COPY_AND_ASSIGN(NATSocketServer);
159 };
160 
161 // Free-standing NAT helper functions.
162 size_t PackAddressForNAT(char* buf, size_t buf_size,
163                          const SocketAddress& remote_addr);
164 size_t UnpackAddressFromNAT(const char* buf, size_t buf_size,
165                             SocketAddress* remote_addr);
166 }  // namespace rtc
167 
168 #endif  // RTC_BASE_NATSOCKETFACTORY_H_
169