]> git.meshlink.io Git - catta/blob - avahi-core/browse-domain.c
* rename avahi_service_name_snprint() to avahi_service_name_join()
[catta] / avahi-core / browse-domain.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 <avahi-common/domain.h>
27 #include <avahi-common/malloc.h>
28 #include <avahi-common/error.h>
29
30 #include "browse.h"
31
32 struct AvahiSDomainBrowser {
33     AvahiServer *server;
34     
35     AvahiSRecordBrowser *record_browser;
36     
37     AvahiSDomainBrowserCallback callback;
38     void* userdata;
39
40     AVAHI_LLIST_FIELDS(AvahiSDomainBrowser, browser);
41 };
42
43 static void record_browser_callback(
44     AvahiSRecordBrowser*rr,
45     AvahiIfIndex interface,
46     AvahiProtocol protocol,
47     AvahiBrowserEvent event,
48     AvahiRecord *record,
49     AvahiLookupResultFlags flags,
50     void* userdata) {
51     
52     AvahiSDomainBrowser *b = userdata;
53     char *n = NULL;
54
55     assert(rr);
56     assert(b);
57     
58     if (record) {
59         assert(record->key->type == AVAHI_DNS_TYPE_PTR);
60         n = record->data.ptr.name;
61     }
62         
63     b->callback(b, interface, protocol, event, n, flags, b->userdata);
64 }
65
66
67 AvahiSDomainBrowser *avahi_s_domain_browser_new(
68     AvahiServer *server,
69     AvahiIfIndex interface,
70     AvahiProtocol protocol,
71     const char *domain,
72     AvahiDomainBrowserType type,
73     AvahiLookupFlags flags,
74     AvahiSDomainBrowserCallback callback,
75     void* userdata) {
76
77     static const char * const type_table[AVAHI_DOMAIN_BROWSER_MAX] = {
78         "r",
79         "dr",
80         "b",
81         "db",
82         "lb"
83     };
84     
85     AvahiSDomainBrowser *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, type < AVAHI_DOMAIN_BROWSER_MAX, AVAHI_ERR_INVALID_FLAGS);
96     AVAHI_CHECK_VALIDITY_RETURN_NULL(server, !domain || avahi_is_valid_domain_name(domain), AVAHI_ERR_INVALID_DOMAIN_NAME);
97     AVAHI_CHECK_VALIDITY_RETURN_NULL(server, AVAHI_FLAGS_VALID(flags, AVAHI_LOOKUP_USE_WIDE_AREA|AVAHI_LOOKUP_USE_MULTICAST), AVAHI_ERR_INVALID_FLAGS);
98
99     if (!domain)
100         domain = server->domain_name;
101
102     if ((r = avahi_service_name_join(n, sizeof(n), type_table[type], "_dns-sd._udp", domain)) < 0) {
103         avahi_server_set_errno(server, r);
104         return NULL;
105     }
106     
107     if (!(b = avahi_new(AvahiSDomainBrowser, 1))) {
108         avahi_server_set_errno(server, AVAHI_ERR_NO_MEMORY);
109         return NULL;
110     }
111     
112     b->server = server;
113     b->callback = callback;
114     b->userdata = userdata;
115     b->record_browser = NULL;
116
117     AVAHI_LLIST_PREPEND(AvahiSDomainBrowser, browser, server->domain_browsers, b);
118
119     if (!(k = avahi_key_new(n, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_PTR))) {
120         avahi_server_set_errno(server, AVAHI_ERR_NO_MEMORY);
121         goto fail;
122     }
123     
124     if (!(b->record_browser = avahi_s_record_browser_new(server, interface, protocol, k, flags, record_browser_callback, b)))
125         goto fail;
126     
127     avahi_key_unref(k);
128     return b;
129     
130 fail:
131     
132     if (k)
133         avahi_key_unref(k);
134     
135     avahi_s_domain_browser_free(b);
136     
137     return NULL;
138 }
139
140 void avahi_s_domain_browser_free(AvahiSDomainBrowser *b) {
141     assert(b);
142
143     AVAHI_LLIST_REMOVE(AvahiSDomainBrowser, browser, b->server->domain_browsers, b);
144
145     if (b->record_browser)
146         avahi_s_record_browser_free(b->record_browser);
147     
148     avahi_free(b);
149 }