-/* $Id$ */
-
/***
This file is part of avahi.
-
+
avahi is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2.1 of the
License, or (at your option) any later version.
-
+
avahi is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
Public License for more details.
-
+
You should have received a copy of the GNU Lesser General Public
License along with avahi; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
void avahi_entry_group_set_state(AvahiEntryGroup *group, AvahiEntryGroupState state) {
assert(group);
- if (group->state == state)
+ if (group->state_valid && group->state == state)
return;
group->state = state;
+ group->state_valid = 1;
if (group->callback)
group->callback(group, state, group->userdata);
}
static int retrieve_state(AvahiEntryGroup *group) {
- DBusMessage *message, *reply;
+ DBusMessage *message = NULL, *reply = NULL;
DBusError error;
int r = AVAHI_OK;
int32_t state;
AvahiClient *client;
-
+
dbus_error_init(&error);
assert(group);
client = group->client;
-
+
if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, group->path, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "GetState"))) {
r = avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
goto fail;
}
-
+
if (!(reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error)) ||
dbus_error_is_set (&error)) {
r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
goto fail;
}
-
+
if (!dbus_message_get_args(reply, &error, DBUS_TYPE_INT32, &state, DBUS_TYPE_INVALID) ||
dbus_error_is_set (&error)) {
r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
dbus_message_unref(message);
dbus_message_unref(reply);
- avahi_entry_group_set_state(group, (AvahiEntryGroupState) state);
-
- return AVAHI_OK;
-
+ return state;
+
fail:
if (dbus_error_is_set(&error)) {
r = avahi_client_set_dbus_error(client, &error);
DBusMessage *message = NULL, *reply = NULL;
DBusError error;
char *path;
+ int state;
assert(client);
dbus_error_init (&error);
- if (client->state == AVAHI_CLIENT_DISCONNECTED) {
+ if (!avahi_client_is_connected(client)) {
avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
goto fail;
}
avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
goto fail;
}
-
+
group->client = client;
group->callback = callback;
group->userdata = userdata;
- group->state = AVAHI_ENTRY_GROUP_UNCOMMITED;
+ group->state_valid = 0;
group->path = NULL;
AVAHI_LLIST_PREPEND(AvahiEntryGroup, groups, client->groups, group);
-
+
if (!(message = dbus_message_new_method_call(
AVAHI_DBUS_NAME,
AVAHI_DBUS_PATH_SERVER,
avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
goto fail;
}
-
+
if (!(group->path = avahi_strdup (path))) {
/* FIXME: We don't remove the object on the server side */
goto fail;
}
- if (retrieve_state(group) < 0)
+ if ((state = retrieve_state(group)) < 0) {
+ avahi_client_set_errno(client, state);
goto fail;
+ }
+
+ avahi_entry_group_set_state(group, (AvahiEntryGroupState) state);
dbus_message_unref(message);
dbus_message_unref(reply);
if (group)
avahi_entry_group_free(group);
-
+
if (message)
dbus_message_unref(message);
if (reply)
dbus_message_unref(reply);
-
+
return NULL;
}
DBusError error;
int r = AVAHI_OK;
AvahiClient *client;
-
+
dbus_error_init(&error);
assert(group);
client = group->client;
-
+
if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, group->path, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, method))) {
r = avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
goto fail;
}
-
+
if (!(reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error)) ||
dbus_error_is_set (&error)) {
r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
goto fail;
}
-
+
if (!dbus_message_get_args(reply, &error, DBUS_TYPE_INVALID) ||
dbus_error_is_set (&error)) {
r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
dbus_message_unref(reply);
return AVAHI_OK;
-
+
fail:
if (dbus_error_is_set(&error)) {
r = avahi_client_set_dbus_error(client, &error);
int avahi_entry_group_free(AvahiEntryGroup *group) {
AvahiClient *client = group->client;
int r = AVAHI_OK;
-
+
assert(group);
-
- if (group->path && client->state != AVAHI_CLIENT_DISCONNECTED)
+
+ if (group->path && avahi_client_is_connected(client))
r = entry_group_simple_method_call(group, "Free");
-
+
AVAHI_LLIST_REMOVE(AvahiEntryGroup, groups, client->groups, group);
avahi_free(group->path);
}
int avahi_entry_group_commit(AvahiEntryGroup *group) {
+ int ret;
assert(group);
-
- if (!group->path || group->client->state == AVAHI_CLIENT_DISCONNECTED)
+
+ if (!group->path || !avahi_client_is_connected(group->client))
return avahi_client_set_errno(group->client, AVAHI_ERR_BAD_STATE);
- return entry_group_simple_method_call(group, "Commit");
+ if ((ret = entry_group_simple_method_call(group, "Commit")) < 0)
+ return ret;
+
+ group->state_valid = 0;
+ return ret;
}
int avahi_entry_group_reset(AvahiEntryGroup *group) {
+ int ret;
assert(group);
-
- if (!group->path || group->client->state == AVAHI_CLIENT_DISCONNECTED)
+
+ if (!group->path || !avahi_client_is_connected(group->client))
return avahi_client_set_errno(group->client, AVAHI_ERR_BAD_STATE);
- return entry_group_simple_method_call(group, "Reset");
+ if ((ret = entry_group_simple_method_call(group, "Reset")) < 0)
+ return ret;
+
+ group->state_valid = 0;
+ return ret;
}
int avahi_entry_group_get_state (AvahiEntryGroup *group) {
assert (group);
- return group->state;
+ if (group->state_valid)
+ return group->state;
+
+ return retrieve_state(group);
}
AvahiClient* avahi_entry_group_get_client (AvahiEntryGroup *group) {
assert(group);
-
+
return group->client;
}
int avahi_entry_group_is_empty (AvahiEntryGroup *group) {
- DBusMessage *message, *reply;
+ DBusMessage *message = NULL, *reply = NULL;
DBusError error;
int r = AVAHI_OK;
int b;
AvahiClient *client;
-
+
assert(group);
client = group->client;
- if (!group->path || group->client->state == AVAHI_CLIENT_DISCONNECTED)
+ if (!group->path || !avahi_client_is_connected(group->client))
return avahi_client_set_errno(group->client, AVAHI_ERR_BAD_STATE);
dbus_error_init(&error);
-
+
if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, group->path, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "IsEmpty"))) {
r = avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
goto fail;
}
-
+
if (!(reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error)) ||
dbus_error_is_set (&error)) {
r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
goto fail;
}
-
+
if (!dbus_message_get_args(reply, &error, DBUS_TYPE_BOOLEAN, &b, DBUS_TYPE_INVALID) ||
dbus_error_is_set (&error)) {
r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
dbus_message_unref(reply);
return !!b;
-
+
fail:
if (dbus_error_is_set(&error)) {
r = avahi_client_set_dbus_error(client, &error);
static int append_rdata(DBusMessage *message, const void *rdata, size_t size) {
DBusMessageIter iter, sub;
-
+
assert(message);
-
+
dbus_message_iter_init_append(message, &iter);
-
+
if (!(dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE_AS_STRING, &sub)) ||
!(dbus_message_iter_append_fixed_array(&sub, DBUS_TYPE_BYTE, &rdata, size)) ||
!(dbus_message_iter_close_container(&iter, &sub)))
return -1;
-
+
return 0;
}
/* Reverse the string list, so that we can pass it in-order to the server */
txt = avahi_string_list_reverse(txt);
-
+
if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "ay", &sub))
goto fail;
for (p = txt; p != NULL; p = p->next) {
DBusMessageIter sub2;
const uint8_t *data = p->text;
-
+
if (!(dbus_message_iter_open_container(&sub, DBUS_TYPE_ARRAY, "y", &sub2)) ||
!(dbus_message_iter_append_fixed_array(&sub2, DBUS_TYPE_BYTE, &data, p->size)) ||
!(dbus_message_iter_close_container(&sub, &sub2)))
const char *host,
uint16_t port,
AvahiStringList *txt) {
-
+
DBusMessage *message = NULL, *reply = NULL;
int r = AVAHI_OK;
DBusError error;
client = group->client;
- if (!group->path || group->client->state == AVAHI_CLIENT_DISCONNECTED)
+ if (!group->path || !avahi_client_is_connected(group->client))
return avahi_client_set_errno(group->client, AVAHI_ERR_BAD_STATE);
if (!domain)
if (!host)
host = "";
-
+
dbus_error_init(&error);
-
+
if (!(message = dbus_message_new_method_call (AVAHI_DBUS_NAME, group->path, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "AddService"))) {
r = avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
goto fail;
r = avahi_client_set_errno(group->client, AVAHI_ERR_NO_MEMORY);
goto fail;
}
-
+
if (!(reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error)) ||
dbus_error_is_set (&error)) {
r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
goto fail;
}
-
+
if (!dbus_message_get_args(reply, &error, DBUS_TYPE_INVALID) ||
dbus_error_is_set (&error)) {
r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
return AVAHI_OK;
fail:
-
+
if (dbus_error_is_set(&error)) {
r = avahi_client_set_dbus_error(client, &error);
dbus_error_free(&error);
const char *host,
uint16_t port,
...) {
-
+
va_list va;
int r;
AvahiStringList *txt;
client = group->client;
- if (!group->path || group->client->state == AVAHI_CLIENT_DISCONNECTED)
+ if (!group->path || !avahi_client_is_connected(group->client))
return avahi_client_set_errno(group->client, AVAHI_ERR_BAD_STATE);
if (!domain)
domain = "";
dbus_error_init(&error);
-
+
if (!(message = dbus_message_new_method_call (AVAHI_DBUS_NAME, group->path, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "AddServiceSubtype"))) {
r = avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
goto fail;
r = avahi_client_set_errno(group->client, AVAHI_ERR_NO_MEMORY);
goto fail;
}
-
+
if (!(reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error)) ||
dbus_error_is_set (&error)) {
r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
goto fail;
}
-
+
if (!dbus_message_get_args(reply, &error, DBUS_TYPE_INVALID) ||
dbus_error_is_set (&error)) {
r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
return AVAHI_OK;
fail:
-
+
if (dbus_error_is_set(&error)) {
r = avahi_client_set_dbus_error(client, &error);
dbus_error_free(&error);
AvahiIfIndex interface,
AvahiProtocol protocol,
AvahiPublishFlags flags,
- const char *name,
- const char *type,
- const char *domain,
+ const char *name,
+ const char *type,
+ const char *domain,
...) {
va_list va;
AvahiIfIndex interface,
AvahiProtocol protocol,
AvahiPublishFlags flags,
- const char *name,
- const char *type,
- const char *domain,
+ const char *name,
+ const char *type,
+ const char *domain,
AvahiStringList *txt) {
DBusMessage *message = NULL, *reply = NULL;
client = group->client;
- if (!group->path || group->client->state == AVAHI_CLIENT_DISCONNECTED)
+ if (!group->path || !avahi_client_is_connected(group->client))
return avahi_client_set_errno(group->client, AVAHI_ERR_BAD_STATE);
if (!domain)
domain = "";
-
+
dbus_error_init(&error);
-
+
if (!(message = dbus_message_new_method_call (AVAHI_DBUS_NAME, group->path, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "UpdateServiceTxt"))) {
r = avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
goto fail;
r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
goto fail;
}
-
+
if (!dbus_message_get_args(reply, &error, DBUS_TYPE_INVALID) ||
dbus_error_is_set (&error)) {
r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
return AVAHI_OK;
fail:
-
+
if (dbus_error_is_set(&error)) {
r = avahi_client_set_dbus_error(client, &error);
dbus_error_free(&error);
dbus_message_unref(reply);
return r;
-
-
}
/** Add a host/address pair */
client = group->client;
- if (!group->path || group->client->state == AVAHI_CLIENT_DISCONNECTED)
+ if (!group->path || !avahi_client_is_connected(group->client))
return avahi_client_set_errno(group->client, AVAHI_ERR_BAD_STATE);
dbus_error_init(&error);
-
+
if (!(message = dbus_message_new_method_call (AVAHI_DBUS_NAME, group->path, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "AddAddress"))) {
r = avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
goto fail;
i_interface = (int32_t) interface;
i_protocol = (int32_t) protocol;
u_flags = (uint32_t) flags;
-
+
if (!avahi_address_snprint (s_address, sizeof (s_address), a))
{
r = avahi_client_set_errno(client, AVAHI_ERR_INVALID_ADDRESS);
r = avahi_client_set_errno(group->client, AVAHI_ERR_NO_MEMORY);
goto fail;
}
-
+
if (!(reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error)) ||
dbus_error_is_set (&error)) {
r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
goto fail;
}
-
+
if (!dbus_message_get_args(reply, &error, DBUS_TYPE_INVALID) ||
dbus_error_is_set (&error)) {
r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
return AVAHI_OK;
fail:
-
+
if (dbus_error_is_set(&error)) {
r = avahi_client_set_dbus_error(client, &error);
dbus_error_free(&error);
client = group->client;
- if (!group->path || group->client->state == AVAHI_CLIENT_DISCONNECTED)
+ if (!group->path || !avahi_client_is_connected(group->client))
return avahi_client_set_errno(group->client, AVAHI_ERR_BAD_STATE);
dbus_error_init(&error);
-
+
if (!(message = dbus_message_new_method_call (AVAHI_DBUS_NAME, group->path, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "AddRecord"))) {
r = avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
goto fail;
i_interface = (int32_t) interface;
i_protocol = (int32_t) protocol;
u_flags = (uint32_t) flags;
-
+
if (!dbus_message_append_args(
message,
DBUS_TYPE_INT32, &i_interface,
r = avahi_client_set_errno(group->client, AVAHI_ERR_NO_MEMORY);
goto fail;
}
-
+
if (!(reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error)) ||
dbus_error_is_set (&error)) {
r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
goto fail;
}
-
+
if (!dbus_message_get_args(reply, &error, DBUS_TYPE_INVALID) ||
dbus_error_is_set (&error)) {
r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
return AVAHI_OK;
fail:
-
+
if (dbus_error_is_set(&error)) {
r = avahi_client_set_dbus_error(client, &error);
dbus_error_free(&error);