X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=avahi-daemon%2Fdbus-protocol.c;h=c30873eb9fca2cddd5b7af7ee4660662301bfec7;hb=b789f76475170c84fce85383279aa8f683ad1ff9;hp=c6a60df22d80c14826090b8dcfeef7f08525f939;hpb=3a625af272ee8c6f878ba4d44ed1c741a1582395;p=catta diff --git a/avahi-daemon/dbus-protocol.c b/avahi-daemon/dbus-protocol.c index c6a60df..c30873e 100644 --- a/avahi-daemon/dbus-protocol.c +++ b/avahi-daemon/dbus-protocol.c @@ -23,14 +23,18 @@ #include #endif -#include #include +#include +#include +#include +#include +#include #define DBUS_API_SUBJECT_TO_CHANGE #include #include -#include +#include #include #include #include @@ -52,10 +56,12 @@ typedef struct ServiceResolverInfo ServiceResolverInfo; #define MAX_OBJECTS_PER_CLIENT 50 #define MAX_ENTRIES_PER_ENTRY_GROUP 20 +/* #define VALGRIND_WORKAROUND */ + struct EntryGroupInfo { guint id; Client *client; - AvahiEntryGroup *entry_group; + AvahiSEntryGroup *entry_group; gchar *path; gint n_entries; @@ -65,7 +71,7 @@ struct EntryGroupInfo { struct HostNameResolverInfo { Client *client; - AvahiHostNameResolver *host_name_resolver; + AvahiSHostNameResolver *host_name_resolver; DBusMessage *message; AVAHI_LLIST_FIELDS(HostNameResolverInfo, host_name_resolvers); @@ -73,7 +79,7 @@ struct HostNameResolverInfo { struct AddressResolverInfo { Client *client; - AvahiAddressResolver *address_resolver; + AvahiSAddressResolver *address_resolver; DBusMessage *message; AVAHI_LLIST_FIELDS(AddressResolverInfo, address_resolvers); @@ -82,7 +88,7 @@ struct AddressResolverInfo { struct DomainBrowserInfo { guint id; Client *client; - AvahiDomainBrowser *domain_browser; + AvahiSDomainBrowser *domain_browser; gchar *path; AVAHI_LLIST_FIELDS(DomainBrowserInfo, domain_browsers); @@ -91,7 +97,7 @@ struct DomainBrowserInfo { struct ServiceTypeBrowserInfo { guint id; Client *client; - AvahiServiceTypeBrowser *service_type_browser; + AvahiSServiceTypeBrowser *service_type_browser; gchar *path; AVAHI_LLIST_FIELDS(ServiceTypeBrowserInfo, service_type_browsers); @@ -100,7 +106,7 @@ struct ServiceTypeBrowserInfo { struct ServiceBrowserInfo { guint id; Client *client; - AvahiServiceBrowser *service_browser; + AvahiSServiceBrowser *service_browser; gchar *path; AVAHI_LLIST_FIELDS(ServiceBrowserInfo, service_browsers); @@ -108,7 +114,7 @@ struct ServiceBrowserInfo { struct ServiceResolverInfo { Client *client; - AvahiServiceResolver *service_resolver; + AvahiSServiceResolver *service_resolver; DBusMessage *message; AVAHI_LLIST_FIELDS(ServiceResolverInfo, service_resolvers); @@ -143,7 +149,7 @@ static void entry_group_free(EntryGroupInfo *i) { g_assert(i); if (i->entry_group) - avahi_entry_group_free(i->entry_group); + avahi_s_entry_group_free(i->entry_group); dbus_connection_unregister_object_path(server->bus, i->path); g_free(i->path); AVAHI_LLIST_REMOVE(EntryGroupInfo, entry_groups, i->client->entry_groups, i); @@ -158,7 +164,7 @@ static void host_name_resolver_free(HostNameResolverInfo *i) { g_assert(i); if (i->host_name_resolver) - avahi_host_name_resolver_free(i->host_name_resolver); + avahi_s_host_name_resolver_free(i->host_name_resolver); dbus_message_unref(i->message); AVAHI_LLIST_REMOVE(HostNameResolverInfo, host_name_resolvers, i->client->host_name_resolvers, i); @@ -172,7 +178,7 @@ static void address_resolver_free(AddressResolverInfo *i) { g_assert(i); if (i->address_resolver) - avahi_address_resolver_free(i->address_resolver); + avahi_s_address_resolver_free(i->address_resolver); dbus_message_unref(i->message); AVAHI_LLIST_REMOVE(AddressResolverInfo, address_resolvers, i->client->address_resolvers, i); @@ -186,7 +192,7 @@ static void domain_browser_free(DomainBrowserInfo *i) { g_assert(i); if (i->domain_browser) - avahi_domain_browser_free(i->domain_browser); + avahi_s_domain_browser_free(i->domain_browser); dbus_connection_unregister_object_path(server->bus, i->path); g_free(i->path); AVAHI_LLIST_REMOVE(DomainBrowserInfo, domain_browsers, i->client->domain_browsers, i); @@ -201,7 +207,7 @@ static void service_type_browser_free(ServiceTypeBrowserInfo *i) { g_assert(i); if (i->service_type_browser) - avahi_service_type_browser_free(i->service_type_browser); + avahi_s_service_type_browser_free(i->service_type_browser); dbus_connection_unregister_object_path(server->bus, i->path); g_free(i->path); AVAHI_LLIST_REMOVE(ServiceTypeBrowserInfo, service_type_browsers, i->client->service_type_browsers, i); @@ -216,7 +222,7 @@ static void service_browser_free(ServiceBrowserInfo *i) { g_assert(i); if (i->service_browser) - avahi_service_browser_free(i->service_browser); + avahi_s_service_browser_free(i->service_browser); dbus_connection_unregister_object_path(server->bus, i->path); g_free(i->path); AVAHI_LLIST_REMOVE(ServiceBrowserInfo, service_browsers, i->client->service_browsers, i); @@ -231,7 +237,7 @@ static void service_resolver_free(ServiceResolverInfo *i) { g_assert(i); if (i->service_resolver) - avahi_service_resolver_free(i->service_resolver); + avahi_s_service_resolver_free(i->service_resolver); dbus_message_unref(i->message); AVAHI_LLIST_REMOVE(ServiceResolverInfo, service_resolvers, i->client->service_resolvers, i); @@ -315,10 +321,41 @@ static Client *client_get(const gchar *name, gboolean create) { return client; } -static DBusHandlerResult respond_error(DBusConnection *c, DBusMessage *m, const gchar *error, const gchar *text) { +static DBusHandlerResult respond_error(DBusConnection *c, DBusMessage *m, gint error, const gchar *text) { DBusMessage *reply; - reply = dbus_message_new_error(m, error, text); + const gchar * const table[- AVAHI_ERR_MAX] = { + NULL, /* OK */ + AVAHI_DBUS_ERR_FAILURE, + AVAHI_DBUS_ERR_BAD_STATE, + AVAHI_DBUS_ERR_INVALID_HOST_NAME, + AVAHI_DBUS_ERR_INVALID_DOMAIN_NAME, + AVAHI_DBUS_ERR_NO_NETWORK, + AVAHI_DBUS_ERR_INVALID_TTL, + AVAHI_DBUS_ERR_IS_PATTERN, + AVAHI_DBUS_ERR_LOCAL_COLLISION, + AVAHI_DBUS_ERR_INVALID_RECORD, + AVAHI_DBUS_ERR_INVALID_SERVICE_NAME, + AVAHI_DBUS_ERR_INVALID_SERVICE_TYPE, + AVAHI_DBUS_ERR_INVALID_PORT, + AVAHI_DBUS_ERR_INVALID_KEY, + AVAHI_DBUS_ERR_INVALID_ADDRESS, + AVAHI_DBUS_ERR_TIMEOUT, + AVAHI_DBUS_ERR_TOO_MANY_CLIENTS, + AVAHI_DBUS_ERR_TOO_MANY_OBJECTS, + AVAHI_DBUS_ERR_TOO_MANY_ENTRIES, + AVAHI_DBUS_ERR_OS, + AVAHI_DBUS_ERR_ACCESS_DENIED, + AVAHI_DBUS_ERR_INVALID_OPERATION, + AVAHI_DBUS_ERR_DBUS_ERROR, + AVAHI_DBUS_ERR_NOT_CONNECTED, + AVAHI_DBUS_ERR_NO_MEMORY + }; + + g_assert(-error > -AVAHI_OK); + g_assert(-error < -AVAHI_ERR_MAX); + + reply = dbus_message_new_error(m, table[-error], text ? text : avahi_strerror(error)); dbus_connection_send(c, reply, NULL); dbus_message_unref(reply); @@ -336,6 +373,17 @@ static DBusHandlerResult respond_string(DBusConnection *c, DBusMessage *m, const return DBUS_HANDLER_RESULT_HANDLED; } +static DBusHandlerResult respond_int32(DBusConnection *c, DBusMessage *m, gint32 i) { + DBusMessage *reply; + + reply = dbus_message_new_method_return(m); + dbus_message_append_args(reply, DBUS_TYPE_INT32, &i, DBUS_TYPE_INVALID); + dbus_connection_send(c, reply, NULL); + dbus_message_unref(reply); + + return DBUS_HANDLER_RESULT_HANDLED; +} + static DBusHandlerResult respond_ok(DBusConnection *c, DBusMessage *m) { DBusMessage *reply; @@ -452,8 +500,7 @@ fail: return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; } - -static void entry_group_callback(AvahiServer *s, AvahiEntryGroup *g, AvahiEntryGroupState state, gpointer userdata) { +static void entry_group_callback(AvahiServer *s, AvahiSEntryGroup *g, AvahiEntryGroupState state, gpointer userdata) { EntryGroupInfo *i = userdata; DBusMessage *m; gint32 t; @@ -491,7 +538,7 @@ static DBusHandlerResult msg_entry_group_impl(DBusConnection *c, DBusMessage *m, /* Access control */ if (strcmp(dbus_message_get_sender(m), i->client->name)) - return respond_error(c, m, DBUS_ERROR_ACCESS_DENIED, NULL); + return respond_error(c, m, AVAHI_ERR_ACCESS_DENIED, NULL); if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "Free")) { @@ -510,7 +557,7 @@ static DBusHandlerResult msg_entry_group_impl(DBusConnection *c, DBusMessage *m, goto fail; } - avahi_entry_group_commit(i->entry_group); + avahi_s_entry_group_commit(i->entry_group); return respond_ok(c, m); @@ -521,7 +568,7 @@ static DBusHandlerResult msg_entry_group_impl(DBusConnection *c, DBusMessage *m, goto fail; } - avahi_entry_group_reset(i->entry_group); + avahi_s_entry_group_reset(i->entry_group); return respond_ok(c, m); } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "IsEmpty")) { @@ -533,39 +580,33 @@ static DBusHandlerResult msg_entry_group_impl(DBusConnection *c, DBusMessage *m, goto fail; } - b = avahi_entry_group_is_empty(i->entry_group); - + b = !!avahi_s_entry_group_is_empty(i->entry_group); + reply = dbus_message_new_method_return(m); - dbus_message_append_args(m, DBUS_TYPE_BOOLEAN, &b, DBUS_TYPE_INVALID); + dbus_message_append_args(reply, DBUS_TYPE_BOOLEAN, &b, DBUS_TYPE_INVALID); dbus_connection_send(c, reply, NULL); dbus_message_unref(reply); return DBUS_HANDLER_RESULT_HANDLED; } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "GetState")) { - DBusMessage *reply; - gint32 t; - + AvahiEntryGroupState state; + if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) { avahi_log_warn("Error parsing EntryGroup::GetState message"); goto fail; } - t = (gint32) avahi_entry_group_get_state(i->entry_group); - reply = dbus_message_new_method_return(m); - dbus_message_append_args(reply, DBUS_TYPE_INT32, &t, DBUS_TYPE_INVALID); - dbus_connection_send(c, reply, NULL); - dbus_message_unref(reply); - - return DBUS_HANDLER_RESULT_HANDLED; + state = avahi_s_entry_group_get_state(i->entry_group); + return respond_int32(c, m, (gint32) state); } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "AddService")) { gint32 interface, protocol; gchar *type, *name, *domain, *host; guint16 port; - gchar **txt = NULL; - gint txt_len; AvahiStringList *strlst; + DBusMessageIter iter, sub; + int j; if (!dbus_message_get_args( m, &error, @@ -576,21 +617,53 @@ static DBusHandlerResult msg_entry_group_impl(DBusConnection *c, DBusMessage *m, DBUS_TYPE_STRING, &domain, DBUS_TYPE_STRING, &host, DBUS_TYPE_UINT16, &port, - DBUS_TYPE_ARRAY, DBUS_TYPE_STRING, &txt, &txt_len, - DBUS_TYPE_INVALID) || !type || !*type || !name || !*name || !port) { + DBUS_TYPE_INVALID) || !type || !name) { avahi_log_warn("Error parsing EntryGroup::AddService message"); goto fail; } + dbus_message_iter_init(m, &iter); + + for (j = 0; j < 7; 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) { + avahi_log_warn("Error parsing EntryGroup::AddService message 2"); + goto fail; + } + + strlst = NULL; + dbus_message_iter_recurse(&iter, &sub); + + for (;;) { + DBusMessageIter sub2; + int at, n; + guint8 *k; + + if ((at = dbus_message_iter_get_arg_type(&sub)) == DBUS_TYPE_INVALID) + break; + + g_assert(at == DBUS_TYPE_ARRAY); + + if (dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_BYTE) { + avahi_log_warn("Error parsing EntryGroup::AddService 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); + } + if (i->n_entries >= MAX_ENTRIES_PER_ENTRY_GROUP) { + avahi_string_list_free(strlst); avahi_log_warn("Too many entries per entry group, client request failed."); - dbus_free_string_array(txt); - return respond_error(c, m, AVAHI_DBUS_ERROR_TOO_MANY_ENTRIES, NULL); + return respond_error(c, m, AVAHI_ERR_TOO_MANY_ENTRIES, NULL); } - strlst = avahi_string_list_new_from_array((const gchar**) txt, txt_len); - dbus_free_string_array(txt); - if (domain && !*domain) domain = NULL; @@ -598,9 +671,8 @@ static DBusHandlerResult msg_entry_group_impl(DBusConnection *c, DBusMessage *m, host = NULL; if (avahi_server_add_service_strlst(avahi_server, i->entry_group, (AvahiIfIndex) interface, (AvahiProtocol) protocol, name, type, domain, host, port, strlst) < 0) { - avahi_log_warn("Failed to add service: %s", name); avahi_string_list_free(strlst); - return respond_error(c, m, AVAHI_DBUS_ERROR_INVALID_SERVICE, NULL); + return respond_error(c, m, avahi_server_errno(avahi_server), NULL); } else i->n_entries ++; @@ -619,25 +691,23 @@ static DBusHandlerResult msg_entry_group_impl(DBusConnection *c, DBusMessage *m, DBUS_TYPE_INT32, &protocol, DBUS_TYPE_STRING, &name, DBUS_TYPE_STRING, &address, - DBUS_TYPE_INVALID) || !name || !*name || !address || !*address) { + DBUS_TYPE_INVALID) || !name || !address) { avahi_log_warn("Error parsing EntryGroup::AddAddress message"); goto fail; } if (i->n_entries >= MAX_ENTRIES_PER_ENTRY_GROUP) { avahi_log_warn("Too many entries per entry group, client request failed."); - return respond_error(c, m, AVAHI_DBUS_ERROR_TOO_MANY_ENTRIES, NULL); + return respond_error(c, m, AVAHI_ERR_TOO_MANY_ENTRIES, NULL); } if (!(avahi_address_parse(address, AVAHI_PROTO_UNSPEC, &a))) { - avahi_log_warn("Error parsing address data"); - return respond_error(c, m, AVAHI_DBUS_ERROR_INVALID_ADDRESS, NULL); + return respond_error(c, m, AVAHI_ERR_INVALID_ADDRESS, NULL); } - if (avahi_server_add_address(avahi_server, i->entry_group, (AvahiIfIndex) interface, (AvahiProtocol) protocol, 0, name, &a) < 0) { - avahi_log_warn("Failed to add service: %s", name); - return respond_error(c, m, AVAHI_DBUS_ERROR_INVALID_ADDRESS, NULL); - } else + if (avahi_server_add_address(avahi_server, i->entry_group, (AvahiIfIndex) interface, (AvahiProtocol) protocol, 0, name, &a) < 0) + return respond_error(c, m, avahi_server_errno(avahi_server), NULL); + else i->n_entries ++; return respond_ok(c, m); @@ -652,9 +722,8 @@ fail: return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; } -static void host_name_resolver_callback(AvahiHostNameResolver *r, AvahiIfIndex interface, AvahiProtocol protocol, AvahiResolverEvent event, const gchar *host_name, const AvahiAddress *a, gpointer userdata) { +static void host_name_resolver_callback(AvahiSHostNameResolver *r, AvahiIfIndex interface, AvahiProtocol protocol, AvahiResolverEvent event, const gchar *host_name, const AvahiAddress *a, gpointer userdata) { HostNameResolverInfo *i = userdata; - DBusMessage *reply; g_assert(r); g_assert(host_name); @@ -663,6 +732,7 @@ static void host_name_resolver_callback(AvahiHostNameResolver *r, AvahiIfIndex i if (event == AVAHI_RESOLVER_FOUND) { char t[256], *pt = t; gint32 i_interface, i_protocol, i_aprotocol; + DBusMessage *reply; g_assert(a); avahi_address_snprint(t, sizeof(t), a); @@ -681,20 +751,19 @@ static void host_name_resolver_callback(AvahiHostNameResolver *r, AvahiIfIndex i DBUS_TYPE_STRING, &pt, DBUS_TYPE_INVALID); + dbus_connection_send(server->bus, reply, NULL); + dbus_message_unref(reply); } else { g_assert(event == AVAHI_RESOLVER_TIMEOUT); - reply = dbus_message_new_error(i->message, AVAHI_DBUS_ERROR_TIMEOUT, NULL); - } - dbus_connection_send(server->bus, reply, NULL); - dbus_message_unref(reply); + respond_error(server->bus, i->message, AVAHI_ERR_TIMEOUT, NULL); + } host_name_resolver_free(i); } -static void address_resolver_callback(AvahiAddressResolver *r, AvahiIfIndex interface, AvahiProtocol protocol, AvahiResolverEvent event, const AvahiAddress *address, const gchar *host_name, gpointer userdata) { +static void address_resolver_callback(AvahiSAddressResolver *r, AvahiIfIndex interface, AvahiProtocol protocol, AvahiResolverEvent event, const AvahiAddress *address, const gchar *host_name, gpointer userdata) { AddressResolverInfo *i = userdata; - DBusMessage *reply; g_assert(r); g_assert(address); @@ -703,6 +772,7 @@ static void address_resolver_callback(AvahiAddressResolver *r, AvahiIfIndex inte if (event == AVAHI_RESOLVER_FOUND) { char t[256], *pt = t; gint32 i_interface, i_protocol, i_aprotocol; + DBusMessage *reply; g_assert(host_name); avahi_address_snprint(t, sizeof(t), address); @@ -721,14 +791,13 @@ static void address_resolver_callback(AvahiAddressResolver *r, AvahiIfIndex inte DBUS_TYPE_STRING, &host_name, DBUS_TYPE_INVALID); + dbus_connection_send(server->bus, reply, NULL); + dbus_message_unref(reply); } else { g_assert(event == AVAHI_RESOLVER_TIMEOUT); - reply = dbus_message_new_error(i->message, AVAHI_DBUS_ERROR_TIMEOUT, NULL); + respond_error(server->bus, i->message, AVAHI_ERR_TIMEOUT, NULL); } - dbus_connection_send(server->bus, reply, NULL); - dbus_message_unref(reply); - address_resolver_free(i); } @@ -753,7 +822,7 @@ static DBusHandlerResult msg_domain_browser_impl(DBusConnection *c, DBusMessage /* Access control */ if (strcmp(dbus_message_get_sender(m), i->client->name)) - return respond_error(c, m, DBUS_ERROR_ACCESS_DENIED, NULL); + return respond_error(c, m, AVAHI_ERR_ACCESS_DENIED, NULL); if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_DOMAIN_BROWSER, "Free")) { @@ -776,7 +845,7 @@ fail: return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; } -static void domain_browser_callback(AvahiDomainBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const gchar *domain, gpointer userdata) { +static void domain_browser_callback(AvahiSDomainBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const gchar *domain, gpointer userdata) { DomainBrowserInfo *i = userdata; DBusMessage *m; gint32 i_interface, i_protocol; @@ -821,7 +890,7 @@ static DBusHandlerResult msg_service_type_browser_impl(DBusConnection *c, DBusMe /* Access control */ if (strcmp(dbus_message_get_sender(m), i->client->name)) - return respond_error(c, m, DBUS_ERROR_ACCESS_DENIED, NULL); + return respond_error(c, m, AVAHI_ERR_ACCESS_DENIED, NULL); if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVICE_TYPE_BROWSER, "Free")) { @@ -844,7 +913,7 @@ fail: return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; } -static void service_type_browser_callback(AvahiServiceTypeBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const gchar *type, const gchar *domain, gpointer userdata) { +static void service_type_browser_callback(AvahiSServiceTypeBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const gchar *type, const gchar *domain, gpointer userdata) { ServiceTypeBrowserInfo *i = userdata; DBusMessage *m; gint32 i_interface, i_protocol; @@ -887,11 +956,11 @@ static DBusHandlerResult msg_service_browser_impl(DBusConnection *c, DBusMessage /* Introspection */ if (dbus_message_is_method_call(m, DBUS_INTERFACE_INTROSPECTABLE, "Introspect")) - return handle_introspect(c, m, "ServiceBrowser.introspect"); + return handle_introspect(c, m, "ServiceBrowser.Introspect"); /* Access control */ if (strcmp(dbus_message_get_sender(m), i->client->name)) - return respond_error(c, m, DBUS_ERROR_ACCESS_DENIED, NULL); + return respond_error(c, m, AVAHI_ERR_ACCESS_DENIED, NULL); if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVICE_BROWSER, "Free")) { @@ -914,7 +983,7 @@ fail: return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; } -static void service_browser_callback(AvahiServiceBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const gchar *name, const gchar *type, const gchar *domain, gpointer userdata) { +static void service_browser_callback(AvahiSServiceBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const gchar *name, const gchar *type, const gchar *domain, gpointer userdata) { ServiceBrowserInfo *i = userdata; DBusMessage *m; gint32 i_interface, i_protocol; @@ -943,7 +1012,7 @@ static void service_browser_callback(AvahiServiceBrowser *b, AvahiIfIndex interf } static void service_resolver_callback( - AvahiServiceResolver *r, + AvahiSServiceResolver *r, AvahiIfIndex interface, AvahiProtocol protocol, AvahiResolverEvent event, @@ -957,7 +1026,6 @@ static void service_resolver_callback( gpointer userdata) { ServiceResolverInfo *i = userdata; - DBusMessage *reply; g_assert(r); g_assert(i); @@ -965,9 +1033,10 @@ static void service_resolver_callback( if (event == AVAHI_RESOLVER_FOUND) { char t[256], *pt = t; gint32 i_interface, i_protocol, i_aprotocol; - gchar **array; guint n, j; AvahiStringList *p; + DBusMessage *reply; + DBusMessageIter iter, sub; g_assert(host_name); @@ -978,12 +1047,6 @@ static void service_resolver_callback( i_protocol = (gint32) protocol; i_aprotocol = (gint32) a->family; - array = g_new(gchar*, (n = avahi_string_list_length(txt))); - - /** FIXME: DBUS doesn't support strings that include NUL bytes (?) */ - for (p = txt, j = n-1; p; p = p->next, j--) - array[j] = g_strndup((gchar*) p->text, p->size); - reply = dbus_message_new_method_return(i->message); dbus_message_append_args( reply, @@ -996,24 +1059,33 @@ static void service_resolver_callback( DBUS_TYPE_INT32, &i_aprotocol, DBUS_TYPE_STRING, &pt, DBUS_TYPE_UINT16, &port, - DBUS_TYPE_ARRAY, DBUS_TYPE_STRING, &array, n, DBUS_TYPE_INVALID); - for (j = 0; j < n; j++) - g_free(array[j]); + dbus_message_iter_init_append(reply, &iter); + dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "ay", &sub); + for (p = txt, j = n-1; p; p = p->next, j--) { + 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(server->bus, reply, NULL); + dbus_message_unref(reply); } else { g_assert(event == AVAHI_RESOLVER_TIMEOUT); - reply = dbus_message_new_error(i->message, AVAHI_DBUS_ERROR_TIMEOUT, NULL); - } - dbus_connection_send(server->bus, reply, NULL); - dbus_message_unref(reply); + respond_error(server->bus, i->message, AVAHI_ERR_TIMEOUT, NULL); + } service_resolver_free(i); } - static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void *userdata) { DBusError error; @@ -1064,27 +1136,89 @@ static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void return respond_string(c, m, PACKAGE_STRING); } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetState")) { - DBusMessage *reply; - gint32 s; - + AvahiServerState state; + if (!(dbus_message_get_args(m, &error, DBUS_TYPE_INVALID))) { avahi_log_warn("Error parsing Server::GetState message"); goto fail; } + + state = avahi_server_get_state(avahi_server); + return respond_int32(c, m, (gint32) state); - s = (gint32) avahi_server_get_state(avahi_server); + } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetNetworkInterfaceNameByIndex")) { + gint32 idx; + int fd; + struct ifreq ifr; - reply = dbus_message_new_method_return(m); - dbus_message_append_args(reply, DBUS_TYPE_INT32, &s, DBUS_TYPE_INVALID); - dbus_connection_send(c, reply, NULL); - dbus_message_unref(reply); + if (!(dbus_message_get_args(m, &error, DBUS_TYPE_INT32, &idx, DBUS_TYPE_INVALID))) { + avahi_log_warn("Error parsing Server::GetNetworkInterfaceNameByIndex message"); + goto fail; + } + +#ifdef VALGRIND_WORKAROUND + return respond_string(c, m, "blah"); +#else - return DBUS_HANDLER_RESULT_HANDLED; + if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { + gchar txt[256]; + g_snprintf(txt, sizeof(txt), "OS Error: %s", strerror(errno)); + return respond_error(c, m, AVAHI_ERR_OS, txt); + } + + memset(&ifr, 0, sizeof(ifr)); + ifr.ifr_ifindex = idx; + + if (ioctl(fd, SIOCGIFNAME, &ifr) < 0) { + gchar txt[256]; + g_snprintf(txt, sizeof(txt), "OS Error: %s", strerror(errno)); + close(fd); + return respond_error(c, m, AVAHI_ERR_OS, txt); + } + + close(fd); + + return respond_string(c, m, ifr.ifr_name); +#endif + + } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetNetworkInterfaceIndexByName")) { + gchar *n; + int fd; + struct ifreq ifr; + + if (!(dbus_message_get_args(m, &error, DBUS_TYPE_STRING, &n, DBUS_TYPE_INVALID)) || !n) { + avahi_log_warn("Error parsing Server::GetNetworkInterfaceIndexByName message"); + goto fail; + } + +#ifdef VALGRIND_WORKAROUND + return respond_int32(c, m, 1); +#else + if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { + gchar txt[256]; + g_snprintf(txt, sizeof(txt), "OS Error: %s", strerror(errno)); + return respond_error(c, m, AVAHI_ERR_OS, txt); + } + + memset(&ifr, 0, sizeof(ifr)); + g_snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s", n); + + if (ioctl(fd, SIOCGIFINDEX, &ifr) < 0) { + gchar txt[256]; + g_snprintf(txt, sizeof(txt), "OS Error: %s", strerror(errno)); + close(fd); + return respond_error(c, m, AVAHI_ERR_OS, txt); + } + + close(fd); + + return respond_int32(c, m, ifr.ifr_ifindex); +#endif } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetAlternativeHostName")) { gchar *n, * t; - if (!(dbus_message_get_args(m, &error, DBUS_TYPE_STRING, &n, DBUS_TYPE_INVALID)) || !n || !*n) { + if (!(dbus_message_get_args(m, &error, DBUS_TYPE_STRING, &n, DBUS_TYPE_INVALID)) || !n) { avahi_log_warn("Error parsing Server::GetAlternativeHostName message"); goto fail; } @@ -1098,7 +1232,7 @@ static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetAlternativeServiceName")) { gchar *n, *t; - if (!(dbus_message_get_args(m, &error, DBUS_TYPE_STRING, &n, DBUS_TYPE_INVALID)) || !n || !*n) { + if (!(dbus_message_get_args(m, &error, DBUS_TYPE_STRING, &n, DBUS_TYPE_INVALID)) || !n) { avahi_log_warn("Error parsing Server::GetAlternativeServiceName message"); goto fail; } @@ -1128,12 +1262,12 @@ static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void if (!(client = client_get(dbus_message_get_sender(m), TRUE))) { avahi_log_warn("Too many clients, client request failed."); - return respond_error(c, m, AVAHI_DBUS_ERROR_TOO_MANY_CLIENTS, NULL); + return respond_error(c, m, AVAHI_ERR_TOO_MANY_CLIENTS, NULL); } if (client->n_objects >= MAX_OBJECTS_PER_CLIENT) { avahi_log_warn("Too many objects for client '%s', client request failed.", client->name); - return respond_error(c, m, AVAHI_DBUS_ERROR_TOO_MANY_OBJECTS, NULL); + return respond_error(c, m, AVAHI_ERR_TOO_MANY_OBJECTS, NULL); } i = g_new(EntryGroupInfo, 1); @@ -1144,10 +1278,9 @@ static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void AVAHI_LLIST_PREPEND(EntryGroupInfo, entry_groups, client->entry_groups, i); client->n_objects++; - if (!(i->entry_group = avahi_entry_group_new(avahi_server, entry_group_callback, i))) { - avahi_log_warn("Failed to create entry group"); + if (!(i->entry_group = avahi_s_entry_group_new(avahi_server, entry_group_callback, i))) { entry_group_free(i); - goto fail; + return respond_error(c, m, avahi_server_errno(avahi_server), NULL); } dbus_connection_register_object_path(c, i->path, &vtable, i); @@ -1165,19 +1298,19 @@ static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void DBUS_TYPE_INT32, &protocol, DBUS_TYPE_STRING, &name, DBUS_TYPE_INT32, &aprotocol, - DBUS_TYPE_INVALID) || !name || !*name) { + DBUS_TYPE_INVALID) || !name) { avahi_log_warn("Error parsing Server::ResolveHostName message"); goto fail; } if (!(client = client_get(dbus_message_get_sender(m), TRUE))) { avahi_log_warn("Too many clients, client request failed."); - return respond_error(c, m, AVAHI_DBUS_ERROR_TOO_MANY_CLIENTS, NULL); + return respond_error(c, m, AVAHI_ERR_TOO_MANY_CLIENTS, NULL); } if (client->n_objects >= MAX_OBJECTS_PER_CLIENT) { avahi_log_warn("Too many objects for client '%s', client request failed.", client->name); - return respond_error(c, m, AVAHI_DBUS_ERROR_TOO_MANY_OBJECTS, NULL); + return respond_error(c, m, AVAHI_ERR_TOO_MANY_OBJECTS, NULL); } i = g_new(HostNameResolverInfo, 1); @@ -1186,10 +1319,9 @@ static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void AVAHI_LLIST_PREPEND(HostNameResolverInfo, host_name_resolvers, client->host_name_resolvers, i); client->n_objects++; - if (!(i->host_name_resolver = avahi_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, host_name_resolver_callback, i))) { host_name_resolver_free(i); - avahi_log_warn("Failed to create host name resolver"); - goto fail; + return respond_error(c, m, avahi_server_errno(avahi_server), NULL); } return DBUS_HANDLER_RESULT_HANDLED; @@ -1206,24 +1338,22 @@ static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void DBUS_TYPE_INT32, &interface, DBUS_TYPE_INT32, &protocol, DBUS_TYPE_STRING, &address, - DBUS_TYPE_INVALID) || !address || !*address) { + DBUS_TYPE_INVALID) || !address) { avahi_log_warn("Error parsing Server::ResolveAddress message"); goto fail; } - if (!avahi_address_parse(address, AVAHI_PROTO_UNSPEC, &a)) { - avahi_log_warn("Error parsing address data"); - return respond_error(c, m, AVAHI_DBUS_ERROR_INVALID_ADDRESS, NULL); - } + 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("Too many clients, client request failed."); - return respond_error(c, m, AVAHI_DBUS_ERROR_TOO_MANY_CLIENTS, NULL); + return respond_error(c, m, AVAHI_ERR_TOO_MANY_CLIENTS, NULL); } if (client->n_objects >= MAX_OBJECTS_PER_CLIENT) { avahi_log_warn("Too many objects for client '%s', client request failed.", client->name); - return respond_error(c, m, AVAHI_DBUS_ERROR_TOO_MANY_OBJECTS, NULL); + return respond_error(c, m, AVAHI_ERR_TOO_MANY_OBJECTS, NULL); } i = g_new(AddressResolverInfo, 1); @@ -1232,10 +1362,9 @@ static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void AVAHI_LLIST_PREPEND(AddressResolverInfo, address_resolvers, client->address_resolvers, i); client->n_objects++; - if (!(i->address_resolver = avahi_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, address_resolver_callback, i))) { address_resolver_free(i); - avahi_log_warn("Failed to create address resolver"); - goto fail; + return respond_error(c, m, avahi_server_errno(avahi_server), NULL); } return DBUS_HANDLER_RESULT_HANDLED; @@ -1268,12 +1397,12 @@ static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void if (!(client = client_get(dbus_message_get_sender(m), TRUE))) { avahi_log_warn("Too many clients, client request failed."); - return respond_error(c, m, AVAHI_DBUS_ERROR_TOO_MANY_CLIENTS, NULL); + return respond_error(c, m, AVAHI_ERR_TOO_MANY_CLIENTS, NULL); } if (client->n_objects >= MAX_OBJECTS_PER_CLIENT) { avahi_log_warn("Too many objects for client '%s', client request failed.", client->name); - return respond_error(c, m, AVAHI_DBUS_ERROR_TOO_MANY_OBJECTS, NULL); + return respond_error(c, m, AVAHI_ERR_TOO_MANY_OBJECTS, NULL); } if (!*domain) @@ -1286,10 +1415,9 @@ static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void AVAHI_LLIST_PREPEND(DomainBrowserInfo, domain_browsers, client->domain_browsers, i); client->n_objects++; - if (!(i->domain_browser = avahi_domain_browser_new(avahi_server, (AvahiIfIndex) interface, (AvahiProtocol) protocol, domain, (AvahiDomainBrowserType) type, domain_browser_callback, i))) { - avahi_log_warn("Failed to create domain browser"); + if (!(i->domain_browser = avahi_s_domain_browser_new(avahi_server, (AvahiIfIndex) interface, (AvahiProtocol) protocol, domain, (AvahiDomainBrowserType) type, domain_browser_callback, i))) { domain_browser_free(i); - goto fail; + return respond_error(c, m, avahi_server_errno(avahi_server), NULL); } dbus_connection_register_object_path(c, i->path, &vtable, i); @@ -1321,13 +1449,13 @@ static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void if (!(client = client_get(dbus_message_get_sender(m), TRUE))) { avahi_log_warn("Too many clients, client request failed."); - return respond_error(c, m, AVAHI_DBUS_ERROR_TOO_MANY_CLIENTS, NULL); + return respond_error(c, m, AVAHI_ERR_TOO_MANY_CLIENTS, NULL); } if (client->n_objects >= MAX_OBJECTS_PER_CLIENT) { avahi_log_warn("Too many objects for client '%s', client request failed.", client->name); - return respond_error(c, m, AVAHI_DBUS_ERROR_TOO_MANY_OBJECTS, NULL); + return respond_error(c, m, AVAHI_ERR_TOO_MANY_OBJECTS, NULL); } if (!*domain) @@ -1340,10 +1468,9 @@ static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void AVAHI_LLIST_PREPEND(ServiceTypeBrowserInfo, service_type_browsers, client->service_type_browsers, i); client->n_objects++; - if (!(i->service_type_browser = avahi_service_type_browser_new(avahi_server, (AvahiIfIndex) interface, (AvahiProtocol) protocol, domain, service_type_browser_callback, i))) { - avahi_log_warn("Failed to create service type browser"); + if (!(i->service_type_browser = avahi_s_service_type_browser_new(avahi_server, (AvahiIfIndex) interface, (AvahiProtocol) protocol, domain, service_type_browser_callback, i))) { service_type_browser_free(i); - goto fail; + return respond_error(c, m, avahi_server_errno(avahi_server), NULL); } dbus_connection_register_object_path(c, i->path, &vtable, i); @@ -1369,20 +1496,20 @@ static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void DBUS_TYPE_INT32, &protocol, DBUS_TYPE_STRING, &type, DBUS_TYPE_STRING, &domain, - DBUS_TYPE_INVALID) || !type || !*type) { + DBUS_TYPE_INVALID) || !type) { avahi_log_warn("Error parsing Server::ServiceBrowserNew message"); goto fail; } if (!(client = client_get(dbus_message_get_sender(m), TRUE))) { avahi_log_warn("Too many clients, client request failed."); - return respond_error(c, m, AVAHI_DBUS_ERROR_TOO_MANY_CLIENTS, NULL); + return respond_error(c, m, AVAHI_ERR_TOO_MANY_CLIENTS, NULL); } if (client->n_objects >= MAX_OBJECTS_PER_CLIENT) { avahi_log_warn("Too many objects for client '%s', client request failed.", client->name); - return respond_error(c, m, AVAHI_DBUS_ERROR_TOO_MANY_OBJECTS, NULL); + return respond_error(c, m, AVAHI_ERR_TOO_MANY_OBJECTS, NULL); } if (!*domain) @@ -1395,10 +1522,9 @@ static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void AVAHI_LLIST_PREPEND(ServiceBrowserInfo, service_browsers, client->service_browsers, i); client->n_objects++; - if (!(i->service_browser = avahi_service_browser_new(avahi_server, (AvahiIfIndex) interface, (AvahiProtocol) protocol, type, domain, service_browser_callback, i))) { - avahi_log_warn("Failed to create service browser"); + if (!(i->service_browser = avahi_s_service_browser_new(avahi_server, (AvahiIfIndex) interface, (AvahiProtocol) protocol, type, domain, service_browser_callback, i))) { service_browser_free(i); - goto fail; + return respond_error(c, m, avahi_server_errno(avahi_server), NULL); } dbus_connection_register_object_path(c, i->path, &vtable, i); @@ -1418,19 +1544,19 @@ static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void DBUS_TYPE_STRING, &type, DBUS_TYPE_STRING, &domain, DBUS_TYPE_INT32, &aprotocol, - DBUS_TYPE_INVALID) || !name || !*name || !type || !*type) { + DBUS_TYPE_INVALID) || !name || !type) { avahi_log_warn("Error parsing Server::ResolveService message"); goto fail; } if (!(client = client_get(dbus_message_get_sender(m), TRUE))) { avahi_log_warn("Too many clients, client request failed."); - return respond_error(c, m, AVAHI_DBUS_ERROR_TOO_MANY_CLIENTS, NULL); + return respond_error(c, m, AVAHI_ERR_TOO_MANY_CLIENTS, NULL); } if (client->n_objects >= MAX_OBJECTS_PER_CLIENT) { avahi_log_warn("Too many objects for client '%s', client request failed.", client->name); - return respond_error(c, m, AVAHI_DBUS_ERROR_TOO_MANY_OBJECTS, NULL); + return respond_error(c, m, AVAHI_ERR_TOO_MANY_OBJECTS, NULL); } if (!*domain) @@ -1442,10 +1568,9 @@ static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void AVAHI_LLIST_PREPEND(ServiceResolverInfo, service_resolvers, client->service_resolvers, i); client->n_objects++; - if (!(i->service_resolver = avahi_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, service_resolver_callback, i))) { service_resolver_free(i); - avahi_log_warn("Failed to create service resolver"); - goto fail; + return respond_error(c, m, avahi_server_errno(avahi_server), NULL); } return DBUS_HANDLER_RESULT_HANDLED; @@ -1500,7 +1625,7 @@ int dbus_protocol_setup(GMainLoop *loop) { } dbus_connection_setup_with_g_main(server->bus, NULL); - dbus_connection_set_exit_on_disconnect(server->bus, FALSE); + dbus_connection_set_exit_on_disconnect(server->bus, TRUE); dbus_bus_request_name(server->bus, AVAHI_DBUS_NAME, 0, &error); if (dbus_error_is_set(&error)) {