agent.c 168 KB
Newer Older
1 2 3
/*
 * This file is part of the Nice GLib ICE library.
 *
4
 * (C) 2006-2010, 2013 Collabora Ltd.
5 6
 *  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
 *   Philip Withnall, Collabora Ltd.
29 30 31 32 33 34 35 36 37 38 39
 *
 * 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
40

41 42 43

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

48
#include <glib.h>
49
#include <gobject/gvaluecollector.h>
50

Dafydd Harries's avatar
Dafydd Harries committed
51
#include <string.h>
52
#include <errno.h>
Dafydd Harries's avatar
Dafydd Harries committed
53

54
#ifndef G_OS_WIN32
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 "datagram-based.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
#include "pseudotcp.h"
76
#include "agent-enum-types.h"
77

78 79 80 81
/* Maximum size of a UDP packet’s payload, as the packet’s length field is 16b
 * wide. */
#define MAX_BUFFER_SIZE ((1 << 16) - 1)  /* 65535 */

82
#define DEFAULT_STUN_PORT  3478
83
#define DEFAULT_UPNP_TIMEOUT 200  /* milliseconds */
84

85
static void
86 87
nice_debug_input_message_composition (const NiceInputMessage *messages,
    guint n_messages);
88
static const gchar *_cand_type_to_sdp (NiceCandidateType type);
89

Dafydd Harries's avatar
Dafydd Harries committed
90 91 92 93
G_DEFINE_TYPE (NiceAgent, nice_agent, G_TYPE_OBJECT);

enum
{
94
  PROP_COMPATIBILITY = 1,
95
  PROP_MAIN_CONTEXT,
96
  PROP_STUN_SERVER,
97
  PROP_STUN_SERVER_PORT,
98
  PROP_CONTROLLING_MODE,
99
  PROP_FULL_MODE,
100
  PROP_STUN_PACING_TIMER,
101 102 103 104 105
  PROP_MAX_CONNECTIVITY_CHECKS,
  PROP_PROXY_TYPE,
  PROP_PROXY_IP,
  PROP_PROXY_PORT,
  PROP_PROXY_USERNAME,
Youness Alaoui's avatar
Youness Alaoui committed
106 107
  PROP_PROXY_PASSWORD,
  PROP_UPNP,
108
  PROP_UPNP_TIMEOUT,
Youness Alaoui's avatar
Youness Alaoui committed
109 110
  PROP_ICE_UDP,
  PROP_ICE_TCP,
111
  PROP_BYTESTREAM_TCP,
112 113
  PROP_KEEPALIVE_CONNCHECK,
  PROP_FORCE_RELAY,
Olivier Crête's avatar
Olivier Crête committed
114
  PROP_TURN_SHORT_TERM,
115 116 117
  PROP_STUN_MAX_RETRANSMISSIONS,
  PROP_STUN_INITIAL_TIMEOUT,
  PROP_STUN_RELIABLE_TIMEOUT,
118
  PROP_NOMINATION_MODE,
Dafydd Harries's avatar
Dafydd Harries committed
119 120 121
};


122 123 124
enum
{
  SIGNAL_COMPONENT_STATE_CHANGED,
125
  SIGNAL_CANDIDATE_GATHERING_DONE,
126 127
  SIGNAL_NEW_SELECTED_PAIR,
  SIGNAL_NEW_CANDIDATE,
128
  SIGNAL_NEW_REMOTE_CANDIDATE,
129
  SIGNAL_INITIAL_BINDING_REQUEST_RECEIVED,
130
  SIGNAL_STREAMS_REMOVED,
131
  SIGNAL_NEW_SELECTED_PAIR_FULL,
132 133 134
  SIGNAL_NEW_CANDIDATE_FULL,
  SIGNAL_NEW_REMOTE_CANDIDATE_FULL,

135 136 137 138 139
  N_SIGNALS,
};

static guint signals[N_SIGNALS];

140
static GMutex agent_mutex;    /* Mutex used for thread-safe lib */
141

142
static void priv_stop_upnp (NiceAgent *agent);
143 144 145 146 147 148
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);

149 150
void agent_lock (void)
{
151
  g_mutex_lock (&agent_mutex);
152 153 154 155
}

void agent_unlock (void)
{
156
  g_mutex_unlock (&agent_mutex);
157 158
}

159 160 161 162 163 164
static GType _nice_agent_stream_ids_get_type (void);

G_DEFINE_POINTER_TYPE (_NiceAgentStreamIds, _nice_agent_stream_ids);

#define NICE_TYPE_AGENT_STREAM_IDS _nice_agent_stream_ids_get_type ()

165 166 167 168 169 170 171 172 173 174 175 176
typedef struct {
  guint signal_id;
  GSignalQuery query;
  GValue *params;
} QueuedSignal;


static void
free_queued_signal (QueuedSignal *sig)
{
  guint i;

Olivier Crête's avatar
Olivier Crête committed
177 178
  g_value_unset (&sig->params[0]);

179
  for (i = 0; i < sig->query.n_params; i++) {
180
    if (G_VALUE_HOLDS(&sig->params[i + 1], NICE_TYPE_AGENT_STREAM_IDS))
Olivier Crête's avatar
Olivier Crête committed
181 182
      g_free (g_value_get_pointer (&sig->params[i + 1]));
    g_value_unset (&sig->params[i + 1]);
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
  }

  g_slice_free1 (sizeof(GValue) * (sig->query.n_params + 1), sig->params);
  g_slice_free (QueuedSignal, sig);
}

