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_record_browser_free(RecordBrowserInfo * i)35 void avahi_dbus_record_browser_free(RecordBrowserInfo *i) {
36 const AvahiPoll *poll_api = NULL;
37
38 assert(i);
39
40 poll_api = avahi_simple_poll_get(simple_poll_api);
41 if (i->delay_timeout)
42 poll_api->timeout_free(i->delay_timeout);
43
44 if (i->record_browser)
45 avahi_s_record_browser_free(i->record_browser);
46
47 if (i->path) {
48 dbus_connection_unregister_object_path(server->bus, i->path);
49 avahi_free(i->path);
50 }
51 AVAHI_LLIST_REMOVE(RecordBrowserInfo, record_browsers, i->client->record_browsers, i);
52
53 assert(i->client->n_objects >= 1);
54 i->client->n_objects--;
55
56 avahi_free(i);
57 }
58
avahi_dbus_record_browser_start(RecordBrowserInfo * i)59 void avahi_dbus_record_browser_start(RecordBrowserInfo *i) {
60 assert(i);
61
62 if(i->record_browser)
63 avahi_s_record_browser_start_query(i->record_browser);
64 }
65
avahi_dbus_msg_record_browser_impl(DBusConnection * c,DBusMessage * m,void * userdata)66 DBusHandlerResult avahi_dbus_msg_record_browser_impl(DBusConnection *c, DBusMessage *m, void *userdata) {
67 DBusError error;
68 RecordBrowserInfo *i = userdata;
69
70 assert(c);
71 assert(m);
72 assert(i);
73
74 dbus_error_init(&error);
75
76 avahi_log_debug(__FILE__": interface=%s, path=%s, member=%s",
77 dbus_message_get_interface(m),
78 dbus_message_get_path(m),
79 dbus_message_get_member(m));
80
81 /* Introspection */
82 if (dbus_message_is_method_call(m, DBUS_INTERFACE_INTROSPECTABLE, "Introspect"))
83 return avahi_dbus_handle_introspect(c, m, "org.freedesktop.Avahi.RecordBrowser.xml");
84
85 /* Access control */
86 if (strcmp(dbus_message_get_sender(m), i->client->name))
87 return avahi_dbus_respond_error(c, m, AVAHI_ERR_ACCESS_DENIED, NULL);
88
89 if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_RECORD_BROWSER, "Free")) {
90
91 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
92 avahi_log_warn("Error parsing RecordBrowser::Free message");
93 goto fail;
94 }
95
96 avahi_dbus_record_browser_free(i);
97 return avahi_dbus_respond_ok(c, m);
98
99 }
100
101 if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_RECORD_BROWSER, "Start")) {
102
103 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
104 avahi_log_warn("Error parsing RecordBrowser::Start message");
105 goto fail;
106 }
107
108 avahi_dbus_record_browser_start(i);
109 return avahi_dbus_respond_ok(c, m);
110
111 }
112
113
114 avahi_log_warn("Missed message %s::%s()", dbus_message_get_interface(m), dbus_message_get_member(m));
115
116 fail:
117 if (dbus_error_is_set(&error))
118 dbus_error_free(&error);
119
120 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
121 }
122
avahi_dbus_record_browser_callback(AvahiSRecordBrowser * b,AvahiIfIndex interface,AvahiProtocol protocol,AvahiBrowserEvent event,AvahiRecord * record,AvahiLookupResultFlags flags,void * userdata)123 void avahi_dbus_record_browser_callback(
124 AvahiSRecordBrowser *b,
125 AvahiIfIndex interface,
126 AvahiProtocol protocol,
127 AvahiBrowserEvent event,
128 AvahiRecord *record,
129 AvahiLookupResultFlags flags,
130 void* userdata) {
131
132 RecordBrowserInfo *i = userdata;
133 DBusMessage *m = NULL;
134 int32_t i_interface, i_protocol;
135 uint32_t u_flags;
136
137 assert(b);
138 assert(i);
139
140 i_interface = (int32_t) interface;
141 i_protocol = (int32_t) protocol;
142 u_flags = (uint32_t) flags;
143
144 m = dbus_message_new_signal(i->path, AVAHI_DBUS_INTERFACE_RECORD_BROWSER, avahi_dbus_map_browse_signal_name(event));
145
146 if (!m) {
147 avahi_log_error("Failed allocate message");
148 return;
149 }
150
151 if (event == AVAHI_BROWSER_NEW || event == AVAHI_BROWSER_REMOVE) {
152 uint8_t rdata[0xFFFF];
153 size_t size;
154 assert(record);
155
156 if (!(dbus_message_append_args(
157 m,
158 DBUS_TYPE_INT32, &i_interface,
159 DBUS_TYPE_INT32, &i_protocol,
160 DBUS_TYPE_STRING, &record->key->name,
161 DBUS_TYPE_UINT16, &record->key->clazz,
162 DBUS_TYPE_UINT16, &record->key->type,
163 DBUS_TYPE_INVALID)))
164 goto fail;
165
166 if ((size = avahi_rdata_serialize(record, rdata, sizeof(rdata))) == (size_t) -1 ||
167 avahi_dbus_append_rdata(m, rdata, size) < 0) {
168 avahi_log_debug(__FILE__": Failed to append rdata");
169 dbus_message_unref(m);
170 return;
171 }
172
173 dbus_message_append_args(
174 m,
175 DBUS_TYPE_UINT32, &u_flags,
176 DBUS_TYPE_INVALID);
177
178 } else if (event == AVAHI_BROWSER_FAILURE)
179 avahi_dbus_append_server_error(m);
180
181 dbus_message_set_destination(m, i->client->name);
182 dbus_connection_send(server->bus, m, NULL);
183 dbus_message_unref(m);
184
185 return;
186
187 fail:
188
189 if (m)
190 dbus_message_unref(m);
191 }
192