1/*
2 * libjingle
3 * Copyright 2004--2005, Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 *  1. Redistributions of source code must retain the above copyright notice,
9 *     this list of conditions and the following disclaimer.
10 *  2. Redistributions in binary form must reproduce the above copyright notice,
11 *     this list of conditions and the following disclaimer in the documentation
12 *     and/or other materials provided with the distribution.
13 *  3. The name of the author may not be used to endorse or promote products
14 *     derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#ifndef TALK_BASE_NATSOCKETFACTORY_H_
29#define TALK_BASE_NATSOCKETFACTORY_H_
30
31#include <string>
32#include <map>
33#include <set>
34
35#include "talk/base/natserver.h"
36#include "talk/base/socketaddress.h"
37#include "talk/base/socketserver.h"
38
39namespace talk_base {
40
41const size_t kNATEncodedIPv4AddressSize = 8U;
42const size_t kNATEncodedIPv6AddressSize = 20U;
43
44// Used by the NAT socket implementation.
45class NATInternalSocketFactory {
46 public:
47  virtual ~NATInternalSocketFactory() {}
48  virtual AsyncSocket* CreateInternalSocket(int family, int type,
49      const SocketAddress& local_addr, SocketAddress* nat_addr) = 0;
50};
51
52// Creates sockets that will send all traffic through a NAT, using an existing
53// NATServer instance running at nat_addr. The actual data is sent using sockets
54// from a socket factory, given to the constructor.
55class NATSocketFactory : public SocketFactory, public NATInternalSocketFactory {
56 public:
57  NATSocketFactory(SocketFactory* factory, const SocketAddress& nat_addr);
58
59  // SocketFactory implementation
60  virtual Socket* CreateSocket(int type);
61  virtual Socket* CreateSocket(int family, int type);
62  virtual AsyncSocket* CreateAsyncSocket(int type);
63  virtual AsyncSocket* CreateAsyncSocket(int family, int type);
64
65  // NATInternalSocketFactory implementation
66  virtual AsyncSocket* CreateInternalSocket(int family, int type,
67      const SocketAddress& local_addr, SocketAddress* nat_addr);
68
69 private:
70  SocketFactory* factory_;
71  SocketAddress nat_addr_;
72  DISALLOW_EVIL_CONSTRUCTORS(NATSocketFactory);
73};
74
75// Creates sockets that will send traffic through a NAT depending on what
76// address they bind to. This can be used to simulate a client on a NAT sending
77// to a client that is not behind a NAT.
78// Note that the internal addresses of clients must be unique. This is because
79// there is only one socketserver per thread, and the Bind() address is used to
80// figure out which NAT (if any) the socket should talk to.
81//
82// Example with 3 NATs (2 cascaded), and 3 clients.
83// ss->AddTranslator("1.2.3.4", "192.168.0.1", NAT_ADDR_RESTRICTED);
84// ss->AddTranslator("99.99.99.99", "10.0.0.1", NAT_SYMMETRIC)->
85//     AddTranslator("10.0.0.2", "192.168.1.1", NAT_OPEN_CONE);
86// ss->GetTranslator("1.2.3.4")->AddClient("1.2.3.4", "192.168.0.2");
87// ss->GetTranslator("99.99.99.99")->AddClient("10.0.0.3");
88// ss->GetTranslator("99.99.99.99")->GetTranslator("10.0.0.2")->
89//     AddClient("192.168.1.2");
90class NATSocketServer : public SocketServer, public NATInternalSocketFactory {
91 public:
92  class Translator;
93  // holds a list of NATs
94  class TranslatorMap : private std::map<SocketAddress, Translator*> {
95   public:
96    ~TranslatorMap();
97    Translator* Get(const SocketAddress& ext_ip);
98    Translator* Add(const SocketAddress& ext_ip, Translator*);
99    void Remove(const SocketAddress& ext_ip);
100    Translator* FindClient(const SocketAddress& int_ip);
101  };
102
103  // a specific NAT
104  class Translator {
105   public:
106    Translator(NATSocketServer* server, NATType type,
107               const SocketAddress& int_addr, SocketFactory* ext_factory,
108               const SocketAddress& ext_addr);
109
110    SocketFactory* internal_factory() { return internal_factory_.get(); }
111    SocketAddress internal_address() const {
112      return nat_server_->internal_address();
113    }
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, NATType type);
121    void RemoveTranslator(const SocketAddress& ext_ip);
122
123    bool AddClient(const SocketAddress& int_ip);
124    void RemoveClient(const SocketAddress& int_ip);
125
126    // Looks for the specified client in this or a child NAT.
127    Translator* FindClient(const SocketAddress& int_ip);
128
129   private:
130    NATSocketServer* server_;
131    scoped_ptr<SocketFactory> internal_factory_;
132    scoped_ptr<NATServer> nat_server_;
133    TranslatorMap nats_;
134    std::set<SocketAddress> clients_;
135  };
136
137  explicit NATSocketServer(SocketServer* ss);
138
139  SocketServer* socketserver() { return server_; }
140  MessageQueue* queue() { return msg_queue_; }
141
142  Translator* GetTranslator(const SocketAddress& ext_ip);
143  Translator* AddTranslator(const SocketAddress& ext_ip,
144                            const SocketAddress& int_ip, NATType type);
145  void RemoveTranslator(const SocketAddress& ext_ip);
146
147  // SocketServer implementation
148  virtual Socket* CreateSocket(int type);
149  virtual Socket* CreateSocket(int family, int type);
150
151  virtual AsyncSocket* CreateAsyncSocket(int type);
152  virtual AsyncSocket* CreateAsyncSocket(int family, int type);
153
154  virtual void SetMessageQueue(MessageQueue* queue) {
155    msg_queue_ = queue;
156    server_->SetMessageQueue(queue);
157  }
158  virtual bool Wait(int cms, bool process_io) {
159    return server_->Wait(cms, process_io);
160  }
161  virtual void WakeUp() {
162    server_->WakeUp();
163  }
164
165  // NATInternalSocketFactory implementation
166  virtual AsyncSocket* CreateInternalSocket(int family, int type,
167      const SocketAddress& local_addr, SocketAddress* nat_addr);
168
169 private:
170  SocketServer* server_;
171  MessageQueue* msg_queue_;
172  TranslatorMap nats_;
173  DISALLOW_EVIL_CONSTRUCTORS(NATSocketServer);
174};
175
176// Free-standing NAT helper functions.
177size_t PackAddressForNAT(char* buf, size_t buf_size,
178                         const SocketAddress& remote_addr);
179size_t UnpackAddressFromNAT(const char* buf, size_t buf_size,
180                            SocketAddress* remote_addr);
181}  // namespace talk_base
182
183#endif  // TALK_BASE_NATSOCKETFACTORY_H_
184