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);
45 dbus_connection_unregister_object_path(server->bus, i->path);
48 AVAHI_LLIST_REMOVE(EntryGroupInfo, entry_groups, i->client->entry_groups, i);
50 i->client->n_objects--;
51 assert(i->client->n_objects >= 0);
56 void avahi_dbus_entry_group_callback(AvahiServer *s, AvahiSEntryGroup *g, AvahiEntryGroupState state, void* userdata) {
57 EntryGroupInfo *i = userdata;
66 m = dbus_message_new_signal(i->path, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "StateChanged");
69 if (state == AVAHI_ENTRY_GROUP_FAILURE)
70 e = avahi_error_number_to_dbus(avahi_server_errno(s));
71 else if (state == AVAHI_ENTRY_GROUP_COLLISION)
72 e = AVAHI_DBUS_ERR_COLLISION;
74 e = AVAHI_DBUS_ERR_OK;
76 dbus_message_append_args(
81 dbus_message_set_destination(m, i->client->name);
82 dbus_connection_send(server->bus, m, NULL);
83 dbus_message_unref(m);
86 DBusHandlerResult avahi_dbus_msg_entry_group_impl(DBusConnection *c, DBusMessage *m, void *userdata) {
88 EntryGroupInfo *i = userdata;
94 dbus_error_init(&error);
96 avahi_log_debug(__FILE__": interface=%s, path=%s, member=%s",
97 dbus_message_get_interface(m),
98 dbus_message_get_path(m),
99 dbus_message_get_member(m));
102 if (dbus_message_is_method_call(m, DBUS_INTERFACE_INTROSPECTABLE, "Introspect"))
103 return avahi_dbus_handle_introspect(c, m, "EntryGroup.introspect");
106 if (strcmp(dbus_message_get_sender(m), i->client->name))
107 return avahi_dbus_respond_error(c, m, AVAHI_ERR_ACCESS_DENIED, NULL);
109 if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "Free")) {
111 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
112 avahi_log_warn("Error parsing EntryGroup::Free message");
116 avahi_dbus_entry_group_free(i);
117 return avahi_dbus_respond_ok(c, m);
119 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "Commit")) {
121 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
122 avahi_log_warn("Error parsing EntryGroup::Commit message");
126 if (avahi_s_entry_group_commit(i->entry_group) < 0)
127 return avahi_dbus_respond_error(c, m, avahi_server_errno(avahi_server), NULL);
129 return avahi_dbus_respond_ok(c, m);
132 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "Reset")) {
134 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
135 avahi_log_warn("Error parsing EntryGroup::Reset message");
139 avahi_s_entry_group_reset(i->entry_group);
141 return avahi_dbus_respond_ok(c, m);
143 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "IsEmpty")) {
145 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
146 avahi_log_warn("Error parsing EntryGroup::IsEmpty message");
150 return avahi_dbus_respond_boolean(c, m, !!avahi_s_entry_group_is_empty(i->entry_group));
152 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "GetState")) {
153 AvahiEntryGroupState state;
155 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
156 avahi_log_warn("Error parsing EntryGroup::GetState message");
160 state = avahi_s_entry_group_get_state(i->entry_group);
161 return avahi_dbus_respond_int32(c, m, (int32_t) state);
163 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "AddService")) {
164 int32_t interface, protocol;
166 char *type, *name, *domain, *host;
168 AvahiStringList *strlst = NULL;
170 if (!dbus_message_get_args(
172 DBUS_TYPE_INT32, &interface,
173 DBUS_TYPE_INT32, &protocol,
174 DBUS_TYPE_UINT32, &flags,
175 DBUS_TYPE_STRING, &name,
176 DBUS_TYPE_STRING, &type,
177 DBUS_TYPE_STRING, &domain,
178 DBUS_TYPE_STRING, &host,
179 DBUS_TYPE_UINT16, &port,
180 DBUS_TYPE_INVALID) ||
182 avahi_dbus_read_strlst(m, 8, &strlst) < 0) {
183 avahi_log_warn("Error parsing EntryGroup::AddService message");
187 if (!(flags & AVAHI_PUBLISH_UPDATE) && i->n_entries >= ENTRIES_PER_ENTRY_GROUP_MAX) {
188 avahi_string_list_free(strlst);
189 return avahi_dbus_respond_error(c, m, AVAHI_ERR_TOO_MANY_ENTRIES, NULL);
192 if (domain && !*domain)
198 if (avahi_server_add_service_strlst(avahi_server, i->entry_group, (AvahiIfIndex) interface, (AvahiProtocol) protocol, (AvahiPublishFlags) flags, name, type, domain, host, port, strlst) < 0) {
199 avahi_string_list_free(strlst);
200 return avahi_dbus_respond_error(c, m, avahi_server_errno(avahi_server), NULL);
203 if (!(flags & AVAHI_PUBLISH_UPDATE))
206 avahi_string_list_free(strlst);
208 return avahi_dbus_respond_ok(c, m);
210 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "AddServiceSubtype")) {
212 int32_t interface, protocol;
214 char *type, *name, *domain, *subtype;
216 if (!dbus_message_get_args(
218 DBUS_TYPE_INT32, &interface,
219 DBUS_TYPE_INT32, &protocol,
220 DBUS_TYPE_UINT32, &flags,
221 DBUS_TYPE_STRING, &name,
222 DBUS_TYPE_STRING, &type,
223 DBUS_TYPE_STRING, &domain,
224 DBUS_TYPE_STRING, &subtype,
225 DBUS_TYPE_INVALID) || !type || !name || !subtype) {
226 avahi_log_warn("Error parsing EntryGroup::AddServiceSubtype message");
230 if (!(flags & AVAHI_PUBLISH_UPDATE) && i->n_entries >= ENTRIES_PER_ENTRY_GROUP_MAX)
231 return avahi_dbus_respond_error(c, m, AVAHI_ERR_TOO_MANY_ENTRIES, NULL);
233 if (domain && !*domain)
236 if (avahi_server_add_service_subtype(avahi_server, i->entry_group, (AvahiIfIndex) interface, (AvahiProtocol) protocol, (AvahiPublishFlags) flags, name, type, domain, subtype) < 0)
237 return avahi_dbus_respond_error(c, m, avahi_server_errno(avahi_server), NULL);
239 if (!(flags & AVAHI_PUBLISH_UPDATE))
242 return avahi_dbus_respond_ok(c, m);
244 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "UpdateServiceTxt")) {
245 int32_t interface, protocol;
247 char *type, *name, *domain;
248 AvahiStringList *strlst;
250 if (!dbus_message_get_args(
252 DBUS_TYPE_INT32, &interface,
253 DBUS_TYPE_INT32, &protocol,
254 DBUS_TYPE_UINT32, &flags,
255 DBUS_TYPE_STRING, &name,
256 DBUS_TYPE_STRING, &type,
257 DBUS_TYPE_STRING, &domain,
258 DBUS_TYPE_INVALID) ||
260 avahi_dbus_read_strlst(m, 6, &strlst)) {
261 avahi_log_warn("Error parsing EntryGroup::UpdateServiceTxt message");
265 if (domain && !*domain)
268 if (avahi_server_update_service_txt_strlst(avahi_server, i->entry_group, (AvahiIfIndex) interface, (AvahiProtocol) protocol, (AvahiPublishFlags) flags, name, type, domain, strlst) < 0) {
269 avahi_string_list_free(strlst);
270 return avahi_dbus_respond_error(c, m, avahi_server_errno(avahi_server), NULL);
273 avahi_string_list_free(strlst);
275 return avahi_dbus_respond_ok(c, m);
277 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "AddAddress")) {
278 int32_t interface, protocol;
280 char *name, *address;
283 if (!dbus_message_get_args(
285 DBUS_TYPE_INT32, &interface,
286 DBUS_TYPE_INT32, &protocol,
287 DBUS_TYPE_UINT32, &flags,
288 DBUS_TYPE_STRING, &name,
289 DBUS_TYPE_STRING, &address,
290 DBUS_TYPE_INVALID) || !name || !address) {
291 avahi_log_warn("Error parsing EntryGroup::AddAddress message");
295 if (!(flags & AVAHI_PUBLISH_UPDATE) && i->n_entries >= ENTRIES_PER_ENTRY_GROUP_MAX)
296 return avahi_dbus_respond_error(c, m, AVAHI_ERR_TOO_MANY_ENTRIES, NULL);
298 if (!(avahi_address_parse(address, AVAHI_PROTO_UNSPEC, &a)))
299 return avahi_dbus_respond_error(c, m, AVAHI_ERR_INVALID_ADDRESS, NULL);
301 if (avahi_server_add_address(avahi_server, i->entry_group, (AvahiIfIndex) interface, (AvahiProtocol) protocol, (AvahiPublishFlags) flags, name, &a) < 0)
302 return avahi_dbus_respond_error(c, m, avahi_server_errno(avahi_server), NULL);
304 if (!(flags & AVAHI_PUBLISH_UPDATE))
307 return avahi_dbus_respond_ok(c, m);
308 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "AddRecord")) {
309 int32_t interface, protocol;
310 uint32_t flags, ttl, size;
311 uint16_t clazz, type;
316 if (!dbus_message_get_args(
318 DBUS_TYPE_INT32, &interface,
319 DBUS_TYPE_INT32, &protocol,
320 DBUS_TYPE_UINT32, &flags,
321 DBUS_TYPE_STRING, &name,
322 DBUS_TYPE_UINT16, &clazz,
323 DBUS_TYPE_UINT16, &type,
324 DBUS_TYPE_UINT32, &ttl,
325 DBUS_TYPE_INVALID) || !name ||
326 avahi_dbus_read_rdata (m, 7, &rdata, &size)) {
327 avahi_log_warn("Error parsing EntryGroup::AddRecord message");
331 if (!(flags & AVAHI_PUBLISH_UPDATE) && i->n_entries >= ENTRIES_PER_ENTRY_GROUP_MAX)
332 return avahi_dbus_respond_error(c, m, AVAHI_ERR_TOO_MANY_ENTRIES, NULL);
334 if (!avahi_is_valid_domain_name (name))
335 return avahi_dbus_respond_error(c, m, AVAHI_ERR_INVALID_DOMAIN_NAME, NULL);
337 if (!(r = avahi_record_new_full (name, clazz, type, ttl)))
338 return avahi_dbus_respond_error(c, m, AVAHI_ERR_NO_MEMORY, NULL);
340 if (avahi_rdata_parse (r, rdata, size) < 0) {
341 avahi_record_unref (r);
342 return avahi_dbus_respond_error(c, m, AVAHI_ERR_INVALID_RDATA, NULL);
345 if (avahi_server_add(avahi_server, i->entry_group, (AvahiIfIndex) interface, (AvahiProtocol) protocol, (AvahiPublishFlags) flags, r) < 0) {
346 avahi_record_unref (r);
347 return avahi_dbus_respond_error(c, m, avahi_server_errno(avahi_server), NULL);
350 if (!(flags & AVAHI_PUBLISH_UPDATE))
353 avahi_record_unref (r);
355 return avahi_dbus_respond_ok(c, m);
359 avahi_log_warn("Missed message %s::%s()", dbus_message_get_interface(m), dbus_message_get_member(m));
362 if (dbus_error_is_set(&error))
363 dbus_error_free(&error);
365 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;