conncheck.c 154 KB
Newer Older
1 2 3
/*
 * This file is part of the Nice GLib ICE library.
 *
4 5 6
 * (C) 2006-2009 Collabora Ltd.
 *  Contact: Youness Alaoui
 * (C) 2006-2009 Nokia Corporation. All rights reserved.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *  Contact: Kai Vehmanen
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is the Nice GLib ICE library.
 *
 * The Initial Developers of the Original Code are Collabora Ltd and Nokia
 * Corporation. All Rights Reserved.
 *
 * Contributors:
 *   Kai Vehmanen, Nokia
26
 *   Youness Alaoui, Collabora Ltd.
27 28 29 30 31 32 33 34 35 36 37 38 39
 *   Dafydd Harries, Collabora Ltd.
 *
 * Alternatively, the contents of this file may be used under the terms of the
 * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
 * case the provisions of LGPL are applicable instead of those above. If you
 * wish to allow use of your version of this file only under the terms of the
 * LGPL and not to allow others to use your version of this file under the
 * MPL, indicate your decision by deleting the provisions above and replace
 * them with the notice and other provisions required by the LGPL. If you do
 * not delete the provisions above, a recipient may use your version of this
 * file under either the MPL or the LGPL.
 */

40
/*
41 42 43 44 45 46 47 48
 * @file conncheck.c
 * @brief ICE connectivity checks
 */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

49 50 51 52 53
#include <errno.h>
#include <string.h>

#include <glib.h>

54 55
#include "debug.h"

56 57 58 59
#include "agent.h"
#include "agent-priv.h"
#include "conncheck.h"
#include "discovery.h"
Jakub Adam's avatar
Jakub Adam committed
60
#include "stun/stun5389.h"
61
#include "stun/usages/ice.h"
62 63
#include "stun/usages/bind.h"
#include "stun/usages/turn.h"
64

65
static void priv_update_check_list_failed_components (NiceAgent *agent, NiceStream *stream);
66
static void priv_update_check_list_state_for_ready (NiceAgent *agent, NiceStream *stream, NiceComponent *component);
67
static guint priv_prune_pending_checks (NiceAgent *agent, NiceStream *stream, guint component_id);
68
static gboolean priv_schedule_triggered_check (NiceAgent *agent, NiceStream *stream, NiceComponent *component, NiceSocket *local_socket, NiceCandidate *remote_cand);
69
static void priv_mark_pair_nominated (NiceAgent *agent, NiceStream *stream, NiceComponent *component, NiceCandidate *localcand, NiceCandidate *remotecand);
70
static size_t priv_create_username (NiceAgent *agent, NiceStream *stream,
71 72
    guint component_id, NiceCandidate *remote, NiceCandidate *local,
    uint8_t *dest, guint dest_len, gboolean inbound);
73
static size_t priv_get_password (NiceAgent *agent, NiceStream *stream,
74
    NiceCandidate *remote, uint8_t **password);
75
static void conn_check_free_item (gpointer data);
76 77 78
static CandidateCheckPair *priv_conn_check_add_for_candidate_pair_matched (
    NiceAgent *agent, guint stream_id, NiceComponent *component,
    NiceCandidate *local, NiceCandidate *remote, NiceCheckState initial_state);
79 80
static gboolean priv_update_selected_pair (NiceAgent *agent,
    NiceComponent *component, CandidateCheckPair *pair);
81

82
static int priv_timer_expired (GTimeVal *timer, GTimeVal *now)
83 84 85 86 87
{
  return (now->tv_sec == timer->tv_sec) ?
    now->tv_usec >= timer->tv_usec :
    now->tv_sec >= timer->tv_sec;
}
88

89 90 91 92 93 94 95 96 97 98 99
static unsigned int priv_timer_remainder (GTimeVal *timer, GTimeVal *now)
{
  unsigned int delay;
  if (now->tv_sec > timer->tv_sec ||
      (now->tv_sec == timer->tv_sec && now->tv_usec > timer->tv_usec))
    return 0;
  delay = (timer->tv_sec - now->tv_sec) * 1000;
  delay += ((signed long)(timer->tv_usec - now->tv_usec)) / 1000;
  return delay;
}

100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
static gchar
priv_state_to_gchar (NiceCheckState state)
{
  switch (state) {
    case NICE_CHECK_WAITING:
      return 'W';
    case NICE_CHECK_IN_PROGRESS:
      return 'I';
    case NICE_CHECK_SUCCEEDED:
      return 'S';
    case NICE_CHECK_FAILED:
      return 'F';
    case NICE_CHECK_FROZEN:
      return 'Z';
    case NICE_CHECK_DISCOVERED:
      return 'D';
    default:
      g_assert_not_reached ();
  }
}

121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
static const gchar *
priv_state_to_string (NiceCheckState state)
{
  switch (state) {
    case NICE_CHECK_WAITING:
      return "waiting";
    case NICE_CHECK_IN_PROGRESS:
      return "in progress";
    case NICE_CHECK_SUCCEEDED:
      return "succeeded";
    case NICE_CHECK_FAILED:
      return "failed";
    case NICE_CHECK_FROZEN:
      return "frozen";
    case NICE_CHECK_DISCOVERED:
      return "discovered";
    default:
      g_assert_not_reached ();
  }
}

static const gchar *
priv_ice_return_to_string (StunUsageIceReturn ice_return)
{
  switch (ice_return) {
    case STUN_USAGE_ICE_RETURN_SUCCESS:
      return "success";
    case STUN_USAGE_ICE_RETURN_ERROR:
      return "error";
    case STUN_USAGE_ICE_RETURN_INVALID:
      return "invalid";
    case STUN_USAGE_ICE_RETURN_ROLE_CONFLICT:
      return "role conflict";
    case STUN_USAGE_ICE_RETURN_INVALID_REQUEST:
      return "invalid request";
    case STUN_USAGE_ICE_RETURN_INVALID_METHOD:
      return "invalid method";
    case STUN_USAGE_ICE_RETURN_MEMORY_ERROR:
      return "memory error";
    case STUN_USAGE_ICE_RETURN_INVALID_ADDRESS:
      return "invalid address";
    case STUN_USAGE_ICE_RETURN_NO_MAPPED_ADDRESS:
      return "no mapped address";
    default:
      g_assert_not_reached ();
  }
}

