stupid WLM 2009 CRC32 typo workaround (ugly, I know...)
[sipe-libnice.git] / agent / component.c
blobd682bae7ee394677979b0a4c30ea96aba68989d2
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.
39 /**
40 * @file component.c
41 * @brief ICE component functions
44 #ifdef HAVE_CONFIG_H
45 # include <config.h>
46 #endif
48 #include <string.h>
50 #include "debug.h"
52 #include "component.h"
53 #include "agent-priv.h"
55 Component *
56 component_new (
57 G_GNUC_UNUSED
58 guint id)
60 Component *component;
62 component = g_slice_new0 (Component);
63 component->id = id;
64 component->state = NICE_COMPONENT_STATE_DISCONNECTED;
65 component->restart_candidate = NULL;
66 return component;
70 void
71 component_free (Component *cmp)
73 GSList *i;
74 GList *item;
76 for (i = cmp->local_candidates; i; i = i->next) {
77 NiceCandidate *candidate = i->data;
78 nice_candidate_free (candidate);
81 for (i = cmp->remote_candidates; i; i = i->next) {
82 NiceCandidate *candidate = i->data;
83 nice_candidate_free (candidate);
86 if (cmp->restart_candidate)
87 nice_candidate_free (cmp->restart_candidate),
88 cmp->restart_candidate = NULL;
90 for (i = cmp->sockets; i; i = i->next) {
91 NiceSocket *udpsocket = i->data;
92 nice_socket_free (udpsocket);
95 for (i = cmp->gsources; i; i = i->next) {
96 GSource *source = i->data;
97 g_source_destroy (source);
98 g_source_unref (source);
101 for (i = cmp->incoming_checks; i; i = i->next) {
102 IncomingCheck *icheck = i->data;
103 g_slice_free (IncomingCheck, icheck);
106 g_slist_free (cmp->local_candidates);
107 g_slist_free (cmp->remote_candidates);
108 g_slist_free (cmp->sockets);
109 g_slist_free (cmp->gsources);
110 g_slist_free (cmp->incoming_checks);
112 for (item = cmp->turn_servers; item; item = g_list_next (item)) {
113 TurnServer *turn = item->data;
114 g_free (turn->username);
115 g_free (turn->password);
116 g_slice_free (TurnServer, turn);
118 g_list_free (cmp->turn_servers);
120 g_slice_free (Component, cmp);
124 * Finds a candidate pair that has matching foundation ids.
126 * @return TRUE if pair found, pointer to pair stored at 'pair'
128 gboolean
129 component_find_pair (Component *cmp, NiceAgent *agent, const gchar *lfoundation, const gchar *rfoundation, CandidatePair *pair)
131 GSList *i;
132 CandidatePair result = { NULL, NULL, 0 };
134 for (i = cmp->local_candidates; i; i = i->next) {
135 NiceCandidate *candidate = i->data;
136 if (strncmp (candidate->foundation, lfoundation, NICE_CANDIDATE_MAX_FOUNDATION) == 0) {
137 result.local = candidate;
138 break;
142 for (i = cmp->remote_candidates; i; i = i->next) {
143 NiceCandidate *candidate = i->data;
144 if (strncmp (candidate->foundation, rfoundation, NICE_CANDIDATE_MAX_FOUNDATION) == 0) {
145 result.remote = candidate;
146 break;
150 if (result.local && result.remote) {
151 result.priority = agent_candidate_pair_priority (agent, result.local, result.remote);
152 if (pair)
153 *pair = result;
154 return TRUE;
157 return FALSE;
161 * Resets the component state to that of a ICE restarted
162 * session.
164 gboolean
165 component_restart (Component *cmp)
167 GSList *i;
169 for (i = cmp->remote_candidates; i; i = i->next) {
170 NiceCandidate *candidate = i->data;
172 /* note: do not remove the remote candidate that is
173 * currently part of the 'selected pair', see ICE
174 * 9.1.1.1. "ICE Restarts" (ID-19) */
175 if (candidate == cmp->selected_pair.remote) {
176 if (cmp->restart_candidate)
177 nice_candidate_free (cmp->restart_candidate);
178 cmp->restart_candidate = candidate;
180 else
181 nice_candidate_free (candidate);
183 g_slist_free (cmp->remote_candidates),
184 cmp->remote_candidates = NULL;
186 for (i = cmp->incoming_checks; i; i = i->next) {
187 IncomingCheck *icheck = i->data;
188 g_slice_free (IncomingCheck, icheck);
191 /* note: component state managed by agent */
193 return TRUE;
197 * Changes the selected pair for the component to 'pair'. Does not
198 * emit the "selected-pair-changed" signal.
200 void component_update_selected_pair (Component *component, const CandidatePair *pair)
202 g_assert (component);
203 g_assert (pair);
204 nice_debug ("setting SELECTED PAIR for component %u: %s:%s (prio:%lu).",
205 component->id, pair->local->foundation, pair->remote->foundation, (long unsigned)pair->priority);
206 component->selected_pair.local = pair->local;
207 component->selected_pair.remote = pair->remote;
208 component->selected_pair.priority = pair->priority;
212 * Finds a remote candidate with matching address and
213 * transport.
215 * @return pointer to candidate or NULL if not found
217 NiceCandidate *
218 component_find_remote_candidate (const Component *component, const NiceAddress *addr, NiceCandidateTransport transport)
220 GSList *i;
222 for (i = component->remote_candidates; i; i = i->next) {
223 NiceCandidate *candidate = i->data;
225 if (nice_address_equal(&candidate->addr, addr) &&
226 candidate->transport == transport)
227 return candidate;
231 return NULL;
235 * Sets the desired remote candidate as the selected pair
237 * It will start sending on the highest priority pair available with
238 * this candidate.
241 NiceCandidate *
242 component_set_selected_remote_candidate (NiceAgent *agent, Component *component,
243 NiceCandidate *candidate)
245 NiceCandidate *local = NULL;
246 NiceCandidate *remote = NULL;
247 guint32 priority = 0;
248 GSList *item = NULL;
250 for (item = component->local_candidates; item; item = g_slist_next (item)) {
251 NiceCandidate *tmp = item->data;
252 guint32 tmp_prio = 0;
254 if (tmp->transport != candidate->transport ||
255 tmp->addr.s.addr.sa_family != candidate->addr.s.addr.sa_family ||
256 tmp->type != NICE_CANDIDATE_TYPE_HOST)
257 continue;
259 tmp_prio = agent_candidate_pair_priority (agent, tmp, candidate);
261 if (tmp_prio > priority) {
262 priority = tmp_prio;
263 local = tmp;
267 if (local == NULL)
268 return NULL;
270 remote = component_find_remote_candidate (component, &candidate->addr,
271 candidate->transport);
273 if (!remote) {
274 GSList *modified_list = NULL;
276 remote = nice_candidate_copy (candidate);
278 modified_list = g_slist_append (component->remote_candidates,
279 remote);
280 if (modified_list) {
281 component->remote_candidates = modified_list;
282 agent_signal_new_remote_candidate (agent, remote);
284 else { /* error: memory alloc / list */
285 nice_candidate_free (remote), remote = NULL;
286 return NULL;
290 component->selected_pair.local = local;
291 component->selected_pair.remote = remote;
292 component->selected_pair.priority = priority;
294 return local;