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
39 #define TXTRecordPrintf(rec, key, args, ...) { \
41 asprintf(&str, args); \
42 TXTRecordSetValue(rec, key, strlen(str), str); \
45 #define TXTRecordKeyPrintf(rec, k, var, args, ...) { \
47 asprintf(&key, k, var); \
48 asprintf(&str, args); \
49 TXTRecordSetValue(rec, key, strlen(str), str); \
50 free(str); free(key); \
53 static struct pollfd *fds;
56 * This is the thread that polls the filehandles
58 static void *polling_thread(void *arg) {
59 // First we loop through getting the filehandles and adding them to our poll, we
60 // need to allocate our pollfd's
61 DNSServiceErrorType error;
62 fds = calloc(svc_ref_count, sizeof(struct pollfd));
65 for(int i=0; i < svc_ref_count; i++) {
66 int fd = DNSServiceRefSockFD(svc_refs[i]);
68 fds[i].events = POLLIN;
71 // Now we can poll and process the results...
72 while(poll(fds, svc_ref_count, -1) > 0) {
73 for(int i=0; i < svc_ref_count; i++) {
74 if(fds[i].revents & POLLIN) {
75 error = DNSServiceProcessResult(svc_refs[i]);
83 * This is the callback for the service register function ... actually there isn't a lot
84 * we can do if we get problems, so we don't really need to do anything other than report
87 static void RegisterReply(DNSServiceRef sdRef, DNSServiceFlags flags, DNSServiceErrorType errorCode,
88 const char *name, const char *regtype, const char *domain, void *context)
90 if (errorCode != kDNSServiceErr_NoError) {
91 LOG(log_error, logtype_afpd, "Failed to register mDNS service: %s%s%s: code=%d",
92 name, regtype, domain, errorCode);
97 * This function unregisters anything we have already
98 * registered and frees associated memory
100 static void unregister_stuff() {
101 pthread_cancel(poller);
103 for (int i = 0; i < svc_ref_count; i++)
109 for(int i=0; i < svc_ref_count; i++) {
110 DNSServiceRefDeallocate(svc_refs[i]);
119 * This function tries to register the AFP DNS
122 static void register_stuff(const AFPObj *obj) {
124 const struct vol *volume;
126 char name[MAXINSTANCENAMELEN+1];
127 DNSServiceErrorType error;
128 TXTRecordRef txt_adisk;
129 TXTRecordRef txt_devinfo;
132 // If we had already registered, then we will unregister and re-register
133 if(svc_refs) unregister_stuff();
135 /* Register our service, prepare the TXT record */
136 TXTRecordCreate(&txt_adisk, 0, NULL);
137 TXTRecordPrintf(&txt_adisk, "sys", "waMa=0,adVF=0x100");
139 /* Build AFP volumes list */
142 for (volume = getvolumes(); volume; volume = volume->v_next) {
144 if (convert_string(CH_UCS2, CH_UTF8_MAC, volume->v_u8mname, -1, tmpname, 255) <= 0) {
145 LOG ( log_error, logtype_afpd, "Could not set Zeroconf volume name for TimeMachine");
149 if (volume->v_flags & AFPVOL_TM) {
150 if (volume->v_uuid) {
151 LOG(log_info, logtype_afpd, "Registering volume '%s' with UUID: '%s' for TimeMachine",
152 volume->v_localname, volume->v_uuid);
153 TXTRecordKeyPrintf(&txt_adisk, "dk%u", i++, "adVN=%s,adVF=0xa1,adVU=%s",
154 tmpname, volume->v_uuid);
156 LOG(log_warning, logtype_afpd, "Registering volume '%s' for TimeMachine. But UUID is invalid.",
157 volume->v_localname);
158 TXTRecordKeyPrintf(&txt_adisk, "dk%u", i++, "adVN=%s,adVF=0xa1", tmpname);
163 // Now we can count the configs so we know how many service
164 // records to allocate
165 for (dsi = obj->dsi; dsi; dsi = dsi->next) {
166 svc_ref_count++; // AFP_DNS_SERVICE_TYPE
167 if (i) svc_ref_count++; // ADISK_SERVICE_TYPE
168 if (obj->options.mimicmodel) svc_ref_count++; // DEV_INFO_SERVICE_TYPE
171 // Allocate the memory to store our service refs
172 svc_refs = calloc(svc_ref_count, sizeof(DNSServiceRef));
177 for (dsi = obj->dsi; dsi; dsi = dsi->next) {
179 port = getip_port((struct sockaddr *)&dsi->server);
181 if (convert_string(obj->options.unixcharset,
183 obj->options.hostname,
186 MAXINSTANCENAMELEN) <= 0) {
187 LOG(log_error, logtype_afpd, "Could not set Zeroconf instance name");
190 if ((dsi->bonjourname = strdup(name)) == NULL) {
191 LOG(log_error, logtype_afpd, "Could not set Zeroconf instance name");
195 LOG(log_info, logtype_afpd, "Registering server '%s' with Bonjour",
198 error = DNSServiceRegister(&svc_refs[svc_ref_count++],
200 0, // all network interfaces
202 AFP_DNS_SERVICE_TYPE,
203 "", // default domains
204 NULL, // default host name
208 RegisterReply, // callback
210 if(error != kDNSServiceErr_NoError) {
211 LOG(log_error, logtype_afpd, "Failed to add service: %s, error=%d",
212 AFP_DNS_SERVICE_TYPE, error);
217 error = DNSServiceRegister(&svc_refs[svc_ref_count++],
219 0, // all network interfaces
222 "", // default domains
223 NULL, // default host name
225 TXTRecordGetLength(&txt_adisk),
226 TXTRecordGetBytesPtr(&txt_adisk),
227 RegisterReply, // callback
229 if(error != kDNSServiceErr_NoError) {
230 LOG(log_error, logtype_afpd, "Failed to add service: %s, error=%d",
231 ADISK_SERVICE_TYPE, error);
236 if (obj->options.mimicmodel) {
237 LOG(log_info, logtype_afpd, "Registering server '%s' with model '%s'",
238 dsi->bonjourname, obj->options.mimicmodel);
239 TXTRecordCreate(&txt_devinfo, 0, NULL);
240 TXTRecordPrintf(&txt_devinfo, "model", obj->options.mimicmodel);
241 error = DNSServiceRegister(&svc_refs[svc_ref_count++],
243 0, // all network interfaces
245 DEV_INFO_SERVICE_TYPE,
246 "", // default domains
247 NULL, // default host name
249 * We would probably use port 0 zero, but we can't, from man DNSServiceRegister:
250 * "A value of 0 for a port is passed to register placeholder services.
251 * Place holder services are not found when browsing, but other
252 * clients cannot register with the same name as the placeholder service."
253 * We therefor use port 9 which is used by the adisk service type.
256 TXTRecordGetLength(&txt_devinfo),
257 TXTRecordGetBytesPtr(&txt_devinfo),
258 RegisterReply, // callback
260 TXTRecordDeallocate(&txt_devinfo);
261 if(error != kDNSServiceErr_NoError) {
262 LOG(log_error, logtype_afpd, "Failed to add service: %s, error=%d",
263 DEV_INFO_SERVICE_TYPE, error);
266 } /* if (config->obj.options.mimicmodel) */
270 * Now we can create the thread that will poll for the results
271 * and handle the calling of the callbacks
273 if(pthread_create(&poller, NULL, polling_thread, NULL) != 0) {
274 LOG(log_error, logtype_afpd, "Unable to start mDNS polling thread");
279 TXTRecordDeallocate(&txt_adisk);
283 /************************************************************************
285 ************************************************************************/
288 * Tries to setup the Zeroconf thread and any
289 * neccessary config setting.
291 void md_zeroconf_register(const AFPObj *obj) {
299 * Tries to shutdown this loop impl.
300 * Call this function from inside this thread.
302 int md_zeroconf_unregister() {
307 #endif /* USE_MDNS */