2 This file is part of avahi.
4 avahi is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
9 avahi is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
12 Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with avahi; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
28 #include <dbus/dbus.h>
30 #include <avahi-client/client.h>
31 #include <avahi-common/dbus.h>
32 #include <avahi-common/llist.h>
33 #include <avahi-common/error.h>
34 #include <avahi-common/malloc.h>
39 void avahi_entry_group_set_state(AvahiEntryGroup *group, AvahiEntryGroupState state) {
42 if (group->state_valid && group->state == state)
46 group->state_valid = 1;
49 group->callback(group, state, group->userdata);
52 static int retrieve_state(AvahiEntryGroup *group) {
53 DBusMessage *message = NULL, *reply = NULL;
59 dbus_error_init(&error);
62 client = group->client;
64 if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, group->path, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "GetState"))) {
65 r = avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
69 if (!(reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error)) ||
70 dbus_error_is_set (&error)) {
71 r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
75 if (!dbus_message_get_args(reply, &error, DBUS_TYPE_INT32, &state, DBUS_TYPE_INVALID) ||
76 dbus_error_is_set (&error)) {
77 r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
81 dbus_message_unref(message);
82 dbus_message_unref(reply);
87 if (dbus_error_is_set(&error)) {
88 r = avahi_client_set_dbus_error(client, &error);
89 dbus_error_free(&error);
93 dbus_message_unref(message);
96 dbus_message_unref(reply);
101 AvahiEntryGroup* avahi_entry_group_new (AvahiClient *client, AvahiEntryGroupCallback callback, void *userdata) {
102 AvahiEntryGroup *group = NULL;
103 DBusMessage *message = NULL, *reply = NULL;
110 dbus_error_init (&error);
112 if (!avahi_client_is_connected(client)) {
113 avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
117 if (!(group = avahi_new(AvahiEntryGroup, 1))) {
118 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
122 group->client = client;
123 group->callback = callback;
124 group->userdata = userdata;
125 group->state_valid = 0;
127 AVAHI_LLIST_PREPEND(AvahiEntryGroup, groups, client->groups, group);
129 if (!(message = dbus_message_new_method_call(
131 AVAHI_DBUS_PATH_SERVER,
132 AVAHI_DBUS_INTERFACE_SERVER,
134 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
138 if (!(reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error)) ||
139 dbus_error_is_set (&error)) {
140 avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
144 if (!dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID) ||
145 dbus_error_is_set (&error)) {
146 avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
150 if (!(group->path = avahi_strdup (path))) {
152 /* FIXME: We don't remove the object on the server side */
154 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
158 if ((state = retrieve_state(group)) < 0) {
159 avahi_client_set_errno(client, state);
163 avahi_entry_group_set_state(group, (AvahiEntryGroupState) state);
165 dbus_message_unref(message);
166 dbus_message_unref(reply);
171 if (dbus_error_is_set(&error)) {
172 avahi_client_set_dbus_error(client, &error);
173 dbus_error_free(&error);
177 avahi_entry_group_free(group);
180 dbus_message_unref(message);
183 dbus_message_unref(reply);
188 static int entry_group_simple_method_call(AvahiEntryGroup *group, const char *method) {
189 DBusMessage *message = NULL, *reply = NULL;
194 dbus_error_init(&error);
197 client = group->client;
199 if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, group->path, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, method))) {
200 r = avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
204 if (!(reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error)) ||
205 dbus_error_is_set (&error)) {
206 r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
210 if (!dbus_message_get_args(reply, &error, DBUS_TYPE_INVALID) ||
211 dbus_error_is_set (&error)) {
212 r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
216 dbus_message_unref(message);
217 dbus_message_unref(reply);
222 if (dbus_error_is_set(&error)) {
223 r = avahi_client_set_dbus_error(client, &error);
224 dbus_error_free(&error);
228 dbus_message_unref(message);
231 dbus_message_unref(reply);
236 int avahi_entry_group_free(AvahiEntryGroup *group) {
237 AvahiClient *client = group->client;
242 if (group->path && avahi_client_is_connected(client))
243 r = entry_group_simple_method_call(group, "Free");
245 AVAHI_LLIST_REMOVE(AvahiEntryGroup, groups, client->groups, group);
247 avahi_free(group->path);
253 int avahi_entry_group_commit(AvahiEntryGroup *group) {
257 if (!group->path || !avahi_client_is_connected(group->client))
258 return avahi_client_set_errno(group->client, AVAHI_ERR_BAD_STATE);
260 if ((ret = entry_group_simple_method_call(group, "Commit")) < 0)
263 group->state_valid = 0;
267 int avahi_entry_group_reset(AvahiEntryGroup *group) {
271 if (!group->path || !avahi_client_is_connected(group->client))
272 return avahi_client_set_errno(group->client, AVAHI_ERR_BAD_STATE);
274 if ((ret = entry_group_simple_method_call(group, "Reset")) < 0)
277 group->state_valid = 0;
281 int avahi_entry_group_get_state (AvahiEntryGroup *group) {
284 if (group->state_valid)
287 return retrieve_state(group);
290 AvahiClient* avahi_entry_group_get_client (AvahiEntryGroup *group) {
293 return group->client;
296 int avahi_entry_group_is_empty (AvahiEntryGroup *group) {
297 DBusMessage *message = NULL, *reply = NULL;
304 client = group->client;
306 if (!group->path || !avahi_client_is_connected(group->client))
307 return avahi_client_set_errno(group->client, AVAHI_ERR_BAD_STATE);
309 dbus_error_init(&error);
311 if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, group->path, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "IsEmpty"))) {
312 r = avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
316 if (!(reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error)) ||
317 dbus_error_is_set (&error)) {
318 r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
322 if (!dbus_message_get_args(reply, &error, DBUS_TYPE_BOOLEAN, &b, DBUS_TYPE_INVALID) ||
323 dbus_error_is_set (&error)) {
324 r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
328 dbus_message_unref(message);
329 dbus_message_unref(reply);
334 if (dbus_error_is_set(&error)) {
335 r = avahi_client_set_dbus_error(client, &error);
336 dbus_error_free(&error);
340 dbus_message_unref(message);
343 dbus_message_unref(reply);
348 static int append_rdata(DBusMessage *message, const void *rdata, size_t size) {
349 DBusMessageIter iter, sub;
353 dbus_message_iter_init_append(message, &iter);
355 if (!(dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE_AS_STRING, &sub)) ||
356 !(dbus_message_iter_append_fixed_array(&sub, DBUS_TYPE_BYTE, &rdata, size)) ||
357 !(dbus_message_iter_close_container(&iter, &sub)))
363 static int append_string_list(DBusMessage *message, AvahiStringList *txt) {
364 DBusMessageIter iter, sub;
370 dbus_message_iter_init_append(message, &iter);
372 /* Reverse the string list, so that we can pass it in-order to the server */
373 txt = avahi_string_list_reverse(txt);
375 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "ay", &sub))
378 /* Assemble the AvahiStringList into an Array of Array of Bytes to send over dbus */
379 for (p = txt; p != NULL; p = p->next) {
380 DBusMessageIter sub2;
381 const uint8_t *data = p->text;
383 if (!(dbus_message_iter_open_container(&sub, DBUS_TYPE_ARRAY, "y", &sub2)) ||
384 !(dbus_message_iter_append_fixed_array(&sub2, DBUS_TYPE_BYTE, &data, p->size)) ||
385 !(dbus_message_iter_close_container(&sub, &sub2)))
389 if (!dbus_message_iter_close_container(&iter, &sub))
396 /* Reverse the string list to the original state */
397 txt = avahi_string_list_reverse(txt);
402 int avahi_entry_group_add_service_strlst(
403 AvahiEntryGroup *group,
404 AvahiIfIndex interface,
405 AvahiProtocol protocol,
406 AvahiPublishFlags flags,
412 AvahiStringList *txt) {
414 DBusMessage *message = NULL, *reply = NULL;
418 int32_t i_interface, i_protocol;
425 client = group->client;
427 if (!group->path || !avahi_client_is_connected(group->client))
428 return avahi_client_set_errno(group->client, AVAHI_ERR_BAD_STATE);
436 dbus_error_init(&error);
438 if (!(message = dbus_message_new_method_call (AVAHI_DBUS_NAME, group->path, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "AddService"))) {
439 r = avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
443 i_interface = (int32_t) interface;
444 i_protocol = (int32_t) protocol;
445 u_flags = (uint32_t) flags;
447 if (!dbus_message_append_args(
449 DBUS_TYPE_INT32, &i_interface,
450 DBUS_TYPE_INT32, &i_protocol,
451 DBUS_TYPE_UINT32, &u_flags,
452 DBUS_TYPE_STRING, &name,
453 DBUS_TYPE_STRING, &type,
454 DBUS_TYPE_STRING, &domain,
455 DBUS_TYPE_STRING, &host,
456 DBUS_TYPE_UINT16, &port,
457 DBUS_TYPE_INVALID) ||
458 append_string_list(message, txt) < 0) {
459 r = avahi_client_set_errno(group->client, AVAHI_ERR_NO_MEMORY);
463 if (!(reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error)) ||
464 dbus_error_is_set (&error)) {
465 r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
469 if (!dbus_message_get_args(reply, &error, DBUS_TYPE_INVALID) ||
470 dbus_error_is_set (&error)) {
471 r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
475 dbus_message_unref(message);
476 dbus_message_unref(reply);
482 if (dbus_error_is_set(&error)) {
483 r = avahi_client_set_dbus_error(client, &error);
484 dbus_error_free(&error);
488 dbus_message_unref(message);
491 dbus_message_unref(reply);
496 int avahi_entry_group_add_service(
497 AvahiEntryGroup *group,
498 AvahiIfIndex interface,
499 AvahiProtocol protocol,
500 AvahiPublishFlags flags,
510 AvahiStringList *txt;
515 txt = avahi_string_list_new_va(va);
516 r = avahi_entry_group_add_service_strlst(group, interface, protocol, flags, name, type, domain, host, port, txt);
517 avahi_string_list_free(txt);
522 int avahi_entry_group_add_service_subtype(
523 AvahiEntryGroup *group,
524 AvahiIfIndex interface,
525 AvahiProtocol protocol,
526 AvahiPublishFlags flags,
530 const char *subtype) {
532 DBusMessage *message = NULL, *reply = NULL;
536 int32_t i_interface, i_protocol;
544 client = group->client;
546 if (!group->path || !avahi_client_is_connected(group->client))
547 return avahi_client_set_errno(group->client, AVAHI_ERR_BAD_STATE);
552 dbus_error_init(&error);
554 if (!(message = dbus_message_new_method_call (AVAHI_DBUS_NAME, group->path, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "AddServiceSubtype"))) {
555 r = avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
559 i_interface = (int32_t) interface;
560 i_protocol = (int32_t) protocol;
561 u_flags = (uint32_t) flags;
563 if (!dbus_message_append_args(
565 DBUS_TYPE_INT32, &i_interface,
566 DBUS_TYPE_INT32, &i_protocol,
567 DBUS_TYPE_UINT32, &u_flags,
568 DBUS_TYPE_STRING, &name,
569 DBUS_TYPE_STRING, &type,
570 DBUS_TYPE_STRING, &domain,
571 DBUS_TYPE_STRING, &subtype,
572 DBUS_TYPE_INVALID)) {
573 r = avahi_client_set_errno(group->client, AVAHI_ERR_NO_MEMORY);
577 if (!(reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error)) ||
578 dbus_error_is_set (&error)) {
579 r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
583 if (!dbus_message_get_args(reply, &error, DBUS_TYPE_INVALID) ||
584 dbus_error_is_set (&error)) {
585 r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
589 dbus_message_unref(message);
590 dbus_message_unref(reply);
596 if (dbus_error_is_set(&error)) {
597 r = avahi_client_set_dbus_error(client, &error);
598 dbus_error_free(&error);
602 dbus_message_unref(message);
605 dbus_message_unref(reply);
611 int avahi_entry_group_update_service_txt(
612 AvahiEntryGroup *group,
613 AvahiIfIndex interface,
614 AvahiProtocol protocol,
615 AvahiPublishFlags flags,
623 AvahiStringList *txt;
625 va_start(va, domain);
626 txt = avahi_string_list_new_va(va);
627 r = avahi_entry_group_update_service_txt_strlst(group, interface, protocol, flags, name, type, domain, txt);
628 avahi_string_list_free(txt);
633 int avahi_entry_group_update_service_txt_strlst(
634 AvahiEntryGroup *group,
635 AvahiIfIndex interface,
636 AvahiProtocol protocol,
637 AvahiPublishFlags flags,
641 AvahiStringList *txt) {
643 DBusMessage *message = NULL, *reply = NULL;
647 int32_t i_interface, i_protocol;
654 client = group->client;
656 if (!group->path || !avahi_client_is_connected(group->client))
657 return avahi_client_set_errno(group->client, AVAHI_ERR_BAD_STATE);
662 dbus_error_init(&error);
664 if (!(message = dbus_message_new_method_call (AVAHI_DBUS_NAME, group->path, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "UpdateServiceTxt"))) {
665 r = avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
669 i_interface = (int32_t) interface;
670 i_protocol = (int32_t) protocol;
671 u_flags = (uint32_t) flags;
673 if (!dbus_message_append_args(
675 DBUS_TYPE_INT32, &i_interface,
676 DBUS_TYPE_INT32, &i_protocol,
677 DBUS_TYPE_UINT32, &u_flags,
678 DBUS_TYPE_STRING, &name,
679 DBUS_TYPE_STRING, &type,
680 DBUS_TYPE_STRING, &domain,
681 DBUS_TYPE_INVALID) ||
682 append_string_list(message, txt) < 0) {
683 r = avahi_client_set_errno(group->client, AVAHI_ERR_NO_MEMORY);
687 if (!(reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error)) ||
688 dbus_error_is_set (&error)) {
689 r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
693 if (!dbus_message_get_args(reply, &error, DBUS_TYPE_INVALID) ||
694 dbus_error_is_set (&error)) {
695 r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
699 dbus_message_unref(message);
700 dbus_message_unref(reply);
706 if (dbus_error_is_set(&error)) {
707 r = avahi_client_set_dbus_error(client, &error);
708 dbus_error_free(&error);
712 dbus_message_unref(message);
715 dbus_message_unref(reply);
720 /** Add a host/address pair */
721 int avahi_entry_group_add_address(
722 AvahiEntryGroup *group,
723 AvahiIfIndex interface,
724 AvahiProtocol protocol,
725 AvahiPublishFlags flags,
727 const AvahiAddress *a) {
729 DBusMessage *message = NULL, *reply = NULL;
733 int32_t i_interface, i_protocol;
735 char s_address[AVAHI_ADDRESS_STR_MAX];
736 char *p_address = s_address;
740 client = group->client;
742 if (!group->path || !avahi_client_is_connected(group->client))
743 return avahi_client_set_errno(group->client, AVAHI_ERR_BAD_STATE);
745 dbus_error_init(&error);
747 if (!(message = dbus_message_new_method_call (AVAHI_DBUS_NAME, group->path, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "AddAddress"))) {
748 r = avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
752 i_interface = (int32_t) interface;
753 i_protocol = (int32_t) protocol;
754 u_flags = (uint32_t) flags;
756 if (!avahi_address_snprint (s_address, sizeof (s_address), a))
758 r = avahi_client_set_errno(client, AVAHI_ERR_INVALID_ADDRESS);
762 if (!dbus_message_append_args(
764 DBUS_TYPE_INT32, &i_interface,
765 DBUS_TYPE_INT32, &i_protocol,
766 DBUS_TYPE_UINT32, &u_flags,
767 DBUS_TYPE_STRING, &name,
768 DBUS_TYPE_STRING, &p_address,
769 DBUS_TYPE_INVALID)) {
770 r = avahi_client_set_errno(group->client, AVAHI_ERR_NO_MEMORY);
774 if (!(reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error)) ||
775 dbus_error_is_set (&error)) {
776 r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
780 if (!dbus_message_get_args(reply, &error, DBUS_TYPE_INVALID) ||
781 dbus_error_is_set (&error)) {
782 r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
786 dbus_message_unref(message);
787 dbus_message_unref(reply);
793 if (dbus_error_is_set(&error)) {
794 r = avahi_client_set_dbus_error(client, &error);
795 dbus_error_free(&error);
799 dbus_message_unref(message);
802 dbus_message_unref(reply);
807 /** Add an arbitrary record */
808 int avahi_entry_group_add_record(
809 AvahiEntryGroup *group,
810 AvahiIfIndex interface,
811 AvahiProtocol protocol,
812 AvahiPublishFlags flags,
820 DBusMessage *message = NULL, *reply = NULL;
824 int32_t i_interface, i_protocol;
829 client = group->client;
831 if (!group->path || !avahi_client_is_connected(group->client))
832 return avahi_client_set_errno(group->client, AVAHI_ERR_BAD_STATE);
834 dbus_error_init(&error);
836 if (!(message = dbus_message_new_method_call (AVAHI_DBUS_NAME, group->path, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "AddRecord"))) {
837 r = avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
841 i_interface = (int32_t) interface;
842 i_protocol = (int32_t) protocol;
843 u_flags = (uint32_t) flags;
845 if (!dbus_message_append_args(
847 DBUS_TYPE_INT32, &i_interface,
848 DBUS_TYPE_INT32, &i_protocol,
849 DBUS_TYPE_UINT32, &u_flags,
850 DBUS_TYPE_STRING, &name,
851 DBUS_TYPE_UINT16, &clazz,
852 DBUS_TYPE_UINT16, &type,
853 DBUS_TYPE_UINT32, &ttl,
854 DBUS_TYPE_INVALID) || append_rdata(message, rdata, size) < 0) {
855 r = avahi_client_set_errno(group->client, AVAHI_ERR_NO_MEMORY);
859 if (!(reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error)) ||
860 dbus_error_is_set (&error)) {
861 r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
865 if (!dbus_message_get_args(reply, &error, DBUS_TYPE_INVALID) ||
866 dbus_error_is_set (&error)) {
867 r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
871 dbus_message_unref(message);
872 dbus_message_unref(reply);
878 if (dbus_error_is_set(&error)) {
879 r = avahi_client_set_dbus_error(client, &error);
880 dbus_error_free(&error);
884 dbus_message_unref(message);
887 dbus_message_unref(reply);