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