4 This file is part of avahi.
6 avahi is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
11 avahi is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
14 Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with avahi; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
30 #include <dbus/dbus.h>
32 #include <avahi-client/client.h>
33 #include <avahi-common/dbus.h>
34 #include <avahi-common/llist.h>
35 #include <avahi-common/error.h>
36 #include <avahi-common/malloc.h>
41 void avahi_entry_group_set_state(AvahiEntryGroup *group, AvahiEntryGroupState state) {
44 if (group->state == state)
50 group->callback(group, state, group->userdata);
53 static int retrieve_state(AvahiEntryGroup *group) {
54 DBusMessage *message = NULL, *reply = NULL;
60 dbus_error_init(&error);
63 client = group->client;
65 if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, group->path, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "GetState"))) {
66 r = avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
70 if (!(reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error)) ||
71 dbus_error_is_set (&error)) {
72 r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
76 if (!dbus_message_get_args(reply, &error, DBUS_TYPE_INT32, &state, DBUS_TYPE_INVALID) ||
77 dbus_error_is_set (&error)) {
78 r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
82 dbus_message_unref(message);
83 dbus_message_unref(reply);
85 avahi_entry_group_set_state(group, (AvahiEntryGroupState) state);
90 if (dbus_error_is_set(&error)) {
91 r = avahi_client_set_dbus_error(client, &error);
92 dbus_error_free(&error);
96 dbus_message_unref(message);
99 dbus_message_unref(reply);
104 AvahiEntryGroup* avahi_entry_group_new (AvahiClient *client, AvahiEntryGroupCallback callback, void *userdata) {
105 AvahiEntryGroup *group = NULL;
106 DBusMessage *message = NULL, *reply = NULL;
112 dbus_error_init (&error);
114 if (!avahi_client_is_connected(client)) {
115 avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
119 if (!(group = avahi_new(AvahiEntryGroup, 1))) {
120 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
124 group->client = client;
125 group->callback = callback;
126 group->userdata = userdata;
127 group->state = AVAHI_ENTRY_GROUP_UNCOMMITED;
129 AVAHI_LLIST_PREPEND(AvahiEntryGroup, groups, client->groups, group);
131 if (!(message = dbus_message_new_method_call(
133 AVAHI_DBUS_PATH_SERVER,
134 AVAHI_DBUS_INTERFACE_SERVER,
136 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
140 if (!(reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error)) ||
141 dbus_error_is_set (&error)) {
142 avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
146 if (!dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID) ||
147 dbus_error_is_set (&error)) {
148 avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
152 if (!(group->path = avahi_strdup (path))) {
154 /* FIXME: We don't remove the object on the server side */
156 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
160 if (retrieve_state(group) < 0)
163 dbus_message_unref(message);
164 dbus_message_unref(reply);
169 if (dbus_error_is_set(&error)) {
170 avahi_client_set_dbus_error(client, &error);
171 dbus_error_free(&error);
175 avahi_entry_group_free(group);
178 dbus_message_unref(message);
181 dbus_message_unref(reply);
186 static int entry_group_simple_method_call(AvahiEntryGroup *group, const char *method) {
187 DBusMessage *message = NULL, *reply = NULL;
192 dbus_error_init(&error);
195 client = group->client;
197 if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, group->path, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, method))) {
198 r = avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
202 if (!(reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error)) ||
203 dbus_error_is_set (&error)) {
204 r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
208 if (!dbus_message_get_args(reply, &error, DBUS_TYPE_INVALID) ||
209 dbus_error_is_set (&error)) {
210 r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
214 dbus_message_unref(message);
215 dbus_message_unref(reply);
220 if (dbus_error_is_set(&error)) {
221 r = avahi_client_set_dbus_error(client, &error);
222 dbus_error_free(&error);
226 dbus_message_unref(message);
229 dbus_message_unref(reply);
234 int avahi_entry_group_free(AvahiEntryGroup *group) {
235 AvahiClient *client = group->client;
240 if (group->path && !avahi_client_is_connected(client))
241 r = entry_group_simple_method_call(group, "Free");
243 AVAHI_LLIST_REMOVE(AvahiEntryGroup, groups, client->groups, group);
245 avahi_free(group->path);
251 int avahi_entry_group_commit(AvahiEntryGroup *group) {
254 if (!group->path || !avahi_client_is_connected(group->client))
255 return avahi_client_set_errno(group->client, AVAHI_ERR_BAD_STATE);
257 return entry_group_simple_method_call(group, "Commit");
260 int avahi_entry_group_reset(AvahiEntryGroup *group) {
263 if (!group->path || !avahi_client_is_connected(group->client))
264 return avahi_client_set_errno(group->client, AVAHI_ERR_BAD_STATE);
266 return entry_group_simple_method_call(group, "Reset");
269 int avahi_entry_group_get_state (AvahiEntryGroup *group) {
275 AvahiClient* avahi_entry_group_get_client (AvahiEntryGroup *group) {
278 return group->client;
281 int avahi_entry_group_is_empty (AvahiEntryGroup *group) {
282 DBusMessage *message = NULL, *reply = NULL;
289 client = group->client;
291 if (!group->path || !avahi_client_is_connected(group->client))
292 return avahi_client_set_errno(group->client, AVAHI_ERR_BAD_STATE);
294 dbus_error_init(&error);
296 if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, group->path, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "IsEmpty"))) {
297 r = avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
301 if (!(reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error)) ||
302 dbus_error_is_set (&error)) {
303 r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
307 if (!dbus_message_get_args(reply, &error, DBUS_TYPE_BOOLEAN, &b, DBUS_TYPE_INVALID) ||
308 dbus_error_is_set (&error)) {
309 r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
313 dbus_message_unref(message);
314 dbus_message_unref(reply);
319 if (dbus_error_is_set(&error)) {
320 r = avahi_client_set_dbus_error(client, &error);
321 dbus_error_free(&error);
325 dbus_message_unref(message);
328 dbus_message_unref(reply);
333 static int append_rdata(DBusMessage *message, const void *rdata, size_t size) {
334 DBusMessageIter iter, sub;
338 dbus_message_iter_init_append(message, &iter);
340 if (!(dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE_AS_STRING, &sub)) ||
341 !(dbus_message_iter_append_fixed_array(&sub, DBUS_TYPE_BYTE, &rdata, size)) ||
342 !(dbus_message_iter_close_container(&iter, &sub)))
348 static int append_string_list(DBusMessage *message, AvahiStringList *txt) {
349 DBusMessageIter iter, sub;
355 dbus_message_iter_init_append(message, &iter);
357 /* Reverse the string list, so that we can pass it in-order to the server */
358 txt = avahi_string_list_reverse(txt);
360 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "ay", &sub))
363 /* Assemble the AvahiStringList into an Array of Array of Bytes to send over dbus */
364 for (p = txt; p != NULL; p = p->next) {
365 DBusMessageIter sub2;
366 const uint8_t *data = p->text;
368 if (!(dbus_message_iter_open_container(&sub, DBUS_TYPE_ARRAY, "y", &sub2)) ||
369 !(dbus_message_iter_append_fixed_array(&sub2, DBUS_TYPE_BYTE, &data, p->size)) ||
370 !(dbus_message_iter_close_container(&sub, &sub2)))
374 if (!dbus_message_iter_close_container(&iter, &sub))
381 /* Reverse the string list to the original state */
382 txt = avahi_string_list_reverse(txt);
387 int avahi_entry_group_add_service_strlst(
388 AvahiEntryGroup *group,
389 AvahiIfIndex interface,
390 AvahiProtocol protocol,
391 AvahiPublishFlags flags,
397 AvahiStringList *txt) {
399 DBusMessage *message = NULL, *reply = NULL;
403 int32_t i_interface, i_protocol;
410 client = group->client;
412 if (!group->path || !avahi_client_is_connected(group->client))
413 return avahi_client_set_errno(group->client, AVAHI_ERR_BAD_STATE);
421 dbus_error_init(&error);
423 if (!(message = dbus_message_new_method_call (AVAHI_DBUS_NAME, group->path, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "AddService"))) {
424 r = avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
428 i_interface = (int32_t) interface;
429 i_protocol = (int32_t) protocol;
430 u_flags = (uint32_t) flags;
432 if (!dbus_message_append_args(
434 DBUS_TYPE_INT32, &i_interface,
435 DBUS_TYPE_INT32, &i_protocol,
436 DBUS_TYPE_UINT32, &u_flags,
437 DBUS_TYPE_STRING, &name,
438 DBUS_TYPE_STRING, &type,
439 DBUS_TYPE_STRING, &domain,
440 DBUS_TYPE_STRING, &host,
441 DBUS_TYPE_UINT16, &port,
442 DBUS_TYPE_INVALID) ||
443 append_string_list(message, txt) < 0) {
444 r = avahi_client_set_errno(group->client, AVAHI_ERR_NO_MEMORY);
448 if (!(reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error)) ||
449 dbus_error_is_set (&error)) {
450 r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
454 if (!dbus_message_get_args(reply, &error, DBUS_TYPE_INVALID) ||
455 dbus_error_is_set (&error)) {
456 r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
460 dbus_message_unref(message);
461 dbus_message_unref(reply);
467 if (dbus_error_is_set(&error)) {
468 r = avahi_client_set_dbus_error(client, &error);
469 dbus_error_free(&error);
473 dbus_message_unref(message);
476 dbus_message_unref(reply);
481 int avahi_entry_group_add_service(
482 AvahiEntryGroup *group,
483 AvahiIfIndex interface,
484 AvahiProtocol protocol,
485 AvahiPublishFlags flags,
495 AvahiStringList *txt;
500 txt = avahi_string_list_new_va(va);
501 r = avahi_entry_group_add_service_strlst(group, interface, protocol, flags, name, type, domain, host, port, txt);
502 avahi_string_list_free(txt);
507 int avahi_entry_group_add_service_subtype(
508 AvahiEntryGroup *group,
509 AvahiIfIndex interface,
510 AvahiProtocol protocol,
511 AvahiPublishFlags flags,
515 const char *subtype) {
517 DBusMessage *message = NULL, *reply = NULL;
521 int32_t i_interface, i_protocol;
529 client = group->client;
531 if (!group->path || !avahi_client_is_connected(group->client))
532 return avahi_client_set_errno(group->client, AVAHI_ERR_BAD_STATE);
537 dbus_error_init(&error);
539 if (!(message = dbus_message_new_method_call (AVAHI_DBUS_NAME, group->path, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "AddServiceSubtype"))) {
540 r = avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
544 i_interface = (int32_t) interface;
545 i_protocol = (int32_t) protocol;
546 u_flags = (uint32_t) flags;
548 if (!dbus_message_append_args(
550 DBUS_TYPE_INT32, &i_interface,
551 DBUS_TYPE_INT32, &i_protocol,
552 DBUS_TYPE_UINT32, &u_flags,
553 DBUS_TYPE_STRING, &name,
554 DBUS_TYPE_STRING, &type,
555 DBUS_TYPE_STRING, &domain,
556 DBUS_TYPE_STRING, &subtype,
557 DBUS_TYPE_INVALID)) {
558 r = avahi_client_set_errno(group->client, AVAHI_ERR_NO_MEMORY);
562 if (!(reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error)) ||
563 dbus_error_is_set (&error)) {
564 r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
568 if (!dbus_message_get_args(reply, &error, DBUS_TYPE_INVALID) ||
569 dbus_error_is_set (&error)) {
570 r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
574 dbus_message_unref(message);
575 dbus_message_unref(reply);
581 if (dbus_error_is_set(&error)) {
582 r = avahi_client_set_dbus_error(client, &error);
583 dbus_error_free(&error);
587 dbus_message_unref(message);
590 dbus_message_unref(reply);
596 int avahi_entry_group_update_service_txt(
597 AvahiEntryGroup *group,
598 AvahiIfIndex interface,
599 AvahiProtocol protocol,
600 AvahiPublishFlags flags,
608 AvahiStringList *txt;
610 va_start(va, domain);
611 txt = avahi_string_list_new_va(va);
612 r = avahi_entry_group_update_service_txt_strlst(group, interface, protocol, flags, name, type, domain, txt);
613 avahi_string_list_free(txt);
618 int avahi_entry_group_update_service_txt_strlst(
619 AvahiEntryGroup *group,
620 AvahiIfIndex interface,
621 AvahiProtocol protocol,
622 AvahiPublishFlags flags,
626 AvahiStringList *txt) {
628 DBusMessage *message = NULL, *reply = NULL;
632 int32_t i_interface, i_protocol;
639 client = group->client;
641 if (!group->path || !avahi_client_is_connected(group->client))
642 return avahi_client_set_errno(group->client, AVAHI_ERR_BAD_STATE);
647 dbus_error_init(&error);
649 if (!(message = dbus_message_new_method_call (AVAHI_DBUS_NAME, group->path, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "UpdateServiceTxt"))) {
650 r = avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
654 i_interface = (int32_t) interface;
655 i_protocol = (int32_t) protocol;
656 u_flags = (uint32_t) flags;
658 if (!dbus_message_append_args(
660 DBUS_TYPE_INT32, &i_interface,
661 DBUS_TYPE_INT32, &i_protocol,
662 DBUS_TYPE_UINT32, &u_flags,
663 DBUS_TYPE_STRING, &name,
664 DBUS_TYPE_STRING, &type,
665 DBUS_TYPE_STRING, &domain,
666 DBUS_TYPE_INVALID) ||
667 append_string_list(message, txt) < 0) {
668 r = avahi_client_set_errno(group->client, AVAHI_ERR_NO_MEMORY);
672 if (!(reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error)) ||
673 dbus_error_is_set (&error)) {
674 r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
678 if (!dbus_message_get_args(reply, &error, DBUS_TYPE_INVALID) ||
679 dbus_error_is_set (&error)) {
680 r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
684 dbus_message_unref(message);
685 dbus_message_unref(reply);
691 if (dbus_error_is_set(&error)) {
692 r = avahi_client_set_dbus_error(client, &error);
693 dbus_error_free(&error);
697 dbus_message_unref(message);
700 dbus_message_unref(reply);
707 /** Add a host/address pair */
708 int avahi_entry_group_add_address(
709 AvahiEntryGroup *group,
710 AvahiIfIndex interface,
711 AvahiProtocol protocol,
712 AvahiPublishFlags flags,
714 const AvahiAddress *a) {
716 DBusMessage *message = NULL, *reply = NULL;
720 int32_t i_interface, i_protocol;
722 char s_address[AVAHI_ADDRESS_STR_MAX];
723 char *p_address = s_address;
727 client = group->client;
729 if (!group->path || !avahi_client_is_connected(group->client))
730 return avahi_client_set_errno(group->client, AVAHI_ERR_BAD_STATE);
732 dbus_error_init(&error);
734 if (!(message = dbus_message_new_method_call (AVAHI_DBUS_NAME, group->path, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "AddAddress"))) {
735 r = avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
739 i_interface = (int32_t) interface;
740 i_protocol = (int32_t) protocol;
741 u_flags = (uint32_t) flags;
743 if (!avahi_address_snprint (s_address, sizeof (s_address), a))
745 r = avahi_client_set_errno(client, AVAHI_ERR_INVALID_ADDRESS);
749 if (!dbus_message_append_args(
751 DBUS_TYPE_INT32, &i_interface,
752 DBUS_TYPE_INT32, &i_protocol,
753 DBUS_TYPE_UINT32, &u_flags,
754 DBUS_TYPE_STRING, &name,
755 DBUS_TYPE_STRING, &p_address,
756 DBUS_TYPE_INVALID)) {
757 r = avahi_client_set_errno(group->client, AVAHI_ERR_NO_MEMORY);
761 if (!(reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error)) ||
762 dbus_error_is_set (&error)) {
763 r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
767 if (!dbus_message_get_args(reply, &error, DBUS_TYPE_INVALID) ||
768 dbus_error_is_set (&error)) {
769 r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
773 dbus_message_unref(message);
774 dbus_message_unref(reply);
780 if (dbus_error_is_set(&error)) {
781 r = avahi_client_set_dbus_error(client, &error);
782 dbus_error_free(&error);
786 dbus_message_unref(message);
789 dbus_message_unref(reply);
794 /** Add an arbitrary record */
795 int avahi_entry_group_add_record(
796 AvahiEntryGroup *group,
797 AvahiIfIndex interface,
798 AvahiProtocol protocol,
799 AvahiPublishFlags flags,
807 DBusMessage *message = NULL, *reply = NULL;
811 int32_t i_interface, i_protocol;
816 client = group->client;
818 if (!group->path || !avahi_client_is_connected(group->client))
819 return avahi_client_set_errno(group->client, AVAHI_ERR_BAD_STATE);
821 dbus_error_init(&error);
823 if (!(message = dbus_message_new_method_call (AVAHI_DBUS_NAME, group->path, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "AddRecord"))) {
824 r = avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
828 i_interface = (int32_t) interface;
829 i_protocol = (int32_t) protocol;
830 u_flags = (uint32_t) flags;
832 if (!dbus_message_append_args(
834 DBUS_TYPE_INT32, &i_interface,
835 DBUS_TYPE_INT32, &i_protocol,
836 DBUS_TYPE_UINT32, &u_flags,
837 DBUS_TYPE_STRING, &name,
838 DBUS_TYPE_UINT16, &clazz,
839 DBUS_TYPE_UINT16, &type,
840 DBUS_TYPE_UINT32, &ttl,
841 DBUS_TYPE_INVALID) || append_rdata(message, rdata, size) < 0) {
842 r = avahi_client_set_errno(group->client, AVAHI_ERR_NO_MEMORY);
846 if (!(reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error)) ||
847 dbus_error_is_set (&error)) {
848 r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
852 if (!dbus_message_get_args(reply, &error, DBUS_TYPE_INVALID) ||
853 dbus_error_is_set (&error)) {
854 r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
858 dbus_message_unref(message);
859 dbus_message_unref(reply);
865 if (dbus_error_is_set(&error)) {
866 r = avahi_client_set_dbus_error(client, &error);
867 dbus_error_free(&error);
871 dbus_message_unref(message);
874 dbus_message_unref(reply);