From 6ecd227ceadc32ed229246aa61df6361c7b58a95 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Tue, 8 Nov 2005 21:56:28 +0000 Subject: [PATCH] * add new flags parameter to avahi_client_new() * introduce flags AVAHI_CLIENT_NO_FAIL and AVAHI_CLIENT_IGNORE_USER_CONFIG * beef up avahi-browse to show how AVAHI_CLIENT_NO_FAIL may be used: add a new parameter "-f" which tells avahi-browse to stay running when the daemon disconnects from DBUS and reconnect when it reappears. For now dbus-daemon restarts are not handled and result in an AVAHI_CLIENT_FAIL event. git-svn-id: file:///home/lennart/svn/public/avahi/trunk@937 941a03a8-eaeb-0310-b9a0-b1bbd8fe43fe --- avahi-client/browser.c | 18 +-- avahi-client/client-test.c | 4 +- avahi-client/client.c | 175 +++++++++++++++++--------- avahi-client/client.h | 10 +- avahi-client/entrygroup.c | 20 +-- avahi-client/internal.h | 3 + avahi-client/resolver.c | 12 +- avahi-client/srv-test.c | 6 +- avahi-compat-howl/compat.c | 3 +- avahi-compat-libdns_sd/compat.c | 10 +- avahi-utils/avahi-browse.c | 133 ++++++++++++++------ avahi-utils/avahi-resolve-host-name.c | 3 +- examples/client-browse-services.c | 2 +- examples/client-publish-service.c | 3 +- examples/glib-integration.c | 1 + 15 files changed, 266 insertions(+), 137 deletions(-) diff --git a/avahi-client/browser.c b/avahi-client/browser.c index bf9c15f..c9af43f 100644 --- a/avahi-client/browser.c +++ b/avahi-client/browser.c @@ -125,7 +125,7 @@ AvahiDomainBrowser* avahi_domain_browser_new( dbus_error_init (&error); - if (client->state == AVAHI_CLIENT_FAILURE) { + if (!avahi_client_is_connected(client)) { avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE); goto fail; } @@ -150,8 +150,10 @@ AvahiDomainBrowser* avahi_domain_browser_new( AVAHI_LLIST_PREPEND(AvahiDomainBrowser, domain_browsers, client->domain_browsers, db); - parse_environment(db); - parse_domain_file(db); + if (!(client->flags & AVAHI_CLIENT_IGNORE_USER_CONFIG)) { + parse_environment(db); + parse_domain_file(db); + } db->static_browse_domains = avahi_string_list_reverse(db->static_browse_domains); @@ -248,7 +250,7 @@ int avahi_domain_browser_free (AvahiDomainBrowser *b) { client = b->client; - if (b->path && client->state != AVAHI_CLIENT_FAILURE) + if (b->path && avahi_client_is_connected(b->client)) r = avahi_client_simple_method_call(client, b->path, AVAHI_DBUS_INTERFACE_DOMAIN_BROWSER, "Free"); AVAHI_LLIST_REMOVE(AvahiDomainBrowser, domain_browsers, client->domain_browsers, b); @@ -367,7 +369,7 @@ AvahiServiceTypeBrowser* avahi_service_type_browser_new( dbus_error_init(&error); - if (client->state == AVAHI_CLIENT_FAILURE) { + if (!avahi_client_is_connected(client)) { avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE); goto fail; } @@ -473,7 +475,7 @@ int avahi_service_type_browser_free (AvahiServiceTypeBrowser *b) { assert(b); client = b->client; - if (b->path && client->state != AVAHI_CLIENT_FAILURE) + if (b->path && avahi_client_is_connected(b->client)) r = avahi_client_simple_method_call(client, b->path, AVAHI_DBUS_INTERFACE_SERVICE_TYPE_BROWSER, "Free"); AVAHI_LLIST_REMOVE(AvahiServiceTypeBrowser, service_type_browsers, b->client->service_type_browsers, b); @@ -583,7 +585,7 @@ AvahiServiceBrowser* avahi_service_browser_new( dbus_error_init(&error); - if (client->state == AVAHI_CLIENT_FAILURE) { + if (!avahi_client_is_connected(client)) { avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE); goto fail; } @@ -694,7 +696,7 @@ int avahi_service_browser_free (AvahiServiceBrowser *b) { assert(b); client = b->client; - if (b->path && client->state != AVAHI_CLIENT_FAILURE) + if (b->path && avahi_client_is_connected(b->client)) r = avahi_client_simple_method_call(client, b->path, AVAHI_DBUS_INTERFACE_SERVICE_BROWSER, "Free"); AVAHI_LLIST_REMOVE(AvahiServiceBrowser, service_browsers, b->client->service_browsers, b); diff --git a/avahi-client/client-test.c b/avahi-client/client-test.c index 22ca073..6ca99d6 100644 --- a/avahi-client/client-test.c +++ b/avahi-client/client-test.c @@ -228,7 +228,7 @@ int main (AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char *argv[]) { simple_poll = avahi_simple_poll_new(); poll_api = avahi_simple_poll_get(simple_poll); - if (!(avahi = avahi_client_new(poll_api, avahi_client_callback, "omghai2u", &error))) { + if (!(avahi = avahi_client_new(poll_api, 0, avahi_client_callback, "omghai2u", &error))) { fprintf(stderr, "Client failed: %s\n", avahi_strerror(error)); goto fail; } @@ -250,7 +250,7 @@ int main (AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char *argv[]) { cookie = avahi_client_get_local_service_cookie(avahi); printf("Local service cookie: %u (Error Return: %s)\n", cookie, cookie != AVAHI_SERVICE_COOKIE_INVALID ? "OK" : avahi_strerror(avahi_client_errno(avahi))); - group = avahi_entry_group_new (avahi, avahi_entry_group_callback, "omghai"); + group = avahi_entry_group_new(avahi, avahi_entry_group_callback, "omghai"); printf("Creating entry group: %s\n", group ? "OK" : avahi_strerror(avahi_client_errno (avahi))); assert(group); diff --git a/avahi-client/client.c b/avahi-client/client.c index ef77366..1067456 100644 --- a/avahi-client/client.c +++ b/avahi-client/client.c @@ -39,6 +39,8 @@ #include "client.h" #include "internal.h" +static int init_server(AvahiClient *client, int *ret_error); + int avahi_client_set_errno (AvahiClient *client, int error) { assert(client); @@ -84,6 +86,7 @@ static void client_set_state (AvahiClient *client, AvahiServerState state) { break; case AVAHI_CLIENT_S_RUNNING: + case AVAHI_CLIENT_CONNECTING: break; } @@ -99,21 +102,18 @@ static DBusHandlerResult filter_func(DBusConnection *bus, DBusMessage *message, assert(bus); assert(message); - dbus_error_init (&error); + dbus_error_init(&error); /* fprintf(stderr, "dbus: interface=%s, path=%s, member=%s\n", */ /* dbus_message_get_interface (message), */ /* dbus_message_get_path (message), */ /* dbus_message_get_member (message)); */ - if (client->state == AVAHI_CLIENT_FAILURE) - goto fail; - if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) { /* The DBUS server died or kicked us */ avahi_client_set_errno(client, AVAHI_ERR_DISCONNECTED); - client_set_state(client, AVAHI_CLIENT_FAILURE); + goto fail; } if (dbus_message_is_signal(message, DBUS_INTERFACE_DBUS, "NameOwnerChanged")) { char *name, *old, *new; @@ -123,22 +123,39 @@ static DBusHandlerResult filter_func(DBusConnection *bus, DBusMessage *message, DBUS_TYPE_STRING, &name, DBUS_TYPE_STRING, &old, DBUS_TYPE_STRING, &new, - DBUS_TYPE_INVALID) || dbus_error_is_set (&error)) { + DBUS_TYPE_INVALID) || dbus_error_is_set(&error)) { fprintf(stderr, "WARNING: Failed to parse NameOwnerChanged signal: %s\n", error.message); + avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR); goto fail; } if (strcmp(name, AVAHI_DBUS_NAME) == 0) { - /* Regardless if the server lost or acquired its name or - * if the name was transfered: our services are no longer - * available, so we disconnect ourselves */ + if (avahi_client_is_connected(client)) { - avahi_client_set_errno(client, AVAHI_ERR_DISCONNECTED); - client_set_state(client, AVAHI_CLIENT_FAILURE); + /* Regardless if the server lost or acquired its name or + * if the name was transfered: our services are no longer + * available, so we disconnect ourselves */ + avahi_client_set_errno(client, AVAHI_ERR_DISCONNECTED); + goto fail; + + } else if (client->state == AVAHI_CLIENT_CONNECTING && (!old || *old == 0)) { + int ret; + + /* Server appeared */ + + if ((ret = init_server(client, NULL)) < 0) { + avahi_client_set_errno(client, ret); + goto fail; + } + } } + } else if (!avahi_client_is_connected(client)) { + + /* Ignore messages, we get in unconnected state */ + } else if (dbus_message_is_signal (message, AVAHI_DBUS_INTERFACE_SERVER, "StateChanged")) { int32_t state; char *e = NULL; @@ -149,7 +166,9 @@ static DBusHandlerResult filter_func(DBusConnection *bus, DBusMessage *message, DBUS_TYPE_INT32, &state, DBUS_TYPE_STRING, &e, DBUS_TYPE_INVALID) || dbus_error_is_set (&error)) { + fprintf(stderr, "WARNING: Failed to parse Server.StateChanged signal: %s\n", error.message); + avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR); goto fail; } @@ -178,7 +197,9 @@ static DBusHandlerResult filter_func(DBusConnection *bus, DBusMessage *message, DBUS_TYPE_STRING, &e, DBUS_TYPE_INVALID) || dbus_error_is_set(&error)) { + fprintf(stderr, "WARNING: Failed to parse EntryGroup.StateChanged signal: %s\n", error.message); + avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR); goto fail; } @@ -235,12 +256,27 @@ static DBusHandlerResult filter_func(DBusConnection *bus, DBusMessage *message, return avahi_address_resolver_event (client, AVAHI_RESOLVER_FOUND, message); else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_ADDRESS_RESOLVER, "Failure")) return avahi_address_resolver_event (client, AVAHI_RESOLVER_FAILURE, message); + else { + + fprintf(stderr, "WARNING: Unhandled message: interface=%s, path=%s, member=%s\n", + dbus_message_get_interface(message), + dbus_message_get_path(message), + dbus_message_get_member(message)); + + return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; + } return DBUS_HANDLER_RESULT_HANDLED; fail: - dbus_error_free (&error); + if (dbus_error_is_set(&error)) { + avahi_client_set_errno(client, avahi_error_dbus_to_number(error.name)); + dbus_error_free(&error); + } + + client_set_state(client, AVAHI_CLIENT_FAILURE); + return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; } @@ -339,45 +375,46 @@ fail: return e; } +static int init_server(AvahiClient *client, int *ret_error) { + int r; + + if ((r = check_version(client, ret_error)) < 0) + return r; -/* This function acts like dbus_bus_get but creates a private - * connection instead */ -static DBusConnection* -avahi_dbus_bus_get (DBusError *error) -{ - DBusConnection *conn; - const char *env_addr; - - env_addr = getenv ("DBUS_SYSTEM_BUS_ADDRESS"); + if ((r = get_server_state(client, ret_error)) < 0) + return r; - if (env_addr == NULL || (*env_addr == 0)) - { - env_addr = DBUS_SYSTEM_BUS_DEFAULT_ADDRESS; - } + return AVAHI_OK; +} - conn = dbus_connection_open_private (env_addr, error); +/* This function acts like dbus_bus_get but creates a private + * connection instead */ +static DBusConnection* avahi_dbus_bus_get(DBusError *error) { + DBusConnection *c; + const char *a; - if (!conn) + if (!(a = getenv("DBUS_SYSTEM_BUS_ADDRESS")) || !*a) + a = DBUS_SYSTEM_BUS_DEFAULT_ADDRESS; + + if (!(c = dbus_connection_open_private(a, error))) return NULL; - dbus_connection_set_exit_on_disconnect (conn, FALSE); - - if (!dbus_bus_register (conn, error)) - { - dbus_connection_close (conn); - dbus_connection_unref (conn); + dbus_connection_set_exit_on_disconnect(c, FALSE); + if (!dbus_bus_register(c, error)) { + dbus_connection_close(c); + dbus_connection_unref(c); return NULL; } - return conn; + return c; } -AvahiClient *avahi_client_new(const AvahiPoll *poll_api, AvahiClientCallback callback, void *userdata, int *ret_error) { +AvahiClient *avahi_client_new(const AvahiPoll *poll_api, AvahiClientFlags flags, AvahiClientCallback callback, void *userdata, int *ret_error) { AvahiClient *client = NULL; DBusError error; - dbus_error_init (&error); + dbus_error_init(&error); if (!(client = avahi_new(AvahiClient, 1))) { if (ret_error) @@ -389,8 +426,9 @@ AvahiClient *avahi_client_new(const AvahiPoll *poll_api, AvahiClientCallback cal client->error = AVAHI_OK; client->callback = callback; client->userdata = userdata; - client->state = AVAHI_CLIENT_FAILURE; - + client->state = (AvahiClientState) -1; + client->flags = flags; + client->host_name = NULL; client->host_name_fqdn = NULL; client->domain_name = NULL; @@ -405,9 +443,11 @@ AvahiClient *avahi_client_new(const AvahiPoll *poll_api, AvahiClientCallback cal AVAHI_LLIST_HEAD_INIT(AvahiHostNameResolver, client->host_name_resolvers); AVAHI_LLIST_HEAD_INIT(AvahiAddressResolver, client->address_resolvers); - if (!(client->bus = avahi_dbus_bus_get(&error)) || - dbus_error_is_set (&error)) + if (!(client->bus = avahi_dbus_bus_get(&error)) || dbus_error_is_set(&error)) { + if (ret_error) + *ret_error = AVAHI_ERR_DBUS_ERROR; goto fail; + } if (avahi_dbus_connection_glue(client->bus, poll_api) < 0) { if (ret_error) @@ -429,7 +469,7 @@ AvahiClient *avahi_client_new(const AvahiPoll *poll_api, AvahiClientCallback cal "path='" AVAHI_DBUS_PATH_SERVER "'", &error); - if (dbus_error_is_set (&error)) + if (dbus_error_is_set(&error)) goto fail; dbus_bus_add_match ( @@ -440,36 +480,43 @@ AvahiClient *avahi_client_new(const AvahiPoll *poll_api, AvahiClientCallback cal "path='" DBUS_PATH_DBUS "'", &error); - if (dbus_error_is_set (&error)) + if (dbus_error_is_set(&error)) goto fail; - dbus_bus_add_match ( + dbus_bus_add_match ( client->bus, "type='signal', " "interface='" DBUS_INTERFACE_LOCAL "'", &error); - if (dbus_error_is_set (&error)) + if (dbus_error_is_set(&error)) goto fail; + if (!(dbus_bus_name_has_owner(client->bus, AVAHI_DBUS_NAME, &error)) || dbus_error_is_set(&error)) { /* We free the error so its not set, that way the fail target * will return the NO_DAEMON error rather than a DBUS error */ - dbus_error_free (&error); + dbus_error_free(&error); - if (ret_error) - *ret_error = AVAHI_ERR_NO_DAEMON; + if (!(flags & AVAHI_CLIENT_NO_FAIL)) { + + if (ret_error) + *ret_error = AVAHI_ERR_NO_DAEMON; - goto fail; - } + goto fail; + } - if (check_version(client, ret_error) < 0) - goto fail; + /* The user doesn't want this call to fail if the daemon is not + * available, so let's return succesfully */ + client_set_state(client, AVAHI_CLIENT_CONNECTING); + + } else { - if (get_server_state(client, ret_error) < 0) - goto fail; + if (init_server(client, ret_error) < 0) + goto fail; + } return client; @@ -576,10 +623,10 @@ fail: return NULL; } -const char* avahi_client_get_version_string (AvahiClient *client) { +const char* avahi_client_get_version_string(AvahiClient *client) { assert(client); - if (client->state == AVAHI_CLIENT_FAILURE) { + if (!avahi_client_is_connected(client)) { avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE); return NULL; } @@ -590,10 +637,10 @@ const char* avahi_client_get_version_string (AvahiClient *client) { return client->version_string; } -const char* avahi_client_get_domain_name (AvahiClient *client) { +const char* avahi_client_get_domain_name(AvahiClient *client) { assert(client); - if (client->state == AVAHI_CLIENT_FAILURE) { + if (!avahi_client_is_connected(client)) { avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE); return NULL; } @@ -604,10 +651,10 @@ const char* avahi_client_get_domain_name (AvahiClient *client) { return client->domain_name; } -const char* avahi_client_get_host_name (AvahiClient *client) { +const char* avahi_client_get_host_name(AvahiClient *client) { assert(client); - if (client->state == AVAHI_CLIENT_FAILURE) { + if (!avahi_client_is_connected(client)) { avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE); return NULL; } @@ -621,7 +668,7 @@ const char* avahi_client_get_host_name (AvahiClient *client) { const char* avahi_client_get_host_name_fqdn (AvahiClient *client) { assert(client); - if (client->state == AVAHI_CLIENT_FAILURE) { + if (!avahi_client_is_connected(client)) { avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE); return NULL; } @@ -699,7 +746,7 @@ uint32_t avahi_client_get_local_service_cookie(AvahiClient *client) { DBusError error; assert(client); - if (client->state == AVAHI_CLIENT_FAILURE) { + if (!avahi_client_is_connected(client)) { avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE); return AVAHI_SERVICE_COOKIE_INVALID; } @@ -743,3 +790,9 @@ fail: return AVAHI_SERVICE_COOKIE_INVALID; } + +int avahi_client_is_connected(AvahiClient *client) { + assert(client); + + return client->state == AVAHI_CLIENT_S_RUNNING || client->state == AVAHI_CLIENT_S_REGISTERING || client->state == AVAHI_CLIENT_S_COLLISION; +} diff --git a/avahi-client/client.h b/avahi-client/client.h index 3e18fca..9d73778 100644 --- a/avahi-client/client.h +++ b/avahi-client/client.h @@ -46,14 +46,20 @@ typedef enum { AVAHI_CLIENT_S_REGISTERING = AVAHI_SERVER_REGISTERING, /**< Server state: REGISTERING */ AVAHI_CLIENT_S_RUNNING = AVAHI_SERVER_RUNNING, /**< Server state: RUNNING */ AVAHI_CLIENT_S_COLLISION = AVAHI_SERVER_COLLISION, /**< Server state: COLLISION */ - AVAHI_CLIENT_FAILURE = 100 /**< Some kind of error happened on the client side */ + AVAHI_CLIENT_FAILURE = 100, /**< Some kind of error happened on the client side */ + AVAHI_CLIENT_CONNECTING = 101 /**< We're still connecting. This state is only entered when AVAHI_CLIENT_NO_FAIL has been passed to avahi_client_new() and the daemon is not yet available. */ } AvahiClientState; +typedef enum { + AVAHI_CLIENT_IGNORE_USER_CONFIG = 1, /**< Don't read user configuration */ + AVAHI_CLIENT_NO_FAIL = 2 /**< Don't fail if the daemon is not available when avahi_client_new() is called, instead enter AVAHI_CLIENT_CONNECTING state and wait for the daemon to appear */ +} AvahiClientFlags; + /** The function prototype for the callback of an AvahiClient */ typedef void (*AvahiClientCallback) (AvahiClient *s, AvahiClientState state, void* userdata); /** Creates a new client instance */ -AvahiClient* avahi_client_new (const AvahiPoll *poll_api, AvahiClientCallback callback, void *userdata, int *error); +AvahiClient* avahi_client_new (const AvahiPoll *poll_api, AvahiClientFlags flags, AvahiClientCallback callback, void *userdata, int *error); /** Free a client instance */ void avahi_client_free(AvahiClient *client); diff --git a/avahi-client/entrygroup.c b/avahi-client/entrygroup.c index 4c5c914..003b17c 100644 --- a/avahi-client/entrygroup.c +++ b/avahi-client/entrygroup.c @@ -111,7 +111,7 @@ AvahiEntryGroup* avahi_entry_group_new (AvahiClient *client, AvahiEntryGroupCall dbus_error_init (&error); - if (client->state == AVAHI_CLIENT_FAILURE) { + if (!avahi_client_is_connected(client)) { avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE); goto fail; } @@ -237,7 +237,7 @@ int avahi_entry_group_free(AvahiEntryGroup *group) { assert(group); - if (group->path && client->state != AVAHI_CLIENT_FAILURE) + if (group->path && !avahi_client_is_connected(client)) r = entry_group_simple_method_call(group, "Free"); AVAHI_LLIST_REMOVE(AvahiEntryGroup, groups, client->groups, group); @@ -251,7 +251,7 @@ int avahi_entry_group_free(AvahiEntryGroup *group) { int avahi_entry_group_commit(AvahiEntryGroup *group) { assert(group); - if (!group->path || group->client->state == AVAHI_CLIENT_FAILURE) + 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"); @@ -260,7 +260,7 @@ int avahi_entry_group_commit(AvahiEntryGroup *group) { int avahi_entry_group_reset(AvahiEntryGroup *group) { assert(group); - if (!group->path || group->client->state == AVAHI_CLIENT_FAILURE) + 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"); @@ -288,7 +288,7 @@ int avahi_entry_group_is_empty (AvahiEntryGroup *group) { assert(group); client = group->client; - if (!group->path || group->client->state == AVAHI_CLIENT_FAILURE) + if (!group->path || !avahi_client_is_connected(group->client)) return avahi_client_set_errno(group->client, AVAHI_ERR_BAD_STATE); dbus_error_init(&error); @@ -409,7 +409,7 @@ int avahi_entry_group_add_service_strlst( client = group->client; - if (!group->path || group->client->state == AVAHI_CLIENT_FAILURE) + if (!group->path || !avahi_client_is_connected(group->client)) return avahi_client_set_errno(group->client, AVAHI_ERR_BAD_STATE); if (!domain) @@ -528,7 +528,7 @@ int avahi_entry_group_add_service_subtype( client = group->client; - if (!group->path || group->client->state == AVAHI_CLIENT_FAILURE) + if (!group->path || !avahi_client_is_connected(group->client)) return avahi_client_set_errno(group->client, AVAHI_ERR_BAD_STATE); if (!domain) @@ -638,7 +638,7 @@ int avahi_entry_group_update_service_txt_strlst( client = group->client; - if (!group->path || group->client->state == AVAHI_CLIENT_FAILURE) + if (!group->path || !avahi_client_is_connected(group->client)) return avahi_client_set_errno(group->client, AVAHI_ERR_BAD_STATE); if (!domain) @@ -726,7 +726,7 @@ int avahi_entry_group_add_address( client = group->client; - if (!group->path || group->client->state == AVAHI_CLIENT_FAILURE) + if (!group->path || !avahi_client_is_connected(group->client)) return avahi_client_set_errno(group->client, AVAHI_ERR_BAD_STATE); dbus_error_init(&error); @@ -815,7 +815,7 @@ int avahi_entry_group_add_record( client = group->client; - if (!group->path || group->client->state == AVAHI_CLIENT_FAILURE) + if (!group->path || !avahi_client_is_connected(group->client)) return avahi_client_set_errno(group->client, AVAHI_ERR_BAD_STATE); dbus_error_init(&error); diff --git a/avahi-client/internal.h b/avahi-client/internal.h index 31f3ce3..ef629e6 100644 --- a/avahi-client/internal.h +++ b/avahi-client/internal.h @@ -33,6 +33,7 @@ struct AvahiClient { DBusConnection *bus; int error; AvahiClientState state; + AvahiClientFlags flags; /* Cache for some seldom changing server data */ char *version_string, *host_name, *host_name_fqdn, *domain_name; @@ -152,4 +153,6 @@ DBusHandlerResult avahi_address_resolver_event (AvahiClient *client, AvahiResolv int avahi_client_simple_method_call(AvahiClient *client, const char *path, const char *interface, const char *method); +int avahi_client_is_connected(AvahiClient *client); + #endif diff --git a/avahi-client/resolver.c b/avahi-client/resolver.c index 2301134..2d9f808 100644 --- a/avahi-client/resolver.c +++ b/avahi-client/resolver.c @@ -209,7 +209,7 @@ AvahiServiceResolver * avahi_service_resolver_new( dbus_error_init (&error); - if (client->state == AVAHI_CLIENT_FAILURE) { + if (!avahi_client_is_connected(client)) { avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE); goto fail; } @@ -331,7 +331,7 @@ int avahi_service_resolver_free(AvahiServiceResolver *r) { assert(r); client = r->client; - if (r->path && client->state != AVAHI_CLIENT_FAILURE) + if (r->path && avahi_client_is_connected(client)) ret = avahi_client_simple_method_call(client, r->path, AVAHI_DBUS_INTERFACE_SERVICE_RESOLVER, "Free"); AVAHI_LLIST_REMOVE(AvahiServiceResolver, service_resolvers, client->service_resolvers, r); @@ -446,7 +446,7 @@ AvahiHostNameResolver * avahi_host_name_resolver_new( dbus_error_init (&error); - if (client->state == AVAHI_CLIENT_FAILURE) { + if (!avahi_client_is_connected(client)) { avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE); goto fail; } @@ -546,7 +546,7 @@ int avahi_host_name_resolver_free(AvahiHostNameResolver *r) { assert(r); client = r->client; - if (r->path && client->state != AVAHI_CLIENT_FAILURE) + if (r->path && avahi_client_is_connected(client)) ret = avahi_client_simple_method_call(client, r->path, AVAHI_DBUS_INTERFACE_HOST_NAME_RESOLVER, "Free"); AVAHI_LLIST_REMOVE(AvahiHostNameResolver, host_name_resolvers, client->host_name_resolvers, r); @@ -669,7 +669,7 @@ AvahiAddressResolver * avahi_address_resolver_new( return NULL; } - if (client->state == AVAHI_CLIENT_FAILURE) { + if (!avahi_client_is_connected(client)) { avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE); goto fail; } @@ -768,7 +768,7 @@ int avahi_address_resolver_free(AvahiAddressResolver *r) { assert(r); client = r->client; - if (r->path && client->state != AVAHI_CLIENT_FAILURE) + if (r->path && avahi_client_is_connected(client)) ret = avahi_client_simple_method_call(client, r->path, AVAHI_DBUS_INTERFACE_ADDRESS_RESOLVER, "Free"); AVAHI_LLIST_REMOVE(AvahiAddressResolver, address_resolvers, client->address_resolvers, r); diff --git a/avahi-client/srv-test.c b/avahi-client/srv-test.c index 8ac7b68..5242947 100644 --- a/avahi-client/srv-test.c +++ b/avahi-client/srv-test.c @@ -63,15 +63,13 @@ int main(AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char *argv[]) { poll_api = avahi_simple_poll_get(simple_poll); assert(poll_api); - client = avahi_client_new(poll_api, NULL, NULL, NULL); + client = avahi_client_new(poll_api, 0, NULL, NULL, NULL); assert(client); r = avahi_service_resolver_new(client, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, NULL, "_domain._udp", "0pointer.de", AVAHI_PROTO_UNSPEC, AVAHI_LOOKUP_NO_TXT, callback, simple_poll); assert(r); - for (;;) - if (avahi_simple_poll_iterate(simple_poll, -1) != 0) - break; + avahi_simple_poll_loop(simple_poll); avahi_client_free(client); avahi_simple_poll_free(simple_poll); diff --git a/avahi-compat-howl/compat.c b/avahi-compat-howl/compat.c index 12f22fa..a7e2809 100644 --- a/avahi-compat-howl/compat.c +++ b/avahi-compat-howl/compat.c @@ -380,7 +380,7 @@ sw_result sw_discovery_init(sw_discovery * self) { avahi_simple_poll_set_func((*self)->simple_poll, poll_func, *self); - if (!((*self)->client = avahi_client_new(avahi_simple_poll_get((*self)->simple_poll), client_callback, *self, &error))) { + if (!((*self)->client = avahi_client_new(avahi_simple_poll_get((*self)->simple_poll), 0, client_callback, *self, &error))) { result = map_error(error); goto fail; } @@ -716,6 +716,7 @@ static void reg_client_callback(oid_data *data, AvahiClientState state) { avahi_entry_group_reset(data->object); break; + case AVAHI_CLIENT_CONNECTING: case AVAHI_CLIENT_S_REGISTERING: /* Ignore */ break; diff --git a/avahi-compat-libdns_sd/compat.c b/avahi-compat-libdns_sd/compat.c index 432bb38..ae1cfb2 100644 --- a/avahi-compat-libdns_sd/compat.c +++ b/avahi-compat-libdns_sd/compat.c @@ -494,6 +494,7 @@ static void generic_client_callback(AvahiClient *s, AvahiClientState state, void case AVAHI_CLIENT_S_RUNNING: case AVAHI_CLIENT_S_COLLISION: case AVAHI_CLIENT_S_REGISTERING: + case AVAHI_CLIENT_CONNECTING: break; } } @@ -532,7 +533,7 @@ DNSServiceErrorType DNSSD_API DNSServiceBrowse( ASSERT_SUCCESS(pthread_mutex_lock(&sdref->mutex)); - if (!(sdref->client = avahi_client_new(avahi_simple_poll_get(sdref->simple_poll), generic_client_callback, sdref, &error))) { + if (!(sdref->client = avahi_client_new(avahi_simple_poll_get(sdref->simple_poll), 0, generic_client_callback, sdref, &error))) { ret = map_error(error); goto finish; } @@ -645,7 +646,7 @@ DNSServiceErrorType DNSSD_API DNSServiceResolve( ASSERT_SUCCESS(pthread_mutex_lock(&sdref->mutex)); - if (!(sdref->client = avahi_client_new(avahi_simple_poll_get(sdref->simple_poll), generic_client_callback, sdref, &error))) { + if (!(sdref->client = avahi_client_new(avahi_simple_poll_get(sdref->simple_poll), 0, generic_client_callback, sdref, &error))) { ret = map_error(error); goto finish; } @@ -757,7 +758,7 @@ DNSServiceErrorType DNSSD_API DNSServiceEnumerateDomains( ASSERT_SUCCESS(pthread_mutex_lock(&sdref->mutex)); - if (!(sdref->client = avahi_client_new(avahi_simple_poll_get(sdref->simple_poll), generic_client_callback, sdref, &error))) { + if (!(sdref->client = avahi_client_new(avahi_simple_poll_get(sdref->simple_poll), 0, generic_client_callback, sdref, &error))) { ret = map_error(error); goto finish; } @@ -902,6 +903,7 @@ static void reg_client_callback(AvahiClient *s, AvahiClientState state, void* us break; + case AVAHI_CLIENT_CONNECTING: case AVAHI_CLIENT_S_REGISTERING: /* Ignore */ break; @@ -1014,7 +1016,7 @@ DNSServiceErrorType DNSSD_API DNSServiceRegister ( ASSERT_SUCCESS(pthread_mutex_lock(&sdref->mutex)); - if (!(sdref->client = avahi_client_new(avahi_simple_poll_get(sdref->simple_poll), reg_client_callback, sdref, &error))) { + if (!(sdref->client = avahi_client_new(avahi_simple_poll_get(sdref->simple_poll), 0, reg_client_callback, sdref, &error))) { ret = map_error(error); goto finish; } diff --git a/avahi-utils/avahi-browse.c b/avahi-utils/avahi-browse.c index 38460f3..4d427cf 100644 --- a/avahi-utils/avahi-browse.c +++ b/avahi-utils/avahi-browse.c @@ -64,6 +64,7 @@ typedef struct Config { int ignore_local; Command command; int resolve; + int no_fail; #ifdef HAVE_GDBM int no_db_lookup; #endif @@ -88,6 +89,7 @@ static int n_all_for_now = 0, n_cache_exhausted = 0, n_resolving = 0; static AvahiStringList *browsed_types = NULL; static ServiceInfo *services = NULL; static int n_columns = 80; +static int browsing = 0; static void check_terminate(Config *c) { @@ -473,17 +475,100 @@ static void browse_domains(Config *c) { n_all_for_now++; } +static int start(Config *config) { + + assert(!browsing); + + if (config->verbose) { + const char *version, *hn; + + if (!(version = avahi_client_get_version_string(client))) { + fprintf(stderr, "Failed to query version string: %s\n", avahi_strerror(avahi_client_errno(client))); + return -1; + } + + if (!(hn = avahi_client_get_host_name_fqdn(client))) { + fprintf(stderr, "Failed to query host name: %s\n", avahi_strerror(avahi_client_errno(client))); + return -1; + } + + fprintf(stderr, "Server version: %s; Host name: %s\n", version, hn); + + if (config->command == COMMAND_BROWSE_DOMAINS) + fprintf(stderr, "E Ifce Prot Domain\n"); + else + fprintf(stderr, "E Ifce Prot %-*s %-20s Domain\n", n_columns-35, "Name", "Type"); + } + + if (config->command == COMMAND_BROWSE_SERVICES) + browse_service_type(config, config->stype, config->domain); + else if (config->command == COMMAND_BROWSE_ALL_SERVICES) + browse_all(config); + else { + assert(config->command == COMMAND_BROWSE_DOMAINS); + browse_domains(config); + } + + browsing = 1; + return 0; +} + static void client_callback(AvahiClient *c, AvahiClientState state, AVAHI_GCC_UNUSED void * userdata) { + Config *config = userdata; + + /* This function might be called when avahi_client_new() has not + * returned yet.*/ + client = c; + switch (state) { case AVAHI_CLIENT_FAILURE: - fprintf(stderr, "Client failure, exiting: %s\n", avahi_strerror(avahi_client_errno(c))); - avahi_simple_poll_quit(simple_poll); + + if (config->no_fail && avahi_client_errno(c) == AVAHI_ERR_DISCONNECTED) { + int error; + + /* We have been disconnected, so let reconnect */ + + fprintf(stderr, "Disconnected, reconnecting ...\n"); + + avahi_client_free(client); + client = NULL; + + avahi_string_list_free(browsed_types); + browsed_types = NULL; + + while (services) + remove_service(config, services); + + browsing = 0; + + if (!(client = avahi_client_new(avahi_simple_poll_get(simple_poll), AVAHI_CLIENT_NO_FAIL, client_callback, config, &error))) { + fprintf(stderr, "Failed to create client object: %s\n", avahi_strerror(error)); + avahi_simple_poll_quit(simple_poll); + } + + } else { + fprintf(stderr, "Client failure, exiting: %s\n", avahi_strerror(avahi_client_errno(c))); + avahi_simple_poll_quit(simple_poll); + } + break; case AVAHI_CLIENT_S_REGISTERING: case AVAHI_CLIENT_S_RUNNING: case AVAHI_CLIENT_S_COLLISION: - ; + + if (!browsing) + if (start(config) < 0) + avahi_simple_poll_quit(simple_poll); + + break; + + case AVAHI_CLIENT_CONNECTING: + + if (config->verbose) + fprintf(stderr, "Waiting for daemon ...\n"); + + break; } } @@ -502,6 +587,7 @@ static void help(FILE *f, const char *argv0) { " -c --cache Terminate after dumping all entries from the cache\n" " -l --ignore-local Ignore local services\n" " -r --resolve Resolve services found\n" + " -f --no-fail Don't fail if the server is not available\n" #ifdef HAVE_GDBM " -k --no-db-lookup Don't lookup service types\n" #endif @@ -522,6 +608,7 @@ static int parse_command_line(Config *c, int argc, char *argv[]) { { "cache", no_argument, NULL, 'c' }, { "ignore-local", no_argument, NULL, 'l' }, { "resolve", no_argument, NULL, 'r' }, + { "no-fail", no_argument, NULL, 'f' }, #ifdef HAVE_GDBM { "no-db-lookup", no_argument, NULL, 'k' }, #endif @@ -535,7 +622,8 @@ static int parse_command_line(Config *c, int argc, char *argv[]) { c->terminate_on_cache_exhausted = c->terminate_on_all_for_now = c->ignore_local = - c->resolve = 0; + c->resolve = + c->no_fail = 0; c->domain = c->stype = NULL; #ifdef HAVE_GDBM @@ -543,7 +631,7 @@ static int parse_command_line(Config *c, int argc, char *argv[]) { #endif opterr = 0; - while ((o = getopt_long(argc, argv, "hVd:avtclrD" + while ((o = getopt_long(argc, argv, "hVd:avtclrDf" #ifdef HAVE_GDBM "k" #endif @@ -580,6 +668,9 @@ static int parse_command_line(Config *c, int argc, char *argv[]) { case 'r': c->resolve = 1; break; + case 'f': + c->no_fail = 1; + break; #ifdef HAVE_GDBM case 'k': c->no_db_lookup = 1; @@ -654,40 +745,10 @@ int main(int argc, char *argv[]) { if (sigint_install(simple_poll) < 0) goto fail; - if (!(client = avahi_client_new(avahi_simple_poll_get(simple_poll), client_callback, NULL, &error))) { + if (!(client = avahi_client_new(avahi_simple_poll_get(simple_poll), config.no_fail ? AVAHI_CLIENT_NO_FAIL : 0, client_callback, &config, &error))) { fprintf(stderr, "Failed to create client object: %s\n", avahi_strerror(error)); goto fail; } - - if (config.verbose) { - const char *version, *hn; - - if (!(version = avahi_client_get_version_string(client))) { - fprintf(stderr, "Failed to query version string: %s\n", avahi_strerror(avahi_client_errno(client))); - goto fail; - } - - if (!(hn = avahi_client_get_host_name_fqdn(client))) { - fprintf(stderr, "Failed to query host name: %s\n", avahi_strerror(avahi_client_errno(client))); - goto fail; - } - - fprintf(stderr, "Server version: %s; Host name: %s\n", version, hn); - - if (config.command == COMMAND_BROWSE_DOMAINS) - fprintf(stderr, "E Ifce Prot Domain\n"); - else - fprintf(stderr, "E Ifce Prot %-*s %-20s Domain\n", n_columns-35, "Name", "Type"); - } - - if (config.command == COMMAND_BROWSE_SERVICES) - browse_service_type(&config, config.stype, config.domain); - else if (config.command == COMMAND_BROWSE_ALL_SERVICES) - browse_all(&config); - else { - assert(config.command == COMMAND_BROWSE_DOMAINS); - browse_domains(&config); - } avahi_simple_poll_loop(simple_poll); ret = 0; diff --git a/avahi-utils/avahi-resolve-host-name.c b/avahi-utils/avahi-resolve-host-name.c index 2a65f75..f634429 100644 --- a/avahi-utils/avahi-resolve-host-name.c +++ b/avahi-utils/avahi-resolve-host-name.c @@ -151,6 +151,7 @@ static void client_callback(AvahiClient *c, AvahiClientState state, AVAHI_GCC_UN case AVAHI_CLIENT_S_REGISTERING: case AVAHI_CLIENT_S_RUNNING: case AVAHI_CLIENT_S_COLLISION: + case AVAHI_CLIENT_CONNECTING: ; } } @@ -266,7 +267,7 @@ int main(int argc, char *argv[]) { if (sigint_install(simple_poll) < 0) goto fail; - if (!(client = avahi_client_new(avahi_simple_poll_get(simple_poll), client_callback, NULL, &error))) { + if (!(client = avahi_client_new(avahi_simple_poll_get(simple_poll), 0, client_callback, NULL, &error))) { fprintf(stderr, "Failed to create client object: %s\n", avahi_strerror(error)); goto fail; } diff --git a/examples/client-browse-services.c b/examples/client-browse-services.c index 00703e6..37550ef 100644 --- a/examples/client-browse-services.c +++ b/examples/client-browse-services.c @@ -164,7 +164,7 @@ int main(AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char*argv[]) { } /* Allocate a new client */ - client = avahi_client_new(avahi_simple_poll_get(simple_poll), client_callback, NULL, &error); + client = avahi_client_new(avahi_simple_poll_get(simple_poll), 0, client_callback, NULL, &error); /* Check wether creating the client object succeeded */ if (!client) { diff --git a/examples/client-publish-service.c b/examples/client-publish-service.c index 50a7002..a9f9f8c 100644 --- a/examples/client-publish-service.c +++ b/examples/client-publish-service.c @@ -157,6 +157,7 @@ static void client_callback(AvahiClient *c, AvahiClientState state, AVAHI_GCC_UN break; + case AVAHI_CLIENT_CONNECTING: case AVAHI_CLIENT_S_REGISTERING: ; } @@ -176,7 +177,7 @@ int main(AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char*argv[]) { name = avahi_strdup("MegaPrinter"); /* Allocate a new client */ - client = avahi_client_new(avahi_simple_poll_get(simple_poll), client_callback, NULL, &error); + client = avahi_client_new(avahi_simple_poll_get(simple_poll), 0, client_callback, NULL, &error); /* Check wether creating the client object succeeded */ if (!client) { diff --git a/examples/glib-integration.c b/examples/glib-integration.c index f1830d8..ab53687 100644 --- a/examples/glib-integration.c +++ b/examples/glib-integration.c @@ -108,6 +108,7 @@ main (AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char *argv[]) /* Create a new AvahiClient instance */ client = avahi_client_new (poll_api, /* AvahiPoll object from above */ + 0, avahi_client_callback, /* Callback function for Client state changes */ loop, /* User data */ &error); /* Error return */ -- 2.39.5