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