2 * Author: Daniel S. Haischt <me@daniel.stefan.haischt.name>
3 * Purpose: Avahi based Zeroconf support
4 * Docs: http://avahi.org/download/doxygen/
17 #include <avahi-common/strlst.h>
19 #include <atalk/logger.h>
20 #include <atalk/util.h>
21 #include <atalk/dsi.h>
22 #include <atalk/unicode.h>
24 #include "afp_avahi.h"
25 #include "afp_config.h"
28 /*****************************************************************
30 *****************************************************************/
31 struct context *ctx = NULL;
33 /*****************************************************************
35 *****************************************************************/
37 static void publish_reply(AvahiEntryGroup *g,
38 AvahiEntryGroupState state,
42 * This function tries to register the AFP DNS
45 static void register_stuff(void) {
47 const AFPConfig *config;
48 const struct vol *volume;
50 char name[MAXINSTANCENAMELEN+1];
51 AvahiStringList *strlist = NULL;
52 AvahiStringList *strlist2 = NULL;
58 if (!(ctx->group = avahi_entry_group_new(ctx->client, publish_reply, ctx))) {
59 LOG(log_error, logtype_afpd, "Failed to create entry group: %s",
60 avahi_strerror(avahi_client_errno(ctx->client)));
65 if (avahi_entry_group_is_empty(ctx->group)) {
66 /* Register our service */
68 /* Build AFP volumes list */
70 strlist = avahi_string_list_add_printf(strlist, "sys=waMa=0,adVF=0x100");
72 for (volume = getvolumes(); volume; volume = volume->v_next) {
74 if (convert_string(CH_UCS2, CH_UTF8_MAC, volume->v_name, -1, tmpname, 255) <= 0) {
75 LOG ( log_error, logtype_afpd, "Could not set Zeroconf volume name for TimeMachine");
79 if (volume->v_flags & AFPVOL_TM) {
81 LOG(log_info, logtype_afpd, "Registering volume '%s' with UUID: '%s' for TimeMachine",
82 volume->v_localname, volume->v_uuid);
83 strlist = avahi_string_list_add_printf(strlist, "dk%u=adVN=%s,adVF=0xa1,adVU=%s",
84 i++, tmpname, volume->v_uuid);
86 LOG(log_warning, logtype_afpd, "Registering volume '%s' for TimeMachine. But UUID is invalid.",
88 strlist = avahi_string_list_add_printf(strlist, "dk%u=adVN=%s,adVF=0xa1",
95 for (config = ctx->configs; config; config = config->next) {
97 dsi = (DSI *)config->obj.handle;
98 port = getip_port((struct sockaddr *)&dsi->server);
100 if (convert_string(config->obj.options.unixcharset,
102 config->obj.options.server ?
103 config->obj.options.server :
104 config->obj.options.hostname,
107 MAXINSTANCENAMELEN) <= 0) {
108 LOG(log_error, logtype_afpd, "Could not set Zeroconf instance name");
111 if ((dsi->bonjourname = strdup(name)) == NULL) {
112 LOG(log_error, logtype_afpd, "Could not set Zeroconf instance name");
116 LOG(log_info, logtype_afpd, "Registering server '%s' with Bonjour",
119 if (avahi_entry_group_add_service(ctx->group,
124 AFP_DNS_SERVICE_TYPE,
129 LOG(log_error, logtype_afpd, "Failed to add service: %s",
130 avahi_strerror(avahi_client_errno(ctx->client)));
134 if (i && avahi_entry_group_add_service_strlst(ctx->group,
144 LOG(log_error, logtype_afpd, "Failed to add service: %s",
145 avahi_strerror(avahi_client_errno(ctx->client)));
149 if (config->obj.options.mimicmodel) {
150 strlist2 = avahi_string_list_add_printf(strlist2, "model=%s", config->obj.options.mimicmodel);
151 if (avahi_entry_group_add_service_strlst(ctx->group,
156 DEV_INFO_SERVICE_TYPE,
161 LOG(log_error, logtype_afpd, "Failed to add service: %s",
162 avahi_strerror(avahi_client_errno(ctx->client)));
165 } /* if (config->obj.options.mimicmodel) */
169 if (avahi_entry_group_commit(ctx->group) < 0) {
170 LOG(log_error, logtype_afpd, "Failed to commit entry group: %s",
171 avahi_strerror(avahi_client_errno(ctx->client)));
175 } /* if avahi_entry_group_is_empty*/
181 // avahi_threaded_poll_quit(ctx->threaded_poll);
184 /* Called when publishing of service data completes */
185 static void publish_reply(AvahiEntryGroup *g,
186 AvahiEntryGroupState state,
187 AVAHI_GCC_UNUSED void *userdata)
189 assert(ctx->group == NULL || g == ctx->group);
193 case AVAHI_ENTRY_GROUP_ESTABLISHED :
194 /* The entry group has been established successfully */
195 LOG(log_debug, logtype_afpd, "publish_reply: AVAHI_ENTRY_GROUP_ESTABLISHED");
198 case AVAHI_ENTRY_GROUP_COLLISION:
199 /* With multiple names there's no way to know which one collided */
200 LOG(log_error, logtype_afpd, "publish_reply: AVAHI_ENTRY_GROUP_COLLISION",
201 avahi_strerror(avahi_client_errno(ctx->client)));
202 avahi_threaded_poll_quit(ctx->threaded_poll);
205 case AVAHI_ENTRY_GROUP_FAILURE:
206 LOG(log_error, logtype_afpd, "Failed to register service: %s",
207 avahi_strerror(avahi_client_errno(ctx->client)));
208 avahi_threaded_poll_quit(ctx->threaded_poll);
211 case AVAHI_ENTRY_GROUP_UNCOMMITED:
213 case AVAHI_ENTRY_GROUP_REGISTERING:
218 static void client_callback(AvahiClient *client,
219 AvahiClientState state,
222 ctx->client = client;
225 case AVAHI_CLIENT_S_RUNNING:
226 /* The server has startup successfully and registered its host
227 * name on the network, so it's time to create our services */
232 case AVAHI_CLIENT_S_COLLISION:
234 avahi_entry_group_reset(ctx->group);
237 case AVAHI_CLIENT_FAILURE: {
238 if (avahi_client_errno(client) == AVAHI_ERR_DISCONNECTED) {
241 avahi_client_free(ctx->client);
245 /* Reconnect to the server */
246 if (!(ctx->client = avahi_client_new(avahi_threaded_poll_get(ctx->threaded_poll),
247 AVAHI_CLIENT_NO_FAIL,
252 LOG(log_error, logtype_afpd, "Failed to contact server: %s",
253 avahi_strerror(error));
255 avahi_threaded_poll_quit(ctx->threaded_poll);
259 LOG(log_error, logtype_afpd, "Client failure: %s",
260 avahi_strerror(avahi_client_errno(client)));
261 avahi_threaded_poll_quit(ctx->threaded_poll);
266 case AVAHI_CLIENT_S_REGISTERING:
268 case AVAHI_CLIENT_CONNECTING:
273 /************************************************************************
275 ************************************************************************/
278 * Tries to setup the Zeroconf thread and any
279 * neccessary config setting.
281 void av_zeroconf_register(const AFPConfig *configs) {
284 /* initialize the struct that holds our config settings. */
286 LOG(log_debug, logtype_afpd, "Resetting zeroconf records");
287 avahi_entry_group_reset(ctx->group);
289 ctx = calloc(1, sizeof(struct context));
290 ctx->configs = configs;
294 /* first of all we need to initialize our threading env */
295 if (!(ctx->threaded_poll = avahi_threaded_poll_new())) {
299 /* now we need to acquire a client */
300 if (!(ctx->client = avahi_client_new(avahi_threaded_poll_get(ctx->threaded_poll),
301 AVAHI_CLIENT_NO_FAIL,
305 LOG(log_error, logtype_afpd, "Failed to create client object: %s",
306 avahi_strerror(error));
310 if (avahi_threaded_poll_start(ctx->threaded_poll) < 0) {
311 LOG(log_error, logtype_afpd, "Failed to create thread: %s",
312 avahi_strerror(avahi_client_errno(ctx->client)));
315 LOG(log_info, logtype_afpd, "Successfully started avahi loop.");
318 ctx->thread_running = 1;
322 av_zeroconf_unregister();
328 * Tries to shutdown this loop impl.
329 * Call this function from inside this thread.
331 int av_zeroconf_unregister() {
332 LOG(log_error, logtype_afpd, "av_zeroconf_unregister");
335 LOG(log_error, logtype_afpd, "av_zeroconf_unregister: avahi_threaded_poll_stop");
336 if (ctx->threaded_poll)
337 avahi_threaded_poll_stop(ctx->threaded_poll);
338 LOG(log_error, logtype_afpd, "av_zeroconf_unregister: avahi_client_free");
340 avahi_client_free(ctx->client);
341 LOG(log_error, logtype_afpd, "av_zeroconf_unregister: avahi_threaded_poll_free");
342 if (ctx->threaded_poll)
343 avahi_threaded_poll_free(ctx->threaded_poll);
350 #endif /* USE_AVAHI */