agent.c 198 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 "iostream.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 86
#define MAX_TCP_MTU 1400 /* Use 1400 because of VPNs and we assume IEE 802.3 */

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

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

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


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

138 139 140 141 142
  N_SIGNALS,
};

static guint signals[N_SIGNALS];

143
static GMutex agent_mutex;    /* Mutex used for thread-safe lib */
144

145
static void priv_stop_upnp (NiceAgent *agent);
Youness Alaoui's avatar
Youness Alaoui committed
146

147 148 149 150 151 152 153
static void pseudo_tcp_socket_opened (PseudoTcpSocket *sock, gpointer user_data);
static void pseudo_tcp_socket_readable (PseudoTcpSocket *sock, gpointer user_data);
static void pseudo_tcp_socket_writable (PseudoTcpSocket *sock, gpointer user_data);
static void pseudo_tcp_socket_closed (PseudoTcpSocket *sock, guint32 err,
    gpointer user_data);
static PseudoTcpWriteResult pseudo_tcp_socket_write_packet (PseudoTcpSocket *sock,
    const gchar *buffer, guint32 len, gpointer user_data);
154
static void adjust_tcp_clock (NiceAgent *agent, NiceStream *stream, NiceComponent *component);
155 156 157 158 159 160 161

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);

162 163
void agent_lock (void)
{
164
  g_mutex_lock (&agent_mutex);
165 166 167 168
}

void agent_unlock (void)
{
169
  g_mutex_unlock (&agent_mutex);
170 171
}

172 173 174 175 176 177
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 ()

178 179 180 181 182 183 184 185 186 187 188 189
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
190 191
  g_value_unset (&sig->params[0]);

192
  for (i = 0; i < sig->query.n_params; i++) {
193
    if (G_VALUE_HOLDS(&sig->params[i + 1], NICE_TYPE_AGENT_STREAM_IDS))
Olivier Crête's avatar
Olivier Crête committed
194 195
      g_free (g_value_get_pointer (&sig->params[i + 1]));
    g_value_unset (&sig->params[i + 1]);
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 244 245 246 247 248 249 250 251 252 253 254 255 256
  }

  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);
}


257 258 259
StunUsageIceCompatibility
agent_to_ice_compatibility (NiceAgent *agent)
{
260
  return agent->compatibility == NICE_COMPATIBILITY_GOOGLE ?
261 262
      STUN_USAGE_ICE_COMPATIBILITY_GOOGLE :
      agent->compatibility == NICE_COMPATIBILITY_MSN ?
263
      STUN_USAGE_ICE_COMPATIBILITY_MSN :
Jakub Adam's avatar
Jakub Adam committed
264
      agent->compatibility == NICE_COMPATIBILITY_WLM2009 ?
265
      STUN_USAGE_ICE_COMPATIBILITY_MSICE2 :
Jakub Adam's avatar
Jakub Adam committed
266 267
      agent->compatibility == NICE_COMPATIBILITY_OC2007 ?
      STUN_USAGE_ICE_COMPATIBILITY_MSN :
Jakub Adam's avatar
Jakub Adam committed
268
      agent->compatibility == NICE_COMPATIBILITY_OC2007R2 ?
269
      STUN_USAGE_ICE_COMPATIBILITY_MSICE2 :
270
      STUN_USAGE_ICE_COMPATIBILITY_RFC5245;
271 272 273 274 275 276
}


StunUsageTurnCompatibility
agent_to_turn_compatibility (NiceAgent *agent)
{
277
  return agent->compatibility == NICE_COMPATIBILITY_GOOGLE ?
278 279
      STUN_USAGE_TURN_COMPATIBILITY_GOOGLE :
      agent->compatibility == NICE_COMPATIBILITY_MSN ?
280 281
      STUN_USAGE_TURN_COMPATIBILITY_MSN :
      agent->compatibility == NICE_COMPATIBILITY_WLM2009 ?
Jakub Adam's avatar
Jakub Adam committed
282 283
      STUN_USAGE_TURN_COMPATIBILITY_MSN :
      agent->compatibility == NICE_COMPATIBILITY_OC2007 ?
284 285 286
      STUN_USAGE_TURN_COMPATIBILITY_OC2007 :
      agent->compatibility == NICE_COMPATIBILITY_OC2007R2 ?
      STUN_USAGE_TURN_COMPATIBILITY_OC2007 :
Marcus Lundblad's avatar
Marcus Lundblad committed
287
      STUN_USAGE_TURN_COMPATIBILITY_RFC5766;
288 289
}