169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
static const gchar *
priv_candidate_type_to_string (NiceCandidateType type)
{
  switch (type) {
    case NICE_CANDIDATE_TYPE_HOST:
      return "host";
    case NICE_CANDIDATE_TYPE_SERVER_REFLEXIVE:
      return "srflx";
    case NICE_CANDIDATE_TYPE_PEER_REFLEXIVE:
      return "prflx";
    case NICE_CANDIDATE_TYPE_RELAYED:
      return "relay";
    default:
      g_assert_not_reached ();
  }
}

/*
 * Dump the conncheck lists of the agent
 */
static void
priv_print_conn_check_lists (NiceAgent *agent, const gchar *where, const gchar *detail)
{
  GSList *i, *k;
  guint j;
194
  GTimeVal now;
195 196 197 198

  if (!nice_debug_is_verbose ())
    return;

199 200
  g_get_current_time (&now);

201 202 203 204 205 206 207 208 209 210 211 212
#define PRIORITY_LEN 32

  nice_debug ("Agent %p : *** conncheck list DUMP (called from %s%s)",
      agent, where, detail ? detail : "");
  for (i = agent->streams; i ; i = i->next) {
    NiceStream *stream = i->data;
    for (j = 1; j <= stream->n_components; j++) {
      for (k = stream->conncheck_list; k ; k = k->next) {
        CandidateCheckPair *pair = k->data;
        if (pair->component_id == j) {
          gchar local_addr[INET6_ADDRSTRLEN];
          gchar remote_addr[INET6_ADDRSTRLEN];
213
          StunTimer *timer = &pair->timer;
214 215 216 217 218

          nice_address_to_string (&pair->local->addr, local_addr);
          nice_address_to_string (&pair->remote->addr, remote_addr);

          nice_debug ("Agent %p : *** sc=%d/%d : pair %p : "
219 220
              "f=%s t=%s:%s timer=%d/%d %d/%dms "
              "[%s]:%u > [%s]:%u state=%c%s%s%s",
221 222 223 224
              agent, pair->stream_id, pair->component_id, pair,
              pair->foundation,
              priv_candidate_type_to_string (pair->local->type),
              priv_candidate_type_to_string (pair->remote->type),
225
              timer->retransmissions, timer->max_retransmissions,
226 227
              timer->delay - priv_timer_remainder (&pair->next_tick, &now),
              timer->delay,
228 229 230 231 232 233 234 235 236 237 238 239
              local_addr, nice_address_get_port (&pair->local->addr),
              remote_addr, nice_address_get_port (&pair->remote->addr),
              priv_state_to_gchar (pair->state),
              pair->valid ? "V" : "",
              pair->nominated ? "N" : "",
              g_slist_find (agent->triggered_check_queue, pair) ? "T" : "");
        }
      }
    }
  }
}

240 241 242 243 244 245 246 247 248 249
/* Verify if the pair is in the triggered checks list
 */

static gboolean
priv_is_pair_in_triggered_check_queue (NiceAgent *agent, CandidateCheckPair *pair)
{
  g_assert (pair);
  return (g_slist_find (agent->triggered_check_queue, pair) != NULL);
}

250 251 252 253 254 255 256
/* Add the pair to the triggered checks list, if not already present
 */
static void
priv_add_pair_to_triggered_check_queue (NiceAgent *agent, CandidateCheckPair *pair)
{
  g_assert (pair);

257 258
  pair->state = NICE_CHECK_WAITING;
  nice_debug ("Agent %p : pair %p state WAITING", agent, pair);
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
  if (agent->triggered_check_queue == NULL ||
      g_slist_find (agent->triggered_check_queue, pair) == NULL)
    agent->triggered_check_queue = g_slist_append (agent->triggered_check_queue, pair);
}

/* Remove the pair from the triggered checks list
 */
static void
priv_remove_pair_from_triggered_check_queue (NiceAgent *agent, CandidateCheckPair *pair)
{
  g_assert (pair);
  agent->triggered_check_queue = g_slist_remove (agent->triggered_check_queue, pair);
}

/* Get the pair from the triggered checks list
 */
static CandidateCheckPair *
priv_get_pair_from_triggered_check_queue (NiceAgent *agent)
{
  CandidateCheckPair *pair = NULL;

  if (agent->triggered_check_queue) {
    pair = (CandidateCheckPair *)agent->triggered_check_queue->data;
    priv_remove_pair_from_triggered_check_queue (agent, pair);
  }
  return pair;
}

287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
/*
 * Check if the conncheck list if Active according to
 * ICE spec, 5.7.4 (Computing States)
 *
 * note: the ICE spec in unclear about that, but the check list should
 * be considered active when there is at least a pair in Waiting state
 * OR a pair in In-Progress state.
 */
static gboolean
priv_is_checklist_active (NiceStream *stream)
{
  GSList *i;

  for (i = stream->conncheck_list; i ; i = i->next) {
    CandidateCheckPair *p = i->data;
    if (p->state == NICE_CHECK_WAITING || p->state == NICE_CHECK_IN_PROGRESS)
      return TRUE;
  }
  return FALSE;
}

/*
 * Check if the conncheck list if Frozen according to
 * ICE spec, 5.7.4 (Computing States)
 */
static gboolean
priv_is_checklist_frozen (NiceStream *stream)
{
  GSList *i;

  if (stream->conncheck_list == NULL)
    return FALSE;

  for (i = stream->conncheck_list; i ; i = i->next) {
    CandidateCheckPair *p = i->data;
    if (p->state != NICE_CHECK_FROZEN)
      return FALSE;
  }
  return TRUE;
}

/*
 * Check if all components of the stream have
 * a valid pair (used for ICE spec, 7.1.3.2.3, point 2.)
 */
