]> git.meshlink.io Git - catta/blob - avahi-client/entrygroup.c
* split client.h into client.h, lookup.h and publish.h just like we did on the server...
[catta] / avahi-client / entrygroup.c
1 /* $Id$ */
2
3 /***
4   This file is part of avahi.
5  
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.
10  
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.
15  
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
19   USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29
30 #include <dbus/dbus.h>
31
32 #include <avahi-client/client.h>
33 #include <avahi-common/dbus.h>
34 #include <avahi-common/llist.h>
35 #include <avahi-common/error.h>
36 #include <avahi-common/malloc.h>
37
38 #include "client.h"
39 #include "internal.h"
40
41 void avahi_entry_group_set_state(AvahiEntryGroup *group, AvahiEntryGroupState state) {
42     assert(group);
43
44     if (group->state == state)
45         return;
46
47     group->state = state;
48
49     if (group->callback)
50         group->callback(group, state, group->userdata);
51 }
52
53 static int retrieve_state(AvahiEntryGroup *group) {
54     DBusMessage *message, *reply;
55     DBusError error;
56     int r = AVAHI_OK;
57     int32_t state;
58     AvahiClient *client;
59     
60     dbus_error_init(&error);
61
62     assert(group);
63     client = group->client;
64     
65     if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, group->path, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "GetState"))) {
66         r = avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
67         goto fail;
68     }
69         
70     if (!(reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error)) ||
71         dbus_error_is_set (&error)) {
72         r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
73         goto fail;
74     }
75     
76     if (!dbus_message_get_args(reply, &error, DBUS_TYPE_INT32, &state, DBUS_TYPE_INVALID) ||
77         dbus_error_is_set (&error)) {
78         r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
79         goto fail;
80     }
81
82     dbus_message_unref(message);
83     dbus_message_unref(reply);
84
85     avahi_entry_group_set_state(group, (AvahiEntryGroupState) state);
86     
87     return AVAHI_OK;
88     
89 fail:
90     if (dbus_error_is_set(&error)) {
91         r = avahi_client_set_dbus_error(client, &error);
92         dbus_error_free(&error);
93     }
94
95     if (message)
96         dbus_message_unref(message);
97
98     if (reply)
99         dbus_message_unref(reply);
100
101     return r;
102 }
103
104 AvahiEntryGroup* avahi_entry_group_new (AvahiClient *client, AvahiEntryGroupCallback callback, void *userdata) {
105     AvahiEntryGroup *group = NULL;
106     DBusMessage *message = NULL, *reply = NULL;
107     DBusError error;
108     char *path;
109
110     assert(client);
111
112     dbus_error_init (&error);
113
114     if (client->state == AVAHI_CLIENT_DISCONNECTED) {
115         avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
116         goto fail;
117     }
118
119     if (!(group = avahi_new(AvahiEntryGroup, 1))) {
120         avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
121         goto fail;
122     }
123     
124     group->client = client;
125     group->callback = callback;
126     group->userdata = userdata;
127     group->state = AVAHI_ENTRY_GROUP_UNCOMMITED;
128     group->path = NULL;
129     AVAHI_LLIST_PREPEND(AvahiEntryGroup, groups, client->groups, group);
130     
131     if (!(message = dbus_message_new_method_call(
132               AVAHI_DBUS_NAME,
133               AVAHI_DBUS_PATH_SERVER,
134               AVAHI_DBUS_INTERFACE_SERVER,
135               "EntryGroupNew"))) {
136         avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
137         goto fail;
138     }
139
140     if (!(reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error)) ||
141         dbus_error_is_set (&error)) {
142         avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
143         goto fail;
144     }
145
146     if (!dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID) ||
147         dbus_error_is_set (&error)) {
148         avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
149         goto fail;
150     }
151     
152     if (!(group->path = avahi_strdup (path))) {
153
154         /* FIXME: We don't remove the object on the server side */
155
156         avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
157         goto fail;
158     }
159
160     if (retrieve_state(group) < 0)
161         goto fail;
162
163     dbus_message_unref(message);
164     dbus_message_unref(reply);
165
166     return group;
167
168 fail:
169     if (dbus_error_is_set(&error)) {
170         avahi_client_set_dbus_error(client, &error);
171         dbus_error_free(&error);
172     }
173
174     if (group)
175         avahi_entry_group_free(group);
176     
177     if (message)
178         dbus_message_unref(message);
179
180     if (reply)
181         dbus_message_unref(reply);
182     
183     return NULL;
184 }
185
186 static int entry_group_simple_method_call(AvahiEntryGroup *group, const char *method) {
187     DBusMessage *message = NULL, *reply = NULL;
188     DBusError error;
189     int r = AVAHI_OK;
190     AvahiClient *client;
191     
192     dbus_error_init(&error);
193
194     assert(group);
195     client = group->client;
196     
197     if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, group->path, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, method))) {
198         r = avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
199         goto fail;
200     }
201         
202     if (!(reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error)) ||
203         dbus_error_is_set (&error)) {
204         r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
205         goto fail;
206     }
207     
208     if (!dbus_message_get_args(reply, &error, DBUS_TYPE_INVALID) ||
209         dbus_error_is_set (&error)) {
210         r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
211         goto fail;
212     }
213
214     dbus_message_unref(message);
215     dbus_message_unref(reply);
216
217     return AVAHI_OK;
218     
219 fail:
220     if (dbus_error_is_set(&error)) {
221         r = avahi_client_set_dbus_error(client, &error);
222         dbus_error_free(&error);
223     }
224
225     if (message)
226         dbus_message_unref(message);
227
228     if (reply)
229         dbus_message_unref(reply);
230
231     return r;
232 }
233
234 int avahi_entry_group_free(AvahiEntryGroup *group) {
235     AvahiClient *client = group->client;
236     int r = AVAHI_OK;
237         
238     assert(group);
239     
240     if (group->path && client->state != AVAHI_CLIENT_DISCONNECTED)
241         r = entry_group_simple_method_call(group, "Free");
242     
243     AVAHI_LLIST_REMOVE(AvahiEntryGroup, groups, client->groups, group);
244
245     avahi_free(group->path);
246     avahi_free(group);
247
248     return r;
249 }
250
251 int avahi_entry_group_commit(AvahiEntryGroup *group) {
252     assert(group);
253     
254     if (!group->path || group->client->state == AVAHI_CLIENT_DISCONNECTED)
255         return avahi_client_set_errno(group->client, AVAHI_ERR_BAD_STATE);
256
257     return entry_group_simple_method_call(group, "Commit");
258 }
259
260 int avahi_entry_group_reset(AvahiEntryGroup *group) {
261     assert(group);
262     
263     if (!group->path || group->client->state == AVAHI_CLIENT_DISCONNECTED)
264         return avahi_client_set_errno(group->client, AVAHI_ERR_BAD_STATE);
265
266     return entry_group_simple_method_call(group, "Reset");
267 }
268
269 int avahi_entry_group_get_state (AvahiEntryGroup *group) {
270     assert (group);
271
272     return group->state;
273 }
274
275 AvahiClient* avahi_entry_group_get_client (AvahiEntryGroup *group) {
276     assert(group);
277     
278     return group->client;
279 }
280
281 int avahi_entry_group_is_empty (AvahiEntryGroup *group) {
282     DBusMessage *message, *reply;
283     DBusError error;
284     int r = AVAHI_OK;
285     int b;
286     AvahiClient *client;
287     
288     assert(group);
289     client = group->client;
290
291     if (!group->path || group->client->state == AVAHI_CLIENT_DISCONNECTED)
292         return avahi_client_set_errno(group->client, AVAHI_ERR_BAD_STATE);
293
294     dbus_error_init(&error);
295     
296     if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, group->path, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "IsEmpty"))) {
297         r = avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
298         goto fail;
299     }
300         
301     if (!(reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error)) ||
302         dbus_error_is_set (&error)) {
303         r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
304         goto fail;
305     }
306     
307     if (!dbus_message_get_args(reply, &error, DBUS_TYPE_BOOLEAN, &b, DBUS_TYPE_INVALID) ||
308         dbus_error_is_set (&error)) {
309         r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
310         goto fail;
311     }
312
313     dbus_message_unref(message);
314     dbus_message_unref(reply);
315
316     return !!b;
317     
318 fail:
319     if (dbus_error_is_set(&error)) {
320         r = avahi_client_set_dbus_error(client, &error);
321         dbus_error_free(&error);
322     }
323
324     if (message)
325         dbus_message_unref(message);
326
327     if (reply)
328         dbus_message_unref(reply);
329
330     return r;
331 }
332
333 static int append_string_list(DBusMessage *message, AvahiStringList *txt) {
334     DBusMessageIter iter, sub;
335     int r = -1;
336     AvahiStringList *p;
337
338     assert(message);
339     assert(txt);
340
341     dbus_message_iter_init_append(message, &iter);
342
343     /* Reverse the string list, so that we can pass it in-order to the server */
344     txt = avahi_string_list_reverse(txt);
345     
346     if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "ay", &sub))
347         goto fail;
348
349     /* Assemble the AvahiStringList into an Array of Array of Bytes to send over dbus */
350     for (p = txt; p != NULL; p = p->next) {
351         DBusMessageIter sub2;
352         const uint8_t *data = p->text;
353         
354         if (!(dbus_message_iter_open_container(&sub, DBUS_TYPE_ARRAY, "y", &sub2)) ||
355             !(dbus_message_iter_append_fixed_array(&sub2, DBUS_TYPE_BYTE, &data, p->size)) ||
356             !(dbus_message_iter_close_container(&sub, &sub2)))
357             goto fail;
358     }
359
360     if (!dbus_message_iter_close_container(&iter, &sub))
361         goto fail;
362
363     r = 0;
364
365 fail:
366
367     /* Reverse the string list to the original state */
368     txt = avahi_string_list_reverse(txt);
369
370     return r;
371 }
372
373 int avahi_entry_group_add_service_strlst(
374     AvahiEntryGroup *group,
375     AvahiIfIndex interface,
376     AvahiProtocol protocol,
377     AvahiPublishFlags flags,
378     const char *name,
379     const char *type,
380     const char *domain,
381     const char *host,
382     uint16_t port,
383     AvahiStringList *txt) {
384     
385     DBusMessage *message = NULL, *reply = NULL;
386     int r = AVAHI_OK;
387     DBusError error;
388     AvahiClient *client;
389     int32_t i_interface, i_protocol;
390     uint32_t u_flags;
391
392     assert(group);
393     assert(name);
394     assert(type);
395
396     client = group->client;
397
398     if (!group->path || group->client->state == AVAHI_CLIENT_DISCONNECTED)
399         return avahi_client_set_errno(group->client, AVAHI_ERR_BAD_STATE);
400
401     if (!domain)
402         domain = "";
403
404     if (!host)
405         host = "";
406     
407     dbus_error_init(&error);
408     
409     if (!(message = dbus_message_new_method_call (AVAHI_DBUS_NAME, group->path, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "AddService"))) {
410         r = avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
411         goto fail;
412     }
413
414     i_interface = (int32_t) interface;
415     i_protocol = (int32_t) protocol;
416     u_flags = (uint32_t) flags;
417
418     if (!dbus_message_append_args(
419             message,
420             DBUS_TYPE_INT32, &i_interface,
421             DBUS_TYPE_INT32, &i_protocol,
422             DBUS_TYPE_UINT32, &u_flags,
423             DBUS_TYPE_STRING, &name,
424             DBUS_TYPE_STRING, &type,
425             DBUS_TYPE_STRING, &domain,
426             DBUS_TYPE_STRING, &host,
427             DBUS_TYPE_UINT16, &port,
428             DBUS_TYPE_INVALID) ||
429         append_string_list(message, txt) < 0) {
430         r = avahi_client_set_errno(group->client, AVAHI_ERR_NO_MEMORY);
431         goto fail;
432     }
433     
434     if (!(reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error)) ||
435         dbus_error_is_set (&error)) {
436         r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
437         goto fail;
438     }
439     
440     if (!dbus_message_get_args(reply, &error, DBUS_TYPE_INVALID) ||
441         dbus_error_is_set (&error)) {
442         r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
443         goto fail;
444     }
445
446     dbus_message_unref(message);
447     dbus_message_unref(reply);
448
449     return AVAHI_OK;
450
451 fail:
452     
453     if (dbus_error_is_set(&error)) {
454         r = avahi_client_set_dbus_error(client, &error);
455         dbus_error_free(&error);
456     }
457
458     if (message)
459         dbus_message_unref(message);
460
461     if (reply)
462         dbus_message_unref(reply);
463
464     return r;
465 }
466
467 int avahi_entry_group_add_service(
468     AvahiEntryGroup *group,
469     AvahiIfIndex interface,
470     AvahiProtocol protocol,
471     AvahiPublishFlags flags,
472     const char *name,
473     const char *type,
474     const char *domain,
475     const char *host,
476     uint16_t port,
477     ...) {
478     
479     va_list va;
480     int r;
481
482     assert(group);
483
484     va_start(va, port);
485     r = avahi_entry_group_add_service_va(group, interface, protocol, flags, name, type, domain, host, port, va);
486     va_end(va);
487     return r;
488 }
489
490 int avahi_entry_group_add_service_va(
491     AvahiEntryGroup *group,
492     AvahiIfIndex interface,
493     AvahiProtocol protocol,
494     AvahiPublishFlags flags,
495     const char *name,
496     const char *type,
497     const char *domain,
498     const char *host,
499     uint16_t port,
500     va_list va) {
501
502     int r;
503     AvahiStringList *txt;
504
505     assert(group);
506
507     txt = avahi_string_list_new_va(va);
508     r = avahi_entry_group_add_service_strlst(group, interface, protocol, flags, name, type, domain, host, port, txt);
509     avahi_string_list_free(txt);
510
511     return r;
512 }
513
514 int avahi_entry_group_add_service_subtype(
515     AvahiEntryGroup *group,
516     AvahiIfIndex interface,
517     AvahiProtocol protocol,
518     AvahiPublishFlags flags,
519     const char *name,
520     const char *type,
521     const char *domain,
522     const char *subtype) {
523
524     DBusMessage *message = NULL, *reply = NULL;
525     int r = AVAHI_OK;
526     DBusError error;
527     AvahiClient *client;
528     int32_t i_interface, i_protocol;
529     uint32_t u_flags;
530
531     assert(group);
532     assert(name);
533     assert(type);
534     assert(subtype);
535
536     client = group->client;
537
538     if (!group->path || group->client->state == AVAHI_CLIENT_DISCONNECTED)
539         return avahi_client_set_errno(group->client, AVAHI_ERR_BAD_STATE);
540
541     if (!domain)
542         domain = "";
543
544     dbus_error_init(&error);
545     
546     if (!(message = dbus_message_new_method_call (AVAHI_DBUS_NAME, group->path, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "AddServiceSubtype"))) {
547         r = avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
548         goto fail;
549     }
550
551     i_interface = (int32_t) interface;
552     i_protocol = (int32_t) protocol;
553     u_flags = (uint32_t) flags;
554
555     if (!dbus_message_append_args(
556             message,
557             DBUS_TYPE_INT32, &i_interface,
558             DBUS_TYPE_INT32, &i_protocol,
559             DBUS_TYPE_UINT32, &u_flags,
560             DBUS_TYPE_STRING, &name,
561             DBUS_TYPE_STRING, &type,
562             DBUS_TYPE_STRING, &domain,
563             DBUS_TYPE_STRING, &subtype,
564             DBUS_TYPE_INVALID)) {
565         r = avahi_client_set_errno(group->client, AVAHI_ERR_NO_MEMORY);
566         goto fail;
567     }
568     
569     if (!(reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error)) ||
570         dbus_error_is_set (&error)) {
571         r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
572         goto fail;
573     }
574     
575     if (!dbus_message_get_args(reply, &error, DBUS_TYPE_INVALID) ||
576         dbus_error_is_set (&error)) {
577         r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
578         goto fail;
579     }
580
581     dbus_message_unref(message);
582     dbus_message_unref(reply);
583
584     return AVAHI_OK;
585
586 fail:
587     
588     if (dbus_error_is_set(&error)) {
589         r = avahi_client_set_dbus_error(client, &error);
590         dbus_error_free(&error);
591     }
592
593     if (message)
594         dbus_message_unref(message);
595
596     if (reply)
597         dbus_message_unref(reply);
598
599     return r;
600
601 }
602
603 int avahi_entry_group_update_service_txt(
604     AvahiEntryGroup *group,
605     AvahiIfIndex interface,
606     AvahiProtocol protocol,
607     AvahiPublishFlags flags,
608     const char *name,     
609     const char *type,     
610     const char *domain,   
611     ...) {
612
613     va_list va;
614     int r;
615
616     va_start(va, domain);
617     r = avahi_entry_group_update_service_txt_va(group, interface, protocol, flags, name, type, domain, va);
618     va_end(va);
619     return r;
620 }
621
622 int avahi_entry_group_update_service_txt_va(
623     AvahiEntryGroup *group,
624     AvahiIfIndex interface,
625     AvahiProtocol protocol,
626     AvahiPublishFlags flags,
627     const char *name,     
628     const char *type,     
629     const char *domain,   
630     va_list va) {
631
632     int r;
633     AvahiStringList *txt;
634
635     txt = avahi_string_list_new_va(va);
636     r = avahi_entry_group_update_service_txt_strlst(group, interface, protocol, flags, name, type, domain, txt);
637     avahi_string_list_free(txt);
638
639     return r;
640 }
641
642 int avahi_entry_group_update_service_txt_strlst(
643     AvahiEntryGroup *group,
644     AvahiIfIndex interface,
645     AvahiProtocol protocol,
646     AvahiPublishFlags flags,
647     const char *name,     
648     const char *type,     
649     const char *domain,   
650     AvahiStringList *txt) {
651
652     DBusMessage *message = NULL, *reply = NULL;
653     int r = AVAHI_OK;
654     DBusError error;
655     AvahiClient *client;
656     int32_t i_interface, i_protocol;
657     uint32_t u_flags;
658
659     assert(group);
660     assert(name);
661     assert(type);
662
663     client = group->client;
664
665     if (!group->path || group->client->state == AVAHI_CLIENT_DISCONNECTED)
666         return avahi_client_set_errno(group->client, AVAHI_ERR_BAD_STATE);
667
668     if (!domain)
669         domain = "";
670     
671     dbus_error_init(&error);
672     
673     if (!(message = dbus_message_new_method_call (AVAHI_DBUS_NAME, group->path, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "UpdateServiceTxt"))) {
674         r = avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
675         goto fail;
676     }
677
678     i_interface = (int32_t) interface;
679     i_protocol = (int32_t) protocol;
680     u_flags = (uint32_t) flags;
681
682     if (!dbus_message_append_args(
683             message,
684             DBUS_TYPE_INT32, &i_interface,
685             DBUS_TYPE_INT32, &i_protocol,
686             DBUS_TYPE_UINT32, &u_flags,
687             DBUS_TYPE_STRING, &name,
688             DBUS_TYPE_STRING, &type,
689             DBUS_TYPE_STRING, &domain,
690             DBUS_TYPE_INVALID) ||
691         append_string_list(message, txt) < 0) {
692         r = avahi_client_set_errno(group->client, AVAHI_ERR_NO_MEMORY);
693         goto fail;
694     }
695
696     if (!(reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error)) ||
697         dbus_error_is_set (&error)) {
698         r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
699         goto fail;
700     }
701     
702     if (!dbus_message_get_args(reply, &error, DBUS_TYPE_INVALID) ||
703         dbus_error_is_set (&error)) {
704         r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
705         goto fail;
706     }
707
708     dbus_message_unref(message);
709     dbus_message_unref(reply);
710
711     return AVAHI_OK;
712
713 fail:
714     
715     if (dbus_error_is_set(&error)) {
716         r = avahi_client_set_dbus_error(client, &error);
717         dbus_error_free(&error);
718     }
719
720     if (message)
721         dbus_message_unref(message);
722
723     if (reply)
724         dbus_message_unref(reply);
725
726     return r;
727     
728     
729 }
730