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/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 #include "main.h"
34
avahi_dbus_service_browser_free(ServiceBrowserInfo * i)35 void avahi_dbus_service_browser_free(ServiceBrowserInfo *i) {
36 const AvahiPoll *poll_api = NULL;
37
38 assert(i);
39
40 poll_api = avahi_simple_poll_get(simple_poll_api);
41
42 if (i->delay_timeout)
43 poll_api->timeout_free(i->delay_timeout);
44
45 if (i->service_browser)
46 avahi_s_service_browser_free(i->service_browser);
47
48 if (i->path) {
49 dbus_connection_unregister_object_path(server->bus, i->path);
50 avahi_free(i->path);
51 }
52
53 AVAHI_LLIST_REMOVE(ServiceBrowserInfo, service_browsers, i->client->service_browsers, i);
54
55 assert(i->client->n_objects >= 1);
56 i->client->n_objects--;
57
58 avahi_free(i);
59 }
60
avahi_dbus_service_browser_start(ServiceBrowserInfo * i)61 void avahi_dbus_service_browser_start(ServiceBrowserInfo *i) {
62 assert(i);
63
64 if(i->service_browser)
65 avahi_s_service_browser_start(i->service_browser);
66 }
67
avahi_dbus_msg_service_browser_impl(DBusConnection * c,DBusMessage * m,void * userdata)68 DBusHandlerResult avahi_dbus_msg_service_browser_impl(DBusConnection *c, DBusMessage *m, void *userdata) {
69 DBusError error;
70 ServiceBrowserInfo *i = userdata;
71
72 assert(c);
73 assert(m);
74 assert(i);
75
76 dbus_error_init(&error);
77
78 avahi_log_debug(__FILE__": interface=%s, path=%s, member=%s",
79 dbus_message_get_interface(m),
80 dbus_message_get_path(m),
81 dbus_message_get_member(m));
82
83 /* Introspection */
84 if (dbus_message_is_method_call(m, DBUS_INTERFACE_INTROSPECTABLE, "Introspect"))
85 return avahi_dbus_handle_introspect(c, m, "org.freedesktop.Avahi.ServiceBrowser.xml");
86
87 /* Access control */
88 if (strcmp(dbus_message_get_sender(m), i->client->name))
89 return avahi_dbus_respond_error(c, m, AVAHI_ERR_ACCESS_DENIED, NULL);
90
91 if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVICE_BROWSER, "Free")) {
92
93 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
94 avahi_log_warn("Error parsing ServiceBrowser::Free message");
95 goto fail;
96 }
97
98 avahi_dbus_service_browser_free(i);
99 return avahi_dbus_respond_ok(c, m);
100
101 }
102
103 if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVICE_BROWSER, "Start")) {
104
105 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
106 avahi_log_warn("Error parsing ServiceBrowser::Start message");
107 goto fail;
108 }
109
110 avahi_dbus_service_browser_start(i);
111 return avahi_dbus_respond_ok(c, m);
112
113 }
114
115 avahi_log_warn("Missed message %s::%s()", dbus_message_get_interface(m), dbus_message_get_member(m));
116
117 fail:
118 if (dbus_error_is_set(&error))
119 dbus_error_free(&error);
120
121 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
122 }
123
avahi_dbus_service_browser_callback(AvahiSServiceBrowser * b,AvahiIfIndex interface,AvahiProtocol protocol,AvahiBrowserEvent event,const char * name,const char * type,const char * domain,AvahiLookupResultFlags flags,void * userdata)124 void avahi_dbus_service_browser_callback(AvahiSServiceBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const char *name, const char *type, const char *domain, AvahiLookupResultFlags flags, void* userdata) {
125 ServiceBrowserInfo *i = userdata;
126 DBusMessage *m;
127 int32_t i_interface, i_protocol;
128 uint32_t u_flags;
129
130 assert(b);
131 assert(i);
132
133 m = dbus_message_new_signal(i->path, AVAHI_DBUS_INTERFACE_SERVICE_BROWSER, avahi_dbus_map_browse_signal_name(event));
134
135 if (!m) {
136 avahi_log_error("Failed allocate message");
137 return;
138 }
139
140 if (event == AVAHI_BROWSER_NEW) {
141 /* Patch in AVAHI_LOOKUP_RESULT_OUR_OWN */
142
143 if (avahi_dbus_is_our_own_service(i->client, interface, protocol, name, type, domain) > 0)
144 flags |= AVAHI_LOOKUP_RESULT_OUR_OWN;
145 }
146
147 i_interface = (int32_t) interface;
148 i_protocol = (int32_t) protocol;
149 u_flags = (uint32_t) flags;
150
151 if (event == AVAHI_BROWSER_NEW || event == AVAHI_BROWSER_REMOVE) {
152 assert(name);
153 assert(type);
154 assert(domain);
155
156 dbus_message_append_args(
157 m,
158 DBUS_TYPE_INT32, &i_interface,
159 DBUS_TYPE_INT32, &i_protocol,
160 DBUS_TYPE_STRING, &name,
161 DBUS_TYPE_STRING, &type,
162 DBUS_TYPE_STRING, &domain,
163 DBUS_TYPE_UINT32, &u_flags,
164 DBUS_TYPE_INVALID);
165 } else if (event == AVAHI_BROWSER_FAILURE)
166 avahi_dbus_append_server_error(m);
167
168 dbus_message_set_destination(m, i->client->name);
169 dbus_connection_send(server->bus, m, NULL);
170 dbus_message_unref(m);
171 }
172