void
agent_unlock_and_emit (NiceAgent *agent)
{
  GQueue queue = G_QUEUE_INIT;
  QueuedSignal *sig;

  queue = agent->pending_signals;
  g_queue_init (&agent->pending_signals);

  agent_unlock ();

  while ((sig = g_queue_pop_head (&queue))) {
    g_signal_emitv (sig->params, sig->signal_id, 0, NULL);

    free_queued_signal (sig);
  }
}

static void
agent_queue_signal (NiceAgent *agent, guint signal_id, ...)
{
  QueuedSignal *sig;
  guint i;
  gchar *error = NULL;
  va_list var_args;

  sig = g_slice_new (QueuedSignal);
  g_signal_query (signal_id, &sig->query);

  sig->signal_id = signal_id;
  sig->params = g_slice_alloc0 (sizeof(GValue) * (sig->query.n_params + 1));

  g_value_init (&sig->params[0], G_TYPE_OBJECT);
  g_value_set_object (&sig->params[0], agent);

  va_start (var_args, signal_id);
  for (i = 0; i < sig->query.n_params; i++) {
    G_VALUE_COLLECT_INIT (&sig->params[i + 1], sig->query.param_types[i],
        var_args, 0, &error);
    if (error)
      break;
  }
  va_end (var_args);

  if (error) {
    free_queued_signal (sig);
    g_critical ("Error collecting values for signal: %s", error);
    g_free (error);
    return;
  }

  g_queue_push_tail (&agent->pending_signals, sig);
}


244 245 246
StunUsageIceCompatibility
agent_to_ice_compatibility (NiceAgent *agent)
{
247
  return agent->compatibility == NICE_COMPATIBILITY_GOOGLE ?
248 249
      STUN_USAGE_ICE_COMPATIBILITY_GOOGLE :
      agent->compatibility == NICE_COMPATIBILITY_MSN ?
250
      STUN_USAGE_ICE_COMPATIBILITY_MSN :
Jakub Adam's avatar
Jakub Adam committed
251
      agent->compatibility == NICE_COMPATIBILITY_WLM2009 ?
252
      STUN_USAGE_ICE_COMPATIBILITY_MSICE2 :
Jakub Adam's avatar
Jakub Adam committed
253 254
      agent->compatibility == NICE_COMPATIBILITY_OC2007 ?
      STUN_USAGE_ICE_COMPATIBILITY_MSN :
Jakub Adam's avatar
Jakub Adam committed
255
      agent->compatibility == NICE_COMPATIBILITY_OC2007R2 ?
256
      STUN_USAGE_ICE_COMPATIBILITY_MSICE2 :
257
      STUN_USAGE_ICE_COMPATIBILITY_RFC5245;
258 259 260 261 262 263
}


StunUsageTurnCompatibility
agent_to_turn_compatibility (NiceAgent *agent)
{
264
  return agent->compatibility == NICE_COMPATIBILITY_GOOGLE ?
265 266
      STUN_USAGE_TURN_COMPATIBILITY_GOOGLE :
      agent->compatibility == NICE_COMPATIBILITY_MSN ?
267 268
      STUN_USAGE_TURN_COMPATIBILITY_MSN :
      agent->compatibility == NICE_COMPATIBILITY_WLM2009 ?
Jakub Adam's avatar
Jakub Adam committed
269 270
      STUN_USAGE_TURN_COMPATIBILITY_MSN :
      agent->compatibility == NICE_COMPATIBILITY_OC2007 ?
271 272 273
      STUN_USAGE_TURN_COMPATIBILITY_OC2007 :
      agent->compatibility == NICE_COMPATIBILITY_OC2007R2 ?
      STUN_USAGE_TURN_COMPATIBILITY_OC2007 :
Marcus Lundblad's avatar
Marcus Lundblad committed
274
      STUN_USAGE_TURN_COMPATIBILITY_RFC5766;
275 276
}

277 278 279
NiceTurnSocketCompatibility
agent_to_turn_socket_compatibility (NiceAgent *agent)
{
280 281 282 283
  if (agent->compatibility == NICE_COMPATIBILITY_RFC5245 &&
      agent->turn_short_term)
    return NICE_TURN_SOCKET_COMPATIBILITY_RFC5766_SHORT_TERM;

284
  return agent->compatibility == NICE_COMPATIBILITY_GOOGLE ?
285 286 287
      NICE_TURN_SOCKET_COMPATIBILITY_GOOGLE :
      agent->compatibility == NICE_COMPATIBILITY_MSN ?
      NICE_TURN_SOCKET_COMPATIBILITY_MSN :
288 289
      agent->compatibility == NICE_COMPATIBILITY_WLM2009 ?
      NICE_TURN_SOCKET_COMPATIBILITY_MSN :
Jakub Adam's avatar
Jakub Adam committed
290
      agent->compatibility == NICE_COMPATIBILITY_OC2007 ?
291 292 293
      NICE_TURN_SOCKET_COMPATIBILITY_OC2007 :
      agent->compatibility == NICE_COMPATIBILITY_OC2007R2 ?
      NICE_TURN_SOCKET_COMPATIBILITY_OC2007 :
Marcus Lundblad's avatar
Marcus Lundblad committed
294
      NICE_TURN_SOCKET_COMPATIBILITY_RFC5766;
295 296
}

