agent.c 75 KB
Newer Older
1 2 3
/*
 * This file is part of the Nice GLib ICE library.
 *
4 5 6
 * (C) 2006-2010 Collabora Ltd.
 *  Contact: Youness Alaoui
 * (C) 2006-2010 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:
 *   Dafydd Harries, Collabora Ltd.
26
 *   Youness Alaoui, Collabora Ltd.
27
 *   Kai Vehmanen, Nokia
28 29 30 31 32 33 34 35 36 37 38
 *
 * 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.
 */
Dafydd Harries's avatar
Dafydd Harries committed
39

40 41 42

#ifdef HAVE_CONFIG_H
# include <config.h>
43 44
#else
#define NICEAPI_EXPORT
45 46
#endif

47 48
#include <glib.h>

Dafydd Harries's avatar
Dafydd Harries committed
49
#include <string.h>
50
#include <errno.h>
Dafydd Harries's avatar
Dafydd Harries committed
51

52 53 54
#ifdef G_OS_WIN32
#include <winsock2.h>
#else
55 56 57
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
58
#endif
Dafydd Harries's avatar
Dafydd Harries committed
59

60 61
#include "debug.h"

62
#include "socket.h"
63
#include "stun/usages/turn.h"
64
#include "candidate.h"
65
#include "component.h"
66 67
#include "conncheck.h"
#include "discovery.h"
68
#include "agent.h"
69
#include "agent-priv.h"
70
#include "agent-signals-marshal.h"
Dafydd Harries's avatar
Dafydd Harries committed
71

72
#include "stream.h"
73
#include "interfaces.h"
Dafydd Harries's avatar
Dafydd Harries committed
74

75 76
#include "pseudotcp.h"

77 78 79 80
/* This is the max size of a UDP packet
 * will it work tcp relaying??
 */
#define MAX_BUFFER_SIZE 65536
81
#define DEFAULT_STUN_PORT  3478
82
#define DEFAULT_UPNP_TIMEOUT 200
83

84 85
#define MAX_TCP_MTU 1400 /* Use 1400 because of VPNs and we assume IEE 802.3 */

Dafydd Harries's avatar
Dafydd Harries committed
86 87 88 89
G_DEFINE_TYPE (NiceAgent, nice_agent, G_TYPE_OBJECT);

enum
{
90
  PROP_COMPATIBILITY = 1,
91
  PROP_MAIN_CONTEXT,
92
  PROP_STUN_SERVER,
93
  PROP_STUN_SERVER_PORT,
94
  PROP_CONTROLLING_MODE,
95
  PROP_FULL_MODE,
96
  PROP_STUN_PACING_TIMER,
97 98 99 100 101
  PROP_MAX_CONNECTIVITY_CHECKS,
  PROP_PROXY_TYPE,
  PROP_PROXY_IP,
  PROP_PROXY_PORT,
  PROP_PROXY_USERNAME,
Youness Alaoui's avatar
Youness Alaoui committed
102 103
  PROP_PROXY_PASSWORD,
  PROP_UPNP,
104 105
  PROP_UPNP_TIMEOUT,
  PROP_RELIABLE
Dafydd Harries's avatar
Dafydd Harries committed
106 107 108
};


109 110 111
enum
{
  SIGNAL_COMPONENT_STATE_CHANGED,
112
  SIGNAL_CANDIDATE_GATHERING_DONE,
113 114
  SIGNAL_NEW_SELECTED_PAIR,
  SIGNAL_NEW_CANDIDATE,
115
  SIGNAL_NEW_REMOTE_CANDIDATE,
116
  SIGNAL_INITIAL_BINDING_REQUEST_RECEIVED,
117
  SIGNAL_RELIABLE_TRANSPORT_WRITABLE,
118 119 120 121 122
  N_SIGNALS,
};

static guint signals[N_SIGNALS];

123 124
static GStaticRecMutex agent_mutex = G_STATIC_REC_MUTEX_INIT;    /* Mutex used for thread-safe lib */

125 126
static gboolean priv_attach_stream_component (NiceAgent *agent,
    Stream *stream,
127
    Component *component);
Youness Alaoui's avatar
Youness Alaoui committed
128
static void priv_detach_stream_component (Stream *stream, Component *component);
129

Youness Alaoui's avatar
Youness Alaoui committed
130 131
static void priv_free_upnp (NiceAgent *agent);

132 133 134 135 136 137 138 139 140 141 142 143 144

void agent_lock (void)
{
  g_static_rec_mutex_lock (&agent_mutex);
}

void agent_unlock (void)
{
  g_static_rec_mutex_unlock (&agent_mutex);
}



145 146 147
StunUsageIceCompatibility
agent_to_ice_compatibility (NiceAgent *agent)
{
148
  return agent->compatibility == NICE_COMPATIBILITY_GOOGLE ?
149 150
      STUN_USAGE_ICE_COMPATIBILITY_GOOGLE :
      agent->compatibility == NICE_COMPATIBILITY_MSN ?
151
      STUN_USAGE_ICE_COMPATIBILITY_MSN :
152
      STUN_USAGE_ICE_COMPATIBILITY_RFC5245;
153 154 155 156 157 158
}


StunUsageTurnCompatibility
agent_to_turn_compatibility (NiceAgent *agent)
{
159
  return agent->compatibility == NICE_COMPATIBILITY_GOOGLE ?
160 161
      STUN_USAGE_TURN_COMPATIBILITY_GOOGLE :
      agent->compatibility == NICE_COMPATIBILITY_MSN ?
162 163
      STUN_USAGE_TURN_COMPATIBILITY_MSN :
      agent->compatibility == NICE_COMPATIBILITY_WLM2009 ?
164 165 166
      STUN_USAGE_TURN_COMPATIBILITY_MSN : STUN_USAGE_TURN_COMPATIBILITY_DRAFT9;
}

167 168 169
NiceTurnSocketCompatibility
agent_to_turn_socket_compatibility (NiceAgent *agent)
{
170
  return agent->compatibility == NICE_COMPATIBILITY_GOOGLE ?
171 172 173
      NICE_TURN_SOCKET_COMPATIBILITY_GOOGLE :
      agent->compatibility == NICE_COMPATIBILITY_MSN ?
      NICE_TURN_SOCKET_COMPATIBILITY_MSN :
174 175
      agent->compatibility == NICE_COMPATIBILITY_WLM2009 ?
      NICE_TURN_SOCKET_COMPATIBILITY_MSN :
176 177 178
      NICE_TURN_SOCKET_COMPATIBILITY_DRAFT9;
}

