1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: t; c-basic-offset: 2 -*- */
3 * Author: Daniel S. Haischt <me@daniel.stefan.haischt.name>
4 * Purpose: Avahi based Zeroconf support
5 * 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;
51 AvahiStringList *strlist = NULL;
57 if (!(ctx->group = avahi_entry_group_new(ctx->client, publish_reply, ctx))) {
58 LOG(log_error, logtype_afpd, "Failed to create entry group: %s",
59 avahi_strerror(avahi_client_errno(ctx->client)));
64 if (avahi_entry_group_is_empty(ctx->group)) {
65 /* Register our service */
67 /* Build AFP volumes list */
69 strlist = avahi_string_list_add_printf(strlist, "sys=waMa=0,adVF=0x100");
71 for (volume = getvolumes(); volume; volume = volume->v_next) {
73 if (convert_string(CH_UCS2, CH_UTF8_MAC, volume->v_name, -1, tmpname, 255) <= 0)
76 if ((volume->v_flags & AFPVOL_TM) && volume->v_uuid) {
77 LOG(log_info, logtype_afpd, "Registering volume '%s' with UUID: '%s' for TimeMachine",
78 volume->v_localname, volume->v_uuid);
79 strlist = avahi_string_list_add_printf(strlist, "dk%u=adVN=%s,adVF=0xa1,adVU=%s",
80 i++, tmpname, volume->v_uuid);
85 for (config = ctx->configs; config; config = config->next) {
87 dsi = (DSI *)config->obj.handle;
88 name = config->obj.options.server ?
89 config->obj.options.server : config->obj.options.hostname;
90 port = getip_port((struct sockaddr *)&dsi->server);
92 if (avahi_entry_group_add_service(ctx->group,
102 LOG(log_error, logtype_afpd, "Failed to add service: %s",
103 avahi_strerror(avahi_client_errno(ctx->client)));
107 if (i && avahi_entry_group_add_service_strlst(ctx->group,
117 LOG(log_error, logtype_afpd, "Failed to add service: %s",
118 avahi_strerror(avahi_client_errno(ctx->client)));
123 if (avahi_entry_group_commit(ctx->group) < 0) {
124 LOG(log_error, logtype_afpd, "Failed to commit entry group: %s",
125 avahi_strerror(avahi_client_errno(ctx->client)));
129 } /* if avahi_entry_group_is_empty*/
134 avahi_client_free (ctx->client);
135 avahi_threaded_poll_quit(ctx->threaded_poll);
138 /* Called when publishing of service data completes */
139 static void publish_reply(AvahiEntryGroup *g,
140 AvahiEntryGroupState state,
141 AVAHI_GCC_UNUSED void *userdata)
143 assert(ctx->group == NULL || g == ctx->group);
147 case AVAHI_ENTRY_GROUP_ESTABLISHED :
148 /* The entry group has been established successfully */
149 LOG(log_debug, logtype_afpd, "publish_reply: AVAHI_ENTRY_GROUP_ESTABLISHED");
152 case AVAHI_ENTRY_GROUP_COLLISION:
153 /* With multiple names there's no way to know which one collided */
154 LOG(log_error, logtype_afpd, "publish_reply: AVAHI_ENTRY_GROUP_COLLISION",
155 avahi_strerror(avahi_client_errno(ctx->client)));
156 avahi_client_free(avahi_entry_group_get_client(g));
157 avahi_threaded_poll_quit(ctx->threaded_poll);
160 case AVAHI_ENTRY_GROUP_FAILURE:
161 LOG(log_error, logtype_afpd, "Failed to register service: %s",
162 avahi_strerror(avahi_client_errno(ctx->client)));
163 avahi_client_free(avahi_entry_group_get_client(g));
164 avahi_threaded_poll_quit(ctx->threaded_poll);
167 case AVAHI_ENTRY_GROUP_UNCOMMITED:
169 case AVAHI_ENTRY_GROUP_REGISTERING:
174 static void client_callback(AvahiClient *client,
175 AvahiClientState state,
178 ctx->client = client;
181 case AVAHI_CLIENT_S_RUNNING:
182 /* The server has startup successfully and registered its host
183 * name on the network, so it's time to create our services */
188 case AVAHI_CLIENT_S_COLLISION:
190 avahi_entry_group_reset(ctx->group);
193 case AVAHI_CLIENT_FAILURE: {
194 if (avahi_client_errno(client) == AVAHI_ERR_DISCONNECTED) {
197 avahi_client_free(ctx->client);
201 /* Reconnect to the server */
202 if (!(ctx->client = avahi_client_new(avahi_threaded_poll_get(ctx->threaded_poll),
203 AVAHI_CLIENT_NO_FAIL,
208 LOG(log_error, logtype_afpd, "Failed to contact server: %s",
209 avahi_strerror(error));
211 avahi_client_free (ctx->client);
212 avahi_threaded_poll_quit(ctx->threaded_poll);
216 LOG(log_error, logtype_afpd, "Client failure: %s",
217 avahi_strerror(avahi_client_errno(client)));
218 avahi_client_free (ctx->client);
219 avahi_threaded_poll_quit(ctx->threaded_poll);
224 case AVAHI_CLIENT_S_REGISTERING:
226 case AVAHI_CLIENT_CONNECTING:
231 /************************************************************************
233 ************************************************************************/
236 * Tries to setup the Zeroconf thread and any
237 * neccessary config setting.
239 void av_zeroconf_setup(const AFPConfig *configs) {
242 /* initialize the struct that holds our config settings. */
244 ctx = calloc(1, sizeof(struct context));
245 ctx->configs = configs;
249 /* first of all we need to initialize our threading env */
250 if (!(ctx->threaded_poll = avahi_threaded_poll_new())) {
254 /* now we need to acquire a client */
255 if (!(ctx->client = avahi_client_new(avahi_threaded_poll_get(ctx->threaded_poll),
256 AVAHI_CLIENT_NO_FAIL,
260 LOG(log_error, logtype_afpd, "Failed to create client object: %s",
261 avahi_strerror(avahi_client_errno(ctx->client)));
269 av_zeroconf_unregister();
275 * This function finally runs the loop impl.
277 int av_zeroconf_run(void) {
280 /* Finally, start the event loop thread */
281 if (avahi_threaded_poll_start(ctx->threaded_poll) < 0) {
282 LOG(log_error, logtype_afpd, "Failed to create thread: %s",
283 avahi_strerror(avahi_client_errno(ctx->client)));
286 LOG(log_info, logtype_afpd, "Successfully started avahi loop.");
289 ctx->thread_running = 1;
294 av_zeroconf_unregister();
300 * Tries to shutdown this loop impl.
301 * Call this function from outside this thread.
303 void av_zeroconf_shutdown() {
304 /* Call this when the app shuts down */
305 avahi_threaded_poll_stop(ctx->threaded_poll);
306 avahi_client_free(ctx->client);
307 avahi_threaded_poll_free(ctx->threaded_poll);
311 * Tries to shutdown this loop impl.
312 * Call this function from inside this thread.
314 int av_zeroconf_unregister() {
315 if (ctx->thread_running) {
316 /* First, block the event loop */
317 avahi_threaded_poll_lock(ctx->threaded_poll);
319 /* Than, do your stuff */
320 avahi_threaded_poll_quit(ctx->threaded_poll);
322 /* Finally, unblock the event loop */
323 avahi_threaded_poll_unlock(ctx->threaded_poll);
324 ctx->thread_running = 0;
328 avahi_client_free(ctx->client);
330 if (ctx->threaded_poll)
331 avahi_threaded_poll_free(ctx->threaded_poll);
338 #endif /* USE_AVAHI */