summaryrefslogtreecommitdiff
path: root/src/protocol.c
blob: bf1fd70c1634758da51bf46633ba149c5b569ea0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
/*
 * protocol.c - source for RakiaProtocol
 * Copyright (C) 2007-2010 Collabora Ltd.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "config.h"

#include "protocol.h"

#include <string.h>

#include <telepathy-glib/telepathy-glib.h>
#include <dbus/dbus-protocol.h>
#include <dbus/dbus-glib.h>

#include <rakia/sofia-decls.h>
#include <rakia/handles.h>
#include <rakia/media-manager.h>
#include <rakia/text-manager.h>
#include <sofia-sip/su_glib.h>

#define DEBUG_FLAG RAKIA_DEBUG_CONNECTION
#include "rakia/debug.h"
#include "sip-connection.h"
#include "sip-connection-helpers.h"

#define PROTOCOL_NAME "sip"
#define ICON_NAME "im-" PROTOCOL_NAME
#define VCARD_FIELD_NAME "x-" PROTOCOL_NAME
#define ENGLISH_NAME "SIP"

G_DEFINE_TYPE (RakiaProtocol,
    rakia_protocol,
    TP_TYPE_BASE_PROTOCOL)

enum {
    PROP_SOFIA_ROOT = 1,
};

struct _RakiaProtocolPrivate
{
  su_root_t *sofia_root;
};

/* Used in the otherwise-unused offset field of the TpCMParamSpec. The first
 * one is nonzero to catch implicit zero-initialization. */
enum {
    PARAM_EASY = 1,
    PARAM_SET_SEPARATELY
};

static TpCMParamSpec rakia_params[] = {
    /* Account (a sip: URI)
     *
     * FIXME: validate account SIP URI properly, using appropriate RFCs */
    { "account", DBUS_TYPE_STRING_AS_STRING, G_TYPE_STRING,
      TP_CONN_MGR_PARAM_FLAG_REQUIRED | TP_CONN_MGR_PARAM_FLAG_REGISTER,
      NULL, PARAM_SET_SEPARATELY, tp_cm_param_filter_string_nonempty, NULL },

    /* Username to register with, if different than in the account URI */
    { "auth-user", DBUS_TYPE_STRING_AS_STRING, G_TYPE_STRING,
      0, NULL, PARAM_EASY },

    /* Password */
    { "password", DBUS_TYPE_STRING_AS_STRING, G_TYPE_STRING,
      TP_CONN_MGR_PARAM_FLAG_SECRET, NULL, PARAM_EASY },

    /* Display name for self */
    { "alias", DBUS_TYPE_STRING_AS_STRING, G_TYPE_STRING, 0, NULL, PARAM_EASY,
      /* setting a 0-length alias makes no sense */
      tp_cm_param_filter_string_nonempty, NULL },

    /* Registrar */
    { "registrar", DBUS_TYPE_STRING_AS_STRING, G_TYPE_STRING, 0, NULL,
      PARAM_EASY },

    /* Used to compose proxy URI */
    { "proxy-host", DBUS_TYPE_STRING_AS_STRING, G_TYPE_STRING, 0, NULL,
      PARAM_SET_SEPARATELY },
    { "port", DBUS_TYPE_UINT16_AS_STRING, G_TYPE_UINT,
      TP_CONN_MGR_PARAM_FLAG_HAS_DEFAULT, GUINT_TO_POINTER(SIP_DEFAULT_PORT),
      PARAM_SET_SEPARATELY, tp_cm_param_filter_uint_nonzero },
    { "transport", DBUS_TYPE_STRING_AS_STRING, G_TYPE_STRING,
      TP_CONN_MGR_PARAM_FLAG_HAS_DEFAULT, "auto", PARAM_SET_SEPARATELY },

    /* Enables loose routing as per RFC 3261 */
    { "loose-routing", DBUS_TYPE_BOOLEAN_AS_STRING, G_TYPE_BOOLEAN,
      TP_CONN_MGR_PARAM_FLAG_HAS_DEFAULT, GUINT_TO_POINTER(FALSE),
      PARAM_EASY },

    /* Used to enable proactive NAT traversal techniques */
    { "discover-binding", DBUS_TYPE_BOOLEAN_AS_STRING, G_TYPE_BOOLEAN,
      TP_CONN_MGR_PARAM_FLAG_HAS_DEFAULT, GUINT_TO_POINTER(TRUE),
      PARAM_EASY },

    /* Mechanism used for connection keepalive maintenance */
    { "keepalive-mechanism", DBUS_TYPE_STRING_AS_STRING, G_TYPE_STRING,
      TP_CONN_MGR_PARAM_FLAG_HAS_DEFAULT, "auto", PARAM_SET_SEPARATELY },

    /* Keep-alive interval */
    { "keepalive-interval", DBUS_TYPE_UINT32_AS_STRING, G_TYPE_UINT,
      0, NULL, PARAM_EASY },

    /* Use SRV DNS lookup to discover STUN server for media NAT traversal
     * (defaults to true unless stun-server is set) */
    { "discover-stun", DBUS_TYPE_BOOLEAN_AS_STRING, G_TYPE_BOOLEAN,
      0, NULL, PARAM_SET_SEPARATELY },

    /* STUN server for media NAT traversal */
    { "stun-server", DBUS_TYPE_STRING_AS_STRING, G_TYPE_STRING, 0, NULL,
      PARAM_SET_SEPARATELY },

    /* STUN port */
    { "stun-port", DBUS_TYPE_UINT16_AS_STRING, G_TYPE_UINT,
      TP_CONN_MGR_PARAM_FLAG_HAS_DEFAULT,
      GUINT_TO_POINTER(RAKIA_DEFAULT_STUN_PORT), PARAM_EASY,
      tp_cm_param_filter_uint_nonzero },

    /* If the session content cannot be modified once initially set up */
    { "immutable-streams", DBUS_TYPE_BOOLEAN_AS_STRING, G_TYPE_BOOLEAN,
      TP_CONN_MGR_PARAM_FLAG_HAS_DEFAULT, GUINT_TO_POINTER(FALSE),
      PARAM_EASY },

    /* Local IP address to use, workaround purposes only */
    { "local-ip-address", DBUS_TYPE_STRING_AS_STRING, G_TYPE_STRING,
      0, NULL, PARAM_EASY },

    /* Local port for SIP, workaround purposes only */
    { "local-port", DBUS_TYPE_UINT16_AS_STRING, G_TYPE_UINT, 0, NULL,
      PARAM_EASY },

    /* Extra-realm authentication */
    { "extra-auth-user", DBUS_TYPE_STRING_AS_STRING, G_TYPE_STRING,
      0, NULL, PARAM_EASY },
    { "extra-auth-password", DBUS_TYPE_STRING_AS_STRING, G_TYPE_STRING,
      TP_CONN_MGR_PARAM_FLAG_SECRET, NULL, PARAM_EASY },

    /* If true, TLS validation errors will be ignored */
    { "ignore-tls-errors", DBUS_TYPE_BOOLEAN_AS_STRING, G_TYPE_BOOLEAN,
      TP_CONN_MGR_PARAM_FLAG_HAS_DEFAULT, GUINT_TO_POINTER(FALSE),
      PARAM_EASY },

    { NULL }
};