179
Stream *agent_find_stream (NiceAgent *agent, guint stream_id)
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
{
  GSList *i;

  for (i = agent->streams; i; i = i->next)
    {
      Stream *s = i->data;

      if (s->id == stream_id)
        return s;
    }

  return NULL;
}


195 196
gboolean
agent_find_component (
197 198 199 200 201 202 203
  NiceAgent *agent,
  guint stream_id,
  guint component_id,
  Stream **stream,
  Component **component)
{
  Stream *s;
204
  Component *c;
205

206
  s = agent_find_stream (agent, stream_id);
207

208
  if (s == NULL)
209 210
    return FALSE;

211 212 213 214 215
  c = stream_find_component_by_id (s, component_id);

  if (c == NULL)
    return FALSE;

216 217 218 219
  if (stream)
    *stream = s;

  if (component)
220
    *component = c;
221 222 223 224 225

  return TRUE;
}


Dafydd Harries's avatar
Dafydd Harries committed
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
static void
nice_agent_dispose (GObject *object);

static void
nice_agent_get_property (
  GObject *object,
  guint property_id,
  GValue *value,
  GParamSpec *pspec);

static void
nice_agent_set_property (
  GObject *object,
  guint property_id,
  const GValue *value,
  GParamSpec *pspec);


static void
nice_agent_class_init (NiceAgentClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);

  gobject_class->get_property = nice_agent_get_property;
  gobject_class->set_property = nice_agent_set_property;
  gobject_class->dispose = nice_agent_dispose;

253
  /* install properties */
254 255 256 257 258 259
  /**
   * NiceAgent:main-context:
   *
   * A GLib main context is needed for all timeouts used by libnice.
   * This is a property being set by the nice_agent_new() call.
   */
260 261 262 263 264 265 266
  g_object_class_install_property (gobject_class, PROP_MAIN_CONTEXT,
      g_param_spec_pointer (
         "main-context",
         "The GMainContext to use for timeouts",
         "The GMainContext to use for timeouts",
         G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));

267 268 269 270 271 272 273
  /**
   * NiceAgent:compatibility:
   *
   * The Nice agent can work in various compatibility modes depending on
   * what the application/peer needs.
   * <para> See also: #NiceCompatibility</para>
   */
274 275 276 277 278
  g_object_class_install_property (gobject_class, PROP_COMPATIBILITY,
      g_param_spec_uint (
         "compatibility",
         "ICE specification compatibility",
         "The compatibility mode for the agent",
279 280
         NICE_COMPATIBILITY_RFC5245, NICE_COMPATIBILITY_LAST,
         NICE_COMPATIBILITY_RFC5245,
281 282
         G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));

Dafydd Harries's avatar
Dafydd Harries committed
283
  g_object_class_install_property (gobject_class, PROP_STUN_SERVER,
284
      g_param_spec_string (
Dafydd Harries's avatar
Dafydd Harries committed
285 286 287
        "stun-server",
        "STUN server",
        "The STUN server used to obtain server-reflexive candidates",
288
        NULL,
Dafydd Harries's avatar
Dafydd Harries committed
289 290
        G_PARAM_READWRITE));

291 292 293 294 295
  g_object_class_install_property (gobject_class, PROP_STUN_SERVER_PORT,
      g_param_spec_uint (
        "stun-server-port",
        "STUN server port",
        "The STUN server used to obtain server-reflexive candidates",
Youness Alaoui's avatar
Youness Alaoui committed
296
        1, 65536,
297
	1, /* not a construct property, ignored */
298 299
        G_PARAM_READWRITE));

300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
  g_object_class_install_property (gobject_class, PROP_CONTROLLING_MODE,
      g_param_spec_boolean (
        "controlling-mode",
        "ICE controlling mode",
        "Whether the agent is in controlling mode",
	FALSE, /* not a construct property, ignored */
        G_PARAM_READWRITE));

   g_object_class_install_property (gobject_class, PROP_FULL_MODE,
      g_param_spec_boolean (
        "full-mode",
        "ICE full mode",
        "Whether agent runs in ICE full mode",
	TRUE, /* use full mode by default */
        G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));

316 317 318 319
  g_object_class_install_property (gobject_class, PROP_STUN_PACING_TIMER,
      g_param_spec_uint (
        "stun-pacing-timer",
        "STUN pacing timer",
Youness Alaoui's avatar
Youness Alaoui committed
320 321
        "Timer 'Ta' (msecs) used in the IETF ICE specification for pacing "
        "candidate gathering and sending of connectivity checks",
Youness Alaoui's avatar
Youness Alaoui committed
322
        1, 0xffffffff,
323 324 325
	NICE_AGENT_TIMER_TA_DEFAULT,
        G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));

326
  /* note: according to spec recommendation in sect 5.7.3 (ID-19) */
327 328 329 330 331
  g_object_class_install_property (gobject_class, PROP_MAX_CONNECTIVITY_CHECKS,
      g_param_spec_uint (
        "max-connectivity-checks",
        "Maximum number of connectivity checks",
        "Upper limit for the total number of connectivity checks performed",
Youness Alaoui's avatar
Youness Alaoui committed
332
        0, 0xffffffff,
333 334 335
	0, /* default set in init */
        G_PARAM_READWRITE));

336 337 338 339 340 341 342
  /**
   * NiceAgent:proxy-ip:
   *
   * The proxy server IP used to bypass a proxy firewall
   *
   * Since: 0.0.4
   */
343 344 345 346
  g_object_class_install_property (gobject_class, PROP_PROXY_IP,
      g_param_spec_string (
        "proxy-ip",
        "Proxy server IP",
347
        "The proxy server IP used to bypass a proxy firewall",
348 349 350
        NULL,
        G_PARAM_READWRITE));

351 352 353 354 355 356 357
  /**
   * NiceAgent:proxy-port:
   *
   * The proxy server port used to bypass a proxy firewall
   *
   * Since: 0.0.4
   */
358 359 360 361
  g_object_class_install_property (gobject_class, PROP_PROXY_PORT,
      g_param_spec_uint (
        "proxy-port",
        "Proxy server port",
362
        "The Proxy server port used to bypass a proxy firewall",
363 364 365 366
        1, 65536,
	1,
        G_PARAM_READWRITE));

367 368 369 370 371 372 373
  /**
   * NiceAgent:proxy-type:
   *
   * The type of proxy set in the proxy-ip property
   *
   * Since: 0.0.4
   */
