2 * Author: Daniel S. Haischt <me@daniel.stefan.haischt.name>
3 * Purpose: Avahi based Zeroconf support
4 * Docs: http://avahi.org/download/doxygen/
16 #include <avahi-common/strlst.h>
18 #include <atalk/logger.h>
19 #include <atalk/util.h>
20 #include <atalk/dsi.h>
21 #include <atalk/unicode.h>
23 #include "afp_avahi.h"
24 #include "afp_config.h"
27 /*****************************************************************
29 *****************************************************************/
30 struct context *ctx = NULL;
32 /*****************************************************************
34 *****************************************************************/
36 static void publish_reply(AvahiEntryGroup *g,
37 AvahiEntryGroupState state,
41 * This function tries to register the AFP DNS
44 static void register_stuff(void) {
46 const AFPConfig *config;
47 const struct vol *volume;
50 AvahiStringList *strlist = NULL;
56 if (!(ctx->group = avahi_entry_group_new(ctx->client, publish_reply, ctx))) {
57 LOG(log_error, logtype_afpd, "Failed to create entry group: %s",
58 avahi_strerror(avahi_client_errno(ctx->client)));
63 if (avahi_entry_group_is_empty(ctx->group)) {
64 /* Register our service */
66 /* Build AFP volumes list */
68 strlist = avahi_string_list_add_printf(strlist, "sys=waMa=0,adVF=0x100");
70 for (volume = getvolumes(); volume; volume = volume->v_next) {
72 if (convert_string(CH_UCS2, CH_UTF8_MAC, volume->v_name, -1, tmpname, 255) <= 0)
75 if (volume->v_flags & AFPVOL_TM) {
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);
82 LOG(log_warning, logtype_afpd, "Registering volume '%s' for TimeMachine. But UUID is invalid.",
84 strlist = avahi_string_list_add_printf(strlist, "dk%u=adVN=%s,adVF=0xa1",
91 for (config = ctx->configs; config; config = config->next) {
93 dsi = (DSI *)config->obj.handle;
94 name = config->obj.options.server ?
95 config->obj.options.server : config->obj.options.hostname;
96 port = getip_port((struct sockaddr *)&dsi->server);
98 if (avahi_entry_group_add_service(ctx->group,
103 AFP_DNS_SERVICE_TYPE,
108 LOG(log_error, logtype_afpd, "Failed to add service: %s",
109 avahi_strerror(avahi_client_errno(ctx->client)));
113 if (i && avahi_entry_group_add_service_strlst(ctx->group,
123 LOG(log_error, logtype_afpd, "Failed to add service: %s",
124 avahi_strerror(avahi_client_errno(ctx->client)));
129 if (avahi_entry_group_commit(ctx->group) < 0) {
130 LOG(log_error, logtype_afpd, "Failed to commit entry group: %s",
131 avahi_strerror(avahi_client_errno(ctx->client)));
135 } /* if avahi_entry_group_is_empty*/
140 avahi_client_free (ctx->client);
141 avahi_threaded_poll_quit(ctx->threaded_poll);
144 /* Called when publishing of service data completes */
145 static void publish_reply(AvahiEntryGroup *g,
146 AvahiEntryGroupState state,
147 AVAHI_GCC_UNUSED void *userdata)
149 assert(ctx->group == NULL || g == ctx->group);
153 case AVAHI_ENTRY_GROUP_ESTABLISHED :
154 /* The entry group has been established successfully */
155 LOG(log_debug, logtype_afpd, "publish_reply: AVAHI_ENTRY_GROUP_ESTABLISHED");
158 case AVAHI_ENTRY_GROUP_COLLISION:
159 /* With multiple names there's no way to know which one collided */
160 LOG(log_error, logtype_afpd, "publish_reply: AVAHI_ENTRY_GROUP_COLLISION",
161 avahi_strerror(avahi_client_errno(ctx->client)));
162 avahi_client_free(avahi_entry_group_get_client(g));
163 avahi_threaded_poll_quit(ctx->threaded_poll);
166 case AVAHI_ENTRY_GROUP_FAILURE:
167 LOG(log_error, logtype_afpd, "Failed to register service: %s",
168 avahi_strerror(avahi_client_errno(ctx->client)));
169 avahi_client_free(avahi_entry_group_get_client(g));
170 avahi_threaded_poll_quit(ctx->threaded_poll);
173 case AVAHI_ENTRY_GROUP_UNCOMMITED:
175 case AVAHI_ENTRY_GROUP_REGISTERING:
180 static void client_callback(AvahiClient *client,
181 AvahiClientState state,
184 ctx->client = client;
187 case AVAHI_CLIENT_S_RUNNING:
188 /* The server has startup successfully and registered its host
189 * name on the network, so it's time to create our services */
194 case AVAHI_CLIENT_S_COLLISION:
196 avahi_entry_group_reset(ctx->group);
199 case AVAHI_CLIENT_FAILURE: {
200 if (avahi_client_errno(client) == AVAHI_ERR_DISCONNECTED) {
203 avahi_client_free(ctx->client);
207 /* Reconnect to the server */
208 if (!(ctx->client = avahi_client_new(avahi_threaded_poll_get(ctx->threaded_poll),
209 AVAHI_CLIENT_NO_FAIL,
214 LOG(log_error, logtype_afpd, "Failed to contact server: %s",
215 avahi_strerror(error));
217 avahi_client_free (ctx->client);
218 avahi_threaded_poll_quit(ctx->threaded_poll);
222 LOG(log_error, logtype_afpd, "Client failure: %s",
223 avahi_strerror(avahi_client_errno(client)));
224 avahi_client_free (ctx->client);
225 avahi_threaded_poll_quit(ctx->threaded_poll);
230 case AVAHI_CLIENT_S_REGISTERING:
232 case AVAHI_CLIENT_CONNECTING:
237 /************************************************************************
239 ************************************************************************/
242 * Tries to setup the Zeroconf thread and any
243 * neccessary config setting.
245 void av_zeroconf_setup(const AFPConfig *configs) {
248 /* initialize the struct that holds our config settings. */
250 LOG(log_debug, logtype_afpd, "Resetting zeroconf records");
251 avahi_entry_group_reset(ctx->group);
253 ctx = calloc(1, sizeof(struct context));
254 ctx->configs = configs;
258 /* first of all we need to initialize our threading env */
259 if (!(ctx->threaded_poll = avahi_threaded_poll_new())) {
263 /* now we need to acquire a client */
264 if (!(ctx->client = avahi_client_new(avahi_threaded_poll_get(ctx->threaded_poll),
265 AVAHI_CLIENT_NO_FAIL,
269 LOG(log_error, logtype_afpd, "Failed to create client object: %s",
270 avahi_strerror(avahi_client_errno(ctx->client)));
278 av_zeroconf_unregister();
284 * This function finally runs the loop impl.
286 int av_zeroconf_run(void) {
289 /* Finally, start the event loop thread */
290 if (avahi_threaded_poll_start(ctx->threaded_poll) < 0) {
291 LOG(log_error, logtype_afpd, "Failed to create thread: %s",
292 avahi_strerror(avahi_client_errno(ctx->client)));
295 LOG(log_info, logtype_afpd, "Successfully started avahi loop.");
298 ctx->thread_running = 1;
303 av_zeroconf_unregister();
309 * Tries to shutdown this loop impl.
310 * Call this function from outside this thread.
312 void av_zeroconf_shutdown() {
313 /* Call this when the app shuts down */
314 avahi_threaded_poll_stop(ctx->threaded_poll);
315 avahi_client_free(ctx->client);
316 avahi_threaded_poll_free(ctx->threaded_poll);
322 * Tries to shutdown this loop impl.
323 * Call this function from inside this thread.
325 int av_zeroconf_unregister() {
326 if (ctx->thread_running) {
327 /* First, block the event loop */
328 avahi_threaded_poll_lock(ctx->threaded_poll);
330 /* Than, do your stuff */
331 avahi_threaded_poll_quit(ctx->threaded_poll);
333 /* Finally, unblock the event loop */
334 avahi_threaded_poll_unlock(ctx->threaded_poll);
335 ctx->thread_running = 0;
339 avahi_client_free(ctx->client);
341 if (ctx->threaded_poll)
342 avahi_threaded_poll_free(ctx->threaded_poll);
350 #endif /* USE_AVAHI */