147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org/*
247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org *  Copyright 2004 The WebRTC Project Authors. All rights reserved.
347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org *
447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org *  Use of this source code is governed by a BSD-style license
547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org *  that can be found in the LICENSE file in the root of the source
647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org *  tree. An additional intellectual property rights grant can be found
747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org *  in the file PATENTS.  All contributing project authors may
847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org *  be found in the AUTHORS file in the root of the source tree.
947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org */
1047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
1147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#ifndef WEBRTC_BASE_NATSOCKETFACTORY_H_
1247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#define WEBRTC_BASE_NATSOCKETFACTORY_H_
1347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
1447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#include <string>
1547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#include <map>
1647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#include <set>
1747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
1847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#include "webrtc/base/natserver.h"
1947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#include "webrtc/base/socketaddress.h"
2047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#include "webrtc/base/socketserver.h"
2147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
2247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgnamespace rtc {
2347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
2447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgconst size_t kNATEncodedIPv4AddressSize = 8U;
2547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgconst size_t kNATEncodedIPv6AddressSize = 20U;
2647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
2747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org// Used by the NAT socket implementation.
2847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgclass NATInternalSocketFactory {
2947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org public:
3047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  virtual ~NATInternalSocketFactory() {}
3147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  virtual AsyncSocket* CreateInternalSocket(int family, int type,
3247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      const SocketAddress& local_addr, SocketAddress* nat_addr) = 0;
3347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org};
3447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
3547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org// Creates sockets that will send all traffic through a NAT, using an existing
3647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org// NATServer instance running at nat_addr. The actual data is sent using sockets
3747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org// from a socket factory, given to the constructor.
3847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgclass NATSocketFactory : public SocketFactory, public NATInternalSocketFactory {
3947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org public:
4047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  NATSocketFactory(SocketFactory* factory, const SocketAddress& nat_addr);
4147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
4247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // SocketFactory implementation
4347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  virtual Socket* CreateSocket(int type);
4447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  virtual Socket* CreateSocket(int family, int type);
4547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  virtual AsyncSocket* CreateAsyncSocket(int type);
4647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  virtual AsyncSocket* CreateAsyncSocket(int family, int type);
4747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
4847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // NATInternalSocketFactory implementation
4947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  virtual AsyncSocket* CreateInternalSocket(int family, int type,
5047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      const SocketAddress& local_addr, SocketAddress* nat_addr);
5147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
5247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org private:
5347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  SocketFactory* factory_;
5447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  SocketAddress nat_addr_;
5547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  DISALLOW_EVIL_CONSTRUCTORS(NATSocketFactory);
5647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org};
5747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
5847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org// Creates sockets that will send traffic through a NAT depending on what
5947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org// address they bind to. This can be used to simulate a client on a NAT sending
6047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org// to a client that is not behind a NAT.
6147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org// Note that the internal addresses of clients must be unique. This is because
6247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org// there is only one socketserver per thread, and the Bind() address is used to
6347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org// figure out which NAT (if any) the socket should talk to.
6447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org//
6547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org// Example with 3 NATs (2 cascaded), and 3 clients.
6647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org// ss->AddTranslator("1.2.3.4", "192.168.0.1", NAT_ADDR_RESTRICTED);
6747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org// ss->AddTranslator("99.99.99.99", "10.0.0.1", NAT_SYMMETRIC)->
6847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org//     AddTranslator("10.0.0.2", "192.168.1.1", NAT_OPEN_CONE);
6947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org// ss->GetTranslator("1.2.3.4")->AddClient("1.2.3.4", "192.168.0.2");
7047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org// ss->GetTranslator("99.99.99.99")->AddClient("10.0.0.3");
7147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org// ss->GetTranslator("99.99.99.99")->GetTranslator("10.0.0.2")->
7247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org//     AddClient("192.168.1.2");
7347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgclass NATSocketServer : public SocketServer, public NATInternalSocketFactory {
7447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org public:
7547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  class Translator;
7647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // holds a list of NATs
7747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  class TranslatorMap : private std::map<SocketAddress, Translator*> {
7847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org   public:
7947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    ~TranslatorMap();
8047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    Translator* Get(const SocketAddress& ext_ip);
8147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    Translator* Add(const SocketAddress& ext_ip, Translator*);
8247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    void Remove(const SocketAddress& ext_ip);
8347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    Translator* FindClient(const SocketAddress& int_ip);
8447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  };
8547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
8647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // a specific NAT
8747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  class Translator {
8847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org   public:
8947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    Translator(NATSocketServer* server, NATType type,
9047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org               const SocketAddress& int_addr, SocketFactory* ext_factory,
9147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org               const SocketAddress& ext_addr);
9247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
9347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    SocketFactory* internal_factory() { return internal_factory_.get(); }
9447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    SocketAddress internal_address() const {
9547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      return nat_server_->internal_address();
9647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    }
9747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    SocketAddress internal_tcp_address() const {
9847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      return SocketAddress();  // nat_server_->internal_tcp_address();
9947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    }
10047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
10147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    Translator* GetTranslator(const SocketAddress& ext_ip);
10247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    Translator* AddTranslator(const SocketAddress& ext_ip,
10347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org                              const SocketAddress& int_ip, NATType type);
10447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    void RemoveTranslator(const SocketAddress& ext_ip);
10547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
10647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    bool AddClient(const SocketAddress& int_ip);
10747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    void RemoveClient(const SocketAddress& int_ip);
10847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
10947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    // Looks for the specified client in this or a child NAT.
11047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    Translator* FindClient(const SocketAddress& int_ip);
11147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
11247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org   private:
11347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    NATSocketServer* server_;
11447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    scoped_ptr<SocketFactory> internal_factory_;
11547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    scoped_ptr<NATServer> nat_server_;
11647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    TranslatorMap nats_;
11747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    std::set<SocketAddress> clients_;
11847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  };
11947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
12047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  explicit NATSocketServer(SocketServer* ss);
12147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
12247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  SocketServer* socketserver() { return server_; }
12347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  MessageQueue* queue() { return msg_queue_; }
12447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
12547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  Translator* GetTranslator(const SocketAddress& ext_ip);
12647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  Translator* AddTranslator(const SocketAddress& ext_ip,
12747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org                            const SocketAddress& int_ip, NATType type);
12847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  void RemoveTranslator(const SocketAddress& ext_ip);
12947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
13047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // SocketServer implementation
13147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  virtual Socket* CreateSocket(int type);
13247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  virtual Socket* CreateSocket(int family, int type);
13347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
13447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  virtual AsyncSocket* CreateAsyncSocket(int type);
13547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  virtual AsyncSocket* CreateAsyncSocket(int family, int type);
13647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
13747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  virtual void SetMessageQueue(MessageQueue* queue) {
13847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    msg_queue_ = queue;
13947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    server_->SetMessageQueue(queue);
14047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  }
14147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  virtual bool Wait(int cms, bool process_io) {
14247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    return server_->Wait(cms, process_io);
14347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  }
14447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  virtual void WakeUp() {
14547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    server_->WakeUp();
14647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  }
14747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
14847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // NATInternalSocketFactory implementation
14947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  virtual AsyncSocket* CreateInternalSocket(int family, int type,
15047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      const SocketAddress& local_addr, SocketAddress* nat_addr);
15147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
15247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org private:
15347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  SocketServer* server_;
15447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  MessageQueue* msg_queue_;
15547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  TranslatorMap nats_;
15647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  DISALLOW_EVIL_CONSTRUCTORS(NATSocketServer);
15747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org};
15847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
15947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org// Free-standing NAT helper functions.
16047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgsize_t PackAddressForNAT(char* buf, size_t buf_size,
16147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org                         const SocketAddress& remote_addr);
16247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgsize_t UnpackAddressFromNAT(const char* buf, size_t buf_size,
16347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org                            SocketAddress* remote_addr);
16447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org}  // namespace rtc
16547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
16647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#endif  // WEBRTC_BASE_NATSOCKETFACTORY_H_
167