1 /***
2   This file is part of avahi.
3 
4   avahi is free software; you can redistribute it and/or modify it
5   under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2.1 of the
7   License, or (at your option) any later version.
8 
9   avahi is distributed in the hope that it will be useful, but WITHOUT
10   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11   or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
12   Public License for more details.
13 
14   You should have received a copy of the GNU Lesser General Public
15   License along with avahi; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17   USA.
18 ***/
19 
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23 
24 #include <string.h>
25 
26 #include "avahi-common/avahi-malloc.h"
27 #include <avahi-common/dbus.h>
28 #include <avahi-common/error.h>
29 #include <avahi-core/log.h>
30 
31 #include "dbus-util.h"
32 #include "dbus-internal.h"
33 
avahi_dbus_async_address_resolver_free(AsyncAddressResolverInfo * i)34 void avahi_dbus_async_address_resolver_free(AsyncAddressResolverInfo *i) {
35     assert(i);
36 
37     if (i->address_resolver)
38         avahi_s_address_resolver_free(i->address_resolver);
39 
40     if (i->path) {
41         dbus_connection_unregister_object_path(server->bus, i->path);
42         avahi_free(i->path);
43     }
44 
45     AVAHI_LLIST_REMOVE(AsyncAddressResolverInfo, async_address_resolvers, i->client->async_address_resolvers, i);
46 
47     assert(i->client->n_objects >= 1);
48     i->client->n_objects--;
49 
50     avahi_free(i);
51 }
52 
avahi_dbus_async_address_resolver_callback(AvahiSAddressResolver * r,AvahiIfIndex interface,AvahiProtocol protocol,AvahiResolverEvent event,const AvahiAddress * address,const char * host_name,AvahiLookupResultFlags flags,void * userdata)53 void avahi_dbus_async_address_resolver_callback(AvahiSAddressResolver *r, AvahiIfIndex interface, AvahiProtocol protocol, AvahiResolverEvent event, const AvahiAddress *address, const char *host_name, AvahiLookupResultFlags flags, void* userdata) {
54     AsyncAddressResolverInfo *i = userdata;
55     DBusMessage *reply;
56 
57     assert(r);
58     assert(i);
59 
60     reply = dbus_message_new_signal(i->path, AVAHI_DBUS_INTERFACE_ADDRESS_RESOLVER, avahi_dbus_map_resolve_signal_name(event));
61 
62     if (!reply) {
63         avahi_log_error("Failed allocate message");
64         return;
65     }
66 
67     if (event == AVAHI_RESOLVER_FOUND) {
68         char t[AVAHI_ADDRESS_STR_MAX], *pt = t;
69         int32_t i_interface, i_protocol, i_aprotocol;
70         uint32_t u_flags;
71 
72         assert(address);
73         assert(host_name);
74         avahi_address_snprint(t, sizeof(t), address);
75 
76         i_interface = (int32_t) interface;
77         i_protocol = (int32_t) protocol;
78         i_aprotocol = (int32_t) address->proto;
79         u_flags = (uint32_t) flags;
80 
81         dbus_message_append_args(
82             reply,
83             DBUS_TYPE_INT32, &i_interface,
84             DBUS_TYPE_INT32, &i_protocol,
85             DBUS_TYPE_INT32, &i_aprotocol,
86             DBUS_TYPE_STRING, &pt,
87             DBUS_TYPE_STRING, &host_name,
88             DBUS_TYPE_UINT32, &u_flags,
89             DBUS_TYPE_INVALID);
90 
91     }  else {
92         assert(event == AVAHI_RESOLVER_FAILURE);
93         avahi_dbus_append_server_error(reply);
94     }
95 
96     dbus_message_set_destination(reply, i->client->name);
97     dbus_connection_send(server->bus, reply, NULL);
98     dbus_message_unref(reply);
99 }
100 
avahi_dbus_msg_async_address_resolver_impl(DBusConnection * c,DBusMessage * m,void * userdata)101 DBusHandlerResult avahi_dbus_msg_async_address_resolver_impl(DBusConnection *c, DBusMessage *m, void *userdata) {
102     DBusError error;
103     AsyncAddressResolverInfo *i = userdata;
104 
105     assert(c);
106     assert(m);
107     assert(i);
108 
109     dbus_error_init(&error);
110 
111     avahi_log_debug(__FILE__": interface=%s, path=%s, member=%s",
112                     dbus_message_get_interface(m),
113                     dbus_message_get_path(m),
114                     dbus_message_get_member(m));
115 
116     /* Introspection */
117     if (dbus_message_is_method_call(m, DBUS_INTERFACE_INTROSPECTABLE, "Introspect"))
118         return avahi_dbus_handle_introspect(c, m, "org.freedesktop.Avahi.AddressResolver.xml");
119 
120     /* Access control */
121     if (strcmp(dbus_message_get_sender(m), i->client->name))
122         return avahi_dbus_respond_error(c, m, AVAHI_ERR_ACCESS_DENIED, NULL);
123 
124     if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ADDRESS_RESOLVER, "Free")) {
125 
126         if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
127             avahi_log_warn("Error parsing AddressResolver::Free message");
128             goto fail;
129         }
130 
131         avahi_dbus_async_address_resolver_free(i);
132         return avahi_dbus_respond_ok(c, m);
133     }
134 
135     avahi_log_warn("Missed message %s::%s()", dbus_message_get_interface(m), dbus_message_get_member(m));
136 
137 fail:
138     if (dbus_error_is_set(&error))
139         dbus_error_free(&error);
140 
141     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
142 }
143