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;
51 AvahiStringList *strlist2 = 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) {
74 LOG ( log_error, logtype_afpd, "Could not set Zeroconf volume name for TimeMachine");
78 if (volume->v_flags & AFPVOL_TM) {
80 LOG(log_info, logtype_afpd, "Registering volume '%s' with UUID: '%s' for TimeMachine",
81 volume->v_localname, volume->v_uuid);
82 strlist = avahi_string_list_add_printf(strlist, "dk%u=adVN=%s,adVF=0xa1,adVU=%s",
83 i++, tmpname, volume->v_uuid);
85 LOG(log_warning, logtype_afpd, "Registering volume '%s' for TimeMachine. But UUID is invalid.",
87 strlist = avahi_string_list_add_printf(strlist, "dk%u=adVN=%s,adVF=0xa1",
94 for (config = ctx->configs; config; config = config->next) {
96 dsi = (DSI *)config->obj.handle;
97 port = getip_port((struct sockaddr *)&dsi->server);
99 if (convert_string(config->obj.options.unixcharset,
101 config->obj.options.server ?
102 config->obj.options.server :
103 config->obj.options.hostname,
106 MAXINSTANCENAMELEN) <= 0) {
107 LOG(log_error, logtype_afpd, "Could not set Zeroconf instance name");
110 if ((dsi->bonjourname = strdup(name)) == NULL) {
111 LOG(log_error, logtype_afpd, "Could not set Zeroconf instance name");
115 LOG(log_info, logtype_afpd, "Registering server '%s' with Bonjour",
118 if (avahi_entry_group_add_service(ctx->group,
123 AFP_DNS_SERVICE_TYPE,
128 LOG(log_error, logtype_afpd, "Failed to add service: %s",
129 avahi_strerror(avahi_client_errno(ctx->client)));
133 if (i && avahi_entry_group_add_service_strlst(ctx->group,
143 LOG(log_error, logtype_afpd, "Failed to add service: %s",
144 avahi_strerror(avahi_client_errno(ctx->client)));
148 if (config->obj.options.mimicmodel) {
149 strlist2 = avahi_string_list_add_printf(strlist2, "model=%s", config->obj.options.mimicmodel);
150 if (avahi_entry_group_add_service_strlst(ctx->group,
155 DEV_INFO_SERVICE_TYPE,
160 LOG(log_error, logtype_afpd, "Failed to add service: %s",
161 avahi_strerror(avahi_client_errno(ctx->client)));
164 } /* if (config->obj.options.mimicmodel) */
168 if (avahi_entry_group_commit(ctx->group) < 0) {
169 LOG(log_error, logtype_afpd, "Failed to commit entry group: %s",
170 avahi_strerror(avahi_client_errno(ctx->client)));
174 } /* if avahi_entry_group_is_empty*/
180 // avahi_threaded_poll_quit(ctx->threaded_poll);
183 /* Called when publishing of service data completes */
184 static void publish_reply(AvahiEntryGroup *g,
185 AvahiEntryGroupState state,
186 AVAHI_GCC_UNUSED void *userdata)
188 assert(ctx->group == NULL || g == ctx->group);
192 case AVAHI_ENTRY_GROUP_ESTABLISHED :
193 /* The entry group has been established successfully */
194 LOG(log_debug, logtype_afpd, "publish_reply: AVAHI_ENTRY_GROUP_ESTABLISHED");
197 case AVAHI_ENTRY_GROUP_COLLISION:
198 /* With multiple names there's no way to know which one collided */
199 LOG(log_error, logtype_afpd, "publish_reply: AVAHI_ENTRY_GROUP_COLLISION",
200 avahi_strerror(avahi_client_errno(ctx->client)));
201 avahi_threaded_poll_quit(ctx->threaded_poll);
204 case AVAHI_ENTRY_GROUP_FAILURE:
205 LOG(log_error, logtype_afpd, "Failed to register service: %s",
206 avahi_strerror(avahi_client_errno(ctx->client)));
207 avahi_threaded_poll_quit(ctx->threaded_poll);
210 case AVAHI_ENTRY_GROUP_UNCOMMITED:
212 case AVAHI_ENTRY_GROUP_REGISTERING:
217 static void client_callback(AvahiClient *client,
218 AvahiClientState state,
221 ctx->client = client;
224 case AVAHI_CLIENT_S_RUNNING:
225 /* The server has startup successfully and registered its host
226 * name on the network, so it's time to create our services */
231 case AVAHI_CLIENT_S_COLLISION:
233 avahi_entry_group_reset(ctx->group);
236 case AVAHI_CLIENT_FAILURE: {
237 if (avahi_client_errno(client) == AVAHI_ERR_DISCONNECTED) {
240 avahi_client_free(ctx->client);
244 /* Reconnect to the server */
245 if (!(ctx->client = avahi_client_new(avahi_threaded_poll_get(ctx->threaded_poll),
246 AVAHI_CLIENT_NO_FAIL,
251 LOG(log_error, logtype_afpd, "Failed to contact server: %s",
252 avahi_strerror(error));
254 avahi_threaded_poll_quit(ctx->threaded_poll);
258 LOG(log_error, logtype_afpd, "Client failure: %s",
259 avahi_strerror(avahi_client_errno(client)));
260 avahi_threaded_poll_quit(ctx->threaded_poll);
265 case AVAHI_CLIENT_S_REGISTERING:
267 case AVAHI_CLIENT_CONNECTING:
272 /************************************************************************
274 ************************************************************************/
277 * Tries to setup the Zeroconf thread and any
278 * neccessary config setting.
280 void av_zeroconf_register(const AFPConfig *configs) {
283 /* initialize the struct that holds our config settings. */
285 LOG(log_debug, logtype_afpd, "Resetting zeroconf records");
286 avahi_entry_group_reset(ctx->group);
288 ctx = calloc(1, sizeof(struct context));
289 ctx->configs = configs;
293 /* first of all we need to initialize our threading env */
294 if (!(ctx->threaded_poll = avahi_threaded_poll_new())) {
298 /* now we need to acquire a client */
299 if (!(ctx->client = avahi_client_new(avahi_threaded_poll_get(ctx->threaded_poll),
300 AVAHI_CLIENT_NO_FAIL,
304 LOG(log_error, logtype_afpd, "Failed to create client object: %s",
305 avahi_strerror(error));
309 if (avahi_threaded_poll_start(ctx->threaded_poll) < 0) {
310 LOG(log_error, logtype_afpd, "Failed to create thread: %s",
311 avahi_strerror(avahi_client_errno(ctx->client)));
314 LOG(log_info, logtype_afpd, "Successfully started avahi loop.");
317 ctx->thread_running = 1;
321 av_zeroconf_unregister();
327 * Tries to shutdown this loop impl.
328 * Call this function from inside this thread.
330 int av_zeroconf_unregister() {
331 LOG(log_error, logtype_afpd, "av_zeroconf_unregister");
334 LOG(log_error, logtype_afpd, "av_zeroconf_unregister: avahi_threaded_poll_stop");
335 if (ctx->threaded_poll)
336 avahi_threaded_poll_stop(ctx->threaded_poll);
337 LOG(log_error, logtype_afpd, "av_zeroconf_unregister: avahi_client_free");
339 avahi_client_free(ctx->client);
340 LOG(log_error, logtype_afpd, "av_zeroconf_unregister: avahi_threaded_poll_free");
341 if (ctx->threaded_poll)
342 avahi_threaded_poll_free(ctx->threaded_poll);
349 #endif /* USE_AVAHI */