3
* Copyright 2004--2005, Google Inc.
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions are met:
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.
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.
28
#ifndef TALK_BASE_NATSOCKETFACTORY_H_
29
#define TALK_BASE_NATSOCKETFACTORY_H_
35
#include "talk/base/natserver.h"
36
#include "talk/base/socketaddress.h"
37
#include "talk/base/socketserver.h"
41
const size_t kNATEncodedIPv4AddressSize = 8U;
42
const size_t kNATEncodedIPv6AddressSize = 20U;
44
// Used by the NAT socket implementation.
45
class NATInternalSocketFactory {
47
virtual ~NATInternalSocketFactory() {}
48
virtual AsyncSocket* CreateInternalSocket(int family, int type,
49
const SocketAddress& local_addr, SocketAddress* nat_addr) = 0;
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.
55
class NATSocketFactory : public SocketFactory, public NATInternalSocketFactory {
57
NATSocketFactory(SocketFactory* factory, const SocketAddress& nat_addr);
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);
65
// NATInternalSocketFactory implementation
66
virtual AsyncSocket* CreateInternalSocket(int family, int type,
67
const SocketAddress& local_addr, SocketAddress* nat_addr);
70
SocketFactory* factory_;
71
SocketAddress nat_addr_;
72
DISALLOW_EVIL_CONSTRUCTORS(NATSocketFactory);
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.
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");
90
class NATSocketServer : public SocketServer, public NATInternalSocketFactory {
93
// holds a list of NATs
94
class TranslatorMap : private std::map<SocketAddress, Translator*> {
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);
106
Translator(NATSocketServer* server, NATType type,
107
const SocketAddress& int_addr, SocketFactory* ext_factory,
108
const SocketAddress& ext_addr);
110
SocketFactory* internal_factory() { return internal_factory_.get(); }
111
SocketAddress internal_address() const {
112
return nat_server_->internal_address();
114
SocketAddress internal_tcp_address() const {
115
return SocketAddress(); // nat_server_->internal_tcp_address();
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);
123
bool AddClient(const SocketAddress& int_ip);
124
void RemoveClient(const SocketAddress& int_ip);
126
// Looks for the specified client in this or a child NAT.
127
Translator* FindClient(const SocketAddress& int_ip);
130
NATSocketServer* server_;
131
scoped_ptr<SocketFactory> internal_factory_;
132
scoped_ptr<NATServer> nat_server_;
134
std::set<SocketAddress> clients_;
137
explicit NATSocketServer(SocketServer* ss);
139
SocketServer* socketserver() { return server_; }
140
MessageQueue* queue() { return msg_queue_; }
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);
147
// SocketServer implementation
148
virtual Socket* CreateSocket(int type);
149
virtual Socket* CreateSocket(int family, int type);
151
virtual AsyncSocket* CreateAsyncSocket(int type);
152
virtual AsyncSocket* CreateAsyncSocket(int family, int type);
154
virtual void SetMessageQueue(MessageQueue* queue) {
156
server_->SetMessageQueue(queue);
158
virtual bool Wait(int cms, bool process_io) {
159
return server_->Wait(cms, process_io);
161
virtual void WakeUp() {
165
// NATInternalSocketFactory implementation
166
virtual AsyncSocket* CreateInternalSocket(int family, int type,
167
const SocketAddress& local_addr, SocketAddress* nat_addr);
170
SocketServer* server_;
171
MessageQueue* msg_queue_;
173
DISALLOW_EVIL_CONSTRUCTORS(NATSocketServer);
176
// Free-standing NAT helper functions.
177
size_t PackAddressForNAT(char* buf, size_t buf_size,
178
const SocketAddress& remote_addr);
179
size_t UnpackAddressFromNAT(const char* buf, size_t buf_size,
180
SocketAddress* remote_addr);
181
} // namespace talk_base
183
#endif // TALK_BASE_NATSOCKETFACTORY_H_