~ubuntu-branches/debian/squeeze/libnice/squeeze

« back to all changes in this revision

Viewing changes to agent/candidate.c

  • Committer: Bazaar Package Importer
  • Author(s): Laurent Bigonville
  • Date: 2009-01-04 17:45:34 UTC
  • Revision ID: james.westby@ubuntu.com-20090104174534-dh5u1pfonumqa99c
Tags: upstream-0.0.4
Import upstream version 0.0.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * This file is part of the Nice GLib ICE library.
 
3
 *
 
4
 * (C) 2006, 2007 Collabora Ltd.
 
5
 *  Contact: Dafydd Harries
 
6
 * (C) 2006, 2007 Nokia Corporation. All rights reserved.
 
7
 *  Contact: Kai Vehmanen
 
8
 *
 
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/
 
13
 *
 
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.
 
18
 *
 
19
 * The Original Code is the Nice GLib ICE library.
 
20
 *
 
21
 * The Initial Developers of the Original Code are Collabora Ltd and Nokia
 
22
 * Corporation. All Rights Reserved.
 
23
 *
 
24
 * Contributors:
 
25
 *   Dafydd Harries, Collabora Ltd.
 
26
 *   Kai Vehmanen, Nokia
 
27
 *
 
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.
 
37
 */
 
38
 
 
39
/**
 
40
 * @file candidate.c
 
41
 * @brief ICE candidate functions
 
42
 */
 
43
 
 
44
#ifdef HAVE_CONFIG_H
 
45
# include <config.h>
 
46
#else
 
47
#define NICEAPI_EXPORT
 
48
#endif
 
49
 
 
50
#include <string.h>
 
51
 
 
52
#include "agent.h"
 
53
#include "component.h"
 
54
 
 
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) */
 
59
 
 
60
NICEAPI_EXPORT NiceCandidate *
 
61
nice_candidate_new (NiceCandidateType type)
 
62
{
 
63
  NiceCandidate *candidate;
 
64
 
 
65
  candidate = g_slice_new0 (NiceCandidate);
 
66
  candidate->type = type;
 
67
  return candidate;
 
68
}
 
69
 
 
70
 
 
71
NICEAPI_EXPORT void
 
72
nice_candidate_free (NiceCandidate *candidate)
 
73
{
 
74
  /* better way of checking if socket is allocated? */
 
75
 
 
76
  if (candidate->username)
 
77
    g_free (candidate->username);
 
78
 
 
79
  if (candidate->password)
 
80
    g_free (candidate->password);
 
81
 
 
82
  g_slice_free (NiceCandidate, candidate);
 
83
}
 
84
 
 
85
 
 
86
guint32
 
87
nice_candidate_jingle_priority (NiceCandidate *candidate)
 
88
{
 
89
  switch (candidate->type)
 
90
    {
 
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;
 
95
    }
 
96
 
 
97
  /* appease GCC */
 
98
  return 0;
 
99
}
 
100
 
 
101
guint32
 
102
nice_candidate_msn_priority (NiceCandidate *candidate)
 
103
{
 
104
  switch (candidate->type)
 
105
    {
 
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;
 
110
    }
 
111
 
 
112
  /* appease GCC */
 
113
  return 0;
 
114
}
 
115
 
 
116
 
 
117
/**
 
118
 * ICE 4.1.2.1. "Recommended Formula" (ID-19):
 
119
 * returns number between 1 and 0x7effffff 
 
120
 */
 
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)
 
129
{
 
130
  return (
 
131
      0x1000000 * type_preference +
 
132
      0x100 * local_preference +
 
133
      (0x100 - component_id));
 
134
}
 
135
 
 
136
 
 
137
guint32
 
138
nice_candidate_ice_priority (const NiceCandidate *candidate)
 
139
{
 
140
  guint8 type_preference = 0;
 
141
 
 
142
  switch (candidate->type)
 
143
    {
 
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;
 
152
    }
 
153
 
 
154
  /* return _candidate_ice_priority (type_preference, 1, candidate->component_id); */
 
155
  return nice_candidate_ice_priority_full (type_preference, 1, candidate->component_id);
 
156
}
 
157
 
 
158
/**
 
159
 * Calculates the pair priority as specified in ICE
 
160
 * sect 5.7.2. "Computing Pair Priority and Ordering Pairs" (ID-19).
 
161
 */
 
162
guint64
 
163
nice_candidate_pair_priority (guint32 o_prio, guint32 a_prio)
 
164
{
 
165
  guint32 max = o_prio > a_prio ? o_prio : a_prio;
 
166
  guint32 min = o_prio < a_prio ? o_prio : a_prio;
 
167
 
 
168
  return ((guint64)1 << 32) * min + 2 * max + (o_prio > a_prio ? 1 : 0);
 
169
}
 
170
 
 
171
/**
 
172
 * Copies a candidate
 
173
 */
 
174
NICEAPI_EXPORT NiceCandidate *
 
175
nice_candidate_copy (const NiceCandidate *candidate)
 
176
{
 
177
  NiceCandidate *copy = nice_candidate_new (candidate->type);
 
178
 
 
179
  memcpy (copy, candidate, sizeof(NiceCandidate));
 
180
 
 
181
  copy->username = g_strdup (copy->username);
 
182
  copy->password = g_strdup (copy->password);
 
183
 
 
184
  return copy;
 
185
}