agent.c 80.2 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
#ifdef G_OS_WIN32
53
54
#  include <winsock2.h>
#  define EWOULDBLOCK WSAEWOULDBLOCK
55
#else
56
57
58
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
59
#endif
Dafydd Harries's avatar
Dafydd Harries committed
60

61
62
#include "debug.h"

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

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

76
77
#include "pseudotcp.h"

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

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

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

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


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

static guint signals[N_SIGNALS];

124
125
126
127
128
#if GLIB_CHECK_VERSION(2,31,8)
static GRecMutex agent_mutex;    /* Mutex used for thread-safe lib */
#else
static GStaticRecMutex agent_mutex = G_STATIC_REC_MUTEX_INIT;
#endif
129

130
131
static gboolean priv_attach_stream_component (NiceAgent *agent,
    Stream *stream,
132
    Component *component);
Youness Alaoui's avatar
Youness Alaoui committed
133
static void priv_detach_stream_component (Stream *stream, Component *component);
134

Youness Alaoui's avatar
Youness Alaoui committed
135
136
static void priv_free_upnp (NiceAgent *agent);

137
#if GLIB_CHECK_VERSION(2,31,8)
138
139
void agent_lock (void)
{
140
  g_rec_mutex_lock (&agent_mutex);
141
142
143
144
}

void agent_unlock (void)
{
145
146
147
148
149
150
151
  g_rec_mutex_unlock (&agent_mutex);
}

#else
void agent_lock(void)
{
  g_static_rec_mutex_lock (&agent_mutex);
152
153
}

154
155
156
157
void agent_unlock(void)
{
  g_static_rec_mutex_unlock (&agent_mutex);
}
158

159
#endif
160

161
162
163
StunUsageIceCompatibility
agent_to_ice_compatibility (NiceAgent *agent)
{
164
  return agent->compatibility == NICE_COMPATIBILITY_GOOGLE ?
165
166
      STUN_USAGE_ICE_COMPATIBILITY_GOOGLE :
      agent->compatibility == NICE_COMPATIBILITY_MSN ?
167
      STUN_USAGE_ICE_COMPATIBILITY_MSN :
Jakub Adam's avatar
Jakub Adam committed
168
169
      agent->compatibility == NICE_COMPATIBILITY_WLM2009 ?
      STUN_USAGE_ICE_COMPATIBILITY_WLM2009 :
Jakub Adam's avatar
Jakub Adam committed
170
171
      agent->compatibility == NICE_COMPATIBILITY_OC2007 ?
      STUN_USAGE_ICE_COMPATIBILITY_MSN :
Jakub Adam's avatar
Jakub Adam committed
172
173
      agent->compatibility == NICE_COMPATIBILITY_OC2007R2 ?
      STUN_USAGE_ICE_COMPATIBILITY_WLM2009 :
174
      STUN_USAGE_ICE_COMPATIBILITY_RFC5245;
175
176
177
178
179
180
}


StunUsageTurnCompatibility
agent_to_turn_compatibility (NiceAgent *agent)
{
181
  return agent->compatibility == NICE_COMPATIBILITY_GOOGLE ?
182
183
      STUN_USAGE_TURN_COMPATIBILITY_GOOGLE :
      agent->compatibility == NICE_COMPATIBILITY_MSN ?
184
185
      STUN_USAGE_TURN_COMPATIBILITY_MSN :
      agent->compatibility == NICE_COMPATIBILITY_WLM2009 ?
Jakub Adam's avatar
Jakub Adam committed
186
187
      STUN_USAGE_TURN_COMPATIBILITY_MSN :
      agent->compatibility == NICE_COMPATIBILITY_OC2007 ?
188
189
190
      STUN_USAGE_TURN_COMPATIBILITY_OC2007 :
      agent->compatibility == NICE_COMPATIBILITY_OC2007R2 ?
      STUN_USAGE_TURN_COMPATIBILITY_OC2007 :
Marcus Lundblad's avatar
Marcus Lundblad committed
191
      STUN_USAGE_TURN_COMPATIBILITY_RFC5766;
192
193
}

