]> git.meshlink.io Git - catta/blob - avahi-core/browse-service-type.c
Rename some server side objects/symbols so that they do not conflict with the same...
[catta] / avahi-core / browse-service-type.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
31 #include "browse.h"
32 #include "log.h"
33
34 struct AvahiSServiceTypeBrowser {
35     AvahiServer *server;
36     char *domain_name;
37     
38     AvahiSRecordBrowser *record_browser;
39
40     AvahiSServiceTypeBrowserCallback callback;
41     void* userdata;
42
43     AVAHI_LLIST_FIELDS(AvahiSServiceTypeBrowser, browser);
44 };
45
46 static void record_browser_callback(AvahiSRecordBrowser*rr, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, AvahiRecord *record, void* userdata) {
47     AvahiSServiceTypeBrowser *b = userdata;
48     char *n, *e, *c;
49
50     assert(rr);
51     assert(record);
52     assert(b);
53
54     assert(record->key->type == AVAHI_DNS_TYPE_PTR);
55
56     n = avahi_normalize_name(record->data.ptr.name);
57
58     if (*n != '_')
59         goto fail;
60     
61     for (c = e = n; *c == '_';) {
62         c += strcspn(c, ".");
63
64         if (*c == 0)
65             goto fail;
66
67         assert(*c == '.');
68         e = c;
69         c++;
70     }
71
72     *e = 0;
73
74     if (!avahi_domain_equal(c, b->domain_name))
75         goto fail;
76     
77     b->callback(b, interface, protocol, event, n, c, b->userdata);
78     avahi_free(n);
79
80     return;
81
82 fail:
83     avahi_log_warn("Invalid service type '%s'", n);
84     avahi_free(n);
85 }
86
87 AvahiSServiceTypeBrowser *avahi_s_service_type_browser_new(AvahiServer *server, AvahiIfIndex interface, AvahiProtocol protocol, const char *domain, AvahiSServiceTypeBrowserCallback callback, void* userdata) {
88     AvahiSServiceTypeBrowser *b;
89     AvahiKey *k;
90     char *n = NULL;
91     
92     assert(server);
93     assert(callback);
94
95     if (domain && !avahi_is_valid_domain_name(domain)) {
96         avahi_server_set_errno(server, AVAHI_ERR_INVALID_DOMAIN_NAME);
97         return NULL;
98     }
99
100     if (!(b = avahi_new(AvahiSServiceTypeBrowser, 1))) {
101         avahi_server_set_errno(server, AVAHI_ERR_NO_MEMORY);
102         return NULL;
103     }
104     
105     b->server = server;
106     b->domain_name = avahi_normalize_name(domain ? domain : "local");
107     b->callback = callback;
108     b->userdata = userdata;
109
110     AVAHI_LLIST_PREPEND(AvahiSServiceTypeBrowser, browser, server->service_type_browsers, b);
111
112     n = avahi_strdup_printf("_services._dns-sd._udp.%s", b->domain_name);
113     k = avahi_key_new(n, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_PTR);
114     avahi_free(n);
115     
116     b->record_browser = avahi_s_record_browser_new(server, interface, protocol, k, record_browser_callback, b);
117     avahi_key_unref(k);
118
119     if (!b->record_browser)
120         return NULL;
121     
122     return b;
123 }
124
125 void avahi_s_service_type_browser_free(AvahiSServiceTypeBrowser *b) {
126     assert(b);
127
128     AVAHI_LLIST_REMOVE(AvahiSServiceTypeBrowser, browser, b->server->service_type_browsers, b);
129
130     if (b->record_browser)
131         avahi_s_record_browser_free(b->record_browser);
132     
133     avahi_free(b->domain_name);
134     avahi_free(b);
135 }
136
137