blob: e987021773501176ff945a1f02b5bd7d8ad10a5f [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_NATSERVER_H_
12#define WEBRTC_BASE_NATSERVER_H_
13
14#include <map>
15#include <set>
16
17#include "webrtc/base/asyncudpsocket.h"
18#include "webrtc/base/socketaddresspair.h"
19#include "webrtc/base/thread.h"
20#include "webrtc/base/socketfactory.h"
21#include "webrtc/base/nattypes.h"
deadbeefc5d0d952015-07-16 10:22:21 -070022#include "webrtc/base/proxyserver.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000023
24namespace rtc {
25
26// Change how routes (socketaddress pairs) are compared based on the type of
27// NAT. The NAT server maintains a hashtable of the routes that it knows
28// about. So these affect which routes are treated the same.
29struct RouteCmp {
30 explicit RouteCmp(NAT* nat);
31 size_t operator()(const SocketAddressPair& r) const;
32 bool operator()(
33 const SocketAddressPair& r1, const SocketAddressPair& r2) const;
34
35 bool symmetric;
36};
37
38// Changes how addresses are compared based on the filtering rules of the NAT.
39struct AddrCmp {
40 explicit AddrCmp(NAT* nat);
41 size_t operator()(const SocketAddress& r) const;
42 bool operator()(const SocketAddress& r1, const SocketAddress& r2) const;
43
44 bool use_ip;
45 bool use_port;
46};
47
48// Implements the NAT device. It listens for packets on the internal network,
49// translates them, and sends them out over the external network.
deadbeefc5d0d952015-07-16 10:22:21 -070050//
51// TCP connections initiated from the internal side of the NAT server are
52// also supported, by making a connection to the NAT server's TCP address and
53// then sending the remote address in quasi-STUN format. The connection status
54// will be indicated back to the client as a 1 byte status code, where '0'
55// indicates success.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000056
deadbeefc5d0d952015-07-16 10:22:21 -070057const int NAT_SERVER_UDP_PORT = 4237;
58const int NAT_SERVER_TCP_PORT = 4238;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000059
60class NATServer : public sigslot::has_slots<> {
61 public:
62 NATServer(
deadbeefc5d0d952015-07-16 10:22:21 -070063 NATType type, SocketFactory* internal,
64 const SocketAddress& internal_udp_addr,
65 const SocketAddress& internal_tcp_addr,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000066 SocketFactory* external, const SocketAddress& external_ip);
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000067 ~NATServer() override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000068
deadbeefc5d0d952015-07-16 10:22:21 -070069 SocketAddress internal_udp_address() const {
70 return udp_server_socket_->GetLocalAddress();
71 }
72
73 SocketAddress internal_tcp_address() const {
74 return tcp_proxy_server_->GetServerAddress();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000075 }
76
77 // Packets received on one of the networks.
deadbeefc5d0d952015-07-16 10:22:21 -070078 void OnInternalUDPPacket(AsyncPacketSocket* socket, const char* buf,
79 size_t size, const SocketAddress& addr,
80 const PacketTime& packet_time);
81 void OnExternalUDPPacket(AsyncPacketSocket* socket, const char* buf,
82 size_t size, const SocketAddress& remote_addr,
83 const PacketTime& packet_time);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000084
85 private:
86 typedef std::set<SocketAddress, AddrCmp> AddressSet;
87
88 /* Records a translation and the associated external socket. */
89 struct TransEntry {
90 TransEntry(const SocketAddressPair& r, AsyncUDPSocket* s, NAT* nat);
91 ~TransEntry();
92
93 void WhitelistInsert(const SocketAddress& addr);
94 bool WhitelistContains(const SocketAddress& ext_addr);
95
96 SocketAddressPair route;
97 AsyncUDPSocket* socket;
98 AddressSet* whitelist;
99 CriticalSection crit_;
100 };
101
102 typedef std::map<SocketAddressPair, TransEntry*, RouteCmp> InternalMap;
103 typedef std::map<SocketAddress, TransEntry*> ExternalMap;
104
105 /* Creates a new entry that translates the given route. */
106 void Translate(const SocketAddressPair& route);
107
108 /* Determines whether the NAT would filter out a packet from this address. */
109 bool ShouldFilterOut(TransEntry* entry, const SocketAddress& ext_addr);
110
111 NAT* nat_;
112 SocketFactory* internal_;
113 SocketFactory* external_;
114 SocketAddress external_ip_;
deadbeefc5d0d952015-07-16 10:22:21 -0700115 AsyncUDPSocket* udp_server_socket_;
116 ProxyServer* tcp_proxy_server_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000117 InternalMap* int_map_;
118 ExternalMap* ext_map_;
Thiago Farinaae0f0ee2015-04-04 23:56:53 +0000119 DISALLOW_COPY_AND_ASSIGN(NATServer);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000120};
121
122} // namespace rtc
123
124#endif // WEBRTC_BASE_NATSERVER_H_