374 375 376 377 378 379 380 381 382
  g_object_class_install_property (gobject_class, PROP_PROXY_TYPE,
      g_param_spec_uint (
         "proxy-type",
         "Type of proxy to use",
         "The type of proxy set in the proxy-ip property",
         NICE_PROXY_TYPE_NONE, NICE_PROXY_TYPE_LAST,
         NICE_PROXY_TYPE_NONE,
         G_PARAM_READWRITE));

383 384 385 386 387 388 389
  /**
   * NiceAgent:proxy-username:
   *
   * The username used to authenticate with the proxy
   *
   * Since: 0.0.4
   */
390 391 392 393 394 395 396 397
  g_object_class_install_property (gobject_class, PROP_PROXY_USERNAME,
      g_param_spec_string (
        "proxy-username",
        "Proxy server username",
        "The username used to authenticate with the proxy",
        NULL,
        G_PARAM_READWRITE));

398 399 400 401 402 403 404
  /**
   * NiceAgent:proxy-password:
   *
   * The password used to authenticate with the proxy
   *
   * Since: 0.0.4
   */
405 406 407 408 409 410 411 412
  g_object_class_install_property (gobject_class, PROP_PROXY_PASSWORD,
      g_param_spec_string (
        "proxy-password",
        "Proxy server password",
        "The password used to authenticate with the proxy",
        NULL,
        G_PARAM_READWRITE));

413 414 415 416 417 418 419 420
  /**
   * NiceAgent:upnp:
   *
   * Whether the agent should use UPnP to open a port in the router and
   * get the external IP
   *
   * Since: 0.0.7
   */
Youness Alaoui's avatar
Youness Alaoui committed
421 422 423
   g_object_class_install_property (gobject_class, PROP_UPNP,
      g_param_spec_boolean (
        "upnp",
424
#ifdef HAVE_GUPNP
Youness Alaoui's avatar
Youness Alaoui committed
425 426 427
        "Use UPnP",
        "Whether the agent should use UPnP to open a port in the router and "
        "get the external IP",
428 429 430 431
#else
        "Use UPnP (disabled in build)",
        "Does nothing because libnice was not built with UPnP support",
#endif
Youness Alaoui's avatar
Youness Alaoui committed
432 433 434
	TRUE, /* enable UPnP by default */
        G_PARAM_READWRITE| G_PARAM_CONSTRUCT));

435 436 437 438
  /**
   * NiceAgent:upnp-timeout:
   *
   * The maximum amount of time to wait for UPnP discovery to finish before
439
   * signaling the #NiceAgent::candidate-gathering-done signal
440 441 442
   *
   * Since: 0.0.7
   */
Youness Alaoui's avatar
Youness Alaoui committed
443 444 445
  g_object_class_install_property (gobject_class, PROP_UPNP_TIMEOUT,
      g_param_spec_uint (
        "upnp-timeout",
446
#ifdef HAVE_GUPNP
Youness Alaoui's avatar
Youness Alaoui committed
447 448 449
        "Timeout for UPnP discovery",
        "The maximum amount of time to wait for UPnP discovery to finish before "
        "signaling the candidate-gathering-done signal",
450 451 452 453
#else
        "Timeout for UPnP discovery (disabled in build)",
        "Does nothing because libnice was not built with UPnP support",
#endif
Youness Alaoui's avatar
Youness Alaoui committed
454
        100, 60000,
455
	DEFAULT_UPNP_TIMEOUT,
Youness Alaoui's avatar
Youness Alaoui committed
456 457
        G_PARAM_READWRITE | G_PARAM_CONSTRUCT));

Youness Alaoui's avatar
Youness Alaoui committed
458 459 460 461 462 463 464 465
  /**
   * NiceAgent:reliable:
   *
   * Whether the agent should use PseudoTcp to ensure a reliable transport
   * of messages
   *
   * Since: 0.0.11
   */
466 467 468 469
   g_object_class_install_property (gobject_class, PROP_RELIABLE,
      g_param_spec_boolean (
        "reliable",
        "reliable mode",
Youness Alaoui's avatar
Youness Alaoui committed
470
        "Whether the agent should use PseudoTcp to ensure a reliable transport"
471 472 473 474
        "of messages",
	FALSE,
        G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));

475 476
  /* install signals */

477 478
  /**
   * NiceAgent::component-state-changed
Youness Alaoui's avatar
Youness Alaoui committed
479
   * @agent: The #NiceAgent object
480 481 482 483 484 485
   * @stream_id: The ID of the stream
   * @component_id: The ID of the component
   * @state: The #NiceComponentState of the component
   *
   * This signal is fired whenever a component's state changes
   */
486 487 488 489
  signals[SIGNAL_COMPONENT_STATE_CHANGED] =
      g_signal_new (
          "component-state-changed",
          G_OBJECT_CLASS_TYPE (klass),
490
          G_SIGNAL_RUN_LAST,
491 492 493 494 495 496 497 498
          0,
          NULL,
          NULL,
          agent_marshal_VOID__UINT_UINT_UINT,
          G_TYPE_NONE,
          3,
          G_TYPE_UINT, G_TYPE_UINT, G_TYPE_UINT,
          G_TYPE_INVALID);
499

500 501
  /**
   * NiceAgent::candidate-gathering-done:
Youness Alaoui's avatar
Youness Alaoui committed
502
   * @agent: The #NiceAgent object
503 504 505 506 507
   * @stream_id: The ID of the stream
   *
   * This signal is fired whenever a stream has finished gathering its
   * candidates after a call to nice_agent_gather_candidates()
   */
508 509 510 511
  signals[SIGNAL_CANDIDATE_GATHERING_DONE] =
      g_signal_new (
          "candidate-gathering-done",
          G_OBJECT_CLASS_TYPE (klass),
512
          G_SIGNAL_RUN_LAST,
513 514 515
          0,
          NULL,
          NULL,
516
          agent_marshal_VOID__UINT,
517
          G_TYPE_NONE,
518 519
          1,
          G_TYPE_UINT, G_TYPE_INVALID);
520

521 522
  /**
   * NiceAgent::new-selected-pair
Youness Alaoui's avatar
Youness Alaoui committed
523
   * @agent: The #NiceAgent object
524 525 526 527 528 529 530 531
   * @stream_id: The ID of the stream
   * @component_id: The ID of the component
   * @lfoundation: The local foundation of the selected candidate pair
   * @rfoundation: The remote foundation of the selected candidate pair
   *
   * This signal is fired once a candidate pair is selected for data transfer for
   * a stream's component
   */