297
NiceStream *agent_find_stream (NiceAgent *agent, guint stream_id)
298 299 300 301 302
{
  GSList *i;

  for (i = agent->streams; i; i = i->next)
    {
303
      NiceStream *s = i->data;
304 305 306 307 308 309 310 311 312

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

  return NULL;
}


313 314
gboolean
agent_find_component (
315 316 317
  NiceAgent *agent,
  guint stream_id,
  guint component_id,
318
  NiceStream **stream,
319
  NiceComponent **component)
320
{
321
  NiceStream *s;
322
  NiceComponent *c;
323

324
  s = agent_find_stream (agent, stream_id);
325

326
  if (s == NULL)
327 328
    return FALSE;

329
  c = nice_stream_find_component_by_id (s, component_id);
330 331 332 333

  if (c == NULL)
    return FALSE;

334 335 336 337
  if (stream)
    *stream = s;

  if (component)
338
    *component = c;
339 340 341 342

  return TRUE;
}

Dafydd Harries's avatar
Dafydd Harries committed
343 344 345 346 347 348 349 350 351
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;

352
  /* install properties */
353 354 355 356 357 358
  /**
   * 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.
   */
359 360 361 362 363 364 365
  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));

366 367 368 369 370 371 372
  /**
   * NiceAgent:compatibility:
   *
   * The Nice agent can work in various compatibility modes depending on
   * what the application/peer needs.
   * <para> See also: #NiceCompatibility</para>
   */
373 374 375 376 377
  g_object_class_install_property (gobject_class, PROP_COMPATIBILITY,
      g_param_spec_uint (
         "compatibility",
         "ICE specification compatibility",
         "The compatibility mode for the agent",
378 379
         NICE_COMPATIBILITY_RFC5245, NICE_COMPATIBILITY_LAST,
         NICE_COMPATIBILITY_RFC5245,
380 381
         G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));

Dafydd Harries's avatar
Dafydd Harries committed
382
  g_object_class_install_property (gobject_class, PROP_STUN_SERVER,
383
      g_param_spec_string (
Dafydd Harries's avatar
Dafydd Harries committed
384
        "stun-server",
385 386
        "STUN server IP address",
        "The IP address (not the hostname) of the STUN server to use",
387
        NULL,
Dafydd Harries's avatar
Dafydd Harries committed
388 389
        G_PARAM_READWRITE));

390 391 392 393
  g_object_class_install_property (gobject_class, PROP_STUN_SERVER_PORT,
      g_param_spec_uint (
        "stun-server-port",
        "STUN server port",
394
        "Port of the STUN server used to gather server-reflexive candidates",
Youness Alaoui's avatar
Youness Alaoui committed
395
        1, 65536,
396
	1, /* not a construct property, ignored */
397 398
        G_PARAM_READWRITE));

399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
  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));

415 416 417 418
  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
419 420
        "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
421
        1, 0xffffffff,
422
	NICE_AGENT_TIMER_TA_DEFAULT,
423
        G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
424

425
  /* note: according to spec recommendation in sect 5.7.3 (ID-19) */
426 427 428 429 430
  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
431
        0, 0xffffffff,
432 433 434
	0, /* default set in init */
        G_PARAM_READWRITE));

435 436 437 438 439 440 441
  /**
   * NiceAgent:nomination-mode:
   *
   * The nomination mode used in the ICE specification for describing
   * the selection of valid pairs to be used upstream.
   * <para> See also: #NiceNominationMode </para>
   *
442
   * Since: 0.1.15
443 444 445 446 447 448 449 450 451 452
   */
  g_object_class_install_property (gobject_class, PROP_NOMINATION_MODE,
      g_param_spec_enum (
         "nomination-mode",
         "ICE nomination mode",
         "Nomination mode used in the ICE specification for describing "
         "the selection of valid pairs to be used upstream",
         NICE_TYPE_NOMINATION_MODE, NICE_NOMINATION_MODE_AGGRESSIVE,
         G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));

453 454 455 456 457 458 459
  /**
   * NiceAgent:proxy-ip:
   *
   * The proxy server IP used to bypass a proxy firewall
   *
   * Since: 0.0.4
   */
460 461 462 463
  g_object_class_install_property (gobject_class, PROP_PROXY_IP,
      g_param_spec_string (
        "proxy-ip",
        "Proxy server IP",
464
        "The proxy server IP used to bypass a proxy firewall",
465 466 467
        NULL,
        G_PARAM_READWRITE));

468 469 470 471 472 473 474
  /**
   * NiceAgent:proxy-port:
   *
   * The proxy server port used to bypass a proxy firewall
   *
   * Since: 0.0.4
   */
475 476 477 478
  g_object_class_install_property (gobject_class, PROP_PROXY_PORT,
      g_param_spec_uint (
        "proxy-port",
        "Proxy server port",
479
        "The Proxy server port used to bypass a proxy firewall",
480 481 482 483
        1, 65536,
	1,
        G_PARAM_READWRITE));

