candidate.c 2.06 KB
Newer Older
Dafydd Harries's avatar
Dafydd Harries committed
1 2 3 4 5 6 7 8 9

#include "agent.h"


/* (ICE-13 §4.1.1) Every candidate is a transport address. It also has a type and
 * a base. Three types are defined and gathered by this specification - host
 * candidates, server reflexive candidates, and relayed candidates. */


10 11
NiceCandidate *
nice_candidate_new (NiceCandidateType type)
Dafydd Harries's avatar
Dafydd Harries committed
12
{
13
  NiceCandidate *candidate;
Dafydd Harries's avatar
Dafydd Harries committed
14

15
  candidate = g_slice_new0 (NiceCandidate);
Dafydd Harries's avatar
Dafydd Harries committed
16 17 18 19 20 21
  candidate->type = type;
  return candidate;
}


void
22
nice_candidate_free (NiceCandidate *candidate)
Dafydd Harries's avatar
Dafydd Harries committed
23
{
24 25
  /* better way of checking if socket is allocated? */

26
  if (candidate->sock.addr.addr_ipv4 != 0)
27 28
    nice_udp_socket_close (&(candidate->sock));

29 30 31
  if (candidate->source)
    g_source_destroy (candidate->source);

32
  g_slice_free (NiceCandidate, candidate);
Dafydd Harries's avatar
Dafydd Harries committed
33 34 35 36
}


gfloat
37
nice_candidate_jingle_priority (NiceCandidate *candidate)
Dafydd Harries's avatar
Dafydd Harries committed
38 39 40
{
  switch (candidate->type)
    {
41 42 43 44
    case NICE_CANDIDATE_TYPE_HOST:             return 1.0;
    case NICE_CANDIDATE_TYPE_SERVER_REFLEXIVE: return 0.9;
    case NICE_CANDIDATE_TYPE_PEER_REFLEXIVE:   return 0.9;
    case NICE_CANDIDATE_TYPE_RELAYED:          return 0.5;
Dafydd Harries's avatar
Dafydd Harries committed
45 46 47 48 49 50 51 52 53
    }

  /* appease GCC */
  return 0;
}


/* ICE-13 §4.1.2; returns number between 1 and 0x7effffff */
G_GNUC_CONST
Dafydd Harries's avatar
Dafydd Harries committed
54
static guint32
Dafydd Harries's avatar
Dafydd Harries committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
_candidate_ice_priority (
  // must be ∈ (0, 126) (max 2^7 - 2)
  guint type_preference,
  // must be ∈ (0, 65535) (max 2^16 - 1)
  guint local_preference,
  // must be ∈ (1, 255) (max 2 ^ 8 - 1)
  guint component_id)
{
  return (
      0x1000000 * type_preference +
      0x100 * local_preference +
      (0x100 - component_id));
}


70
G_GNUC_CONST
Dafydd Harries's avatar
Dafydd Harries committed
71
guint32
72
nice_candidate_ice_priority (const NiceCandidate *candidate)
Dafydd Harries's avatar
Dafydd Harries committed
73 74 75 76 77
{
  guint8 type_preference = 0;

  switch (candidate->type)
    {
78 79 80 81
    case NICE_CANDIDATE_TYPE_HOST:             type_preference = 120; break;
    case NICE_CANDIDATE_TYPE_PEER_REFLEXIVE:   type_preference = 110; break;
    case NICE_CANDIDATE_TYPE_SERVER_REFLEXIVE: type_preference = 100; break;
    case NICE_CANDIDATE_TYPE_RELAYED:          type_preference =  60; break;
Dafydd Harries's avatar
Dafydd Harries committed
82 83 84 85 86
    }

  return _candidate_ice_priority (type_preference, 1, candidate->component_id);
}