static gboolean
priv_all_components_have_valid_pair (NiceStream *stream)
{
  guint i;
  GSList *j;

  for (i = 1; i <= stream->n_components; i++) {
    for (j = stream->conncheck_list; j ; j = j->next) {
      CandidateCheckPair *p = j->data;
      if (p->component_id == i && p->valid)
        break;
    }
    if (j == NULL)
      return FALSE;
  }
  return TRUE;
}

/*
 * Check if the foundation in parameter matches the foundation
 * of a valid pair in the conncheck list [of stream] (used for ICE spec,
 * 7.1.3.2.3, point 2.)
 */
static gboolean
priv_foundation_matches_a_valid_pair (const gchar *foundation, NiceStream *stream)
{
  GSList *i;

  for (i = stream->conncheck_list; i ; i = i->next) {
    CandidateCheckPair *p = i->data;
    if (p->valid &&
        strncmp (p->foundation, foundation,
            NICE_CANDIDATE_PAIR_MAX_FOUNDATION) == 0)
      return TRUE;
  }
  return FALSE;
}

370
/*
371 372 373 374 375 376
 * Finds the next connectivity check in WAITING state.
 */
static CandidateCheckPair *priv_conn_check_find_next_waiting (GSList *conn_check_list)
{
  GSList *i;

377 378
  /* note: list is sorted in priority order to first waiting check has
   *       the highest priority */
379 380 381 382 383 384 385 386 387
  for (i = conn_check_list; i ; i = i->next) {
    CandidateCheckPair *p = i->data;
    if (p->state == NICE_CHECK_WAITING)
      return p;
  }

  return NULL;
}

388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
/*
 * Finds the next connectivity check in FROZEN state.
 */
static CandidateCheckPair *
priv_conn_check_find_next_frozen (GSList *conn_check_list)
{
  GSList *i;

  /* note: list is sorted in priority order to first frozen check has
   *       the highest priority */
  for (i = conn_check_list; i ; i = i->next) {
    CandidateCheckPair *p = i->data;
    if (p->state == NICE_CHECK_FROZEN)
      return p;
  }

  return NULL;
}

/*
 * Returns the number of check lists of the agent
 */
static guint
priv_number_of_check_lists (NiceAgent *agent)
{
  guint n = 0;
  GSList *i;

  for (i = agent->streams; i ; i = i->next) {
    NiceStream *stream = i->data;
    if (stream->conncheck_list != NULL)
      n++;
  }
  return n;
}

/*
 * Returns the number of active check lists of the agent
 */
static guint
priv_number_of_active_check_lists (NiceAgent *agent)
{
  guint n = 0;
  GSList *i;

  for (i = agent->streams; i ; i = i->next)
    if (priv_is_checklist_active (i->data))
      n++;
  return n;
}

/*
 * Returns the first stream of the agent having a Frozen
 * connection check list
 */
static NiceStream *
priv_find_first_frozen_check_list (NiceAgent *agent)
{
  GSList *i;

  for (i = agent->streams; i ; i = i->next) {
    NiceStream *stream = i->data;
    if (priv_is_checklist_frozen (stream))
      return stream;
  }
  return NULL;
}

456
/*
457 458 459 460 461 462 463
 * Initiates a new connectivity check for a ICE candidate pair.
 *
 * @return TRUE on success, FALSE on error
 */
static gboolean priv_conn_check_initiate (NiceAgent *agent, CandidateCheckPair *pair)
{
  pair->state = NICE_CHECK_IN_PROGRESS;
464
  nice_debug ("Agent %p : pair %p state IN_PROGRESS", agent, pair);
465 466 467 468
  conn_check_send (agent, pair);
  return TRUE;
}

469
/*
470
 * Unfreezes the next connectivity check in the list. Follows the
471
 * algorithm (2.) defined in 5.7.4 (Computing States) of the ICE spec
472
 * (RFC5245)
473 474 475
 *
 * See also sect 7.1.2.2.3 (Updating Pair States), and
 * priv_conn_check_unfreeze_related().
476 477 478
 * 
 * @return TRUE on success, and FALSE if no frozen candidates were found.
 */
479
static gboolean priv_conn_check_unfreeze_next (NiceAgent *agent, NiceStream *stream)
480
{
481
  GSList *i, *j;
482 483
  GSList *found_list = NULL;
  gboolean result = FALSE;
484

485 486
  priv_print_conn_check_lists (agent, G_STRFUNC, NULL);

487 488 489 490 491
  for (i = stream->conncheck_list; i ; i = i->next) {
    CandidateCheckPair *p1 = i->data;
    CandidateCheckPair *pair = NULL;
    guint lowest_component_id = stream->n_components + 1;
    guint64 highest_priority = 0;
492

493 494 495
    if (g_slist_find_custom (found_list, p1->foundation, (GCompareFunc)strcmp))
      continue;
    found_list = g_slist_prepend (found_list, p1->foundation);
496

497
    for (j = stream->conncheck_list; j ; j = j->next) {
498 499 500 501 502 503 504 505 506 507
      CandidateCheckPair *p2 = i->data;
      if (strncmp (p2->foundation, p1->foundation,
              NICE_CANDIDATE_PAIR_MAX_FOUNDATION) == 0) {
        if (p2->component_id < lowest_component_id ||
            (p2->component_id == lowest_component_id &&
             p2->priority > highest_priority)) {
          pair = p2;
          lowest_component_id = p2->component_id;
          highest_priority = p2->priority;
        }
508 509
      }
    }
510

511 512 513 514 515 516 517
    if (pair) {
      nice_debug ("Agent %p : Pair %p with s/c-id %u/%u (%s) unfrozen.",
          agent, pair, pair->stream_id, pair->component_id, pair->foundation);
      pair->state = NICE_CHECK_WAITING;
      nice_debug ("Agent %p : pair %p state WAITING", agent, pair);
      result = TRUE;
    }
518
  }
519 520
  g_slist_free (found_list);
  return result;
521 522
}

523
/*
524
 * Unfreezes the next next connectivity check in the list after
Youness Alaoui's avatar
Youness Alaoui committed
525
 * check 'success_check' has successfully completed.
526
 *
527
 * See sect 7.1.2.2.3 (Updating Pair States) of ICE spec (ID-19).
528 529 530 531 532 533
 * 
 * @param agent context
 * @param ok_check a connectivity check that has just completed
 *
 * @return TRUE on success, and FALSE if no frozen candidates were found.
 */