484 485 486 487 488 489 490
  /**
   * NiceAgent:proxy-type:
   *
   * The type of proxy set in the proxy-ip property
   *
   * Since: 0.0.4
   */
491 492 493 494 495 496 497 498 499
  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));

500 501 502 503 504 505 506
  /**
   * NiceAgent:proxy-username:
   *
   * The username used to authenticate with the proxy
   *
   * Since: 0.0.4
   */
507 508 509 510 511 512 513 514
  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));

515 516 517 518 519 520 521
  /**
   * NiceAgent:proxy-password:
   *
   * The password used to authenticate with the proxy
   *
   * Since: 0.0.4
   */
522 523 524 525 526 527 528 529
  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));

530 531 532 533 534 535 536 537
  /**
   * 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
538 539 540
   g_object_class_install_property (gobject_class, PROP_UPNP,
      g_param_spec_boolean (
        "upnp",
541
#ifdef HAVE_GUPNP
Youness Alaoui's avatar
Youness Alaoui committed
542 543 544
        "Use UPnP",
        "Whether the agent should use UPnP to open a port in the router and "
        "get the external IP",
545 546 547 548
#else
        "Use UPnP (disabled in build)",
        "Does nothing because libnice was not built with UPnP support",
#endif
Youness Alaoui's avatar
Youness Alaoui committed
549 550 551
	TRUE, /* enable UPnP by default */
        G_PARAM_READWRITE| G_PARAM_CONSTRUCT));

552 553 554
  /**
   * NiceAgent:upnp-timeout:
   *
555 556
   * The maximum amount of time (in milliseconds) to wait for UPnP discovery to
   * finish before signaling the #NiceAgent::candidate-gathering-done signal
557 558 559
   *
   * Since: 0.0.7
   */
Youness Alaoui's avatar
Youness Alaoui committed
560 561 562
  g_object_class_install_property (gobject_class, PROP_UPNP_TIMEOUT,
      g_param_spec_uint (
        "upnp-timeout",
563
#ifdef HAVE_GUPNP
Youness Alaoui's avatar
Youness Alaoui committed
564 565 566
        "Timeout for UPnP discovery",
        "The maximum amount of time to wait for UPnP discovery to finish before "
        "signaling the candidate-gathering-done signal",
567 568 569 570
#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
571
        100, 60000,
572
	DEFAULT_UPNP_TIMEOUT,
Youness Alaoui's avatar
Youness Alaoui committed
573 574
        G_PARAM_READWRITE | G_PARAM_CONSTRUCT));

575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590
  /**
   * NiceAgent:ice-udp:
   *
   * Whether the agent should use ICE-UDP when gathering candidates.
   * If the option is disabled, no UDP candidates will be generated. If the
   * agent is in reliable mode, then pseudotcp will not be used since pseudotcp
   * works on top of UDP candidates.
   * <para>
   * This option should be set before gathering candidates and should not be
   * modified afterwards.
   * </para>
   * The #NiceAgent:ice-udp property can be set at the same time as the
   * #NiceAgent:ice-tcp property, but both cannot be unset at the same time.
   * If #NiceAgent:ice-tcp is set to %FALSE, then this property cannot be set
   * to %FALSE as well.
   *
Olivier Crête's avatar
Olivier Crête committed
591
   * Since: 0.1.8
592
   */
Youness Alaoui's avatar
Youness Alaoui committed
593 594 595 596 597 598 599
   g_object_class_install_property (gobject_class, PROP_ICE_UDP,
      g_param_spec_boolean (
        "ice-udp",
        "Use ICE-UDP",
        "Use ICE-UDP specification to generate UDP candidates",
        TRUE, /* use ice-udp by default */
        G_PARAM_READWRITE));
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615

  /**
   * NiceAgent:ice-tcp:
   *
   * Whether the agent should use ICE-TCP when gathering candidates.
   * If the option is disabled, no TCP candidates will be generated. If the
   * agent is in reliable mode, then pseudotcp will need to be used over UDP
   * candidates.
   * <para>
   * This option should be set before gathering candidates and should not be
   * modified afterwards.
   * </para>
   * The #NiceAgent:ice-tcp property can be set at the same time as the
   * #NiceAgent:ice-udp property, but both cannot be unset at the same time.
   * If #NiceAgent:ice-udp is set to %FALSE, then this property cannot be set
   * to %FALSE as well.
616 617 618 619 620 621 622
   * <note>
   <para>
   ICE-TCP is only supported for %NICE_COMPATIBILITY_RFC5245,
   %NICE_COMPATIBILITY_OC2007 and %NICE_COMPATIBILITY_OC2007R2 compatibility
   modes.
   </para>
   * </note>
623
   *
Olivier Crête's avatar
Olivier Crête committed
624
   * Since: 0.1.8
625
   */
