• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_CANDIDATE_H_
29 #define TALK_P2P_BASE_CANDIDATE_H_
30 
31 #include <limits.h>
32 #include <math.h>
33 
34 #include <string>
35 #include <sstream>
36 #include <iomanip>
37 
38 #include "talk/base/basictypes.h"
39 #include "talk/base/socketaddress.h"
40 #include "talk/p2p/base/constants.h"
41 
42 namespace cricket {
43 
44 // Candidate for ICE based connection discovery.
45 
46 class Candidate {
47  public:
48   // TODO: Match the ordering and param list as per RFC 5245
49   // candidate-attribute syntax. http://tools.ietf.org/html/rfc5245#section-15.1
Candidate()50   Candidate() : component_(0), priority_(0), generation_(0) {}
Candidate(const std::string & id,int component,const std::string & protocol,const talk_base::SocketAddress & address,uint32 priority,const std::string & username,const std::string & password,const std::string & type,const std::string & network_name,uint32 generation,const std::string & foundation)51   Candidate(const std::string& id, int component, const std::string& protocol,
52             const talk_base::SocketAddress& address, uint32 priority,
53             const std::string& username, const std::string& password,
54             const std::string& type, const std::string& network_name,
55             uint32 generation, const std::string& foundation)
56       : id_(id), component_(component), protocol_(protocol), address_(address),
57         priority_(priority), username_(username), password_(password),
58         type_(type), network_name_(network_name), generation_(generation),
59         foundation_(foundation) {
60   }
61 
id()62   const std::string & id() const { return id_; }
set_id(const std::string & id)63   void set_id(const std::string & id) { id_ = id; }
64 
component()65   int component() const { return component_; }
set_component(int component)66   void set_component(int component) { component_ = component; }
67 
protocol()68   const std::string & protocol() const { return protocol_; }
set_protocol(const std::string & protocol)69   void set_protocol(const std::string & protocol) { protocol_ = protocol; }
70 
address()71   const talk_base::SocketAddress & address() const { return address_; }
set_address(const talk_base::SocketAddress & address)72   void set_address(const talk_base::SocketAddress & address) {
73     address_ = address;
74   }
75 
priority()76   uint32 priority() const { return priority_; }
set_priority(const uint32 priority)77   void set_priority(const uint32 priority) { priority_ = priority; }
78 
79 //  void set_type_preference(uint32 type_preference) {
80 //    priority_ = GetPriority(type_preference);
81 //  }
82 
83   // Maps old preference (which was 0.0-1.0) to match priority (which
84   // is 0-2^32-1) to to match RFC 5245, section 4.1.2.1.  Also see
85   // https://docs.google.com/a/google.com/document/d/
86   // 1iNQDiwDKMh0NQOrCqbj3DKKRT0Dn5_5UJYhmZO-t7Uc/edit
preference()87   float preference() const {
88     // The preference value is clamped to two decimal precision.
89     return static_cast<float>(((priority_ >> 24) * 100 / 127) / 100.0);
90   }
91 
set_preference(float preference)92   void set_preference(float preference) {
93     // Limiting priority to UINT_MAX when value exceeds uint32 max.
94     // This can happen for e.g. when preference = 3.
95     uint64 prio_val = static_cast<uint64>(preference * 127) << 24;
96     priority_ = static_cast<uint32>(
97       talk_base::_min(prio_val, static_cast<uint64>(UINT_MAX)));
98   }
99 
username()100   const std::string & username() const { return username_; }
set_username(const std::string & username)101   void set_username(const std::string & username) { username_ = username; }
102 
password()103   const std::string & password() const { return password_; }
set_password(const std::string & password)104   void set_password(const std::string & password) { password_ = password; }
105 
type()106   const std::string & type() const { return type_; }
set_type(const std::string & type)107   void set_type(const std::string & type) { type_ = type; }
108 
network_name()109   const std::string & network_name() const { return network_name_; }
set_network_name(const std::string & network_name)110   void set_network_name(const std::string & network_name) {
111     network_name_ = network_name;
112   }
113 
114   // Candidates in a new generation replace those in the old generation.
generation()115   uint32 generation() const { return generation_; }
set_generation(uint32 generation)116   void set_generation(uint32 generation) { generation_ = generation; }
generation_str()117   const std::string generation_str() const {
118     std::ostringstream ost;
119     ost << generation_;
120     return ost.str();
121   }
set_generation_str(const std::string & str)122   void set_generation_str(const std::string& str) {
123     std::istringstream ist(str);
124     ist >> generation_;
125   }
126 
foundation()127   const std::string& foundation() const {
128     return foundation_;
129   }
130 
set_foundation(const std::string & foundation)131   void set_foundation(const std::string& foundation) {
132     foundation_ = foundation;
133   }
134 
related_address()135   const talk_base::SocketAddress & related_address() const {
136     return related_address_;
137   }
set_related_address(const talk_base::SocketAddress & related_address)138   void set_related_address(
139       const talk_base::SocketAddress & related_address) {
140     related_address_ = related_address;
141   }
142 
143   // Determines whether this candidate is equivalent to the given one.
IsEquivalent(const Candidate & c)144   bool IsEquivalent(const Candidate& c) const {
145     // We ignore the network name, since that is just debug information, and
146     // the priority, since that should be the same if the rest is (and it's
147     // a float so equality checking is always worrisome).
148     return (id_ == c.id_) &&
149            (component_ == c.component_) &&
150            (protocol_ == c.protocol_) &&
151            (address_ == c.address_) &&
152            (username_ == c.username_) &&
153            (password_ == c.password_) &&
154            (type_ == c.type_) &&
155            (generation_ == c.generation_) &&
156            (foundation_ == c.foundation_) &&
157            (related_address_ == c.related_address_);
158   }
159 
ToString()160   std::string ToString() const {
161     return ToStringInternal(false);
162   }
163 
ToSensitiveString()164   std::string ToSensitiveString() const {
165     return ToStringInternal(true);
166   }
167 
GetPriority(uint32 type_preference,int network_adapter_preference)168   uint32 GetPriority(uint32 type_preference,
169                      int network_adapter_preference) const {
170     // RFC 5245 - 4.1.2.1.
171     // priority = (2^24)*(type preference) +
172     //            (2^8)*(local preference) +
173     //            (2^0)*(256 - component ID)
174 
175     // |local_preference| length is 2 bytes, 0-65535 inclusive.
176     // In our implemenation we will partion local_preference into
177     //              0                 1
178     //       0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
179     //      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
180     //      |  NIC Pref     |    Addr Pref  |
181     //      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
182     // NIC Type - Type of the network adapter e.g. 3G/Wifi/Wired.
183     // Addr Pref - Address preference value as per RFC 3484.
184     // local preference is calculated as - NIC Type << 8 | Addr_Pref.
185 
186     int addr_pref = IPAddressPrecedence(address_.ipaddr());
187     int local_preference = (network_adapter_preference << 8) | addr_pref;
188 
189     return (type_preference << 24) |
190            (local_preference << 8) |
191            (256 - component_);
192   }
193 
194  private:
ToStringInternal(bool sensitive)195   std::string ToStringInternal(bool sensitive) const {
196     std::ostringstream ost;
197     std::string address = sensitive ? address_.ToSensitiveString() :
198                                       address_.ToString();
199     ost << "Cand[" << foundation_ << ":" << component_ << ":"
200         << protocol_ << ":" << priority_ << ":"
201         << address << ":" << type_ << ":" << related_address_ << ":"
202         << username_ << ":" << password_ << "]";
203     return ost.str();
204   }
205 
206   std::string id_;
207   int component_;
208   std::string protocol_;
209   talk_base::SocketAddress address_;
210   uint32 priority_;
211   std::string username_;
212   std::string password_;
213   std::string type_;
214   std::string network_name_;
215   uint32 generation_;
216   std::string foundation_;
217   talk_base::SocketAddress related_address_;
218 };
219 
220 }  // namespace cricket
221 
222 #endif  // TALK_P2P_BASE_CANDIDATE_H_
223