Make stun_timer timeouts configurable (breaks API)
[sipe-libnice.git] / agent / component.c
blob0c45976230ff63e911495ab5e8ddd7ab5a76b2a4
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 component.c
42 * @brief ICE component functions
45 #ifdef HAVE_CONFIG_H
46 # include <config.h>
47 #endif
49 #include <string.h>
51 #include "debug.h"
53 #include "component.h"
54 #include "agent-priv.h"
56 Component *
57 component_new (guint id)
59 Component *component;
61 component = g_slice_new0 (Component);
62 component->id = id;
63 component->state = NICE_COMPONENT_STATE_DISCONNECTED;
64 component->restart_candidate = NULL;
65 component->tcp = NULL;
67 return component;
71 void
72 component_free (Component *cmp)
74 GSList *i;
75 GList *item;
77 for (i = cmp->local_candidates; i; i = i->next) {
78 NiceCandidate *candidate = i->data;
79 nice_candidate_free (candidate);
82 for (i = cmp->remote_candidates; i; i = i->next) {
83 NiceCandidate *candidate = i->data;
84 nice_candidate_free (candidate);
87 if (cmp->restart_candidate)
88 nice_candidate_free (cmp->restart_candidate),
89 cmp->restart_candidate = NULL;
91 for (i = cmp->sockets; i; i = i->next) {
92 NiceSocket *udpsocket = i->data;
93 nice_socket_free (udpsocket);
96 for (i = cmp->gsources; i; i = i->next) {
97 GSource *source = i->data;
98 g_source_destroy (source);
99 g_source_unref (source);
102 for (i = cmp->incoming_checks; i; i = i->next) {
103 IncomingCheck *icheck = i->data;
104 g_free (icheck->username);
105 g_slice_free (IncomingCheck, icheck);
108 g_slist_free (cmp->local_candidates);
109 g_slist_free (cmp->remote_candidates);
110 g_slist_free (cmp->sockets);
111 g_slist_free (cmp->gsources);
112 g_slist_free (cmp->incoming_checks);
114 for (item = cmp->turn_servers; item; item = g_list_next (item)) {
115 TurnServer *turn = item->data;
116 g_free (turn->username);
117 g_free (turn->password);
118 g_slice_free (TurnServer, turn);
120 g_list_free (cmp->turn_servers);
122 if (cmp->selected_pair.keepalive.tick_source != NULL) {
123 g_source_destroy (cmp->selected_pair.keepalive.tick_source);
124 g_source_unref (cmp->selected_pair.keepalive.tick_source);
125 cmp->selected_pair.keepalive.tick_source = NULL;
128 if (cmp->tcp_clock) {
129 g_source_destroy (cmp->tcp_clock);
130 g_source_unref (cmp->tcp_clock);
131 cmp->tcp_clock = NULL;
133 if (cmp->tcp) {
134 pseudo_tcp_socket_close (cmp->tcp, TRUE);
135 g_object_unref (cmp->tcp);
136 cmp->tcp = NULL;
138 if (cmp->tcp_data != NULL) {
139 g_slice_free (TcpUserData, cmp->tcp_data);
140 cmp->tcp_data = NULL;
143 g_slice_free (Component, cmp);
147 * Finds a candidate pair that has matching foundation ids.
149 * @return TRUE if pair found, pointer to pair stored at 'pair'
151 gboolean
152 component_find_pair (Component *cmp, NiceAgent *agent, const gchar *lfoundation, const gchar *rfoundation, CandidatePair *pair)
154 GSList *i;
155 CandidatePair result;
157 memset (&result, 0, sizeof(result));
159 for (i = cmp->local_candidates; i; i = i->next) {
160 NiceCandidate *candidate = i->data;
161 if (strncmp (candidate->foundation, lfoundation, NICE_CANDIDATE_MAX_FOUNDATION) == 0) {
162 result.local = candidate;
163 break;
167 for (i = cmp->remote_candidates; i; i = i->next) {
168 NiceCandidate *candidate = i->data;
169 if (strncmp (candidate->foundation, rfoundation, NICE_CANDIDATE_MAX_FOUNDATION) == 0) {
170 result.remote = candidate;
171 break;
175 if (result.local && result.remote) {
176 result.priority = agent_candidate_pair_priority (agent, result.local, result.remote);
177 if (pair)
178 *pair = result;
179 return TRUE;
182 return FALSE;
186 * Resets the component state to that of a ICE restarted
187 * session.
189 gboolean
190 component_restart (Component *cmp)
192 GSList *i;
194 for (i = cmp->remote_candidates; i; i = i->next) {
195 NiceCandidate *candidate = i->data;
197 /* note: do not remove the remote candidate that is
198 * currently part of the 'selected pair', see ICE
199 * 9.1.1.1. "ICE Restarts" (ID-19) */
200 if (candidate == cmp->selected_pair.remote) {
201 if (cmp->restart_candidate)
202 nice_candidate_free (cmp->restart_candidate);
203 cmp->restart_candidate = candidate;
205 else
206 nice_candidate_free (candidate);
208 g_slist_free (cmp->remote_candidates),
209 cmp->remote_candidates = NULL;
211 for (i = cmp->incoming_checks; i; i = i->next) {
212 IncomingCheck *icheck = i->data;
213 g_free (icheck->username);
214 g_slice_free (IncomingCheck, icheck);
216 g_slist_free (cmp->incoming_checks);
217 cmp->incoming_checks = NULL;
219 /* note: component state managed by agent */
221 return TRUE;
225 * Changes the selected pair for the component to 'pair'. Does not
226 * emit the "selected-pair-changed" signal.
228 void component_update_selected_pair (Component *component, const CandidatePair *pair)
230 g_assert (component);
231 g_assert (pair);
232 nice_debug ("setting SELECTED PAIR for component %u: %s:%s (prio:%"
233 G_GUINT64_FORMAT ").", component->id, pair->local->foundation,
234 pair->remote->foundation, pair->priority);
236 if (component->selected_pair.keepalive.tick_source != NULL) {
237 g_source_destroy (component->selected_pair.keepalive.tick_source);
238 g_source_unref (component->selected_pair.keepalive.tick_source);
239 component->selected_pair.keepalive.tick_source = NULL;
242 memset (&component->selected_pair, 0, sizeof(CandidatePair));
244 component->selected_pair.local = pair->local;
245 component->selected_pair.remote = pair->remote;
246 component->selected_pair.priority = pair->priority;
251 * Finds a remote candidate with matching address and
252 * transport.
254 * @return pointer to candidate or NULL if not found
256 NiceCandidate *
257 component_find_remote_candidate (const Component *component, const NiceAddress *addr, NiceCandidateTransport transport)
259 GSList *i;
261 for (i = component->remote_candidates; i; i = i->next) {
262 NiceCandidate *candidate = i->data;
264 if (nice_address_equal(&candidate->addr, addr) &&
265 candidate->transport == transport)
266 return candidate;
270 return NULL;
274 * Sets the desired remote candidate as the selected pair
276 * It will start sending on the highest priority pair available with
277 * this candidate.
280 NiceCandidate *
281 component_set_selected_remote_candidate (NiceAgent *agent, Component *component,
282 NiceCandidate *candidate)
284 NiceCandidate *local = NULL;
285 NiceCandidate *remote = NULL;
286 guint32 priority = 0;
287 GSList *item = NULL;
289 for (item = component->local_candidates; item; item = g_slist_next (item)) {
290 NiceCandidate *tmp = item->data;
291 guint32 tmp_prio = 0;
293 if (tmp->transport != candidate->transport ||
294 tmp->addr.s.addr.sa_family != candidate->addr.s.addr.sa_family ||
295 tmp->type != NICE_CANDIDATE_TYPE_HOST)
296 continue;
298 tmp_prio = agent_candidate_pair_priority (agent, tmp, candidate);
300 if (tmp_prio > priority) {
301 priority = tmp_prio;
302 local = tmp;
306 if (local == NULL)
307 return NULL;
309 remote = component_find_remote_candidate (component, &candidate->addr,
310 candidate->transport);
312 if (!remote) {
313 remote = nice_candidate_copy (candidate);
314 component->remote_candidates = g_slist_append (component->remote_candidates,
315 remote);
316 agent_signal_new_remote_candidate (agent, remote);
319 if (component->selected_pair.keepalive.tick_source != NULL) {
320 g_source_destroy (component->selected_pair.keepalive.tick_source);
321 g_source_unref (component->selected_pair.keepalive.tick_source);
322 component->selected_pair.keepalive.tick_source = NULL;
325 memset (&component->selected_pair, 0, sizeof(CandidatePair));
326 component->selected_pair.local = local;
327 component->selected_pair.remote = remote;
328 component->selected_pair.priority = priority;
330 return local;