blob: 0a8459153bb49baf783229796adefd59e4033d67 [file] [log] [blame]
Patrik Höglunde2d6a062017-10-05 14:53:33 +02001/*
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 API_CANDIDATE_H_
12#define API_CANDIDATE_H_
13
14#include <limits.h>
15#include <stdint.h>
16
17#include <algorithm>
18#include <string>
19
20#include "rtc_base/checks.h"
Patrik Höglunde2d6a062017-10-05 14:53:33 +020021#include "rtc_base/network_constants.h"
22#include "rtc_base/socketaddress.h"
23
24namespace cricket {
25
26// Candidate for ICE based connection discovery.
27// TODO(phoglund): remove things in here that are not needed in the public API.
28
29class Candidate {
30 public:
Steve Anton36b28db2017-10-26 11:27:17 -070031 Candidate();
Patrik Höglunde2d6a062017-10-05 14:53:33 +020032 // TODO(pthatcher): Match the ordering and param list as per RFC 5245
33 // candidate-attribute syntax. http://tools.ietf.org/html/rfc5245#section-15.1
Patrik Höglunde2d6a062017-10-05 14:53:33 +020034 Candidate(int component,
35 const std::string& protocol,
36 const rtc::SocketAddress& address,
37 uint32_t priority,
38 const std::string& username,
39 const std::string& password,
40 const std::string& type,
41 uint32_t generation,
42 const std::string& foundation,
43 uint16_t network_id = 0,
Steve Anton36b28db2017-10-26 11:27:17 -070044 uint16_t network_cost = 0);
45 Candidate(const Candidate&);
46 ~Candidate();
Patrik Höglunde2d6a062017-10-05 14:53:33 +020047
Yves Gerey665174f2018-06-19 15:03:05 +020048 const std::string& id() const { return id_; }
49 void set_id(const std::string& id) { id_ = id; }
Patrik Höglunde2d6a062017-10-05 14:53:33 +020050
51 int component() const { return component_; }
52 void set_component(int component) { component_ = component; }
53
Yves Gerey665174f2018-06-19 15:03:05 +020054 const std::string& protocol() const { return protocol_; }
55 void set_protocol(const std::string& protocol) { protocol_ = protocol; }
Patrik Höglunde2d6a062017-10-05 14:53:33 +020056
57 // The protocol used to talk to relay.
58 const std::string& relay_protocol() const { return relay_protocol_; }
59 void set_relay_protocol(const std::string& protocol) {
60 relay_protocol_ = protocol;
61 }
62
Yves Gerey665174f2018-06-19 15:03:05 +020063 const rtc::SocketAddress& address() const { return address_; }
64 void set_address(const rtc::SocketAddress& address) { address_ = address; }
Patrik Höglunde2d6a062017-10-05 14:53:33 +020065
66 uint32_t priority() const { return priority_; }
67 void set_priority(const uint32_t priority) { priority_ = priority; }
68
69 // TODO(pthatcher): Remove once Chromium's jingle/glue/utils.cc
70 // doesn't use it.
71 // Maps old preference (which was 0.0-1.0) to match priority (which
72 // is 0-2^32-1) to to match RFC 5245, section 4.1.2.1. Also see
73 // https://docs.google.com/a/google.com/document/d/
74 // 1iNQDiwDKMh0NQOrCqbj3DKKRT0Dn5_5UJYhmZO-t7Uc/edit
75 float preference() const {
76 // The preference value is clamped to two decimal precision.
77 return static_cast<float>(((priority_ >> 24) * 100 / 127) / 100.0);
78 }
79
80 // TODO(pthatcher): Remove once Chromium's jingle/glue/utils.cc
81 // doesn't use it.
82 void set_preference(float preference) {
83 // Limiting priority to UINT_MAX when value exceeds uint32_t max.
84 // This can happen for e.g. when preference = 3.
85 uint64_t prio_val = static_cast<uint64_t>(preference * 127) << 24;
86 priority_ = static_cast<uint32_t>(
87 std::min(prio_val, static_cast<uint64_t>(UINT_MAX)));
88 }
89
90 // TODO(honghaiz): Change to usernameFragment or ufrag.
Yves Gerey665174f2018-06-19 15:03:05 +020091 const std::string& username() const { return username_; }
92 void set_username(const std::string& username) { username_ = username; }
Patrik Höglunde2d6a062017-10-05 14:53:33 +020093
Yves Gerey665174f2018-06-19 15:03:05 +020094 const std::string& password() const { return password_; }
95 void set_password(const std::string& password) { password_ = password; }
Patrik Höglunde2d6a062017-10-05 14:53:33 +020096
Yves Gerey665174f2018-06-19 15:03:05 +020097 const std::string& type() const { return type_; }
98 void set_type(const std::string& type) { type_ = type; }
Patrik Höglunde2d6a062017-10-05 14:53:33 +020099
Yves Gerey665174f2018-06-19 15:03:05 +0200100 const std::string& network_name() const { return network_name_; }
101 void set_network_name(const std::string& network_name) {
Patrik Höglunde2d6a062017-10-05 14:53:33 +0200102 network_name_ = network_name;
103 }
104
105 rtc::AdapterType network_type() const { return network_type_; }
106 void set_network_type(rtc::AdapterType network_type) {
107 network_type_ = network_type;
108 }
109
110 // Candidates in a new generation replace those in the old generation.
111 uint32_t generation() const { return generation_; }
112 void set_generation(uint32_t generation) { generation_ = generation; }
Patrik Höglunde2d6a062017-10-05 14:53:33 +0200113
114 // |network_cost| measures the cost/penalty of using this candidate. A network
115 // cost of 0 indicates this candidate can be used freely. A value of
116 // rtc::kNetworkCostMax indicates it should be used only as the last resort.
117 void set_network_cost(uint16_t network_cost) {
118 RTC_DCHECK_LE(network_cost, rtc::kNetworkCostMax);
119 network_cost_ = network_cost;
120 }
121 uint16_t network_cost() const { return network_cost_; }
122
123 // An ID assigned to the network hosting the candidate.
124 uint16_t network_id() const { return network_id_; }
125 void set_network_id(uint16_t network_id) { network_id_ = network_id; }
126
Yves Gerey665174f2018-06-19 15:03:05 +0200127 const std::string& foundation() const { return foundation_; }
Patrik Höglunde2d6a062017-10-05 14:53:33 +0200128 void set_foundation(const std::string& foundation) {
129 foundation_ = foundation;
130 }
131
Yves Gerey665174f2018-06-19 15:03:05 +0200132 const rtc::SocketAddress& related_address() const { return related_address_; }
133 void set_related_address(const rtc::SocketAddress& related_address) {
Patrik Höglunde2d6a062017-10-05 14:53:33 +0200134 related_address_ = related_address;
135 }
136 const std::string& tcptype() const { return tcptype_; }
Yves Gerey665174f2018-06-19 15:03:05 +0200137 void set_tcptype(const std::string& tcptype) { tcptype_ = tcptype; }
Patrik Höglunde2d6a062017-10-05 14:53:33 +0200138
139 // The name of the transport channel of this candidate.
140 // TODO(phoglund): remove.
141 const std::string& transport_name() const { return transport_name_; }
142 void set_transport_name(const std::string& transport_name) {
143 transport_name_ = transport_name;
144 }
145
146 // The URL of the ICE server which this candidate is gathered from.
147 const std::string& url() const { return url_; }
148 void set_url(const std::string& url) { url_ = url; }
149
150 // Determines whether this candidate is equivalent to the given one.
Steve Anton36b28db2017-10-26 11:27:17 -0700151 bool IsEquivalent(const Candidate& c) const;
Patrik Höglunde2d6a062017-10-05 14:53:33 +0200152
153 // Determines whether this candidate can be considered equivalent to the
154 // given one when looking for a matching candidate to remove.
Steve Anton36b28db2017-10-26 11:27:17 -0700155 bool MatchesForRemoval(const Candidate& c) const;
Patrik Höglunde2d6a062017-10-05 14:53:33 +0200156
Yves Gerey665174f2018-06-19 15:03:05 +0200157 std::string ToString() const { return ToStringInternal(false); }
Patrik Höglunde2d6a062017-10-05 14:53:33 +0200158
Yves Gerey665174f2018-06-19 15:03:05 +0200159 std::string ToSensitiveString() const { return ToStringInternal(true); }
Patrik Höglunde2d6a062017-10-05 14:53:33 +0200160
161 uint32_t GetPriority(uint32_t type_preference,
162 int network_adapter_preference,
Steve Anton36b28db2017-10-26 11:27:17 -0700163 int relay_preference) const;
Patrik Höglunde2d6a062017-10-05 14:53:33 +0200164
Steve Anton36b28db2017-10-26 11:27:17 -0700165 bool operator==(const Candidate& o) const;
166 bool operator!=(const Candidate& o) const;
Patrik Höglunde2d6a062017-10-05 14:53:33 +0200167
168 private:
Steve Anton36b28db2017-10-26 11:27:17 -0700169 std::string ToStringInternal(bool sensitive) const;
Patrik Höglunde2d6a062017-10-05 14:53:33 +0200170
171 std::string id_;
172 int component_;
173 std::string protocol_;
174 std::string relay_protocol_;
175 rtc::SocketAddress address_;
176 uint32_t priority_;
177 std::string username_;
178 std::string password_;
179 std::string type_;
180 std::string network_name_;
181 rtc::AdapterType network_type_;
182 uint32_t generation_;
183 std::string foundation_;
184 rtc::SocketAddress related_address_;
185 std::string tcptype_;
186 std::string transport_name_;
187 uint16_t network_id_;
188 uint16_t network_cost_;
189 std::string url_;
190};
191
192} // namespace cricket
193
194#endif // API_CANDIDATE_H_