]> git.meshlink.io Git - catta/blob - avahi-core/browse-service.c
* Add avahi-client examples to doxygen
[catta] / avahi-core / browse-service.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 <string.h>
27
28 #include <avahi-common/domain.h>
29 #include <avahi-common/malloc.h>
30 #include <avahi-common/error.h>
31
32 #include "browse.h"
33 #include "log.h"
34
35 struct AvahiSServiceBrowser {
36     AvahiServer *server;
37     char *domain_name;
38     char *service_type;
39     
40     AvahiSRecordBrowser *record_browser;
41
42     AvahiSServiceBrowserCallback callback;
43     void* userdata;
44
45     AVAHI_LLIST_FIELDS(AvahiSServiceBrowser, browser);
46 };
47
48 static void record_browser_callback(AvahiSRecordBrowser*rr, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, AvahiRecord *record, void* userdata) {
49     AvahiSServiceBrowser *b = userdata;
50     char *n, *e, *c, *s;
51     char service[128];
52
53     assert(rr);
54     assert(record);
55     assert(b);
56
57     assert(record->key->type == AVAHI_DNS_TYPE_PTR);
58
59     c = n = avahi_normalize_name(record->data.ptr.name);
60
61     if (!(avahi_unescape_label((const char**) &c, service, sizeof(service))))
62         goto fail;
63
64     for (s = e = c; *c == '_';) {
65         c += strcspn(c, ".");
66
67         if (*c == 0)
68             goto fail;
69
70         assert(*c == '.');
71         e = c;
72         c++;
73     }
74
75     *e = 0;
76
77     if (!avahi_domain_equal(c, b->domain_name))
78         goto fail;
79     
80     b->callback(b, interface, protocol, event, service, s, c, b->userdata);
81     avahi_free(n);
82
83     return;
84
85 fail:
86     avahi_log_warn("Invalid service '%s'", n);
87     avahi_free(n);
88 }
89
90 AvahiSServiceBrowser *avahi_s_service_browser_new(AvahiServer *server, AvahiIfIndex interface, AvahiProtocol protocol, const char *service_type, const char *domain, AvahiSServiceBrowserCallback callback, void* userdata) {
91     AvahiSServiceBrowser *b;
92     AvahiKey *k;
93     char *n = NULL;
94     
95     assert(server);
96     assert(callback);
97     assert(service_type);
98
99     if (!avahi_is_valid_service_type(service_type)) {
100         avahi_server_set_errno(server, AVAHI_ERR_INVALID_SERVICE_TYPE);
101         return NULL;
102     }
103
104     if (domain && !avahi_is_valid_domain_name(domain)) {
105         avahi_server_set_errno(server, AVAHI_ERR_INVALID_DOMAIN_NAME);
106         return NULL;
107     }
108
109     if (!(b = avahi_new(AvahiSServiceBrowser, 1))) {
110         avahi_server_set_errno(server, AVAHI_ERR_NO_MEMORY);
111         return NULL;
112     }
113     
114     b->server = server;
115     b->domain_name = avahi_normalize_name(domain ? domain : "local");
116     b->service_type = avahi_normalize_name(service_type);
117     b->callback = callback;
118     b->userdata = userdata;
119     AVAHI_LLIST_PREPEND(AvahiSServiceBrowser, browser, server->service_browsers, b);
120
121     n = avahi_strdup_printf("%s.%s", b->service_type, b->domain_name);
122     k = avahi_key_new(n, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_PTR);
123     avahi_free(n);
124     
125     b->record_browser = avahi_s_record_browser_new(server, interface, protocol, k, record_browser_callback, b);
126
127     avahi_key_unref(k);
128
129     if (!b->record_browser) {
130         avahi_s_service_browser_free(b);
131         return NULL;
132     }
133     
134     return b;
135 }
136
137 void avahi_s_service_browser_free(AvahiSServiceBrowser *b) {
138     assert(b);
139
140     AVAHI_LLIST_REMOVE(AvahiSServiceBrowser, browser, b->server->service_browsers, b);
141
142     if (b->record_browser)
143         avahi_s_record_browser_free(b->record_browser);
144     
145     avahi_free(b->domain_name);
146     avahi_free(b->service_type);
147     avahi_free(b);
148 }