X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=avahi-daemon%2Fdbus-protocol.c;h=022187be5b07646d132938672aab3e5b6048cd48;hb=9f55e3e3af8c4b8913d19475f3e9e9be8c6f76ae;hp=9b83da9c214ff03ab324601340e7878e60119e84;hpb=3d2e63d2c5c351dde38b24a45a679ce819ed0e41;p=catta diff --git a/avahi-daemon/dbus-protocol.c b/avahi-daemon/dbus-protocol.c index 9b83da9..022187b 100644 --- a/avahi-daemon/dbus-protocol.c +++ b/avahi-daemon/dbus-protocol.c @@ -28,16 +28,23 @@ #include #include #include -#include +#include +#include +#include +#include +#include -#define DBUS_API_SUBJECT_TO_CHANGE #include -#include +#include #include +#include +#include +#include +#include +#include #include #include -#include #include "dbus-protocol.h" #include "main.h" @@ -59,12 +66,12 @@ typedef struct ServiceResolverInfo ServiceResolverInfo; /* #define VALGRIND_WORKAROUND */ struct EntryGroupInfo { - guint id; + unsigned id; Client *client; AvahiSEntryGroup *entry_group; - gchar *path; + char *path; - gint n_entries; + int n_entries; AVAHI_LLIST_FIELDS(EntryGroupInfo, entry_groups); }; @@ -86,28 +93,28 @@ struct AddressResolverInfo { }; struct DomainBrowserInfo { - guint id; + unsigned id; Client *client; AvahiSDomainBrowser *domain_browser; - gchar *path; + char *path; AVAHI_LLIST_FIELDS(DomainBrowserInfo, domain_browsers); }; struct ServiceTypeBrowserInfo { - guint id; + unsigned id; Client *client; AvahiSServiceTypeBrowser *service_type_browser; - gchar *path; + char *path; AVAHI_LLIST_FIELDS(ServiceTypeBrowserInfo, service_type_browsers); }; struct ServiceBrowserInfo { - guint id; + unsigned id; Client *client; AvahiSServiceBrowser *service_browser; - gchar *path; + char *path; AVAHI_LLIST_FIELDS(ServiceBrowserInfo, service_browsers); }; @@ -121,10 +128,10 @@ struct ServiceResolverInfo { }; struct Client { - guint id; - gchar *name; - guint current_id; - gint n_objects; + unsigned id; + char *name; + unsigned current_id; + int n_objects; AVAHI_LLIST_FIELDS(Client, clients); AVAHI_LLIST_HEAD(EntryGroupInfo, entry_groups); @@ -139,29 +146,29 @@ struct Client { struct Server { DBusConnection *bus; AVAHI_LLIST_HEAD(Client, clients); - gint n_clients; - guint current_id; + int n_clients; + unsigned current_id; }; static Server *server = NULL; static void entry_group_free(EntryGroupInfo *i) { - g_assert(i); + assert(i); if (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_free(i->path); AVAHI_LLIST_REMOVE(EntryGroupInfo, entry_groups, i->client->entry_groups, i); i->client->n_objects--; - g_assert(i->client->n_objects >= 0); + assert(i->client->n_objects >= 0); - g_free(i); + avahi_free(i); } static void host_name_resolver_free(HostNameResolverInfo *i) { - g_assert(i); + assert(i); if (i->host_name_resolver) avahi_s_host_name_resolver_free(i->host_name_resolver); @@ -169,13 +176,13 @@ static void host_name_resolver_free(HostNameResolverInfo *i) { AVAHI_LLIST_REMOVE(HostNameResolverInfo, host_name_resolvers, i->client->host_name_resolvers, i); i->client->n_objects--; - g_assert(i->client->n_objects >= 0); + assert(i->client->n_objects >= 0); - g_free(i); + avahi_free(i); } static void address_resolver_free(AddressResolverInfo *i) { - g_assert(i); + assert(i); if (i->address_resolver) avahi_s_address_resolver_free(i->address_resolver); @@ -183,58 +190,58 @@ static void address_resolver_free(AddressResolverInfo *i) { AVAHI_LLIST_REMOVE(AddressResolverInfo, address_resolvers, i->client->address_resolvers, i); i->client->n_objects--; - g_assert(i->client->n_objects >= 0); + assert(i->client->n_objects >= 0); - g_free(i); + avahi_free(i); } static void domain_browser_free(DomainBrowserInfo *i) { - g_assert(i); + assert(i); if (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_free(i->path); AVAHI_LLIST_REMOVE(DomainBrowserInfo, domain_browsers, i->client->domain_browsers, i); i->client->n_objects--; - g_assert(i->client->n_objects >= 0); + assert(i->client->n_objects >= 0); - g_free(i); + avahi_free(i); } static void service_type_browser_free(ServiceTypeBrowserInfo *i) { - g_assert(i); + assert(i); if (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_free(i->path); AVAHI_LLIST_REMOVE(ServiceTypeBrowserInfo, service_type_browsers, i->client->service_type_browsers, i); i->client->n_objects--; - g_assert(i->client->n_objects >= 0); + assert(i->client->n_objects >= 0); - g_free(i); + avahi_free(i); } static void service_browser_free(ServiceBrowserInfo *i) { - g_assert(i); + assert(i); if (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_free(i->path); AVAHI_LLIST_REMOVE(ServiceBrowserInfo, service_browsers, i->client->service_browsers, i); i->client->n_objects--; - g_assert(i->client->n_objects >= 0); + assert(i->client->n_objects >= 0); - g_free(i); + avahi_free(i); } static void service_resolver_free(ServiceResolverInfo *i) { - g_assert(i); + assert(i); if (i->service_resolver) avahi_s_service_resolver_free(i->service_resolver); @@ -242,15 +249,15 @@ static void service_resolver_free(ServiceResolverInfo *i) { AVAHI_LLIST_REMOVE(ServiceResolverInfo, service_resolvers, i->client->service_resolvers, i); i->client->n_objects--; - g_assert(i->client->n_objects >= 0); + assert(i->client->n_objects >= 0); - g_free(i); + avahi_free(i); } static void client_free(Client *c) { - g_assert(server); - g_assert(c); + assert(server); + assert(c); while (c->entry_groups) entry_group_free(c->entry_groups); @@ -273,21 +280,21 @@ static void client_free(Client *c) { while (c->service_resolvers) service_resolver_free(c->service_resolvers); - g_assert(c->n_objects == 0); + assert(c->n_objects == 0); - g_free(c->name); + avahi_free(c->name); AVAHI_LLIST_REMOVE(Client, clients, server->clients, c); - g_free(c); + avahi_free(c); server->n_clients --; - g_assert(server->n_clients >= 0); + assert(server->n_clients >= 0); } -static Client *client_get(const gchar *name, gboolean create) { +static Client *client_get(const char *name, int create) { Client *client; - g_assert(server); - g_assert(name); + assert(server); + assert(name); for (client = server->clients; client; client = client->clients_next) if (!strcmp(name, client->name)) @@ -300,9 +307,9 @@ static Client *client_get(const gchar *name, gboolean create) { return NULL; /* If not existant yet, create a new entry */ - client = g_new(Client, 1); + client = avahi_new(Client, 1); client->id = server->current_id++; - client->name = g_strdup(name); + client->name = avahi_strdup(name); client->current_id = 0; client->n_objects = 0; AVAHI_LLIST_HEAD_INIT(EntryGroupInfo, client->entry_groups); @@ -316,54 +323,25 @@ static Client *client_get(const gchar *name, gboolean create) { AVAHI_LLIST_PREPEND(Client, clients, server->clients, client); server->n_clients++; - g_assert(server->n_clients > 0); + assert(server->n_clients > 0); return client; } -static DBusHandlerResult respond_error(DBusConnection *c, DBusMessage *m, gint error, const gchar *text) { +static DBusHandlerResult respond_error(DBusConnection *c, DBusMessage *m, int error, const char *text) { DBusMessage *reply; - 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, - AVAHI_DBUS_ERR_INVALID_OBJECT - }; - - g_assert(-error > -AVAHI_OK); - g_assert(-error < -AVAHI_ERR_MAX); + assert(-error > -AVAHI_OK); + assert(-error < -AVAHI_ERR_MAX); - reply = dbus_message_new_error(m, table[-error], text ? text : avahi_strerror(error)); + reply = dbus_message_new_error(m, avahi_error_number_to_dbus (error), text ? text : avahi_strerror(error)); dbus_connection_send(c, reply, NULL); dbus_message_unref(reply); return DBUS_HANDLER_RESULT_HANDLED; } -static DBusHandlerResult respond_string(DBusConnection *c, DBusMessage *m, const gchar *text) { +static DBusHandlerResult respond_string(DBusConnection *c, DBusMessage *m, const char *text) { DBusMessage *reply; reply = dbus_message_new_method_return(m); @@ -374,7 +352,7 @@ static DBusHandlerResult respond_string(DBusConnection *c, DBusMessage *m, const return DBUS_HANDLER_RESULT_HANDLED; } -static DBusHandlerResult respond_int32(DBusConnection *c, DBusMessage *m, gint32 i) { +static DBusHandlerResult respond_int32(DBusConnection *c, DBusMessage *m, int32_t i) { DBusMessage *reply; reply = dbus_message_new_method_return(m); @@ -395,7 +373,7 @@ static DBusHandlerResult respond_ok(DBusConnection *c, DBusMessage *m) { return DBUS_HANDLER_RESULT_HANDLED; } -static DBusHandlerResult respond_path(DBusConnection *c, DBusMessage *m, const gchar *path) { +static DBusHandlerResult respond_path(DBusConnection *c, DBusMessage *m, const char *path) { DBusMessage *reply; reply = dbus_message_new_method_return(m); @@ -406,36 +384,83 @@ static DBusHandlerResult respond_path(DBusConnection *c, DBusMessage *m, const g return DBUS_HANDLER_RESULT_HANDLED; } -static DBusHandlerResult handle_introspect(DBusConnection *c, DBusMessage *m, const gchar *fname) { - gchar *path = NULL; - gchar *contents; - GError *gerror = NULL; +static char *file_get_contents(char *fname) { + int fd = -1; + struct stat st; + ssize_t size; + char *buf = NULL; + + assert(fname); + + if (!(fd = open(fname, O_RDONLY))) { + avahi_log_error("Failed to open %s: %s", fname, strerror(errno)); + goto fail; + } + + if (fstat(fd, &st) < 0) { + avahi_log_error("stat(%s) failed: %s", fname, strerror(errno)); + goto fail; + } + + if (!(S_ISREG(st.st_mode))) { + avahi_log_error("Invalid file %s", fname); + goto fail; + } + + if (st.st_size > 1024*1024) { /** 1MB */ + avahi_log_error("File too large %s", fname); + goto fail; + } + + buf = avahi_new(char, st.st_size+1); + + if ((size = read(fd, buf, st.st_size)) < 0) { + avahi_log_error("read() failed: %s\n", strerror(errno)); + goto fail; + } + + buf[size] = 0; + + close(fd); + return buf; + +fail: + if (fd >= 0) + close(fd); + + if (buf) + avahi_free(buf); + + return NULL; + +} + +static DBusHandlerResult handle_introspect(DBusConnection *c, DBusMessage *m, const char *fname) { + char *path, *contents; DBusError error; - g_assert(c); - g_assert(m); - g_assert(fname); + assert(c); + assert(m); + assert(fname); dbus_error_init(&error); if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) { - avahi_log_warn("Error parsing Introspect message: %s", error.message); + avahi_log_error("Error parsing Introspect message: %s", error.message); goto fail; } - path = g_strdup_printf("%s/%s", AVAHI_DBUS_INTROSPECTION_DIR, fname); - - if (!(g_file_get_contents(path, &contents, NULL, &gerror))) { - avahi_log_warn("Failed to load introspection data: %s", gerror->message); - g_error_free(gerror); - g_free(path); + path = avahi_strdup_printf("%s/%s", AVAHI_DBUS_INTROSPECTION_DIR, fname); + contents = file_get_contents(path); + avahi_free(path); + + if (!contents) { + avahi_log_error("Failed to load introspection data."); goto fail; } - - g_free(path); respond_string(c, m, contents); - g_free(contents); + avahi_free(contents); return DBUS_HANDLER_RESULT_HANDLED; @@ -448,7 +473,6 @@ fail: } static DBusHandlerResult msg_signal_filter_impl(DBusConnection *c, DBusMessage *m, void *userdata) { - GMainLoop *loop = userdata; DBusError error; dbus_error_init(&error); @@ -462,11 +486,13 @@ static DBusHandlerResult msg_signal_filter_impl(DBusConnection *c, DBusMessage * /* No, we shouldn't quit, but until we get somewhere * usefull such that we can restore our state, we will */ avahi_log_warn("Disconnnected from d-bus, terminating..."); - g_main_loop_quit (loop); + + raise(SIGQUIT); /* The signal handler will catch this and terminate the process cleanly*/ + return DBUS_HANDLER_RESULT_HANDLED; } else if (dbus_message_is_signal(m, DBUS_INTERFACE_DBUS, "NameAcquired")) { - gchar *name; + char *name; if (!dbus_message_get_args(m, &error, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID)) { avahi_log_warn("Error parsing NameAcquired message"); @@ -477,7 +503,7 @@ static DBusHandlerResult msg_signal_filter_impl(DBusConnection *c, DBusMessage * return DBUS_HANDLER_RESULT_HANDLED; } else if (dbus_message_is_signal(m, DBUS_INTERFACE_DBUS, "NameOwnerChanged")) { - gchar *name, *old, *new; + char *name, *old, *new; if (!dbus_message_get_args(m, &error, DBUS_TYPE_STRING, &name, DBUS_TYPE_STRING, &old, DBUS_TYPE_STRING, &new, DBUS_TYPE_INVALID)) { avahi_log_warn("Error parsing NameOwnerChanged message"); @@ -501,17 +527,17 @@ fail: return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; } -static void entry_group_callback(AvahiServer *s, AvahiSEntryGroup *g, AvahiEntryGroupState state, gpointer userdata) { +static void entry_group_callback(AvahiServer *s, AvahiSEntryGroup *g, AvahiEntryGroupState state, void* userdata) { EntryGroupInfo *i = userdata; DBusMessage *m; - gint32 t; + int32_t t; - g_assert(s); - g_assert(g); - g_assert(i); + assert(s); + assert(g); + assert(i); m = dbus_message_new_signal(i->path, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "StateChanged"); - t = (gint32) state; + t = (int32_t) state; dbus_message_append_args(m, DBUS_TYPE_INT32, &t, DBUS_TYPE_INVALID); dbus_message_set_destination(m, i->client->name); dbus_connection_send(server->bus, m, NULL); @@ -522,9 +548,9 @@ static DBusHandlerResult msg_entry_group_impl(DBusConnection *c, DBusMessage *m, DBusError error; EntryGroupInfo *i = userdata; - g_assert(c); - g_assert(m); - g_assert(i); + assert(c); + assert(m); + assert(i); dbus_error_init(&error); @@ -574,7 +600,7 @@ static DBusHandlerResult msg_entry_group_impl(DBusConnection *c, DBusMessage *m, } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "IsEmpty")) { DBusMessage *reply; - gboolean b; + int b; if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) { avahi_log_warn("Error parsing EntryGroup::IsEmpty message"); @@ -599,12 +625,12 @@ static DBusHandlerResult msg_entry_group_impl(DBusConnection *c, DBusMessage *m, } state = avahi_s_entry_group_get_state(i->entry_group); - return respond_int32(c, m, (gint32) state); + return respond_int32(c, m, (int32_t) 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; + int32_t interface, protocol; + char *type, *name, *domain, *host; + uint16_t port; AvahiStringList *strlst; DBusMessageIter iter, sub; int j; @@ -640,12 +666,12 @@ static DBusHandlerResult msg_entry_group_impl(DBusConnection *c, DBusMessage *m, for (;;) { DBusMessageIter sub2; int at, n; - guint8 *k; + uint8_t *k; if ((at = dbus_message_iter_get_arg_type(&sub)) == DBUS_TYPE_INVALID) break; - g_assert(at == DBUS_TYPE_ARRAY); + assert(at == DBUS_TYPE_ARRAY); if (dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_BYTE) { avahi_log_warn("Error parsing EntryGroup::AddService message"); @@ -682,8 +708,8 @@ static DBusHandlerResult msg_entry_group_impl(DBusConnection *c, DBusMessage *m, return respond_ok(c, m); } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "AddAddress")) { - gint32 interface, protocol; - gchar *name, *address; + int32_t interface, protocol; + char *name, *address; AvahiAddress a; if (!dbus_message_get_args( @@ -723,24 +749,24 @@ fail: return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; } -static void host_name_resolver_callback(AvahiSHostNameResolver *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 char *host_name, const AvahiAddress *a, void* userdata) { HostNameResolverInfo *i = userdata; - g_assert(r); - g_assert(host_name); - g_assert(i); + assert(r); + assert(host_name); + assert(i); if (event == AVAHI_RESOLVER_FOUND) { char t[256], *pt = t; - gint32 i_interface, i_protocol, i_aprotocol; + int32_t i_interface, i_protocol, i_aprotocol; DBusMessage *reply; - g_assert(a); + assert(a); avahi_address_snprint(t, sizeof(t), a); - i_interface = (gint32) interface; - i_protocol = (gint32) protocol; - i_aprotocol = (gint32) a->family; + i_interface = (int32_t) interface; + i_protocol = (int32_t) protocol; + i_aprotocol = (int32_t) a->family; reply = dbus_message_new_method_return(i->message); dbus_message_append_args( @@ -755,7 +781,7 @@ static void host_name_resolver_callback(AvahiSHostNameResolver *r, AvahiIfIndex dbus_connection_send(server->bus, reply, NULL); dbus_message_unref(reply); } else { - g_assert(event == AVAHI_RESOLVER_TIMEOUT); + assert(event == AVAHI_RESOLVER_TIMEOUT); respond_error(server->bus, i->message, AVAHI_ERR_TIMEOUT, NULL); } @@ -763,24 +789,24 @@ static void host_name_resolver_callback(AvahiSHostNameResolver *r, AvahiIfIndex host_name_resolver_free(i); } -static void address_resolver_callback(AvahiSAddressResolver *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 char *host_name, void* userdata) { AddressResolverInfo *i = userdata; - g_assert(r); - g_assert(address); - g_assert(i); + assert(r); + assert(address); + assert(i); if (event == AVAHI_RESOLVER_FOUND) { char t[256], *pt = t; - gint32 i_interface, i_protocol, i_aprotocol; + int32_t i_interface, i_protocol, i_aprotocol; DBusMessage *reply; - g_assert(host_name); + assert(host_name); avahi_address_snprint(t, sizeof(t), address); - i_interface = (gint32) interface; - i_protocol = (gint32) protocol; - i_aprotocol = (gint32) address->family; + i_interface = (int32_t) interface; + i_protocol = (int32_t) protocol; + i_aprotocol = (int32_t) address->family; reply = dbus_message_new_method_return(i->message); dbus_message_append_args( @@ -795,7 +821,7 @@ static void address_resolver_callback(AvahiSAddressResolver *r, AvahiIfIndex int dbus_connection_send(server->bus, reply, NULL); dbus_message_unref(reply); } else { - g_assert(event == AVAHI_RESOLVER_TIMEOUT); + assert(event == AVAHI_RESOLVER_TIMEOUT); respond_error(server->bus, i->message, AVAHI_ERR_TIMEOUT, NULL); } @@ -806,9 +832,9 @@ static DBusHandlerResult msg_domain_browser_impl(DBusConnection *c, DBusMessage DBusError error; DomainBrowserInfo *i = userdata; - g_assert(c); - g_assert(m); - g_assert(i); + assert(c); + assert(m); + assert(i); dbus_error_init(&error); @@ -846,17 +872,17 @@ fail: return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; } -static void domain_browser_callback(AvahiSDomainBrowser *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 char *domain, void* userdata) { DomainBrowserInfo *i = userdata; DBusMessage *m; - gint32 i_interface, i_protocol; + int32_t i_interface, i_protocol; - g_assert(b); - g_assert(domain); - g_assert(i); + assert(b); + assert(domain); + assert(i); - i_interface = (gint32) interface; - i_protocol = (gint32) protocol; + i_interface = (int32_t) interface; + i_protocol = (int32_t) protocol; m = dbus_message_new_signal(i->path, AVAHI_DBUS_INTERFACE_DOMAIN_BROWSER, event == AVAHI_BROWSER_NEW ? "ItemNew" : "ItemRemove"); dbus_message_append_args( @@ -874,9 +900,9 @@ static DBusHandlerResult msg_service_type_browser_impl(DBusConnection *c, DBusMe DBusError error; ServiceTypeBrowserInfo *i = userdata; - g_assert(c); - g_assert(m); - g_assert(i); + assert(c); + assert(m); + assert(i); dbus_error_init(&error); @@ -914,18 +940,18 @@ fail: return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; } -static void service_type_browser_callback(AvahiSServiceTypeBrowser *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 char *type, const char *domain, void* userdata) { ServiceTypeBrowserInfo *i = userdata; DBusMessage *m; - gint32 i_interface, i_protocol; + int32_t i_interface, i_protocol; - g_assert(b); - g_assert(type); - g_assert(domain); - g_assert(i); + assert(b); + assert(type); + assert(domain); + assert(i); - i_interface = (gint32) interface; - i_protocol = (gint32) protocol; + i_interface = (int32_t) interface; + i_protocol = (int32_t) protocol; m = dbus_message_new_signal(i->path, AVAHI_DBUS_INTERFACE_SERVICE_TYPE_BROWSER, event == AVAHI_BROWSER_NEW ? "ItemNew" : "ItemRemove"); dbus_message_append_args( @@ -944,9 +970,9 @@ static DBusHandlerResult msg_service_browser_impl(DBusConnection *c, DBusMessage DBusError error; ServiceBrowserInfo *i = userdata; - g_assert(c); - g_assert(m); - g_assert(i); + assert(c); + assert(m); + assert(i); dbus_error_init(&error); @@ -984,19 +1010,19 @@ fail: return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; } -static void service_browser_callback(AvahiSServiceBrowser *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 char *name, const char *type, const char *domain, void* userdata) { ServiceBrowserInfo *i = userdata; DBusMessage *m; - gint32 i_interface, i_protocol; + int32_t i_interface, i_protocol; - g_assert(b); - g_assert(name); - g_assert(type); - g_assert(domain); - g_assert(i); + assert(b); + assert(name); + assert(type); + assert(domain); + assert(i); - i_interface = (gint32) interface; - i_protocol = (gint32) protocol; + i_interface = (int32_t) interface; + i_protocol = (int32_t) protocol; m = dbus_message_new_signal(i->path, AVAHI_DBUS_INTERFACE_SERVICE_BROWSER, event == AVAHI_BROWSER_NEW ? "ItemNew" : "ItemRemove"); dbus_message_append_args( @@ -1017,36 +1043,36 @@ static void service_resolver_callback( AvahiIfIndex interface, AvahiProtocol protocol, AvahiResolverEvent event, - const gchar *name, - const gchar *type, - const gchar *domain, - const gchar *host_name, + const char *name, + const char *type, + const char *domain, + const char *host_name, const AvahiAddress *a, - guint16 port, + uint16_t port, AvahiStringList *txt, - gpointer userdata) { + void* userdata) { ServiceResolverInfo *i = userdata; - g_assert(r); - g_assert(i); + assert(r); + assert(i); if (event == AVAHI_RESOLVER_FOUND) { char t[256], *pt = t; - gint32 i_interface, i_protocol, i_aprotocol; - guint n, j; + int32_t i_interface, i_protocol, i_aprotocol; + unsigned n, j; AvahiStringList *p; DBusMessage *reply; DBusMessageIter iter, sub; - g_assert(host_name); + assert(host_name); - g_assert(a); + assert(a); avahi_address_snprint(t, sizeof(t), a); - i_interface = (gint32) interface; - i_protocol = (gint32) protocol; - i_aprotocol = (gint32) a->family; + i_interface = (int32_t) interface; + i_protocol = (int32_t) protocol; + i_aprotocol = (int32_t) a->family; reply = dbus_message_new_method_return(i->message); dbus_message_append_args( @@ -1067,7 +1093,7 @@ static void service_resolver_callback( for (p = txt, j = n-1; p; p = p->next, j--) { DBusMessageIter sub2; - const guint8 *data = p->text; + const uint8_t *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); @@ -1079,7 +1105,7 @@ static void service_resolver_callback( dbus_connection_send(server->bus, reply, NULL); dbus_message_unref(reply); } else { - g_assert(event == AVAHI_RESOLVER_TIMEOUT); + assert(event == AVAHI_RESOLVER_TIMEOUT); respond_error(server->bus, i->message, AVAHI_ERR_TIMEOUT, NULL); } @@ -1145,10 +1171,10 @@ static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void } state = avahi_server_get_state(avahi_server); - return respond_int32(c, m, (gint32) state); + return respond_int32(c, m, (int32_t) state); } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetNetworkInterfaceNameByIndex")) { - gint32 idx; + int32_t idx; int fd; struct ifreq ifr; @@ -1162,8 +1188,8 @@ static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void #else if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { - gchar txt[256]; - g_snprintf(txt, sizeof(txt), "OS Error: %s", strerror(errno)); + char txt[256]; + snprintf(txt, sizeof(txt), "OS Error: %s", strerror(errno)); return respond_error(c, m, AVAHI_ERR_OS, txt); } @@ -1171,8 +1197,8 @@ static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void ifr.ifr_ifindex = idx; if (ioctl(fd, SIOCGIFNAME, &ifr) < 0) { - gchar txt[256]; - g_snprintf(txt, sizeof(txt), "OS Error: %s", strerror(errno)); + char txt[256]; + snprintf(txt, sizeof(txt), "OS Error: %s", strerror(errno)); close(fd); return respond_error(c, m, AVAHI_ERR_OS, txt); } @@ -1183,7 +1209,7 @@ static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void #endif } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetNetworkInterfaceIndexByName")) { - gchar *n; + char *n; int fd; struct ifreq ifr; @@ -1196,17 +1222,17 @@ static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void 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)); + char txt[256]; + 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); + 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)); + char txt[256]; + snprintf(txt, sizeof(txt), "OS Error: %s", strerror(errno)); close(fd); return respond_error(c, m, AVAHI_ERR_OS, txt); } @@ -1217,7 +1243,7 @@ static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void #endif } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetAlternativeHostName")) { - gchar *n, * t; + char *n, * t; if (!(dbus_message_get_args(m, &error, DBUS_TYPE_STRING, &n, DBUS_TYPE_INVALID)) || !n) { avahi_log_warn("Error parsing Server::GetAlternativeHostName message"); @@ -1226,12 +1252,12 @@ static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void t = avahi_alternative_host_name(n); respond_string(c, m, t); - g_free(t); + avahi_free(t); return DBUS_HANDLER_RESULT_HANDLED; } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetAlternativeServiceName")) { - gchar *n, *t; + char *n, *t; if (!(dbus_message_get_args(m, &error, DBUS_TYPE_STRING, &n, DBUS_TYPE_INVALID)) || !n) { avahi_log_warn("Error parsing Server::GetAlternativeServiceName message"); @@ -1240,7 +1266,7 @@ static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void t = avahi_alternative_service_name(n); respond_string(c, m, t); - g_free(t); + avahi_free(t); return DBUS_HANDLER_RESULT_HANDLED; @@ -1271,10 +1297,10 @@ static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void return respond_error(c, m, AVAHI_ERR_TOO_MANY_OBJECTS, NULL); } - i = g_new(EntryGroupInfo, 1); + i = avahi_new(EntryGroupInfo, 1); i->id = ++client->current_id; i->client = client; - i->path = g_strdup_printf("/Client%u/EntryGroup%u", client->id, i->id); + i->path = avahi_strdup_printf("/Client%u/EntryGroup%u", client->id, i->id); i->n_entries = 0; AVAHI_LLIST_PREPEND(EntryGroupInfo, entry_groups, client->entry_groups, i); client->n_objects++; @@ -1289,8 +1315,8 @@ static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "ResolveHostName")) { Client *client; - gint32 interface, protocol, aprotocol; - gchar *name; + int32_t interface, protocol, aprotocol; + char *name; HostNameResolverInfo *i; if (!dbus_message_get_args( @@ -1314,7 +1340,7 @@ static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void return respond_error(c, m, AVAHI_ERR_TOO_MANY_OBJECTS, NULL); } - i = g_new(HostNameResolverInfo, 1); + i = avahi_new(HostNameResolverInfo, 1); i->client = client; i->message = dbus_message_ref(m); AVAHI_LLIST_PREPEND(HostNameResolverInfo, host_name_resolvers, client->host_name_resolvers, i); @@ -1329,8 +1355,8 @@ static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "ResolveAddress")) { Client *client; - gint32 interface, protocol; - gchar *address; + int32_t interface, protocol; + char *address; AddressResolverInfo *i; AvahiAddress a; @@ -1357,7 +1383,7 @@ static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void return respond_error(c, m, AVAHI_ERR_TOO_MANY_OBJECTS, NULL); } - i = g_new(AddressResolverInfo, 1); + i = avahi_new(AddressResolverInfo, 1); i->client = client; i->message = dbus_message_ref(m); AVAHI_LLIST_PREPEND(AddressResolverInfo, address_resolvers, client->address_resolvers, i); @@ -1381,8 +1407,8 @@ static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void NULL, NULL }; - gint32 interface, protocol, type; - gchar *domain; + int32_t interface, protocol, type; + char *domain; if (!dbus_message_get_args( @@ -1409,10 +1435,10 @@ static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void if (!*domain) domain = NULL; - i = g_new(DomainBrowserInfo, 1); + i = avahi_new(DomainBrowserInfo, 1); i->id = ++client->current_id; i->client = client; - i->path = g_strdup_printf("/Client%u/DomainBrowser%u", client->id, i->id); + i->path = avahi_strdup_printf("/Client%u/DomainBrowser%u", client->id, i->id); AVAHI_LLIST_PREPEND(DomainBrowserInfo, domain_browsers, client->domain_browsers, i); client->n_objects++; @@ -1435,8 +1461,8 @@ static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void NULL, NULL }; - gint32 interface, protocol; - gchar *domain; + int32_t interface, protocol; + char *domain; if (!dbus_message_get_args( m, &error, @@ -1462,10 +1488,10 @@ static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void if (!*domain) domain = NULL; - i = g_new(ServiceTypeBrowserInfo, 1); + i = avahi_new(ServiceTypeBrowserInfo, 1); i->id = ++client->current_id; i->client = client; - i->path = g_strdup_printf("/Client%u/ServiceTypeBrowser%u", client->id, i->id); + i->path = avahi_strdup_printf("/Client%u/ServiceTypeBrowser%u", client->id, i->id); AVAHI_LLIST_PREPEND(ServiceTypeBrowserInfo, service_type_browsers, client->service_type_browsers, i); client->n_objects++; @@ -1488,8 +1514,8 @@ static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void NULL, NULL }; - gint32 interface, protocol; - gchar *domain, *type; + int32_t interface, protocol; + char *domain, *type; if (!dbus_message_get_args( m, &error, @@ -1516,10 +1542,10 @@ static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void if (!*domain) domain = NULL; - i = g_new(ServiceBrowserInfo, 1); + i = avahi_new(ServiceBrowserInfo, 1); i->id = ++client->current_id; i->client = client; - i->path = g_strdup_printf("/Client%u/ServiceBrowser%u", client->id, i->id); + i->path = avahi_strdup_printf("/Client%u/ServiceBrowser%u", client->id, i->id); AVAHI_LLIST_PREPEND(ServiceBrowserInfo, service_browsers, client->service_browsers, i); client->n_objects++; @@ -1533,8 +1559,8 @@ static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "ResolveService")) { Client *client; - gint32 interface, protocol, aprotocol; - gchar *name, *type, *domain; + int32_t interface, protocol, aprotocol; + char *name, *type, *domain; ServiceResolverInfo *i; if (!dbus_message_get_args( @@ -1563,7 +1589,7 @@ static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void if (!*domain) domain = NULL; - i = g_new(ServiceResolverInfo, 1); + i = avahi_new(ServiceResolverInfo, 1); i->client = client; i->message = dbus_message_ref(m); AVAHI_LLIST_PREPEND(ServiceResolverInfo, service_resolvers, client->service_resolvers, i); @@ -1588,19 +1614,19 @@ fail: void dbus_protocol_server_state_changed(AvahiServerState state) { DBusMessage *m; - gint32 t; + int32_t t; if (!server) return; m = dbus_message_new_signal(AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "StateChanged"); - t = (gint32) state; + t = (int32_t) state; dbus_message_append_args(m, DBUS_TYPE_INT32, &t, DBUS_TYPE_INVALID); dbus_connection_send(server->bus, m, NULL); dbus_message_unref(m); } -int dbus_protocol_setup(GMainLoop *loop) { +int dbus_protocol_setup(const AvahiPoll *poll_api) { DBusError error; static const DBusObjectPathVTable server_vtable = { @@ -1614,30 +1640,48 @@ int dbus_protocol_setup(GMainLoop *loop) { dbus_error_init(&error); - server = g_malloc(sizeof(Server)); - server->clients = NULL; + server = avahi_new(Server, 1); + AVAHI_LLIST_HEAD_INIT(Clients, server->clients); server->current_id = 0; server->n_clients = 0; - server->bus = dbus_bus_get(DBUS_BUS_SYSTEM, &error); - if (dbus_error_is_set(&error)) { - avahi_log_warn("dbus_bus_get(): %s", error.message); + if (!(server->bus = dbus_bus_get(DBUS_BUS_SYSTEM, &error))) { + assert(dbus_error_is_set(&error)); + avahi_log_error("dbus_bus_get(): %s", error.message); goto fail; } - dbus_connection_setup_with_g_main(server->bus, NULL); - dbus_connection_set_exit_on_disconnect(server->bus, TRUE); + if (avahi_dbus_connection_glue(server->bus, poll_api) < 0) { + avahi_log_error("avahi_dbus_connection_glue() failed"); + goto fail; + } - dbus_bus_request_name(server->bus, AVAHI_DBUS_NAME, 0, &error); - if (dbus_error_is_set(&error)) { - avahi_log_warn("dbus_bus_request_name(): %s", error.message); + if (dbus_bus_request_name(server->bus, AVAHI_DBUS_NAME, DBUS_NAME_FLAG_PROHIBIT_REPLACEMENT, &error) != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) { + if (dbus_error_is_set(&error)) { + avahi_log_error("dbus_bus_request_name(): %s", error.message); + goto fail; + } + + avahi_log_error("Failed to acquire DBUS name '"AVAHI_DBUS_NAME"'"); goto fail; } + if (!(dbus_connection_add_filter(server->bus, msg_signal_filter_impl, (void*) poll_api, NULL))) { + avahi_log_error("dbus_connection_add_filter() failed"); + goto fail; + } + dbus_bus_add_match(server->bus, "type='signal',""interface='" DBUS_INTERFACE_DBUS "'", &error); - dbus_connection_add_filter(server->bus, msg_signal_filter_impl, loop, NULL); - dbus_connection_register_object_path(server->bus, AVAHI_DBUS_PATH_SERVER, &server_vtable, NULL); + if (dbus_error_is_set(&error)) { + avahi_log_error("dbus_bus_add_match(): %s", error.message); + goto fail; + } + + if (!(dbus_connection_register_object_path(server->bus, AVAHI_DBUS_PATH_SERVER, &server_vtable, NULL))) { + avahi_log_error("dbus_connection_register_object_path() failed"); + goto fail; + } return 0; @@ -1646,9 +1690,11 @@ fail: dbus_connection_disconnect(server->bus); dbus_connection_unref(server->bus); } - - dbus_error_free (&error); - g_free(server); + + if (dbus_error_is_set(&error)) + dbus_error_free(&error); + + avahi_free(server); server = NULL; return -1; } @@ -1660,14 +1706,14 @@ void dbus_protocol_shutdown(void) { while (server->clients) client_free(server->clients); - g_assert(server->n_clients == 0); + assert(server->n_clients == 0); if (server->bus) { dbus_connection_disconnect(server->bus); dbus_connection_unref(server->bus); } - g_free(server); + avahi_free(server); server = NULL; } }