]> git.meshlink.io Git - catta/blob - avahi-core/browse-dns-server.c
* strip glib from avahi-core
[catta] / avahi-core / browse-dns-server.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
31 #include "browse.h"
32 #include "log.h"
33 #include "rr.h"
34
35 typedef struct AvahiDNSServerInfo AvahiDNSServerInfo;
36
37 struct AvahiDNSServerInfo {
38     AvahiDNSServerBrowser *browser;
39
40     AvahiIfIndex interface;
41     AvahiProtocol protocol;
42     AvahiRecord *srv_record;
43     AvahiHostNameResolver *host_name_resolver;
44     AvahiAddress address;
45     
46     AVAHI_LLIST_FIELDS(AvahiDNSServerInfo, info);
47 };
48
49 struct AvahiDNSServerBrowser {
50     AvahiServer *server;
51     char *domain_name;
52     
53     AvahiRecordBrowser *record_browser;
54     AvahiDNSServerBrowserCallback callback;
55     void* userdata;
56     AvahiProtocol aprotocol;
57
58     unsigned n_info;
59     
60     AVAHI_LLIST_FIELDS(AvahiDNSServerBrowser, browser);
61     AVAHI_LLIST_HEAD(AvahiDNSServerInfo, info);
62 };
63
64 static AvahiDNSServerInfo* get_server_info(AvahiDNSServerBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiRecord *r) {
65     AvahiDNSServerInfo *i;
66     
67     assert(b);
68     assert(r);
69
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))
74             return i;
75
76     return NULL;
77 }
78
79 static void server_info_free(AvahiDNSServerBrowser *b, AvahiDNSServerInfo *i) {
80     assert(b);
81     assert(i);
82
83     avahi_record_unref(i->srv_record);
84     if (i->host_name_resolver)
85         avahi_host_name_resolver_free(i->host_name_resolver);
86     
87     AVAHI_LLIST_REMOVE(AvahiDNSServerInfo, info, b->info, i);
88
89     assert(b->n_info >= 1);
90     b->n_info--;
91     
92     avahi_free(i);
93 }
94
95 static void host_name_resolver_callback(AvahiHostNameResolver *r, AvahiIfIndex interface, AvahiProtocol protocol, AvahiResolverEvent event, const char *host_name, const AvahiAddress *a, void* userdata) {
96     AvahiDNSServerInfo *i = userdata;
97     
98     assert(r);
99     assert(host_name);
100     assert(i);
101
102     if (event == AVAHI_RESOLVER_FOUND) {
103         i->address = *a;
104
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);
106     }
107
108     avahi_host_name_resolver_free(i->host_name_resolver);
109     i->host_name_resolver = NULL;
110 }
111
112 static void record_browser_callback(AvahiRecordBrowser*rr, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, AvahiRecord *record, void* userdata) {
113     AvahiDNSServerBrowser *b = userdata;
114
115     assert(rr);
116     assert(record);
117     assert(b);
118
119     assert(record->key->type == AVAHI_DNS_TYPE_SRV);
120
121     if (event == AVAHI_BROWSER_NEW) {
122         AvahiDNSServerInfo *i;
123
124         if (get_server_info(b, interface, protocol, record))
125             return;
126
127         if (b->n_info >= 10)
128             return;
129         
130         if (!(i = avahi_new(AvahiDNSServerInfo, 1)))
131             return; /* OOM */
132         
133         i->browser = b;
134         i->interface = interface;
135         i->protocol = protocol;
136         i->srv_record = avahi_record_ref(record);
137         i->host_name_resolver = avahi_host_name_resolver_new(b->server, interface, protocol, record->data.srv.name, b->aprotocol, host_name_resolver_callback, i);
138         
139         AVAHI_LLIST_PREPEND(AvahiDNSServerInfo, info, b->info, i);
140
141         b->n_info++;
142     } else if (event == AVAHI_BROWSER_REMOVE) {
143         AvahiDNSServerInfo *i;
144
145         if (!(i = get_server_info(b, interface, protocol, record)))
146             return;
147
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);
150
151         server_info_free(b, i);
152     }
153 }
154
155 AvahiDNSServerBrowser *avahi_dns_server_browser_new(AvahiServer *server, AvahiIfIndex interface, AvahiProtocol protocol, const char *domain, AvahiDNSServerType type, AvahiProtocol aprotocol, AvahiDNSServerBrowserCallback callback, void* userdata) {
156     AvahiDNSServerBrowser *b;
157     AvahiKey *k;
158     char *n = NULL;
159     
160     assert(server);
161     assert(callback);
162     assert(type == AVAHI_DNS_SERVER_RESOLVE || type == AVAHI_DNS_SERVER_UPDATE);
163
164     if (domain && !avahi_is_valid_domain_name(domain)) {
165         avahi_server_set_errno(server, AVAHI_ERR_INVALID_DOMAIN_NAME);
166         return NULL;
167     }
168     
169     if (!(b = avahi_new(AvahiDNSServerBrowser, 1))) {
170         avahi_server_set_errno(server, AVAHI_ERR_NO_MEMORY);
171         return NULL;
172     }
173     
174     b->server = server;
175     b->domain_name = avahi_normalize_name(domain ? domain : "local");
176     b->callback = callback;
177     b->userdata = userdata;
178     b->aprotocol = aprotocol;
179     b->n_info = 0;
180
181     AVAHI_LLIST_HEAD_INIT(AvahiDNSServerInfo, b->info);
182     AVAHI_LLIST_PREPEND(AvahiDNSServerBrowser, browser, server->dns_server_browsers, b);
183     
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);
186     avahi_free(n);
187     
188     b->record_browser = avahi_record_browser_new(server, interface, protocol, k, record_browser_callback, b);
189     avahi_key_unref(k);
190
191     if (!b->record_browser) {
192         avahi_dns_server_browser_free(b);
193         return NULL;
194     }
195     
196     return b;
197 }
198
199 void avahi_dns_server_browser_free(AvahiDNSServerBrowser *b) {
200     assert(b);
201
202     while (b->info)
203         server_info_free(b, b->info);
204     
205     AVAHI_LLIST_REMOVE(AvahiDNSServerBrowser, browser, b->server->dns_server_browsers, b);
206
207     if (b->record_browser)
208         avahi_record_browser_free(b->record_browser);
209     avahi_free(b->domain_name);
210     avahi_free(b);
211 }
212