]> git.meshlink.io Git - catta/blob - avahi-core/browse-service-type.c
* rename avahi_service_name_snprint() to avahi_service_name_join()
[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
58     assert(rr);
59     assert(b);
60
61     if (record) {
62         char type[AVAHI_DOMAIN_NAME_MAX], domain[AVAHI_DOMAIN_NAME_MAX];
63         
64         assert(record->key->type == AVAHI_DNS_TYPE_PTR);
65
66         if (avahi_service_name_split(record->data.ptr.name, NULL, 0, type, sizeof(type), domain, sizeof(domain)) < 0) {
67             avahi_log_warn("Invalid service type '%s'", record->key->name);
68             return;
69         }
70
71         b->callback(b, interface, protocol, event, type, domain, flags, b->userdata);
72     } else
73         b->callback(b, interface, protocol, event, NULL, b->domain_name, flags, b->userdata);
74 }
75
76 AvahiSServiceTypeBrowser *avahi_s_service_type_browser_new(
77     AvahiServer *server,
78     AvahiIfIndex interface,
79     AvahiProtocol protocol,
80     const char *domain,
81     AvahiLookupFlags flags,
82     AvahiSServiceTypeBrowserCallback callback,
83     void* userdata) {
84     
85     AvahiSServiceTypeBrowser *b;
86     AvahiKey *k = NULL;
87     char n[AVAHI_DOMAIN_NAME_MAX];
88     int r;
89     
90     assert(server);
91     assert(callback);
92
93     AVAHI_CHECK_VALIDITY_RETURN_NULL(server, AVAHI_IF_VALID(interface), AVAHI_ERR_INVALID_INTERFACE);
94     AVAHI_CHECK_VALIDITY_RETURN_NULL(server, AVAHI_PROTO_VALID(protocol), AVAHI_ERR_INVALID_PROTOCOL);
95     AVAHI_CHECK_VALIDITY_RETURN_NULL(server, !domain || avahi_is_valid_domain_name(domain), AVAHI_ERR_INVALID_DOMAIN_NAME);
96     AVAHI_CHECK_VALIDITY_RETURN_NULL(server, AVAHI_FLAGS_VALID(flags, AVAHI_LOOKUP_USE_WIDE_AREA|AVAHI_LOOKUP_USE_MULTICAST), AVAHI_ERR_INVALID_FLAGS);
97
98     if (!domain)
99         domain = server->domain_name;
100
101     if ((r = avahi_service_name_join(n, sizeof(n), NULL, "_services._dns-sd._udp", domain)) < 0) {
102         avahi_server_set_errno(server, r);
103         return NULL;
104     }
105     
106     if (!(b = avahi_new(AvahiSServiceTypeBrowser, 1))) {
107         avahi_server_set_errno(server, AVAHI_ERR_NO_MEMORY);
108         return NULL;
109     }
110     
111     b->server = server;
112     b->callback = callback;
113     b->userdata = userdata;
114     b->record_browser = NULL;
115     
116     AVAHI_LLIST_PREPEND(AvahiSServiceTypeBrowser, browser, server->service_type_browsers, b);
117
118     if (!(b->domain_name = avahi_normalize_name_strdup(domain))) {
119         avahi_server_set_errno(server, AVAHI_ERR_NO_MEMORY);
120         goto fail;
121     }
122     
123     if (!(k = avahi_key_new(n, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_PTR))) {
124         avahi_server_set_errno(server, AVAHI_ERR_NO_MEMORY);
125         goto fail;
126     }
127     
128     if (!(b->record_browser = avahi_s_record_browser_new(server, interface, protocol, k, flags, record_browser_callback, b)))
129         goto fail;
130         
131     avahi_key_unref(k);
132
133     return b;
134
135 fail:
136     if (k)
137         avahi_key_unref(k);
138
139     avahi_s_service_type_browser_free(b);
140     
141     return NULL;
142 }
143
144 void avahi_s_service_type_browser_free(AvahiSServiceTypeBrowser *b) {
145     assert(b);
146
147     AVAHI_LLIST_REMOVE(AvahiSServiceTypeBrowser, browser, b->server->service_type_browsers, b);
148
149     if (b->record_browser)
150         avahi_s_record_browser_free(b->record_browser);
151
152     avahi_free(b->domain_name);
153     avahi_free(b);
154 }
155
156