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

40
41
42

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

47
48
#include <glib.h>

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

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

60
61
#include "debug.h"

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

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

75
76
#include "pseudotcp.h"

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

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

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

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


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

static guint signals[N_SIGNALS];

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

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

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

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

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

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



145
146
147
StunUsageIceCompatibility
agent_to_ice_compatibility (NiceAgent *agent)
{
148
  return agent->compatibility == NICE_COMPATIBILITY_GOOGLE ?
149
150
      STUN_USAGE_ICE_COMPATIBILITY_GOOGLE :
      agent->compatibility == NICE_COMPATIBILITY_MSN ?
151
      STUN_USAGE_ICE_COMPATIBILITY_MSN :
Jakub Adam's avatar
Jakub Adam committed
152
153
      agent->compatibility == NICE_COMPATIBILITY_WLM2009 ?
      STUN_USAGE_ICE_COMPATIBILITY_WLM2009 :
Jakub Adam's avatar
Jakub Adam committed
154
155
      agent->compatibility == NICE_COMPATIBILITY_OC2007 ?
      STUN_USAGE_ICE_COMPATIBILITY_MSN :
Jakub Adam's avatar
Jakub Adam committed
156
157
      agent->compatibility == NICE_COMPATIBILITY_OC2007R2 ?
      STUN_USAGE_ICE_COMPATIBILITY_WLM2009 :
158
      STUN_USAGE_ICE_COMPATIBILITY_RFC5245;
159
160
161
162
163
164
}


StunUsageTurnCompatibility
agent_to_turn_compatibility (NiceAgent *agent)
{
165
  return agent->compatibility == NICE_COMPATIBILITY_GOOGLE ?
166
167
      STUN_USAGE_TURN_COMPATIBILITY_GOOGLE :
      agent->compatibility == NICE_COMPATIBILITY_MSN ?
168
169
      STUN_USAGE_TURN_COMPATIBILITY_MSN :
      agent->compatibility == NICE_COMPATIBILITY_WLM2009 ?
Jakub Adam's avatar
Jakub Adam committed
170
171
      STUN_USAGE_TURN_COMPATIBILITY_MSN :
      agent->compatibility == NICE_COMPATIBILITY_OC2007 ?
172
173
174
175
      STUN_USAGE_TURN_COMPATIBILITY_OC2007 :
      agent->compatibility == NICE_COMPATIBILITY_OC2007R2 ?
      STUN_USAGE_TURN_COMPATIBILITY_OC2007 :
      STUN_USAGE_TURN_COMPATIBILITY_DRAFT9;
176
177
}

178
179
180
NiceTurnSocketCompatibility
agent_to_turn_socket_compatibility (NiceAgent *agent)
{
181
  return agent->compatibility == NICE_COMPATIBILITY_GOOGLE ?
182
183
184
      NICE_TURN_SOCKET_COMPATIBILITY_GOOGLE :
      agent->compatibility == NICE_COMPATIBILITY_MSN ?
      NICE_TURN_SOCKET_COMPATIBILITY_MSN :
185
186
      agent->compatibility == NICE_COMPATIBILITY_WLM2009 ?
      NICE_TURN_SOCKET_COMPATIBILITY_MSN :
Jakub Adam's avatar
Jakub Adam committed
187
      agent->compatibility == NICE_COMPATIBILITY_OC2007 ?
188
189
190
      NICE_TURN_SOCKET_COMPATIBILITY_OC2007 :
      agent->compatibility == NICE_COMPATIBILITY_OC2007R2 ?
      NICE_TURN_SOCKET_COMPATIBILITY_OC2007 :
191
192
193
      NICE_TURN_SOCKET_COMPATIBILITY_DRAFT9;
}

194
Stream *agent_find_stream (NiceAgent *agent, guint stream_id)
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
{
  GSList *i;

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

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

  return NULL;
}