Youness Alaoui's avatar
Youness Alaoui committed
626 627 628 629 630 631 632
   g_object_class_install_property (gobject_class, PROP_ICE_TCP,
      g_param_spec_boolean (
        "ice-tcp",
        "Use ICE-TCP",
        "Use ICE-TCP specification to generate TCP candidates",
        TRUE, /* use ice-tcp by default */
        G_PARAM_READWRITE));
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656

  /**
   * NiceAgent:bytestream-tcp:
   *
   * This property defines whether receive/send over a TCP or pseudo-TCP, in
   * reliable mode, are considered as packetized or as bytestream.
   * In unreliable mode, every send/recv is considered as packetized, and
   * this property is ignored and cannot be set.
   * <para>
   * In reliable mode, this property will always return %TRUE in the
   * %NICE_COMPATIBILITY_GOOGLE compatibility mode.
   * </para>
   * If the property is %TRUE, the stream is considered in bytestream mode
   * and data can be read with any receive size. If the property is %FALSE, then
   * the stream is considred packetized and each receive will return one packet
   * of the same size as what was sent from the peer. If in packetized mode,
   * then doing a receive with a size smaller than the packet, will cause the
   * remaining bytes in the packet to be dropped, breaking the reliability
   * of the stream.
   * <para>
   * This property is currently read-only, and will become read/write once
   * bytestream mode will be supported.
   * </para>
   *
Olivier Crête's avatar
Olivier Crête committed
657
   * Since: 0.1.8
658 659 660 661 662 663 664 665 666
   */
   g_object_class_install_property (gobject_class, PROP_BYTESTREAM_TCP,
      g_param_spec_boolean (
        "bytestream-tcp",
        "Bytestream TCP",
        "Use bytestream mode for reliable TCP and Pseudo-TCP connections",
        FALSE,
        G_PARAM_READABLE));

667 668 669 670 671 672 673 674 675 676 677 678 679
  /**
   * NiceAgent:keepalive-conncheck:
   *
   * Use binding requests as keepalives instead of binding
   * indications. This means that the keepalives may time out which
   * will change the component state to %NICE_COMPONENT_STATE_FAILED.
   *
   * Enabing this is a slight violation of RFC 5245 section 10 which
   * recommends using Binding Indications for keepalives.
   *
   * This is always enabled if the compatibility mode is
   * %NICE_COMPATIBILITY_GOOGLE.
   *
Olivier Crête's avatar
Olivier Crête committed
680
   * Since: 0.1.8
681 682 683 684 685 686 687 688 689 690
   */
   g_object_class_install_property (gobject_class, PROP_KEEPALIVE_CONNCHECK,
      g_param_spec_boolean (
        "keepalive-conncheck",
        "Use conncheck as keepalives",
        "Use binding requests which require a reply as keepalives instead of "
        "binding indications which don't.",
	FALSE,
        G_PARAM_READWRITE));

691 692 693 694 695 696 697 698
   /**
   * NiceAgent:force-relay
   *
   * Force all traffic to go through a relay for added privacy, this
   * allows hiding the local IP address. When this is enabled, so
   * local candidates are available before relay servers have been set
   * with nice_agent_set_relay_info().
   *
Olivier Crête's avatar
Olivier Crête committed
699
   * Since: 0.1.14
700 701 702 703 704 705 706 707 708
   */
   g_object_class_install_property (gobject_class, PROP_FORCE_RELAY,
      g_param_spec_boolean (
        "force-relay",
        "Force Relay",
        "Force all traffic to go through a relay for added privacy.",
	FALSE,
        G_PARAM_READWRITE));

709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726
   /**
   * NiceAgent:turn-short-term
   *
   * Force all traffic to go through a relay for added privacy, this
   * allows hiding the local IP address. When this is enabled, so
   * local candidates are available before relay servers have been set
   * with nice_agent_set_relay_info().
   *
   * Since: UNRELEASED
   */
   g_object_class_install_property (gobject_class, PROP_TURN_SHORT_TERM,
      g_param_spec_boolean (
        "turn-short-term",
        "TURN Short Term Credentials",
        "Login into TURN server with short term credentials",
	FALSE,
        G_PARAM_READWRITE));