534
static void priv_conn_check_unfreeze_related (NiceAgent *agent, NiceStream *stream, CandidateCheckPair *ok_check)
535 536 537 538 539
{
  GSList *i, *j;

  g_assert (ok_check);
  g_assert (ok_check->state == NICE_CHECK_SUCCEEDED);
540 541
  g_assert (stream);
  g_assert (stream->id == ok_check->stream_id);
542

543 544
  priv_print_conn_check_lists (agent, G_STRFUNC, NULL);

545
  /* step: perform the step (1) of 'Updating Pair States' */
546
  for (i = stream->conncheck_list; i ; i = i->next) {
547 548 549 550
    CandidateCheckPair *p = i->data;
   
    if (p->stream_id == ok_check->stream_id) {
      if (p->state == NICE_CHECK_FROZEN &&
551 552
          strncmp (p->foundation, ok_check->foundation,
              NICE_CANDIDATE_PAIR_MAX_FOUNDATION) == 0) {
Youness Alaoui's avatar
Youness Alaoui committed
553
	nice_debug ("Agent %p : Unfreezing check %p (after successful check %p).", agent, p, ok_check);
554
	p->state = NICE_CHECK_WAITING;
555
        nice_debug ("Agent %p : pair %p state WAITING", agent, p);
556 557 558 559 560 561
      }
    }
  }

  /* step: perform the step (2) of 'Updating Pair States' */
  stream = agent_find_stream (agent, ok_check->stream_id);
562
  if (priv_all_components_have_valid_pair (stream)) {
563
    for (i = agent->streams; i ; i = i->next) {
564 565 566
      /* the agent examines the check list for each other
       * media stream in turn
       */
567
      NiceStream *s = i->data;
568 569 570 571 572 573 574 575 576
      if (s->id == ok_check->stream_id)
        continue;
      if (priv_is_checklist_active (s)) {
        /* checklist is Active
         */
        for (j = s->conncheck_list; j ; j = j->next) {
	  CandidateCheckPair *p = j->data;
          if (p->state == NICE_CHECK_FROZEN &&
              priv_foundation_matches_a_valid_pair (p->foundation, stream)) {
Youness Alaoui's avatar
Youness Alaoui committed
577
	    nice_debug ("Agent %p : Unfreezing check %p from stream %u (after successful check %p).", agent, p, s->id, ok_check);
578
	    p->state = NICE_CHECK_WAITING;
579
            nice_debug ("Agent %p : pair %p state WAITING", agent, p);
580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605
          }
        }
      } else if (priv_is_checklist_frozen (s)) {
        /* checklist is Frozen
         */
        gboolean match_found = FALSE;
        /* check if there is one pair in the check list whose
         * foundation matches a pair in the valid list under
         * consideration
         */
        for (j = s->conncheck_list; j ; j = j->next) {
	  CandidateCheckPair *p = j->data;
          if (priv_foundation_matches_a_valid_pair (p->foundation, stream)) {
            match_found = TRUE;
            nice_debug ("Agent %p : Unfreezing check %p from stream %u (after successful check %p).", agent, p, s->id, ok_check);
            p->state = NICE_CHECK_WAITING;
            nice_debug ("Agent %p : pair %p state WAITING", agent, p);
          }
        }

        if (!match_found) {
          /* set the pair with the lowest component ID
           * and highest priority to Waiting
           */
          priv_conn_check_unfreeze_next (agent, s);
        }
606 607 608 609 610
      }
    }
  }    
}

611
static void
612
candidate_check_pair_fail (NiceStream *stream, NiceAgent *agent, CandidateCheckPair *p)
613 614
{
  StunTransactionId id;
615
  NiceComponent *component;
616

617
  component = nice_stream_find_component_by_id (stream, p->component_id);
618 619 620 621

  p->state = NICE_CHECK_FAILED;
  nice_debug ("Agent %p : pair %p state FAILED", agent, p);

622 623 624 625
  if (p->stun_message.buffer != NULL) {
    stun_message_id (&p->stun_message, id);
    stun_agent_forget_transaction (&component->stun_agent, id);
  }
626 627 628 629 630

  p->stun_message.buffer = NULL;
  p->stun_message.buffer_len = 0;
}

631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658
/*
 * Function that resubmits a new connection check, after a previous
 * check in in-progress state got cancelled due to an incoming stun
 * request matching this same pair
 *
 * @return will return TRUE if the pair is scheduled for recheck
 */
static gboolean
priv_conn_recheck_on_timeout (NiceAgent *agent, CandidateCheckPair *p)
{
  if (p->recheck_on_timeout) {
    g_assert (p->state == NICE_CHECK_IN_PROGRESS);
    /* this cancelled pair may have the flag 'mark nominated
     * on response arrival' set, we want to keep it, because
     * this is needed to nominate this pair in aggressive
     * nomination, when the agent is in controlled mode.
     *
     * this cancelled pair may also have the flag 'use candidate
     * on next check' set, that we want to preserve too.
     */
    nice_debug ("Agent %p : pair %p was cancelled, "
        "triggering a new connection check", agent, p);
    priv_add_pair_to_triggered_check_queue (agent, p);
    return TRUE;
  }
  return FALSE;
}

659
/*
660 661 662 663 664 665 666
 * Helper function for connectivity check timer callback that
 * runs through the stream specific part of the state machine. 
 *
 * @param schedule if TRUE, schedule a new check
 *
 * @return will return FALSE when no more pending timers.
 */
