4 This file is part of avahi.
6 avahi is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
11 avahi is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
14 Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with avahi; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
28 #include <avahi-common/malloc.h>
29 #include <avahi-common/dbus.h>
30 #include <avahi-common/error.h>
31 #include <avahi-common/domain.h>
32 #include <avahi-core/log.h>
34 #include "dbus-util.h"
35 #include "dbus-internal.h"
38 void avahi_dbus_entry_group_free(EntryGroupInfo *i) {
42 avahi_s_entry_group_free(i->entry_group);
43 dbus_connection_unregister_object_path(server->bus, i->path);
45 AVAHI_LLIST_REMOVE(EntryGroupInfo, entry_groups, i->client->entry_groups, i);
47 i->client->n_objects--;
48 assert(i->client->n_objects >= 0);
53 void avahi_dbus_entry_group_callback(AvahiServer *s, AvahiSEntryGroup *g, AvahiEntryGroupState state, void* userdata) {
54 EntryGroupInfo *i = userdata;
63 m = dbus_message_new_signal(i->path, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "StateChanged");
66 if (state == AVAHI_ENTRY_GROUP_FAILURE)
67 e = avahi_error_number_to_dbus(avahi_server_errno(s));
68 else if (state == AVAHI_ENTRY_GROUP_COLLISION)
69 e = AVAHI_DBUS_ERR_COLLISION;
71 e = AVAHI_DBUS_ERR_OK;
73 dbus_message_append_args(
78 dbus_message_set_destination(m, i->client->name);
79 dbus_connection_send(server->bus, m, NULL);
80 dbus_message_unref(m);
83 DBusHandlerResult avahi_dbus_msg_entry_group_impl(DBusConnection *c, DBusMessage *m, void *userdata) {
85 EntryGroupInfo *i = userdata;
91 dbus_error_init(&error);
93 avahi_log_debug(__FILE__": interface=%s, path=%s, member=%s",
94 dbus_message_get_interface(m),
95 dbus_message_get_path(m),
96 dbus_message_get_member(m));
99 if (dbus_message_is_method_call(m, DBUS_INTERFACE_INTROSPECTABLE, "Introspect"))
100 return avahi_dbus_handle_introspect(c, m, "EntryGroup.introspect");
103 if (strcmp(dbus_message_get_sender(m), i->client->name))
104 return avahi_dbus_respond_error(c, m, AVAHI_ERR_ACCESS_DENIED, NULL);
106 if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "Free")) {
108 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
109 avahi_log_warn("Error parsing EntryGroup::Free message");
113 avahi_dbus_entry_group_free(i);
114 return avahi_dbus_respond_ok(c, m);
116 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "Commit")) {
118 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
119 avahi_log_warn("Error parsing EntryGroup::Commit message");
123 if (avahi_s_entry_group_commit(i->entry_group) < 0)
124 return avahi_dbus_respond_error(c, m, avahi_server_errno(avahi_server), NULL);
126 return avahi_dbus_respond_ok(c, m);
129 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "Reset")) {
131 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
132 avahi_log_warn("Error parsing EntryGroup::Reset message");
136 avahi_s_entry_group_reset(i->entry_group);
138 return avahi_dbus_respond_ok(c, m);
140 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "IsEmpty")) {
142 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
143 avahi_log_warn("Error parsing EntryGroup::IsEmpty message");
147 return avahi_dbus_respond_boolean(c, m, !!avahi_s_entry_group_is_empty(i->entry_group));
149 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "GetState")) {
150 AvahiEntryGroupState state;
152 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
153 avahi_log_warn("Error parsing EntryGroup::GetState message");
157 state = avahi_s_entry_group_get_state(i->entry_group);
158 return avahi_dbus_respond_int32(c, m, (int32_t) state);
160 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "AddService")) {
161 int32_t interface, protocol;
163 char *type, *name, *domain, *host;
165 AvahiStringList *strlst = NULL;
167 if (!dbus_message_get_args(
169 DBUS_TYPE_INT32, &interface,
170 DBUS_TYPE_INT32, &protocol,
171 DBUS_TYPE_UINT32, &flags,
172 DBUS_TYPE_STRING, &name,
173 DBUS_TYPE_STRING, &type,
174 DBUS_TYPE_STRING, &domain,
175 DBUS_TYPE_STRING, &host,
176 DBUS_TYPE_UINT16, &port,
177 DBUS_TYPE_INVALID) ||
179 avahi_dbus_read_strlst(m, 8, &strlst) < 0) {
180 avahi_log_warn("Error parsing EntryGroup::AddService message");
184 if (!(flags & AVAHI_PUBLISH_UPDATE) && i->n_entries >= ENTRIES_PER_ENTRY_GROUP_MAX) {
185 avahi_string_list_free(strlst);
186 return avahi_dbus_respond_error(c, m, AVAHI_ERR_TOO_MANY_ENTRIES, NULL);
189 if (domain && !*domain)
195 if (avahi_server_add_service_strlst(avahi_server, i->entry_group, (AvahiIfIndex) interface, (AvahiProtocol) protocol, (AvahiPublishFlags) flags, name, type, domain, host, port, strlst) < 0) {
196 avahi_string_list_free(strlst);
197 return avahi_dbus_respond_error(c, m, avahi_server_errno(avahi_server), NULL);
200 if (!(flags & AVAHI_PUBLISH_UPDATE))
203 avahi_string_list_free(strlst);
205 return avahi_dbus_respond_ok(c, m);
207 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "AddServiceSubtype")) {
209 int32_t interface, protocol;
211 char *type, *name, *domain, *subtype;
213 if (!dbus_message_get_args(
215 DBUS_TYPE_INT32, &interface,
216 DBUS_TYPE_INT32, &protocol,
217 DBUS_TYPE_UINT32, &flags,
218 DBUS_TYPE_STRING, &name,
219 DBUS_TYPE_STRING, &type,
220 DBUS_TYPE_STRING, &domain,
221 DBUS_TYPE_STRING, &subtype,
222 DBUS_TYPE_INVALID) || !type || !name || !subtype) {
223 avahi_log_warn("Error parsing EntryGroup::AddServiceSubtype message");
227 if (!(flags & AVAHI_PUBLISH_UPDATE) && i->n_entries >= ENTRIES_PER_ENTRY_GROUP_MAX)
228 return avahi_dbus_respond_error(c, m, AVAHI_ERR_TOO_MANY_ENTRIES, NULL);
230 if (domain && !*domain)
233 if (avahi_server_add_service_subtype(avahi_server, i->entry_group, (AvahiIfIndex) interface, (AvahiProtocol) protocol, (AvahiPublishFlags) flags, name, type, domain, subtype) < 0)
234 return avahi_dbus_respond_error(c, m, avahi_server_errno(avahi_server), NULL);
236 if (!(flags & AVAHI_PUBLISH_UPDATE))
239 return avahi_dbus_respond_ok(c, m);
241 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "UpdateServiceTxt")) {
242 int32_t interface, protocol;
244 char *type, *name, *domain;
245 AvahiStringList *strlst;
247 if (!dbus_message_get_args(
249 DBUS_TYPE_INT32, &interface,
250 DBUS_TYPE_INT32, &protocol,
251 DBUS_TYPE_UINT32, &flags,
252 DBUS_TYPE_STRING, &name,
253 DBUS_TYPE_STRING, &type,
254 DBUS_TYPE_STRING, &domain,
255 DBUS_TYPE_INVALID) ||
257 avahi_dbus_read_strlst(m, 6, &strlst)) {
258 avahi_log_warn("Error parsing EntryGroup::UpdateServiceTxt message");
262 if (domain && !*domain)
265 if (avahi_server_update_service_txt_strlst(avahi_server, i->entry_group, (AvahiIfIndex) interface, (AvahiProtocol) protocol, (AvahiPublishFlags) flags, name, type, domain, strlst) < 0) {
266 avahi_string_list_free(strlst);
267 return avahi_dbus_respond_error(c, m, avahi_server_errno(avahi_server), NULL);
270 avahi_string_list_free(strlst);
272 return avahi_dbus_respond_ok(c, m);
274 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "AddAddress")) {
275 int32_t interface, protocol;
277 char *name, *address;
280 if (!dbus_message_get_args(
282 DBUS_TYPE_INT32, &interface,
283 DBUS_TYPE_INT32, &protocol,
284 DBUS_TYPE_UINT32, &flags,
285 DBUS_TYPE_STRING, &name,
286 DBUS_TYPE_STRING, &address,
287 DBUS_TYPE_INVALID) || !name || !address) {
288 avahi_log_warn("Error parsing EntryGroup::AddAddress message");
292 if (!(flags & AVAHI_PUBLISH_UPDATE) && i->n_entries >= ENTRIES_PER_ENTRY_GROUP_MAX)
293 return avahi_dbus_respond_error(c, m, AVAHI_ERR_TOO_MANY_ENTRIES, NULL);
295 if (!(avahi_address_parse(address, AVAHI_PROTO_UNSPEC, &a)))
296 return avahi_dbus_respond_error(c, m, AVAHI_ERR_INVALID_ADDRESS, NULL);
298 if (avahi_server_add_address(avahi_server, i->entry_group, (AvahiIfIndex) interface, (AvahiProtocol) protocol, (AvahiPublishFlags) flags, name, &a) < 0)
299 return avahi_dbus_respond_error(c, m, avahi_server_errno(avahi_server), NULL);
301 if (!(flags & AVAHI_PUBLISH_UPDATE))
304 return avahi_dbus_respond_ok(c, m);
305 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "AddRecord")) {
306 int32_t interface, protocol;
307 uint32_t flags, ttl, size;
308 uint16_t clazz, type;
313 if (!dbus_message_get_args(
315 DBUS_TYPE_INT32, &interface,
316 DBUS_TYPE_INT32, &protocol,
317 DBUS_TYPE_UINT32, &flags,
318 DBUS_TYPE_STRING, &name,
319 DBUS_TYPE_UINT16, &clazz,
320 DBUS_TYPE_UINT16, &type,
321 DBUS_TYPE_UINT32, &ttl,
322 DBUS_TYPE_INVALID) || !name ||
323 avahi_dbus_read_rdata (m, 7, &rdata, &size)) {
324 avahi_log_warn("Error parsing EntryGroup::AddRecord message");
328 if (!(flags & AVAHI_PUBLISH_UPDATE) && i->n_entries >= ENTRIES_PER_ENTRY_GROUP_MAX)
329 return avahi_dbus_respond_error(c, m, AVAHI_ERR_TOO_MANY_ENTRIES, NULL);
331 if (!avahi_is_valid_domain_name (name))
332 return avahi_dbus_respond_error(c, m, AVAHI_ERR_INVALID_DOMAIN_NAME, NULL);
334 if (!(r = avahi_record_new_full (name, clazz, type, ttl)))
335 return avahi_dbus_respond_error(c, m, AVAHI_ERR_NO_MEMORY, NULL);
337 if (avahi_rdata_parse (r, rdata, size) < 0) {
338 avahi_record_unref (r);
339 return avahi_dbus_respond_error(c, m, AVAHI_ERR_INVALID_RDATA, NULL);
342 if (avahi_server_add(avahi_server, i->entry_group, (AvahiIfIndex) interface, (AvahiProtocol) protocol, (AvahiPublishFlags) flags, r) < 0) {
343 avahi_record_unref (r);
344 return avahi_dbus_respond_error(c, m, avahi_server_errno(avahi_server), NULL);
347 if (!(flags & AVAHI_PUBLISH_UPDATE))
350 avahi_record_unref (r);
352 return avahi_dbus_respond_ok(c, m);
356 avahi_log_warn("Missed message %s::%s()", dbus_message_get_interface(m), dbus_message_get_member(m));
359 if (dbus_error_is_set(&error))
360 dbus_error_free(&error);
362 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;