From: Lennart Poettering Date: Tue, 23 Aug 2005 18:00:47 +0000 (+0000) Subject: * Implement asynchronous address resolver X-Git-Url: https://git.meshlink.io/?a=commitdiff_plain;h=d51775dec66c56ed2512568ac6bda2b87dc70814;p=catta * Implement asynchronous address resolver git-svn-id: file:///home/lennart/svn/public/avahi/trunk@425 941a03a8-eaeb-0310-b9a0-b1bbd8fe43fe --- diff --git a/avahi-common/dbus.h b/avahi-common/dbus.h index bcbbb99..cdfef4d 100644 --- a/avahi-common/dbus.h +++ b/avahi-common/dbus.h @@ -38,6 +38,9 @@ AVAHI_C_DECL_BEGIN #define AVAHI_DBUS_INTERFACE_DOMAIN_BROWSER AVAHI_DBUS_NAME".DomainBrowser" #define AVAHI_DBUS_INTERFACE_SERVICE_TYPE_BROWSER AVAHI_DBUS_NAME".ServiceTypeBrowser" #define AVAHI_DBUS_INTERFACE_SERVICE_BROWSER AVAHI_DBUS_NAME".ServiceBrowser" +#define AVAHI_DBUS_INTERFACE_ADDRESS_RESOLVER AVAHI_DBUS_NAME".AddressResolver" +#define AVAHI_DBUS_INTERFACE_HOST_NAME_RESOLVER AVAHI_DBUS_NAME".HostNameResolver" +#define AVAHI_DBUS_INTERFACE_SERVICE_RESOLVER AVAHI_DBUS_NAME".ServiceResolver" #define AVAHI_DBUS_ERR_FAILURE "org.freedesktop.Avahi.Failure" #define AVAHI_DBUS_ERR_BAD_STATE "org.freedesktop.Avahi.BadStateError" diff --git a/avahi-daemon/Server.introspect b/avahi-daemon/Server.introspect index da1b6fb..9ce3259 100644 --- a/avahi-daemon/Server.introspect +++ b/avahi-daemon/Server.introspect @@ -127,7 +127,6 @@ - diff --git a/avahi-daemon/dbus-protocol.c b/avahi-daemon/dbus-protocol.c index 36ad690..b998d13 100644 --- a/avahi-daemon/dbus-protocol.c +++ b/avahi-daemon/dbus-protocol.c @@ -198,7 +198,7 @@ static void entry_group_free(EntryGroupInfo *i) { assert(i->client->n_objects >= 0); avahi_free(i); - } +} static void sync_host_name_resolver_free(SyncHostNameResolverInfo *i) { assert(i); @@ -838,7 +838,7 @@ fail: return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; } -static void host_name_resolver_callback(AvahiSHostNameResolver *r, AvahiIfIndex interface, AvahiProtocol protocol, AvahiResolverEvent event, const char *host_name, const AvahiAddress *a, void* userdata) { +static void sync_host_name_resolver_callback(AvahiSHostNameResolver *r, AvahiIfIndex interface, AvahiProtocol protocol, AvahiResolverEvent event, const char *host_name, const AvahiAddress *a, void* userdata) { SyncHostNameResolverInfo *i = userdata; assert(r); @@ -878,7 +878,7 @@ static void host_name_resolver_callback(AvahiSHostNameResolver *r, AvahiIfIndex sync_host_name_resolver_free(i); } -static void address_resolver_callback(AvahiSAddressResolver *r, AvahiIfIndex interface, AvahiProtocol protocol, AvahiResolverEvent event, const AvahiAddress *address, const char *host_name, void* userdata) { +static void sync_address_resolver_callback(AvahiSAddressResolver *r, AvahiIfIndex interface, AvahiProtocol protocol, AvahiResolverEvent event, const AvahiAddress *address, const char *host_name, void* userdata) { SyncAddressResolverInfo *i = userdata; assert(r); @@ -1127,7 +1127,7 @@ static void service_browser_callback(AvahiSServiceBrowser *b, AvahiIfIndex inter dbus_message_unref(m); } -static void service_resolver_callback( +static void sync_service_resolver_callback( AvahiSServiceResolver *r, AvahiIfIndex interface, AvahiProtocol protocol, @@ -1202,6 +1202,89 @@ static void service_resolver_callback( sync_service_resolver_free(i); } +static void async_address_resolver_callback(AvahiSAddressResolver *r, AvahiIfIndex interface, AvahiProtocol protocol, AvahiResolverEvent event, const AvahiAddress *address, const char *host_name, void* userdata) { + AsyncAddressResolverInfo *i = userdata; + DBusMessage *reply; + + assert(r); + assert(address); + assert(i); + + if (event == AVAHI_RESOLVER_FOUND) { + char t[256], *pt = t; + int32_t i_interface, i_protocol, i_aprotocol; + + assert(host_name); + avahi_address_snprint(t, sizeof(t), address); + + i_interface = (int32_t) interface; + i_protocol = (int32_t) protocol; + i_aprotocol = (int32_t) address->family; + + reply = dbus_message_new_signal(i->path, AVAHI_DBUS_INTERFACE_ADDRESS_RESOLVER, "Found"); + dbus_message_append_args( + reply, + DBUS_TYPE_INT32, &i_interface, + DBUS_TYPE_INT32, &i_protocol, + DBUS_TYPE_INT32, &i_aprotocol, + DBUS_TYPE_STRING, &pt, + DBUS_TYPE_STRING, &host_name, + DBUS_TYPE_INVALID); + + } else { + assert(event == AVAHI_RESOLVER_TIMEOUT); + + reply = dbus_message_new_signal(i->path, AVAHI_DBUS_INTERFACE_ADDRESS_RESOLVER, "Timeout"); + } + + dbus_connection_send(server->bus, reply, NULL); + dbus_message_unref(reply); +} + +static DBusHandlerResult msg_async_address_resolver_impl(DBusConnection *c, DBusMessage *m, void *userdata) { + DBusError error; + AsyncAddressResolverInfo *i = userdata; + + assert(c); + assert(m); + assert(i); + + dbus_error_init(&error); + + avahi_log_debug("dbus: interface=%s, path=%s, member=%s", + dbus_message_get_interface(m), + dbus_message_get_path(m), + dbus_message_get_member(m)); + + /* Introspection */ + if (dbus_message_is_method_call(m, DBUS_INTERFACE_INTROSPECTABLE, "Introspect")) + return handle_introspect(c, m, "AddressResolver.Introspect"); + + /* Access control */ + if (strcmp(dbus_message_get_sender(m), i->client->name)) + return respond_error(c, m, AVAHI_ERR_ACCESS_DENIED, NULL); + + if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ADDRESS_RESOLVER, "Free")) { + + if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) { + avahi_log_warn("Error parsing AddressResolver::Free message"); + goto fail; + } + + async_address_resolver_free(i); + return respond_ok(c, m); + + } + + avahi_log_warn("Missed message %s::%s()", dbus_message_get_interface(m), dbus_message_get_member(m)); + +fail: + if (dbus_error_is_set(&error)) + dbus_error_free(&error); + + return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; +} + static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void *userdata) { DBusError error; @@ -1435,7 +1518,7 @@ static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void AVAHI_LLIST_PREPEND(SyncHostNameResolverInfo, sync_host_name_resolvers, client->sync_host_name_resolvers, i); client->n_objects++; - if (!(i->host_name_resolver = avahi_s_host_name_resolver_new(avahi_server, (AvahiIfIndex) interface, (AvahiProtocol) protocol, name, (AvahiProtocol) aprotocol, host_name_resolver_callback, i))) { + if (!(i->host_name_resolver = avahi_s_host_name_resolver_new(avahi_server, (AvahiIfIndex) interface, (AvahiProtocol) protocol, name, (AvahiProtocol) aprotocol, sync_host_name_resolver_callback, i))) { sync_host_name_resolver_free(i); return respond_error(c, m, avahi_server_errno(avahi_server), NULL); } @@ -1478,7 +1561,7 @@ static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void AVAHI_LLIST_PREPEND(SyncAddressResolverInfo, sync_address_resolvers, client->sync_address_resolvers, i); client->n_objects++; - if (!(i->address_resolver = avahi_s_address_resolver_new(avahi_server, (AvahiIfIndex) interface, (AvahiProtocol) protocol, &a, address_resolver_callback, i))) { + if (!(i->address_resolver = avahi_s_address_resolver_new(avahi_server, (AvahiIfIndex) interface, (AvahiProtocol) protocol, &a, sync_address_resolver_callback, i))) { sync_address_resolver_free(i); return respond_error(c, m, avahi_server_errno(avahi_server), NULL); } @@ -1592,7 +1675,7 @@ static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void dbus_connection_register_object_path(c, i->path, &vtable, i); return respond_path(c, m, i->path); - } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "ServiceBrowserNew")) { + } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "ServiceBrowserNew")) { Client *client; ServiceBrowserInfo *i; static const DBusObjectPathVTable vtable = { @@ -1684,13 +1767,71 @@ static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void AVAHI_LLIST_PREPEND(SyncServiceResolverInfo, sync_service_resolvers, client->sync_service_resolvers, i); client->n_objects++; - if (!(i->service_resolver = avahi_s_service_resolver_new(avahi_server, (AvahiIfIndex) interface, (AvahiProtocol) protocol, name, type, domain, (AvahiProtocol) aprotocol, service_resolver_callback, i))) { + if (!(i->service_resolver = avahi_s_service_resolver_new(avahi_server, (AvahiIfIndex) interface, (AvahiProtocol) protocol, name, type, domain, (AvahiProtocol) aprotocol, sync_service_resolver_callback, i))) { sync_service_resolver_free(i); return respond_error(c, m, avahi_server_errno(avahi_server), NULL); } return DBUS_HANDLER_RESULT_HANDLED; - } + } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "ServiceResolverNew")) { + + } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "HostNameResolverNew")) { + + } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "AddressResolverNew")) { + + Client *client; + static const DBusObjectPathVTable vtable = { + NULL, + msg_async_address_resolver_impl, + NULL, + NULL, + NULL, + NULL + }; + + int32_t interface, protocol; + char *address; + AsyncAddressResolverInfo *i; + AvahiAddress a; + + if (!dbus_message_get_args( + m, &error, + DBUS_TYPE_INT32, &interface, + DBUS_TYPE_INT32, &protocol, + DBUS_TYPE_STRING, &address, + DBUS_TYPE_INVALID) || !address) { + avahi_log_warn("Error parsing Server::AddressResolverNew message"); + goto fail; + } + + if (!avahi_address_parse(address, AVAHI_PROTO_UNSPEC, &a)) + return respond_error(c, m, AVAHI_ERR_INVALID_ADDRESS, NULL); + + if (!(client = client_get(dbus_message_get_sender(m), TRUE))) { + avahi_log_warn(__FILE__": Too many clients, client request failed."); + return respond_error(c, m, AVAHI_ERR_TOO_MANY_CLIENTS, NULL); + } + + if (client->n_objects >= MAX_OBJECTS_PER_CLIENT) { + avahi_log_warn(__FILE__": Too many objects for client '%s', client request failed.", client->name); + return respond_error(c, m, AVAHI_ERR_TOO_MANY_OBJECTS, NULL); + } + + i = avahi_new(AsyncAddressResolverInfo, 1); + i->id = ++client->current_id; + i->client = client; + i->path = avahi_strdup_printf("/Client%u/AddressResolver%u", client->id, i->id); + AVAHI_LLIST_PREPEND(AsyncAddressResolverInfo, async_address_resolvers, client->async_address_resolvers, i); + client->n_objects++; + + if (!(i->address_resolver = avahi_s_address_resolver_new(avahi_server, (AvahiIfIndex) interface, (AvahiProtocol) protocol, &a, async_address_resolver_callback, i))) { + async_address_resolver_free(i); + return respond_error(c, m, avahi_server_errno(avahi_server), NULL); + } + + dbus_connection_register_object_path(c, i->path, &vtable, i); + return respond_path(c, m, i->path); + } avahi_log_warn("Missed message %s::%s()", dbus_message_get_interface(m), dbus_message_get_member(m));