667
static gboolean priv_conn_check_tick_stream (NiceStream *stream, NiceAgent *agent)
668 669
{
  gboolean keep_timer_going = FALSE;
670 671
  GSList *i;
  CandidateCheckPair *pair;
672
  unsigned int timeout;
673 674 675
  GTimeVal now;

  g_get_current_time (&now);
676

677
  /* step: process ongoing STUN transactions */
678 679
  for (i = stream->conncheck_list; i ; i = i->next) {
    CandidateCheckPair *p = i->data;
680 681 682 683 684 685 686 687 688
    gchar tmpbuf1[INET6_ADDRSTRLEN], tmpbuf2[INET6_ADDRSTRLEN];
    NiceComponent *component;

    if (!agent_find_component (agent, p->stream_id, p->component_id,
        NULL, &component))
      continue;

    if (p->state != NICE_CHECK_IN_PROGRESS)
      continue;
689

690 691 692 693 694 695 696
    if (p->stun_message.buffer == NULL) {
      nice_debug ("Agent %p : STUN connectivity check was cancelled, marking as done.", agent);
      p->state = NICE_CHECK_FAILED;
      nice_debug ("Agent %p : pair %p state FAILED", agent, p);
      continue;
    }

697
    if (!priv_timer_expired (&p->next_tick, &now))
698
      continue;
699

700 701
    switch (stun_timer_refresh (&p->timer)) {
      case STUN_USAGE_TIMER_RETURN_TIMEOUT:
702
timer_timeout:
703 704 705 706 707 708 709
        /* case: conncheck cancelled due to in-progress incoming
         * check, requeing the pair, ICE spec, sect 7.2.1.4
         * "Triggered Checks", "If the state of that pair is
         * In-Progress..."
         */
        if (priv_conn_recheck_on_timeout (agent, p))
          break;
710

711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736
        /* case: error, abort processing */
        nice_address_to_string (&p->local->addr, tmpbuf1);
        nice_address_to_string (&p->remote->addr, tmpbuf2);
        nice_debug ("Agent %p : Retransmissions failed, giving up on connectivity check %p", agent, p);
        nice_debug ("Agent %p : Failed pair is [%s]:%u --> [%s]:%u", agent,
            tmpbuf1, nice_address_get_port (&p->local->addr),
            tmpbuf2, nice_address_get_port (&p->remote->addr));
        candidate_check_pair_fail (stream, agent, p);
        priv_print_conn_check_lists (agent, G_STRFUNC,
            ", retransmission failed");

        /* perform a check if a transition state from connected to
         * ready can be performed. This may happen here, when the last
         * in-progress pair has expired its retransmission count
         * in priv_conn_check_tick_stream(), which is a condition to
         * make the transition connected to ready.
         */
        priv_update_check_list_state_for_ready (agent, stream, component);
        break;
      case STUN_USAGE_TIMER_RETURN_RETRANSMIT:
        /* case: retransmission stopped, due to the nomination of
         * a pair with a higher priority than this in-progress pair,
         * ICE spec, sect 8.1.2 "Updating States", item 2.2
         */
        if (!p->retransmit_on_timeout)
          goto timer_timeout;
737

738 739 740 741 742 743 744
        /* case: conncheck cancelled due to in-progress incoming
         * check, requeing the pair, ICE spec, sect 7.2.1.4
         * "Triggered Checks", "If the state of that pair is
         * In-Progress..."
         */
        if (priv_conn_recheck_on_timeout (agent, p))
          break;
745

746
        /* case: not ready, so schedule a new timeout */
747 748
        timeout = stun_timer_remainder (&p->timer);

749
        nice_debug ("Agent %p :STUN transaction retransmitted on pair %p "
750 751 752 753
            "(timer=%d/%d %d/%dms).",
            agent, p,
            p->timer.retransmissions, p->timer.max_retransmissions,
            p->timer.delay - timeout, p->timer.delay);
754

755 756 757
        agent_socket_send (p->sockptr, &p->remote->addr,
            stun_message_length (&p->stun_message),
            (gchar *)p->stun_buffer);
758 759


760
        /* note: convert from milli to microseconds for g_time_val_add() */
761
        p->next_tick = now;
762
        g_time_val_add (&p->next_tick, timeout * 1000);
763

764 765 766
        return TRUE;
      case STUN_USAGE_TIMER_RETURN_SUCCESS:
        timeout = stun_timer_remainder (&p->timer);
767

768
        /* note: convert from milli to microseconds for g_time_val_add() */
769
        p->next_tick = now;
770
        g_time_val_add (&p->next_tick, timeout * 1000);
Youness Alaoui's avatar
Youness Alaoui committed
771

772 773 774 775 776
        keep_timer_going = TRUE;
        break;
      default:
        /* Nothing to do. */
        break;
777
    }
778
  }
779 780 781 782 783 784 785

  /* step: perform an ordinary check, ICE spec, 5.8 "Scheduling Checks"
   * note: This code is executed when the triggered checks list is
   * empty, and when no STUN message has been sent (pacing constraint)
   */
  pair = priv_conn_check_find_next_waiting (stream->conncheck_list);
  if (pair) {
786 787 788 789 790 791
    /* remove the pair from the triggered check list if needed. This
     * may happen with the cancelled pair, that's just been added
     * in state waiting to the triggered check list above in the
     * same function.
     */
    priv_remove_pair_from_triggered_check_queue (agent, pair);
792 793
    priv_print_conn_check_lists (agent, G_STRFUNC,
        ", got a pair in Waiting state");
794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812
    priv_conn_check_initiate (agent, pair);
    return TRUE;
  }

  /* note: this is unclear in the ICE spec, but a check list in Frozen
   * state (where all pairs are in Frozen state) is not supposed to
   * change its state by an ordinary check, but only by the success of
   * checks in other check lists, in priv_conn_check_unfreeze_related().
   * The underlying idea is to concentrate the checks on a single check
   * list initially.
   */
  if (priv_is_checklist_frozen (stream))
    return keep_timer_going;

  /* step: ordinary check continued, if there's no pair in the waiting
   * state, pick a pair in the frozen state
   */
  pair = priv_conn_check_find_next_frozen (stream->conncheck_list);
  if (pair) {
813 814
    priv_print_conn_check_lists (agent, G_STRFUNC,
        ", got a pair in Frozen state");
815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835
    pair->state = NICE_CHECK_WAITING;
    nice_debug ("Agent %p : pair %p state WAITING", agent, pair);
    priv_conn_check_initiate (agent, pair);
    return TRUE;
  }
  return keep_timer_going;
}

