22
22
#include <stdlib.h>
23
23
#include <string.h>
28
#include <glib/gstdio.h>
28
30
#include <dbus/dbus-glib.h>
29
31
#include <dbus/dbus-glib-lowlevel.h>
30
32
#include <telepathy-glib/util.h>
32
34
#include <gibber/gibber-bytestream-ibb.h>
35
#include <gibber/gibber-bytestream-muc.h>
33
36
#include <gibber/gibber-muc-connection.h>
35
38
#define DEBUG_FLAG DEBUG_TUBES
37
#include "extensions/extensions.h"
38
40
#include "salut-connection.h"
39
#include "namespaces.h"
40
41
#include "tube-iface.h"
42
#include "sha1/sha1-util.h"
43
#include "bytestream-factory.h"
44
/* When we receive D-Bus messages to be delivered to the application and the
45
* application is not yet connected to the D-Bus tube, theses D-Bus messages
46
* are queued and delivered when the application connects to the D-Bus tube.
48
* If the application never connects, there is a risk that the contact sends
49
* too many messages and eat all the memory. To avoid this, there is an
50
* arbitrary limit on the queue size set to 4MB. */
51
#define MAX_QUEUE_SIZE (4096*1024)
47
54
tube_iface_init (gpointer g_iface, gpointer iface_data);
100
107
gchar *dbus_local_name;
101
108
/* the address that we are listening for D-Bus connections on */
102
109
gchar *dbus_srv_addr;
110
/* the path of the UNIX socket used by the D-Bus server */
103
112
/* the server that's listening on dbus_srv_addr */
104
113
DBusServer *dbus_srv;
105
114
/* the connection to dbus_srv from a local client, or NULL */
106
115
DBusConnection *dbus_conn;
107
/* mapping of contact handle -> D-Bus name */
116
/* the queue of D-Bus messages to be delivered to a local client when it
118
GSList *dbus_msg_queue;
119
/* current size of the queue in bytes. The maximum is MAX_QUEUE_SIZE */
120
unsigned long dbus_msg_queue_size;
121
/* mapping of contact handle -> D-Bus name (NULL for 1-1 D-Bus tubes) */
108
122
GHashTable *dbus_names;
124
/* Message reassembly buffer (CONTACT tubes only) */
125
GString *reassembly_buffer;
126
/* Number of bytes that will be in the next message, 0 if unknown */
127
guint32 reassembly_bytes_needed;
110
129
gboolean dispose_has_run;
113
132
#define SALUT_TUBE_DBUS_GET_PRIVATE(obj) \
114
((SalutTubeDBusPrivate *) obj->priv)
133
((SalutTubeDBusPrivate *) ((SalutTubeDBus *)obj)->priv)
116
static void data_received_cb (GibberBytestreamIBB *ibb, const gchar *from,
117
GString *data, gpointer user_data);
135
static void data_received_cb (GibberBytestreamIface *bytestream,
136
const gchar *from, GString *data, gpointer user_data);
120
139
* Characters used are permissible both in filenames and in D-Bus names. (See
167
dbus_message_set_sender (msg, priv->dbus_local_name);
239
if (priv->dbus_local_name != NULL)
241
dbus_message_set_sender (msg, priv->dbus_local_name);
169
244
if (!dbus_message_marshal (msg, &marshalled, &len))
172
gibber_bytestream_ibb_send (priv->bytestream, len, marshalled);
247
gibber_bytestream_iface_send (priv->bytestream, len, marshalled);
249
if (GIBBER_IS_BYTESTREAM_MUC (priv->bytestream))
251
/* In a Salut MUC we never receive messages we sent, so we need to
252
* artificially receive our own messages. */
253
destination = dbus_message_get_destination (msg);
254
if (destination == NULL || !tp_strdiff (priv->dbus_local_name,
257
dbus_connection_send (priv->dbus_conn, msg, &serial);
175
262
if (marshalled != NULL)
197
286
dbus_connection_setup_with_g_main (conn, NULL);
198
287
dbus_connection_add_filter (conn, filter_cb, tube, NULL);
199
288
priv->dbus_conn = conn;
290
/* We may have received messages to deliver before the local connection is
291
* established. Theses messages are kept in the dbus_msg_queue list and are
292
* delivered as soon as we get the connection. */
293
DEBUG ("%u messages in the queue (%lu bytes)",
294
g_slist_length (priv->dbus_msg_queue), priv->dbus_msg_queue_size);
295
priv->dbus_msg_queue = g_slist_reverse (priv->dbus_msg_queue);
296
for (i = priv->dbus_msg_queue; i != NULL; i = g_slist_delete_link (i, i))
298
DBusMessage *msg = i->data;
299
DEBUG ("delivering queued message from '%s' to '%s' on the "
301
dbus_message_get_sender (msg),
302
dbus_message_get_destination (msg));
303
dbus_connection_send (priv->dbus_conn, msg, &serial);
304
dbus_message_unref (msg);
306
priv->dbus_msg_queue = NULL;
307
priv->dbus_msg_queue_size = 0;
203
311
tube_dbus_open (SalutTubeDBus *self)
313
#define SERVER_LISTEN_MAX_TRIES 5
205
314
SalutTubeDBusPrivate *priv = SALUT_TUBE_DBUS_GET_PRIVATE (self);
206
DBusError error = {0,};
209
317
g_signal_connect (priv->bytestream, "data-received",
210
318
G_CALLBACK (data_received_cb), self);
212
generate_ascii_string (8, suffix);
213
priv->dbus_srv_addr = g_strdup_printf (
214
"unix:path=/tmp/dbus-salut-%.8s", suffix);
320
for (i = 0; i < SERVER_LISTEN_MAX_TRIES; i++)
325
g_free (priv->dbus_srv_addr);
326
g_free (priv->socket_path);
328
generate_ascii_string (8, suffix);
329
priv->socket_path = g_strdup_printf ("%s/dbus-salut-%.8s",
330
g_get_tmp_dir (), suffix);
331
priv->dbus_srv_addr = g_strdup_printf ("unix:path=%s",
334
dbus_error_init (&error);
335
priv->dbus_srv = dbus_server_listen (priv->dbus_srv_addr, &error);
337
if (priv->dbus_srv_addr != NULL)
340
DEBUG ("dbus_server_listen failed (try %u): %s: %s", i, error.name,
342
dbus_error_free (&error);
345
if (priv->dbus_srv_addr == NULL)
347
DEBUG ("all attempts failed. Close the tube");
348
salut_tube_iface_accept (SALUT_TUBE_IFACE (self), NULL);
215
352
DEBUG ("listening on %s", priv->dbus_srv_addr);
216
priv->dbus_srv = dbus_server_listen (priv->dbus_srv_addr, &error);
218
/* XXX: if dbus_server_listen fails, we should retry with different
219
* addresses, then close the tube if we give up
221
g_assert (priv->dbus_srv);
223
354
dbus_server_set_new_connection_function (priv->dbus_srv, new_connection_cb,
260
376
tp_handle_unref (contact_repo, handle);
263
static SalutTubeState
264
380
get_tube_state (SalutTubeDBus *self)
266
382
SalutTubeDBusPrivate *priv = SALUT_TUBE_DBUS_GET_PRIVATE (self);
267
GibberBytestreamIBBState bytestream_state;
383
GibberBytestreamState bytestream_state;
269
385
if (priv->bytestream == NULL)
270
386
/* bytestream not yet created as we're waiting for the SI reply */
271
return SALUT_TUBE_STATE_REMOTE_PENDING;
387
return TP_TUBE_STATE_REMOTE_PENDING;
273
389
g_object_get (priv->bytestream, "state", &bytestream_state, NULL);
275
if (bytestream_state == GIBBER_BYTESTREAM_IBB_STATE_OPEN)
276
return SALUT_TUBE_STATE_OPEN;
278
else if (bytestream_state == GIBBER_BYTESTREAM_IBB_STATE_LOCAL_PENDING ||
279
bytestream_state == GIBBER_BYTESTREAM_IBB_STATE_ACCEPTED)
280
return SALUT_TUBE_STATE_LOCAL_PENDING;
282
else if (bytestream_state == GIBBER_BYTESTREAM_IBB_STATE_INITIATING)
283
return SALUT_TUBE_STATE_REMOTE_PENDING;
286
g_assert_not_reached ();
287
return SALUT_TUBE_STATE_REMOTE_PENDING;
391
switch (bytestream_state)
393
case GIBBER_BYTESTREAM_STATE_OPEN:
394
return TP_TUBE_STATE_OPEN;
396
case GIBBER_BYTESTREAM_STATE_LOCAL_PENDING:
397
case GIBBER_BYTESTREAM_STATE_ACCEPTED:
398
return TP_TUBE_STATE_LOCAL_PENDING;
400
case GIBBER_BYTESTREAM_STATE_INITIATING:
401
return TP_TUBE_STATE_REMOTE_PENDING;
404
g_assert_not_reached ();
291
bytestream_state_changed_cb (GibberBytestreamIBB *bytestream,
292
GibberBytestreamIBBState state,
409
bytestream_state_changed_cb (GibberBytestreamIface *bytestream,
410
GibberBytestreamState state,
293
411
gpointer user_data)
295
413
SalutTubeDBus *self = SALUT_TUBE_DBUS (user_data);
296
414
SalutTubeDBusPrivate *priv = SALUT_TUBE_DBUS_GET_PRIVATE (self);
298
if (state == GIBBER_BYTESTREAM_IBB_STATE_CLOSED)
416
if (state == GIBBER_BYTESTREAM_STATE_CLOSED)
300
418
if (priv->bytestream != NULL)
337
455
if (priv->dbus_srv)
338
456
dbus_server_unref (priv->dbus_srv);
340
if (priv->dbus_srv_addr)
341
g_free (priv->dbus_srv_addr);
343
if (priv->dbus_local_name)
344
g_free (priv->dbus_local_name);
458
if (priv->socket_path != NULL)
460
if (g_unlink (priv->socket_path) != 0)
462
DEBUG ("unlink of %s failed: %s", priv->socket_path,
467
if (priv->dbus_msg_queue != NULL)
470
for (i = priv->dbus_msg_queue; i != NULL; i = g_slist_delete_link (i, i))
472
DBusMessage *msg = i->data;
473
dbus_message_unref (msg);
475
priv->dbus_msg_queue = NULL;
476
priv->dbus_msg_queue_size = 0;
479
g_free (priv->dbus_srv_addr);
480
priv->dbus_srv_addr = NULL;
481
g_free (priv->socket_path);
482
priv->socket_path = NULL;
483
g_free (priv->dbus_local_name);
484
priv->dbus_local_name = NULL;
346
486
if (priv->dbus_names)
543
682
if (priv->handle_type == TP_HANDLE_TYPE_ROOM)
546
* We have to create an IBB bytestream that will be
685
* We have to create an MUC bytestream that will be
547
686
* used by this MUC tube to communicate.
549
688
* We don't create the bytestream of private D-Bus tube yet.
550
689
* It will be when we'll receive the answer of the SI request
552
GibberBytestreamIBB *bytestream;
553
GibberBytestreamIBBState state;
691
GibberBytestreamMuc *bytestream;
692
GibberBytestreamState state;
554
693
const gchar *peer_id;
556
695
g_assert (priv->muc_connection != NULL);
557
g_assert (priv->stream_id != NULL);
697
priv->dbus_names = g_hash_table_new_full (g_direct_hash, g_direct_equal,
700
priv->dbus_local_name = generate_dbus_unique_name (priv->conn->name);
702
DEBUG ("local name: %s", priv->dbus_local_name);
559
704
peer_id = tp_handle_inspect (handles_repo, priv->handle);
560
705
if (priv->initiator == priv->self_handle)
562
707
/* We create this tube, bytestream is open */
563
state = GIBBER_BYTESTREAM_IBB_STATE_OPEN;
708
state = GIBBER_BYTESTREAM_STATE_OPEN;
567
712
/* We don't create this tube, bytestream is local pending */
568
state = GIBBER_BYTESTREAM_IBB_STATE_LOCAL_PENDING;
713
state = GIBBER_BYTESTREAM_STATE_LOCAL_PENDING;
571
bytestream = g_object_new (GIBBER_TYPE_BYTESTREAM_IBB,
716
bytestream = g_object_new (GIBBER_TYPE_BYTESTREAM_MUC,
572
717
"muc-connection", priv->muc_connection,
573
"stream-id", priv->stream_id,
575
719
"self-id", priv->conn->name,
576
720
"peer-id", peer_id,
577
"stream-init-id", NULL,
723
g_object_get (bytestream, "stream-id", &priv->stream_id, NULL);
580
725
g_object_set (self, "bytestream", bytestream, NULL);
726
g_object_unref (bytestream);
584
730
/* Private tube */
585
731
g_assert (priv->muc_connection == NULL);
733
/* The D-Bus names mapping is used in muc tubes only */
734
priv->dbus_local_name = NULL;
735
priv->dbus_names = NULL;
737
/* For contact tubes we need to be able to reassemble messages. */
738
priv->reassembly_buffer = g_string_new ("");
739
priv->reassembly_bytes_needed = 0;
604
758
object_class->dispose = salut_tube_dbus_dispose;
605
759
object_class->finalize = salut_tube_dbus_finalize;
607
param_spec = g_param_spec_object (
609
"SalutConnection object",
610
"Salut connection object that owns this D-Bus tube object.",
611
SALUT_TYPE_CONNECTION,
612
G_PARAM_CONSTRUCT_ONLY |
614
G_PARAM_STATIC_NAME |
615
G_PARAM_STATIC_NICK |
616
G_PARAM_STATIC_BLURB);
617
g_object_class_install_property (object_class, PROP_CONNECTION, param_spec);
619
param_spec = g_param_spec_uint (
622
"The TpHandle associated with the tubes channel that"
623
"owns this D-Bus tube object.",
625
G_PARAM_CONSTRUCT_ONLY |
627
G_PARAM_STATIC_NAME |
628
G_PARAM_STATIC_NICK |
629
G_PARAM_STATIC_BLURB);
630
g_object_class_install_property (object_class, PROP_HANDLE, param_spec);
632
param_spec = g_param_spec_uint (
635
"The TpHandleType of the handle associated with the tubes channel that"
636
"owns this D-Bus tube object.",
638
G_PARAM_CONSTRUCT_ONLY |
640
G_PARAM_STATIC_NAME |
641
G_PARAM_STATIC_NICK |
642
G_PARAM_STATIC_BLURB);
643
g_object_class_install_property (object_class, PROP_HANDLE_TYPE,
646
param_spec = g_param_spec_uint (
649
"The handle to use for ourself. This can be different from the "
650
"connection's self handle if our handle is a room handle.",
652
G_PARAM_CONSTRUCT_ONLY |
654
G_PARAM_STATIC_NAME |
655
G_PARAM_STATIC_NICK |
656
G_PARAM_STATIC_BLURB);
657
g_object_class_install_property (object_class, PROP_SELF_HANDLE, param_spec);
761
g_object_class_override_property (object_class, PROP_CONNECTION,
763
g_object_class_override_property (object_class, PROP_HANDLE,
765
g_object_class_override_property (object_class, PROP_HANDLE_TYPE,
767
g_object_class_override_property (object_class, PROP_SELF_HANDLE,
769
g_object_class_override_property (object_class, PROP_ID,
771
g_object_class_override_property (object_class, PROP_TYPE,
773
g_object_class_override_property (object_class, PROP_INITIATOR,
775
g_object_class_override_property (object_class, PROP_SERVICE,
777
g_object_class_override_property (object_class, PROP_PARAMETERS,
779
g_object_class_override_property (object_class, PROP_STATE,
659
782
param_spec = g_param_spec_object (
660
783
"muc-connection",
700
810
"The identifier of this tube's bytestream",
702
G_PARAM_CONSTRUCT_ONLY |
704
813
G_PARAM_STATIC_NAME |
705
814
G_PARAM_STATIC_NICK |
706
815
G_PARAM_STATIC_BLURB);
707
816
g_object_class_install_property (object_class, PROP_STREAM_ID, param_spec);
709
param_spec = g_param_spec_uint (
712
"The TpTubeType this D-Bus tube object.",
713
0, G_MAXUINT32, SALUT_TUBE_TYPE_DBUS,
715
G_PARAM_STATIC_NAME |
716
G_PARAM_STATIC_NICK |
717
G_PARAM_STATIC_BLURB);
718
g_object_class_install_property (object_class, PROP_TYPE, param_spec);
720
param_spec = g_param_spec_uint (
723
"The TpHandle of the initiator of this D-Bus tube object.",
725
G_PARAM_CONSTRUCT_ONLY |
727
G_PARAM_STATIC_NAME |
728
G_PARAM_STATIC_NICK |
729
G_PARAM_STATIC_BLURB);
730
g_object_class_install_property (object_class, PROP_INITIATOR, param_spec);
732
param_spec = g_param_spec_string (
735
"the service associated with this D-BUS tube object.",
737
G_PARAM_CONSTRUCT_ONLY |
739
G_PARAM_STATIC_NAME |
740
G_PARAM_STATIC_NICK |
741
G_PARAM_STATIC_BLURB);
742
g_object_class_install_property (object_class, PROP_SERVICE, param_spec);
744
param_spec = g_param_spec_boxed (
746
"parameters GHashTable",
747
"GHashTable containing parameters of this DBUS tube object.",
749
G_PARAM_CONSTRUCT_ONLY |
751
G_PARAM_STATIC_NAME |
752
G_PARAM_STATIC_NICK |
753
G_PARAM_STATIC_BLURB);
754
g_object_class_install_property (object_class, PROP_PARAMETERS, param_spec);
756
param_spec = g_param_spec_uint (
759
"The SalutTubeState of this DBUS tube object",
760
0, G_MAXUINT32, SALUT_TUBE_STATE_REMOTE_PENDING,
762
G_PARAM_STATIC_NAME |
763
G_PARAM_STATIC_NICK |
764
G_PARAM_STATIC_BLURB);
765
g_object_class_install_property (object_class, PROP_STATE, param_spec);
767
818
param_spec = g_param_spec_string (
821
data_received_cb (GibberBytestreamIBB *ibb,
872
message_received (SalutTubeDBus *tube,
877
SalutTubeDBusPrivate *priv = SALUT_TUBE_DBUS_GET_PRIVATE (tube);
879
DBusError error = {0,};
882
msg = dbus_message_demarshal (data, len, &error);
886
/* message was corrupted */
887
DEBUG ("received corrupted message from %d: %s: %s", sender,
888
error.name, error.message);
889
dbus_error_free (&error);
893
if (priv->handle_type == TP_HANDLE_TYPE_ROOM)
895
const gchar *destination;
896
const gchar *sender_name;
898
destination = dbus_message_get_destination (msg);
899
/* If destination is NULL this msg is broadcasted (signals) so we don't
900
* have to check it */
901
if (destination != NULL && tp_strdiff (priv->dbus_local_name,
904
/* This message is not intended to this tube.
906
DEBUG ("message not intended to this tube (destination = %s)",
911
sender_name = g_hash_table_lookup (priv->dbus_names,
912
GUINT_TO_POINTER (sender));
914
if (tp_strdiff (sender_name, dbus_message_get_sender (msg)))
916
DEBUG ("invalid sender %s (expected %s for sender handle %d)",
917
dbus_message_get_sender (msg), sender_name, sender);
922
if (!priv->dbus_conn)
924
DEBUG ("no D-Bus connection: queuing the message");
926
/* If the application never connects to the private dbus connection, we
927
* don't want to eat all the memory. Only queue MAX_QUEUE_SIZE bytes. If
928
* there are more messages, drop them. */
929
if (priv->dbus_msg_queue_size + len > MAX_QUEUE_SIZE)
931
DEBUG ("D-Bus message queue size limit reached (%u bytes). "
932
"Ignore this message.",
937
priv->dbus_msg_queue = g_slist_prepend (priv->dbus_msg_queue, msg);
938
priv->dbus_msg_queue_size += len;
940
/* returns without unref the message */
944
DEBUG ("delivering message from '%s' to '%s'",
945
dbus_message_get_sender (msg),
946
dbus_message_get_destination (msg));
948
/* XXX: what do do if this returns FALSE? */
949
dbus_connection_send (priv->dbus_conn, msg, &serial);
952
dbus_message_unref (msg);
956
collect_le32 (char *str)
958
unsigned char *bytes = (unsigned char *) str;
960
return bytes[0] | (bytes[1] << 8) | (bytes[2] << 16) | (bytes[3] << 24);
964
collect_be32 (char *str)
966
unsigned char *bytes = (unsigned char *) str;
968
return (bytes[0] << 24) | (bytes[1] << 16) | (bytes[2] << 24) | bytes[3];
972
data_received_cb (GibberBytestreamIface *stream,
822
973
const gchar *from,
824
975
gpointer user_data)
828
979
TpHandleRepoIface *contact_repo = tp_base_connection_get_handles (
829
980
(TpBaseConnection *) priv->conn, TP_HANDLE_TYPE_CONTACT);
832
DBusError error = {0,};
834
const gchar *sender_name;
835
const gchar *destination;
837
if (!priv->dbus_conn)
839
DEBUG ("no D-Bus connection");
843
/* XXX: This naïvely assumes that the underlying transport always gives
844
* us complete messages. This is true for IBB, at least.
847
msg = dbus_message_demarshal (data->str, data->len, &error);
850
/* message was corrupted */
851
DEBUG ("received corrupted message from %s", from);
855
destination = dbus_message_get_destination (msg);
856
/* If destination is NULL this msg is broadcasted (signals) so we don't have
858
if (destination != NULL && tp_strdiff (priv->dbus_local_name, destination))
860
/* This message is not intended to this tube.
862
DEBUG ("message not intended to this tube (destination = %s)",
864
dbus_message_unref (msg);
868
983
sender = tp_handle_lookup (contact_repo, from, NULL, NULL);
871
986
DEBUG ("unkown sender: %s", from);
872
dbus_message_unref (msg);
876
sender_name = g_hash_table_lookup (priv->dbus_names,
877
GUINT_TO_POINTER (sender));
879
if (tp_strdiff (sender_name, dbus_message_get_sender (msg)))
881
DEBUG ("invalid sender %s (expected %s for sender handle %d)",
882
dbus_message_get_sender (msg), sender_name, sender);
883
dbus_message_unref (msg);
887
/* XXX: what do do if this returns FALSE? */
888
dbus_connection_send (priv->dbus_conn, msg, &serial);
890
dbus_message_unref (msg);
990
if (priv->handle_type == TP_HANDLE_TYPE_CONTACT)
992
GString *buf = priv->reassembly_buffer;
994
g_assert (buf != NULL);
996
g_string_append_len (buf, data->str, data->len);
997
DEBUG ("Received %" G_GSIZE_FORMAT " bytes, so we now have %"
998
G_GSIZE_FORMAT " bytes in reassembly buffer", data->len, buf->len);
1000
/* Each D-Bus message has a 16-byte fixed header, in which
1002
* * byte 0 is 'l' (ell) or 'B' for endianness
1003
* * bytes 4-7 are body length "n" in bytes in that endianness
1004
* * bytes 12-15 are length "m" of param array in bytes in that
1007
* followed by m + n + ((8 - (m % 8)) % 8) bytes of other content.
1010
while (buf->len >= 16)
1012
guint32 body_length, params_length, m;
1014
/* see if we have a whole message and have already calculated
1015
* how many bytes it needs */
1017
if (priv->reassembly_bytes_needed != 0)
1019
if (buf->len >= priv->reassembly_bytes_needed)
1021
DEBUG ("Received complete D-Bus message of size %"
1022
G_GINT32_FORMAT, priv->reassembly_bytes_needed);
1023
message_received (tube, sender, buf->str,
1024
priv->reassembly_bytes_needed);
1025
g_string_erase (buf, 0, priv->reassembly_bytes_needed);
1026
priv->reassembly_bytes_needed = 0;
1030
/* we'll have to wait for more data */
1038
/* work out how big the next message is going to be */
1040
if (buf->str[0] == DBUS_BIG_ENDIAN)
1042
body_length = collect_be32 (buf->str + 4);
1043
m = collect_be32 (buf->str + 12);
1045
else if (buf->str[0] == DBUS_LITTLE_ENDIAN)
1047
body_length = collect_le32 (buf->str + 4);
1048
m = collect_le32 (buf->str + 12);
1052
DEBUG ("D-Bus message has unknown endianness byte 0x%x, "
1053
"closing tube", (unsigned int) buf->str[0]);
1054
salut_tube_iface_close (SALUT_TUBE_IFACE (tube));
1058
/* pad to 8-byte boundary */
1059
params_length = m + ((8 - (m % 8)) % 8);
1060
g_assert (params_length % 8 == 0);
1061
g_assert (params_length >= m);
1062
g_assert (params_length < m + 8);
1064
priv->reassembly_bytes_needed = params_length + body_length + 16;
1066
/* n.b.: this looks as if it could be simplified to just the third
1067
* test, but that would be wrong if the addition had overflowed, so
1068
* don't do that. The first and second tests are sufficient to
1069
* ensure no overflow on 32-bit platforms */
1070
if (body_length > DBUS_MAXIMUM_MESSAGE_LENGTH ||
1071
params_length > DBUS_MAXIMUM_ARRAY_LENGTH ||
1072
priv->reassembly_bytes_needed > DBUS_MAXIMUM_MESSAGE_LENGTH)
1074
DEBUG ("D-Bus message is too large to be valid, closing tube");
1075
salut_tube_iface_close (SALUT_TUBE_IFACE (tube));
1079
g_assert (priv->reassembly_bytes_needed != 0);
1080
DEBUG ("We need %" G_GINT32_FORMAT " bytes for the next full "
1081
"message", priv->reassembly_bytes_needed);
1086
/* MUC bytestreams are message-boundary preserving, which is necessary,
1087
* because we can't assume we started at the beginning */
1088
g_assert (GIBBER_IS_BYTESTREAM_MUC (priv->bytestream));
1089
message_received (tube, sender, data->str, data->len);
935
1139
"state", &state,
938
if (state != GIBBER_BYTESTREAM_IBB_STATE_LOCAL_PENDING)
941
g_object_get (priv->bytestream,
942
"stream-init-id", &stream_init_id,
945
if (stream_init_id != NULL)
1142
if (state != GIBBER_BYTESTREAM_STATE_LOCAL_PENDING)
1145
if (priv->handle_type == TP_HANDLE_TYPE_CONTACT)
947
/* Bytestream was created using a SI request so
948
* we have to accept it */
951
LmMessageNode *si, *tube_node;
953
DEBUG ("accept the SI request");
955
msg = salut_bytestream_ibb_make_accept_iq (priv->bytestream);
958
DEBUG ("can't create SI accept IQ. Close the bytestream");
959
gibber_bytestream_ibb_close (priv->bytestream);
963
si = lm_message_node_get_child_with_namespace (msg->node, "si",
965
g_assert (si != NULL);
967
tube_node = lm_message_node_add_child (si, "tube", "");
968
lm_message_node_set_attribute (tube_node, "xmlns", NS_SI_TUBES_OLD);
970
lm_message_node_add_child (tube_node, "dbus-name",
971
priv->dbus_local_name);
973
salut_bytestream_ibb_accept (priv->bytestream, msg);
975
lm_message_unref (msg);
976
g_free (stream_init_id);
1147
/* TODO: SI reply */
981
1151
/* No SI so the bytestream is open */
982
1152
DEBUG ("no SI, bytestream open");
983
1153
g_object_set (priv->bytestream,
984
"state", GIBBER_BYTESTREAM_IBB_STATE_OPEN,
1154
"state", GIBBER_BYTESTREAM_STATE_OPEN,
1017
1189
salut_tube_dbus_add_bytestream (SalutTubeIface *tube,
1018
GibberBytestreamIBB *bytestream)
1190
GibberBytestreamIface *bytestream)
1020
1192
DEBUG ("D-Bus doesn't support extra bytestream");
1021
gibber_bytestream_ibb_close (bytestream);
1193
gibber_bytestream_iface_close (bytestream, NULL);
1197
salut_tube_dbus_add_name (SalutTubeDBus *self,
1201
SalutTubeDBusPrivate *priv = SALUT_TUBE_DBUS_GET_PRIVATE (self);
1202
TpHandleRepoIface *contact_repo = tp_base_connection_get_handles (
1203
(TpBaseConnection *) priv->conn, TP_HANDLE_TYPE_CONTACT);
1205
g_assert (priv->handle_type == TP_HANDLE_TYPE_ROOM);
1207
if (g_hash_table_lookup (priv->dbus_names, GUINT_TO_POINTER (handle))
1210
DEBUG ("contact %d has already announced his D-Bus name", handle);
1214
if (g_str_has_prefix (name, ":2."))
1216
gchar *supposed_name;
1217
const gchar *contact_name;
1219
contact_name = tp_handle_inspect (contact_repo, handle);
1220
supposed_name = generate_dbus_unique_name (contact_name);
1222
if (tp_strdiff (name, supposed_name))
1224
DEBUG ("contact %s announces %s as D-Bus name but it should be %s",
1225
contact_name, name, supposed_name);
1226
g_free (supposed_name);
1229
g_free (supposed_name);
1232
g_hash_table_insert (priv->dbus_names, GUINT_TO_POINTER (handle),
1234
tp_handle_ref (contact_repo, handle);
1240
salut_tube_dbus_remove_name (SalutTubeDBus *self,
1243
SalutTubeDBusPrivate *priv = SALUT_TUBE_DBUS_GET_PRIVATE (self);
1244
TpHandleRepoIface *contact_repo = tp_base_connection_get_handles (
1245
(TpBaseConnection *) priv->conn, TP_HANDLE_TYPE_CONTACT);
1248
g_assert (priv->handle_type == TP_HANDLE_TYPE_ROOM);
1250
name = g_hash_table_lookup (priv->dbus_names, GUINT_TO_POINTER (handle));
1254
g_hash_table_remove (priv->dbus_names, GUINT_TO_POINTER (handle));
1256
tp_handle_unref (contact_repo, handle);
1261
salut_tube_dbus_handle_in_names (SalutTubeDBus *self,
1264
SalutTubeDBusPrivate *priv = SALUT_TUBE_DBUS_GET_PRIVATE (self);
1266
g_assert (priv->handle_type == TP_HANDLE_TYPE_ROOM);
1268
return (g_hash_table_lookup (priv->dbus_names, GUINT_TO_POINTER (handle))