2 * Author: Lee Essen <lee.essen@nowonline.co.uk>
3 * Based on: avahi support from Daniel S. Haischt <me@daniel.stefan.haischt.name>
4 * Purpose: mdns based Zeroconf support
19 #include <atalk/logger.h>
20 #include <atalk/util.h>
21 #include <atalk/dsi.h>
22 #include <atalk/unicode.h>
23 #include <atalk/netatalk_conf.h>
25 #include "afp_zeroconf.h"
29 * We'll store all the DNSServiceRef's here so that we can
30 * deallocate them later
32 static DNSServiceRef *svc_refs = NULL;
33 static int svc_ref_count = 0;
34 static pthread_t poller;
37 * Its easier to use asprintf to set the TXT record values
40 int TXTRecordPrintf(TXTRecordRef * rec, const char * key, const char * fmt, ... )
47 if( 0 > vasprintf(&str, fmt, ap ) ) {
53 if( kDNSServiceErr_NoError != TXTRecordSetValue(rec, key, strlen(str), str) ) {
61 int TXTRecordKeyPrintf(TXTRecordRef * rec, const char * key_fmt, int key_var, const char * fmt, ...)
64 char *key = NULL, *str = NULL;
67 if( 0 > asprintf(&key, key_fmt, key_var))
71 if( 0 > vasprintf(&str, fmt, ap )) {
78 if( kDNSServiceErr_NoError != TXTRecordSetValue(rec, key, strlen(str), str) ) {
91 static struct pollfd *fds;
94 * This is the thread that polls the filehandles
96 static void *polling_thread(void *arg) {
97 // First we loop through getting the filehandles and adding them to our poll, we
98 // need to allocate our pollfd's
99 DNSServiceErrorType error;
100 fds = calloc(svc_ref_count, sizeof(struct pollfd));
103 for(int i=0; i < svc_ref_count; i++) {
104 int fd = DNSServiceRefSockFD(svc_refs[i]);
106 fds[i].events = POLLIN;
109 // Now we can poll and process the results...
110 while(poll(fds, svc_ref_count, -1) > 0) {
111 for(int i=0; i < svc_ref_count; i++) {
112 if(fds[i].revents & POLLIN) {
113 error = DNSServiceProcessResult(svc_refs[i]);
121 * This is the callback for the service register function ... actually there isn't a lot
122 * we can do if we get problems, so we don't really need to do anything other than report
125 static void RegisterReply(DNSServiceRef sdRef, DNSServiceFlags flags, DNSServiceErrorType errorCode,
126 const char *name, const char *regtype, const char *domain, void *context)
128 if (errorCode != kDNSServiceErr_NoError) {
129 LOG(log_error, logtype_afpd, "Failed to register mDNS service: %s%s%s: code=%d",
130 name, regtype, domain, errorCode);
135 * This function unregisters anything we have already
136 * registered and frees associated memory
138 static void unregister_stuff() {
139 pthread_cancel(poller);
141 for (int i = 0; i < svc_ref_count; i++)
147 for(int i=0; i < svc_ref_count; i++) {
148 DNSServiceRefDeallocate(svc_refs[i]);
157 * This function tries to register the AFP DNS
160 static void register_stuff(const AFPObj *obj) {
162 const struct vol *volume;
164 char name[MAXINSTANCENAMELEN+1];
165 DNSServiceErrorType error;
166 TXTRecordRef txt_adisk;
167 TXTRecordRef txt_devinfo;
170 // If we had already registered, then we will unregister and re-register
171 if(svc_refs) unregister_stuff();
173 /* Register our service, prepare the TXT record */
174 TXTRecordCreate(&txt_adisk, 0, NULL);
175 if( 0 > TXTRecordPrintf(&txt_adisk, "sys", "waMa=0,adVF=0x100") ) {
176 LOG ( log_error, logtype_afpd, "Could not create Zeroconf TXTRecord for sys");
180 /* Build AFP volumes list */
183 for (volume = getvolumes(); volume; volume = volume->v_next) {
185 if (convert_string(CH_UCS2, CH_UTF8_MAC, volume->v_u8mname, -1, tmpname, 255) <= 0) {
186 LOG ( log_error, logtype_afpd, "Could not set Zeroconf volume name for TimeMachine");
190 if (volume->v_flags & AFPVOL_TM) {
191 if (volume->v_uuid) {
192 LOG(log_info, logtype_afpd, "Registering volume '%s' with UUID: '%s' for TimeMachine",
193 volume->v_localname, volume->v_uuid);
194 if( 0 > TXTRecordKeyPrintf(&txt_adisk, "dk%u", i++, "adVN=%s,adVF=0xa1,adVU=%s",
195 tmpname, volume->v_uuid) ) {
196 LOG ( log_error, logtype_afpd, "Could not set Zeroconf TXTRecord for dk%u", i);
200 LOG(log_warning, logtype_afpd, "Registering volume '%s' for TimeMachine. But UUID is invalid.",
201 volume->v_localname);
202 if( 0 > TXTRecordKeyPrintf(&txt_adisk, "dk%u", i++, "adVN=%s,adVF=0xa1", tmpname) ) {
203 LOG ( log_error, logtype_afpd, "Could not set Zeroconf TXTRecord for dk%u", i);
210 // Now we can count the configs so we know how many service
211 // records to allocate
212 for (dsi = obj->dsi; dsi; dsi = dsi->next) {
213 svc_ref_count++; // AFP_DNS_SERVICE_TYPE
214 if (i) svc_ref_count++; // ADISK_SERVICE_TYPE
215 if (obj->options.mimicmodel) svc_ref_count++; // DEV_INFO_SERVICE_TYPE
218 // Allocate the memory to store our service refs
219 svc_refs = calloc(svc_ref_count, sizeof(DNSServiceRef));
224 for (dsi = obj->dsi; dsi; dsi = dsi->next) {
226 port = getip_port((struct sockaddr *)&dsi->server);
228 if (convert_string(obj->options.unixcharset,
230 obj->options.hostname,
233 MAXINSTANCENAMELEN) <= 0) {
234 LOG(log_error, logtype_afpd, "Could not set Zeroconf instance name");
237 if ((dsi->bonjourname = strdup(name)) == NULL) {
238 LOG(log_error, logtype_afpd, "Could not set Zeroconf instance name");
242 LOG(log_info, logtype_afpd, "Registering server '%s' with Bonjour",
245 error = DNSServiceRegister(&svc_refs[svc_ref_count++],
247 0, // all network interfaces
249 AFP_DNS_SERVICE_TYPE,
250 "", // default domains
251 NULL, // default host name
255 RegisterReply, // callback
257 if(error != kDNSServiceErr_NoError) {
258 LOG(log_error, logtype_afpd, "Failed to add service: %s, error=%d",
259 AFP_DNS_SERVICE_TYPE, error);
264 error = DNSServiceRegister(&svc_refs[svc_ref_count++],
266 0, // all network interfaces
269 "", // default domains
270 NULL, // default host name
272 TXTRecordGetLength(&txt_adisk),
273 TXTRecordGetBytesPtr(&txt_adisk),
274 RegisterReply, // callback
276 if(error != kDNSServiceErr_NoError) {
277 LOG(log_error, logtype_afpd, "Failed to add service: %s, error=%d",
278 ADISK_SERVICE_TYPE, error);
283 if (obj->options.mimicmodel) {
284 LOG(log_info, logtype_afpd, "Registering server '%s' with model '%s'",
285 dsi->bonjourname, obj->options.mimicmodel);
286 TXTRecordCreate(&txt_devinfo, 0, NULL);
287 if( 0 > TXTRecordPrintf(&txt_devinfo, "model", obj->options.mimicmodel) ) {
288 LOG ( log_error, logtype_afpd, "Could not create Zeroconf TXTRecord for model");
292 error = DNSServiceRegister(&svc_refs[svc_ref_count++],
294 0, // all network interfaces
296 DEV_INFO_SERVICE_TYPE,
297 "", // default domains
298 NULL, // default host name
300 * We would probably use port 0 zero, but we can't, from man DNSServiceRegister:
301 * "A value of 0 for a port is passed to register placeholder services.
302 * Place holder services are not found when browsing, but other
303 * clients cannot register with the same name as the placeholder service."
304 * We therefor use port 9 which is used by the adisk service type.
307 TXTRecordGetLength(&txt_devinfo),
308 TXTRecordGetBytesPtr(&txt_devinfo),
309 RegisterReply, // callback
311 TXTRecordDeallocate(&txt_devinfo);
312 if(error != kDNSServiceErr_NoError) {
313 LOG(log_error, logtype_afpd, "Failed to add service: %s, error=%d",
314 DEV_INFO_SERVICE_TYPE, error);
317 } /* if (config->obj.options.mimicmodel) */
321 * Now we can create the thread that will poll for the results
322 * and handle the calling of the callbacks
324 if(pthread_create(&poller, NULL, polling_thread, NULL) != 0) {
325 LOG(log_error, logtype_afpd, "Unable to start mDNS polling thread");
330 TXTRecordDeallocate(&txt_adisk);
334 /************************************************************************
336 ************************************************************************/
339 * Tries to setup the Zeroconf thread and any
340 * neccessary config setting.
342 void md_zeroconf_register(const AFPObj *obj) {
350 * Tries to shutdown this loop impl.
351 * Call this function from inside this thread.
353 int md_zeroconf_unregister() {
358 #endif /* USE_MDNS */