290 291 292
NiceTurnSocketCompatibility
agent_to_turn_socket_compatibility (NiceAgent *agent)
{
293
  return agent->compatibility == NICE_COMPATIBILITY_GOOGLE ?
294 295 296
      NICE_TURN_SOCKET_COMPATIBILITY_GOOGLE :
      agent->compatibility == NICE_COMPATIBILITY_MSN ?
      NICE_TURN_SOCKET_COMPATIBILITY_MSN :
297 298
      agent->compatibility == NICE_COMPATIBILITY_WLM2009 ?
      NICE_TURN_SOCKET_COMPATIBILITY_MSN :
Jakub Adam's avatar
Jakub Adam committed
299
      agent->compatibility == NICE_COMPATIBILITY_OC2007 ?
300 301 302
      NICE_TURN_SOCKET_COMPATIBILITY_OC2007 :
      agent->compatibility == NICE_COMPATIBILITY_OC2007R2 ?
      NICE_TURN_SOCKET_COMPATIBILITY_OC2007 :
Marcus Lundblad's avatar
Marcus Lundblad committed
303
      NICE_TURN_SOCKET_COMPATIBILITY_RFC5766;
304 305
}

306
NiceStream *agent_find_stream (NiceAgent *agent, guint stream_id)
307 308 309 310 311
{
  GSList *i;

  for (i = agent->streams; i; i = i->next)
    {
312
      NiceStream *s = i->data;
313 314 315 316 317 318 319 320 321

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

  return NULL;
}


322 323
gboolean
agent_find_component (
324 325 326
  NiceAgent *agent,
  guint stream_id,
  guint component_id,
327
  NiceStream **stream,
328
  NiceComponent **component)
329
{
330
  NiceStream *s;
331
  NiceComponent *c;
332

333
  s = agent_find_stream (agent, stream_id);
334

335
  if (s == NULL)
336 337
    return FALSE;

338
  c = nice_stream_find_component_by_id (s, component_id);
339 340 341 342

  if (c == NULL)
    return FALSE;

343 344 345 346
  if (stream)
    *stream = s;

  if (component)
347
    *component = c;
348 349 350 351

  return TRUE;
}

Dafydd Harries's avatar
Dafydd Harries committed
352 353 354 355 356 357 358 359 360
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;

361
  /* install properties */
362 363 364 365 366 367
  /**
   * 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.
   */
368 369 370 371 372 373 374
  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));

375 376 377 378 379 380 381
  /**
   * NiceAgent:compatibility:
   *
   * The Nice agent can work in various compatibility modes depending on
   * what the application/peer needs.
   * <para> See also: #NiceCompatibility</para>
   */
382 383 384 385 386
  g_object_class_install_property (gobject_class, PROP_COMPATIBILITY,
      g_param_spec_uint (
         "compatibility",
         "ICE specification compatibility",
         "The compatibility mode for the agent",
387 388
         NICE_COMPATIBILITY_RFC5245, NICE_COMPATIBILITY_LAST,
         NICE_COMPATIBILITY_RFC5245,
389 390
         G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));

Dafydd Harries's avatar
Dafydd Harries committed
391
  g_object_class_install_property (gobject_class, PROP_STUN_SERVER,
392
      g_param_spec_string (
Dafydd Harries's avatar
Dafydd Harries committed
393
        "stun-server",
394 395
        "STUN server IP address",
        "The IP address (not the hostname) of the STUN server to use",
396
        NULL,
Dafydd Harries's avatar
Dafydd Harries committed
397 398
        G_PARAM_READWRITE));

399 400 401 402
  g_object_class_install_property (gobject_class, PROP_STUN_SERVER_PORT,
      g_param_spec_uint (
        "stun-server-port",
        "STUN server port",
403
        "Port of the STUN server used to gather server-reflexive candidates",
Youness Alaoui's avatar
Youness Alaoui committed
404
        1, 65536,
405
	1, /* not a construct property, ignored */
406 407
        G_PARAM_READWRITE));

408 409 410 411 412 413 414
  /**
   * NiceAgent:controlling-mode:
   *
   * Whether the agent has the controlling role. This property should
   * be modified before gathering candidates, any modification occuring
   * later will be hold until ICE is restarted.
   */
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
  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));

