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