2 This file is part of avahi.
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.
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.
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
26 #include <avahi-common/malloc.h>
27 #include <avahi-common/dbus.h>
28 #include <avahi-common/error.h>
29 #include <avahi-common/domain.h>
30 #include <avahi-core/log.h>
32 #include "dbus-util.h"
33 #include "dbus-internal.h"
36 void avahi_dbus_entry_group_free(EntryGroupInfo *i) {
40 avahi_s_entry_group_free(i->entry_group);
43 dbus_connection_unregister_object_path(server->bus, i->path);
46 AVAHI_LLIST_REMOVE(EntryGroupInfo, entry_groups, i->client->entry_groups, i);
48 i->client->n_objects--;
49 assert(i->client->n_objects >= 0);
54 void avahi_dbus_entry_group_callback(AvahiServer *s, AvahiSEntryGroup *g, AvahiEntryGroupState state, void* userdata) {
55 EntryGroupInfo *i = userdata;
64 m = dbus_message_new_signal(i->path, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "StateChanged");
67 if (state == AVAHI_ENTRY_GROUP_FAILURE)
68 e = avahi_error_number_to_dbus(avahi_server_errno(s));
69 else if (state == AVAHI_ENTRY_GROUP_COLLISION)
70 e = AVAHI_DBUS_ERR_COLLISION;
72 e = AVAHI_DBUS_ERR_OK;
74 dbus_message_append_args(
79 dbus_message_set_destination(m, i->client->name);
80 dbus_connection_send(server->bus, m, NULL);
81 dbus_message_unref(m);
84 DBusHandlerResult avahi_dbus_msg_entry_group_impl(DBusConnection *c, DBusMessage *m, void *userdata) {
86 EntryGroupInfo *i = userdata;
92 dbus_error_init(&error);
94 avahi_log_debug(__FILE__": interface=%s, path=%s, member=%s",
95 dbus_message_get_interface(m),
96 dbus_message_get_path(m),
97 dbus_message_get_member(m));
100 if (dbus_message_is_method_call(m, DBUS_INTERFACE_INTROSPECTABLE, "Introspect"))
101 return avahi_dbus_handle_introspect(c, m, "EntryGroup.introspect");
104 if (strcmp(dbus_message_get_sender(m), i->client->name))
105 return avahi_dbus_respond_error(c, m, AVAHI_ERR_ACCESS_DENIED, NULL);
107 if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "Free")) {
109 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
110 avahi_log_warn("Error parsing EntryGroup::Free message");
114 avahi_dbus_entry_group_free(i);
115 return avahi_dbus_respond_ok(c, m);
117 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "Commit")) {
119 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
120 avahi_log_warn("Error parsing EntryGroup::Commit message");
124 if (avahi_s_entry_group_commit(i->entry_group) < 0)
125 return avahi_dbus_respond_error(c, m, avahi_server_errno(avahi_server), NULL);
127 return avahi_dbus_respond_ok(c, m);
130 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "Reset")) {
132 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
133 avahi_log_warn("Error parsing EntryGroup::Reset message");
137 avahi_s_entry_group_reset(i->entry_group);
139 return avahi_dbus_respond_ok(c, m);
141 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "IsEmpty")) {
143 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
144 avahi_log_warn("Error parsing EntryGroup::IsEmpty message");
148 return avahi_dbus_respond_boolean(c, m, !!avahi_s_entry_group_is_empty(i->entry_group));
150 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "GetState")) {
151 AvahiEntryGroupState state;
153 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
154 avahi_log_warn("Error parsing EntryGroup::GetState message");
158 state = avahi_s_entry_group_get_state(i->entry_group);
159 return avahi_dbus_respond_int32(c, m, (int32_t) state);
161 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "AddService")) {
162 int32_t interface, protocol;
164 char *type, *name, *domain, *host;
166 AvahiStringList *strlst = NULL;
168 if (!dbus_message_get_args(
170 DBUS_TYPE_INT32, &interface,
171 DBUS_TYPE_INT32, &protocol,
172 DBUS_TYPE_UINT32, &flags,
173 DBUS_TYPE_STRING, &name,
174 DBUS_TYPE_STRING, &type,
175 DBUS_TYPE_STRING, &domain,
176 DBUS_TYPE_STRING, &host,
177 DBUS_TYPE_UINT16, &port,
178 DBUS_TYPE_INVALID) ||
180 avahi_dbus_read_strlst(m, 8, &strlst) < 0) {
181 avahi_log_warn("Error parsing EntryGroup::AddService message");
185 if (!(flags & AVAHI_PUBLISH_UPDATE) && i->n_entries >= ENTRIES_PER_ENTRY_GROUP_MAX) {
186 avahi_string_list_free(strlst);
187 return avahi_dbus_respond_error(c, m, AVAHI_ERR_TOO_MANY_ENTRIES, NULL);
190 if (domain && !*domain)
196 if (avahi_server_add_service_strlst(avahi_server, i->entry_group, (AvahiIfIndex) interface, (AvahiProtocol) protocol, (AvahiPublishFlags) flags, name, type, domain, host, port, strlst) < 0) {
197 avahi_string_list_free(strlst);
198 return avahi_dbus_respond_error(c, m, avahi_server_errno(avahi_server), NULL);
201 if (!(flags & AVAHI_PUBLISH_UPDATE))
204 avahi_string_list_free(strlst);
206 return avahi_dbus_respond_ok(c, m);
208 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "AddServiceSubtype")) {
210 int32_t interface, protocol;
212 char *type, *name, *domain, *subtype;
214 if (!dbus_message_get_args(
216 DBUS_TYPE_INT32, &interface,
217 DBUS_TYPE_INT32, &protocol,
218 DBUS_TYPE_UINT32, &flags,
219 DBUS_TYPE_STRING, &name,
220 DBUS_TYPE_STRING, &type,
221 DBUS_TYPE_STRING, &domain,
222 DBUS_TYPE_STRING, &subtype,
223 DBUS_TYPE_INVALID) || !type || !name || !subtype) {
224 avahi_log_warn("Error parsing EntryGroup::AddServiceSubtype message");
228 if (!(flags & AVAHI_PUBLISH_UPDATE) && i->n_entries >= ENTRIES_PER_ENTRY_GROUP_MAX)
229 return avahi_dbus_respond_error(c, m, AVAHI_ERR_TOO_MANY_ENTRIES, NULL);
231 if (domain && !*domain)
234 if (avahi_server_add_service_subtype(avahi_server, i->entry_group, (AvahiIfIndex) interface, (AvahiProtocol) protocol, (AvahiPublishFlags) flags, name, type, domain, subtype) < 0)
235 return avahi_dbus_respond_error(c, m, avahi_server_errno(avahi_server), NULL);
237 if (!(flags & AVAHI_PUBLISH_UPDATE))
240 return avahi_dbus_respond_ok(c, m);
242 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "UpdateServiceTxt")) {
243 int32_t interface, protocol;
245 char *type, *name, *domain;
246 AvahiStringList *strlst;
248 if (!dbus_message_get_args(
250 DBUS_TYPE_INT32, &interface,
251 DBUS_TYPE_INT32, &protocol,
252 DBUS_TYPE_UINT32, &flags,
253 DBUS_TYPE_STRING, &name,
254 DBUS_TYPE_STRING, &type,
255 DBUS_TYPE_STRING, &domain,
256 DBUS_TYPE_INVALID) ||
258 avahi_dbus_read_strlst(m, 6, &strlst)) {
259 avahi_log_warn("Error parsing EntryGroup::UpdateServiceTxt message");
263 if (domain && !*domain)
266 if (avahi_server_update_service_txt_strlst(avahi_server, i->entry_group, (AvahiIfIndex) interface, (AvahiProtocol) protocol, (AvahiPublishFlags) flags, name, type, domain, strlst) < 0) {
267 avahi_string_list_free(strlst);
268 return avahi_dbus_respond_error(c, m, avahi_server_errno(avahi_server), NULL);
271 avahi_string_list_free(strlst);
273 return avahi_dbus_respond_ok(c, m);
275 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "AddAddress")) {
276 int32_t interface, protocol;
278 char *name, *address;
281 if (!dbus_message_get_args(
283 DBUS_TYPE_INT32, &interface,
284 DBUS_TYPE_INT32, &protocol,
285 DBUS_TYPE_UINT32, &flags,
286 DBUS_TYPE_STRING, &name,
287 DBUS_TYPE_STRING, &address,
288 DBUS_TYPE_INVALID) || !name || !address) {
289 avahi_log_warn("Error parsing EntryGroup::AddAddress message");
293 if (!(flags & AVAHI_PUBLISH_UPDATE) && i->n_entries >= ENTRIES_PER_ENTRY_GROUP_MAX)
294 return avahi_dbus_respond_error(c, m, AVAHI_ERR_TOO_MANY_ENTRIES, NULL);
296 if (!(avahi_address_parse(address, AVAHI_PROTO_UNSPEC, &a)))
297 return avahi_dbus_respond_error(c, m, AVAHI_ERR_INVALID_ADDRESS, NULL);
299 if (avahi_server_add_address(avahi_server, i->entry_group, (AvahiIfIndex) interface, (AvahiProtocol) protocol, (AvahiPublishFlags) flags, name, &a) < 0)
300 return avahi_dbus_respond_error(c, m, avahi_server_errno(avahi_server), NULL);
302 if (!(flags & AVAHI_PUBLISH_UPDATE))
305 return avahi_dbus_respond_ok(c, m);
306 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "AddRecord")) {
307 int32_t interface, protocol;
308 uint32_t flags, ttl, size;
309 uint16_t clazz, type;
314 if (!dbus_message_get_args(
316 DBUS_TYPE_INT32, &interface,
317 DBUS_TYPE_INT32, &protocol,
318 DBUS_TYPE_UINT32, &flags,
319 DBUS_TYPE_STRING, &name,
320 DBUS_TYPE_UINT16, &clazz,
321 DBUS_TYPE_UINT16, &type,
322 DBUS_TYPE_UINT32, &ttl,
323 DBUS_TYPE_INVALID) || !name ||
324 avahi_dbus_read_rdata (m, 7, &rdata, &size)) {
325 avahi_log_warn("Error parsing EntryGroup::AddRecord message");
329 if (!(flags & AVAHI_PUBLISH_UPDATE) && i->n_entries >= ENTRIES_PER_ENTRY_GROUP_MAX)
330 return avahi_dbus_respond_error(c, m, AVAHI_ERR_TOO_MANY_ENTRIES, NULL);
332 if (!avahi_is_valid_domain_name (name))
333 return avahi_dbus_respond_error(c, m, AVAHI_ERR_INVALID_DOMAIN_NAME, NULL);
335 if (!(r = avahi_record_new_full (name, clazz, type, ttl)))
336 return avahi_dbus_respond_error(c, m, AVAHI_ERR_NO_MEMORY, NULL);
338 if (avahi_rdata_parse (r, rdata, size) < 0) {
339 avahi_record_unref (r);
340 return avahi_dbus_respond_error(c, m, AVAHI_ERR_INVALID_RDATA, NULL);
343 if (avahi_server_add(avahi_server, i->entry_group, (AvahiIfIndex) interface, (AvahiProtocol) protocol, (AvahiPublishFlags) flags, r) < 0) {
344 avahi_record_unref (r);
345 return avahi_dbus_respond_error(c, m, avahi_server_errno(avahi_server), NULL);
348 if (!(flags & AVAHI_PUBLISH_UPDATE))
351 avahi_record_unref (r);
353 return avahi_dbus_respond_ok(c, m);
357 avahi_log_warn("Missed message %s::%s()", dbus_message_get_interface(m), dbus_message_get_member(m));
360 if (dbus_error_is_set(&error))
361 dbus_error_free(&error);
363 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;