/* AvahiDomainBrowser */
-AvahiDomainBrowser* avahi_domain_browser_new (AvahiClient *client, AvahiIfIndex interface, AvahiProtocol protocol, const char *domain, AvahiDomainBrowserType btype, AvahiDomainBrowserCallback callback, void *user_data)
+AvahiDomainBrowser* avahi_domain_browser_new (AvahiClient *client, AvahiIfIndex interface, AvahiProtocol protocol, const char *domain, AvahiDomainBrowserType btype, AvahiDomainBrowserCallback callback, void *userdata)
{
AvahiDomainBrowser *tmp = NULL;
DBusMessage *message = NULL, *reply;
tmp = avahi_new (AvahiDomainBrowser, 1);
tmp->client = client;
tmp->callback = callback;
- tmp->user_data = user_data;
+ tmp->userdata = userdata;
tmp->path = strdup (path);
AVAHI_LLIST_PREPEND(AvahiDomainBrowser, domain_browsers, client->domain_browsers, tmp);
if (dbus_error_is_set (&error))
goto out;
- db->callback (db, interface, protocol, event, domain, db->user_data);
+ db->callback (db, interface, protocol, event, domain, db->userdata);
return DBUS_HANDLER_RESULT_HANDLED;
}
/* AvahiServiceTypeBrowser */
-AvahiServiceTypeBrowser* avahi_service_type_browser_new (AvahiClient *client, AvahiIfIndex interface, AvahiProtocol protocol, const char *domain, AvahiServiceTypeBrowserCallback callback, void *user_data)
+AvahiServiceTypeBrowser* avahi_service_type_browser_new (AvahiClient *client, AvahiIfIndex interface, AvahiProtocol protocol, const char *domain, AvahiServiceTypeBrowserCallback callback, void *userdata)
{
AvahiServiceTypeBrowser *tmp = NULL;
DBusMessage *message = NULL, *reply;
tmp = avahi_new(AvahiServiceTypeBrowser, 1);
tmp->client = client;
tmp->callback = callback;
- tmp->user_data = user_data;
+ tmp->userdata = userdata;
tmp->path = strdup (path);
AVAHI_LLIST_PREPEND(AvahiServiceTypeBrowser, service_type_browsers, client->service_type_browsers, tmp);
if (dbus_error_is_set (&error))
goto out;
- db->callback (db, interface, protocol, event, type, domain, db->user_data);
+ db->callback (db, interface, protocol, event, type, domain, db->userdata);
return DBUS_HANDLER_RESULT_HANDLED;
/* AvahiServiceBrowser */
-AvahiServiceBrowser* avahi_service_browser_new (AvahiClient *client, AvahiIfIndex interface, AvahiProtocol protocol, const char *type, const char *domain, AvahiServiceBrowserCallback callback, void *user_data)
+AvahiServiceBrowser* avahi_service_browser_new (AvahiClient *client, AvahiIfIndex interface, AvahiProtocol protocol, const char *type, const char *domain, AvahiServiceBrowserCallback callback, void *userdata)
{
AvahiServiceBrowser *tmp = NULL;
DBusMessage *message = NULL, *reply;
tmp = avahi_new(AvahiServiceBrowser, 1);
tmp->client = client;
tmp->callback = callback;
- tmp->user_data = user_data;
+ tmp->userdata = userdata;
tmp->path = strdup (path);
AVAHI_LLIST_PREPEND(AvahiServiceBrowser, service_browsers, client->service_browsers, tmp);
if (dbus_error_is_set (&error))
goto out;
- db->callback (db, interface, protocol, event, name, type, domain, db->user_data);
+ db->callback (db, interface, protocol, event, name, type, domain, db->userdata);
return DBUS_HANDLER_RESULT_HANDLED;
AvahiServiceTypeBrowser *st;
AvahiSimplePoll *simple_poll;
char *ret;
+ int error;
struct timeval tv;
simple_poll = avahi_simple_poll_new();
poll_api = avahi_simple_poll_get(simple_poll);
- avahi = avahi_client_new(poll_api, avahi_client_callback, "omghai2u");
+ if (!(avahi = avahi_client_new(poll_api, avahi_client_callback, "omghai2u", &error))) {
+ fprintf(stderr, "Client failed: %s\n", avahi_strerror(error));
+ goto fail;
+ }
assert (avahi != NULL);
if (avahi_simple_poll_iterate(simple_poll, -1) != 0)
break;
+fail:
- avahi_simple_poll_free(simple_poll);
+ if (simple_poll)
+ avahi_simple_poll_free(simple_poll);
- avahi_free (avahi);
+ if (avahi)
+ avahi_free (avahi);
return 0;
}
int
avahi_client_set_errno (AvahiClient *client, int error)
{
- if (client == NULL) return error;
+ assert(client);
client->error = error;
return error;
}
-static
-void avahi_client_state_change (AvahiClient *client, int state)
-{
- if (client == NULL || client->callback == NULL)
+static void avahi_client_set_state (AvahiClient *client, AvahiServerState state) {
+ assert(state);
+
+ if (client->state == state)
return;
- client->callback (client, state, client->user_data);
+ client->state = state;
+
+ if (client->callback)
+ client->callback (client, state, client->userdata);
}
static void
if (dbus_error_is_set (&error))
return;
- avahi_client_state_change (client, state);
+ avahi_client_set_state (client, state);
} else if (type == DBUS_MESSAGE_TYPE_ERROR) {
dbus_set_error_from_message (&error, reply);
}
DBusMessage *message;
DBusPendingCall *pcall;
+ /*** Lennart says that this can't happen this way since it will
+ * never be called if no main loop is used. This call has to
+ * happen synchronously */
+
if (client == NULL) return;
message = dbus_message_new_method_call (AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "GetState");
if (strcmp (name, AVAHI_DBUS_NAME) == 0) {
if (old == NULL && new != NULL) {
- avahi_client_state_change (client, AVAHI_CLIENT_RECONNECTED);
+ avahi_client_set_state (client, AVAHI_CLIENT_RECONNECTED);
} else if (old != NULL && new == NULL) {
- avahi_client_state_change (client, AVAHI_CLIENT_DISCONNECTED);
+ avahi_client_set_state (client, AVAHI_CLIENT_DISCONNECTED);
/* XXX: we really need to expire all entry groups */
}
}
if (group != NULL) {
int state;
- DBusError error;
- dbus_error_init (&error);
dbus_message_get_args (message, &error, DBUS_TYPE_INT32, &state, DBUS_TYPE_INVALID);
if (dbus_error_is_set (&error))
goto out;
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
+static int translate_dbus_error(const DBusError *error) {
+ assert(error);
+
+ /*** FIXME! Some more eloquent error translation should happen here */
+
+ return AVAHI_ERR_DBUS_ERROR;
+}
+
AvahiClient *
-avahi_client_new (const AvahiPoll *poll_api, AvahiClientCallback callback, void *user_data)
+avahi_client_new (const AvahiPoll *poll_api, AvahiClientCallback callback, void *userdata, int *ret_error)
{
- AvahiClient *tmp = NULL;
+ AvahiClient *client = NULL;
DBusError error;
dbus_error_init (&error);
- if (!(tmp = avahi_new(AvahiClient, 1)))
+ if (!(client = avahi_new(AvahiClient, 1))) {
+ if (ret_error)
+ *ret_error = AVAHI_ERR_NO_MEMORY;
goto fail;
+ }
- AVAHI_LLIST_HEAD_INIT(AvahiEntryGroup, tmp->groups);
- AVAHI_LLIST_HEAD_INIT(AvahiDomainBrowser, tmp->domain_browsers);
- AVAHI_LLIST_HEAD_INIT(AvahiServiceBrowser, tmp->service_browsers);
- AVAHI_LLIST_HEAD_INIT(AvahiServiceTypeBrowser, tmp->service_type_browsers);
-
- tmp->poll_api = poll_api;
- tmp->bus = dbus_bus_get (DBUS_BUS_SYSTEM, &error);
+ client->poll_api = poll_api;
+ client->error = AVAHI_OK;
+ client->callback = callback;
+ client->userdata = userdata;
+ client->state = AVAHI_SERVER_INVALID;
+
+ AVAHI_LLIST_HEAD_INIT(AvahiEntryGroup, client->groups);
+ AVAHI_LLIST_HEAD_INIT(AvahiDomainBrowser, client->domain_browsers);
+ AVAHI_LLIST_HEAD_INIT(AvahiServiceBrowser, client->service_browsers);
+ AVAHI_LLIST_HEAD_INIT(AvahiServiceTypeBrowser, client->service_type_browsers);
+ client->bus = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
if (dbus_error_is_set (&error)) {
+ if (ret_error)
+ *ret_error = translate_dbus_error(&error);
goto fail;
}
-/* dbus_connection_setup_with_g_main (tmp->bus, NULL); */
-/* dbus_connection_set_exit_on_disconnect (tmp->bus, FALSE); */
-
- if (avahi_dbus_connection_glue(tmp->bus, poll_api) < 0)
+ if (avahi_dbus_connection_glue(client->bus, poll_api) < 0) {
+ if (ret_error)
+ *ret_error = AVAHI_ERR_NO_MEMORY; /* Not optimal */
goto fail;
+ }
- if (!dbus_connection_add_filter (tmp->bus, filter_func, tmp, NULL))
- {
+ if (!dbus_connection_add_filter (client->bus, filter_func, client, NULL)) {
+ if (ret_error)
+ *ret_error = AVAHI_ERR_NO_MEMORY;
goto fail;
}
+
+ dbus_bus_add_match(
+ client->bus,
+ "type='signal', "
+ "interface='" AVAHI_DBUS_INTERFACE_SERVER "', "
+ "sender='" AVAHI_DBUS_NAME "', "
+ "path='" AVAHI_DBUS_PATH_SERVER "'",
+ &error);
- dbus_bus_add_match (tmp->bus,
- "type='signal', "
- "interface='" AVAHI_DBUS_INTERFACE_SERVER "', "
- "sender='" AVAHI_DBUS_NAME "', "
- "path='" AVAHI_DBUS_PATH_SERVER "'",
- &error);
-
- if (dbus_error_is_set (&error))
- {
+ if (dbus_error_is_set (&error)) {
+ if (ret_error)
+ *ret_error = translate_dbus_error(&error);
goto fail;
}
- dbus_bus_add_match (tmp->bus,
- "type='signal', "
- "interface='" DBUS_INTERFACE_DBUS "', "
- "sender='" DBUS_SERVICE_DBUS "', "
- "path='" DBUS_PATH_DBUS "'",
- &error);
+ dbus_bus_add_match (
+ client->bus,
+ "type='signal', "
+ "interface='" DBUS_INTERFACE_DBUS "', "
+ "sender='" DBUS_SERVICE_DBUS "', "
+ "path='" DBUS_PATH_DBUS "'",
+ &error);
- if (dbus_error_is_set (&error))
- {
+ if (dbus_error_is_set (&error)) {
+ if (ret_error)
+ *ret_error = translate_dbus_error(&error);
goto fail;
}
- tmp->callback = callback;
- tmp->user_data = user_data;
+ if (!(dbus_bus_name_has_owner(client->bus, AVAHI_DBUS_NAME, &error))) {
- avahi_client_schedule_state_request (tmp);
+ if (dbus_error_is_set (&error)) {
+ if (ret_error)
+ *ret_error = translate_dbus_error(&error);
+ goto fail;
+ }
+
+ if (ret_error)
+ *ret_error = AVAHI_ERR_NO_DAEMON;
+ goto fail;
+ }
+
+ /* This can't happen asynchronously, since it is not guaranteed that a main loop is used */
+
+ /*client_get_server_state (client);*/
- avahi_client_set_errno (tmp, AVAHI_OK);
- return tmp;
+ return client;
fail:
- avahi_free (tmp);
+
+ if (client) {
+
+ if (client->bus) {
+ dbus_connection_disconnect(client->bus);
+ dbus_connection_unref(client->bus);
+ }
+
+ avahi_free(client);
+ }
if (dbus_error_is_set(&error))
dbus_error_free(&error);
typedef void (*AvahiEntryGroupCallback) (AvahiEntryGroup *g, AvahiEntryGroupState state, void* userdata);
/** The function prototype for the callback of an AvahiDomainBrowser */
-typedef void (*AvahiDomainBrowserCallback) (AvahiDomainBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const char *domain, void *user_data);
+typedef void (*AvahiDomainBrowserCallback) (AvahiDomainBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const char *domain, void *userdata);
/** The function prototype for the callback of an AvahiServiceBrowser */
-typedef void (*AvahiServiceBrowserCallback) (AvahiServiceBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const char *name, const char *type, const char *domain, void *user_data);
+typedef void (*AvahiServiceBrowserCallback) (AvahiServiceBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const char *name, const char *type, const char *domain, void *userdata);
/** The function prototype for the callback of an AvahiServiceTypeBrowser */
-typedef void (*AvahiServiceTypeBrowserCallback) (AvahiServiceTypeBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const char *type, const char *domain, void *user_data);
+typedef void (*AvahiServiceTypeBrowserCallback) (AvahiServiceTypeBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const char *type, const char *domain, void *userdata);
/** Creates a new client instance */
-AvahiClient* avahi_client_new (const AvahiPoll *poll_api, AvahiClientCallback callback, void *user_data);
+AvahiClient* avahi_client_new (const AvahiPoll *poll_api, AvahiClientCallback callback, void *userdata, int *error);
/** Get the version of the server */
char* avahi_client_get_version_string (AvahiClient*);
char* avahi_client_get_host_name_fqdn (AvahiClient*);
/** Create a new AvahiEntryGroup object */
-AvahiEntryGroup* avahi_entry_group_new (AvahiClient*, AvahiEntryGroupCallback callback, void *user_data);
+AvahiEntryGroup* avahi_entry_group_new (AvahiClient*, AvahiEntryGroupCallback callback, void *userdata);
/** Clean up and free an AvahiEntryGroup object */
int avahi_entry_group_free (AvahiEntryGroup *);
const char *domain,
AvahiDomainBrowserType btype,
AvahiDomainBrowserCallback callback,
- void *user_data);
+ void *userdata);
/** Get the D-Bus path of an AvahiDomainBrowser object, for debugging purposes only. */
const char* avahi_domain_browser_path (AvahiDomainBrowser *);
AvahiProtocol protocol,
const char *domain,
AvahiServiceTypeBrowserCallback callback,
- void *user_data);
+ void *userdata);
/** Get the D-Bus path of an AvahiServiceTypeBrowser object, for debugging purposes only. */
const char* avahi_service_type_browser_path (AvahiServiceTypeBrowser *);
const char *type,
const char *domain,
AvahiServiceBrowserCallback callback,
- void *user_data);
+ void *userdata);
/** Get the D-Bus path of an AvahiServiceBrowser object, for debugging purposes only. */
const char* avahi_service_browser_path (AvahiServiceBrowser *);
if (group == NULL || group->callback == NULL)
return;
- group->callback (group, state, group->user_data);
+ group->callback (group, state, group->userdata);
}
AvahiEntryGroup*
tmp->path = avahi_strdup (path);
tmp->callback = callback;
- tmp->user_data = user_data;
+ tmp->userdata = user_data;
AVAHI_LLIST_PREPEND(AvahiEntryGroup, groups, client->groups, tmp);
const AvahiPoll *poll_api;
DBusConnection *bus;
int error;
+ AvahiServerState state;
+
AvahiClientCallback callback;
- void *user_data;
+ void *userdata;
+
AVAHI_LLIST_HEAD(AvahiEntryGroup, groups);
AVAHI_LLIST_HEAD(AvahiDomainBrowser, domain_browsers);
AVAHI_LLIST_HEAD(AvahiServiceBrowser, service_browsers);
char *path;
AvahiClient *client;
AvahiEntryGroupCallback callback;
- void *user_data;
+ void *userdata;
AVAHI_LLIST_FIELDS(AvahiEntryGroup, groups);
};
char *path;
AvahiClient *client;
AvahiDomainBrowserCallback callback;
- void *user_data;
+ void *userdata;
AVAHI_LLIST_FIELDS(AvahiDomainBrowser, domain_browsers);
};
char *path;
AvahiClient *client;
AvahiServiceBrowserCallback callback;
- void *user_data;
+ void *userdata;
AVAHI_LLIST_FIELDS(AvahiServiceBrowser, service_browsers);
};
char *path;
AvahiClient *client;
AvahiServiceTypeBrowserCallback callback;
- void *user_data;
+ void *userdata;
AVAHI_LLIST_FIELDS(AvahiServiceTypeBrowser, service_type_browsers);
};
#define AVAHI_DBUS_ERR_DBUS_ERROR "org.freedesktop.Avahi.DBusError"
#define AVAHI_DBUS_ERR_NOT_CONNECTED "org.freedesktop.Avahi.NotConnectedError"
#define AVAHI_DBUS_ERR_NO_MEMORY "org.freedesktop.Avahi.NoMemoryError"
-#define AVAHI_DBUS_ERR_INVALID_OBJECT "org.freedesktop.Avahi.InvalidObject"
+#define AVAHI_DBUS_ERR_INVALID_OBJECT "org.freedesktop.Avahi.InvalidObjectError"
+#define AVAHI_DBUS_ERR_NO_DAEMON "org.freedesktop.Avahi.NoDaemonError"
AVAHI_C_DECL_END
#ifndef foodomainhfoo
-#define foodimainhfoo
+#define foodomainhfoo
/* $Id$ */
#include <avahi-common/cdecl.h>
+#ifndef DOXYGEN_SHOULD_SKIP_THIS
AVAHI_C_DECL_BEGIN
+#endif
/** Normalize a domain name into canonical form. This drops trailing
* dots and removes useless backslash escapes. avahi_free() the
/** Return some kind of hash value for the domain, useful for using domains as hash table keys. */
unsigned avahi_domain_hash(const char *name);
-
-
+#ifndef DOXYGEN_SHOULD_SKIP_THIS
AVAHI_C_DECL_END
+#endif
#endif
"Invalid operation",
"An unexpected DBUS error occured",
"Could not get a connection to the daemon",
- "Memory exhausted"
- "The object passed in was not valid"
+ "Memory exhausted",
+ "The object passed in was not valid",
+ "Daemon not running",
};
if (-error < 0 || -error >= -AVAHI_ERR_MAX)
AVAHI_ERR_NOT_CONNECTED = -23, /**< Could not get a connection to the daemon */
AVAHI_ERR_NO_MEMORY = -24, /**< Memory exhausted */
AVAHI_ERR_INVALID_OBJECT = -25, /**< The object passed to this function was invalid */
+ AVAHI_ERR_NO_DAEMON = -26, /**< Daemon not running */
/****
**** IF YOU ADD A NEW ERROR CODE HERE, PLEASE DON'T FORGET TO ADD
**** Also remember to update the MAX value below.
****/
- AVAHI_ERR_MAX = -26
+ AVAHI_ERR_MAX = -27
};
/** Return a human readable error string for the specified error code */
#include <avahi-common/cdecl.h>
+#ifndef DOXYGEN_SHOULD_SKIP_THIS
AVAHI_C_DECL_BEGIN
+#endif
+/** A numeric data type for storing microsecond values. (signed 64bit integer) */
typedef int64_t AvahiUsec;
+/** Compare two timeval structures and return a ngeative value when a < b, 0 when a == b and a positive value otherwise */
int avahi_timeval_compare(const struct timeval *a, const struct timeval *b);
+
+/** Calculate the difference between to timeval structures as microsecond value */
AvahiUsec avahi_timeval_diff(const struct timeval *a, const struct timeval *b);
+
+/** Add a number of microseconds to the specified timeval structure and return it. *a is modified. */
struct timeval* avahi_timeval_add(struct timeval *a, AvahiUsec usec);
+/** Return the difference between the current time and *a. Positive if *a was earlier */
AvahiUsec avahi_age(const struct timeval *a);
-struct timeval *avahi_elapse_time(struct timeval *tv, unsigned msec, unsigned jitter);
+/** Fill *tv with the current time plus "ms" milliseconds plus an
+ * extra jitter of "j" milliseconds. Pass 0 for j if you don't want
+ * the jitter */
+struct timeval *avahi_elapse_time(struct timeval *tv, unsigned ms, unsigned j);
+
+#ifndef DOXYGEN_SHOULD_SKIP_THIS
AVAHI_C_DECL_END
+#endif
#endif
AvahiEntryFlags flags,
uint32_t ttl, /**< DNS TTL for this record */
const char *name, /**< TXT record name */
- ... /**< Text record data, terminated by NULL */);
+ ... /**< Text record data, terminated by NULL */) AVAHI_GCC_SENTINEL;
/** Add a PTR RR to the server. Mostly identical to
* avahi_server_add_text but takes a va_list instead of a variable
const char *domain,
const char *host, /**< Host name where this servcie resides, or NULL if on the local host */
uint16_t port, /**< Port number of the service */
- ... /**< Text records, terminated by NULL */);
+ ... /**< Text records, terminated by NULL */) AVAHI_GCC_SENTINEL;
/** Mostly identical to avahi_server_add_service(), but takes an va_list for the TXT records. */
int avahi_server_add_service_va(
AVAHI_DBUS_ERR_DBUS_ERROR,
AVAHI_DBUS_ERR_NOT_CONNECTED,
AVAHI_DBUS_ERR_NO_MEMORY,
- AVAHI_DBUS_ERR_INVALID_OBJECT
+ AVAHI_DBUS_ERR_INVALID_OBJECT,
+ AVAHI_DBUS_ERR_NO_DAEMON
};
assert(-error > -AVAHI_OK);
goto fail;
}
- dbus_bus_add_match(server->bus, "type='signal',""interface='" DBUS_INTERFACE_DBUS "'", &error);
-
dbus_connection_add_filter(server->bus, msg_signal_filter_impl, (void*) poll_api, NULL);
+ dbus_bus_add_match(server->bus, "type='signal',""interface='" DBUS_INTERFACE_DBUS "'", &error);
dbus_connection_register_object_path(server->bus, AVAHI_DBUS_PATH_SERVER, &server_vtable, NULL);
return 0;