532 533 534 535
  signals[SIGNAL_NEW_SELECTED_PAIR] =
      g_signal_new (
          "new-selected-pair",
          G_OBJECT_CLASS_TYPE (klass),
536
          G_SIGNAL_RUN_LAST,
537 538 539 540 541 542 543 544 545
          0,
          NULL,
          NULL,
          agent_marshal_VOID__UINT_UINT_STRING_STRING,
          G_TYPE_NONE,
          4,
          G_TYPE_UINT, G_TYPE_UINT, G_TYPE_STRING, G_TYPE_STRING,
          G_TYPE_INVALID);

546 547
  /**
   * NiceAgent::new-candidate
Youness Alaoui's avatar
Youness Alaoui committed
548
   * @agent: The #NiceAgent object
549 550 551 552 553
   * @stream_id: The ID of the stream
   * @component_id: The ID of the component
   * @foundation: The foundation of the new candidate
   *
   * This signal is fired when the agent discovers a new candidate
554
   * <para> See also: #NiceAgent::candidate-gathering-done </para>
555
   */
556 557 558 559
  signals[SIGNAL_NEW_CANDIDATE] =
      g_signal_new (
          "new-candidate",
          G_OBJECT_CLASS_TYPE (klass),
560
          G_SIGNAL_RUN_LAST,
561 562 563 564 565 566 567 568 569
          0,
          NULL,
          NULL,
          agent_marshal_VOID__UINT_UINT_STRING,
          G_TYPE_NONE,
          3,
          G_TYPE_UINT, G_TYPE_UINT, G_TYPE_STRING,
          G_TYPE_INVALID);

570 571
  /**
   * NiceAgent::new-remote-candidate
Youness Alaoui's avatar
Youness Alaoui committed
572
   * @agent: The #NiceAgent object
573 574 575 576 577 578 579
   * @stream_id: The ID of the stream
   * @component_id: The ID of the component
   * @foundation: The foundation of the new candidate
   *
   * This signal is fired when the agent discovers a new remote candidate.
   * This can happen with peer reflexive candidates.
   */
580 581 582 583
  signals[SIGNAL_NEW_REMOTE_CANDIDATE] =
      g_signal_new (
          "new-remote-candidate",
          G_OBJECT_CLASS_TYPE (klass),
584
          G_SIGNAL_RUN_LAST,
585 586 587 588 589 590 591 592 593
          0,
          NULL,
          NULL,
          agent_marshal_VOID__UINT_UINT_STRING,
          G_TYPE_NONE,
          3,
          G_TYPE_UINT, G_TYPE_UINT, G_TYPE_STRING,
          G_TYPE_INVALID);

594 595
  /**
   * NiceAgent::initial-binding-request-received
Youness Alaoui's avatar
Youness Alaoui committed
596
   * @agent: The #NiceAgent object
597 598 599 600 601
   * @stream_id: The ID of the stream
   *
   * This signal is fired when we received our first binding request from
   * the peer.
   */
602 603 604 605
  signals[SIGNAL_INITIAL_BINDING_REQUEST_RECEIVED] =
      g_signal_new (
          "initial-binding-request-received",
          G_OBJECT_CLASS_TYPE (klass),
606
          G_SIGNAL_RUN_LAST,
607 608 609 610 611 612 613 614 615
          0,
          NULL,
          NULL,
          agent_marshal_VOID__UINT,
          G_TYPE_NONE,
          1,
          G_TYPE_UINT,
          G_TYPE_INVALID);

616 617
  /**
   * NiceAgent::reliable-transport-writable
Youness Alaoui's avatar
Youness Alaoui committed
618
   * @agent: The #NiceAgent object
619 620 621 622 623 624 625 626
   * @stream_id: The ID of the stream
   * @component_id: The ID of the component
   *
   * This signal is fired on the reliable #NiceAgent when the underlying reliable
   * transport becomes writable.
   * This signal is only emitted when the nice_agent_send() function returns less
   * bytes than requested to send (or -1) and once when the connection
   * is established.
627 628
   *
   * Since: 0.0.11
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644
   */
  signals[SIGNAL_RELIABLE_TRANSPORT_WRITABLE] =
      g_signal_new (
          "reliable-transport-writable",
          G_OBJECT_CLASS_TYPE (klass),
          G_SIGNAL_RUN_LAST,
          0,
          NULL,
          NULL,
          agent_marshal_VOID__UINT_UINT,
          G_TYPE_NONE,
          2,
          G_TYPE_UINT, G_TYPE_UINT,
          G_TYPE_INVALID);


645 646
  /* Init debug options depending on env variables */
  nice_debug_init ();
Dafydd Harries's avatar
Dafydd Harries committed
647 648
}

649 650 651 652
static void priv_generate_tie_breaker (NiceAgent *agent) 
{
  nice_rng_generate_bytes (agent->rng, 8, (gchar*)&agent->tie_breaker);
}
Dafydd Harries's avatar
Dafydd Harries committed
653 654 655 656 657 658

static void
nice_agent_init (NiceAgent *agent)
{
  agent->next_candidate_id = 1;
  agent->next_stream_id = 1;
659

660
  /* set defaults; not construct params, so set here */
661
  agent->stun_server_port = DEFAULT_STUN_PORT;
662
  agent->controlling_mode = TRUE;
663
  agent->max_conn_checks = NICE_AGENT_MAX_CONNECTIVITY_CHECKS_DEFAULT;
664 665

  agent->discovery_list = NULL;
666
  agent->discovery_unsched_items = 0;
667 668 669
  agent->discovery_timer_source = NULL;
  agent->conncheck_timer_source = NULL;
  agent->keepalive_timer_source = NULL;
670
  agent->refresh_list = NULL;
671
  agent->media_after_tick = FALSE;
672
  agent->software_attribute = NULL;
673

674
  agent->compatibility = NICE_COMPATIBILITY_RFC5245;
675
  agent->reliable = FALSE;
676

677
  stun_agent_init (&agent->stun_agent, STUN_ALL_KNOWN_ATTRIBUTES,
678
      STUN_COMPATIBILITY_RFC5389,
679 680 681
      STUN_AGENT_USAGE_SHORT_TERM_CREDENTIALS |
      STUN_AGENT_USAGE_USE_FINGERPRINT);

Dafydd Harries's avatar
Dafydd Harries committed
682
  agent->rng = nice_rng_new ();
683
  priv_generate_tie_breaker (agent);
Dafydd Harries's avatar
Dafydd Harries committed
684 685 686
}