static void
rakia_protocol_init (RakiaProtocol *self)
{
  self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, RAKIA_TYPE_PROTOCOL,
      RakiaProtocolPrivate);
}

static const TpCMParamSpec *
get_parameters (TpBaseProtocol *self G_GNUC_UNUSED)
{
  return rakia_params;
}

static RakiaConnectionKeepaliveMechanism
priv_parse_keepalive (const gchar *str)
{
  if (str == NULL || strcmp (str, "auto") == 0)
    return RAKIA_CONNECTION_KEEPALIVE_AUTO;
  if (strcmp (str, "register") == 0)
    return RAKIA_CONNECTION_KEEPALIVE_REGISTER;
  if (strcmp (str, "options") == 0)
    return RAKIA_CONNECTION_KEEPALIVE_OPTIONS;
  if (strcmp (str, "stun") == 0)
    return RAKIA_CONNECTION_KEEPALIVE_STUN;
  if (strcmp (str, "off") == 0)
    return RAKIA_CONNECTION_KEEPALIVE_NONE;

  WARNING ("unsupported keepalive-method value \"%s\", falling back to auto", str);
  return RAKIA_CONNECTION_KEEPALIVE_AUTO;
}

static gchar *
priv_compose_proxy_uri (const gchar *host,
                        const gchar *transport,
                        guint port)
{
  const gchar *scheme = "sip";

  if (host == NULL)
    return NULL;

  /* Set scheme to SIPS if transport is TLS */

  if (transport != NULL && !g_ascii_strcasecmp (transport, "tls"))
    scheme = "sips";

  /* Format the resulting URI */

  if (port)
    return g_strdup_printf ("%s:%s:%u", scheme, host, port);
  else
    return g_strdup_printf ("%s:%s", scheme, host);
}

