4 This file is part of avahi.
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.
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.
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
28 #include <avahi-common/domain.h>
29 #include <avahi-common/malloc.h>
35 typedef struct AvahiDNSServerInfo AvahiDNSServerInfo;
37 struct AvahiDNSServerInfo {
38 AvahiSDNSServerBrowser *browser;
40 AvahiIfIndex interface;
41 AvahiProtocol protocol;
42 AvahiRecord *srv_record;
43 AvahiSHostNameResolver *host_name_resolver;
46 AVAHI_LLIST_FIELDS(AvahiDNSServerInfo, info);
49 struct AvahiSDNSServerBrowser {
53 AvahiSRecordBrowser *record_browser;
54 AvahiSDNSServerBrowserCallback callback;
56 AvahiProtocol aprotocol;
60 AVAHI_LLIST_FIELDS(AvahiSDNSServerBrowser, browser);
61 AVAHI_LLIST_HEAD(AvahiDNSServerInfo, info);
64 static AvahiDNSServerInfo* get_server_info(AvahiSDNSServerBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiRecord *r) {
65 AvahiDNSServerInfo *i;
70 for (i = b->info; i; i = i->info_next)
71 if (i->interface == interface &&
72 i->protocol == protocol &&
73 avahi_record_equal_no_ttl(r, i->srv_record))
79 static void server_info_free(AvahiSDNSServerBrowser *b, AvahiDNSServerInfo *i) {
83 avahi_record_unref(i->srv_record);
84 if (i->host_name_resolver)
85 avahi_s_host_name_resolver_free(i->host_name_resolver);
87 AVAHI_LLIST_REMOVE(AvahiDNSServerInfo, info, b->info, i);
89 assert(b->n_info >= 1);
95 static void host_name_resolver_callback(AvahiSHostNameResolver *r, AvahiIfIndex interface, AvahiProtocol protocol, AvahiResolverEvent event, const char *host_name, const AvahiAddress *a, void* userdata) {
96 AvahiDNSServerInfo *i = userdata;
102 if (event == AVAHI_RESOLVER_FOUND) {
105 i->browser->callback(i->browser, i->interface, i->protocol, AVAHI_BROWSER_NEW, i->srv_record->data.srv.name, &i->address, i->srv_record->data.srv.port, i->browser->userdata);
108 avahi_s_host_name_resolver_free(i->host_name_resolver);
109 i->host_name_resolver = NULL;
112 static void record_browser_callback(AvahiSRecordBrowser*rr, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, AvahiRecord *record, void* userdata) {
113 AvahiSDNSServerBrowser *b = userdata;
119 assert(record->key->type == AVAHI_DNS_TYPE_SRV);
121 if (event == AVAHI_BROWSER_NEW) {
122 AvahiDNSServerInfo *i;
124 if (get_server_info(b, interface, protocol, record))
130 if (!(i = avahi_new(AvahiDNSServerInfo, 1)))
134 i->interface = interface;
135 i->protocol = protocol;
136 i->srv_record = avahi_record_ref(record);
137 i->host_name_resolver = avahi_s_host_name_resolver_new(b->server, interface, protocol, record->data.srv.name, b->aprotocol, host_name_resolver_callback, i);
139 AVAHI_LLIST_PREPEND(AvahiDNSServerInfo, info, b->info, i);
142 } else if (event == AVAHI_BROWSER_REMOVE) {
143 AvahiDNSServerInfo *i;
145 if (!(i = get_server_info(b, interface, protocol, record)))
148 if (!i->host_name_resolver)
149 b->callback(b, interface, protocol, event, i->srv_record->data.srv.name, &i->address, i->srv_record->data.srv.port, b->userdata);
151 server_info_free(b, i);
155 AvahiSDNSServerBrowser *avahi_s_dns_server_browser_new(AvahiServer *server, AvahiIfIndex interface, AvahiProtocol protocol, const char *domain, AvahiDNSServerType type, AvahiProtocol aprotocol, AvahiSDNSServerBrowserCallback callback, void* userdata) {
156 AvahiSDNSServerBrowser *b;
162 assert(type == AVAHI_DNS_SERVER_RESOLVE || type == AVAHI_DNS_SERVER_UPDATE);
164 if (domain && !avahi_is_valid_domain_name(domain)) {
165 avahi_server_set_errno(server, AVAHI_ERR_INVALID_DOMAIN_NAME);
169 if (!(b = avahi_new(AvahiSDNSServerBrowser, 1))) {
170 avahi_server_set_errno(server, AVAHI_ERR_NO_MEMORY);
175 b->domain_name = avahi_normalize_name(domain ? domain : "local");
176 b->callback = callback;
177 b->userdata = userdata;
178 b->aprotocol = aprotocol;
181 AVAHI_LLIST_HEAD_INIT(AvahiDNSServerInfo, b->info);
182 AVAHI_LLIST_PREPEND(AvahiSDNSServerBrowser, browser, server->dns_server_browsers, b);
184 n = avahi_strdup_printf("%s.%s",type == AVAHI_DNS_SERVER_RESOLVE ? "_domain._udp" : "_dns-update._udp", b->domain_name);
185 k = avahi_key_new(n, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_SRV);
188 b->record_browser = avahi_s_record_browser_new(server, interface, protocol, k, record_browser_callback, b);
191 if (!b->record_browser) {
192 avahi_s_dns_server_browser_free(b);
199 void avahi_s_dns_server_browser_free(AvahiSDNSServerBrowser *b) {
203 server_info_free(b, b->info);
205 AVAHI_LLIST_REMOVE(AvahiSDNSServerBrowser, browser, b->server->dns_server_browsers, b);
207 if (b->record_browser)
208 avahi_s_record_browser_free(b->record_browser);
209 avahi_free(b->domain_name);