194
195
196
NiceTurnSocketCompatibility
agent_to_turn_socket_compatibility (NiceAgent *agent)
{
197
  return agent->compatibility == NICE_COMPATIBILITY_GOOGLE ?
198
199
200
      NICE_TURN_SOCKET_COMPATIBILITY_GOOGLE :
      agent->compatibility == NICE_COMPATIBILITY_MSN ?
      NICE_TURN_SOCKET_COMPATIBILITY_MSN :
201
202
      agent->compatibility == NICE_COMPATIBILITY_WLM2009 ?
      NICE_TURN_SOCKET_COMPATIBILITY_MSN :
Jakub Adam's avatar
Jakub Adam committed
203
      agent->compatibility == NICE_COMPATIBILITY_OC2007 ?
204
205
206
      NICE_TURN_SOCKET_COMPATIBILITY_OC2007 :
      agent->compatibility == NICE_COMPATIBILITY_OC2007R2 ?
      NICE_TURN_SOCKET_COMPATIBILITY_OC2007 :
Marcus Lundblad's avatar
Marcus Lundblad committed
207
      NICE_TURN_SOCKET_COMPATIBILITY_RFC5766;
208
209
}

210
Stream *agent_find_stream (NiceAgent *agent, guint stream_id)
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
{
  GSList *i;

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

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

  return NULL;
}


226
227
gboolean
agent_find_component (
228
229
230
231
232
233
234
  NiceAgent *agent,
  guint stream_id,
  guint component_id,
  Stream **stream,
  Component **component)
{
  Stream *s;
235
  Component *c;
236

237
  s = agent_find_stream (agent, stream_id);
238

239
  if (s == NULL)
240
241
    return FALSE;

242
243
244
245
246
  c = stream_find_component_by_id (s, component_id);

  if (c == NULL)
    return FALSE;

247
248
249
250
  if (stream)
    *stream = s;

  if (component)
251
    *component = c;
252
253
254
255
256

  return TRUE;
}


Dafydd Harries's avatar
Dafydd Harries committed
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
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;

284
  /* install properties */
285
286
287
288
289
290
  /**
   * 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.
   */
291
292
293
294
295
296
297
  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));

298
299
300
301
302
303
304
  /**
   * NiceAgent:compatibility:
   *
   * The Nice agent can work in various compatibility modes depending on
   * what the application/peer needs.
   * <para> See also: #NiceCompatibility</para>
   */
305
306
307
308
309
  g_object_class_install_property (gobject_class, PROP_COMPATIBILITY,
      g_param_spec_uint (
         "compatibility",
         "ICE specification compatibility",
         "The compatibility mode for the agent",
310
311
         NICE_COMPATIBILITY_RFC5245, NICE_COMPATIBILITY_LAST,
         NICE_COMPATIBILITY_RFC5245,
312
313
         G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));

Dafydd Harries's avatar
Dafydd Harries committed
314
  g_object_class_install_property (gobject_class, PROP_STUN_SERVER,
315
      g_param_spec_string (
Dafydd Harries's avatar
Dafydd Harries committed
316
        "stun-server",
317
318
        "STUN server IP address",
        "The IP address (not the hostname) of the STUN server to use",
319
        NULL,
Dafydd Harries's avatar
Dafydd Harries committed
320
321
        G_PARAM_READWRITE));

322
323
324
325
  g_object_class_install_property (gobject_class, PROP_STUN_SERVER_PORT,
      g_param_spec_uint (
        "stun-server-port",
        "STUN server port",
326
        "Port of the STUN server used to gather server-reflexive candidates",
Youness Alaoui's avatar
Youness Alaoui committed
327
        1, 65536,
328
	1, /* not a construct property, ignored */
329
330
        G_PARAM_READWRITE));

331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
  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));

347
348
349
350
  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
351
352
        "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
353
        1, 0xffffffff,
354
355
356
	NICE_AGENT_TIMER_TA_DEFAULT,
        G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));

357
  /* note: according to spec recommendation in sect 5.7.3 (ID-19) */