210
211
gboolean
agent_find_component (
212
213
214
215
216
217
218
  NiceAgent *agent,
  guint stream_id,
  guint component_id,
  Stream **stream,
  Component **component)
{
  Stream *s;
219
  Component *c;
220

221
  s = agent_find_stream (agent, stream_id);
222

223
  if (s == NULL)
224
225
    return FALSE;

226
227
228
229
230
  c = stream_find_component_by_id (s, component_id);

  if (c == NULL)
    return FALSE;

231
232
233
234
  if (stream)
    *stream = s;

  if (component)
235
    *component = c;
236
237
238
239
240

  return TRUE;
}


Dafydd Harries's avatar
Dafydd Harries committed
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
static void
nice_agent_dispose (GObject *object);

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

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


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

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

268
  /* install properties */
269
270
271
272
273
274
  /**
   * 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.
   */
275
276
277
278
279
280
281
  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));

282
283
284
285
286
287
288
  /**
   * NiceAgent:compatibility:
   *
   * The Nice agent can work in various compatibility modes depending on
   * what the application/peer needs.
   * <para> See also: #NiceCompatibility</para>
   */
289
290
291
292
293
  g_object_class_install_property (gobject_class, PROP_COMPATIBILITY,
      g_param_spec_uint (
         "compatibility",
         "ICE specification compatibility",
         "The compatibility mode for the agent",
294
295
         NICE_COMPATIBILITY_RFC5245, NICE_COMPATIBILITY_LAST,
         NICE_COMPATIBILITY_RFC5245,
296
297
         G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));

Dafydd Harries's avatar
Dafydd Harries committed
298
  g_object_class_install_property (gobject_class, PROP_STUN_SERVER,
299
      g_param_spec_string (
Dafydd Harries's avatar
Dafydd Harries committed
300
        "stun-server",
301
302
        "STUN server IP address",
        "The IP address (not the hostname) of the STUN server to use",
303
        NULL,
Dafydd Harries's avatar
Dafydd Harries committed
304
305
        G_PARAM_READWRITE));

306
307
308
309
  g_object_class_install_property (gobject_class, PROP_STUN_SERVER_PORT,
      g_param_spec_uint (
        "stun-server-port",
        "STUN server port",
310
        "Port of the STUN server used to gather server-reflexive candidates",
Youness Alaoui's avatar
Youness Alaoui committed
311
        1, 65536,
312
	1, /* not a construct property, ignored */
313
314
        G_PARAM_READWRITE));

315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
  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));

331
332
333
334
  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
335
336
        "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
337
        1, 0xffffffff,
338
339
340
	NICE_AGENT_TIMER_TA_DEFAULT,
        G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));

341
  /* note: according to spec recommendation in sect 5.7.3 (ID-19) */
342
343
344
345
346
  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
347
        0, 0xffffffff,
348
349
350
	0, /* default set in init */
        G_PARAM_READWRITE));

351
352
353
354
355
356
357
  /**
   * NiceAgent:proxy-ip:
   *
   * The proxy server IP used to bypass a proxy firewall
   *
   * Since: 0.0.4
   */
358
359
360
361
  g_object_class_install_property (gobject_class, PROP_PROXY_IP,
      g_param_spec_string (
        "proxy-ip",
        "Proxy server IP",
362
        "The proxy server IP used to bypass a proxy firewall",
363
364
365
        NULL,
        G_PARAM_READWRITE));

366
367
368
369
370
371
372
  /**
   * NiceAgent:proxy-port:
   *
   * The proxy server port used to bypass a proxy firewall
   *
   * Since: 0.0.4
   */
373
374
375
376
  g_object_class_install_property (gobject_class, PROP_PROXY_PORT,
      g_param_spec_uint (
        "proxy-port",
        "Proxy server port",
377
        "The Proxy server port used to bypass a proxy firewall",
378
379
380
381
        1, 65536,
	1,
        G_PARAM_READWRITE));

382
383
384
385
386
387
388
  /**
   * NiceAgent:proxy-type:
   *
   * The type of proxy set in the proxy-ip property
   *
   * Since: 0.0.4
   */
389
390
391
392
393
394
395
396
397
  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));

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

413
414
415
416
417
418
419
  /**
   * NiceAgent:proxy-password:
   *
   * The password used to authenticate with the proxy
   *
   * Since: 0.0.4
   */