static gboolean
priv_conn_check_tick_stream_nominate (NiceStream *stream, NiceAgent *agent)
{
  gboolean keep_timer_going = FALSE;
  guint s_inprogress = 0;
  guint s_succeeded = 0;
  guint s_discovered = 0;
  guint s_nominated = 0;
  guint s_waiting_for_nomination = 0;
  guint s_valid = 0;
  guint frozen = 0;
  guint waiting = 0;
  GSList *i, *k;
836

837 838
  for (i = stream->conncheck_list; i ; i = i->next) {
    CandidateCheckPair *p = i->data;
839 840 841 842 843 844 845 846
    if (p->state == NICE_CHECK_FROZEN)
      ++frozen;
    else if (p->state == NICE_CHECK_IN_PROGRESS)
      ++s_inprogress;
    else if (p->state == NICE_CHECK_WAITING)
      ++waiting;
    else if (p->state == NICE_CHECK_SUCCEEDED)
      ++s_succeeded;
847 848
    else if (p->state == NICE_CHECK_DISCOVERED)
      ++s_discovered;
849 850
    if (p->valid)
      ++s_valid;
851

852 853
    if ((p->state == NICE_CHECK_SUCCEEDED || p->state == NICE_CHECK_DISCOVERED)
        && p->nominated)
854
      ++s_nominated;
855 856
    else if ((p->state == NICE_CHECK_SUCCEEDED ||
            p->state == NICE_CHECK_DISCOVERED) && !p->nominated)
857
      ++s_waiting_for_nomination;
858
  }
859

860 861 862 863 864 865
    /* note: keep the timer going as long as there is work to be done */
  if (s_inprogress)
    keep_timer_going = TRUE;
  
  if (s_nominated < stream->n_components &&
      s_waiting_for_nomination) {
866 867
    if (NICE_AGENT_IS_COMPATIBLE_WITH_RFC5245_OR_OC2007R2 (agent)) {
      if (agent->nomination_mode == NICE_NOMINATION_MODE_REGULAR &&
868 869
          agent->controlling_mode) {
#define NICE_MIN_NUMBER_OF_VALID_PAIRS 2
870
        /* ICE 8.1.1.1 Regular nomination
871 872 873 874
         * we choose to nominate the valid pair of a component if
         * - there is no pair left frozen, waiting or in-progress, or
         * - if there are at least two valid pairs, or
         * - if there is at least one valid pair of type HOST-HOST
875 876 877 878 879 880 881 882 883 884 885 886
         *
         * This is the "stopping criterion" described in 8.1.1.1, and is
         * a "local optimization" between accumulating more valid pairs,
         * and limiting the time spent waiting for in-progress connections
         * checks until they finally fail.
         */
        GSList *component_item;

        for (component_item = stream->components; component_item;
             component_item = component_item->next) {
          NiceComponent *component = component_item->data;
          gboolean already_done = FALSE;
887 888 889 890 891 892
          gboolean stopping_criterion = FALSE;
          guint p_valid = 0;
          guint p_frozen = 0;
          guint p_waiting = 0;
          guint p_inprogress = 0;
          guint p_host_host_valid = 0;
893 894 895 896 897 898

          /* verify that the choice of the pair to be nominated
           * has not already been done
           */
          for (k = stream->conncheck_list; k ; k = k->next) {
            CandidateCheckPair *p = k->data;
899 900 901 902 903 904 905 906 907 908 909 910 911 912 913
            if (p->component_id == component->id) {
              if (p->use_candidate_on_next_check)
                already_done = TRUE;
              if (p->state == NICE_CHECK_FROZEN)
                p_frozen++;
              else if (p->state == NICE_CHECK_WAITING)
                p_waiting++;
              else if (p->state == NICE_CHECK_IN_PROGRESS)
                p_inprogress++;
              if (p->valid)
                p_valid++;
              if (p->valid &&
                  p->local->type == NICE_CANDIDATE_TYPE_HOST &&
                  p->remote->type == NICE_CANDIDATE_TYPE_HOST)
                p_host_host_valid++;
914 915 916
            }
          }

917 918 919 920 921 922 923 924 925 926 927 928 929 930
          if (already_done)
            continue;

          stopping_criterion =
              (p_host_host_valid > 0 ||
               p_valid >= NICE_MIN_NUMBER_OF_VALID_PAIRS ||
               (p_waiting == 0 && p_inprogress == 0 && p_frozen == 0));

          if (!stopping_criterion)
            continue;

          /* when the stopping criterion is satisfied, we choose
           * a pair to be nominated in the list of valid pairs,
           * and add it to the triggered checks list
931
           */
932 933 934 935 936 937 938
          for (k = stream->conncheck_list; k ; k = k->next) {
            CandidateCheckPair *p = k->data;
            /* note: highest priority item selected (list always sorted) */
            if (p->component_id == component->id &&
                !p->nominated &&
                !p->use_candidate_on_next_check &&
                p->valid) {
939 940 941 942 943 944 945 946 947 948
              /* According a ICE spec, sect 8.1.1.1.  "Regular
               * Nomination", we enqueue the check that produced this
               * valid pair. When this pair has been discovered, we want
               * to test its parent pair instead.
               */
              if (p->succeeded_pair != NULL) {
                g_assert (p->state == NICE_CHECK_DISCOVERED);
                p = p->succeeded_pair;
              }
              g_assert (p->state == NICE_CHECK_SUCCEEDED);
949 950
              nice_debug ("Agent %p : restarting check %p with "
                  "USE-CANDIDATE attrib (regular nomination)", agent, p);
951
              p->recheck_on_timeout = FALSE;
952 953
              p->use_candidate_on_next_check = TRUE;
              priv_add_pair_to_triggered_check_queue (agent, p);
954
              keep_timer_going = TRUE;
955
              break; /* move to the next component */
956 957 958 959 960
            }
          }
        }
      }
    } else if (agent->controlling_mode) {
961 962 963 964
      GSList *component_item;

      for (component_item = stream->components; component_item;
           component_item = component_item->next) {
965
        NiceComponent *component = component_item->data;
966

967 968 969
	for (k = stream->conncheck_list; k ; k = k->next) {
	  CandidateCheckPair *p = k->data;
	  /* note: highest priority item selected (list always sorted) */
970 971 972
	  if (p->component_id == component->id &&
              (p->state == NICE_CHECK_SUCCEEDED ||
               p->state == NICE_CHECK_DISCOVERED)) {
973
	    nice_debug ("Agent %p : restarting check %p as the nominated pair.", agent, p);
974
	    p->nominated = TRUE;
975
            p->recheck_on_timeout = FALSE;
976
            priv_update_selected_pair (agent, component, p);
977
            priv_add_pair_to_triggered_check_queue (agent, p);
978
            keep_timer_going = TRUE;
979 980 981 982 983 984
	    break; /* move to the next component */
	  }
	}
      }
    }
  }
985 986 987 988 989 990
  if (stream->tick_counter++ % 50 == 0)
    nice_debug ("Agent %p : stream %u: timer tick #%u: %u frozen, %u in-progress, "
        "%u waiting, %u succeeded, %u discovered, %u nominated, "
        "%u waiting-for-nom, %u valid.", agent, stream->id,
        stream->tick_counter, frozen, s_inprogress, waiting, s_succeeded,
        s_discovered, s_nominated, s_waiting_for_nomination, s_valid);
991 992 993 994 995

  return keep_timer_going;

}

