agent.c 201 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 void priv_stop_upnp (NiceAgent *agent);
Youness Alaoui's avatar
Youness Alaoui committed
144

145 146 147 148 149 150 151
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);
152
static void adjust_tcp_clock (NiceAgent *agent, NiceStream *stream, NiceComponent *component);
153 154 155 156 157 158 159

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

160
void agent_lock (NiceAgent *agent)
161
{
162
  g_mutex_lock (&agent->agent_mutex);
163 164
}

165
void agent_unlock (NiceAgent *agent)
166
{
167
  g_mutex_unlock (&agent->agent_mutex);
168 169
}

170 171 172 173 174 175
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 ()

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

190
  for (i = 0; i < sig->query.n_params; i++) {
191
    if (G_VALUE_HOLDS(&sig->params[i + 1], NICE_TYPE_AGENT_STREAM_IDS))
Olivier Crête's avatar
Olivier Crête committed
192 193
      g_free (g_value_get_pointer (&sig->params[i + 1]));
    g_value_unset (&sig->params[i + 1]);
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
  }

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

209
  agent_unlock (agent);
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

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


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


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

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

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

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

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

  return NULL;
}


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

331
  s = agent_find_stream (agent, stream_id);
332

333
  if (s == NULL)
334 335
    return FALSE;

336
  c = nice_stream_find_component_by_id (s, component_id);
337 338 339 340

  if (c == NULL)
    return FALSE;

341 342 343 344
  if (stream)
    *stream = s;

  if (component)
345
    *component = c;
346 347 348 349

  return TRUE;
}

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

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

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

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

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

406 407 408 409 410 411 412
  /**
   * 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.
   */
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
  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));

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

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

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

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

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

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

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

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

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

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

Youness Alaoui's avatar
Youness Alaoui committed
589 590 591
  /**
   * NiceAgent:reliable:
   *
592 593
   * 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
594 595 596
   *
   * Since: 0.0.11
   */
597 598 599 600
   g_object_class_install_property (gobject_class, PROP_RELIABLE,
      g_param_spec_boolean (
        "reliable",
        "reliable mode",
601
        "Whether the agent provides a reliable transport of messages",
602 603 604
	FALSE,
        G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));

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

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

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

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

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

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

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

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

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

809 810
  /* install signals */

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

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

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

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

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

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

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

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

1022

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

1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061
  /**
   * 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
1062
   * Since: 0.1.8
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088
   */
  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
1089
   * Since: 0.1.8
1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104
   */
  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);

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

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

1114 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
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
1155 1156 1157 1158 1159
static void
nice_agent_init (NiceAgent *agent)
{
  agent->next_candidate_id = 1;
  agent->next_stream_id = 1;
1160

1161
  /* set defaults; not construct params, so set here */
1162
  agent->stun_server_port = DEFAULT_STUN_PORT;