420
421
422
423
424
425
426
427
  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));

428
429
430
431
432
433
434
435
  /**
   * 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
436
437
438
   g_object_class_install_property (gobject_class, PROP_UPNP,
      g_param_spec_boolean (
        "upnp",
439
#ifdef HAVE_GUPNP
Youness Alaoui's avatar
Youness Alaoui committed
440
441
442
        "Use UPnP",
        "Whether the agent should use UPnP to open a port in the router and "
        "get the external IP",
443
444
445
446
#else
        "Use UPnP (disabled in build)",
        "Does nothing because libnice was not built with UPnP support",
#endif
Youness Alaoui's avatar
Youness Alaoui committed
447
448
449
	TRUE, /* enable UPnP by default */
        G_PARAM_READWRITE| G_PARAM_CONSTRUCT));

450
451
452
453
  /**
   * NiceAgent:upnp-timeout:
   *
   * The maximum amount of time to wait for UPnP discovery to finish before
454
   * signaling the #NiceAgent::candidate-gathering-done signal
455
456
457
   *
   * Since: 0.0.7
   */
Youness Alaoui's avatar
Youness Alaoui committed
458
459
460
  g_object_class_install_property (gobject_class, PROP_UPNP_TIMEOUT,
      g_param_spec_uint (
        "upnp-timeout",
461
#ifdef HAVE_GUPNP
Youness Alaoui's avatar
Youness Alaoui committed
462
463
464
        "Timeout for UPnP discovery",
        "The maximum amount of time to wait for UPnP discovery to finish before "
        "signaling the candidate-gathering-done signal",
465
466
467
468
#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
469
        100, 60000,
470
	DEFAULT_UPNP_TIMEOUT,
Youness Alaoui's avatar
Youness Alaoui committed
471
472
        G_PARAM_READWRITE | G_PARAM_CONSTRUCT));

Youness Alaoui's avatar
Youness Alaoui committed
473
474
475
476
477
478
479
480
  /**
   * NiceAgent:reliable:
   *
   * Whether the agent should use PseudoTcp to ensure a reliable transport
   * of messages
   *
   * Since: 0.0.11
   */
481
482
483
484
   g_object_class_install_property (gobject_class, PROP_RELIABLE,
      g_param_spec_boolean (
        "reliable",
        "reliable mode",
Youness Alaoui's avatar
Youness Alaoui committed
485
        "Whether the agent should use PseudoTcp to ensure a reliable transport"
486
487
488
489
        "of messages",
	FALSE,
        G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));

490
491
  /* install signals */

492
493
  /**
   * NiceAgent::component-state-changed
Youness Alaoui's avatar
Youness Alaoui committed
494
   * @agent: The #NiceAgent object
495
496
497
498
499
500
   * @stream_id: The ID of the stream
   * @component_id: The ID of the component
   * @state: The #NiceComponentState of the component
   *
   * This signal is fired whenever a component's state changes
   */
501
502
503
504
  signals[SIGNAL_COMPONENT_STATE_CHANGED] =
      g_signal_new (
          "component-state-changed",
          G_OBJECT_CLASS_TYPE (klass),
505
          G_SIGNAL_RUN_LAST,
506
507
508
509
510
511
512
513
          0,
          NULL,
          NULL,
          agent_marshal_VOID__UINT_UINT_UINT,
          G_TYPE_NONE,
          3,
          G_TYPE_UINT, G_TYPE_UINT, G_TYPE_UINT,
          G_TYPE_INVALID);
514

515
516
  /**
   * NiceAgent::candidate-gathering-done:
Youness Alaoui's avatar
Youness Alaoui committed
517
   * @agent: The #NiceAgent object
518
519
520
521
522
   * @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()
   */
523
524
525
526
  signals[SIGNAL_CANDIDATE_GATHERING_DONE] =
      g_signal_new (
          "candidate-gathering-done",
          G_OBJECT_CLASS_TYPE (klass),
527
          G_SIGNAL_RUN_LAST,
528
529
530
          0,
          NULL,
          NULL,
531
          agent_marshal_VOID__UINT,
532
          G_TYPE_NONE,
533
534
          1,
          G_TYPE_UINT, G_TYPE_INVALID);
