1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
|
/* $Id$ */
/***
This file is part of avahi.
avahi is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2.1 of the
License, or (at your option) any later version.
avahi is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with avahi; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA.
***/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <string.h>
#include <avahi-common/domain.h>
#include <avahi-common/malloc.h>
#include <avahi-common/error.h>
#include "browse.h"
#include "log.h"
#include "rr.h"
typedef struct AvahiDNSServerInfo AvahiDNSServerInfo;
struct AvahiDNSServerInfo {
AvahiSDNSServerBrowser *browser;
AvahiIfIndex interface;
AvahiProtocol protocol;
AvahiRecord *srv_record;
AvahiSHostNameResolver *host_name_resolver;
AvahiAddress address;
AVAHI_LLIST_FIELDS(AvahiDNSServerInfo, info);
};
struct AvahiSDNSServerBrowser {
AvahiServer *server;
char *domain_name;
AvahiSRecordBrowser *record_browser;
AvahiSDNSServerBrowserCallback callback;
void* userdata;
AvahiProtocol aprotocol;
unsigned n_info;
AVAHI_LLIST_FIELDS(AvahiSDNSServerBrowser, browser);
AVAHI_LLIST_HEAD(AvahiDNSServerInfo, info);
};
static AvahiDNSServerInfo* get_server_info(AvahiSDNSServerBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiRecord *r) {
AvahiDNSServerInfo *i;
assert(b);
assert(r);
for (i = b->info; i; i = i->info_next)
if (i->interface == interface &&
i->protocol == protocol &&
avahi_record_equal_no_ttl(r, i->srv_record))
return i;
return NULL;
}
static void server_info_free(AvahiSDNSServerBrowser *b, AvahiDNSServerInfo *i) {
assert(b);
assert(i);
avahi_record_unref(i->srv_record);
if (i->host_name_resolver)
avahi_s_host_name_resolver_free(i->host_name_resolver);
AVAHI_LLIST_REMOVE(AvahiDNSServerInfo, info, b->info, i);
assert(b->n_info >= 1);
b->n_info--;
avahi_free(i);
}
static void host_name_resolver_callback(AvahiSHostNameResolver *r, AvahiIfIndex interface, AvahiProtocol protocol, AvahiResolverEvent event, const char *host_name, const AvahiAddress *a, void* userdata) {
AvahiDNSServerInfo *i = userdata;
assert(r);
assert(host_name);
assert(i);
if (event == AVAHI_RESOLVER_FOUND) {
i->address = *a;
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);
}
avahi_s_host_name_resolver_free(i->host_name_resolver);
i->host_name_resolver = NULL;
}
static void record_browser_callback(AvahiSRecordBrowser*rr, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, AvahiRecord *record, void* userdata) {
AvahiSDNSServerBrowser *b = userdata;
assert(rr);
assert(record);
assert(b);
assert(record->key->type == AVAHI_DNS_TYPE_SRV);
if (event == AVAHI_BROWSER_NEW) {
AvahiDNSServerInfo *i;
if (get_server_info(b, interface, protocol, record))
return;
if (b->n_info >= 10)
return;
if (!(i = avahi_new(AvahiDNSServerInfo, 1)))
return; /* OOM */
i->browser = b;
i->interface = interface;
i->protocol = protocol;
i->srv_record = avahi_record_ref(record);
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);
AVAHI_LLIST_PREPEND(AvahiDNSServerInfo, info, b->info, i);
b->n_info++;
} else if (event == AVAHI_BROWSER_REMOVE) {
AvahiDNSServerInfo *i;
if (!(i = get_server_info(b, interface, protocol, record)))
return;
if (!i->host_name_resolver)
b->callback(b, interface, protocol, event, i->srv_record->data.srv.name, &i->address, i->srv_record->data.srv.port, b->userdata);
server_info_free(b, i);
}
}
AvahiSDNSServerBrowser *avahi_s_dns_server_browser_new(AvahiServer *server, AvahiIfIndex interface, AvahiProtocol protocol, const char *domain, AvahiDNSServerType type, AvahiProtocol aprotocol, AvahiSDNSServerBrowserCallback callback, void* userdata) {
AvahiSDNSServerBrowser *b;
AvahiKey *k;
char *n = NULL;
assert(server);
assert(callback);
assert(type == AVAHI_DNS_SERVER_RESOLVE || type == AVAHI_DNS_SERVER_UPDATE);
if (domain && !avahi_is_valid_domain_name(domain)) {
avahi_server_set_errno(server, AVAHI_ERR_INVALID_DOMAIN_NAME);
return NULL;
}
if (!(b = avahi_new(AvahiSDNSServerBrowser, 1))) {
avahi_server_set_errno(server, AVAHI_ERR_NO_MEMORY);
return NULL;
}
b->server = server;
b->domain_name = avahi_normalize_name(domain ? domain : "local");
b->callback = callback;
b->userdata = userdata;
b->aprotocol = aprotocol;
b->n_info = 0;
AVAHI_LLIST_HEAD_INIT(AvahiDNSServerInfo, b->info);
AVAHI_LLIST_PREPEND(AvahiSDNSServerBrowser, browser, server->dns_server_browsers, b);
n = avahi_strdup_printf("%s.%s",type == AVAHI_DNS_SERVER_RESOLVE ? "_domain._udp" : "_dns-update._udp", b->domain_name);
k = avahi_key_new(n, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_SRV);
avahi_free(n);
b->record_browser = avahi_s_record_browser_new(server, interface, protocol, k, record_browser_callback, b);
avahi_key_unref(k);
if (!b->record_browser) {
avahi_s_dns_server_browser_free(b);
return NULL;
}
return b;
}
void avahi_s_dns_server_browser_free(AvahiSDNSServerBrowser *b) {
assert(b);
while (b->info)
server_info_free(b, b->info);
AVAHI_LLIST_REMOVE(AvahiSDNSServerBrowser, browser, b->server->dns_server_browsers, b);
if (b->record_browser)
avahi_s_record_browser_free(b->record_browser);
avahi_free(b->domain_name);
avahi_free(b);
}
|