Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(177)

Side by Side Diff: webrtc/base/natsocketfactory.h

Issue 2877023002: Move webrtc/{base => rtc_base} (Closed)
Patch Set: update presubmit.py and DEPS include rules Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « webrtc/base/natserver.cc ('k') | webrtc/base/natsocketfactory.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 #ifndef WEBRTC_BASE_NATSOCKETFACTORY_H_ 11 #ifndef WEBRTC_BASE_NATSOCKETFACTORY_H_
12 #define WEBRTC_BASE_NATSOCKETFACTORY_H_ 12 #define WEBRTC_BASE_NATSOCKETFACTORY_H_
13 13
14 #include <string>
15 #include <map>
16 #include <memory>
17 #include <set>
18 14
19 #include "webrtc/base/constructormagic.h" 15 // This header is deprecated and is just left here temporarily during
20 #include "webrtc/base/natserver.h" 16 // refactoring. See https://bugs.webrtc.org/7634 for more details.
21 #include "webrtc/base/socketaddress.h" 17 #include "webrtc/rtc_base/natsocketfactory.h"
22 #include "webrtc/base/socketserver.h"
23
24 namespace rtc {
25
26 const size_t kNATEncodedIPv4AddressSize = 8U;
27 const size_t kNATEncodedIPv6AddressSize = 20U;
28
29 // Used by the NAT socket implementation.
30 class 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.
40 class NATSocketFactory : public SocketFactory, public NATInternalSocketFactory {
41 public:
42 NATSocketFactory(SocketFactory* factory, const SocketAddress& nat_udp_addr,
43 const SocketAddress& nat_tcp_addr);
44
45 // SocketFactory implementation
46 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;
50
51 // NATInternalSocketFactory implementation
52 AsyncSocket* CreateInternalSocket(int family,
53 int type,
54 const SocketAddress& local_addr,
55 SocketAddress* nat_addr) override;
56
57 private:
58 SocketFactory* factory_;
59 SocketAddress nat_udp_addr_;
60 SocketAddress nat_tcp_addr_;
61 RTC_DISALLOW_COPY_AND_ASSIGN(NATSocketFactory);
62 };
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");
79 class 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);
98 ~Translator();
99
100 SocketFactory* internal_factory() { return internal_factory_.get(); }
101 SocketAddress internal_udp_address() const {
102 return nat_server_->internal_udp_address();
103 }
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_;
121 std::unique_ptr<SocketFactory> internal_factory_;
122 std::unique_ptr<NATServer> nat_server_;
123 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
138 Socket* CreateSocket(int type) override;
139 Socket* CreateSocket(int family, int type) override;
140
141 AsyncSocket* CreateAsyncSocket(int type) override;
142 AsyncSocket* CreateAsyncSocket(int family, int type) override;
143
144 void SetMessageQueue(MessageQueue* queue) override;
145 bool Wait(int cms, bool process_io) override;
146 void WakeUp() override;
147
148 // NATInternalSocketFactory implementation
149 AsyncSocket* CreateInternalSocket(int family,
150 int type,
151 const SocketAddress& local_addr,
152 SocketAddress* nat_addr) override;
153
154 private:
155 SocketServer* server_;
156 MessageQueue* msg_queue_;
157 TranslatorMap nats_;
158 RTC_DISALLOW_COPY_AND_ASSIGN(NATSocketServer);
159 };
160
161 // Free-standing NAT helper functions.
162 size_t PackAddressForNAT(char* buf, size_t buf_size,
163 const SocketAddress& remote_addr);
164 size_t UnpackAddressFromNAT(const char* buf, size_t buf_size,
165 SocketAddress* remote_addr);
166 } // namespace rtc
167 18
168 #endif // WEBRTC_BASE_NATSOCKETFACTORY_H_ 19 #endif // WEBRTC_BASE_NATSOCKETFACTORY_H_
OLDNEW
« no previous file with comments | « webrtc/base/natserver.cc ('k') | webrtc/base/natsocketfactory.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698