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