431 432 433 434
  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
435 436
        "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
437
        1, 0xffffffff,
438
	NICE_AGENT_TIMER_TA_DEFAULT,
439
        G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
440

441
  /* note: according to spec recommendation in sect 5.7.3 (ID-19) */
442 443 444 445 446
  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
447
        0, 0xffffffff,
448 449 450
	0, /* default set in init */
        G_PARAM_READWRITE));

451 452 453 454 455 456 457
  /**
   * 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>
   *
458
   * Since: 0.1.15
459 460 461 462 463 464 465 466 467 468
   */
  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));

469 470 471 472 473 474 475
  /**
   * NiceAgent:proxy-ip:
   *
   * The proxy server IP used to bypass a proxy firewall
   *
   * Since: 0.0.4
   */
476 477 478 479
  g_object_class_install_property (gobject_class, PROP_PROXY_IP,
      g_param_spec_string (
        "proxy-ip",
        "Proxy server IP",
480
        "The proxy server IP used to bypass a proxy firewall",
481 482 483
        NULL,
        G_PARAM_READWRITE));

484 485 486 487 488 489 490
  /**
   * NiceAgent:proxy-port:
   *
   * The proxy server port used to bypass a proxy firewall
   *
   * Since: 0.0.4
   */
491 492 493 494
  g_object_class_install_property (gobject_class, PROP_PROXY_PORT,
      g_param_spec_uint (
        "proxy-port",
        "Proxy server port",
495
        "The Proxy server port used to bypass a proxy firewall",
496 497 498 499
        1, 65536,
	1,
        G_PARAM_READWRITE));

500 501 502 503 504 505 506
  /**
   * NiceAgent:proxy-type:
   *
   * The type of proxy set in the proxy-ip property
   *
   * Since: 0.0.4
   */
507 508 509 510 511 512 513 514 515
  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));

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

531 532 533 534 535 536 537
  /**
   * NiceAgent:proxy-password:
   *
   * The password used to authenticate with the proxy
   *
   * Since: 0.0.4
   */
538 539 540 541 542 543 544 545
  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));

546 547 548 549 550 551 552 553
  /**
   * 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
554 555 556
   g_object_class_install_property (gobject_class, PROP_UPNP,
      g_param_spec_boolean (
        "upnp",
557
#ifdef HAVE_GUPNP
Youness Alaoui's avatar
Youness Alaoui committed
558 559 560
        "Use UPnP",
        "Whether the agent should use UPnP to open a port in the router and "
        "get the external IP",
561 562 563 564
#else
        "Use UPnP (disabled in build)",
        "Does nothing because libnice was not built with UPnP support",
#endif
Youness Alaoui's avatar
Youness Alaoui committed
565 566 567
	TRUE, /* enable UPnP by default */
        G_PARAM_READWRITE| G_PARAM_CONSTRUCT));

568 569 570
  /**
   * NiceAgent:upnp-timeout:
   *
571 572
   * The maximum amount of time (in milliseconds) to wait for UPnP discovery to
   * finish before signaling the #NiceAgent::candidate-gathering-done signal
573 574 575
   *
   * Since: 0.0.7
   */
Youness Alaoui's avatar
Youness Alaoui committed
576 577 578
  g_object_class_install_property (gobject_class, PROP_UPNP_TIMEOUT,
      g_param_spec_uint (
        "upnp-timeout",
579
#ifdef HAVE_GUPNP
Youness Alaoui's avatar
Youness Alaoui committed
580 581 582
        "Timeout for UPnP discovery",
        "The maximum amount of time to wait for UPnP discovery to finish before "
        "signaling the candidate-gathering-done signal",
583 584 585 586
#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
587
        100, 60000,
588
	DEFAULT_UPNP_TIMEOUT,
Youness Alaoui's avatar
Youness Alaoui committed
589 590
        G_PARAM_READWRITE | G_PARAM_CONSTRUCT));

Youness Alaoui's avatar
Youness Alaoui committed
591 592 593
  /**
   * NiceAgent:reliable:
   *
594 595
   * Whether the agent is providing a reliable transport of messages (through
   * ICE-TCP or PseudoTCP over ICE-UDP)
Youness Alaoui's avatar
Youness Alaoui committed
596 597 598
   *
   * Since: 0.0.11
   */