727 728 729 730 731 732 733 734 735 736 737 738 739
   /**
   * NiceAgent:stun-max-retransmissions
   *
   * The maximum number of retransmissions of the STUN binding requests
   * used in the gathering stage, to find our local candidates, and used
   * in the connection check stage, to test the validity of each
   * constructed pair. This property is described as 'Rc' in the RFC
   * 5389, with a default value of 7. The timeout of each STUN request
   * is doubled for each retransmission, so the choice of this value has
   * a direct impact on the time needed to move from the CONNECTED state
   * to the READY state, and on the time needed to complete the GATHERING
   * state.
   *
740
   * Since: 0.1.15
741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764
   */

   g_object_class_install_property (gobject_class, PROP_STUN_MAX_RETRANSMISSIONS,
      g_param_spec_uint (
        "stun-max-retransmissions",
        "STUN Max Retransmissions",
        "Maximum number of STUN binding requests retransmissions "
        "described as 'Rc' in the STUN specification.",
        1, 99,
        STUN_TIMER_DEFAULT_MAX_RETRANSMISSIONS,
        G_PARAM_READWRITE | G_PARAM_CONSTRUCT));

   /**
   * NiceAgent:stun-initial-timeout
   *
   * The initial timeout (msecs) of the STUN binding requests
   * used in the gathering stage, to find our local candidates.
   * This property is described as 'RTO' in the RFC 5389 and RFC 5245.
   * This timeout is doubled for each retransmission, until
   * #NiceAgent:stun-max-retransmissions have been done,
   * with an exception for the last restransmission, where the timeout is
   * divided by two instead (RFC 5389 indicates that a customisable
   * multiplier 'Rm' to 'RTO' should be used).
   *
765
   * Since: 0.1.15
766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783
   */

   g_object_class_install_property (gobject_class, PROP_STUN_INITIAL_TIMEOUT,
      g_param_spec_uint (
        "stun-initial-timeout",
        "STUN Initial Timeout",
        "STUN timeout in msecs of the initial binding requests used in the "
        "gathering state, described as 'RTO' in the ICE specification.",
        20, 9999,
        STUN_TIMER_DEFAULT_TIMEOUT,
        G_PARAM_READWRITE | G_PARAM_CONSTRUCT));

   /**
   * NiceAgent:stun-reliable-timeout
   *
   * The initial timeout of the STUN binding requests used
   * for a reliable timer.
   *
784
   * Since: 0.1.15
785 786 787 788 789 790 791 792 793 794 795 796
   */

   g_object_class_install_property (gobject_class, PROP_STUN_RELIABLE_TIMEOUT,
      g_param_spec_uint (
        "stun-reliable-timeout",
        "STUN Reliable Timeout",
        "STUN timeout in msecs of the initial binding requests used for "
        "a reliable timer.",
        20, 99999,
        STUN_TIMER_DEFAULT_RELIABLE_TIMEOUT,
        G_PARAM_READWRITE | G_PARAM_CONSTRUCT));

797 798
  /* install signals */

799 800
  /**
   * NiceAgent::component-state-changed
Youness Alaoui's avatar
Youness Alaoui committed
801
   * @agent: The #NiceAgent object
802 803
   * @stream_id: The ID of the stream
   * @component_id: The ID of the component
804
   * @state: The new #NiceComponentState of the component
805
   *
806 807 808 809
   * This signal is fired whenever a component’s state changes. There are many
   * valid state transitions.
   *
   * ![State transition diagram](states.png)
810
   */
811 812 813 814
  signals[SIGNAL_COMPONENT_STATE_CHANGED] =
      g_signal_new (
          "component-state-changed",
          G_OBJECT_CLASS_TYPE (klass),
815
          G_SIGNAL_RUN_LAST,
816 817 818
          0,
          NULL,
          NULL,
Olivier Crête's avatar
Olivier Crête committed
819
          NULL,
820 821 822 823
          G_TYPE_NONE,
          3,
          G_TYPE_UINT, G_TYPE_UINT, G_TYPE_UINT,
          G_TYPE_INVALID);
824

825 826
  /**
   * NiceAgent::candidate-gathering-done:
Youness Alaoui's avatar
Youness Alaoui committed
827
   * @agent: The #NiceAgent object
828 829 830 831 832
   * @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()
   */
833 834 835 836
  signals[SIGNAL_CANDIDATE_GATHERING_DONE] =
      g_signal_new (
          "candidate-gathering-done",
          G_OBJECT_CLASS_TYPE (klass),
837
          G_SIGNAL_RUN_LAST,
838 839 840
          0,
          NULL,
          NULL,
Olivier Crête's avatar
Olivier Crête committed
841
          NULL,
842
          G_TYPE_NONE,
843 844
          1,
          G_TYPE_UINT, G_TYPE_INVALID);
845

846 847
  /**
   * NiceAgent::new-selected-pair
Youness Alaoui's avatar
Youness Alaoui committed
848
   * @agent: The #NiceAgent object
849 850 851 852 853
   * @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
   *
854 855 856 857 858 859
   * This signal is fired once a candidate pair is selected for data
   * transfer for a stream's component This is emitted along with
   * #NiceAgent::new-selected-pair-full which has the whole candidate,
   * the Foundation of a Candidate is not a unique identifier.
   *
   * See also: #NiceAgent::new-selected-pair-full
Olivier Crête's avatar
Olivier Crête committed
860
   * Deprecated: 0.1.8: Use #NiceAgent::new-selected-pair-full
861
   */
862 863 864 865
  signals[SIGNAL_NEW_SELECTED_PAIR] =
      g_signal_new (
          "new-selected-pair",
          G_OBJECT_CLASS_TYPE (klass),
866
          G_SIGNAL_RUN_LAST,
867 868 869
          0,
          NULL,
          NULL,
Olivier Crête's avatar
Olivier Crête committed
870
          NULL,
871 872 873 874 875
          G_TYPE_NONE,
          4,
          G_TYPE_UINT, G_TYPE_UINT, G_TYPE_STRING, G_TYPE_STRING,
          G_TYPE_INVALID);

876 877
  /**
   * NiceAgent::new-candidate
Youness Alaoui's avatar
Youness Alaoui committed
878
   * @agent: The #NiceAgent object
879 880 881 882
   * @stream_id: The ID of the stream
   * @component_id: The ID of the component
   * @foundation: The foundation of the new candidate
   *
883
   * This signal is fired when the agent discovers a new local candidate.
884 885 886 887 888
   * When this signal is emitted, a matching #NiceAgent::new-candidate-full is
   * also emitted with the candidate.
   *
   * See also: #NiceAgent::candidate-gathering-done,
   * #NiceAgent::new-candidate-full
Olivier Crête's avatar
Olivier Crête committed
889
   * Deprecated: 0.1.8: Use #NiceAgent::new-candidate-full
890
   */