/**
 * Returns a default SIP proxy address based on the public 
 * SIP address 'sip_address' and . For instance
 * "sip:first.surname@company.com" would result in "sip:company.com".
 * The SIP stack will further utilize DNS lookups to find the IP address 
 * for the SIP server responsible for the domain "company.com".
 */
static gchar *
priv_compose_default_proxy_uri (const gchar *sip_address,
                                const gchar *transport)
{
  char *result = NULL;
  char *host;
  char *found;

  g_return_val_if_fail (sip_address != NULL, NULL);

  /* skip sip and sips prefixes, updating transport if necessary */
  found = strchr (sip_address, ':');
  if (found != NULL) {
    if (g_ascii_strncasecmp ("sip:", sip_address, 4) == 0)
      ;
    else if (g_ascii_strncasecmp ("sips:", sip_address, 5) == 0)
      {
        if (transport == NULL ||
            strcmp (transport, "auto") == 0)
          transport = "tls";
      }
    else
      {
        /* error, unknown URI prefix */
        return NULL;
      }

    sip_address = found + 1;
  }

  /* skip userinfo */
  found = strchr (sip_address, '@');
  if (found != NULL)
    sip_address = found + 1;

  /* copy rest of the string */
  host = g_strdup (sip_address);

  /* mark end (before uri-parameters defs or headers) */
  found = strchr (host, ';');
  if (found == NULL)
    found = strchr (host, '?');
  if (found != NULL)
    *found = '\0';

  result = priv_compose_proxy_uri (host, transport, 0);

  g_free (host);

  return result;
}

static TpBaseConnection *
new_connection (TpBaseProtocol *protocol,
                GHashTable *params,
                GError **error)
{
  RakiaProtocol *self = RAKIA_PROTOCOL (protocol);
  RakiaConnection *conn;
  guint i;
  const gchar *account;
  const gchar *transport;
  const gchar *proxy_host;
  guint16 port;
  gboolean discover_stun;
  gboolean discover_stun_valid;
  const gchar *stun_server;
  gchar *proxy;
  RakiaConnectionKeepaliveMechanism keepalive_mechanism;

  account = tp_asv_get_string (params, "account");
  transport = tp_asv_get_string (params, "transport");
  port = tp_asv_get_uint32 (params, "port", NULL);

  conn = g_object_new (RAKIA_TYPE_CONNECTION,
                       "protocol", PROTOCOL_NAME,
                       "sofia-root", self->priv->sofia_root,
                       "address", account,
                       NULL);

  proxy_host = tp_asv_get_string (params, "proxy-host");

  if (tp_str_empty (proxy_host))
    {
      proxy = priv_compose_default_proxy_uri (account, transport);
      DEBUG("set outbound proxy address to <%s>, based on <%s>", proxy,
          account);
    }
  else
    {
      proxy = priv_compose_proxy_uri (proxy_host, transport, port);
    }

  g_object_set (conn,
      "proxy", proxy,
      NULL);
  g_free (proxy);

  if (!tp_str_empty (transport) && tp_strdiff (transport, "auto"))
    g_object_set (conn,
        "transport", transport,
        NULL);

  discover_stun = tp_asv_get_boolean (params, "discover-stun",
      &discover_stun_valid);
  stun_server = tp_asv_get_string (params, "stun-server");

  if (!discover_stun_valid && stun_server == NULL)
    discover_stun = TRUE;

  g_object_set (conn,
      "discover-stun", discover_stun,
      "stun-server", stun_server,
      NULL);

  for (i = 0; rakia_params[i].name != NULL; i++)
    {
      if (rakia_params[i].offset == PARAM_SET_SEPARATELY)
        {
          DEBUG ("Parameter %s is handled specially", rakia_params[i].name);
          continue;
        }

      g_assert (rakia_params[i].offset == PARAM_EASY);

      switch (rakia_params[i].gtype)
        {
          case G_TYPE_STRING:
              {
                const gchar *s = tp_asv_get_string (params,
                    rakia_params[i].name);

                if (!tp_str_empty (s))
                  g_object_set (conn,
                      rakia_params[i].name, s,
                      NULL);
              }
            break;

          case G_TYPE_UINT:
              {
                gboolean valid = FALSE;
                guint u = tp_asv_get_uint32 (params,
                    rakia_params[i].name, &valid);

                if (valid)
                  g_object_set (conn,
                      rakia_params[i].name, u,
                      NULL);
              }
            break;

          case G_TYPE_BOOLEAN:
              {
                gboolean valid = FALSE;
                gboolean b = tp_asv_get_boolean (params, rakia_params[i].name,
                    &valid);

                if (valid)
                  g_object_set (conn,
                      rakia_params[i].name, b,
                      NULL);
              }
            break;

          default:
            /* no other parameters have been written yet */
            g_assert_not_reached ();
        }
    }

  keepalive_mechanism = priv_parse_keepalive (tp_asv_get_string (params,
        "keepalive-mechanism"));
  g_object_set (conn,
      "keepalive-mechanism", keepalive_mechanism,
      NULL);

  return TP_BASE_CONNECTION (conn);
}