996 997 998 999 1000 1001 1002 1003 1004
static void
conn_check_stop (NiceAgent *agent)
{
  if (agent->conncheck_timer_source == NULL)
    return;

  g_source_destroy (agent->conncheck_timer_source);
  g_source_unref (agent->conncheck_timer_source);
  agent->conncheck_timer_source = NULL;
1005
  agent->conncheck_timer_grace_period = 0;
1006 1007
}

Youness Alaoui's avatar
Youness Alaoui committed
1008

1009
/*
1010 1011 1012 1013 1014 1015 1016
 * Timer callback that handles initiating and managing connectivity
 * checks (paced by the Ta timer).
 *
 * This function is designed for the g_timeout_add() interface.
 *
 * @return will return FALSE when no more pending timers.
 */
1017
static gboolean priv_conn_check_tick_unlocked (NiceAgent *agent)
1018 1019 1020
{
  CandidateCheckPair *pair = NULL;
  gboolean keep_timer_going = FALSE;
1021
  GSList *i, *j;
1022

1023 1024 1025 1026
  /* the conncheck really starts when we have built
   * a connection check list for each stream
   */
  if (priv_number_of_check_lists (agent) < g_slist_length (agent->streams))
1027 1028
    return TRUE;

1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
  /* configure the initial state of the check lists of the agent
   * as described in ICE spec, 5.7.4
   *
   * if all pairs in all check lists are in frozen state, then
   * we are in the initial state (5.7.4, point 1.)
   */
  if (priv_number_of_active_check_lists (agent) == 0) {
    /* set some pairs of the first stream in the waiting state
     * ICE spec, 5.7.4, point 2.
     *
     * note: we adapt the ICE spec here, by selecting the first
     * frozen check list, which is not necessarily the check
     * list of the first stream (the first stream may be completed)
     */
    NiceStream *stream = priv_find_first_frozen_check_list (agent);
    if (stream)
      priv_conn_check_unfreeze_next (agent, stream);
1046
  }
1047

1048 1049 1050 1051 1052
  /* step: perform a test from the triggered checks list,
   * ICE spec, 5.8 "Scheduling Checks"
   */
  pair = priv_get_pair_from_triggered_check_queue (agent);

1053
  if (pair) {
1054 1055
    priv_print_conn_check_lists (agent, G_STRFUNC,
        ", got a pair from triggered check list");
1056
    priv_conn_check_initiate (agent, pair);
1057
    return TRUE;
1058 1059
  }

1060 1061
  /* step: process ongoing STUN transactions and
   * perform an ordinary check, ICE spec, 5.8, "Scheduling Checks"
1062 1063 1064
   */
  for (i = agent->streams; i ; i = i->next) {
    NiceStream *stream = i->data;
1065
    if (priv_conn_check_tick_stream (stream, agent))
1066 1067 1068
      keep_timer_going = TRUE;
    if (priv_conn_check_tick_stream_nominate (stream, agent))
      keep_timer_going = TRUE;
1069
  }
1070

1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099
  /* step: if no work left and a conncheck list of a stream is still
   * frozen, set the pairs to waiting, according to ICE SPEC, sect
   * 7.1.3.3.  "Check List and Timer State Updates"
   */
  if (!keep_timer_going) {
    for (i = agent->streams; i ; i = i->next) {
      NiceStream *stream = i->data;
      if (priv_is_checklist_frozen (stream)) {
        nice_debug ("Agent %p : stream %d conncheck list is still "
            "frozen, while other lists are completed. Unfreeze it.",
            agent, stream->id);
        keep_timer_going = priv_conn_check_unfreeze_next (agent, stream);
      }
    }
  }

  /* note: we provide a grace period before declaring a component as
   * failed. Components marked connected, and then ready follow another
   * code path, and are not concerned by this grace period.
   */
  if (!keep_timer_going && agent->conncheck_timer_grace_period == 0)
    nice_debug ("Agent %p : waiting %d msecs before checking "
        "for failed components.", agent, NICE_AGENT_MAX_TIMER_GRACE_PERIOD);

  if (keep_timer_going)
    agent->conncheck_timer_grace_period = 0;
  else
    agent->conncheck_timer_grace_period += agent->timer_ta;

1100
  /* step: stop timer if no work left */
1101 1102 1103
  if (!keep_timer_going &&
      agent->conncheck_timer_grace_period >= NICE_AGENT_MAX_TIMER_GRACE_PERIOD) {
    nice_debug ("Agent %p : checking for failed components now.", agent);
1104
    for (i = agent->streams; i; i = i->next) {
1105
      NiceStream *stream = i->data;
1106
      priv_update_check_list_failed_components (agent, stream);
1107
      for (j = stream->components; j; j = j->next) {
1108
        NiceComponent *component = j->data;
1109 1110
        priv_update_check_list_state_for_ready (agent, stream, component);
      }
1111
    }
1112

1113
    nice_debug ("Agent %p : %s: stopping conncheck timer", agent, G_STRFUNC);
1114 1115 1116
    priv_print_conn_check_lists (agent, G_STRFUNC,
        ", conncheck timer stopped");

1117 1118 1119
    /* Stopping the timer so destroy the source.. this will allow
       the timer to be reset if we get a set_remote_candidates after this
       point */
1120
    conn_check_stop (agent);
1121

1122
    /* XXX: what to signal, is all processing now really done? */
1123
    nice_debug ("Agent %p : changing conncheck state to COMPLETED.", agent);
1124
    return FALSE;
1125 1126
  }

1127
  return TRUE;
1128 1129
}

