1/*
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"
22
23namespace rtc {
24
25// Change how routes (socketaddress pairs) are compared based on the type of
26// NAT.  The NAT server maintains a hashtable of the routes that it knows
27// about.  So these affect which routes are treated the same.
28struct RouteCmp {
29  explicit RouteCmp(NAT* nat);
30  size_t operator()(const SocketAddressPair& r) const;
31  bool operator()(
32      const SocketAddressPair& r1, const SocketAddressPair& r2) const;
33
34  bool symmetric;
35};
36
37// Changes how addresses are compared based on the filtering rules of the NAT.
38struct AddrCmp {
39  explicit AddrCmp(NAT* nat);
40  size_t operator()(const SocketAddress& r) const;
41  bool operator()(const SocketAddress& r1, const SocketAddress& r2) const;
42
43  bool use_ip;
44  bool use_port;
45};
46
47// Implements the NAT device.  It listens for packets on the internal network,
48// translates them, and sends them out over the external network.
49
50const int NAT_SERVER_PORT = 4237;
51
52class NATServer : public sigslot::has_slots<> {
53 public:
54  NATServer(
55      NATType type, SocketFactory* internal, const SocketAddress& internal_addr,
56      SocketFactory* external, const SocketAddress& external_ip);
57  ~NATServer();
58
59  SocketAddress internal_address() const {
60    return server_socket_->GetLocalAddress();
61  }
62
63  // Packets received on one of the networks.
64  void OnInternalPacket(AsyncPacketSocket* socket, const char* buf,
65                        size_t size, const SocketAddress& addr,
66                        const PacketTime& packet_time);
67  void OnExternalPacket(AsyncPacketSocket* socket, const char* buf,
68                        size_t size, const SocketAddress& remote_addr,
69                        const PacketTime& packet_time);
70
71 private:
72  typedef std::set<SocketAddress, AddrCmp> AddressSet;
73
74  /* Records a translation and the associated external socket. */
75  struct TransEntry {
76    TransEntry(const SocketAddressPair& r, AsyncUDPSocket* s, NAT* nat);
77    ~TransEntry();
78
79    void WhitelistInsert(const SocketAddress& addr);
80    bool WhitelistContains(const SocketAddress& ext_addr);
81
82    SocketAddressPair route;
83    AsyncUDPSocket* socket;
84    AddressSet* whitelist;
85    CriticalSection crit_;
86  };
87
88  typedef std::map<SocketAddressPair, TransEntry*, RouteCmp> InternalMap;
89  typedef std::map<SocketAddress, TransEntry*> ExternalMap;
90
91  /* Creates a new entry that translates the given route. */
92  void Translate(const SocketAddressPair& route);
93
94  /* Determines whether the NAT would filter out a packet from this address. */
95  bool ShouldFilterOut(TransEntry* entry, const SocketAddress& ext_addr);
96
97  NAT* nat_;
98  SocketFactory* internal_;
99  SocketFactory* external_;
100  SocketAddress external_ip_;
101  AsyncUDPSocket* server_socket_;
102  AsyncSocket* tcp_server_socket_;
103  InternalMap* int_map_;
104  ExternalMap* ext_map_;
105  DISALLOW_EVIL_CONSTRUCTORS(NATServer);
106};
107
108}  // namespace rtc
109
110#endif  // WEBRTC_BASE_NATSERVER_H_
111