358
359
360
361
362
  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
363
        0, 0xffffffff,
364
365
366
	0, /* default set in init */
        G_PARAM_READWRITE));

367
368
369
370
371
372
373
  /**
   * NiceAgent:proxy-ip:
   *
   * The proxy server IP used to bypass a proxy firewall
   *
   * Since: 0.0.4
   */
374
375
376
377
  g_object_class_install_property (gobject_class, PROP_PROXY_IP,
      g_param_spec_string (
        "proxy-ip",
        "Proxy server IP",
378
        "The proxy server IP used to bypass a proxy firewall",
379
380
381
        NULL,
        G_PARAM_READWRITE));

382
383
384
385
386
387
388
  /**
   * NiceAgent:proxy-port:
   *
   * The proxy server port used to bypass a proxy firewall
   *
   * Since: 0.0.4
   */
389
390
391
392
  g_object_class_install_property (gobject_class, PROP_PROXY_PORT,
      g_param_spec_uint (
        "proxy-port",
        "Proxy server port",
393
        "The Proxy server port used to bypass a proxy firewall",
394
395
396
397
        1, 65536,
	1,
        G_PARAM_READWRITE));

398
399
400
401
402
403
404
  /**
   * NiceAgent:proxy-type:
   *
   * The type of proxy set in the proxy-ip property
   *
   * Since: 0.0.4
   */
405
406
407
408
409
410
411
412
413
  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));

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

429
430
431
432
433
434
435
  /**
   * NiceAgent:proxy-password:
   *
   * The password used to authenticate with the proxy
   *
   * Since: 0.0.4
   */
436
437
438
439
440
441
442
443
  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));

444
445
446
447
448
449
450
451
  /**
   * 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
452
453
454
   g_object_class_install_property (gobject_class, PROP_UPNP,
      g_param_spec_boolean (
        "upnp",
455
#ifdef HAVE_GUPNP
Youness Alaoui's avatar
Youness Alaoui committed
456
457
458
        "Use UPnP",
        "Whether the agent should use UPnP to open a port in the router and "
        "get the external IP",
459
460
461
462
#else
        "Use UPnP (disabled in build)",
        "Does nothing because libnice was not built with UPnP support",
#endif
Youness Alaoui's avatar
Youness Alaoui committed
463
464
465
	TRUE, /* enable UPnP by default */
        G_PARAM_READWRITE| G_PARAM_CONSTRUCT));

466
467
468
469
  /**
   * NiceAgent:upnp-timeout:
   *
   * The maximum amount of time to wait for UPnP discovery to finish before
470
   * signaling the #NiceAgent::candidate-gathering-done signal
471
472
473
   *
   * Since: 0.0.7
   */
Youness Alaoui's avatar
Youness Alaoui committed
474
475
476
  g_object_class_install_property (gobject_class, PROP_UPNP_TIMEOUT,
      g_param_spec_uint (
        "upnp-timeout",
477
#ifdef HAVE_GUPNP
Youness Alaoui's avatar
Youness Alaoui committed
478
479
480
        "Timeout for UPnP discovery",
        "The maximum amount of time to wait for UPnP discovery to finish before "
        "signaling the candidate-gathering-done signal",
481
482
483
484
#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
485
        100, 60000,
486
	DEFAULT_UPNP_TIMEOUT,
Youness Alaoui's avatar
Youness Alaoui committed
487
488
        G_PARAM_READWRITE | G_PARAM_CONSTRUCT));

Youness Alaoui's avatar
Youness Alaoui committed
489
490
491
492
493
494
495
496
  /**
   * NiceAgent:reliable:
   *
   * Whether the agent should use PseudoTcp to ensure a reliable transport
   * of messages
   *
   * Since: 0.0.11
   */
497
498
499
500
   g_object_class_install_property (gobject_class, PROP_RELIABLE,
      g_param_spec_boolean (
        "reliable",
        "reliable mode",
Youness Alaoui's avatar
Youness Alaoui committed
501
        "Whether the agent should use PseudoTcp to ensure a reliable transport"
502
503
504
505
        "of messages",
	FALSE,
        G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));