Youness Alaoui's avatar
Youness Alaoui committed
1130 1131 1132
static gboolean priv_conn_check_tick (gpointer pointer)
{
  gboolean ret;
1133
  NiceAgent *agent = pointer;
Youness Alaoui's avatar
Youness Alaoui committed
1134

1135 1136 1137 1138 1139 1140 1141
  agent_lock();
  if (g_source_is_destroyed (g_main_current_source ())) {
    nice_debug ("Source was destroyed. "
        "Avoided race condition in priv_conn_check_tick");
    agent_unlock ();
    return FALSE;
  }
1142 1143 1144

  ret = priv_conn_check_tick_unlocked (agent);
  agent_unlock_and_emit (agent);
Youness Alaoui's avatar
Youness Alaoui committed
1145 1146 1147 1148

  return ret;
}

1149 1150 1151 1152
static gboolean priv_conn_keepalive_retransmissions_tick (gpointer pointer)
{
  CandidatePair *pair = (CandidatePair *) pointer;

1153
  agent_lock();
1154

1155 1156 1157 1158
  /* A race condition might happen where the mutex above waits for the lock
   * and in the meantime another thread destroys the source.
   * In that case, we don't need to run our retransmission tick since it should
   * have been cancelled */
1159 1160 1161 1162
  if (g_source_is_destroyed (g_main_current_source ())) {
    nice_debug ("Source was destroyed. "
        "Avoided race condition in priv_conn_keepalive_retransmissions_tick");
    agent_unlock ();
1163 1164 1165
    return FALSE;
  }

1166 1167 1168 1169 1170 1171
  g_source_destroy (pair->keepalive.tick_source);
  g_source_unref (pair->keepalive.tick_source);
  pair->keepalive.tick_source = NULL;

  switch (stun_timer_refresh (&pair->keepalive.timer)) {
    case STUN_USAGE_TIMER_RETURN_TIMEOUT:
1172 1173 1174
      {
        /* Time out */
        StunTransactionId id;
1175
        NiceComponent *component;
1176 1177 1178 1179 1180 1181 1182 1183 1184

        if (!agent_find_component (pair->keepalive.agent,
                pair->keepalive.stream_id, pair->keepalive.component_id,
                NULL, &component)) {
          nice_debug ("Could not find stream or component in"
              " priv_conn_keepalive_retransmissions_tick");
          agent_unlock ();
          return FALSE;
        }
1185 1186

        stun_message_id (&pair->keepalive.stun_message, id);
1187
        stun_agent_forget_transaction (&component->stun_agent, id);
1188
        pair->keepalive.stun_message.buffer = NULL;
1189

1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200
        if (pair->keepalive.agent->media_after_tick) {
          nice_debug ("Agent %p : Keepalive conncheck timed out!! "
              "but media was received. Suspecting keepalive lost because of "
              "network bottleneck", pair->keepalive.agent);
        } else {
          nice_debug ("Agent %p : Keepalive conncheck timed out!! "
              "peer probably lost connection", pair->keepalive.agent);
          agent_signal_component_state_change (pair->keepalive.agent,
              pair->keepalive.stream_id, pair->keepalive.component_id,
              NICE_COMPONENT_STATE_FAILED);
        }
1201 1202
        break;
      }
1203 1204
    case STUN_USAGE_TIMER_RETURN_RETRANSMIT:
      /* Retransmit */
1205
      agent_socket_send (pair->local->sockptr, &pair->remote->addr,
1206 1207 1208
          stun_message_length (&pair->keepalive.stun_message),
          (gchar *)pair->keepalive.stun_buffer);

1209 1210
      nice_debug ("Agent %p : Retransmitting keepalive conncheck",
          pair->keepalive.agent);
1211 1212 1213 1214
      agent_timeout_add_with_context (pair->keepalive.agent,
          &pair->keepalive.tick_source,
          "Pair keepalive", stun_timer_remainder (&pair->keepalive.timer),
          priv_conn_keepalive_retransmissions_tick, pair);
1215 1216
      break;
    case STUN_USAGE_TIMER_RETURN_SUCCESS:
1217 1218 1219 1220
      agent_timeout_add_with_context (pair->keepalive.agent,
          &pair->keepalive.tick_source,
          "Pair keepalive", stun_timer_remainder (&pair->keepalive.timer),
          priv_conn_keepalive_retransmissions_tick, pair);
1221
      break;
1222
    default:
1223
      g_assert_not_reached();
1224
      break;
1225 1226 1227
  }


1228
  agent_unlock_and_emit (pair->keepalive.agent);
1229 1230 1231
  return FALSE;
}

1232 1233 1234 1235 1236 1237 1238 1239 1240
static guint32 peer_reflexive_candidate_priority (NiceAgent *agent,
    NiceCandidate *local_candidate)
{
  NiceCandidate *candidate_priority =
      nice_candidate_new (NICE_CANDIDATE_TYPE_PEER_REFLEXIVE);
  guint32 priority;

  candidate_priority->transport = local_candidate->transport;
  candidate_priority->component_id = local_candidate->component_id;
1241
  candidate_priority->base_addr = local_candidate->addr;
1242 1243 1244 1245 1246 1247