687
NICEAPI_EXPORT NiceAgent *
688
nice_agent_new (GMainContext *ctx, NiceCompatibility compat)
Dafydd Harries's avatar
Dafydd Harries committed
689
{
690 691
  NiceAgent *agent = g_object_new (NICE_TYPE_AGENT,
      "compatibility", compat,
692
      "main-context", ctx,
693 694 695 696 697 698 699 700 701 702 703 704 705 706
      "reliable", FALSE,
      NULL);

  return agent;
}


NICEAPI_EXPORT NiceAgent *
nice_agent_new_reliable (GMainContext *ctx, NiceCompatibility compat)
{
  NiceAgent *agent = g_object_new (NICE_TYPE_AGENT,
      "compatibility", compat,
      "main-context", ctx,
      "reliable", TRUE,
Dafydd Harries's avatar
Dafydd Harries committed
707
      NULL);
708 709

  return agent;
Dafydd Harries's avatar
Dafydd Harries committed
710 711 712 713 714 715 716 717 718 719 720
}


static void
nice_agent_get_property (
  GObject *object,
  guint property_id,
  GValue *value,
  GParamSpec *pspec)
{
  NiceAgent *agent = NICE_AGENT (object);
Dafydd Harries's avatar
Dafydd Harries committed
721

722
  agent_lock();
Youness Alaoui's avatar
Youness Alaoui committed
723

Dafydd Harries's avatar
Dafydd Harries committed
724 725
  switch (property_id)
    {
726 727 728 729
    case PROP_MAIN_CONTEXT:
      g_value_set_pointer (value, agent->main_context);
      break;

730 731 732 733
    case PROP_COMPATIBILITY:
      g_value_set_uint (value, agent->compatibility);
      break;

Dafydd Harries's avatar
Dafydd Harries committed
734
    case PROP_STUN_SERVER:
735
      g_value_set_string (value, agent->stun_server_ip);
736
      break;
737 738 739

    case PROP_STUN_SERVER_PORT:
      g_value_set_uint (value, agent->stun_server_port);
740
      break;
741

742 743 744 745 746 747 748
    case PROP_CONTROLLING_MODE:
      g_value_set_boolean (value, agent->controlling_mode);
      break;

    case PROP_FULL_MODE:
      g_value_set_boolean (value, agent->full_mode);
      break;
Dafydd Harries's avatar
Dafydd Harries committed
749

750 751 752 753
    case PROP_STUN_PACING_TIMER:
      g_value_set_uint (value, agent->timer_ta);
      break;

754 755 756 757 758
    case PROP_MAX_CONNECTIVITY_CHECKS:
      g_value_set_uint (value, agent->max_conn_checks);
      /* XXX: should we prune the list of already existing checks? */
      break;

759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778
    case PROP_PROXY_IP:
      g_value_set_string (value, agent->proxy_ip);
      break;

    case PROP_PROXY_PORT:
      g_value_set_uint (value, agent->proxy_port);
      break;

    case PROP_PROXY_TYPE:
      g_value_set_uint (value, agent->proxy_type);
      break;

    case PROP_PROXY_USERNAME:
      g_value_set_string (value, agent->proxy_username);
      break;

    case PROP_PROXY_PASSWORD:
      g_value_set_string (value, agent->proxy_password);
      break;

Youness Alaoui's avatar
Youness Alaoui committed
779
    case PROP_UPNP:
780
#ifdef HAVE_GUPNP
Youness Alaoui's avatar
Youness Alaoui committed
781
      g_value_set_boolean (value, agent->upnp_enabled);
782 783 784
#else
      g_value_set_boolean (value, FALSE);
#endif
Youness Alaoui's avatar
Youness Alaoui committed
785 786 787
      break;

    case PROP_UPNP_TIMEOUT:
788
#ifdef HAVE_GUPNP
Youness Alaoui's avatar
Youness Alaoui committed
789
      g_value_set_uint (value, agent->upnp_timeout);
790 791
#else
      g_value_set_uint (value, DEFAULT_UPNP_TIMEOUT);
Youness Alaoui's avatar
Youness Alaoui committed
792
#endif
793
      break;
Youness Alaoui's avatar
Youness Alaoui committed
794

795 796 797 798
    case PROP_RELIABLE:
      g_value_set_boolean (value, agent->reliable);
      break;

Dafydd Harries's avatar
Dafydd Harries committed
799 800 801
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
    }
Youness Alaoui's avatar
Youness Alaoui committed
802

803
  agent_unlock();
Dafydd Harries's avatar
Dafydd Harries committed
804 805 806 807 808 809 810 811 812 813 814 815
}


