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
33 typedef struct AvahiDNSServerInfo AvahiDNSServerInfo;
35 struct AvahiDNSServerInfo {
36 AvahiDNSServerBrowser *browser;
38 AvahiIfIndex interface;
39 AvahiProtocol protocol;
40 AvahiRecord *srv_record;
41 AvahiHostNameResolver *host_name_resolver;
44 AVAHI_LLIST_FIELDS(AvahiDNSServerInfo, info);
47 struct AvahiDNSServerBrowser {
51 AvahiRecordBrowser *record_browser;
52 AvahiDNSServerBrowserCallback callback;
54 AvahiProtocol aprotocol;
58 AVAHI_LLIST_FIELDS(AvahiDNSServerBrowser, browser);
59 AVAHI_LLIST_HEAD(AvahiDNSServerInfo, info);
62 static AvahiDNSServerInfo* get_server_info(AvahiDNSServerBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiRecord *r) {
63 AvahiDNSServerInfo *i;
68 for (i = b->info; i; i = i->info_next)
69 if (i->interface == interface &&
70 i->protocol == protocol &&
71 avahi_record_equal_no_ttl(r, i->srv_record))
77 static void server_info_free(AvahiDNSServerBrowser *b, AvahiDNSServerInfo *i) {
81 avahi_record_unref(i->srv_record);
82 if (i->host_name_resolver)
83 avahi_host_name_resolver_free(i->host_name_resolver);
85 AVAHI_LLIST_REMOVE(AvahiDNSServerInfo, info, b->info, i);
87 g_assert(b->n_info >= 1);
93 static void host_name_resolver_callback(AvahiHostNameResolver *r, AvahiIfIndex interface, AvahiProtocol protocol, AvahiResolverEvent event, const gchar *host_name, const AvahiAddress *a, gpointer userdata) {
94 AvahiDNSServerInfo *i = userdata;
100 if (event == AVAHI_RESOLVER_FOUND) {
103 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);
106 avahi_host_name_resolver_free(i->host_name_resolver);
107 i->host_name_resolver = NULL;
110 static void record_browser_callback(AvahiRecordBrowser*rr, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, AvahiRecord *record, gpointer userdata) {
111 AvahiDNSServerBrowser *b = userdata;
117 g_assert(record->key->type == AVAHI_DNS_TYPE_SRV);
119 if (event == AVAHI_BROWSER_NEW) {
120 AvahiDNSServerInfo *i;
122 if (get_server_info(b, interface, protocol, record))
128 i = g_new(AvahiDNSServerInfo, 1);
130 i->interface = interface;
131 i->protocol = protocol;
132 i->srv_record = avahi_record_ref(record);
133 i->host_name_resolver = avahi_host_name_resolver_new(b->server, interface, protocol, record->data.srv.name, b->aprotocol, host_name_resolver_callback, i);
135 AVAHI_LLIST_PREPEND(AvahiDNSServerInfo, info, b->info, i);
138 } else if (event == AVAHI_BROWSER_REMOVE) {
139 AvahiDNSServerInfo *i;
141 if (!(i = get_server_info(b, interface, protocol, record)))
144 if (!i->host_name_resolver)
145 b->callback(b, interface, protocol, event, i->srv_record->data.srv.name, &i->address, i->srv_record->data.srv.port, b->userdata);
147 server_info_free(b, i);
151 AvahiDNSServerBrowser *avahi_dns_server_browser_new(AvahiServer *server, AvahiIfIndex interface, AvahiProtocol protocol, const gchar *domain, AvahiDNSServerType type, AvahiProtocol aprotocol, AvahiDNSServerBrowserCallback callback, gpointer userdata) {
152 AvahiDNSServerBrowser *b;
158 g_assert(type == AVAHI_DNS_SERVER_RESOLVE || type == AVAHI_DNS_SERVER_UPDATE);
160 b = g_new(AvahiDNSServerBrowser, 1);
162 b->domain_name = avahi_normalize_name(domain ? domain : "local.");
163 b->callback = callback;
164 b->userdata = userdata;
165 b->aprotocol = aprotocol;
168 AVAHI_LLIST_HEAD_INIT(AvahiDNSServerInfo, b->info);
170 n = g_strdup_printf("%s.%s",type == AVAHI_DNS_SERVER_RESOLVE ? "_domain._udp" : "_dns-update._udp", b->domain_name);
171 k = avahi_key_new(n, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_SRV);
174 b->record_browser = avahi_record_browser_new(server, interface, protocol, k, record_browser_callback, b);
177 AVAHI_LLIST_PREPEND(AvahiDNSServerBrowser, browser, server->dns_server_browsers, b);
182 void avahi_dns_server_browser_free(AvahiDNSServerBrowser *b) {
186 server_info_free(b, b->info);
188 AVAHI_LLIST_REMOVE(AvahiDNSServerBrowser, browser, b->server->dns_server_browsers, b);
190 avahi_record_browser_free(b->record_browser);
191 g_free(b->domain_name);