Make libnice dependent on gupnp-igd >= 0.1.2 since the error-mapping signal API changed
[sipe-libnice.git] / agent / candidate.c
blob4d19615277e9bcec32ff228b866d1723645bc128
1 /*
2 * This file is part of the Nice GLib ICE library.
4 * (C) 2006, 2007 Collabora Ltd.
5 * Contact: Dafydd Harries
6 * (C) 2006, 2007 Nokia Corporation. All rights reserved.
7 * Contact: Kai Vehmanen
9 * The contents of this file are subject to the Mozilla Public License Version
10 * 1.1 (the "License"); you may not use this file except in compliance with
11 * the License. You may obtain a copy of the License at
12 * http://www.mozilla.org/MPL/
14 * Software distributed under the License is distributed on an "AS IS" basis,
15 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
16 * for the specific language governing rights and limitations under the
17 * License.
19 * The Original Code is the Nice GLib ICE library.
21 * The Initial Developers of the Original Code are Collabora Ltd and Nokia
22 * Corporation. All Rights Reserved.
24 * Contributors:
25 * Dafydd Harries, Collabora Ltd.
26 * Kai Vehmanen, Nokia
28 * Alternatively, the contents of this file may be used under the terms of the
29 * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
30 * case the provisions of LGPL are applicable instead of those above. If you
31 * wish to allow use of your version of this file only under the terms of the
32 * LGPL and not to allow others to use your version of this file under the
33 * MPL, indicate your decision by deleting the provisions above and replace
34 * them with the notice and other provisions required by the LGPL. If you do
35 * not delete the provisions above, a recipient may use your version of this
36 * file under either the MPL or the LGPL.
40 * @file candidate.c
41 * @brief ICE candidate functions
44 #ifdef HAVE_CONFIG_H
45 # include <config.h>
46 #else
47 #define NICEAPI_EXPORT
48 #endif
50 #include <string.h>
52 #include "agent.h"
53 #include "component.h"
55 /* (ICE 4.1.1 "Gathering Candidates") ""Every candidate is a transport
56 * address. It also has a type and a base. Three types are defined and
57 * gathered by this specification - host candidates, server reflexive
58 * candidates, and relayed candidates."" (ID-19) */
60 NICEAPI_EXPORT NiceCandidate *
61 nice_candidate_new (NiceCandidateType type)
63 NiceCandidate *candidate;
65 candidate = g_slice_new0 (NiceCandidate);
66 candidate->type = type;
67 return candidate;
71 NICEAPI_EXPORT void
72 nice_candidate_free (NiceCandidate *candidate)
74 /* better way of checking if socket is allocated? */
76 if (candidate->username)
77 g_free (candidate->username);
79 if (candidate->password)
80 g_free (candidate->password);
82 g_slice_free (NiceCandidate, candidate);
86 guint32
87 nice_candidate_jingle_priority (NiceCandidate *candidate)
89 switch (candidate->type)
91 case NICE_CANDIDATE_TYPE_HOST: return 1000;
92 case NICE_CANDIDATE_TYPE_SERVER_REFLEXIVE: return 900;
93 case NICE_CANDIDATE_TYPE_PEER_REFLEXIVE: return 900;
94 case NICE_CANDIDATE_TYPE_RELAYED: return 500;
97 /* appease GCC */
98 return 0;
101 guint32
102 nice_candidate_msn_priority (NiceCandidate *candidate)
104 switch (candidate->type)
106 case NICE_CANDIDATE_TYPE_HOST: return 830;
107 case NICE_CANDIDATE_TYPE_SERVER_REFLEXIVE: return 550;
108 case NICE_CANDIDATE_TYPE_PEER_REFLEXIVE: return 550;
109 case NICE_CANDIDATE_TYPE_RELAYED: return 450;
112 /* appease GCC */
113 return 0;
118 * ICE 4.1.2.1. "Recommended Formula" (ID-19):
119 * returns number between 1 and 0x7effffff
121 guint32
122 nice_candidate_ice_priority_full (
123 // must be ∈ (0, 126) (max 2^7 - 2)
124 guint type_preference,
125 // must be ∈ (0, 65535) (max 2^16 - 1)
126 guint local_preference,
127 // must be ∈ (0, 255) (max 2 ^ 8 - 1)
128 guint component_id)
130 return (
131 0x1000000 * type_preference +
132 0x100 * local_preference +
133 (0x100 - component_id));
137 guint32
138 nice_candidate_ice_priority (const NiceCandidate *candidate)
140 guint8 type_preference = 0;
142 switch (candidate->type)
144 case NICE_CANDIDATE_TYPE_HOST:
145 type_preference = NICE_CANDIDATE_TYPE_PREF_HOST; break;
146 case NICE_CANDIDATE_TYPE_PEER_REFLEXIVE:
147 type_preference = NICE_CANDIDATE_TYPE_PREF_PEER_REFLEXIVE; break;
148 case NICE_CANDIDATE_TYPE_SERVER_REFLEXIVE:
149 type_preference = NICE_CANDIDATE_TYPE_PREF_SERVER_REFLEXIVE; break;
150 case NICE_CANDIDATE_TYPE_RELAYED:
151 type_preference = NICE_CANDIDATE_TYPE_PREF_RELAYED; break;
154 /* return _candidate_ice_priority (type_preference, 1, candidate->component_id); */
155 return nice_candidate_ice_priority_full (type_preference, 1, candidate->component_id);
159 * Calculates the pair priority as specified in ICE
160 * sect 5.7.2. "Computing Pair Priority and Ordering Pairs" (ID-19).
162 guint64
163 nice_candidate_pair_priority (guint32 o_prio, guint32 a_prio)
165 guint32 max = o_prio > a_prio ? o_prio : a_prio;
166 guint32 min = o_prio < a_prio ? o_prio : a_prio;
168 return ((guint64)1 << 32) * min + 2 * max + (o_prio > a_prio ? 1 : 0);
172 * Copies a candidate
174 NICEAPI_EXPORT NiceCandidate *
175 nice_candidate_copy (const NiceCandidate *candidate)
177 NiceCandidate *copy = nice_candidate_new (candidate->type);
179 memcpy (copy, candidate, sizeof(NiceCandidate));
181 copy->username = g_strdup (copy->username);
182 copy->password = g_strdup (copy->password);
184 return copy;