DBUS as IPC medium for communicating signals/events
Thiago Macieira
thiago at kde.org
Wed Aug 1 05:16:32 PDT 2012
On quarta-feira, 1 de agosto de 2012 17.26.09, rajnikant jachak wrote:
> Hi All,
>
> I'm considering DBUS (version-1.6.2/ linux platform) as option to signal
> different modules within event driven system. Eg. Module-A sends signal to
> Module-B, Module-B process signal and replies module-A with response
> signal.
>
> So I wanted to send some extra information with signal across different
> module. I tried sending extra information in Byte array. Following is code
> to do so:
>
> struct MyExample {
> char cchar[30];
> int iint;
> double ddouble;
> char c;
> };
>
> /--------------- Send Signal Code -------------------/
> // Extra Information with signal is in MyExample Object
> struct MyExample obj;
> strcpy(obj.cchar,"String");
> obj.iint = 9209;
> obj.ddouble = 10.12;
> obj.c = 'A';
> char tBuff[100];
> memcpy(tBuff, &obj,sizeof(obj)); // Serialising obj into buffer
If you send this buffer, please note that you might be leaking security-
sensitive data to the world. The structure you defined above, with 30
characters in a string, did not get overwritten by that strcpy, so it still
contain information from the stack which could be important.
You wanted strncpy -- and really, that's the only place where strncpy's weird
length behaviour comes in handy.
> int len = sizeof(obj);
> unsigned char *val = (unsigned char *)tBuff;
> if (!dbus_message_append_args (signal,
> DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &val, len,
> DBUS_TYPE_INVALID))
> die ("No memory");
> if (!dbus_connection_send (connection, signal, NULL))
> die ("No memory\n");
> /------------------ End Code ----------------/
>
> On receiver side, ideally I should get correct 'extra information' after
> type casting the byte array I would be getting by
> dbus_message_iter_get_fixed_array API. But when I type cast the buffer, I'm
> seeing some weird data. Following is the code to read the byte array:
> /---------------- Processing Signal --------------/
> DBusMessageIter iter;
> dbus_message_iter_init (message,& iter);
> int argType, eleType;
> argType = dbus_message_iter_get_arg_type(&iter);
> if (argType == DBUS_TYPE_ARRAY) {
> if (dbus_message_iter_get_element_type(&iter) ==
> DBUS_TYPE_BYTE) {
> DBusMessageIter arrayIter;
> dbus_message_iter_recurse(&iter,&arrayIter);
> unsigned char buff[50];
> int length;
>
> dbus_message_iter_get_fixed_array(&arrayIter,&buff,&length);
> struct MyExample *obj = (struct MyExample*) buff;
> printf ("\nLength: %d Name: %s, Int: %d, Double:%f,
> Char: %c\n",length, obj->cchar, obj->iint,obj->ddouble,obj->c);
> }
> }
> /---------------- End Processing -----------------/
>
> I got the correct information when I used 'dbus_message_iter_get_basic' API
> iterating over byte array. But I dont want to use this method as its
> execution is expensive.
You passed the wrong parameters to dbus_message_iter_get_fixed_array.
The correct code would have been:
int len;
char* data;
dbus_message_iter_get_fixed_array(&arrayIter,&data,&len);
You get a pointer to the message's internal buffers. You can use it as long as
you don't unref that message.
> 1. Basic question is, is Byte Array is good choice to use for information
> forwarding in DBUS?
No. D-Bus has a proper complex type system. You can pass your struct in a
nicer manner and avoid the security leak I pointed out.
Note that D-Bus strings are *always* UTF-8 encoded. If your char* strings are
arbitrary data, use byte arrays, not strings.
> 2. What am I missing cause of which I'm not getting correct data on
> receiver side?
--
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
Software Architect - Intel Open Source Technology Center
PGP/GPG: 0x6EF45358; fingerprint:
E067 918B B660 DBD1 105C 966C 33F5 F005 6EF4 5358
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 190 bytes
Desc: This is a digitally signed message part.
URL: <http://lists.freedesktop.org/archives/dbus/attachments/20120801/384906e5/attachment.pgp>
More information about the dbus
mailing list