Home | History | Annotate | Download | only in base
      1 /*
      2  * libjingle
      3  * Copyright 2004--2005, Google Inc.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions are met:
      7  *
      8  *  1. Redistributions of source code must retain the above copyright notice,
      9  *     this list of conditions and the following disclaimer.
     10  *  2. Redistributions in binary form must reproduce the above copyright notice,
     11  *     this list of conditions and the following disclaimer in the documentation
     12  *     and/or other materials provided with the distribution.
     13  *  3. The name of the author may not be used to endorse or promote products
     14  *     derived from this software without specific prior written permission.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
     17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     18  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
     19  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     20  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  */
     27 
     28 #ifndef TALK_P2P_BASE_PORTALLOCATOR_H_
     29 #define TALK_P2P_BASE_PORTALLOCATOR_H_
     30 
     31 #include <string>
     32 #include <vector>
     33 
     34 #include "talk/base/sigslot.h"
     35 #include "talk/p2p/base/port.h"
     36 
     37 namespace cricket {
     38 
     39 // PortAllocator is responsible for allocating Port types for a given
     40 // P2PSocket. It also handles port freeing.
     41 //
     42 // Clients can override this class to control port allocation, including
     43 // what kinds of ports are allocated.
     44 
     45 const uint32 PORTALLOCATOR_DISABLE_UDP = 0x01;
     46 const uint32 PORTALLOCATOR_DISABLE_STUN = 0x02;
     47 const uint32 PORTALLOCATOR_DISABLE_RELAY = 0x04;
     48 const uint32 PORTALLOCATOR_DISABLE_TCP = 0x08;
     49 const uint32 PORTALLOCATOR_ENABLE_SHAKER = 0x10;
     50 
     51 const uint32 kDefaultPortAllocatorFlags = 0;
     52 
     53 class PortAllocatorSession : public sigslot::has_slots<> {
     54  public:
     55   explicit PortAllocatorSession(uint32 flags) : flags_(flags) {}
     56 
     57   // Subclasses should clean up any ports created.
     58   virtual ~PortAllocatorSession() {}
     59 
     60   uint32 flags() const { return flags_; }
     61   void set_flags(uint32 flags) { flags_ = flags; }
     62 
     63   // Prepares an initial set of ports to try.
     64   virtual void GetInitialPorts() = 0;
     65 
     66   // Starts and stops the flow of additional ports to try.
     67   virtual void StartGetAllPorts() = 0;
     68   virtual void StopGetAllPorts() = 0;
     69   virtual bool IsGettingAllPorts() = 0;
     70 
     71   sigslot::signal2<PortAllocatorSession*, Port*> SignalPortReady;
     72   sigslot::signal2<PortAllocatorSession*,
     73                    const std::vector<Candidate>&> SignalCandidatesReady;
     74 
     75   uint32 generation() { return generation_; }
     76   void set_generation(uint32 generation) { generation_ = generation; }
     77 
     78  private:
     79   uint32 flags_;
     80   uint32 generation_;
     81 };
     82 
     83 class PortAllocator {
     84  public:
     85   PortAllocator() :
     86       flags_(kDefaultPortAllocatorFlags),
     87       min_port_(0),
     88       max_port_(0) {
     89   }
     90   virtual ~PortAllocator() {}
     91 
     92   virtual PortAllocatorSession *CreateSession(const std::string &name,
     93       const std::string &session_type) = 0;
     94 
     95   uint32 flags() const { return flags_; }
     96   void set_flags(uint32 flags) { flags_ = flags; }
     97 
     98   const std::string& user_agent() const { return agent_; }
     99   const talk_base::ProxyInfo& proxy() const { return proxy_; }
    100   void set_proxy(const std::string& agent, const talk_base::ProxyInfo& proxy) {
    101     agent_ = agent;
    102     proxy_ = proxy;
    103   }
    104 
    105   // Gets/Sets the port range to use when choosing client ports.
    106   int min_port() const { return min_port_; }
    107   int max_port() const { return max_port_; }
    108   bool SetPortRange(int min_port, int max_port) {
    109     if (min_port > max_port) {
    110       return false;
    111     }
    112 
    113     min_port_ = min_port;
    114     max_port_ = max_port;
    115     return true;
    116   }
    117 
    118  protected:
    119   uint32 flags_;
    120   std::string agent_;
    121   talk_base::ProxyInfo proxy_;
    122   int min_port_;
    123   int max_port_;
    124 };
    125 
    126 }  // namespace cricket
    127 
    128 #endif  // TALK_P2P_BASE_PORTALLOCATOR_H_
    129