535

536
537
  /**
   * NiceAgent::new-selected-pair
Youness Alaoui's avatar
Youness Alaoui committed
538
   * @agent: The #NiceAgent object
539
540
541
542
543
544
545
546
   * @stream_id: The ID of the stream
   * @component_id: The ID of the component
   * @lfoundation: The local foundation of the selected candidate pair
   * @rfoundation: The remote foundation of the selected candidate pair
   *
   * This signal is fired once a candidate pair is selected for data transfer for
   * a stream's component
   */
547
548
549
550
  signals[SIGNAL_NEW_SELECTED_PAIR] =
      g_signal_new (
          "new-selected-pair",
          G_OBJECT_CLASS_TYPE (klass),
551
          G_SIGNAL_RUN_LAST,
552
553
554
555
556
557
558
559
560
          0,
          NULL,
          NULL,
          agent_marshal_VOID__UINT_UINT_STRING_STRING,
          G_TYPE_NONE,
          4,
          G_TYPE_UINT, G_TYPE_UINT, G_TYPE_STRING, G_TYPE_STRING,
          G_TYPE_INVALID);

561
562
  /**
   * NiceAgent::new-candidate
Youness Alaoui's avatar
Youness Alaoui committed
563
   * @agent: The #NiceAgent object
564
565
566
567
568
   * @stream_id: The ID of the stream
   * @component_id: The ID of the component
   * @foundation: The foundation of the new candidate
   *
   * This signal is fired when the agent discovers a new candidate
569
   * <para> See also: #NiceAgent::candidate-gathering-done </para>
570
   */
571
572
573
574
  signals[SIGNAL_NEW_CANDIDATE] =
      g_signal_new (
          "new-candidate",
          G_OBJECT_CLASS_TYPE (klass),
575
          G_SIGNAL_RUN_LAST,
576
577
578
579
580
581
582
583
584
          0,
          NULL,
          NULL,
          agent_marshal_VOID__UINT_UINT_STRING,
          G_TYPE_NONE,
          3,
          G_TYPE_UINT, G_TYPE_UINT, G_TYPE_STRING,
          G_TYPE_INVALID);

585
586
  /**
   * NiceAgent::new-remote-candidate
Youness Alaoui's avatar
Youness Alaoui committed
587
   * @agent: The #NiceAgent object
588
589
590
591
592
593
594
   * @stream_id: The ID of the stream
   * @component_id: The ID of the component
   * @foundation: The foundation of the new candidate
   *
   * This signal is fired when the agent discovers a new remote candidate.
   * This can happen with peer reflexive candidates.
   */
595
596
597
598
  signals[SIGNAL_NEW_REMOTE_CANDIDATE] =
      g_signal_new (
          "new-remote-candidate",
          G_OBJECT_CLASS_TYPE (klass),
599
          G_SIGNAL_RUN_LAST,
600
601
602
603
604
605
606
607
608
          0,
          NULL,
          NULL,
          agent_marshal_VOID__UINT_UINT_STRING,
          G_TYPE_NONE,
          3,
          G_TYPE_UINT, G_TYPE_UINT, G_TYPE_STRING,
          G_TYPE_INVALID);

609
610
  /**
   * NiceAgent::initial-binding-request-received
Youness Alaoui's avatar
Youness Alaoui committed
611
   * @agent: The #NiceAgent object
612
613
614
615
616
   * @stream_id: The ID of the stream
   *
   * This signal is fired when we received our first binding request from
   * the peer.
   */
617
618
619
620
  signals[SIGNAL_INITIAL_BINDING_REQUEST_RECEIVED] =
      g_signal_new (
          "initial-binding-request-received",
          G_OBJECT_CLASS_TYPE (klass),
621
          G_SIGNAL_RUN_LAST,
622
623
624
625
626
627
628
629
630
          0,
          NULL,
          NULL,
          agent_marshal_VOID__UINT,
          G_TYPE_NONE,
          1,
          G_TYPE_UINT,
          G_TYPE_INVALID);

