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;
128 AVAHI_LLIST_PREPEND(AvahiEntryGroup, groups, client->groups, group);
130 if (!(message = dbus_message_new_method_call(
132 AVAHI_DBUS_PATH_SERVER,
133 AVAHI_DBUS_INTERFACE_SERVER,
135 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
139 if (!(reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error)) ||
140 dbus_error_is_set (&error)) {
141 avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
145 if (!dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID) ||
146 dbus_error_is_set (&error)) {
147 avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
151 if (!(group->path = avahi_strdup (path))) {
153 /* FIXME: We don't remove the object on the server side */
155 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, *reply;
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 dbus_error_init(&error);
293 if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, group->path, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "IsEmpty"))) {
294 r = avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
298 if (!(reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error)) ||
299 dbus_error_is_set (&error)) {
300 r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
304 if (!dbus_message_get_args(reply, &error, DBUS_TYPE_BOOLEAN, &b, DBUS_TYPE_INVALID) ||
305 dbus_error_is_set (&error)) {
306 r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
310 dbus_message_unref(message);
311 dbus_message_unref(reply);
316 if (dbus_error_is_set(&error)) {
317 r = avahi_client_set_dbus_error(client, &error);
318 dbus_error_free(&error);
322 dbus_message_unref(message);
325 dbus_message_unref(reply);
330 int avahi_entry_group_add_service_strlst(
331 AvahiEntryGroup *group,
332 AvahiIfIndex interface,
333 AvahiProtocol protocol,
339 AvahiStringList *txt) {
341 DBusMessage *message = NULL, *reply = NULL;
342 DBusMessageIter iter, sub;
344 int reverse = 0, r = AVAHI_OK;
352 client = group->client;
354 dbus_error_init(&error);
356 if (!(message = dbus_message_new_method_call (AVAHI_DBUS_NAME, group->path, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "AddService"))) {
357 r = avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
361 if (!dbus_message_append_args(
363 DBUS_TYPE_INT32, &interface,
364 DBUS_TYPE_INT32, &protocol,
365 DBUS_TYPE_STRING, &name,
366 DBUS_TYPE_STRING, &type,
367 DBUS_TYPE_STRING, &domain,
368 DBUS_TYPE_STRING, &host,
369 DBUS_TYPE_UINT16, &port,
370 DBUS_TYPE_INVALID)) {
371 r = avahi_client_set_errno(group->client, AVAHI_ERR_NO_MEMORY);
375 dbus_message_iter_init_append(message, &iter);
377 /* Reverse the string list, so that we can pass it in-order to the server */
378 txt = avahi_string_list_reverse(txt);
381 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "ay", &sub)) {
382 r = avahi_client_set_errno(group->client, AVAHI_ERR_NO_MEMORY);
386 /* Assemble the AvahiStringList into an Array of Array of Bytes to send over dbus */
387 for (p = txt; p != NULL; p = p->next) {
388 DBusMessageIter sub2;
389 const uint8_t *data = p->text;
391 if (!(dbus_message_iter_open_container(&sub, DBUS_TYPE_ARRAY, "y", &sub2)) ||
392 !(dbus_message_iter_append_fixed_array(&sub2, DBUS_TYPE_BYTE, &data, p->size)) ||
393 !(dbus_message_iter_close_container(&sub, &sub2))) {
394 r = avahi_client_set_errno(group->client, AVAHI_ERR_NO_MEMORY);
399 if (!dbus_message_iter_close_container(&iter, &sub)) {
400 r = avahi_client_set_errno(group->client, AVAHI_ERR_NO_MEMORY);
404 /* Reverse the string list to the original state */
405 txt = avahi_string_list_reverse(txt);
408 if (!(reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error)) ||
409 dbus_error_is_set (&error)) {
410 r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
414 if (!dbus_message_get_args(reply, &error, DBUS_TYPE_INVALID) ||
415 dbus_error_is_set (&error)) {
416 r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
420 dbus_message_unref(message);
421 dbus_message_unref(reply);
427 txt = avahi_string_list_reverse(txt);
429 if (dbus_error_is_set(&error)) {
430 r = avahi_client_set_dbus_error(client, &error);
431 dbus_error_free(&error);
435 dbus_message_unref(message);
438 dbus_message_unref(reply);
443 int avahi_entry_group_add_service(
444 AvahiEntryGroup *group,
445 AvahiIfIndex interface,
446 AvahiProtocol protocol,
460 r = avahi_entry_group_add_service_va(group, interface, protocol, name, type, domain, host, port, va);
465 int avahi_entry_group_add_service_va(
466 AvahiEntryGroup *group,
467 AvahiIfIndex interface,
468 AvahiProtocol protocol,
477 AvahiStringList *txt;
481 txt = avahi_string_list_new_va(va);
482 r = avahi_entry_group_add_service_strlst(group, interface, protocol, name, type, domain, host, port, txt);
483 avahi_string_list_free(txt);
488 const char* avahi_entry_group_get_dbus_path(AvahiEntryGroup *group) {