506
507
  /* install signals */

508
509
  /**
   * NiceAgent::component-state-changed
Youness Alaoui's avatar
Youness Alaoui committed
510
   * @agent: The #NiceAgent object
511
512
513
514
515
516
   * @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
   */
517
518
519
520
  signals[SIGNAL_COMPONENT_STATE_CHANGED] =
      g_signal_new (
          "component-state-changed",
          G_OBJECT_CLASS_TYPE (klass),
521
          G_SIGNAL_RUN_LAST,
522
523
524
525
526
527
528
529
          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);
530

531
532
  /**
   * NiceAgent::candidate-gathering-done:
Youness Alaoui's avatar
Youness Alaoui committed
533
   * @agent: The #NiceAgent object
534
535
536
537
538
   * @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()
   */
539
540
541
542
  signals[SIGNAL_CANDIDATE_GATHERING_DONE] =
      g_signal_new (
          "candidate-gathering-done",
          G_OBJECT_CLASS_TYPE (klass),
543
          G_SIGNAL_RUN_LAST,
544
545
546
          0,
          NULL,
          NULL,
547
          agent_marshal_VOID__UINT,
548
          G_TYPE_NONE,
549
550
          1,
          G_TYPE_UINT, G_TYPE_INVALID);
551

552
553
  /**
   * NiceAgent::new-selected-pair
Youness Alaoui's avatar
Youness Alaoui committed
554
   * @agent: The #NiceAgent object
555
556
557
558
559
560
561
562
   * @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
   */
563
564
565
566
  signals[SIGNAL_NEW_SELECTED_PAIR] =
      g_signal_new (
          "new-selected-pair",
          G_OBJECT_CLASS_TYPE (klass),
567
          G_SIGNAL_RUN_LAST,
568
569
570
571
572
573
574
575
576
          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);

577
578
  /**
   * NiceAgent::new-candidate
Youness Alaoui's avatar
Youness Alaoui committed
579
   * @agent: The #NiceAgent object
580
581
582
583
584
   * @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
585
   * <para> See also: #NiceAgent::candidate-gathering-done </para>
586
   */
587
588
589
590
  signals[SIGNAL_NEW_CANDIDATE] =
      g_signal_new (
          "new-candidate",
          G_OBJECT_CLASS_TYPE (klass),
591
          G_SIGNAL_RUN_LAST,
592
593
594
595
596
597
598
599
600
          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);

601
602
  /**
   * NiceAgent::new-remote-candidate
Youness Alaoui's avatar
Youness Alaoui committed
603
   * @agent: The #NiceAgent object
604
605
606
607
608
609
610
   * @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.
   */
611
612
613
614
  signals[SIGNAL_NEW_REMOTE_CANDIDATE] =
      g_signal_new (
          "new-remote-candidate",
          G_OBJECT_CLASS_TYPE (klass),
615
          G_SIGNAL_RUN_LAST,
616
617
618
619
620
621
622
623
624
          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);

625
626
  /**
   * NiceAgent::initial-binding-request-received
Youness Alaoui's avatar
Youness Alaoui committed
627
   * @agent: The #NiceAgent object
628
629
630
631
632
   * @stream_id: The ID of the stream
   *
   * This signal is fired when we received our first binding request from
   * the peer.
   */
633
634
635
636
  signals[SIGNAL_INITIAL_BINDING_REQUEST_RECEIVED] =
      g_signal_new (
          "initial-binding-request-received",
          G_OBJECT_CLASS_TYPE (klass),
637
          G_SIGNAL_RUN_LAST,
638
639
640
641
642
643
644
645
646
          0,
          NULL,
          NULL,
          agent_marshal_VOID__UINT,
          G_TYPE_NONE,
          1,
          G_TYPE_UINT,
          G_TYPE_INVALID);

647
648
  /**
   * NiceAgent::reliable-transport-writable
Youness Alaoui's avatar
Youness Alaoui committed
649
   * @agent: The #NiceAgent object
650
651
652
653
654
655
656
657
   * @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.
658
659
   *
   * Since: 0.0.11
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
   */
  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);