631
632
  /**
   * NiceAgent::reliable-transport-writable
Youness Alaoui's avatar
Youness Alaoui committed
633
   * @agent: The #NiceAgent object
634
635
636
637
638
639
640
641
   * @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.
642
643
   *
   * Since: 0.0.11
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
   */
  signals[SIGNAL_RELIABLE_TRANSPORT_WRITABLE] =
      g_signal_new (
          "reliable-transport-writable",
          G_OBJECT_CLASS_TYPE (klass),
          G_SIGNAL_RUN_LAST,
          0,
          NULL,
          NULL,
          agent_marshal_VOID__UINT_UINT,
          G_TYPE_NONE,
          2,
          G_TYPE_UINT, G_TYPE_UINT,
          G_TYPE_INVALID);


660
661
  /* Init debug options depending on env variables */
  nice_debug_init ();
Dafydd Harries's avatar
Dafydd Harries committed
662
663
}

664
665
666
667
static void priv_generate_tie_breaker (NiceAgent *agent) 
{
  nice_rng_generate_bytes (agent->rng, 8, (gchar*)&agent->tie_breaker);
}
Dafydd Harries's avatar
Dafydd Harries committed
668
669
670
671
672
673

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

675
  /* set defaults; not construct params, so set here */
676
  agent->stun_server_port = DEFAULT_STUN_PORT;
677
  agent->controlling_mode = TRUE;
678
  agent->max_conn_checks = NICE_AGENT_MAX_CONNECTIVITY_CHECKS_DEFAULT;
679
680

  agent->discovery_list = NULL;
681
  agent->discovery_unsched_items = 0;
682
683
684
  agent->discovery_timer_source = NULL;
  agent->conncheck_timer_source = NULL;
  agent->keepalive_timer_source = NULL;
685
  agent->refresh_list = NULL;
686
  agent->media_after_tick = FALSE;
687
  agent->software_attribute = NULL;
688

689
  agent->compatibility = NICE_COMPATIBILITY_RFC5245;
690
  agent->reliable = FALSE;
691

692
  stun_agent_init (&agent->stun_agent, STUN_ALL_KNOWN_ATTRIBUTES,
693
      STUN_COMPATIBILITY_RFC5389,
694
695
696
      STUN_AGENT_USAGE_SHORT_TERM_CREDENTIALS |
      STUN_AGENT_USAGE_USE_FINGERPRINT);

Dafydd Harries's avatar
Dafydd Harries committed
697
  agent->rng = nice_rng_new ();
698
  priv_generate_tie_breaker (agent);
Dafydd Harries's avatar
Dafydd Harries committed
699
700
701
}


702
NICEAPI_EXPORT NiceAgent *
703
nice_agent_new (GMainContext *ctx, NiceCompatibility compat)
Dafydd Harries's avatar
Dafydd Harries committed
704
{
705
706
  NiceAgent *agent = g_object_new (NICE_TYPE_AGENT,
      "compatibility", compat,
707
      "main-context", ctx,
708
709
710
711
712
713
714
715
716
717
718
719
720
721
      "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
722
      NULL);
723
724

  return agent;
Dafydd Harries's avatar
Dafydd Harries committed
725
726
727
728
729
730
731
732
733
734
735
}


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
736

737
  agent_lock();
Youness Alaoui's avatar
Youness Alaoui committed
738