599 600 601 602
   g_object_class_install_property (gobject_class, PROP_RELIABLE,
      g_param_spec_boolean (
        "reliable",
        "reliable mode",
603
        "Whether the agent provides a reliable transport of messages",
604 605 606
	FALSE,
        G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));

607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622
  /**
   * 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
623
   * Since: 0.1.8
624
   */
Youness Alaoui's avatar
Youness Alaoui committed
625 626 627 628 629 630 631
   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));
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647

  /**
   * 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.
648 649 650 651 652 653 654
   * <note>
   <para>
   ICE-TCP is only supported for %NICE_COMPATIBILITY_RFC5245,
   %NICE_COMPATIBILITY_OC2007 and %NICE_COMPATIBILITY_OC2007R2 compatibility
   modes.
   </para>
   * </note>
655
   *
Olivier Crête's avatar
Olivier Crête committed
656
   * Since: 0.1.8
657
   */
Youness Alaoui's avatar
Youness Alaoui committed
658 659 660 661 662 663 664
   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));
665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688

  /**
   * 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
689
   * Since: 0.1.8
690 691 692 693 694 695 696 697 698
   */
   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));

699 700 701 702 703 704 705 706 707 708 709 710 711
  /**
   * 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
712
   * Since: 0.1.8
713 714 715 716 717 718 719 720 721 722
   */
   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));

723 724 725 726 727 728 729 730
   /**
   * 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
731
   * Since: 0.1.14
732 733 734 735 736 737 738 739 740
   */
   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));

741 742 743 744 745 746 747 748 749 750 751 752 753
   /**
   * 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.
   *
754
   * Since: 0.1.15
755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778
   */

   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).
   *
779
   * Since: 0.1.15
780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797
   */

   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.
   *
798
   * Since: 0.1.15
799 800 801 802 803 804 805 806 807 808 809 810
   */

   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));

811 812
  /* install signals */

813 814
  /**
   * NiceAgent::component-state-changed
Youness Alaoui's avatar
Youness Alaoui committed
815
   * @agent: The #NiceAgent object
816 817
   * @stream_id: The ID of the stream
   * @component_id: The ID of the component
818
   * @state: The new #NiceComponentState of the component
819
   *
820 821 822 823
   * This signal is fired whenever a component’s state changes. There are many
   * valid state transitions.
   *
   * ![State transition diagram](states.png)
824
   */
825 826 827 828
  signals[SIGNAL_COMPONENT_STATE_CHANGED] =
      g_signal_new (
          "component-state-changed",
          G_OBJECT_CLASS_TYPE (klass),
829
          G_SIGNAL_RUN_LAST,
830 831 832
          0,
          NULL,
          NULL,
833
          NULL,
834 835 836 837
          G_TYPE_NONE,
          3,
          G_TYPE_UINT, G_TYPE_UINT, G_TYPE_UINT,
          G_TYPE_INVALID);
838

839 840
  /**
   * NiceAgent::candidate-gathering-done:
Youness Alaoui's avatar
Youness Alaoui committed
841
   * @agent: The #NiceAgent object
842 843 844 845 846
   * @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()
   */
847 848 849 850
  signals[SIGNAL_CANDIDATE_GATHERING_DONE] =
      g_signal_new (
          "candidate-gathering-done",
          G_OBJECT_CLASS_TYPE (klass),
851
          G_SIGNAL_RUN_LAST,
852 853 854
          0,
          NULL,
          NULL,
855
          NULL,
856
          G_TYPE_NONE,
857 858
          1,
          G_TYPE_UINT, G_TYPE_INVALID);
859

860 861
  /**
   * NiceAgent::new-selected-pair
Youness Alaoui's avatar
Youness Alaoui committed
862
   * @agent: The #NiceAgent object
863 864 865 866 867
   * @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
   *
868 869 870 871 872 873
   * 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
874
   * Deprecated: 0.1.8: Use #NiceAgent::new-selected-pair-full
875
   */
876 877 878 879
  signals[SIGNAL_NEW_SELECTED_PAIR] =
      g_signal_new (
          "new-selected-pair",
          G_OBJECT_CLASS_TYPE (klass),
880
          G_SIGNAL_RUN_LAST,
881 882 883
          0,
          NULL,
          NULL,
884
          NULL,
885 886 887 888 889
          G_TYPE_NONE,
          4,
          G_TYPE_UINT, G_TYPE_UINT, G_TYPE_STRING, G_TYPE_STRING,
          G_TYPE_INVALID);

