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, *reply;
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 (client->state == AVAHI_CLIENT_DISCONNECTED) {
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 && client->state != AVAHI_CLIENT_DISCONNECTED)
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 || group->client->state == AVAHI_CLIENT_DISCONNECTED)
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 || group->client->state == AVAHI_CLIENT_DISCONNECTED)
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, *reply;
289 client = group->client;
291 if (!group->path || group->client->state == AVAHI_CLIENT_DISCONNECTED)
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_string_list(DBusMessage *message, AvahiStringList *txt) {
334 DBusMessageIter iter, sub;
340 dbus_message_iter_init_append(message, &iter);
342 /* Reverse the string list, so that we can pass it in-order to the server */
343 txt = avahi_string_list_reverse(txt);
345 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "ay", &sub))
348 /* Assemble the AvahiStringList into an Array of Array of Bytes to send over dbus */
349 for (p = txt; p != NULL; p = p->next) {
350 DBusMessageIter sub2;
351 const uint8_t *data = p->text;
353 if (!(dbus_message_iter_open_container(&sub, DBUS_TYPE_ARRAY, "y", &sub2)) ||
354 !(dbus_message_iter_append_fixed_array(&sub2, DBUS_TYPE_BYTE, &data, p->size)) ||
355 !(dbus_message_iter_close_container(&sub, &sub2)))
359 if (!dbus_message_iter_close_container(&iter, &sub))
366 /* Reverse the string list to the original state */
367 txt = avahi_string_list_reverse(txt);
372 int avahi_entry_group_add_service_strlst(
373 AvahiEntryGroup *group,
374 AvahiIfIndex interface,
375 AvahiProtocol protocol,
376 AvahiPublishFlags flags,
382 AvahiStringList *txt) {
384 DBusMessage *message = NULL, *reply = NULL;
388 int32_t i_interface, i_protocol;
395 client = group->client;
397 if (!group->path || group->client->state == AVAHI_CLIENT_DISCONNECTED)
398 return avahi_client_set_errno(group->client, AVAHI_ERR_BAD_STATE);
406 dbus_error_init(&error);
408 if (!(message = dbus_message_new_method_call (AVAHI_DBUS_NAME, group->path, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "AddService"))) {
409 r = avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
413 i_interface = (int32_t) interface;
414 i_protocol = (int32_t) protocol;
415 u_flags = (uint32_t) flags;
417 if (!dbus_message_append_args(
419 DBUS_TYPE_INT32, &i_interface,
420 DBUS_TYPE_INT32, &i_protocol,
421 DBUS_TYPE_UINT32, &u_flags,
422 DBUS_TYPE_STRING, &name,
423 DBUS_TYPE_STRING, &type,
424 DBUS_TYPE_STRING, &domain,
425 DBUS_TYPE_STRING, &host,
426 DBUS_TYPE_UINT16, &port,
427 DBUS_TYPE_INVALID) ||
428 append_string_list(message, txt) < 0) {
429 r = avahi_client_set_errno(group->client, AVAHI_ERR_NO_MEMORY);
433 if (!(reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error)) ||
434 dbus_error_is_set (&error)) {
435 r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
439 if (!dbus_message_get_args(reply, &error, DBUS_TYPE_INVALID) ||
440 dbus_error_is_set (&error)) {
441 r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
445 dbus_message_unref(message);
446 dbus_message_unref(reply);
452 if (dbus_error_is_set(&error)) {
453 r = avahi_client_set_dbus_error(client, &error);
454 dbus_error_free(&error);
458 dbus_message_unref(message);
461 dbus_message_unref(reply);
466 int avahi_entry_group_add_service(
467 AvahiEntryGroup *group,
468 AvahiIfIndex interface,
469 AvahiProtocol protocol,
470 AvahiPublishFlags flags,
480 AvahiStringList *txt;
485 txt = avahi_string_list_new_va(va);
486 r = avahi_entry_group_add_service_strlst(group, interface, protocol, flags, name, type, domain, host, port, txt);
487 avahi_string_list_free(txt);
492 int avahi_entry_group_add_service_subtype(
493 AvahiEntryGroup *group,
494 AvahiIfIndex interface,
495 AvahiProtocol protocol,
496 AvahiPublishFlags flags,
500 const char *subtype) {
502 DBusMessage *message = NULL, *reply = NULL;
506 int32_t i_interface, i_protocol;
514 client = group->client;
516 if (!group->path || group->client->state == AVAHI_CLIENT_DISCONNECTED)
517 return avahi_client_set_errno(group->client, AVAHI_ERR_BAD_STATE);
522 dbus_error_init(&error);
524 if (!(message = dbus_message_new_method_call (AVAHI_DBUS_NAME, group->path, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "AddServiceSubtype"))) {
525 r = avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
529 i_interface = (int32_t) interface;
530 i_protocol = (int32_t) protocol;
531 u_flags = (uint32_t) flags;
533 if (!dbus_message_append_args(
535 DBUS_TYPE_INT32, &i_interface,
536 DBUS_TYPE_INT32, &i_protocol,
537 DBUS_TYPE_UINT32, &u_flags,
538 DBUS_TYPE_STRING, &name,
539 DBUS_TYPE_STRING, &type,
540 DBUS_TYPE_STRING, &domain,
541 DBUS_TYPE_STRING, &subtype,
542 DBUS_TYPE_INVALID)) {
543 r = avahi_client_set_errno(group->client, AVAHI_ERR_NO_MEMORY);
547 if (!(reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error)) ||
548 dbus_error_is_set (&error)) {
549 r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
553 if (!dbus_message_get_args(reply, &error, DBUS_TYPE_INVALID) ||
554 dbus_error_is_set (&error)) {
555 r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
559 dbus_message_unref(message);
560 dbus_message_unref(reply);
566 if (dbus_error_is_set(&error)) {
567 r = avahi_client_set_dbus_error(client, &error);
568 dbus_error_free(&error);
572 dbus_message_unref(message);
575 dbus_message_unref(reply);
581 int avahi_entry_group_update_service_txt(
582 AvahiEntryGroup *group,
583 AvahiIfIndex interface,
584 AvahiProtocol protocol,
585 AvahiPublishFlags flags,
593 AvahiStringList *txt;
595 va_start(va, domain);
596 txt = avahi_string_list_new_va(va);
597 r = avahi_entry_group_update_service_txt_strlst(group, interface, protocol, flags, name, type, domain, txt);
598 avahi_string_list_free(txt);
603 int avahi_entry_group_update_service_txt_strlst(
604 AvahiEntryGroup *group,
605 AvahiIfIndex interface,
606 AvahiProtocol protocol,
607 AvahiPublishFlags flags,
611 AvahiStringList *txt) {
613 DBusMessage *message = NULL, *reply = NULL;
617 int32_t i_interface, i_protocol;
624 client = group->client;
626 if (!group->path || group->client->state == AVAHI_CLIENT_DISCONNECTED)
627 return avahi_client_set_errno(group->client, AVAHI_ERR_BAD_STATE);
632 dbus_error_init(&error);
634 if (!(message = dbus_message_new_method_call (AVAHI_DBUS_NAME, group->path, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "UpdateServiceTxt"))) {
635 r = avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
639 i_interface = (int32_t) interface;
640 i_protocol = (int32_t) protocol;
641 u_flags = (uint32_t) flags;
643 if (!dbus_message_append_args(
645 DBUS_TYPE_INT32, &i_interface,
646 DBUS_TYPE_INT32, &i_protocol,
647 DBUS_TYPE_UINT32, &u_flags,
648 DBUS_TYPE_STRING, &name,
649 DBUS_TYPE_STRING, &type,
650 DBUS_TYPE_STRING, &domain,
651 DBUS_TYPE_INVALID) ||
652 append_string_list(message, txt) < 0) {
653 r = avahi_client_set_errno(group->client, AVAHI_ERR_NO_MEMORY);
657 if (!(reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error)) ||
658 dbus_error_is_set (&error)) {
659 r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
663 if (!dbus_message_get_args(reply, &error, DBUS_TYPE_INVALID) ||
664 dbus_error_is_set (&error)) {
665 r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
669 dbus_message_unref(message);
670 dbus_message_unref(reply);
676 if (dbus_error_is_set(&error)) {
677 r = avahi_client_set_dbus_error(client, &error);
678 dbus_error_free(&error);
682 dbus_message_unref(message);
685 dbus_message_unref(reply);
692 /** Add a host/address pair */
693 int avahi_entry_group_add_address(
694 AvahiEntryGroup *group,
695 AvahiIfIndex interface,
696 AvahiProtocol protocol,
697 AvahiPublishFlags flags,
699 const AvahiAddress *a) {
701 DBusMessage *message = NULL, *reply = NULL;
705 int32_t i_interface, i_protocol;
707 char s_address[AVAHI_ADDRESS_STR_MAX];
708 char *p_address = s_address;
712 client = group->client;
714 if (!group->path || group->client->state == AVAHI_CLIENT_DISCONNECTED)
715 return avahi_client_set_errno(group->client, AVAHI_ERR_BAD_STATE);
717 dbus_error_init(&error);
719 if (!(message = dbus_message_new_method_call (AVAHI_DBUS_NAME, group->path, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "AddAddress"))) {
720 r = avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
724 i_interface = (int32_t) interface;
725 i_protocol = (int32_t) protocol;
726 u_flags = (uint32_t) flags;
728 if (!avahi_address_snprint (s_address, sizeof (s_address), a))
730 r = avahi_client_set_errno(client, AVAHI_ERR_INVALID_ADDRESS);
734 if (!dbus_message_append_args(
736 DBUS_TYPE_INT32, &i_interface,
737 DBUS_TYPE_INT32, &i_protocol,
738 DBUS_TYPE_UINT32, &u_flags,
739 DBUS_TYPE_STRING, &name,
740 DBUS_TYPE_STRING, &p_address,
741 DBUS_TYPE_INVALID)) {
742 r = avahi_client_set_errno(group->client, AVAHI_ERR_NO_MEMORY);
746 if (!(reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error)) ||
747 dbus_error_is_set (&error)) {
748 r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
752 if (!dbus_message_get_args(reply, &error, DBUS_TYPE_INVALID) ||
753 dbus_error_is_set (&error)) {
754 r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
758 dbus_message_unref(message);
759 dbus_message_unref(reply);
765 if (dbus_error_is_set(&error)) {
766 r = avahi_client_set_dbus_error(client, &error);
767 dbus_error_free(&error);
771 dbus_message_unref(message);
774 dbus_message_unref(reply);