891 892 893 894
  signals[SIGNAL_NEW_CANDIDATE] =
      g_signal_new (
          "new-candidate",
          G_OBJECT_CLASS_TYPE (klass),
895
          G_SIGNAL_RUN_LAST,
896 897 898
          0,
          NULL,
          NULL,
Olivier Crête's avatar
Olivier Crête committed
899
          NULL,
900 901 902 903 904
          G_TYPE_NONE,
          3,
          G_TYPE_UINT, G_TYPE_UINT, G_TYPE_STRING,
          G_TYPE_INVALID);

905 906
  /**
   * NiceAgent::new-remote-candidate
Youness Alaoui's avatar
Youness Alaoui committed
907
   * @agent: The #NiceAgent object
908 909 910 911
   * @stream_id: The ID of the stream
   * @component_id: The ID of the component
   * @foundation: The foundation of the new candidate
   *
912 913 914 915 916 917 918
   * This signal is fired when the agent discovers a new remote
   * candidate.  This can happen with peer reflexive candidates.  When
   * this signal is emitted, a matching
   * #NiceAgent::new-remote-candidate-full is also emitted with the
   * candidate.
   *
   * See also: #NiceAgent::new-remote-candidate-full
Olivier Crête's avatar
Olivier Crête committed
919
   * Deprecated: 0.1.8: Use #NiceAgent::new-remote-candidate-full
920
   */
921 922 923 924
  signals[SIGNAL_NEW_REMOTE_CANDIDATE] =
      g_signal_new (
          "new-remote-candidate",
          G_OBJECT_CLASS_TYPE (klass),
925
          G_SIGNAL_RUN_LAST,
926 927 928
          0,
          NULL,
          NULL,
Olivier Crête's avatar
Olivier Crête committed
929
          NULL,
930 931 932 933 934
          G_TYPE_NONE,
          3,
          G_TYPE_UINT, G_TYPE_UINT, G_TYPE_STRING,
          G_TYPE_INVALID);

935 936
  /**
   * NiceAgent::initial-binding-request-received
Youness Alaoui's avatar
Youness Alaoui committed
937
   * @agent: The #NiceAgent object
938 939 940 941 942
   * @stream_id: The ID of the stream
   *
   * This signal is fired when we received our first binding request from
   * the peer.
   */
943 944 945 946
  signals[SIGNAL_INITIAL_BINDING_REQUEST_RECEIVED] =
      g_signal_new (
          "initial-binding-request-received",
          G_OBJECT_CLASS_TYPE (klass),
947
          G_SIGNAL_RUN_LAST,
948 949 950
          0,
          NULL,
          NULL,
Olivier Crête's avatar
Olivier Crête committed
951
          NULL,
952 953 954 955 956
          G_TYPE_NONE,
          1,
          G_TYPE_UINT,
          G_TYPE_INVALID);

957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978
  /**
   * NiceAgent::streams-removed
   * @agent: The #NiceAgent object
   * @stream_ids: (array zero-terminated=1) (element-type uint): An array of
   * unsigned integer stream IDs, ending with a 0 ID
   *
   * This signal is fired whenever one or more streams are removed from the
   * @agent.
   *
   * Since: 0.1.5
   */
  signals[SIGNAL_STREAMS_REMOVED] =
      g_signal_new (
          "streams-removed",
          G_OBJECT_CLASS_TYPE (klass),
          G_SIGNAL_RUN_LAST,
          0,
          NULL,
          NULL,
          g_cclosure_marshal_VOID__POINTER,
          G_TYPE_NONE,
          1,
979
          NICE_TYPE_AGENT_STREAM_IDS,
980
          G_TYPE_INVALID);
981

982

983 984 985 986 987 988 989 990 991 992 993 994 995
  /**
   * NiceAgent::new-selected-pair-full
   * @agent: The #NiceAgent object
   * @stream_id: The ID of the stream
   * @component_id: The ID of the component
   * @lcandidate: The local #NiceCandidate of the selected candidate pair
   * @rcandidate: The remote #NiceCandidate of the selected candidate pair
   *
   * This signal is fired once a candidate pair is selected for data
   * transfer for a stream's component. This is emitted along with
   * #NiceAgent::new-selected-pair.
   *
   * See also: #NiceAgent::new-selected-pair
Olivier Crête's avatar
Olivier Crête committed
996
   * Since: 0.1.8
997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
   */
  signals[SIGNAL_NEW_SELECTED_PAIR_FULL] =
      g_signal_new (
          "new-selected-pair-full",
          G_OBJECT_CLASS_TYPE (klass),
          G_SIGNAL_RUN_LAST,
          0,
          NULL,
          NULL,
          NULL,
          G_TYPE_NONE,
          4, G_TYPE_UINT, G_TYPE_UINT, NICE_TYPE_CANDIDATE, NICE_TYPE_CANDIDATE,
          G_TYPE_INVALID);