Dafydd Harries's avatar
Dafydd Harries committed
739
740
  switch (property_id)
    {
741
742
743
744
    case PROP_MAIN_CONTEXT:
      g_value_set_pointer (value, agent->main_context);
      break;

745
746
747
748
    case PROP_COMPATIBILITY:
      g_value_set_uint (value, agent->compatibility);
      break;

Dafydd Harries's avatar
Dafydd Harries committed
749
    case PROP_STUN_SERVER:
750
      g_value_set_string (value, agent->stun_server_ip);
751
      break;
752
753
754

    case PROP_STUN_SERVER_PORT:
      g_value_set_uint (value, agent->stun_server_port);
755
      break;
756

757
758
759
760
761
762
763
    case PROP_CONTROLLING_MODE:
      g_value_set_boolean (value, agent->controlling_mode);
      break;

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

765
766
767
768
    case PROP_STUN_PACING_TIMER:
      g_value_set_uint (value, agent->timer_ta);
      break;

769
770
771
772
773
    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;

774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
    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
794
    case PROP_UPNP:
795
#ifdef HAVE_GUPNP
Youness Alaoui's avatar
Youness Alaoui committed
796
      g_value_set_boolean (value, agent->upnp_enabled);
797
798
799
#else
      g_value_set_boolean (value, FALSE);
#endif
Youness Alaoui's avatar
Youness Alaoui committed
800
801
802
      break;

    case PROP_UPNP_TIMEOUT:
803
#ifdef HAVE_GUPNP
Youness Alaoui's avatar
Youness Alaoui committed
804
      g_value_set_uint (value, agent->upnp_timeout);
805
806
#else
      g_value_set_uint (value, DEFAULT_UPNP_TIMEOUT);
Youness Alaoui's avatar
Youness Alaoui committed
807
#endif
808
      break;
Youness Alaoui's avatar
Youness Alaoui committed
809

810
811
812
813
    case PROP_RELIABLE:
      g_value_set_boolean (value, agent->reliable);
      break;

Dafydd Harries's avatar
Dafydd Harries committed
814
815
816
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
    }
Youness Alaoui's avatar
Youness Alaoui committed
817

818
  agent_unlock();
Dafydd Harries's avatar
Dafydd Harries committed
819
820
821
822
823
824
825
826
827
828
829
830
}


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

831
  agent_lock();
Youness Alaoui's avatar
Youness Alaoui committed
832

