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;
49 char name[MAXINSTANCENAMELEN+1];
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) {
73 LOG ( log_error, logtype_afpd, "Could not set Zeroconf volume name for TimeMachine");
77 if (volume->v_flags & AFPVOL_TM) {
79 LOG(log_info, logtype_afpd, "Registering volume '%s' with UUID: '%s' for TimeMachine",
80 volume->v_localname, volume->v_uuid);
81 strlist = avahi_string_list_add_printf(strlist, "dk%u=adVN=%s,adVF=0xa1,adVU=%s",
82 i++, tmpname, volume->v_uuid);
84 LOG(log_warning, logtype_afpd, "Registering volume '%s' for TimeMachine. But UUID is invalid.",
86 strlist = avahi_string_list_add_printf(strlist, "dk%u=adVN=%s,adVF=0xa1",
93 for (config = ctx->configs; config; config = config->next) {
95 dsi = (DSI *)config->obj.handle;
96 port = getip_port((struct sockaddr *)&dsi->server);
98 if (convert_string(config->obj.options.unixcharset,
100 config->obj.options.server ?
101 config->obj.options.server :
102 config->obj.options.hostname,
105 MAXINSTANCENAMELEN) <= 0) {
106 LOG(log_error, logtype_afpd, "Could not set Zeroconf instance name");
109 if ((dsi->bonjourname = strdup(name)) == NULL) {
110 LOG(log_error, logtype_afpd, "Could not set Zeroconf instance name");
114 LOG(log_info, logtype_afpd, "Registering server '%s' with with Bonjour",
117 if (avahi_entry_group_add_service(ctx->group,
122 AFP_DNS_SERVICE_TYPE,
127 LOG(log_error, logtype_afpd, "Failed to add service: %s",
128 avahi_strerror(avahi_client_errno(ctx->client)));
132 if (i && avahi_entry_group_add_service_strlst(ctx->group,
142 LOG(log_error, logtype_afpd, "Failed to add service: %s",
143 avahi_strerror(avahi_client_errno(ctx->client)));
148 if (avahi_entry_group_commit(ctx->group) < 0) {
149 LOG(log_error, logtype_afpd, "Failed to commit entry group: %s",
150 avahi_strerror(avahi_client_errno(ctx->client)));
154 } /* if avahi_entry_group_is_empty*/
159 avahi_client_free (ctx->client);
160 avahi_threaded_poll_quit(ctx->threaded_poll);
163 /* Called when publishing of service data completes */
164 static void publish_reply(AvahiEntryGroup *g,
165 AvahiEntryGroupState state,
166 AVAHI_GCC_UNUSED void *userdata)
168 assert(ctx->group == NULL || g == ctx->group);
172 case AVAHI_ENTRY_GROUP_ESTABLISHED :
173 /* The entry group has been established successfully */
174 LOG(log_debug, logtype_afpd, "publish_reply: AVAHI_ENTRY_GROUP_ESTABLISHED");
177 case AVAHI_ENTRY_GROUP_COLLISION:
178 /* With multiple names there's no way to know which one collided */
179 LOG(log_error, logtype_afpd, "publish_reply: AVAHI_ENTRY_GROUP_COLLISION",
180 avahi_strerror(avahi_client_errno(ctx->client)));
181 avahi_client_free(avahi_entry_group_get_client(g));
182 avahi_threaded_poll_quit(ctx->threaded_poll);
185 case AVAHI_ENTRY_GROUP_FAILURE:
186 LOG(log_error, logtype_afpd, "Failed to register service: %s",
187 avahi_strerror(avahi_client_errno(ctx->client)));
188 avahi_client_free(avahi_entry_group_get_client(g));
189 avahi_threaded_poll_quit(ctx->threaded_poll);
192 case AVAHI_ENTRY_GROUP_UNCOMMITED:
194 case AVAHI_ENTRY_GROUP_REGISTERING:
199 static void client_callback(AvahiClient *client,
200 AvahiClientState state,
203 ctx->client = client;
206 case AVAHI_CLIENT_S_RUNNING:
207 /* The server has startup successfully and registered its host
208 * name on the network, so it's time to create our services */
213 case AVAHI_CLIENT_S_COLLISION:
215 avahi_entry_group_reset(ctx->group);
218 case AVAHI_CLIENT_FAILURE: {
219 if (avahi_client_errno(client) == AVAHI_ERR_DISCONNECTED) {
222 avahi_client_free(ctx->client);
226 /* Reconnect to the server */
227 if (!(ctx->client = avahi_client_new(avahi_threaded_poll_get(ctx->threaded_poll),
228 AVAHI_CLIENT_NO_FAIL,
233 LOG(log_error, logtype_afpd, "Failed to contact server: %s",
234 avahi_strerror(error));
236 avahi_client_free (ctx->client);
237 avahi_threaded_poll_quit(ctx->threaded_poll);
241 LOG(log_error, logtype_afpd, "Client failure: %s",
242 avahi_strerror(avahi_client_errno(client)));
243 avahi_client_free (ctx->client);
244 avahi_threaded_poll_quit(ctx->threaded_poll);
249 case AVAHI_CLIENT_S_REGISTERING:
251 case AVAHI_CLIENT_CONNECTING:
256 /************************************************************************
258 ************************************************************************/
261 * Tries to setup the Zeroconf thread and any
262 * neccessary config setting.
264 void av_zeroconf_setup(const AFPConfig *configs) {
267 /* initialize the struct that holds our config settings. */
269 LOG(log_debug, logtype_afpd, "Resetting zeroconf records");
270 avahi_entry_group_reset(ctx->group);
272 ctx = calloc(1, sizeof(struct context));
273 ctx->configs = configs;
277 /* first of all we need to initialize our threading env */
278 if (!(ctx->threaded_poll = avahi_threaded_poll_new())) {
282 /* now we need to acquire a client */
283 if (!(ctx->client = avahi_client_new(avahi_threaded_poll_get(ctx->threaded_poll),
284 AVAHI_CLIENT_NO_FAIL,
288 LOG(log_error, logtype_afpd, "Failed to create client object: %s",
289 avahi_strerror(avahi_client_errno(ctx->client)));
297 av_zeroconf_unregister();
303 * This function finally runs the loop impl.
305 int av_zeroconf_run(void) {
306 /* Finally, start the event loop thread */
307 if (avahi_threaded_poll_start(ctx->threaded_poll) < 0) {
308 LOG(log_error, logtype_afpd, "Failed to create thread: %s",
309 avahi_strerror(avahi_client_errno(ctx->client)));
312 LOG(log_info, logtype_afpd, "Successfully started avahi loop.");
315 ctx->thread_running = 1;
320 av_zeroconf_unregister();
326 * Tries to shutdown this loop impl.
327 * Call this function from outside this thread.
329 void av_zeroconf_shutdown() {
330 /* Call this when the app shuts down */
331 avahi_threaded_poll_stop(ctx->threaded_poll);
332 avahi_client_free(ctx->client);
333 avahi_threaded_poll_free(ctx->threaded_poll);
339 * Tries to shutdown this loop impl.
340 * Call this function from inside this thread.
342 int av_zeroconf_unregister() {
343 if (ctx->thread_running) {
344 /* First, block the event loop */
345 avahi_threaded_poll_lock(ctx->threaded_poll);
347 /* Than, do your stuff */
348 avahi_threaded_poll_quit(ctx->threaded_poll);
350 /* Finally, unblock the event loop */
351 avahi_threaded_poll_unlock(ctx->threaded_poll);
352 ctx->thread_running = 0;
356 avahi_client_free(ctx->client);
358 if (ctx->threaded_poll)
359 avahi_threaded_poll_free(ctx->threaded_poll);
367 #endif /* USE_AVAHI */