890 891
  /**
   * NiceAgent::new-candidate
Youness Alaoui's avatar
Youness Alaoui committed
892
   * @agent: The #NiceAgent object
893 894 895 896
   * @stream_id: The ID of the stream
   * @component_id: The ID of the component
   * @foundation: The foundation of the new candidate
   *
897
   * This signal is fired when the agent discovers a new local candidate.
898 899 900 901 902
   * 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
903
   * Deprecated: 0.1.8: Use #NiceAgent::new-candidate-full
904
   */
905 906 907 908
  signals[SIGNAL_NEW_CANDIDATE] =
      g_signal_new (
          "new-candidate",
          G_OBJECT_CLASS_TYPE (klass),
909
          G_SIGNAL_RUN_LAST,
910 911 912
          0,
          NULL,
          NULL,
913
          NULL,
914 915 916 917 918
          G_TYPE_NONE,
          3,
          G_TYPE_UINT, G_TYPE_UINT, G_TYPE_STRING,
          G_TYPE_INVALID);

919 920
  /**
   * NiceAgent::new-remote-candidate
Youness Alaoui's avatar
Youness Alaoui committed
921
   * @agent: The #NiceAgent object
922 923 924 925
   * @stream_id: The ID of the stream
   * @component_id: The ID of the component
   * @foundation: The foundation of the new candidate
   *
926 927 928 929 930 931 932
   * 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
933
   * Deprecated: 0.1.8: Use #NiceAgent::new-remote-candidate-full
934
   */
935 936 937 938
  signals[SIGNAL_NEW_REMOTE_CANDIDATE] =
      g_signal_new (
          "new-remote-candidate",
          G_OBJECT_CLASS_TYPE (klass),
939
          G_SIGNAL_RUN_LAST,
940 941 942
          0,
          NULL,
          NULL,
943
          NULL,
944 945 946 947 948
          G_TYPE_NONE,
          3,
          G_TYPE_UINT, G_TYPE_UINT, G_TYPE_STRING,
          G_TYPE_INVALID);

949 950
  /**
   * NiceAgent::initial-binding-request-received
Youness Alaoui's avatar
Youness Alaoui committed
951
   * @agent: The #NiceAgent object
952 953 954 955 956
   * @stream_id: The ID of the stream
   *
   * This signal is fired when we received our first binding request from
   * the peer.
   */
957 958 959 960
  signals[SIGNAL_INITIAL_BINDING_REQUEST_RECEIVED] =
      g_signal_new (
          "initial-binding-request-received",
          G_OBJECT_CLASS_TYPE (klass),
961
          G_SIGNAL_RUN_LAST,
962 963 964
          0,
          NULL,
          NULL,
965
          NULL,
966 967 968 969 970
          G_TYPE_NONE,
          1,
          G_TYPE_UINT,
          G_TYPE_INVALID);

971 972
  /**
   * NiceAgent::reliable-transport-writable
Youness Alaoui's avatar
Youness Alaoui committed
973
   * @agent: The #NiceAgent object
974 975 976 977 978 979 980 981
   * @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.
982 983
   *
   * Since: 0.0.11
984 985 986 987 988 989 990 991 992
   */
  signals[SIGNAL_RELIABLE_TRANSPORT_WRITABLE] =
      g_signal_new (
          "reliable-transport-writable",
          G_OBJECT_CLASS_TYPE (klass),
          G_SIGNAL_RUN_LAST,
          0,
          NULL,
          NULL,
993
          NULL,
994 995 996 997 998
          G_TYPE_NONE,
          2,
          G_TYPE_UINT, G_TYPE_UINT,
          G_TYPE_INVALID);

999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
  /**
   * 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,
1021
          NICE_TYPE_AGENT_STREAM_IDS,
1022
          G_TYPE_INVALID);
1023

1024

1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037
  /**
   * 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
1038
   * Since: 0.1.8
1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052
   */
  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);

1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063
  /**
   * 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
1064
   * Since: 0.1.8
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
   */
  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
1091
   * Since: 0.1.8
1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106
   */
  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);

1107 1108
  /* Init debug options depending on env variables */
  nice_debug_init ();
Dafydd Harries's avatar
Dafydd Harries committed
1109 1110
}

Rohan Garg's avatar
Rohan Garg committed
1111
static void priv_generate_tie_breaker (NiceAgent *agent)
1112 1113 1114
{
  nice_rng_generate_bytes (agent->rng, 8, (gchar*)&agent->tie_breaker);
}
Dafydd Harries's avatar
Dafydd Harries committed
1115