static void
nice_agent_set_property (
  GObject *object,
  guint property_id,
  const GValue *value,
  GParamSpec *pspec)
{
  NiceAgent *agent = NICE_AGENT (object);

816
  agent_lock();
Youness Alaoui's avatar
Youness Alaoui committed
817

Dafydd Harries's avatar
Dafydd Harries committed
818 819
  switch (property_id)
    {
820 821
    case PROP_MAIN_CONTEXT:
      agent->main_context = g_value_get_pointer (value);
822 823
      if (agent->main_context != NULL)
        g_main_context_ref (agent->main_context);
824 825
      break;

826 827
    case PROP_COMPATIBILITY:
      agent->compatibility = g_value_get_uint (value);
828
      if (agent->compatibility == NICE_COMPATIBILITY_GOOGLE) {
829 830 831 832
        stun_agent_init (&agent->stun_agent, STUN_ALL_KNOWN_ATTRIBUTES,
            STUN_COMPATIBILITY_RFC3489,
            STUN_AGENT_USAGE_SHORT_TERM_CREDENTIALS |
            STUN_AGENT_USAGE_IGNORE_CREDENTIALS);
Youness Alaoui's avatar
Youness Alaoui committed
833 834 835
      } else if (agent->compatibility == NICE_COMPATIBILITY_MSN) {
        stun_agent_init (&agent->stun_agent, STUN_ALL_KNOWN_ATTRIBUTES,
            STUN_COMPATIBILITY_RFC3489,
836 837
            STUN_AGENT_USAGE_SHORT_TERM_CREDENTIALS |
            STUN_AGENT_USAGE_FORCE_VALIDATER);
838 839 840 841 842 843 844 845 846 847
      } else if (agent->compatibility == NICE_COMPATIBILITY_WLM2009) {
        stun_agent_init (&agent->stun_agent, STUN_ALL_KNOWN_ATTRIBUTES,
            STUN_COMPATIBILITY_WLM2009,
            STUN_AGENT_USAGE_SHORT_TERM_CREDENTIALS |
            STUN_AGENT_USAGE_USE_FINGERPRINT);
      } else {
        stun_agent_init (&agent->stun_agent, STUN_ALL_KNOWN_ATTRIBUTES,
            STUN_COMPATIBILITY_RFC5389,
            STUN_AGENT_USAGE_SHORT_TERM_CREDENTIALS |
            STUN_AGENT_USAGE_USE_FINGERPRINT);
848
      }
849
      stun_agent_set_software (&agent->stun_agent, agent->software_attribute);
850

851 852
      break;

Dafydd Harries's avatar
Dafydd Harries committed
853
    case PROP_STUN_SERVER:
854 855 856 857 858 859 860
      agent->stun_server_ip = g_value_dup_string (value);
      break;

    case PROP_STUN_SERVER_PORT:
      agent->stun_server_port = g_value_get_uint (value);
      break;

861 862 863 864 865 866 867 868
    case PROP_CONTROLLING_MODE:
      agent->controlling_mode = g_value_get_boolean (value);
      break;

    case PROP_FULL_MODE:
      agent->full_mode = g_value_get_boolean (value);
      break;

869 870 871 872
    case PROP_STUN_PACING_TIMER:
      agent->timer_ta = g_value_get_uint (value);
      break;

873 874 875 876
    case PROP_MAX_CONNECTIVITY_CHECKS:
      agent->max_conn_checks = g_value_get_uint (value);
      break;

877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896
    case PROP_PROXY_IP:
      agent->proxy_ip = g_value_dup_string (value);
      break;

    case PROP_PROXY_PORT:
      agent->proxy_port = g_value_get_uint (value);
      break;

    case PROP_PROXY_TYPE:
      agent->proxy_type = g_value_get_uint (value);
      break;

    case PROP_PROXY_USERNAME:
      agent->proxy_username = g_value_dup_string (value);
      break;

    case PROP_PROXY_PASSWORD:
      agent->proxy_password = g_value_dup_string (value);
      break;

Youness Alaoui's avatar
Youness Alaoui committed
897
    case PROP_UPNP_TIMEOUT:
898
#ifdef HAVE_GUPNP
Youness Alaoui's avatar
Youness Alaoui committed
899
      agent->upnp_timeout = g_value_get_uint (value);
900
#endif
Youness Alaoui's avatar
Youness Alaoui committed
901 902 903
      break;

    case PROP_UPNP:
904
#ifdef HAVE_GUPNP
Youness Alaoui's avatar
Youness Alaoui committed
905 906
      agent->upnp_enabled = g_value_get_boolean (value);
#endif
907
      break;
908 909 910 911 912

    case PROP_RELIABLE:
      agent->reliable = g_value_get_boolean (value);
      break;

Dafydd Harries's avatar
Dafydd Harries committed
913 914 915
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
    }
Youness Alaoui's avatar
Youness Alaoui committed
916

917
  agent_unlock();
Youness Alaoui's avatar
Youness Alaoui committed
918

Dafydd Harries's avatar
Dafydd Harries committed
919 920
}

921

922 923 924 925 926 927 928 929 930 931 932 933
static void priv_destroy_component_tcp (Component *component)
{
    if (component->tcp_clock) {
      g_source_destroy (component->tcp_clock);
      g_source_unref (component->tcp_clock);
      component->tcp_clock = NULL;
    }
    if (component->tcp) {
      pseudo_tcp_socket_close (component->tcp, TRUE);
      g_object_unref (component->tcp);
      component->tcp = NULL;
    }
934 935 936 937
    if (component->tcp_data != NULL) {
      g_slice_free (TcpUserData, component->tcp_data);
      component->tcp_data = NULL;
    }
938 939 940 941 942 943 944 945 946 947
}

static void priv_pseudo_tcp_error (NiceAgent *agent, Stream *stream,
    Component *component)
{
  if (component->tcp) {
    agent_signal_component_state_change (agent, stream->id,
        component->id, NICE_COMPONENT_STATE_FAILED);
    priv_detach_stream_component (stream, component);
  }
948
  priv_destroy_component_tcp (component);
949 950 951 952
}

static void
adjust_tcp_clock (NiceAgent *agent, Stream *stream, Component *component);
953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975


static void
pseudo_tcp_socket_opened (PseudoTcpSocket *sock, gpointer user_data)
{
  TcpUserData *data = (TcpUserData *)user_data;
  NiceAgent *agent = data->agent;
  Component *component = data->component;
  Stream *stream = data->stream;

  nice_debug ("Agent %p: s%d:%d pseudo Tcp socket Opened", data->agent,
      stream->id, component->id);
  g_signal_emit (agent, signals[SIGNAL_RELIABLE_TRANSPORT_WRITABLE], 0,
      stream->id, component->id);
}

static void
pseudo_tcp_socket_readable (PseudoTcpSocket *sock, gpointer user_data)
{
  TcpUserData *data = (TcpUserData *)user_data;
  NiceAgent *agent = data->agent;
  Component *component = data->component;
  Stream *stream = data->stream;
976
  gchar buf[MAX_BUFFER_SIZE];
977 978 979 980 981
  gint len;

  nice_debug ("Agent %p: s%d:%d pseudo Tcp socket readable", agent,
      stream->id, component->id);

982 983
  component->tcp_readable = TRUE;

984 985 986
  g_object_add_weak_pointer (G_OBJECT (sock), (gpointer *)&sock);
  g_object_add_weak_pointer (G_OBJECT (agent), (gpointer *)&agent);

987
  do {
988 989 990 991
    if (component->g_source_io_cb)
      len = pseudo_tcp_socket_recv (sock, buf, sizeof(buf));
    else
      len = 0;
992

993
    if (len > 0) {
994 995 996 997 998 999 1000 1001
      gpointer data = component->data;
      gint sid = stream->id;
      gint cid = component->id;
      NiceAgentRecvFunc callback = component->g_source_io_cb;
      /* Unlock the agent before calling the callback */
      agent_unlock();
      callback (agent, sid, cid, len, buf, data);
      agent_lock();
1002 1003 1004 1005
      if (sock == NULL) {
        nice_debug ("PseudoTCP socket got destroyed in readable callback!");
        break;
      }
1006 1007 1008
    } else if (len < 0 &&
        pseudo_tcp_socket_get_error (sock) != EWOULDBLOCK) {
      /* Signal error */
1009
      priv_pseudo_tcp_error (agent, stream, component);
1010 1011 1012
    } else if (len < 0 &&
        pseudo_tcp_socket_get_error (sock) == EWOULDBLOCK){
      component->tcp_readable = FALSE;
1013 1014 1015
    }
  } while (len > 0);

1016
  if (agent) {
1017
    adjust_tcp_clock (agent, stream, component);
1018 1019
    g_object_remove_weak_pointer (G_OBJECT (agent), (gpointer *)&agent);
  } else {
1020
    nice_debug ("Not calling adjust_tcp_clock.. agent got destroyed!");
1021 1022 1023
  }
  if (sock)
    g_object_remove_weak_pointer (G_OBJECT (sock), (gpointer *)&sock);
1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044
}