676
677
  /* Init debug options depending on env variables */
  nice_debug_init ();
Dafydd Harries's avatar
Dafydd Harries committed
678
679
}

680
681
682
683
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
684
685
686
687
688
689

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

691
  /* set defaults; not construct params, so set here */
692
  agent->stun_server_port = DEFAULT_STUN_PORT;
693
  agent->controlling_mode = TRUE;
694
  agent->max_conn_checks = NICE_AGENT_MAX_CONNECTIVITY_CHECKS_DEFAULT;
695
696

  agent->discovery_list = NULL;
697
  agent->discovery_unsched_items = 0;
698
699
700
  agent->discovery_timer_source = NULL;
  agent->conncheck_timer_source = NULL;
  agent->keepalive_timer_source = NULL;
701
  agent->refresh_list = NULL;
702
  agent->media_after_tick = FALSE;
703
  agent->software_attribute = NULL;
704

705
  agent->compatibility = NICE_COMPATIBILITY_RFC5245;
706
  agent->reliable = FALSE;
707

708
  stun_agent_init (&agent->stun_agent, STUN_ALL_KNOWN_ATTRIBUTES,
709
      STUN_COMPATIBILITY_RFC5389,
710
711
712
      STUN_AGENT_USAGE_SHORT_TERM_CREDENTIALS |
      STUN_AGENT_USAGE_USE_FINGERPRINT);

Dafydd Harries's avatar
Dafydd Harries committed
713
  agent->rng = nice_rng_new ();
714
  priv_generate_tie_breaker (agent);
Dafydd Harries's avatar
Dafydd Harries committed
715
716
717
}


718
NICEAPI_EXPORT NiceAgent *
719
nice_agent_new (GMainContext *ctx, NiceCompatibility compat)
Dafydd Harries's avatar
Dafydd Harries committed
720
{
721
722
  NiceAgent *agent = g_object_new (NICE_TYPE_AGENT,
      "compatibility", compat,
723
      "main-context", ctx,
724
725
726
727
728
729
730
731
732
733
734
735
736
737
      "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
738
      NULL);
739
740

  return agent;
Dafydd Harries's avatar
Dafydd Harries committed
741
742
743
744
745
746
747
748
749
750
751
}


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
752

753
  agent_lock();
Youness Alaoui's avatar
Youness Alaoui committed
754

Dafydd Harries's avatar
Dafydd Harries committed
755
756
  switch (property_id)
    {
757
758
759
760
    case PROP_MAIN_CONTEXT:
      g_value_set_pointer (value, agent->main_context);
      break;

761
762
763
764
    case PROP_COMPATIBILITY:
      g_value_set_uint (value, agent->compatibility);
      break;

Dafydd Harries's avatar
Dafydd Harries committed
765
    case PROP_STUN_SERVER:
766
      g_value_set_string (value, agent->stun_server_ip);
767
      break;
768
769
770

    case PROP_STUN_SERVER_PORT:
      g_value_set_uint (value, agent->stun_server_port);
771
      break;
772

773
774
775
776
777
778
779
    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
780

781
782
783
784
    case PROP_STUN_PACING_TIMER:
      g_value_set_uint (value, agent->timer_ta);
      break;

785
786
787
788
789
    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;

790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
    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
810
    case PROP_UPNP:
811
#ifdef HAVE_GUPNP
Youness Alaoui's avatar
Youness Alaoui committed
812
      g_value_set_boolean (value, agent->upnp_enabled);
813
814
815
#else
      g_value_set_boolean (value, FALSE);
#endif
Youness Alaoui's avatar
Youness Alaoui committed
816
817
818
      break;

    case PROP_UPNP_TIMEOUT:
819
#ifdef HAVE_GUPNP
Youness Alaoui's avatar
Youness Alaoui committed
820
      g_value_set_uint (value, agent->upnp_timeout);
821
822
#else
      g_value_set_uint (value, DEFAULT_UPNP_TIMEOUT);
Youness Alaoui's avatar
Youness Alaoui committed
823
#endif
824
      break;
Youness Alaoui's avatar
Youness Alaoui committed
825

826
827
828
829
    case PROP_RELIABLE:
      g_value_set_boolean (value, agent->reliable);
      break;

Dafydd Harries's avatar
Dafydd Harries committed
830
831
832
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
    }