1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156
static void
priv_update_controlling_mode (NiceAgent *agent, gboolean value)
{
  gboolean update_controlling_mode;
  GSList *i, *j;

  agent->saved_controlling_mode = value;
  /* It is safe to update the agent controlling mode when all
   * components are still in state disconnected. When we leave
   * this state, the role must stay under the control of the
   * conncheck algorithm exclusively, until the conncheck is
   * eventually restarted. See RFC5245, sect 5.2. Determining Role
   */
  if (agent->controlling_mode != agent->saved_controlling_mode) {
    update_controlling_mode = TRUE;
    for (i = agent->streams;
         i && update_controlling_mode; i = i->next) {
      NiceStream *stream = i->data;
      for (j = stream->components;
           j && update_controlling_mode; j = j->next) {
        NiceComponent *component = j->data;
        if (component->state > NICE_COMPONENT_STATE_DISCONNECTED)
          update_controlling_mode = FALSE;
      }
    }
    if (update_controlling_mode) {
      agent->controlling_mode = agent->saved_controlling_mode;
      nice_debug ("Agent %p : Property set, changing role to \"%s\".",
        agent, agent->controlling_mode ? "controlling" : "controlled");
    } else {
      nice_debug ("Agent %p : Property set, role switch requested "
        "but conncheck already started.", agent);
      nice_debug ("Agent %p : Property set, staying with role \"%s\" "
        "until restart.", agent,
        agent->controlling_mode ? "controlling" : "controlled");
    }
  } else
    nice_debug ("Agent %p : Property set, role is already \"%s\".", agent,
      agent->controlling_mode ? "controlling" : "controlled");
}

Dafydd Harries's avatar
Dafydd Harries committed
1157 1158 1159 1160 1161
static void
nice_agent_init (NiceAgent *agent)
{
  agent->next_candidate_id = 1;
  agent->next_stream_id = 1;
1162

1163
  /* set defaults; not construct params, so set here */
1164
  agent->stun_server_port = DEFAULT_STUN_PORT;
1165
  agent->controlling_mode = TRUE;
1166
  agent->saved_controlling_mode = TRUE;
1167
  agent->max_conn_checks = NICE_AGENT_MAX_CONNECTIVITY_CHECKS_DEFAULT;
1168
  agent->nomination_mode = NICE_NOMINATION_MODE_AGGRESSIVE;
1169 1170

  agent->discovery_list = NULL;
1171
  agent->discovery_unsched_items = 0;
1172 1173 1174
  agent->discovery_timer_source = NULL;
  agent->conncheck_timer_source = NULL;
  agent->keepalive_timer_source = NULL;
1175
  agent->refresh_list = NULL;
1176
  agent->media_after_tick = FALSE;
1177
  agent->software_attribute = NULL;
1178

1179
  agent->compatibility = NICE_COMPATIBILITY_RFC5245;
1180
  agent->reliable = FALSE;
Youness Alaoui's avatar
Youness Alaoui committed
1181 1182
  agent->use_ice_udp = TRUE;
  agent->use_ice_tcp = TRUE;
1183

Dafydd Harries's avatar
Dafydd Harries committed
1184
  agent->rng = nice_rng_new ();
1185
  priv_generate_tie_breaker (agent);
1186 1187

  g_queue_init (&agent->pending_signals);
Dafydd Harries's avatar
Dafydd Harries committed
1188 1189 1190
}


1191
NICEAPI_EXPORT NiceAgent *
1192
nice_agent_new (GMainContext *ctx, NiceCompatibility compat)
Dafydd Harries's avatar
Dafydd Harries committed
1193
{
1194 1195
  NiceAgent *agent = g_object_new (NICE_TYPE_AGENT,
      "compatibility", compat,
1196
      "main-context", ctx,
1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210
      "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
1211
      NULL);
1212 1213

  return agent;
Dafydd Harries's avatar
Dafydd Harries committed
1214 1215 1216
}


1217 1218 1219
NICEAPI_EXPORT NiceAgent *
nice_agent_new_full (GMainContext *ctx,
  NiceCompatibility compat,
1220
  NiceAgentOption flags)