static void
pseudo_tcp_socket_writable (PseudoTcpSocket *sock, gpointer user_data)
{
  TcpUserData *data = (TcpUserData *)user_data;
  NiceAgent *agent = data->agent;
  Component *component = data->component;
  Stream *stream = data->stream;

  nice_debug ("Agent %p: s%d:%d pseudo Tcp socket writable", data->agent,
      data->stream->id, data->component->id);
  g_signal_emit (agent, signals[SIGNAL_RELIABLE_TRANSPORT_WRITABLE], 0,
      stream->id, component->id);
}

static void
pseudo_tcp_socket_closed (PseudoTcpSocket *sock, guint32 err,
    gpointer user_data)
{
  TcpUserData *data = (TcpUserData *)user_data;
1045 1046 1047 1048 1049 1050 1051
  NiceAgent *agent = data->agent;
  Component *component = data->component;
  Stream *stream = data->stream;

  nice_debug ("Agent %p: s%d:%d pseudo Tcp socket closed",  agent,
      stream->id, component->id);
  priv_pseudo_tcp_error (agent, stream, component);
1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092
}


static PseudoTcpWriteResult
pseudo_tcp_socket_write_packet (PseudoTcpSocket *sock,
    const gchar *buffer, guint32 len, gpointer user_data)
{
  TcpUserData *data = (TcpUserData *)user_data;
  NiceAgent *agent = data->agent;
  Component *component = data->component;
  Stream *stream = data->stream;

  if (component->selected_pair.local != NULL) {
    NiceSocket *sock;
    NiceAddress *addr;

#ifndef NDEBUG
    gchar tmpbuf[INET6_ADDRSTRLEN];
    nice_address_to_string (&component->selected_pair.remote->addr, tmpbuf);

    nice_debug ("Agent %p : s%d:%d: sending %d bytes to [%s]:%d", agent,
        stream->id, component->id, len, tmpbuf,
        nice_address_get_port (&component->selected_pair.remote->addr));
#endif

    sock = component->selected_pair.local->sockptr;
    addr = &component->selected_pair.remote->addr;
    if (nice_socket_send (sock, addr, len, buffer)) {
      return WR_SUCCESS;
    }
  }

  return WR_FAIL;
}


static gboolean
notify_pseudo_tcp_socket_clock (gpointer user_data)
{
  TcpUserData *data = (TcpUserData *)user_data;
  Component *component = data->component;
1093
  Stream *stream = data->stream;
1094 1095 1096 1097
  NiceAgent *agent = data->agent;

  agent_lock();

1098 1099 1100 1101 1102 1103
  if (g_source_is_destroyed (g_main_current_source ())) {
    nice_debug ("Source was destroyed. "
        "Avoided race condition in notify_pseudo_tcp_socket_clock");
    agent_unlock ();
    return FALSE;
  }
1104 1105 1106 1107 1108 1109 1110
  if (component->tcp_clock) {
    g_source_destroy (component->tcp_clock);
    g_source_unref (component->tcp_clock);
    component->tcp_clock = NULL;
  }

  pseudo_tcp_socket_notify_clock (component->tcp);
1111
  adjust_tcp_clock (agent, stream, component);
1112 1113 1114 1115 1116 1117

  agent_unlock();

  return FALSE;
}

1118 1119
static void
adjust_tcp_clock (NiceAgent *agent, Stream *stream, Component *component)
1120 1121
{
  long timeout = 0;
1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134
  if (component->tcp) {
    if (pseudo_tcp_socket_get_next_clock (component->tcp, &timeout)) {
      if (component->tcp_clock) {
        g_source_destroy (component->tcp_clock);
        g_source_unref (component->tcp_clock);
        component->tcp_clock = NULL;
      }
      component->tcp_clock = agent_timeout_add_with_context (agent,
          timeout, notify_pseudo_tcp_socket_clock, component->tcp_data);
    } else {
      nice_debug ("Agent %p: component %d pseudo tcp socket should be destroyed",
          agent, component->id);
      priv_pseudo_tcp_error (agent, stream, component);
1135 1136 1137 1138 1139
    }
  }
}


1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151
void agent_gathering_done (NiceAgent *agent)
{

  GSList *i, *j, *k, *l, *m;

  for (i = agent->streams; i; i = i->next) {
    Stream *stream = i->data;
    for (j = stream->components; j; j = j->next) {
      Component *component = j->data;

      for (k = component->local_candidates; k; k = k->next) {
        NiceCandidate *local_candidate = k->data;
Youness Alaoui's avatar
Youness Alaoui committed
1152 1153 1154 1155 1156 1157 1158 1159 1160
	{
	  gchar tmpbuf[INET6_ADDRSTRLEN];
	  nice_address_to_string (&local_candidate->addr, tmpbuf);
          nice_debug ("Agent %p: gathered local candidate : [%s]:%u"
              " for s%d/c%d. U/P '%s'/'%s'", agent,
              tmpbuf, nice_address_get_port (&local_candidate->addr),
              local_candidate->stream_id, local_candidate->component_id,
              local_candidate->username, local_candidate->password);
	}
1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177
        for (l = component->remote_candidates; l; l = l->next) {
          NiceCandidate *remote_candidate = l->data;

          for (m = stream->conncheck_list; m; m = m->next) {
            CandidateCheckPair *p = m->data;

            if (p->local == local_candidate && p->remote == remote_candidate)
              break;
          }
          if (m == NULL) {
            conn_check_add_for_candidate (agent, stream->id, component, remote_candidate);
          }
        }
      }
    }
  }

1178
#ifdef HAVE_GUPNP
Youness Alaoui's avatar
Youness Alaoui committed
1179 1180 1181 1182
  if (agent->discovery_timer_source == NULL &&
      agent->upnp_timer_source == NULL) {
    agent_signal_gathering_done (agent);
  }
1183 1184 1185
#else
  agent_signal_gathering_done (agent);
#endif
1186 1187
}

