version 0.1.1
[sipe-libnice.git] / agent / candidate.c
blobb7636a89cf17ccc646fdcd56aa0e83464c815b98
1 /*
2 * This file is part of the Nice GLib ICE library.
4 * (C) 2006-2009 Collabora Ltd.
5 * Contact: Youness Alaoui
6 * (C) 2006-2009 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 * Youness Alaoui, Collabora Ltd.
27 * Kai Vehmanen, Nokia
29 * Alternatively, the contents of this file may be used under the terms of the
30 * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
31 * case the provisions of LGPL are applicable instead of those above. If you
32 * wish to allow use of your version of this file only under the terms of the
33 * LGPL and not to allow others to use your version of this file under the
34 * MPL, indicate your decision by deleting the provisions above and replace
35 * them with the notice and other provisions required by the LGPL. If you do
36 * not delete the provisions above, a recipient may use your version of this
37 * file under either the MPL or the LGPL.
41 * @file candidate.c
42 * @brief ICE candidate functions
45 #ifdef HAVE_CONFIG_H
46 # include <config.h>
47 #else
48 #define NICEAPI_EXPORT
49 #endif
51 #include <string.h>
53 #include "agent.h"
54 #include "component.h"
56 /* (ICE 4.1.1 "Gathering Candidates") ""Every candidate is a transport
57 * address. It also has a type and a base. Three types are defined and
58 * gathered by this specification - host candidates, server reflexive
59 * candidates, and relayed candidates."" (ID-19) */
61 NICEAPI_EXPORT NiceCandidate *
62 nice_candidate_new (NiceCandidateType type)
64 NiceCandidate *candidate;
66 candidate = g_slice_new0 (NiceCandidate);
67 candidate->type = type;
68 return candidate;
72 NICEAPI_EXPORT void
73 nice_candidate_free (NiceCandidate *candidate)
75 /* better way of checking if socket is allocated? */
77 if (candidate->username)
78 g_free (candidate->username);
80 if (candidate->password)
81 g_free (candidate->password);
83 g_slice_free (NiceCandidate, candidate);
87 guint32
88 nice_candidate_jingle_priority (NiceCandidate *candidate)
90 switch (candidate->type)
92 case NICE_CANDIDATE_TYPE_HOST: return 1000;
93 case NICE_CANDIDATE_TYPE_SERVER_REFLEXIVE: return 900;
94 case NICE_CANDIDATE_TYPE_PEER_REFLEXIVE: return 900;
95 case NICE_CANDIDATE_TYPE_RELAYED: return 500;
98 /* appease GCC */
99 return 0;
102 guint32
103 nice_candidate_msn_priority (NiceCandidate *candidate)
105 switch (candidate->type)
107 case NICE_CANDIDATE_TYPE_HOST: return 830;
108 case NICE_CANDIDATE_TYPE_SERVER_REFLEXIVE: return 550;
109 case NICE_CANDIDATE_TYPE_PEER_REFLEXIVE: return 550;
110 case NICE_CANDIDATE_TYPE_RELAYED: return 450;
113 /* appease GCC */
114 return 0;
119 * ICE 4.1.2.1. "Recommended Formula" (ID-19):
120 * returns number between 1 and 0x7effffff
122 guint32
123 nice_candidate_ice_priority_full (
124 // must be ∈ (0, 126) (max 2^7 - 2)
125 guint type_preference,
126 // must be ∈ (0, 65535) (max 2^16 - 1)
127 guint local_preference,
128 // must be ∈ (0, 255) (max 2 ^ 8 - 1)
129 guint component_id)
131 return (
132 0x1000000 * type_preference +
133 0x100 * local_preference +
134 (0x100 - component_id));
138 guint32
139 nice_candidate_ice_priority (const NiceCandidate *candidate)
141 guint8 type_preference = 0;
143 switch (candidate->type)
145 case NICE_CANDIDATE_TYPE_HOST:
146 type_preference = NICE_CANDIDATE_TYPE_PREF_HOST; break;
147 case NICE_CANDIDATE_TYPE_PEER_REFLEXIVE:
148 type_preference = NICE_CANDIDATE_TYPE_PREF_PEER_REFLEXIVE; break;
149 case NICE_CANDIDATE_TYPE_SERVER_REFLEXIVE:
150 type_preference = NICE_CANDIDATE_TYPE_PREF_SERVER_REFLEXIVE; break;
151 case NICE_CANDIDATE_TYPE_RELAYED:
152 type_preference = NICE_CANDIDATE_TYPE_PREF_RELAYED; break;
155 /* return _candidate_ice_priority (type_preference, 1, candidate->component_id); */
156 return nice_candidate_ice_priority_full (type_preference, 1, candidate->component_id);
160 * Calculates the pair priority as specified in ICE
161 * sect 5.7.2. "Computing Pair Priority and Ordering Pairs" (ID-19).
163 guint64
164 nice_candidate_pair_priority (guint32 o_prio, guint32 a_prio)
166 guint32 max = o_prio > a_prio ? o_prio : a_prio;
167 guint32 min = o_prio < a_prio ? o_prio : a_prio;
169 return ((guint64)1 << 32) * min + 2 * max + (o_prio > a_prio ? 1 : 0);
173 * Copies a candidate
175 NICEAPI_EXPORT NiceCandidate *
176 nice_candidate_copy (const NiceCandidate *candidate)
178 NiceCandidate *copy = nice_candidate_new (candidate->type);
180 memcpy (copy, candidate, sizeof(NiceCandidate));
182 copy->username = g_strdup (copy->username);
183 copy->password = g_strdup (copy->password);
185 return copy;