]> git.meshlink.io Git - catta/blob - avahi-core/browse-service-type.c
* split off lookup.h and publish.h from core.h
[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 #include <avahi-common/error.h>
31
32 #include "browse.h"
33 #include "log.h"
34
35 struct AvahiSServiceTypeBrowser {
36     AvahiServer *server;
37     char *domain_name;
38     
39     AvahiSRecordBrowser *record_browser;
40
41     AvahiSServiceTypeBrowserCallback callback;
42     void* userdata;
43
44     AVAHI_LLIST_FIELDS(AvahiSServiceTypeBrowser, browser);
45 };
46
47 static void record_browser_callback(
48     AvahiSRecordBrowser*rr,
49     AvahiIfIndex interface,
50     AvahiProtocol protocol,
51     AvahiBrowserEvent event,
52     AvahiRecord *record,
53     AvahiLookupResultFlags flags,
54     void* userdata) {
55     
56     AvahiSServiceTypeBrowser *b = userdata;
57     char *n = NULL, *c = NULL;
58
59     assert(rr);
60     assert(b);
61
62     if (record) {
63         char *e;
64         
65         assert(record->key->type == AVAHI_DNS_TYPE_PTR);
66         
67         n = avahi_normalize_name(record->data.ptr.name);
68         
69         if (*n != '_')
70             goto fail;
71         
72         for (c = e = n; *c == '_';) {
73             c += strcspn(c, ".");
74             
75             if (*c == 0)
76                 goto fail;
77             
78             assert(*c == '.');
79             e = c;
80             c++;
81         }
82         
83         *e = 0;
84         
85         if (!avahi_domain_equal(c, b->domain_name))
86             goto fail;
87     }
88     
89     b->callback(b, interface, protocol, event, n, c, flags, b->userdata);
90     avahi_free(n);
91     
92     return;
93
94 fail:
95     avahi_log_warn("Invalid service type '%s'", n);
96     avahi_free(n);
97 }
98
99 AvahiSServiceTypeBrowser *avahi_s_service_type_browser_new(
100     AvahiServer *server,
101     AvahiIfIndex interface,
102     AvahiProtocol protocol,
103     const char *domain,
104     AvahiLookupFlags flags,
105     AvahiSServiceTypeBrowserCallback callback,
106     void* userdata) {
107     
108     AvahiSServiceTypeBrowser *b;
109     AvahiKey *k;
110     char *n = NULL;
111     
112     assert(server);
113     assert(callback);
114
115     if (domain && !avahi_is_valid_domain_name(domain)) {
116         avahi_server_set_errno(server, AVAHI_ERR_INVALID_DOMAIN_NAME);
117         return NULL;
118     }
119
120     if (!domain)
121         domain = server->domain_name;
122
123     if (!AVAHI_VALID_FLAGS(flags, AVAHI_LOOKUP_USE_WIDE_AREA|AVAHI_LOOKUP_USE_MULTICAST)) {
124         avahi_server_set_errno(server, AVAHI_ERR_INVALID_FLAGS);
125         return NULL;
126     }
127
128     if (!(b = avahi_new(AvahiSServiceTypeBrowser, 1))) {
129         avahi_server_set_errno(server, AVAHI_ERR_NO_MEMORY);
130         return NULL;
131     }
132     
133     b->server = server;
134     b->domain_name = avahi_normalize_name(domain);
135     b->callback = callback;
136     b->userdata = userdata;
137
138     AVAHI_LLIST_PREPEND(AvahiSServiceTypeBrowser, browser, server->service_type_browsers, b);
139
140     n = avahi_strdup_printf("_services._dns-sd._udp.%s", b->domain_name);
141     k = avahi_key_new(n, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_PTR);
142     avahi_free(n);
143     
144     b->record_browser = avahi_s_record_browser_new(server, interface, protocol, k, flags, record_browser_callback, b);
145     avahi_key_unref(k);
146
147     if (!b->record_browser)
148         return NULL;
149     
150     return b;
151 }
152
153 void avahi_s_service_type_browser_free(AvahiSServiceTypeBrowser *b) {
154     assert(b);
155
156     AVAHI_LLIST_REMOVE(AvahiSServiceTypeBrowser, browser, b->server->service_type_browsers, b);
157
158     if (b->record_browser)
159         avahi_s_record_browser_free(b->record_browser);
160     
161     avahi_free(b->domain_name);
162     avahi_free(b);
163 }
164
165