Youness Alaoui's avatar
Youness Alaoui committed
833

834
  agent_unlock();
Dafydd Harries's avatar
Dafydd Harries committed
835
836
837
838
839
840
841
842
843
844
845
846
}


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

847
  agent_lock();
Youness Alaoui's avatar
Youness Alaoui committed
848

Dafydd Harries's avatar
Dafydd Harries committed
849
850
  switch (property_id)
    {
851
852
    case PROP_MAIN_CONTEXT:
      agent->main_context = g_value_get_pointer (value);
853
854
      if (agent->main_context != NULL)
        g_main_context_ref (agent->main_context);
855
856
      break;

857
858
    case PROP_COMPATIBILITY:
      agent->compatibility = g_value_get_uint (value);
859
      if (agent->compatibility == NICE_COMPATIBILITY_GOOGLE) {
860
861
862
863
        stun_agent_init (&agent->stun_agent, STUN_ALL_KNOWN_ATTRIBUTES,
            STUN_COMPATIBILITY_RFC3489,
            STUN_AGENT_USAGE_SHORT_TERM_CREDENTIALS |
            STUN_AGENT_USAGE_IGNORE_CREDENTIALS);
864
      } else if (agent->compatibility == NICE_COMPATIBILITY_MSN) {
Youness Alaoui's avatar
Youness Alaoui committed
865
866
        stun_agent_init (&agent->stun_agent, STUN_ALL_KNOWN_ATTRIBUTES,
            STUN_COMPATIBILITY_RFC3489,
867
868
            STUN_AGENT_USAGE_SHORT_TERM_CREDENTIALS |
            STUN_AGENT_USAGE_FORCE_VALIDATER);
869
870
871
872
      } 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
873
            STUN_AGENT_USAGE_USE_FINGERPRINT);
874
875
876
877
878
879
      } 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
880
881
882
883
      } 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 |
884
885
            STUN_AGENT_USAGE_USE_FINGERPRINT |
            STUN_AGENT_USAGE_NO_ALIGNED_ATTRIBUTES);
886
887
888
889
890
      } 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);
891
      }
892
      stun_agent_set_software (&agent->stun_agent, agent->software_attribute);
893

894
895
      break;

Dafydd Harries's avatar
Dafydd Harries committed
896
    case PROP_STUN_SERVER:
897
      g_free (agent->stun_server_ip);
898
899
900
901
902
903
904
      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;

905
906
907
908
909
910
911
912
    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;

913
914
915
916
    case PROP_STUN_PACING_TIMER:
      agent->timer_ta = g_value_get_uint (value);
      break;

917
918
919
920
    case PROP_MAX_CONNECTIVITY_CHECKS:
      agent->max_conn_checks = g_value_get_uint (value);
      break;

921
    case PROP_PROXY_IP:
922
      g_free (agent->proxy_ip);
923
924
925
926
927
928
929
930
931
932
933
934
      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:
935
      g_free (agent->proxy_username);
936
937
938
939
      agent->proxy_username = g_value_dup_string (value);
      break;

    case PROP_PROXY_PASSWORD:
940
      g_free (agent->proxy_password);
941
942
943
      agent->proxy_password = g_value_dup_string (value);
      break;

Youness Alaoui's avatar
Youness Alaoui committed
944
    case PROP_UPNP_TIMEOUT:
945
#ifdef HAVE_GUPNP
Youness Alaoui's avatar
Youness Alaoui committed
946
      agent->upnp_timeout = g_value_get_uint (value);
947
#endif
Youness Alaoui's avatar
Youness Alaoui committed
948
949
950
      break;

    case PROP_UPNP:
951
#ifdef HAVE_GUPNP
Youness Alaoui's avatar
Youness Alaoui committed
952
953
      agent->upnp_enabled = g_value_get_boolean (value);
#endif
954
      break;
955
956
957
958
959

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

Dafydd Harries's avatar
Dafydd Harries committed
960
961
962
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
    }
Youness Alaoui's avatar
Youness Alaoui committed
963

964
  agent_unlock();
Youness Alaoui's avatar
Youness Alaoui committed
965

Dafydd Harries's avatar
Dafydd Harries committed
966
967
}

968

969
970
971
972
973
974
975
976
977
978
979
980
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;
    }
981
982
983
984
    if (component->tcp_data != NULL) {
      g_slice_free (TcpUserData, component->tcp_data);
      component->tcp_data = NULL;
    }
985
986
987
988
989
990
991
992
993
994
}

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);
  }
995
  priv_destroy_component_tcp (component);
996
997
998
999
}

static void
adjust_tcp_clock (NiceAgent *agent, Stream *stream, Component *component);
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022


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

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

static void
pseudo_tcp_socket_readable (PseudoTcpSocket *sock, gpointer user_data)
{
  TcpUserData *data = (TcpUserData *)user_data;
  NiceAgent *agent = data->agent;
  Component *component = data->component;
  Stream *stream = data->stream;
1023
  gchar buf[MAX_BUFFER_SIZE];
1024
1025
1026
1027
1028
  gint len;

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

1029
1030
  component->tcp_readable = TRUE;

1031
1032
1033
  g_object_add_weak_pointer (G_OBJECT (sock), (gpointer *)&sock);
  g_object_add_weak_pointer (G_OBJECT (agent), (gpointer *)&agent);

1034
  do {
1035
1036
1037
1038
    if (component->g_source_io_cb)
      len = pseudo_tcp_socket_recv (sock, buf, sizeof(buf));
    else
      len = 0;
1039

1040
    if (len > 0) {
1041
1042
1043
1044
1045
1046
1047
1048
      gpointer data = component->data;
      gint sid = stream->id;
      gint cid = component->id;
      NiceAgentRecvFunc callback = component->g_source_io_cb;
      /* Unlock the agent before calling the callback */
      agent_unlock();
      callback (agent, sid, cid, len, buf, data);
      agent_lock();
1049
1050
1051
1052
      if (sock == NULL) {
        nice_debug ("PseudoTCP socket got destroyed in readable callback!");
        break;
      }
1053
1054
1055
    } else if (len < 0 &&
        pseudo_tcp_socket_get_error (sock) != EWOULDBLOCK) {
      /* Signal error */
1056
      priv_pseudo_tcp_error (agent, stream, component);
1057
1058
1059
    } else if (len < 0 &&
        pseudo_tcp_socket_get_error (sock) == EWOULDBLOCK){
      component->tcp_readable = FALSE;
1060
1061
1062
    }
  } while (len > 0);

1063
  if (agent) {
1064
    adjust_tcp_clock (agent, stream, component);
1065
1066
    g_object_remove_weak_pointer (G_OBJECT (agent), (gpointer *)&agent);
  } else {
1067
    nice_debug ("Not calling adjust_tcp_clock.. agent got destroyed!");
1068
1069
1070
  }
  if (sock)
    g_object_remove_weak_pointer (G_OBJECT (sock), (gpointer *)&sock);
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
}

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

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

static void
pseudo_tcp_socket_closed (PseudoTcpSocket *sock, guint32 err,
    gpointer user_data)
{
  TcpUserData *data = (TcpUserData *)user_data;
1092
1093
1094
1095
1096
1097
1098
  NiceAgent *agent = data->agent;
  Component *component = data->component;
  Stream *stream = data->stream;

  nice_debug ("Agent %p: s%d:%d pseudo Tcp socket closed",  agent,
      stream->id, component->id);
  priv_pseudo_tcp_error (agent, stream, component);
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
}


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

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

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

1117
1118
    nice_debug ("Agent %p : s%d:%d: sending %d bytes to [%s]:%d", data->agent,
        data->stream->id, component->id, len, tmpbuf,
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
        nice_address_get_port (&component->selected_pair.remote->addr));
