From d0c6f66e03793c19c034c26c1267f9382167b3ad Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Sat, 20 Aug 2005 17:44:43 +0000 Subject: [PATCH] * complete error handling in avahi-client git-svn-id: file:///home/lennart/svn/public/avahi/trunk@362 941a03a8-eaeb-0310-b9a0-b1bbd8fe43fe --- avahi-client/browser.c | 647 +++++++++++++++++++++++-------------- avahi-client/client-test.c | 31 +- avahi-client/client.c | 36 ++- avahi-client/client.h | 6 +- avahi-client/entrygroup.c | 24 +- docs/TODO | 3 + 6 files changed, 478 insertions(+), 269 deletions(-) diff --git a/avahi-client/browser.c b/avahi-client/browser.c index dfc70f1..5de4824 100644 --- a/avahi-client/browser.c +++ b/avahi-client/browser.c @@ -38,380 +38,559 @@ #include "client.h" #include "internal.h" -/* AvahiDomainBrowser */ +static int simple_method_call(AvahiClient *client, const char *path, const char *interface, const char *method) { + DBusMessage *message = NULL, *reply = NULL; + DBusError error; + int r = AVAHI_OK; + + dbus_error_init(&error); + + assert(client); + assert(path); + assert(interface); + assert(method); + + if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, path, interface, 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); + goto fail; + } + + dbus_message_unref(message); + dbus_message_unref(reply); + + return AVAHI_OK; + +fail: + if (dbus_error_is_set(&error)) { + r = avahi_client_set_dbus_error(client, &error); + dbus_error_free(&error); + } + + if (message) + dbus_message_unref(message); + + if (reply) + dbus_message_unref(reply); + + return r; +} -AvahiDomainBrowser* avahi_domain_browser_new (AvahiClient *client, AvahiIfIndex interface, AvahiProtocol protocol, const char *domain, AvahiDomainBrowserType btype, AvahiDomainBrowserCallback callback, void *userdata) -{ - AvahiDomainBrowser *tmp = NULL; +AvahiDomainBrowser* avahi_domain_browser_new( + AvahiClient *client, + AvahiIfIndex interface, + AvahiProtocol protocol, + const char *domain, + AvahiDomainBrowserType btype, + AvahiDomainBrowserCallback callback, + void *userdata) { + + AvahiDomainBrowser *db = NULL; DBusMessage *message = NULL, *reply; DBusError error; char *path; + int32_t i_interface, i_protocol, bt; - if (client == NULL) - return NULL; + assert(client); + assert(callback); dbus_error_init (&error); - message = dbus_message_new_method_call (AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, - AVAHI_DBUS_INTERFACE_SERVER, "DomainBrowserNew"); + if (client->state == AVAHI_CLIENT_DISCONNECTED) { + avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE); + goto fail; + } - if (!dbus_message_append_args (message, DBUS_TYPE_INT32, &interface, DBUS_TYPE_INT32, &protocol, DBUS_TYPE_STRING, &domain, DBUS_TYPE_INT32, &btype, DBUS_TYPE_INVALID)) - goto dbus_error; + if (!domain) + domain = ""; - reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error); + if (!(db = avahi_new (AvahiDomainBrowser, 1))) { + avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY); + goto fail; + } - if (dbus_error_is_set (&error) || reply == NULL) - goto dbus_error; + db->client = client; + db->callback = callback; + db->userdata = userdata; + db->path = NULL; - if (!dbus_message_get_args (reply, &error, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID)) - goto dbus_error; + AVAHI_LLIST_PREPEND(AvahiDomainBrowser, domain_browsers, client->domain_browsers, db); - if (dbus_error_is_set (&error) || path == NULL) - goto dbus_error; + if (!(message = dbus_message_new_method_call (AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "DomainBrowserNew"))) { + avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY); + goto fail; + } - tmp = avahi_new (AvahiDomainBrowser, 1); - tmp->client = client; - tmp->callback = callback; - tmp->userdata = userdata; - tmp->path = strdup (path); + i_interface = interface; + i_protocol = protocol; + bt = btype; + + if (!(dbus_message_append_args( + message, + DBUS_TYPE_INT32, &i_interface, + DBUS_TYPE_INT32, &i_protocol, + DBUS_TYPE_STRING, &domain, + DBUS_TYPE_INT32, &bt, + DBUS_TYPE_INVALID))) { + avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY); + goto fail; + } - AVAHI_LLIST_PREPEND(AvahiDomainBrowser, domain_browsers, client->domain_browsers, tmp); + if (!(reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error)) || + dbus_error_is_set(&error)) { + avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR); + goto fail; + } - return tmp; + if (!dbus_message_get_args (reply, &error, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID) || + dbus_error_is_set(&error) || + !path) { + avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR); + goto fail; + } -dbus_error: - dbus_error_free (&error); - avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR); + if (!(db->path = avahi_strdup(path))) { - return NULL; -} + /* FIXME: We don't remove the object on the server side */ -int -avahi_domain_browser_free (AvahiDomainBrowser *b) -{ - AvahiClient *client = b->client; - DBusMessage *message = NULL; + avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY); + goto fail; + } - if (b == NULL || b->path == NULL) - return avahi_client_set_errno (client, AVAHI_ERR_INVALID_OBJECT); + dbus_message_unref(message); + dbus_message_unref(reply); + + return db; - message = dbus_message_new_method_call (AVAHI_DBUS_NAME, - b->path, - AVAHI_DBUS_INTERFACE_DOMAIN_BROWSER, "Free"); +fail: - if (message == NULL) - return avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR); + if (dbus_error_is_set(&error)) { + avahi_client_set_dbus_error(client, &error); + dbus_error_free(&error); + } - dbus_connection_send (client->bus, message, NULL); + if (db) + avahi_domain_browser_free(db); + + if (message) + dbus_message_unref(message); + + if (reply) + dbus_message_unref(reply); + + return NULL; +} + +int avahi_domain_browser_free (AvahiDomainBrowser *b) { + AvahiClient *client; + int r = AVAHI_OK; + + assert(b); + client = b->client; + + if (b->path && client->state != AVAHI_CLIENT_DISCONNECTED) + r = simple_method_call(client, b->path, AVAHI_DBUS_INTERFACE_DOMAIN_BROWSER, "Free"); AVAHI_LLIST_REMOVE(AvahiDomainBrowser, domain_browsers, client->domain_browsers, b); - avahi_free (b); + avahi_free(b->path); + avahi_free(b); - return avahi_client_set_errno (client, AVAHI_OK); + return r; } -const char* -avahi_domain_browser_path (AvahiDomainBrowser *b) -{ +const char* avahi_domain_browser_get_dbus_path(AvahiDomainBrowser *b) { + assert(b); + return b->path; } -DBusHandlerResult -avahi_domain_browser_event (AvahiClient *client, AvahiBrowserEvent event, DBusMessage *message) -{ - AvahiDomainBrowser *n, *db = NULL; +DBusHandlerResult avahi_domain_browser_event (AvahiClient *client, AvahiBrowserEvent event, DBusMessage *message) { + AvahiDomainBrowser *db = NULL; DBusError error; const char *path; char *domain; - int interface, protocol; + int32_t interface, protocol; + assert(client); + assert(message); + dbus_error_init (&error); - path = dbus_message_get_path (message); - - if (path == NULL) - goto out; + if (!(path = dbus_message_get_path(message))) + goto fail; - for (n = client->domain_browsers; n != NULL; n = n->domain_browsers_next) - { - if (strcmp (n->path, path) == 0) { - db = n; + for (db = client->domain_browsers; db; db = db->domain_browsers_next) + if (strcmp (db->path, path) == 0) break; - } - } - - if (db == NULL) - goto out; - dbus_message_get_args (message, &error, DBUS_TYPE_INT32, &interface, - DBUS_TYPE_INT32, &protocol, DBUS_TYPE_STRING, &domain, DBUS_TYPE_INVALID); - - if (dbus_error_is_set (&error)) - goto out; + if (!db) + goto fail; + + if (!dbus_message_get_args( + message, &error, + DBUS_TYPE_INT32, &interface, + DBUS_TYPE_INT32, &protocol, + DBUS_TYPE_STRING, &domain, + DBUS_TYPE_INVALID) || + dbus_error_is_set (&error)) { + fprintf(stderr, "Failed to parse browser event.\n"); + goto fail; + } - db->callback (db, interface, protocol, event, domain, db->userdata); + db->callback(db, (AvahiIfIndex) interface, (AvahiProtocol) protocol, event, domain, db->userdata); return DBUS_HANDLER_RESULT_HANDLED; -out: +fail: dbus_error_free (&error); return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; } /* AvahiServiceTypeBrowser */ -AvahiServiceTypeBrowser* avahi_service_type_browser_new (AvahiClient *client, AvahiIfIndex interface, AvahiProtocol protocol, const char *domain, AvahiServiceTypeBrowserCallback callback, void *userdata) -{ - AvahiServiceTypeBrowser *tmp = NULL; +AvahiServiceTypeBrowser* avahi_service_type_browser_new( + AvahiClient *client, + AvahiIfIndex interface, + AvahiProtocol protocol, + const char *domain, + AvahiServiceTypeBrowserCallback callback, + void *userdata) { + + AvahiServiceTypeBrowser *b = NULL; DBusMessage *message = NULL, *reply; DBusError error; char *path; + int32_t i_interface, i_protocol; - if (client == NULL) - return NULL; + assert(client); + assert(callback); - dbus_error_init (&error); + dbus_error_init(&error); - message = dbus_message_new_method_call (AVAHI_DBUS_NAME, - AVAHI_DBUS_PATH_SERVER, - AVAHI_DBUS_INTERFACE_SERVER, - "ServiceTypeBrowserNew"); + if (client->state == AVAHI_CLIENT_DISCONNECTED) { + avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE); + goto fail; + } - if (!dbus_message_append_args (message, DBUS_TYPE_INT32, &interface, DBUS_TYPE_INT32, &protocol, DBUS_TYPE_STRING, &domain, DBUS_TYPE_INVALID)) - goto dbus_error; + if (!domain) + domain = ""; - reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error); + if (!(b = avahi_new(AvahiServiceTypeBrowser, 1))) { + avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY); + goto fail; + } - if (dbus_error_is_set (&error) || reply == NULL) - goto dbus_error; + b->client = client; + b->callback = callback; + b->userdata = userdata; + b->path = NULL; - if (!dbus_message_get_args (reply, &error, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID)) - goto dbus_error; + AVAHI_LLIST_PREPEND(AvahiServiceTypeBrowser, service_type_browsers, client->service_type_browsers, b); - if (dbus_error_is_set (&error) || path == NULL) - goto dbus_error; + if (!(message = dbus_message_new_method_call (AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "ServiceTypeBrowserNew"))) { + avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY); + goto fail; + } + + i_interface = interface; + i_protocol = protocol; - tmp = avahi_new(AvahiServiceTypeBrowser, 1); - tmp->client = client; - tmp->callback = callback; - tmp->userdata = userdata; - tmp->path = strdup (path); + if (!dbus_message_append_args( + message, + DBUS_TYPE_INT32, &interface, + DBUS_TYPE_INT32, &protocol, + DBUS_TYPE_STRING, &domain, + DBUS_TYPE_INVALID)) { + avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY); + goto fail; + } - AVAHI_LLIST_PREPEND(AvahiServiceTypeBrowser, service_type_browsers, client->service_type_browsers, tmp); + if (!(reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error)) || + dbus_error_is_set(&error)) { + avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR); + goto fail; + } - return tmp; + if (!dbus_message_get_args (reply, &error, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID) || + dbus_error_is_set(&error) || + !path) { + avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR); + goto fail; + } -dbus_error: - dbus_error_free (&error); - avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR); + if (!(b->path = avahi_strdup(path))) { - return NULL; -} + /* FIXME: We don't remove the object on the server side */ -int -avahi_service_type_browser_free (AvahiServiceTypeBrowser *b) -{ - AvahiClient *client = b->client; - DBusMessage *message = NULL; + avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY); + goto fail; + } - if (b == NULL || b->path == NULL) - return avahi_client_set_errno (client, AVAHI_ERR_INVALID_OBJECT); + dbus_message_unref(message); + dbus_message_unref(reply); - message = dbus_message_new_method_call (AVAHI_DBUS_NAME, - b->path, - AVAHI_DBUS_INTERFACE_SERVICE_TYPE_BROWSER, "Free"); + return b; - if (message == NULL) - return avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR); +fail: + + if (dbus_error_is_set(&error)) { + avahi_client_set_dbus_error(client, &error); + dbus_error_free(&error); + } - dbus_connection_send (b->client->bus, message, NULL); + if (b) + avahi_service_type_browser_free(b); + + if (message) + dbus_message_unref(message); - AVAHI_LLIST_REMOVE(AvahiServiceTypeBrowser, service_type_browsers, b->client->service_type_browsers, b); + if (reply) + dbus_message_unref(reply); + + return NULL; +} + +int avahi_service_type_browser_free (AvahiServiceTypeBrowser *b) { + AvahiClient *client; + int r = AVAHI_OK; - avahi_free (b); + assert(b); + client = b->client; - return avahi_client_set_errno (client, AVAHI_OK); + if (b->path && client->state != AVAHI_CLIENT_DISCONNECTED) + r = 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); + + avahi_free(b->path); + avahi_free(b); + return r; } -const char* -avahi_service_type_browser_path (AvahiServiceTypeBrowser *b) -{ +const char* avahi_service_type_browser_get_dbus_path(AvahiServiceTypeBrowser *b) { + assert(b); + return b->path; } -DBusHandlerResult -avahi_service_type_browser_event (AvahiClient *client, AvahiBrowserEvent event, DBusMessage *message) -{ - AvahiServiceTypeBrowser *n, *db = NULL; +DBusHandlerResult avahi_service_type_browser_event (AvahiClient *client, AvahiBrowserEvent event, DBusMessage *message) { + AvahiServiceTypeBrowser *b = NULL; DBusError error; const char *path; char *domain, *type; - int interface, protocol; + int32_t interface, protocol; + assert(client); + assert(message); + dbus_error_init (&error); - path = dbus_message_get_path (message); - - if (path == NULL) - goto out; + if (!(path = dbus_message_get_path(message))) + goto fail; - for (n = client->service_type_browsers; n != NULL; n = n->service_type_browsers_next) - { - if (strcmp (n->path, path) == 0) { - db = n; + for (b = client->service_type_browsers; b; b = b->service_type_browsers_next) + if (strcmp (b->path, path) == 0) break; - } - } - if (db == NULL) - goto out; - - dbus_message_get_args (message, &error, - DBUS_TYPE_INT32, &interface, - DBUS_TYPE_INT32, &protocol, - DBUS_TYPE_STRING, &type, - DBUS_TYPE_STRING, &domain, - DBUS_TYPE_INVALID); - - if (dbus_error_is_set (&error)) - goto out; + if (!b) + goto fail; + + if (!dbus_message_get_args( + message, &error, + DBUS_TYPE_INT32, &interface, + DBUS_TYPE_INT32, &protocol, + DBUS_TYPE_STRING, &type, + DBUS_TYPE_STRING, &domain, + DBUS_TYPE_INVALID) || + dbus_error_is_set(&error)) { + fprintf(stderr, "Failed to parse browser event.\n"); + goto fail; + } - db->callback (db, interface, protocol, event, type, domain, db->userdata); + b->callback(b, (AvahiIfIndex) interface, (AvahiProtocol) protocol, event, type, domain, b->userdata); return DBUS_HANDLER_RESULT_HANDLED; -out: +fail: dbus_error_free (&error); return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; } /* AvahiServiceBrowser */ -AvahiServiceBrowser* avahi_service_browser_new (AvahiClient *client, AvahiIfIndex interface, AvahiProtocol protocol, const char *type, const char *domain, AvahiServiceBrowserCallback callback, void *userdata) -{ - AvahiServiceBrowser *tmp = NULL; +AvahiServiceBrowser* avahi_service_browser_new( + AvahiClient *client, + AvahiIfIndex interface, + AvahiProtocol protocol, + const char *type, + const char *domain, + AvahiServiceBrowserCallback callback, + void *userdata) { + + AvahiServiceBrowser *b = NULL; DBusMessage *message = NULL, *reply; DBusError error; char *path; + int32_t i_protocol, i_interface; - if (client == NULL) - return NULL; + assert(client); + assert(type); + assert(callback); - dbus_error_init (&error); + dbus_error_init(&error); + + if (client->state == AVAHI_CLIENT_DISCONNECTED) { + avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE); + goto fail; + } + + if (!domain) + domain = ""; + + if (!(b = avahi_new(AvahiServiceBrowser, 1))) { + avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY); + goto fail; + } + + b->client = client; + b->callback = callback; + b->userdata = userdata; + b->path = NULL; - message = dbus_message_new_method_call (AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, - AVAHI_DBUS_INTERFACE_SERVER, "ServiceBrowserNew"); + AVAHI_LLIST_PREPEND(AvahiServiceBrowser, service_browsers, client->service_browsers, b); - if (!dbus_message_append_args (message, - DBUS_TYPE_INT32, &interface, - DBUS_TYPE_INT32, &protocol, - DBUS_TYPE_STRING, &type, - DBUS_TYPE_STRING, &domain, - DBUS_TYPE_INVALID)) - goto dbus_error; + if (!(message = dbus_message_new_method_call (AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "ServiceBrowserNew"))) { + avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY); + goto fail; + } - reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error); + i_interface = interface; + i_protocol = protocol; + - if (dbus_error_is_set (&error) || reply == NULL) - goto dbus_error; + if (!dbus_message_append_args( + message, + DBUS_TYPE_INT32, &i_interface, + DBUS_TYPE_INT32, &i_protocol, + DBUS_TYPE_STRING, &type, + DBUS_TYPE_STRING, &domain, + DBUS_TYPE_INVALID)) { + avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY); + goto fail; + } - if (!dbus_message_get_args (reply, &error, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID)) - goto dbus_error; + if (!(reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error)) || + dbus_error_is_set(&error)) { + avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR); + goto fail; + } - if (dbus_error_is_set (&error) || path == NULL) - goto dbus_error; + if (!dbus_message_get_args (reply, &error, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID) || + dbus_error_is_set(&error) || + !path) { + avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR); + goto fail; + } - tmp = avahi_new(AvahiServiceBrowser, 1); - tmp->client = client; - tmp->callback = callback; - tmp->userdata = userdata; - tmp->path = strdup (path); + if (!(b->path = avahi_strdup(path))) { - AVAHI_LLIST_PREPEND(AvahiServiceBrowser, service_browsers, client->service_browsers, tmp); + /* FIXME: We don't remove the object on the server side */ - return tmp; + avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY); + goto fail; + } -dbus_error: - dbus_error_free (&error); - avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR); + dbus_message_unref(message); + dbus_message_unref(reply); + + return b; + +fail: + if (dbus_error_is_set(&error)) { + avahi_client_set_dbus_error(client, &error); + dbus_error_free(&error); + } + + if (b) + avahi_service_browser_free(b); + + if (message) + dbus_message_unref(message); + + if (reply) + dbus_message_unref(reply); return NULL; } -int -avahi_service_browser_free (AvahiServiceBrowser *b) -{ - AvahiClient *client = b->client; - DBusMessage *message = NULL; - - if (b == NULL || b->path == NULL) - return avahi_client_set_errno (client, AVAHI_ERR_INVALID_OBJECT); +int avahi_service_browser_free (AvahiServiceBrowser *b) { + AvahiClient *client; + int r = AVAHI_OK; - message = dbus_message_new_method_call (AVAHI_DBUS_NAME, - b->path, - AVAHI_DBUS_INTERFACE_SERVICE_BROWSER, "Free"); + assert(b); + client = b->client; - if (message == NULL) - return avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR); - - dbus_connection_send (b->client->bus, message, NULL); + if (b->path && client->state != AVAHI_CLIENT_DISCONNECTED) + r = simple_method_call(client, b->path, AVAHI_DBUS_INTERFACE_SERVICE_BROWSER, "Free"); AVAHI_LLIST_REMOVE(AvahiServiceBrowser, service_browsers, b->client->service_browsers, b); - avahi_free (b); - - return avahi_client_set_errno (client, AVAHI_OK); + avahi_free(b->path); + avahi_free(b); + return r; } -const char* -avahi_service_browser_path (AvahiServiceBrowser *b) -{ +const char* avahi_service_browser_get_dbus_path(AvahiServiceBrowser *b) { + assert(b); + return b->path; } -DBusHandlerResult -avahi_service_browser_event (AvahiClient *client, AvahiBrowserEvent event, DBusMessage *message) -{ - AvahiServiceBrowser *n, *db = NULL; +DBusHandlerResult avahi_service_browser_event (AvahiClient *client, AvahiBrowserEvent event, DBusMessage *message) { + AvahiServiceBrowser *b = NULL; DBusError error; const char *path; char *name, *type, *domain; - int interface, protocol; + int32_t interface, protocol; dbus_error_init (&error); - path = dbus_message_get_path (message); - - if (path == NULL) - goto out; + if (!(path = dbus_message_get_path(message))) + goto fail; - for (n = client->service_browsers; n != NULL; n = n->service_browsers_next) - { - if (strcmp (n->path, path) == 0) { - db = n; + for (b = client->service_browsers; b; b = b->service_browsers_next) + if (strcmp (b->path, path) == 0) break; - } - } - if (db == NULL) - goto out; - - dbus_message_get_args (message, &error, - DBUS_TYPE_INT32, &interface, - DBUS_TYPE_INT32, &protocol, - DBUS_TYPE_STRING, &name, - DBUS_TYPE_STRING, &type, - DBUS_TYPE_STRING, &domain, - DBUS_TYPE_INVALID); - - if (dbus_error_is_set (&error)) - goto out; + if (!dbus_message_get_args ( + message, &error, + DBUS_TYPE_INT32, &interface, + DBUS_TYPE_INT32, &protocol, + DBUS_TYPE_STRING, &name, + DBUS_TYPE_STRING, &type, + DBUS_TYPE_STRING, &domain, + DBUS_TYPE_INVALID) || + dbus_error_is_set(&error)) { + fprintf(stderr, "Failed to parse browser event.\n"); + goto fail; + } - db->callback (db, interface, protocol, event, name, type, domain, db->userdata); + b->callback(b, (AvahiIfIndex) interface, (AvahiProtocol) protocol, event, name, type, domain, b->userdata); return DBUS_HANDLER_RESULT_HANDLED; -out: +fail: dbus_error_free (&error); return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; } diff --git a/avahi-client/client-test.c b/avahi-client/client-test.c index 29dd62a..a91e6af 100644 --- a/avahi-client/client-test.c +++ b/avahi-client/client-test.c @@ -31,6 +31,7 @@ #include static const AvahiPoll *poll_api = NULL; +static AvahiSimplePoll *simple_poll = NULL; static void @@ -48,19 +49,19 @@ avahi_entry_group_callback (AvahiEntryGroup *g, AvahiEntryGroupState state, void static void avahi_domain_browser_callback (AvahiDomainBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const char *domain, void *user_data) { - printf ("XXX: Callback on %s, interface (%d), protocol (%d), event (%d), domain (%s), data (%s)\n", avahi_domain_browser_path (b), interface, protocol, event, domain, (char*)user_data); + printf ("XXX: Callback on %s, interface (%d), protocol (%d), event (%d), domain (%s), data (%s)\n", avahi_domain_browser_get_dbus_path (b), interface, protocol, event, domain, (char*)user_data); } static void avahi_service_browser_callback (AvahiServiceBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const char *name, const char *type, const char *domain, void *user_data) { - printf ("XXX: Callback on %s, interface (%d), protocol (%d), event (%d), name (%s), type (%s), domain (%s), data (%s)\n", avahi_service_browser_path (b), interface, protocol, event, name, type, domain, (char*)user_data); + printf ("XXX: Callback on %s, interface (%d), protocol (%d), event (%d), name (%s), type (%s), domain (%s), data (%s)\n", avahi_service_browser_get_dbus_path (b), interface, protocol, event, name, type, domain, (char*)user_data); } static void avahi_service_type_browser_callback (AvahiServiceTypeBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const char *type, const char *domain, void *user_data) { - printf ("XXX: Callback on %s, interface (%d), protocol (%d), event (%d), type (%s), domain (%s), data (%s)\n", avahi_service_type_browser_path (b), interface, protocol, event, type, domain, (char*)user_data); + printf ("XXX: Callback on %s, interface (%d), protocol (%d), event (%d), type (%s), domain (%s), data (%s)\n", avahi_service_type_browser_get_dbus_path (b), interface, protocol, event, type, domain, (char*)user_data); } static void test_free_domain_browser(AvahiTimeout *timeout, void* userdata) @@ -77,13 +78,17 @@ static void test_free_entry_group (AvahiTimeout *timeout, void* userdata) avahi_entry_group_free (g); } +static void terminate(AvahiTimeout *timeout, void *userdata) { + + avahi_simple_poll_quit(simple_poll); +} + int main (int argc, char *argv[]) { AvahiClient *avahi; AvahiEntryGroup *group; AvahiDomainBrowser *domain; AvahiServiceBrowser *sb; AvahiServiceTypeBrowser *st; - AvahiSimplePoll *simple_poll; const char *ret; int error; struct timeval tv; @@ -121,23 +126,24 @@ int main (int argc, char *argv[]) { avahi_entry_group_commit (group); - domain = avahi_domain_browser_new (avahi, AVAHI_IF_UNSPEC, AF_UNSPEC, "", AVAHI_DOMAIN_BROWSER_BROWSE, avahi_domain_browser_callback, "omghai3u"); + domain = avahi_domain_browser_new (avahi, AVAHI_IF_UNSPEC, AF_UNSPEC, NULL, AVAHI_DOMAIN_BROWSER_BROWSE, avahi_domain_browser_callback, "omghai3u"); + if (domain == NULL) printf ("Failed to create domain browser object\n"); else - printf ("Sucessfully created domain browser, path %s\n", avahi_domain_browser_path (domain)); + printf ("Sucessfully created domain browser, path %s\n", avahi_domain_browser_get_dbus_path (domain)); st = avahi_service_type_browser_new (avahi, AVAHI_IF_UNSPEC, AF_UNSPEC, "", avahi_service_type_browser_callback, "omghai3u"); if (st == NULL) printf ("Failed to create service type browser object\n"); else - printf ("Sucessfully created service type browser, path %s\n", avahi_service_type_browser_path (st)); + printf ("Sucessfully created service type browser, path %s\n", avahi_service_type_browser_get_dbus_path (st)); sb = avahi_service_browser_new (avahi, AVAHI_IF_UNSPEC, AF_UNSPEC, "_http._tcp", "", avahi_service_browser_callback, "omghai3u"); if (sb == NULL) printf ("Failed to create service browser object\n"); else - printf ("Sucessfully created service browser, path %s\n", avahi_service_browser_path (sb)); + printf ("Sucessfully created service browser, path %s\n", avahi_service_browser_get_dbus_path (sb)); avahi_elapse_time(&tv, 5000, 0); @@ -145,17 +151,20 @@ int main (int argc, char *argv[]) { avahi_elapse_time(&tv, 8000, 0); poll_api->timeout_new(poll_api, &tv, test_free_domain_browser, sb); + avahi_elapse_time(&tv, 20000, 0); + poll_api->timeout_new(poll_api, &tv, terminate, NULL); + for (;;) if (avahi_simple_poll_iterate(simple_poll, -1) != 0) break; fail: - if (simple_poll) - avahi_simple_poll_free(simple_poll); - if (avahi) avahi_client_free (avahi); + if (simple_poll) + avahi_simple_poll_free(simple_poll); + return 0; } diff --git a/avahi-client/client.c b/avahi-client/client.c index 242afff..2361cbc 100644 --- a/avahi-client/client.c +++ b/avahi-client/client.c @@ -170,19 +170,20 @@ static DBusHandlerResult filter_func(DBusConnection *bus, DBusMessage *message, avahi_entry_group_set_state(g, state); } - } else if (dbus_message_is_signal (message, AVAHI_DBUS_INTERFACE_DOMAIN_BROWSER, "ItemNew")) { - return avahi_domain_browser_event (client, AVAHI_BROWSER_NEW, message); - } else if (dbus_message_is_signal (message, AVAHI_DBUS_INTERFACE_DOMAIN_BROWSER, "ItemRemove")) { - return avahi_domain_browser_event (client, AVAHI_BROWSER_REMOVE, message); - } else if (dbus_message_is_signal (message, AVAHI_DBUS_INTERFACE_SERVICE_TYPE_BROWSER, "ItemNew")) { + } else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_DOMAIN_BROWSER, "ItemNew")) + return avahi_domain_browser_event(client, AVAHI_BROWSER_NEW, message); + else if (dbus_message_is_signal (message, AVAHI_DBUS_INTERFACE_DOMAIN_BROWSER, "ItemRemove")) + return avahi_domain_browser_event(client, AVAHI_BROWSER_REMOVE, message); + + else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_SERVICE_TYPE_BROWSER, "ItemNew")) return avahi_service_type_browser_event (client, AVAHI_BROWSER_NEW, message); - } else if (dbus_message_is_signal (message, AVAHI_DBUS_INTERFACE_SERVICE_TYPE_BROWSER, "ItemRemove")) { + else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_SERVICE_TYPE_BROWSER, "ItemRemove")) return avahi_service_type_browser_event (client, AVAHI_BROWSER_REMOVE, message); - } else if (dbus_message_is_signal (message, AVAHI_DBUS_INTERFACE_SERVICE_BROWSER, "ItemNew")) { + + else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_SERVICE_BROWSER, "ItemNew")) return avahi_service_browser_event (client, AVAHI_BROWSER_NEW, message); - } else if (dbus_message_is_signal (message, AVAHI_DBUS_INTERFACE_SERVICE_BROWSER, "ItemRemove")) { + else if (dbus_message_is_signal(message, AVAHI_DBUS_INTERFACE_SERVICE_BROWSER, "ItemRemove")) return avahi_service_browser_event (client, AVAHI_BROWSER_REMOVE, message); - } return DBUS_HANDLER_RESULT_HANDLED; @@ -259,8 +260,8 @@ AvahiClient *avahi_client_new(const AvahiPoll *poll_api, AvahiClientCallback cal 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 (!client->bus || dbus_error_is_set (&error)) + if (!(client->bus = dbus_bus_get(DBUS_BUS_SYSTEM, &error)) || + dbus_error_is_set (&error)) goto fail; if (avahi_dbus_connection_glue(client->bus, poll_api) < 0) { @@ -306,7 +307,8 @@ AvahiClient *avahi_client_new(const AvahiPoll *poll_api, AvahiClientCallback cal if (dbus_error_is_set (&error)) goto fail; - if (!(dbus_bus_name_has_owner(client->bus, AVAHI_DBUS_NAME, &error))) { + if (!(dbus_bus_name_has_owner(client->bus, AVAHI_DBUS_NAME, &error)) || + dbus_error_is_set(&error)) { if (ret_error) *ret_error = AVAHI_ERR_NO_DAEMON; @@ -338,11 +340,6 @@ fail: void avahi_client_free(AvahiClient *client) { assert(client); - if (client->bus) { - dbus_connection_disconnect(client->bus); - dbus_connection_unref(client->bus); - } - while (client->groups) avahi_entry_group_free(client->groups); @@ -355,6 +352,11 @@ void avahi_client_free(AvahiClient *client) { while (client->service_type_browsers) avahi_service_type_browser_free(client->service_type_browsers); + if (client->bus) { + dbus_connection_disconnect(client->bus); + dbus_connection_unref(client->bus); + } + avahi_free(client->version_string); avahi_free(client->host_name); avahi_free(client->host_name_fqdn); diff --git a/avahi-client/client.h b/avahi-client/client.h index fee6d91..056adfb 100644 --- a/avahi-client/client.h +++ b/avahi-client/client.h @@ -161,7 +161,7 @@ AvahiDomainBrowser* avahi_domain_browser_new (AvahiClient *client, void *userdata); /** Get the D-Bus path of an AvahiDomainBrowser object, for debugging purposes only. */ -const char* avahi_domain_browser_path (AvahiDomainBrowser *); +const char* avahi_domain_browser_get_dbus_path (AvahiDomainBrowser *); /** Cleans up and frees an AvahiDomainBrowser object */ int avahi_domain_browser_free (AvahiDomainBrowser *); @@ -176,7 +176,7 @@ AvahiServiceTypeBrowser* avahi_service_type_browser_new ( 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* avahi_service_type_browser_get_dbus_path(AvahiServiceTypeBrowser *); /** Cleans up and frees an AvahiServiceTypeBrowser object */ int avahi_service_type_browser_free (AvahiServiceTypeBrowser *); @@ -192,7 +192,7 @@ AvahiServiceBrowser* avahi_service_browser_new ( void *userdata); /** Get the D-Bus path of an AvahiServiceBrowser object, for debugging purposes only. */ -const char* avahi_service_browser_path (AvahiServiceBrowser *); +const char* avahi_service_browser_get_dbus_path (AvahiServiceBrowser *); /* Cleans up and frees an AvahiServiceBrowser object */ int avahi_service_browser_free (AvahiServiceBrowser *); diff --git a/avahi-client/entrygroup.c b/avahi-client/entrygroup.c index c22d8a6..646cf6f 100644 --- a/avahi-client/entrygroup.c +++ b/avahi-client/entrygroup.c @@ -125,6 +125,7 @@ AvahiEntryGroup* avahi_entry_group_new (AvahiClient *client, AvahiEntryGroupCall group->callback = callback; group->userdata = userdata; group->state = AVAHI_ENTRY_GROUP_UNCOMMITED; + group->path = NULL; AVAHI_LLIST_PREPEND(AvahiEntryGroup, groups, client->groups, group); if (!(message = dbus_message_new_method_call( @@ -156,7 +157,6 @@ AvahiEntryGroup* avahi_entry_group_new (AvahiClient *client, AvahiEntryGroupCall goto fail; } - if (retrieve_state(group) < 0) goto fail; @@ -184,7 +184,7 @@ fail: } static int entry_group_simple_method_call(AvahiEntryGroup *group, const char *method) { - DBusMessage *message, *reply; + DBusMessage *message = NULL, *reply = NULL; DBusError error; int r = AVAHI_OK; AvahiClient *client; @@ -288,6 +288,9 @@ int avahi_entry_group_is_empty (AvahiEntryGroup *group) { assert(group); client = group->client; + if (!group->path || group->client->state == AVAHI_CLIENT_DISCONNECTED) + 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"))) { @@ -344,6 +347,7 @@ int avahi_entry_group_add_service_strlst( int reverse = 0, r = AVAHI_OK; DBusError error; AvahiClient *client; + int32_t i_interface, i_protocol; assert(group); assert(name); @@ -351,6 +355,15 @@ int avahi_entry_group_add_service_strlst( client = group->client; + if (!group->path || group->client->state == AVAHI_CLIENT_DISCONNECTED) + return avahi_client_set_errno(group->client, AVAHI_ERR_BAD_STATE); + + if (!domain) + 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"))) { @@ -358,10 +371,13 @@ int avahi_entry_group_add_service_strlst( goto fail; } + i_interface = (int32_t) interface; + i_protocol = (int32_t) protocol; + if (!dbus_message_append_args( message, - DBUS_TYPE_INT32, &interface, - DBUS_TYPE_INT32, &protocol, + DBUS_TYPE_INT32, &i_interface, + DBUS_TYPE_INT32, &i_protocol, DBUS_TYPE_STRING, &name, DBUS_TYPE_STRING, &type, DBUS_TYPE_STRING, &domain, diff --git a/docs/TODO b/docs/TODO index f04e4ff..88aa257 100644 --- a/docs/TODO +++ b/docs/TODO @@ -1,4 +1,7 @@ todo: +* avahi-client: + * service resolving + * examples * release! later: -- 2.39.5