#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;
}
#include <assert.h>
static const AvahiPoll *poll_api = NULL;
+static AvahiSimplePoll *simple_poll = NULL;
static 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)
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;
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);
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;
}