]> git.meshlink.io Git - catta/blob - avahi-client/browser.c
6f6e447378721497d35049274f18578ac84f2ff3
[catta] / avahi-client / browser.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 /* AvahiDomainBrowser */
44
45 AvahiDomainBrowser* avahi_domain_browser_new (AvahiClient *client, AvahiIfIndex interface, AvahiProtocol protocol, char *domain, AvahiDomainBrowserType btype, AvahiDomainBrowserCallback callback, void *user_data)
46 {
47     AvahiDomainBrowser *tmp = NULL;
48     DBusMessage *message = NULL, *reply;
49     DBusError error;
50     char *path;
51
52     if (client == NULL)
53         return NULL;
54
55     dbus_error_init (&error);
56
57     message = dbus_message_new_method_call (AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER,
58             AVAHI_DBUS_INTERFACE_SERVER, "DomainBrowserNew");
59
60     if (!dbus_message_append_args (message, DBUS_TYPE_INT32, &interface, DBUS_TYPE_INT32, &protocol, DBUS_TYPE_STRING, &domain, DBUS_TYPE_INT32, &btype, DBUS_TYPE_INVALID))
61         goto dbus_error;
62
63     reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error);
64
65     if (dbus_error_is_set (&error) || reply == NULL)
66         goto dbus_error;
67
68     if (!dbus_message_get_args (reply, &error, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID))
69         goto dbus_error;
70
71     if (dbus_error_is_set (&error) || path == NULL)
72         goto dbus_error;
73
74     tmp = malloc (sizeof (AvahiDomainBrowser));
75     tmp->client = client;
76     tmp->callback = callback;
77     tmp->user_data = user_data;
78     tmp->path = strdup (path);
79
80     AVAHI_LLIST_PREPEND(AvahiDomainBrowser, domain_browsers, client->domain_browsers, tmp);
81
82     return tmp;
83
84 dbus_error:
85     dbus_error_free (&error);
86     avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
87
88     return NULL;
89 }
90
91 int
92 avahi_domain_browser_free (AvahiDomainBrowser *b)
93 {
94     AvahiClient *client = b->client;
95     DBusMessage *message = NULL;
96
97     if (b == NULL || b->path == NULL)
98         return avahi_client_set_errno (client, AVAHI_ERR_INVALID_OBJECT);
99
100     message = dbus_message_new_method_call (AVAHI_DBUS_NAME,
101             b->path,
102             AVAHI_DBUS_INTERFACE_DOMAIN_BROWSER, "Free");
103
104     if (message == NULL)
105         return avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
106
107     dbus_connection_send (client->bus, message, NULL);
108
109     AVAHI_LLIST_REMOVE(AvahiDomainBrowser, domain_browsers, client->domain_browsers, b);
110
111     free (b);
112
113     return avahi_client_set_errno (client, AVAHI_OK);
114 }
115
116 char*
117 avahi_domain_browser_path (AvahiDomainBrowser *b)
118 {
119     return b->path;
120 }
121
122 DBusHandlerResult
123 avahi_domain_browser_event (AvahiClient *client, AvahiBrowserEvent event, DBusMessage *message)
124 {
125     AvahiDomainBrowser *n, *db = NULL;
126     DBusError error;
127     const char *path;
128     char *domain;
129     int interface, protocol;
130
131     dbus_error_init (&error);
132
133     path = dbus_message_get_path (message);
134
135     if (path == NULL)
136         goto out;
137
138     for (n = client->domain_browsers; n != NULL; n = n->domain_browsers_next)
139     {
140         if (strcmp (n->path, path) == 0) {
141             db = n;
142             break;
143         }
144     }
145
146     if (db == NULL)
147         goto out;
148
149     dbus_message_get_args (message, &error, DBUS_TYPE_INT32, &interface,
150             DBUS_TYPE_INT32, &protocol, DBUS_TYPE_STRING, &domain, DBUS_TYPE_INVALID);
151
152     if (dbus_error_is_set (&error))
153         goto out;
154
155     db->callback (db, interface, protocol, event, domain, db->user_data);
156
157     return DBUS_HANDLER_RESULT_HANDLED;
158
159 out:
160     dbus_error_free (&error);
161     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
162 }
163
164 /* AvahiServiceTypeBrowser */
165 AvahiServiceTypeBrowser* avahi_service_type_browser_new (AvahiClient *client, AvahiIfIndex interface, AvahiProtocol protocol, char *domain, AvahiServiceTypeBrowserCallback callback, void *user_data)
166 {
167     AvahiServiceTypeBrowser *tmp = NULL;
168     DBusMessage *message = NULL, *reply;
169     DBusError error;
170     char *path;
171
172     if (client == NULL)
173         return NULL;
174
175     dbus_error_init (&error);
176
177     message = dbus_message_new_method_call (AVAHI_DBUS_NAME,
178             AVAHI_DBUS_PATH_SERVER,
179             AVAHI_DBUS_INTERFACE_SERVER,
180             "ServiceTypeBrowserNew");
181
182     if (!dbus_message_append_args (message, DBUS_TYPE_INT32, &interface, DBUS_TYPE_INT32, &protocol, DBUS_TYPE_STRING, &domain, DBUS_TYPE_INVALID))
183         goto dbus_error;
184
185     reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error);
186
187     if (dbus_error_is_set (&error) || reply == NULL)
188         goto dbus_error;
189
190     if (!dbus_message_get_args (reply, &error, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID))
191         goto dbus_error;
192
193     if (dbus_error_is_set (&error) || path == NULL)
194         goto dbus_error;
195
196     tmp = malloc (sizeof (AvahiServiceTypeBrowser));
197     tmp->client = client;
198     tmp->callback = callback;
199     tmp->user_data = user_data;
200     tmp->path = strdup (path);
201
202     AVAHI_LLIST_PREPEND(AvahiServiceTypeBrowser, service_type_browsers, client->service_type_browsers, tmp);
203
204     return tmp;
205
206 dbus_error:
207     dbus_error_free (&error);
208     avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
209
210     return NULL;
211 }
212
213 int
214 avahi_service_type_browser_free (AvahiServiceTypeBrowser *b)
215 {
216     AvahiClient *client = b->client;
217     DBusMessage *message = NULL;
218
219     if (b == NULL || b->path == NULL)
220         return avahi_client_set_errno (client, AVAHI_ERR_INVALID_OBJECT);
221
222     message = dbus_message_new_method_call (AVAHI_DBUS_NAME,
223             b->path,
224             AVAHI_DBUS_INTERFACE_SERVICE_TYPE_BROWSER, "Free");
225
226     if (message == NULL)
227         return avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
228
229     dbus_connection_send (b->client->bus, message, NULL);
230
231     AVAHI_LLIST_REMOVE(AvahiServiceTypeBrowser, service_type_browsers, b->client->service_type_browsers, b);
232
233     free (b);
234
235     return avahi_client_set_errno (client, AVAHI_OK);
236 }
237
238 char*
239 avahi_service_type_browser_path (AvahiServiceTypeBrowser *b)
240 {
241     return b->path;
242 }
243
244 DBusHandlerResult
245 avahi_service_type_browser_event (AvahiClient *client, AvahiBrowserEvent event, DBusMessage *message)
246 {
247     AvahiServiceTypeBrowser *n, *db = NULL;
248     DBusError error;
249     const char *path;
250     char *domain, *type;
251     int interface, protocol;
252
253     dbus_error_init (&error);
254
255     path = dbus_message_get_path (message);
256
257     if (path == NULL)
258         goto out;
259
260     for (n = client->service_type_browsers; n != NULL; n = n->service_type_browsers_next)
261     {
262         if (strcmp (n->path, path) == 0) {
263             db = n;
264             break;
265         }
266     }
267
268     if (db == NULL)
269         goto out;
270
271     dbus_message_get_args (message, &error,
272             DBUS_TYPE_INT32, &interface,
273             DBUS_TYPE_INT32, &protocol,
274             DBUS_TYPE_STRING, &type,
275             DBUS_TYPE_STRING, &domain,
276             DBUS_TYPE_INVALID);
277     
278     if (dbus_error_is_set (&error))
279         goto out;
280
281     db->callback (db, interface, protocol, event, type, domain, db->user_data);
282
283     return DBUS_HANDLER_RESULT_HANDLED;
284
285 out:
286     dbus_error_free (&error);
287     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
288 }
289
290 /* AvahiServiceBrowser */
291
292 AvahiServiceBrowser* avahi_service_browser_new (AvahiClient *client, AvahiIfIndex interface, AvahiProtocol protocol, char *type, char *domain, AvahiServiceBrowserCallback callback, void *user_data)
293 {
294     AvahiServiceBrowser *tmp = NULL;
295     DBusMessage *message = NULL, *reply;
296     DBusError error;
297     char *path;
298
299     if (client == NULL)
300         return NULL;
301
302     dbus_error_init (&error);
303
304     message = dbus_message_new_method_call (AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER,
305             AVAHI_DBUS_INTERFACE_SERVER, "ServiceBrowserNew");
306
307     if (!dbus_message_append_args (message,
308                 DBUS_TYPE_INT32, &interface,
309                 DBUS_TYPE_INT32, &protocol,
310                 DBUS_TYPE_STRING, &type,
311                 DBUS_TYPE_STRING, &domain,
312                 DBUS_TYPE_INVALID))
313         goto dbus_error;
314
315     reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error);
316
317     if (dbus_error_is_set (&error) || reply == NULL)
318         goto dbus_error;
319
320     if (!dbus_message_get_args (reply, &error, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID))
321         goto dbus_error;
322
323     if (dbus_error_is_set (&error) || path == NULL)
324         goto dbus_error;
325
326     tmp = malloc (sizeof (AvahiServiceBrowser));
327     tmp->client = client;
328     tmp->callback = callback;
329     tmp->user_data = user_data;
330     tmp->path = strdup (path);
331
332     AVAHI_LLIST_PREPEND(AvahiServiceBrowser, service_browsers, client->service_browsers, tmp);
333
334     return tmp;
335
336 dbus_error:
337     dbus_error_free (&error);
338     avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
339
340     return NULL;
341 }
342     
343 int
344 avahi_service_browser_free (AvahiServiceBrowser *b)
345 {
346     AvahiClient *client = b->client;
347     DBusMessage *message = NULL;
348     
349     if (b == NULL || b->path == NULL)
350         return avahi_client_set_errno (client, AVAHI_ERR_INVALID_OBJECT);
351
352     message = dbus_message_new_method_call (AVAHI_DBUS_NAME,
353             b->path,
354             AVAHI_DBUS_INTERFACE_SERVICE_BROWSER, "Free");
355
356     if (message == NULL)
357         return avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
358
359     dbus_connection_send (b->client->bus, message, NULL);
360
361     AVAHI_LLIST_REMOVE(AvahiServiceBrowser, service_browsers, b->client->service_browsers, b);
362
363     free (b);
364
365     return avahi_client_set_errno (client, AVAHI_OK);
366 }
367
368 char*
369 avahi_service_browser_path (AvahiServiceBrowser *b)
370 {
371     return b->path;
372 }
373
374 DBusHandlerResult
375 avahi_service_browser_event (AvahiClient *client, AvahiBrowserEvent event, DBusMessage *message)
376 {
377     AvahiServiceBrowser *n, *db = NULL;
378     DBusError error;
379     const char *path;
380     char *name, *type, *domain;
381     int interface, protocol;
382
383     dbus_error_init (&error);
384
385     path = dbus_message_get_path (message);
386
387     if (path == NULL)
388         goto out;
389
390     for (n = client->service_browsers; n != NULL; n = n->service_browsers_next)
391     {
392         if (strcmp (n->path, path) == 0) {
393             db = n;
394             break;
395         }
396     }
397
398     if (db == NULL)
399         goto out;
400
401     dbus_message_get_args (message, &error,
402             DBUS_TYPE_INT32, &interface,
403             DBUS_TYPE_INT32, &protocol,
404             DBUS_TYPE_STRING, &name,
405             DBUS_TYPE_STRING, &type,
406             DBUS_TYPE_STRING, &domain,
407             DBUS_TYPE_INVALID);
408
409     if (dbus_error_is_set (&error))
410         goto out;
411
412     db->callback (db, interface, protocol, event, name, type, domain, db->user_data);
413
414     return DBUS_HANDLER_RESULT_HANDLED;
415
416 out:
417     dbus_error_free (&error);
418     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
419 }
420
421