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