1188
void agent_signal_gathering_done (NiceAgent *agent)
1189
{
1190 1191 1192 1193 1194 1195
  GSList *i;

  for (i = agent->streams; i; i = i->next) {
    Stream *stream = i->data;
    if (stream->gathering) {
      stream->gathering = FALSE;
1196
      g_signal_emit (agent, signals[SIGNAL_CANDIDATE_GATHERING_DONE], 0, stream->id);
1197 1198
    }
  }
1199
}
Dafydd Harries's avatar
Dafydd Harries committed
1200

1201
void agent_signal_initial_binding_request_received (NiceAgent *agent, Stream *stream)
1202
{
1203 1204 1205 1206
  if (stream->initial_binding_request_received != TRUE) {
    stream->initial_binding_request_received = TRUE;
    g_signal_emit (agent, signals[SIGNAL_INITIAL_BINDING_REQUEST_RECEIVED], 0, stream->id);
  }
1207 1208
}

1209
void agent_signal_new_selected_pair (NiceAgent *agent, guint stream_id, guint component_id, const gchar *local_foundation, const gchar *remote_foundation)
1210 1211
{
  Component *component;
1212
  Stream *stream;
1213 1214
  gchar *lf_copy;
  gchar *rf_copy;
1215

1216 1217
  if (!agent_find_component (agent, stream_id, component_id,
          &stream, &component))
1218 1219
    return;

1220
  if (component->selected_pair.local->type == NICE_CANDIDATE_TYPE_RELAYED) {
1221
    nice_turn_socket_set_peer (component->selected_pair.local->sockptr,
1222 1223
                                   &component->selected_pair.remote->addr);
  }
1224

1225 1226
  if (component->tcp) {
    pseudo_tcp_socket_connect (component->tcp);
1227
    pseudo_tcp_socket_notify_mtu (component->tcp, MAX_TCP_MTU);
1228 1229 1230 1231
    adjust_tcp_clock (agent, stream, component);
  } else if(agent->reliable) {
    nice_debug ("New selected pair received when pseudo tcp socket in error");
    return;
1232 1233
  }

1234 1235 1236
  lf_copy = g_strdup (local_foundation);
  rf_copy = g_strdup (remote_foundation);

1237 1238 1239 1240 1241
  g_signal_emit (agent, signals[SIGNAL_NEW_SELECTED_PAIR], 0,
      stream_id, component_id, lf_copy, rf_copy);

  g_free (lf_copy);
  g_free (rf_copy);
1242 1243
}

1244
void agent_signal_new_candidate (NiceAgent *agent, NiceCandidate *candidate)
1245
{
1246 1247 1248
  g_signal_emit (agent, signals[SIGNAL_NEW_CANDIDATE], 0,
		 candidate->stream_id,
		 candidate->component_id,
1249
		 candidate->foundation);
1250 1251
}

1252 1253 1254 1255 1256 1257 1258 1259
void agent_signal_new_remote_candidate (NiceAgent *agent, NiceCandidate *candidate)
{
  g_signal_emit (agent, signals[SIGNAL_NEW_REMOTE_CANDIDATE], 0, 
		 candidate->stream_id, 
		 candidate->component_id, 
		 candidate->foundation);
}

1260
void agent_signal_component_state_change (NiceAgent *agent, guint stream_id, guint component_id, NiceComponentState state)
1261 1262
{
  Component *component;
1263
  Stream *stream;
1264

1265 1266 1267 1268 1269 1270 1271 1272 1273
  if (!agent_find_component (agent, stream_id, component_id,
          &stream, &component))
    return;

  if (agent->reliable && component->tcp == NULL &&
      state != NICE_COMPONENT_STATE_FAILED) {
    nice_debug ("Agent %p: not changing component state for s%d:%d to %d "
        "because pseudo tcp socket does not exist in reliable mode", agent,
        stream->id, component->id, state);
1274
    return;
1275
  }
1276

1277
  if (component->state != state && state < NICE_COMPONENT_STATE_LAST) {
1278
    nice_debug ("Agent %p : stream %u component %u STATE-CHANGE %u -> %u.", agent,
1279
	     stream_id, component_id, component->state, state);
1280

1281
    component->state = state;
1282 1283

    g_signal_emit (agent, signals[SIGNAL_COMPONENT_STATE_CHANGED], 0,
1284
		   stream_id, component_id, state);
1285 1286 1287
  }
}

1288 1289 1290
guint64
agent_candidate_pair_priority (NiceAgent *agent, NiceCandidate *local, NiceCandidate *remote)
{
Olivier Crête's avatar
Olivier Crête committed
1291
  if (agent->controlling_mode)
1292
    return nice_candidate_pair_priority (local->priority, remote->priority);
Olivier Crête's avatar
Olivier Crête committed
1293 1294
  else
    return nice_candidate_pair_priority (remote->priority, local->priority);
1295 1296
}

1297
static void
1298
priv_add_new_candidate_discovery_stun (NiceAgent *agent,
1299
    NiceSocket *socket, NiceAddress server,
1300
    Stream *stream, guint component_id)
1301 1302 1303 1304 1305
{
  CandidateDiscovery *cdisco;

  /* note: no need to check for redundant candidates, as this is
   *       done later on in the process */
1306

1307
  cdisco = g_slice_new0 (CandidateDiscovery);
1308

1309 1310 1311 1312 1313 1314 1315 1316
  cdisco->type = NICE_CANDIDATE_TYPE_SERVER_REFLEXIVE;
  cdisco->nicesock = socket;
  cdisco->server = server;
  cdisco->stream = stream;
  cdisco->component = stream_find_component_by_id (stream, component_id);
  cdisco->agent = agent;
  stun_agent_init (&cdisco->stun_agent, STUN_ALL_KNOWN_ATTRIBUTES,
      STUN_COMPATIBILITY_RFC3489, 0);
1317

1318 1319
  nice_debug ("Agent %p : Adding new srv-rflx candidate discovery %p\n",
      agent, cdisco);
1320

1321 1322
  agent->discovery_list = g_slist_append (agent->discovery_list, cdisco);
  ++agent->discovery_unsched_items;
1323 1324
}

1325
static void
1326
priv_add_new_candidate_discovery_turn (NiceAgent *agent,
1327
    NiceSocket *socket, TurnServer *turn,
1328
    Stream *stream, guint component_id)
1329 1330
{
  CandidateDiscovery *cdisco;