Dafydd Harries's avatar
Dafydd Harries committed
833
834
  switch (property_id)
    {
835
836
    case PROP_MAIN_CONTEXT:
      agent->main_context = g_value_get_pointer (value);
837
838
      if (agent->main_context != NULL)
        g_main_context_ref (agent->main_context);
839
840
      break;

841
842
    case PROP_COMPATIBILITY:
      agent->compatibility = g_value_get_uint (value);
843
      if (agent->compatibility == NICE_COMPATIBILITY_GOOGLE) {
844
845
846
847
        stun_agent_init (&agent->stun_agent, STUN_ALL_KNOWN_ATTRIBUTES,
            STUN_COMPATIBILITY_RFC3489,
            STUN_AGENT_USAGE_SHORT_TERM_CREDENTIALS |
            STUN_AGENT_USAGE_IGNORE_CREDENTIALS);
848
      } else if (agent->compatibility == NICE_COMPATIBILITY_MSN) {
Youness Alaoui's avatar
Youness Alaoui committed
849
850
        stun_agent_init (&agent->stun_agent, STUN_ALL_KNOWN_ATTRIBUTES,
            STUN_COMPATIBILITY_RFC3489,
851
852
            STUN_AGENT_USAGE_SHORT_TERM_CREDENTIALS |
            STUN_AGENT_USAGE_FORCE_VALIDATER);
853
854
855
856
      } else if (agent->compatibility == NICE_COMPATIBILITY_WLM2009) {
        stun_agent_init (&agent->stun_agent, STUN_ALL_KNOWN_ATTRIBUTES,
            STUN_COMPATIBILITY_WLM2009,
            STUN_AGENT_USAGE_SHORT_TERM_CREDENTIALS |
Jakub Adam's avatar
Jakub Adam committed
857
            STUN_AGENT_USAGE_USE_FINGERPRINT);
858
859
860
861
862
863
      } else if (agent->compatibility == NICE_COMPATIBILITY_OC2007) {
        stun_agent_init (&agent->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);
Jakub Adam's avatar
Jakub Adam committed
864
865
866
867
      } else if (agent->compatibility == NICE_COMPATIBILITY_OC2007R2) {
        stun_agent_init (&agent->stun_agent, STUN_ALL_KNOWN_ATTRIBUTES,
            STUN_COMPATIBILITY_WLM2009,
            STUN_AGENT_USAGE_SHORT_TERM_CREDENTIALS |
868
869
            STUN_AGENT_USAGE_USE_FINGERPRINT |
            STUN_AGENT_USAGE_NO_ALIGNED_ATTRIBUTES);
870
871
872
873
874
      } else {
        stun_agent_init (&agent->stun_agent, STUN_ALL_KNOWN_ATTRIBUTES,
            STUN_COMPATIBILITY_RFC5389,
            STUN_AGENT_USAGE_SHORT_TERM_CREDENTIALS |
            STUN_AGENT_USAGE_USE_FINGERPRINT);
875
      }
876
      stun_agent_set_software (&agent->stun_agent, agent->software_attribute);
877

878
879
      break;

Dafydd Harries's avatar
Dafydd Harries committed
880
    case PROP_STUN_SERVER:
881
      g_free (agent->stun_server_ip);
882
883
884
885
886
887
888
      agent->stun_server_ip = g_value_dup_string (value);
      break;

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

889
890
891
892
893
894
895
896
    case PROP_CONTROLLING_MODE:
      agent->controlling_mode = g_value_get_boolean (value);
      break;

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

897
898
899
900
    case PROP_STUN_PACING_TIMER:
      agent->timer_ta = g_value_get_uint (value);
      break;

901
902
903
904
    case PROP_MAX_CONNECTIVITY_CHECKS:
      agent->max_conn_checks = g_value_get_uint (value);
      break;

905
    case PROP_PROXY_IP:
906
      g_free (agent->proxy_ip);
907
908
909
910
911
912
913
914
915
916
917
918
      agent->proxy_ip = g_value_dup_string (value);
      break;

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

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

    case PROP_PROXY_USERNAME:
919
      g_free (agent->proxy_username);
920
921
922
923
      agent->proxy_username = g_value_dup_string (value);
      break;

    case PROP_PROXY_PASSWORD:
924
      g_free (agent->proxy_password);
925
926
927
      agent->proxy_password = g_value_dup_string (value);
      break;

Youness Alaoui's avatar
Youness Alaoui committed
928
    case PROP_UPNP_TIMEOUT:
929
#ifdef HAVE_GUPNP
Youness Alaoui's avatar
Youness Alaoui committed
930
      agent->upnp_timeout = g_value_get_uint (value);
931
#endif
Youness Alaoui's avatar
Youness Alaoui committed
932
933
934
      break;

    case PROP_UPNP:
935
#ifdef HAVE_GUPNP
Youness Alaoui's avatar
Youness Alaoui committed
936
937
      agent->upnp_enabled = g_value_get_boolean (value);
#endif
938
      break;
939
940
941
942
943

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

Dafydd Harries's avatar
Dafydd Harries committed
944
945
946
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
    }
Youness Alaoui's avatar
Youness Alaoui committed
947

948
  agent_unlock();
Youness Alaoui's avatar
Youness Alaoui committed
949

Dafydd Harries's avatar
Dafydd Harries committed
950
951
}

952

953
954
955
956
957
958
959
960
961
962
963
964
static void priv_destroy_component_tcp (Component *component)
{
    if (component->tcp_clock) {
      g_source_destroy (component->tcp_clock);
      g_source_unref (component->tcp_clock);
      component->tcp_clock = NULL;
    }
    if (component->tcp) {
      pseudo_tcp_socket_close (component->tcp, TRUE);
      g_object_unref (component->tcp);
      component->tcp = NULL;
    }
965
966
967
968
    if (component->tcp_data != NULL) {
      g_slice_free (TcpUserData, component->tcp_data);
      component->tcp_data = NULL;
    }
969
970
971
972
973
974
975
976
977
978
}

static void priv_pseudo_tcp_error (NiceAgent *agent, Stream *stream,
    Component *component)
{
  if (component->tcp) {
    agent_signal_component_state_change (agent, stream->id,
        component->id, NICE_COMPONENT_STATE_FAILED);
    priv_detach_stream_component (stream, component);
  }
979
  priv_destroy_component_tcp (component);
980
981
982
983
}

static void
adjust_tcp_clock (NiceAgent *agent, Stream *stream, Component *component);
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000


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

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

static void