From 7231641e5c53a9d1c255e06a7a134fa60a919f88 Mon Sep 17 00:00:00 2001 From: Trent Lloyd Date: Thu, 11 Aug 2005 07:31:07 +0000 Subject: * Work on avahi-client - Support for connecting and registering services works - Needs some more error handling (DBus Errors from the daemon) - Needs browser support yet * Fix avahi-utils to pass make dist-check * Add new error codes git-svn-id: file:///home/lennart/svn/public/avahi/trunk@288 941a03a8-eaeb-0310-b9a0-b1bbd8fe43fe --- avahi-client/Makefile.am | 8 +- avahi-client/client-test.c | 42 ++++++-- avahi-client/client.c | 165 ++++++++++++++++++++++------- avahi-client/client.h | 51 ++++++++- avahi-client/entrygroup.c | 256 +++++++++++++++++++++++++++++++++++++++++++++ avahi-client/internal.h | 50 +++++++++ 6 files changed, 528 insertions(+), 44 deletions(-) create mode 100644 avahi-client/entrygroup.c create mode 100644 avahi-client/internal.h (limited to 'avahi-client') diff --git a/avahi-client/Makefile.am b/avahi-client/Makefile.am index a01ede2..d42f33d 100644 --- a/avahi-client/Makefile.am +++ b/avahi-client/Makefile.am @@ -33,6 +33,9 @@ avahi_clientincludedir=$(includedir)/avahi-client avahi_clientinclude_HEADERS = \ client.h +noinst_HEADERS = \ + internal.h + noinst_PROGRAMS = \ client-test @@ -40,11 +43,12 @@ lib_LTLIBRARIES = \ libavahi-client.la libavahi_client_la_SOURCES = \ - client.c client.h + client.c client.h \ + entrygroup.c client_test_SOURCES = \ client-test.c client_test_CFLAGS = $(AM_CFLAGS) -client_test_LDADD = $(AM_LDADD) libavahi-client.la +client_test_LDADD = $(AM_LDADD) libavahi-client.la ../avahi-common/libavahi-common.la endif diff --git a/avahi-client/client-test.c b/avahi-client/client-test.c index 530394f..64ad9a5 100644 --- a/avahi-client/client-test.c +++ b/avahi-client/client-test.c @@ -20,41 +20,71 @@ ***/ #include +#include #include #include +void +avahi_client_callback (AvahiClient *c, AvahiClientState state, void *user_data) +{ + printf ("XXX: Callback on client, state -> %d, data -> %s\n", state, (char*)user_data); +} + +void +avahi_entry_group_callback (AvahiClient *c, AvahiEntryGroup *g, AvahiEntryGroupState state, void *user_data) +{ + printf ("Callback on %s, state -> %d, data -> %s\n", avahi_entry_group_get_path (g), state, (char*)user_data); +} + int main (int argc, char *argv[]) { GMainLoop *loop; AvahiClient *avahi; + AvahiEntryGroup *group; + AvahiStringList *txt; char *ret; loop = g_main_loop_new (NULL, FALSE); - avahi = avahi_client_new (); + avahi = avahi_client_new (avahi_client_callback, "omghai2u"); g_assert (avahi != NULL); ret = avahi_client_get_version_string (avahi); - printf ("Avahi Server Version: %s\n", ret); + printf ("Avahi Server Version: %s (Error Return: %s)\n", ret, avahi_strerror (avahi_client_errno (avahi))); g_free (ret); ret = avahi_client_get_host_name (avahi); - printf ("Host Name: %s\n", ret); + printf ("Host Name: %s (Error Return: %s)\n", ret, avahi_strerror (avahi_client_errno (avahi))); g_free (ret); ret = avahi_client_get_domain_name (avahi); - printf ("Domain Name: %s\n", ret); + printf ("Domain Name: %s (Error Return: %s)\n", ret, avahi_strerror (avahi_client_errno (avahi))); g_free (ret); ret = avahi_client_get_host_name_fqdn (avahi); - printf ("FQDN: %s\n", ret); + printf ("FQDN: %s (Error Return: %s)\n", ret, avahi_strerror (avahi_client_errno (avahi))); g_free (ret); + + group = avahi_entry_group_new (avahi, avahi_entry_group_callback, "omghai"); - g_free (avahi); + printf ("Creating entry group: %s\n", avahi_strerror (avahi_client_errno (avahi))); + + if (group == NULL) + printf ("Failed to create entry group object\n"); + else + printf ("Sucessfully created entry group, path %s\n", avahi_entry_group_get_path (group)); + + txt = avahi_string_list_new ("foo=bar", NULL); + + avahi_entry_group_add_service (group, AVAHI_IF_UNSPEC, AF_UNSPEC, "Lathiat's Site", "_http._tcp", "", "", 80, txt); + + avahi_entry_group_commit (group); g_main_loop_run (loop); + g_free (avahi); + return 0; } diff --git a/avahi-client/client.c b/avahi-client/client.c index f5de858..43b9fc1 100644 --- a/avahi-client/client.c +++ b/avahi-client/client.c @@ -26,6 +26,7 @@ #include #include #include +#include #include #include #include @@ -36,26 +37,84 @@ #include -struct _AvahiClient +#include "client.h" +#include "internal.h" + +int +avahi_client_set_errno (AvahiClient *client, int errno) +{ + if (client == NULL) return errno; + + client->errno = errno; + + return errno; +} + +static +void avahi_client_state_change (AvahiClient *client, int state) +{ + if (client == NULL || client->callback == NULL) + return; + + client->callback (client, state, client->user_data); +} + +void +avahi_client_state_request_callback (DBusPendingCall *call, void *data) +{ + AvahiClient *client = data; + DBusError error; + DBusMessage *reply; + int state, type; + + dbus_error_init (&error); + + reply = dbus_pending_call_steal_reply (call); + + type = dbus_message_get_type (reply); + + if (type == DBUS_MESSAGE_TYPE_METHOD_RETURN) + { + dbus_message_get_args (reply, &error, DBUS_TYPE_INT32, &state, DBUS_TYPE_INVALID); + + if (dbus_error_is_set (&error)) + { + fprintf (stderr, "internal error parsing client state change for client\n"); + return; + } + + printf ("statechange (client) to %d\n", state); + + avahi_client_state_change (client, state); + } else if (type == DBUS_MESSAGE_TYPE_ERROR) { + dbus_set_error_from_message (&error, reply); + fprintf (stderr, "Error from reply: %s\n", error.message); + } + + dbus_pending_call_unref (call); +} + +void +avahi_client_schedule_state_request (AvahiClient *client) { - DBusConnection *bus; - AVAHI_LLIST_HEAD(AvahiEntryGroup, groups); -}; + DBusMessage *message; + DBusPendingCall *pcall; + + if (client == NULL) return; -struct _AvahiEntryGroup { - char *path; - AvahiClient *parent; - AVAHI_LLIST_FIELDS(AvahiEntryGroup, groups); -}; + message = dbus_message_new_method_call (AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "GetState"); + + dbus_connection_send_with_reply (client->bus, message, &pcall, -1); + + dbus_pending_call_set_notify (pcall, avahi_client_state_request_callback, client, NULL); +} static DBusHandlerResult filter_func (DBusConnection *bus, DBusMessage *message, void *data) { + AvahiClient *client = data; DBusError error; - g_assert (bus != NULL); - g_assert (message != NULL); - printf ("dbus: interface=%s, path=%s, member=%s\n", dbus_message_get_interface (message), dbus_message_get_path (message), @@ -74,24 +133,65 @@ filter_func (DBusConnection *bus, DBusMessage *message, void *data) } if (strcmp (name, AVAHI_DBUS_NAME) == 0) { + if (old == NULL && new != NULL) { fprintf(stderr, "Avahi Daemon connected\n"); + avahi_client_state_change (client, AVAHI_CLIENT_RECONNECTED); } else if (old != NULL && new == NULL) { fprintf(stderr, "Avahi Daemon disconnected\n"); + avahi_client_state_change (client, AVAHI_CLIENT_DISCONNECTED); + /* XXX: we really need to expire all entry groups */ + } + } + } else if (dbus_message_is_signal (message, AVAHI_DBUS_NAME, "StateChanged")) { + printf ("server statehcange\n"); + } else if (dbus_message_is_signal (message, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "StateChanged")) { + const char *path; + AvahiEntryGroup *n, *group = NULL; + path = dbus_message_get_path (message); + + for (n = client->groups; n != NULL; n = n->groups_next) + { + if (strcmp (n->path, path) == 0) + { + group = n; + break; } } - return DBUS_HANDLER_RESULT_HANDLED; + if (group == NULL) + { + fprintf (stderr, "Received state change for unknown EntryGroup object (%s)\n", path); + } else { + int state; + DBusError error; + + dbus_error_init (&error); + + dbus_message_get_args (message, &error, DBUS_TYPE_INT32, &state, DBUS_TYPE_INVALID); + + if (dbus_error_is_set (&error)) + { + fprintf (stderr, "internal error parsing entrygroup statechange for %s\n", group->path); + goto out; + } + + printf ("statechange (%s) to %d\n", group->path, state); + + avahi_entry_group_state_change (group, state); + } } + return DBUS_HANDLER_RESULT_HANDLED; + out: return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; } AvahiClient * -avahi_client_new () +avahi_client_new (AvahiClientCallback callback, void *user_data) { - AvahiClient *tmp; + AvahiClient *tmp = NULL; DBusError error; tmp = g_new (AvahiClient, 1); @@ -148,9 +248,14 @@ avahi_client_new () fprintf (stderr, "Error adding filter match: %s\n", error.message); dbus_error_free (&error); goto fail; + } - } + tmp->callback = callback; + tmp->user_data = user_data; + avahi_client_schedule_state_request (tmp); + + avahi_client_set_errno (tmp, AVAHI_OK); return tmp; fail: @@ -166,8 +271,7 @@ avahi_client_get_string_reply_and_block (AvahiClient *client, char *method, char DBusError error; char *ret, *new; - g_assert (client != NULL); - g_assert (method != NULL); + if (client == NULL || method == NULL) return NULL; dbus_error_init (&error); @@ -177,6 +281,7 @@ avahi_client_get_string_reply_and_block (AvahiClient *client, char *method, char { if (!dbus_message_append_args (message, DBUS_TYPE_STRING, ¶m, DBUS_TYPE_INVALID)) { + avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR); fprintf (stderr, "Failed to append string argument to %s message\n", method); return NULL; } @@ -189,6 +294,8 @@ avahi_client_get_string_reply_and_block (AvahiClient *client, char *method, char fprintf (stderr, "Error sending %s message: %s\n", method, error.message); dbus_error_free (&error); dbus_message_unref (message); + + avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR); return NULL; } @@ -196,6 +303,8 @@ avahi_client_get_string_reply_and_block (AvahiClient *client, char *method, char { dbus_message_unref (message); fprintf (stderr, "Could not connect to Avahi daemon\n"); + + avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR); return NULL; } @@ -205,11 +314,14 @@ avahi_client_get_string_reply_and_block (AvahiClient *client, char *method, char { fprintf (stderr, "Failed to parse %s reply: %s\n", method, error.message); dbus_error_free (&error); + + avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR); return NULL; } new = strdup (ret); + avahi_client_set_errno (client, AVAHI_OK); return new; } @@ -236,20 +348,3 @@ avahi_client_get_host_name_fqdn (AvahiClient *client) { return avahi_client_get_string_reply_and_block (client, "GetHostNameFqdn", NULL); } - -AvahiEntryGroup* -avahi_entry_group_new (AvahiClient *client) -{ - AvahiEntryGroup *tmp; - - tmp = malloc (sizeof (AvahiEntryGroup)); - - tmp->parent = client; - - AVAHI_LLIST_PREPEND(AvahiEntryGroup, groups, client->groups, tmp); - - return tmp; -fail: - if (tmp) free (tmp); - return NULL; -} diff --git a/avahi-client/client.h b/avahi-client/client.h index e0af110..c368bb3 100644 --- a/avahi-client/client.h +++ b/avahi-client/client.h @@ -23,6 +23,9 @@ ***/ #include +#include +#include +#include /** \file client.h Definitions and functions for the client API over D-Bus */ @@ -34,8 +37,18 @@ typedef struct _AvahiClient AvahiClient; typedef struct _AvahiEntryGroup AvahiEntryGroup; +/** States of a client object, note that AvahiServerStates are also emitted */ +typedef enum { + AVAHI_CLIENT_DISCONNECTED = 100, /**< Lost DBUS connection to the Avahi daemon */ + AVAHI_CLIENT_RECONNECTED = 101 /**< Regained connection to the daemon, all records need to be re-added */ +} AvahiClientState; + +typedef void (*AvahiClientCallback) (AvahiClient *s, AvahiClientState state, void* userdata); + +typedef void (*AvahiEntryGroupCallback) (AvahiClient *s, AvahiEntryGroup *g, AvahiEntryGroupState state, void* userdata); + /** Creates a new client instance */ -AvahiClient* avahi_client_new (); +AvahiClient* avahi_client_new (AvahiClientCallback callback, void *user_data); /** Get the version of the server */ char* avahi_client_get_version_string (AvahiClient*); @@ -49,6 +62,42 @@ char* avahi_client_get_domain_name (AvahiClient*); /** Get FQDN domain name */ char* avahi_client_get_host_name_fqdn (AvahiClient*); +/** Create a new AvahiEntryGroup object */ +AvahiEntryGroup* avahi_entry_group_new (AvahiClient*, AvahiEntryGroupCallback callback, void *user_data); + +/** Commit an AvahiEntryGroup */ +int avahi_entry_group_commit (AvahiEntryGroup*); + +/** Reset an AvahiEntryGroup */ +int avahi_entry_group_reset (AvahiEntryGroup*); + +/** Get an AvahiEntryGroup's state */ +int avahi_entry_group_state (AvahiEntryGroup*); + +/** Check if an AvahiEntryGroup is empty */ +int avahi_entry_group_is_empty (AvahiEntryGroup*); + +/** Get the last error number */ +int avahi_client_errno (AvahiClient*); + +/** Get an AvahiEntryGroup's owning client instance */ +AvahiClient* avahi_entry_group_get_client (AvahiEntryGroup*); + +/** Add a service, takes an AvahiStringList for text records */ +int +avahi_entry_group_add_service (AvahiEntryGroup *group, + AvahiIfIndex interface, + AvahiProtocol protocol, + const char *name, + const char *type, + const char *domain, + const char *host, + int port, + AvahiStringList *txt); + +/** Get the D-Bus path of an AvahiEntryGroup object, for debugging purposes only. */ +char* avahi_entry_group_get_path (AvahiEntryGroup *); + #ifndef DOXYGEN_SHOULD_SKIP_THIS AVAHI_C_DECL_END #endif diff --git a/avahi-client/entrygroup.c b/avahi-client/entrygroup.c new file mode 100644 index 0000000..9760cd9 --- /dev/null +++ b/avahi-client/entrygroup.c @@ -0,0 +1,256 @@ +/* $Id$ */ + +/*** + This file is part of avahi. + + avahi is free software; you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as + published by the Free Software Foundation; either version 2.1 of the + License, or (at your option) any later version. + + avahi is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General + Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with avahi; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + USA. +***/ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include +#include +#include +#include +#include +#include +#include + +#define DBUS_API_SUBJECT_TO_CHANGE +#include +#include + +#include + +#include "client.h" +#include "internal.h" + +void avahi_entry_group_state_change (AvahiEntryGroup *group, int state) +{ + if (group == NULL || group->callback == NULL) + return; + + group->callback (group->client, group, state, group->user_data); +} + +AvahiEntryGroup* +avahi_entry_group_new (AvahiClient *client, AvahiEntryGroupCallback callback, void *user_data) +{ + AvahiEntryGroup *tmp = NULL; + DBusMessage *message = NULL, *reply; + DBusError error; + char *path; + + if (client == NULL) + return NULL; + + dbus_error_init (&error); + + message = dbus_message_new_method_call (AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, + AVAHI_DBUS_INTERFACE_SERVER, "EntryGroupNew"); + + reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error); + + if (dbus_error_is_set (&error)) + { + fprintf (stderr, "Error sending EntryGroupNew message: %s\n", error.message); + dbus_error_free (&error); + + avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR); + goto fail; + } + + if (reply == NULL) + { + fprintf (stderr, "Got NULL reply from EntryGroupNew\n"); + + avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR); + goto fail; + } + + dbus_message_get_args (reply, &error, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID); + + if (dbus_error_is_set (&error)) + { + fprintf (stderr, "Failure parsing EntryGroupNew reply: %s\n", error.message); + avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR); + goto fail; + } + + tmp = malloc (sizeof (AvahiEntryGroup)); + + tmp->client = client; + + tmp->path = strdup (path); + tmp->callback = callback; + tmp->user_data = user_data; + + AVAHI_LLIST_PREPEND(AvahiEntryGroup, groups, client->groups, tmp); + + dbus_message_unref (message); + + avahi_client_set_errno (client, AVAHI_OK); + return tmp; + +fail: + if (tmp) free (tmp); + if (message) dbus_message_unref (message); + return NULL; +} + +int +avahi_entry_group_commit (AvahiEntryGroup *group) +{ + DBusMessage *message; + DBusError error; + + dbus_error_init (&error); + + message = dbus_message_new_method_call (AVAHI_DBUS_NAME, group->path, + AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "Commit"); + + dbus_connection_send (group->client->bus, message, NULL); + + return avahi_client_set_errno (group->client, AVAHI_OK); +} + +int +avahi_entry_group_reset (AvahiEntryGroup *group) +{ + DBusMessage *message; + + message = dbus_message_new_method_call (AVAHI_DBUS_NAME, group->path, + AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "Reset"); + + dbus_connection_send (group->client->bus, message, NULL); + + return avahi_client_set_errno (group->client, AVAHI_OK); +} + +int +avahi_entry_group_state (AvahiEntryGroup *group) +{ + DBusMessage *message, *reply; + DBusError error; + int state; + + dbus_error_init (&error); + + message = dbus_message_new_method_call (AVAHI_DBUS_NAME, group->path, + AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "GetState"); + + reply = dbus_connection_send_with_reply_and_block (group->client->bus, message, -1, &error); + + if (dbus_error_is_set (&error)) + { + fprintf (stderr, "Error sending GetState message for %s EntryGroup: %s\n", group->path, error.message); + dbus_error_free (&error); + + return avahi_client_set_errno (group->client, AVAHI_ERR_DBUS_ERROR); + } + + dbus_message_get_args(message, &error, DBUS_TYPE_BOOLEAN, &state, DBUS_TYPE_INVALID); + + if (dbus_error_is_set (&error)) + { + fprintf (stderr, "Error parsing GetState reply for %s EntryGroup: %s\n", group->path, error.message); + dbus_error_free (&error); + + return avahi_client_set_errno (group->client, AVAHI_ERR_DBUS_ERROR); + } + + avahi_client_set_errno (group->client, AVAHI_OK); + return state; +} + +int +avahi_client_errno (AvahiClient *client) +{ + return client->errno; +} + +AvahiClient* +avahi_entry_group_get_client (AvahiEntryGroup *group) +{ + return group->client; +} + +int +avahi_entry_group_is_empty (AvahiEntryGroup *group) +{ + return AVAHI_OK; +} + +int +avahi_entry_group_add_service (AvahiEntryGroup *group, + AvahiIfIndex interface, + AvahiProtocol protocol, + const char *name, + const char *type, + const char *domain, + const char *host, + int port, + AvahiStringList *txt) +{ + DBusMessage *message; + DBusMessageIter iter, sub; + AvahiStringList *p; + + message = dbus_message_new_method_call (AVAHI_DBUS_NAME, group->path, + AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "AddService"); + + if (!message) + { + dbus_message_unref (message); + return avahi_client_set_errno (group->client, AVAHI_ERR_DBUS_ERROR); + } + + if (!dbus_message_append_args (message, DBUS_TYPE_INT32, &interface, DBUS_TYPE_INT32, &protocol, + DBUS_TYPE_STRING, &name, DBUS_TYPE_STRING, &type, DBUS_TYPE_STRING, &domain, + DBUS_TYPE_STRING, &host, DBUS_TYPE_UINT16, &port, DBUS_TYPE_INVALID)) + { + dbus_message_unref (message); + return avahi_client_set_errno (group->client, AVAHI_ERR_DBUS_ERROR); + } + + dbus_message_iter_init_append(message, &iter); + dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_BYTE_AS_STRING, &sub); + + /* Assemble the AvahiStringList into an Array of Array of Bytes to send over dbus */ + for (p = txt; p != NULL; p = p->next) { + DBusMessageIter sub2; + const guint8 *data = p->text; + + dbus_message_iter_open_container(&sub, DBUS_TYPE_ARRAY, "y", &sub2); + dbus_message_iter_append_fixed_array(&sub2, DBUS_TYPE_BYTE, &data, p->size); + dbus_message_iter_close_container(&sub, &sub2); + } + + dbus_message_iter_close_container(&iter, &sub); + + dbus_connection_send (group->client->bus, message, NULL); + + return avahi_client_set_errno (group->client, AVAHI_OK); +} + +/* XXX: debug function */ +char* avahi_entry_group_get_path (AvahiEntryGroup *group) +{ + if (group != NULL) return group->path; + else return NULL; +} diff --git a/avahi-client/internal.h b/avahi-client/internal.h new file mode 100644 index 0000000..6fea0b0 --- /dev/null +++ b/avahi-client/internal.h @@ -0,0 +1,50 @@ +#ifndef foointernalhfoo +#define foointernalhfoo + +/* $Id$ */ + +/*** + This file is part of avahi. + + avahi is free software; you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as + published by the Free Software Foundation; either version 2.1 of the + License, or (at your option) any later version. + + avahi is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General + Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with avahi; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + USA. +***/ + +#ifdef HAVE_CONFIG_H +#include +#endif + +struct _AvahiClient +{ + DBusConnection *bus; + int errno; + AvahiClientCallback callback; + void *user_data; + AVAHI_LLIST_HEAD(AvahiEntryGroup, groups); +}; + +struct _AvahiEntryGroup { + char *path; + AvahiClient *client; + AvahiEntryGroupCallback callback; + void *user_data; + AVAHI_LLIST_FIELDS(AvahiEntryGroup, groups); +}; + +int avahi_client_set_errno (AvahiClient *client, int errno); + +void avahi_entry_group_state_change (AvahiEntryGroup *group, int state); + +#endif -- cgit