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, i++) {
72 if (convert_string(CH_UCS2, CH_UTF8_MAC, volume->v_name, -1, tmpname, 255) <= 0)
74 LOG(log_debug, logtype_afpd, "Adding volume '%s'", volume->v_localname);
75 strlist = avahi_string_list_add_printf(strlist, "dk%u=adVN=%s,adVF=0x01", i, tmpname);
79 for (config = ctx->configs; config; config = config->next) {
81 dsi = (DSI *)config->obj.handle;
82 name = config->obj.options.server ?
83 config->obj.options.server : config->obj.options.hostname;
84 port = getip_port((struct sockaddr *)&dsi->server);
86 if (avahi_entry_group_add_service(ctx->group,
96 LOG(log_error, logtype_afpd, "Failed to add service: %s",
97 avahi_strerror(avahi_client_errno(ctx->client)));
101 if (avahi_entry_group_add_service_strlst(ctx->group,
111 LOG(log_error, logtype_afpd, "Failed to add service: %s",
112 avahi_strerror(avahi_client_errno(ctx->client)));
117 if (avahi_entry_group_commit(ctx->group) < 0) {
118 LOG(log_error, logtype_afpd, "Failed to commit entry group: %s",
119 avahi_strerror(avahi_client_errno(ctx->client)));
123 } /* if avahi_entry_group_is_empty*/
128 avahi_client_free (ctx->client);
129 avahi_threaded_poll_quit(ctx->threaded_poll);
132 /* Called when publishing of service data completes */
133 static void publish_reply(AvahiEntryGroup *g,
134 AvahiEntryGroupState state,
135 AVAHI_GCC_UNUSED void *userdata)
137 assert(ctx->group == NULL || g == ctx->group);
141 case AVAHI_ENTRY_GROUP_ESTABLISHED :
142 /* The entry group has been established successfully */
143 LOG(log_debug, logtype_afpd, "publish_reply: AVAHI_ENTRY_GROUP_ESTABLISHED");
146 case AVAHI_ENTRY_GROUP_COLLISION:
147 /* With multiple names there's no way to know which one collided */
148 LOG(log_error, logtype_afpd, "publish_reply: AVAHI_ENTRY_GROUP_COLLISION",
149 avahi_strerror(avahi_client_errno(ctx->client)));
150 avahi_client_free(avahi_entry_group_get_client(g));
151 avahi_threaded_poll_quit(ctx->threaded_poll);
154 case AVAHI_ENTRY_GROUP_FAILURE:
155 LOG(log_error, logtype_afpd, "Failed to register service: %s",
156 avahi_strerror(avahi_client_errno(ctx->client)));
157 avahi_client_free(avahi_entry_group_get_client(g));
158 avahi_threaded_poll_quit(ctx->threaded_poll);
161 case AVAHI_ENTRY_GROUP_UNCOMMITED:
163 case AVAHI_ENTRY_GROUP_REGISTERING:
168 static void client_callback(AvahiClient *client,
169 AvahiClientState state,
172 ctx->client = client;
175 case AVAHI_CLIENT_S_RUNNING:
176 /* The server has startup successfully and registered its host
177 * name on the network, so it's time to create our services */
182 case AVAHI_CLIENT_S_COLLISION:
184 avahi_entry_group_reset(ctx->group);
187 case AVAHI_CLIENT_FAILURE: {
188 if (avahi_client_errno(client) == AVAHI_ERR_DISCONNECTED) {
191 avahi_client_free(ctx->client);
195 /* Reconnect to the server */
196 if (!(ctx->client = avahi_client_new(avahi_threaded_poll_get(ctx->threaded_poll),
197 AVAHI_CLIENT_NO_FAIL,
202 LOG(log_error, logtype_afpd, "Failed to contact server: %s",
203 avahi_strerror(error));
205 avahi_client_free (ctx->client);
206 avahi_threaded_poll_quit(ctx->threaded_poll);
210 LOG(log_error, logtype_afpd, "Client failure: %s",
211 avahi_strerror(avahi_client_errno(client)));
212 avahi_client_free (ctx->client);
213 avahi_threaded_poll_quit(ctx->threaded_poll);
218 case AVAHI_CLIENT_S_REGISTERING:
220 case AVAHI_CLIENT_CONNECTING:
225 /************************************************************************
227 ************************************************************************/
230 * Tries to setup the Zeroconf thread and any
231 * neccessary config setting.
233 void av_zeroconf_setup(const AFPConfig *configs) {
236 /* initialize the struct that holds our config settings. */
238 ctx = calloc(1, sizeof(struct context));
239 ctx->configs = configs;
243 /* first of all we need to initialize our threading env */
244 if (!(ctx->threaded_poll = avahi_threaded_poll_new())) {
248 /* now we need to acquire a client */
249 if (!(ctx->client = avahi_client_new(avahi_threaded_poll_get(ctx->threaded_poll),
250 AVAHI_CLIENT_NO_FAIL,
254 LOG(log_error, logtype_afpd, "Failed to create client object: %s",
255 avahi_strerror(avahi_client_errno(ctx->client)));
263 av_zeroconf_unregister();
269 * This function finally runs the loop impl.
271 int av_zeroconf_run(void) {
274 /* Finally, start the event loop thread */
275 if (avahi_threaded_poll_start(ctx->threaded_poll) < 0) {
276 LOG(log_error, logtype_afpd, "Failed to create thread: %s",
277 avahi_strerror(avahi_client_errno(ctx->client)));
280 LOG(log_info, logtype_afpd, "Successfully started avahi loop.");
283 ctx->thread_running = 1;
288 av_zeroconf_unregister();
294 * Tries to shutdown this loop impl.
295 * Call this function from outside this thread.
297 void av_zeroconf_shutdown() {
298 /* Call this when the app shuts down */
299 avahi_threaded_poll_stop(ctx->threaded_poll);
300 avahi_client_free(ctx->client);
301 avahi_threaded_poll_free(ctx->threaded_poll);
305 * Tries to shutdown this loop impl.
306 * Call this function from inside this thread.
308 int av_zeroconf_unregister() {
309 if (ctx->thread_running) {
310 /* First, block the event loop */
311 avahi_threaded_poll_lock(ctx->threaded_poll);
313 /* Than, do your stuff */
314 avahi_threaded_poll_quit(ctx->threaded_poll);
316 /* Finally, unblock the event loop */
317 avahi_threaded_poll_unlock(ctx->threaded_poll);
318 ctx->thread_running = 0;
322 avahi_client_free(ctx->client);
324 if (ctx->threaded_poll)
325 avahi_threaded_poll_free(ctx->threaded_poll);
332 #endif /* USE_AVAHI */