1221 1222 1223 1224
{
  NiceAgent *agent = g_object_new (NICE_TYPE_AGENT,
      "compatibility", compat,
      "main-context", ctx,
1225 1226 1227 1228
      "reliable", (flags & NICE_AGENT_OPTION_RELIABLE) ? TRUE : FALSE,
      "nomination-mode", (flags & NICE_AGENT_OPTION_REGULAR_NOMINATION) ?
      NICE_NOMINATION_MODE_REGULAR : NICE_NOMINATION_MODE_AGGRESSIVE,
      "full-mode", (flags & NICE_AGENT_OPTION_LITE_MODE) ? FALSE : TRUE,
1229 1230 1231 1232 1233 1234
      NULL);

  return agent;
}


Dafydd Harries's avatar
Dafydd Harries committed
1235 1236 1237 1238 1239 1240 1241 1242
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
1243

1244
  agent_lock();
Youness Alaoui's avatar
Youness Alaoui committed
1245

Dafydd Harries's avatar
Dafydd Harries committed
1246 1247
  switch (property_id)
    {
1248 1249 1250 1251
    case PROP_MAIN_CONTEXT:
      g_value_set_pointer (value, agent->main_context);
      break;

1252 1253 1254 1255
    case PROP_COMPATIBILITY:
      g_value_set_uint (value, agent->compatibility);
      break;

Dafydd Harries's avatar
Dafydd Harries committed
1256
    case PROP_STUN_SERVER:
1257
      g_value_set_string (value, agent->stun_server_ip);
1258
      break;
1259 1260 1261

    case PROP_STUN_SERVER_PORT:
      g_value_set_uint (value, agent->stun_server_port);
1262
      break;
1263

1264
    case PROP_CONTROLLING_MODE:
1265
      g_value_set_boolean (value, agent->saved_controlling_mode);
1266 1267 1268 1269 1270
      break;

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

1272 1273 1274 1275
    case PROP_STUN_PACING_TIMER:
      g_value_set_uint (value, agent->timer_ta);
      break;

1276 1277 1278 1279 1280
    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;

1281 1282 1283 1284
    case PROP_NOMINATION_MODE:
      g_value_set_enum (value, agent->nomination_mode);
      break;

1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304
    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
1305
    case PROP_UPNP:
1306
#ifdef HAVE_GUPNP
Youness Alaoui's avatar
Youness Alaoui committed
1307
      g_value_set_boolean (value, agent->upnp_enabled);
1308 1309 1310
#else
      g_value_set_boolean (value, FALSE);
#endif
Youness Alaoui's avatar
Youness Alaoui committed
1311 1312 1313
      break;

    case PROP_UPNP_TIMEOUT:
1314
#ifdef HAVE_GUPNP
Youness Alaoui's avatar
Youness Alaoui committed
1315
      g_value_set_uint (value, agent->upnp_timeout);
1316 1317
#else
      g_value_set_uint (value, DEFAULT_UPNP_TIMEOUT);
Youness Alaoui's avatar
Youness Alaoui committed
1318
#endif
1319
      break;
Youness Alaoui's avatar
Youness Alaoui committed
1320

1321 1322 1323 1324
    case PROP_RELIABLE:
      g_value_set_boolean (value, agent->reliable);
      break;

Youness Alaoui's avatar
Youness Alaoui committed
1325 1326 1327 1328 1329 1330 1331 1332
    case PROP_ICE_UDP:
      g_value_set_boolean (value, agent->use_ice_udp);
      break;

    case PROP_ICE_TCP:
      g_value_set_boolean (value, agent->use_ice_tcp);
      break;

1333 1334 1335 1336 1337 1338 1339 1340 1341 1342
    case PROP_BYTESTREAM_TCP:
      if (agent->reliable) {
        if (agent->compatibility == NICE_COMPATIBILITY_GOOGLE)
          g_value_set_boolean (value, TRUE);
        else
          g_value_set_boolean (value, FALSE);
      } else {
        g_value_set_boolean (value, FALSE);
      }
      break;
1343 1344 1345 1346 1347 1348 1349 1350

    case PROP_KEEPALIVE_CONNCHECK:
      if (agent->compatibility == NICE_COMPATIBILITY_GOOGLE)
        g_value_set_boolean (value, TRUE);
      else
        g_value_set_boolean (value, agent->keepalive_conncheck);
      break;

1351 1352 1353 1354
    case PROP_FORCE_RELAY:
      g_value_set_boolean (value, agent->force_relay);
      break;

1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366
    case PROP_STUN_MAX_RETRANSMISSIONS:
      g_value_set_uint (value, agent->stun_max_retransmissions);
      break;

    case PROP_STUN_INITIAL_TIMEOUT:
      g_value_set_uint (value, agent->stun_initial_timeout);
      break;

    case PROP_STUN_RELIABLE_TIMEOUT:
      g_value_set_uint (value, agent->stun_reliable_timeout);
      break;

Dafydd Harries's avatar
Dafydd Harries committed
1367 1368 1369
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
    }
