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_valid && group->state == state)
48 group->state_valid = 1;
51 group->callback(group, state, group->userdata);
54 static int retrieve_state(AvahiEntryGroup *group) {
55 DBusMessage *message = NULL, *reply = NULL;
61 dbus_error_init(&error);
64 client = group->client;
66 if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, group->path, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "GetState"))) {
67 r = avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
71 if (!(reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error)) ||
72 dbus_error_is_set (&error)) {
73 r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
77 if (!dbus_message_get_args(reply, &error, DBUS_TYPE_INT32, &state, DBUS_TYPE_INVALID) ||
78 dbus_error_is_set (&error)) {
79 r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
83 dbus_message_unref(message);
84 dbus_message_unref(reply);
89 if (dbus_error_is_set(&error)) {
90 r = avahi_client_set_dbus_error(client, &error);
91 dbus_error_free(&error);
95 dbus_message_unref(message);
98 dbus_message_unref(reply);
103 AvahiEntryGroup* avahi_entry_group_new (AvahiClient *client, AvahiEntryGroupCallback callback, void *userdata) {
104 AvahiEntryGroup *group = NULL;
105 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_valid = 0;
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 ((state = retrieve_state(group)) < 0) {
161 avahi_client_set_errno(client, state);
165 avahi_entry_group_set_state(group, (AvahiEntryGroupState) state);
167 dbus_message_unref(message);
168 dbus_message_unref(reply);
173 if (dbus_error_is_set(&error)) {
174 avahi_client_set_dbus_error(client, &error);
175 dbus_error_free(&error);
179 avahi_entry_group_free(group);
182 dbus_message_unref(message);
185 dbus_message_unref(reply);
190 static int entry_group_simple_method_call(AvahiEntryGroup *group, const char *method) {
191 DBusMessage *message = NULL, *reply = NULL;
196 dbus_error_init(&error);
199 client = group->client;
201 if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, group->path, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, method))) {
202 r = avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
206 if (!(reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error)) ||
207 dbus_error_is_set (&error)) {
208 r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
212 if (!dbus_message_get_args(reply, &error, DBUS_TYPE_INVALID) ||
213 dbus_error_is_set (&error)) {
214 r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
218 dbus_message_unref(message);
219 dbus_message_unref(reply);
224 if (dbus_error_is_set(&error)) {
225 r = avahi_client_set_dbus_error(client, &error);
226 dbus_error_free(&error);
230 dbus_message_unref(message);
233 dbus_message_unref(reply);
238 int avahi_entry_group_free(AvahiEntryGroup *group) {
239 AvahiClient *client = group->client;
244 if (group->path && avahi_client_is_connected(client))
245 r = entry_group_simple_method_call(group, "Free");
247 AVAHI_LLIST_REMOVE(AvahiEntryGroup, groups, client->groups, group);
249 avahi_free(group->path);
255 int avahi_entry_group_commit(AvahiEntryGroup *group) {
259 if (!group->path || !avahi_client_is_connected(group->client))
260 return avahi_client_set_errno(group->client, AVAHI_ERR_BAD_STATE);
262 if ((ret = entry_group_simple_method_call(group, "Commit")) < 0)
265 group->state_valid = 0;
269 int avahi_entry_group_reset(AvahiEntryGroup *group) {
273 if (!group->path || !avahi_client_is_connected(group->client))
274 return avahi_client_set_errno(group->client, AVAHI_ERR_BAD_STATE);
276 if ((ret = entry_group_simple_method_call(group, "Reset")) < 0)
279 group->state_valid = 0;
283 int avahi_entry_group_get_state (AvahiEntryGroup *group) {
286 if (group->state_valid)
289 return retrieve_state(group);
292 AvahiClient* avahi_entry_group_get_client (AvahiEntryGroup *group) {
295 return group->client;
298 int avahi_entry_group_is_empty (AvahiEntryGroup *group) {
299 DBusMessage *message = NULL, *reply = NULL;
306 client = group->client;
308 if (!group->path || !avahi_client_is_connected(group->client))
309 return avahi_client_set_errno(group->client, AVAHI_ERR_BAD_STATE);
311 dbus_error_init(&error);
313 if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, group->path, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "IsEmpty"))) {
314 r = avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
318 if (!(reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error)) ||
319 dbus_error_is_set (&error)) {
320 r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
324 if (!dbus_message_get_args(reply, &error, DBUS_TYPE_BOOLEAN, &b, DBUS_TYPE_INVALID) ||
325 dbus_error_is_set (&error)) {
326 r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
330 dbus_message_unref(message);
331 dbus_message_unref(reply);
336 if (dbus_error_is_set(&error)) {
337 r = avahi_client_set_dbus_error(client, &error);
338 dbus_error_free(&error);
342 dbus_message_unref(message);
345 dbus_message_unref(reply);
350 static int append_rdata(DBusMessage *message, const void *rdata, size_t size) {
351 DBusMessageIter iter, sub;
355 dbus_message_iter_init_append(message, &iter);
357 if (!(dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE_AS_STRING, &sub)) ||
358 !(dbus_message_iter_append_fixed_array(&sub, DBUS_TYPE_BYTE, &rdata, size)) ||
359 !(dbus_message_iter_close_container(&iter, &sub)))
365 static int append_string_list(DBusMessage *message, AvahiStringList *txt) {
366 DBusMessageIter iter, sub;
372 dbus_message_iter_init_append(message, &iter);
374 /* Reverse the string list, so that we can pass it in-order to the server */
375 txt = avahi_string_list_reverse(txt);
377 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "ay", &sub))
380 /* Assemble the AvahiStringList into an Array of Array of Bytes to send over dbus */
381 for (p = txt; p != NULL; p = p->next) {
382 DBusMessageIter sub2;
383 const uint8_t *data = p->text;
385 if (!(dbus_message_iter_open_container(&sub, DBUS_TYPE_ARRAY, "y", &sub2)) ||
386 !(dbus_message_iter_append_fixed_array(&sub2, DBUS_TYPE_BYTE, &data, p->size)) ||
387 !(dbus_message_iter_close_container(&sub, &sub2)))
391 if (!dbus_message_iter_close_container(&iter, &sub))
398 /* Reverse the string list to the original state */
399 txt = avahi_string_list_reverse(txt);
404 int avahi_entry_group_add_service_strlst(
405 AvahiEntryGroup *group,
406 AvahiIfIndex interface,
407 AvahiProtocol protocol,
408 AvahiPublishFlags flags,
414 AvahiStringList *txt) {
416 DBusMessage *message = NULL, *reply = NULL;
420 int32_t i_interface, i_protocol;
427 client = group->client;
429 if (!group->path || !avahi_client_is_connected(group->client))
430 return avahi_client_set_errno(group->client, AVAHI_ERR_BAD_STATE);
438 dbus_error_init(&error);
440 if (!(message = dbus_message_new_method_call (AVAHI_DBUS_NAME, group->path, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "AddService"))) {
441 r = avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
445 i_interface = (int32_t) interface;
446 i_protocol = (int32_t) protocol;
447 u_flags = (uint32_t) flags;
449 if (!dbus_message_append_args(
451 DBUS_TYPE_INT32, &i_interface,
452 DBUS_TYPE_INT32, &i_protocol,
453 DBUS_TYPE_UINT32, &u_flags,
454 DBUS_TYPE_STRING, &name,
455 DBUS_TYPE_STRING, &type,
456 DBUS_TYPE_STRING, &domain,
457 DBUS_TYPE_STRING, &host,
458 DBUS_TYPE_UINT16, &port,
459 DBUS_TYPE_INVALID) ||
460 append_string_list(message, txt) < 0) {
461 r = avahi_client_set_errno(group->client, AVAHI_ERR_NO_MEMORY);
465 if (!(reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error)) ||
466 dbus_error_is_set (&error)) {
467 r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
471 if (!dbus_message_get_args(reply, &error, DBUS_TYPE_INVALID) ||
472 dbus_error_is_set (&error)) {
473 r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
477 dbus_message_unref(message);
478 dbus_message_unref(reply);
484 if (dbus_error_is_set(&error)) {
485 r = avahi_client_set_dbus_error(client, &error);
486 dbus_error_free(&error);
490 dbus_message_unref(message);
493 dbus_message_unref(reply);
498 int avahi_entry_group_add_service(
499 AvahiEntryGroup *group,
500 AvahiIfIndex interface,
501 AvahiProtocol protocol,
502 AvahiPublishFlags flags,
512 AvahiStringList *txt;
517 txt = avahi_string_list_new_va(va);
518 r = avahi_entry_group_add_service_strlst(group, interface, protocol, flags, name, type, domain, host, port, txt);
519 avahi_string_list_free(txt);
524 int avahi_entry_group_add_service_subtype(
525 AvahiEntryGroup *group,
526 AvahiIfIndex interface,
527 AvahiProtocol protocol,
528 AvahiPublishFlags flags,
532 const char *subtype) {
534 DBusMessage *message = NULL, *reply = NULL;
538 int32_t i_interface, i_protocol;
546 client = group->client;
548 if (!group->path || !avahi_client_is_connected(group->client))
549 return avahi_client_set_errno(group->client, AVAHI_ERR_BAD_STATE);
554 dbus_error_init(&error);
556 if (!(message = dbus_message_new_method_call (AVAHI_DBUS_NAME, group->path, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "AddServiceSubtype"))) {
557 r = avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
561 i_interface = (int32_t) interface;
562 i_protocol = (int32_t) protocol;
563 u_flags = (uint32_t) flags;
565 if (!dbus_message_append_args(
567 DBUS_TYPE_INT32, &i_interface,
568 DBUS_TYPE_INT32, &i_protocol,
569 DBUS_TYPE_UINT32, &u_flags,
570 DBUS_TYPE_STRING, &name,
571 DBUS_TYPE_STRING, &type,
572 DBUS_TYPE_STRING, &domain,
573 DBUS_TYPE_STRING, &subtype,
574 DBUS_TYPE_INVALID)) {
575 r = avahi_client_set_errno(group->client, AVAHI_ERR_NO_MEMORY);
579 if (!(reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error)) ||
580 dbus_error_is_set (&error)) {
581 r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
585 if (!dbus_message_get_args(reply, &error, DBUS_TYPE_INVALID) ||
586 dbus_error_is_set (&error)) {
587 r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
591 dbus_message_unref(message);
592 dbus_message_unref(reply);
598 if (dbus_error_is_set(&error)) {
599 r = avahi_client_set_dbus_error(client, &error);
600 dbus_error_free(&error);
604 dbus_message_unref(message);
607 dbus_message_unref(reply);
613 int avahi_entry_group_update_service_txt(
614 AvahiEntryGroup *group,
615 AvahiIfIndex interface,
616 AvahiProtocol protocol,
617 AvahiPublishFlags flags,
625 AvahiStringList *txt;
627 va_start(va, domain);
628 txt = avahi_string_list_new_va(va);
629 r = avahi_entry_group_update_service_txt_strlst(group, interface, protocol, flags, name, type, domain, txt);
630 avahi_string_list_free(txt);
635 int avahi_entry_group_update_service_txt_strlst(
636 AvahiEntryGroup *group,
637 AvahiIfIndex interface,
638 AvahiProtocol protocol,
639 AvahiPublishFlags flags,
643 AvahiStringList *txt) {
645 DBusMessage *message = NULL, *reply = NULL;
649 int32_t i_interface, i_protocol;
656 client = group->client;
658 if (!group->path || !avahi_client_is_connected(group->client))
659 return avahi_client_set_errno(group->client, AVAHI_ERR_BAD_STATE);
664 dbus_error_init(&error);
666 if (!(message = dbus_message_new_method_call (AVAHI_DBUS_NAME, group->path, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "UpdateServiceTxt"))) {
667 r = avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
671 i_interface = (int32_t) interface;
672 i_protocol = (int32_t) protocol;
673 u_flags = (uint32_t) flags;
675 if (!dbus_message_append_args(
677 DBUS_TYPE_INT32, &i_interface,
678 DBUS_TYPE_INT32, &i_protocol,
679 DBUS_TYPE_UINT32, &u_flags,
680 DBUS_TYPE_STRING, &name,
681 DBUS_TYPE_STRING, &type,
682 DBUS_TYPE_STRING, &domain,
683 DBUS_TYPE_INVALID) ||
684 append_string_list(message, txt) < 0) {
685 r = avahi_client_set_errno(group->client, AVAHI_ERR_NO_MEMORY);
689 if (!(reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error)) ||
690 dbus_error_is_set (&error)) {
691 r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
695 if (!dbus_message_get_args(reply, &error, DBUS_TYPE_INVALID) ||
696 dbus_error_is_set (&error)) {
697 r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
701 dbus_message_unref(message);
702 dbus_message_unref(reply);
708 if (dbus_error_is_set(&error)) {
709 r = avahi_client_set_dbus_error(client, &error);
710 dbus_error_free(&error);
714 dbus_message_unref(message);
717 dbus_message_unref(reply);
722 /** Add a host/address pair */
723 int avahi_entry_group_add_address(
724 AvahiEntryGroup *group,
725 AvahiIfIndex interface,
726 AvahiProtocol protocol,
727 AvahiPublishFlags flags,
729 const AvahiAddress *a) {
731 DBusMessage *message = NULL, *reply = NULL;
735 int32_t i_interface, i_protocol;
737 char s_address[AVAHI_ADDRESS_STR_MAX];
738 char *p_address = s_address;
742 client = group->client;
744 if (!group->path || !avahi_client_is_connected(group->client))
745 return avahi_client_set_errno(group->client, AVAHI_ERR_BAD_STATE);
747 dbus_error_init(&error);
749 if (!(message = dbus_message_new_method_call (AVAHI_DBUS_NAME, group->path, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "AddAddress"))) {
750 r = avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
754 i_interface = (int32_t) interface;
755 i_protocol = (int32_t) protocol;
756 u_flags = (uint32_t) flags;
758 if (!avahi_address_snprint (s_address, sizeof (s_address), a))
760 r = avahi_client_set_errno(client, AVAHI_ERR_INVALID_ADDRESS);
764 if (!dbus_message_append_args(
766 DBUS_TYPE_INT32, &i_interface,
767 DBUS_TYPE_INT32, &i_protocol,
768 DBUS_TYPE_UINT32, &u_flags,
769 DBUS_TYPE_STRING, &name,
770 DBUS_TYPE_STRING, &p_address,
771 DBUS_TYPE_INVALID)) {
772 r = avahi_client_set_errno(group->client, AVAHI_ERR_NO_MEMORY);
776 if (!(reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error)) ||
777 dbus_error_is_set (&error)) {
778 r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
782 if (!dbus_message_get_args(reply, &error, DBUS_TYPE_INVALID) ||
783 dbus_error_is_set (&error)) {
784 r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
788 dbus_message_unref(message);
789 dbus_message_unref(reply);
795 if (dbus_error_is_set(&error)) {
796 r = avahi_client_set_dbus_error(client, &error);
797 dbus_error_free(&error);
801 dbus_message_unref(message);
804 dbus_message_unref(reply);
809 /** Add an arbitrary record */
810 int avahi_entry_group_add_record(
811 AvahiEntryGroup *group,
812 AvahiIfIndex interface,
813 AvahiProtocol protocol,
814 AvahiPublishFlags flags,
822 DBusMessage *message = NULL, *reply = NULL;
826 int32_t i_interface, i_protocol;
831 client = group->client;
833 if (!group->path || !avahi_client_is_connected(group->client))
834 return avahi_client_set_errno(group->client, AVAHI_ERR_BAD_STATE);
836 dbus_error_init(&error);
838 if (!(message = dbus_message_new_method_call (AVAHI_DBUS_NAME, group->path, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "AddRecord"))) {
839 r = avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
843 i_interface = (int32_t) interface;
844 i_protocol = (int32_t) protocol;
845 u_flags = (uint32_t) flags;
847 if (!dbus_message_append_args(
849 DBUS_TYPE_INT32, &i_interface,
850 DBUS_TYPE_INT32, &i_protocol,
851 DBUS_TYPE_UINT32, &u_flags,
852 DBUS_TYPE_STRING, &name,
853 DBUS_TYPE_UINT16, &clazz,
854 DBUS_TYPE_UINT16, &type,
855 DBUS_TYPE_UINT32, &ttl,
856 DBUS_TYPE_INVALID) || append_rdata(message, rdata, size) < 0) {
857 r = avahi_client_set_errno(group->client, AVAHI_ERR_NO_MEMORY);
861 if (!(reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error)) ||
862 dbus_error_is_set (&error)) {
863 r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
867 if (!dbus_message_get_args(reply, &error, DBUS_TYPE_INVALID) ||
868 dbus_error_is_set (&error)) {
869 r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
873 dbus_message_unref(message);
874 dbus_message_unref(reply);
880 if (dbus_error_is_set(&error)) {
881 r = avahi_client_set_dbus_error(client, &error);
882 dbus_error_free(&error);
886 dbus_message_unref(message);
889 dbus_message_unref(reply);