1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
  /**
   * NiceAgent::new-candidate-full
   * @agent: The #NiceAgent object
   * @candidate: The new #NiceCandidate
   *
   * This signal is fired when the agent discovers a new local candidate.
   * When this signal is emitted, a matching #NiceAgent::new-candidate is
   * also emitted with the candidate's foundation.
   *
   * See also: #NiceAgent::candidate-gathering-done,
   * #NiceAgent::new-candidate
Olivier Crête's avatar
Olivier Crête committed
1022
   * Since: 0.1.8
1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048
   */
  signals[SIGNAL_NEW_CANDIDATE_FULL] =
      g_signal_new (
          "new-candidate-full",
          G_OBJECT_CLASS_TYPE (klass),
          G_SIGNAL_RUN_LAST,
          0,
          NULL,
          NULL,
          NULL,
          G_TYPE_NONE,
          1,
          NICE_TYPE_CANDIDATE,
          G_TYPE_INVALID);

  /**
   * NiceAgent::new-remote-candidate-full
   * @agent: The #NiceAgent object
   * @candidate: The new #NiceCandidate
   *
   * This signal is fired when the agent discovers a new remote candidate.
   * This can happen with peer reflexive candidates.
   * When this signal is emitted, a matching #NiceAgent::new-remote-candidate is
   * also emitted with the candidate's foundation.
   *
   * See also: #NiceAgent::new-remote-candidate
Olivier Crête's avatar
Olivier Crête committed
1049
   * Since: 0.1.8
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064
   */
  signals[SIGNAL_NEW_REMOTE_CANDIDATE_FULL] =
      g_signal_new (
          "new-remote-candidate-full",
          G_OBJECT_CLASS_TYPE (klass),
          G_SIGNAL_RUN_LAST,
          0,
          NULL,
          NULL,
          NULL,
          G_TYPE_NONE,
          1,
          NICE_TYPE_CANDIDATE,
          G_TYPE_INVALID);

1065 1066
  /* Init debug options depending on env variables */
  nice_debug_init ();
Dafydd Harries's avatar
Dafydd Harries committed
1067 1068
}

Rohan Garg's avatar
Rohan Garg committed
1069
static void priv_generate_tie_breaker (NiceAgent *agent)
1070 1071 1072
{
  nice_rng_generate_bytes (agent->rng, 8, (gchar*)&agent->tie_breaker);
}
Dafydd Harries's avatar
Dafydd Harries committed
1073 1074 1075 1076 1077 1078

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

1080
  /* set defaults; not construct params, so set here */
1081
  agent->stun_server_port = DEFAULT_STUN_PORT;
1082
  agent->controlling_mode = TRUE;
1083
  agent->max_conn_checks = NICE_AGENT_MAX_CONNECTIVITY_CHECKS_DEFAULT;
1084
  agent->nomination_mode = NICE_NOMINATION_MODE_AGGRESSIVE;
1085 1086

  agent->discovery_list = NULL;
1087
  agent->discovery_unsched_items = 0;
1088 1089 1090
  agent->discovery_timer_source = NULL;
  agent->conncheck_timer_source = NULL;
  agent->keepalive_timer_source = NULL;
1091
  agent->refresh_list = NULL;
1092
  agent->media_after_tick = FALSE;
1093
  agent->software_attribute = NULL;
1094

1095
  agent->compatibility = NICE_COMPATIBILITY_RFC5245;
Youness Alaoui's avatar
Youness Alaoui committed
1096 1097
  agent->use_ice_udp = TRUE;
  agent->use_ice_tcp = TRUE;
1098

Dafydd Harries's avatar
Dafydd Harries committed
1099
  agent->rng = nice_rng_new ();
1100
  priv_generate_tie_breaker (agent);
1101 1102

  g_queue_init (&agent->pending_signals);
Dafydd Harries's avatar
Dafydd Harries committed
1103 1104 1105
}


1106
NICEAPI_EXPORT NiceAgent *
1107
nice_agent_new (GMainContext *ctx, NiceCompatibility compat)
Dafydd Harries's avatar
Dafydd Harries committed
1108
{
1109 1110
  NiceAgent *agent = g_object_new (NICE_TYPE_AGENT,
      "compatibility", compat,
1111
      "main-context", ctx,
1112 1113 1114 1115 1116
      NULL);

  return agent;
}

Dafydd Harries's avatar
Dafydd Harries committed
1117

1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131
NICEAPI_EXPORT NiceAgent *
nice_agent_new_regular_nomination (GMainContext *ctx, NiceCompatibility compat)
{
  NiceAgent *agent = g_object_new (NICE_TYPE_AGENT,
      "compatibility", compat,
      "main-context", ctx,
      "reliable", FALSE,
      "nomination-mode", NICE_NOMINATION_MODE_REGULAR,
      NULL);

  return agent;
}


1132 1133 1134
NICEAPI_EXPORT NiceAgent *
nice_agent_new_full (GMainContext *ctx,
  NiceCompatibility compat,
1135
  NiceAgentOption flags)
1136 1137 1138 1139
{
  NiceAgent *agent = g_object_new (NICE_TYPE_AGENT,
      "compatibility", compat,
      "main-context", ctx,
1140 1141 <