blob: 6fad30c56027a48b8db1930b254c8d065a6a1cbc [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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>
jbauch555604a2016-04-26 03:13:22 -070016#include <memory>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000017#include <set>
18
kwiberg4485ffb2016-04-26 08:14:39 -070019#include "webrtc/base/constructormagic.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000020#include "webrtc/base/natserver.h"
21#include "webrtc/base/socketaddress.h"
22#include "webrtc/base/socketserver.h"
23
24namespace rtc {
25
26const size_t kNATEncodedIPv4AddressSize = 8U;
27const size_t kNATEncodedIPv6AddressSize = 20U;
28
29// Used by the NAT socket implementation.
30class NATInternalSocketFactory {
31 public:
32 virtual ~NATInternalSocketFactory() {}
33 virtual AsyncSocket* CreateInternalSocket(int family, int type,
34 const SocketAddress& local_addr, SocketAddress* nat_addr) = 0;
35};
36
37// Creates sockets that will send all traffic through a NAT, using an existing
38// NATServer instance running at nat_addr. The actual data is sent using sockets
39// from a socket factory, given to the constructor.
40class NATSocketFactory : public SocketFactory, public NATInternalSocketFactory {
41 public:
deadbeefc5d0d952015-07-16 10:22:21 -070042 NATSocketFactory(SocketFactory* factory, const SocketAddress& nat_udp_addr,
43 const SocketAddress& nat_tcp_addr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000044
45 // SocketFactory implementation
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000046 Socket* CreateSocket(int type) override;
47 Socket* CreateSocket(int family, int type) override;
48 AsyncSocket* CreateAsyncSocket(int type) override;
49 AsyncSocket* CreateAsyncSocket(int family, int type) override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000050
51 // NATInternalSocketFactory implementation
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000052 AsyncSocket* CreateInternalSocket(int family,
53 int type,
54 const SocketAddress& local_addr,
55 SocketAddress* nat_addr) override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000056
57 private:
58 SocketFactory* factory_;
deadbeefc5d0d952015-07-16 10:22:21 -070059 SocketAddress nat_udp_addr_;
60 SocketAddress nat_tcp_addr_;
henrikg3c089d72015-09-16 05:37:44 -070061 RTC_DISALLOW_COPY_AND_ASSIGN(NATSocketFactory);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000062};
63
64// Creates sockets that will send traffic through a NAT depending on what
65// address they bind to. This can be used to simulate a client on a NAT sending
66// to a client that is not behind a NAT.
67// Note that the internal addresses of clients must be unique. This is because
68// there is only one socketserver per thread, and the Bind() address is used to
69// figure out which NAT (if any) the socket should talk to.
70//
71// Example with 3 NATs (2 cascaded), and 3 clients.
72// ss->AddTranslator("1.2.3.4", "192.168.0.1", NAT_ADDR_RESTRICTED);
73// ss->AddTranslator("99.99.99.99", "10.0.0.1", NAT_SYMMETRIC)->
74// AddTranslator("10.0.0.2", "192.168.1.1", NAT_OPEN_CONE);
75// ss->GetTranslator("1.2.3.4")->AddClient("1.2.3.4", "192.168.0.2");
76// ss->GetTranslator("99.99.99.99")->AddClient("10.0.0.3");
77// ss->GetTranslator("99.99.99.99")->GetTranslator("10.0.0.2")->
78// AddClient("192.168.1.2");
79class NATSocketServer : public SocketServer, public NATInternalSocketFactory {
80 public:
81 class Translator;
82 // holds a list of NATs
83 class TranslatorMap : private std::map<SocketAddress, Translator*> {
84 public:
85 ~TranslatorMap();
86 Translator* Get(const SocketAddress& ext_ip);
87 Translator* Add(const SocketAddress& ext_ip, Translator*);
88 void Remove(const SocketAddress& ext_ip);
89 Translator* FindClient(const SocketAddress& int_ip);
90 };
91
92 // a specific NAT
93 class Translator {
94 public:
95 Translator(NATSocketServer* server, NATType type,
96 const SocketAddress& int_addr, SocketFactory* ext_factory,
97 const SocketAddress& ext_addr);
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000098 ~Translator();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000099
100 SocketFactory* internal_factory() { return internal_factory_.get(); }
deadbeefc5d0d952015-07-16 10:22:21 -0700101 SocketAddress internal_udp_address() const {
102 return nat_server_->internal_udp_address();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000103 }
104 SocketAddress internal_tcp_address() const {
105 return SocketAddress(); // nat_server_->internal_tcp_address();
106 }
107
108 Translator* GetTranslator(const SocketAddress& ext_ip);
109 Translator* AddTranslator(const SocketAddress& ext_ip,
110 const SocketAddress& int_ip, NATType type);
111 void RemoveTranslator(const SocketAddress& ext_ip);
112
113 bool AddClient(const SocketAddress& int_ip);
114 void RemoveClient(const SocketAddress& int_ip);
115
116 // Looks for the specified client in this or a child NAT.
117 Translator* FindClient(const SocketAddress& int_ip);
118
119 private:
120 NATSocketServer* server_;
jbauch555604a2016-04-26 03:13:22 -0700121 std::unique_ptr<SocketFactory> internal_factory_;
122 std::unique_ptr<NATServer> nat_server_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000123 TranslatorMap nats_;
124 std::set<SocketAddress> clients_;
125 };
126
127 explicit NATSocketServer(SocketServer* ss);
128
129 SocketServer* socketserver() { return server_; }
130 MessageQueue* queue() { return msg_queue_; }
131
132 Translator* GetTranslator(const SocketAddress& ext_ip);
133 Translator* AddTranslator(const SocketAddress& ext_ip,
134 const SocketAddress& int_ip, NATType type);
135 void RemoveTranslator(const SocketAddress& ext_ip);
136
137 // SocketServer implementation
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000138 Socket* CreateSocket(int type) override;
139 Socket* CreateSocket(int family, int type) override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000140
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000141 AsyncSocket* CreateAsyncSocket(int type) override;
142 AsyncSocket* CreateAsyncSocket(int family, int type) override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000143
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000144 void SetMessageQueue(MessageQueue* queue) override;
145 bool Wait(int cms, bool process_io) override;
146 void WakeUp() override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000147
148 // NATInternalSocketFactory implementation
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000149 AsyncSocket* CreateInternalSocket(int family,
150 int type,
151 const SocketAddress& local_addr,
152 SocketAddress* nat_addr) override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000153
154 private:
155 SocketServer* server_;
156 MessageQueue* msg_queue_;
157 TranslatorMap nats_;
henrikg3c089d72015-09-16 05:37:44 -0700158 RTC_DISALLOW_COPY_AND_ASSIGN(NATSocketServer);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000159};
160
161// Free-standing NAT helper functions.
162size_t PackAddressForNAT(char* buf, size_t buf_size,
163 const SocketAddress& remote_addr);
164size_t UnpackAddressFromNAT(const char* buf, size_t buf_size,
165 SocketAddress* remote_addr);
166} // namespace rtc
167
168#endif // WEBRTC_BASE_NATSOCKETFACTORY_H_