From: Trent Lloyd Date: Fri, 26 Aug 2005 19:16:10 +0000 (+0000) Subject: * Implement AddressResolver in the avahi-client C api X-Git-Url: https://git.meshlink.io/?a=commitdiff_plain;h=4743c6236a42978862fa6cf3fa95e40c04fcbd5e;p=catta * Implement AddressResolver in the avahi-client C api * Small fix to the HostNameResolver git-svn-id: file:///home/lennart/svn/public/avahi/trunk@450 941a03a8-eaeb-0310-b9a0-b1bbd8fe43fe --- diff --git a/avahi-client/client-test.c b/avahi-client/client-test.c index 4142172..037867d 100644 --- a/avahi-client/client-test.c +++ b/avahi-client/client-test.c @@ -37,19 +37,19 @@ static AvahiSimplePoll *simple_poll = NULL; static 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); + printf ("Callback on client, state -> %d, data -> %s\n", state, (char*)user_data); } static void avahi_entry_group_callback (AvahiEntryGroup *g, AvahiEntryGroupState state, void *user_data) { - printf ("XXX: Callback on %s, state -> %d, data -> %s\n", avahi_entry_group_get_dbus_path(g), state, (char*)user_data); + printf ("Callback on %s, state -> %d, data -> %s\n", avahi_entry_group_get_dbus_path(g), state, (char*)user_data); } 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_get_dbus_path (b), interface, protocol, event, domain, (char*)user_data); + printf ("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 @@ -60,11 +60,12 @@ avahi_service_resolver_callback (AvahiServiceResolver *r, AvahiIfIndex interface if (event == AVAHI_RESOLVER_TIMEOUT) { printf ("ServiceResolver timed out\n"); + avahi_service_resolver_free (r); return; } avahi_address_snprint (addr, sizeof (addr), a); txtr = avahi_string_list_to_string (txt); - printf ("XXX: Callback on ServiceResolver, interface (%d), protocol (%d), event (%d), name (%s), type (%s), domain (%s), host_name (%s), address (%s), port (%d), txtdata (%s), data(%s)\n", interface, protocol, event, name, type, domain, host_name, addr, port, txtr, (char*)user_data); + printf ("Callback on ServiceResolver, interface (%d), protocol (%d), event (%d), name (%s), type (%s), domain (%s), host_name (%s), address (%s), port (%d), txtdata (%s), data(%s)\n", interface, protocol, event, name, type, domain, host_name, addr, port, txtr, (char*)user_data); } static void @@ -75,40 +76,64 @@ avahi_service_browser_callback (AvahiServiceBrowser *b, AvahiIfIndex interface, { sr = avahi_service_resolver_new (avahi_service_browser_get_client (b), interface, protocol, name, type, domain, AF_UNSPEC, avahi_service_resolver_callback, "xxXXxx"); } - 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); + printf ("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_get_dbus_path (b), interface, protocol, event, type, domain, (char*)user_data); + printf ("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 -avahi_host_name_resolver_callback (AvahiHostNameResolver *r, AvahiIfIndex interface, AvahiProtocol protocol, AvahiResolverEvent event, const char *name, const AvahiAddress *a, void *user_data) +avahi_address_resolver_callback (AvahiAddressResolver *r, AvahiIfIndex interface, AvahiProtocol protocol, AvahiResolverEvent event, AvahiProtocol aprotocol, const AvahiAddress *address, const char *name, const char *userdata) { char addr[64]; - if (event == AVAHI_RESOLVER_TIMEOUT) { - printf ("XXX: Callback on HostNameResolver, timed out.\n"); + printf ("Callback on AddressResolver, timed out.\n"); + avahi_address_resolver_free (r); return; } - avahi_address_snprint (addr, sizeof (addr), a); - printf ("XXX: Callback on HostNameResolver, interface (%d), protocol (%d), event (%d), name (%s), address (%s), data (%s)\n", interface, protocol, event, name, addr, (char*)user_data); + avahi_address_snprint (&addr, sizeof (addr), address); + printf ("Callback on AddressResolver, interface (%d), protocol (%d), even (%d), aprotocol (%d), address (%s), name (%s), data(%s)\n", interface, protocol, event, aprotocol, addr, name); } +static void +avahi_host_name_resolver_callback (AvahiHostNameResolver *r, AvahiIfIndex interface, AvahiProtocol protocol, AvahiResolverEvent event, const char *name, const AvahiAddress *a, void *user_data) +{ + AvahiClient *client; + AvahiAddressResolver *ar; + char addr[64]; + + if (event == AVAHI_RESOLVER_TIMEOUT) + { + printf ("Callback on HostNameResolver, timed out.\n"); + avahi_host_name_resolver_free (r); + return; + } + client = avahi_host_name_resolver_get_client (r); + ar = avahi_address_resolver_new_a (client, AVAHI_IF_UNSPEC, a, avahi_address_resolver_callback, "omghai6u"); + if (ar) + { + printf ("Succesfully created address resolver object\n"); + } else { + printf ("Failed to create AddressResolver\n"); + } + avahi_address_snprint (&addr, sizeof (addr), a); + printf ("Callback on HostNameResolver, interface (%d), protocol (%d), event (%d), name (%s), address (%s), data (%s)\n", interface, protocol, event, name, addr, (char*)user_data); +} static void test_free_domain_browser(AvahiTimeout *timeout, void* userdata) { AvahiServiceBrowser *b = userdata; - printf ("XXX: freeing domain browser\n"); + printf ("Freeing domain browser\n"); avahi_service_browser_free (b); } static void test_free_entry_group (AvahiTimeout *timeout, void* userdata) { AvahiEntryGroup *g = userdata; - printf ("XXX: freeing entry group\n"); + printf ("Freeing entry group\n"); avahi_entry_group_free (g); } @@ -116,7 +141,7 @@ static void test_entry_group_reset (AvahiTimeout *timeout, void* userdata) { AvahiEntryGroup *g = userdata; - printf ("XXX: resetting entry group\n"); + printf ("Resetting entry group\n"); avahi_entry_group_reset (g); avahi_entry_group_add_service (g, AVAHI_IF_UNSPEC, AF_UNSPEC, "Lathiat's Site", "_http._tcp", "", "", 80, "foo=bar2", NULL); diff --git a/avahi-client/client.c b/avahi-client/client.c index 837d237..1bffc49 100644 --- a/avahi-client/client.c +++ b/avahi-client/client.c @@ -293,6 +293,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); AVAHI_LLIST_HEAD_INIT(AvahiServiceResolver, client->service_resolvers); + AVAHI_LLIST_HEAD_INIT(AvahiHostNameResolver, client->host_name_resolvers); + AVAHI_LLIST_HEAD_INIT(AvahiAddressResolver, client->address_resolvers); if (!(client->bus = avahi_dbus_bus_get(&error)) || dbus_error_is_set (&error)) diff --git a/avahi-client/client.h b/avahi-client/client.h index 4db2aef..c544c44 100644 --- a/avahi-client/client.h +++ b/avahi-client/client.h @@ -69,6 +69,9 @@ typedef struct AvahiServiceResolver AvahiServiceResolver; /** A service resolver object */ typedef struct AvahiHostNameResolver AvahiHostNameResolver; +/** An address resolver object */ +typedef struct AvahiAddressResolver AvahiAddressResolver; + /** States of a client object, a superset of AvahiServerState */ typedef enum { AVAHI_CLIENT_S_INVALID = AVAHI_SERVER_INVALID, @@ -118,6 +121,17 @@ typedef void (*AvahiHostNameResolverCallback) ( const AvahiAddress *a, void *userdata); +/** The function prototype for the callback of an AvahiAddressResolver */ +typedef void (*AvahiAddressResolverCallback) ( + AvahiAddressResolver *r, + AvahiIfIndex interface, + AvahiProtocol protocol, + AvahiResolverEvent event, + AvahiProtocol aprotocol, + const AvahiAddress *a, + const char *name, + void *userdata); + /** Creates a new client instance */ AvahiClient* avahi_client_new (const AvahiPoll *poll_api, AvahiClientCallback callback, void *userdata, int *error); @@ -297,6 +311,32 @@ int avahi_host_name_resolver_free(AvahiHostNameResolver *r); /** Block until the resolving is complete */ int avahi_host_name_resolver_block(AvahiHostNameResolver *r); +/** Create a new address resolver object from an address string. Set protocol to AF_UNSPEC for protocol detection. */ +AvahiAddressResolver * avahi_address_resolver_new( + AvahiClient *client, + AvahiIfIndex interface, + AvahiProtocol protocol, + const char *address, + AvahiAddressResolverCallback callback, + void *userdata); + +/** Create a new address resolver object from an AvahiAddress object */ +AvahiAddressResolver* avahi_address_resolver_new_a( + AvahiClient *client, + AvahiIfIndex interface, + const AvahiAddress *a, + AvahiAddressResolverCallback callback, + void *userdata); + +/** Get the parent client of an AvahiAddressResolver object */ +AvahiClient* avahi_address_resolver_get_client (AvahiAddressResolver *); + +/** Free a AvahiAddressResolver resolver object */ +int avahi_address_resolver_free(AvahiAddressResolver *r); + +/** Block until the resolving is complete */ +int avahi_address_resolver_block(AvahiAddressResolver *r); + #ifndef DOXYGEN_SHOULD_SKIP_THIS AVAHI_C_DECL_END #endif diff --git a/avahi-client/internal.h b/avahi-client/internal.h index 4fc963f..f5401fc 100644 --- a/avahi-client/internal.h +++ b/avahi-client/internal.h @@ -43,6 +43,7 @@ struct AvahiClient { AVAHI_LLIST_HEAD(AvahiServiceTypeBrowser, service_type_browsers); AVAHI_LLIST_HEAD(AvahiServiceResolver, service_resolvers); AVAHI_LLIST_HEAD(AvahiHostNameResolver, host_name_resolvers); + AVAHI_LLIST_HEAD(AvahiAddressResolver, address_resolvers); }; struct AvahiEntryGroup { @@ -95,6 +96,15 @@ struct AvahiHostNameResolver { AVAHI_LLIST_FIELDS(AvahiHostNameResolver, host_name_resolvers); }; +struct AvahiAddressResolver { + char *path; + DBusPendingCall *call; + AvahiClient *client; + AvahiAddressResolverCallback callback; + void *userdata; + AVAHI_LLIST_FIELDS(AvahiAddressResolver, address_resolvers); +}; + int avahi_client_set_errno (AvahiClient *client, int error); int avahi_client_set_dbus_error(AvahiClient *client, DBusError *error); diff --git a/avahi-client/resolver.c b/avahi-client/resolver.c index 2e44351..aaff502 100644 --- a/avahi-client/resolver.c +++ b/avahi-client/resolver.c @@ -55,7 +55,8 @@ service_pending_call_callback(DBusPendingCall *pending, void *userdata) { if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_METHOD_RETURN) { int j; - int32_t interface, protocol, aprotocol; + int32_t interface; + AvahiProtocol protocol, aprotocol; char *name, *type, *domain, *host, *address; uint16_t port; DBusMessageIter iter, sub; @@ -161,7 +162,8 @@ hostname_pending_call_callback(DBusPendingCall *pending, void *userdata) { goto fail; if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_METHOD_RETURN) { - int32_t interface, protocol, aprotocol; + int32_t interface; + AvahiProtocol protocol, aprotocol; char *name, *address; AvahiAddress a; @@ -203,6 +205,65 @@ fail: dbus_error_free (&error); } +static void +address_pending_call_callback(DBusPendingCall *pending, void *userdata) { + AvahiAddressResolver *r = userdata; + DBusMessage *message = 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) { + int32_t interface; + AvahiProtocol protocol, aprotocol; + char *name, *address; + AvahiAddress a; + + if (!dbus_message_get_args( + message, &error, + DBUS_TYPE_INT32, &interface, + DBUS_TYPE_INT32, &protocol, + DBUS_TYPE_INT32, &aprotocol, + DBUS_TYPE_STRING, &address, + DBUS_TYPE_STRING, &name, + DBUS_TYPE_INVALID) || + dbus_error_is_set (&error)) { + fprintf(stderr, "Failed to parse resolver event.\n"); + goto fail; + } + + 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, (AvahiProtocol) aprotocol, &a, name, 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, (AvahiProtocol) 0, NULL, NULL, r->userdata); + } + +fail: + + if (message) + dbus_message_unref(message); + + dbus_error_free (&error); +} + +/* AvahiServiceResolver implementation */ AvahiServiceResolver * avahi_service_resolver_new( AvahiClient *client, AvahiIfIndex interface, @@ -295,8 +356,6 @@ fail: } AvahiClient* avahi_service_resolver_get_client (AvahiServiceResolver *r) { - AvahiClient *client; - assert (r); return r->client; @@ -332,6 +391,8 @@ int avahi_service_resolver_block(AvahiServiceResolver *r) { return AVAHI_OK; } +/* AvahiHostNameResolver implementation */ + AvahiHostNameResolver * avahi_host_name_resolver_new( AvahiClient *client, AvahiIfIndex interface, @@ -434,14 +495,148 @@ int avahi_host_name_resolver_free(AvahiHostNameResolver *r) { } AvahiClient* avahi_host_name_resolver_get_client (AvahiHostNameResolver *r) { + assert (r); + + return r->client; +} + +int avahi_host_name_resolver_block(AvahiHostNameResolver *r) { AvahiClient *client; + assert(r); + client = r->client; + + if (r->call) + dbus_pending_call_block(r->call); + + return AVAHI_OK; +} + +/* AvahiAddressResolver implementation */ + +AvahiAddressResolver * avahi_address_resolver_new_a( + AvahiClient *client, + AvahiIfIndex interface, + const AvahiAddress *a, + AvahiAddressResolverCallback callback, + void *userdata) { + + char addr[64]; + + assert (a); + + avahi_address_snprint (addr, sizeof (addr), a); + + return avahi_address_resolver_new (client, interface, + a->family, addr, + callback, userdata); +} + +AvahiAddressResolver * avahi_address_resolver_new( + AvahiClient *client, + AvahiIfIndex interface, + AvahiProtocol protocol, + const char *address, + AvahiAddressResolverCallback callback, + void *userdata) { + + DBusError error; + AvahiAddressResolver *r; + DBusMessage *message; + int32_t i_interface; + AvahiProtocol i_protocol; + + assert(client); + + 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(AvahiAddressResolver, 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(AvahiAddressResolver, address_resolvers, client->address_resolvers, r); + + if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "ResolveAddress"))) { + avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY); + goto fail; + } + + i_interface = interface; + i_protocol = protocol; + + if (!(dbus_message_append_args( + message, + DBUS_TYPE_INT32, &i_interface, + DBUS_TYPE_INT32, &i_protocol, + DBUS_TYPE_STRING, &address, + 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, address_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_address_resolver_free(r); + + if (message) + dbus_message_unref(message); + + return NULL; + +} + +AvahiClient* avahi_address_resolver_get_client (AvahiAddressResolver *r) { assert (r); return r->client; } -int avahi_host_name_resolver_block(AvahiHostNameResolver *r) { +int avahi_address_resolver_free(AvahiAddressResolver *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(AvahiAddressResolver, address_resolvers, client->address_resolvers, r); + + avahi_free(r); + + return AVAHI_OK; +} + +int avahi_address_resolver_block(AvahiAddressResolver *r) { AvahiClient *client; assert(r);