static gchar *
normalize_contact (TpBaseProtocol *self G_GNUC_UNUSED,
                   const gchar *contact,
                   GError **error)
{
  return rakia_normalize_contact (contact, NULL, NULL, error);
}

static gchar *
identify_account (TpBaseProtocol *self G_GNUC_UNUSED,
    GHashTable *asv,
    GError **error)
{
  const gchar *account = tp_asv_get_string (asv, "account");

  g_assert (account != NULL);
  return g_strdup (account);
}

static GStrv
get_interfaces (TpBaseProtocol *self)
{
  return g_new0 (gchar *, 1);
}

static void
get_connection_details (TpBaseProtocol *self,
    GStrv *connection_interfaces,
    GType **channel_managers,
    gchar **icon_name,
    gchar **english_name,
    gchar **vcard_field)
{
  if (connection_interfaces != NULL)
    {
      *connection_interfaces = g_strdupv (
          (GStrv) rakia_connection_get_implemented_interfaces ());
    }

  if (channel_managers != NULL)
    {
      GType types[] = {
          RAKIA_TYPE_TEXT_MANAGER,
          RAKIA_TYPE_MEDIA_MANAGER,
          G_TYPE_INVALID };

      *channel_managers = g_memdup (types, sizeof(types));
    }

  if (icon_name != NULL)
    {
      *icon_name = g_strdup (ICON_NAME);
    }

  if (vcard_field != NULL)
    {
      *vcard_field = g_strdup (VCARD_FIELD_NAME);
    }

  if (english_name != NULL)
    {
      *english_name = g_strdup (ENGLISH_NAME);
    }
}

static GStrv
dup_authentication_types (TpBaseProtocol *base)
{
  const gchar * const types[] = {
    TP_IFACE_CHANNEL_INTERFACE_SASL_AUTHENTICATION,
    NULL
  };

  return g_strdupv ((GStrv) types);
}

static void
rakia_protocol_get_property (GObject *object,
    guint property_id,
    GValue *value,
    GParamSpec *pspec)
{
  RakiaProtocol *self = RAKIA_PROTOCOL (object);

  switch (property_id)
    {
      case PROP_SOFIA_ROOT:
        g_value_set_pointer (value, self->priv->sofia_root);
        break;

      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
        break;
    }
}

static void
rakia_protocol_set_property (GObject *object,
    guint property_id,
    const GValue *value,
    GParamSpec *pspec)
{
  RakiaProtocol *self = RAKIA_PROTOCOL (object);

  switch (property_id)
    {
      case PROP_SOFIA_ROOT:
        self->priv->sofia_root = g_value_get_pointer (value);
        break;

      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
        break;
    }
}

static void
rakia_protocol_class_init (RakiaProtocolClass *klass)
{
  TpBaseProtocolClass *base_class = (TpBaseProtocolClass *) klass;
  GObjectClass *object_class = (GObjectClass *) klass;
  GParamSpec *param_spec;

  g_type_class_add_private (klass, sizeof (RakiaProtocolPrivate));

  base_class->get_parameters = get_parameters;
  base_class->new_connection = new_connection;
  base_class->normalize_contact = normalize_contact;
  base_class->identify_account = identify_account;
  base_class->get_interfaces = get_interfaces;
  base_class->get_connection_details = get_connection_details;
  base_class->dup_authentication_types = dup_authentication_types;

  object_class->get_property = rakia_protocol_get_property;
  object_class->set_property = rakia_protocol_set_property;

  param_spec = g_param_spec_pointer ("sofia-root", "Sofia-SIP root",
      "the root object for Sofia-SIP",
      G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
  g_object_class_install_property (object_class, PROP_SOFIA_ROOT,
      param_spec);
}

TpBaseProtocol *
rakia_protocol_new (su_root_t *sofia_root)
{
  return g_object_new (RAKIA_TYPE_PROTOCOL,
      "name", PROTOCOL_NAME,
      "sofia-root", sofia_root,
      NULL);
}