]> git.meshlink.io Git - catta/blob - avahi-client/client.c
* Add service browser support to C client API
[catta] / avahi-client / client.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 <avahi-client/client.h>
27 #include <avahi-common/dbus.h>
28 #include <avahi-common/llist.h>
29 #include <avahi-common/error.h>
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <string.h>
33
34 #define DBUS_API_SUBJECT_TO_CHANGE
35 #include <dbus/dbus.h>
36 #include <dbus/dbus-glib-lowlevel.h>
37
38 #include <stdlib.h>
39
40 #include "client.h"
41 #include "internal.h"
42
43 int
44 avahi_client_set_errno (AvahiClient *client, int error)
45 {
46     if (client == NULL) return error;
47
48     client->error = error;
49
50     return error;
51 }
52     
53 static
54 void avahi_client_state_change (AvahiClient *client, int state)
55 {
56     if (client == NULL || client->callback == NULL) 
57         return;
58
59     client->callback (client, state, client->user_data);
60 }
61
62 void
63 avahi_client_state_request_callback (DBusPendingCall *call, void *data)
64 {
65     AvahiClient *client = data;
66     DBusError error;
67     DBusMessage *reply;
68     int state, type;
69
70     dbus_error_init (&error);
71
72     reply = dbus_pending_call_steal_reply (call);
73
74     type = dbus_message_get_type (reply);
75
76     if (type == DBUS_MESSAGE_TYPE_METHOD_RETURN)
77     {
78         dbus_message_get_args (reply, &error, DBUS_TYPE_INT32, &state, DBUS_TYPE_INVALID);
79         
80         if (dbus_error_is_set (&error))
81         {
82             fprintf (stderr, "internal error parsing client state change for client\n");
83             return;
84         }
85         
86         printf ("statechange (client) to %d\n", state);
87         
88         avahi_client_state_change (client, state);
89     } else if (type == DBUS_MESSAGE_TYPE_ERROR) {
90         dbus_set_error_from_message (&error, reply);
91         fprintf (stderr, "Error from reply: %s\n", error.message);
92     }
93
94     dbus_pending_call_unref (call);
95 }
96
97 void
98 avahi_client_schedule_state_request (AvahiClient *client)
99 {
100     DBusMessage *message;
101     DBusPendingCall *pcall;
102
103     if (client == NULL) return;
104
105     message = dbus_message_new_method_call (AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "GetState");
106
107     dbus_connection_send_with_reply (client->bus, message, &pcall, -1);
108
109     dbus_pending_call_set_notify (pcall, avahi_client_state_request_callback, client, NULL);
110 }
111
112 static DBusHandlerResult
113 filter_func (DBusConnection *bus, DBusMessage *message, void *data)
114 {
115     AvahiClient *client = data;
116     DBusError error;
117     
118     printf ("dbus: interface=%s, path=%s, member=%s\n",
119             dbus_message_get_interface (message),
120             dbus_message_get_path (message),
121             dbus_message_get_member (message));
122
123     dbus_error_init (&error);
124
125     if (dbus_message_is_signal(message, DBUS_INTERFACE_DBUS, "NameOwnerChanged")) {
126         gchar *name, *old, *new;
127         dbus_message_get_args(message, &error, DBUS_TYPE_STRING, &name, DBUS_TYPE_STRING, &old, DBUS_TYPE_STRING, &new, DBUS_TYPE_INVALID);
128         
129         if (dbus_error_is_set (&error)) {
130             fprintf(stderr, "Failed to parse NameOwnerChanged message: %s", error.message);
131             dbus_error_free (&error);
132             goto out;
133         }
134
135         if (strcmp (name, AVAHI_DBUS_NAME) == 0) {
136
137             if (old == NULL && new != NULL) {
138                 fprintf(stderr, "Avahi Daemon connected\n");
139                 avahi_client_state_change (client, AVAHI_CLIENT_RECONNECTED);
140             } else if (old != NULL && new == NULL) {
141                 fprintf(stderr, "Avahi Daemon disconnected\n");
142                 avahi_client_state_change (client, AVAHI_CLIENT_DISCONNECTED);
143                 /* XXX: we really need to expire all entry groups */
144             }
145         }
146     } else if (dbus_message_is_signal (message, AVAHI_DBUS_NAME, "StateChanged")) {
147         printf ("server statehcange\n");
148     } else if (dbus_message_is_signal (message, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "StateChanged")) {
149         const char *path;
150         AvahiEntryGroup *n, *group = NULL;
151         path = dbus_message_get_path (message);
152
153         for (n = client->groups; n != NULL; n = n->groups_next)
154         {
155             if (strcmp (n->path, path) == 0)
156             {
157                 group = n;
158                 break;
159             }
160         }
161         
162         if (group == NULL)
163         {
164             fprintf (stderr, "Received state change for unknown EntryGroup object (%s)\n", path);
165         } else {
166             int state;
167             DBusError error;
168             dbus_error_init (&error);
169             dbus_message_get_args (message, &error, DBUS_TYPE_INT32, &state, DBUS_TYPE_INVALID);
170             if (dbus_error_is_set (&error))
171             {
172                 fprintf (stderr, "internal error parsing entrygroup statechange for %s\n", group->path);
173                 goto out;
174             }
175             printf ("statechange (%s) to %d\n", group->path, state);
176             avahi_entry_group_state_change (group, state);
177         }
178     } else if (dbus_message_is_signal (message, AVAHI_DBUS_INTERFACE_DOMAIN_BROWSER, "ItemNew")) {
179         return avahi_domain_browser_event (client, AVAHI_BROWSER_NEW, message);
180     } else if (dbus_message_is_signal (message, AVAHI_DBUS_INTERFACE_DOMAIN_BROWSER, "ItemRemove")) {
181         return avahi_domain_browser_event (client, AVAHI_BROWSER_REMOVE, message);
182     } else if (dbus_message_is_signal (message, AVAHI_DBUS_INTERFACE_SERVICE_TYPE_BROWSER, "ItemNew")) {
183         return avahi_service_type_browser_event (client, AVAHI_BROWSER_NEW, message);
184     } else if (dbus_message_is_signal (message, AVAHI_DBUS_INTERFACE_SERVICE_TYPE_BROWSER, "ItemRemove")) {
185         return avahi_service_type_browser_event (client, AVAHI_BROWSER_REMOVE, message);
186     } else if (dbus_message_is_signal (message, AVAHI_DBUS_INTERFACE_SERVICE_BROWSER, "ItemNew")) {
187         return avahi_service_browser_event (client, AVAHI_BROWSER_NEW, message);
188     } else if (dbus_message_is_signal (message, AVAHI_DBUS_INTERFACE_SERVICE_BROWSER, "ItemRemove")) {
189         return avahi_service_browser_event (client, AVAHI_BROWSER_REMOVE, message);
190     }
191
192     return DBUS_HANDLER_RESULT_HANDLED;
193
194 out: 
195     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
196 }
197
198 AvahiClient *
199 avahi_client_new (AvahiClientCallback callback, void *user_data)
200 {
201     AvahiClient *tmp = NULL;
202     DBusError error;
203
204     dbus_error_init (&error);
205
206     if (!(tmp = malloc(sizeof(AvahiClient))))
207         goto fail;
208
209     AVAHI_LLIST_HEAD_INIT(AvahiEntryGroup, tmp->groups);
210     AVAHI_LLIST_HEAD_INIT(AvahiDomainBrowser, tmp->domain_browsers);
211     AVAHI_LLIST_HEAD_INIT(AvahiServiceBrowser, tmp->service_browsers);
212     AVAHI_LLIST_HEAD_INIT(AvahiServiceTypeBrowser, tmp->service_type_browsers);
213     
214     tmp->bus = dbus_bus_get (DBUS_BUS_SYSTEM, &error);
215
216     if (dbus_error_is_set (&error)) {
217         fprintf(stderr, "Error getting system d-bus: %s\n", error.message);
218         goto fail;
219     }
220
221     dbus_connection_setup_with_g_main (tmp->bus, NULL);
222     dbus_connection_set_exit_on_disconnect (tmp->bus, FALSE);
223
224     if (!dbus_connection_add_filter (tmp->bus, filter_func, tmp, NULL))
225     {
226         fprintf (stderr, "Failed to add d-bus filter\n");
227         goto fail;
228     }
229
230     dbus_bus_add_match (tmp->bus,
231             "type='signal', "
232             "interface='" AVAHI_DBUS_INTERFACE_SERVER "', "
233             "sender='" AVAHI_DBUS_NAME "', "
234             "path='" AVAHI_DBUS_PATH_SERVER "'",
235             &error);
236
237     if (dbus_error_is_set (&error))
238     {
239         fprintf (stderr, "Error adding filter match: %s\n", error.message);
240         goto fail;
241
242     }   
243
244     dbus_bus_add_match (tmp->bus,
245             "type='signal', "
246             "interface='" DBUS_INTERFACE_DBUS "', "
247             "sender='" DBUS_SERVICE_DBUS "', "
248             "path='" DBUS_PATH_DBUS "'",
249             &error);
250
251     if (dbus_error_is_set (&error))
252     {
253         fprintf (stderr, "Error adding filter match: %s\n", error.message);
254         goto fail;
255     }
256
257     tmp->callback = callback;
258     tmp->user_data = user_data;
259
260     avahi_client_schedule_state_request (tmp);
261
262     avahi_client_set_errno (tmp, AVAHI_OK);
263     return tmp;
264
265 fail:
266     free (tmp);
267
268     if (dbus_error_is_set(&error))
269         dbus_error_free(&error);
270         
271     return NULL;
272 }
273
274 static char*
275 avahi_client_get_string_reply_and_block (AvahiClient *client, char *method, char *param)
276 {
277     DBusMessage *message;
278     DBusMessage *reply;
279     DBusError error;
280     char *ret, *new;
281
282     if (client == NULL || method == NULL) return NULL;
283
284     dbus_error_init (&error);
285
286     message = dbus_message_new_method_call (AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, method);
287
288     if (param != NULL)
289     {
290         if (!dbus_message_append_args (message, DBUS_TYPE_STRING, &param, DBUS_TYPE_INVALID))
291         {
292             avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
293             fprintf (stderr, "Failed to append string argument to %s message\n", method);
294             return NULL;
295         }
296     }
297     
298     reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error);
299
300     if (dbus_error_is_set (&error))
301     {
302         fprintf (stderr, "Error sending %s message: %s\n", method, error.message);
303         dbus_error_free (&error);
304         dbus_message_unref (message);
305
306         avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
307         return NULL;
308     }
309
310     if (reply == NULL)
311     {
312         dbus_message_unref (message);
313         fprintf (stderr, "Could not connect to Avahi daemon\n");
314
315         avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
316         return NULL;
317     }
318
319     dbus_message_get_args (reply, &error, DBUS_TYPE_STRING, &ret, DBUS_TYPE_INVALID);
320
321     if (dbus_error_is_set (&error))
322     {
323         fprintf (stderr, "Failed to parse %s reply: %s\n", method, error.message);
324         dbus_error_free (&error);
325
326         avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
327         return NULL;
328     }
329
330     new = strdup (ret);
331
332     avahi_client_set_errno (client, AVAHI_OK);
333     return new;
334 }
335
336 char*
337 avahi_client_get_version_string (AvahiClient *client)
338 {
339     return avahi_client_get_string_reply_and_block (client, "GetVersionString", NULL);
340 }
341
342 char*
343 avahi_client_get_domain_name (AvahiClient *client)
344 {
345     return avahi_client_get_string_reply_and_block (client, "GetDomainName", NULL);
346 }
347
348 char*
349 avahi_client_get_host_name (AvahiClient *client)
350 {
351     return avahi_client_get_string_reply_and_block (client, "GetHostName", NULL);
352 }
353
354 char*
355 avahi_client_get_host_name_fqdn (AvahiClient *client)
356 {
357     return avahi_client_get_string_reply_and_block (client, "GetHostNameFqdn", NULL);
358 }