Youness Alaoui's avatar
Youness Alaoui committed
1370

1371
  agent_unlock_and_emit(agent);
Dafydd Harries's avatar
Dafydd Harries committed
1372 1373
}

1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388
void
nice_agent_init_stun_agent (NiceAgent *agent, StunAgent *stun_agent)
{
  if (agent->compatibility == NICE_COMPATIBILITY_GOOGLE) {
    stun_agent_init (stun_agent, STUN_ALL_KNOWN_ATTRIBUTES,
        STUN_COMPATIBILITY_RFC3489,
        STUN_AGENT_USAGE_SHORT_TERM_CREDENTIALS |
        STUN_AGENT_USAGE_IGNORE_CREDENTIALS);
  } else if (agent->compatibility == NICE_COMPATIBILITY_MSN) {
    stun_agent_init (stun_agent, STUN_ALL_KNOWN_ATTRIBUTES,
        STUN_COMPATIBILITY_RFC3489,
        STUN_AGENT_USAGE_SHORT_TERM_CREDENTIALS |
        STUN_AGENT_USAGE_FORCE_VALIDATER);
  } else if (agent->compatibility == NICE_COMPATIBILITY_WLM2009) {
    stun_agent_init (stun_agent, STUN_ALL_KNOWN_ATTRIBUTES,
1389
        STUN_COMPATIBILITY_MSICE2,
1390 1391 1392 1393 1394 1395 1396 1397 1398 1399
        STUN_AGENT_USAGE_SHORT_TERM_CREDENTIALS |
        STUN_AGENT_USAGE_USE_FINGERPRINT);
  } else if (agent->compatibility == NICE_COMPATIBILITY_OC2007) {
    stun_agent_init (stun_agent, STUN_ALL_KNOWN_ATTRIBUTES,
        STUN_COMPATIBILITY_RFC3489,
        STUN_AGENT_USAGE_SHORT_TERM_CREDENTIALS |
        STUN_AGENT_USAGE_FORCE_VALIDATER |
        STUN_AGENT_USAGE_NO_ALIGNED_ATTRIBUTES);
  } else if (agent->compatibility == NICE_COMPATIBILITY_OC2007R2) {
    stun_agent_init (stun_agent, STUN_ALL_KNOWN_ATTRIBUTES,
1400
        STUN_COMPATIBILITY_MSICE2,
1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419
        STUN_AGENT_USAGE_SHORT_TERM_CREDENTIALS |
        STUN_AGENT_USAGE_USE_FINGERPRINT |
        STUN_AGENT_USAGE_NO_ALIGNED_ATTRIBUTES);
  } else {
    stun_agent_init (stun_agent, STUN_ALL_KNOWN_ATTRIBUTES,
        STUN_COMPATIBILITY_RFC5389,
        STUN_AGENT_USAGE_SHORT_TERM_CREDENTIALS |
        STUN_AGENT_USAGE_USE_FINGERPRINT);
  }
  stun_agent_set_software (stun_agent, agent->software_attribute);
}

static void
nice_agent_reset_all_stun_agents (NiceAgent *agent, gboolean only_software)
{
  GSList *stream_item, *component_item;

  for (stream_item = agent->streams; stream_item;
       stream_item = stream_item->next) {
1420
    NiceStream *stream = stream_item->data;
1421 1422 1423

    for (component_item = stream->components; component_item;
         component_item = component_item->next) {
1424
      NiceComponent *component = component_item->data;
1425 1426 1427 1428 1429 1430 1431 1432 1433

      if (only_software)
        stun_agent_set_software (&component->stun_agent,
            agent->software_attribute);
      else
        nice_agent_init_stun_agent(agent, &component->stun_agent);
    }
  }
}
Dafydd Harries's avatar
Dafydd Harries committed
1434 1435 1436 1437 1438 1439 1440 1441 1442 1443

static void
nice_agent_set_property (
<