diff options
| -rw-r--r-- | avahi-client/Makefile.am | 4 | ||||
| -rw-r--r-- | avahi-client/client.c | 12 | ||||
| -rw-r--r-- | avahi-client/client.h | 45 | ||||
| -rw-r--r-- | avahi-client/internal.h | 10 | ||||
| -rw-r--r-- | avahi-client/resolver.c | 263 | ||||
| -rw-r--r-- | avahi-common/dbus-watch-glue.c | 43 | ||||
| -rw-r--r-- | examples/Makefile.am | 26 | ||||
| -rw-r--r-- | examples/client-browse-services.c | 164 | ||||
| -rw-r--r-- | examples/client-publish-service.c | 178 | ||||
| -rw-r--r-- | examples/core-browse-services.c (renamed from examples/browse-services.c) | 0 | ||||
| -rw-r--r-- | examples/core-publish-service.c (renamed from examples/publish-service.c) | 2 | 
11 files changed, 728 insertions, 19 deletions
diff --git a/avahi-client/Makefile.am b/avahi-client/Makefile.am index 972aba9..d1cba37 100644 --- a/avahi-client/Makefile.am +++ b/avahi-client/Makefile.am @@ -36,7 +36,9 @@ lib_LTLIBRARIES = libavahi-client.la  libavahi_client_la_SOURCES = \  	client.c client.h \  	entrygroup.c \ -	browser.c +	browser.c \ +	resolver.c +  libavahi_client_la_CFLAGS = $(AM_CFLAGS) $(DBUS_CFLAGS)  libavahi_client_la_LIBADD = $(AM_LDADD) $(DBUS_LIBS) ../avahi-common/libavahi-common.la ../avahi-common/libdbus-common.la  libavahi_client_la_LDFLAGS = $(AM_LDFLAGS) -export-dynamic -version-info 0:0:0 diff --git a/avahi-client/client.c b/avahi-client/client.c index 2361cbc..bb623c0 100644 --- a/avahi-client/client.c +++ b/avahi-client/client.c @@ -102,10 +102,10 @@ static DBusHandlerResult filter_func(DBusConnection *bus, DBusMessage *message,      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)); +/*     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_DISCONNECTED)          goto fail; @@ -259,6 +259,7 @@ AvahiClient *avahi_client_new(const AvahiPoll *poll_api, AvahiClientCallback cal      AVAHI_LLIST_HEAD_INIT(AvahiDomainBrowser, client->domain_browsers);      AVAHI_LLIST_HEAD_INIT(AvahiServiceBrowser, client->service_browsers);      AVAHI_LLIST_HEAD_INIT(AvahiServiceTypeBrowser, client->service_type_browsers); +    AVAHI_LLIST_HEAD_INIT(AvahiServiceResolver, client->service_resolvers);      if (!(client->bus = dbus_bus_get(DBUS_BUS_SYSTEM, &error)) ||          dbus_error_is_set (&error)) @@ -352,6 +353,9 @@ void avahi_client_free(AvahiClient *client) {      while (client->service_type_browsers)          avahi_service_type_browser_free(client->service_type_browsers); +    while (client->service_resolvers) +        avahi_service_resolver_free(client->service_resolvers); +      if (client->bus) {          dbus_connection_disconnect(client->bus);          dbus_connection_unref(client->bus); diff --git a/avahi-client/client.h b/avahi-client/client.h index 056adfb..2753f1b 100644 --- a/avahi-client/client.h +++ b/avahi-client/client.h @@ -37,12 +37,24 @@  AVAHI_C_DECL_BEGIN  #endif +/** A connection context */  typedef struct AvahiClient AvahiClient; + +/** An entry group object */  typedef struct AvahiEntryGroup AvahiEntryGroup; + +/** A domain browser object */  typedef struct AvahiDomainBrowser AvahiDomainBrowser; + +/** A service browser object */  typedef struct AvahiServiceBrowser AvahiServiceBrowser; + +/** A service type browser object */  typedef struct AvahiServiceTypeBrowser AvahiServiceTypeBrowser; +/** A service resolver object */ +typedef struct AvahiServiceResolver AvahiServiceResolver; +  /** States of a client object, note that AvahiServerStates are also emitted */  typedef enum {      AVAHI_CLIENT_S_INVALID = AVAHI_SERVER_INVALID, @@ -67,6 +79,21 @@ typedef void (*AvahiServiceBrowserCallback) (AvahiServiceBrowser *b, AvahiIfInde  /** The function prototype for the callback of an AvahiServiceTypeBrowser */  typedef void (*AvahiServiceTypeBrowserCallback) (AvahiServiceTypeBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const char *type, const char *domain, void *userdata); +/** The function prototype for the callback of an AvahiServiceResolver */ +typedef void (*AvahiServiceResolverCallback) ( +    AvahiServiceResolver *r, +    AvahiIfIndex interface, +    AvahiProtocol protocol, +    AvahiResolverEvent event, +    const char *name, +    const char *type, +    const char *domain, +    const char *host_name, +    const AvahiAddress *a, +    uint16_t port, +    AvahiStringList *txt, +    void *userdata); +  /** Creates a new client instance */  AvahiClient* avahi_client_new (const AvahiPoll *poll_api, AvahiClientCallback callback, void *userdata, int *error); @@ -197,6 +224,24 @@ const char* avahi_service_browser_get_dbus_path (AvahiServiceBrowser *);  /* Cleans up and frees an AvahiServiceBrowser object */  int avahi_service_browser_free (AvahiServiceBrowser *); +/** Create a new service resolver object */ +AvahiServiceResolver * avahi_service_resolver_new( +    AvahiClient *client, +    AvahiIfIndex interface, +    AvahiProtocol protocol, +    const char *name, +    const char *type, +    const char *domain, +    AvahiProtocol aprotocol, +    AvahiServiceResolverCallback callback, +    void *userdata); + +/** Free a service resolver object */ +int avahi_service_resolver_free(AvahiServiceResolver *r); + +/** Block until the resolving is complete */ +int avahi_service_resolver_block(AvahiServiceResolver *r); +  #ifndef DOXYGEN_SHOULD_SKIP_THIS  AVAHI_C_DECL_END  #endif diff --git a/avahi-client/internal.h b/avahi-client/internal.h index 3859d70..1788122 100644 --- a/avahi-client/internal.h +++ b/avahi-client/internal.h @@ -41,6 +41,7 @@ struct AvahiClient {      AVAHI_LLIST_HEAD(AvahiDomainBrowser, domain_browsers);      AVAHI_LLIST_HEAD(AvahiServiceBrowser, service_browsers);      AVAHI_LLIST_HEAD(AvahiServiceTypeBrowser, service_type_browsers); +    AVAHI_LLIST_HEAD(AvahiServiceResolver, service_resolvers);  };  struct AvahiEntryGroup { @@ -76,6 +77,14 @@ struct AvahiServiceTypeBrowser {      AVAHI_LLIST_FIELDS(AvahiServiceTypeBrowser, service_type_browsers);  }; +struct AvahiServiceResolver { +    DBusPendingCall *call; +    AvahiClient *client; +    AvahiServiceResolverCallback callback; +    void *userdata; +    AVAHI_LLIST_FIELDS(AvahiServiceResolver, service_resolvers); +}; +  int avahi_client_set_errno (AvahiClient *client, int error);  int avahi_client_set_dbus_error(AvahiClient *client, DBusError *error); @@ -86,4 +95,5 @@ DBusHandlerResult avahi_domain_browser_event (AvahiClient *client, AvahiBrowserE  DBusHandlerResult avahi_service_type_browser_event (AvahiClient *client, AvahiBrowserEvent event, DBusMessage *message);  DBusHandlerResult avahi_service_browser_event (AvahiClient *client, AvahiBrowserEvent event, DBusMessage *message); +  #endif diff --git a/avahi-client/resolver.c b/avahi-client/resolver.c new file mode 100644 index 0000000..66208cf --- /dev/null +++ b/avahi-client/resolver.c @@ -0,0 +1,263 @@ +/* $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 <config.h> +#endif + +#include <stdlib.h> +#include <stdio.h> +#include <string.h> + +#include <dbus/dbus.h> + +#include <avahi-client/client.h> +#include <avahi-common/dbus.h> +#include <avahi-common/llist.h> +#include <avahi-common/error.h> +#include <avahi-common/malloc.h> + +#include "client.h" +#include "internal.h" + +static void pending_call_callback(DBusPendingCall *pending, void *userdata) { +    AvahiServiceResolver *r =  userdata; +    DBusMessage *message = NULL; +    AvahiStringList *strlst = NULL; +    DBusError error; +     +    assert(pending); +    assert(r); + +    dbus_error_init(&error); + +    if (!(message = dbus_pending_call_steal_reply(pending))) +        goto fail; + +    if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_METHOD_RETURN) { +        int j; +        int32_t interface, protocol, aprotocol; +        char *name, *type, *domain, *host, *address; +        uint16_t port; +        DBusMessageIter iter, sub; +        AvahiAddress a; +         +        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_STRING, &host, +                DBUS_TYPE_INT32, &aprotocol, +                DBUS_TYPE_STRING, &address, +                DBUS_TYPE_UINT16, &port, +                DBUS_TYPE_INVALID) || +            dbus_error_is_set (&error)) { +            fprintf(stderr, "Failed to parse resolver event.\n"); +            goto fail; +        } +         +        dbus_message_iter_init(message, &iter); +         +        for (j = 0; j < 9; j++) +            dbus_message_iter_next(&iter); +         +        if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY || +            dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_ARRAY) { +            fprintf(stderr, "Error parsing service resolving message"); +            goto fail; +        } +         +        strlst = NULL; +        dbus_message_iter_recurse(&iter, &sub); +         +        for (;;) { +            DBusMessageIter sub2; +            int at, n; +            uint8_t *k; +             +            if ((at = dbus_message_iter_get_arg_type(&sub)) == DBUS_TYPE_INVALID) +                break; +             +            assert(at == DBUS_TYPE_ARRAY); +             +            if (dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_BYTE) { +                fprintf(stderr, "Error parsing service resolving message"); +                goto fail; +            } +             +            dbus_message_iter_recurse(&sub, &sub2); +            dbus_message_iter_get_fixed_array(&sub2, &k, &n); +            strlst = avahi_string_list_add_arbitrary(strlst, k, n); +             +            dbus_message_iter_next(&sub); +        } + +        assert(address); +        if (!avahi_address_parse(address, (AvahiProtocol) aprotocol, &a)) { +            fprintf(stderr, "Failed to parse address\n"); +            goto fail; +        } +     +        r->callback(r, (AvahiIfIndex) interface, (AvahiProtocol) protocol, AVAHI_RESOLVER_FOUND, name, type, domain, host, &a, port, strlst, r->userdata); + +    } else { + +        assert(dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_ERROR); + +        avahi_client_set_errno(r->client, avahi_error_dbus_to_number(dbus_message_get_error_name(message))); + +        r->callback(r, (AvahiIfIndex) 0, (AvahiProtocol) 0, AVAHI_RESOLVER_TIMEOUT, NULL, NULL, NULL, NULL, NULL, 0, NULL, r->userdata); +    } + +fail: +     +    if (message) +        dbus_message_unref(message); +     +    avahi_string_list_free(strlst); +     +    dbus_error_free (&error); +} + +AvahiServiceResolver * avahi_service_resolver_new( +    AvahiClient *client, +    AvahiIfIndex interface, +    AvahiProtocol protocol, +    const char *name, +    const char *type, +    const char *domain, +    AvahiProtocol aprotocol, +    AvahiServiceResolverCallback callback, +    void *userdata) { + +    DBusError error; +    AvahiServiceResolver *r; +    DBusMessage *message; +    int32_t i_interface, i_protocol, i_aprotocol; +     +    assert(client); +    assert(name); +    assert(type); + +    if (!domain) +        domain = ""; +     +    dbus_error_init (&error); + +    if (client->state == AVAHI_CLIENT_DISCONNECTED) { +        avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE); +        goto fail; +    } + +    if (!(r = avahi_new(AvahiServiceResolver, 1))) { +        avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY); +        goto fail; +    } + +    r->client = client; +    r->callback = callback; +    r->userdata = userdata; +    r->call = NULL; +     +    AVAHI_LLIST_PREPEND(AvahiServiceResolver, service_resolvers, client->service_resolvers, r); + +    if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "ResolveService"))) { +        avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY); +        goto fail; +    } + +    i_interface = interface; +    i_protocol = protocol; +    i_aprotocol = aprotocol; + +    if (!(dbus_message_append_args( +              message, +              DBUS_TYPE_INT32, &i_interface, +              DBUS_TYPE_INT32, &i_protocol, +              DBUS_TYPE_STRING, &name, +              DBUS_TYPE_STRING, &type, +              DBUS_TYPE_STRING, &domain, +              DBUS_TYPE_INT32, &i_aprotocol, +              DBUS_TYPE_INVALID))) { +        avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY); +        goto fail; +    } + +    if (!dbus_connection_send_with_reply(client->bus, message, &r->call, -1) || +        !dbus_pending_call_set_notify(r->call, pending_call_callback, r, NULL)) { +        avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY); +        goto fail; +    } + +    dbus_message_unref(message); + +    return r; +     +fail: + +    if (dbus_error_is_set(&error)) { +        avahi_client_set_dbus_error(client, &error); +        dbus_error_free(&error); +    } + +    if (r) +        avahi_service_resolver_free(r); +     +    if (message) +        dbus_message_unref(message); + +    return NULL; + +} + +int avahi_service_resolver_free(AvahiServiceResolver *r) { +    AvahiClient *client; + +    assert(r); +    client = r->client; + +    if (r->call) { +        dbus_pending_call_cancel(r->call); +        dbus_pending_call_unref(r->call); +    } + +    AVAHI_LLIST_REMOVE(AvahiServiceResolver, service_resolvers, client->service_resolvers, r); + +    avahi_free(r); + +    return AVAHI_OK; +} + +int avahi_service_resolver_block(AvahiServiceResolver *r) { +    AvahiClient *client; + +    assert(r); +    client = r->client; + +    if (r->call) +        dbus_pending_call_block(r->call); + +    return AVAHI_OK; +} + diff --git a/avahi-common/dbus-watch-glue.c b/avahi-common/dbus-watch-glue.c index 7ca1c9d..80d35f2 100644 --- a/avahi-common/dbus-watch-glue.c +++ b/avahi-common/dbus-watch-glue.c @@ -95,9 +95,15 @@ static void dispatch_timeout_callback(AvahiTimeout *t, void *userdata) {      assert(t);      assert(d); +    connection_data_ref(d); +    dbus_connection_ref(d->connection); +      if (dbus_connection_dispatch(d->connection) == DBUS_DISPATCH_DATA_REMAINS)          /* If there's still data, request that this handler is called again */          request_dispatch(d); + +    dbus_connection_unref(d->connection); +    connection_data_unref(d);  }  static void watch_callback(AvahiWatch *avahi_watch, int fd, AvahiWatchEvent events, void *userdata) { @@ -180,10 +186,32 @@ typedef struct TimeoutData {      const AvahiPoll *poll_api;      AvahiTimeout *avahi_timeout;      DBusTimeout *dbus_timeout; +    int ref;  } TimeoutData; +static TimeoutData* timeout_data_ref(TimeoutData *t) { +    assert(t); +    assert(t->ref >= 1); + +    t->ref++; +    return t; +} + +static void timeout_data_unref(TimeoutData *t) { +    assert(t); +    assert(t->ref >= 1); + +    if (--t->ref <= 0) { +        if (t->avahi_timeout) +            t->poll_api->timeout_free(t->avahi_timeout); +         +        avahi_free(t); +    } +} +  static void update_timeout(TimeoutData *timeout) {      assert(timeout); +    assert(timeout->ref >= 1);      if (dbus_timeout_get_enabled(timeout->dbus_timeout)) {          struct timeval tv; @@ -201,10 +229,15 @@ static void timeout_callback(AvahiTimeout *avahi_timeout, void *userdata) {      assert(avahi_timeout);      assert(timeout); +    timeout_data_ref(timeout); +      dbus_timeout_handle(timeout->dbus_timeout);      /* Ignore the return value */ + +    if (timeout->avahi_timeout) +        update_timeout(timeout); -    update_timeout(timeout); +    timeout_data_unref(timeout);  }  static dbus_bool_t add_timeout(DBusTimeout *dbus_timeout, void *userdata) { @@ -221,6 +254,7 @@ static dbus_bool_t add_timeout(DBusTimeout *dbus_timeout, void *userdata) {      timeout->dbus_timeout = dbus_timeout;      timeout->poll_api = d->poll_api; +    timeout->ref = 1;      if ((b = dbus_timeout_get_enabled(dbus_timeout)))          avahi_elapse_time(&tv, dbus_timeout_get_interval(dbus_timeout), 0); @@ -229,12 +263,12 @@ static dbus_bool_t add_timeout(DBusTimeout *dbus_timeout, void *userdata) {                d->poll_api,                b ? &tv : NULL,                timeout_callback, -              dbus_timeout))) { +              timeout))) {          avahi_free(timeout);          return FALSE;      } -    dbus_timeout_set_data(dbus_timeout, timeout, NULL); +    dbus_timeout_set_data(dbus_timeout, timeout, (DBusFreeFunction) timeout_data_unref);      return TRUE;  } @@ -249,8 +283,7 @@ static void remove_timeout(DBusTimeout *dbus_timeout, void *userdata) {      assert(timeout);      d->poll_api->timeout_free(timeout->avahi_timeout); -    avahi_free(timeout); -    dbus_timeout_set_data(dbus_timeout, NULL, NULL); +    timeout->avahi_timeout = NULL;  }  static void timeout_toggled(DBusTimeout *dbus_timeout, void *userdata) { diff --git a/examples/Makefile.am b/examples/Makefile.am index d819103..da233b9 100644 --- a/examples/Makefile.am +++ b/examples/Makefile.am @@ -20,13 +20,23 @@  AM_CFLAGS=-I$(top_srcdir)  noinst_PROGRAMS = \ -	publish-service \ -	browse-services +	core-publish-service \ +	core-browse-services \ +	client-publish-service \ +	client-browse-services -publish_service_SOURCES = publish-service.c -publish_service_CFLAGS = $(AM_CFLAGS) -publish_service_LDADD = $(AM_LDADD) ../avahi-core/libavahi-core.la ../avahi-common/libavahi-common.la +core_publish_service_SOURCES = core-publish-service.c +core_publish_service_CFLAGS = $(AM_CFLAGS) +core_publish_service_LDADD = $(AM_LDADD) ../avahi-core/libavahi-core.la ../avahi-common/libavahi-common.la -browse_services_SOURCES = browse-services.c -browse_services_CFLAGS = $(AM_CFLAGS) -browse_services_LDADD = $(AM_LDADD) ../avahi-core/libavahi-core.la ../avahi-common/libavahi-common.la +core_browse_services_SOURCES = core-browse-services.c +core_browse_services_CFLAGS = $(AM_CFLAGS) +core_browse_services_LDADD = $(AM_LDADD) ../avahi-core/libavahi-core.la ../avahi-common/libavahi-common.la + +client_publish_service_SOURCES = client-publish-service.c +client_publish_service_CFLAGS = $(AM_CFLAGS) +client_publish_service_LDADD = $(AM_LDADD) ../avahi-client/libavahi-client.la ../avahi-common/libavahi-common.la + +client_browse_services_SOURCES = client-browse-services.c +client_browse_services_CFLAGS = $(AM_CFLAGS) +client_browse_services_LDADD = $(AM_LDADD) ../avahi-client/libavahi-client.la ../avahi-common/libavahi-common.la diff --git a/examples/client-browse-services.c b/examples/client-browse-services.c new file mode 100644 index 0000000..80fa934 --- /dev/null +++ b/examples/client-browse-services.c @@ -0,0 +1,164 @@ +/* $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 <config.h> +#endif + +#include <stdio.h> +#include <assert.h> +#include <stdlib.h> +#include <time.h> + +#include <avahi-client/client.h> +#include <avahi-common/simple-watch.h> +#include <avahi-common/malloc.h> +#include <avahi-common/error.h> + +static AvahiSimplePoll *simple_poll = NULL; + +static void resolve_callback( +    AvahiServiceResolver *r, +    AvahiIfIndex interface, +    AvahiProtocol protocol, +    AvahiResolverEvent event, +    const char *name, +    const char *type, +    const char *domain, +    const char *host_name, +    const AvahiAddress *address, +    uint16_t port, +    AvahiStringList *txt, +    void* userdata) { + +    assert(r); + +    /* Called whenever a service has been resolved successfully or timed out */ + +    if (event == AVAHI_RESOLVER_TIMEOUT) +        fprintf(stderr, "Failed to resolve service '%s' of type '%s' in domain '%s'.\n", name, type, domain); +    else { +        char a[128], *t; + +        assert(event == AVAHI_RESOLVER_FOUND); +         +        fprintf(stderr, "Service '%s' of type '%s' in domain '%s':\n", name, type, domain); + +        avahi_address_snprint(a, sizeof(a), address); +        t = avahi_string_list_to_string(txt); +        fprintf(stderr, "\t%s:%u (%s) TXT=%s\n", host_name, port, a, t); +        avahi_free(t); +    } + +    avahi_service_resolver_free(r); +} + +static void browse_callback( +    AvahiServiceBrowser *b, +    AvahiIfIndex interface, +    AvahiProtocol protocol, +    AvahiBrowserEvent event, +    const char *name, +    const char *type, +    const char *domain, +    void* userdata) { +     +    AvahiClient *c = userdata; +    assert(b); + +    /* Called whenever a new services becomes available on the LAN or is removed from the LAN */ + +    fprintf(stderr, "%s: service '%s' of type '%s' in domain '%s'\n", +            event == AVAHI_BROWSER_NEW ? "NEW" : "REMOVED", +            name, +            type, +            domain); +     +    /* If it's new, let's resolve it */ +    if (event == AVAHI_BROWSER_NEW) +         +        /* We ignore the returned resolver object. In the callback function +        we free it. If the server is terminated before the callback +        function is called the server will free the resolver for us. */ + +        if (!(avahi_service_resolver_new(c, interface, protocol, name, type, domain, AVAHI_PROTO_UNSPEC, resolve_callback, c))) +            fprintf(stderr, "Failed to resolve service '%s': %s\n", name, avahi_strerror(avahi_client_errno(c))); +} + +static void client_callback(AvahiClient *c, AvahiClientState state, void * userdata) { +    assert(c); + +    /* Called whenever the client or server state changes */ + +    if (state == AVAHI_CLIENT_DISCONNECTED) { +        fprintf(stderr, "Server connection terminated.\n"); +        avahi_simple_poll_quit(simple_poll); +    } +} + +int main(int argc, char*argv[]) { +    AvahiClient *client = NULL; +    AvahiServiceBrowser *sb; +    int error; +    int ret = 1; + +    /* Allocate main loop object */ +    if (!(simple_poll = avahi_simple_poll_new())) { +        fprintf(stderr, "Failed to create simple poll object.\n"); +        goto fail; +    } + +    /* Allocate a new client */ +    client = avahi_client_new(avahi_simple_poll_get(simple_poll), client_callback, NULL, &error); + +    /* Check wether creating the client object succeeded */ +    if (!client) { +        fprintf(stderr, "Failed to create client: %s\n", avahi_strerror(error)); +        goto fail; +    } +     +    /* Create the service browser */ +    if (!(sb = avahi_service_browser_new(client, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, "_http._tcp", NULL, browse_callback, client))) { +        fprintf(stderr, "Failed to create service browser: %s\n", avahi_strerror(avahi_client_errno(client))); +        goto fail; +    } +     +    /* Run the main loop */ +    for (;;) +        if (avahi_simple_poll_iterate(simple_poll, -1) != 0) +            break; +     +    ret = 0; +     +fail: +     +    /* Cleanup things */ +    if (sb) +        avahi_service_browser_free(sb); +     +    if (client) +        avahi_client_free(client); + +    if (simple_poll) +        avahi_simple_poll_free(simple_poll); + +    return ret; +} diff --git a/examples/client-publish-service.c b/examples/client-publish-service.c new file mode 100644 index 0000000..9b12f5c --- /dev/null +++ b/examples/client-publish-service.c @@ -0,0 +1,178 @@ +/* $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 <config.h> +#endif + +#include <time.h> +#include <stdio.h> +#include <stdlib.h> +#include <assert.h> + +#include <avahi-client/client.h> +#include <avahi-common/alternative.h> +#include <avahi-common/simple-watch.h> +#include <avahi-common/malloc.h> +#include <avahi-common/error.h> + +static AvahiEntryGroup *group = NULL; +static AvahiSimplePoll *simple_poll = NULL; +static char *name = NULL; + +static void create_services(AvahiClient *c); + +static void entry_group_callback(AvahiEntryGroup *g, AvahiEntryGroupState state, void *userdata) { +    assert(g == group); + +    /* Called whenever the entry group state changes */ + +    if (state == AVAHI_ENTRY_GROUP_ESTABLISHED) +        /* The entry group has been established successfully */ +        fprintf(stderr, "Service '%s' successfully established.\n", name); +     +    else if (state == AVAHI_ENTRY_GROUP_COLLISION) { +        char *n; + +        /* A service name collision happened. Let's pick a new name */ +        n = avahi_alternative_service_name(name); +        avahi_free(name); +        name = n; + +        fprintf(stderr, "Service name collision, renaming service to '%s'\n", name); + +        /* And recreate the services */ +        create_services(avahi_entry_group_get_client(g)); +    } +} + +static void create_services(AvahiClient *c) { +    char r[128]; +    int ret; +    assert(c); + +    /* If this is the first time we're called, let's create a new entry group */ +    if (!group) { +        if (!(group = avahi_entry_group_new(c, entry_group_callback, NULL))) { +            fprintf(stderr, "avahi_entry_group_new() failed: %s\n", avahi_strerror(avahi_client_errno(c))); +            goto fail; +        } +    } +     +    fprintf(stderr, "Adding service '%s'\n", name); + +    /* Create some random TXT data */ +    snprintf(r, sizeof(r), "random=%i", rand()); + +    /* Add the service for IPP */ +    if ((ret = avahi_entry_group_add_service(group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, name, "_ipp._tcp", NULL, NULL, 651, "test=blah", r, NULL)) < 0) { +        fprintf(stderr, "Failed to add _ipp._tcp service: %s\n", avahi_strerror(ret)); +        goto fail; +    } + +    /* Add the same service for BSD LPR */ +    if ((ret = avahi_entry_group_add_service(group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, name, "_printer._tcp", NULL, NULL, 515, NULL)) < 0) { +        fprintf(stderr, "Failed to add _printer._tcp service: %s\n", avahi_strerror(ret)); +        goto fail; +    } + +    /* Tell the server to register the service */ +    if ((ret = avahi_entry_group_commit(group)) < 0) { +        fprintf(stderr, "Failed to commit entry_group: %s\n", avahi_strerror(ret)); +        goto fail; +    } + +    return; + +fail: +    avahi_simple_poll_quit(simple_poll); +    return; +} + +static void client_callback(AvahiClient *c, AvahiClientState state, void * userdata) { +    assert(c); + +    /* Called whenever the client or server state changes */ + +    if (state == AVAHI_CLIENT_S_RUNNING) +        /* The serve has startup successfully and registered its host +         * name on the network, so it's time to create our services */ +        create_services(c); +     +    else if (state == AVAHI_CLIENT_S_COLLISION) { +        /* Let's drop our registered services. When the server is back +         * in AVAHI_SERVER_RUNNING state we will register them +         * again with the new host name. */ +        if (group) +            avahi_entry_group_reset(group); +         +    } else if (state == AVAHI_CLIENT_DISCONNECTED) { + +        fprintf(stderr, "Server connection terminated.\n"); +        avahi_simple_poll_quit(simple_poll); +    } +} + +int main(int argc, char*argv[]) { +    AvahiClient *client = NULL; +    int error; +    int ret = 1; +     +    /* Allocate main loop object */ +    if (!(simple_poll = avahi_simple_poll_new())) { +        fprintf(stderr, "Failed to create simple poll object.\n"); +        goto fail; +    } +     +    name = avahi_strdup("MegaPrinter"); + +    /* Allocate a new client */ +    client = avahi_client_new(avahi_simple_poll_get(simple_poll), client_callback, NULL, &error); + +    /* Check wether creating the client object succeeded */ +    if (!client) { +        fprintf(stderr, "Failed to create client: %s\n", avahi_strerror(error)); +        goto fail; +    } +     +    /* Run the main loop */ +    for (;;) +        if (avahi_simple_poll_iterate(simple_poll, -1) != 0) +            break; +     +    ret = 0; +     +fail: +     +    /* Cleanup things */ +    if (group) +        avahi_entry_group_free(group); + +    if (client) +        avahi_client_free(client); + +    if (simple_poll) +        avahi_simple_poll_free(simple_poll); + +    avahi_free(name); +     +    return ret; +} diff --git a/examples/browse-services.c b/examples/core-browse-services.c index 7227bd7..7227bd7 100644 --- a/examples/browse-services.c +++ b/examples/core-browse-services.c diff --git a/examples/publish-service.c b/examples/core-publish-service.c index 6132622..dace548 100644 --- a/examples/publish-service.c +++ b/examples/core-publish-service.c @@ -171,7 +171,7 @@ int main(int argc, char*argv[]) {      /* Check wether creating the server object succeeded */      if (!server) { -        fprintf(stderr, "Failed to create server: %s", avahi_strerror(error)); +        fprintf(stderr, "Failed to create server: %s\n", avahi_strerror(error));          goto fail;      }  | 