#endif

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

  return WR_FAIL;
}


static gboolean
notify_pseudo_tcp_socket_clock (gpointer user_data)
{
  TcpUserData *data = (TcpUserData *)user_data;
  Component *component = data->component;
1138
  Stream *stream = data->stream;
1139
1140
1141
1142
  NiceAgent *agent = data->agent;

  agent_lock();

1143
1144
1145
1146
1147
1148
  if (g_source_is_destroyed (g_main_current_source ())) {
    nice_debug ("Source was destroyed. "
        "Avoided race condition in notify_pseudo_tcp_socket_clock");
    agent_unlock ();
    return FALSE;
  }
1149
1150
1151
1152
1153
1154
1155
  if (component->tcp_clock) {
    g_source_destroy (component->tcp_clock);
    g_source_unref (component->tcp_clock);
    component->tcp_clock = NULL;
  }

  pseudo_tcp_socket_notify_clock (component->tcp);
1156
  adjust_tcp_clock (agent, stream, component);
1157
1158
1159
1160
1161
1162

  agent_unlock();

  return FALSE;
}

1163
1164
static void
adjust_tcp_clock (NiceAgent *agent, Stream *stream, Component *component)
1165
1166
{
  long timeout = 0;
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
  if (component->tcp) {
    if (pseudo_tcp_socket_get_next_clock (component->tcp, &timeout)) {
      if (component->tcp_clock) {
        g_source_destroy (component->tcp_clock);
        g_source_unref (component->tcp_clock);
        component->tcp_clock = NULL;
      }
      component->tcp_clock = agent_timeout_add_with_context (agent,
          timeout, notify_pseudo_tcp_socket_clock, component->tcp_data);
    } else {
      nice_debug ("Agent %p: component %d pseudo tcp socket should be destroyed",
          agent, component->id);
      priv_pseudo_tcp_error (agent, stream, component);
1180
1181
1182
1183
1184
    }
  }
}


1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
void agent_gathering_done (NiceAgent *agent)
{

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

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

      for (k = component->local_candidates; k; k = k->next) {
        NiceCandidate *local_candidate = k->data;
Youness Alaoui's avatar
Youness Alaoui committed
1197
1198
1199
1200
1201
1202
1203
1204
1205
	{
	  gchar tmpbuf[INET6_ADDRSTRLEN];
	  nice_address_to_string (&local_candidate->addr, tmpbuf);
          nice_debug ("Agent %p: gathered local candidate : [%s]:%u"
              " for s%d/c%d. U/P '%s'/'%s'", agent,
              tmpbuf, nice_address_get_port (&local_candidate->addr),
              local_candidate->stream_id, local_candidate->component_id,
              local_candidate->username, local_candidate->password);
	}
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
        for (l = component->remote_candidates; l; l = l->next) {
          NiceCandidate *remote_candidate = l->data;

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

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

1223
#ifdef HAVE_GUPNP
Youness Alaoui's avatar
Youness Alaoui committed
1224
1225
1226
1227
  if (agent->discovery_timer_source == NULL &&
      agent->upnp_timer_source == NULL) {
    agent_signal_gathering_done (agent);
  }
1228
#else
1229
1230
  if (agent->discovery_timer_source == NULL)
    agent_signal_gathering_done (agent);
1231
#endif
1232
1233
}

1234
void agent_signal_gathering_done (NiceAgent *agent)
1235
{
1236
1237
1238
1239
1240
1241
  GSList *i;

  for (i = agent->streams; i; i = i->next) {
    Stream *stream = i->data;
    if (stream->gathering) {
      stream->gathering = FALSE;
1242
      g_signal_emit (agent, signals[SIGNAL_CANDIDATE_GATHERING_DONE], 0, stream->id);
1243
1244
    }
  }
1245
}
Dafydd Harries's avatar
Dafydd Harries committed
1246

1247
void agent_signal_initial_binding_request_received (NiceAgent *agent, Stream *stream)
1248
{
1249
1250
1251